I want to switch from Arduino Nano BLE 33 to Seeed XIAO nrf52840 (because of size and price). So it is near at hand to use the toolchain with mbed-OS. But my BLE communication (based on Direct Register Programming) does not work with Seeed, though it works with Arduino. So I switched to the toolchain with TinyUSB and indeed, my BLE communication is working, but very very slow. I started with some tests:
First I wanted to compare the mbed-OS toolchains of Arduino and Seeed concerning the timing via micros(), because that is my basic function for time control. Following my test program:
// ----------------------------------------------------------------------------
// T e s t T i m i n g . i n o
// ----------------------------------------------------------------------------
// Simple program to test the timing of Arduino boards (and compatibles)
// Information about the used functions comes from ARDUINODOCS
//
#include "Arduino.h"
#ifdef UseTinyUSB
#include <Adafruit_TinyUSB.h>
#endif
#define macroMics micros()
// Using a macro for the call to <micros()> to test own routines instead later
unsigned long loopStartMics = 0; // Will be set beginning of loop
unsigned long loopEndMics = 0; // Will be set end of loop and in setup
unsigned long oldStartMics = 0; // To check the whole cycle
bool togglePrint = false;
// Being prepared to measure loop time without serial communication
// for finer measurements of cycle times
// Some configurations
//
void setup()
{
//Initialize serial and wait for port to open:
Serial.begin(115200); // Bitrate is not relevant for USB CDC
while (!Serial) { ; } // wait for serial port to connect. Needed for native USB
loopEndMics = macroMics; // Preset for end of loop
}
// The loop function is called in an endless loop
// So this is the function (entry) for the user and its timing will be relevant
//
void loop()
{
oldStartMics = loopStartMics;
// --------------------------------------------------------------------------
// Testing the behaviour of <micros()>
// --------------------------------------------------------------------------
// To test the timing you need a function returning a time with a high
// resolution. On Arduino this the function <micros()>
//
loopStartMics = macroMics; // Time of entering the loop
unsigned long loopOutMics = loopStartMics - loopEndMics;
// Time outside the loop, it is the cycle time for empty loops
// Measure the time of running micros itself
//
for(int i = 0; i < 1000; i++)
macroMics;
unsigned long microsCallTime = macroMics - loopStartMics;
// this is the delay caused by using <micros()> muliplied with 1000
// and neglecting the delay caused by the for-loop and the 2 extra <micros()>.
loopEndMics = macroMics;
// --------------------------------------------------------------------------
// Printing the result via Serial
// --------------------------------------------------------------------------
// Printing every 2. loop-cycle to measure loopOutMics without influence by
// delay of printing and <delay(1000)>
if(togglePrint)
{
Serial.print("1000x micros = ");
Serial.print(microsCallTime);
Serial.print(" Out of loop time = ");
Serial.println(loopOutMics);
delay(1000);
togglePrint = false;
}
else
togglePrint = true;
}
With this test I found, that from the view of using micros(), Arduino and Seeed are identical. Calling micros() takes 8.6 microseconds (it will be less because the for-loop is included here) and the delay outside loop() is about 9 microseconds. So an empty loop() would be repeated with 100 kHz. No problems for my software, because my high speed state machines run with 10 kHz. My problem with the BLE communication on Seeed will have other reasons.
This test running with the TinyUSB (not mbed-OS, but some RTOS seems to be used) gives the surprising result, that no time is needed for the call of micros() and for the delay outside loop() (0 microseconds). Simply, micros() is always (in the slots of my testing) returning the same value. So time difference is zero. Estimating from the speed of my BLE software, micros() seems to change its return value only every millisecond. Hopefully the value is then increased by 1000.
Because my BLE is at least running, my next step is to write my own micros() routine based on using a timer of the nRF52840 (of course with direct register access). Currently I have no idea, why my BLE is running on the Arduino but not on the Seeed (both using mbed-OS and having the same timing with micros()). So far I will continue with TinyUSB.


