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?
Some updates to the issues described in the thread.
This line of code should be deleted. The addComponent() method is already called in the constructor.
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.
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: