📄 main.c
字号:
/*=============================================================================Cyan Technology LimitedFILE - main.cDESCRIPTION Main bootloader function The function is executed from IROM and copies the precompiled bootloader software residing in IROM (addr 0x7600) to RAM, before re-configuring the MMU to map the RAM into code space. The function then exits and returns to cstartup CHANGE LOG 27/11/04 - Version 1.0 - Tony Ward 20/06/05 - Version 2.0 - Tony Ward Updated to use XModem for file transfer and to improve robustness 08/05/05 - V2.1 ADC 08/25/05 - V2.2 ADC Added checks on application size Added code to flush duart input buffer 02/11/05 - V2.3 EJH Removed delay before starting XModem tranfer=============================================================================*/#include <ecog1.h>#include <stdlib.h>#include "driver_lib.h"#define VERBOSE#define FALSE 0#define TRUE 1extern void bootloader(void);extern int txchar(int c);// External CRC calculation routinesextern unsigned long CalcCrc(unsigned short *ptr, unsigned long count);extern void msec_delay(unsigned int delay);char uart_putstr(const char *s);int main(int argc, char* argv[]){ unsigned int app_size; // Application size unsigned long p_cs = 0; // Programmed checksum unsigned long c_cs = 0; // Calculated checksum int run_app = TRUE; int i; // Bootloader source and destination RAM addresses unsigned int *src_add_ptr = (unsigned int *)(0x7600); unsigned int *dest_add_ptr = (unsigned int *)(0xe800); // Set up the system clocks ssm_cpu_clk(SSM_HIGH_PLL, 6, 7); ssm_tmr_clk(SSM_HIGH_REF, 0); #ifdef VERBOSE // Welcome Message uart_putstr("\r\n\nBootloader V2.3 (c) Cyan Technology 2005"); uart_putstr("\r\nVerifying application..."); uart_putstr(" Press a key to erase flash and download new code");#endif // Map entire flash memory into bottom half of data space rg.mmu.flash_data_log = 0x0000; // Logical data address rg.mmu.flash_data_phy = 0x0000; // Physical flash address rg.mmu.flash_data_size = 0x7f; // 32k words of flash rg.mmu.translate_en = 0x0010; // Only flash data translation enabled fd.mmu.flash_ctrl.wait_states = 3; // We need to look for the checksum in ROM address 0x01fd-ff app_size = *((unsigned int *)0x01fd); if (app_size > (0x7000 - 0x0200)) // Size is not valid.. app_size = 0; // ..reset size to zero p_cs = *((unsigned long *)0x01fe); // Calculate the code checksum over locations 0x0200 - 0x6fff c_cs = CalcCrc((unsigned short *)0x200, app_size); #ifdef VERBOSE // Put the memory map back as it was before so data constants work rg.mmu.flash_data_log = 0x0000; // Logical data address rg.mmu.flash_data_phy = 0x007f; // Physical flash address rg.mmu.flash_data_size = 0x0; // 32k words of flash rg.mmu.translate_en = 0x0010; // Only flash data translation enabled fd.mmu.flash_ctrl.wait_states = 3; #endif // If app exists, jump to the reset vector if ((app_size > 0) && (p_cs == c_cs)) {#ifdef VERBOSE uart_putstr("\rApplication verified... ");#endif // Allow time for user to enter a character msec_delay(0x6000 - (app_size >> 2)); // Check if data has been received on UART A if (fd.duart.a_sts.rx_1b_rdy) // Key pressed, run bootloader { run_app = FALSE;#ifdef VERBOSE uart_putstr("\r\nKey pressed...");#endif } else { run_app = TRUE;#ifdef VERBOSE uart_putstr("\r\nApplication started...");#endif } } else { run_app = FALSE;#ifdef VERBOSE uart_putstr("\r\nVerification failed!");#endif } if (FALSE == run_app) {#ifdef VERBOSE uart_putstr("\r\nErasing flash."); uart_putstr("\r\nPlease start XModem transfer... "); // Map entire flash memory into bottom half of data space rg.mmu.flash_data_log = 0x0000; // Logical data address rg.mmu.flash_data_phy = 0x0000; // Physical flash address rg.mmu.flash_data_size = 0x7f; // 32k words of flash rg.mmu.translate_en = 0x0010; // Only flash data translation enabled#endif // Copy routine mapped to logical RAM to destination address (in RAM) for (i = 0; i < 0x0600; i++) // Routine size (max) 0x0600 { *dest_add_ptr++ = *src_add_ptr++; } // Map the RAM routine destination address back to code space rg.mmu.translate_en = 0x0000; // Disable RAM mapping for code area rg.mmu.ram_code_log = 0x0080; // Logical code address 0x8000 rg.mmu.ram_code_phy = 0x0000; // Physical RAM address 0x0000 rg.mmu.ram_code_size = 0x07; // 2K words of RAM rg.mmu.translate_en = 0x0002; // Enable RAM mapping for code area // Flush any input characters before starting xmodem download while (fd.duart.a_sts.rx_1b_rdy) { i = rg.duart.a_rx; // Empty input buffer } } return (run_app); // Return to cstartup and branch to the bootloader code or the user application}// Simple routine to output a string to UART Achar uart_putstr (const char *s) { while (txchar(*s++) != '\0') ; return (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -