- A simple interface between UART and SPI master connected to a nRF24L01
- nRF24L01+ module wiring
Gnd [1](2) Vcc
Gnd <- CE (3)(4) CSN -> SS
SCL <- SCK (5)(6) MOSI -> MOSI
MISO <- MISO (7)(8) IRQ
- Test application
./main.py
should display:
Register CONFIG : 0x00 = 08
Register EN_AA : 0x01 = 3F
Register EN_RX_ADDR: 0x02 = 03
Register SETUP_AW : 0x03 = 03
Register SETUP_RETR: 0x04 = 03
Register RF_CH : 0x05 = 02
Register RF_SETUP : 0x06 = 0F
Register STATUS : 0x07 = 0E
Register RX_ADDR_P0: 0x0A = E7 E7 E7 E7 E7
Register RX_ADDR_P1: 0x0B = C2 C2 C2 C2 C2
Register DYNPD : 0x1C = 00
Register FEATURE : 0x1D = 00
BOARD_H
is the name of the target board header file. It can be defined at compile time via the command line. For example, make BOARD=nanodccduino
will define BOARD_H
as <boards/nanodccduino.h>
. See Development boards for the board definitions provided with HWA.
- main.c
#include BOARD_H
#if defined ARDUINO
# define NRF_CSN PIN_SS
#else
# error That should not happen
#endif
#define UART uart0
uint8_t spi ( uint8_t v )
{
while( !
hw( read, (spi0,irq) ) ) {}
}
uint8_t uart_rx()
{
while ( !
hw( read, (UART,irq,rxc) ) ) {}
}
void uart_tx( uint8_t c )
{
while ( !
hw( read, (UART,irq,txqnf) ) ) {}
}
int
main ( )
{
bps, 115200 );
function, master,
clock, ioclk / 128,
idle_state, low,
sampling_edge, rising,
data_order, msb_first );
hwa( configure, NRF_CSN, mode, digital_output );
hwa( write, NRF_CSN, 1 );
for(;;) {
uart_tx( '$' );
uint8_t c = uart_rx();
if ( c == '=' ) {
uint8_t ntx = uart_rx();
if ( ntx < 1 || ntx > 33 )
goto error ;
uint8_t nrx = uart_rx();
if ( nrx > 32 )
goto error ;
while ( ntx-- ) {
c = uart_rx();
spi( c );
}
while ( nrx-- ) {
c = spi(0);
uart_tx(c);
}
}
else {
do {
error:
uart_tx('!');
c = uart_rx();
} while ( c != '\n' ) ;
}
}
}