nRF5 action!
-
@d00616 said in nRF5 Bluetooth action!:
In the ESB code, I use the bitcounter event to start the rssi sample task via PPI. The results are looking plausible.
Do you know of any good PPI tutorials? The datasheet seems awfully skimpy on its explanation of exactly how to use it.
Right now I have RSSI triggers working on the receiver (presently using the MCU, not PPI), but it takes 1400 samples to guarantee not missing any transmissions. That's because of the gap between single shot packets when they get sent. If I can reduce that to one sample, by finding a way to make a transmitter transmit continuously, then that will save a lot of energy on the receiver.
There is a way to do more rapid fire transmission of packets, so that would be the fall-back plan if I can't find a way to, for example, send a continuous preamble.
@NeverDie Thank you sharing your experience here. It helps me of better understanding some nRF5 internals. It would be awesome if you share your code.
@NeverDie said in nRF5 Bluetooth action!:
Do you know of any good PPI tutorials? The datasheet seems awfully skimpy on its explanation of exactly how to use it.
To understand PPI you have to be in mind that nearly everything is driven by tasks and events. If you want to do something, you have to start a task like 'NRF_RADIO->TASKS_TXEN=1'. If a task ends it generates an event like 'NRF_RADIO->EVENTS_READY'. You can replace NRF_RADIO with another periphery the registers have an equal naming scheme.
For an event an Interrupt can be enabled with the NRF_RADIO->INTENSET register. Each event correspondents with a bit in that register. In an interrupt, you have to reset the NRF_RADIO->EVENTS_READY register to 0 to allow triggering a new interrupt. For compatibility, you can use the NRF_RESET_EVENT macro in interrupts. This reads back the register on nRF52 to avoid caching effects. Interrupts doesn't matter for PPI :-)
The next fine thing are Shortcuts. Shortcuts are limited to the same peripheral unit. Bits in the NRF_RADIO->SHORTS register are corresponding to a connection between an event and a test. If the event is triggered the task is started. This allows to trigger things like send an packet after the radio is ready. To use this, you have to enable the shortcut in the NRF_RADIO->SHORTS register.
To break the limits of shortcuts, there is the PPI unit with 32 channels. Some of the channels are predefined but interesting to see how things are implemented with BLE. The other PPI channels are flexible. To use one of these channels, you have to write a pointer of your event register, like '(uint32_t)&NRF_RADIO->EVENTS_END' to the NRF_PPI->CH[YOUR_CHANNEL].EEP register and a pointer to your task you want to start in the NRF_PPI->CH[YOUR_CHANNEL].TEP register like '(uint32_t)&NRF_TIMER0->TASKS_START'. Then you have to enable the PPI channel by setting the corresponding bit like 'NRF_PPI->CHENSET |= (1 << COUR_CHANNEL)' that's all.
The nRF52, but not the nRF52 comes with NRF_PPI->FORK[YOUR_CHANNEL].TEP registers. in my reading you can start a second task with this register like writing to NRF_PPI->CH[YOUR_CHANNEL].TEP.
I have no idea about using the PPI Groups.
Arudino provides a PPI library for the primo: http://cdn.devarduino.org/learning/reference/ppi I think we have to use this library to be compatible in the future. I hope there is a chance to port things to arduino-nrf5 back.
Edit: The arduino PPI library is not flexible enough to support radio events. :-(
-
Thanks! At least notionally, the PPI sounds excellent. Presently, if I want to move the radio into a particular state which takes a few state changes to get there, using the MCU with a conservative coding style, I have to initiate the first state change, then busy-wait until the new state is confirmed, then make the next state-change, etc. It sounds as though the PPI is a good fit for this, because it would eliminate the busy-waits. It would automatically transition from one state to the next using just the interrupt scheme you outlined until the target state is reached. Well, at least in theory. Meanwhile the CPU could be doing other things or sleeping. This does sound like a definite improvement, especially for more efficient control over the radio. :)
-
@NeverDie Thank you sharing your experience here. It helps me of better understanding some nRF5 internals. It would be awesome if you share your code.
@NeverDie said in nRF5 Bluetooth action!:
Do you know of any good PPI tutorials? The datasheet seems awfully skimpy on its explanation of exactly how to use it.
To understand PPI you have to be in mind that nearly everything is driven by tasks and events. If you want to do something, you have to start a task like 'NRF_RADIO->TASKS_TXEN=1'. If a task ends it generates an event like 'NRF_RADIO->EVENTS_READY'. You can replace NRF_RADIO with another periphery the registers have an equal naming scheme.
For an event an Interrupt can be enabled with the NRF_RADIO->INTENSET register. Each event correspondents with a bit in that register. In an interrupt, you have to reset the NRF_RADIO->EVENTS_READY register to 0 to allow triggering a new interrupt. For compatibility, you can use the NRF_RESET_EVENT macro in interrupts. This reads back the register on nRF52 to avoid caching effects. Interrupts doesn't matter for PPI :-)
The next fine thing are Shortcuts. Shortcuts are limited to the same peripheral unit. Bits in the NRF_RADIO->SHORTS register are corresponding to a connection between an event and a test. If the event is triggered the task is started. This allows to trigger things like send an packet after the radio is ready. To use this, you have to enable the shortcut in the NRF_RADIO->SHORTS register.
To break the limits of shortcuts, there is the PPI unit with 32 channels. Some of the channels are predefined but interesting to see how things are implemented with BLE. The other PPI channels are flexible. To use one of these channels, you have to write a pointer of your event register, like '(uint32_t)&NRF_RADIO->EVENTS_END' to the NRF_PPI->CH[YOUR_CHANNEL].EEP register and a pointer to your task you want to start in the NRF_PPI->CH[YOUR_CHANNEL].TEP register like '(uint32_t)&NRF_TIMER0->TASKS_START'. Then you have to enable the PPI channel by setting the corresponding bit like 'NRF_PPI->CHENSET |= (1 << COUR_CHANNEL)' that's all.
The nRF52, but not the nRF52 comes with NRF_PPI->FORK[YOUR_CHANNEL].TEP registers. in my reading you can start a second task with this register like writing to NRF_PPI->CH[YOUR_CHANNEL].TEP.
I have no idea about using the PPI Groups.
Arudino provides a PPI library for the primo: http://cdn.devarduino.org/learning/reference/ppi I think we have to use this library to be compatible in the future. I hope there is a chance to port things to arduino-nrf5 back.
Edit: The arduino PPI library is not flexible enough to support radio events. :-(
@d00616 said in nRF5 Bluetooth action!:
@NeverDie Thank you sharing your experience here. It helps me of better understanding some nRF5 internals. It would be awesome if you share your code.
Here it is:
#include <MySensors.h> #include <nrf.h> void setup() { NRF_POWER->DCDCEN=1; //enable the DCDC voltage regulator as the default. //guarantee RESET pin is working if (((NRF_UICR-> PSELRESET[0])==0xFFFFFFFF) && ((NRF_UICR-> PSELRESET[1])==0xFFFFFFFF)) { //if the two RESET registers are erased NRF_NVMC->CONFIG=1; // Write enable the UICR NRF_UICR-> PSELRESET[0]=21; //designate pin P0.21 as the RESET pin NRF_UICR-> PSELRESET[1]=21; //designate pin P0.21 as the RESET pin NRF_NVMC->CONFIG=0; // Put the UICR back into read-only mode. } NRF_RADIO->FREQUENCY=123; NRF_RADIO->MODE=2; //set 250kbps datarate. May as well stretch out the NULL packet as much as possible. NRF_RADIO->TASKS_DISABLE=1; //turn-off the radio to establish known state. while (NRF_RADIO->STATE!=0) {} //busy-wait until radio is disabled NRF_RADIO->TASKS_TXEN=1; //wake-up the radio while ((NRF_RADIO->STATE)!=10) {} //busy-wait until radio has started TXIDLE //Assertion: radio is now in TXIDLE state. } void loop() { //assume radio is in TXIDLE state. NRF_RADIO->TASKS_START=1; //Move from TXIDLE state to TX state. This sends a NULL packet. while ((NRF_RADIO->STATE)!=11) {} //busy-wait until radio is in TX state while ((NRF_RADIO->STATE)==11) {} //busy-wait until radio is back to TXIDLE state //Assertion: radio is now back to TXIDLE state } -
So, to make the above code work as a PPI, all I would need is some kind of linkage such that whenever the "event" of TXIDLE occurs, then a "task" (in this case it would be TASKS_START) is executed to move the radio back into the TX state.
Hmmm.. Still not obvious though from just the datasheet how to actually setup even that simple linkage.
-
So, to make the above code work as a PPI, all I would need is some kind of linkage such that whenever the "event" of TXIDLE occurs, then a "task" (in this case it would be TASKS_START) is executed to move the radio back into the TX state.
Hmmm.. Still not obvious though from just the datasheet how to actually setup even that simple linkage.
@NeverDie said in nRF5 Bluetooth action!:
So, to make the above code work as a PPI, all I would need is some kind of linkage such that whenever the "event" of TXIDLE occurs, then a "task" (in this case it would be TASKS_START) is executed to move the radio back into the TX state.
This is a use case for shortcuts. PPI is not required.
There is no TXIDLE event but looking at the state diagram is TXIDLE a result of ether READY or END event. You can enable following shortcurts:
NRF_RADIO->SHORTS = RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_START_Msk;In PPI this should be the code (untested):
#define CHANNEL (1) NRF_PPI->CH[CHANNEL].EEP = (uint32_t)&NRF_RADIO->EVENTS_END; NRF_PPI->CH[CHANNEL].TEP = (uint32_t)&NRF_RADIO->TASKS_START; NRF_PPI->CH[CHANNEL+1].EEP = (uint32_t)&NRF_RADIO->EVENTS_READY; NRF_PPI->CH[CHANNEL]+1.TEP = (uint32_t)&NRF_RADIO->TASKS_START; NRF_PPI->CHENSET = (1 << CHANNEL) | (1 <<( CHANNEL+1)); -
Thanks! That helps my understanding quite a bit. I've tested the following shortcut code, and it works:
#include <MySensors.h> #include <nrf.h> void setup() { NRF_POWER->DCDCEN=1; //enable the DCDC voltage regulator as the default. //guarantee RESET pin is working if (((NRF_UICR-> PSELRESET[0])==0xFFFFFFFF) && ((NRF_UICR-> PSELRESET[1])==0xFFFFFFFF)) { //if the two RESET registers are erased NRF_NVMC->CONFIG=1; // Write enable the UICR NRF_UICR-> PSELRESET[0]=21; //designate pin P0.21 as the RESET pin NRF_UICR-> PSELRESET[1]=21; //designate pin P0.21 as the RESET pin NRF_NVMC->CONFIG=0; // Put the UICR back into read-only mode. } NRF_RADIO->FREQUENCY=123; NRF_RADIO->MODE=2; //set 250kbps datarate. May as well stretch out the NULL packet as much as possible. NRF_RADIO->TASKS_DISABLE=1; //turn-off the radio to establish known state. while (NRF_RADIO->STATE!=0) {} //busy-wait until radio is disabled NRF_RADIO->SHORTS = B100001; //Implement shortcuts: READY_START and END_START NRF_RADIO->TASKS_TXEN=1; //wake-up the radio transmitter and move it into state TXIDLE. //The shortcuts will take-over the moment the state TXIDLE becomes activated. } void loop() { } -
Is there any example code which illustrates the use of interrupts on the nRF52832?
-
Thanks! That helps my understanding quite a bit. I've tested the following shortcut code, and it works:
#include <MySensors.h> #include <nrf.h> void setup() { NRF_POWER->DCDCEN=1; //enable the DCDC voltage regulator as the default. //guarantee RESET pin is working if (((NRF_UICR-> PSELRESET[0])==0xFFFFFFFF) && ((NRF_UICR-> PSELRESET[1])==0xFFFFFFFF)) { //if the two RESET registers are erased NRF_NVMC->CONFIG=1; // Write enable the UICR NRF_UICR-> PSELRESET[0]=21; //designate pin P0.21 as the RESET pin NRF_UICR-> PSELRESET[1]=21; //designate pin P0.21 as the RESET pin NRF_NVMC->CONFIG=0; // Put the UICR back into read-only mode. } NRF_RADIO->FREQUENCY=123; NRF_RADIO->MODE=2; //set 250kbps datarate. May as well stretch out the NULL packet as much as possible. NRF_RADIO->TASKS_DISABLE=1; //turn-off the radio to establish known state. while (NRF_RADIO->STATE!=0) {} //busy-wait until radio is disabled NRF_RADIO->SHORTS = B100001; //Implement shortcuts: READY_START and END_START NRF_RADIO->TASKS_TXEN=1; //wake-up the radio transmitter and move it into state TXIDLE. //The shortcuts will take-over the moment the state TXIDLE becomes activated. } void loop() { }Looks as though it should be possible to send tightly packed meaningful packets, not just null packets, using almost the same methodology.
-
@NeverDie said in nRF5 Bluetooth action!:
Is there any example code which illustrates the use of interrupts on the nRF52832?
Yes. In a sketch, you have to put the interrupt routine into one line. You can define the interrupt only once. If you want to use the radio ISR, you can't enable the radio in MySensors.
https://github.com/sandeepmistry/arduino-nRF5/issues/52
https://github.com/mysensors/MySensors/blob/development/drivers/NRF5/Radio_ESB.cpp#L500
-
Interestingly enough, it turns out all I need to do is transmit one packet, and afterward just leave the radio in TXIDLE mode. That's because, as indicated in the datasheet, it transmits a carrier wave of one's (or any pattern you program) after the packet, expecting that another packet will be sent soon. This is illustrated in Figure 37 of the DS.
-
So, I've got the transmit side of this problem figured out. Next up: the receiver side, which already works using the MCU.
The next step will be to see whether I can setup timed events from the RTC which can be used to trigger the PPI to measure the RSSI without waking up the MCU. Also, I'll need some way for the PPI to evaluate the magnitude of the RSSI without involving the MCU. Ideally it would also trigger a Rx sequence if the RSSI is above threshold and wake the MCU if something gets received. Not sure how much of this will be possible, but that's the wish list.I'd say the energy consumption is already pretty good after switching to the RSSI paradigm, but if this succeeds, then it may cut what remains of the energy consumption roughly in half. At that point, I think we will have wrung just about every possible bit of efficiency out of this radio, with the remaining to-do's as mostly mop-up and maybe some fine tuning (e.g. to better mitigate against false positives on the RSSI threshhold trigger).
-
So, I figure the way to get started is to do something "easy", like maybe use the PPI to blink an LED.
We want the lower power RTC, not the system clock. We want to use the RTC TICK event, so that the mpu can be powered down while the PPI is running.
So, because I want a timer event every 100ms, that means the prescaler should be 3276.
-
So, just starting on this, where I'm at is:
#include <nrf.h> #include <MySensors.h> #define LED_PIN 18 bool toggle=false; //track whether or not to toggle the LED pin void setup() { NRF_CLOCK->LFCLKSRC=1; //use the crystal oscillator. NRF_CLOCK->TASKS_LFCLKSTART=1; //start the crystal oscillator clock while (!(NRF_CLOCK->EVENTS_LFCLKSTARTED)) {} //busy-wait until the clock is confirmed started. NRF_RTC1->TASKS_STOP=1; //stop the RTC so that we can set the prescaler NRF_RTC1->PRESCALER=3276; //once per 100ms NRF_RTC1->TASKS_START=1; //start the RTC so that we can start getting TICK events hwPinMode(LED_PIN,OUTPUT_H0H1); //establish P0.18 as the LED pin. } void loop() { if (NRF_RTC1->EVENTS_TICK) { toggle=!toggle; digitalWrite(LED_PIN,toggle); } }Unfortunately, this does not work because (NRF_RTC1->EVENTS_TICK) always reads as zero. Not sure why(?).
-
This whole topic has been brewing in the back of my mind for a couple years now:
https://forum.mysensors.org/topic/1788/nrf51822-as-an-all-in-one
and
https://forum.mysensors.org/topic/3836/anyone-besides-me-looking-into-long-range-bluetooth-for-their-wireless-nodesWith the nRF52840, it looks as though the moment has finally arrived to tie it all together and give it a try. :)
I just have no idea where to even begin though. Just order the nRF52840 preview DK? Is it fairly quick to get something up and running, or is it a fairly steep learning curve?
-
@NeverDie
nrf52832 is definitely better than nrf24l01. if i'm not wrong, 4db can double range in theory.
For range, an important point is the antenna, as you already know.
Chip antenna can be ok, depending on the environment and usecase, but can't compete with a rfm69. These antennas are not for long range, so the adafruit board. How to miniaturize antennas without loosing performance.. -
@Jokgi
of course I agree !
that's why in the past i preferred rfm69 modules (better range of course, but more power hungry). 832 being better than nrf24, i'm now using it. And I also like the 840dk (neat package) :+1:
That said, if i remember well, nrf52832 is not fully BLE5 long range compatible as 840 is. -
@NeverDie you will need long range capabilities on both sides of the link. So two preview kits work great. Long range is supported by SDK 14 and the current softdevice.
@Jokgi said in nRF5 Bluetooth action!:
@NeverDie you will need long range capabilities on both sides of the link. So two preview kits work great. Long range is supported by SDK 14 and the current softdevice.
I'm not disagreeing, but presently modules for it (other than the preview DK) aren't yet available. Meanwhile, hopefully nearly all of what's being learned here about the nRF52832 will be of direct relevance. For instance: PPI.
-
So, just starting on this, where I'm at is:
#include <nrf.h> #include <MySensors.h> #define LED_PIN 18 bool toggle=false; //track whether or not to toggle the LED pin void setup() { NRF_CLOCK->LFCLKSRC=1; //use the crystal oscillator. NRF_CLOCK->TASKS_LFCLKSTART=1; //start the crystal oscillator clock while (!(NRF_CLOCK->EVENTS_LFCLKSTARTED)) {} //busy-wait until the clock is confirmed started. NRF_RTC1->TASKS_STOP=1; //stop the RTC so that we can set the prescaler NRF_RTC1->PRESCALER=3276; //once per 100ms NRF_RTC1->TASKS_START=1; //start the RTC so that we can start getting TICK events hwPinMode(LED_PIN,OUTPUT_H0H1); //establish P0.18 as the LED pin. } void loop() { if (NRF_RTC1->EVENTS_TICK) { toggle=!toggle; digitalWrite(LED_PIN,toggle); } }Unfortunately, this does not work because (NRF_RTC1->EVENTS_TICK) always reads as zero. Not sure why(?).
@NeverDie said in nRF5 Bluetooth action!:
Unfortunately, this does not work because (NRF_RTC1->EVENTS_TICK) always reads as zero. Not sure why(?).
It should be working, but it isn't. Nor do I see a way to check it with an oscilliscope. So, my current theory is that it gets set but cleared so quickly that it can't be read by the MCU. So, the next step will be to assume that it is, in fact, working, and to use it as a PPI trigger, which is what this is all building toward anyway.
On the other hand, perhaps there's an easy way to have the EVENTS_TICK set an interrupt bit, which would persist until it was cleared? Hmmm. No, not quite, but there is INTENSET, which will set an interrupt on an EVENT_TICK. That will do. Exactly which interrupt gets triggered though? Figure 46 shows that an IRQ signal is sent to NVIC ( the Nested Vectored Interrupt Controller). According to the table in sectoin 7.3, the NVIC has 37 interrupt vectors. According to section 15.8:
A peripheral only occupies one interrupt, and the interrupt number follows the peripheral ID. For example, the
peripheral with ID=4 is connected to interrupt number 4 in the Nested Vectored Interrupt Controller (NVIC).So, based on that, we need to know the ID number for the RTC, and then we'll know which interrupt number to track. According to Table 10, the Peripheral ID for the RTC is 11 (well, at least it is for the RTC0, so I will recode to use RTC0 instead of RTC1).
Now, according to Table 10, the memory location that corresponds to Peripheral ID 11 is 0x4000B000. Therefore, it is this memory location we need to examine to know if a TICK interrupt has occured.
-
Close, but no cigar. What I found out is that if I set the TICK interrupt with:
NRF_RTC0->INTENSET=1; //set the TICK interrupt bitthen at the following memory addresses, the value stored there immediately becomes 1:
4000B300
4000B304
4000B308and if I clear the TICK interrupt with:
NRF_RTC0->INTENCLR=1; //clear the TICK interrupt bitthen the values at those same memory addresses immediately becomes zero. I can toggle back and forth as much as I want, and this is always true.
However, none of this is telling me whether the TICK interrupt has actually triggered. Where do I find that?
Based on the current pre-scaler, COUNTER increments once every TICK (i.e. once every 100ms). However, is there an actual TICK flag somewhere that goes high at those times and then low again after getting cleared? Or, is it only accessible indirectly by using PPI?
-
OK, I came up with a simple equivalent. Basically, every time COUNTER is incremented, I reset it to zero. It then effectively acts much the way a TICK should. For whatever reason, once EVENTS_TICK goes high, it just stays high forever. So, it doesn't seem very useful per se, though maybe there's a way to clear it that I haven't yet found.