Added RF24 as an alternative.

Added some initialization for RadioHead.
This commit is contained in:
Dirk Jahnke 2018-11-07 17:06:52 +01:00
parent bb141a2615
commit 7ad2d6e404
6 changed files with 4102 additions and 0 deletions

1634
src/RF24/RF24.cpp Normal file

File diff suppressed because it is too large Load Diff

2013
src/RF24/RF24.h Normal file

File diff suppressed because it is too large Load Diff

164
src/RF24/RF24_config.h Normal file
View File

@ -0,0 +1,164 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/* spaniakos <spaniakos@gmail.com>
Added __ARDUINO_X86__ support
*/
#ifndef __RF24_CONFIG_H__
#define __RF24_CONFIG_H__
/*** USER DEFINES: ***/
//#define FAILURE_HANDLING
//#define SERIAL_DEBUG
//#define MINIMAL
//#define SPI_UART // Requires library from https://github.com/TMRh20/Sketches/tree/master/SPI_UART
//#define SOFTSPI // Requires library from https://github.com/greiman/DigitalIO
/**********************/
#define rf24_max(a,b) (a>b?a:b)
#define rf24_min(a,b) (a<b?a:b)
#if defined SPI_HAS_TRANSACTION && !defined SPI_UART && !defined SOFTSPI
#define RF24_SPI_TRANSACTIONS
#endif
//ATXMega
#if defined(__AVR_ATxmega64D3__) || defined(__AVR_ATxmega128D3__) || defined(__AVR_ATxmega192D3__) || defined(__AVR_ATxmega256D3__) || defined(__AVR_ATxmega384D3__) // In order to be available both in windows and linux this should take presence here.
#define XMEGA
#define XMEGA_D3
#include "utility/ATXMegaD3/RF24_arch_config.h"
#elif ( !defined (ARDUINO) ) // Any non-arduino device is handled via configure/Makefile
// The configure script detects device and copies the correct includes.h file to /utility/includes.h
// This behavior can be overridden by calling configure with respective parameters
// The includes.h file defines either RF24_RPi, MRAA, LITTLEWIRE or RF24_SPIDEV and includes the correct RF24_arch_config.h file
#include "utility/includes.h"
//ATTiny
#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny2313__) || defined(__AVR_ATtiny4313__) || defined(__AVR_ATtiny861__)
#define RF24_TINY
#include "utility/ATTiny/RF24_arch_config.h"
//LittleWire
#elif defined(LITTLEWIRE)
#include "utility/LittleWire/RF24_arch_config.h"
//Teensy
#elif defined (TEENSYDUINO)
#include "utility/Teensy/RF24_arch_config.h"
//Everything else
#else
#include <Arduino.h>
// RF modules support 10 Mhz SPI bus speed
const uint32_t RF24_SPI_SPEED = 10000000;
#if defined (ARDUINO) && !defined (__arm__) && !defined (__ARDUINO_X86__)
#if defined SPI_UART
#include <SPI_UART.h>
#define _SPI uspi
#elif defined SOFTSPI
// change these pins to your liking
//
#ifndef SOFT_SPI_MISO_PIN
#define SOFT_SPI_MISO_PIN 9
#endif
#ifndef SOFT_SPI_MOSI_PIN
#define SOFT_SPI_MOSI_PIN 8
#endif
#ifndef SOFT_SPI_SCK_PIN
#define SOFT_SPI_SCK_PIN 7
#endif
const uint8_t SPI_MODE = 0;
#define _SPI spi
#else
#include <SPI.h>
#define _SPI SPI
#endif
#else
// Define _BV for non-Arduino platforms and for Arduino DUE
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#if defined(__arm__) || defined (__ARDUINO_X86__)
#if defined (__arm__) && defined (SPI_UART)
#include <SPI_UART.h>
#define _SPI uspi
#else
#include <SPI.h>
#define _SPI SPI
#endif
#elif !defined(__arm__) && !defined (__ARDUINO_X86__)
extern HardwareSPI SPI;
#endif
#define _BV(x) (1<<(x))
#endif
#ifdef SERIAL_DEBUG
#define IF_SERIAL_DEBUG(x) ({x;})
#else
#define IF_SERIAL_DEBUG(x)
#if defined(RF24_TINY)
#define printf_P(...)
#endif
#endif
#if defined (__ARDUINO_X86__)
#define printf_P printf
#define _BV(bit) (1<<(bit))
#endif
// Progmem is Arduino-specific
// Arduino DUE is arm and does not include avr/pgmspace
#if defined (ARDUINO_ARCH_ESP8266)
#include <pgmspace.h>
#define PRIPSTR "%s"
#elif defined(ARDUINO) && !defined(ESP_PLATFORM) && ! defined(__arm__) && !defined (__ARDUINO_X86__) || defined(XMEGA)
#include <avr/pgmspace.h>
#define PRIPSTR "%S"
#else
#if ! defined(ARDUINO) // This doesn't work on Arduino DUE
typedef char const char;
#else // Fill in pgm_read_byte that is used, but missing from DUE
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#endif
typedef uint16_t prog_uint16_t;
#define PSTR(x) (x)
#define printf_P printf
#define strlen_P strlen
#define PROGMEM
#define pgm_read_word(p) (*(p))
#define PRIPSTR "%s"
#endif
#endif
#endif // __RF24_CONFIG_H__

