main.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Date : 03/01/2015 16:46:04
  5. * Description : Main program body
  6. ******************************************************************************
  7. *
  8. * COPYRIGHT(c) 2015 STMicroelectronics
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. ******************************************************************************
  33. * A small change.
  34. */
  35. /* Includes ------------------------------------------------------------------*/
  36. #include "stm32l1xx_hal.h"
  37. /* USER CODE BEGIN Includes */
  38. #include <stdio.h>
  39. #include "main.h"
  40. #include "app.h"
  41. /* USER CODE END Includes */
  42. /* Private variables ---------------------------------------------------------*/
  43. TIM_HandleTypeDef htim2;
  44. TIM_HandleTypeDef htim3;
  45. UART_HandleTypeDef huart1;
  46. /* USER CODE BEGIN PV */
  47. /* USER CODE END PV */
  48. /* Private function prototypes -----------------------------------------------*/
  49. void SystemClock_Config(void);
  50. static void MX_GPIO_Init(void);
  51. static void MX_TIM2_Init(void);
  52. static void MX_TIM3_Init(void);
  53. static void MX_USART1_UART_Init(void);
  54. /* USER CODE BEGIN PFP */
  55. unsigned long tim3mSTick = 0;
  56. /* USER CODE END PFP */
  57. /* USER CODE BEGIN 0 */
  58. // Function returns number of uS since startup
  59. unsigned long micros() {
  60. //#define __HAL_TIM_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((__HANDLE__)->Instance->DIER &= ~(__INTERRUPT__))
  61. //#define TIM_IT_UPDATE (TIM_DIER_UIE)
  62. //#define TIM_DIER_UIE ((uint32_t)0x00000001) /*!<Update interrupt enable */
  63. //
  64. // #define __HAL_TIM_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((__HANDLE__)->Instance->DIER |= (__INTERRUPT__))
  65. __HAL_TIM_DISABLE_IT(&htim3, TIM_IT_UPDATE);
  66. unsigned long mS = tim3mSTick;
  67. unsigned long uS = TIM3->CNT;
  68. __HAL_TIM_ENABLE_IT(&htim3, TIM_IT_UPDATE);
  69. return (mS * 1000UL) + uS;
  70. }
  71. void delayMicroseconds(unsigned int delay)
  72. {
  73. volatile unsigned long endtime = micros() + ((unsigned long)delay);
  74. while( micros() < endtime);
  75. }
  76. /* USER CODE END 0 */
  77. int main(void)
  78. {
  79. /* USER CODE BEGIN 1 */
  80. /* USER CODE END 1 */
  81. /* MCU Configuration----------------------------------------------------------*/
  82. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  83. HAL_Init();
  84. /* Configure the system clock */
  85. SystemClock_Config();
  86. /* Initialize all configured peripherals */
  87. MX_GPIO_Init();
  88. MX_TIM2_Init();
  89. MX_TIM3_Init();
  90. MX_USART1_UART_Init();
  91. /* USER CODE BEGIN 2 */
  92. //HAL_TIM_Base_Start(&htim2);
  93. HAL_TIM_Base_Start_IT(&htim2);
  94. HAL_TIM_Base_Start_IT(&htim3);
  95. // Enable USART1 Interrupts
  96. USART1->CR1 |= USART_CR1_RXNEIE | USART_CR1_TXEIE;
  97. setup();
  98. /* USER CODE END 2 */
  99. /* USER CODE BEGIN 3 */
  100. /* Infinite loop */
  101. //volatile uint32_t cnt=0;
  102. //TIM2->CR1 |= TIMX_CR1_CEN;
  103. while (1)
  104. {
  105. loop();
  106. }
  107. /* USER CODE END 3 */
  108. }
  109. /** System Clock Configuration
  110. */
  111. void SystemClock_Config(void)
  112. {
  113. RCC_OscInitTypeDef RCC_OscInitStruct;
  114. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  115. __PWR_CLK_ENABLE();
  116. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  117. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  118. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  119. RCC_OscInitStruct.HSICalibrationValue = 16;
  120. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  121. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  122. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  123. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
  124. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  125. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  126. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  127. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  128. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  129. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  130. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
  131. __SYSCFG_CLK_ENABLE();
  132. }
  133. /* TIM2 init function */
  134. void MX_TIM2_Init(void)
  135. {
  136. TIM_ClockConfigTypeDef sClockSourceConfig;
  137. TIM_MasterConfigTypeDef sMasterConfig;
  138. htim2.Instance = TIM2;
  139. htim2.Init.Prescaler = 31;
  140. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  141. htim2.Init.Period = 10;
  142. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  143. HAL_TIM_Base_Init(&htim2);
  144. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  145. HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
  146. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  147. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  148. HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
  149. }
  150. /* TIM3 init function */
  151. void MX_TIM3_Init(void)
  152. {
  153. TIM_ClockConfigTypeDef sClockSourceConfig;
  154. TIM_MasterConfigTypeDef sMasterConfig;
  155. htim3.Instance = TIM3;
  156. htim3.Init.Prescaler = 31;
  157. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  158. htim3.Init.Period = 1000;
  159. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  160. HAL_TIM_Base_Init(&htim3);
  161. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  162. HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
  163. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  164. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  165. HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
  166. }
  167. /* USART1 init function */
  168. void MX_USART1_UART_Init(void)
  169. {
  170. huart1.Instance = USART1;
  171. huart1.Init.BaudRate = 38400;
  172. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  173. huart1.Init.StopBits = UART_STOPBITS_1;
  174. huart1.Init.Parity = UART_PARITY_NONE;
  175. huart1.Init.Mode = UART_MODE_TX_RX;
  176. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  177. huart1.Init.OverSampling = UART_OVERSAMPLING_8;
  178. HAL_UART_Init(&huart1);
  179. }
  180. /** Configure pins as
  181. * Analog
  182. * Input
  183. * Output
  184. * EVENT_OUT
  185. * EXTI
  186. */
  187. void MX_GPIO_Init(void)
  188. {
  189. GPIO_InitTypeDef GPIO_InitStruct;
  190. /* GPIO Ports Clock Enable */
  191. __GPIOA_CLK_ENABLE();
  192. __GPIOB_CLK_ENABLE();
  193. /*Configure GPIO pin : PA0 */
  194. GPIO_InitStruct.Pin = GPIO_PIN_0;
  195. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  196. GPIO_InitStruct.Pull = GPIO_NOPULL;
  197. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  198. /*Configure GPIO pins : PA1 PA2 */
  199. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2;
  200. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  201. GPIO_InitStruct.Pull = GPIO_PULLUP;
  202. GPIO_InitStruct.Speed = GPIO_SPEED_VERY_LOW;
  203. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  204. /*Configure GPIO pin : PA3 */
  205. GPIO_InitStruct.Pin = GPIO_PIN_3;
  206. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  207. GPIO_InitStruct.Pull = GPIO_NOPULL;
  208. GPIO_InitStruct.Speed = GPIO_SPEED_VERY_LOW;
  209. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  210. /*Configure GPIO pin : PB3 */
  211. GPIO_InitStruct.Pin = GPIO_PIN_3;
  212. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  213. GPIO_InitStruct.Pull = GPIO_NOPULL;
  214. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  215. /*Configure GPIO pins : PB6 PB7 */
  216. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  217. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  218. GPIO_InitStruct.Pull = GPIO_NOPULL;
  219. GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
  220. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  221. }
  222. /* USER CODE BEGIN 4 */
  223. /* USER CODE END 4 */
  224. #ifdef USE_FULL_ASSERT
  225. /**
  226. * @brief Reports the name of the source file and the source line number
  227. * where the assert_param error has occurred.
  228. * @param file: pointer to the source file name
  229. * @param line: assert_param error line source number
  230. * @retval None
  231. */
  232. void assert_failed(uint8_t* file, uint32_t line)
  233. {
  234. /* USER CODE BEGIN 6 */
  235. /* User can add his own implementation to report the file name and line number,
  236. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  237. /* USER CODE END 6 */
  238. }
  239. #endif
  240. /**
  241. * @}
  242. */
  243. /**
  244. * @}
  245. */
  246. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/