-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.c
208 lines (185 loc) · 4.39 KB
/
main.c
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
#include <stdint.h>
#include "config.h"
#include "stm8.h"
#include "ram.h"
static uint8_t CRC;
static uint8_t ivt[128];
static uint8_t f_ram[128];
static uint8_t rx_buffer[BLOCK_SIZE];
static volatile uint8_t RAM_SEG_LEN;
static void (*flash_write_block)(uint16_t addr, const uint8_t *buf) =
(void (*)(uint16_t, const uint8_t *)) f_ram;
/**
* Write RAM_SEG section length into RAM_SEG_LEN
*/
inline void get_ram_section_length() {
__asm__("mov _RAM_SEG_LEN, #l_RAM_SEG");
}
/**
* Initialize watchdog:
* prescaler = 32, timeout = 63.70ms
*/
inline void iwdg_init() {
IWDG_KR = IWDG_KEY_ENABLE;
IWDG_KR = IWDG_KEY_ACCESS;
IWDG_PR = 2;
IWDG_KR = IWDG_KEY_REFRESH;
}
/**
* Kick the dog
*/
inline void iwdg_refresh() {
IWDG_KR = IWDG_KEY_REFRESH;
}
/**
* Initialize UART1 in 8N1 mode
*/
inline void uart_init() {
/* enable UART clock (STM8L only)*/
UART_CLK_ENABLE();
/* madness.. */
UART_BRR2 = ((UART_DIV >> 8) & 0xF0) + (UART_DIV & 0x0F);
UART_BRR1 = UART_DIV >> 4;
/* enable transmitter and receiver */
UART_CR2 = (1 << UART_CR2_TEN) | (1 << UART_CR2_REN);
}
/**
* Write byte into UART
*/
static void uart_write(uint8_t data) {
UART_DR = data;
while (!(UART_SR & (1 << UART_SR_TC)));
}
/**
* Read byte from UART and reset watchdog
*/
static uint8_t uart_read() {
iwdg_refresh();
while (!(UART_SR & (1 << UART_SR_RXNE)));
return UART_DR;
}
/**
* Calculate CRC-8-CCIT.
* Polynomial: x^8 + x^2 + x + 1 (0x07)
*
* @param data input byte
* @param crc initial CRC
* @return CRC value
*/
inline uint8_t crc8_update(uint8_t data, uint8_t crc) {
crc ^= data;
for (uint8_t i = 0; i < 8; i++)
crc = (crc & 0x80) ? (crc << 1) ^ 0x07 : crc << 1;
return crc;
}
/**
* Send ACK response
*/
static void serial_send_ack() {
uart_write(0xAA);
uart_write(0xBB);
}
/**
* Send NACK response (CRC mismatch)
*/
inline void serial_send_nack() {
uart_write(0xDE);
uart_write(0xAD);
}
/**
* Read BLOCK_SIZE bytes from UART
*
* @param dest destination buffer
*/
static void serial_read_block(uint8_t *dest) {
serial_send_ack();
for (uint8_t i = 0; i < BLOCK_SIZE; i++) {
uint8_t rx = uart_read();
dest[i] = rx;
CRC = crc8_update(rx, CRC);
}
}
/**
* Enter bootloader and perform firmware update
*/
inline void bootloader_exec() {
uint8_t chunks, crc_rx;
uint16_t addr = BOOT_ADDR;
/* enter bootloader */
for (;;) {
uint8_t rx = uart_read();
if (rx != 0xDE) continue;
rx = uart_read();
if (rx != 0xAD) continue;
rx = uart_read();
if (rx != 0xBE) continue;
rx = uart_read();
if (rx != 0xEF) continue;
chunks = uart_read();
crc_rx = uart_read();
rx = uart_read();
if (crc_rx != rx)
continue;
break;
}
#if !RELOCATE_IVT
/* get application interrupt table */
serial_read_block(ivt);
chunks--;
#if BLOCK_SIZE == 64
chunks--;
serial_read_block(ivt + BLOCK_SIZE);
#endif
#endif
/* unlock flash */
FLASH_PUKR = FLASH_PUKR_KEY1;
FLASH_PUKR = FLASH_PUKR_KEY2;
while (!(FLASH_IAPSR & (1 << FLASH_IAPSR_PUL)));
/* get main firmware */
for (uint8_t i = 0; i < chunks; i++) {
serial_read_block(rx_buffer);
flash_write_block(addr, rx_buffer);
addr += BLOCK_SIZE;
}
/* verify CRC */
if (CRC != crc_rx) {
serial_send_nack();
for (;;) ;
}
#if !RELOCATE_IVT
/* overwrite vector table preserving the reset interrupt */
*(uint32_t *) ivt = *(uint32_t *) (0x8000);
flash_write_block(0x8000, ivt);
#if BLOCK_SIZE == 64
flash_write_block(0x8000 + BLOCK_SIZE, ivt + BLOCK_SIZE);
#endif
#endif
/* lock flash */
FLASH_IAPSR &= ~(1 << FLASH_IAPSR_PUL);
serial_send_ack();
/* reboot */
for (;;) ;
}
/**
* Copy ram_flash_write_block routine into RAM
*/
inline void ram_cpy() {
get_ram_section_length();
for (uint8_t i = 0; i < RAM_SEG_LEN; i++)
f_ram[i] = ((uint8_t *) ram_flash_write_block)[i];
}
void bootloader_main() {
BOOT_PIN_CR1 = 1 << BOOT_PIN;
if (!(BOOT_PIN_IDR & (1 << BOOT_PIN))) {
/* execute bootloader */
CLK_CKDIVR = 0;
ram_cpy();
iwdg_init();
uart_init();
bootloader_exec();
} else {
/* jump to application */
BOOT_PIN_CR1 = 0x00;
BOOT();
}
}