;; ;; spreadspace pic utils ;; ;; ;; Copyright (C) 2011 Christian Pointner ;; ;; This file is part of spreadspace pic utils. ;; ;; spreadspace pic utils is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; any later version. ;; ;; spreadspace pic utils is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with spreadspace pic utils. If not, see . ;; ;; ------------------------------------- ;; PREAMBLE LIST p=16F887 include "p16f887.inc" __config _CONFIG1, _DEBUG_OFF & _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _INTOSC __config _CONFIG2, _BOR21V & _WRT_256 ;; ------------------------------------- ;; DEFINES #define BOOTPIN PORTC,7 USERVECT EQU H'100' ISRVECT EQU USERVECT + H'4' VERSION_MAJ EQU .0 VERSION_MIN EQU .1 DEVID_H EQU H'20' DEVID_L EQU H'82' FSS EQU .8 MESS_H EQU .0 MESS_L EQU .64 SUPPORTED_H EQU .0 SUPPORTED_L EQU b'00011111' ; reset, read/write flash, read/write eeprom ;; ERROR codes E_OK EQU .0 E_INV_CMD EQU .1 E_BAD_CSUM EQU .2 E_NOT_IMPL EQU .3 E_FLASH_WERR EQU .4 E_ADDR_INVALID EQU .5 E_ADDR_PROHIB EQU .6 E_VALUE_OOB EQU .7 ;; CMD codes CMD_INVALID EQU .0 CMD_IDENTIFY EQU .1 CMD_BOOT EQU .2 CMD_RESET EQU .3 CMD_R_FLASH EQU .4 CMD_W_FLASH EQU .5 CMD_R_EEPROM EQU .6 CMD_W_EEPROM EQU .7 CMD_R_CONFIG EQU .8 CMD_W_CONFIG EQU .9 CMD_MAX EQU .9 ;; 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_INVALID_CMD flags,0 #define F_WEEP flags,7 cnt2 EQU H'007E' cnt1 EQU H'007F' ;; ------------------------------------- ;; Boot test org .0 ;; btfsc BOOTPIN ;; goto USERVECT goto boot ;; ------------------------------------- ;; goto user ISR org .4 isr goto ISRVECT ;; ------------------------------------- ;; Bootloader (Subroutines) uart_tx_byte btfss PIR1,TXIF goto uart_tx_byte movwf TXREG xorwf csum,f return send_answer movwf cnt1 movlw combuff movwf FSR clrf csum send_answer_next movf INDF,w call uart_tx_byte incf FSR,f decfsz cnt1,f goto send_answer_next movf csum,w call uart_tx_byte return check_cmd_code movf combuff,w sublw CMD_MAX btfss STATUS,C bsf F_INVALID_CMD return get_cmdlen addwf PCL,f retlw .0 ; invalid retlw .1 ; identify: retlw .1 ; boot: retlw .1 ; reset: retlw .3 ; read flash: addr | retlw .3 + .2*FSS ; write flash: addr | data | retlw .5 ; read eeprom: addr | len | retlw .5 ; write eeprom: addr | len | data | (minimal) retlw .2 ; read config: nr | retlw .4 ; write config: nr | word | get_name addwf PCL,f nop retlw 'c' retlw 'i' retlw 'p' retlw '-' retlw 'd' retlw 'a' retlw 'e' retlw 'r' retlw 'p' retlw 's' ;; ------------------------------------- ;; Bootloader (init) boot ;; bank 3 bsf STATUS,RP0 bsf STATUS,RP1 movlw b'00001000' ; TX non-inverted, 16bit Baudrate, no auto baud detect ;; movlw b'00011000' ; TX inverted, 16bit Baudrate, no auto baud detect movwf BAUDCTL ;; bank 1 bcf STATUS,RP1 movlw b'01110000' ; set internal OSC to 8MHz movwf OSCCON movlw b'00100100' ; Baudrate = High Speed, async mode, transmit enabled, 8bit movwf TXSTA movlw .34 ; Baudrate = 57600 (@ 8MHz) -> -0,79 % Error ;; movlw .51 ; Baudrate = 38400 (@ 8MHz) -> -0,002 % Error ;; movlw .103 ; Baudrate = 19200 (@ 8MHz) -> 0,16 % Error movwf SPBRG clrf SPBRGH ;; bank 0 bcf STATUS,RP0 movlw b'10010000' ; enable Serial Port, 8bit, enable continues receive, disable address detection movwf RCSTA wait_new_cmd movlw combuff movwf FSR clrf combuff clrf current_cmdlen clrf flags wait_cmd 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 command btfsc RCSTA,FERR goto uart_rx_fe movf RCREG,w movwf INDF incf FSR,f movf current_cmdlen,f btfsc STATUS,Z goto new_cmd decfsz current_cmdlen,f goto wait_cmd goto exec_cmd uart_rx_fe ; recover from framing error movf RCREG,w goto wait_new_cmd new_cmd ; got new command code call check_cmd_code btfsc F_INVALID_CMD goto invalid_cmd movf combuff,w call get_cmdlen movwf current_cmdlen goto wait_cmd invalid_cmd ; received command code is not known movlw E_INV_CMD movwf combuff + .1 movlw .2 call send_answer goto wait_new_cmd exec_cmd ; command is correct and complete ;; TODO: check csum -> csum for write eeprom will not be correct because ;; it isn't finished yet....??? movf combuff,w ; dispatch commands addwf PCL,f nop goto cmd_identify goto cmd_boot goto cmd_reset goto cmd_r_flash goto cmd_w_flash goto cmd_r_eeprom goto cmd_w_eeprom goto cmd_r_config goto cmd_w_config goto wait_new_cmd ;; ** Command Handlers ******************** ;; ** identify ******* cmd_identify clrf csum movf combuff,w call uart_tx_byte movlw E_OK call uart_tx_byte movlw VERSION_MIN call uart_tx_byte movlw VERSION_MAJ call uart_tx_byte movlw .10 movwf cnt1 cmd_identify_send_name movf cnt1,w call get_name call uart_tx_byte decfsz cnt1,f goto cmd_identify_send_name movlw DEVID_L call uart_tx_byte movlw DEVID_H call uart_tx_byte movlw FSS call uart_tx_byte movlw MESS_L call uart_tx_byte movlw MESS_H call uart_tx_byte movlw SUPPORTED_L call uart_tx_byte movlw SUPPORTED_H call uart_tx_byte movf csum,w call uart_tx_byte goto wait_new_cmd ;; ** boot ******* cmd_boot movlw E_OK movwf combuff + .1 movlw .2 call send_answer goto USERVECT ;; ** reset ******* cmd_reset movlw E_OK movwf combuff + .1 movlw .2 call send_answer movlw b'00000001' movwf WDTCON ; why does this not work????? cmd_reset_wait goto cmd_reset_wait ;; ** 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 movlw E_ADDR_INVALID movwf combuff + .1 movlw .2 call send_answer goto wait_new_cmd ;; ** read eeprom ******* cmd_r_eeprom ;; call read_eeprom movlw E_ADDR_INVALID movwf combuff + .1 movlw .2 call send_answer goto wait_new_cmd ;; ** write eeprom ******* cmd_w_eeprom btfss F_WEEP goto cmd_w_eeprom_len ;; call write_eeprom movlw E_ADDR_INVALID movwf combuff + .1 movlw .2 call send_answer goto wait_new_cmd cmd_w_eeprom_len movf combuff + .3,w movwf current_cmdlen bsf F_WEEP goto wait_cmd ;; ** not implemented commands ******* cmd_r_config cmd_w_config movlw E_NOT_IMPL movwf combuff + .1 movlw .2 call send_answer goto wait_new_cmd ;; ------------------------------------- ;; actual flash/eeprom functions read_flash_segment movlw FSS movwf cnt1 movlw b'01011000' movwf STATUS movlw combuff + .1 movwf FSR movf INDF,w movwf EEADR incf FSR,f movf INDF,w movwf EEADRH read_flash_segment_loop bsf STATUS,RP0 bsf EECON1,EEPGD bsf EECON1,RD nop nop bcf STATUS,RP0 movf EEDAT,w movwf INDF incf FSR,f movf EEDATH,w movwf INDF incf FSR,f incfsz EEADR,f goto read_flash_segment_next incf EEADRH,f read_flash_segment_next decfsz cnt1,f goto read_flash_segment_loop bcf STATUS,RP1 clrf combuff + .1 ; = E_OK retlw .2 + .2*FSS ; bytes to send ;; ------------------------------------- ;; ------------------------------------- ;; dummy user code org USERVECT ;; goto USERVECT bsf STATUS,RP0 bcf TRISD,0 bcf STATUS,RP0 userloop movlw b'00000001' xorwf PORTD,f movlw .255 movwf cnt1 usercnt1 movlw .255 movwf cnt2 usercnt2 decfsz cnt2,f goto usercnt2 decfsz cnt1,f goto usercnt1 goto userloop ;; ------------------------------------- ;; END end