aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/src/hal_crc.c
blob: 63799e441812913246ffa3b7004b2f31915802df (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
/*
    ChibiOS - Copyright (C) 2015 Michael D. Spradling

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/*
 * Hardware Abstraction Layer for CRC Unit
 */
#include "hal.h"

#if (HAL_USE_CRC == TRUE) || defined(__DOXYGEN__)

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local variables and types.                                         */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   CRC Driver initialization.
 *
 * @init
 */
void crcInit(void) {
  crc_lld_init();
}

/**
 * @brief   Initializes the standard part of a @p CRCDriver structure.
 *
 * @param[out] crcp    Pointer to the @p CRCDriver object
 *
 * @init
 */
void crcObjectInit(CRCDriver *crcp) {
  crcp->state  = CRC_STOP;
  crcp->config = NULL;
#if CRC_USE_DMA == TRUE
  crcp->thread = NULL;
#endif
#if CRC_USE_MUTUAL_EXCLUSION == TRUE
  osalMutexObjectInit(&crcp->mutex);
#endif
#if defined(CRC_DRIVER_EXT_INIT_HOOK)
  CRC_DRIVER_EXT_INIT_HOOK(crcp);
#endif
}

/**
 * @brief   Configures and activates the CRC peripheral.
 *
 * @param[in] crcp      Pointer to the @p CRCDriver object
 * @param[in] config    Pointer to the @p CRCConfig object
 *                      @p NULL if the low level driver implementation
 *                      supports a default configuration
 *
 * @api
 */
void crcStart(CRCDriver *crcp, const CRCConfig *config) {
  osalDbgCheck(crcp != NULL);

  osalSysLock();
  osalDbgAssert((crcp->state == CRC_STOP) || (crcp->state == CRC_READY),
                "invalid state");
  crcp->config = config;
  crc_lld_start(crcp);
  crcp->state = CRC_READY;
  osalSysUnlock();
}

/**
 * @brief   Deactivates the CRC peripheral.
 *
 * @param[in] crcp     Pointer to the @p CRCDriver object
 *
 * @api
 */
void crcStop(CRCDriver *crcp) {
  osalDbgCheck(crcp != NULL);

  osalSysLock();
  osalDbgAssert((crcp->state == CRC_STOP) || (crcp->state == CRC_READY),
                "invalid state");
  crc_lld_stop(crcp);
  crcp->state = CRC_STOP;
  osalSysUnlock();
}

/**
 * @brief   Resets the CRC calculation
 *
 * @param[in] crcp     Pointer to the @p CRCDriver object
 *
 * @api
 */
void crcReset(CRCDriver *crcp) {
  osalSysLock();
  crcResetI(crcp);
  osalSysUnlock();
}

/**
 * @brief   Resets the current CRC calculation
 *
 * @param[in] crcp      pointer to the @p CRCDriver object
 *
 * @iclass
 */
void crcResetI(CRCDriver *crcp) {
  osalDbgCheck(crcp != NULL);
  osalDbgAssert(crcp->state == CRC_READY, "Not ready");
  crc_lld_reset(crcp);
}

/**
 * @brief   Performs a CRC calculation.
 * @details This synchronous function performs a crc calculation operation.
 * @pre     In order to use this function the driver must have been configured
 *          without callbacks (@p end_cb = @p NULL).
 *
 * @param[in] crcp      pointer to the @p CRCDriver object
 * @param[in] n         number of bytes to send
 * @param[in] buf       the pointer to the buffer
 *
 * @api
 */
uint32_t crcCalc(CRCDriver *crcp, size_t n, const void *buf) {
  uint32_t crc;
#if CRC_USE_DMA
  osalSysLock();
#endif
  crc = crcCalcI(crcp, n, buf);
#if CRC_USE_DMA
  osalSysUnlock();
#endif
  return crc;
}

/**
 * @brief   Performs a CRC calculation.
 * @details This synchronous function performs a crc calcuation operation.
 * @pre     In order to use this function the driver must have been configured
 *          without callbacks (@p end_cb = @p NULL).
 * @post    At the end of the operation the configured callback is invoked.
 *
 * @param[in] crcp      pointer to the @p CRCDriver object
 * @param[in] n         number of bytes to send
 * @param[in] buf       the pointer to the buffer
 *
 * @iclass
 */
uint32_t crcCalcI(CRCDriver *crcp, size_t n, const void *buf) {
  osalDbgCheck((crcp != NULL) && (n > 0U) && (buf != NULL));
  osalDbgAssert(crcp->state == CRC_READY, "not ready");
#if CRC_USE_DMA
  osalDbgAssert(crcp->config->end_cb == NULL, "callback defined");
  (crcp)->state = CRC_ACTIVE;
#endif
  return crc_lld_calc(crcp, n, buf);
}

#if CRC_USE_DMA == TRUE
/**
 * @brief   Performs a CRC calculation.
 * @details This asynchronous function starts a crc calcuation operation.
 * @pre     In order to use this function the driver must have been configured
 *          with callbacks (@p end_cb != @p NULL).
 * @post    At the end of the operation the configured callback is invoked.
 *
 * @param[in] crcp      pointer to the @p CRCDriver object
 * @param[in] n         number of bytes to send
 * @param[in] buf       the pointer to the buffer
 *
 * @api
 */
void crcStartCalc(CRCDriver *crcp, size_t n, const void *buf) {
  osalSysLock();
  crcStartCalcI(crcp, n, buf);
  osalSysUnlock();
}

/**
 * @brief   Performs a CRC calculation.
 * @details This asynchronous function starts a crc calcuation operation.
 * @pre     In order to use this function the driver must have been configured
 *          without callbacks (@p end_cb = @p NULL).
 * @post    At the end of the operation the configured callback is invoked.
 *
 * @param[in] crcp      pointer to the @p CRCDriver object
 * @param[in] n         number of bytes to send
 * @param[in] buf       the pointer to the buffer
 *
 *
 * @iclass
 */
void crcStartCalcI(CRCDriver *crcp, size_t n, const void *buf) {
  osalDbgCheck((crcp != NULL) && (n > 0U) && (buf != NULL));
  osalDbgAssert(crcp->state == CRC_READY, "not ready");
  osalDbgAssert(crcp->config->end_cb != NULL, "callback not defined");
  (crcp)->state = CRC_ACTIVE;
  crc_lld_start_calc(crcp, n, buf);
}
#endif

#if (CRC_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__)
/**
 * @brief   Gains exclusive access to the CRC unit.
 * @details This function tries to gain ownership to the CRC, if the CRC is
 *          already being used then the invoking thread is queued.
 * @pre     In order to use this function the option @p CRC_USE_MUTUAL_EXCLUSION
 *          must be enabled.
 *
 * @param[in] crcp      pointer to the @p CRCDriver object
 *
 * @api
 */
void crcAcquireUnit(CRCDriver *crcp) {
  osalDbgCheck(crcp != NULL);

  osalMutexLock(&crcp->mutex);
}

/**
 * @brief   Releases exclusive access to the CRC unit.
 * @pre     In order to use this function the option @p CRC_USE_MUTUAL_EXCLUSION
 *          must be enabled.
 *
 * @param[in] crcp      pointer to the @p CRCDriver object
 *
 * @api
 */
void crcReleaseUnit(CRCDriver *crcp) {
  osalDbgCheck(crcp != NULL);

  osalMutexUnlock(&crcp->mutex);
}
#endif /* CRC_USE_MUTUAL_EXCLUSION == TRUE */

#endif /* HAL_USE_CRC */