1 /******************************************************************************
2 * @brief    Cyclic redundancy check (CRC) source code.
3 *
4 ******************************************************************************/
5 #include "common.h"
6 #include "crc.h"
7 
8 /******************************************************************************
9 * Global variables
10 ******************************************************************************/
11 
12 /******************************************************************************
13 * Constants and macros
14 ******************************************************************************/
15 
16 /******************************************************************************
17 * Local types
18 ******************************************************************************/
19 
20 /******************************************************************************
21 * Local function prototypes
22 ******************************************************************************/
23 
24 /******************************************************************************
25 * Local variables
26 ******************************************************************************/
27 
28 /******************************************************************************
29 * Local functions
30 ******************************************************************************/
31 
32 /******************************************************************************
33 * Global functions
34 ******************************************************************************/
35 
36 /******************************************************************************
37 * define CRC APIs
38 *
39 *//*! @addtogroup crc_api_list
40 * @{
41 *******************************************************************************/
42 
43 /*****************************************************************************//*!
44 *
45 * @brief initialize CRC with poly per control parameters
46 *
47 * @param[in]  pConfig point to configuration.
48 *
49 * @return none
50 *
51 * @ Pass/ Fail criteria: none
52 *****************************************************************************/
CRC_Init(CRC_ConfigType * pConfig)53 void CRC_Init(CRC_ConfigType *pConfig)
54 {
55     uint32_t     u32Sc ;
56 
57     u32Sc      = 0;
58 
59     SIM->SCGC |= SIM_SCGC_CRC_MASK;
60 
61     u32Sc     |= ((pConfig->bWidth & 0x01)<<24);
62     u32Sc     |= CRC_CTRL_TOTR(pConfig->bTransposeReadType & 0x03);
63     u32Sc     |= CRC_CTRL_TOT(pConfig->bTransposeWriteType & 0x03);
64 
65     if (pConfig->bFinalXOR)
66     {
67         u32Sc |= CRC_CTRL_FXOR_MASK;
68     }
69 
70     CRC0->CTRL  = u32Sc;
71 
72     if ( pConfig->bWidth )
73     {
74         CRC0->GPOLY = pConfig->u32PolyData;
75     }
76     else
77     {
78         CRC0->GPOLY_ACCESS16BIT.GPOLYL = pConfig->u32PolyData;  /*!< only 16-bit write allowed */
79     }
80 
81 }
82 
83 
84 /*****************************************************************************//*!
85 *
86 * @brief crc module 16-bit mode calculation.
87 *
88 * @param[in]  seed
89 * @param[in]  msg  poiont to message buffer
90 * @param[in]  sizeBytes  size of message
91 *
92 * @return data_out convertion result
93 *
94 * @ Pass/ Fail criteria: none
95 *****************************************************************************/
CRC_Cal16(uint32_t seed,uint8_t * msg,uint32_t sizeBytes)96 uint32_t CRC_Cal16(uint32_t seed, uint8_t *msg, uint32_t sizeBytes)
97 {
98   uint32_t ctrl_reg,data_out,data_in;
99   uint8_t  *pCRCBytes;
100   uint32_t sizeWords;
101   uint32_t i,j;
102 
103   /* Input seed, Set WaS=1 */
104   ctrl_reg  = CRC0->CTRL;
105   CRC0->CTRL  = ctrl_reg | CRC_CTRL_WAS_MASK;
106   CRC0->ACCESS16BIT.DATAL = seed;
107 
108   /*Input data, Set WaS=0*/
109   CRC0->CTRL  = ctrl_reg & 0xFD000000;
110 
111   /*Wait for calculation completion*/
112   sizeWords = sizeBytes>>1;
113   j = 0;
114   for(i=0;i<sizeWords;i++){
115       data_in = (msg[j] << 8) | (msg[j+1]);
116       j += 2;
117       CRC0->ACCESS16BIT.DATAL =data_in;
118   }
119   if (j<sizeBytes)
120   {
121      pCRCBytes = (uint8_t*)&CRC0->ACCESS8BIT.DATALL;
122      *pCRCBytes++ = msg[j];
123   }
124     data_out=CRC0->ACCESS16BIT.DATAL;
125 
126   return(data_out);
127 }
128 
129 
130 /*****************************************************************************//*!
131 *
132 * @brief crc module 32-bit mode calculation.
133 *
134 * @param[in]  seed
135 * @param[in]  msg  poiont to message buffer
136 * @param[in]  sizeBytes  size of message
137 *
138 * @return data_out convertion result
139 *
140 * @ Pass/ Fail criteria: none
141 *****************************************************************************/
CRC_Cal32(uint32_t seed,uint8_t * msg,uint32_t sizeBytes)142 uint32_t CRC_Cal32(uint32_t seed, uint8_t *msg, uint32_t sizeBytes)
143 {
144   uint32_t ctrl_reg,data_out,data_in;
145   uint32_t sizeDwords;
146   uint8_t  *pCRCBytes;
147   uint32_t i,j;
148 
149   /*Input seed, Set WaS=1*/
150   ctrl_reg = CRC0->CTRL;
151   CRC0->CTRL = ctrl_reg | 0x02000000;
152   CRC0->DATA = seed;
153 
154   /*Input data, Set WaS=0*/
155   CRC0->CTRL = ctrl_reg & 0xFD000000;
156 
157   /*Wait for calculation completion*/
158   sizeDwords = sizeBytes>>2;
159   j = 0;
160   for(i=0;i<sizeDwords;i++)
161   {
162       data_in = ((msg[j] << 24) | (msg[j+1] << 16) | (msg[j+2] << 8) | msg[j+3]);
163       j += 4;
164       CRC0->DATA = data_in;
165   }
166   if (j<sizeBytes)
167   {
168     pCRCBytes = (uint8_t*)&CRC0->ACCESS8BIT.DATALL;
169 
170 #if  defined(BYTE_ENABLES_1_2_4_8)
171 
172     /*write single byte*/
173     for(;j<sizeBytes;j++)
174     {
175        *pCRCBytes++ = msg[j];
176     }
177 #elif  defined(BYTE_ENABLES_3_6_C)
178 
179     /*write two bytes*/
180     data_in = 0;
181     i = 0;
182     for(;j<sizeBytes;j++)
183     {
184       data_in = (data_in <<8) | msg[j];
185       i++;
186       if (i==2)
187       {
188         i = 0;
189         CRC0->ACCESS16BIT.DATAL = data_in;
190       }
191     }
192     if (i==1)
193     {
194        CRC0->ACCESS8BIT.DATALL = data_in;                /*!< write last byte */
195     }
196 #elif  defined(BYTE_ENABLES_7_E)
197     /*!< write three bytes */
198     data_in = 0;
199     i = 0;
200     for(;j<sizeBytes;j++)
201     {
202       data_in = (data_in <<8) | msg[j];
203       i++;
204       if (i==3)
205       {
206         i = 0;
207         /*write first  char*/
208         CRC0->ACCESS8BIT.DATAHL  = (data_in>>16) & 0xff; /*!< write low byte of high word */
209         /*write last two chars*/
210         CRC0->ACCESS16BIT.DATAL = data_in & 0x00ffff;    /*!< write low word */
211        }
212     }
213     if ( i == 2)
214     {
215        CRC0->ACCESS16BIT.DATAL = (data_in);              /*!< write last 2 bytes */
216     }
217     else if (i == 1)
218     {
219        CRC0->ACCESS8BIT.DATALL = data_in;                /*!< write last byte */
220     }
221 #else                                                    /*!< write low byte only */
222     for(;j<sizeBytes;j++)
223     {
224        *pCRCBytes = msg[j];
225     }
226 #endif
227   }
228   data_out=CRC0->DATA;
229 
230   return(data_out);
231 }
232 
233 
234 /*****************************************************************************//*!
235 *
236 * @brief de-initialize crc module, reset crc register.
237 *
238 * @param none
239 *
240 * @return none
241 *
242 * @ Pass/ Fail criteria: none
243 *****************************************************************************/
CRC_DeInit(void)244 void CRC_DeInit(void)
245 {
246    CRC0->CTRL  = 0x3000000; /*!< prepare for write 32-bit seed*/
247    CRC0->DATA  = 0xFFFFFFFF;/*!< write 32-bit seed to data register*/
248    while(!(CRC0->DATA == 0xFFFFFFFF));
249    CRC0->GPOLY = 0x00001021;
250    CRC0->CTRL  = 0;         /*!< reset ctrl register*/
251    SIM->SCGC &= ~SIM_SCGC_CRC_MASK;
252 }
253 
254 /*! @} End of crc_api_list                                                   */
255 
256 
257