I am using a Kinetis K70, KDS 3.0.0, and MQX Lite.
I want to use srand() and rand()
I have noticed that if I call srand or rand within a MQX lite task, I get a hard fault.
However, if I call srand in the main.c, prior to PEX_RTOS_START, rand will work fine in MQX tasks.
If there a MQX setting or something I need to do. I would rather not modify main.c since it was generated.
I was able to reproduce the problem with a fresh project with MQX lite added.
This code produces a hard fault at the call of srand
void Task1_task(uint32_t task_init_data)
{
int counter = 0;
srand(50);
while(1)
{
counter = rand();
}
}
But adding srand here, makes it start working..
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
/* Write your local variable definition here */
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
PE_low_level_init();
/*** End of Processor Expert internal initialization. ***/
/* Write your code here */
/* For example: for(;;) { } */
srand(10);
/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of RTOS startup code. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;){}
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
Why?
Hi,
I also have exactly same issue. But in my side , i am using FREERTOS. Any solution found ?
Hi Daniel,
Thanks for the help, but that didn't seem to work.
I ave a PE/MQX lite project with nothing but 1 task calling srand and rand.
If I don't call Srand in main.c, it will crash the first time it is called in the task in mqx_tasks.c
I upped the stack to 4000 (for a task that does nothing, and it still crashes).
I also tried making it a floating point task, and turned on the FP context switch, but that didn't help either.
Any other ideas?
Thanks,
Tim
Hi Tim:
There seems have an issue in the srand function in KDS library, what causes the hard fault. It happens only with MQX Lite.
The workaround is just like you mentioned, calling srand in main function instead of MQX-Lite task.
Thank you very much for your feedback.
Regards
Daniel
Hi Robbins:
KDS is using GNU library for srand() and rand(). Have you tried to increase the task stack to see if it works?
Regards
Daniel