|
| 1 | +/* |
| 2 | + * Generic binary BCH encoding/decoding library |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it |
| 5 | + * under the terms of the GNU General Public License version 2 as published by |
| 6 | + * the Free Software Foundation. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | + * more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License along with |
| 14 | + * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | + * |
| 17 | + * Copyright © 2011 Parrot S.A. |
| 18 | + * |
| 19 | + * Author: Ivan Djelic <[email protected]> |
| 20 | + * |
| 21 | + * Description: |
| 22 | + * |
| 23 | + * This library provides runtime configurable encoding/decoding of binary |
| 24 | + * Bose-Chaudhuri-Hocquenghem (BCH) codes. |
| 25 | +*/ |
| 26 | +/* mbed Microcontroller Library |
| 27 | + * Copyright (c) 2022 ARM Limited |
| 28 | + * SPDX-License-Identifier: Apache-2.0 |
| 29 | + * |
| 30 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 31 | + * you may not use this file except in compliance with the License. |
| 32 | + * You may obtain a copy of the License at |
| 33 | + * |
| 34 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 35 | + * |
| 36 | + * Unless required by applicable law or agreed to in writing, software |
| 37 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 38 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 39 | + * See the License for the specific language governing permissions and |
| 40 | + * limitations under the License. |
| 41 | + */ |
| 42 | +#ifndef _BCH_H |
| 43 | +#define _BCH_H |
| 44 | +#ifdef __cplusplus |
| 45 | +extern "C" { |
| 46 | +#endif |
| 47 | +#include <stdint.h> |
| 48 | +//typedef unsigned char uint8_t; |
| 49 | +//typedef unsigned short uint16_t; |
| 50 | +//typedef unsigned int uint32_t; |
| 51 | + |
| 52 | +#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) |
| 53 | +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) |
| 54 | +/** |
| 55 | + * struct bch_control - BCH control structure |
| 56 | + * @m: Galois field order |
| 57 | + * @n: maximum codeword size in bits (= 2^m-1) |
| 58 | + * @t: error correction capability in bits |
| 59 | + * @ecc_bits: ecc exact size in bits, i.e. generator polynomial degree (<=m*t) |
| 60 | + * @ecc_bytes: ecc max size (m*t bits) in bytes |
| 61 | + * @a_pow_tab: Galois field GF(2^m) exponentiation lookup table |
| 62 | + * @a_log_tab: Galois field GF(2^m) log lookup table |
| 63 | + * @mod8_tab: remainder generator polynomial lookup tables |
| 64 | + * @ecc_buf: ecc parity words buffer |
| 65 | + * @ecc_buf2: ecc parity words buffer |
| 66 | + * @xi_tab: GF(2^m) base for solving degree 2 polynomial roots |
| 67 | + * @syn: syndrome buffer |
| 68 | + * @cache: log-based polynomial representation buffer |
| 69 | + * @elp: error locator polynomial |
| 70 | + * @poly_2t: temporary polynomials of degree 2t |
| 71 | + */ |
| 72 | +struct bch_control { |
| 73 | + unsigned int m; |
| 74 | + unsigned int n; |
| 75 | + unsigned int t; |
| 76 | + unsigned int ecc_bits; |
| 77 | + unsigned int ecc_bytes; |
| 78 | + /* private: */ |
| 79 | + uint16_t *a_pow_tab; |
| 80 | + uint16_t *a_log_tab; |
| 81 | + uint32_t *mod8_tab; |
| 82 | + uint32_t *ecc_buf; |
| 83 | + uint32_t *ecc_buf2; |
| 84 | + unsigned int *xi_tab; |
| 85 | + unsigned int *syn; |
| 86 | + int *cache; |
| 87 | + struct gf_poly *elp; |
| 88 | + struct gf_poly *poly_2t[4]; |
| 89 | +}; |
| 90 | + |
| 91 | +struct bch_control *init_bch(int m, int t, unsigned int prim_poly); |
| 92 | + |
| 93 | +void free_bch(struct bch_control *bch); |
| 94 | + |
| 95 | +void encode_bch(struct bch_control *bch, const uint8_t *data, |
| 96 | + unsigned int len, uint8_t *ecc); |
| 97 | + |
| 98 | +int decode_bch(struct bch_control *bch, const uint8_t *data, unsigned int len, |
| 99 | + const uint8_t *recv_ecc, const uint8_t *calc_ecc, |
| 100 | + const unsigned int *syn, unsigned int *errloc); |
| 101 | +int fls(int x); |
| 102 | +#ifdef _X86_ |
| 103 | +#define cpu_to_be32(x) ((((x)&0xff)<<24) | (((x)&0xff00)<<8) | (((x)>>8)&0xff00) | (((x)>>24)&0xff)) |
| 104 | +#else |
| 105 | +#define cpu_to_be32(x) x |
| 106 | +#endif |
| 107 | +#ifdef __cplusplus |
| 108 | +} |
| 109 | +#endif |
| 110 | +#endif /* _BCH_H */ |
0 commit comments