From 749ac9fa8ee0d5d91fd267b85eedd9443232e3b7 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Mon, 8 Jul 2013 01:22:08 +0000 Subject: cleaned up code (seperated into generic and specific part) git-svn-id: https://svn.spreadspace.org/pic/trunk@57 a09c6847-51d9-44de-8ef2-e725cf50f3c7 --- bootloader/bootloader.asm | 230 ++++++++++++++++++++++++---------------------- 1 file changed, 118 insertions(+), 112 deletions(-) (limited to 'bootloader/bootloader.asm') diff --git a/bootloader/bootloader.asm b/bootloader/bootloader.asm index 531c3d8..56d7eed 100644 --- a/bootloader/bootloader.asm +++ b/bootloader/bootloader.asm @@ -28,10 +28,11 @@ __config _CONFIG2, _BOR21V & _WRT_256 ;; ------------------------------------- - ;; DEFINES + ;; DEFINES (chip/com specific) #define BOOTPIN PORTC,7 USERVECT EQU H'100' ISRVECT EQU USERVECT + H'4' +FLASH_BOUNDARY EQU b'00001111' ; flash write boundary is at 16 bytes boundaries VERSION_MAJ EQU .0 VERSION_MIN EQU .1 @@ -49,6 +50,30 @@ MESS EQU .64 ; this limit is because of to combuff size SUPPORTED_H EQU .0 SUPPORTED_L EQU b'00000110' ; only read/write flash is supported by now +#define HOOK_CMD_RESET cmd_not_impl +#define HOOK_CMD_R_FLASH cmd_r_flash +#define HOOK_CMD_W_FLASH cmd_w_flash +#define HOOK_CMD_R_EEPROM cmd_not_impl +#define HOOK_CMD_W_EEPROM cmd_not_impl +#define HOOK_CMD_R_CONFIG cmd_not_impl +#define HOOK_CMD_W_CONFIG cmd_not_impl + + ;; Variables +combuff EQU H'0020' +combuff_end EQU H'006F' + +current_cmdlen EQU H'0070' +csum EQU H'0071' + +flags EQU H'007D' +#define F_CMD_STARTED flags,0 + +cnt2 EQU H'007E' +cnt EQU H'007F' + + + ;; ------------------------------------- + ;; DEFINES (generic defines) ;; ERROR codes E_OK EQU .0 E_INV_CMD EQU .1 @@ -73,30 +98,14 @@ CMD_R_CONFIG EQU .8 CMD_W_CONFIG EQU .9 CMD_MAX EQU .9 - - ;; Sanity Checks CMD_MIN_LEN EQU .3 -FLASH_BOUNDARY EQU b'00001111' ; flash boundary is at 16 bytes boundaries - - ;; Variables -combuff EQU H'0020' -combuff_end EQU H'006F' - -current_cmdlen EQU H'0070' -csum EQU H'0071' - -flags EQU H'007D' -#define F_CMD_STARTED flags,0 - -cnt2 EQU H'007E' -cnt1 EQU H'007F' ;; ------------------------------------- ;; Boot test - org .0 + org .0 ;; btfsc BOOTPIN ;; goto USERVECT - goto boot + goto com_init ;; ------------------------------------- ;; goto user ISR @@ -105,32 +114,24 @@ isr goto ISRVECT ;; ------------------------------------- - ;; Bootloader (Subroutines) -uart_tx_byte ;send one byte and add it to csum - btfss PIR1,TXIF - goto uart_tx_byte - movwf TXREG - xorwf csum,f - return - - ;; ------------------ + ;; Bootloader (Generic Subroutines) send_answer ; generic answer message, leave len of data in W addlw .3 movwf combuff + .1 decf combuff + .1,w - movwf cnt1 + movwf cnt movlw combuff movwf FSR clrf csum send_answer_next movf INDF,w - call uart_tx_byte + call com_tx_byte incf FSR,f - decfsz cnt1,f + decfsz cnt,f goto send_answer_next movf csum,w - call uart_tx_byte + call com_tx_byte return ;; ------------------ @@ -140,9 +141,39 @@ ack_cmd ; short answers which only contain return code call send_answer return + ;; ------------------------------------- - ;; Bootloader (init) -boot + ;; Bootloader (com specific subroutines and init) + ;; ------------------ +com_tx_byte ;send one byte and add it to csum + btfss PIR1,TXIF + goto com_tx_byte + movwf TXREG + xorwf csum,f + return + + ;; ------------------ +com_rx_byte + btfsc RCSTA,OERR + goto uart_rx_oe + btfss PIR1,RCIF + goto com_rx_byte + btfsc RCSTA,FERR + goto uart_rx_fe + movf RCREG,w + return + +uart_rx_oe ; recover from overflow + bcf RCSTA,CREN + bsf RCSTA,CREN + goto com_rx_byte + +uart_rx_fe ; recover from framing error + movf RCREG,w + goto com_rx_byte + + ;; ------------------ +com_init ;; bank 3 bsf STATUS,RP0 bsf STATUS,RP1 @@ -167,7 +198,8 @@ boot movlw b'10010000' ; enable Serial Port, 8bit, enable continues receive, disable address detection movwf RCSTA - ;; --------- end of init + ;; ------------------------------------- + ;; Bootloader (generic init) wait_new_cmd movlw combuff @@ -176,23 +208,9 @@ wait_new_cmd clrf current_cmdlen clrf flags -wait_cmd ; wait for new commands - btfsc PIR1,RCIF - goto uart_rx_byte - btfsc RCSTA,OERR - goto uart_rx_oe - goto wait_cmd - -uart_rx_oe ; recover from overflow - bcf RCSTA,CREN - bsf RCSTA,CREN - goto wait_new_cmd - -uart_rx_byte ; process received byte - btfsc RCSTA,FERR - goto uart_rx_fe - movf RCREG,w - movwf INDF +wait_cmd + call com_rx_byte + movwf INDF ; process received byte incf FSR,f btfss F_CMD_STARTED goto wait_cmd_len @@ -203,10 +221,6 @@ uart_rx_byte ; process received byte goto wait_cmd goto exec_cmd -uart_rx_fe ; recover from framing error - movf RCREG,w - goto wait_new_cmd - wait_cmd_len ; command code received, now wait for length bsf F_CMD_STARTED goto wait_cmd @@ -229,13 +243,13 @@ exec_cmd ; command is complete -> check csum movlw combuff movwf FSR movf combuff + .1,w - movwf cnt1 + movwf cnt clrf csum exec_cmd_check_csum movf INDF,w xorwf csum,f incf FSR,f - decfsz cnt1,f + decfsz cnt,f goto exec_cmd_check_csum movf csum,f btfss STATUS,Z @@ -246,15 +260,15 @@ exec_cmd_check_csum goto wait_new_cmd goto cmd_identify ; identify goto cmd_boot ; boot - goto cmd_not_impl ; reset - goto cmd_r_flash ; read flash segment - goto cmd_w_flash ; write flash segment - goto cmd_not_impl ; read eeprom - goto cmd_not_impl ; write eeprom - goto cmd_not_impl ; read config - goto cmd_not_impl ; write config - - ;; ** error messages ******* + goto HOOK_CMD_RESET ; reset + goto HOOK_CMD_R_FLASH ; read flash segment + goto HOOK_CMD_W_FLASH ; write flash segment + goto HOOK_CMD_R_EEPROM ; read eeprom + goto HOOK_CMD_W_EEPROM ; write eeprom + goto HOOK_CMD_R_CONFIG ; read config + goto HOOK_CMD_R_CONFIG ; write config + + ;; ** generic error messages ******* invalid_cmd ; received command code is not known or len is bogus movlw E_INV_CMD call ack_cmd @@ -275,57 +289,57 @@ address_prohibited ; received command uses prohibited address (i.e. FLAS call ack_cmd goto wait_new_cmd - ;; ** Command Handlers ******************** + ;; ** generic command handlers ************ ;; ** identify ******* cmd_identify clrf csum movf combuff,w - call uart_tx_byte + call com_tx_byte movlw .19 - call uart_tx_byte + call com_tx_byte movlw E_OK - call uart_tx_byte + call com_tx_byte movlw VERSION_MIN - call uart_tx_byte + call com_tx_byte movlw VERSION_MAJ - call uart_tx_byte + call com_tx_byte movlw NAME_0 - call uart_tx_byte + call com_tx_byte movlw NAME_1 - call uart_tx_byte + call com_tx_byte movlw NAME_2 - call uart_tx_byte + call com_tx_byte movlw DEVID_L - call uart_tx_byte + call com_tx_byte movlw DEVID_H - call uart_tx_byte + call com_tx_byte movlw FLASH_SIZE_L - call uart_tx_byte + call com_tx_byte movlw FLASH_SIZE_H - call uart_tx_byte + call com_tx_byte movlw FSS - call uart_tx_byte + call com_tx_byte movlw EEPROM_SIZE_L - call uart_tx_byte + call com_tx_byte movlw EEPROM_SIZE_H - call uart_tx_byte + call com_tx_byte movlw MESS - call uart_tx_byte + call com_tx_byte movlw SUPPORTED_L - call uart_tx_byte + call com_tx_byte movlw SUPPORTED_H - call uart_tx_byte + call com_tx_byte movf csum,w - call uart_tx_byte + call com_tx_byte goto wait_new_cmd ;; ** boot ******* @@ -334,18 +348,6 @@ cmd_boot call ack_cmd goto USERVECT - ;; ** read flash ******* -cmd_r_flash - call read_flash_segment - call send_answer - goto wait_new_cmd - - ;; ** write flash ******* -cmd_w_flash - call write_flash_segment - call send_answer - goto wait_new_cmd - ;; ** not implemented commands ******* cmd_not_impl movlw E_NOT_IMPL @@ -353,11 +355,11 @@ cmd_not_impl goto wait_new_cmd ;; ------------------------------------- - ;; actual flash/eeprom functions + ;; chip specific commands ;; flash read -------- -read_flash_segment +cmd_r_flash movlw FSS ; initialize EEADR:EEADRH and FSR - movwf cnt1 + movwf cnt movlw b'01011000' movwf STATUS movlw combuff + .2 @@ -384,14 +386,16 @@ read_flash_segment_loop goto read_flash_segment_next incf EEADRH,f read_flash_segment_next - decfsz cnt1,f + decfsz cnt,f goto read_flash_segment_loop bcf STATUS,RP1 clrf combuff + .2 ; = E_OK - retlw .1 + .2*FSS ; bytes to send + movlw .1 + .2*FSS ; bytes to send + call send_answer + goto wait_new_cmd ;; flash write -------- -write_flash_segment +cmd_w_flash movf combuff + .3,f ; if addr[15:8] == 0 -> boot loader section btfsc STATUS,Z goto address_prohibited @@ -400,7 +404,7 @@ write_flash_segment btfss STATUS,Z goto address_invalid movlw FSS ; initialize EEADR:EEADRH and FSR - movwf cnt1 + movwf cnt movlw b'01011000' movwf STATUS movlw combuff + .2 @@ -435,7 +439,7 @@ write_flash_segment_loop goto write_flash_segment_next incf EEADRH,f write_flash_segment_next - decfsz cnt1,f + decfsz cnt,f goto write_flash_segment_loop bsf STATUS,RP0 clrw ; check if a write error occured @@ -444,7 +448,9 @@ write_flash_segment_next bcf STATUS,RP1 bcf STATUS,RP0 movwf combuff + .2 ; = E_OK - retlw .1 ; bytes to send + movlw .1 ; bytes to send + call send_answer + goto wait_new_cmd ;; ------------------------------------- ;; ------------------------------------- @@ -458,15 +464,15 @@ userloop movlw b'00000001' xorwf PORTD,f movlw .255 - movwf cnt1 -usercnt1 + movwf cnt +usercnt movlw .255 movwf cnt2 usercnt2 decfsz cnt2,f goto usercnt2 - decfsz cnt1,f - goto usercnt1 + decfsz cnt,f + goto usercnt goto userloop ;; ------------------------------------- -- cgit v1.2.3