* Project: USBaspLoader
* Author: Stephan Baerwolf
* Creation Date: 2012-08-01
- * Copyright: (c) 2012 by Stephan Baerwolf
+ * Copyright: (c) 2013 by Stephan Baerwolf
* License: GNU GPL v2 (see License.txt)
- * Version: 0.95
+ * Version: 0.97
*/
#ifndef SPMINTERFACE_H_f70ba6adf7624275947e859bdbff0599
*
*/
-#include <avr/io.h>
#include "bootloaderconfig.h"
+#ifndef SREG
+# include <avr/io.h>
+#endif
/*
#define funcaddr___bootloader__do_spm (&bootloader__do_spm)
#endif
#else
- #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
+ #if defined (__AVR_ATmega8535__)
+ #define funcaddr___bootloader__do_spm 0x182a
+ #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
#define funcaddr___bootloader__do_spm 0x1826
+ #elif defined (__AVR_ATmega16__)
+ #define funcaddr___bootloader__do_spm 0x3854
#elif defined (__AVR_ATmega32__)
#define funcaddr___bootloader__do_spm 0x7054
#elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88PA__)
#define funcaddr___bootloader__do_spm 0x1834
+ #elif defined (__AVR_ATmega162__)
+ #define funcaddr___bootloader__do_spm 0x3870
#elif defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
#define funcaddr___bootloader__do_spm 0x387c
#elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168PA__)
#define funcaddr___bootloader__do_spm 0x707c
#elif defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
#define funcaddr___bootloader__do_spm 0x7068
- #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
+ #elif defined (__AVR_ATmega640__)
+ #define funcaddr___bootloader__do_spm 0xe0e4
+ #elif defined (__AVR_ATmega644__)
+ #define funcaddr___bootloader__do_spm 0xe070
+ #elif defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
#define funcaddr___bootloader__do_spm 0xe07c
#elif defined (__AVR_ATmega128__)
#define funcaddr___bootloader__do_spm 0x1e08c
+ #elif defined (__AVR_ATmega1280__)
+ #define funcaddr___bootloader__do_spm 0x1e0e4
+ #elif defined (__AVR_ATmega1281__)
+ #define funcaddr___bootloader__do_spm 0x1e0cc
#elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
#define funcaddr___bootloader__do_spm 0x1e08c
+ #elif defined (__AVR_ATmega2560__)
+ #define funcaddr___bootloader__do_spm 0x3e0e4
+ #elif defined (__AVR_ATmega2561__)
+ #define funcaddr___bootloader__do_spm 0x3e0cc
#else
#error "unknown MCU - where is bootloader__do_spm located?"
#endif
+
+ #if ((defined(_VECTORS_SIZE)) && (defined(BOOTLOADER_ADDRESS)))
+ #if (funcaddr___bootloader__do_spm != (BOOTLOADER_ADDRESS+_VECTORS_SIZE))
+ #error "bootloader__do_spm is not located after interrupts - sth. is very wrong here!"
+ #endif
+ #endif
+
#endif
#endif
* REMEMBER: interrupts have to be disabled! (otherwise code may crash non-deterministic)
*
*/
+
+#define __do_spm_Ex(arguments...) __do_spm_GeneralEx(HAVE_SPMINTEREFACE_MAGICVALUE, ##arguments)
+
+
#if HAVE_SPMINTEREFACE_MAGICVALUE
-#define __do_spm_Ex __do_spm_Ex_magic
+#define __do_spm_GeneralEx __do_spm_ExASMEx_magic
#else
-#define __do_spm_Ex __do_spm_Ex_
+#define __do_spm_GeneralEx __do_spm_ExASMEx_
#endif
-
-#define __do_spm_Ex_(flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
-({ \
- asm volatile ( \
- "push r0\n\t" \
- "push r1\n\t" \
- \
- "mov r13, %B[flashaddress]\n\t" \
- "mov r12, %A[flashaddress]\n\t" \
- "mov r11, %C[flashaddress]\n\t" \
- \
- /* also load the spmcrval */ \
- "mov r18, %[spmcrval]\n\t" \
- \
- \
- "mov r1, %B[data]\n\t" \
- "mov r0, %A[data]\n\t" \
- \
- /* finally call the bootloader-function */ \
- "icall\n\t" \
- \
- /* \
- * bootloader__do_spm should change spmcrval (r18) to \
- * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
- */ \
- "cpi r18, %[spmret]\n\t" \
- /* loop infitinte if not so, most likely we called an bootloader__do_spm \
- * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
- */ \
-"loop%=: \n\t" \
- "brne loop%= \n\t" \
- \
- "pop r1\n\t" \
- "pop r0\n\t" \
- \
- : \
- : [flashaddress] "r" (flash_wordaddress), \
- [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
- [spmcrval] "r" (spmcrval), \
- [data] "r" (dataword), \
- [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)) \
- : "r0","r1","r11","r12","r13","r18" \
- ); \
-})
-
-#define __do_spm_Ex_magic(flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
-({ \
- asm volatile ( \
- "push r0\n\t" \
- "push r1\n\t" \
- \
- "ldi r23, %[magicD] \n\t" \
- "ldi r22, %[magicC] \n\t" \
- "ldi r21, %[magicB] \n\t" \
- "ldi r20, %[magicA] \n\t" \
- \
- "mov r13, %B[flashaddress]\n\t" \
- "mov r12, %A[flashaddress]\n\t" \
- "mov r11, %C[flashaddress]\n\t" \
- \
- /* also load the spmcrval */ \
- "mov r18, %[spmcrval]\n\t" \
- \
- \
- "mov r1, %B[data]\n\t" \
- "mov r0, %A[data]\n\t" \
- \
- /* finally call the bootloader-function */ \
- "icall\n\t" \
- \
- /* \
- * bootloader__do_spm should change spmcrval (r18) to \
- * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
- */ \
- "cpi r18, %[spmret]\n\t" \
- /* loop infitinte if not so, most likely we called an bootloader__do_spm \
- * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
- */ \
-"loop%=: \n\t" \
- "brne loop%= \n\t" \
- \
- "pop r1\n\t" \
- "pop r0\n\t" \
- \
- : \
- : [flashaddress] "r" (flash_wordaddress), \
- [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
- [spmcrval] "r" (spmcrval), \
- [data] "r" (dataword), \
- [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)), \
- [magicD] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>>24)&0xff), \
- [magicC] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>>16)&0xff), \
- [magicB] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>> 8)&0xff), \
- [magicA] "M" ((HAVE_SPMINTEREFACE_MAGICVALUE>> 0)&0xff) \
- : "r0","r1","r11","r12","r13","r18","r20","r21","r22","r23" \
- ); \
-})
+#if (defined(EIND) && ((FLASHEND)>131071))
+ /*
+ * Huge flash version using eicall (and EIND)
+ * MV defines the magic value to be send to bootloader_do_spm
+ */
+ #define __do_spm_ExASMEx_(MV, flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
+ ({ \
+ asm volatile ( \
+ "push r0\n\t" \
+ "push r1\n\t" \
+ \
+ "mov r13, %B[flashaddress]\n\t" \
+ "mov r12, %A[flashaddress]\n\t" \
+ "mov r11, %C[flashaddress]\n\t" \
+ \
+ /* prepare the EIND for following eicall */ \
+ "in r18, %[eind]\n\t" \
+ "push r18\n\t" \
+ "ldi r18, %[spmfuncaddrEIND]\n\t" \
+ "out %[eind], r18\n\t" \
+ \
+ /* also load the spmcrval */ \
+ "mov r18, %[spmcrval]\n\t" \
+ \
+ \
+ "mov r1, %B[data]\n\t" \
+ "mov r0, %A[data]\n\t" \
+ \
+ /* finally call the bootloader-function */ \
+ "eicall\n\t" \
+ "pop r1\n\t" \
+ "out %[eind], r1\n\t" \
+ \
+ /* \
+ * bootloader__do_spm should change spmcrval (r18) to \
+ * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
+ */ \
+ "cpi r18, %[spmret]\n\t" \
+ /* loop infitinte if not so, most likely we called an bootloader__do_spm \
+ * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
+ */ \
+ "loop%=: \n\t" \
+ "brne loop%= \n\t" \
+ \
+ "pop r1\n\t" \
+ "pop r0\n\t" \
+ \
+ : \
+ : [flashaddress] "r" (flash_wordaddress), \
+ [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
+ [spmfuncaddrEIND] "M" ((uint8_t)(___bootloader__do_spm__ptr>>16)), \
+ [eind] "I" (_SFR_IO_ADDR(EIND)), \
+ [spmcrval] "r" (spmcrval), \
+ [data] "r" (dataword), \
+ [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)) \
+ : "r0","r1","r11","r12","r13","r18" \
+ ); \
+ })
+
+ #define __do_spm_ExASMEx_magic(MV, flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
+ ({ \
+ asm volatile ( \
+ "push r0\n\t" \
+ "push r1\n\t" \
+ \
+ "ldi r23, %[magicD] \n\t" \
+ "ldi r22, %[magicC] \n\t" \
+ "ldi r21, %[magicB] \n\t" \
+ "ldi r20, %[magicA] \n\t" \
+ \
+ "mov r13, %B[flashaddress]\n\t" \
+ "mov r12, %A[flashaddress]\n\t" \
+ "mov r11, %C[flashaddress]\n\t" \
+ \
+ /* prepare the EIND for following eicall */ \
+ "in r18, %[eind]\n\t" \
+ "push r18\n\t" \
+ "ldi r18, %[spmfuncaddrEIND]\n\t" \
+ "out %[eind], r18\n\t" \
+ \
+ /* also load the spmcrval */ \
+ "mov r18, %[spmcrval]\n\t" \
+ \
+ \
+ "mov r1, %B[data]\n\t" \
+ "mov r0, %A[data]\n\t" \
+ \
+ /* finally call the bootloader-function */ \
+ "eicall\n\t" \
+ "pop r1\n\t" \
+ "out %[eind], r1\n\t" \
+ \
+ /* \
+ * bootloader__do_spm should change spmcrval (r18) to \
+ * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
+ */ \
+ "cpi r18, %[spmret]\n\t" \
+ /* loop infitinte if not so, most likely we called an bootloader__do_spm \
+ * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
+ */ \
+ "loop%=: \n\t" \
+ "brne loop%= \n\t" \
+ \
+ "pop r1\n\t" \
+ "pop r0\n\t" \
+ \
+ : \
+ : [flashaddress] "r" (flash_wordaddress), \
+ [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
+ [spmfuncaddrEIND] "M" ((uint8_t)(___bootloader__do_spm__ptr>>16)), \
+ [eind] "I" (_SFR_IO_ADDR(EIND)), \
+ [spmcrval] "r" (spmcrval), \
+ [data] "r" (dataword), \
+ [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)), \
+ [magicD] "M" (((MV)>>24)&0xff), \
+ [magicC] "M" (((MV)>>16)&0xff), \
+ [magicB] "M" (((MV)>> 8)&0xff), \
+ [magicA] "M" (((MV)>> 0)&0xff) \
+ : "r0","r1","r11","r12","r13","r18","r20","r21","r22","r23" \
+ ); \
+ })
+
+
+#else
+ /*
+ * Normal version for devices with <=128KiB flash (using icall)
+ */
+ #if ((FLASHEND)>131071)
+ #error "Using inappropriate code for device with more than 128kib flash"
+ #endif
+
+ #define __do_spm_ExASMEx_(MV, flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
+ ({ \
+ asm volatile ( \
+ "push r0\n\t" \
+ "push r1\n\t" \
+ \
+ "mov r13, %B[flashaddress]\n\t" \
+ "mov r12, %A[flashaddress]\n\t" \
+ "mov r11, %C[flashaddress]\n\t" \
+ \
+ /* also load the spmcrval */ \
+ "mov r18, %[spmcrval]\n\t" \
+ \
+ \
+ "mov r1, %B[data]\n\t" \
+ "mov r0, %A[data]\n\t" \
+ \
+ /* finally call the bootloader-function */ \
+ "icall\n\t" \
+ \
+ /* \
+ * bootloader__do_spm should change spmcrval (r18) to \
+ * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
+ */ \
+ "cpi r18, %[spmret]\n\t" \
+ /* loop infitinte if not so, most likely we called an bootloader__do_spm \
+ * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
+ */ \
+ "loop%=: \n\t" \
+ "brne loop%= \n\t" \
+ \
+ "pop r1\n\t" \
+ "pop r0\n\t" \
+ \
+ : \
+ : [flashaddress] "r" (flash_wordaddress), \
+ [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
+ [spmcrval] "r" (spmcrval), \
+ [data] "r" (dataword), \
+ [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)) \
+ : "r0","r1","r11","r12","r13","r18" \
+ ); \
+ })
+
+ #define __do_spm_ExASMEx_magic(MV, flash_wordaddress, spmcrval, dataword, ___bootloader__do_spm__ptr) \
+ ({ \
+ asm volatile ( \
+ "push r0\n\t" \
+ "push r1\n\t" \
+ \
+ "ldi r23, %[magicD] \n\t" \
+ "ldi r22, %[magicC] \n\t" \
+ "ldi r21, %[magicB] \n\t" \
+ "ldi r20, %[magicA] \n\t" \
+ \
+ "mov r13, %B[flashaddress]\n\t" \
+ "mov r12, %A[flashaddress]\n\t" \
+ "mov r11, %C[flashaddress]\n\t" \
+ \
+ /* also load the spmcrval */ \
+ "mov r18, %[spmcrval]\n\t" \
+ \
+ \
+ "mov r1, %B[data]\n\t" \
+ "mov r0, %A[data]\n\t" \
+ \
+ /* finally call the bootloader-function */ \
+ "icall\n\t" \
+ \
+ /* \
+ * bootloader__do_spm should change spmcrval (r18) to \
+ * "((1<<RWWSRE) | (1<<SPMEN))" in case of success \
+ */ \
+ "cpi r18, %[spmret]\n\t" \
+ /* loop infitinte if not so, most likely we called an bootloader__do_spm \
+ * with wrong magic! To avoid calls to wrong initialized pages, better crash here... \
+ */ \
+ "loop%=: \n\t" \
+ "brne loop%= \n\t" \
+ \
+ "pop r1\n\t" \
+ "pop r0\n\t" \
+ \
+ : \
+ : [flashaddress] "r" (flash_wordaddress), \
+ [spmfunctionaddress] "z" ((uint16_t)(___bootloader__do_spm__ptr)), \
+ [spmcrval] "r" (spmcrval), \
+ [data] "r" (dataword), \
+ [spmret] "M" ((1<<RWWSRE) | (1<<SPMEN)), \
+ [magicD] "M" (((MV)>>24)&0xff), \
+ [magicC] "M" (((MV)>>16)&0xff), \
+ [magicB] "M" (((MV)>> 8)&0xff), \
+ [magicA] "M" (((MV)>> 0)&0xff) \
+ : "r0","r1","r11","r12","r13","r18","r20","r21","r22","r23" \
+ ); \
+ })
+#endif
+
#if (!(defined(BOOTLOADER_ADDRESS))) || (defined(NEW_BOOTLOADER_ADDRESS))
void do_spm(const uint32_t flash_byteaddress, const uint8_t spmcrval, const uint16_t dataword) {
__do_spm_Ex(flash_byteaddress, spmcrval, dataword, funcaddr___bootloader__do_spm >> 1);
/*
* insert architecture dependend "bootloader_do_spm"-code
- *
- * try to make this array as big as possible
- * (so bootloader always uses 2kbytes flash)
*/
-#if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__) || defined (__AVR_ATmega32__)
+#if defined (__AVR_ATmega8535__) || defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega162__) || defined (__AVR_ATmega32__)
-#if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
+#if defined (__AVR_ATmega8535__) || defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
#if (BOOTLOADER_ADDRESS != 0x1800)
#error BOOTLOADER_ADDRESS!=0x1800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
#endif
+#elif defined (__AVR_ATmega16__) || defined (__AVR_ATmega162__)
+ #if (BOOTLOADER_ADDRESS != 0x3800)
+ #error BOOTLOADER_ADDRESS!=0x3800, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
+ #endif
#elif defined (__AVR_ATmega32__)
#if (BOOTLOADER_ADDRESS != 0x7000)
#error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
-#elif defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__) || defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__) || defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__) || defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
+#elif defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__) || defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__) || defined (__AVR_ATmega640__) || defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__) || defined (__AVR_ATmega1280__) || defined (__AVR_ATmega1281__) || defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__) || defined (__AVR_ATmega2560__) || defined (__AVR_ATmega2561__)
#if defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
#if (BOOTLOADER_ADDRESS != 0x3800)
#if (BOOTLOADER_ADDRESS != 0x7000)
#error BOOTLOADER_ADDRESS!=0x7000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
#endif
+#elif defined (__AVR_ATmega640__)
+ #if (BOOTLOADER_ADDRESS != 0xE000)
+ #error BOOTLOADER_ADDRESS!=0xE000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
+ #endif
#elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
#if (BOOTLOADER_ADDRESS != 0xE000)
#error BOOTLOADER_ADDRESS!=0xE000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
#endif
+#elif defined (__AVR_ATmega1280__)
+ #if (BOOTLOADER_ADDRESS != 0x1E000)
+ #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
+ #endif
+#elif defined (__AVR_ATmega1281__)
+ #if (BOOTLOADER_ADDRESS != 0x1E000)
+ #error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
+ #endif
#elif defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
#if (BOOTLOADER_ADDRESS != 0x1E000)
#error BOOTLOADER_ADDRESS!=0x1E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
#endif
+#elif defined (__AVR_ATmega2560__)
+ #if (BOOTLOADER_ADDRESS != 0x3E000)
+ #error BOOTLOADER_ADDRESS!=0x3E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
+ #endif
+#elif defined (__AVR_ATmega2561__)
+ #if (BOOTLOADER_ADDRESS != 0x3E000)
+ #error BOOTLOADER_ADDRESS!=0x3E000, on current MCU "funcaddr___bootloader__do_spm" might be currupted - please edit spminterface.h for nonstandard use
+ #endif
#else
#error undefined device selection - this should not happen!
#endif
#endif
#endif
-
\ No newline at end of file
+