Connecting the nRF24L01+
to the Raspberry Pi

Pi Logo

Boring specs on the Pi

Boot process - 1

  1. SoC internal ROM
    • Mounts FAT32 SD card
    • Loads bootcode.bin to L2 cache
    • Starts GPU
  2. GPU loads start.elf (2.6 MB!)
    • Loads config.txt
    • Configures memory using fixup.dat
    • Starts ARM CPU

Boot process - 2

Raspbian - 1

Raspbian - 2

Contents of the fat partition:

-rwxr-xr-x  1 root root   17808 Jun 19 11:08 bootcode.bin*
-rwxr-xr-x  1 root root     142 Jul 26 12:50 cmdline.txt*
-rwxr-xr-x  1 root root    1180 Jul 26 12:50 config.txt*
-rwxr-xr-x  1 root root    2024 Jun 19 11:08 fixup_cd.dat*
-rwxr-xr-x  1 root root    5882 Jun 19 11:08 fixup.dat*
-rwxr-xr-x  1 root root    8832 Jun 19 11:08 fixup_x.dat*
-rwxr-xr-x  1 root root     137 Jul 26 14:44 issue.txt*
-rwxr-xr-x  1 root root 9610248 Jun 19 11:08 kernel_emergency.img*
-rwxr-xr-x  1 root root 2803520 Jun 19 11:08 kernel.img*
-rwxr-xr-x  1 root root  468536 Jun 19 11:08 start_cd.elf*
-rwxr-xr-x  1 root root 2689268 Jun 19 11:08 start.elf*
-rwxr-xr-x  1 root root 3656516 Jun 19 11:08 start_x.elf*
So what are all these files good for?

Raspbian - 3

Raspbian - 4

We have pre-configured an image with DHCP and SSH enabled

Login: pi / hamster

Two Linksys APs (Rasp Pi 1 and 2): admin / hamster

Use raspi-config to expand the file system

External connector on the Pi

Pi Connector

GPIO on the Pi - 1

GPIO on the Pi - 2

...export GSYS=/sys/class/gpio
Exporting a pin 25 as output:echo 25 >$GSYS/export
echo out >$GSYS/gpio25/direction
Setting it high:echo 1 >$GSYS/gpio25/value
Setting it low:echo 0 >$GSYS/gpio25/value
Exporting a pin as input:echo in >$GSYS/gpio25/direction
Reading the state:cat $GSYS/gpio25/value

Sucks being super user

gpio-admin

Your first success with an LED and the Pi

Let us check we can control the GPIOs by attaching an LED:

LED

Anode goes to the resistor (long pin). Cathode goes to ground (short pin)

A few words on SPI

Example on wiring

SPI three slaves

nRF24L01+ features

nRF24L01+ state diagram

nRF state diagram

nRF24L01+ registers

Chip is controlled by a number of registers

From nRF24L01.h

#define CONFIG      0x00
#define EN_AA       0x01
#define EN_RXADDR   0x02
#define SETUP_AW    0x03
#define SETUP_RETR  0x04
#define RF_CH       0x05
...

Let us check section 9 of the datasheet

nRF24L01+ SPI interface

Let us check section 8.3.1 of the datasheet

SPI interface code example

uint8_t RF24::read_register(uint8_t reg,
                            uint8_t* buf,
                            uint8_t len)
{
  uint8_t status;

  csn(LOW);

  status = spi->transfer( R_REGISTER | ( REGISTER_MASK & reg ) );

  while ( len-- )
    *buf++ = spi->transfer(0xff);

  csn(HIGH);

  return status;
}

nRF24L01+ PCB pinout

nRF PCB pinout

Enable SPI on Pi - 1

Enable SPI kernel module

/etc/modprobe.d/raspi-blacklist.conf

You should end up with:

pi@raspberrypi /etc/modprobe.d $ ls -la /dev/spidev0.*
crw-rw---T 1 root spi 153, 0 Jan  1  1970 /dev/spidev0.0
crw-rw---T 1 root spi 153, 1 Jan  1  1970 /dev/spidev0.1

Enable SPI on Pi - 2

Make a SPI group and add yourself:

sudo groupadd -f --system spi

sudo /bin/sh -c 'echo ''SUBSYSTEM==\"spidev\", GROUP=\"spi\"'' >/etc/udev/rules.d/90-spi.rules'

sudo adduser $USER spi

Hook it up

Time to connect the pieces

  1. CE goes on GPIO 25
  2. CSN goes on GPIO 8

git clone software from:

https://github.com/openspaceaarhus/Datalogforeningen-workshop.git

SPI software

Library:

cd rpi/librf24

make

sudo make install

Examples:

cd rpi/librf24/examples

make

Expected output from 'pingtest ping'

SPI device	 = /dev/spidev0.0
SPI speed	 = 8000000
CE GPIO	 = 25
STATUS		 = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1	 = 0xf0f0f0f0e1 0xf0f0f0f0d2
RX_ADDR_P2-5	 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR		 = 0xf0f0f0f0e1
RX_PW_P0-6	 = 0x20 0x20 0x00 0x00 0x00 0x00
EN_AA		 = 0x00
EN_RXADDR	 = 0x03
RF_CH		 = 0x4c
RF_SETUP	 = 0x07
CONFIG		 = 0x0f
DYNPD/FEATURE	 = 0x00 0x00
Data Rate	 = 1MBPS
Model		 = nRF24L01+
...

Getting data transferred

Set the channel to:your age since the epoch % 128

Try to do a ping/pong with somebody else

Chip supports Auto Ack payloads.

Play with enableAckPayload, writeAckPayload and isAckPayloadAvailable

For the hackers

Find the message that our beacon is sending

For the real hackers

Arduino - what is that?

arduino uno

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software

Arduino Uno

Arduino simplicity - blink the LED


#define LED_PIN 13
 
void setup () {
  pinMode (LED_PIN, OUTPUT); // Enable pin 13 for digital output
}
 
void loop () {
  digitalWrite (LED_PIN, HIGH); // Turn on the LED
  delay (1000);
  digitalWrite (LED_PIN, LOW); // Turn off the LED
  delay (1000);
}

Arduino Uno - SPI

SPI on the Arduino _UNO_

SPI Arduino

D10 == SS == CSN and we use D9 for CE

Arduino Leonardo - SPI

On the Arduino Leonardo SPI is on the ICSP header

PIN 1 is the one with the small dot next to it

Arduino - Makefile

ARDUINO_DIR  = /usr/share/arduino

TARGET       = pingpair
ARDUINO_LIBS = SPI

BOARD_TAG    = uno
ARDUINO_PORT = /dev/cu.usb*

ARDUINO_PORT =/dev/ttyACM0
ARD_PROG = -c stk500v2

include /usr/share/arduino/Arduino.mk

Type make to build and make upload to write to the Arduino

Arduino - software details