cancel
Showing results for 
Search instead for 
Did you mean: 

Embedded C program to demonstrate USART-DMA-USB functionality of stm32 bluepill MCU

bdpak
Associate II

 

/* USER CODE BEGIN Header */

/**

  ******************************************************************************

  * @file           : main.c

  * @brief          : Main program body

  ******************************************************************************

  * @attention

  *

  * Copyright (c) 2023 STMicroelectronics.

  * All rights reserved.

  *

  * This software is licensed under terms that can be found in the LICENSE file

  * in the root directory of this software component.

  * If no LICENSE file comes with this software, it is provided AS-IS.

  *

  ******************************************************************************

  */

/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "dma.h"

#include "usart.h"

#include "usb_device.h"

#include "gpio.h"

void SystemClock_Config(void);

uint8_t rx_data[6];


int main(void)

{
  HAL_Init();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_DMA_Init();

  MX_USB_DEVICE_Init();

  MX_USART1_UART_Init();

  // Refer https://github.com/akospasztor/stm32-dma-uart/blob/master/Src/main.c to understand DMA
 // functionality

  HAL_UART_Receive_DMA(&huart1,rx_data,6);

  while (1)

  {

  }


}
//  Refer https://youtu.be/dEQwSl8mCFs?si=wEeoZyRNYjOkMgXI video to understand  USART
// call back function
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
   UNUSED(huart);
   HAL_UART_Transmit(&huart1,rx_data,6, 10);

// Refer https://youtu.be/92A98iEFmaA?si=jRMQV2SEAKNCuGaQ Video to understand USB virtual
// com port function
  CDC_Transmit_FS(rx_data, 6);

}


void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)

{

    Error_Handler();

}

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct = {0};

  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};


  /** Initializes the RCC Oscillators according to the specified parameters

  * in the RCC_OscInitTypeDef structure.

  */

  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();

  }


  /** Initializes the CPU, AHB and APB buses 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();

  }

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;

  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;

  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

  {

    Error_Handler();

  }


  /** Enables the Clock Security System

  */

  HAL_RCC_EnableCSS();

}



void Error_Handler(void)

{

  __disable_irq();

  while (1)

  {

  }


}


#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)

{


}

#endif /* USE_FULL_ASSERT */

 

Block diagram of setupBlock diagram of setup

 Once you flash the program onto MCU, you can open Putty SSH client program for two instances i.e. USB virtual com port and USART com port. Message you type on the USART com port instance on Putty will be displayed on the USB virtual com port instance via DMA on the linux terminal.

0 REPLIES 0