forked from chwdt/vanmoof-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrec.c
More file actions
121 lines (104 loc) · 2.33 KB
/
Copy pathsrec.c
File metadata and controls
121 lines (104 loc) · 2.33 KB
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
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef uint32_t size_t;
#define NULL ((void *)0)
#define FLASH_START 0x08000000
#define FLASH_SIZE 0x00180000
#define UART7_START 0x40007800
#define WWDG_START 0x40002c00
typedef struct {
volatile uint32_t CR;
} WWDG_t;
typedef struct {
volatile uint32_t SR;
volatile uint32_t DR;
volatile uint32_t BRR;
volatile uint32_t CR1;
} UART_t;
static int send_srec(uint8_t type, uint32_t addr, size_t addr_len, const uint8_t *data, size_t len);
void dump_flash(void)
{
UART_t *UART7 = (void *)UART7_START;
uint8_t *FLASH = (void *)FLASH_START;
uint32_t head = 0x48445200;
size_t count = 0;
size_t i, j;
uint32_t cr1 = UART7->CR1;
UART7->CR1 = cr1 & ~(0x1f0);
send_srec(0, 0, 2, (uint8_t *)&head, 4);
for (i = 0; i < FLASH_SIZE; i += 32) {
send_srec(3, FLASH_START + i, 4, FLASH + i, 32);
count++;
}
send_srec(5, count, 2, NULL, 0);
send_srec(7, FLASH_START, 4, NULL, 0);
while (!(UART7->SR & 0x40))
/* wait */;
UART7->CR1 = cr1;
}
static uint8_t byte_to_rec(char* buffer, uint8_t byte)
{
uint8_t nib = (byte >> 4) & 0x0f;
if (nib < 10)
buffer[0] = '0' + nib;
else
buffer[0] = 'A' + (nib - 10);
nib = byte & 0x0f;
if (nib < 10)
buffer[1] = '0' + nib;
else
buffer[1] = 'A' + (nib - 10);
return byte;
}
static void wdg(void)
{
WWDG_t *WWDG = (void *)WWDG_START;
WWDG->CR = 0x7f;
}
static void uart_send(const char* data, size_t len)
{
UART_t *UART7 = (void *)UART7_START;
size_t i;
for (i = 0; i < len; i++) {
while (!(UART7->SR & 0x80))
/* wait */;
UART7->DR = data[i];
}
wdg();
}
static int send_srec(uint8_t type, uint32_t addr, size_t addr_len, const uint8_t *data, size_t len)
{
char buffer[2 + 2 + 8 + 64 + 2 + 2];
char* p = buffer + 2;
uint8_t total;
uint8_t sum = 0;
size_t i;
buffer[0] = 'S';
buffer[1] = '0' + type;
total = addr_len + len + 1;
sum += byte_to_rec(p, total);
p += 2;
switch (addr_len) {
case 4:
sum += byte_to_rec(p, (addr >> 24) & 0xff);
p += 2;
case 3:
sum += byte_to_rec(p, (addr >> 16) & 0xff);
p += 2;
default:
sum += byte_to_rec(p, (addr >> 8) & 0xff);
p += 2;
sum += byte_to_rec(p, addr & 0xff);
p += 2;
}
for (i = 0; i < len; i++) {
sum += byte_to_rec(p, data[i]);
p += 2;
}
sum ^= 0xff;
byte_to_rec(p, sum);
p += 2;
*p++ = '\r';
*p++ = '\n';
uart_send(buffer, p - buffer);
}