;; ;; 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' FSS EQU 8 ;; 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 ;; CMD codes CCMD_IDENTIFY EQU 'i' CMD_IDENTIFY EQU 0 CCMD_BOOT EQU 'b' CMD_BOOT EQU 1 CCMD_RESET EQU 'r' CMD_RESET EQU 2 CCMD_R_FLASH EQU 'f' CMD_R_FLASH EQU 3 CCMD_W_FLASH EQU 'F' CMD_W_FLASH EQU 4 CCMD_R_EEPROM EQU 'e' CMD_R_EEPROM EQU 5 CCMD_W_EEPROM EQU 'E' CMD_W_EEPROM EQU 6 CCMD_R_CONFIG EQU 'c' CMD_R_CONFIG EQU 7 CCMD_W_CONFIG EQU 'C' CMD_W_CONFIG EQU 8 ;; ... CNT1 EQU H'0020' CNT2 EQU H'0021' ;; ------------------------------------- ;; 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 return translate_cmd_codes xorlw CCMD_IDENTIFY btfsc STATUS,Z retlw CMD_IDENTIFY xorlw CCMD_BOOT ^ CCMD_IDENTIFY btfsc STATUS,Z retlw CMD_BOOT xorlw CCMD_RESET ^ CCMD_BOOT btfsc STATUS,Z retlw CMD_RESET xorlw CCMD_R_FLASH ^ CCMD_RESET btfsc STATUS,Z retlw CMD_R_FLASH xorlw CCMD_W_FLASH ^ CCMD_R_FLASH btfsc STATUS,Z retlw CMD_W_FLASH xorlw CCMD_R_EEPROM ^ CCMD_W_FLASH btfsc STATUS,Z retlw CMD_R_EEPROM xorlw CCMD_W_EEPROM ^ CCMD_R_EEPROM btfsc STATUS,Z retlw CMD_W_EEPROM xorlw CCMD_R_CONFIG ^ CCMD_W_EEPROM btfsc STATUS,Z retlw CMD_R_CONFIG xorlw CCMD_W_CONFIG ^ CCMD_R_CONFIG btfsc STATUS,Z retlw CMD_W_CONFIG retlw H'FF' ;; ------------------------------------- ;; 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_cmd btfsc PIR1,RCIF goto uart_rx_byte btfsc RCSTA,OERR goto uart_rx_oe goto wait_cmd uart_rx_oe bcf RCSTA,CREN bsf RCSTA,CREN goto wait_cmd uart_rx_byte btfsc RCSTA,FERR goto uart_rx_fe movf RCREG,w ; TODO: check parity?? ; TODO: move to input string ;; TODO: check if command is finished and jump ;; to 'exec_cmd' call translate_cmd_codes call uart_tx_byte goto wait_cmd goto exec_cmd uart_rx_fe movf RCREG,w goto wait_cmd exec_cmd ;; TODO: execute received command goto wait_cmd ;; ------------------------------------- ;; dummy user code org USERVECT ;; goto USERVECT bsf STATUS,RP0 bcf TRISD,0 bcf STATUS,RP0 userloop movlw b'00000001' xorwf TRISD,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