board.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "board.h"
  2. /* private defines */
  3. /* private variables */
  4. static volatile uint32_t TDelay;
  5. /* private typedef */
  6. /* private functions */
  7. static void GPIO_Init(void);
  8. static void ADC_Init(void);
  9. static void TIM1_Init(void);
  10. static void TIM3_Init(void);
  11. static void IWDG_Init(void);
  12. /* Board perephireal Configuration */
  13. void Board_Init(void)
  14. {
  15. /* Main peripheral clock enable */
  16. RCC->APBENR1 = (RCC_APBENR1_PWREN | RCC_APBENR1_TIM3EN);
  17. RCC->APBENR2 = (RCC_APBENR2_SYSCFGEN | RCC_APBENR2_ADCEN | RCC_APBENR2_TIM1EN);
  18. /* GPIO Ports Clock Enable */
  19. RCC->IOPENR = (RCC_IOPENR_GPIOAEN | RCC_IOPENR_GPIOBEN);
  20. /* Peripheral interrupt init*/
  21. /* RCC_IRQn interrupt configuration */
  22. NVIC_SetPriority(RCC_IRQn, 0);
  23. NVIC_EnableIRQ(RCC_IRQn);
  24. /* Configure the system clock */
  25. SystemClock_Config();
  26. /* Configure SysTick */
  27. SysTick->LOAD = (uint32_t)(SystemCoreClock/1000 - 1UL); /* set reload register */
  28. NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */
  29. SysTick->VAL = 0UL; /* Load the SysTick Counter Value */
  30. SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
  31. SysTick_CTRL_TICKINT_Msk |
  32. SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */
  33. /* Processor uses sleep as its low power mode */
  34. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
  35. /* DisableSleepOnExit */
  36. SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPONEXIT_Msk);
  37. /* Initialize all configured peripherals */
  38. GPIO_Init();
  39. ADC_Init();
  40. TIM1_Init();
  41. //TIM3_Init();
  42. //IWDG_Init();
  43. }
  44. /**
  45. * @brief System Clock Configuration
  46. * @retval None
  47. */
  48. void SystemClock_Config(void)
  49. {
  50. /* HSI configuration and activation */
  51. RCC->CR |= RCC_CR_HSION; // Enable HSI
  52. while((RCC->CR & RCC_CR_HSIRDY) == 0);
  53. /* Main PLL configuration and activation */
  54. RCC->PLLCFGR = (RCC_PLLCFGR_PLLSRC_HSI | RCC_PLLCFGR_PLLM_0 | (9 << RCC_PLLCFGR_PLLN_Pos) | RCC_PLLCFGR_PLLR_1);
  55. RCC->PLLCFGR |= RCC_PLLCFGR_PLLREN; // RCC_PLL_EnableDomain_SYS
  56. RCC->CR |= RCC_CR_PLLON; // RCC_PLL_Enable
  57. while((RCC->CR & RCC_CR_PLLRDY) == 0);
  58. /* Sysclk activation on the main PLL */
  59. RCC->CFGR &= RCC_CFGR_SW;
  60. RCC->CFGR |= RCC_CFGR_SW_1;
  61. while((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_1);
  62. /* Update CMSIS variable (which can be updated also through SystemCoreClockUpdate function) */
  63. SystemCoreClock = 24000000;
  64. }
  65. /**
  66. * @brief GPIO Initialization Function
  67. * @param None
  68. * @retval None
  69. */
  70. static void GPIO_Init(void)
  71. {
  72. /* Servo_1_Pin, Servo_2_Pin - Servos control, Alt PP out, middle speed */
  73. #ifdef USE_SERVO_1
  74. GPIO_SetAFPin_0_7(Servo_1_Port, Servo_1_Pin, GPIO_AF_1);
  75. GPIO_SetPinOutputType(Servo_1_Port, Servo_1_Pin, GPIO_OTYPE_PP);
  76. GPIO_SetPinPull(Servo_1_Port, Servo_1_Pin, GPIO_PUPDR_NO);
  77. GPIO_SetPinSpeed(Servo_1_Port, Servo_1_Pin, GPIO_OSPEED_LW);
  78. GPIO_SetPinMode(Servo_1_Port, Servo_1_Pin, GPIO_MODE_AFF);
  79. #endif
  80. #ifdef USE_SERVO_2
  81. GPIO_SetAFPin_8_15(Servo_2_Port, Servo_2_Pin, GPIO_AF_2);
  82. GPIO_SetPinOutputType(Servo_2_Port, Servo_2_Pin, GPIO_OTYPE_PP);
  83. GPIO_SetPinPull(Servo_2_Port, Servo_2_Pin, GPIO_PUPDR_NO);
  84. GPIO_SetPinSpeed(Servo_2_Port, Servo_2_Pin, GPIO_OSPEED_LW);
  85. GPIO_SetPinMode(Servo_2_Port, Servo_2_Pin, GPIO_MODE_AFF);
  86. #endif
  87. /* Photo_Pin: analog in, pull none */
  88. GPIO_SetPinPull(Photo_Port, Photo_Pin, GPIO_PUPDR_NO);
  89. GPIO_SetPinMode(Photo_Port, Photo_Pin, GPIO_MODE_ANL);
  90. /* Num of chnls select pin */
  91. GPIO_SetPinPull(Select_Port, Select_Pin, GPIO_PUPDR_UP);
  92. }
  93. /**
  94. * @brief ADC1 Initialization Function
  95. * @param None
  96. * @retval None
  97. */
  98. static void ADC_Init(void)
  99. {
  100. /* ADC1 interrupt Init */
  101. NVIC_SetPriority(ADC1_IRQn, 0);
  102. NVIC_EnableIRQ(ADC1_IRQn);
  103. /* Configure the global features of the ADC
  104. * (Clock, Resolution, Data Alignment and number of conversion)
  105. */
  106. //ADC synchronous clock derived from AHB clock divided by 2
  107. ADC1->CFGR2 |= ADC_CFGR2_CKMODE_0;
  108. /* Clear flag ADC channel configuration ready */
  109. ADC1->ISR |= ADC_ISR_CCRDY;
  110. /* External trigger - Tim3 */
  111. //ADC1->CFGR1 |= (ADC_CFGR1_EXTEN_0 | ADC_CFGR1_EXTSEL_1 | ADC_CFGR1_EXTSEL_0 | ADC_CFGR1_OVRMOD);
  112. ADC1->CFGR1 = (ADC_CFGR1_EXTEN_0 |ADC_CFGR1_OVRMOD);
  113. /* Enable ADC internal voltage regulator */
  114. ADC1->CR |= ADC_CR_ADVREGEN; // ???
  115. /** Configure Regular Channel */
  116. ADC1->CHSELR = ADC_CHSELR_CHSEL11;
  117. /* Poll for ADC channel configuration ready */
  118. while ((ADC1->ISR & ADC_ISR_CCRDY) == 0) {};
  119. /* Clear flag ADC channel configuration ready */
  120. ADC1->ISR |= ADC_ISR_CCRDY;
  121. // SetChannelSamplingTime
  122. ADC1->SMPR |= ADC_SMPR_SMPSEL11;
  123. /* Calibration in single conversion mode */
  124. ADC1->CR |= ADC_CR_ADCAL; // ADC calibration
  125. while ((ADC1->CR & ADC_CR_ADCAL) != 0) {};
  126. /* Enable Interrupt */
  127. ADC1->IER |= ADC_IER_EOCIE; // ADC interrupt enable
  128. /* Enable ADC */
  129. ADC1->CR |= ADC_CR_ADEN; // ADC enable
  130. /* Start ADC software conversion */
  131. ADC1->CR |= ADC_CR_ADSTART; // ADC start
  132. }
  133. /**
  134. * @brief TIM1 Initialization Function
  135. * @param None
  136. * @retval None
  137. */
  138. static void TIM1_Init(void)
  139. {
  140. /* target clock */
  141. TIM1->PSC = TIM1_PSC; // prescaler
  142. TIM1->ARR = TIM1_ARR; // auto reload value
  143. TIM1->CR1 = TIM_CR1_ARPE;
  144. // initial pwm value
  145. TIM1->CCR1 = SERVO_INIT_VAL;
  146. TIM1->CCR2 = SERVO_INIT_VAL;
  147. // pwm mode 1 for chanels
  148. #ifdef USE_SERVO_1
  149. TIM1->CCMR1 = (TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2PE);
  150. #endif
  151. #ifdef USE_SERVO_2
  152. TIM1->CCMR1 |= (TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1PE);
  153. #endif
  154. //TIM1->SR |= TIM_SR_UIF;
  155. TIM1->BDTR = TIM_BDTR_MOE; // enable main output
  156. TIM1->EGR = TIM_EGR_UG; // force timer update
  157. /* TIM1 CC_EnableChannel */
  158. #ifdef USE_SERVO_1
  159. TIM1->CCER = (TIM_CCER_CC2E | TIM_CCER_CC2P);
  160. #endif
  161. #ifdef USE_SERVO_2
  162. TIM1->CCER |= (TIM_CCER_CC1E | TIM_CCER_CC1P);
  163. #endif
  164. /* Set the trigger output 2 (TRGO2) used for ADC synchronization */
  165. TIM1->CR2 |= TIM_CR2_MMS2_1; // update event
  166. /* TIM_EnableCounter */
  167. TIM1->CR1 |= TIM_CR1_CEN;
  168. }
  169. /**
  170. * @brief TIM3 Initialization Function
  171. * @param None
  172. * @retval None
  173. */
  174. static void TIM3_Init(void)
  175. {
  176. /* target clock */
  177. TIM3->PSC = TIM3_PSC; // prescaler
  178. TIM3->ARR = TIM3_ARR; // auto reload value
  179. TIM3->CR1 = (TIM_CR1_ARPE | TIM_CR1_DIR);
  180. // launch timer
  181. TIM3->EGR = TIM_EGR_TG | TIM_EGR_UG; // force timer update
  182. /* Set the trigger output 2 (TRGO2) used for ADC synchronization */
  183. TIM3->CR2 |= TIM_CR2_MMS2_1; // update event
  184. /* TIM3 enable */
  185. TIM3->CR1 |= TIM_CR1_CEN;
  186. }
  187. /**
  188. * @brief IWDG Initialization Function
  189. * @param None
  190. * @retval None
  191. */
  192. static void IWDG_Init(void)
  193. {
  194. IWDG->KR = 0xCCCC;
  195. IWDG->KR = 0x5555;
  196. IWDG->PR = 0x0;
  197. IWDG->RLR = 4095;
  198. while (IWDG->SR != 0x00000000) {};
  199. IWDG->KR = 0xAAAA;
  200. }
  201. /**
  202. * @brief Inserts a delay time.
  203. * @param msec: specifies the delay time length, in milliseconds.
  204. * @retval None
  205. */
  206. void delay_ms(uint32_t msek) {
  207. TDelay = msek;
  208. do {
  209. __WFI();
  210. } while (TDelay != 0);
  211. }
  212. #pragma GCC optimize ("O3")
  213. /**
  214. * @brief This function handles SysTick Handler.
  215. * @param None
  216. * @retval None
  217. */
  218. void SysTick_Handler(void) {
  219. if (TDelay != 0) {
  220. TDelay --;
  221. }
  222. }