forked and modified initial value for return values and changed timer definition to TIM1 instead of int(1)

This commit is contained in:
willem oldemans
2020-10-01 09:36:42 +02:00
commit 6fab141c7e
181 changed files with 38514 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
// nrf51_audio_rx.pde
// Sample sketch for nRF51822 and RadioHead
//
// Plays audio samples received in the radio receiver
// through a MCP4725 DAC such as on a SparkFun I2C DAC Breakout - MCP4725 (BOB-12918)
// works with matching transmitter (see nrf51_audio_tx.pde)
// Works with RedBear nRF51822 board.
// See examples/nrf51_audiotx/nrf51_audio.pdf for connection details
#include <nrf51.h>
#include <nrf51_bitfields.h>
#include <esb/nrf_esb.h>
#include <RH_NRF51.h>
#include <Wire.h>
// Number of samples per second to play at.
// Should match SAMPLE_RATE in nrf51_audio_tx
// The limiting factor is the time it takes to output a new sample through the DAC
#define SAMPLE_RATE 5000
// Number of 8 bit samples per packet
// Should equal or exceed the PACKET_SIZE in nrf51_audio_tx
#define MAX_PACKET_SIZE 255
// Singleton instance of the radio driver
RH_NRF51 driver;
void setup()
{
delay(1000);
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect.
if (!driver.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
// Set up TIMER
// Use TIMER0
// Timer freq before prescaling is 16MHz (VARIANT_MCK)
// We set up a 32 bit timer that restarts every 100us and outputs a new sample
NRF_TIMER0->PRESCALER = 0 << TIMER_PRESCALER_PRESCALER_Pos;
NRF_TIMER0->MODE = TIMER_MODE_MODE_Timer << TIMER_BITMODE_BITMODE_Pos;
NRF_TIMER0->BITMODE = TIMER_BITMODE_BITMODE_32Bit << TIMER_BITMODE_BITMODE_Pos;
NRF_TIMER0->CC[0] = VARIANT_MCK / SAMPLE_RATE; // Counts per cycle
// When timer count expires, its cleared and restarts
NRF_TIMER0->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk;
NRF_TIMER0->TASKS_START = 1;
// Enable an interrupt when timer completes
NRF_TIMER0->INTENSET = TIMER_INTENSET_COMPARE0_Msk;
// Enable the TIMER0 interrupt, and set the priority
// TIMER0_IRQHandler() will be called after each sample is available
NVIC_SetPriority(TIMER0_IRQn, 1);
NVIC_EnableIRQ(TIMER0_IRQn);
// Initialise comms with the I2C DAC as fast as we can
// Shame the 51822 does not suport the high speed I2C mode that the DAC does
Wire.begin(TWI_SCL, TWI_SDA, TWI_FREQUENCY_400K);
}
volatile uint32_t count = 0;
uint8_t buffer_length = 0;
uint8_t buffer[MAX_PACKET_SIZE];
uint16_t buffer_play_index = 0;
// Write this sample to analog out
void analog_out(uint8_t val)
{
// This takes about 120usecs, which
// is the limiting factor for our sample rate of 5kHz
// Writes to MCP4725 DAC over I2C using the Wire library
Wire.beginTransmission(0x60); // 7 bit addressing
Wire.write((val >> 4) & 0x0f);
Wire.write((val << 4) & 0xf0);
Wire.endTransmission();
}
// Called by timer interrupt
// Output the next available sample
void output_next_sample()
{
if (buffer_play_index < buffer_length)
{
analog_out(buffer[buffer_play_index++]);
}
}
void loop()
{
// Look for a new packet of samples
if (driver.available())
{
// expect one of these every 40ms = 25Hz
// This takes about 400us:
buffer_length = sizeof(buffer);
driver.recv(buffer, &buffer_length);
buffer_play_index = 0; // Trigger the interrupt playing of this buffer from the start
}
}
// This interrupt handler called when the timer interrupt fires
// Time to output the next sample
void TIMER0_IRQHandler(void)
{
// It is vitally important that analog output completes before
// the next interrupt becomes due!
output_next_sample();
NRF_TIMER0->EVENTS_COMPARE[0] = 0; // Clear the COMPARE[0] event and the interrupt
}

Binary file not shown.

View File

@@ -0,0 +1,143 @@
// nrf51_audio_tx.pde
// Sample sketch for nRF51822 and RadioHead
//
// Reads audio samples from an electret microphone
// via the built in ADC in the nRF51822
// Blocks of samples are sent by RadioHEad RH_NRF51 driver
// to a matching receiver (see nrf51_audio_rx.pde)
// Works with RedBear nRF51822 board.
// See examples/nrf51_audiotx/nrf51_audio.pdf for connection details
#include <nrf51.h>
#include <nrf51_bitfields.h>
#include <esb/nrf_esb.h>
#include <RH_NRF51.h>
// Number of audio samples per second
// Should match SAMPLE_RATE in nrf51_audio_rx
// Limited by the rate we can output samples in the receiver
#define SAMPLE_RATE 5000
// Number of 8 bit samples per packet
#define PACKET_SIZE 200
// Number of ADC data buffers
#define NUM_BUFFERS 2
// Minimum diff between smallest and largest reading in a given buffer
// before we will send that buffer. We dont transmit quiet signals or silence
#define USE_SQUELCH 0
#define SQUELCH_THRESHOLD 2
// These provide data transfer between the low level ADC interrupt handler and the
// higher level packet assembly and transmission
volatile uint8_t buffers[NUM_BUFFERS][PACKET_SIZE];
volatile uint16_t sample_index = 0; // Of the next sample to write
volatile uint8_t buffer_index = 0; // Of the bufferbeing filled
volatile bool buffer_ready[NUM_BUFFERS]; // Set when a buffer is full
// These hold the state of the high level transmitter code
uint8_t next_tx_buffer = 0;
// Singleton instance of the radio driver
RH_NRF51 driver;
void setup()
{
delay(1000);
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect.
if (!driver.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
// Set up ADC
// Uses the builtin 1.2V bandgap reference and no prescaling
// AnalogInput2 is A0 on RedBear nrf51822 board
// Input voltage range is 0.0 to 1.2 V
NRF_ADC->CONFIG = ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos
| ADC_CONFIG_INPSEL_AnalogInputNoPrescaling << ADC_CONFIG_INPSEL_Pos
| ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos
| ADC_CONFIG_PSEL_AnalogInput2 << ADC_CONFIG_PSEL_Pos;
NRF_ADC->ENABLE = 1;
NRF_ADC->INTENSET = ADC_INTENSET_END_Msk; // Interrupt at completion of each sample
// Set up TIMER to trigger ADC samples
// Use TIMER0
// Timer freq before prescaling is 16MHz (VARIANT_MCK)
// We set up a 32 bit timer that restarts every 100us and trggers a new ADC sample
NRF_TIMER0->PRESCALER = 0 << TIMER_PRESCALER_PRESCALER_Pos;
NRF_TIMER0->MODE = TIMER_MODE_MODE_Timer << TIMER_BITMODE_BITMODE_Pos;
NRF_TIMER0->BITMODE = TIMER_BITMODE_BITMODE_32Bit << TIMER_BITMODE_BITMODE_Pos;
NRF_TIMER0->CC[0] = VARIANT_MCK / SAMPLE_RATE; // Counts per cycle
// When timer count expires, its cleared and restarts
NRF_TIMER0->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk;
NRF_TIMER0->TASKS_START = 1;
// When the timer expires, trigger an ADC conversion
NRF_PPI->CH[0].EEP = (uint32_t)(&NRF_TIMER0->EVENTS_COMPARE[0]);
NRF_PPI->CH[0].TEP = (uint32_t)(&NRF_ADC->TASKS_START);
NRF_PPI->CHENSET = PPI_CHEN_CH0_Msk;
// Enable the ADC interrupt, and set the priority
// ADC_IRQHandler() will be called after each sample is available
NVIC_SetPriority(ADC_IRQn, 1);
NVIC_EnableIRQ(ADC_IRQn);
}
// Called when a new sample is available from the ADC.
// Add it to the current buffer.
// when the buffer is full, signal that and switch to the other buffer.
void handle_sample()
{
buffers[buffer_index][sample_index++] = NRF_ADC->RESULT;
if (sample_index >= PACKET_SIZE)
{
sample_index = 0;
buffer_ready[buffer_index] = true;
buffer_index = (buffer_index + 1) % NUM_BUFFERS;
// If the next buffer is still still full, we have an overrun
if (buffer_ready[buffer_index])
Serial.println("Overrun");
}
}
void loop() {
// Wait for the adc to fill the current buffer
if (buffer_ready[next_tx_buffer])
{
#if USE_SQUELCH
// Honour squelch settings
uint8_t min_value = 255;
uint8_t max_value = 0;
uint16_t i;
for (i = 0; i < PACKET_SIZE; i++)
{
if (buffers[next_tx_buffer][i] > max_value)
max_value = buffers[next_tx_buffer][i];
if (buffers[next_tx_buffer][i] < min_value)
min_value = buffers[next_tx_buffer][i];
}
if (max_value - min_value > SQUELCH_THRESHOLD)
#endif
{
// OK to send this one
driver.waitPacketSent(); // Make sure the previous packet has gone
driver.send((uint8_t*)buffers[next_tx_buffer], PACKET_SIZE);
}
// Now get ready to wait for the next buffer
buffer_ready[next_tx_buffer] = false;
next_tx_buffer = (next_tx_buffer + 1) % NUM_BUFFERS;
}
}
// Called as an interrupt after each new ADC sample is complete.
void ADC_IRQHandler(void)
{
NRF_ADC->EVENTS_END = 0; // Clear the end event
handle_sample();
}

View File

@@ -0,0 +1,73 @@
// nrf51_client.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing client
// with the RH_NRF51 class. RH_NRF51 class does not provide for addressing or
// reliability, so you should only use RH_NRF51 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example nrf51_server.
// Tested on RedBearLabs nRF51822 and BLE Nano kit, built with Arduino 1.6.4.
// See http://redbearlab.com/getting-started-nrf51822/
// for how to set up your Arduino build environment
// Also tested with Sparkfun nRF52832 breakout board, witth Arduino 1.6.13 and
// Sparkfun nRF52 boards manager 0.2.3
#include <RH_NRF51.h>
// Singleton instance of the radio driver
RH_NRF51 nrf51;
void setup()
{
delay(1000); // Wait for serial port etc to be ready
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect.
if (!nrf51.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf51.setChannel(1))
Serial.println("setChannel failed");
if (!nrf51.setRF(RH_NRF51::DataRate2Mbps, RH_NRF51::TransmitPower0dBm))
Serial.println("setRF failed");
// AES encryption can be enabled by setting the same key in the sender and receiver
// uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
// 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
// nrf51.setEncryptionKey(key);
// nrf51.printRegisters();
}
void loop()
{
Serial.println("Sending to nrf51_server");
// Send a message to nrf51_server
uint8_t data[] = "Hello World!";
nrf51.send(data, sizeof(data));
nrf51.waitPacketSent();
// Now wait for a reply
uint8_t buf[RH_NRF51_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (nrf51.waitAvailableTimeout(500))
{
// Should be a reply message for us now
if (nrf51.recv(buf, &len))
{
Serial.print("got reply: ");
Serial.println((char*)buf);
}
else
{
Serial.println("recv failed");
}
}
else
{
Serial.println("No reply, is nrf51_server running?");
}
delay(400);
}

View File

@@ -0,0 +1,66 @@
// nrf51_reliable_datagram_client.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, reliable messaging client
// with the RHReliableDatagram class, using the RH_NRF51 driver to control a NRF51 radio.
// It is designed to work with the other example nrf51_reliable_datagram_server
// Tested on RedBearLabs nRF51822 and BLE Nano kit, built with Arduino 1.6.4.
// See http://redbearlab.com/getting-started-nrf51822/
// for how to set up your Arduino build environment
// Also tested with Sparkfun nRF52832 breakout board, witth Arduino 1.6.13 and
// Sparkfun nRF52 boards manager 0.2.3
#include <RHReliableDatagram.h>
#include <RH_NRF51.h>
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
// Singleton instance of the radio driver
RH_NRF51 driver;
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, CLIENT_ADDRESS);
void setup()
{
delay(1000); // Wait for serial port etc to be ready
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect.
if (!manager.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
}
uint8_t data[] = "Hello World!";
// Dont put this on the stack:
uint8_t buf[RH_NRF51_MAX_MESSAGE_LEN];
void loop()
{
Serial.println("Sending to nrf51_reliable_datagram_server");
// Send a message to manager_server
if (manager.sendtoWait(data, sizeof(data), SERVER_ADDRESS))
{
// Now wait for a reply from the server
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAckTimeout(buf, &len, 2000, &from))
{
Serial.print("got reply from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
}
else
{
Serial.println("No reply, is nrf51_reliable_datagram_server running?");
}
}
else
Serial.println("sendtoWait failed");
delay(500);
}

View File

@@ -0,0 +1,61 @@
// nrf51_reliable_datagram_server.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, reliable messaging server
// with the RHReliableDatagram class, using the RH_NRF51 driver to control a NRF51 radio.
// It is designed to work with the other example nrf51_reliable_datagram_client
// Tested on RedBearLabs nRF51822 and BLE Nano kit, built with Arduino 1.6.4.
// See http://redbearlab.com/getting-started-nrf51822/
// for how to set up your Arduino build environment
// Also tested with Sparkfun nRF52832 breakout board, witth Arduino 1.6.13 and
// Sparkfun nRF52 boards manager 0.2.3
#include <RHReliableDatagram.h>
#include <RH_NRF51.h>
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
// Singleton instance of the radio driver
RH_NRF51 driver;
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram manager(driver, SERVER_ADDRESS);
void setup()
{
delay(1000); // Wait for serial port etc to be ready
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect.
if (!manager.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
}
uint8_t data[] = "And hello back to you";
// Dont put this on the stack:
uint8_t buf[RH_NRF51_MAX_MESSAGE_LEN];
void loop()
{
if (manager.available())
{
// Wait for a message addressed to us from the client
uint8_t len = sizeof(buf);
uint8_t from;
if (manager.recvfromAck(buf, &len, &from))
{
Serial.print("got request from : 0x");
Serial.print(from, HEX);
Serial.print(": ");
Serial.println((char*)buf);
// Send a reply back to the originator client
if (!manager.sendtoWait(data, sizeof(data), from))
Serial.println("sendtoWait failed");
}
}
}

View File

@@ -0,0 +1,64 @@
// nrf51_server.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing server
// with the RH_NRF51 class. RH_NRF51 class does not provide for addressing or
// reliability, so you should only use RH_NRF51 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example nrf51_client
// Tested on RedBearLabs nRF51822 and BLE Nano kit, built with Arduino 1.6.4.
// See http://redbearlab.com/getting-started-nrf51822/
// for how to set up your Arduino build environment
// Also tested with Sparkfun nRF52832 breakout board, witth Arduino 1.6.13 and
// Sparkfun nRF52 boards manager 0.2.3
#include <RH_NRF51.h>
// Singleton instance of the radio driver
RH_NRF51 nrf51;
void setup()
{
delay(1000); // Wait for serial port etc to be ready
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect. Needed for Leonardo only
if (!nrf51.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf51.setChannel(1))
Serial.println("setChannel failed");
if (!nrf51.setRF(RH_NRF51::DataRate2Mbps, RH_NRF51::TransmitPower0dBm))
Serial.println("setRF failed");
// AES encryption can be enabled by setting the same key in the sender and receiver
// uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
// 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
// nrf51.setEncryptionKey(key);
}
void loop()
{
if (nrf51.available())
{
// Should be a message for us now
uint8_t buf[RH_NRF51_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (nrf51.recv(buf, &len))
{
// NRF51::printBuffer("request: ", buf, len);
Serial.print("got request: ");
Serial.println((char*)buf);
// Send a reply
uint8_t data[] = "And hello back to you";
nrf51.send(data, sizeof(data));
nrf51.waitPacketSent();
Serial.println("Sent a reply");
}
else
{
Serial.println("recv failed");
}
}
}