Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[2b058a]: stm8.c Maximize Restore History

Download this file

stm8.c    338 lines (271 with data), 8.1 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
stm8flash - Open Source ST STM8 flash program for *nix
Copyright (C) 2010 Geoffrey McRae <geoff@spacevs.com>
adapted for STM8 - by Georg Ottinger <g.ottinger@gmx.at>
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 2
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "stm8.h"
#include "utils.h"
#include "e_w_routines.h"
#define STM8_ACK 0x79
#define STM8_NACK 0x1F
#define STM8_BUSY 0xAA
#define STM8_CMD_INIT 0x7F
#define STM8_CMD_GET 0x00 /* get the version and command supported */
struct stm8_cmd {
uint8_t get;
uint8_t rm;
uint8_t go;
uint8_t wm;
uint8_t er; /* this may be extended erase */
};
/* device table */
const stm8_dev_t devices[] = {
{0x012, "Medium density STM8S 32kB", 0x000000, 0x0007FF, 0x008000, 0x00FFFF, 1, 512, 0x004800, 0x00487F, 0x004000, 0x0043FF},
{0x0}
};
/* internal functions */
uint8_t stm8_gen_cs(const uint32_t v);
void stm8_send_byte(const stm8_t *stm, uint8_t byte);
uint8_t stm8_read_byte(const stm8_t *stm);
char stm8_send_command(const stm8_t *stm, const uint8_t cmd);
/* stm8 programs */
extern unsigned int stmreset_length;
extern unsigned char stmreset_binary[];
uint8_t stm8_gen_cs(const uint32_t v) {
return ((v & 0xFF000000) >> 24) ^
((v & 0x00FF0000) >> 16) ^
((v & 0x0000FF00) >> 8) ^
((v & 0x000000FF) >> 0);
}
void stm8_send_byte(const stm8_t *stm, uint8_t byte) {
serial_err_t err;
err = serial_write(stm->serial, &byte, 1);
if (err != SERIAL_ERR_OK) {
perror("send_byte");
assert(0);
}
}
uint8_t stm8_read_byte(const stm8_t *stm) {
uint8_t byte;
serial_err_t err;
err = serial_read(stm->serial, &byte, 1);
if (err != SERIAL_ERR_OK) {
perror("read_byte");
assert(0);
}
//REPLY-MODE
stm8_send_byte(stm, byte);
return byte;
}
char stm8_send_command(const stm8_t *stm, const uint8_t cmd) {
stm8_send_byte(stm, cmd);
stm8_send_byte(stm, cmd ^ 0xFF);
if (stm8_read_byte(stm) != STM8_ACK) {
fprintf(stderr, "Error sending command 0x%02x to device\n", cmd);
return 0;
}
return 1;
}
uint8_t *stm8_get_e_w_routine(int *len, char bl_version)
{
int i;
for(i = 0; i < E_W_ROUTINES_NUM ;i++)
{
if(e_w_routines[i].bl_version == bl_version)
{
*len = *(e_w_routines[i].bytes);
return e_w_routines[i].data;
}
}
return NULL;
}
stm8_t* stm8_init(const serial_t *serial, const char init) {
uint8_t len;
stm8_t *stm;
int routine_len;
int routine_offset;
uint8_t *routine_data;
stm = calloc(sizeof(stm8_t), 1);
stm->cmd = calloc(sizeof(stm8_cmd_t), 1);
stm->serial = serial;
if (init) {
stm8_send_byte(stm, STM8_CMD_INIT);
if (stm8_read_byte(stm) != STM8_ACK) {
stm8_close(stm);
fprintf(stderr, "Failed to get init ACK from device\n");
return NULL;
}
}
/* get the bootloader information */
if (!stm8_send_command(stm, STM8_CMD_GET)) return 0;
len = stm8_read_byte(stm) + 1;
stm->bl_version = stm8_read_byte(stm); --len;
stm->cmd->get = stm8_read_byte(stm); --len;
stm->cmd->rm = stm8_read_byte(stm); --len;
stm->cmd->go = stm8_read_byte(stm); --len;
stm->cmd->wm = stm8_read_byte(stm); --len;
stm->cmd->er = stm8_read_byte(stm); --len;
if (len > 0) {
fprintf(stderr, "Seems this bootloader returns more then we understand in the GET command, we will skip the unknown bytes\n");
while(len-- > 0) stm8_read_byte(stm);
}
if (stm8_read_byte(stm) != STM8_ACK) {
stm8_close(stm);
return NULL;
}
//FIX ME: Points to first device in List
stm->dev = devices;
routine_data = stm8_get_e_w_routine(&routine_len,stm->bl_version);
if(!routine_data)
{
fprintf(stderr, "Erase and Write Routines for Bootloader-Version not found!\n");
return NULL;
}
routine_offset = 0x0;
while(routine_len)
{
if(routine_len > 128)
{
if(!stm8_write_memory(stm, 0xA0 + routine_offset,&e_w_routine_32k_1_2_data[routine_offset],128))
return 0;
routine_len-=128;
routine_offset+=128;
} else {
if(!stm8_write_memory(stm, 0xA0 + routine_offset,&e_w_routine_32k_1_2_data[routine_offset],routine_len))
return 0;
routine_len=0;
}
}
return stm;
}
void stm8_close(stm8_t *stm) {
if (stm) free(stm->cmd);
free(stm);
}
char stm8_read_memory(const stm8_t *stm, uint32_t address, uint8_t data[], unsigned int len) {
uint8_t cs;
unsigned int i;
assert(len > 0 && len < 257);
/* must be 32bit aligned */
//assert(address % 4 == 0);
address = be_u32 (address);
cs = stm8_gen_cs(address);
if (!stm8_send_command(stm, stm->cmd->rm)) return 0;
assert(serial_write(stm->serial, &address, 4) == SERIAL_ERR_OK);
stm8_send_byte(stm, cs);
if (stm8_read_byte(stm) != STM8_ACK) return 0;
i = len - 1;
stm8_send_byte(stm, i);
stm8_send_byte(stm, i ^ 0xFF);
if (stm8_read_byte(stm) != STM8_ACK) return 0;
assert(serial_read(stm->serial, data, len) == SERIAL_ERR_OK);
return 1;
}
char stm8_write_memory(const stm8_t *stm, uint32_t address, uint8_t data[], unsigned int len) {
uint8_t cs;
unsigned int i;
// int c;
// int extra;
char ack;
assert(len > 0 && len < 129);
// /* must be 32bit aligned */
// assert(address % 4 == 0);
address = be_u32 (address);
cs = stm8_gen_cs(address);
/* send the address and checksum */
if (!stm8_send_command(stm, stm->cmd->wm)) return 0;
assert(serial_write(stm->serial, &address, 4) == SERIAL_ERR_OK);
stm8_send_byte(stm, cs);
do {
ack = stm8_read_byte(stm);
} while (ack == STM8_BUSY);
if (ack != STM8_ACK) return 0;
/* setup the cs and send the length */
// extra = len % 4;
// cs = len - 1 + extra;
cs = len - 1;
stm8_send_byte(stm, cs);
/* write the data and build the checksum */
for(i = 0; i < len; ++i)
cs ^= data[i];
assert(serial_write(stm->serial, data, len) == SERIAL_ERR_OK);
/* write the alignment padding */
/* for(c = 0; c < extra; ++c) {
stm8_send_byte(stm, 0xFF);
cs ^= 0xFF;
}
*/
/* send the checksum */
stm8_send_byte(stm, cs);
do {
ack = stm8_read_byte(stm);
} while (ack == STM8_BUSY);
return ack == STM8_ACK;
}
char stm8_erase_memory(const stm8_t *stm, uint8_t pages) {
char ack;
if (!stm8_send_command(stm, stm->cmd->er)) return 0;
if (pages == 0xFF) {
return stm8_send_command(stm, 0xFF);
} else {
uint8_t cs = 0;
uint8_t pg_num;
stm8_send_byte(stm, pages);
cs ^= pages;
for (pg_num = 0; pg_num <= pages; pg_num++) {
stm8_send_byte(stm, pg_num);
cs ^= pg_num;
}
stm8_send_byte(stm, cs);
do {
ack = stm8_read_byte(stm);
} while (ack == STM8_BUSY);
return ack == STM8_ACK;
}
}
char stm8_go(const stm8_t *stm, uint32_t address) {
uint8_t cs;
address = be_u32 (address);
cs = stm8_gen_cs(address);
if (!stm8_send_command(stm, stm->cmd->go)) return 0;
serial_write(stm->serial, &address, 4);
serial_write(stm->serial, &cs , 1);
return stm8_read_byte(stm) == STM8_ACK;
}
char stm8_reset_device(const stm8_t *stm) {
/*
since the bootloader does not have a reset command, we
upload the stmreset program into ram and run it, which
resets the device for us
*/
//For now we stick with DTR Reset
/*
uint32_t length = stmreset_length;
unsigned char* pos = stmreset_binary;
uint32_t address = stm->dev->ram_start;
while(length > 0) {
uint32_t w = length > 256 ? 256 : length;
if (!stm8_write_memory(stm, address, pos, w))
return 0;
address += w;
pos += w;
length -= w;
}
return stm8_go(stm, stm->dev->ram_start);
*/
return 1;
}