-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathdevice_config.c
179 lines (170 loc) · 9.29 KB
/
device_config.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
/*
* MIT License
*
* Copyright (c) 2020 Kirill Kotyagin
*/
#include <string.h>
#include <stm32f1xx.h>
#include <limits.h>
#include "device_config.h"
#define DEVICE_CONFIG_FLASH_SIZE 0x10000UL
#define DEVICE_CONFIG_NUM_PAGES 2
#define DEVICE_CONFIG_PAGE_SIZE 0x400UL
#define DEVICE_CONFIG_FLASH_END (FLASH_BASE + DEVICE_CONFIG_FLASH_SIZE)
#define DEVICE_CONFIG_BASE_ADDR ((void*)(DEVICE_CONFIG_FLASH_END - DEVICE_CONFIG_NUM_PAGES * DEVICE_CONFIG_PAGE_SIZE))
#define DEVICE_CONFIG_MAGIC 0xDECFDECFUL
static const device_config_t default_device_config = {
.status_led_pin = { .port = GPIOC, .pin = 13, .dir = gpio_dir_output, .speed = gpio_speed_low, .func = gpio_func_general, .output = gpio_output_od, .polarity = gpio_polarity_low },
.config_pin = { .port = GPIOB, .pin = 5, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low },
.cdc_config = {
.port_config = {
/* Port 0 */
{
.pins =
{
/* rx */ { .port = GPIOA, .pin = 10, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_high },
/* tx */ { .port = GPIOA, .pin = 9, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_alternate, .output = gpio_output_pp, .polarity = gpio_polarity_high },
/* rts */ { .port = GPIOA, .pin = 15, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_low},
/* cts */ { .port = 0 }, /* CTS pin is occupied by USB */
/* dsr */ { .port = GPIOB, .pin = 7, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low },
/* dtr */ { .port = GPIOA, .pin = 4, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_low },
/* dcd */ { .port = GPIOB, .pin = 15, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low },
/* ri */ { .port = GPIOB, .pin = 3, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low },
/* txa */ { .port = GPIOB, .pin = 0, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_high },
}
},
/* Port 1 */
{
.pins =
{
/* rx */ { .port = GPIOA, .pin = 3, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_high },
/* tx */ { .port = GPIOA, .pin = 2, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_alternate, .output = gpio_output_pp, .polarity = gpio_polarity_high },
/* rts */ { .port = GPIOA, .pin = 1, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_low},
/* cts */ { .port = GPIOA, .pin = 0, .dir = gpio_dir_input, .pull = gpio_pull_down, .polarity = gpio_polarity_low },
/* dsr */ { .port = GPIOB, .pin = 4, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low },
/* dtr */ { .port = GPIOA, .pin = 5, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_low },
/* dcd */ { .port = GPIOB, .pin = 8, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low },
/* ri */ { .port = GPIOB, .pin = 12, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low },
/* txa */ { .port = GPIOB, .pin = 1, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_high },
}
},
/* Port 2 */
{
.pins =
{
/* rx */ { .port = GPIOB, .pin = 11, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_high },
/* tx */ { .port = GPIOB, .pin = 10, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_alternate, .output = gpio_output_pp, .polarity = gpio_polarity_high },
/* rts */ { .port = GPIOB, .pin = 14, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_low },
/* cts */ { .port = GPIOB, .pin = 13, .dir = gpio_dir_input, .pull = gpio_pull_down, .polarity = gpio_polarity_low },
/* dsr */ { .port = GPIOB, .pin = 6, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low },
/* dtr */ { .port = GPIOA, .pin = 6, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_low },
/* dcd */ { .port = GPIOB, .pin = 9, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low },
/* ri */ { .port = GPIOA, .pin = 8, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low },
/* txa */ { .port = GPIOA, .pin = 7, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_high },
}
},
}
}
};
static device_config_t current_device_config;
static uint32_t device_config_calc_crc(const device_config_t *device_config) {
uint32_t *word_p = (uint32_t*)device_config;
size_t bytes_left = offsetof(device_config_t, crc);
CRC->CR |= CRC_CR_RESET;
while (bytes_left > sizeof(*word_p)) {
CRC->DR = *word_p++;
bytes_left -= sizeof(*word_p);
}
if (bytes_left) {
uint32_t shift = 0;
uint32_t tail = 0;
uint8_t *byte_p = (uint8_t*)word_p;
for (int i = 0; i < bytes_left; i++) {
tail |= (uint32_t)(*byte_p++) << (shift);
shift += CHAR_BIT;
}
CRC->DR = tail;
}
return CRC->DR;
}
const static device_config_t* device_config_get_stored() {
uint8_t *config_page = (uint8_t*)DEVICE_CONFIG_BASE_ADDR;
size_t config_pages = DEVICE_CONFIG_NUM_PAGES;
while (config_pages--) {
const device_config_t *stored_config = (device_config_t*)config_page;
if ((stored_config->magic == DEVICE_CONFIG_MAGIC) &&
(device_config_calc_crc(stored_config) == stored_config->crc)) {
return stored_config;
}
config_page += DEVICE_CONFIG_PAGE_SIZE;
}
return 0;
}
void device_config_init() {
RCC->AHBENR |= RCC_AHBENR_CRCEN;
const device_config_t *stored_config = device_config_get_stored();
if (stored_config == 0) {
stored_config = &default_device_config;
}
memcpy(¤t_device_config, stored_config, sizeof(*stored_config));
}
device_config_t *device_config_get() {
return ¤t_device_config;
}
void device_config_save() {
uint16_t *last_config_magic = 0;
uint8_t *config_page = (uint8_t*)DEVICE_CONFIG_BASE_ADDR;
size_t config_pages = DEVICE_CONFIG_NUM_PAGES;
uint16_t *src_word_p = (uint16_t*)¤t_device_config;
uint16_t *dst_word_p;
size_t bytes_left = sizeof(current_device_config);
while (config_pages-- && (last_config_magic == 0)) {
const device_config_t *stored_config = (device_config_t*)config_page;
if ((stored_config->magic == DEVICE_CONFIG_MAGIC) &&
(device_config_calc_crc(stored_config) == stored_config->crc)) {
last_config_magic = (uint16_t*)&stored_config->magic;
}
config_page += DEVICE_CONFIG_PAGE_SIZE;
}
if (config_page == ((uint8_t*)DEVICE_CONFIG_BASE_ADDR + (DEVICE_CONFIG_NUM_PAGES * DEVICE_CONFIG_PAGE_SIZE))) {
config_page = (uint8_t*)DEVICE_CONFIG_BASE_ADDR;
}
dst_word_p = (uint16_t*)config_page;
if (FLASH->CR & FLASH_CR_LOCK) {
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
}
while (FLASH->SR & FLASH_SR_BSY);
FLASH->SR = FLASH->SR & FLASH_SR_EOP;
FLASH->CR = FLASH_CR_PER;
FLASH->AR = (uint32_t)config_page;
FLASH->CR |= FLASH_CR_STRT;
while (!(FLASH->SR & FLASH_SR_EOP));
FLASH->SR = FLASH_SR_EOP;
FLASH->CR &= ~FLASH_CR_PER;
current_device_config.magic = DEVICE_CONFIG_MAGIC;
current_device_config.crc = device_config_calc_crc(¤t_device_config);
FLASH->CR |= FLASH_CR_PG;
while (bytes_left > 1) {
*dst_word_p++ = *src_word_p++;
while (!(FLASH->SR & FLASH_SR_EOP));
FLASH->SR = FLASH_SR_EOP;
bytes_left -= sizeof(*dst_word_p);
}
if (bytes_left) {
*dst_word_p = (uint16_t)(*(uint8_t*)src_word_p);
while (!(FLASH->SR & FLASH_SR_EOP));
FLASH->SR = FLASH_SR_EOP;
}
if (last_config_magic) {
*last_config_magic = 0x0000;
while (!(FLASH->SR & FLASH_SR_EOP));
FLASH->SR = FLASH_SR_EOP;
}
FLASH->CR &= ~(FLASH_CR_PG);
FLASH->CR |= FLASH_CR_LOCK;
}
void device_config_reset() {
memcpy(¤t_device_config, &default_device_config, sizeof(default_device_config));
device_config_save();
}