-
Notifications
You must be signed in to change notification settings - Fork 0
Canup bootloading changes #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
shayana18
wants to merge
8
commits into
master
Choose a base branch
from
canup-bootloading-changes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
26dd5b6
initial run
shayana18 2d0e16a
In progress changes
shayana18 7a20986
more changes. Transmission working, however if a packet is dropped ev…
shayana18 14c9c61
NAK implemented, needs to be tested
shayana18 c61e161
interm progress
shayana18 1eb9a0b
ready to test
shayana18 b8a9dd8
Merge branch 'master' into canup-bootloading-changes
shayana18 31588c3
clean up ... a bit
shayana18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,9 @@ | |
| PROGRAM_CAN_ID = 1001 | ||
| VERIFY_CAN_ID = 1002 | ||
|
|
||
| NAK_ID = 0 | ||
| NAK_TIMEOUT = 0.1 | ||
|
|
||
| # CAN reply message IDs. | ||
| ERASE_SECTOR_COMPLETE_CAN_ID = 1010 | ||
| APP_VALIDITY_CAN_ID = 1011 | ||
|
|
@@ -34,6 +37,10 @@ | |
| # The minimum amount of data the microcontroller can program at a time. | ||
| MIN_PROG_SIZE_BYTES = 32 | ||
|
|
||
| ALL_PACKETS_VALID = 0xFFFFFFFFFFFFFFFF | ||
| WINDOW_SIZE = 64 | ||
|
|
||
| BOARD_10HZ_STATUS_ID = [1309, 1209, 1249, 1229, 1239, 1219] | ||
|
|
||
| class Bootloader: | ||
| def __init__( | ||
|
|
@@ -49,6 +56,8 @@ def __init__( | |
| self.board = board | ||
| self.timeout = timeout | ||
| self.ui_callback = ui_callback | ||
| self.dropped_packets = set() | ||
| self.__transmission_address = 0 | ||
|
|
||
| def start_update(self) -> bool: | ||
| """ | ||
|
|
@@ -123,21 +132,46 @@ def program(self) -> None: | |
| Program the binary into flash. There is no CAN handshake here to reduce | ||
| latency during programming. Also, the bootloader will verify the app's code is valid | ||
| by computing a checksum. | ||
|
|
||
| """ | ||
| for i, address in enumerate( | ||
|
|
||
| # updated TCP inspired can bootloading: sender will send packet with can id's representing their memory address. The reciever will expect a can ID, of the sender can ID and reciever can ID match all is good, if there is a miss match the board will transmit the excepted can ID back. | ||
| # if the expected Can ID is a multiple of 4 it will directly be sent back to the user to begin retransmission at that address, if it is not an interval of 4 then retransmision is restarted at the closests (smaller) interval of 4. Note that a nuiassnace that we have to figure out is that | ||
| # if we do want to move back to the closest interval of 4 then we need to figure out how to reflash adddresses that were already flashed (ie if its address 247 and the closest interval of 4 is 244) then we will attempt to reflash 244-246 even though they have already been flashed. Now with the | ||
| # stm32s to write to flash memory we need to first erase it. Now if that is the case we need to figure out how to erase as the stm32 can only erase sectors at a time which is 16kb.... I am unsure on what happens if we try to flash memory that has not been erased. If it retains its originally value that that would be best and we can jsut do redundant flashing until we | ||
| # get to the address that actually needs to be reflashed. | ||
|
|
||
| def _validator(msg: can.Message): | ||
| # Ignore all known status messages | ||
| print(f"Can ID that come through {msg.arbitration_id}\n") | ||
| if msg.arbitration_id == NAK_ID: | ||
| self.__transmission_address = int.from_bytes(msg.data, "little") | ||
| return False | ||
| else: | ||
| return True | ||
|
|
||
| for i, self.__transmission_address in enumerate( | ||
|
||
| range(self.ih.minaddr(), self.ih.minaddr() + self.size_bytes(), 8) | ||
| ): | ||
| if self.ui_callback and i % 128 == 0: | ||
| self.ui_callback("Programming data", self.size_bytes(), i * 8) | ||
| data = [self.ih[self.__transmission_address + i] for i in range(0, 8)] | ||
|
|
||
| data = [self.ih[address + i] for i in range(0, 8)] | ||
| self.bus.send( | ||
| can.Message( | ||
| arbitration_id=PROGRAM_CAN_ID, data=data, is_extended_id=False | ||
| arbitration_id= self.__transmission_address, data=data, is_extended_id=False | ||
| ) | ||
| ) | ||
|
|
||
| nak = ~self._await_can_msg(_validator, timeout=NAK_TIMEOUT) | ||
|
|
||
| if nak == False: | ||
| print(f"WE LIT!!!!! PACKET {self.__transmission_address} recieved\n") | ||
|
|
||
| elif nak == True: | ||
| print(f"PACKET DROPPED AT {self.__transmission_address}\n") | ||
| else: | ||
| print("TIMEOUT\n") | ||
|
|
||
| # Empirically, this tiny delay between messages seems to improve reliability. | ||
| time.sleep(0.0005) | ||
|
|
||
|
|
@@ -177,7 +211,6 @@ def update(self) -> None: | |
| Run the update procedure for this bootloader. | ||
|
|
||
| """ | ||
|
|
||
| def _intersect(a_min, a_max, b_min, b_max): | ||
| """1-D intersection to check if an app's hex and a flash sector share any addresses.""" | ||
| return a_max >= b_min and b_max >= a_min | ||
|
|
@@ -295,4 +328,4 @@ def size_bytes(self) -> int: | |
| return int( | ||
| math.ceil((self.ih.maxaddr() - self.ih.minaddr()) / MIN_PROG_SIZE_BYTES) | ||
| * MIN_PROG_SIZE_BYTES | ||
| ) | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra space here lol