Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*----------------------------------------------------------------------------
- * Name: Blinky.c
- * Purpose: LED Flasher for STM32F4-Discovery
- *----------------------------------------------------------------------------
- * This file is part of the uVision/ARM development tools.
- * This software may only be used under the terms of a valid, current,
- * end user licence from KEIL for a compatible version of KEIL software
- * development tools. Nothing else gives you the right to use this software.
- *
- * This software is supplied "AS IS" without warranties of any kind.
- *
- * Copyright (c) 2004-2015 Keil - An ARM Company. All rights reserved.
- *----------------------------------------------------------------------------*/
- #include "stm32f4xx_hal.h" // Keil::Device:STM32Cube HAL:Common
- #include "cmsis_os.h" // ARM::CMSIS:RTOS:Keil RTX
- #include "Board_LED.h" // ::Board Support:LED
- #include "Board_Buttons.h" // ::Board Support:Buttons
- #include "RTE_Components.h" // Component selection
- #include "stm32f4xx.h"
- #include "stm32f4xx_hal_rcc.h"
- #include "stm32f4xx_hal_gpio.h"
- #include <math.h>
- extern int Init_Thread_LED(void);
- extern osThreadId tid_Thread_LED;
- #ifdef RTE_CMSIS_RTOS_RTX
- extern uint32_t os_time;
- uint32_t HAL_GetTick(void) {return os_time;}
- #endif
- /**
- * System Clock Configuration
- */
- void SystemClock_Config(void) {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /* Enable Power Control clock */
- __HAL_RCC_PWR_CLK_ENABLE();
- /* The voltage scaling allows optimizing the power consumption when the
- device is clocked below the maximum system frequency (see datasheet). */
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /* Enable HSE Oscillator and activate PLL with HSE as source */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 8;
- RCC_OscInitStruct.PLL.PLLN = 336;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 7;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
- /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
- clocks dividers */
- RCC_ClkInitStruct.ClockType = 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_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
- //__APB1_FORCE_RESET();
- //__APB2_FORCE_RESET();
- }
- int32_t GPIOA_Initialize (void) {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __GPIOA_CLK_ENABLE();
- /* Configure GPIO pins: PD12 PD13 PD14 PD15 */
- GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_PULLDOWN;
- GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- return 0;
- }
- int32_t LED_Initialize3 (void) {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __GPIOA_CLK_ENABLE();
- GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- return 0;
- }
- int32_t LED_Initialize2 (void) {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __GPIOB_CLK_ENABLE();
- /* Configure GPIO pins: PD12 PD13 PD14 PD15 */
- GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_3;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_PULLDOWN;
- GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- return 0;
- }
- int32_t GPIO_PWM_INIT (void) {
- GPIO_InitTypeDef GPIO_InitStruct;
- __GPIOA_CLK_ENABLE();
- GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
- GPIO_InitStruct.Alternate=GPIO_AF1_TIM2;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- return 0;
- }
- float calc_pwm(float val)
- {
- const float k = 0.1f;
- const float x0 = 60.0f;
- return 300.0f / (1.0f + exp(-k * (val - x0)));
- }
- void SetComparators(TIM_TypeDef* TIMx,float a1,float a2,float a3)
- {
- TIMx->CCR1 = a1;
- TIMx->CCR2 = a2;
- TIMx->CCR3 = a3;
- }
- void InitTimer(int32_t PSC,int32_t REL)
- {
- __TIM3_CLK_ENABLE();
- TIM3->PSC = PSC;
- TIM3->ARR = REL; // Auto reload value 1000
- TIM3->DIER |= TIM_DIER_UIE; // Enable update interrupt (timer level)
- TIM3->CR1 |= TIM_CR1_CEN; // Enable timer
- NVIC_EnableIRQ(TIM3_IRQn); // Enable interrupt from TIM3 (NVIC level)
- }
- extern void PWM_BD_INIT(TIM_TypeDef* TIMx,uint16_t Mode,uint16_t Polarity,uint16_t OutputState,uint16_t Pulse);
- extern void PWM_BD_INIT2(TIM_TypeDef* TIMx,uint16_t Mode,uint16_t Polarity,uint16_t OutputState,uint16_t Pulse);
- extern void PWM_BD_INIT3(TIM_TypeDef* TIMx,uint16_t Mode,uint16_t Polarity,uint16_t OutputState,uint16_t Pulse);
- void InitPWMTimer(int32_t PSC,int32_t REL)
- {
- int16_t tmpcr1=0;
- __TIM2_CLK_ENABLE();
- TIM2->PSC = PSC;
- TIM2->ARR = REL; // Auto reload value 1000
- TIM2->DIER |= TIM_DIER_UIE; // Enable update interrupt (timer level)
- PWM_BD_INIT(TIM2,0x0070,0x0000,0x0001,0x0000);
- PWM_BD_INIT2(TIM2,0x0070,0x0000,0x0001,0x0000);
- PWM_BD_INIT3(TIM2,0x0070,0x0000,0x0001,0x0000);
- TIM2->CR1 |= TIM_CR1_CEN; // Enable timer
- NVIC_EnableIRQ(TIM2_IRQn); // Enable interrupt from TIM3 (NVIC level)
- }
- unsigned swiec=0;
- extern void StatusUP();
- void TIM3_IRQHandler(void)
- {
- if(TIM3->SR & TIM_SR_UIF){TIM3->SR &= ~TIM_SR_UIF;swiec=swiec==0?1:0; StatusUP();}
- }
- void TIM2_IRQHandler(void)
- {
- //if(TIM2->SR & TIM_SR_UIF){TIM2->SR &= ~TIM_SR_UIF;swiec=swiec==0?1:0; StatusUP();}
- }
- /**
- * Main function
- */
- void StatusUP()
- {
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, swiec?GPIO_PIN_SET:GPIO_PIN_RESET);
- }
- void PWM_BD_INIT(TIM_TypeDef* TIMx,uint16_t Mode,uint16_t Polarity,uint16_t OutputState,uint16_t Pulse)
- {
- uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
- TIMx->CCER &= (uint16_t)(~(uint16_t)TIM_CCER_CC1E);
- tmpccer = TIMx->CCER;
- tmpcr2 = TIMx->CR2;
- tmpccmrx = TIMx->CCMR1;
- tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_OC1M));
- tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_CC1S));
- tmpccmrx |= Mode;
- tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC1P));
- tmpccer |= Polarity;
- tmpccer |= OutputState;
- TIMx->CR2 = tmpcr2;
- TIMx->CCMR1 = tmpccmrx;
- TIMx->CCR1 = Pulse;
- TIMx->CCER = tmpccer;
- }
- void PWM_BD_INIT2(TIM_TypeDef* TIMx,uint16_t Mode,uint16_t Polarity,uint16_t OutputState,uint16_t Pulse)
- {
- uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
- TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC2E));
- tmpccer = TIMx->CCER;
- tmpcr2 = TIMx->CR2;
- tmpccmrx = TIMx->CCMR1;
- tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_OC2M));
- tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_CC2S));
- tmpccmrx |=(uint16_t)(Mode << 8);
- tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC2P));
- tmpccer |= (uint16_t)(Polarity << 4);
- tmpccer |= (uint16_t)(OutputState << 4);
- TIMx->CR2 = tmpcr2;
- TIMx->CCMR1 = tmpccmrx;
- TIMx->CCR2 = Pulse;
- TIMx->CCER = tmpccer;
- }
- void PWM_BD_INIT3(TIM_TypeDef* TIMx,uint16_t Mode,uint16_t Polarity,uint16_t OutputState,uint16_t Pulse)
- {
- uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
- TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC3E));
- tmpccer = TIMx->CCER;
- tmpcr2 = TIMx->CR2;
- tmpccmrx = TIMx->CCMR2;
- tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR2_OC3M));
- tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR2_CC3S));
- tmpccmrx |=(uint16_t)(Mode);
- tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC3P));
- tmpccer |= (uint16_t)(Polarity << 8);
- tmpccer |= (uint16_t)(OutputState << 8);
- TIMx->CR2 = tmpcr2;
- TIMx->CCMR2 = tmpccmrx;
- TIMx->CCR3 = Pulse;
- TIMx->CCER = tmpccer;
- }
- int main (void) {
- osKernelInitialize(); /* initialize CMSIS-RTOS */
- HAL_Init(); /* Initialize the HAL Library */
- SystemClock_Config(); /* Configure the System Clock */
- LED_Initialize2(); /* Initialize LED */
- //LED_Initialize3();
- GPIO_PWM_INIT();
- //InitTimer(23999,500);
- InitPWMTimer(64000 - 1,1000 - 1);
- //GPIOA_Initialize();
- // Buttons_Initialize(); /* Initialize Buttons */
- //Init_Thread_LED(); /* Create LED thread */
- osKernelStart(); /* start thread execution */
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
- float cnt=0;
- while (1)
- {
- float r = 50 * (1.0f + sin(cnt / 100.0f));
- float g = 50 * (1.0f + sin(1.5f * cnt / 100.0f));
- float b = 50 * (1.0f + sin(2.0f * cnt / 100.0f));
- SetComparators(TIM2,calc_pwm(b),calc_pwm(g),calc_pwm(r));
- //if (cnt>100)cnt=0;
- cnt++;
- osDelay(10); /* Wait 500ms */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement