Skip to content
Snippets Groups Projects
Commit aef971c6 authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Added the patches and the necessary modifications to the Makefile to download,...

Added the patches and the necessary modifications to the Makefile to download, patch and program the bootloader.
parent 70e6f01b
No related branches found
No related tags found
No related merge requests found
......@@ -56,10 +56,27 @@ $(PROJECT).elf: $(OBJS)
$(CC) -c $(CFLAGS) -I$(INCLUDE_DIR) -o $@ $<
download: $(MAIN_OUT_HEX)
avrdude -c avrisp2 -P usb -p m128 -U flash:w:$(PROJECT).hex
avrdude -c avrisp2 -P usb -p m128 -U eeprom:w:$(PROJECT)_eeprom.hex
avrdude -c avr109 -P /dev/ttyUSB0 -b 19200 -p m128 -U flash:w:$(PROJECT).hex
avrdude -c avr109 -P /dev/ttyUSB0 -b 19200 -p m128 -U eeprom:w:$(PROJECT)_eeprom.hex
bootloader: bootloader_hex
-$(MAKE) -C remote-controlled-socket/uc/avrprog_boot_v0_85/ program
-rm -rf remote-controlled-socket
bootloader_hex:
@if [ ! -d "./remote-controlled-socket" ] ; then \
echo "AVR bootloader not found"; \
hg clone https://bitbucket.org/befi/remote-controlled-socket; \
patch remote-controlled-socket/uc/avrprog_boot_v0_85/main.c < patches/main.c.patch; \
patch remote-controlled-socket/uc/avrprog_boot_v0_85/makefile < patches/makefile.patch; \
patch remote-controlled-socket/uc/avrprog_boot_v0_85/chipdef.h < patches/chipdef.h.patch; \
cp patches/uart_usb.c remote-controlled-socket/uc/avrprog_boot_v0_85/; \
fi
$(MAKE) -C remote-controlled-socket/uc/avrprog_boot_v0_85/
clean:
rm $(PROJECT).*
rm $(PROJECT)_eeprom.*
rm $(OBJS)
-rm $(PROJECT).*
-rm $(PROJECT)_eeprom.*
-rm $(OBJS)
-rm src/*.lst
-rm -rf remote-controlled-socket
diff -r 407d6aac41dc uc/avrprog_boot_v0_85/chipdef.h
--- a/uc/avrprog_boot_v0_85/chipdef.h Sun Apr 01 13:47:42 2012 +0200
+++ b/uc/avrprog_boot_v0_85/chipdef.h Tue Nov 10 10:52:39 2015 +0100
@@ -12,11 +12,13 @@
#if defined (SPMCSR)
#define SPM_REG SPMCSR
-#elif defined (SPMCR)
+#else
+#if defined (SPMCR)
#define SPM_REG SPMCR
#else
#error "AVR processor does not provide bootloader support!"
#endif
+#endif
#define APP_END (FLASHEND - (BOOTSIZE * 2))
diff -r 407d6aac41dc uc/avrprog_boot_v0_85/main.c
--- a/uc/avrprog_boot_v0_85/main.c Sun Apr 01 13:47:42 2012 +0200
+++ b/uc/avrprog_boot_v0_85/main.c Tue Nov 10 10:52:26 2015 +0100
@@ -51,8 +51,8 @@
#endif
/* UART Baudrate */
- #define BAUDRATE 19200
// #define BAUDRATE 19200
+#define BAUDRATE 19200
//#define BAUDRATE 115200
/* use "Double Speed Operation" */
@@ -167,6 +167,7 @@
#include <avr/interrupt.h>
#include <util/delay.h>
+#include "uart_usb.h"
#include "chipdef.h"
uint8_t gBuffer[SPM_PAGESIZE];
@@ -180,14 +181,20 @@
static void sendchar(uint8_t data)
{
- while (!(UART_STATUS & (1<<UART_TXREADY)));
- UART_DATA = data;
+// while (!(UART_STATUS & (1<<UART_TXREADY)));
+// UART_DATA = data;
+ uart_usb_send_byte(data);
}
static uint8_t recvchar(void)
{
- while (!(UART_STATUS & (1<<UART_RXREADY)));
- return UART_DATA;
+ uint8_t data;
+
+// while (!(UART_STATUS & (1<<UART_RXREADY)));
+// return UART_DATA;
+ while(!uart_usb_is_data_available());
+ uart_usb_receive_byte(&data);
+ return data;
}
static inline void eraseFlash(void)
@@ -205,7 +212,7 @@
static inline void recvBuffer(pagebuf_t size)
{
pagebuf_t cnt;
- uint8_t *tmp = gBuffer;
+ uint8_t *tmp = gBuffer;
for (cnt = 0; cnt < sizeof(gBuffer); cnt++) {
*tmp++ = (cnt < size) ? recvchar() : 0xFF;
@@ -331,11 +338,27 @@
static void (*jump_to_app)(void) = 0x0000;
+static void enter_app(void)
+{
+ cli();
+ MCUCR = (1<<IVCE); //Enable change of interrupt vectors
+ MCUCR = (0<<IVSEL); //Move interrupts to start flash section
+ sei();
+ jump_to_app();
+}
+
int main(void)
{
uint16_t address = 0;
uint8_t device = 0, val;
+ MCUCR = (1<<IVCE); //Enable change of interrupt vectors
+ MCUCR = (1<<IVSEL); //Move interrupts to boot flash section
+
+ uart_usb_init();
+ uart_usb_set_baudrate(19200);
+ uart_usb_start();
+
#ifdef DISABLE_WDT_AT_STARTUP
#ifdef WDT_OFF_SPECIAL
#warning "using target specific watchdog_off"
@@ -346,24 +369,25 @@
wdt_disable();
#endif
#endif
+ sei();// enable interrupts for the software serial port.
#ifdef START_POWERSAVE
uint8_t OK = 1;
#endif
- BLDDR &= ~(1<<BLPNUM); // set as Input
- BLPORT |= (1<<BLPNUM); // Enable pullup
+// BLDDR &= ~(1<<BLPNUM); // set as Input
+// BLPORT |= (1<<BLPNUM); // Enable pullup
// Set baud rate
- UART_BAUD_HIGH = (UART_CALC_BAUDRATE(BAUDRATE)>>8) & 0xFF;
- UART_BAUD_LOW = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF);
+// UART_BAUD_HIGH = (UART_CALC_BAUDRATE(BAUDRATE)>>8) & 0xFF;
+// UART_BAUD_LOW = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF);
#ifdef UART_DOUBLESPEED
- UART_STATUS = ( 1<<UART_DOUBLE );
+// UART_STATUS = ( 1<<UART_DOUBLE );
#endif
- UART_CTRL = UART_CTRL_DATA;
- UART_CTRL2 = UART_CTRL2_DATA;
+// UART_CTRL = UART_CTRL_DATA;
+// UART_CTRL2 = UART_CTRL2_DATA;
#if defined(START_POWERSAVE)
/*
@@ -376,10 +400,11 @@
if ((BLPIN & (1<<BLPNUM))) {
// jump to main app if pin is not grounded
BLPORT &= ~(1<<BLPNUM); // set to default
-#ifdef UART_DOUBLESPEED
- UART_STATUS &= ~( 1<<UART_DOUBLE );
-#endif
- jump_to_app(); // Jump to application sector
+//#ifdef UART_DOUBLESPEED
+// UART_STATUS &= ~( 1<<UART_DOUBLE );
+//#endif
+// jump_to_app(); // Jump to application sector
+ enter_app();
} else {
val = recvchar();
@@ -405,10 +430,11 @@
if ((BLPIN & (1<<BLPNUM))) {
// jump to main app if pin is not grounded
BLPORT &= ~(1<<BLPNUM); // set to default
-#ifdef UART_DOUBLESPEED
- UART_STATUS &= ~( 1<<UART_DOUBLE );
-#endif
- jump_to_app(); // Jump to application sector
+//#ifdef UART_DOUBLESPEED
+// UART_STATUS &= ~( 1<<UART_DOUBLE );
+//#endif
+// jump_to_app(); // Jump to application sector
+ enter_app();
}
#elif defined(START_WAIT)
@@ -416,13 +442,17 @@
uint16_t cnt = 0;
while (1) {
- if (UART_STATUS & (1<<UART_RXREADY))
- if (UART_DATA == START_WAIT_UARTCHAR)
+// if (UART_STATUS & (1<<UART_RXREADY))
+// if (UART_DATA == START_WAIT_UARTCHAR)
+// break;
+ if(uart_usb_is_data_available())
+ if(recvchar()==START_WAIT_UARTCHAR)
break;
if (cnt++ >= WAIT_VALUE) {
BLPORT &= ~(1<<BLPNUM); // set to default
- jump_to_app(); // Jump to application sector
+// jump_to_app(); // Jump to application sector
+ enter_app();
}
_delay_ms(10);
diff -r 407d6aac41dc uc/avrprog_boot_v0_85/makefile
--- a/uc/avrprog_boot_v0_85/makefile Sun Apr 01 13:47:42 2012 +0200
+++ b/uc/avrprog_boot_v0_85/makefile Tue Nov 10 10:52:17 2015 +0100
@@ -37,7 +37,7 @@
# user defined values
# MCU name
-MCU = atmega8
+# MCU = atmega8
## MCU = atmega16
## MCU = atmega162
## MCU = atmega169
@@ -46,7 +46,7 @@
## MCU = atmega64
## MCU = atmega644
## MCU = atmega644p
-## MCU = atmega128
+MCU = atmega128
## MCU = at90can128
################## BOOTLOADER ######################
@@ -59,15 +59,16 @@
#/* Select Boot Size in Words (select one, comment out the others) */
## NO! BOOTSIZE=128
## NO! BOOTSIZE=256
-BOOTSIZE=512
-##BOOTSIZE=1024
+## BOOTSIZE=512
+## BOOTSIZE=1024
## BOOTSIZE=2048
+BOOTSIZE=4096
# /* Select if bootloader should include the inverrupt-vectors
# when selecting 'no' here, the bootloader must not use
# any interrupts and the modified linker-scripts are used. */
-##BOOTINTVEC=yes
-BOOTINTVEC=no
+BOOTINTVEC=yes
+## BOOTINTVEC=no
##
ifeq ($(MCU), atmega8)
@@ -266,7 +267,7 @@
# List C source files here. (C dependencies are automatically generated.)
-SRC = $(TARGET).c
+SRC = $(TARGET).c uart_usb.c
# List Assembler source files here.
@@ -292,7 +293,7 @@
# List any extra directories to look for include files here.
# Each directory must be seperated by a space.
-EXTRAINCDIRS =
+EXTRAINCDIRS = ../../../include
# Compiler flag to set the C Standard level.
@@ -303,7 +304,7 @@
CSTANDARD = -std=gnu99
# Place -D or -U options here
-CDEFS = -DBOOTSIZE=$(BOOTSIZE)
+CDEFS = -DBOOTSIZE=$(BOOTSIZE) -DF_CPU=16000000
# Place -I options here
CINCS =
@@ -405,7 +406,7 @@
AVRDUDE_PORT = usb # programmer connected to serial device
#AVRDUDE_PORT = /dev/ttyS0 # programmer connected to serial device
-AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex -U lfuse:w:0xe4:m -U hfuse:w:0xda:m
+AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex -U lfuse:w:0xff:m -U hfuse:w:0xd8:m
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
#include <avr/io.h>
#include <avr/interrupt.h>
#include "uart_usb.h"
#define UART_USB_DEFAULT_BAUDRATE 9600
#define UART_USB_RX_BUFFER_SIZE 0x20
#define UART_USB_TX_PORT PORTD
#define UART_USB_TX_DDR DDRD
#define UART_USB_TX_PIN PD5
#define UART_USB_RX_PORT PORTD
#define UART_USB_RX_DDR DDRD
#define UART_USB_RX_PORTIN PIND
#define UART_USB_RX_PIN PD4
// uartsw transmit status and data variables
volatile uint8_t uart_usb_tx_busy;
volatile uint8_t uart_usb_tx_data;
volatile uint8_t uart_usb_tx_bit_num;
// baud rate common to transmit and receive
volatile uint16_t uart_usb_baudrate_div;
// uartsw receive status and data variables
volatile uint8_t uart_usb_rx_busy;
volatile uint8_t uart_usb_rx_data;
volatile uint8_t uart_usb_rx_bit_num;
/* rx buffer */
unsigned char uart_usb_rx_data_buffer[UART_USB_RX_BUFFER_SIZE];
unsigned char uart_usb_rx_read_ptr;
unsigned char uart_usb_rx_write_ptr;
unsigned char uart_usb_rx_num_data;
/* private functions */
/* interrupt handlers */
ISR(TIMER1_CAPT_vect)//RX
{
uint16_t capture;
// clear ICP interrupt flag
TIFR|=(1<<ICF1);
if(!uart_usb_rx_busy)
{
// disable input capture interrutps
TIMSK&=~(1<<TICIE1);
capture=TCNT1+uart_usb_baudrate_div + uart_usb_baudrate_div/2;
// schedule data bit sampling 1.5 bit periods from now
OCR1BH=(capture>>8)&0xFF;
OCR1BL=capture&0xFF;
// clear OC1B interrupt flag
TIFR|=(1<<OCF1B);
// enable OC1B interrupt
TIMSK|=(1<<OCIE1B);
// set start bit flag
uart_usb_rx_busy = 0x01;
// reset bit counter
uart_usb_rx_bit_num = 0;
// reset data
uart_usb_rx_data = 0;
}
}
ISR(TIMER1_COMPB_vect)//RX
{
uint16_t capture;
// clear OC1B interrupt flag
TIFR|=(1<<OCF1B);
// shift data byte to make room for new bit
uart_usb_rx_data = uart_usb_rx_data>>1;
if(UART_USB_RX_PORTIN&(1<<UART_USB_RX_PIN))
uart_usb_rx_data |= 0x80;
// increment bit counter
uart_usb_rx_bit_num++;
// schedule next bit sample
capture=OCR1B+uart_usb_baudrate_div;
OCR1BH=(capture>>8)&0xFF;
OCR1BL=capture&0xFF;
// check if we have a full byte
if(uart_usb_rx_bit_num >= 8)
{
// save data in receive buffer
uart_usb_rx_data_buffer[uart_usb_rx_write_ptr]=uart_usb_rx_data;
uart_usb_rx_write_ptr=(uart_usb_rx_write_ptr+1)%UART_USB_RX_BUFFER_SIZE;
uart_usb_rx_num_data++;
// disable OC1B interrupt
TIMSK&=~(1<<OCIE1B);
// enable ICP interrupt
TIMSK|=(1<<TICIE1);
// clear start bit flag
uart_usb_rx_busy = 0x00;
}
}
ISR(TIMER1_COMPA_vect)//TX
{
uint16_t capture;
// clear OC1B interrupt flag
TIFR|=(1<<OCF1A);
if(uart_usb_tx_bit_num)
{
if(uart_usb_tx_bit_num>1)
{
// transmit data bits (inverted, LSB first)
if((uart_usb_tx_data&0x01))
UART_USB_TX_PORT|=(1<<UART_USB_TX_PIN);
else
UART_USB_TX_PORT&=~(1<<UART_USB_TX_PIN);
// shift bits down
uart_usb_tx_data = uart_usb_tx_data>>1;
}
else
UART_USB_TX_PORT|=(1<<UART_USB_TX_PIN);
// schedule the next bit
capture=OCR1A+uart_usb_baudrate_div;
OCR1AH=(capture>>8)&0xFF;
OCR1AL=capture&0xFF;
// count down
uart_usb_tx_bit_num--;
}
else
{
// enable OC1A interrupt
TIMSK&=~(1<<OCIE1A);
uart_usb_tx_busy = 0x00;
}
}
/* public functions */
void uart_usb_init(void)
{
// configure GPIO ports
UART_USB_TX_DDR|=(1<<UART_USB_TX_PIN);// output
UART_USB_RX_DDR&=~(1<<UART_USB_RX_PIN);//input
// set input to 0
UART_USB_RX_PORT&=~(1<<UART_USB_RX_PIN);
// initialize baud rate
uart_usb_set_baudrate(UART_USB_DEFAULT_BAUDRATE);
// setup the transmitter
uart_usb_tx_busy = 0x00;
TIMSK&=~(1<<OCIE1A);
// setup the receiver
uart_usb_rx_busy = 0x00;
TIMSK&=~(1<<OCIE1B);
// trigger on falling edge
TCCR1B&=~(1<<ICES1);
// enable ICP interrupt
TIMSK|=(1<<TICIE1);
/* initlaize buffer variables */
uart_usb_rx_read_ptr=0;
uart_usb_rx_write_ptr=0;
uart_usb_rx_num_data=0;
}
void uart_usb_start(void)
{
TIMSK&=~(1<<OCIE1A);
TIMSK&=~(1<<OCIE1B);
// clear ICP interrupt flag
TIFR|=(1<<ICF1);
TIMSK|=(1<<TICIE1);
}
void uart_usb_stop(void)
{
// disable interrupts
TIMSK&=~(1<<OCIE1A);
TIMSK&=~(1<<OCIE1B);
TIMSK&=~(1<<TICIE1);
}
void uart_usb_set_baudrate(uint32_t baudrate)
{
TCCR1B&=0xF8;
TCCR1B|=0x01;
// calculate division factor for requested baud rate, and set it
uart_usb_baudrate_div = (uint16_t)((F_CPU+(baudrate/2L))/(baudrate*1L));
}
void uart_usb_send_byte(uint8_t data)
{
uint16_t capture;
// wait until uart is ready
while(uart_usb_tx_busy);
// set busy flag
uart_usb_tx_busy = 0x01;
// save data
uart_usb_tx_data = data;
// set number of bits (+1 for stop bit)
uart_usb_tx_bit_num = 9;
UART_USB_TX_PORT&=~(1<<UART_USB_TX_PIN);
capture=TCNT1+uart_usb_baudrate_div;
OCR1AH=(capture>>8)&0xFF;
OCR1AL=capture&0xFF;
// clear interrupts
TIFR|=(1<<OCF1A);
// enable OC1A interrupt
TIMSK|=(1<<OCIE1A);
}
uint8_t uart_usb_is_sending(void)
{
return uart_usb_tx_busy;
}
uint8_t uart_usb_receive_byte(uint8_t* data)
{
// make sure we have a receive buffer
if(uart_usb_rx_num_data>0)
{
*data=uart_usb_rx_data_buffer[uart_usb_rx_read_ptr];
uart_usb_rx_read_ptr=(uart_usb_rx_read_ptr+1)%UART_USB_RX_BUFFER_SIZE;
uart_usb_rx_num_data--;
// make sure we have data
return 0x01;
}
else
return 0x00;
}
uint8_t uart_usb_is_data_available(void)
{
if(uart_usb_rx_num_data>0)
return 0x01;
else
return 0x00;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment