|
25 | 25 | _PARITY = 0
|
26 | 26 | _STOP = 1
|
27 | 27 |
|
28 |
| -# led = Pin(48, Pin.OUT) |
29 |
| -# led_state = False |
| 28 | +readAddress = bytearray(STM32_ADDRESS) |
| 29 | +writeAddress = bytearray(STM32_ADDRESS) |
30 | 30 |
|
31 | 31 | uart = UART(_UART_ID, baudrate=_BAUDRATE, bits=_BITS, parity=_PARITY, stop=_STOP, tx=_TX_PIN,
|
32 | 32 | rx=_RX_PIN) # parity 0 equals to Even, 1 to Odd
|
@@ -132,20 +132,91 @@ def STM32_getVER() -> bytearray:
|
132 | 132 | return res
|
133 | 133 |
|
134 | 134 |
|
135 |
| -def STM32_writeMode(): |
136 |
| - lock = True |
137 |
| - while lock: |
138 |
| - STM32_sendCommand(STM32_WRITE) |
139 |
| - if STM32_waitOK() == 0: |
140 |
| - lock = False |
| 135 | +def _STM32_readMode() -> bytes: |
| 136 | + """ |
| 137 | + Enters read memory mode. Blocking |
| 138 | + :return: returns ACK or NACK |
| 139 | + """ |
| 140 | + STM32_sendCommand(STM32_READ) |
| 141 | + return _STM32_waitForAnswer() |
141 | 142 |
|
142 | 143 |
|
143 |
| -def STM32_address(address): |
144 |
| - check = 0x00 |
145 |
| - check = check ^ address[0] |
146 |
| - check = check ^ address[1] |
147 |
| - check = check ^ address[2] |
148 |
| - check = check ^ address[3] |
| 144 | +def _STM32_writeMode() -> bytes: |
| 145 | + """ |
| 146 | + Enters write memory mode. Blocking |
| 147 | + :return: returns ACK or NACK |
| 148 | + """ |
| 149 | + STM32_sendCommand(STM32_WRITE) |
| 150 | + return _STM32_waitForAnswer() |
| 151 | + |
| 152 | + |
| 153 | +def _STM32_sendAddress(address: bytes) -> bytes: |
| 154 | + """ |
| 155 | + Sends the start address of read/write operations. Blocking |
| 156 | + :param address: |
| 157 | + :return: |
| 158 | + """ |
| 159 | + assert len(address) == 4 |
149 | 160 |
|
| 161 | + checksum = address[0] ^ address[1] ^ address[2] ^ address[3] |
150 | 162 | uart.write(address)
|
151 |
| - uart.write(bytes([check])) |
| 163 | + uart.write(bytes([checksum])) |
| 164 | + |
| 165 | + return _STM32_waitForAnswer() |
| 166 | + |
| 167 | + |
| 168 | +def _incrementAddress(address: bytearray): |
| 169 | + """ |
| 170 | + Incremets address by one page (256 bytes) |
| 171 | + :param address: |
| 172 | + :return: |
| 173 | + """ |
| 174 | + |
| 175 | + address[2] = address[2] + 1 |
| 176 | + if address[2] == 0: |
| 177 | + address[1] = address[1] + 1 |
| 178 | + if address[1] == 0: |
| 179 | + address[0] = address[0] + 1 |
| 180 | + |
| 181 | + |
| 182 | +def _STM32_readPage() -> bytearray: |
| 183 | + """ |
| 184 | + Reads a 256 bytes data page from STM32. Returns a 256 bytearray. Blocking |
| 185 | + :return: page bytearray |
| 186 | + """ |
| 187 | + |
| 188 | + STM32_sendCommand(b'\xFF') |
| 189 | + res = _STM32_waitForAnswer() |
| 190 | + if res != STM32_ACK: |
| 191 | + print("READ PAGE: Cannot read STM32") |
| 192 | + return bytearray(0) |
| 193 | + out = bytearray(0) |
| 194 | + i = 0 |
| 195 | + while i < 256: |
| 196 | + b = uart.read(1) |
| 197 | + if b is None: |
| 198 | + continue |
| 199 | + out.append(b[0]) |
| 200 | + i = i+1 |
| 201 | + return out |
| 202 | + |
| 203 | + |
| 204 | +def _STM32_flashPage(data: bytearray) -> bytes: |
| 205 | + """ |
| 206 | + Sends a 256 bytes data page to STM32. Blocking |
| 207 | + :param data: |
| 208 | + :return: |
| 209 | + """ |
| 210 | + |
| 211 | + assert len(data) == 256 |
| 212 | + |
| 213 | + uart.write(b'\xff') # page length |
| 214 | + checksum = 0xff # starting checksum = page length |
| 215 | + |
| 216 | + for d in data: |
| 217 | + uart.write(d) |
| 218 | + checksum = checksum ^ d |
| 219 | + |
| 220 | + uart.write(bytes([checksum])) |
| 221 | + |
| 222 | + return _STM32_waitForAnswer() |
0 commit comments