summaryrefslogtreecommitdiff
path: root/bsp/es32f0334/libraries/ES32F033x_ALD_StdPeriph_Driver/Source/ald_crc.c
blob: be7e51b97df26c3bb0348e60ca24c2ce70eab402 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
  *********************************************************************************
  *
  * @file    ald_crc.c
  * @brief   CRC module driver.
  *
  * @version V1.0
  * @date    6 Dec 2017
  * @author  AE Team
  * @note
  *
  * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  *
  *********************************************************************************
  */

#include "ald_crc.h"

/** @addtogroup ES32FXXX_ALD
  * @{
  */

/** @defgroup CRC CRC
  * @brief CRC module driver
  * @{
  */
#ifdef ALD_CRC

/** @addtogroup CRC_Private_Functions   CRC Private Functions
  * @{
  */
void crc_reset(crc_handle_t *hperh);
#ifdef ALD_DMA
static void crc_dma_calculate_cplt(void *arg);
static void crc_dma_error(void *arg);
#endif
/**
  * @}
  */


/** @defgroup CRC_Public_Functions CRC Public Functions
  * @{
  */

/** @defgroup CRC_Public_Functions_Group1 Initialization functions
  * @brief Initialization and Configuration functions
  * @{
  */

/**
  * @brief  Initializes the CRC mode according to the specified parameters in
  *         the crc_handle_t and create the associated handle.
  * @param  hperh: Pointer to a crc_handle_t structure that contains
  *         the configuration information for the specified CRC module.
  * @retval Status, see @ref ald_status_t.
  */
ald_status_t crc_init(crc_handle_t *hperh)
{
	uint32_t tmp = 0;

	if (hperh == NULL)
		return ERROR;

	assert_param(IS_CRC(hperh->perh));
	assert_param(IS_CRC_MODE(hperh->init.mode));
	assert_param(IS_FUNC_STATE(hperh->init.chs_rev));
	assert_param(IS_FUNC_STATE(hperh->init.data_inv));
	assert_param(IS_FUNC_STATE(hperh->init.data_rev));
	assert_param(IS_FUNC_STATE(hperh->init.chs_inv));

	crc_reset(hperh);
	__LOCK(hperh);

	CRC_ENABLE(hperh);

	tmp = hperh->perh->CR;

	tmp |= ((hperh->init.chs_rev << CRC_CR_CHSREV_POS) | (hperh->init.data_inv << CRC_CR_DATREV_POS) |
		(hperh->init.chs_inv << CRC_CR_CHSINV_POS) | (hperh->init.mode << CRC_CR_MODE_POSS) |
		(CRC_DATASIZE_8 << CRC_CR_DATLEN_POSS) | (hperh->init.data_rev << CRC_CR_DATREV_POS) |
		(1 << CRC_CR_BYTORD_POS));

	hperh->perh->CR = tmp;
	hperh->perh->SEED = hperh->init.seed;
	CRC_RESET(hperh);

	hperh->state = CRC_STATE_READY;

	__UNLOCK(hperh);
	return OK;
}

/**
  * @}
  */

/** @defgroup CRC_Public_Functions_Group2 Calculate functions
  * @brief Calculate functions
  * @{
  */

/**
  * @brief  Calculate the crc value of data.
  * @param  hperh: Pointer to a crc_handle_t structure that contains
  *         the configuration information for the specified CRC module.
  * @param  buf: Pointer to data buffer
  * @param  size: The size of data to be calculate
  * @retval result, the result of a amount data
  */
uint32_t crc_calculate(crc_handle_t *hperh, uint8_t *buf, uint32_t size)
{
	uint32_t i;
	uint32_t ret;

	assert_param(IS_CRC(hperh->perh));

	if (buf == NULL || size == 0)
		return 0;

	__LOCK(hperh);
	hperh->state = CRC_STATE_BUSY;

	for (i = 0; i < size; i++)
 		CRC->DATA = buf[i];

	ret          = CRC->CHECKSUM;
	hperh->state = CRC_STATE_READY;
	__UNLOCK(hperh);

	return ret;
}
/**
  * @}
  */

#ifdef ALD_DMA
/** @defgroup CRC_Public_Functions_Group3 DMA operation functions
  * @brief DMA operation functions
  * @{
  */

/**
  * @brief  Calculate an amount of data used dma channel
  * @param  hperh: Pointer to a crc_handle_t structure that contains
  *         the configuration information for the specified CRC module.
  * @param  buf: Pointer to data buffer
  * @param  res: Pointer to result
  * @param  size: Amount of data to be Calculate
  * @param  channel: DMA channel as CRC transmit
  * @retval Status, see @ref ald_status_t.
  */
