AnsweredAssumed Answered

IRDA Communication Problem - Unreasonable Recieved Data

Question asked by selçuk Yolcu on Mar 23, 2018
Latest reply on Mar 26, 2018 by selçuk Yolcu

Hi,

    I'm trying to use simple IRDA IT communication.  I am using Interrupt to receive data. I set for 7 bytes to receive data and when 6th array  character is taken, I transmit data. But with second cycle, the received data is crashed by somethings. If receive data is used alone without transmit, then there is no problem, However, while either receive and transmit functions are used,  the received data became unreasonable. I use HAL functions How can I fix it?

The MCU is STM32L031G6Ux

the crashed Data is "L;S&~ö?"   instead of "1234567"

 

 

  main.c   -----  setting Recieve Data 

 

UART_HandleTypeDef hlpuart1;
IRDA_HandleTypeDef hirda2;

TIM_HandleTypeDef htim22;

 

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_IRDA_Init(void);
static void MX_LPUART1_UART_Init(void);
static void MX_TIM22_Init(void);

 

uint32_t count=0;

uint8_t RecievedData[7];

uint8_t FinalData[7];

uint8_t TransmitedData[] = "Hello world!";

HAL_IRDA_StateTypeDef stat;
HAL_StatusTypeDef status;

 

int main(void)

{

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();
MX_USART2_IRDA_Init();
MX_LPUART1_UART_Init();
MX_TIM22_Init();

HAL_TIM_Base_Init(&htim22);
HAL_TIM_Base_Start(&htim22);

HAL_IRDA_Receive_IT(&hirda2, RecievedData, sizeof(RecievedData));

 

while (1)
{

if (button == true)

   {   

        HAL_IRDA_Transmit(&hirda2, TransmitedData, strlen(TransmitedData)-1, 100);

   }

}

}

... Other initialization

 

stm3210xx_it.c ------- Interrupt wait for data ------ Recieve function is set for 7 bytes 

 

void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */

count = __HAL_TIM_GET_COUNTER(&htim22);    // If time between two recieved data is bigger than 100 ms 
if (count > 100 )                                                 // then reset RecievedData Array
{                                                                         // Because, Sometimes data is "1234" instead of "1234567"

                                                                          // And assume that second data is "1234567"
memset(RecievedData, 0, 7);                           // In this case with second data, function give me "1234123"
IRDA_EndRxTransfer(&hirda2);                       // And it continue in this synchronous (next data "4567123")

IRDA_EndTxTransfer(&hirda2);                       // This Block prevents this situation 
}
/* USER CODE END USART2_IRQn 0 */


HAL_IRDA_IRQHandler(&hirda2);


/* USER CODE BEGIN USART2_IRQn 1 */
stat = HAL_IRDA_GetState(&hirda2);
if (stat== HAL_IRDA_STATE_READY && RecievedData[6] != 0) 
   {
      for (int i=0; i<7; i++)                               // if 7 bytes come, The recieve array to final array
         {
            FinalData[i]= RecievedData[i];
         }
     memset(RecievedData, 0, 7);                                                        // Reset array
   }

if (stat== HAL_IRDA_STATE_RESET)
   {
      memset(RecievedData, 0, 7);
   }
__HAL_TIM_SET_COUNTER(&htim22, 0);                                   // Reset Counter
HAL_IRDA_Receive_IT(&hirda2, RecievedData, 7);                     // set Interruprt for next interrupt


/* USER CODE END USART2_IRQn 1 */
}

Outcomes