From 464dbc78e424a4ff5675a3ac26cbfe41e739676d Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 11 Nov 2021 19:59:53 +0100 Subject: [PATCH] LED PWM WIP --- fw/.mxproject | 16 ++++++++-------- fw/Core/Inc/rgbled.h | 4 ++++ fw/Core/Src/main.c | 18 +++++++++--------- fw/Core/Src/rgbled.c | 15 ++++++++++++--- fw/iaq_wired_sensor.ioc | 12 +++++++++--- 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/fw/.mxproject b/fw/.mxproject index 13a39f9..00bcebb 100644 --- a/fw/.mxproject +++ b/fw/.mxproject @@ -1,5 +1,5 @@ [PreviousLibFiles] -LibFiles=Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_gpio.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_i2c.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_dma.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_bus.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_cortex.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_rcc.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_system.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_utils.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_exti.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_pwr.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_crs.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_tim.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_usart.h;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_gpio.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_i2c.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_dma.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_rcc.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_utils.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_exti.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_pwr.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_tim.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_usart.c;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_gpio.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_i2c.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_dma.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_bus.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_cortex.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_rcc.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_system.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_utils.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_exti.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_pwr.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_crs.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_tim.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_usart.h;Drivers/CMSIS/Device/ST/STM32L0xx/Include/stm32l031xx.h;Drivers/CMSIS/Device/ST/STM32L0xx/Include/stm32l0xx.h;Drivers/CMSIS/Device/ST/STM32L0xx/Include/system_stm32l0xx.h;Drivers/CMSIS/Device/ST/STM32L0xx/Source/Templates/system_stm32l0xx.c;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/core_armv8mbl.h;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/cmsis_version.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/mpu_armv7.h;Drivers/CMSIS/Include/mpu_armv8.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/core_sc300.h;Drivers/CMSIS/Include/tz_context.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/core_cm4.h;Drivers/CMSIS/Include/cmsis_armcc.h;Drivers/CMSIS/Include/core_cm3.h; +LibFiles=Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_gpio.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_i2c.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_dma.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_bus.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_cortex.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_rcc.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_system.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_utils.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_exti.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_pwr.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_crs.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_tim.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_usart.h;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_gpio.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_i2c.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_dma.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_rcc.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_utils.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_exti.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_pwr.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_tim.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_usart.c;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_gpio.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_i2c.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_dma.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_bus.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_cortex.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_rcc.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_system.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_utils.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_exti.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_pwr.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_crs.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_tim.h;Drivers/STM32L0xx_HAL_Driver/Inc/stm32l0xx_ll_usart.h;Drivers/CMSIS/Device/ST/STM32L0xx/Include/stm32l031xx.h;Drivers/CMSIS/Device/ST/STM32L0xx/Include/stm32l0xx.h;Drivers/CMSIS/Device/ST/STM32L0xx/Include/system_stm32l0xx.h;Drivers/CMSIS/Device/ST/STM32L0xx/Source/Templates/system_stm32l0xx.c;Drivers/CMSIS/Include/core_armv8mbl.h;Drivers/CMSIS/Include/cmsis_version.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/core_sc300.h;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/cmsis_armcc.h;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/core_cm3.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/core_cm4.h;Drivers/CMSIS/Include/tz_context.h;Drivers/CMSIS/Include/mpu_armv8.h;Drivers/CMSIS/Include/mpu_armv7.h; [PreviousUsedCubeIDEFiles] SourceFiles=Core/Src/main.c;Core/Src/stm32l0xx_it.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_gpio.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_i2c.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_dma.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_rcc.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_utils.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_exti.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_pwr.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_tim.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_usart.c;Drivers/CMSIS/Device/ST/STM32L0xx/Source/Templates/system_stm32l0xx.c;Core/Src/system_stm32l0xx.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_gpio.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_i2c.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_dma.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_rcc.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_utils.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_exti.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_pwr.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_tim.c;Drivers/STM32L0xx_HAL_Driver/Src/stm32l0xx_ll_usart.c;Drivers/CMSIS/Device/ST/STM32L0xx/Source/Templates/system_stm32l0xx.c;Core/Src/system_stm32l0xx.c;;; @@ -9,16 +9,16 @@ CDefines=USE_FULL_LL_DRIVER;HSE_VALUE:8000000;HSE_STARTUP_TIMEOUT:100;LSE_STARTU [PreviousGenFiles] AdvancedFolderStructure=true HeaderFileListSize=3 -HeaderFiles#0=/home/mrs/Personal/HDIoT/Smart_Household/iaq_wired_sensor/fw/Core/Inc/stm32l0xx_it.h -HeaderFiles#1=/home/mrs/Personal/HDIoT/Smart_Household/iaq_wired_sensor/fw/Core/Inc/stm32_assert.h -HeaderFiles#2=/home/mrs/Personal/HDIoT/Smart_Household/iaq_wired_sensor/fw/Core/Inc/main.h +HeaderFiles#0=/home/dooku/projekty/hdiot/iaq_wired_sensor/fw/Core/Inc/stm32l0xx_it.h +HeaderFiles#1=/home/dooku/projekty/hdiot/iaq_wired_sensor/fw/Core/Inc/stm32_assert.h +HeaderFiles#2=/home/dooku/projekty/hdiot/iaq_wired_sensor/fw/Core/Inc/main.h HeaderFolderListSize=1 -HeaderPath#0=/home/mrs/Personal/HDIoT/Smart_Household/iaq_wired_sensor/fw/Core/Inc +HeaderPath#0=/home/dooku/projekty/hdiot/iaq_wired_sensor/fw/Core/Inc HeaderFiles=; SourceFileListSize=2 -SourceFiles#0=/home/mrs/Personal/HDIoT/Smart_Household/iaq_wired_sensor/fw/Core/Src/stm32l0xx_it.c -SourceFiles#1=/home/mrs/Personal/HDIoT/Smart_Household/iaq_wired_sensor/fw/Core/Src/main.c +SourceFiles#0=/home/dooku/projekty/hdiot/iaq_wired_sensor/fw/Core/Src/stm32l0xx_it.c +SourceFiles#1=/home/dooku/projekty/hdiot/iaq_wired_sensor/fw/Core/Src/main.c SourceFolderListSize=1 -SourcePath#0=/home/mrs/Personal/HDIoT/Smart_Household/iaq_wired_sensor/fw/Core/Src +SourcePath#0=/home/dooku/projekty/hdiot/iaq_wired_sensor/fw/Core/Src SourceFiles=; diff --git a/fw/Core/Inc/rgbled.h b/fw/Core/Inc/rgbled.h index d285d2a..a9fdd56 100644 --- a/fw/Core/Inc/rgbled.h +++ b/fw/Core/Inc/rgbled.h @@ -18,6 +18,9 @@ #define RGBLED_OK 0 #define RGBLED_ERROR -1 // generic error +#define RGBLED_PRESCALER 0 +#define RGBLED_PERIOD 255 + /* * Type definitions */ @@ -26,6 +29,7 @@ typedef struct { // tim2 ch1 B // tim22 ch1 G // tim22 ch2 R + // TODO union, aby se dalo nastavovat LED v cyklu (set_color) TIM_TypeDef *timer_R; TIM_TypeDef *timer_G; TIM_TypeDef *timer_B; diff --git a/fw/Core/Src/main.c b/fw/Core/Src/main.c index 26fa05b..85ab184 100644 --- a/fw/Core/Src/main.c +++ b/fw/Core/Src/main.c @@ -220,11 +220,12 @@ int main(void) rgbled_context.timer_R = TIM22; rgbled_context.timer_G = TIM22; rgbled_context.timer_B = TIM2; + rgbled_context.channel_R = 2; + rgbled_context.channel_G = 1; + rgbled_context.channel_B = 1; rgbled_init(&rgbled_context); - LL_TIM_OC_SetCompareCH1(TIM2, 0); // B - LL_TIM_OC_SetCompareCH1(TIM22, 65535); // G - LL_TIM_OC_SetCompareCH2(TIM22, 65535); // R + rgbled_set_color(255, 0, 0); while (1) { // rgbled_set_color(30000, 0, 0); @@ -513,9 +514,9 @@ static void MX_TIM2_Init(void) /* USER CODE BEGIN TIM2_Init 1 */ /* USER CODE END TIM2_Init 1 */ - TIM_InitStruct.Prescaler = 0; + TIM_InitStruct.Prescaler = RGBLED_PRESCALER; TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; - TIM_InitStruct.Autoreload = 65535; + TIM_InitStruct.Autoreload = RGBLED_PERIOD; TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; LL_TIM_Init(TIM2, &TIM_InitStruct); LL_TIM_DisableARRPreload(TIM2); @@ -607,21 +608,20 @@ static void MX_TIM22_Init(void) /* USER CODE BEGIN TIM22_Init 1 */ /* USER CODE END TIM22_Init 1 */ - TIM_InitStruct.Prescaler = 0; + TIM_InitStruct.Prescaler = RGBLED_PRESCALER; TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP; - TIM_InitStruct.Autoreload = 65535; + TIM_InitStruct.Autoreload = RGBLED_PERIOD; TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1; LL_TIM_Init(TIM22, &TIM_InitStruct); LL_TIM_DisableARRPreload(TIM22); LL_TIM_OC_EnablePreload(TIM22, LL_TIM_CHANNEL_CH1); TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1; TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE; - TIM_OC_InitStruct.CompareValue = 32768; + TIM_OC_InitStruct.CompareValue = 0; TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH; LL_TIM_OC_Init(TIM22, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct); LL_TIM_OC_DisableFast(TIM22, LL_TIM_CHANNEL_CH1); LL_TIM_OC_EnablePreload(TIM22, LL_TIM_CHANNEL_CH2); - TIM_OC_InitStruct.CompareValue = 0; LL_TIM_OC_Init(TIM22, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct); LL_TIM_OC_DisableFast(TIM22, LL_TIM_CHANNEL_CH2); LL_TIM_SetTriggerOutput(TIM22, LL_TIM_TRGO_RESET); diff --git a/fw/Core/Src/rgbled.c b/fw/Core/Src/rgbled.c index 9cc6334..c3c998e 100644 --- a/fw/Core/Src/rgbled.c +++ b/fw/Core/Src/rgbled.c @@ -25,9 +25,18 @@ int8_t rgbled_set_color(uint16_t R, uint16_t G, uint16_t B) // tim2 ch1 B // tim22 ch1 G // tim22 ch2 R - LL_TIM_OC_SetCompareCH1(TIM2, (uint32_t)B); // B - LL_TIM_OC_SetCompareCH1(TIM22, (uint32_t)G); // G - LL_TIM_OC_SetCompareCH2(TIM22, (uint32_t)R); // R + if ( + R > RGBLED_PERIOD || \ + G > RGBLED_PERIOD || \ + B > RGBLED_PERIOD) + { + return RGBLED_ERROR; + } + LL_TIM_OC_SetCompareCH1(rgbled_context->timer_B, (uint32_t)(RGBLED_PERIOD - B)); // B + LL_TIM_OC_SetCompareCH1(rgbled_context->timer_G, (uint32_t)(RGBLED_PERIOD - G)); // G + LL_TIM_OC_SetCompareCH2(rgbled_context->timer_R, (uint32_t)(RGBLED_PERIOD - R)); // R + + return RGBLED_OK; } int8_t rgbled_off(void) diff --git a/fw/iaq_wired_sensor.ioc b/fw/iaq_wired_sensor.ioc index 91fa60c..a53f467 100644 --- a/fw/iaq_wired_sensor.ioc +++ b/fw/iaq_wired_sensor.ioc @@ -133,7 +133,10 @@ SH.S_TIM22_CH2.ConfNb=1 SH.S_TIM2_CH1.0=TIM2_CH1,PWM Generation1 CH1 SH.S_TIM2_CH1.ConfNb=1 TIM2.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 -TIM2.IPParameters=Channel-PWM Generation1 CH1 +TIM2.IPParameters=Channel-PWM Generation1 CH1,Prescaler,Period +TIM2.IPParametersWithoutCheck=Prescaler,Period +TIM2.Period=RGBLED_PERIOD +TIM2.Prescaler=RGBLED_PRESCALER TIM21.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE TIM21.IPParameters=Prescaler,Period,AutoReloadPreload TIM21.IPParametersWithoutCheck=Prescaler,Period @@ -141,8 +144,11 @@ TIM21.Period=tim21_period TIM21.Prescaler=tim21_prescaler TIM22.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 TIM22.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 -TIM22.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2,Pulse-PWM Generation1 CH1 -TIM22.Pulse-PWM\ Generation1\ CH1=32768 +TIM22.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2,Pulse-PWM Generation1 CH1,Period,Prescaler +TIM22.IPParametersWithoutCheck=Prescaler,Period +TIM22.Period=RGBLED_PERIOD +TIM22.Prescaler=RGBLED_PRESCALER +TIM22.Pulse-PWM\ Generation1\ CH1=0 USART2.BaudRate=config_baudrates[sensor_config.baudrate_index] USART2.IPParameters=VirtualMode-Asynchronous,VirtualMode-Hardware Flow Control (RS485),BaudRate,WordLength,Parity USART2.IPParametersWithoutCheck=BaudRate