STM32로 CDC를 하려는 중에 질문이 있습니다.

익명 사용자의 이미지

STM32 CubeMX를 통해 다음과 같이 설정하였습니다.

◆ System Core - SYS
Debug - Serial Wire

◆ System Core - GPIO
PB7 - GPIO Output (파란색 LED)
PB14 - GPIO Output (빨간색 LED)
PC7 - GPIO Output (녹색 LED)
PC13 - GPIO EXTI13 (사용자 버튼)

◆ System Core - NVIC
EXTI line[15:10] interrput - Enabled 체크

◆ Connectivity - USB_OTG_FS
Mode - Device_Only

◆ Middleware - USB_DEVICE
Class For FS IP - Communication Device Class (Virtual Port Com)

Clock Configuration에서는 CubeMX가 권장하는 대로 하였고,
이후 코드를 생성해서 다음과 같은 부분을 수정했습니다.

/* main.c */
 
#include "main.h"
#include "usb_device.h"
#include "usbd_cdc_if.h" //추가
 
...
 
int main(void)
{
 
...
 
 uint8_t data[] = "Hello, World!\r\n";
 
  while (1)
  {
    /* USER CODE END WHILE */
	  if (CDC_Transmit_FS(data, sizeof(data)) == USBD_OK)
	  {
		  HAL_GPIO_TogglePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin);
	  }
	  HAL_Delay(1000);
    /* USER CODE BEGIN 3 */
  }
 
...
 
}

/* usbd_cdc_if.c */
 
 
/* USER CODE BEGIN INCLUDE */
USBD_CDC_LineCodingTypeDef LineCoding =
    {
        115200, /* baud rate      */
        0x00,   /* stop bits-1    */
        0x00,   /* parity - none  */
        0x08    /* nb. of bits 8  */
    };
/* USER CODE END INCLUDE */
 
...
 
static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length)
{
 
...
 
case CDC_SET_LINE_CODING:
    	LineCoding.bitrate    = (uint32_t)(pbuf[0]);
    	LineCoding.bitrate   |= (uint32_t)(pbuf[1]<<8);
    	LineCoding.bitrate   |= (uint32_t)(pbuf[2]<<16);
    	LineCoding.bitrate   |= (uint32_t)(pbuf[3]<<24);
    	LineCoding.format     = pbuf[4];
    	LineCoding.paritytype = pbuf[5];
    	LineCoding.datatype   = pbuf[6];
    break;
 
    case CDC_GET_LINE_CODING:
    	pbuf[0] = (uint8_t)(LineCoding.bitrate);
    	pbuf[1] = (uint8_t)(LineCoding.bitrate >> 8);
    	pbuf[2] = (uint8_t)(LineCoding.bitrate >> 16);
    	pbuf[3] = (uint8_t)(LineCoding.bitrate >> 24);
    	pbuf[4] = LineCoding.format;
    	pbuf[5] = LineCoding.paritytype;
    	pbuf[6] = LineCoding.datatype;
    break;
 
...
 
}

/* usbd_cdc.h */
 
...
 
#define CDC_DATA_HS_MAX_PACKET_SIZE                 256U  /* Endpoint IN & OUT Packet size */
 
...

main.c의 while문 안에 작성한
CDC_Transmit_FS(data, sizeof(data)) 이 부분이 USBD_BUSY를 리턴합니다.

Tera Term에 "Hello World!"를 찍는 게 목표긴 한데,
인터넷에 올라와 있는 예제들을 따라해 봐도 생각처럼 잘 안 되네요.

익명 사용자의 이미지

/* usbd_cdc_if.c */
 
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
{
  uint8_t result = USBD_OK;
  /* USER CODE BEGIN 7 */
  USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)USBD_Device.pClassData;
 
  if (hcdc->TxState != 0){
    return USBD_BUSY;
  }
 
  USBD_CDC_SetTxBuffer(&USBD_Device, Buf, Len);
  result = USBD_CDC_TransmitPacket(&USBD_Device);
  /* USER CODE END 7 */
  return result;
}

이 부분에서 중간의 hcdc->TxState의 값이 0이 아니어서 USBD_BUSY를 리턴합니다.
그리고 여기서 USBD_CDC_TransmitPacket(&USBD_Device); 부분은 USBD_FAIL을 리턴하는데,
해당 부분의 코드는 다음과 같습니다.

/* usbd_cdc.c */
 
uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev)
{
  USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData;
  USBD_StatusTypeDef ret = USBD_BUSY;
 
  if (pdev->pClassData == NULL)
  {
    return (uint8_t)USBD_FAIL;
  }
 
  if (hcdc->TxState == 0U)
  {
    /* Tx Transfer in progress */
    hcdc->TxState = 1U;
 
    /* Update the packet total length */
    pdev->ep_in[CDC_IN_EP & 0xFU].total_length = hcdc->TxLength;
 
    /* Transmit next packet */
    (void)USBD_LL_Transmit(pdev, CDC_IN_EP, hcdc->TxBuffer, hcdc->TxLength);
 
    ret = USBD_OK;
  }
 
  return (uint8_t)ret;
}

이 부분에서는 pdev->pClassData == NULL 인 모양인지 USBD_FAIL을 리턴합니다.
왜 이렇게 된 건지 원인조차 모르고 있으니 머리만 싸매고 답답하네요...

나빌레라의 이미지

질문에 있는 정보가 충분치 않아서, 확실친 않은데...

USB_CDC의 디바이스 초기화나 USB descripter의 초기화는 했나요?

----------------------
얇은 사 하이얀 고깔은 고이 접어서 나빌레라

익명 사용자의 이미지

다른 예제들에서는 CubeMX에서 코드 자동 생성을 한 이후에는
메인 코드 정도 외엔 따로 수정을 하는 과정이 없어서 다른 과정은 잘 모르겠습니다.

우선 main.c에는 MX_USB_DEVICE_Init();가 호출되어 있습니다.
MX_USB_DEVICE_Init();는 아래와 같이 작성되어 있습니다.

/* usb_device.c */
void MX_USB_DEVICE_Init(void)
{
  /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */
 
  /* USER CODE END USB_DEVICE_Init_PreTreatment */
 
  /* Init Device Library, add supported class and start the library. */
  if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) != USBD_OK)
  {
    Error_Handler();
  }
  if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */
 
  /* USER CODE END USB_DEVICE_Init_PostTreatment */
}

제가 많이 부족해서 어떤 부분을 말씀드려야 좋을지도 잘 몰라 죄송합니다 ㅜㅜ

나빌레라의 이미지

USB 꼽아서 PC 쪽에 장치 잡힌건 확인하셨나요?

----------------------
얇은 사 하이얀 고깔은 고이 접어서 나빌레라

익명 사용자의 이미지

네, 장치 관리자에
STMicroelectronics STLink Virtual COM Port(COM6)로 잡혔습니다.

bushi의 이미지

USB 는 ISO type end-point 외엔 전부 synchronous 통신입니다.
받아주는 놈이 있어야 보낼 수 있습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.