7 years, 6 months ago.

nrf51822 never sleeps with Serial declared

Hi,

I have Seeed Tiny_BLE board with power monitoring circuit.

  • Q1) How to turn off Serial port correctly?
  • Q2) When compiler finds Serial initialization (see code below) it never put device to sleep (current consumtion about 1mA) . Even if this part of code will not be called. If I comment pc = new Serial(UART_TX, UART_RX); current cosumption is ok (10-20uA). So the question: is it possible to use sleep mode together with Serial port object?

include the mbed library with this snippet

#include "mbed.h"
#include "BLE.h"

#define LED_BLUE    p23
#define BUTTON_PIN  p17

#define UART_TX     p9
#define UART_RX     p11

DigitalOut blue(LED_BLUE);
Serial *pc  = NULL;

BLE  ble;
volatile bool bleIsConnected = false;
InterruptIn button(BUTTON_PIN); 

void startUart()
{
    pc = new Serial(UART_TX, UART_RX);
} 
void detect(void)
{
    blue = !blue;
    if (pc!=NULL) 
        startUart();
    else
        pc = NULL;
}

int main(void)
{
    blue  = 1;
    button.fall(detect);
    ble.init();
    ble.setAdvertisingInterval(800); /* 100ms; in multiples of 0.625ms. */
    ble.gap().startAdvertising();
    while (true) {        ble.waitForEvent();  }
}

1 Answer

7 years, 6 months ago.

Hi Klym,

It is not possible to put the serial to sleep with mbed primitives. This is a known issue (see here) which has to be resolved.

You can directly disable it as a workaround:

// include definitions
#include "device.h"

// disable the UART: 
((NRF_UART_Type *) UART0)->ENABLE = (UART_ENABLE_ENABLE_Disabled<< UART_ENABLE_ENABLE_Pos);


// enable the UART: 
((NRF_UART_Type *) UART0)->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);

Accepted Answer

Vincent,

thanks for info. Seems this code is working but there is one condition: UART should finish to transmit all data before 'Disable'. Otherwise, for example if you have pc.printf("something"); disbaleUart(); UART will not work after wake up.

What I can also find, possible it's the best way:

//to disable UART
   NRF_UART0->TASKS_SUSPEND=1;  
//to re-enable UART
     NRF_UART0->ENABLE        = UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos;
     NRF_UART0->TASKS_STARTTX = 1;
     NRF_UART0->TASKS_STARTRX = 1;
posted by Klym T 24 Nov 2016