AnsweredAssumed Answered

F030 USART half dupliex

Question asked by das.sanjib on May 9, 2014
Latest reply on May 20, 2014 by das.sanjib
Hi All

I have configured USART 1 and USART 2 of same f030 board to check half duplex communication....USART 1 will be in received mode first(power on Reset) ......have set an timer 2 interrupt for changing the line direction of USART1 to transmit mode and enabled the transmit buffer IRQ. and disabling the  RXNE_IRQ .......USART1 will send the data to USART 2 and change its line direction to received mode(as it has to receive the ACK).... USART2 too will be in receive mode (to receive the data from USART1) on power on reset but after receiving the data it will turn the line to transmit mode and send the ack which is then received by USARt1 .
Here data is getting send by USART1 but it is not getting recieved from USART2. RXNE bit is set but index_Uart2_Received_case++ flag is not getting increment it is just entering to case 0
index_Uart2_Received_case++ flag is increment to 1(for the first time when it entered) but next time also rather going to case 1 it is entering the case 0 again. I have experience the same problem while sending data in usart 1 before ,Bufflength was not getting decremented but I solved it by setting the priority of timer from 0 to 2 .but present problem is not getting solved

Secondly : In recieved buffer it is not receiving anything no data is received. but sending is perfect
please help me

For instance I have pasted the code


/**
  ******************************************************************************
  * @file    USART/HalfDuplex/main.c
  * @author  MCD Application Team
  * @version V3.4.0
  * @date    10/15/2010
  * @brief   Main program body
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  */

/* Includes ------------------------------------------------------------------*/
#include <stm32f0xx.h>
#include "stm32f0xx_adc.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_usart.h"
#include <stm32f0xx_syscfg.h>
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_misc.h"
#include "stm32f0xx_tim.h"

char a;

#include "stdio.h"


# define Board1 1

# define Board2 1



/* Private typedef -----------------------------------------------------------*/
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;

/* Private define ------------------------------------------------------------*/

/**
 * size of the buffer send from stm32f030 required
 * for checking purpose to check wheter the received data
   is same with the data send from stm32f030
 **/

//#define BufferSize   (countof(TxBuffer1) - 1)

/* Private macro -------------------------------------------------------------*/
//#define countof(a)   (sizeof(a) / sizeof(*(a)))

/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
/*
 * send to stm32f030
**/
#if Board1
uint8_t imu_data[7] = "SANJIBD";
uint8_t temp_imu_data[];
uint8_t Bufflength = 0;
uint8_t RxBufferUsart1[];
__IO uint8_t Uart1_Rcv_count = 0x00;
__IO uint8_t Uart1_Trnsmt_count = 0x00;
uint8_t index_Uart1_Received_case = 0;
__IO uint8_t Uart1_Received_Buffer_Byte ;
__IO uint8_t Uart1_Src ;
__IO uint8_t Uart1_Dst ;
__IO uint8_t Uart1_Cmd ;

#endif

#if Board2
uint8_t ACK[12] = "BABABABABABA";
__IO uint8_t Uart2_Src ;
__IO uint8_t Uart2_Dst ;
__IO uint8_t Uart2_Cmd ;
__IO uint8_t Uart2_Rcv_count = 0x00;
__IO uint8_t Uart2_Trnsmt_count = 0x00;
__IO uint8_t Uart2_Received_Buffer_Byte ;
__IO uint8_t index_Uart2_Received_case = 0;
uint8_t RxBufferUsart2[];
uint8_t BufflengthBuffer_size_of_ACK_of_USART2 = 0;
#endif
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);


/* Private functions ---------------------------------------------------------*/

/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */
    // STM_EVAL_LEDInit(LED3);
  /* System Clocks Configuration */


  RCC_Configuration();

  /* Configure the GPIO ports */
  GPIO_Configuration();
  Uart_init();
#if  Board1
  INTTIM_Config();

  Interrupt_enable1();
#endif
#if Board2
  Interrupt_enable2();
#endif
#if  Board1
  EnableTimerInterrupt();
#endif
 // Interrupt_enable2();




  //STM_EVAL_PBInit(BUTTON_TAMPER,BUTTON_MODE_GPIO);


 while (1)
 {

 }

 }


