Question

*pSpiTxBuf++ = CC2520_INS_SFLUSHRX; // get rid of this packet
*pSpiTxBuf-- = CC2520_INS_SFLUSHRX; // double flush [CC2520 Bug#1]

Can anyone explain to me what the above two lines are attempting to do? pSpiTxBuf is a uint8_t *

Was it helpful?

Solution

This is equivalent to:

pSpiTxBuf[0] = CC2520_INS_SFLUSHRX; // get rid of this packet
pSpiTxBuf[1] = CC2520_INS_SFLUSHRX; // double flush [CC2520 Bug#1]

(which would probably have been a clearer way to write the code in the first place).

i.e. it just sets two adjacent register values to CC2520_INS_SFLUSHRX.

OTHER TIPS

The increment and decrement operators change the address in the pointer before dereference. So you advance in the buffer, and than change the value in the newly pointed cell.

Putting some brackets in to make it clearer:

*(pSpiTxBuf++) = CC2520_INS_SFLUSHRX;

Ignoring the ++, it's a standard pointer deference and an assignment of the form *p = q. But p happens to have a post-increment operator in it, which means, use the value as normal, but after you've used it, increment it.

The first one is assigning the value CC2520_INS_SFLUSHRX to *pSpiTxBuf, i.e. to the position pointed to by pSpiTxBuf. It is then incrementing pSpiTxBuf so that it now points to the next position.

The second one is similar, but decrementing the pointer afterwards, so it now points to the previous position.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top