main.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. * Yet another change
  35. */
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32l1xx_hal.h"
  38. /* USER CODE BEGIN Includes */
  39. #include <stdio.h>
  40. #include "main.h"
  41. #include "app.h"
  42. /* USER CODE END Includes */
  43. /* Private variables ---------------------------------------------------------*/
  44. TIM_HandleTypeDef htim2;
  45. TIM_HandleTypeDef htim3;
  46. UART_HandleTypeDef huart1;
  47. /* USER CODE BEGIN PV */
  48. /* USER CODE END PV */
  49. /* Private function prototypes -----------------------------------------------*/
  50. void SystemClock_Config(void);
  51. static void MX_GPIO_Init(void);
  52. static void MX_TIM2_Init(void);
  53. static void MX_TIM3_Init(void);
  54. static void MX_USART1_UART_Init(void);
  55. /* USER CODE BEGIN PFP */
  56. unsigned long tim3mSTick = 0;
  57. /* USER CODE END PFP */
  58. /* USER CODE BEGIN 0 */
  59. // Function returns number of uS since startup
  60. unsigned long micros() {
  61. //#define __HAL_TIM_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((__HANDLE__)->Instance->DIER &= ~(__INTERRUPT__))
  62. //#define TIM_IT_UPDATE (TIM_DIER_UIE)
  63. //#define TIM_DIER_UIE ((uint32_t)0x00000001) /*!<Update interrupt enable */
  64. //
  65. // #define __HAL_TIM_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((__HANDLE__)->Instance->DIER |= (__INTERRUPT__))
  66. __HAL_TIM_DISABLE_IT(&htim3, TIM_IT_UPDATE);
  67. unsigned long mS = tim3mSTick;
  68. unsigned long uS = TIM3->CNT;
  69. __HAL_TIM_ENABLE_IT(&htim3, TIM_IT_UPDATE);
  70. return (mS * 1000UL) + uS;
  71. }
  72. void delayMicroseconds(unsigned int delay)
  73. {
  74. volatile unsigned long endtime = micros() + ((unsigned long)delay);
  75. while( micros() < endtime);
  76. }
  77. /* USER CODE END 0 */
  78. int main(void)
  79. {
  80. /* USER CODE BEGIN 1 */
  81. /* USER CODE END 1 */
  82. /* MCU Configuration----------------------------------------------------------*/
  83. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  84. HAL_Init();
  85. /* Configure the system clock */
  86. SystemClock_Config();
  87. /* Initialize all configured peripherals */
  88. MX_GPIO_Init();
  89. MX_TIM2_Init();
  90. MX_TIM3_Init();
  91. MX_USART1_UART_Init();
  92. /* USER CODE BEGIN 2 */
  93. //HAL_TIM_Base_Start(&htim2);
  94. HAL_TIM_Base_Start_IT(&htim2);
  95. HAL_TIM_Base_Start_IT(&htim3);
  96. // Enable USART1 Interrupts
  97. USART1->CR1 |= USART_CR1_RXNEIE | USART_CR1_TXEIE;
  98. setup();
  99. /* USER CODE END 2 */
  100. /* USER CODE BEGIN 3 */
  101. /* Infinite loop */
  102. //volatile uint32_t cnt=0;
  103. //TIM2->CR1 |= TIMX_CR1_CEN;
  104. while (1)
  105. {
  106. loop();
  107. }
  108. /* USER CODE END 3 */
  109. }
  110. /** System Clock Configuration
  111. */
  112. void SystemClock_Config(void)
  113. {
  114. RCC_OscInitTypeDef RCC_OscInitStruct;
  115. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  116. __PWR_CLK_ENABLE();
  117. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  118. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  119. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  120. RCC_OscInitStruct.HSICalibrationValue = 16;
  121. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  122. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  123. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  124. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
  125. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  126. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  127. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  128. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  129. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  130. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  131. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
  132. __SYSCFG_CLK_ENABLE();
  133. }
  134. /* TIM2 init function */
  135. void MX_TIM2_Init(void)
  136. {
  137. TIM_ClockConfigTypeDef sClockSourceConfig;
  138. TIM_MasterConfigTypeDef sMasterConfig;
  139. htim2.Instance = TIM2;
  140. htim2.Init.Prescaler = 31;
  141. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  142. htim2.Init.Period = 10;
  143. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  144. HAL_TIM_Base_Init(&htim2);
  145. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  146. HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
  147. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  148. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  149. HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
  150. }
  151. /* TIM3 init function */
  152. void MX_TIM3_Init(void)
  153. {
  154. TIM_ClockConfigTypeDef sClockSourceConfig;
  155. TIM_MasterConfigTypeDef sMasterConfig;
  156. htim3.Instance = TIM3;
  157. htim3.Init.Prescaler = 31;
  158. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  159. htim3.Init.Period = 1000;
  160. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  161. HAL_TIM_Base_Init(&htim3);
  162. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  163. HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
  164. sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  165. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  166. HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
  167. }
  168. /* USART1 init function */
  169. void MX_USART1_UART_Init(void)
  170. {
  171. huart1.Instance = USART1;
  172. huart1.Init.BaudRate = 38400;
  173. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  174. huart1.Init.StopBits = UART_STOPBITS_1;
  175. huart1.Init.Parity = UART_PARITY_NONE;
  176. huart1.Init.Mode = UART_MODE_TX_RX;
  177. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  178. huart1.Init.OverSampling = UART_OVERSAMPLING_8;
  179. HAL_UART_Init(&huart1);
  180. }
  181. /** Configure pins as
  182. * Analog
  183. * Input
  184. * Output
  185. * EVENT_OUT
  186. * EXTI
  187. */
  188. void MX_GPIO_Init(void)
  189. {
  190. GPIO_InitTypeDef GPIO_InitStruct;
  191. /* GPIO Ports Clock Enable */
  192. __GPIOA_CLK_ENABLE();
  193. __GPIOB_CLK_ENABLE();
  194. /*Configure GPIO pin : PA0 */
  195. GPIO_InitStruct.Pin = GPIO_PIN_0;
  196. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  197. GPIO_InitStruct.Pull = GPIO_NOPULL;
  198. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  199. /*Configure GPIO pins : PA1 PA2 */
  200. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2;
  201. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  202. GPIO_InitStruct.Pull = GPIO_PULLUP;
  203. GPIO_InitStruct.Speed = GPIO_SPEED_VERY_LOW;
  204. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  205. /*Configure GPIO pin : PA3 */
  206. GPIO_InitStruct.Pin = GPIO_PIN_3;
  207. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  208. GPIO_InitStruct.Pull = GPIO_NOPULL;
  209. GPIO_InitStruct.Speed = GPIO_SPEED_VERY_LOW;
  210. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  211. /*Configure GPIO pin : PB3 */
  212. GPIO_InitStruct.Pin = GPIO_PIN_3;
  213. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  214. GPIO_InitStruct.Pull = GPIO_NOPULL;
  215. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  216. /*Configure GPIO pins : PB6 PB7 */
  217. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  218. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  219. GPIO_InitStruct.Pull = GPIO_NOPULL;
  220. GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
  221. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  222. }
  223. /* USER CODE BEGIN 4 */
  224. /* USER CODE END 4 */
  225. #ifdef USE_FULL_ASSERT
  226. /**
  227. * @brief Reports the name of the source file and the source line number
  228. * where the assert_param error has occurred.
  229. * @param file: pointer to the source file name
  230. * @param line: assert_param error line source number
  231. * @retval None
  232. */
  233. void assert_failed(uint8_t* file, uint32_t line)
  234. {
  235. /* USER CODE BEGIN 6 */
  236. /* User can add his own implementation to report the file name and line number,
  237. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  238. /* USER CODE END 6 */
  239. }
  240. #endif
  241. /**
  242. * @}
  243. */
  244. /**
  245. * @}
  246. */
  247. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/