Не самая лучшая идея использовать для создания моста между huart с помощью стандартных HAL API функций.

Будем использовать циклический буфер.

main.c:

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define RX1BUF_LEN            128 // must be power of 2
#define TX1BUF_LEN            128 // must be power of 2
#define RX1BUF_MSK            (RX1BUF_LEN-1)
#define TX1BUF_MSK            (TX1BUF_LEN-1)
//---
#define RX2BUF_LEN            128 // must be power of 2
#define TX2BUF_LEN            128 // must be power of 2
#define RX2BUF_MSK            (RX2BUF_LEN-1)
#define TX2BUF_MSK            (TX2BUF_LEN-1)
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */
/* Single byte to store input */
uint8_t rx1_buf[RX1BUF_LEN], tx1_buf[TX1BUF_LEN];
/* xx_i - counter of input bytes (tx - pushed for transmit, rx - received)
   xx_o - counter of output bytes (tx - transmitted, rx - parsed)
   xx_e - counter of echoed bytes */
volatile uint16_t rx1_i = 0, tx1_o = 0;
uint16_t rx1_o = 0, rx1_e = 0, tx1_i = 0;
volatile uint8_t tx1_busy = 0;
//---
uint8_t rx2_buf[RX2BUF_LEN], tx2_buf[TX2BUF_LEN];
/* xx_i - counter of input bytes (tx - pushed for transmit, rx - received)
   xx_o - counter of output bytes (tx - transmitted, rx - parsed)
   xx_e - counter of echoed bytes */
volatile uint16_t rx2_i = 0, tx2_o = 0;
uint16_t rx2_o = 0, rx2_e = 0, tx2_i = 0;
volatile uint8_t tx2_busy = 0;

/* USER CODE BEGIN 0 */
/* This callback is called by the HAL_UART_IRQHandler when the given number of bytes are received */
void transmit1(uint8_t byte1) 
{
    tx1_buf[TX1BUF_MSK & tx1_i] = byte1;
    tx1_i++;
   // tx2_busy = 1;
   __HAL_UART_ENABLE_IT(&huart2, UART_IT_TXE);
}
void transmit2(uint8_t byte2) 
{
    tx2_buf[TX2BUF_MSK & tx2_i] = byte2;
    tx2_i++;
    tx2_busy = 1;
    __HAL_UART_ENABLE_IT(&huart1, UART_IT_TXE);
}
/* USER CODE END 0 */
/* Initialize all configured peripherals */
  ......_Init();
/* USER CODE BEGIN 2 */
	__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
	__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
  /* USER CODE END 2 */
while (1)
  {
		while (rx1_i != rx1_e) {
            /* echo here */
            transmit1(rx1_buf[RX1BUF_MSK & rx1_e]);
            rx1_e++;
        }
        while (rx1_i != rx1_o) {
            // parse here /
            // ... /
            rx1_o++;
        }
				while (rx2_i != rx2_e) {
            // echo here /
            transmit2(rx2_buf[RX2BUF_MSK & rx2_e]);
            rx2_e++;
        }
        while (rx2_i != rx2_o) {
            /* parse here */
            /* ... */
            rx2_o++;
        }
}

stm32f0xx_it.c:

/* USER CODE BEGIN PV */
extern uint8_t rx1_buf[RX1BUF_LEN], tx1_buf[TX1BUF_LEN];
extern volatile uint16_t rx1_i, tx1_o;
extern uint16_t rx1_o, rx1_e, tx1_i;
extern volatile uint8_t tx1_busy;
//--
extern uint8_t rx2_buf[RX2BUF_LEN], tx2_buf[TX2BUF_LEN];
extern volatile uint16_t rx2_i, tx2_o;
extern uint16_t rx2_o, rx2_e, tx2_i;
extern volatile uint8_t tx2_busy;
/* USER CODE END PV */

void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */
if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET) && 
       (__HAL_UART_GET_IT_SOURCE(&huart1, UART_IT_RXNE) != RESET))
    {
        rx1_buf[rx1_i & RX1BUF_MSK] = (uint8_t)(huart1.Instance->DR & 0x00FF);
        rx1_i++;
        /* Clear RXNE interrupt flag */
        __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
    }
		
    if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TXE) != RESET) &&
       (__HAL_UART_GET_IT_SOURCE(&huart1, UART_IT_TXE) != RESET))
    {
        if (tx2_i == tx2_o) {
            __HAL_UART_DISABLE_IT(&huart1, UART_IT_TXE);
            __HAL_UART_ENABLE_IT(&huart1, UART_IT_TC);
        } else {
            huart1.Instance->DR = (uint8_t)(tx2_buf[TX2BUF_MSK & tx2_o] & (uint8_t)0xFF);
            tx2_o++;
        }
    } 
		
    if((__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) != RESET) &&
       (__HAL_UART_GET_IT_SOURCE(&huart1, UART_IT_TC) != RESET))
    {
        tx1_busy = 0;
        __HAL_UART_DISABLE_IT(&huart1, UART_IT_TC);
    }
		
    /* And never call default handler */
    return;
  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */

  /* USER CODE END USART1_IRQn 1 */
}

/**
  * @brief This function handles USART2 global interrupt.
  */
void USART2_IRQHandler(void)
{
  /* USER CODE BEGIN USART2_IRQn 0 */
if((__HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE) != RESET) && 
       (__HAL_UART_GET_IT_SOURCE(&huart2, UART_IT_RXNE) != RESET))
    {
        rx2_buf[rx2_i & RX2BUF_MSK] = (uint8_t)(huart2.Instance->DR & 0x00FF);
        rx2_i++;
        /* Clear RXNE interrupt flag */
        __HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_RXNE);
    }
    if((__HAL_UART_GET_FLAG(&huart2, UART_FLAG_TXE) != RESET) &&
       (__HAL_UART_GET_IT_SOURCE(&huart2, UART_IT_TXE) != RESET))
    {
        if (tx1_i == tx1_o) {
            __HAL_UART_DISABLE_IT(&huart2, UART_IT_TXE);
            __HAL_UART_ENABLE_IT(&huart2, UART_IT_TC);
        } else {
            huart2.Instance->DR = (uint8_t)(tx1_buf[TX1BUF_MSK & tx1_o] & (uint8_t)0xFF);
            tx1_o++;
        }
    }
    if((__HAL_UART_GET_FLAG(&huart2, UART_FLAG_TC) != RESET) &&
       (__HAL_UART_GET_IT_SOURCE(&huart2, UART_IT_TC) != RESET))
    {
        tx2_busy = 0;
        __HAL_UART_DISABLE_IT(&huart2, UART_IT_TC);
    }
    /* And never call default handler */
    return;
  /* USER CODE END USART2_IRQn 0 */
  HAL_UART_IRQHandler(&huart2);
  /* USER CODE BEGIN USART2_IRQn 1 */

  /* USER CODE END USART2_IRQn 1 */
}

0 комментариев

Добавить комментарий