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

ble_app_uart(SDK 15.0.0)

Hi ,

    I am using the example "ble_app_uart".

    I  enable uart while receiving data from nus handler and turn it off after transmitting.

   After I mondified the code, it can't transmit data through UART.

    But If I enable the uart peripheral while power on and never turn it off, it can work.

   Is there any suggestion to fix this problem?

static void nus_data_handler(ble_nus_evt_t * p_evt)
{
	uint8_t tmp_Status;
	
	if(uart_status == 0 )
	{
		tmp_Status = 0;
		uart_init();
	}
	
	for(uint16_t tmp = 0 ; tmp<0xFF ; tmp++);
	
	if (p_evt->type == BLE_NUS_EVT_RX_DATA)
	{
		uint32_t err_code;

		NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
		NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);

		for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
		{
			do
			{
				err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
				if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
				{
					NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
					APP_ERROR_CHECK(err_code);
				}
			} while (err_code == NRF_ERROR_BUSY);
		}
		if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
		{
			while (app_uart_put('\n') == NRF_ERROR_BUSY);
		}
	}

	if(tmp_Status == 0)
	{
		uart_status = 0;
		app_uart_close();
	}
}

  • Hi 

    Are you disabling the UART to save power, or for some other reason?

    Closing and re-initializing it in the NUS event handler seems a bit drastic, and I don't know if it has been properly tested. 

    It might be a better idea simply to stop the UART RX, if the goal is to keep the power consumption down. 

    Best regards
    Torbjørn

  • Yes, I disable UART while not transmitting or receiving data is for saving power.

    I tried to find the function of "STOP UART RX" or "STOP UART TX".

    But, I can't find that.

    Could you helping me to show more detail or maybe your code. Thanks

  • Hi

    Enabling or disabling the RX is handled by the nrfx_uart_rx_enable(..) and nrfx_uart_rx_disable(..) functions in the nrfx_uart.c driver, but it seems like these functions are not utilized by the app_uart_fifo.c module. 

    In theory you could call them directly from the application, but you need to refer to the UART instance to use these functions, and the UART instance is only available in app_uart_fifo.c. 

    I suggest you make a copy of the app_uart_fifo.c and add functions to disable/enable UART RX, and call the functions I mentioned above from there. 

    Best regards
    Torbjørn 

Related