Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

code clean-up

update to Optiboot 4.5
  • Loading branch information...
commit 4d223c50b5f343ebb376930fefd2d1e9f7dbec70 1 parent 42d2c4a
@awatterott awatterott authored
Showing with 125 additions and 21 deletions.
  1. +118 −14 optiboot/optiboot.c
  2. +7 −7 optiboot/pin_defs.h
View
132 optiboot/optiboot.c
@@ -5,6 +5,9 @@
/* */
/* Arduino-maintained version */
/* http://code.google.com/p/arduino/ */
+/* It is the intent that changes not relevant to the */
+/* Arduino production envionment get moved from the */
+/* optiboot project to the arduino project in "lumps." */
/* */
/* Heavily optimised bootloader that is faster and */
/* smaller than the Arduino standard bootloader */
@@ -29,11 +32,16 @@
/* ATmega168 based devices (Diecimila etc) */
/* ATmega328P based devices (Duemilanove etc) */
/* */
+/* Beta test (believed working.) */
+/* ATmega8 based devices (Arduino legacy) */
+/* ATmega328 non-picopower devices */
+/* ATmega644P based devices (Sanguino) */
+/* ATmega1284P based devices */
+/* */
/* Alpha test */
/* ATmega1280 based devices (Arduino Mega) */
/* */
/* Work in progress: */
-/* ATmega644P based devices (Sanguino) */
/* ATtiny84 based devices (Luminet) */
/* */
/* Does not support: */
@@ -113,6 +121,10 @@
/* Bootloader timeout period, in milliseconds. */
/* 500,1000,2000,4000,8000 supported. */
/* */
+/* UART: */
+/* UART number (0..n) for devices with more than */
+/* one hardware uart (644P, 1284P, etc) */
+/* */
/**********************************************************/
/**********************************************************/
@@ -132,6 +144,26 @@
/**********************************************************/
/* Edit History: */
/* */
+/* Mar 2012 */
+/* 4.5 WestfW: add infrastructure for non-zero UARTS. */
+/* 4.5 WestfW: fix SIGNATURE_2 for m644 (bad in avr-libc) */
+/* Jan 2012: */
+/* 4.5 WestfW: fix NRWW value for m1284. */
+/* 4.4 WestfW: use attribute OS_main instead of naked for */
+/* main(). This allows optimizations that we */
+/* count on, which are prohibited in naked */
+/* functions due to PR42240. (keeps us less */
+/* than 512 bytes when compiler is gcc4.5 */
+/* (code from 4.3.2 remains the same.) */
+/* 4.4 WestfW and Maniacbug: Add m1284 support. This */
+/* does not change the 328 binary, so the */
+/* version number didn't change either. (?) */
+/* June 2011: */
+/* 4.4 WestfW: remove automatic soft_uart detect (didn't */
+/* know what it was doing or why.) Added a */
+/* check of the calculated BRG value instead. */
+/* Version stays 4.4; existing binaries are */
+/* not changed. */
/* 4.4 WestfW: add initialization of address to keep */
/* the compiler happy. Change SC'ed targets. */
/* Return the SW version via READ PARAM */
@@ -185,16 +217,35 @@ asm(".section .text\n");
#elif F_CPU >= 128000L
#define BAUD_RATE 4800L // Good for 128kHz internal RC
#else
-#define BAUD_RATE 1200L // Good even at 32768Hz
+#define BAUD_RATE 1200L // Good even at 32768Hz
+#endif
#endif
+
+#ifndef UART
+#define UART 0
#endif
+#if 0
/* Switch in soft UART for hard baud rates */
+/*
+ * I don't understand what this was supposed to accomplish, where the
+ * constant "280" came from, or why automatically (and perhaps unexpectedly)
+ * switching to a soft uart is a good thing, so I'm undoing this in favor
+ * of a range check using the same calc used to config the BRG...
+ */
#if (F_CPU/BAUD_RATE) > 280 // > 57600 for 16MHz
#ifndef SOFT_UART
#define SOFT_UART
#endif
#endif
+#else // 0
+#if (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 > 250
+#error Unachievable baud rate (too slow) BAUD_RATE
+#endif // baud rate slow check
+#if (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 < 3
+#error Unachievable baud rate (too fast) BAUD_RATE
+#endif // baud rate fastn check
+#endif
/* Watchdog settings */
#define WATCHDOG_OFF (0)
@@ -231,6 +282,21 @@ void uartDelay() __attribute__ ((naked));
#endif
void appStart() __attribute__ ((naked));
+/*
+ * NRWW memory
+ * Addresses below NRWW (Non-Read-While-Write) can be programmed while
+ * continuing to run code from flash, slightly speeding up programming
+ * time. Beware that Atmel data sheets specify this as a WORD address,
+ * while optiboot will be comparing against a 16-bit byte address. This
+ * means that on a part with 128kB of memory, the upper part of the lower
+ * 64k will get NRWW processing as well, even though it doesn't need it.
+ * That's OK. In fact, you can disable the overlapping processing for
+ * a part entirely by setting NRWWSTART to zero. This reduces code
+ * space a bit, at the expense of being slightly slower, overall.
+ *
+ * RAMSTART should be self-explanatory. It's bigger on parts with a
+ * lot of peripheral registers.
+ */
#if defined(__AVR_ATmega168__)
#define RAMSTART (0x100)
#define NRWWSTART (0x3800)
@@ -240,6 +306,12 @@ void appStart() __attribute__ ((naked));
#elif defined (__AVR_ATmega644P__)
#define RAMSTART (0x100)
#define NRWWSTART (0xE000)
+// correct for a bug in avr-libc
+#undef SIGNATURE_2
+#define SIGNATURE_2 0x0A
+#elif defined (__AVR_ATmega1284P__)
+#define RAMSTART (0x100)
+#define NRWWSTART (0xE000)
#elif defined(__AVR_ATtiny84__)
#define RAMSTART (0x100)
#define NRWWSTART (0x0000)
@@ -260,6 +332,37 @@ void appStart() __attribute__ ((naked));
#define wdtVect (*(uint16_t*)(RAMSTART+SPM_PAGESIZE*2+6))
#endif
+/*
+ * Handle devices with up to 4 uarts (eg m1280.) Rather inelegantly.
+ * Note that mega8 still needs special handling, because ubrr is handled
+ * differently.
+ */
+#if UART == 0
+# define UART_SRA UCSR0A
+# define UART_SRB UCSR0B
+# define UART_SRC UCSR0C
+# define UART_SRL UBRR0L
+# define UART_UDR UDR0
+#elif UART == 1
+# define UART_SRA UCSR1A
+# define UART_SRB UCSR1B
+# define UART_SRC UCSR1C
+# define UART_SRL UBRR1L
+# define UART_UDR UDR1
+#elif UART == 2
+# define UART_SRA UCSR2A
+# define UART_SRB UCSR2B
+# define UART_SRC UCSR2C
+# define UART_SRL UBRR2L
+# define UART_UDR UDR2
+#elif UART == 3
+# define UART_SRA UCSR3A
+# define UART_SRB UCSR3B
+# define UART_SRC UCSR3C
+# define UART_SRL UBRR3L
+# define UART_UDR UDR3
+#endif
+
/* main program starts here */
int main(void) {
uint8_t ch;
@@ -303,10 +406,10 @@ int main(void) {
UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0); // config USART; 8N1
//UBRRL = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );
#else
- UCSR0A = _BV(U2X0); //Double speed mode USART0
- UCSR0B = _BV(RXEN0) | _BV(TXEN0);
- UCSR0C = _BV(UCSZ00) | _BV(UCSZ01);
- //UBRR0L = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );
+ UART_SRA = _BV(U2X0); //Double speed mode USART0
+ UART_SRB = _BV(RXEN0) | _BV(TXEN0);
+ UART_SRC = _BV(UCSZ00) | _BV(UCSZ01);
+ //UART_SRL = (uint8_t)( (F_CPU + BAUD_RATE * 4L) / (BAUD_RATE * 8L) - 1 );
#endif
#endif
@@ -495,7 +598,8 @@ int main(void) {
putch(ch);
} while (--length);
#else
-#ifdef __AVR_ATmega1280__
+#ifdef RAMPZ
+// Since RAMPZ should already be set, we need to use EPLM directly.
// do putch(pgm_read_byte_near(address++));
// while (--length);
do {
@@ -520,7 +624,7 @@ int main(void) {
putch(SIGNATURE_1);
putch(SIGNATURE_2);
}
- else if (ch == 'Q') {
+ else if (ch == STK_LEAVE_PROGMODE) { /* 'Q' */
// Adaboot no-wait mod
watchdogConfig(WATCHDOG_16MS);
verifySpace();
@@ -535,8 +639,8 @@ int main(void) {
void putch(char ch) {
#ifndef SOFT_UART
- while (!(UCSR0A & _BV(UDRE0)));
- UDR0 = ch;
+ while (!(UART_SRA & _BV(UDRE0)));
+ UART_UDR = ch;
#else
__asm__ __volatile__ (
" com %[ch]\n" // ones complement, carry set
@@ -599,9 +703,9 @@ uint8_t getch(void) {
"r25"
);
#else
- while(!(UCSR0A & _BV(RXC0)))
+ while(!(UART_SRA & _BV(RXC0)))
;
- if (!(UCSR0A & _BV(FE0))) {
+ if (!(UART_SRA & _BV(FE0))) {
/*
* A Framing Error indicates (probably) that something is talking
* to us at the wrong bit rate. Assume that this is because it
@@ -613,7 +717,7 @@ uint8_t getch(void) {
watchdogReset();
}
- ch = UDR0;
+ ch = UART_UDR;
#endif
#ifdef LED_DATA_FLASH
@@ -628,7 +732,7 @@ uint8_t getch(void) {
}
#ifdef SOFT_UART
-// AVR350 equation: #define UART_B_VALUE (((F_CPU/BAUD_RATE)-23)/6)
+// AVR305 equation: #define UART_B_VALUE (((F_CPU/BAUD_RATE)-23)/6)
// Adding 3 to numerator simulates nearest rounding for more accurate baud rates
#define UART_B_VALUE (((F_CPU/BAUD_RATE)-20)/6)
#if UART_B_VALUE > 255
View
14 optiboot/pin_defs.h
@@ -17,13 +17,13 @@
#if defined(__AVR_ATmega8__)
//Name conversion R.Wiersma
- #define UCSR0A UCSRA
- #define UDR0 UDR
- #define UDRE0 UDRE
- #define RXC0 RXC
- #define FE0 FE
- #define TIFR1 TIFR
- #define WDTCSR WDTCR
+ #define UCSR0A UCSRA
+ #define UDR0 UDR
+ #define UDRE0 UDRE
+ #define RXC0 RXC
+ #define FE0 FE
+ #define TIFR1 TIFR
+ #define WDTCSR WDTCR
#endif
/* Luminet support */
Please sign in to comment.
Something went wrong with that request. Please try again.