- A simple interface between software UART and USI configured as SPI master.
- nRF24L01+ module wiring
Gnd [1](2) Vcc
Gnd <- CE (3)(4) CSN ->
SCL <- SCK (5)(6) MOSI -> DO or MOSI
MISO or DI <- MISO (7)(8) IRQ
- Note
- For devices that use an USI to emulate an SPI interface, the MCU is considered a slave regarding the SPI pin names. Pin MISO, output of the nRF, has to be connected to pin MOSI/DI of the MCU, and pin MOSI, input of the nRF, has to be connected to pin MISO/DO of the MCU.
- 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
- config.h
#ifndef CONFIG_H
#define CONFIG_H
#include BOARD_H
#define UART HW_SWUARTA( txd, DIABOLO_PIN_TX, \
rxd, DIABOLO_PIN_RX, \
startirq, (DIABOLO_PIN_RX,port,pcic,irq), \
counter, counter1, \
compare, compare0, \
clkdiv, 1, \
autosync, 51, \
fastreg, (shared,gpior0) )
#endif
Symbols:
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 "config.h"
#if HW_ADDRESS(usi0) == -1
# error device `HW_DEVICE` does not have a USI
#endif
#if defined ARDUINO
# define USI spi0
# define NRF_CSN PIN_D2
#else
# define USI usi0
# define NRF_CSN (pin,3)
#endif
uint8_t uart_getbyte ( )
{
while ( !
hw(stat,UART).rxc )
}
void uart_putbyte ( uint8_t byte )
{
while ( !
hw(stat,UART).txc )
}
static void write_usi ( char c )
{
hw( clear, (USI,irq,txc) );
do {
}
while ( !
hw( read, (USI,irq,txc) ) );
}
int
main ( )
{
mode, spi_master,
clock, software );
hwa( configure, NRF_CSN, mode, digital_output );
hwa( write, NRF_CSN, 1 );
hw( enable, interrupts );
while ( !
hw(stat,UART).sync ) {}
for(;;) {
uart_putbyte( '$' );
uint8_t c = uart_getbyte();
if ( c == '=' ) {
uint8_t ntx = uart_getbyte();
if ( ntx < 1 || ntx > 33 )
goto error ;
uint8_t nrx = uart_getbyte();
if ( nrx > 32 )
goto error ;
while ( ntx-- ) {
c = uart_getbyte();
write_usi( c );
}
while ( nrx-- ) {
write_usi( 0 );
uart_putbyte( c );
}
}
else {
do {
error:
uart_putbyte( '!' );
c = uart_getbyte();
} while ( c != '\n' ) ;
}
}
}