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

UART resieve interrupt trouble.

Hello. I'm currently developing a software for nRF52832AA device. It needs to communicate with external device via UART. It resieves text message and takes some data from it. I used standart functions from libraries to obtain data from FIFO. So the question is, how can I put the resieve actions to the interrupt? so it won't affect main code?  Inqluding main code. It resieves a parcel and then converts char data to float, then shows it via UART. 

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "app_button.h"
#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf.h"
#include "bsp.h"

//#define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 32                         /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256                           /**< UART RX buffer size. */
#define UART_HWFC APP_UART_FLOW_CONTROL_ENABLED

void uart_error_handle(app_uart_evt_t * p_event)
{
//    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
//    {
//        APP_ERROR_HANDLER(p_event->data.error_communication);
//    }
//    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
//    {
//        APP_ERROR_HANDLER(p_event->data.error_code);
//    }
}

/** @brief Function for setting the @ref ERROR_PIN high, and then enter an infinite loop.
 */
static void show_error(void)
{
    
    LEDS_ON(LEDS_MASK);//All LED on
    while(true)
    {
        // Do nothing
    }


	}

int main(void)
{
    LEDS_CONFIGURE(LEDS_MASK);
    LEDS_OFF(LEDS_MASK);
    uint32_t err_code;
	
	
	
	const app_uart_comm_params_t comm_params =
	{
			RX_PIN_NUMBER,
			TX_PIN_NUMBER,
			RTS_PIN_NUMBER,
			CTS_PIN_NUMBER,
			APP_UART_FLOW_CONTROL_ENABLED,
			false,//parity control 
			UART_BAUDRATE_BAUDRATE_Baud115200
	};
	
	APP_UART_FIFO_INIT(&comm_params,
										 UART_RX_BUF_SIZE,
										 UART_TX_BUF_SIZE,
										 uart_error_handle,
										 APP_IRQ_PRIORITY_LOW,
										 err_code);
	

	APP_ERROR_CHECK(err_code);

	nrf_gpio_cfg_input( BUTTON_1, NRF_GPIO_PIN_PULLUP);	

//-----------Buffer-------------------------------------	
	uint8_t cr[10] = {0};
	uint8_t cr_all [28] = {0};
	uint8_t cr_d [8] = {0};
	uint8_t cr_e [8] = {0};
	uint8_t cr_t [8] = {0};

	uint8_t INFO[7] = {'?','D','A','T','D',0x0D,0x0A};//System message
  float DIST = 000.000F; //Resieved distantion, meters
	float ELEV_ANG = 000.000F;//Resieved angle, grad
	float TILT_ANG = 000.000F;//Resieved tilt angle, grad
  int D_cnt = 0;//Distance counter
  int E_cnt = 0;//Elevation counter
  int T_cnt = 0;//Tilt counter	
//------------------------------------------------------
	  app_uart_put(INFO[0]);
		app_uart_put(INFO[1]);
		app_uart_put(INFO[2]);
		app_uart_put(INFO[3]);
		app_uart_put(INFO[4]);
		app_uart_put(INFO[5]);
		app_uart_put(INFO[6]);
	
while(true)
{

//MC REQUEST

/*if (nrf_gpio_pin_read(BUTTON_1)==0)	
{	
	LEDS_ON(BSP_LED_3_MASK);
    app_uart_put(INFO[0]);
		app_uart_put(INFO[1]);
		app_uart_put(INFO[2]);
		app_uart_put(INFO[3]);
		app_uart_put(INFO[4]);
		app_uart_put(INFO[5]);
		app_uart_put(INFO[6]);

  while (nrf_gpio_pin_read(BUTTON_1)==0);
  LEDS_OFF(BSP_LED_3_MASK);
}*/

//Waiting characters from PC	(Big buffer for preload)
//-----------InfoByte-----------------------------------
	while(app_uart_get(&cr[0]) != NRF_SUCCESS);//+
	if (cr[0]=='+')
	{
			while(app_uart_get(&cr[1]) != NRF_SUCCESS);//D
			while(app_uart_get(&cr[2]) != NRF_SUCCESS);//A
			while(app_uart_get(&cr[3]) != NRF_SUCCESS);//T
			while(app_uart_get(&cr[4]) != NRF_SUCCESS);//D
			while(app_uart_get(&cr[5]) != NRF_SUCCESS);//0D
			while(app_uart_get(&cr[6]) != NRF_SUCCESS);//0A
			while(app_uart_get(&cr[7]) != NRF_SUCCESS);//0D
			while(app_uart_get(&cr[8]) != NRF_SUCCESS);//0A
			while(app_uart_get(&cr[9]) != NRF_SUCCESS);//space bar
			
			LEDS_OFF(LEDS_MASK);

			if ((cr[0] == '+') && (cr[1] == 'D') && (cr[2] == 'A') && (cr[3] == 'T') && (cr[4] == 'D') && (cr[5] == 0x0D) && (cr[6] == 0x0A)) //TRash out
			{
//-----------DISTANTION---------------------------------
			while(app_uart_get(&cr_all[0]) != NRF_SUCCESS);// +/-
			while(app_uart_get(&cr_all[1]) != NRF_SUCCESS);// 100
			while(app_uart_get(&cr_all[2]) != NRF_SUCCESS);// 10
			while(app_uart_get(&cr_all[3]) != NRF_SUCCESS);// 1
			while(app_uart_get(&cr_all[4]) != NRF_SUCCESS);//	.
			while(app_uart_get(&cr_all[5]) != NRF_SUCCESS);//0.1
			while(app_uart_get(&cr_all[6]) != NRF_SUCCESS);//0.01
			while(app_uart_get(&cr_all[7]) != NRF_SUCCESS);//0.001
//-----------ELEVATION_ANGLE---------------------------------		
			while(app_uart_get(&cr_all[8]) != NRF_SUCCESS);// +/-
			while(app_uart_get(&cr_all[9]) != NRF_SUCCESS);// 100
			while(app_uart_get(&cr_all[10]) != NRF_SUCCESS);// 10
			while(app_uart_get(&cr_all[11]) != NRF_SUCCESS);// 1
			while(app_uart_get(&cr_all[12]) != NRF_SUCCESS);//	.
			while(app_uart_get(&cr_all[13]) != NRF_SUCCESS);//0.1
			while(app_uart_get(&cr_all[14]) != NRF_SUCCESS);//0.01
			while(app_uart_get(&cr_all[15]) != NRF_SUCCESS);//0.001
//-----------TILT_ANGLE----------------------------
			while(app_uart_get(&cr_all[16]) != NRF_SUCCESS);// +/-
			while(app_uart_get(&cr_all[17]) != NRF_SUCCESS);// 100
			while(app_uart_get(&cr_all[18]) != NRF_SUCCESS);// 10
			while(app_uart_get(&cr_all[19]) != NRF_SUCCESS);// 1
			while(app_uart_get(&cr_all[20]) != NRF_SUCCESS);//	.
			while(app_uart_get(&cr_all[21]) != NRF_SUCCESS);//0.1
			while(app_uart_get(&cr_all[22]) != NRF_SUCCESS);//0.01
			while(app_uart_get(&cr_all[23]) != NRF_SUCCESS);//0.001
//-----------SPACEBAR------------------------------------
			while(app_uart_get(&cr_all[24]) != NRF_SUCCESS);//0x20
			while(app_uart_get(&cr_all[25]) != NRF_SUCCESS);//0x20
			while(app_uart_get(&cr_all[26]) != NRF_SUCCESS);//OD
			while(app_uart_get(&cr_all[27]) != NRF_SUCCESS);//OA
//--------------TEST------------------------------------

//-----------DISTANTION---------------------------------
			app_uart_put(cr_all[0]);// +/-
			app_uart_put(cr_all[1]) ;// 100
			app_uart_put(cr_all[2]) ;// 10
			app_uart_put(cr_all[3]) ;// 1
			app_uart_put(cr_all[4]) ;//	.
			app_uart_put(cr_all[5]) ;//0.1
			app_uart_put(cr_all[6]) ;//0.01
			app_uart_put(cr_all[7]) ;//0.001
//-----------ELEVATION_ANGLE---------------------------------		
			app_uart_put(cr_all[8]) ;// +/-
			app_uart_put(cr_all[9]) ;// 100
			app_uart_put(cr_all[10]) ;// 10
			app_uart_put(cr_all[11]);// 1
			app_uart_put(cr_all[12]) ;//	.
			app_uart_put(cr_all[13]) ;//0.1
			app_uart_put(cr_all[14]) ;//0.01
			app_uart_put(cr_all[15]) ;//0.001
//-----------TILT_ANGLE----------------------------
			app_uart_put(cr_all[16]) ;// +/-
			app_uart_put(cr_all[17]) ;// 100
			app_uart_put(cr_all[18]) ;// 10
			app_uart_put(cr_all[19]) ;// 1
			app_uart_put(cr_all[20]) ;//	.
			app_uart_put(cr_all[21]) ;//0.1
			app_uart_put(cr_all[22]) ;//0.01
			app_uart_put(cr_all[23]) ;//0.001
//-----------SPACEBAR------------------------------------
			app_uart_put(cr_all[24]) ;//0x20
			app_uart_put(cr_all[25]) ;//0x20
			app_uart_put(cr_all[26]) ;//OD
			app_uart_put(cr_all[27]) ;//OA
//-----------TO CHAR------------------------------------

			while (cr_all[D_cnt] != 0x20)//till not space
			{D_cnt++;}

			while (cr_all[D_cnt + 1 + E_cnt] != 0x20)//till not space
			{E_cnt++;}

			while (cr_all[D_cnt + 1 + E_cnt + 1 + T_cnt] != 0x0D)//till not space
			{T_cnt++;}


			for (int i = 0; i < (D_cnt + 1); i++)
			{
				cr_d[i]=cr_all[i];
			}

			for (int i = D_cnt + 1; i < D_cnt + 1 + E_cnt; i++)
			{
				cr_e[i-(D_cnt + 1)]=cr_all[i];
			}

			for (int i = D_cnt + E_cnt + 2; i < D_cnt + E_cnt + 2 + T_cnt; i++)
			{
				cr_t[i-(D_cnt + E_cnt + 2)]=cr_all[i];
			}
//-----------Conversion to float---------------------------------- 
					if (cr_d[0]== '-')
					{
						if (D_cnt == 8)
						{DIST = (-1)*((cr_d[1]-0x30)*100000 + (cr_d[2]-0x30)*10000 + (cr_d[3]-0x30)*1000 + (cr_d[5]-0x30)*100 + (cr_d[6]-0x30)*10 + (cr_d[7]-0x30)*1); 
						 DIST = DIST / 1000.000F;
						}
						if (D_cnt == 7 )
						{DIST = (-1)*((cr_d[1]-0x30)*10000 + (cr_d[2]-0x30)*1000 + (cr_d[4]-0x30)*100 + (cr_d[5]-0x30)*10 + (cr_d[6]-0x30)*1);
						 DIST = DIST / 1000.000F;
						}
						if (D_cnt == 6)
						{DIST = (-1)*((cr_d[1]-0x30)*1000 + (cr_d[3]-0x30)*100 + (cr_d[4]-0x30)*10 + (cr_d[5]-0x30)*1);
						 DIST = DIST / 1000.000F;
						}				

					}
					else 
					{
						if (D_cnt == 7)
						{DIST = ((cr_d[0]-0x30)*100000 + (cr_d[1]-0x30)*10000 + (cr_d[2]-0x30)*1000 + (cr_d[4]-0x30)*100 + (cr_d[5]-0x30)*10 + (cr_d[6]-0x30)*1);
						 DIST = DIST / 1000.000F;
						}
						if (D_cnt == 6)
						{DIST = ((cr_d[0]-0x30)*10000 + (cr_d[1]-0x30)*1000 + (cr_d[3]-0x30)*100 + (cr_d[4]-0x30)*10 + (cr_d[5]-0x30)*1);
						 DIST = DIST / 1000.000F;
						}
						if (D_cnt == 5)
						{DIST = ((cr_d[0]-0x30)*1000 + (cr_d[2]-0x30)*100 + (cr_d[3]-0x30)*10 + (cr_d[4]-0x30)*1);
						 DIST = DIST / 1000.000F;
						}					
					}
					
					if (cr_e[0]== '-')
					{
						if (E_cnt == 8)
						{ELEV_ANG = (-1)*((cr_e[1]-0x30)*100000 + (cr_e[2]-0x30)*10000 + (cr_e[3]-0x30)*1000 + (cr_e[5]-0x30)*100 + (cr_e[6]-0x30)*10 + (cr_e[7]-0x30)*1);
						 ELEV_ANG = ELEV_ANG / 1000.000F;
						}
						if (E_cnt == 7)
						{ELEV_ANG = (-1)*((cr_e[1]-0x30)*10000 + (cr_e[2]-0x30)*1000 + (cr_e[4]-0x30)*100 + (cr_e[5]-0x30)*10 + (cr_e[6]-0x30)*1);
						 ELEV_ANG = ELEV_ANG / 1000.000F;
						}
						if (E_cnt == 6)
						{ELEV_ANG = (-1)*((cr_e[1]-0x30)*1000 + (cr_e[3]-0x30)*100 + (cr_e[4]-0x30)*10 + (cr_e[5]-0x30)*1);
						 ELEV_ANG = ELEV_ANG / 1000.000F;
						}
					}
					else 
					{
						if (E_cnt == 7)
						{ELEV_ANG = ((cr_e[0]-0x30)*100000 + (cr_e[1]-0x30)*10000 + (cr_e[2]-0x30)*1000 + (cr_e[4]-0x30)*100 + (cr_e[5]-0x30)*10 + (cr_e[6]-0x30)*1);
						 ELEV_ANG = ELEV_ANG / 1000.000F;
						}
						if (E_cnt == 6)
						{ELEV_ANG = ((cr_e[0]-0x30)*10000 + (cr_e[1]-0x30)*1000 + (cr_e[3]-0x30)*100 + (cr_e[4]-0x30)*10 + (cr_e[5]-0x30)*1);
						 ELEV_ANG = ELEV_ANG / 1000.000F;
						}
						if (E_cnt == 5)
						{ELEV_ANG = ((cr_e[0]-0x30)*1000 + (cr_e[2]-0x30)*100 + (cr_e[3]-0x30)*10 + (cr_e[4]-0x30)*1);
						 ELEV_ANG = ELEV_ANG / 1000.000F;	}
						
					}
					
					if (cr_t[0]== '-')
					{
						if (T_cnt == 8)
						{TILT_ANG = (-1)*((cr_t[1]-0x30)*100000 + (cr_t[2]-0x30)*10000 + (cr_t[3]-0x30)*1000 + (cr_t[5]-0x30)*100 + (cr_t[6]-0x30)*10 + (cr_t[7]-0x30)*1);
						 TILT_ANG = TILT_ANG / 1000.000F;
						}
						if (T_cnt == 7)
						{TILT_ANG = (-1)*((cr_t[1]-0x30)*10000 + (cr_t[2]-0x30)*1000 + (cr_t[4]-0x30)*100 + (cr_t[5]-0x30)*10 + (cr_t[6]-0x30)*1);
						 TILT_ANG = TILT_ANG / 1000.000F;
						}
						if (T_cnt == 6)
						{TILT_ANG = (-1)*((cr_t[1]-0x30)*1000 + (cr_t[3]-0x30)*100 + (cr_t[4]-0x30)*10 + (cr_t[5]-0x30)*1);
						 TILT_ANG = TILT_ANG / 1000.000F;
						}

					}
					else 
					{
						if (T_cnt == 7)
						{TILT_ANG = ((cr_t[0]-0x30)*100000 + (cr_t[1]-0x30)*10000 + (cr_t[2]-0x30)*1000 + (cr_t[4]-0x30)*100 + (cr_t[5]-0x30)*10 + (cr_t[6]-0x30)*1);
							 TILT_ANG = TILT_ANG / 1000.000F;
						}
						if (T_cnt == 6)
						{TILT_ANG = ((cr_t[0]-0x30)*10000 + (cr_t[1]-0x30)*1000 + (cr_t[3]-0x30)*100 + (cr_t[4]-0x30)*10 + (cr_t[5]-0x30)*1);
						 TILT_ANG = TILT_ANG / 1000.000F;
						}
						if (T_cnt == 5)
						{TILT_ANG = ((cr_t[0]-0x30)*1000 + (cr_t[2]-0x30)*100 + (cr_t[3]-0x30)*10 + (cr_t[4]-0x30)*1);
						 TILT_ANG = TILT_ANG / 1000.000F;
						}	
					}

	        app_uart_put(INFO[0]);
		      app_uart_put(INFO[1]);
		      app_uart_put(INFO[2]);
		      app_uart_put(INFO[3]);
		      app_uart_put(INFO[4]);
		      app_uart_put(INFO[5]);
		      app_uart_put(INFO[6]); 
					
			}
			else 
			{
				for(int i=0; i<10;i++)
				{
					cr[i]=0;
				}
				for(int i=0;i<28; i++)
				{
					cr_all[i]=0;
				}
			}

			char DIST_TX[8]={0};
			if (DIST>0) 
				{ 
					 double INT = DIST;	
				
					
					 DIST_TX[0]=' ';
					 DIST_TX[1]=INT/100+0x30;
					 INT = fmod(INT,100);
					 DIST_TX[2]=INT/10+0x30;
					 INT = fmod(INT,10);
					 DIST_TX[3]=INT/1+0x30;
					 INT = fmod(INT,1);
					 DIST_TX[4]='.';
					 INT = INT * 100;
					 DIST_TX[5]=INT/10+0x30;
					 INT = fmod(INT,10);
					 DIST_TX[6]=INT/1+0x30;
					 INT = fmod(INT,1);
					 DIST_TX[7] =INT/0.1+0x30;
					 

				 }
			else
				{
					 double INT = DIST;	
				
					 DIST_TX[0]='-';
					 DIST_TX[1]=INT/100-0x30;
					 INT = fmod(INT,100);
					 DIST_TX[2]=INT/10-0x30;
					 INT = fmod(INT,10);
					 DIST_TX[3]=INT/1-0x30;
					 INT = fmod(INT,1);
					 DIST_TX[4]='.';
					 INT = INT * 100;
					 DIST_TX[5]=INT/10-0x30;
					 INT = fmod(INT,10);
					 DIST_TX[6]=INT/1-0x30;
					 INT = fmod(INT,1);
					 DIST_TX[7] =INT/0.1-0x30;
				}
//----------------ELEV_ANG------------------------------
			char ELEV_ANG_TX[6]={0};
			if (ELEV_ANG>0) 
				{ 
					 double INT = ELEV_ANG;	
				
					 ELEV_ANG_TX[0]=' ';

					 ELEV_ANG_TX[1]=INT/1+0x30;
					 INT = fmod(INT,1);
					 ELEV_ANG_TX[2]='.';
					 INT = INT * 100;
					 ELEV_ANG_TX[3]=INT/10+0x30;
					 INT = fmod(INT,10);
					 ELEV_ANG_TX[4]=INT/1+0x30;
					 INT = fmod(INT,1);
					 ELEV_ANG_TX[5] =INT/0.1+0x30;
					 
				}
			else
				{
					 double INT = ELEV_ANG;	
				
					 ELEV_ANG_TX[0]='-';

					 ELEV_ANG_TX[1]=INT/1-0x30;
					 INT = fmod(INT,1);
					 ELEV_ANG_TX[2]='.';
					 INT = INT * 100;
					 ELEV_ANG_TX[3]=INT/10-0x30;
					 INT = fmod(INT,10);
					 ELEV_ANG_TX[4]=INT/1-0x30;
					 INT = fmod(INT,1);
					 ELEV_ANG_TX[5] =INT/0.1-0x30;
					
				}
//------------------------------------------------------
//----------------TILT ANG------------------------------
			char TILT_ANG_TX[6]={0};
			if (TILT_ANG>0) 
				{ 
					 double INT = TILT_ANG;	
				
					 TILT_ANG_TX[0]=' ';

					 TILT_ANG_TX[1]=INT/1+0x30;
					 INT = fmod(INT,1);
					 TILT_ANG_TX[2]='.';
					 INT = INT * 100;
					 TILT_ANG_TX[3]=INT/10+0x30;
					 INT = fmod(INT,10);
					 TILT_ANG_TX[4]=INT/1+0x30;
					 INT = fmod(INT,1);
					 TILT_ANG_TX[5] =INT/0.1+0x30;
					 
				}
			else
				{
					 double INT = TILT_ANG;	
				
					
					 TILT_ANG_TX[0]='-';

					 TILT_ANG_TX[1]=INT/1-0x30;
					 INT = fmod(INT,1);
					 TILT_ANG_TX[2]='.';
					 INT = INT * 100;
					 TILT_ANG_TX[3]=INT/10-0x30;
					 INT = fmod(INT,10);
					 TILT_ANG_TX[4]=INT/1-0x30;
					 INT = fmod(INT,1);
					 TILT_ANG_TX[5] =INT/0.1-0x30;
				}
//------------------------------------------------------				
//-----------Transmitting part of the UART--------------
			if ((cr[0] == '+') && (cr[1] == 'D') && (cr[2] == 'A') && (cr[3] == 'T') && (cr[4] == 'D'))
			{
		    //Message formation
				
				LEDS_ON(LEDS_MASK);
				//printf("LED ON!\r\n");
				app_uart_put(INFO[0]);
				app_uart_put(INFO[1]);
				app_uart_put(INFO[2]);
				app_uart_put(INFO[3]);
				app_uart_put(INFO[4]);

				app_uart_put(' ');
				
				app_uart_put(DIST_TX[0]);
				app_uart_put(DIST_TX[1]);
				app_uart_put(DIST_TX[2]);
				app_uart_put(DIST_TX[3]);
				app_uart_put(DIST_TX[4]);
				app_uart_put(DIST_TX[5]);
				app_uart_put(DIST_TX[6]);
				app_uart_put(DIST_TX[7]);

				app_uart_put(' ');

				app_uart_put(ELEV_ANG_TX[0]);
				app_uart_put(ELEV_ANG_TX[1]);
				app_uart_put(ELEV_ANG_TX[2]);
				app_uart_put(ELEV_ANG_TX[3]);
				app_uart_put(ELEV_ANG_TX[4]);
				app_uart_put(ELEV_ANG_TX[5]);

				app_uart_put(' ');

				app_uart_put(TILT_ANG_TX[0]);
				app_uart_put(TILT_ANG_TX[1]);
				app_uart_put(TILT_ANG_TX[2]);
				app_uart_put(TILT_ANG_TX[3]);
				app_uart_put(TILT_ANG_TX[4]);
				app_uart_put(TILT_ANG_TX[5]);

			}
//Test of succ resieve 
			
			if (DIST == 12.456F)
			{
				LEDS_ON(BSP_LED_0_MASK); 
			}
			
			if (ELEV_ANG == 2.333F)
			{
				LEDS_ON(BSP_LED_1_MASK); 
			}
			
			if (TILT_ANG == -43.456F)
			{
				LEDS_ON(BSP_LED_2_MASK); 
			} 
	
	}
	
 }

}

  • Hello,

    I suggest you study the SDK\examples\ble_peripheral\ble_app_uart example, and how it uses the uart_event_handle() to receive bytes from the UART using interrupts.

    Basically, it is the same as your current uart_error_handle(), except that it also has an event for new data, APP_UART_DATA_READY.

    In your case, it is already set up, but the event handler doesn't check for this event. The reason it also works to use app_uart_get() as a blocking call is that it returns NRF_ERROR_NOT_FOUND until it actually receives a byte. 

    Best regards,

    Edvin

Related