basilean wrote on Saturday, January 09, 2016:
Hello community,
First, I want to thank you for this great proyect, I found it specially good to learning c, embebed and OS in just one shoot. I started coding this week, I’m not sure if my approach is the right one but besides that when I’m trying to move it forward I’m finding an error that is driving me nuts.
I’m on linux, using avr-gcc 4.8.1, an arduino duemillenova (at328p) and heap1, tacking serial example as starting point.
Going to code itself, it is working fine as expected, when I un-coment that two lines at “MESSAGE[]”, I compile and load program as before, just that task “Pwm1” stop works while “Pwm2” still does fine. I can check Pwm1 switches and runs (printing that commented chars by uart) but it does’t set pins.
I would appreciate any advice regarding this issue and whatever other you can share about my code, I’m self learning so any insight would be gladly welcome.
Thank you.
Andres.-
#include "FreeRTOS.h"
#include "task.h"
#include "uart/uart.h"
static const char *MESSAGE[] =
{
"CMD not found.",
// "PWM Not Found.",
// "PWM Updated."
};
typedef union {
uint32_t u32;
uint8_t u8[4];
} msg_t;
typedef struct {
uint8_t com;
uint8_t bit;
volatile uint8_t *port;
volatile uint8_t *reg;
volatile uint8_t *ocr;
uint8_t vol;
} pwmio_t;
typedef struct {
TaskHandle_t h;
volatile uint8_t *tccra;
uint8_t tcaset;
volatile uint8_t *tccrb;
uint8_t tcbset;
pwmio_t pin[2];
} pwm_t;
typedef pwm_t *ppwm_t;
typedef struct {
pwm_t pwm[4];
} fumenbot_t;
static fumenbot_t fumenbot = {
{
{
NULL, &TCCR0A, ((1 << WGM01) | (1 << WGM00)), &TCCR0B, (1 << CS01),
{
{COM0A1, PORTD5, &PORTD, &DDRD, &OCR0A, 255},
{COM0B1, PORTD6, &PORTD, &DDRD, &OCR0B, 255}
}
},
{
NULL, &TCCR2A, ((1 << WGM21) | (1 << WGM20)), &TCCR2B, (1 << CS21),
{
{COM2A1, PORTB3, &PORTB, &DDRB, &OCR2A, 255},
{COM2B1, PORTD3, &PORTD, &DDRD, &OCR2B, 255}
}
}
}
};
static uint8_t FUMENBOT_PWM = 2;
void vUart(void *pvParameters);
static TaskHandle_t xUart0 = NULL;
void vPwm(void *pvParameters);
void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName);
void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName) {
uart0_puts((const char *)pcTaskName);
}
char *fumenbot_handler(char *in);
char *fumenbot_handler(char *in)
{
char **out;
char a = in[0];
msg_t pkt;
pkt.u8[0] = in[1] - '0';
pkt.u8[1] = in[2] - '0';
pkt.u8[2] = in[3] - '0';
pkt.u8[3] = in[4] - '0';
if (a == 'p') {
if (pkt.u8[0] >= FUMENBOT_PWM) {
out = (char **)MESSAGE[1];
}
else {
xTaskNotify(fumenbot.pwm[pkt.u8[0]].h, pkt.u32, eSetValueWithOverwrite);
out = (char **)MESSAGE[2];
}
}
else {
out = (char **)MESSAGE[0];
}
return (char *)out;
}
#define UART_BAUD_RATE 19200
char uart0_buffer[16];
uint8_t uart0_buffer_idx = 0;
void vUart(void *pvParameters) {
uart0_puts("Fumenbot v0.7\n\r");
for(;;) {
if(uart0_available()) {
char c = uart0_getc();
if(c == '\r') {
uart0_puts("\n\r");
uart0_puts(fumenbot_handler((char*)uart0_buffer));
uart0_buffer_idx = 0;
uint8_t i;
for(i=0; i<16; i++) {
uart0_buffer[i] = '\0';
}
uart0_puts("\n\rOK\n\r");
}
else {
uart0_buffer[uart0_buffer_idx] = c;
uart0_putc(c);
uart0_buffer_idx++;
}
}
}
vTaskDelete(NULL);
}
void vPwm(void *pvParameters) {
ppwm_t pPwm = pvPortMalloc(sizeof(pwm_t));
pPwm = (ppwm_t)pvParameters;
*(pPwm->tccra) |= pPwm->tcaset;
*(pPwm->tccrb) |= pPwm->tcbset;
msg_t msg;
uint8_t i = 0;
for(i=0;i<2;i++) {
*(pPwm->pin[i].reg) |= (1<<pPwm->pin[i].bit);
*(pPwm->pin[i].port) &= ~(1<<pPwm->pin[i].bit);
}
for(;;) {
xTaskNotifyWait(0x00, 0xffffffff, (uint32_t *)&msg.u32, portMAX_DELAY);
// uart0_putc('.');
if (msg.u8[2] < 1 || msg.u8[2] > 9) {
*(pPwm->tccra) &= ~(1<<pPwm->pin[msg.u8[1]].com);
*(pPwm->pin[msg.u8[1]].port) &= ~(1<<pPwm->pin[msg.u8[1]].bit);
}
else {
*(pPwm->tccra) |= (1<<pPwm->pin[msg.u8[1]].com);
*(pPwm->pin[msg.u8[1]].ocr) = pPwm->pin[msg.u8[1]].vol / msg.u8[2];
}
// uart0_putc('-');
taskYIELD();
}
vTaskDelete(NULL);
}
int main( void )
{
uart0_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) );
xTaskCreate(vUart, "Uart0", 200, NULL, 1, &xUart0);
xTaskCreate(vPwm, "Pwm0", 100, &fumenbot.pwm[0], 2, &fumenbot.pwm[0].h);
xTaskCreate(vPwm, "Pwm1", 100, &fumenbot.pwm[1], 2, &fumenbot.pwm[1].h);
vTaskStartScheduler();
return 0;
}