Advertisement
Guest User

Untitled

a guest
Nov 24th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.27 KB | None | 0 0
  1. /*----------------------------------------------------------------------------
  2.  * Name:    Blinky.c
  3.  * Purpose: LED Flasher for STM32F4-Discovery
  4.  *----------------------------------------------------------------------------
  5.  * This file is part of the uVision/ARM development tools.
  6.  * This software may only be used under the terms of a valid, current,
  7.  * end user licence from KEIL for a compatible version of KEIL software
  8.  * development tools. Nothing else gives you the right to use this software.
  9.  *
  10.  * This software is supplied "AS IS" without warranties of any kind.
  11.  *
  12.  * Copyright (c) 2004-2015 Keil - An ARM Company. All rights reserved.
  13.  *----------------------------------------------------------------------------*/
  14.  
  15. #include "stm32f4xx_hal.h"              // Keil::Device:STM32Cube HAL:Common
  16. #include "cmsis_os.h"                   // ARM::CMSIS:RTOS:Keil RTX
  17. #include "Board_LED.h"                  // ::Board Support:LED
  18. #include "Board_Buttons.h"              // ::Board Support:Buttons
  19. #include "RTE_Components.h"             // Component selection
  20. #include "stm32f4xx.h"
  21. #include "stm32f4xx_hal_rcc.h"
  22. #include "stm32f4xx_hal_gpio.h"
  23. #include <math.h>
  24. extern int Init_Thread_LED(void);
  25. extern osThreadId tid_Thread_LED;
  26.  
  27. #ifdef RTE_CMSIS_RTOS_RTX
  28. extern uint32_t os_time;
  29.  
  30. uint32_t HAL_GetTick(void) {return os_time;}
  31. #endif
  32.  
  33. /**
  34.   * System Clock Configuration
  35.   */
  36. void SystemClock_Config(void) {
  37.   RCC_OscInitTypeDef RCC_OscInitStruct;
  38.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  39.  
  40.   /* Enable Power Control clock */
  41.   __HAL_RCC_PWR_CLK_ENABLE();
  42.  
  43.   /* The voltage scaling allows optimizing the power consumption when the
  44.      device is clocked below the maximum system frequency (see datasheet). */
  45.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  46.  
  47.   /* Enable HSE Oscillator and activate PLL with HSE as source */
  48.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  49.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  50.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  51.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  52.   RCC_OscInitStruct.PLL.PLLM = 8;
  53.   RCC_OscInitStruct.PLL.PLLN = 336;
  54.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  55.   RCC_OscInitStruct.PLL.PLLQ = 7;
  56.   HAL_RCC_OscConfig(&RCC_OscInitStruct);
  57.  
  58.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  59.      clocks dividers */
  60.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 |
  61.                                 RCC_CLOCKTYPE_PCLK2;
  62.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  63.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  64.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  65.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  66.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
  67. //__APB1_FORCE_RESET();
  68. //__APB2_FORCE_RESET();
  69. }
  70.  
  71. int32_t GPIOA_Initialize (void) {
  72.   GPIO_InitTypeDef GPIO_InitStruct;
  73.  
  74.   /* GPIO Ports Clock Enable */
  75.   __GPIOA_CLK_ENABLE();
  76.  
  77.   /* Configure GPIO pins: PD12 PD13 PD14 PD15 */
  78.   GPIO_InitStruct.Pin   = GPIO_PIN_0;
  79.   GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
  80.   GPIO_InitStruct.Pull  = GPIO_PULLDOWN;
  81.   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  82.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  83.  
  84.   return 0;
  85. }
  86.  
  87. int32_t LED_Initialize3 (void) {
  88.   GPIO_InitTypeDef GPIO_InitStruct;
  89.   /* GPIO Ports Clock Enable */
  90.   __GPIOA_CLK_ENABLE();
  91.  
  92.   GPIO_InitStruct.Pin   = GPIO_PIN_0;
  93.   GPIO_InitStruct.Mode  = GPIO_MODE_INPUT;
  94.   GPIO_InitStruct.Pull  = GPIO_NOPULL;
  95.   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  96.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  97.  
  98.   return 0;
  99. }
  100.  
  101.  
  102. int32_t LED_Initialize2 (void) {
  103.   GPIO_InitTypeDef GPIO_InitStruct;
  104.  
  105.   /* GPIO Ports Clock Enable */
  106.   __GPIOB_CLK_ENABLE();
  107.  
  108.   /* Configure GPIO pins: PD12 PD13 PD14 PD15 */
  109.   GPIO_InitStruct.Pin   = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_3;
  110.   GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
  111.   GPIO_InitStruct.Pull  = GPIO_PULLDOWN;
  112.   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  113.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  114.  
  115.   return 0;
  116. }
  117.  
  118.  
  119. int32_t GPIO_PWM_INIT (void) {
  120.  
  121.     GPIO_InitTypeDef GPIO_InitStruct;
  122.   __GPIOA_CLK_ENABLE();
  123.   GPIO_InitStruct.Pin   =   GPIO_PIN_0;
  124.   GPIO_InitStruct.Mode  =   GPIO_MODE_AF_PP;
  125.   GPIO_InitStruct.Pull  =   GPIO_NOPULL;
  126.   GPIO_InitStruct.Speed =   GPIO_SPEED_LOW;
  127.     GPIO_InitStruct.Alternate=GPIO_AF1_TIM2;
  128.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  129.    
  130.   return 0;
  131. }
  132.  
  133.  
  134. float calc_pwm(float val)
  135. {
  136.  const float k = 0.1f;
  137.  const float x0 = 60.0f;
  138.  return 300.0f / (1.0f + exp(-k * (val - x0)));
  139. }
  140.  
  141. void SetComparators(TIM_TypeDef* TIMx,float a1,float a2,float a3)
  142. {
  143. TIMx->CCR1 = a1;   
  144. TIMx->CCR2 = a2;   
  145. TIMx->CCR3 = a3;   
  146. }
  147. void InitTimer(int32_t PSC,int32_t REL)
  148. {
  149.     __TIM3_CLK_ENABLE();
  150.     TIM3->PSC = PSC;        
  151.     TIM3->ARR = REL;           // Auto reload value 1000
  152.     TIM3->DIER |= TIM_DIER_UIE; // Enable update interrupt (timer level)
  153.     TIM3->CR1 |= TIM_CR1_CEN;   // Enable timer
  154.   NVIC_EnableIRQ(TIM3_IRQn); // Enable interrupt from TIM3 (NVIC level)
  155. }
  156.  
  157.  
  158. extern void PWM_BD_INIT(TIM_TypeDef* TIMx,uint16_t Mode,uint16_t Polarity,uint16_t OutputState,uint16_t Pulse);
  159. extern void PWM_BD_INIT2(TIM_TypeDef* TIMx,uint16_t Mode,uint16_t Polarity,uint16_t OutputState,uint16_t Pulse);
  160. extern void PWM_BD_INIT3(TIM_TypeDef* TIMx,uint16_t Mode,uint16_t Polarity,uint16_t OutputState,uint16_t Pulse);
  161. void InitPWMTimer(int32_t PSC,int32_t REL)
  162. {
  163.     int16_t tmpcr1=0;
  164.     __TIM2_CLK_ENABLE();
  165.     TIM2->PSC = PSC;        
  166.     TIM2->ARR = REL;           // Auto reload value 1000
  167.     TIM2->DIER |= TIM_DIER_UIE; // Enable update interrupt (timer level)
  168.     PWM_BD_INIT(TIM2,0x0070,0x0000,0x0001,0x0000);
  169.     PWM_BD_INIT2(TIM2,0x0070,0x0000,0x0001,0x0000);
  170.     PWM_BD_INIT3(TIM2,0x0070,0x0000,0x0001,0x0000);
  171.     TIM2->CR1 |= TIM_CR1_CEN;   // Enable timer
  172.   NVIC_EnableIRQ(TIM2_IRQn); // Enable interrupt from TIM3 (NVIC level)
  173. }
  174.  
  175. unsigned swiec=0;
  176. extern void StatusUP();
  177. void TIM3_IRQHandler(void)
  178. {
  179. if(TIM3->SR & TIM_SR_UIF){TIM3->SR &= ~TIM_SR_UIF;swiec=swiec==0?1:0; StatusUP();}
  180. }
  181.  
  182. void TIM2_IRQHandler(void)
  183. {
  184. //if(TIM2->SR & TIM_SR_UIF){TIM2->SR &= ~TIM_SR_UIF;swiec=swiec==0?1:0; StatusUP();}
  185. }
  186.  
  187. /**
  188.   * Main function
  189.   */
  190. void StatusUP()
  191. {
  192.     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, swiec?GPIO_PIN_SET:GPIO_PIN_RESET);
  193. }
  194.  
  195.  
  196. void PWM_BD_INIT(TIM_TypeDef* TIMx,uint16_t Mode,uint16_t Polarity,uint16_t OutputState,uint16_t Pulse)
  197. {
  198.     uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  199.     TIMx->CCER &= (uint16_t)(~(uint16_t)TIM_CCER_CC1E);
  200.     tmpccer = TIMx->CCER;
  201.     tmpcr2 =  TIMx->CR2;
  202.     tmpccmrx = TIMx->CCMR1;
  203.     tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_OC1M));
  204.     tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_CC1S));
  205.     tmpccmrx |= Mode;
  206.     tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC1P));
  207.     tmpccer |= Polarity;
  208.     tmpccer |= OutputState;
  209.     TIMx->CR2 = tmpcr2;
  210.     TIMx->CCMR1 = tmpccmrx;
  211.     TIMx->CCR1 = Pulse;
  212.     TIMx->CCER = tmpccer;
  213. }
  214.  
  215. void PWM_BD_INIT2(TIM_TypeDef* TIMx,uint16_t Mode,uint16_t Polarity,uint16_t OutputState,uint16_t Pulse)
  216. {
  217.     uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  218.     TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC2E));
  219.     tmpccer = TIMx->CCER;
  220.     tmpcr2 =  TIMx->CR2;
  221.     tmpccmrx = TIMx->CCMR1;
  222.     tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_OC2M));
  223.     tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_CC2S));
  224.     tmpccmrx |=(uint16_t)(Mode << 8);
  225.     tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC2P));
  226.     tmpccer |=  (uint16_t)(Polarity << 4);
  227.     tmpccer |= (uint16_t)(OutputState << 4);
  228.     TIMx->CR2 = tmpcr2;
  229.     TIMx->CCMR1 = tmpccmrx;
  230.     TIMx->CCR2 = Pulse;
  231.     TIMx->CCER = tmpccer;
  232. }
  233.  
  234.  
  235. void PWM_BD_INIT3(TIM_TypeDef* TIMx,uint16_t Mode,uint16_t Polarity,uint16_t OutputState,uint16_t Pulse)
  236. {
  237.     uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  238.     TIMx->CCER &= (uint16_t)(~((uint16_t)TIM_CCER_CC3E));
  239.     tmpccer = TIMx->CCER;
  240.     tmpcr2 =  TIMx->CR2;
  241.     tmpccmrx = TIMx->CCMR2;
  242.   tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR2_OC3M));
  243.   tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR2_CC3S));  
  244.     tmpccmrx |=(uint16_t)(Mode);
  245.     tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC3P));
  246.     tmpccer |=  (uint16_t)(Polarity << 8);
  247.     tmpccer |= (uint16_t)(OutputState << 8);
  248.     TIMx->CR2 = tmpcr2;
  249.     TIMx->CCMR2 = tmpccmrx;
  250.     TIMx->CCR3 = Pulse;
  251.     TIMx->CCER = tmpccer;
  252. }
  253.  
  254.  
  255.  
  256.  
  257. int main (void) {
  258.  
  259.    
  260.    
  261.   osKernelInitialize();                     /* initialize CMSIS-RTOS          */
  262.   HAL_Init();                               /* Initialize the HAL Library     */
  263.  
  264.   SystemClock_Config();                     /* Configure the System Clock     */
  265.   LED_Initialize2();    /* Initialize LED                 */
  266.     //LED_Initialize3();
  267.     GPIO_PWM_INIT();
  268.     //InitTimer(23999,500);
  269.    
  270.     InitPWMTimer(64000 - 1,1000 - 1);
  271.    
  272.     //GPIOA_Initialize();
  273.    
  274.  // Buttons_Initialize();                     /* Initialize Buttons             */
  275.  
  276.   //Init_Thread_LED();                        /* Create LED thread              */
  277.   osKernelStart();                          /* start thread execution         */
  278.     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
  279.     float cnt=0;
  280.   while (1)
  281.     {
  282.  float r = 50 * (1.0f + sin(cnt / 100.0f));
  283.  float g = 50 * (1.0f + sin(1.5f * cnt / 100.0f));
  284.  float b = 50 * (1.0f + sin(2.0f * cnt / 100.0f));
  285.  SetComparators(TIM2,calc_pwm(b),calc_pwm(g),calc_pwm(r));
  286.  //if (cnt>100)cnt=0;
  287.         cnt++;
  288.     osDelay(10);                           /* Wait 500ms  */  
  289.   }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement