00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <avr/io.h>
00025 #include <avr/interrupt.h>
00026 #include <avr/pgmspace.h>
00027 #include <avr/wdt.h>
00028 #include <avr/boot.h>
00029
00030 #include "usbdrv.h"
00031 #include "bootloaderconfig.h"
00032
00033 #define USBBOOT_FUNC_WRITE_PAGE 2
00034 #define USBBOOT_FUNC_LEAVE_BOOT 1
00035 #define USBBOOT_FUNC_GET_PAGESIZE 3
00036
00037 #define STATE_IDLE 0
00038 #define STATE_WRITE_PAGE 1
00039
00040 static uchar replyBuffer[8];
00041 static uchar state = STATE_IDLE;
00042 static unsigned int page_address;
00043 static unsigned int page_offset;
00044
00045 void (*jump_to_app)(void) = 0x0000;
00046
00047 void leaveBootloader() {
00048 cli();
00049 boot_rww_enable();
00050 GICR = (1 << IVCE);
00051 GICR = (0 << IVSEL);
00052 jump_to_app();
00053 }
00054
00055 uchar usbFunctionSetup(uchar data[8])
00056 {
00057 uchar len = 0;
00058
00059 if (data[1] == USBBOOT_FUNC_LEAVE_BOOT) {
00060 leaveBootloader();
00061 } else if (data[1] == USBBOOT_FUNC_WRITE_PAGE) {
00062
00063 state = STATE_WRITE_PAGE;
00064
00065 page_address = (data[3] << 8) | data[2];
00066 page_offset = 0;
00067
00068 eeprom_busy_wait();
00069 cli();
00070 boot_page_erase(page_address);
00071 sei();
00072 boot_spm_busy_wait();
00073
00074 len = 0xff;
00075
00076 } else if (data[1] == USBBOOT_FUNC_GET_PAGESIZE) {
00077
00078 replyBuffer[0] = SPM_PAGESIZE >> 8;
00079 replyBuffer[1] = SPM_PAGESIZE & 0xff;
00080 len = 2;
00081
00082 }
00083
00084 usbMsgPtr = replyBuffer;
00085
00086 return len;
00087 }
00088
00089
00090 uchar usbFunctionWrite(uchar *data, uchar len)
00091 {
00092
00093 uchar i;
00094
00095
00096 if (state != STATE_WRITE_PAGE)
00097 return 0xff;
00098
00099 for (i = 0; i < len; i+=2) {
00100
00101 cli();
00102 boot_page_fill(page_address + page_offset, data[i] | (data[i + 1] << 8));
00103 sei();
00104 page_offset += 2;
00105
00106
00107 if (page_offset >= SPM_PAGESIZE) {
00108
00109
00110 cli();
00111 boot_page_write(page_address);
00112 sei();
00113 boot_spm_busy_wait();
00114
00115 state = STATE_IDLE;
00116 return 1;
00117 }
00118
00119 }
00120
00121 return 0;
00122 }
00123
00124 int main(void)
00125 {
00126
00127 BOOTLOADER_INIT;
00128
00129
00130 if (!BOOTLOADER_CONDITION) {
00131 leaveBootloader();
00132 }
00133
00134 GICR = (1 << IVCE);
00135 GICR = (1 << IVSEL);
00136
00137 usbInit();
00138 sei();
00139 for(;;){
00140 usbPoll();
00141 }
00142 return 0;
00143 }
00144