STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/CRC/CRC_8BitsCRCMessage/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file CRC/CRC_8BitsCRCMessage/main.c 00004 * @author MCD Application Team 00005 * @version V1.4.0 00006 * @date 24-July-2014 00007 * @brief Main program body 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 00029 /* Includes ------------------------------------------------------------------*/ 00030 #include "main.h" 00031 00032 /** @addtogroup STM32F0xx_StdPeriph_Examples 00033 * @{ 00034 */ 00035 00036 /** @addtogroup CRC_8BitsCRCMessage 00037 * @{ 00038 */ 00039 00040 /* Private typedef -----------------------------------------------------------*/ 00041 /* Private define ------------------------------------------------------------*/ 00042 #define countof(a) (uint8_t)(sizeof(a) / sizeof(*(a))) 00043 #define BUFFER_SIZE (countof(CRCBuffer) - 1) 00044 00045 /* Private macro -------------------------------------------------------------*/ 00046 /* Private variables ---------------------------------------------------------*/ 00047 uint8_t CRCBuffer[] = "STM32F0xx CortexM0 Device running on STM32072B-EVAL"; 00048 uint8_t ComputedCRC = 0; 00049 uint8_t ExpectedCRC = 0xAF; /* The expected CRC value of CRCBuffer using the 00050 polynomial x8 + x7 + x6 + x4 + x2 + 1. 00051 This value is already computed using on-line CRC tool */ 00052 00053 /* Private function prototypes -----------------------------------------------*/ 00054 static void CRC_Config(uint8_t poly); 00055 static uint8_t CRC_8BitsCompute(uint8_t* data, uint32_t size); 00056 00057 /* Private functions ---------------------------------------------------------*/ 00058 00059 /** 00060 * @brief Main program. 00061 * @param None 00062 * @retval None 00063 */ 00064 int main(void) 00065 { 00066 /*!< At this stage the microcontroller clock setting is already configured, 00067 this is done through SystemInit() function which is called from startup 00068 file (startup_stm32f0xx.s) before to branch to application main. 00069 To reconfigure the default setting of SystemInit() function, refer to 00070 system_stm32f0xx.c file 00071 */ 00072 00073 /* Initialize LEDs available on STM32072B-EVAL board */ 00074 STM_EVAL_LEDInit(LED1); 00075 STM_EVAL_LEDInit(LED3); 00076 00077 /* Configure the CRC peripheral to use the polynomial x8 + x7 + x6 + x4 + x2 + 1 */ 00078 CRC_Config(0xD5); 00079 00080 /* Compute the CRC value of the 8-bit buffer: CRCBuffer */ 00081 ComputedCRC = CRC_8BitsCompute(CRCBuffer, BUFFER_SIZE); 00082 00083 /* Check if the computed CRC matches the expected one */ 00084 if (ComputedCRC != ExpectedCRC) 00085 { 00086 /* Turn on LD3 */ 00087 STM_EVAL_LEDOn(LED3); 00088 } 00089 else 00090 { 00091 /* Turn on LD1 */ 00092 STM_EVAL_LEDOn(LED1); 00093 } 00094 00095 /* Infinite loop */ 00096 while(1) 00097 { 00098 } 00099 } 00100 00101 /** 00102 * @brief Configure CRC peripheral to use 8-bit polynomials 00103 * @param poly: the CRC polynomial 00104 * @retval None 00105 */ 00106 static void CRC_Config(uint8_t poly) 00107 { 00108 /* Enable CRC AHB clock interface */ 00109 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE); 00110 00111 /* DeInit CRC peripheral */ 00112 CRC_DeInit(); 00113 00114 /* Init the INIT register */ 00115 CRC_SetInitRegister(0); 00116 00117 /* Select 8-bit polynomial size */ 00118 CRC_PolynomialSizeSelect(CRC_PolSize_8); 00119 00120 /* Set the polynomial coefficients */ 00121 CRC_SetPolynomial(poly); 00122 } 00123 00124 /** 00125 * @brief Compute CRC value for input message 00126 * @param data: pointer at uint8_t 00127 * @param size: the size of the input message 00128 * @retval The computed CRC value 00129 */ 00130 static uint8_t CRC_8BitsCompute(uint8_t* data, uint32_t size) 00131 { 00132 uint8_t* dataEnd = data+size; 00133 00134 /* Reset CRC data register to avoid overlap when computing new data stream */ 00135 CRC_ResetDR(); 00136 00137 while(data < dataEnd) 00138 { 00139 /* Write the input data in the CRC data register */ 00140 CRC_CalcCRC8bits(*data++); 00141 } 00142 /* Return the CRC value */ 00143 return (uint8_t)CRC_GetCRC(); 00144 } 00145 00146 #ifdef USE_FULL_ASSERT 00147 00148 /** 00149 * @brief Reports the name of the source file and the source line number 00150 * where the assert_param error has occurred. 00151 * @param file: pointer to the source file name 00152 * @param line: assert_param error line source number 00153 * @retval None 00154 */ 00155 void assert_failed(uint8_t* file, uint32_t line) 00156 { 00157 /* User can add his own implementation to report the file name and line number, 00158 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00159 00160 /* Infinite loop */ 00161 while (1) 00162 { 00163 } 00164 } 00165 #endif 00166 00167 /** 00168 * @} 00169 */ 00170 00171 /** 00172 * @} 00173 */ 00174 00175 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/