Advertisement
Guest User

Untitled

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