Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nrf24] Update driver and examples #253

Merged
merged 2 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ make gdb

## Interesting Examples

We have a lot of examples, <!--examplecount-->191<!--/examplecount--> to be
We have a lot of examples, <!--examplecount-->190<!--/examplecount--> to be
exact, but here are some of our favorite examples for our supported development
boards:

Expand Down
10 changes: 10 additions & 0 deletions examples/nucleo_f411re/radio/lbuild.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library>
<extends>modm:nucleo-f411re</extends>
<modules>
<module>modm:driver:nrf24</module>
<module>modm:platform:spi:2</module>
<module>modm:platform:spi:3</module>
<module>modm:platform:timer:2</module>
<module>modm:processing:timer</module>
</modules>
</library>
150 changes: 150 additions & 0 deletions examples/nucleo_f411re/radio/nrf24-basic-comm/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2014, Daniel Krebs
* Copyright (c) 2014, 2017, Sascha Schade
* Copyright (c) 2014-2019, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include "../radio.hpp"

/*
* Basic communication with least config possible
*
* In this example one module is configured as primary tx, so it will just send
* packets and never receive anything. The second module is configured as
* primary rx, so it will only receive packets.
*/

using Register = Nrf1Phy::NrfRegister;
using Status = Nrf1Phy::Status;
using Pipe = Nrf1Phy::Pipe;
using Config = Nrf1Phy::Config;
using FifoStatus = Nrf1Phy::FifoStatus;

int main()
{
Board::initialize();
MODM_LOG_INFO << "Hello from nrf24-basic-comm example" << modm::endl;

/* Configuration values for nRF24 */
/* Use channel 2400 + 60 MHz = 2460 MHz */
constexpr const uint8_t rf_channel = 60;

/* 4 byte payload length */
constexpr const uint8_t payload_length = 4;

/* Address for ptx module. Not used here because ptx won't receive packets */
// constexpr const uint64_t ptx_address = 0xB3B4B5B605;
constexpr const uint64_t prx_address = 0xB3B4B5B607;

/* Dummy payload */
uint8_t payload[payload_length] = {
0xaa,
0xbb,
0xcc,
0xdd
};

initializeSpi();

Nrf1Ce::set();
Nrf2Ce::set();

// Initialize nRF24-HAL
Nrf1Phy::initialize(payload_length);
Nrf2Phy::initialize(payload_length);

/* set RF channel */
Nrf1Phy::writeRegister(Register::RF_CH, rf_channel);
Nrf2Phy::writeRegister(Register::RF_CH, rf_channel);

/* Set payload length for pipe 1 on receiver */
Nrf2Phy::writeRegister(Register::RX_PW_P1, payload_length);

/* Set tx address of ptx device to prx's address and also set
* receive pipe 0 to the same address to receive ACK packets
* from prx.
*/
Nrf1Phy::setTxAddress(prx_address);
Nrf1Phy::setRxAddress(Pipe::PIPE_0, prx_address);

/* Set receive pipe 1 of prx device to receive packets from ptx */
Nrf2Phy::setRxAddress(Pipe::PIPE_1, prx_address);

/* Configure ptx as primary sender and power up */
Nrf1Phy::clearBits(Register::CONFIG, Config::PRIM_RX);
Nrf1Phy::setBits(Register::CONFIG, Config::PWR_UP);

/* Configure prx as primary receiver and power up*/
Nrf2Phy::setBits(Register::CONFIG, Config::PRIM_RX);
Nrf2Phy::setBits(Register::CONFIG, Config::PWR_UP);


/* Timer to send packets every 1000ms */
modm::ShortPeriodicTimer sendPacket(1000);

/* Buffer for received payload */
uint8_t received_data[payload_length];

while (true)
{
// ------------------------- Primary sender ---------------------------

/* Send packet every 1000ms */
if(sendPacket.execute())
{
/* Copy packet into ptx device. Because CE is always high here, the
* packet will be transmitted immediately
*/
Nrf1Phy::writeTxPayload(payload, payload_length);
payload[0]++;

Board::LedD13::toggle();
}

/* Check if packet was sent successfully */
if(Nrf1Phy::readStatus() & ((uint8_t)Status::TX_DS | (uint8_t)Status::MAX_RT))
{
if(Nrf1Phy::readStatus() & (uint8_t)Status::MAX_RT)
{
MODM_LOG_INFO.printf("Packet lost, MAX_RT reached\n");
MODM_LOG_INFO.printf(" Status: %x\n", Nrf1Phy::readStatus());
Nrf1Phy::setBits(Register::STATUS, Status::MAX_RT);
MODM_LOG_INFO.printf(" Status: %x\n", Nrf1Phy::readStatus());
} else
{
MODM_LOG_INFO.printf("Packet successfully sent\n");
Nrf1Phy::setBits(Register::STATUS, Status::TX_DS);
}

Board::LedD13::toggle();
}


// ----------------------- Primary receiver ---------------------------

/* Check for received bytes */
if( (!(Nrf2Phy::readRegister(Register::FIFO_STATUS) & (uint8_t)FifoStatus::RX_EMPTY)) || (Nrf2Phy::readStatus() & ((uint8_t)Status::RX_DR)))
{
uint8_t pl = 0;

/* Read payload of received packet */
pl = Nrf2Phy::readRxPayload(received_data);

/* Clear RX_DR flag after payload is read */
Nrf2Phy::setBits(Register::STATUS, Status::RX_DR);

MODM_LOG_INFO.printf("Received packet, pl=%d, data: %x %x %x %x\n", pl, received_data[3], received_data[2], received_data[1], received_data[0]);

Board::LedD13::toggle();
}
}

return 0;
}
8 changes: 8 additions & 0 deletions examples/nucleo_f411re/radio/nrf24-basic-comm/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<library>
<options>
<option name="modm:build:build.path">../../../../build/nucleo_f411re/radio/nrf24-basic-comm</option>
</options>
<modules>
<module>modm:build:scons</module>
</modules>
</library>
48 changes: 48 additions & 0 deletions examples/nucleo_f411re/radio/nrf24-data/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2015, Daniel Krebs
* Copyright (c) 2015, 2017, Sascha Schade
* Copyright (c) 2015-2019, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include "../radio.hpp"

