简体   繁体   English

Nucleo STM32f103RB/F4 探索

[英]Nucleo STM32f103RB/F4 Discovery

Has anybody used Duplex communication via VCP on Nucleo/or discovery single USART with RX TX interrupts.有没有人在 Nucleo 上通过 VCP 使用双工通信/或发现具有 RX TX 中断的单个 USART。

Would appreciate sample code to Echo back(transmit) what is received .希望能回显(传输)接收到的内容的示例代码。

There are certainly some example among the STM32CubeF4 and STM32CubeF1 package. STM32CubeF4STM32CubeF1包中肯定有一些示例。

Also see this example, in which the microcontroller echos back the received bytes to the sender using UART RX interrupt:另请参见此示例,其中微控制器使用 UART RX 中断将接收到的字节回传给发送方:

#include "stm32f4xx.h"

UART_HandleTypeDef huart2;

/* Single byte to store input */
uint8_t byte;

void SystemClock_Config(void);

/* UART2 Interrupt Service Routine */
void USART2_IRQHandler(void)
{
  HAL_UART_IRQHandler(&huart2);
}

/* This callback is called by the HAL_UART_IRQHandler when the given number of bytes are received */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if (huart->Instance == USART2)
  {
    /* Transmit one byte with 100 ms timeout */
    HAL_UART_Transmit(&huart2, &byte, 1, 100);

    /* Receive one byte in interrupt mode */ 
    HAL_UART_Receive_IT(&huart2, &byte, 1);
  }
}

void uart_gpio_init()
{
  GPIO_InitTypeDef GPIO_InitStruct;

  __GPIOA_CLK_ENABLE();

  /**USART2 GPIO Configuration
  PA2     ------> USART2_TX
  PA3     ------> USART2_RX
  */
  GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void uart_init()
{
  __USART2_CLK_ENABLE();

  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  HAL_UART_Init(&huart2);

  /* Peripheral interrupt init*/
  HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(USART2_IRQn);
}

int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  uart_gpio_init();
  uart_init();

  HAL_UART_Receive_IT(&huart2, &byte, 1);

  while(1)
  {

  }
}
  1. Initialize the GPIO pins of the UART.初始化 UART 的 GPIO 引脚。

    a.一种。 Enable the appropriate GPIO port's clock.启用适当的 GPIO 端口的时钟。

    b.Configure the UART pins in alternate function mode.在复用功能模式下配置 UART 引脚。

  2. Initialize the UART peripheral.初始化 UART 外设。

    a.一种。 Enable the clock of the appropriate UART peripheral.启用相应 UART 外设的时钟。

    b.Configure BAUD rate, word length, stop and parity bits, flow control etc.配置波特率、字长、停止位和奇偶校验位、流量控制等。

    c. C。 Enable the UART IRQ in NVIC and set the priority.在 NVIC 中启用 UART IRQ 并设置优先级。

  3. Call the HAL_UART_IRQHandler(UART_HandleTypeDef* huart);调用HAL_UART_IRQHandler(UART_HandleTypeDef* huart); in the UART ISR ( USART2_IRQHandler ).在 UART ISR ( USART2_IRQHandler ) 中。

  4. The HAL_UART_IRQHandler will call the HAL_UART_RxCpltCallback when the receive procedure is complete.当接收过程完成时, HAL_UART_IRQHandler将调用HAL_UART_RxCpltCallback In this callback you can transmit the received bytes.在此回调中,您可以传输接收到的字节。

  5. Start the echo loop with a single HAL_UART_Receive_IT(&huart2, &byte, 1);使用单个HAL_UART_Receive_IT(&huart2, &byte, 1);启动回声循环HAL_UART_Receive_IT(&huart2, &byte, 1); call.称呼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM