Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf_serial_read behaving strangely in polling mode

Hi there

For test purposes, I first implemented a simple serial reading & parsing function in polling mode of a gps device (sending @ 1 Hz).

void read_gps_tag(void)
{
	uint8_t cnt = 0;
	char uart_buf[GPS_NMEA_MAX_SIZE];   // Read UART buffer
	char c;                             // Read UART character
	ret_code_t ret;                     // Return value of the nrf_serial_read function
	bool reading = true;                // Reading flag
	char *p_str;                        // Pointer on the string comparison result
	while(reading) {
		ret = nrf_serial_read(&gps_uart, &c, sizeof(c), NULL, 1000);
		APP_ERROR_CHECK(ret);
		uart_buf[cnt++] = c;
		if(c == GPS_NMEA_STOP_CHAR) {
			if(uart_buf[0] == GPS_NMEA_START_CHAR) {
				static char comp[7] = "$GPRMC";
				p_str = strstr(uart_buf, comp);
				if(p_str != NULL) {
					strcpy(cur_tag.raw_tag, uart_buf);
					cur_tag.length = strlen(uart_buf);
					reading = false;
				}
			}
		cnt = 0;
		}
	}
}

Everything works fine when I call the function directly from the main(), e.g. like:

int main (void)
{
    // ...initializations...
    
    while(true) {
        read_gps_tag();
    }
}

But when I embed the function in the following code, it keeps stuck on reading

int main(void)
{
    // ...initializations...
    
    while (true)
    {
		if(ui_rec_start_req) {
			NRF_LOG_INFO("Start request received");
			card_status = sdc_init();
			if(card_status == RES_OK) {
				NRF_LOG_INFO("SD card init done.");
				ff_result = sdc_mount();
				if(ff_result == FR_OK) {
					NRF_LOG_INFO("SD card mounted.");
					read_gps_tag();
				}
				else {
					NRF_LOG_INFO("SD card init failed. Result: %d", ff_result);
					ui_sdc_init_cnt++;
				}
			}
			else {
				NRF_LOG_INFO("SD card check failed. Status: %d", card_status);
				ui_sdc_init_cnt++;
			}
			if(ui_sdc_init_cnt >= 3) {
				ui_rec_start_req = false;
			}
		}
    }
}

For information: the 'ui_rec_start_req' flag is set by interruption when a button is pressed. Everything works basically fine, except 'read_gps_tag' which stays stuck at 'nrf_serial_read(&gps_uart, &c, sizeof(c), NULL, 1000)'. Any idea?

Thanks in advance,

Sébastien

Related