// This example showcases a simple usage of the data layer implementation for
// the nRF24L01+ radio modules.

int main()
{
Board::initialize();
MODM_LOG_INFO << "Hello from nrf24-data example" << modm::endl;

initializeNrf();

while (true)
{
Nrf1Data::update();
Nrf2Data::update();

static modm::PeriodicTimer sendTimer{1000};
if (Nrf1Data::Packet packet; sendTimer.execute())
{
static uint8_t counter{0};
packet.setDestination(0x20);
packet.payload[0] = counter++;
Nrf1Data::sendPacket(packet);
MODM_LOG_INFO << "Sending packet " << counter << " to " << packet.getDestination() << modm::endl;
}

if (Nrf2Data::Packet packet; Nrf2Data::getPacket(packet))
{
MODM_LOG_INFO << "Receiving packet " << packet.payload[0] << " from " << packet.getSource() << modm::endl;
}
}

return 0;
}
8 changes: 8 additions & 0 deletions examples/nucleo_f411re/radio/nrf24-data/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<library>
<options>
<option name="modm:build:build.path">../../../../build/nucleo_f411re/radio/nrf24-data</option>
</options>
<modules>
<module>modm:build:scons</module>
</modules>
</library>
60 changes: 60 additions & 0 deletions examples/nucleo_f411re/radio/nrf24-phy-test/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2014, Daniel Krebs
* Copyright (c) 2014, 2017, Sascha Schade
* Copyright (c) 2014-2017, 2019, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include "../radio.hpp"

template< class Phy >
void
test()
{
Phy::setRxAddress(Phy::Pipe::PIPE_0, 0xdeadb33f05);
uint64_t addr = Phy::getRxAddress(Phy::Pipe::PIPE_0);
MODM_LOG_INFO.printf("Setting RX_P0 address to: 0xdeadb33f05\n");
MODM_LOG_INFO.printf("Reading RX_P0 address: 0x%" PRIx32 "%" PRIx32 "\n",
uint32_t((addr >> 32) & 0xffffffff),
uint32_t(addr & 0xffffffff));

Phy::setTxAddress(0xabcdef55ff);
addr = Phy::getTxAddress();
MODM_LOG_INFO.printf("Setting TX address to: 0xabcdef55ff\n");
MODM_LOG_INFO.printf("Reading TX address: 0x%" PRIx32 "%" PRIx32 "\n",
uint32_t((addr >> 32) & 0xffffffff),
uint32_t(addr & 0xffffffff));

uint8_t rf_ch = Phy::readRegister(Phy::NrfRegister::RF_CH);
MODM_LOG_INFO.printf("Expected output for RF_CH: 0x2\n");
MODM_LOG_INFO.printf("Reading RF_CH: 0x%" PRIx8 "\n\n", rf_ch);
}

// Test SPI communication by writing and reading out registers on the
// nRF24L01+ module.
int main()
{
Board::initialize();
MODM_LOG_INFO << "Hello from nRF24-phy-test example" << modm::endl;

initializeSpi();

while (true)
{
MODM_LOG_INFO << "Testing PHY1" << modm::endl;
test<Nrf1Phy>();

MODM_LOG_INFO << "Testing PHY2" << modm::endl;
test<Nrf2Phy>();

modm::delayMilliseconds(1000);
}

return 0;
}
8 changes: 8 additions & 0 deletions examples/nucleo_f411re/radio/nrf24-phy-test/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<library>
<options>
<option name="modm:build:build.path">../../../../build/nucleo_f411re/radio/nrf24-phy-test</option>
</options>
<modules>
<module>modm:build:scons</module>
</modules>
</library>
Loading