Skip to content

Commit

Permalink
Some doco.
Browse files Browse the repository at this point in the history
  • Loading branch information
2m committed Feb 15, 2015
1 parent 859d946 commit 2cc1e99
Show file tree
Hide file tree
Showing 20 changed files with 158 additions and 43 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Samana network components

This repository contains transmitters and receivers implemented using [nRF24L01+](https://www.nordicsemi.com/eng/Products/2.4GHz-RF/nRF24L01P) wireless chip.

Implementation for Raspberry PI can be found in the [rpi](rpi/) directory, and (tiny)[tiny/] directory contains implementation for ATtiny85 MCU.
1 change: 0 additions & 1 deletion bcm2835

This file was deleted.

22 changes: 0 additions & 22 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,6 @@ void common_config()
spi_transfern(buf, 2);
}

void ptx()
{
// read CONFIG register
uint8_t buf[2] = { 0, 0 };
spi_transfern(buf, 2);

buf[0] = 0b00100000; // write to CONFIG
buf[1] |= 0 << 0; // PRX
spi_transfern(buf, 2);
}

void prx()
{
// read CONFIG register
uint8_t buf[2] = { 0, 0 };
spi_transfern(buf, 2);

buf[0] = 0b00100000; // write to CONFIG
buf[1] |= 1 << 0; // PRX
spi_transfern(buf, 2);
}

