forked from jsnyder/stm32ld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
223 lines (197 loc) · 5.08 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Loader driver
#include "stm32ld.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
static FILE *fp;
static u32 fpsize;
#define BL_VERSION_MAJOR 2
#define BL_VERSION_MINOR 1
#define BL_MKVER( major, minor ) ( ( major ) * 256 + ( minor ) )
#define BL_MINVERSION BL_MKVER( BL_VERSION_MAJOR, BL_VERSION_MINOR )
// Add other chip IDs here? Is there any reason to actually check this?
#define CHIP_ID 0x0414
#define CHIP_ID_ALT 0x0413
// ****************************************************************************
// Helper functions and macros
// Get data function
static u32 writeh_read_data( u8 *dst, u32 len )
{
size_t readbytes = 0;
if( !feof( fp ) )
readbytes = fread( dst, 1, len, fp );
return ( u32 )readbytes;
}
// Progress function
static void writeh_progress( u32 wrote )
{
unsigned pwrite = ( wrote * 100 ) / fpsize;
// static int expected_next = 10;
// if( pwrite >= expected_next )
// {
// printf( "%d%% ", expected_next );
// expected_next += 10;
// }
static int expected_next = 2;
if( pwrite >= expected_next )
{
printf("=");
expected_next += 2;
}
}
// ****************************************************************************
// Entry point
int main( int argc, const char **argv )
{
u8 not_flashing=0;
u8 send_go_command=0;
u8 minor, major;
u16 version;
long baud;
// Argument validation
if( argc < 5 )
{
fprintf( stderr, "Usage: stm32ld <port> <baud> <entry> <binary image name|0 to not flash> [<0|1 to send Go>]\n" );
fprintf( stderr, " <entry> one of 'dtr_rts' (Mainboard v1) or 'mblc'\n" );
// fprintf( stderr, "Note: Thanks to Go command you don't need to change status of BOOT0 after flashing,\n\t\ttake care after power cycle ...\n\n\n" );
exit( 1 );
}
errno = 0;
baud = strtol( argv[ 2 ], NULL, 10 );
if( ( errno == ERANGE && ( baud == LONG_MAX || baud == LONG_MIN ) ) || ( errno != 0 && baud == 0 ) || ( baud < 0 ) )
{
fprintf( stderr, "Invalid baud '%s'\n", argv[ 2 ] );
exit( 1 );
}
if( argc >= 6 && strlen(argv[ 5 ])==1 && strncmp(argv[ 5 ], "1", 1)==0 )
{
send_go_command=1;
}
if( strlen(argv[ 4 ])==1 && strncmp(argv[ 4 ], "0", 1)==0 )
{
not_flashing=1;
}
else
{
if( ( fp = fopen( argv[ 4 ], "rb" ) ) == NULL )
{
fprintf( stderr, "Unable to open %s\n", argv[ 4 ] );
exit( 1 );
}
else
{
fseek( fp, 0, SEEK_END );
fpsize = ftell( fp );
fseek( fp, 0, SEEK_SET );
}
}
// new entry type
entry_type_t ent = MBLC;
if (strncmp(argv[3], "mblc", 4) == 0) {
ent = MBLC;
} else if (strncmp(argv[3], "dtr_rts", 7) == 0) {
ent = MAINBOARD_V1;
} else if (strncmp(argv[3], "rts_trpl_inv", 12) == 0) {
ent = MAINBOARD_V2;
} else {
fprintf(stderr, "Unrecognized entry type: %s\n", argv[3]);
exit( 1 );
}
// Connect to bootloader
if( stm32_init( argv[ 1 ], baud, ent ) != STM32_OK )
{
fprintf( stderr, "Unable to connect to bootloader\n" );
exit( 1 );
}
// Get version
if( stm32_get_version( &major, &minor ) != STM32_OK )
{
fprintf( stderr, "Unable to get bootloader version\n" );
exit( 1 );
}
else
{
printf( "Bootloader version: %d.%d; ", major, minor );
if( BL_MKVER( major, minor ) < BL_MINVERSION )
{
fprintf( stderr, "Unsupported bootloader version" );
exit( 1 );
}
}
// Get chip ID
if( stm32_get_chip_id( &version ) != STM32_OK )
{
fprintf( stderr, "Unable to get chip ID\n" );
exit( 1 );
}
else
{
printf( "chip ID: %04X\r\n", version );
// if( version != CHIP_ID && version != CHIP_ID_ALT )
// {
// fprintf( stderr, "Unsupported chip ID\n" );
// exit( 1 );
// }
}
fflush(stdout);
fflush(stderr);
if( not_flashing == 0 )
{
// Write unprotect
if( stm32_write_unprotect() != STM32_OK )
{
fprintf( stderr, "Unable to execute write unprotect\n" );
exit( 1 );
}
else {
// printf( "Cleared write protection\n" );
}
// Erase flash
if( major == 3 )
{
printf("Extended erase...\r\n");
fflush(stdout);
if( stm32_extended_erase_flash() != STM32_OK )
{
fprintf( stderr, "Unable to extended erase chip\n" );
exit( 1 );
}
}
else
{
if( stm32_erase_flash() != STM32_OK )
{
fprintf( stderr, "Unable to erase chip\n" );
exit( 1 );
}
else
printf( "Erased FLASH memory\n" );
}
// Program flash
setbuf( stdout, NULL );
printf( "Programming flash: ");
if( stm32_write_flash( writeh_read_data, writeh_progress ) != STM32_OK )
{
fprintf( stderr, "Unable to program FLASH memory.\n" );
exit( 1 );
}
else
printf( "\n" );
fclose( fp );
}
else
printf( "Skipping flashing\n" );
if( send_go_command == 1 )
{
// Run GO
// printf( "Sending go command\n" );
if( stm32_go_command( ) != STM32_OK )
{
fprintf( stderr, "Unable to run Go command.\n" );
exit( 1 );
}
}
return 0;
}