127
src/RF24/nRF24L01.h Normal file
View File

@ -0,0 +1,127 @@
/*
Copyright (c) 2007 Stefan Engelke <mbox@stefanengelke.de>
Portions Copyright (C) 2011 Greg Copeland
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
/* Memory Map */
#define NRF_CONFIG 0x00
#define EN_AA 0x01
#define EN_RXADDR 0x02
#define SETUP_AW 0x03
#define SETUP_RETR 0x04
#define RF_CH 0x05
#define RF_SETUP 0x06
#define NRF_STATUS 0x07
#define OBSERVE_TX 0x08
#define CD 0x09
#define RX_ADDR_P0 0x0A
#define RX_ADDR_P1 0x0B
#define RX_ADDR_P2 0x0C
#define RX_ADDR_P3 0x0D
#define RX_ADDR_P4 0x0E
#define RX_ADDR_P5 0x0F
#define TX_ADDR 0x10
#define RX_PW_P0 0x11
#define RX_PW_P1 0x12
#define RX_PW_P2 0x13
#define RX_PW_P3 0x14
#define RX_PW_P4 0x15
#define RX_PW_P5 0x16
#define FIFO_STATUS 0x17
#define DYNPD 0x1C
#define FEATURE 0x1D
/* Bit Mnemonics */
#define MASK_RX_DR 6
#define MASK_TX_DS 5
#define MASK_MAX_RT 4
#define EN_CRC 3
#define CRCO 2
#define PWR_UP 1
#define PRIM_RX 0
#define ENAA_P5 5
#define ENAA_P4 4
#define ENAA_P3 3
#define ENAA_P2 2
#define ENAA_P1 1
#define ENAA_P0 0
#define ERX_P5 5
#define ERX_P4 4
#define ERX_P3 3
#define ERX_P2 2
#define ERX_P1 1
#define ERX_P0 0
#define AW 0
#define ARD 4
#define ARC 0
#define PLL_LOCK 4
#define RF_DR 3
#define RF_PWR 6
#define RX_DR 6
#define TX_DS 5
#define MAX_RT 4
#define RX_P_NO 1
#define TX_FULL 0
#define PLOS_CNT 4
#define ARC_CNT 0
#define TX_REUSE 6
#define FIFO_FULL 5
#define TX_EMPTY 4
#define RX_FULL 1
#define RX_EMPTY 0
#define DPL_P5 5
#define DPL_P4 4
#define DPL_P3 3
#define DPL_P2 2
#define DPL_P1 1
#define DPL_P0 0
#define EN_DPL 2
#define EN_ACK_PAY 1
#define EN_DYN_ACK 0
/* Instruction Mnemonics */
#define R_REGISTER 0x00
#define W_REGISTER 0x20
#define REGISTER_MASK 0x1F
#define ACTIVATE 0x50
#define R_RX_PL_WID 0x60
#define R_RX_PAYLOAD 0x61
#define W_TX_PAYLOAD 0xA0
#define W_ACK_PAYLOAD 0xA8
#define FLUSH_TX 0xE1
#define FLUSH_RX 0xE2
#define REUSE_TX_PL 0xE3
#define RF24_NOP 0xFF
/* Non-P omissions */
#define LNA_HCURR 0
/* P model memory Map */
#define RPD 0x09
#define W_TX_PAYLOAD_NO_ACK 0xB0
/* P model bit Mnemonics */
#define RF_DR_LOW 5
#define RF_DR_HIGH 3
#define RF_PWR_LOW 1
#define RF_PWR_HIGH 2

