From 2cc1e9944664bb2c963f60f875081e7952e40eec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martynas=20Mickevi=C4=8Dius?= Date: Sun, 15 Feb 2015 23:39:30 +0200 Subject: [PATCH] Some doco. --- README.md | 5 +++++ bcm2835 | 1 - common.c | 22 ---------------------- common.h | 44 +++++++++++++++++++++++++++++++++++++++++--- platform.h | 4 ++++ receiver.c | 11 +++++++++++ receiver.h | 12 ++++++++++++ rpi/README.md | 5 +++++ rpi/bcm2835 | 1 + rpi/build.sh | 8 ++++---- tiny/Makefile | 2 +- tiny/README.md | 25 +++++++++++++++++++++++++ tiny/definitions.h | 4 ++++ tiny/platform.c | 1 + tiny/tiny_spi.c | 12 ++++++------ tiny/tiny_spi.h | 6 ++---- tiny/tinytemplate | 1 + tinytemplate | 1 - transmitter.c | 11 +++++++++++ transmitter.h | 25 ++++++++++++++++++++++++- 20 files changed, 158 insertions(+), 43 deletions(-) create mode 100644 README.md delete mode 120000 bcm2835 create mode 100644 rpi/README.md create mode 120000 rpi/bcm2835 create mode 100644 tiny/README.md create mode 120000 tiny/tinytemplate delete mode 120000 tinytemplate diff --git a/README.md b/README.md new file mode 100644 index 0000000..e623132 --- /dev/null +++ b/README.md @@ -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. diff --git a/bcm2835 b/bcm2835 deleted file mode 120000 index 628ee19..0000000 --- a/bcm2835 +++ /dev/null @@ -1 +0,0 @@ -../bcm2835-1.38 \ No newline at end of file diff --git a/common.c b/common.c index 1fae3bf..9da9e40 100644 --- a/common.c +++ b/common.c @@ -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 diff --git a/common.h b/common.h index aef80d6..1e115ad 100644 --- a/common.h +++ b/common.h @@ -3,21 +3,59 @@ #include +/** + * 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 diff --git a/platform.h b/platform.h index 32cf52f..66d95c2 100644 --- a/platform.h +++ b/platform.h @@ -3,6 +3,10 @@ #include +/** + * 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); diff --git a/receiver.c b/receiver.c index 32611e9..4c21342 100644 --- a/receiver.c +++ b/receiver.c @@ -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); diff --git a/receiver.h b/receiver.h index fb0e1aa..2ef8ac7 100644 --- a/receiver.h +++ b/receiver.h @@ -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 diff --git a/rpi/README.md b/rpi/README.md new file mode 100644 index 0000000..e07b2c5 --- /dev/null +++ b/rpi/README.md @@ -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`. diff --git a/rpi/bcm2835 b/rpi/bcm2835 new file mode 120000 index 0000000..7ba204d --- /dev/null +++ b/rpi/bcm2835 @@ -0,0 +1 @@ +../../bcm2835-1.38 \ No newline at end of file diff --git a/rpi/build.sh b/rpi/build.sh index f8e90e3..3913a5a 100755 --- a/rpi/build.sh +++ b/rpi/build.sh @@ -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 \ diff --git a/tiny/Makefile b/tiny/Makefile index 5bc2da2..3257359 100644 --- a/tiny/Makefile +++ b/tiny/Makefile @@ -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 \ diff --git a/tiny/README.md b/tiny/README.md new file mode 100644 index 0000000..a19f103 --- /dev/null +++ b/tiny/README.md @@ -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|prx |Binary (primary transmitter or primary receiver) to build | +|avrType |attiny85|attiny2313|Controller type. Tested only with attiny85 | +|dudeHost|alarmpi|... |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`. diff --git a/tiny/definitions.h b/tiny/definitions.h index 066131a..fce6b39 100644 --- a/tiny/definitions.h +++ b/tiny/definitions.h @@ -3,6 +3,10 @@ #include +/** + * This overrides definitions in hardware.h of the tinytemplate library. + */ + #define UART_ENABLED #define BAUD_RATE 57600 diff --git a/tiny/platform.c b/tiny/platform.c index 9e04e34..37e8490 100644 --- a/tiny/platform.c +++ b/tiny/platform.c @@ -54,4 +54,5 @@ void spi_transfern(uint8_t *buf, uint8_t n) void ce_high() { + // CE is conntected to VCC. } diff --git a/tiny/tiny_spi.c b/tiny/tiny_spi.c index 046200b..cd0769e 100644 --- a/tiny/tiny_spi.c +++ b/tiny/tiny_spi.c @@ -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)); @@ -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) @@ -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) diff --git a/tiny/tiny_spi.h b/tiny/tiny_spi.h index d9b0105..8a28c6e 100644 --- a/tiny/tiny_spi.h +++ b/tiny/tiny_spi.h @@ -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); diff --git a/tiny/tinytemplate b/tiny/tinytemplate new file mode 120000 index 0000000..2822b51 --- /dev/null +++ b/tiny/tinytemplate @@ -0,0 +1 @@ +../../tinytemplate-2db013c \ No newline at end of file diff --git a/tinytemplate b/tinytemplate deleted file mode 120000 index 02aa5c0..0000000 --- a/tinytemplate +++ /dev/null @@ -1 +0,0 @@ -../tinytemplate \ No newline at end of file diff --git a/transmitter.c b/transmitter.c index 494283f..053a31c 100644 --- a/transmitter.c +++ b/transmitter.c @@ -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 diff --git a/transmitter.h b/transmitter.h index e615951..9707a89 100644 --- a/transmitter.h +++ b/transmitter.h @@ -3,11 +3,34 @@ #include +/** + * 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