teensy 3.5 reset/crash when comparing unsigned char with int??

Status
Not open for further replies.

nemail

Member
Hi

i'm doing this, using a modified version of the RF24Network lib by tmrh20:

...
// Read the beginning of the frame as the header
RF24NetworkHeader *header = (RF24NetworkHeader*)(&frame_buffer);
...
if ((header->type != 128 && header->type != 129 && header->type != 194 && header->type != 195))

when execution hits the if statement, the teensy crashes and resets. i've put serial outputs before and after the statements to verify the exact crash location.

This works well on arduino (i am in the process of porting a software from arduino mega to teensy).

is there anything obvious? strangely if the conditions do not apply, code execution continues and everything works. so only if header type does contain something different than 128, 129, 194 or 195, the teensy crashes.

any ideas? header.type is unsigned char but i also tried to declare it as uint8_t. didn't change anything.

thanks!
 
The crash wont be because you're comparing different data types (although ideally you should cast those ints to unsigned char for the test), the crash will be because you are dereferencing header, which probably is an invalid address, or a peripheral that hasn't had its clock switched on.

Where is frame_buffer located?
 
That makes sense, thanks! There is even a pull request in the github repo of the mesh lib which changes a similar piece of code to memcopy: https://github.com/TMRh20/RF24Mesh/pull/113

frame_buffer is an uint8_t array of size 32 and is filled right before the dereferencing:

// Dump the payloads until we've gotten everything
// Fetch the payload, and see if this was the last one.
radio.read(frame_buffer, frame_size);
 
In cases like this it really helps to see the definition of the variable.

For example:
Code:
RF24NetworkHeader *header = (RF24NetworkHeader*)(&frame_buffer);
Is Frame buffer defined like: uint8_t frame_buffer[32] or like uint8_t *frame_buffer ?

If so your header may be set to the address to the pointer...
So what you may need to do is instead:

Code:
RF24NetworkHeader *header = (RF24NetworkHeader*)(frame_buffer);
 
Status
Not open for further replies.
Back
Top