altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / stmf0 / ao_crc_stm.c
1 /*
2  * Copyright © 2015 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_crc.h>
21
22 #ifndef AO_CRC_WIDTH
23 #error "Must define AO_CRC_WIDTH"
24 #endif
25
26 /* Only the STM32F07x and ST32F09x series have
27  * programmable CRC units. Others can only do the ANSI CRC-32 computation
28  */
29
30 #if !AO_HAVE_PROGRAMMABLE_CRC_UNIT && AO_CRC_WIDTH != 32
31 #error "Target hardware does not have programmable CRC unit"
32 #endif
33
34 #ifndef AO_CRC_POLY
35 #if AO_CRC_WIDTH == 16
36 #define AO_CRC_POLY     AO_CRC_16_DEFAULT
37 #endif
38 #if AO_CRC_WIDTH == 32
39 #define AO_CRC_POLY     AO_CRC_32_DEFAULT
40 #endif
41 #endif
42
43 #if !AO_HAVE_PROGRAMMABLE_CRC_UNIT && (AO_CRC_WIDTH != 32 || AO_CRC_POLY != AO_CRC_32_ANSI)
44 #error "Target hardware does not have programmable CRC unit"
45 #endif
46
47 #if AO_CRC_WIDTH == 32
48 #define AO_CRC_CR_POLYSIZE      STM_CRC_CR_POLYSIZE_32
49 #endif
50
51 #if AO_CRC_WIDTH == 16
52 #define AO_CRC_CR_POLYSIZE      STM_CRC_CR_POLYSIZE_16
53 #endif
54
55 #if AO_CRC_WIDTH == 8
56 #define AO_CRC_CR_POLYSIZE      STM_CRC_CR_POLYSIZE_8
57 #endif
58
59 #if AO_CRC_WIDTH == 7
60 #define AO_CRC_CR_POLYSIZE      STM_CRC_CR_POLYSIZE_7
61 #endif
62
63 #ifndef AO_CRC_INIT
64 #define AO_CRC_INIT     0xffffffff
65 #endif
66
67 void
68 ao_crc_reset(void)
69 {
70         stm_crc.cr |= (1 << STM_CRC_CR_RESET);
71         while ((stm_crc.cr & (1 << STM_CRC_CR_RESET)) != 0)
72                 ;
73 }
74
75 void
76 ao_crc_init(void)
77 {
78         /* Turn on the CRC clock */
79         stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_CRCEN);
80
81         /* Need to initialize CR even on non-programmable hardware,
82          * the write to the POLYSIZE bits will be ignored in that
83          * case
84          */
85         stm_crc.cr = (AO_CRC_CR_POLYSIZE << STM_CRC_CR_POLYSIZE);
86         stm_crc.init = AO_CRC_INIT;
87 #if AO_HAVE_PROGRAMMABLE_CRC_UNIT
88         stm_crc.pol = AO_CRC_POLY;
89 #endif
90         ao_crc_reset();
91 }