59
src/RF24/printf.h Normal file
View File

@ -0,0 +1,59 @@
/*
Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/* Galileo support from spaniakos <spaniakos@gmail.com> */
/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/
#ifndef __PRINTF_H__
#define __PRINTF_H__
#if defined (ARDUINO) && !defined (__arm__) && !defined(__ARDUINO_X86__)
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
#elif defined (__arm__)
void printf_begin(void){}
#elif defined(__ARDUINO_X86__)
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
//JESUS - For reddirect stdout to /dev/ttyGS0 (Serial Monitor port)
stdout = freopen("/dev/ttyGS0","w",stdout);
delay(500);
printf("redirecting to Serial...");
//JESUS -----------------------------------------------------------
}
#else
#error This example is only for use on Arduino.
#endif // ARDUINO
#endif // __PRINTF_H__

View File

@ -2,13 +2,20 @@
* Copyright (c) 2014-2017 Cesanta Software Limited
* All rights reserved
*/
#define RF_RadioHead
//#define RF_RF24
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#ifdef RF_RadioHead
#include "RadioHead/RH_NRF24.h"
#include "RadioHead/RHDatagram.h"
#endif
#ifdef RF_RF24
#include "RF24/RF24.h"
#endif
// #include <Fonts/Picopixel.h>
#include <Fonts/Org_01.h>
#include <Fonts/TomThumb.h>
@ -51,11 +58,25 @@ static const unsigned char PROGMEM logo16_glcd_bmp[] =
#endif
#endif
#ifdef RF_RadioHead
// Singleton instance of the radio driver
RH_NRF24 nrf24(PIN_NRF24_CSN, PIN_NRF24_CE);
// Address RH_BROADCAST_ADDRESS can be used for broadcasts as destination
RHDatagram Datagram(nrf24, THIS_ADRESS);
#endif
#ifdef RF_RF24
RF24 radio(PIN_NRF24_CSN,PIN_NRF24_CE); // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL }; // Radio pipe addresses for the 2 nodes to communicate.
byte data[32]; //Data buffer for testing data transfer speeds
unsigned long sendFailedCounter=0, rxTimer; //Counter and timer for keeping track transfer info
unsigned long receivedCounter=0;
unsigned long startTime, stopTime;
bool TX=1,RX=0,role=0;
#endif
@ -124,6 +145,11 @@ static void initDisplay_cb(void *arg) {
void setup(void) {
LOG(LL_INFO, ("*** Setup started"));
#ifdef RF_RadioHead
if (!Datagram.init())
LOG(LL_ERROR, ("*** Datagram init failed"));
}
#endif
LOG(LL_INFO, ("*** Setting timer"));
mgos_set_timer(2000 /* ms */, false /* repeat */, initDisplay_cb, NULL);
mgos_set_timer(7000 /* ms */, false /* repeat */, initFastclockRF_cb, NULL);
@ -217,9 +243,31 @@ static void incrementClockByMilliseconds(int amount) {
LOG(LL_INFO, ("*** new clock: %02d:%02d:%02d.%03d day %d, incBy_ms=%d", fastclock.hour, fastclock.minute, fastclock.second, fastclock.millisecond, fastclock.day, amount));
}
#ifdef RF_RF24
static void switchToSenderRole()
{
LOG(LL_INFO, ("*** CHANGING TO TRANSMIT ROLE"));
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
radio.stopListening();
role = TX; // Become the primary transmitter (ping out)
}
static void switchToReceiverRole()
{
LOG(LL_INFO, ("*** CHANGING TO RECEIVER ROLE"));
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
radio.startListening();
role = RX; // Become the primary receiver (pong back)
}
#endif
static void fastclockRF_receive_cb(void *arg) {
(void) arg;
#ifdef RF_RadioHead
// check for incoming messages
if (Datagram.available())
{
@ -236,6 +284,26 @@ static void fastclockRF_receive_cb(void *arg) {
LOG(LL_INFO, ("*** Datagram.recvfrom failed"));
}
}
#endif
#ifdef RF_RF24
uint8_t buf[RH_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
unsigned int counger=0;
switchToReceiverRole();
while (radio.available()) {
radio.read(buf, len);
counter++;
}
if (millis() - rxTimer > 1000) {
rxTimer = millis();
receivedCounter += counter;
unsigned long numBytes = counter*len;
LOG(LL_INFO, ("Bytes: %d, Msg count: %d", numBytes, counter));
counter = 0;
}
#endif
}
static struct clockMsg_s clockMsg;
@ -251,9 +319,29 @@ static void fastclockRF_send_cb(void *arg) {
// send clock info as a broadcast message
LOG(LL_INFO, ("*** Sending clock packet (broadcast)"));
#ifdef RF_RadioHead
if (Datagram.sendto((uint8_t *) &clockMsg, sizeof(clockMsg), RH_BROADCAST_ADDRESS)) {
LOG(LL_INFO, ("%02d:%02d:%02d - Sent new clock tick", fastclock.hour, fastclock.minute, fastclock.second));
}
#endif
#ifdef RF_RF24
switchToSenderRole();
if (!radio.writeFast(&clockMsg,sizeof(clockMsg))) { //Write to the FIFO buffers
sendFailedCounter++; //Keep count of failed payloads
}
//This is only required when NO ACK ( enableAutoAck(0) ) payloads are used
if (millis() - pauseTime > 3) {
pauseTime = millis();
radio.txStandBy(); // Need to drop out of TX mode every 4ms if sending a steady stream of multicast data
//delayMicroseconds(130); // This gives the PLL time to sync back up
}
stopTime = millis();
//This should be called to wait for completion and put the radio in standby mode after transmission, returns 0 if data still in FIFO (timed out), 1 if success
if (!radio.txStandBy()) { sendFailedCounter += 3; } //Standby, block only until FIFO empty or auto-retry timeout. Flush TX FIFO if failed
//radio.txStandBy(1000); //Standby, using extended timeout period of 1 second
#endif
}
static void timeTick_cb(void *arg) {
@ -275,6 +363,23 @@ static void initFastclockRF_cb(void *arg) {
fastclock.minute = 0;
fastclock.second = 0;
fastclock.millisecond = 0;
#ifdef RF_RF24
radio.begin();
radio.setChannel(1);
radio.setPALevel(RF24_PA_MAX);
radio.setDataRate(RF24_1MBPS);
radio.setAutoAck(0);
//radio.setRetries(2,15); // Optionally, increase the delay between retries & # of retries
radio.setCRCLength(RF24_CRC_8);
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
radio.startListening();
radio.printDetails();
randomSeed(analogRead(0));
radio.powerUp();
#endif
mgos_set_timer(100 /* ms */, true /* repeat */, fastclockRF_receive_cb, NULL);
mgos_set_timer(500 /* ms */, true /* repeat */, timeTick_cb, NULL);
mgos_set_timer(3000 /* ms */, true /* repeat */, fastclockRF_send_cb, NULL);