;***************************************************************************** ;***** FICHIER : BOOTLD10.ASM ;***** VERSION : 1.0 ;***** DESCRIPTION : Bios pour téléchargement logiciel en flash depuis PC ;***** PCB : Toute carte à base de PIC 16f876 ou 18F877 avec liaison RS232 ;***** AUTEUR : G. B ;***** DATE : 08/2007 ;***** ;***** Modifications par rapport a la version precedente : ;***** ;***** Bugs connus : ;***** ;***************************************************************************** ;***************************************************************************** ; Paramétrage de la compilation * ;***************************************************************************** LIST p=16F877a ; Définition de processeur variable CLCK_SPEED=20 ; Définit la freq du quartz en MHz. Permet aux tempos et timers de s'adapter ; Valeurs prevues : 4 ou 20 MHz #include ; fichier include #include ; Macros utiles __CONFIG _CP_OFF & _DEBUG_OFF & _WRT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC ;_CP_ALL protection Flash totale ;_CP_OFF Pas de protection Flash ;_DEBUG_ON RB6 et RB7 utilisées pour debugger ;_DEBUG_OFF RB6 et RB7 en utilisation normale ;_WRT_OFF No prog memory write protection ;_WRT_256 First 256 prog memory write protected ;_WRT_1FOURTH First quarter prog memory write protected ;_WRT_HALF First half memory write protected ;_CPD_ON Mémoire EEprom protégée ;_CPD_OFF Mémoire EEprom déprotégée ;_LVP_ON RB3 permet la programmation série de la PIC ;_LVP_OFF RB3 en utilisation normale ; _BODEN_ON Reset si Vdd < 4V. Valide PWRTE_ON automatiquement ; _BODEN_OFF Reset tension hors service ;_PWRTE_OFF Démarrage rapide ;_PWRTE_ON Démarrage temporisé ;_WDT_ON Watchdog en service ;_WDT_OFF Watchdog hors service ;_LP_OSC Oscillateur basse vitesse (32 0003h sinon la déplace ;***************************************************************************** TranslateBootSegment ; Copie l'adresse dans l'adresse d'écriture MOVR16ToR16 V_BIOS_AdrH, V_BIOS_AdrL, V_BIOS_WriteAdrH, V_BIOS_WriteAdrL ; Vérifie que l'adresse est > 0003h sinon déplace movf V_BIOS_WriteAdrH,W btfss STATUS,Z ; AddressH = 0 ? goto TBS_Fin ; Sinon, fin movlw 0xfc addwf V_BIOS_WriteAdrL,W ; W = AddressL-4 btfsc STATUS,C ; W < 0 ? goto TBS_Fin ; Sinon, fin ; Translation vers la zone ADR_Appli -> ADR_Appli + 3 movf V_BIOS_WriteAdrL,W ; relocate low adress addlw low (ADR_Appli) ; add low address to new location movwf V_BIOS_WriteAdrL MOVLF high (ADR_Appli), V_BIOS_WriteAdrH ;get new location high address TBS_Fin return ;***************************************************************************** ;*********** Ecriture en FLASH ;***************************************************************************** FLASH_Write ; ---- Ecrire FLASH (adresse = V_BIOS_AdrH:V_BIOS_AdrL, ; data = V_BIOS_RegH:V_BIOS_RegL) ; Verif que AdressH <= 0x1F pour ne pas dépasser la capacité du processeur movlw (0xFF-high(ADR_Max)) addwf V_BIOS_WriteAdrH,W ; W = AddressH - high(adresseMax)-1 btfsc STATUS,C ; W < 0 ? return ; Sinon, fin ; Ecriture BANK2 MOVR16ToR16 V_BIOS_WriteAdrH, V_BIOS_WriteAdrL, EEADRH, EEADR ;Write address of desired program memory location MOVR16ToR16 V_BIOS_RegH, V_BIOS_RegL, EEDATH, EEDATA ;Write value to program at desired memory location bsf STATUS, RP0 ;Bank 3 bsf EECON1, EEPGD ;Point to Program memory bsf EECON1, WREN ;Enable writes bcf STATUS,C btfss INTCON, GIE ;If interrupts are enabled, goto FHW_Suite bsf STATUS,C ;Set C=1 bcf INTCON, GIE ;and disable interrupts FHW_Suite MOVLF 0x55, EECON2 MOVLF 0xAA, EECON2 bsf EECON1, WR ;Start write operation nop ;Two NOPs to allow micro to setup for write nop btfsc STATUS,C bsf INTCON, GIE ;Enable interrupts if enabled before bcf EECON1, WREN ;Disable writes BANK0 return ; //////////////////////////////////////////////////////////////////////////// ; P R O G R A M M E P R I N C I P A L ; //////////////////////////////////////////////////////////////////////////// Principal ; ---- initialisation PORTS (banque 0 et 1) ; BANK0 ; clrf PORTA ; Sorties PORTA à 0 ; clrf PORTB ; sorties PORTB à 0 ; clrf PORTC ; sorties PORTC à 0 BANK1 ; MOVLF DIRPORTA, TRISA ; Direction PORTA ; MOVLF DIRPORTB, TRISB ; Direction PORTB MOVLF DIRPORTC, TRISC ; Direction PORTC MOVLF OPTIONVAL, OPTION_REG ; Registre d'options ; MOVLF INTCONVAL, INTCON ; Interruptions // !!!!!!!!!! Les interruptions sont inactives pour que le bootloader n'occupe pas la zone de gestion des interruptions. BANK0 clrf V_TRS_Checksum MOVLF B'00000001', V_TRS_RxStep call TRS_OpenComm ; ---- Init BIOS bcf V_BIOSMode ; ///// Config timer 1 (Attente réception série pour choix mode BOOT) (100ms) if CLCK_SPEED==20 MOVLF 0x00,TMR1H else MOVLF 0xCC,TMR1H endif MOVLF 0x00,TMR1L MOVLF B'00110001',T1CON ; Source = FOsc/4, Prescaler = 8, Start timer PP_AttenteChoixMode btfsc PIR1,RCIF ; Réception sur RS232 ? goto PP_DemarrerModeBIOS ; Oui => Démarrer en mode BIOS btfss PIR1,TMR1IF goto PP_AttenteChoixMode PP_DemarrerModeAPPLI clrf T1CON ; Arrête timer goto ADR_Appli PP_DemarrerModeBIOS clrf T1CON ; Arrête timer bsf V_BIOSMode PP_Attente ; ---- Boucle principale btfss PIR1,RCIF ; Tester si interrupt en cours. LE FLAG NE DOIT PAS ETRE REMIS A 0 (fait par LECTURE DE RCREG) goto PP_Attente ; non sauter call TRS_RxProg ; oui, traiter interrupt btfsc V_BIOSMode ; Si téléchargement non terminé, continue attente goto PP_Attente ;Attente goto $ END