ald_status_t crc_calculate_by_dma(crc_handle_t *hperh, uint8_t *buf, uint32_t *res, uint16_t size, uint8_t channel)
{
	if (hperh->state != CRC_STATE_READY)
		return BUSY;

	if (buf == NULL || size == 0)
		return ERROR;

	__LOCK(hperh);

	hperh->state = CRC_STATE_BUSY;

	hperh->cal_buf = buf;
	hperh->cal_res = res;

	if (hperh->hdma.perh == NULL)
		hperh->hdma.perh = DMA0;

	hperh->hdma.cplt_arg = (void *)hperh;
	hperh->hdma.cplt_cbk = &crc_dma_calculate_cplt;
	hperh->hdma.err_arg  = (void *)hperh;
	hperh->hdma.err_cbk  = &crc_dma_error;

	dma_config_struct(&(hperh->hdma.config));
	hperh->hdma.config.data_width = DMA_DATA_SIZE_BYTE;
	hperh->hdma.config.src        = (void *)buf;
	hperh->hdma.config.dst        = (void *)&hperh->perh->DATA;
	hperh->hdma.config.size       = size;
	hperh->hdma.config.src_inc    = DMA_DATA_INC_BYTE;
	hperh->hdma.config.dst_inc    = DMA_DATA_INC_NONE;
	hperh->hdma.config.msel       = DMA_MSEL_CRC;
	hperh->hdma.config.msigsel    = DMA_MSIGSEL_NONE;
	hperh->hdma.config.channel    = channel;
	dma_config_basic(&(hperh->hdma));

	__UNLOCK(hperh);
	CRC_DMA_ENABLE(hperh);

	return OK;
}

/**
  * @brief  Pauses the DMA Transfer.
  * @param  hperh: Pointer to a crc_handle_t structure that contains
  *         the configuration information for the specified CRC module.
  * @retval Status, see @ref ald_status_t.
  */
ald_status_t crc_dma_pause(crc_handle_t *hperh)
{
	__LOCK(hperh);
	CRC_DMA_DISABLE(hperh);
	__UNLOCK(hperh);

	return OK;
}

/**
  * @brief  Resumes the DMA Transfer.
  * @param  hperh: Pointer to a crc_handle_t structure that contains
  *         the configuration information for the specified CRC module.
  * @retval Status, see @ref ald_status_t.
  */
ald_status_t crc_dma_resume(crc_handle_t *hperh)
{
	__LOCK(hperh);
	CRC_DMA_ENABLE(hperh);
	__UNLOCK(hperh);

	return OK;
}

/**
  * @brief  Stops the DMA Transfer.
  * @param  hperh: Pointer to a crc_handle_t structure that contains
  *         the configuration information for the specified CRC module.
  * @retval Status, see @ref ald_status_t.
  */
ald_status_t crc_dma_stop(crc_handle_t *hperh)
{
	__LOCK(hperh);
	CRC_DMA_DISABLE(hperh);
	__UNLOCK(hperh);

	hperh->state = CRC_STATE_READY;
	return OK;
}

/**
  * @}
  */
#endif

/** @defgroup CRC_Public_Functions_Group4 Peripheral State and Errors functions
  * @brief    CRC State and Errors functions
  * @{
  */

/**
  * @brief  Returns the CRC state.
  * @param  hperh: Pointer to a crc_handle_t structure that contains
  *         the configuration information for the specified CRC module.
  * @retval CRC state
  */
crc_state_t crc_get_state(crc_handle_t *hperh)
{
	assert_param(IS_CRC(hperh->perh));

	return hperh->state;
}
/**
  * @}
  */

/**
  * @}
  */

/** @defgroup CRC_Private_Functions   CRC Private Functions
  *  @brief   CRC Private functions
  * @{
  */

/**
  * @brief  Reset the CRC peripheral.
  * @param  hperh: Pointer to a crc_handle_t structure that contains
  *         the configuration information for the specified CRC module.
  * @retval None
  */
void crc_reset(crc_handle_t *hperh)
{
	hperh->perh->DATA = 0x0;
	hperh->perh->CR   = 0x2;
	hperh->perh->SEED = 0xFFFFFFFF;

	hperh->state = CRC_STATE_READY;
	__UNLOCK(hperh);
	return;
}

#ifdef ALD_DMA
/**
  * @brief  DMA CRC calculate process complete callback.
  * @param  arg: Pointer to a crc_handle_t structure that contains
  *         the configuration information for the specified CRC module.
  * @retval None
  */
static void crc_dma_calculate_cplt(void *arg)
{
	crc_handle_t *hperh = (crc_handle_t *)arg;

	*(hperh->cal_res) = CRC->CHECKSUM;
	CRC_DMA_DISABLE(hperh);

	hperh->state = CRC_STATE_READY;

	if (hperh->cal_cplt_cbk)
		hperh->cal_cplt_cbk(hperh);
}

/**
  * @brief  DMA CRC communication error callback.
  * @param  arg: Pointer to a crc_handle_t structure that contains
  *         the configuration information for the specified CRC module.
  * @retval None
  */
static void crc_dma_error(void *arg)
{
	crc_handle_t *hperh = (crc_handle_t *)arg;

	CRC_CLEAR_ERROR_FLAG(hperh);
	CRC_DMA_DISABLE(hperh);

	hperh->state = CRC_STATE_READY;

	if (hperh->err_cplt_cbk)
		hperh->err_cplt_cbk(hperh);
}
#endif
/**
  * @}
  */

/**
  * @}
  */
#endif /* ALD_CRC */

/**
  * @}
  */