forked from rene-dev/stmbl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
104 lines (94 loc) · 2.87 KB
/
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
/*
* This file is part of the stmbl project.
*
* Copyright (C) 2013-2015 Rene Hopf <[email protected]>
* Copyright (C) 2013-2015 Nico Stute <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include "hal.h"
#include "commands.h"
#include "stm32f4xx_conf.h"
char config[15 * 1024];
const char *config_ro = (char *)0x08008000;
void confcrc(char *ptr) {
uint32_t len = strnlen(config, sizeof(config) - 1);
CRC_ResetDR();
uint32_t crc = CRC_CalcBlockCRC((uint32_t *)config, len / 4);
for(int i = 0; i < len; i++) {
printf("%x ", config[i]);
}
printf("\n");
printf("size: %lu words: %lu crc:%lx\n", len, len / 4, crc);
}
COMMAND("confcrc", confcrc, "foo");
void flashloadconf(char *ptr) {
strncpy(config, config_ro, sizeof(config));
}
COMMAND("flashloadconf", flashloadconf, "load config from flash");
void flashsaveconf(char *ptr) {
printf("erasing flash page...\n");
FLASH_Unlock();
if(FLASH_EraseSector(FLASH_Sector_2, VoltageRange_3) != FLASH_COMPLETE) {
printf("error!\n");
FLASH_Lock();
return;
}
printf("saving conf\n");
int i = 0;
int ret = 0;
do {
ret = FLASH_ProgramByte((uint32_t)(config_ro + i), config[i]) != FLASH_COMPLETE;
if(ret) {
printf("error writing %i\n", ret);
break;
}
} while(config[i++] != 0);
printf("OK %i bytes written\n", i);
FLASH_Lock();
}
COMMAND("flashsaveconf", flashsaveconf, "save config to flash");
void loadconf(char *ptr) {
hal_parse(config);
}
COMMAND("loadconf", loadconf, "parse config");
void showconf(char *ptr) {
printf("%s", config_ro);
}
COMMAND("showconf", showconf, "show config");
void appendconf(char *ptr) {
printf("adding %s\n", ptr);
strncat(config, ptr, sizeof(config) - 1);
strncat(config, "\n", sizeof(config) - 1);
}
COMMAND("appendconf", appendconf, "append string to config");
void deleteconf(char *ptr) {
config[0] = '\0';
}
COMMAND("deleteconf", deleteconf, "delete config");
void hardboot(char *ptr) {
printf("erasing flash page...\n");
FLASH_Unlock();
if(FLASH_EraseSector(FLASH_Sector_4, VoltageRange_3) != FLASH_COMPLETE) {
printf("error!\n");
FLASH_Lock();
return;
}
printf("OK, call bootloader\n");
FLASH_Lock();
NVIC_SystemReset();
}
COMMAND("hardboot", hardboot, "destroy firmware to force bootloader");