void Uart_init()
 {
      USART_InitStructure.USART_BaudRate = 9600;
      USART_InitStructure.USART_WordLength = USART_WordLength_8b;
      USART_InitStructure.USART_StopBits = USART_StopBits_1;
      USART_InitStructure.USART_Parity = USART_Parity_No;
      USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
      USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
#if Board1
 /* Configure USART1 */
 USART_Init(USART1, &USART_InitStructure);
 /* Enable the USARTy */
 USART_Cmd(USART1, ENABLE);
 USART_DirectionModeCmd(USART1,USART_Mode_Rx, ENABLE); // enable RX mode // on startup
 USART_DirectionModeCmd(USART1,USART_Mode_Tx, DISABLE); // disable TX mode
 /* Enable USARTy Half Duplex Mode*/
 USART_HalfDuplexCmd(USART1, ENABLE);
 // USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
 USART_Init(USART2, &USART_InitStructure);

#endif
#if Board2
 /* Enable the USARTz */
 USART_Cmd(USART2, ENABLE);
 /* Enable USARTz Half Duplex Mode*/
 USART_HalfDuplexCmd(USART2, ENABLE);
USART_DirectionModeCmd(USART2,USART_Mode_Tx, DISABLE); // enable RX mode // on startup
USART_DirectionModeCmd(USART2,USART_Mode_Rx, ENABLE); // disable TX mode
// USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
#endif
 }
#if Board2
void Interrupt_enable2()
{

    NVIC_InitTypeDef NVIC_InitStructure;


   /* Enable the USARTx Interrupt */
   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;

   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);
 }
#endif
#if  Board1
void Interrupt_enable1()
{

    NVIC_InitTypeDef NVIC_InitStructure;


   /* Enable the USARTx Interrupt */
   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;

   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);





 }



void EnableTimerInterrupt()
{

    NVIC_InitTypeDef NVIC_InitStructure;


       /* Enable the USARTx Interrupt */
       NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn ;

       NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
       NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
       NVIC_Init(&NVIC_InitStructure);

}
void INTTIM_Config(void)
{
    TIM_TimeBaseInitTypeDef    TIM_InitStruct;
  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  /* Time base configuration */
  TIM_InitStruct.TIM_Prescaler = 800 - 1;
  TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_InitStruct.TIM_Period = 60000 - 1;
  TIM_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_InitStruct.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM2, &TIM_InitStruct);
  TIM_Cmd(TIM2, ENABLE);
  /* TIM2 enable counter */

  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}

void TIM2_IRQHandler()

{




    // unsigned long long int digit = 0;
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)




        Form_Pkt();

        Send_Pkt();

    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

        }

void Form_Pkt()
{
    Bufflength = sizeof(imu_data);
    memcpy(temp_imu_data,imu_data,Bufflength);
}
void Send_Pkt()
{

     USART_DirectionModeCmd(USART1,USART_Mode_Rx, DISABLE); // enable RX mode // on startup
     USART_DirectionModeCmd(USART1,USART_Mode_Tx, ENABLE); // disable TX mode
     USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
     USART_ITConfig(USART1, USART_IT_TXE, ENABLE);


}


