1 | /*******************************************************************************
|
2 | ce451 main faunction
|
3 |
|
4 | Company:
|
5 | Microchip Technology Inc.
|
6 |
|
7 | File Name:
|
8 | crc_generation.c
|
9 |
|
10 | Summary:
|
11 | This file is used to call CRC APIs
|
12 |
|
13 | Description:
|
14 | The main.c is used to call the CRC Initialization function and function to calculate checksum.
|
15 | *******************************************************************************/
|
16 | /*******************************************************************************
|
17 | Copyright (c) 2012 released Microchip Technology Inc. All rights reserved.
|
18 |
|
19 | Microchip licenses to you the right to use, modify, copy and distribute
|
20 | Software only when embedded on a Microchip microcontroller or digital signal
|
21 | controller that is integrated into your product or third party product
|
22 | (pursuant to the sublicense terms in the accompanying license agreement).
|
23 |
|
24 | You should refer to the license agreement accompanying this Software for
|
25 | additional information regarding your rights and obligations.
|
26 |
|
27 | SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
28 | EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
|
29 | MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
|
30 | IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
|
31 | CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
|
32 | OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
|
33 | INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
|
34 | CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
|
35 | SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
|
36 | (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
|
37 | *******************************************************************************/
|
38 | // *****************************************************************************
|
39 | // *****************************************************************************
|
40 | // Section: Included Files
|
41 | // *****************************************************************************
|
42 | // *****************************************************************************
|
43 | #include <xc.h>
|
44 | #include <stdint.h>
|
45 | #include "crc.h"
|
46 |
|
47 |
|
48 | // Expected result depends on the contents of messageString[8] Array. If the content is changed
|
49 | // the below CRC value may not suit. The below CRC checksum is for data 0x01 to 0x08(given).
|
50 |
|
51 | #define EXPECTED_RESULT 0x76AC
|
52 |
|
53 | // *****************************************************************************
|
54 | // *****************************************************************************
|
55 | // Section: File Scope or Global Constants
|
56 | // *****************************************************************************
|
57 | // *****************************************************************************
|
58 | uint8_t messageString[8] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
|
59 | uint16_t crcResult=0;
|
60 | /******************************************************************************
|
61 | * Function: int main(void)
|
62 | *
|
63 | * PreCondition: None
|
64 | *
|
65 | * Input: None
|
66 | *
|
67 | * Output: None
|
68 | *
|
69 | * Side Effects: None
|
70 | *
|
71 | * Overview: Main function
|
72 | *****************************************************************************/
|
73 | #ifdef TEST_MODE
|
74 | int ce_main(void)
|
75 | #else
|
76 | int main(void)
|
77 | #endif
|
78 | {
|
79 | uint16_t prev_CRC = 0x0000;
|
80 |
|
81 | CRC_Init();
|
82 |
|
83 | Nop();
|
84 | Nop();
|
85 |
|
86 | crcResult = CRC_ChecksumByte( messageString, 8, prev_CRC );
|
87 | #ifdef TEST_MODE
|
88 | if(crcResult == EXPECTED_RESULT )
|
89 | return 0;
|
90 | else
|
91 | return 1;
|
92 | #else
|
93 | while( 1 );
|
94 | #endif
|
95 | }
|
96 |
|
97 | /******************************************************************************
|
98 | * Function: void CRC_Init(void)
|
99 | *
|
100 | * PreCondition: None
|
101 | *
|
102 | * Input: None
|
103 | *
|
104 | * Output: None
|
105 | *
|
106 | * Side Effects: None
|
107 | *
|
108 | * Overview: This function configures CRC module
|
109 | *****************************************************************************/
|
110 | void CRC_Init( void )
|
111 | {
|
112 |
|
113 | CRCCON1bits.CRCEN = 0;
|
114 | CRCXORL = 0x0000;
|
115 | Nop();
|
116 | Nop();
|
117 |
|
118 | CRCWDATL = 0x0000;
|
119 |
|
120 | CRCWDATH = 0x0000; // NOTE:Non-direct Initial value fed to CRCWDAT
|
121 | CRCXORL = POLY; // Generator Polynomial // X^12 + X^5
|
122 | CRCXORH = 0x0000;
|
123 | CRCCON1bits.CRCISEL = 0;
|
124 | CRCCON2bits.PLEN = POLYLEN; // Length of polynomial-1"
|
125 | CRCCON2bits.DWIDTH = 7;
|
126 | CRCCON1bits.LENDIAN = 0x0;
|
127 | CRCCON1bits.CRCEN = 1;
|
128 | }
|
129 |
|
130 | /******************************************************************************
|
131 | * Function: uint16_t CRC_ChecksumByte(uint8_t* data, uint8_t Number_of_bytes, uint16_t prev_CRC)
|
132 | *
|
133 | * PreCondition: None
|
134 | *
|
135 | * Input: data - Pointer to the first data byte for which CRC needs to be
|
136 | * calculated.
|
137 | * Number_of_bytess - Total number of bytes for which CRC needs to be
|
138 | * calculated.
|
139 | * prev_CRC - previous CRC result.
|
140 | *
|
141 | * Output: CRCWDATL Register content that is the two byte checksum
|
142 | *
|
143 | * Side Effects: None
|
144 | *
|
145 | * Overview: Calculates the checksum and returns the value
|
146 | *****************************************************************************/
|
147 | uint16_t CRC_ChecksumByte( uint8_t *data, uint8_t Number_of_bytes, uint16_t prev_CRC )
|
148 | {
|
149 | uint8_t volatile *dest = ( uint8_t * ) &CRCDATL;
|
150 |
|
151 |
|
152 | CRCWDATL = prev_CRC;
|
153 | IFS4bits.CRCIF=0; //CRC status Flag is Cleared
|
154 | do
|
155 | {
|
156 | while( (0 == CRCCON1bits.CRCFUL) && (0 < Number_of_bytes) )
|
157 | {
|
158 | *dest = *data;
|
159 | data++;
|
160 | Number_of_bytes--;
|
161 | }
|
162 | } while( 0 < Number_of_bytes );
|
163 | CRCCON1bits.CRCGO = 1;
|
164 | CRCDATL = 0x0000; /* Do this to shift the last word out of the CRC shift register */
|
165 | while( CRCCON1bits.CRCFUL == 1 );
|
166 |
|
167 |
|
168 |
|
169 | Nop();
|
170 | Nop();
|
171 | Nop();
|
172 | Nop();
|
173 | Nop();
|
174 | Nop();
|
175 | while(CRCCON1bits.CRCMPT!=1);
|
176 |
|
177 | Nop();
|
178 | Nop();
|
179 | Nop();
|
180 | Nop();
|
181 | Nop();
|
182 | Nop();
|
183 | while(IFS4bits.CRCIF!=1);
|
184 |
|
185 | Nop();
|
186 | Nop();
|
187 | Nop();
|
188 | Nop();
|
189 | Nop();
|
190 | Nop();
|
191 | CRCCON1bits.CRCGO=0;
|
192 | Nop();
|
193 | Nop();
|
194 | Nop();
|
195 | Nop();
|
196 | Nop();
|
197 | return ( CRCWDATL );
|
198 | }
|
199 |
|
200 | /*******************************************************************************
|
201 | End of File
|
202 | */
|