Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Newbie looking for advice on odd behavior with pointer.

Posted by basilean on January 9, 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 { uint32t u32; uint8t u8[4]; } msg_t;

typedef struct { uint8t com; uint8t bit; volatile uint8t *port; volatile uint8t *reg; volatile uint8t *ocr; uint8t vol; } pwmio_t;

typedef struct { TaskHandlet h; volatile uint8t *tccra; uint8t tcaset; volatile uint8t *tccrb; uint8t tcbset; pwmiot pin[2]; } pwm_t;

typedef pwmt *ppwmt;

typedef struct { pwmt pwm[4]; } fumenbott;

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 uint8t FUMENBOTPWM = 2;

void vUart(void *pvParameters); static TaskHandle_t xUart0 = NULL; void vPwm(void *pvParameters);

void vApplicationStackOverflowHook(TaskHandlet xTask, signed char *pcTaskName); void vApplicationStackOverflowHook(TaskHandlet xTask, signed char *pcTaskName) { uart0_puts((const char *)pcTaskName); }

char *fumenbothandler(char *in); char *fumenbothandler(char *in) { char **out; char a = in[0]; msgt 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] >= FUMENBOTPWM) { 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 UARTBAUDRATE 19200

char uart0buffer[16]; uint8t uart0bufferidx = 0; void vUart(void pvParameters) { uart0puts("Fumenbot v0.7nr"); for(;;) { if(uart0available()) { char c = uart0getc(); if(c == 'r') { uart0puts("nr"); uart0puts(fumenbothandler((char)uart0buffer)); uart0bufferidx = 0; uint8t i; for(i=0; i<16; i++) { uart0buffer[i] = '0'; } uart0puts("nrOKnr"); } else { uart0buffer[uart0bufferidx] = c; uart0putc(c); uart0bufferidx++; } } } vTaskDelete(NULL); }

void vPwm(void *pvParameters) { ppwmt pPwm = pvPortMalloc(sizeof(pwmt)); pPwm = (ppwmt)pvParameters; *(pPwm->tccra) |= pPwm->tcaset; *(pPwm->tccrb) |= pPwm->tcbset; msgt msg; uint8t i = 0; for(i=0;i<2;i++) { *(pPwm->pin[i].reg) |= (1<pin[i].bit); *(pPwm->pin[i].port) &= ~(1<pin[i].bit); } for(;;) { xTaskNotifyWait(0x00, 0xffffffff, (uint32t *)&msg.u32, portMAXDELAY); // uart0putc('.'); if (msg.u8[2] < 1 || msg.u8[2] > 9) { *(pPwm->tccra) &= ~(1<pin[msg.u8[1]].com); *(pPwm->pin[msg.u8[1]].port) &= ~(1<pin[msg.u8[1]].bit); } else { *(pPwm->tccra) |= (1<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 ) { uart0init( UARTBAUDSELECTDOUBLESPEED(UARTBAUDRATE,FCPU) ); 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; } ~~~~

Newbie looking for advice on odd behavior with pointer.

Posted by rtel on January 13, 2016

Sorry for you post not showing up right away. It went into moderation - which its not supposed to do.

First I would suggest that you update your stack overflow hook so that it can't return - if there is a stack overflow you don't want to try and recover.

It doesn't look like the MESSAGE structure is being used by the pwm tasks, but only the uart task. Are you sure the code works ok when you are not using FreeRTOS?

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner