summaryrefslogtreecommitdiff
path: root/bsp/lpc408x/Libraries/Drivers/source/lpc_crc.c
blob: 8d74c052ea859d139038a7dc45083f36c884a054 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**********************************************************************
* $Id$      lpc_crc.c           2011-06-02
*//**
* @file     lpc_crc.c
* @brief    Contains all functions support for CRC firmware library on
*           LPC
* @version  1.0
* @date     02. June. 2011
* @author   NXP MCU SW Application Team
* 
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, under NXP Semiconductors'
* relevant copyright in the software, without fee, provided that it
* is used in conjunction with NXP Semiconductors microcontrollers.  This
* copyright, permission, and disclaimer notice must appear in all copies of
* this code.
**********************************************************************/
#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc_libcfg.h"
#else
#include "lpc_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */
#ifdef _CRC

/* Includes ------------------------------------------------------------------- */
#include "lpc_crc.h"
#include "lpc_clkpwr.h"

/* Private Variables ----------------------------------------------------------- */
volatile CRC_Type crc_cur_type;

/* Public Functions ----------------------------------------------------------- */
/** @addtogroup CRC_Public_Functions
 * @{
 */
/*********************************************************************//**
 * @brief       Initialize CRC operation
 * @param[in]   CRCType CRC standard type, should be:
 *              - CRC_POLY_CRCCCITT: CRC-CCITT polynomial
 *              - CRC_POLY_CRC16: CRC-16 polynomial
 *              - CRC_POLY_CRC32: CRC-32 polynomial
 * @return      None
 **********************************************************************/
void CRC_Init(CRC_Type CRCType)
{
    if(CRCType == CRC_POLY_CRCCCITT)
    {
        LPC_CRC->MODE = 0x00;
        LPC_CRC->SEED = 0xFFFF;
        crc_cur_type = CRC_POLY_CRCCCITT;
    }
    else if(CRCType == CRC_POLY_CRC16)
    {
        LPC_CRC->MODE = 0x15;
        LPC_CRC->SEED = 0x0000;
        crc_cur_type = CRC_POLY_CRC16;

    }
    else if(CRCType == CRC_POLY_CRC32)
    {
        LPC_CRC->MODE = 0x36;
        LPC_CRC->SEED = 0xFFFFFFFF;
        crc_cur_type = CRC_POLY_CRC32;
    }
    else
    {
        //Invalid input parameter
        while(1);//error loop
    }
}
/*********************************************************************//**
 * @brief       CRC reset
 * @param[in]   None
 * @return      None
 **********************************************************************/
void CRC_Reset(void)
{
    if(crc_cur_type == CRC_POLY_CRCCCITT)
        LPC_CRC->SEED = 0xFFFF;
    else if (crc_cur_type == CRC_POLY_CRC16)
        LPC_CRC->SEED = 0x0000;
    else if (crc_cur_type == CRC_POLY_CRC32)
        LPC_CRC->SEED = 0xFFFFFFFF;
}

/*********************************************************************//**
 * @brief       CRC data checksum calculation
 * @param[in]   data    input data
 * @return      data checksum result
 **********************************************************************/
uint32_t CRC_CalcDataChecksum(uint32_t data, CRC_WR_SIZE SizeType)
{
    if(SizeType == CRC_WR_8BIT)
        LPC_CRC->WR_DATA_BYTE.DATA = (uint8_t)data;
    else if(SizeType == CRC_WR_16BIT)
        LPC_CRC->WR_DATA_WORD.DATA = (uint16_t)data;
    else
        LPC_CRC->WR_DATA_DWORD.DATA = data;
    return(LPC_CRC->SUM);
}

/*********************************************************************//**
 * @brief       CRC block data checksum calculation
 * @param[in]   blockdata   pointer to block input data
 * @param[in]   blocksize   size of block data
 * @param[in]   SizeType    size of data width per write, should be:
                - CRC_WR_8BIT : 8-bit write
                - CRC_WR_16BIT: 16-bit write
                - CRC_WR_32BIT: 32-bit write
 * @return      block data checksum result
 **********************************************************************/
uint32_t CRC_CalcBlockChecksum(void *block_data, uint32_t block_size, CRC_WR_SIZE data_size)
{
    uint8_t  *data = (uint8_t*) block_data;
    
    while(block_size !=0) {
        
      switch(data_size) {
        
        case CRC_WR_8BIT:
        {
           uint8_t *tmp = data;
               LPC_CRC->WR_DATA_BYTE.DATA = *tmp;
                   data++;
        }
        break;
        case CRC_WR_16BIT:
        {
           uint16_t *tmp = (uint16_t*)data;
                   LPC_CRC->WR_DATA_WORD.DATA = *tmp;
                   data +=2;
        }
         break;
        default:
        {
           uint32_t *tmp = (uint32_t*)data;
                   LPC_CRC->WR_DATA_DWORD.DATA = *tmp;
                   data += 4;
        }
        break;
      }
      block_size--;
    }
    return(LPC_CRC->SUM);
}

/**
 * @}
 */
#endif /*_CRC*/

/**
 * @}
 */

/* --------------------------------- End Of File ------------------------------ */