IWPengineering / IWP_Firmware_2.0_Beta

Rewrite of IWP Firmware for PIC24F32KA302 (3.3V version of previous micro)
0 stars 1 forks source link

Stack overflow errors in simulator #6

Closed Ken-Kok closed 8 years ago

Ken-Kok commented 8 years ago

When running in MPLAB simulator (I haven't checked on the PICKit3 debugger), sometimes I run in to a stack overflow error. It seems like this only happens near startup, and only happens during a call to the float_AverageQueueElements() function.

I think this might have to do with stack pointer depth. Average queue elements is only called during the ProcessAccelQueue function, which is only one call away from main. It would make a lot more sense to have a stack overflow if the function was called during an ISR, just because that is when our stack is deepest.

That said, the fact that it happens intermittently (only on startup) tells me it probably has to do with stack depth and interrupt routines. On startup, lots of interrupts are being called in rapid succession, just because I'm turning everything on.

Specifically, in the disassembly, the error happens on the ULNK instruction when exiting float_AverageQueueElements only when the float queue was already empty (and thus tried to return 0). In normal operation, this error should never really occur.

Ken-Kok commented 8 years ago

This error wasn't actually a stack overflow, it was an _AddressError, which I discovered by creating a handler for that specific error. I think it has to do with pointers moving around instead of moving the contents, thus pushing things into bad spaces.

It appears to happen only on a yQueue push operation when front = 0x3030 and back = 0x3031, cnt = 1, maxSize = 8, and contents is pointing to 0x3030. It starts at 0x09EA, so for some reason the contents pointer is moving - I believe this is causing the issue.

Ken-Kok commented 8 years ago

This error ended up having to do with allocation in queue functions. I fixed it by allocating a fixed 32 byte block for all queues contents, rather than trying to make a pointer to a different block during runtime.

The 32 can be reduced as low as 10 with the current code.

Fixed in da981a70267cca2c0dcddd5b8d5779b90ddd2485