lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
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
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include <stdint.h>
 
#include "stm32_bl.h"
 
/*
 * checksum a sequence of bytes.
 * length == 1 invert the byte
 * length > 1 xor all bytes
 */
uint8_t checksum(__attribute__((unused)) handle_t *handle, uint8_t *bytes, int length)
{
    int i;
    uint8_t csum;
 
    if (length == 1) {
        csum = ~bytes[0];
    } else if (length > 1) {
        for (csum=0,i=0; i<length; i++)
            csum ^= bytes[i];
    } else {
        csum = 0xFF;
    }
 
    return csum;
}
 
static uint8_t write_len(handle_t *handle, int len)
{
    uint8_t buffer[sizeof(uint8_t)+1];
 
    buffer[0] = len-1;
 
    return handle->write_data(handle, buffer, sizeof(uint8_t));
}
 
static uint8_t write_cnt(handle_t *handle, uint16_t cnt)
{
    uint8_t buffer[sizeof(uint16_t)+1];
 
    buffer[0] = (cnt >> 8) & 0xFF;
    buffer[1] = (cnt     ) & 0xFF;
 
    return handle->write_data(handle, buffer, sizeof(uint16_t));
}
 
static uint8_t write_addr(handle_t *handle, uint32_t addr)
{
    uint8_t buffer[sizeof(uint32_t)+1];
 
    buffer[0] = (addr >> 24) & 0xFF;
    buffer[1] = (addr >> 16) & 0xFF;
    buffer[2] = (addr >>  8) & 0xFF;
    buffer[3] = (addr      ) & 0xFF;
 
    return handle->write_data(handle, buffer, sizeof(uint32_t));
}
 
/* write length followed by the data */
static uint8_t write_len_data(handle_t *handle, int len, uint8_t *data)
{
    uint8_t buffer[sizeof(uint8_t)+256+sizeof(uint8_t)];
    int i;
 
    buffer[0] = len-1;
 
    for (i=0; i<len; i++)
        buffer[1+i] = data[i];
 
    return handle->write_data(handle, buffer, sizeof(uint8_t)+len);
}
 
/* keep checking for ack until we receive a ack or nack */
static uint8_t read_ack_loop(handle_t *handle)
{
    uint8_t ret;
 
    do {
        ret = handle->read_ack(handle);
    } while (ret != CMD_ACK && ret != CMD_NACK);
 
    return ret;
}
 
/* erase a single sector */
uint8_t erase_sector(handle_t *handle, uint16_t sector)
{
    uint8_t buffer[sizeof(uint16_t)+sizeof(uint16_t)+1];
    uint8_t ret;
 
    handle->write_cmd(handle, handle->cmd_erase);
    ret = handle->read_ack(handle);
    if (ret != CMD_ACK)
        return ret;
 
    if (sector >= 0xFFF0) {
        /* special erase */
        write_cnt(handle, sector);
    } else if (handle->no_extra_sync) {
        /* sector erase without extra sync (UART case) */
        buffer[0] = 0;  /* MSB num of sectors - 1 */
        buffer[1] = 0;  /* LSB num of sectors - 1 */
        buffer[2] = (sector >> 8) & 0xFF;
        buffer[3] = (sector     ) & 0xFF;
        handle->write_data(handle, buffer, sizeof(uint16_t)+sizeof(uint16_t));
    } else {
        /* sector erase */
        write_cnt(handle, 0x0000);
        ret = read_ack_loop(handle);
        if (ret != CMD_ACK)
            return ret;
        write_cnt(handle, sector);
    }
 
    return read_ack_loop(handle);
}
 
/* read memory - this will chop the request into 256 byte reads */
uint8_t read_memory(handle_t *handle, uint32_t addr, uint32_t length, uint8_t *buffer)
{
    uint8_t ret = CMD_ACK;
    uint32_t offset = 0;
 
    while (ret == CMD_ACK && length > offset) {
        handle->write_cmd(handle, handle->cmd_read_memory);
        ret = handle->read_ack(handle);
        if (ret == CMD_ACK) {
            write_addr(handle, addr+offset);
            ret = read_ack_loop(handle);
            if (ret == CMD_ACK) {
                if (length-offset >= 256) {
                    write_len(handle, 256);
                    ret = read_ack_loop(handle);
                    if (ret == CMD_ACK) {
                        handle->read_data(handle, &buffer[offset], 256);
                        offset += 256;
                    }
                } else {
                    write_len(handle, length-offset);
                    ret = read_ack_loop(handle);
                    if (ret == CMD_ACK) {
                        handle->read_data(handle, &buffer[offset], length - offset);
                        offset = length;
                    }
                }
            }
        }
    }
 
    return ret;
}
 
/* write memory - this will chop the request into 256 byte writes */
uint8_t write_memory(handle_t *handle, uint32_t addr, uint32_t length, uint8_t *buffer)
{
    uint8_t ret = CMD_ACK;
    uint32_t offset = 0;
 
    while (ret == CMD_ACK && length > offset) {
        handle->write_cmd(handle, handle->cmd_write_memory);
        ret = handle->read_ack(handle);
        if (ret == CMD_ACK) {
            write_addr(handle, addr+offset);
            ret = read_ack_loop(handle);
            if (ret == CMD_ACK) {
                if (length-offset >= 256) {
                    write_len_data(handle, 256, &buffer[offset]);
                    offset += 256;
                } else {
                    write_len_data(handle, length-offset, &buffer[offset]);
                    offset = length;
                }
                ret = read_ack_loop(handle);
            }
        }
    }
 
    return ret;
}