diff options
-rw-r--r-- | bootloader/bootloader.asm | 44 | ||||
-rwxr-xr-x | downloader/downloader.py | 6 | ||||
-rw-r--r-- | downloader/proto.txt | 4 |
3 files changed, 40 insertions, 14 deletions
diff --git a/bootloader/bootloader.asm b/bootloader/bootloader.asm index ff23d61..6df5906 100644 --- a/bootloader/bootloader.asm +++ b/bootloader/bootloader.asm @@ -66,6 +66,7 @@ CMD_R_CONFIG EQU .8 CMD_W_CONFIG EQU .9 CMD_MAX EQU .9 +CMD_MIN_LEN EQU .3 ;; Variables combuff EQU H'0020' @@ -75,7 +76,7 @@ current_cmdlen EQU H'0070' csum EQU H'0071' flags EQU H'007D' -#define F_NEW_CMD flags,0 +#define F_CMD_STARTED flags,0 cnt2 EQU H'007E' cnt1 EQU H'007F' @@ -186,7 +187,7 @@ uart_rx_byte ; process received command movf RCREG,w movwf INDF incf FSR,f - btfss F_NEW_CMD + btfss F_CMD_STARTED goto wait_cmd_len movf current_cmdlen,f btfsc STATUS,Z @@ -200,7 +201,7 @@ uart_rx_fe ; recover from framing error goto wait_new_cmd wait_cmd_len - bsf F_NEW_CMD + bsf F_CMD_STARTED goto wait_cmd new_cmd ; got new command code @@ -208,13 +209,15 @@ new_cmd ; got new command code sublw CMD_MAX btfss STATUS,C goto invalid_cmd - decfsz combuff + .1,w - goto load_cmdlen + movlw CMD_MIN_LEN ; check for minimum command len + subwf combuff + .1,w + btfss STATUS,C goto invalid_cmd -load_cmdlen + movlw .2 ; 2 bytes already received + subwf combuff + .1,w movwf current_cmdlen - decfsz current_cmdlen,f goto wait_cmd + invalid_cmd ; received command code is not known or len is bogus movlw E_INV_CMD movwf combuff + .2 @@ -222,9 +225,23 @@ invalid_cmd ; received command code is not known or len is bogus call send_answer goto wait_new_cmd -exec_cmd ; command is correct and complete - ;; TODO: check csum - movf combuff,w ; dispatch commands +exec_cmd ; command is complete -> check csum + movlw combuff + movwf FSR + movf combuff + .1,w + movwf cnt1 + clrf csum +exec_cmd_check_csum + movf INDF,w + xorwf csum,f + incf FSR,f + decfsz cnt1,f + goto exec_cmd_check_csum + movf csum,f + btfss STATUS,Z + goto csum_error + + movf combuff,w ; command is correct and complete -> dispatch addwf PCL,f goto wait_new_cmd goto cmd_identify ; identify @@ -237,6 +254,13 @@ exec_cmd ; command is correct and complete goto cmd_not_impl ; read config goto cmd_not_impl ; write config +csum_error ; received command has an invalid check sum + movlw E_BAD_CSUM + movwf combuff + .2 + movlw .1 + call send_answer + goto wait_new_cmd + ;; ** Command Handlers ******************** ;; ** identify ******* cmd_identify diff --git a/downloader/downloader.py b/downloader/downloader.py index f90eced..b85326f 100755 --- a/downloader/downloader.py +++ b/downloader/downloader.py @@ -108,9 +108,11 @@ def exec_command(dev, cmd, param, answer): 5: "address invalid", 6: "address prohibited", 7: "value out of bounds" } - cstr = bytearray(struct.pack('<BB', cmd, 0) + param) + dev.flushInput() + dev.flushOutput() + + cstr = bytearray(struct.pack('<BB', cmd, len(param)+3) + param) cstr.extend(struct.pack("<B", calc_csum(cstr))) - cstr[1] = len(cstr) dev.write(cstr) astr = bytearray() diff --git a/downloader/proto.txt b/downloader/proto.txt index 17dec93..98beebe 100644 --- a/downloader/proto.txt +++ b/downloader/proto.txt @@ -157,12 +157,12 @@ write eeprom: ~~~~~~~~~~~~~ command: - 7 | len=4+2<len(data) | addr | data | <csum> + 7 | len=4+len(data) | addr | data | <csum> answer: 7 | len=4 | <ret> | <csum> - The bootloader writes <data> (which has to contain exactly <len> bytes) to address + The bootloader writes <data> (which has to contain exactly <len>-4 bytes) to address <addr> inside the eeprom. len is 1byte long and the value must not exceed <mess> bytes. |