S32 SDK
crc_driver.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
44 #include "crc_driver.h"
45 
46 /*******************************************************************************
47  * Variables
48  ******************************************************************************/
49 /* Table of base addresses for CRC instances. */
51 
52 /*******************************************************************************
53  * Code
54  ******************************************************************************/
55 /*FUNCTION**********************************************************************
56  *
57  * Function Name : CRC_DRV_Init
58  * Description : This function initializes CRC driver based on user configuration input.
59  * The user must make sure that the clock is enabled.
60  *
61  * Implements : CRC_DRV_Init_Activity
62  *END**************************************************************************/
63 status_t CRC_DRV_Init(uint32_t instance,
64  const crc_user_config_t * userConfigPtr)
65 {
66  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
67  DEV_ASSERT(userConfigPtr != NULL);
68  CRC_Type * base = g_crcBase[instance];
69  status_t retStatus = STATUS_SUCCESS;
70 
71  /* Set the default configuration */
72  CRC_HAL_Init(base);
73  /* Set the CRC configuration */
74  retStatus = CRC_DRV_Configure(instance, userConfigPtr);
75 
76  return retStatus;
77 }
78 
79 /*FUNCTION**********************************************************************
80  *
81  * Function Name : CRC_DRV_Deinit
82  * Description : This function sets the default configuration.
83  *
84  * Implements : CRC_DRV_Deinit_Activity
85  *END**************************************************************************/
86 status_t CRC_DRV_Deinit(uint32_t instance)
87 {
88  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
89  CRC_Type * base = g_crcBase[instance];
90 
91  /* Set the default configuration */
92  CRC_HAL_Init(base);
93 
94  return STATUS_SUCCESS;
95 }
96 
97 /*FUNCTION**********************************************************************
98  *
99  * Function Name : CRC_DRV_WriteData
100  * Description : This function appends a block of bytes to the current CRC calculation.
101  *
102  * Implements : CRC_DRV_WriteData_Activity
103  *END**************************************************************************/
104 void CRC_DRV_WriteData(uint32_t instance,
105  const uint8_t * data,
106  uint32_t dataSize)
107 {
108  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
109  DEV_ASSERT(data != NULL);
110  uint32_t i;
111  CRC_Type * base = g_crcBase[instance];
112 
113  /* 8-bit reads and writes till end of data buffer */
114  for (i = 0U; i < dataSize; i++)
115  {
116  CRC_HAL_SetDataLLReg(base, data[i]);
117  }
118 }
119 
120 /*FUNCTION**********************************************************************
121  *
122  * Function Name : CRC_DRV_GetCrcResult
123  * Description : This function returns the current result of the CRC calculation.
124  *
125  * Implements : CRC_DRV_GetCrcResult_Activity
126  *END**************************************************************************/
127 uint32_t CRC_DRV_GetCrcResult(uint32_t instance)
128 {
129  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
130  const CRC_Type * base = g_crcBase[instance];
131 
132  /* Result of the CRC calculation */
133  return CRC_HAL_GetCrcResult(base);
134 }
135 
136 /*FUNCTION**********************************************************************
137  *
138  * Function Name : CRC_DRV_Configure
139  * Description : This function configures the CRC module from a user configuration structure.
140  *
141  * Implements : CRC_DRV_Configure_Activity
142  *END**************************************************************************/
143 status_t CRC_DRV_Configure(uint32_t instance,
144  const crc_user_config_t * userConfigPtr)
145 {
146  DEV_ASSERT(instance < CRC_INSTANCE_COUNT);
147  DEV_ASSERT(userConfigPtr != NULL);
148  CRC_Type * base = g_crcBase[instance];
149 
150  /* Set CRC mode */
151  CRC_HAL_SetProtocolWidth(base, userConfigPtr->crcWidth);
152 
153  /* Set transposes and complement options */
154  CRC_HAL_SetWriteTranspose(base, userConfigPtr->writeTranspose);
155  CRC_HAL_SetReadTranspose(base, userConfigPtr->readTranspose);
156  CRC_HAL_SetFXorMode(base, userConfigPtr->complementChecksum);
157 
158  /* 3Write a polynomial */
159  CRC_HAL_SetPolyReg(base, userConfigPtr->polynomial);
160 
161  /* Write a seed - initial checksum */
162  CRC_HAL_SetSeedOrDataMode(base, true);
163  CRC_HAL_SetDataReg(base, userConfigPtr->seed);
164  CRC_HAL_SetSeedOrDataMode(base, false);
165 
166  return STATUS_SUCCESS;
167 }
168 
169 /*******************************************************************************
170  * EOF
171  ******************************************************************************/
#define CRC_BASE_PTRS
Definition: S32K144.h:2035
status_t CRC_DRV_Init(uint32_t instance, const crc_user_config_t *userConfigPtr)
Initializes the CRC module.
Definition: crc_driver.c:63
static void CRC_HAL_SetWriteTranspose(CRC_Type *const base, crc_transpose_t transp)
Sets the CRC transpose type for writes.
Definition: crc_hal.h:563
static void CRC_HAL_SetDataLLReg(CRC_Type *const base, uint8_t value)
Sets the Low Lower Byte - LL.
Definition: crc_hal.h:298
static void CRC_HAL_SetReadTranspose(CRC_Type *const base, crc_transpose_t transp)
Sets the CRC transpose type for reads.
Definition: crc_hal.h:615
status_t CRC_DRV_Configure(uint32_t instance, const crc_user_config_t *userConfigPtr)
Configures the CRC module from a user configuration structure.
Definition: crc_driver.c:143
uint32_t polynomial
Definition: crc_driver.h:51
static void CRC_HAL_SetSeedOrDataMode(CRC_Type *const base, bool enable)
Sets the CRC_DATA register mode.
Definition: crc_hal.h:428
crc_transpose_t readTranspose
Definition: crc_driver.h:54
CRC configuration structure. Implements : crc_user_config_t_Class.
Definition: crc_driver.h:47
#define DEV_ASSERT(x)
Definition: devassert.h:78
crc_bit_width_t crcWidth
Definition: crc_driver.h:49
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:31
uint32_t CRC_HAL_GetCrcResult(const CRC_Type *const base)
Returns the current result of the CRC calculation.
Definition: crc_hal.c:156
bool complementChecksum
Definition: crc_driver.h:55
status_t CRC_DRV_Deinit(uint32_t instance)
Sets the default configuration.
Definition: crc_driver.c:86
void CRC_HAL_Init(CRC_Type *const base)
Initializes the CRC module.
Definition: crc_hal.c:52
static void CRC_HAL_SetPolyReg(CRC_Type *const base, uint32_t value)
Sets the polynomial register.
Definition: crc_hal.h:327
crc_transpose_t writeTranspose
Definition: crc_driver.h:53
static void CRC_HAL_SetDataReg(CRC_Type *const base, uint32_t value)
Sets the 32 bits of CRC data register.
Definition: crc_hal.h:180
uint32_t CRC_DRV_GetCrcResult(uint32_t instance)
Returns the current result of the CRC calculation.
Definition: crc_driver.c:127
static void CRC_HAL_SetProtocolWidth(CRC_Type *const base, crc_bit_width_t width)
Sets the CRC protocol width.
Definition: crc_hal.h:511
#define CRC_INSTANCE_COUNT
Definition: S32K144.h:2024
void CRC_DRV_WriteData(uint32_t instance, const uint8_t *data, uint32_t dataSize)
Appends a block of bytes to the current CRC calculation.
Definition: crc_driver.c:104
static void CRC_HAL_SetFXorMode(CRC_Type *const base, bool enable)
Sets complement read of CRC data register.
Definition: crc_hal.h:467
CRC_Type *const g_crcBase[]
Table of base addresses for CRC instances.
Definition: crc_driver.c:50