PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/NRF24L01 C/Stac1/main.c

https://bitbucket.org/jansmol/mbd-priedas-programu-kodai
C | 361 lines | 200 code | 65 blank | 96 comment | 19 complexity | 78a2c186e99818338afcc4d9d887ffae MD5 | raw file
  1. ///////////////////////////////////////////////
  2. //main.c failas skirtas 1 siustuvo irenginiui//
  3. ///////////////////////////////////////////////
  4. /* Includes ------------------------------------------------------------------*/
  5. #include "main.h"
  6. #include "stm32f1xx_hal.h"
  7. /* USER CODE BEGIN Includes */
  8. #include "NRF24.h"
  9. #include <string.h>
  10. /* USER CODE END Includes */
  11. /* Private variables ---------------------------------------------------------*/
  12. I2C_HandleTypeDef hi2c2;
  13. SPI_HandleTypeDef hspi1;
  14. TIM_HandleTypeDef htim1;
  15. UART_HandleTypeDef huart1;
  16. /* USER CODE BEGIN PV */
  17. /* Private variables ---------------------------------------------------------*/
  18. uint16_t i=1,retr_cnt_full=0, cnt_lost=0;
  19. uint8_t dk;
  20. uint8_t buf1[20]={0};
  21. /* USER CODE END PV */
  22. /* Private function prototypes -----------------------------------------------*/
  23. void SystemClock_Config(void);
  24. static void MX_GPIO_Init(void);
  25. static void MX_SPI1_Init(void);
  26. static void MX_USART1_UART_Init(void);
  27. static void MX_I2C2_Init(void);
  28. static void MX_TIM1_Init(void);
  29. /* USER CODE BEGIN PFP */
  30. /* Private function prototypes -----------------------------------------------*/
  31. /* USER CODE END PFP */
  32. /* USER CODE BEGIN 0 */
  33. /* USER CODE END 0 */
  34. /**
  35. * @brief The application entry point.
  36. *
  37. * @retval None
  38. */
  39. int main(void)
  40. {
  41. /* USER CODE BEGIN 1 */
  42. /* USER CODE END 1 */
  43. /* MCU Configuration----------------------------------------------------------*/
  44. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  45. HAL_Init();
  46. /* USER CODE BEGIN Init */
  47. /* USER CODE END Init */
  48. /* Configure the system clock */
  49. SystemClock_Config();
  50. /* USER CODE BEGIN SysInit */
  51. /* USER CODE END SysInit */
  52. /* Initialize all configured peripherals */
  53. MX_GPIO_Init();
  54. MX_SPI1_Init();
  55. MX_USART1_UART_Init();
  56. MX_I2C2_Init();
  57. MX_TIM1_Init();
  58. /* USER CODE BEGIN 2 */
  59. NRF24_ini();
  60. HAL_TIM_Base_Start(&htim1);
  61. /* USER CODE END 2 */
  62. /* Infinite loop */
  63. /* USER CODE BEGIN WHILE */
  64. while (1)
  65. {
  66. /* USER CODE END WHILE */
  67. /* USER CODE BEGIN 3 */
  68. HAL_Delay(1000);
  69. memcpy(buf1,(uint8_t*)&i,2);
  70. memcpy(buf1+2,(uint8_t*)&retr_cnt_full,2);
  71. memcpy(buf1+4,(uint8_t*)&cnt_lost,2);
  72. dk = NRF24L01_Send(buf1);
  73. retr_cnt_full += dk & 0xF;
  74. cnt_lost = dk>>4;
  75. i++;
  76. HAL_Delay(100);
  77. NRF24L01_Receive();
  78. HAL_Delay(1);
  79. }
  80. /* USER CODE END 3 */
  81. }
  82. /**
  83. * @brief System Clock Configuration
  84. * @retval None
  85. */
  86. void SystemClock_Config(void)
  87. {
  88. RCC_OscInitTypeDef RCC_OscInitStruct;
  89. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  90. /**Initializes the CPU, AHB and APB busses clocks
  91. */
  92. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  93. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  94. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  95. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  96. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  97. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  98. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  99. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  100. {
  101. _Error_Handler(__FILE__, __LINE__);
  102. }
  103. /**Initializes the CPU, AHB and APB busses clocks
  104. */
  105. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  106. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  107. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  108. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  109. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  110. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  111. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  112. {
  113. _Error_Handler(__FILE__, __LINE__);
  114. }
  115. /**Configure the Systick interrupt time
  116. */
  117. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  118. /**Configure the Systick
  119. */
  120. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  121. /* SysTick_IRQn interrupt configuration */
  122. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  123. }
  124. /* I2C2 init function */
  125. static void MX_I2C2_Init(void)
  126. {
  127. hi2c2.Instance = I2C2;
  128. hi2c2.Init.ClockSpeed = 100000;
  129. hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
  130. hi2c2.Init.OwnAddress1 = 0;
  131. hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  132. hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  133. hi2c2.Init.OwnAddress2 = 0;
  134. hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  135. hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  136. if (HAL_I2C_Init(&hi2c2) != HAL_OK)
  137. {
  138. _Error_Handler(__FILE__, __LINE__);
  139. }
  140. }
  141. /* SPI1 init function */
  142. static void MX_SPI1_Init(void)
  143. {
  144. /* SPI1 parameter configuration*/
  145. hspi1.Instance = SPI1;
  146. hspi1.Init.Mode = SPI_MODE_MASTER;
  147. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  148. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  149. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  150. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  151. hspi1.Init.NSS = SPI_NSS_SOFT;
  152. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  153. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  154. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  155. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  156. hspi1.Init.CRCPolynomial = 10;
  157. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  158. {
  159. _Error_Handler(__FILE__, __LINE__);
  160. }
  161. }
  162. /* TIM1 init function */
  163. static void MX_TIM1_Init(void)
  164. {
  165. TIM_ClockConfigTypeDef sClockSourceConfig;
  166. TIM_MasterConfigTypeDef sMasterConfig;
  167. htim1.Instance = TIM1;
  168. htim1.Init.Prescaler = 2;
  169. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  170. htim1.Init.Period = 800;
  171. htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  172. htim1.Init.RepetitionCounter = 0;
  173. htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  174. if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  175. {
  176. _Error_Handler(__FILE__, __LINE__);
  177. }
  178. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  179. if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  180. {
  181. _Error_Handler(__FILE__, __LINE__);
  182. }
  183. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  184. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  185. if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  186. {
  187. _Error_Handler(__FILE__, __LINE__);
  188. }
  189. }
  190. /* USART1 init function */
  191. static void MX_USART1_UART_Init(void)
  192. {
  193. huart1.Instance = USART1;
  194. huart1.Init.BaudRate = 115200;
  195. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  196. huart1.Init.StopBits = UART_STOPBITS_1;
  197. huart1.Init.Parity = UART_PARITY_NONE;
  198. huart1.Init.Mode = UART_MODE_TX_RX;
  199. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  200. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  201. if (HAL_UART_Init(&huart1) != HAL_OK)
  202. {
  203. _Error_Handler(__FILE__, __LINE__);
  204. }
  205. }
  206. /** Configure pins as
  207. * Analog
  208. * Input
  209. * Output
  210. * EVENT_OUT
  211. * EXTI
  212. */
  213. static void MX_GPIO_Init(void)
  214. {
  215. GPIO_InitTypeDef GPIO_InitStruct;
  216. /* GPIO Ports Clock Enable */
  217. __HAL_RCC_GPIOC_CLK_ENABLE();
  218. __HAL_RCC_GPIOD_CLK_ENABLE();
  219. __HAL_RCC_GPIOA_CLK_ENABLE();
  220. __HAL_RCC_GPIOB_CLK_ENABLE();
  221. /*Configure GPIO pin Output Level */
  222. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  223. /*Configure GPIO pin Output Level */
  224. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3|GPIO_PIN_4, GPIO_PIN_RESET);
  225. /*Configure GPIO pin Output Level */
  226. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1, GPIO_PIN_RESET);
  227. /*Configure GPIO pin : PC13 */
  228. GPIO_InitStruct.Pin = GPIO_PIN_13;
  229. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  230. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  231. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  232. /*Configure GPIO pin : PA2 */
  233. GPIO_InitStruct.Pin = GPIO_PIN_2;
  234. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  235. GPIO_InitStruct.Pull = GPIO_NOPULL;
  236. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  237. /*Configure GPIO pins : PA3 PA4 */
  238. GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
  239. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  240. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  241. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  242. /*Configure GPIO pins : PB0 PB1 */
  243. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
  244. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  245. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  246. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  247. /* EXTI interrupt init*/
  248. HAL_NVIC_SetPriority(EXTI2_IRQn, 0, 0);
  249. HAL_NVIC_EnableIRQ(EXTI2_IRQn);
  250. }
  251. /* USER CODE BEGIN 4 */
  252. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  253. {
  254. if(GPIO_Pin == GPIO_PIN_2)
  255. {
  256. IRQ_Callback();
  257. }
  258. else
  259. {
  260. __NOP();
  261. }
  262. }
  263. //-------------------------------------------------------------
  264. /* USER CODE END 4 */
  265. /**
  266. * @brief This function is executed in case of error occurrence.
  267. * @param file: The file name as string.
  268. * @param line: The line in file as a number.
  269. * @retval None
  270. */
  271. void _Error_Handler(char *file, int line)
  272. {
  273. /* USER CODE BEGIN Error_Handler_Debug */
  274. /* User can add his own implementation to report the HAL error return state */
  275. while(1)
  276. {
  277. }
  278. /* USER CODE END Error_Handler_Debug */
  279. }
  280. #ifdef USE_FULL_ASSERT
  281. /**
  282. * @brief Reports the name of the source file and the source line number
  283. * where the assert_param error has occurred.
  284. * @param file: pointer to the source file name
  285. * @param line: assert_param error line source number
  286. * @retval None
  287. */
  288. void assert_failed(uint8_t* file, uint32_t line)
  289. {
  290. /* USER CODE BEGIN 6 */
  291. /* User can add his own implementation to report the file name and line number,
  292. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  293. /* USER CODE END 6 */
  294. }
  295. #endif /* USE_FULL_ASSERT */
  296. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/