void USART1_IRQHandler(void)
{
/*PACKET format src_add,dest_add,cmd,size_of_data_bytes_to_follow,required_bytes*/
  /* USART in Receiver mode */
  if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
  {
      switch (index_Uart1_Received_case)
               {
               case 0:
                   Uart1_Src = USART_ReceiveData(USART1);
                   index_Uart1_Received_case ++;
                    break;
               case 1:
                   Uart1_Dst = USART_ReceiveData(USART1);
                   index_Uart1_Received_case ++;
                    break;
               case 2:
                   Uart1_Cmd = USART_ReceiveData(USART1);
                       index_Uart1_Received_case ++;
                       break;
               case 3:
                      Uart1_Received_Buffer_Byte = USART_ReceiveData(USART1);
                       index_Uart1_Received_case ++;
                       break;
               case 4:
                   if (Uart1_Received_Buffer_Byte>0)
                   {
                   RxBufferUsart1[Uart1_Rcv_count++] = USART_ReceiveData(USART1);
                   }
                   break;

    }

  }






  /* USART in Transmitter mode
   *  Checking for the Tx_buffer is empty or not
   *  */

  if (USART_GetITStatus(USART1, USART_IT_TXE) == SET)
  {

      /* checking the count to be zero which implies the finish of the packet
       */
    if (Bufflength > 0)
    {
      /* Send Transaction data */
      USART_SendData(USART1, temp_imu_data[ Uart1_Trnsmt_count++ ]);

      GPIO_SetBits(GPIOC, (GPIO_Pin_8 | GPIO_Pin_9));

   /*
    * Decrementing the count
    */

      Bufflength--;

    }
    /*
     * After The Packet  transfer is done Disabling the TXE IRQ and enabling
     * the RX IRQ in else part
     */
    else
    {
      /* Disable the Tx buffer empty interrupt */
      USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
      USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
      USART_DirectionModeCmd(USART1,USART_Mode_Tx  , DISABLE);
      USART_DirectionModeCmd(USART1,USART_Mode_Rx, ENABLE);
      Uart1_Trnsmt_count = 0;

    }
  }
}
#endif
#if Board2
void USART2_IRQHandler(void)
{
    /*PACKET format src_add,dest_add,cmd,size_of_data_bytes_to_follow,required_bytes*/

  /* USART in Receiver mode */
  if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
  {

      switch (index_Uart2_Received_case)
         {
         case 0:
             Uart2_Src = USART_ReceiveData(USART2);
             index_Uart2_Received_case++;

              break;
         case 1:
             Uart2_Dst = USART_ReceiveData(USART2);
             index_Uart2_Received_case++;

              break;
         case 2:
             Uart2_Cmd = USART_ReceiveData(USART2);
                 index_Uart2_Received_case++;

                 break;
         case 3:
                Uart2_Received_Buffer_Byte = USART_ReceiveData(USART2);
                 index_Uart2_Received_case++;

                 break;
         case 4:
             if (Uart2_Received_Buffer_Byte>0)
             {
             RxBufferUsart2[Uart2_Rcv_count++] = USART_ReceiveData(USART2);
             GPIO_SetBits(GPIOC, (GPIO_Pin_8 | GPIO_Pin_9));
             }

                 break;


         }
       }
    else
    {
      /* Disable the Rx buffer not empty interrupt */
      USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);

      USART_DirectionModeCmd(USART2,USART_Mode_Rx, DISABLE);
      USART_DirectionModeCmd(USART2,USART_Mode_Tx, ENABLE);
      USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
      Uart2_Rcv_count = 0;

  //    index_Uart2_Received_case = 0;
    }


  /* USART in Tramitter mode */
  if (USART_GetITStatus(USART2, USART_IT_TXE) == SET)
  {


      /* Send Transaction data */
      USART_SendData(USART2, ACK[Uart2_Trnsmt_count++]);
      /** After Receiving the data ack need to be senf*/
      ACK_Packet_size();
    //  a = USART1->TDR; // to check whether the data is getting send
      if (BufflengthBuffer_size_of_ACK_of_USART2  > 0)
          {
            /* Send Transaction data */
            USART_SendData(USART2, ACK[ Uart2_Trnsmt_count++ ]);
         /*
          * Decrementing the count
          */
            BufflengthBuffer_size_of_ACK_of_USART2  --;


    }
    else
    {
      /* Disable the Tx buffer empty interrupt */
      USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
      USART_DirectionModeCmd(USART2,USART_Mode_Tx, DISABLE);
      USART_DirectionModeCmd(USART2,USART_Mode_Rx, ENABLE);

          USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
          Uart2_Trnsmt_count = 0;
    }
  }

}

ACK_Packet_size()
{
    BufflengthBuffer_size_of_ACK_of_USART2 = sizeof(ACK);
}
#endif
/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{
#if Board1
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
#endif
#if Board1
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
#endif
#if Board2
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
#endif

}

/**
  * @brief  Configures the different GPIO ports.
  * @param  None
  * @retval None
 */
void GPIO_Configuration(void)
{

  GPIO_InitTypeDef GPIO_InitStructure;


     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_Init(GPIOC, &GPIO_InitStructure);
#if Board2
    /* This sequence sets up the TX and RX pins
     * so they work correctly with the USART1 peripheral
     */
     /* Configure USART2 Tx as alternate function open-drain */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_PuPd =  GPIO_PuPd_UP;
      GPIO_InitStructure.GPIO_PuPd =   GPIO_OType_OD;
      GPIO_Init(GPIOA, &GPIO_InitStructure);

#endif

#if Board1
      /* Configure USART1 Tx as alternate function open-drain */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_PuPd =  GPIO_PuPd_UP;
             GPIO_InitStructure.GPIO_PuPd =   GPIO_OType_OD;
      GPIO_Init(GPIOB, &GPIO_InitStructure);
      GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);
       //
#endif
#if Board2
      GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_1);
#endif
}

/**
  * @brief  Compares two buffers.
  * @param  pBuffer1, pBuffer2: buffers to be compared.
  * @param  BufferLength: buffer's length
  * @retval PASSED: pBuffer1 identical to pBuffer2
  *   FAILED: pBuffer1 differs from pBuffer2
  */



#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */

/**
  * @}
  */

/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/

Outcomes