Skip to content

Commit bbc639c

Browse files
committed
feat: read/write modes, sendAddress, incrementAddress, readPage, flashPage(to be checked)
utils: read/write pointers examples: read_mem.py
1 parent 324b4f5 commit bbc639c

File tree

3 files changed

+108
-15
lines changed

3 files changed

+108
-15
lines changed

examples/check_stm32.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from sys import exit
12
from stm32_flash import *
23

34
ans = STM32_startCommunication()

examples/read_mem.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from sys import exit
2+
from stm32_flash import *
3+
from stm32_flash import _STM32_readMode, _STM32_sendAddress, _STM32_readPage
4+
5+
ans = STM32_startCommunication()
6+
if ans == STM32_NACK:
7+
print("Cannot etablish connection with STM32")
8+
exit(-1)
9+
10+
print('\nSTM32 FOUND')
11+
12+
print('\nREADING MEMORY')
13+
14+
_STM32_readMode()
15+
print('\nREAD MODE')
16+
17+
_STM32_sendAddress(STM32_ADDRESS)
18+
print('\nADDRESS SENT')
19+
20+
page = _STM32_readPage()
21+
print(page)

stm32_flash.py

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
_PARITY = 0
2626
_STOP = 1
2727

28-
# led = Pin(48, Pin.OUT)
29-
# led_state = False
28+
readAddress = bytearray(STM32_ADDRESS)
29+
writeAddress = bytearray(STM32_ADDRESS)
3030

3131
uart = UART(_UART_ID, baudrate=_BAUDRATE, bits=_BITS, parity=_PARITY, stop=_STOP, tx=_TX_PIN,
3232
rx=_RX_PIN) # parity 0 equals to Even, 1 to Odd
@@ -132,20 +132,91 @@ def STM32_getVER() -> bytearray:
132132
return res
133133

134134

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()
141142

142143

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
149160

161+
checksum = address[0] ^ address[1] ^ address[2] ^ address[3]
150162
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

Comments
 (0)