From 7c3e3a1a7421477c3b945049cbb990eb700c9f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=98=A4=EB=B3=91=EC=A4=80?= Date: Mon, 27 May 2024 01:14:44 +0900 Subject: [PATCH] add: STM32F4 support --- devices/stm32f4/Core/Src/main.c | 2 +- devices/stm32f4/USB_DEVICE/App/usbd_cdc_if.c | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/devices/stm32f4/Core/Src/main.c b/devices/stm32f4/Core/Src/main.c index d696192..8ec6d62 100644 --- a/devices/stm32f4/Core/Src/main.c +++ b/devices/stm32f4/Core/Src/main.c @@ -91,7 +91,7 @@ int main(void) MX_TIM1_Init(); MX_USB_DEVICE_Init(); /* USER CODE BEGIN 2 */ - + HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); /* USER CODE END 2 */ /* Infinite loop */ diff --git a/devices/stm32f4/USB_DEVICE/App/usbd_cdc_if.c b/devices/stm32f4/USB_DEVICE/App/usbd_cdc_if.c index 68f2cfb..7594b98 100644 --- a/devices/stm32f4/USB_DEVICE/App/usbd_cdc_if.c +++ b/devices/stm32f4/USB_DEVICE/App/usbd_cdc_if.c @@ -22,7 +22,7 @@ #include "usbd_cdc_if.h" /* USER CODE BEGIN INCLUDE */ - +#include "tim.h" /* USER CODE END INCLUDE */ /* Private typedef -----------------------------------------------------------*/ @@ -261,6 +261,17 @@ static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len) { /* USER CODE BEGIN 6 */ + /* TIM1 clock (APB2 timer clock) = 32 MHz + * PWM frequency = 1 kHz + * Period = 1100 for easy pulse calculation + * pulse = 1000 => PWM output 3V (pulse 1100 => 3.3V) + * pulse = (10 * cpu usage). + * Prescaler = 28 for 1 kHz PWM. (exact 28.09) + * Received Buf is 4 byte little endian float cpu usage percent + */ + int value = (int)(*(float *)Buf * 10.0); + __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, value); + USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]); USBD_CDC_ReceivePacket(&hUsbDeviceFS); return (USBD_OK);