Digital Power Starter Kit 3 Firmware
dsPIC33C Boost Converter Voltage Mode Control Example
p33c_crc.c
1 /* Microchip Technology Inc. and its subsidiaries. You may use this software
2  * and any derivatives exclusively with Microchip products.
3  *
4  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
5  * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
6  * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
7  * PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
8  * WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
9  *
10  * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
11  * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
12  * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
13  * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
14  * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS
15  * IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF
16  * ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
17  *
18  * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
19  * TERMS.
20  */
21 
22 // Include standard header files
23 #include <xc.h> // include processor files - each processor file is guarded.
24 #include <stdint.h> // include standard integer data types
25 #include <stdbool.h> // include standard boolean data types
26 #include <stddef.h> // include standard definition data types
27 
28 #include "p33c_crc.h"
29 
30 /*********************************************************************************
31  * @fn volatile uint16_t p33c_CrcModule_Dispose(void)
32  * @ingroup lib-layer-pral-functions-public-crc
33  * @brief Resets all Crc Module registers to their RESET default values
34  * @param void
35  * @return 0 = failure, disposing CRC module was not successful
36  * @return 1 = success, disposing CRC module was successful
37  *
38  * @details
39  * This function clears all CRC module registers to their
40  * default values set when the device comes out of RESET.
41  *
42  * Default configuration:
43  * - stop operation in IDLE mode
44  * - polynomial and seed are clear
45  * - polynomial length and data width are clear
46  *
47  *********************************************************************************/
48 
49 volatile uint16_t p33c_CrcModule_Dispose(void)
50 {
51  volatile uint16_t retval=1;
52 
53  retval = p33c_CrcModule_ConfigWrite(crcModuleConfigClear);
54 
55  return(retval);
56 }
57 
58 /*********************************************************************************
59  * @fn volatile struct P33C_CRC_MODULE_s p33c_CrcModule_ConfigRead(void)
60  * @ingroup lib-layer-pral-functions-public-crc
61  * @brief Read the current configuration from the CRC module base registers
62  * @param void
63  * @return 0 = failure, reading CRC module was not successful (returns NULL)
64  * @return n = success, reading CRC module was successful (returns result of type P33C_CRC_MODULE_s)
65  *
66  * @details
67  * This function reads all registers with their current configuration into
68  * a data structure of type P33C_CRC_MODULE_s. Users can read and
69  * verify or modify the configuration to write it back to the CRC module
70  * registers.
71  *
72  *********************************************************************************/
73 
74 volatile struct P33C_CRC_MODULE_s p33c_CrcModule_ConfigRead(void)
75 {
76  volatile struct P33C_CRC_MODULE_s* crc;
77 
78  // Set pointer to memory address of DMA base register set
79  crc = p33c_CrcModule_GetHandle();
80 
81  return(*crc);
82 
83 }
84 
85 /*********************************************************************************
86  * @fn volatile uint16_t p33c_CrcModule_ConfigWrite(volatile struct P33C_CRC_MODULE_s crcModuleConfig)
87  * @ingroup lib-layer-pral-functions-public-crc
88  * @brief Writes a user-defined configuration to the CRC module base registers
89  * @param crcModuleConfig Cyclic Redundancy Check module configuration of type struct P33C_CRC_MODULE_s
90  * @return 0 = failure, writing CRC module was not successful
91  * @return 1 = success, writing CRC module was successful
92  *
93  * @details
94  * This function writes a user-defined CRC module configuration of
95  * type P33C_CRC_MODULE_s to the CRC module base registers. The
96  * individual register configurations have to be set in user-code
97  * before calling this function. To simplify the configuration process
98  * of standard functions, this driver provides templates, which can be
99  * loaded and written directly.
100  *
101  *********************************************************************************/
102 
103 volatile uint16_t p33c_CrcModule_ConfigWrite(
104  volatile struct P33C_CRC_MODULE_s crcModuleConfig)
105 {
106  volatile uint16_t retval=1;
107  volatile struct P33C_CRC_MODULE_s* crc;
108 
109  // Set pointer to memory address of the CRC module base registers
110  crc = p33c_CrcModule_GetHandle();
111  *crc = crcModuleConfig;
112 
113  return(retval);
114 
115 }
116 
117 
118 volatile uint16_t p33c_CrcModule_Initialize(void){
119 
120  volatile uint16_t retval=1;
121  volatile struct P33C_CRC_MODULE_s* crc;
122 
123  // Set pointer to memory address of the CRC module base registers
124  crc = p33c_CrcModule_GetHandle();
125 
126  // Clear all CRC registers
127  retval &= p33c_CrcModule_ConfigWrite(crcModuleConfigClear);
128 
129  return(retval);
130 }
131 
132 /*********************************************************************************
133  * @ingroup lib-layer-pral-properties-public-crc
134  * @fn volatile uint16_t p33c_CrcModule_Polynomial_Config(volatile uint8_t lendian, volatile uint8_t data_width,
135  volatile uint8_t poly_lrngth, volatile enum CRC_POLYNOMIAL_e poly, volatile uint16_t seed)
136  * @brief Initializes a CRC module with user-defined settings
137  * @param 0 = lendian Data Word Little Endian Configuration
138  * @param 1 = data_width Configures the width of the data word
139  * @param 2 = poly_length Configures the length of the polynomial
140  * @param 3 = poly XOR of Polynomial Term x^n
141  * @param 4 = seed CRC Shift value
142  * @return 0 = failure, CRC configuration was not successful
143  * @return 1 = success, CRC configuration was successful
144  * @details
145  * User-define configuration of the CRC module when used to calculate the
146  * 8-bit or 16-bit wide Cyclic Redundancy Check in the specified protocol
147  * applications.
148  ********************************************************************************/
149 volatile uint16_t p33c_CrcModule_Polynomial_Config(volatile uint8_t lendian, volatile uint8_t data_width,
150  volatile uint8_t poly_length, volatile enum CRC_POLYNOMIAL_e poly, volatile uint16_t seed){
151 
152  volatile uint16_t retval=1;
153  volatile struct P33C_CRC_MODULE_s* crc;
154 
155  // Set pointer to memory address of the CRC module base registers
156  crc = p33c_CrcModule_GetHandle();
157 
158  // Clear all CRC registers
159  retval &= p33c_CrcModule_ConfigWrite(crcModuleConfigClear);
160 
161  // Initialize configuration registers for standard CRC calculation
162  crc->CrcConl.bits.CRCEN = 1; // enable CRC
163  crc->CrcConl.bits.MOD = 1; // 0=Legacy Mode, 1=Alternate Mode
164  crc->CrcConl.bits.CRCISEL = 0; // interrupt when all shifts are done
165  crc->CrcConl.bits.LENDIAN = lendian; // 1=little endian, 0=big endian
166 
167  crc->CrcConh.bits.DWIDTH = (data_width-1); // 16-bit data width (even if 8-bit data is used)
168  crc->CrcConh.bits.PLEN = (poly_length-1); // 16-bit polynomial order
169 
170  // Set polynomial and seed
171  crc->CrcConl.bits.CRCGO = 0; // disable CRC calculation
172  crc->CrcXorl.value = poly; // set polynomial
173  crc->CrcXorh.value = 0;
174  crc->CrcWdatl.value= seed; // set initial value
175  crc->CrcDath.value = 0;
176 
177  return(retval);
178 }
179 
180 /*********************************************************************************
181  * @ingroup lib-layer-pral-functions-public-crc
182  * @fn uint16_t p33c_CrcModule_Get_Data8_CRC16(volatile uint8_t *data, volatile uint8_t start, volatile uint8_t size)
183  * @brief Calculates a 16-bit CRC out of 8-bit data stream
184  * @param data Pointer to 8-bit data stream (array)
185  * @param start Value of the array index at which the CRC calculation should start
186  * @param size Number of bytes across which the CRC should be calculated
187  * @return 16-bit unsigned integer result of the CRC calculation
188  * @details
189  * Calculates a 16-bit CRC out of 8-bit data stream by using a pre-configured
190  * CRC logic configuration.
191  *
192  * @note
193  * Users must set up the CRC registers before calling this function.
194  *
195  **********************************************************************************/
196 
197 volatile uint16_t p33c_CrcModule_Get_Data8_CRC16(
198  volatile uint8_t *data, volatile uint8_t start, volatile uint8_t size)
199 {
200  volatile uint16_t i=0;
201  volatile uint16_t fifo_buf=0;
202  volatile uint16_t crc_buf=0;
203  volatile uint16_t data_size=0;
204  volatile uint16_t is_odd=false;
205  volatile uint16_t rev_poly=0;
206  volatile struct P33C_CRC_MODULE_s* crc;
207 
208  // Set pointer to memory address of the CRC module base registers
209  crc = p33c_CrcModule_GetHandle();
210 
211  crc->CrcConl.bits.CRCGO = 1; // start CRC calculation
212 
213  // Check if data length is odd or even
214  is_odd = (bool) (size & 0x01); // data length is even if least significant bit = 0
215  // data length is odd if least significant bit = 1
216  if(is_odd)
217  data_size = (size & 0xFE); // capture largest even length
218  else
219  data_size = size; // copy data length as is
220 
221  // Calculate CRC16 across defined data array by always
222  // loading two bytes into 16-bit wide FIFO buffer
223 
224  for(i=start; i < (start + data_size); i+=2)
225  {
226  //merge two data bytes
227  fifo_buf = ((data[i + 1]) << 8 | data[i]);
228 
229  while(crc->CrcConl.bits.CRCFUL); // wait if FIFO is full
230  _CRCIF = 0; // clear the interrupt flag
231  crc->CrcDatl.value = fifo_buf; // load 16-bit word data into FIFO
232  while(!_CRCIF); // Wait until all shifts have been completed
233 
234  }
235 
236  // Read CRC result of even number of bytes
237  crc->CrcConl.bits.CRCGO = 0;
238  crc_buf = crc->CrcWdatl.value;
239 
240  // Processing a standard polynomial produces a reversed
241  // bit order of the result. The following macro reverses
242  // the bit order of the 16-bit wide CRC result
243  crc_buf = ReverseBitOrder16b(crc_buf);
244 
245  // If data length was odd, process last byte manually
246  // in reverse order to finish up CRC result
247  if(is_odd){
248 
249 // rev_poly = CRC16_PVIZ_REV_POLYNOMIAL;
250  rev_poly = ReverseBitOrder16b(crc->CrcXorl.value);
251  crc_buf ^= data[start + size - 1];
252 
253  for (i = 0; i < 8; ++i) // Process last 8-bit of last, odd data byte
254  {
255  if (crc_buf & 0x0001)
256  crc_buf = (crc_buf >> 1) ^ rev_poly;
257  else
258  crc_buf = (crc_buf >> 1);
259  }
260  }
261 
262  return(crc_buf);
263 }
264 
265 /* ============================================================================== */
266 /* ============================================================================== */
267 /* ============================================================================== */
268 
269 /*********************************************************************************
270  * @var crcModuleConfigClear
271  * @ingroup lib-layer-pral-properties-private-crc
272  * @brief Default RESET configuration of CRC module
273  *
274  * @details
275  * Default configuration of the CRC module SFRs with all its registers
276  * being reset to their default state when the device comes out of RESET.
277  * The minimum and maximum addresses specifying the memory range addressable
278  * by the CRC module affects/limits all CRC channels.
279  * (Please read the device data sheet for details)
280  *
281  * Programmers can use this template to reset a previously used
282  * CRC module when it's not used anymore or to ensure a known startup
283  * condition before writing individual configurations to its SFRs.
284  *
285  ********************************************************************************/
286 
287 volatile struct P33C_CRC_MODULE_s crcModuleConfigClear = {
288 
289  .CrcConl.value = 0x0000,
290  .CrcConh.value = 0x0000,
291  .CrcDath.value = 0x0000,
292  .CrcDatl.value = 0x0000,
293  .CrcWdath.value= 0x0000,
294  .CrcWdatl.value= 0x0000,
295  .CrcXorh.value = 0x0000,
296  .CrcXorl.value = 0x0000,
297 };
298 
299 /*********************************************************************************
300  * @var crcModuleConfigPviz
301  * @ingroup lib-layer-pral-properties-private-crc
302  * @brief Default configuration for PVIZ communication protocol of CRC module
303  *
304  * @details
305  * Default configuration of the CRC module when used to calculate the
306  * 16-bit wide Cyclic Redundancy Check in Power Board Visualizer protocol
307  * applications.
308  ********************************************************************************/
309 
310 volatile struct P33C_CRC_MODULE_s crcModuleConfigPviz = {
311 
312  // Initialize configuration registers for standard CRC calculation
313  .CrcConl.bits.CRCEN = 1, // enable CRC
314  .CrcConl.bits.MOD = 1, // 0=Legacy Mode, 1=Alternate Mode
315  .CrcConl.bits.CRCISEL = 0, // interrupt when all shifts are done
316  .CrcConl.bits.LENDIAN = 1, // 1=little endian, 0=big endian
317 
318  .CrcConh.bits.DWIDTH = (16-1), // 16-bit data width (even if 8-bit data is used)
319  .CrcConh.bits.PLEN = (16-1), // 16-bit polynomial order
320 
321  // Set polynomial and seed
322  .CrcConl.bits.CRCGO = 0, // disable CRC calculation
323  .CrcXorl.value = CRC16_PVIZ_POLYNOMIAL, // set polynomial
324  .CrcXorh.value = 0,
325  .CrcWdatl.value= CRC16_PVIZ_SEED_VALUE, // set initial value
326  .CrcDath.value = 0
327 };
328 
329 
330 // end of file
volatile struct tagCRCCONLBITS bits
Definition: p33c_crc.h:63
union P33C_CRC_MODULE_s::@48 CrcDatl
union P33C_CRC_MODULE_s::@44 CrcConl
union P33C_CRC_MODULE_s::@50 CrcWdatl
union P33C_CRC_MODULE_s::@46 CrcXorl
union P33C_CRC_MODULE_s::@49 CrcDath
volatile uint16_t value
Definition: p33c_crc.h:64
union P33C_CRC_MODULE_s::@47 CrcXorh
union P33C_CRC_MODULE_s::@45 CrcConh