?? boot.c
字號:
/*
Turbo BDM Light ColdFire - USB bootloader
Copyright (C) 2005 Daniel Malik
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "MC68HC908JB16.h"
#include "start08.h"
#include "version.h"
#include "boot.h"
#include "commands.h"
#include "usb.h"
#include "timer.h"
#include "bdmcf.h"
/* bootloader state:
after mass/block erase the state will be 0xFF, 0xFF
after the code is programmed in and verified, the boot application programs the state to 0xFF, 0x01
when the state is 0xFF,0x00 the application will be executed on power-up
when the application receives request to return to the boot state, it programs the second byte and state becomes 0x00, 0x01
state of 0x00, 0x01 will force the boot operation again */
#pragma CONST_SEG BOOTLOADER_STATE_SEG
const unsigned char bootloader_state[2]={0xFF,0x01};
/* secondary vectors:
the primary vectors are grouped together with the boot decision code and should only be erased when really needed
the danger is power brown-out during programming as there might be no valid code left in the flash if this happens
the secondary "vectors" are in the application part of the flash and can be erased/changed without loosing the bootcode */
#pragma CODE_SEG SECONDARY_VECTORS_SEG
void secondary_vectors(void) {
#pragma NO_RETURN
#pragma MESSAGE DISABLE C20000
asm {
JMP _Startup /* Keyboard */
JMP _Startup /* SCI transmit */
JMP _Startup /* SCI receive */
JMP _Startup /* SCI error */
JMP timer2_10ms_tick /* TIM2 overflow */
JMP _Startup /* TIM2 channel 0 and 1 */
JMP _Startup /* TIM2 channel 1 */
JMP _Startup /* TIM2 channel 0 */
JMP _Startup /* TIM1 overflow */
JMP _Startup /* TIM1 channel 0 and 1 */
JMP _Startup /* TIM1 channel 1 */
JMP rsto_detect /* TIM1 channel 0 */
JMP _Startup /* IRQ */
JMP usb_isr /* USB */
JMP _Startup /* SWI */
JMP _Startup /* Reset */
}
}
/* primary vectors point to the secondary vectors */
#pragma CONST_SEG PRIMARY_VECTORS_SEG
void * const primary_vectors[16] = {
(char*)(void*)secondary_vectors+0, /* Keyboard */
(char*)(void*)secondary_vectors+3, /* SCI transmit */
(char*)(void*)secondary_vectors+6, /* SCI receive */
(char*)(void*)secondary_vectors+9, /* SCI error */
(char*)(void*)secondary_vectors+12, /* TIM2 overflow */
(char*)(void*)secondary_vectors+15, /* TIM2 channel 0 and 1 */
(char*)(void*)secondary_vectors+18, /* TIM2 channel 1 */
(char*)(void*)secondary_vectors+21, /* TIM2 channel 0 */
(char*)(void*)secondary_vectors+24, /* TIM1 overflow */
(char*)(void*)secondary_vectors+27, /* TIM1 channel 0 and 1 */
(char*)(void*)secondary_vectors+30, /* TIM1 channel 1 */
(char*)(void*)secondary_vectors+33, /* TIM1 channel 0 */
(char*)(void*)secondary_vectors+36, /* IRQ */
(char*)(void*)secondary_vectors+39, /* USB */
(char*)(void*)secondary_vectors+42, /* SWI */
(char*)(void*)bootloader /* Reset */
};
/* boot code: this code decides whether the application or the in-circuit programming should be launched */
/* written in assembly to fit into the 16 bytes left unused above the vector table */
#pragma CODE_SEG BOOTLOADER_SEG
void bootloader(void) {
#pragma NO_RETURN
asm {
LDHX @bootloader_state /* load address of bootloader_state into H:X */
LDA ,X /* load first byte of bootloader_state into A */
ADD 1,X /* add second byte of bootloader_state into A */
BEQ Run_Application /* if the sum of the bytes is 0, jump to the secondary reset "vector" (e.g. 0xFF and 0x01) */
BSET UCR3_PULLEN_BITNUM,UCR3 /* enable internal USB pull-up resistor */
BSET CONFIG_COPD_BITNUM,CONFIG /* disable COP (the ICP routine does not service it) */
/* JMP ICP_ADDRESS */ /* jump to the in-circuit programming routine address */
JMP (ICP_ADDRESS-BOOTLOADER_STATE_ADDR),X /* dirty trick to save flash space */
Run_Application:
/* JMP secondary_vectors:45 */ /* jump to the secondary reset "vector" */
JMP 47,X /* dirty trick to save flash space - bootloader_state is 2 bytes before secondary_vectors... */
}
}
/* test of monitor code */
#pragma CODE_SEG BOOTLOADER_SEG
void bootloader_(void) {
#pragma NO_RETURN
asm {
BSET CONFIG_COPD_BITNUM,CONFIG /* disable COP (the ICP routine does not service it) */
JMP 0xFE10 /* jump to monitor code entry */
}
}
/* this function will program bootloader_state[0] to 0x00 and thus force bootloader action on next power-up */
#pragma CODE_SEG DEFAULT
void force_bootloader(void) {
unsigned char temp_storage[4]; /* stack space for storing values in the RAM locations used by ROM Flash programming */
temp_storage[0] = CPUSPD; /* save RAM contents */
*((unsigned int *)&(temp_storage[1])) = LADDR;
temp_storage[3] = *(DATABUFF+0);
CPUSPD = 24; /* CPU speed = 4x bus speed in MHz */
LADDR = (unsigned int)&bootloader_state[0]; /* last byte to program */
*(DATABUFF+0) = 0; /* value to be programmed */
FLBPR=0xFE; /* remove flash protection (only keep the primary vectors & bootloader protected) */
asm {
LDHX @bootloader_state /* load the address to be programmed into H:X */
JSR FLASH_PRG_ADDRESS /* call the flash programming routine in ROM */
}
CPUSPD = temp_storage[0]; /* restore RAM contents */
LADDR = *((unsigned int *)&temp_storage[1]);
*(DATABUFF+0) = temp_storage[3];
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -