Pemrograman STM32F103C8 USB Serial

Blue pill merupakan sebuah development board untuk mikrokontroler STM32F103C8T6, yang sudah terdapat port USB. Port USB ini dapat digunakan untuk berkomunikasi dengan komputer tanpa harus menggunakan USB to Serial adapter, karena mikrokontroler STM32F103C8xx sudah terdapat fitur USB Device. Port USB ini tersambung dengan pin PA12 (USB_DP) dan PA11 (USB_DM.). USB Device di STM32 dapat digunakan untuk berbagai macam keperluan, salah satunya dapat digunakan untuk Communication Device Class (Virtual Port Com) atau Serial Port Com. 

Berikut akan dijelaskan bagaimana memprogram USB Device ini menjadi Serial Port.

Pertama buat project baru di STM32CubeMX dan aktifkan USB Device sebagai Communication Device Class (Virtual Port Com). 

Kemudian Generate Code dan buka Project-nya di Eclipse. Setelah berhasil di import ke Eclise, kemudian buka file main.c. Kita perlu menambahkan beberapa program agar mudah ketika menggunakan USB Serial.

Tentukan maksimal buffer serial transmiter-nya


#include "main.h"
#include "stm32f1xx_hal.h"
#include "usb_device.h"

#define MAX_SERIAL_BUFF 50

Buat fungsi baru untuk mengirimkan data serial.

void USB_print(char dataText[MAX_SERIAL_BUFF]){
	int len ;
	char buffer[MAX_SERIAL_BUFF] ;

	len=sprintf(buffer,dataText);
	CDC_Transmit_FS(buffer,len);
}

Kemudian buat program utama untuk mengirim data dengan USB Serial

while (1)
  {
	  USB_print("USB Serial STM32F103 dengan STM32CubeMX + Eclipse \r\n");
	  HAL_Delay(1000) ;
  }

Setelah selesai, Debug dan upload menggunakan ST-Link Programmer.

Colokkan kabel data micro USB dari komputer ke Blue Pill. Jangan Lupa untuk menginstal Driver STM32 VCP Driver agar com port terdeteksi di komputer. Jika Komputer kita online, maka dapat install/update driver otomatis secara online. Atau bisa download driver-nya di website resmi ST di sini.

Setelah di install driver VCP

Selanjutnya tinggal mengecek data serialnya dengan menggunakan software serial monitor seperti RealTerm

Sekian tutorial kali ini. Source Code penuh ada dibagian bawah artikel.

Semoga bermanfaat

 

FULL SOURCE CODE


#include "main.h"
#include "stm32f1xx_hal.h"
#include "usb_device.h"

#define MAX_SERIAL_BUFF 50

void SystemClock_Config(void);
static void MX_GPIO_Init(void);


int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USB_DEVICE_Init();

  while (1)
  {
	  USB_print("USB Serial STM32F103 dengan STM32CubeMX + Eclipse \r\n");
	  HAL_Delay(1000) ;
  }


}

//=========================================================================================================
void USB_print(char dataText[MAX_SERIAL_BUFF]){
	int len ;
	char buffer[MAX_SERIAL_BUFF] ;

	len=sprintf(buffer,dataText);
	CDC_Transmit_FS(buffer,len);
}

//================================================================================================================
/** System Clock Configuration
*/
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure the Systick interrupt time 
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick 
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/** Configure pins as 
        * Analog 
        * Input 
        * Output
        * EVENT_OUT
        * EXTI
*/
static void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);

  /*Configure GPIO pin : PC13 */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
void _Error_Handler(char * file, int line)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  while(1) 
  {
  }
  /* USER CODE END Error_Handler_Debug */ 
}

#ifdef USE_FULL_ASSERT

/**
   * @brief Reports the name of the source file and the source line number
   * where the assert_param error has occurred.
   * @param file: pointer to the source file name
   * @param line: assert_param error line source number
   * @retval None
   */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */

}

#endif

/**
  * @}
  */ 

/**
  * @}
*/ 

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Leave a Reply

Your email address will not be published. Required fields are marked *