Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "main.h"
- #define CRC_BUFFER_SIZE 4
- #define CRC_DIM 8;
- #define CRC_POLYNOMIAL_8B 0x9B /* X^8 + X^7 + X^4 + X^3 + X + 1 */
- #define UART_BUFFER_SIZE 2+CRC_BUFFER_SIZE*4
- #define CRC_POLYNOMIAL_1 0x100D4E63
- #define CRC_POLYNOMIAL_2 0x8CE56011
- #define CRC_DEFAULTVALUE_1 0x00
- #define CRC_DEFAULTVALUE_2 0x00
- #define BOARD_MODE 0 // 0 - TX --- 1 -> RX
- //#define TRANSMITTER_BOARD
- /* UART handler declaration */
- UART_HandleTypeDef UartHandle;
- __IO ITStatus UartReady = RESET;
- __IO uint32_t UserButtonStatus = 0; /* set to 1 after User Button interrupt */
- #define UART_BAUDRATE 9600
- /* Buffer used for transmission */
- uint8_t Payload[CRC_BUFFER_SIZE];
- uint32_t Frame[UART_BUFFER_SIZE];
- /* Buffer used for reception */
- uint8_t aRxBuffer[UART_BUFFER_SIZE];
- CRC_HandleTypeDef CrcHandle;
- __IO uint32_t uwCRCValue = 0;
- static const uint32_t crcDataBuffer[CRC_BUFFER_SIZE] = {0x00001234,0x00001234,0x00001234,0x00001234 };
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void Error_Handler(void);
- void CRC_Config(uint32_t CRC_Polynomial,uint32_t CRC_DefaultValue);
- void SendRec_CRC(uint32_t * MSG,uint8_t channel, uint8_t mode);
- void UART_Config(uint32_t Baudrate);
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program
- * @param None
- * @retval None
- */
- int main(void)
- {
- HAL_Init();
- /* Configure the system clock to 72 MHz */
- SystemClock_Config();
- /* Configure LED3, LED4, LED5 and LED6 */
- BSP_LED_Init(LED3);
- BSP_LED_Init(LED4);
- BSP_LED_Init(LED5);
- BSP_LED_Init(LED6);
- BSP_LED_Init(LED7);
- CRC_Config(CRC_POLYNOMIAL_1,CRC_DEFAULTVALUE_1);
- uint32_t CRC32_1 = HAL_CRC_Calculate(&CrcHandle, (uint32_t *)&Payload, CRC_BUFFER_SIZE);
- CRC_Config(CRC_POLYNOMIAL_2,CRC_DEFAULTVALUE_2);
- uint32_t CRC32_2 = HAL_CRC_Calculate(&CrcHandle, (uint32_t *)&Payload, CRC_BUFFER_SIZE);
- Frame[UART_BUFFER_SIZE-2]=CRC32_1;
- Frame[UART_BUFFER_SIZE-1]=CRC32_2;
- SendRec_CRC(Frame,0,BOARD_MODE);
- while (1)
- {
- }
- }
- void SystemClock_Config(void)
- {
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_OscInitTypeDef RCC_OscInitStruct;
- /* Enable HSE Oscillator and activate PLL with HSE as source */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct)!= HAL_OK)
- {
- /* Initialization Error */
- while(1);
- }
- /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
- clocks dividers */
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2)!= HAL_OK)
- {
- /* Initialization Error */
- while(1);
- }
- }
- /**
- * @brief Tx Transfer completed callback
- * @param UartHandle: UART handle.
- * @note This example shows a simple way to report end of IT Tx transfer, and
- * you can add your own implementation.
- * @retval None
- */
- void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
- {
- /* Set transmission flag: transfer complete */
- UartReady = SET;
- /* Turn LED3 on: Transfer in transmission process is correct */
- BSP_LED_On(LED3);
- }
- /**
- * @brief Rx Transfer completed callback
- * @param UartHandle: UART handle
- * @note This example shows a simple way to report end of DMA Rx transfer, and
- * you can add your own implementation.
- * @retval None
- */
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
- {
- /* Set transmission flag: transfer complete */
- UartReady = SET;
- /* Turn LED5 on: Transfer in reception process is correct */
- BSP_LED_On(LED5);
- }
- /**
- * @brief UART error callbacks
- * @param UartHandle: UART handle
- * @note This example shows a simple way to report transfer error, and you can
- * add your own implementation.
- * @retval None
- */
- void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
- {
- /* Turn LED6 on: Transfer error in reception/transmission process */
- BSP_LED_On(LED6);
- }
- /**
- * @brief EXTI line detection callbacks
- * @param GPIO_Pin: Specifies the pins connected EXTI line
- * @retval None
- */
- void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
- {
- if(GPIO_Pin == USER_BUTTON_PIN)
- {
- UserButtonStatus = 1;
- }
- }
- /**
- * @brief Compares two buffers.
- * @param pBuffer1, pBuffer2: buffers to be compared.
- * @param BufferLength: buffer's length
- * @retval 0 : pBuffer1 identical to pBuffer2
- * >0 : pBuffer1 differs from pBuffer2
- */
- static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
- {
- while (BufferLength--)
- {
- if ((*pBuffer1) != *pBuffer2)
- {
- return BufferLength;
- }
- pBuffer1++;
- pBuffer2++;
- }
- return 0;
- }
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- static void Error_Handler(void)
- {
- /* Turn LED6 on */
- BSP_LED_On(LED6);
- }
- uint8_t * Frame32to8(uint32_t * frame32){
- uint8_t buffer[];
- uint8_t chunk =0;
- for(int i=0;i<CRC_BUFFER_SIZE;i++){
- for (int j=0;j<4;j++){
- chunk = frame32[i] >> j*8 & 255;
- buffer[4*i+j]=chunk;
- }
- }
- return buffer;
- }
- uint32_t * Frame8to32(uint8_t * frame8){
- uint32_t receivedData[CRC_BUFFER_SIZE];
- uint32_t chunk32=0;
- uint32_t temp=0;
- for (int i=0;i<CRC_BUFFER_SIZE;i++){
- for (int j=0;j<4;j++){
- temp = frame8[(4*i)+j];
- temp = temp << 8*j;
- chunk32 = chunk32+temp;
- temp=0;
- }
- receivedData[i] = chunk32;
- chunk32 = 0;
- }
- return receivedData;
- }
- void CRC_Config(uint32_t CRC_Polynomial,uint32_t CRC_DefaultValue){
- if (HAL_CRC_DeInit(&CrcHandle) != HAL_OK)
- {
- /* Initialization Error */
- Error_Handler();
- }
- /*##-1- Configure the CRC peripheral #######################################*/
- CrcHandle.Instance = CRC;
- /* The default polynomial is not used. It is required to defined it in CrcHandle.Init.GeneratingPolynomial*/
- CrcHandle.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_DISABLE;
- /* Set the value of the polynomial */
- CrcHandle.Init.GeneratingPolynomial = CRC_Polynomial;
- /* The size of the polynomial to configure the IP is 8b*/
- CrcHandle.Init.CRCLength = CRC_POLYLENGTH_32B;
- /* The default init value is used */
- CrcHandle.Init.DefaultInitValueUse = CRC_DefaultValue;
- /* The input data are not inverted */
- CrcHandle.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
- /* The output data are not inverted */
- CrcHandle.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
- /* The input data are 32 bits lenght */
- CrcHandle.InputDataFormat = CRC_INPUTDATA_FORMAT_WORDS;
- if (HAL_CRC_Init(&CrcHandle) != HAL_OK)
- {
- /* Initialization Error */
- Error_Handler();
- }
- }
- void UART_Config(uint32_t Baudrate){
- /*##-1- Configure the UART peripheral ######################################*/
- UartHandle.Instance = USARTx;
- UartHandle.Init.BaudRate = Baudrate;
- UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
- UartHandle.Init.StopBits = UART_STOPBITS_1;
- UartHandle.Init.Parity = UART_PARITY_NONE;
- UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- UartHandle.Init.Mode = UART_MODE_TX_RX;
- UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
- if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
- {
- Error_Handler();
- }
- if(HAL_UART_Init(&UartHandle) != HAL_OK)
- {
- Error_Handler();
- }
- }
- void SendRec_CRC(uint32_t * MSG,uint8_t channel, uint8_t mode){
- switch (channel){
- case 0:
- UART_Config(UART_BAUDRATE);
- if(mode){
- uint8_t * aRxBuffer[UART_BUFFER_SIZE];
- /*##-2- Put UART peripheral in reception process ###########################*/
- if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, UART_BUFFER_SIZE) != HAL_OK)
- {
- Error_Handler();
- }
- /*##-3- Wait for the end of the transfer ###################################*/
- /* While waiting for message to come from the other board, LED4 is
- blinking according to the following pattern: a double flash every half-second */
- while (UartReady != SET)
- {
- BSP_LED_On(LED4);
- HAL_Delay(100);
- BSP_LED_Off(LED4);
- HAL_Delay(100);
- BSP_LED_On(LED4);
- HAL_Delay(100);
- BSP_LED_Off(LED4);
- HAL_Delay(500);
- }
- /* Reset transmission flag */
- UartReady = RESET;
- BSP_LED_Off(LED4);
- uint32_t * ReceivedFrame = Frame8to32(aRxBuffer);
- CRC_Config(CRC_POLYNOMIAL_1,CRC_DEFAULTVALUE_1);
- uint32_t CRC32_1 = HAL_CRC_Calculate(&CrcHandle, (uint32_t *)&crcDataBuffer, CRC_BUFFER_SIZE);
- CRC_Config(CRC_POLYNOMIAL_2,CRC_DEFAULTVALUE_2);
- uint32_t CRC32_2 = HAL_CRC_Calculate(&CrcHandle, (uint32_t *)&crcDataBuffer, CRC_BUFFER_SIZE);
- if (CRC32_1 != ReceivedFrame[UART_BUFFER_SIZE-2]){
- Error_Handler();
- }else BSP_LED_On(LED7);
- if (CRC32_2 != ReceivedFrame[UART_BUFFER_SIZE-1]){
- Error_Handler();
- }else BSP_LED_On(LED9);
- }
- uint8_t * aTxBuffer=Frame32to8(MSG);
- /* Configure User push-button in Interrupt mode */
- BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
- /* Wait for User push-button press before starting the Communication.
- In the meantime, LED4 is blinking */
- while(UserButtonStatus == 0)
- {
- /* Toggle LED4*/
- BSP_LED_Toggle(LED4);
- HAL_Delay(100);
- }
- BSP_LED_Off(LED4);
- /*##-2- Start the transmission process #####################################*/
- /* While the UART in reception process, user can transmit data through
- "aTxBuffer" buffer */
- if(HAL_UART_Transmit_IT(&UartHandle, (uint8_t*)aTxBuffer, UART_BUFFER_SIZE)!= HAL_OK)
- {
- Error_Handler();
- }
- /*##-3- Wait for the end of the transfer ###################################*/
- while (UartReady != SET)
- {
- }
- /* Reset transmission flag */
- UartReady = RESET;
- /* The board receives the message and sends it back */
- if(!mode){
- uint8_t * aRxBuffer[UART_BUFFER_SIZE];
- /*##-2- Put UART peripheral in reception process ###########################*/
- if(HAL_UART_Receive_IT(&UartHandle, (uint8_t *)aRxBuffer, UART_BUFFER_SIZE) != HAL_OK)
- {
- Error_Handler();
- }
- /*##-3- Wait for the end of the transfer ###################################*/
- /* While waiting for message to come from the other board, LED4 is
- blinking according to the following pattern: a double flash every half-second */
- while (UartReady != SET)
- {
- BSP_LED_On(LED4);
- HAL_Delay(100);
- BSP_LED_Off(LED4);
- HAL_Delay(100);
- BSP_LED_On(LED4);
- HAL_Delay(100);
- BSP_LED_Off(LED4);
- HAL_Delay(500);
- }
- /* Reset transmission flag */
- UartReady = RESET;
- BSP_LED_Off(LED4);
- uint32_t * ReceivedFrame = Frame8to32(aRxBuffer);
- CRC_Config(CRC_POLYNOMIAL_1,CRC_DEFAULTVALUE_1);
- uint32_t CRC32_1 = HAL_CRC_Calculate(&CrcHandle, (uint32_t *)&crcDataBuffer, CRC_BUFFER_SIZE);
- CRC_Config(CRC_POLYNOMIAL_2,CRC_DEFAULTVALUE_2);
- uint32_t CRC32_2 = HAL_CRC_Calculate(&CrcHandle, (uint32_t *)&crcDataBuffer, CRC_BUFFER_SIZE);
- if (CRC32_1 != ReceivedFrame[UART_BUFFER_SIZE-2]){
- Error_Handler();
- }else BSP_LED_On(LED7);
- if (CRC32_2 != ReceivedFrame[UART_BUFFER_SIZE-1]){
- Error_Handler();
- }else BSP_LED_On(LED9);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement