lancaster-university / microbit-dal

http://lancaster-university.github.io/microbit-docs
Other
256 stars 130 forks source link

Serial sends \0x00 on init #446

Open microbit-sam opened 5 years ago

microbit-sam commented 5 years ago

Bug Description

When the serial interface is initialised some garbage data is sent out

This is probably to do with the mbed serial driver as a similar problem is described here: https://github.com/bbcmicrobit/micropython/issues/472#issuecomment-334364423

image003 image001

How To Reproduce

#include "MicroBit.h"
MicroBit uBit;
MicroBitSerial serl(P0_24, P0_25);

int main()
{
    // Initialise the micro:bit runtime.
    uBit.init();
    serl.baud(115200);
    uBit.sleep( 2000 );    //delay 2s
    while(1)
        uBit.sleep(100);
    release_fiber();
}

Expected behavior

The initial packet should not be sent

microbit-sam commented 5 years ago

Carlos from other ticket

From our fork of Mbed: https://github.com/lancaster-university/mbed-classic/blob/3d3ff2e7eb9e25edac1dc0784193e440d86aeda6/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/serial_api.c#L76-L78

void serial_init(serial_t *obj, PinName tx, PinName rx) {
    ...
    // dummy write needed or TXDRDY trails write rather than leads write.
    //  pins are disconnected so nothing is physically transmitted on the wire
    obj->uart->TXD = 0;

And from upstream MbedOS the feature has been upgraded to be more robust! https://github.com/ARMmbed/mbed-os/blob/646cc89a620f978ed6174cb7bbb921e0f0d910f2/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/serial_api.c#L79-L84

void serial_init(serial_t *obj, PinName tx, PinName rx) {
    ...
    // dummy write needed or TXDRDY trails write rather than leads write.
    //  pins are disconnected so nothing is physically transmitted on the wire
    obj->uart->PSELTXD = 0xFFFFFFFF;
    obj->uart->EVENTS_TXDRDY = 0;
    obj->uart->TXD = 0;
    while (obj->uart->EVENTS_TXDRDY != 1);

The interesting part is the comment that says pins are disconnected so nothing is physically transmitted on the wire, perhaps we are configuring the pins before the init function does it, or maybe the comment is not correct anymore. We need to compare the entire function from MbedOS as perhaps sending the byte on the wire was a bug.