LonelyWolf / stm32

STM32 stuff
The Unlicense
949 stars 496 forks source link

Issue with the way that the nrf24l01 library writes the address #18

Closed zigon15 closed 5 years ago

zigon15 commented 5 years ago

At the moment the nrf24l01 library writes the address in reverse order compared to the Arduino library which makes it incompatible unless you specify the address in reverse order.

void nRF24_SetAddr(uint8_t pipe, const uint8_t *addr) {
    uint8_t addr_width;

    // RX_ADDR_Px register
    switch (pipe) {
        case nRF24_PIPETX:
        case nRF24_PIPE0:
        case nRF24_PIPE1:
            // Get address width
            addr_width = nRF24_ReadReg(nRF24_REG_SETUP_AW) + 1;
            // Write address in reverse order (LSByte first)
            addr += addr_width;
            nRF24_CSN_L();
            nRF24_LL_RW(nRF24_CMD_W_REGISTER | nRF24_ADDR_REGS[pipe]);
            do {
                nRF24_LL_RW(*addr--);
            } while (addr_width--);
            nRF24_CSN_H();
            break;
        case nRF24_PIPE2:
        case nRF24_PIPE3:
        case nRF24_PIPE4:
        case nRF24_PIPE5:
            // Write address LSBbyte (only first byte from the addr buffer)
            nRF24_WriteReg(nRF24_ADDR_REGS[pipe], *addr);
            break;
        default:
            // Incorrect pipe number -> do nothing
            break;
    }
}

is how it is currently done which makes it incompatible with nrf24 modules connected up to an Arduino.

void nRF24_SetAddr(uint8_t pipe, const uint8_t *addr) {
    uint8_t addr_width;

    // RX_ADDR_Px register
    switch (pipe) {
        case nRF24_PIPETX:
        case nRF24_PIPE0:
        case nRF24_PIPE1:
            // Get address width
            addr_width = nRF24_ReadReg(nRF24_REG_SETUP_AW) + 1;
            nRF24_CSN_L();
            nRF24_LL_RW(nRF24_CMD_W_REGISTER | nRF24_ADDR_REGS[pipe]);
            //Write the address
            for(int x = 0; x < addr_width; x++){
                nRF24_LL_RW(*addr++);
            }
            nRF24_CSN_H();
            break;
        case nRF24_PIPE2:
        case nRF24_PIPE3:
        case nRF24_PIPE4:
        case nRF24_PIPE5:
            // Write address LSBbyte (only first byte from the addr buffer)
            nRF24_WriteReg(nRF24_ADDR_REGS[pipe], *addr);
            break;
        default:
            // Incorrect pipe number -> do nothing
            break;
    }
}

fixes it, I haven't tried it with pipes 2->5 though.

LonelyWolf commented 5 years ago

I don’t remember why I did it this way. I believe that was some device (arduino-like stuff?) which wrote the address the back to front. Anyways, the opportunity to specify of how the address will be transmitted to the transceiver (at the compile time) sound like a good idea. Please take a look at the latest commit (the nRF24_ADDR_REVERSE define).

PS: right now I don't have F103 to test changes, so the demo code can be broken... But the library itself is working (tested it on the L4 device).