|
| 1 | +#ifndef MBED_CRC_HAL_API_H |
| 2 | +#define MBED_CRC_HAL_API_H |
| 3 | + |
| 4 | +#include <stdbool.h> |
| 5 | +#include <stddef.h> |
| 6 | +#include <stdint.h> |
| 7 | + |
| 8 | +#ifdef DEVICE_CRC |
| 9 | + |
| 10 | +#ifdef __cplusplus |
| 11 | +extern "C" { |
| 12 | +#endif |
| 13 | + |
| 14 | +/** Determine if the current platform supports hardware CRC for given polynomial |
| 15 | + * |
| 16 | + * The purpose of this function is to inform the CRC Platform API whether the |
| 17 | + * current platform has a hardware CRC module and that it can support the |
| 18 | + * requested polynomial. |
| 19 | + * |
| 20 | + * Supported polynomials are restricted to the named polynomials that can be |
| 21 | + * constructed in the MbedCRC class, POLY_8BIT_CCITT, POLY_7BIT_SD, |
| 22 | + * POLY_16BIT_CCITT, POLY_16BIT_IBM, POLY_32BIT_ANSI. |
| 23 | + * |
| 24 | + * The current platform must support the given polynomials default parameters |
| 25 | + * in order to return a true response, these include: reflect in, reflect out, |
| 26 | + * initial xor, and final xor. For example POLY_32BIT_ANSI requires an initial |
| 27 | + * and final xor of 0xFFFFFFFF, and reflection of both input and output. If any |
| 28 | + * of these settings cannot be configured the polynomial is not supported. |
| 29 | + * |
| 30 | + * \param polynomial CRC Polynomial. Example polynomial: 0x1021 = x^12+x^5+1 |
| 31 | + * |
| 32 | + * \return True if running if the polynomial is supported, false if not. |
| 33 | + */ |
| 34 | +bool hal_crc_is_supported(const uint32_t polynomial); |
| 35 | + |
| 36 | +/** Initialise the hardware CRC module with the given polynomial |
| 37 | + * |
| 38 | + * After calling this function the CRC HAL module will be ready to receive data |
| 39 | + * using the hal_crc_compute_partial() function. The CRC module on the board |
| 40 | + * will be configured internally with the specified configuration and be ready |
| 41 | + * to receive data. |
| 42 | + * |
| 43 | + * The platform will configure itself based on the default configuration |
| 44 | + * parameters of the input polynomial. |
| 45 | + * |
| 46 | + * This function must be called before calling hal_crc_compute_partial(). |
| 47 | + * |
| 48 | + * This function must be called with a valid polynomial supported by the |
| 49 | + * platform. The polynomial must be checked for support using the |
| 50 | + * hal_crc_is_supported() function. |
| 51 | + * |
| 52 | + * Calling hal_crc_compute_partial_start() multiple times without finalising the |
| 53 | + * CRC calculation with hal_crc_get_result() will override the current |
| 54 | + * configuration and state and the intermediate result of the computation will |
| 55 | + * be lost. |
| 56 | + * |
| 57 | + * This function is not thread safe, a CRC calculation must not be started from |
| 58 | + * two different threads or contexts at the same time, calling this function |
| 59 | + * from two different contexts may lead to configurations being overwrite and |
| 60 | + * results being lost. |
| 61 | + * |
| 62 | + * \param polynomial CRC Polynomial. Example polynomial: 0x1021 = x^12+x^5+1 |
| 63 | + */ |
| 64 | +void hal_crc_compute_partial_start(const uint32_t polynomial); |
| 65 | + |
| 66 | +/** Writes data to the current CRC module. |
| 67 | + * |
| 68 | + * Writes input data buffer bytes to the CRC data register. The CRC module |
| 69 | + * must interpret the data as an array of bytes. |
| 70 | + * |
| 71 | + * The final transformations are not applied to the data, the CRC module must |
| 72 | + * retain the intermediate result so that additional calls to this function |
| 73 | + * can be made, appending the additional data to the calculation. |
| 74 | + * |
| 75 | + * To obtain the final result of the CRC calculation hal_crc_get_result() is |
| 76 | + * called to apply the final transformations to the data. |
| 77 | + * |
| 78 | + * This function can be call multiple times in succession, this can be used |
| 79 | + * to calculate the CRC result of streamed data. |
| 80 | + * |
| 81 | + * This function is not thread safe. There is only one instance of the CRC |
| 82 | + * module active at a time, calling this function from multiple contexts will |
| 83 | + * append different data to the same, single instance of the module causing an |
| 84 | + * erroneous value to be calculated. |
| 85 | + * |
| 86 | + * \param data Input data stream to be written into the CRC calculation |
| 87 | + * \param size Size of the data stream in bytes |
| 88 | + */ |
| 89 | +void hal_crc_compute_partial(const uint8_t *data, const size_t size); |
| 90 | + |
| 91 | +/* Reads the checksum result from the CRC module. |
| 92 | + * |
| 93 | + * Reads the final checksum result for the final checksum value. The returned |
| 94 | + * value is cast as an unsigned 32-bit integer. The actual size of the returned |
| 95 | + * result depends on the polynomial used to configure the CRC module. |
| 96 | + * |
| 97 | + * Additional transformations that are used in the default configuration of the |
| 98 | + * input polynomial are applied to the result before it is returned from this |
| 99 | + * function, these transformations include: The final xor being appended to the |
| 100 | + * calculation, and the result being reflected if required. |
| 101 | + * |
| 102 | + * Calling this function multiple times is undefined. The first call to this |
| 103 | + * function will return the final result of the CRC calculation, the return |
| 104 | + * value on successive calls is undefined as the contents of the register after |
| 105 | + * accessing them is platform-specific. |
| 106 | + * |
| 107 | + * \return The final CRC checksum after the reflections and final calculations |
| 108 | + * have been applied. |
| 109 | + */ |
| 110 | +uint32_t hal_crc_get_result(void); |
| 111 | + |
| 112 | +#ifdef __cplusplus |
| 113 | +}; |
| 114 | +#endif |
| 115 | + |
| 116 | +#endif // DEVICE_CRC |
| 117 | +#endif // MBED_CRC_HAL_API_H |
0 commit comments