board.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. GPIO_SetPinMode(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_MODE_AFF);
  74. GPIO_SetPinOutputType(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_OTYPE_PP);
  75. GPIO_SetPinSpeed(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_OSPEED_LW);
  76. GPIO_SetPinPull(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_PUPDR_NO);
  77. GPIO_SetAFPin_0_7(Servo_1_Port, (Servo_1_Pin|Servo_2_Pin), GPIO_AF_2);
  78. /* Photo_Pin: analog in, pull none */
  79. GPIO_SetPinPull(Photo_Port, Photo_Pin, GPIO_PUPDR_NO);
  80. GPIO_SetPinMode(Photo_Port, Photo_Pin, GPIO_MODE_ANL);
  81. /* Test out Pin A4 */
  82. GPIO_SetPinMode(GPIOA, GPIO_PIN_4, GPIO_MODE_OUT);
  83. GPIO_SetPinOutputType(GPIOA, GPIO_PIN_4, GPIO_OTYPE_PP);
  84. GPIO_SetPinSpeed(GPIOA, GPIO_PIN_4, GPIO_OSPEED_LW);
  85. GPIO_SetPinPull(GPIOA, GPIO_PIN_4, GPIO_PUPDR_NO);
  86. /* Test in Pin A5 */
  87. GPIO_SetPinPull(GPIOA, GPIO_PIN_4, GPIO_PUPDR_UP);
  88. }
  89. /**
  90. * @brief ADC1 Initialization Function
  91. * @param None
  92. * @retval None
  93. */
  94. static void ADC_Init(void)
  95. {
  96. /* ADC1 interrupt Init */
  97. NVIC_SetPriority(ADC1_IRQn, 0);
  98. NVIC_EnableIRQ(ADC1_IRQn);
  99. /* Configure the global features of the ADC
  100. * (Clock, Resolution, Data Alignment and number of conversion)
  101. */
  102. //ADC synchronous clock derived from AHB clock divided by 2
  103. ADC1->CFGR2 |= ADC_CFGR2_CKMODE_0;
  104. /* Poll for ADC channel configuration ready */
  105. while ((ADC1->ISR & ADC_ISR_CCRDY) == 0) {};
  106. /* Clear flag ADC channel configuration ready */
  107. ADC1->ISR |= ADC_ISR_CCRDY;
  108. ADC1->CFGR1 |= (ADC_CFGR1_EXTSEL_1 | ADC_CFGR1_EXTSEL_0 | ADC_CFGR1_EXTEN_0) | ADC_CFGR1_OVRMOD | ADC_CFGR1_EXTEN_0;
  109. /* Enable ADC internal voltage regulator */
  110. ADC1->CR |= ADC_CR_ADVREGEN; // ???
  111. /** Configure Regular Channel */
  112. ADC1->CHSELR = ADC_CHSELR_CHSEL11;
  113. /* Poll for ADC channel configuration ready */
  114. while ((ADC1->ISR & ADC_ISR_CCRDY) == 0) {};
  115. /* Clear flag ADC channel configuration ready */
  116. ADC1->ISR |= ADC_ISR_CCRDY;
  117. // SetChannelSamplingTime
  118. ADC1->SMPR |= ADC_SMPR_SMPSEL11;
  119. /* Calibration in single conversion mode */
  120. ADC1->CR |= ADC_CR_ADCAL; // ADC calibration
  121. while ((ADC1->CR & ADC_CR_ADCAL) != 0) {};
  122. /* Enable Interrupt */
  123. ADC1->IER |= ADC_IER_EOCIE; // ADC interrupt enable
  124. /* Enable ADC */
  125. ADC1->CR |= ADC_CR_ADEN; // ADC enable
  126. /* Start ADC software conversion */
  127. ADC1->CR |= ADC_CR_ADSTART; // ADC start
  128. }
  129. /**
  130. * @brief TIM1 Initialization Function
  131. * @param None
  132. * @retval None
  133. */
  134. static void TIM1_Init(void)
  135. {
  136. /* target clock */
  137. TIM1->PSC = TIM1_PSC; // prescaler
  138. TIM1->ARR = TIM1_ARR; // auto reload value
  139. TIM1->CR1 = TIM_CR1_ARPE;
  140. // initial pwm value
  141. TIM1->CCR1 = SERVO_INIT_VAL;
  142. TIM1->CCR4 = SERVO_INIT_VAL;
  143. // pwm mode 1 for chanels
  144. TIM1->CCMR1 = (TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1PE);
  145. TIM1->CCMR2 = (TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4PE);
  146. //TIM1->SR |= TIM_SR_UIF;
  147. TIM1->BDTR = TIM_BDTR_MOE; // enable main output
  148. TIM1->EGR = TIM_EGR_UG; // force timer update
  149. /* TIM1 CC_EnableChannel */
  150. TIM1->CCER = (TIM_CCER_CC1E | TIM_CCER_CC1P | TIM_CCER_CC4E | TIM_CCER_CC4P);
  151. /* TIM_EnableCounter */
  152. TIM1->CR1 |= TIM_CR1_CEN;
  153. }
  154. /**
  155. * @brief TIM3 Initialization Function
  156. * @param None
  157. * @retval None
  158. */
  159. static void TIM3_Init(void)
  160. {
  161. /* target clock */
  162. TIM3->PSC = TIM3_PSC; // prescaler
  163. TIM3->ARR = TIM3_ARR; // auto reload value
  164. TIM3->CR1 = TIM_CR1_ARPE;
  165. // launch timer
  166. TIM3->EGR = TIM_EGR_UG; // force timer update
  167. /* Set the trigger output 2 (TRGO2) used for ADC synchronization */
  168. TIM3->CR2 |= TIM_CR2_MMS2_2; // update event
  169. /* TIM3 enable */
  170. TIM3->CR1 |= TIM_CR1_CEN;
  171. }
  172. /**
  173. * @brief IWDG Initialization Function
  174. * @param None
  175. * @retval None
  176. */
  177. static void IWDG_Init(void)
  178. {
  179. IWDG->KR = 0xCCCC;
  180. IWDG->KR = 0x5555;
  181. IWDG->PR = 0x0;
  182. IWDG->RLR = 4095;
  183. while (IWDG->SR != 0x00000000) {};
  184. IWDG->KR = 0xAAAA;
  185. }
  186. /**
  187. * @brief Inserts a delay time.
  188. * @param msec: specifies the delay time length, in milliseconds.
  189. * @retval None
  190. */
  191. void delay_ms(uint32_t msek) {
  192. TDelay = msek;
  193. do {
  194. __WFI();
  195. } while (TDelay != 0);
  196. }
  197. #pragma GCC optimize ("O3")
  198. /**
  199. * @brief This function handles SysTick Handler.
  200. * @param None
  201. * @retval None
  202. */
  203. void SysTick_Handler(void) {
  204. if (TDelay != 0) {
  205. TDelay --;
  206. }
  207. }