Desarrollo de comunicación CDC con STM32CubeMX
- Habilitar el modo USB Device FS (Full Speed) en la pestaña de Pinout & Configuration.
- Seleccionar el modo CDC (Communication Device Class) en la pestaña de USB Device.
- Generar el código del proyecto.
- Establecer el búfer de recepción USB utilizando
USBD_CDC_SetRxBuffer().
- Llamar a
USBD_CDC_ReceivePacket() para indicar que el dispositivo está listo para recibir más datos.
- Si se desea enviar los datos recibidos de vuelta al host, llamar a
CDC_Transmit_FS().
- Retornar
USBD_OK para indicar que los datos han sido procesados.
- Verificar el estado del búfer de transmisión USB (
hcdc->TxState).
- Establecer el búfer de transmisión USB utilizando
USBD_CDC_SetTxBuffer().
- Llamar a
USBD_CDC_TransmitPacket() para enviar los datos.
- Retornar el resultado de la transmisión.
- Habilitar las interrupciones USB en el proyecto generado por STM32CubeMX.
- Implementar las funciones de callback de recepción y transmisión USB, como
USBD_CDC_DataReceived_FS() y USBD_CDC_TransmitCplt_FS().
- En las funciones de callback, procesar los datos recibidos y preparar los datos a enviar.
- Utilizar las funciones
CDC_Receive_FS() y CDC_Transmit_FS() para gestionar la recepción y transmisión de datos.
CDC Development with STM32CubeMX
- Enable USB Device FS (Full Speed) mode in the Pinout & Configuration tab.
- Select CDC (Communication Device Class) mode in the USB Device tab.
- Generate the project code.
- Set the USB receive buffer using
USBD_CDC_SetRxBuffer().
- Call
USBD_CDC_ReceivePacket() to indicate the device is ready to receive more data.
- If you want to send the received data back to the host, call
CDC_Transmit_FS().
- Return
USBD_OK to indicate the data has been processed.
- Verify the state of the USB transmit buffer (
hcdc->TxState).
- Set the USB transmit buffer using
USBD_CDC_SetTxBuffer().
- Call
USBD_CDC_TransmitPacket() to send the data.
- Return the result of the transmission.
- Enable the USB interrupts in the project generated by STM32CubeMX.
- Implement the USB receive and transmit callback functions, such as
USBD_CDC_DataReceived_FS() and USBD_CDC_TransmitCplt_FS().
- In the callback functions, process the received data and prepare the data to be sent.
- Use the
CDC_Receive_FS() and CDC_Transmit_FS() functions to manage the data reception and transmission.
使用 STM32CubeMX 開發 CDC 通訊
- 在 Pinout & Configuration 標籤中啟用 USB Device FS (Full Speed) 模式。
- 在 USB Device 標籤中選擇 CDC (Communication Device Class) 模式。
- 產生專案程式碼。
- 使用
USBD_CDC_SetRxBuffer() 設定接收緩衝區。
- 呼叫
USBD_CDC_ReceivePacket() 表示設備已準備好接收更多資料。
- 如果需要將收到的資料回傳至主機, 呼叫
CDC_Transmit_FS()。
- 返回
USBD_OK 表示資料已處理完成。
- 檢查傳輸緩衝區的狀態 (
hcdc->TxState)。
- 使用
USBD_CDC_SetTxBuffer() 設定傳輸緩衝區。
- 呼叫
USBD_CDC_TransmitPacket() 傳送資料。
- 返回傳輸結果。
- 在 STM32CubeMX 產生的專案中啟用 USB 中斷。
- 實作接收和傳輸的回呼函數, 如
USBD_CDC_DataReceived_FS() 和 USBD_CDC_TransmitCplt_FS()。
- 在回呼函數中處理接收到的資料, 並準備要傳送的資料。
- 使用
CDC_Receive_FS() 和 CDC_Transmit_FS() 函數管理資料的接收和傳輸。