Files
MySensors/drivers/NRF5/Radio.cpp
T
d00616 726a20dad9 Add nrf5 platform (#830)
* Add NVRAM support

This is an EEPROM replacement based on MCU's internal Flash memory.
The NVRAM class is designed to be stateless. Each operation is
written to flash immediately.

The last 16k(nRF51)/32k(nRF52) of Flash memory are managed by the
VirtualPage class.  This class allows writing a single emulated 4k
page up 40,000 times.

The NVRAM class is a log based byte-wise storage allowing up to
40,000,000 writes depending on the number of used addresses.  Each
four allocated addresses from the beginning of the NVRAM reduce the
write log by one.

When first 256 bytes of the emulated storage are used, there is an
average writing capacity of 145,000 cycles per byte.

When the write log is full, the NVRAM is compressed into a new flash
page. After a new page is ready, the old page is released. This
process can take up to 4500 ms and cannot be interrupted.

The NVRAM functionality can easily be ported to ESP8266 or SAMD by
providing a Flash interface.

* Add NRF5 platform support

This is the support of the Nordic Semiconductor NRF51 (nrf51822,
nrf51422) and NRF52 (nrf52832) platform. Based on arduino-nRF5
(https://github.com/sandeepmistry/arduino-nRF5).

The included radio driver is compatible with the NRF24 devices.
Using a SoftDevice (BLE)is not supported and must be removed before
flashing MySensors with a "mass_erase" operation.
2017-05-14 11:53:01 +02:00

89 lines
2.3 KiB
C++

#include "drivers/NRF5/Radio.h"
int16_t NRF5_getTxPowerPercent(void)
{
// NRF5_PA_MAX = 100% NRF5_PA_MIN=0%
int16_t dbm = NRF5_getTxPowerLevel();
int16_t dbm_diff = ((int8_t)NRF5_PA_MAX-(int8_t)NRF5_PA_MIN);
int16_t dbm_min = (int8_t)NRF5_PA_MIN;
return ((dbm-dbm_min)*100)/dbm_diff;
}
int16_t NRF5_getTxPowerLevel(void)
{
return (int8_t)NRF_RADIO->TXPOWER;
}
bool NRF5_setTxPowerPercent(const uint8_t powerPercent)
{
/* Current mapping:
* NRF51/NRF52822:
* 0.. 2% -> -40dBm (0%)
* 3..56% -> -16dBm (54%)
* 57..65% -> -12dBm (63%)
* 66..72% -> -8dBm (72%)
* 75..84% -> -4dBm (81%)
* 85..95% -> 0dBm (90%)
* NRF51 96..100%-> 4dBm (100%)
* NRF52 96..99% -> 3dBm (97%)
* NRF52 100% -> 4dBm (100%)
*/
// Calculate dbm level
int16_t dbm_diff = ((int8_t)NRF5_PA_MAX-(int8_t)NRF5_PA_MIN);
int16_t dbm_min = (int8_t)NRF5_PA_MIN;
int8_t dbm = ((dbm_diff * powerPercent)/100)+dbm_min;
if (dbm >= (int8_t)NRF5_PA_MAX) {
NRF_RADIO->TXPOWER = NRF5_PA_MAX;
return true;
}
if (dbm > 1) {
#ifdef RADIO_TXPOWER_TXPOWER_Pos2dBm
// NRF52840
NRF_RADIO->TXPOWER = dbm;
#elif defined(NRF51)
// nRF51x22
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Pos4dBm;
#else
// NRF52822
if (dbm > 3) {
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Pos4dBm;
} else {
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Pos3dBm;
}
#endif
return true;
}
if (dbm > (int8_t)RADIO_TXPOWER_TXPOWER_Neg4dBm) {
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_0dBm;
return true;
}
if (dbm > (int8_t)RADIO_TXPOWER_TXPOWER_Neg4dBm) {
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_0dBm;
return true;
}
if (dbm > (int8_t)RADIO_TXPOWER_TXPOWER_Neg8dBm) {
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Neg4dBm;
return true;
}
if (dbm > (int8_t)RADIO_TXPOWER_TXPOWER_Neg12dBm) {
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Neg8dBm;
return true;
}
if (dbm > (int8_t)RADIO_TXPOWER_TXPOWER_Neg16dBm) {
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Neg12dBm;
return true;
}
if (dbm > (int8_t)RADIO_TXPOWER_TXPOWER_Neg20dBm) {
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Neg16dBm;
return true;
}
if (dbm > (int8_t)NRF5_PA_MIN) {
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Neg16dBm;
return true;
}
NRF_RADIO->TXPOWER = NRF5_PA_MIN;
return true;
}