This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

BLE_NUS change UART baudrate at runtime

Can you please help i use ble _ nus and after few message i need to change uart baudrate. I cloes uart then init uart with new baudrate parameter but everytime MCU goes to in error handler function. Where will be best to put  new uart initialization. I know that after i send via bluetooth to nrf52840 0x06 0x39 0x36 0x30 0x30 and nrf52840 forward this message on tx  in next 200 ms device connected to nrf52840s uart will respond on new baudrate which i have send that it changes.

Parents Reply Children
  • Still unclear where, exactly, it is crashing.

    Again, have you tried this alone - without BLE ?

    If you stop the CPU with a breakpoint, that will crash the softdevice - so if you're seeing crashed while debugging that could be a reason ...

  • its not on debugging. i dont get data on my smartphone and nrf52840 get disconnected from smartphone. If i dont send message to change uart everything works well and messages from device goes via UART on nrf and then over BLE to my smartphone and vice versa. 

  • i dont get data on my smartphone

    That could be for any number of reasons - so you need to find out what, exactly, is causing it!

    For that, you will need to debug.

    Again, the easiest approach is to get the baud rate change working and debugged on its own - without the added complications of BLE.

    Once you have the baud rate change working and debugged on its own, then - and only then - move on to adding the BLE stuff.

  •     while (true)
        {
            uint8_t cr;
            while (app_uart_get(&cr) != NRF_SUCCESS);
            while (app_uart_put(cr) != NRF_SUCCESS);
    
    
          
    
            if (cr == 'q' || cr == 'Q')
            {
                printf(" \r\nBAUDRATE 1200\r\n");
                bsp_board_led_invert(0);
                NRF_UART0->BAUDRATE = (UART_BAUDRATE_BAUDRATE_Baud1200 << UART_BAUDRATE_BAUDRATE_Pos);
                
    
              /*  while (true)
                {
                    // Do nothing.
                }*/
            }
        }

    Default baud rate on which app start is 9600 and later i change it to 1200. When i added  NRF_UART0->BAUDRATE = (UART_BAUDRATE_BAUDRATE_Baud1200 << UART_BAUDRATE_BAUDRATE_Pos);  as you say ithat try changing baudrate n uart example without BLE. I can change baudrate  and communicate with device normally on new baud rate which are stored now in NRF_UART0 register.  but when added this feature in BLE UART that doesnt work.

  • OK - so you need to start debugging to fins what it is with the BLE that causes the crash!

    Remember that BLE events come in asynchronously - so you need to be particularly careful that no BLE event tries to use the UART while it is in the middle of being reconfigured ...

Related