1 /***************************************************************************//**
2  * \file cyhal_crc_impl.h
3 *
4 * Description:
5 * Provides a high level interface for interacting with the Infineon CRC accelerator.
6 * This is a wrapper around the lower level PDL API.
7 *
8 ********************************************************************************
9 * \copyright
10 * Copyright 2018-2022 Cypress Semiconductor Corporation (an Infineon company) or
11 * an affiliate of Cypress Semiconductor Corporation
12 *
13 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 *     http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 *******************************************************************************/
27 
28 #pragma once
29 
30 #include "cyhal_crc.h"
31 #include "cyhal_hwmgr.h"
32 #include "cy_utils.h"
33 
34 #if (CYHAL_DRIVER_AVAILABLE_CRC)
35 
36 #if defined(__cplusplus)
37 extern "C" {
38 #endif /* __cplusplus */
39 
40 #if defined(CY_IP_MXCRYPTO)
41 #define _cyhal_crc_calcinit(base, width, polynomial, data_reverse, data_xor, rem_reverse, rem_xor, lfsr_init_state) \
42             Cy_Crypto_Core_Crc_CalcInit((base), (width), (polynomial), (data_reverse) ? 1u : 0u, (data_xor), (rem_reverse) ? 1u : 0u, (rem_xor), (lfsr_init_state))
43 
44 #define _cyhal_crc_calcpartial(base, data, length) \
45             Cy_Crypto_Core_Crc_CalcPartial((base), (data), (length))
46 
47 #define _cyhal_crc_calc_finish(base, crc) \
48             Cy_Crypto_Core_Crc_CalcFinish((base), (obj->crc_width), (crc))
49 
50 #define _cyhal_crc_calc_free(base)
51 
52 #elif defined(CY_IP_M0S8CRYPTO)
53 extern cy_stc_crypto_crc_context_t _cyhal_crc_context[CY_IP_M0S8CRYPTO_INSTANCES];
54 #define _cyhal_crc_calcinit(base, width, polynomial, data_reverse, data_xor, rem_reverse, rem_xor, lfsr_init_state) \
55             Cy_Crypto_Crc_CalcInit((base), (width), (polynomial), (data_reverse), (data_xor), (rem_reverse), (rem_xor), (lfsr_init_state), (&_cyhal_crc_context[obj->resource.block_num]))
56 
57 #define _cyhal_crc_calcpartial(base, data, length) \
58             Cy_Crypto_Crc_CalcPartial((base), (data), (length), (&_cyhal_crc_context[obj->resource.block_num]))
59 
60 #define _cyhal_crc_calc_finish(base, crc) \
61             Cy_Crypto_Crc_CalcFinish((base), (crc), (&_cyhal_crc_context[obj->resource.block_num]))
62 
63 #define _cyhal_crc_calc_free(base) \
64             Cy_Crypto_Crc_CalcFree((base), (&_cyhal_crc_context[obj->resource.block_num]))
65 
66 #endif
67 
68 // This helper function mirrors the definition of cyhal_crc_start
_cyhal_crc_start(cyhal_crc_t * obj,const crc_algorithm_t * algorithm)69 __STATIC_INLINE cy_rslt_t _cyhal_crc_start(cyhal_crc_t *obj, const crc_algorithm_t *algorithm)
70 {
71     CY_ASSERT(NULL != obj);
72     if(NULL == algorithm)
73         return CYHAL_CRC_RSLT_ERR_BAD_ARGUMENT;
74 
75     obj->crc_width = algorithm->width;
76     return _cyhal_crc_calcinit(obj->base,
77                                 algorithm->width,
78                                 algorithm->polynomial,
79                                 algorithm->dataReverse,
80                                 algorithm->dataXor,
81                                 algorithm->remReverse,
82                                 algorithm->remXor,
83                                 algorithm->lfsrInitState);
84 }
85 
86 #define cyhal_crc_start(obj, algorithm) _cyhal_crc_start((obj), (algorithm))
87 
88 // This helper function mirrors the definition of cyhal_crc_compute
_cyhal_crc_compute(const cyhal_crc_t * obj,const uint8_t * data,size_t length)89 __STATIC_INLINE cy_rslt_t _cyhal_crc_compute(const cyhal_crc_t *obj, const uint8_t *data, size_t length)
90 {
91     CY_ASSERT(NULL != obj);
92     if(NULL == data || 0 == length)
93         return CYHAL_CRC_RSLT_ERR_BAD_ARGUMENT;
94 
95     return _cyhal_crc_calcpartial(obj->base, data, length);
96 }
97 
98 #define cyhal_crc_compute(obj, data, length) _cyhal_crc_compute((obj), (data), (length))
99 
100 // This helper function mirrors the definition of cyhal_crc_finish
_cyhal_crc_finish(const cyhal_crc_t * obj,uint32_t * crc)101 __STATIC_INLINE cy_rslt_t _cyhal_crc_finish(const cyhal_crc_t *obj, uint32_t *crc)
102 {
103     CY_ASSERT(NULL != obj);
104     if(NULL == crc)
105         return CYHAL_CRC_RSLT_ERR_BAD_ARGUMENT;
106     return _cyhal_crc_calc_finish(obj->base, crc);
107 }
108 
109 #define cyhal_crc_finish(obj, crc) _cyhal_crc_finish((obj), (crc))
110 
111 #if defined(__cplusplus)
112 }
113 #endif /* __cplusplus */
114 
115 #endif /* CYHAL_DRIVER_AVAILABLE_CRC */
116