이번에 CUBEMX 를 업데이트하고 나서, USB CDC 를 테스트 해 봤습니다.


예전에, USBD_CDC.h 파일에서 수정해야 동작하던 상수값이, 
이번 버전(Ver 4.23.0)의 CUBE MX 에서는 제대로 생성해 주는 것을 확인했고,
몇가지 간단하게 수정하면 USB CDC 를 바로 쓸 수가 있었습니다.




usbd_cdc.h  예전의 설정 (USB COM Port 장치 에러 발 생)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/** @defgroup usbd_cdc_Exported_Defines
  * @{
  */ 
#define CDC_IN_EP                                   0x81  /* EP1 for data IN */
#define CDC_OUT_EP                                  0x01  /* EP1 for data OUT */
#define CDC_CMD_EP                                  0x82  /* EP2 for CDC commands */
 
/* CDC Endpoints parameters: you can fine tune these values depending on the needed baudrates and performance. */
#define CDC_DATA_HS_MAX_PACKET_SIZE                 512  /* Endpoint IN & OUT Packet size */
#define CDC_DATA_FS_MAX_PACKET_SIZE                 64  /* Endpoint IN & OUT Packet size */
#define CDC_CMD_PACKET_SIZE                         8  /* Control Endpoint Packet size */ 
 
#define USB_CDC_CONFIG_DESC_SIZ                     67
#define CDC_DATA_HS_IN_PACKET_SIZE                  CDC_DATA_HS_MAX_PACKET_SIZE
#define CDC_DATA_HS_OUT_PACKET_SIZE                 CDC_DATA_HS_MAX_PACKET_SIZE
 
#define CDC_DATA_FS_IN_PACKET_SIZE                  CDC_DATA_FS_MAX_PACKET_SIZE
#define CDC_DATA_FS_OUT_PACKET_SIZE                 CDC_DATA_FS_MAX_PACKET_SIZE
cs


usbd_cdc.h  새로 수정된 내용(CDC_DATA_HS_MAX_PACKET_SIZE 값이 256 으로 초기설정됨)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/** @defgroup usbd_cdc_Exported_Defines
  * @{
  */ 
#define CDC_IN_EP                                   0x81  /* EP1 for data IN */
#define CDC_OUT_EP                                  0x01  /* EP1 for data OUT */
#define CDC_CMD_EP                                  0x82  /* EP2 for CDC commands */
 
/* CDC Endpoints parameters: you can fine tune these values depending on the needed baudrates and performance. */
#define CDC_DATA_HS_MAX_PACKET_SIZE                 256  /* Endpoint IN & OUT Packet size */
#define CDC_DATA_FS_MAX_PACKET_SIZE                 64  /* Endpoint IN & OUT Packet size */
#define CDC_CMD_PACKET_SIZE                         8  /* Control Endpoint Packet size */ 
 
#define USB_CDC_CONFIG_DESC_SIZ                     67
#define CDC_DATA_HS_IN_PACKET_SIZE                  CDC_DATA_HS_MAX_PACKET_SIZE
#define CDC_DATA_HS_OUT_PACKET_SIZE                 CDC_DATA_HS_MAX_PACKET_SIZE
 
#define CDC_DATA_FS_IN_PACKET_SIZE                  CDC_DATA_FS_MAX_PACKET_SIZE
#define CDC_DATA_FS_OUT_PACKET_SIZE                 CDC_DATA_FS_MAX_PACKET_SIZE
cs

그래서 수정할 내용은, 
1. #include 에서 usbd_cdc_if.h 추가하고,
1
2
#include "string.h"
#include "usbd_cdc_if.h"
cs

2. 저는 보통 cdc를 디버그 출력용으로만 사용해서 Txd 기능만 주로 사용합니다.
  - 그리고, 쓰기 편하게 printf() 함수를 사용하다 보니, string.h 파일을 include 해 줍니다.
    USB_FS 로  printf() 함수를 사용하기 위한 추가 코드는 다음과 같습니다. main.c 파일의 User code 에 추가해 주면 됩니다.
 main.c 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* USER CODE BEGIN 0 */
#ifdef __GNUC__
 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
 
PUTCHAR_PROTOTYPE
{
 while(CDC_Transmit_FS((uint8_t *)&ch, 1== USBD_BUSY);
 return ch;
}
 
/* USER CODE END 0 */
cs
  - JTAG 나 SSTLINK 디버거용 핀을 보드에 배치하면 보드 크기가 커져서, 저는 USB CDC를 디버깅 기능으로 사용합니다.
  - USB 만 연결해서, 프로그램 다운로드는 USB DFU 를 사용하고, 디버깅이 필요할 시 USB CDC 를 사용하면 편리합니다. ^^

3. 위 설정만 완료하면, USB COM Port 로 PC 와 통신하면 됩니다.




다음은 간단한 USB CDC 출력 테스트 프로그램입니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f4xx_hal.h"
#include "usb_device.h"
 
/* USER CODE BEGIN Includes */
#include "string.h"
#include "usbd_cdc_if.h"
 
/* USER CODE END Includes */
 
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
 
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
 
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
 
/* USER CODE END PFP */
 
/* USER CODE BEGIN 0 */
#ifdef __GNUC__
 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
 
PUTCHAR_PROTOTYPE
{
 while(CDC_Transmit_FS((uint8_t *)&ch, 1== USBD_BUSY);
 return ch;
}
 
/* USER CODE END 0 */
 
int main(void)
{
 
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration----------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_USB_DEVICE_Init();
 
  /* USER CODE BEGIN 2 */
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
        printf("USB CDC Test ^^\r\n");
        HAL_Delay(500);
  /* USER CODE END WHILE */
 
  /* USER CODE BEGIN 3 */
 
  }
  /* USER CODE END 3 */
 
}
cs


터미날 프로그램으로 테스트한 내용입니다.



alpu_v100.zip


+ Recent posts