0

I try to use CMSIS-RTOS virtual timer to periodically call a function that send "hello world!" to PC by using serial port. I can correct pass a word to the function, but failed to pass a pointer! I have no idea what's the problem. Maybe the limitation of CMSIS-RTOS?

"H" is sent back to PC by the comment out parts of code, which is what I wanted. However, now in this code, I try to pass the pointer of the array to the callback function, a "P" is sent back to PC. Why?? Is my code wrong??

void callback(void const *param);
osTimerDef(timer_handle, callback);
void callback(void const *param){
    uint8_t *t=(uint8_t *)param;
    SER_Send(t, 1);
}

//void callback(void const *param){
//  uint8_t t = (uint8_t)param;
//  SER_Send(&t, 1);
//}

int main (void) {
    uint8_t text[]="Hello world!";
    osTimerId timer = osTimerCreate(osTimer(timer_handle), osTimerPeriodic,   (void *)text);
//  osTimerId timer = osTimerCreate(osTimer(timer_handle), osTimerPeriodic, (void *)text[0]);

SystemCoreClockUpdate(); 

osKernelInitialize ();                    // initialize CMSIS-RTOS
SER_Config(UART0);  
SER_Init(9600);

osTimerStart(timer, 500);
osKernelStart ();                         // start thread execution 

}

夏徵帆
  • 1
  • 1
  • What is the declaration of `SER_Send`? – user694733 Feb 16 '16 at 08:31
  • Looks like your main() function exits before the callback() is executed, and so text[] no longer actually exists. The commented out code just passed the first element by value, so it didn't matter, but if you pass the address of the string, the string must still be 'alive' during execution of callback(). Try defining text[] before main(). – dognotdog Feb 16 '16 at 09:32
  • Yes, you are right. It works fine when I globally define text[]. Thanks!! One thing I am not quite understand. When the commented out code passed the first element by value to the virtual timer, the virtual timer (maybe a thread??) will always hold the value?? Because, in my test, the passed value is periodically sent back to the terminal. – 夏徵帆 Feb 17 '16 at 23:47

1 Answers1

0

Looks like your main() function exits before the callback() is executed, and so text[] no longer actually exists. The commented out code just passed the first element by value, so it didn't matter, but if you pass the address of the string, the string must still be 'alive' during execution of callback(). Try defining text[] before main(). – dognotdog

Armali
  • 18,255
  • 14
  • 57
  • 171