Advertisement
Guest User

Untitled

a guest
May 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.93 KB | None | 0 0
  1. #include "main.h"
  2.  
  3. /** @addtogroup STM32F3xx_HAL_Examples
  4. * @{
  5. */
  6.  
  7. /** @addtogroup UART_TwoBoards_ComIT
  8. * @{
  9. */
  10.  
  11. #define BUFFER_SIZE 1
  12. #define CRC_POLYNOMIAL_8B 0x9B /* X^8 + X^7 + X^4 + X^3 + X + 1 */
  13.  
  14.  
  15.  
  16.  
  17.  
  18. /* Private typedef -----------------------------------------------------------*/
  19. /* Private define ------------------------------------------------------------*/
  20. #define TRANSMITTER_BOARD
  21.  
  22. /* Private macro -------------------------------------------------------------*/
  23. /* Private variables ---------------------------------------------------------*/
  24. /* UART handler declaration */
  25. UART_HandleTypeDef UartHandle;
  26. __IO ITStatus UartReady = RESET;
  27. __IO uint32_t UserButtonStatus = 0; /* set to 1 after User Button interrupt */
  28.  
  29. /* Buffer used for transmission */
  30. uint8_t aTxBuffer[6];
  31.  
  32. /* Buffer used for reception */
  33. uint8_t aRxBuffer[RXBUFFERSIZE];
  34.  
  35. CRC_HandleTypeDef CrcHandle;
  36. __IO uint32_t uwCRCValue = 0;
  37.  
  38.  
  39. static const uint32_t aDataBuffer = 0x1234;
  40. uint32_t uwExpectedCRCValue = 0xEF;
  41.  
  42.  
  43.  
  44.  
  45. /* Private function prototypes -----------------------------------------------*/
  46. void SystemClock_Config(void);
  47. static void Error_Handler(void);
  48. static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
  49.  
  50.  
  51.  
  52. void CRC_Config();
  53.  
  54.  
  55.  
  56. /* Private functions ---------------------------------------------------------*/
  57.  
  58. /**
  59. * @brief Main program
  60. * @param None
  61. * @retval None
  62. */
  63. int main(void)
  64. {
  65.  
  66. HAL_Init();
  67.  
  68. /* Configure the system clock to 72 MHz */
  69. SystemClock_Config();
  70.  
  71. /* Configure LED3, LED4, LED5 and LED6 */
  72. BSP_LED_Init(LED3);
  73. BSP_LED_Init(LED4);
  74. BSP_LED_Init(LED5);
  75. BSP_LED_Init(LED6);
  76. BSP_LED_Init(LED7);
  77.  
  78.  
  79.  
  80.  
  81. CRC_Config();
  82.  
  83. /*##-2- Compute the CRC of "aDataBuffer" ###################################*/
  84. uwCRCValue = HAL_CRC_Calculate(&CrcHandle, (uint32_t *)&aDataBuffer, BUFFER_SIZE);
  85.  
  86. uint8_t chunk =0;
  87.  
  88. for(int j=0;j<4;j++){
  89. chunk = (aDataBuffer >> j*8) & 255;
  90. aTxBuffer[j]=chunk;
  91. }
  92.  
  93. aTxBuffer[4]=uwCRCValue;
  94. aTxBuffer[5]=uwCRCValue;
  95.  
  96.  
  97. /*##-1- Configure the UART peripheral ######################################*/
  98. UartHandle.Instance = USARTx;
  99.  
  100. UartHandle.Init.BaudRate = 9600;
  101. UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  102. UartHandle.Init.StopBits = UART_STOPBITS_1;
  103. UartHandle.Init.Parity = UART_PARITY_NONE;
  104. UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  105. UartHandle.Init.Mode = UART_MODE_TX_RX;
  106. UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  107. if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
  108. {
  109. Error_Handler();
  110. }
  111. if(HAL_UART_Init(&UartHandle) != HAL_OK)
  112. {
  113. Error_Handler();
  114. }
  115.  
  116. #ifdef TRANSMITTER_BOARD
  117.  
  118. /* Configure User push-button in Interrupt mode */
  119. BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
  120.  
  121. /* Wait for User push-button press before starting the Communication.
  122. In the meantime, LED4 is blinking */
  123. while(UserButtonStatus == 0)
  124. {
  125. /* Toggle LED4*/
  126. BSP_LED_Toggle(LED4);
  127. HAL_Delay(100);
  128. }
  129.  
  130. BSP_LED_Off(LED4);
  131.  
  132. /* The board sends the message and expects to receive it back */
  133.  
  134. /*##-2- Start the transmission process #####################################*/
  135. /* While the UART in reception process, user can transmit data through
  136. "aTxBuffer" buffer */
  137. if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)
  138. {
  139. Error_Handler();
  140. }
  141.  
  142. /*##-3- Wait for the end of the transfer ###################################*/
  143. while (UartReady != SET)
  144. {
  145. }
  146.  
  147. /* Reset transmission flag */
  148. UartReady = RESET;
  149.  
  150. /*##-4- Put UART peripheral in reception process ###########################*/
  151. // if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  152. // {
  153. // Error_Handler();
  154. // }
  155.  
  156. #else
  157.  
  158. /* The board receives the message and sends it back */
  159.  
  160. /*##-2- Put UART peripheral in reception process ###########################*/
  161. if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  162. {
  163. Error_Handler();
  164. }
  165.  
  166. /*##-3- Wait for the end of the transfer ###################################*/
  167. /* While waiting for message to come from the other board, LED4 is
  168. blinking according to the following pattern: a double flash every half-second */
  169. while (UartReady != SET)
  170. {
  171. BSP_LED_On(LED4);
  172. HAL_Delay(100);
  173. BSP_LED_Off(LED4);
  174. HAL_Delay(100);
  175. BSP_LED_On(LED4);
  176. HAL_Delay(100);
  177. BSP_LED_Off(LED4);
  178. HAL_Delay(500);
  179. }
  180.  
  181. /* Reset transmission flag */
  182. UartReady = RESET;
  183. BSP_LED_Off(LED4);
  184.  
  185. uint32_t receivedData =0;
  186.  
  187. for (int i=0;i<4;i++){
  188. receivedData = recivedData | ((uint32_t)aRxBuffer << j * 8);
  189. }
  190.  
  191.  
  192.  
  193. uint8_t crc = HAL_CRC_Calculate(&CrcHandle, (uint32_t *)&receivedData, BUFFER_SIZE);
  194.  
  195. if(crc == receivedData[5]){
  196. BSP_LED_On(LED7);
  197. }
  198.  
  199. /*##-4- Start the transmission process #####################################*/
  200. /* While the UART in reception process, user can transmit data through
  201. "aTxBuffer" buffer */
  202. // if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)
  203. // {
  204. // Error_Handler();
  205. // }
  206.  
  207. #endif /* TRANSMITTER_BOARD */
  208.  
  209. /*##-5- Wait for the end of the transfer ###################################*/
  210. while (UartReady != SET)
  211. {
  212. }
  213.  
  214. /* Reset transmission flag */
  215. UartReady = RESET;
  216.  
  217. /*##-6- Compare the sent and received buffers ##############################*/
  218. if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE))
  219. {
  220. Error_Handler();
  221. }
  222.  
  223. /* Infinite loop */
  224. while (1)
  225. {
  226. }
  227. }
  228.  
  229. /**
  230. * @brief System Clock Configuration
  231. * The system Clock is configured as follow :
  232. * System Clock source = PLL (HSE)
  233. * SYSCLK(Hz) = 72000000
  234. * HCLK(Hz) = 72000000
  235. * AHB Prescaler = 1
  236. * APB1 Prescaler = 2
  237. * APB2 Prescaler = 1
  238. * HSE Frequency(Hz) = 8000000
  239. * HSE PREDIV = 1
  240. * PLLMUL = RCC_PLL_MUL9 (9)
  241. * Flash Latency(WS) = 2
  242. * @param None
  243. * @retval None
  244. */
  245. void SystemClock_Config(void)
  246. {
  247. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  248. RCC_OscInitTypeDef RCC_OscInitStruct;
  249.  
  250. /* Enable HSE Oscillator and activate PLL with HSE as source */
  251. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  252. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  253. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  254. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  255. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  256. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  257. if (HAL_RCC_OscConfig(&RCC_OscInitStruct)!= HAL_OK)
  258. {
  259. /* Initialization Error */
  260. while(1);
  261. }
  262.  
  263. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  264. clocks dividers */
  265. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  266. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  267. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  268. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  269. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  270. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2)!= HAL_OK)
  271. {
  272. /* Initialization Error */
  273. while(1);
  274. }
  275. }
  276. /**
  277. * @brief Tx Transfer completed callback
  278. * @param UartHandle: UART handle.
  279. * @note This example shows a simple way to report end of IT Tx transfer, and
  280. * you can add your own implementation.
  281. * @retval None
  282. */
  283. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
  284. {
  285. /* Set transmission flag: transfer complete */
  286. UartReady = SET;
  287.  
  288. /* Turn LED3 on: Transfer in transmission process is correct */
  289. BSP_LED_On(LED3);
  290.  
  291. }
  292.  
  293. /**
  294. * @brief Rx Transfer completed callback
  295. * @param UartHandle: UART handle
  296. * @note This example shows a simple way to report end of DMA Rx transfer, and
  297. * you can add your own implementation.
  298. * @retval None
  299. */
  300. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
  301. {
  302. /* Set transmission flag: transfer complete */
  303. UartReady = SET;
  304.  
  305. /* Turn LED5 on: Transfer in reception process is correct */
  306. BSP_LED_On(LED5);
  307.  
  308. }
  309.  
  310. /**
  311. * @brief UART error callbacks
  312. * @param UartHandle: UART handle
  313. * @note This example shows a simple way to report transfer error, and you can
  314. * add your own implementation.
  315. * @retval None
  316. */
  317. void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
  318. {
  319. /* Turn LED6 on: Transfer error in reception/transmission process */
  320. BSP_LED_On(LED6);
  321. }
  322.  
  323.  
  324. /**
  325. * @brief EXTI line detection callbacks
  326. * @param GPIO_Pin: Specifies the pins connected EXTI line
  327. * @retval None
  328. */
  329. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  330. {
  331. if(GPIO_Pin == USER_BUTTON_PIN)
  332. {
  333. UserButtonStatus = 1;
  334. }
  335. }
  336. /**
  337. * @brief Compares two buffers.
  338. * @param pBuffer1, pBuffer2: buffers to be compared.
  339. * @param BufferLength: buffer's length
  340. * @retval 0 : pBuffer1 identical to pBuffer2
  341. * >0 : pBuffer1 differs from pBuffer2
  342. */
  343. static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
  344. {
  345. while (BufferLength--)
  346. {
  347. if ((*pBuffer1) != *pBuffer2)
  348. {
  349. return BufferLength;
  350. }
  351. pBuffer1++;
  352. pBuffer2++;
  353. }
  354.  
  355. return 0;
  356. }
  357.  
  358. /**
  359. * @brief This function is executed in case of error occurrence.
  360. * @param None
  361. * @retval None
  362. */
  363. static void Error_Handler(void)
  364. {
  365. /* Turn LED6 on */
  366. BSP_LED_On(LED6);
  367. while(1)
  368. {
  369. }
  370. }
  371.  
  372.  
  373. void CRC_Config(){
  374.  
  375. /*##-1- Configure the CRC peripheral #######################################*/
  376. CrcHandle.Instance = CRC;
  377.  
  378. /* The default polynomial is not used. It is required to defined it in CrcHandle.Init.GeneratingPolynomial*/
  379. CrcHandle.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_DISABLE;
  380.  
  381. /* Set the value of the polynomial */
  382. CrcHandle.Init.GeneratingPolynomial = CRC_POLYNOMIAL_8B;
  383.  
  384. /* The size of the polynomial to configure the IP is 8b*/
  385. CrcHandle.Init.CRCLength = CRC_POLYLENGTH_8B;
  386.  
  387. /* The default init value is used */
  388. CrcHandle.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
  389.  
  390. /* The input data are not inverted */
  391. CrcHandle.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
  392.  
  393. /* The output data are not inverted */
  394. CrcHandle.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
  395.  
  396. /* The input data are 32 bits lenght */
  397. CrcHandle.InputDataFormat = CRC_INPUTDATA_FORMAT_WORDS;
  398.  
  399. if (HAL_CRC_Init(&CrcHandle) != HAL_OK)
  400. {
  401. /* Initialization Error */
  402. Error_Handler();
  403. }
  404. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement