使用中断模式的STM32f072vb UART通信

时间:2019-06-11 14:55:46

标签: c++ stm32 uart

我正在尝试在基于STM32f072vb ARM的32位板和Raspberry Pi(Edge设备)之间开发无线通信通道。为此,我使用WiFly模块RN171。在中断模式下,使用stm32f0xx_hal库在STM32f0和WiFly模块之间建立了UART通信。我在使用C,C ++编程方面还很陌生,为了使它正常工作,我已经停留了一段时间。

一段时间后,系统出现错误,我不知道可能是什么。我主要使用两个类,主要类和receive.c类。在上一篇中,我实现了TX和RX的方法,并在主要部分中称呼它们。

开发板必须将从连接的传感器收集的信息连续传输到RPi(使用UART2通过wiFi模块)(tracedataf->参见代码)。 Pi处理该数据,并在需要时使用TCP连接将固定帧发送到模块,模块随后将数据发送到板卡。在这里,我将其分配给缓冲区(dorobo_Buffer_RX->参见代码),然后将其拆分以获取要分配给一组电动机的单个值(拆分,获取电动机->参见代码)。我的问题似乎出在UART通讯的管理中,因为我从HAL_UART_ErrorCallback收到一条错误消息

我附上我的代码,所有信息将对您有所帮助。

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
main
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

//--Import--
int main()
{
//--Initialization--

  receive_Dorobo(); //-->from receive.c

  while(1){

// ---Acquire data---
//--Get data from buffer (UART)--
       m1rx=getMotor(0); //-->from receive.c
       m2rx=getMotor(1); //-->from receive.c
       m3rx=getMotor(2); //-->from receive.c
//--Set motor speed --

 //--Generate message--
      sprintf(msg,"I:%i:%i:%i:%i:%i:%i:%i:%i:%i:E_",sensorLeft,sensorRight,SwLeft,SwRight,t1,t2,m1,m2,m3);

//--Send data via UART--

         tracedataf("%s",msg); // TRACE DOROBO32 MSG //-->from receive.c
         delay(50);
  }
  return 0;                                    
}

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
receive.c
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

#include "receive.h"
#include "stm32f0xx_hal.h"
#include "string.h"
#include "stdio.h"
#include "stdarg.h"
#include "dorobo32.h"
/***************************************************************************
***************************************************************************/
extern UART_HandleTypeDef huart2;
static UART_HandleTypeDef *trace_uart_ptr = &huart2;
/***************************************************************************
***************************************************************************/
#define MAX_RECEIVE_LENGTH 100 //MAX LENGTH OF THE MSG TO RECEIVE USING 
/***************************************************************************
***************************************************************************/
#define DOROBO32_PACKET_LENGTH 16
#define SAFE_STATE_DOROBO32 "I: +00:+00:+00:E_"
#define HEADER_PACKET_DOROBO32 'I'
#define END_PACKET_DOROBO32 '_'
#define SPLIT_START_P 3
#define LENGHT_DATA_MOTOR 3
/***************************************************************************
***************************************************************************/
#define TX_BUF_LENGTH 80
static char tx_buffer[TX_BUF_LENGTH];
volatile uint8_t synch=0;
//************************************************************************
uint8_t dorobo_Buffer_RX[DOROBO32_PACKET_LENGTH];

int8_t motors [3];
char * Safe_State= SAFE_STATE_DOROBO32;
uint8_t Last_Data[DOROBO32_PACKET_LENGTH];
//************************************************************************

 void receive_Dorobo (void)
{
  HAL_UART_Receive_IT(trace_uart_ptr,(uint8_t *)dorobo_Buffer_RX,DOROBO32_PACKET_LENGTH);
}

 /* @brief  Split the data received from the Edge Device and gets the values to be assigned to the motors
 * @param: Dorobo32_packet: char *
 * Example of packet: I:+10:-20:+30:E_
 */

 static void split(char * string)
 {
  char sub[3];
  int position, length, c = 0;
  position=SPLIT_START_P;
  length=LENGHT_DATA_MOTOR;

  for (int i=0;i<3;i++){
     c=0;
    while (c < length) {
          sub[c] = string[position+c-1];
          c++;
    }
      sub[c] = '\0';
      position=position+4;
      motors [i]=atoi(sub); //assigns the complete value accumulated in the sub[] as an integer to the position motor[i] of the array
    }
 }

 /*
  * @brief  Return the motor speed received from the Edge Device. In case of reception of a wrong packet, the function will
  * return the speed of the last correct packet received.
  * @return received speed for current motor in percentage (0-100)
  * @param: number of motor: int
 */

 int getMotor(int numberMotor){
    flag_bufferData=1;
     if(Last_Data[0]==HEADER_PACKET_DOROBO32 & Last_Data[DOROBO32_PACKET_LENGTH-1]==END_PACKET_DOROBO32) //A validation of the received frame, comparing header and tail
     {
       split(Last_Data);
       Safe_State=Last_Data;
     }
     else { split(Safe_State);}
     flag_bufferData=0;
return motors [numberMotor];
}
 /*
 * @brief   Rx completed Callback
 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  led_green_toggle();  //toggle green led in Dorobo32 board each time a reception of UART is completed
  __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
       synch=0;   
       // "cleaning" buffer;
        for (uint8_t i=0;i<DOROBO32_PACKET_LENGTH;i++)
            {
             Last_Data[i]=dorobo_Buffer_RX[i];
            dorobo_Buffer_RX[i]=0;//clear Rx_Buffer before receiving new data
                    }
          HAL_UART_Receive_IT(trace_uart_ptr,(uint8_t*)dorobo_Buffer_RX,DOROBO32_PACKET_LENGTH);
          synch=1;
       }
/* * @brief: function used to send the data via USART and trigger the interrupt */

void tracedata(char *msg)
{
if (synch==1){
          HAL_UART_Transmit_IT(&huart2, msg,  strlen(msg));
        HAL_Delay(1); //uncomment to enable a delay o the HAL
  }
 }

/** @brief  formats a message to trace using arguments */
void tracedataf(char *fmt, ...)
{
  va_list args;
  va_start(args, fmt);
  vsniprintf(tx_buffer, TX_BUF_LENGTH, fmt, args);
  tracedata(tx_buffer);
  va_end(args);
}

/* @brief   Callback in case of error in UART*/
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
  UNUSED(huart);   /* Prevent unused argument(s) compilation warning */
  if (huart->Instance == USART2)
  {
     __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
      huart->ErrorCode |= HAL_UART_ERROR_ORE;
    //TODO: panic("ERROR IN UART!");
   }
}

/** @brief  Tx completed Callback*/
  void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  {
  led_red_toggle();  //toggle red led in Dorobo32 board each time a Tx of UART is completed
  }

0 个答案:

没有答案