跳至主要内容

Desarrollo de comunicación CDC con STM32CubeMX

Configuración de STM32CubeMX​

  1. Habilitar el modo USB Device FS (Full Speed) en la pestaña de Pinout & Configuration.
  2. Seleccionar el modo CDC (Communication Device Class) en la pestaña de USB Device.
  3. Generar el código del proyecto.

Implementación del código​

En la función CDC_Receive_FS():​

  1. Establecer el búfer de recepción USB utilizando USBD_CDC_SetRxBuffer().
  2. Llamar a USBD_CDC_ReceivePacket() para indicar que el dispositivo está listo para recibir más datos.
  3. Si se desea enviar los datos recibidos de vuelta al host, llamar a CDC_Transmit_FS().
  4. Retornar USBD_OK para indicar que los datos han sido procesados.

En la función CDC_Transmit_FS():​

  1. Verificar el estado del búfer de transmisión USB (hcdc->TxState).
  2. Establecer el búfer de transmisión USB utilizando USBD_CDC_SetTxBuffer().
  3. Llamar a USBD_CDC_TransmitPacket() para enviar los datos.
  4. Retornar el resultado de la transmisión.

Habilitación de interrupciones​

  1. Habilitar las interrupciones USB en el proyecto generado por STM32CubeMX.
  2. Implementar las funciones de callback de recepción y transmisión USB, como USBD_CDC_DataReceived_FS() y USBD_CDC_TransmitCplt_FS().

Procesamiento de datos​

  1. En las funciones de callback, procesar los datos recibidos y preparar los datos a enviar.
  2. Utilizar las funciones CDC_Receive_FS() y CDC_Transmit_FS() para gestionar la recepción y transmisión de datos.

CDC Development with STM32CubeMX

STM32CubeMX Configuration​

  1. Enable USB Device FS (Full Speed) mode in the Pinout & Configuration tab.
  2. Select CDC (Communication Device Class) mode in the USB Device tab.
  3. Generate the project code.

Code Implementation​

In the CDC_Receive_FS() function:​

  1. Set the USB receive buffer using USBD_CDC_SetRxBuffer().
  2. Call USBD_CDC_ReceivePacket() to indicate the device is ready to receive more data.
  3. If you want to send the received data back to the host, call CDC_Transmit_FS().
  4. Return USBD_OK to indicate the data has been processed.

In the CDC_Transmit_FS() function:​

  1. Verify the state of the USB transmit buffer (hcdc->TxState).
  2. Set the USB transmit buffer using USBD_CDC_SetTxBuffer().
  3. Call USBD_CDC_TransmitPacket() to send the data.
  4. Return the result of the transmission.

Interrupt Enablement​

  1. Enable the USB interrupts in the project generated by STM32CubeMX.
  2. Implement the USB receive and transmit callback functions, such as USBD_CDC_DataReceived_FS() and USBD_CDC_TransmitCplt_FS().

Data Processing​

  1. In the callback functions, process the received data and prepare the data to be sent.
  2. Use the CDC_Receive_FS() and CDC_Transmit_FS() functions to manage the data reception and transmission.

使用 STM32CubeMX 開發 CDC 通訊

STM32CubeMX 設定​

  1. 在 Pinout & Configuration 標籤中啟用 USB Device FS (Full Speed) 模式。
  2. 在 USB Device 標籤中選擇 CDC (Communication Device Class) 模式。
  3. 產生專案程式碼。

程式碼實作​

在 CDC_Receive_FS() 函數中:​

  1. 使用 USBD_CDC_SetRxBuffer() 設定接收緩衝區。
  2. 呼叫 USBD_CDC_ReceivePacket() 表示設備已準備好接收更多資料。
  3. 如果需要將收到的資料回傳至主機, 呼叫 CDC_Transmit_FS()。
  4. 返回 USBD_OK 表示資料已處理完成。

在 CDC_Transmit_FS() 函數中:​

  1. 檢查傳輸緩衝區的狀態 (hcdc->TxState)。
  2. 使用 USBD_CDC_SetTxBuffer() 設定傳輸緩衝區。
  3. 呼叫 USBD_CDC_TransmitPacket() 傳送資料。
  4. 返回傳輸結果。

中斷啟用​

  1. 在 STM32CubeMX 產生的專案中啟用 USB 中斷。
  2. 實作接收和傳輸的回呼函數, 如 USBD_CDC_DataReceived_FS() 和 USBD_CDC_TransmitCplt_FS()。

資料處理​

  1. 在回呼函數中處理接收到的資料, 並準備要傳送的資料。
  2. 使用 CDC_Receive_FS() 和 CDC_Transmit_FS() 函數管理資料的接收和傳輸。