Codal-microbit-v2: How to instantiate & use a second serial?

Hi!

I am planning to create an extension targetting Micro:bit v2 which would require a second serial interface. As a quick test I had a simple C++ program built against codal-microbit-v2 as following:

#include “MicroBit.h”
#include “samples/Tests.h”

MicroBit uBit;

int main()
{

uBit.init();

// out_of_box_experience();

codal::MicroBitIO io = uBit.io;
NRF52Serial serial2(io.P13, io.P14, NRF_UARTE1);
serial2.init();
NVIC_SetPriority(UARTE1_IRQn, 2); // Serial port
serial2.addComponent();

while (1)
{
    serial2.sendChar('!');
    uBit.sleep(1000);
}

microbit_panic(999);

}

However a HardFault was caught upon the 1st char was sent. Could someone please help?

–
Muz

Some updates to the issues described in the thread.

  1. This line of code should be deleted. The addComponent() method is already called in the constructor.
  1. I found that if I remove all the code that causes fiber to go into sleep, like the following code, then the program works fine.
while (1)
{
    serial2.send("hello!\n", codal::SYNC_SPINWAIT);
    uBit.wait(1000);
}

However, I think this just avoids the real problem, and doesn’t solve it. I expect the second serial instance to fit into the Micro:bit runtime and work perfectly fine, including when the device goes into a low-power sleep state.

Any help would be appreciated!

–
Muz

I managed to get the program to work, although I still don’t understand the source of the problem, like - as the old Chinese saying goes - a blind cat catches a dead mouse (瞎猫抓到死耗子). Anyway, here’s my modified program code:

#include “MicroBit.h”
#include “samples/Tests.h”

MicroBit uBit;
NRF52Serial serial2(uBit.io.P13, uBit.io.P14, NRF_UARTE1);

int main()
{

NVIC_SetPriority(UARTE1_IRQn, 2); // Serial port

uBit.init();

while (1)
{
    serial2.send("hello!\n");
    uBit.sleep(1000);
}

microbit_panic(999);

}

I hope this will inspire someone in the same situation as me.

Of course, I’d be grateful if someone with bright eyes could tell me the full picture…

–
Muz