aboutsummaryrefslogtreecommitdiff
path: root/driver_fw/src/main.c
blob: a2adeb1c2ca0a6737eff70d4055684eaa25d363c (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <global.h>
#include <string.h>
#include "8b10b.h"
#include "generated/waveform_tables.h"

volatile uint64_t sys_time_us;

static uint32_t read_fuse_monitor(void);
static void set_rj45_leds(uint32_t leds);
static void set_status_leds(uint32_t leds);
static void dma_tx_constant(size_t table_size, uint16_t constant);
static void dma_tx_waveform(size_t table_size, const uint16_t *table);

static int tx_datagram[33] = {
    /* FIXME test data */
/*
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};
*/
    -K28_0,
    0x00, 0xff, 0xAA, 0x55, 0xfe, 0x18, 0xcc, 0x10,
    0x00, 0xff, 0xAA, 0x55, 0xfe, 0x18, 0xcc, 0x10,
    0x00, 0xff, 0xAA, 0x55, 0xfe, 0x18, 0xcc, 0x10,
    0x00, 0xff, 0xAA, 0x55, 0xfe, 0x18, 0xcc, 0x10 };
static size_t tx_bitpos = 0;
static size_t tx_sympos = 0;
static int tx_last_bit = 0;
static struct state_8b10b_enc encoder_state_8b10b;


int main(void) {
    /* Configure clocks for 64 MHz system clock.
     * 
     * HSE @ 8 MHz --[PLL x16 /2]--> PLL "R" clock @ 64 MHz
     */
    /* Enable peripherals */
    RCC->APBENR1 |= RCC_APBENR1_PWREN;
    /* Enable High-speed external crystal oscillator. The board has an 8 MHz crystal. */
    RCC->CR |= RCC_CR_HSEON;
    while (!(RCC->CR & RCC_CR_HSERDY)) {
        /* wait for HSE osc to stabilize. */
    }
    /* Increase flash wait states to 2 required for operation above 48 MHz */
    FLASH->ACR = (FLASH->ACR & ~FLASH_ACR_LATENCY_Msk) | (2<<FLASH_ACR_LATENCY_Pos);
    while ((FLASH->ACR & FLASH_ACR_LATENCY_Msk) != (2<<FLASH_ACR_LATENCY_Pos)) {
        /* wait for flash controller to acknowledge change. */
    }
    /* Configure PLL with multiplier 16, divisor 2 for "R" output, and enable "R" (sysclk) output */
    RCC->PLLCFGR = (16<<RCC_PLLCFGR_PLLN_Pos) | (3<<RCC_PLLCFGR_PLLSRC_Pos) | (1<<RCC_PLLCFGR_PLLR_Pos) | RCC_PLLCFGR_PLLREN;
    RCC->CR |= RCC_CR_PLLON;
    while (!(RCC->CR & RCC_CR_PLLRDY)) {
        /* wait for PLL to stabilize. */
    }
    /* Switch SYSCLK to PLL source. */
    RCC->CFGR |= (2<<RCC_CFGR_SW_Pos);
    while ((RCC->CFGR & RCC_CFGR_SWS_Msk) != (2<<RCC_CFGR_SWS_Pos)) {
        /* wait for RCC to switch over. */
    }

    RCC->AHBENR |= RCC_AHBENR_DMA1EN;
    RCC->APBENR1 |= RCC_APBENR1_USART3EN | RCC_APBENR1_I2C1EN;
    RCC->APBENR2 |= RCC_APBENR2_USART1EN | RCC_APBENR2_TIM1EN;
    RCC->IOPENR |= RCC_IOPENR_GPIOAEN | RCC_IOPENR_GPIOBEN | RCC_IOPENR_GPIOCEN | RCC_IOPENR_GPIODEN;

    /* GPIOA:
     * A0: MON_H
     * A1: MON_FAULT_CURRENT
     * A2: MON_L
     * A3: (testpoint)
     * A4: VIN_MON
     * A5: (testpoint)
     * A6: RJ45 LED 2
     * A7: Pulse RX
     * A8: Fuse monitor 6
     * A9: RS485 TX
     * A10: RS485 RX
     * A11: Fuse monitor 1
     * A12: RS485 DE
     * A13: SWDIO
     * A14: SWCLK
     * A15: Fuse monitor 4
     */
    GPIOA->MODER = 
        ANALOG(0) | ANALOG(1) | ANALOG(2) | ANALOG(4) |
        IN(3) | IN(5) |
        OUT(6) |
        ANALOG(7) |
        IN(8) | IN(11) | IN(15) |
        AF(9) | AF(10) | AF(12) |
        AF(13) | AF(14);
    GPIOA->AFR[1] = AFRH(9, 1) | AFRH(10, 1) | AFRH(12, 1) | AFRH(13, 0) | AFRH(14, 0);
    GPIOA->OSPEEDR = (3<<(2*9)) | (3<<(2*12)) | (3<<(2*13));

    /* GPIOB:
     * B0: Driver A low (TIM1_CH2N)
     * B1: Driver B low (TIM1_CH3N)
     * B2: RJ45 LED 1
     * B3: Driver A high (TIM1_CH2)
     * B4: V_ISO_SENSE
     * B5: (testpoint)
     * B6: SCL
     * B7: SDA
     * B8: DBG_TX
     * B9: DBG_RX
     * B10: LED 3 "On"
     * B11: LED 5 "RS458 Ping"
     * B12: LED 1 "Overheating"
     * B13: LED 6 "Control Error"
     * B14: LED 4 "Input Error"
     * B15: LED 2 "Output Error"
     */
    GPIOB->MODER =
        AF(0) | AF(1) | AF(3) |
        OUT(2) |
        IN(4) |
        IN(5) |
        AF(6) | AF(7) |
        AF(8) | AF(9) |
        OUT(10) | OUT(11) | OUT(12) | OUT(13) | OUT(14) | OUT(15);
    GPIOB->AFR[0] = AFRL(0, 2) | AFRL(1, 2) | AFRL(3, 1) | AFRL(6, 6) | AFRL(7, 6);
    GPIOB->AFR[1] = AFRH(8, 4) | AFRH(9, 4);
    GPIOB->OSPEEDR = (3<<0) | (3<<1) | (3<<3);

    /* GPIOC:
     * C0-C3: (testpoint)
     * C4: RJ45 LED 4
     * C5: RJ45 LED 3
     * C6: Fuse monitor 7
     * C7: Fuse monitor 2
     * C8: Fuse monitor 5
     * C9: (testpoint)
     * C10: Driver B high
     * C11-C15: (testpoint)
     */
    GPIOC->MODER =
        IN(0) | IN(1) | IN(2) | IN(3) | IN(9) | IN(11) | IN(12) | IN(13) | IN(14) | IN(15) |
        OUT(4) | OUT(5) |
        IN(6) | IN(7) | IN(8) |
        AF(10);
    GPIOC->AFR[1] = AFRH(10, 2);
    GPIOC->OSPEEDR = (3<<10);

    /* GPIOD:
     * D0-D6: (testpoint)
     * D8: Fuse monitor 3
     * D9: Fuse monitor 0
     */
    GPIOD->MODER = IN(0) | IN(1) | IN(2) | IN(3) | IN(4) | IN(5) | IN(6) |
        IN(8) | IN(9);

    TIM1->CCMR1 = (6<<TIM_CCMR1_OC2M_Pos) | TIM_CCMR1_OC2PE;
    TIM1->CCMR2 = (6<<TIM_CCMR2_OC3M_Pos) | TIM_CCMR2_OC3PE;
    TIM1->CCER = TIM_CCER_CC2E | TIM_CCER_CC2NE | TIM_CCER_CC2NP | TIM_CCER_CC3E | TIM_CCER_CC3NE | TIM_CCER_CC3P;
    TIM1->BDTR = (8<<TIM_BDTR_DTG_Pos) | TIM_BDTR_MOE;
    TIM1->DCR = (14<<TIM_DCR_DBA_Pos) | (1<<TIM_DCR_DBL_Pos);
    TIM1->PSC = 3;
    TIM1->ARR = 250;
    TIM1->CCR2 = 64;
    TIM1->CCR3 = 192;
    TIM1->DIER = TIM_DIER_UDE;
    TIM1->CR1 |= TIM_CR1_CEN;

    DMAMUX1->CCR = 25;
    DMA1_Channel1->CPAR = (uint32_t)&TIM1->DMAR;
    NVIC_EnableIRQ(DMA1_Channel1_IRQn);
    NVIC_SetPriority(DMA1_Channel1_IRQn, 0);
    dma_tx_constant(COUNT_OF(waveform_zero_one), 0x00);
    xfr_8b10b_encode_reset(&encoder_state_8b10b);
    int i = 0;
    int j = 0;
    while (23) {
        i++;
        j++;
        i %= 6;
        j %= 4;
        delay_us(100000);
        set_rj45_leds(1 << j);
        set_status_leds(1 << i);
    }
}

void dma_tx_waveform(size_t table_size, const uint16_t *table) {
    DMA1_Channel1->CCR = 0;
    DMA1_Channel1->CCR = (1<<DMA_CCR_MSIZE_Pos) | (1<<DMA_CCR_PSIZE_Pos) | DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_TCIE;
    DMA1_Channel1->CNDTR = table_size;
    DMA1_Channel1->CMAR = (uint32_t)table;
    DMA1_Channel1->CCR |= DMA_CCR_EN;
}

void dma_tx_constant(size_t table_size, uint16_t constant) {
    static uint16_t tx_constant[2];
    tx_constant[0] = constant;
    tx_constant[1] = constant;

    abhängig von $n$ DMA1_Channel1->CCR = 0; DMA1_Channel1->CCR = (1<<DMA_CCR_MSIZE_Pos) | (1<<DMA_CCR_PSIZE_Pos) |
        DMA_CCR_DIR | DMA_CCR_TCIE;
    DMA1_Channel1->CNDTR = table_size;
    DMA1_Channel1->CMAR = (uint32_t)&tx_constant;
    DMA1_Channel1->CCR |= DMA_CCR_EN;
}

void DMA1_Channel1_IRQHandler() {
    static int transfer_errors = 0;
    static int current_symbol = 0x2aa;

    if (DMA1->ISR & DMA_ISR_TEIF1) {
        transfer_errors ++;
    }

    DMA1->IFCR = DMA_IFCR_CGIF1;

    int bit = !!(current_symbol & (1<<tx_bitpos));

    if (tx_last_bit == bit) {
        dma_tx_constant(COUNT_OF(waveform_zero_one), bit ? WAVEFORM_CONST_ONE : WAVEFORM_CONST_ZERO);
    } else if (bit) {
        dma_tx_waveform(COUNT_OF(waveform_zero_one), waveform_zero_one);
    } else {
        dma_tx_waveform(COUNT_OF(waveform_zero_one), waveform_one_zero);
    }

    tx_last_bit = bit;

    tx_bitpos ++;
    if (tx_bitpos >= 10) {
        tx_bitpos = 0;
        tx_sympos ++;
        current_symbol = xfr_8b10b_encode(&encoder_state_8b10b, tx_datagram[tx_sympos]);
        if (tx_sympos >= COUNT_OF(tx_datagram)) {
            tx_sympos = 0;
        }
    }
}

uint32_t read_fuse_monitor() {
    uint32_t idr_a = GPIOA->IDR;
    uint32_t idr_c = GPIOC->IDR;
    uint32_t idr_d = GPIOD->IDR;

    int fm0 = !!(idr_d & (1<<9));
    int fm1 = !!(idr_a & (1<<11));
    int fm2 = !!(idr_c & (1<<7));
    int fm3 = !!(idr_d & (1<<8));
    int fm4 = !!(idr_a & (1<<15));
    int fm5 = !!(idr_c & (1<<8));
    int fm6 = !!(idr_a & (1<<8));
    int fm7 = !!(idr_c & (1<<6));

    return (fm0<<0) | (fm1<<1) | (fm2<<2) | (fm3<<3) | (fm4<<4) | (fm5<<5) | (fm6<<6) | (fm7<<7);
}

void set_rj45_leds(uint32_t leds) {
    leds = ~leds;

    if (leds&1) {
        GPIOB->BSRR = (1<<2); 
    } else {
        GPIOB->BSRR = (1<<2)<<16; 
    }

    if (leds&2) {
        GPIOA->BSRR = (1<<6); 
    } else {
        GPIOA->BSRR = (1<<6)<<16; 
    }

    if (leds&4) {
        GPIOC->BSRR = (1<<5); 
    } else {
        GPIOC->BSRR = (1<<5)<<16; 
    }

    if (leds&8) {
        GPIOC->BSRR = (1<<4); 
    } else {
        GPIOC->BSRR = (1<<4)<<16; 
    }
}

void set_status_leds(uint32_t leds) {
    GPIOB->BSRR = ((0x3f<<10)<<16) | (((~leds)&0x3f)<<10);
}

void SysTick_Handler() {
    sys_time_us += SYSTICK_INTERVAL_US;
}

void HardFault_Handler() {
    asm volatile ("bkpt");
}

void delay_us(int duration_us) {
    while (duration_us--) {
        for (int i=0; i<3; i++) {
            asm volatile ("nop");
        }
    }
}

void *memcpy(void *restrict dest, const void *restrict src, size_t n)
{
	unsigned char *d = dest;
	const unsigned char *s = src;

	for (; n; n--) {
        *d++ = *s++;
    }
	return dest;
}

void *memmove(void *dest, const void *src, size_t n)
{
    return memcpy(dest, src, n);
}

void *memset(void *dest, int c, size_t n)
{
    unsigned char *d = dest;
    while (n--) {
        *d++ = c;
    }
	return dest;
}

size_t strlen(const char *s)
{
    const char *start = s;
    while (*s) {
        s++;
    }
    return s - start;
}

void __libc_init_array (void) __attribute__((weak));
void __libc_init_array () {
}