void prx_addr()
{
// RX_ADDR_P0
Expand Down
44 changes: 41 additions & 3 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,59 @@

#include <stdint.h>

/**
* Configure NRF24 registers that are common for prx and ptr.
*/
void common_config();
void ptx();
void prx();

/**
* Set prx listen address.
*/
void prx_addr();

/**
* Set receiver address and listen address for acks.
*/
void receiver_addr();

/**
* Go to POWER UP state.
*/
void power_up();

/**
* Got to POWER DOWN state.
*/
void power_down();

/**
* Check if data is ready in the RX FIFO by querying which data pipe has data available.
*/
uint8_t rx_data_ready();
uint8_t get_rx_data();

/**
* Clock in received data from RX FIFO.
*/
uint8_t get_rx_data(uint8_t *rx_data);

/**
* Flush TX FIFO. In prx this holds ACK payloads.
*/
void flush_tx();

/**
* Flush RX FIFO.
*/
void flush_rx();

/**
* Clear interrupt flags.
*/
void clean_int_flags();

/**
* Flush TX and RX FIFOs and clear interrupt flags.
*/
void clean_up();

#endif
4 changes: 4 additions & 0 deletions platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include <stdint.h>

/**
* Contains function definitions which implementations are platform specific.
*/

void print(char *str);
void println(char *str);
void print_buf(uint8_t *buf, uint8_t n);
Expand Down
11 changes: 11 additions & 0 deletions receiver.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#include "platform.h"
#include "common.h"

void prx()
{
// read CONFIG register
uint8_t buf[2] = { 0, 0 };
spi_transfern(buf, 2);

buf[0] = 0b00100000; // write to CONFIG
buf[1] |= 1 << 0; // PRX
spi_transfern(buf, 2);
}

void clean_rx_dr_int()
{
uint8_t status = spi_transfer(0xFF);
Expand Down
12 changes: 12 additions & 0 deletions receiver.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
#ifndef RECEIVER_H
#define RECEIVER_H

/**
* Configure prx specific registers.
*/
void prx();

/**
* Clean RX Data Ready interrupt flag.
*/
void clean_rx_dr_int();

/**
* A loop where receiver puts ACK payloads and handles received messages.
*/
void receiver_loop() __attribute__ ((noreturn));

#endif
5 changes: 5 additions & 0 deletions rpi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Building

Make sure `bcm2835` symlink is pointing to a directory which contains [bcm2835](http://www.airspayce.com/mikem/bcm2835/) library. By default symlink points to a sibling directory of this repository. Version of the library is specified in the contents of the symlink.

Build both transmitter and receiver binaries by running `build.sh`.
1 change: 1 addition & 0 deletions rpi/bcm2835
8 changes: 4 additions & 4 deletions rpi/build.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env bash

gcc -g -o ptx -std=gnu99 -Wall -I ../bcm2835/src/ \
../bcm2835/src/bcm2835.c \
gcc -g -o ptx -std=gnu99 -Wall -I bcm2835/src/ \
bcm2835/src/bcm2835.c \
../common.c \
../transmitter.c \
platform.c \
ptx.c

gcc -g -o prx -std=gnu99 -Wall -I ../bcm2835/src/ \
../bcm2835/src/bcm2835.c \
gcc -g -o prx -std=gnu99 -Wall -I bcm2835/src/ \
bcm2835/src/bcm2835.c \
../common.c \
../receiver.c \
platform.c \
Expand Down
2 changes: 1 addition & 1 deletion tiny/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ endif
cflags+=-include definitions.h

# https://github.com/thegaragelab/tinytemplate library
tinytpl=../tinytemplate
tinytpl=tinytemplate
cflags+=-I $(tinytpl)/firmware/include
srcs+= \
$(tinytpl)/firmware/shared/uart_send.c \
Expand Down
25 changes: 25 additions & 0 deletions tiny/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Building

Make sure `tinytemplate` symlink is pointing to a directory which contains checked out [tinytemplate](https://github.com/thegaragelab/tinytemplate) repository. By default symlink points to a sibling directory of this repository. Commit id of the repository is specified in the contents of symplink.

Makefile accepts the following arguments:

|Argument|Possible Values|Notes |
|--------|---------------|-------------------------------------------------------------------|
|name |ptx&#124;prx |Binary (primary transmitter or primary receiver) to build |
|avrType |attiny85&#124;attiny2313|Controller type. Tested only with attiny85 |
|dudeHost|alarmpi&#124;... |When specified runs `avrdude` on the specified host |

Most common make commands are `elf` (compile) and `flash` (compile and write to MCU). Flashing is done using a Raspberry Pi which has MCU connected via SPI.

For example, the following command would compile primary transmitter and flash it to ATtiny85 connected to Raspberry PI running as alarmpi2:

``` bash
make name=ptx avrType=attiny85 dudeHost=alarmpi2 flash
```

## Hardware

Code assumes [three wire](http://nerdralph.blogspot.com/2014/01/nrf24l01-control-with-3-attiny85-pins.html) setup where CSN clock is held by a capacitor and is controlled by SCK pin before and after transfers.

Logging is enabled via UART on `PB4`.
4 changes: 4 additions & 0 deletions tiny/definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include <avr/io.h>

/**
* This overrides definitions in hardware.h of the tinytemplate library.
*/

#define UART_ENABLED
#define BAUD_RATE 57600

Expand Down
1 change: 1 addition & 0 deletions tiny/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ void spi_transfern(uint8_t *buf, uint8_t n)

void ce_high()
{
// CE is conntected to VCC.
}
12 changes: 6 additions & 6 deletions tiny/tiny_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

#include "tiny_spi.h"

/**
* Thank you goes to https://github.com/JChristensen/tinySPI/blob/master/tinySPI.cpp
* for implementing SPI in C.
*/

void tiny_spi_begin(void)
{
USICR &= ~(_BV(USISIE) | _BV(USIOIE) | _BV(USIWM1));
Expand All @@ -12,9 +17,6 @@ void tiny_spi_begin(void)
SPI_DDR_PORT &= ~_BV(DI_DD_PIN); //set the DI pin as input

USICR &= ~_BV(USICS0); // SPI MODE 0

//SPI_DDR_PORT |= _BV(SPI_CSN_PIN);
//SPI_PORT |= _BV(SPI_CSN_PIN);
}

uint8_t tiny_spi_crank(uint8_t spiData)
Expand All @@ -29,16 +31,14 @@ uint8_t tiny_spi_crank(uint8_t spiData)

void before_transfer()
{
//SPI_PORT &= ~_BV(SPI_CSN_PIN);
SPI_PORT &= ~_BV(USCK_DD_PIN); // SCK low will pull CSN low as well
_delay_us(16); // allow CSN to settle
}

void after_transfer()
{
//SPI_PORT |= _BV(SPI_CSN_PIN);
SPI_PORT |= _BV(USCK_DD_PIN); // SCK high will pull CSN high as well
_delay_us(128); // allow csn to settle
_delay_us(128); // allow CSN to settle
}

void tiny_spi_transfern(uint8_t *buf, uint8_t n)
Expand Down
6 changes: 2 additions & 4 deletions tiny/tiny_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
#define SPI_PORT PORTB
#define SPI_DDR_PORT DDRB
#define USCK_DD_PIN DDB2
#define DO_DD_PIN DDB1
#define DI_DD_PIN DDB0

#define SPI_CSN_PIN DDB3
#define DO_DD_PIN DDB1 // DO is MISO on ATtiny85
#define DI_DD_PIN DDB0 // DI is MOSI on ATtiny85

void tiny_spi_begin();
uint8_t tiny_spi_transfer(uint8_t spiData);
Expand Down
1 change: 1 addition & 0 deletions tiny/tinytemplate
1 change: 0 additions & 1 deletion tinytemplate

This file was deleted.

11 changes: 11 additions & 0 deletions transmitter.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#include "platform.h"
#include "common.h"

void ptx()
{
// read CONFIG register
uint8_t buf[2] = { 0, 0 };
spi_transfern(buf, 2);

buf[0] = 0b00100000; // write to CONFIG
buf[1] |= 0 << 0; // PRX
spi_transfern(buf, 2);
}

void tx_payload()
{
// W_TX_PAYLOAD command
Expand Down
25 changes: 24 additions & 1 deletion transmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,34 @@

#include <stdint.h>

/**
* Configure ptx specific registers.
*/
void ptx();

/**
* Transmit payload.
*/
void tx_payload();

/**
* Check if max retransmits has been reached.
*/
uint8_t max_retransmits_reached();
uint8_t tx_data_sent();

/**
* Clear max retransmits reached flag.
*/
void clean_max_rt_int();

/**
* Check if data has been sent, which means that ACK has been received with or without payload.
*/
uint8_t tx_data_sent();

/**
* Configure and send predefined payload,
*/
void send_payload();

#endif

0 comments on commit 2cc1e99

Please sign in to comment.