Hi
im using a mega328p for my project
and chip45boot2 bootloader.
The code works just fine without the bootloader,
but when a have the bootloader in the flash the main program gets irregularly weird errors, or the mega just hangs.
i take it from the top because its the first time for me dealing with bootloader in the code.
i use my AVRISPMK2 to download
chip45boot2_atmega328p_uart0_v2.53.hex from chip45.com
with Fuses set to
BODLEVEL = 4,3V
BOOTSZ = size=2048words and start=$3800
BOOTRST = checked
Ext Crystal 16K +65ms
i running the Mega328p at 20Mhz
is there a magic settings somewhere that a missed, so the bootloader gets at the right part of the flash ?
what a get is that de bootloader is in the end of the flash.
is this done by defult??
i have been working with this problem quite a while now, read others questions and problems, googled my as of :D
the bootloader programing seams to be works fine
i do a reset of some registers.
then to weird part.
i get the code running with som miner defekts,
tex it sends a ? infront of rs232 output strngs
? MCU Reset Update RESPONSOK
when a do some change in the code the mega gets weird.
like when a insert a Delay
PortInit(); // initilize ports SerialInit(); // initilize serial Delay(50); send_command("MCU Reset",1);
i have a function that returns the version as a output on the serial port.
the respons from a function change value "SWVersionMinor"
from
RESPONSOK
41
To
RESPONSOK
14
other changes makes the mega freeze or just acting strange, working for a while then just stops.
and then a just erase and flash the program without the bootloader and its back to normal, working fine.
Pleas give me some hint or point me at the right direction
Here is the main code
// Main includes #include "StepMaster328.h" /*--------------------------------------------------------------------------------------------------*/ volatile unsigned int received_value; /*-------------------------------------------------------------------------------------------------- Private function prototypes --------------------------------------------------------------------------------------------------*/ // Function prototypes are mandatory otherwise the compiler generates unreliable code. void Delay(long int count); void SerialInit(); void send_command(char * command, char newline); void PortInit(); void Load_settings(); void PortSettingsInit (unsigned char PortSetting, unsigned char PortNumber, unsigned char PortFunction, unsigned int Data1, unsigned int data2); void ScanPortFunctions (); void ResetMCURegister(); void Activate_Function(); void init_Timer1(void); void init_Timer2(void); void init_ISR_PortA(void); void Init_AD(void); void Write_eeprom_byte(unsigned char addres, unsigned char data); void SetBisySignal(); ISR(TIMER1_COMPA_vect); ISR(PCINT0_vect); ISR(PCINT2_vect); void ItoH(unsigned int num, char *str); int main (void) { ResetMCURegister(); SWVersionMinor = 0x29; // 41 PortInit(); // initilize ports SerialInit(); // initilize serial rs232 send_command("MCU Reset",1);// Interrupt init init_Timer1(); // timer interrupt enable init_Timer2(); init_ISR_PortA();// PortA interrupt enable sei(); // Enable global interrupts PortCounter = 0x00; // Resets value received_value = 0x00; Init_AD(); for (;;) // Loop forever { Delay(200); // Wait PORTC ^= LED; // Toggle status led } ----------------------------------------------------------------------------------------------- Target Timer Count = (1 / Target Frequency) / (Prescale / Input Frequency) Target Frequency = time fore counter = 1/20 of a secund = 20 Input Frequency = system freq Target Timer Count = (1 / 1 ) / (Prescale / Input Frequency) CS12 CS11 CS10 prescale 0 0 1 clk/1 0 1 0 clk/8 0 1 1 clk/64 1 0 0 clk/256 1 0 1 clk/1024 --------------------------------------------------------------------------------------------------*/ void init_Timer1(void) { TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode TIMSK1 |= (0 << OCIE1A); // disable CTC interrupt OCR1A = 0x01F0; // TCCR1B |= ((1 << CS10) |(0 << CS10) | (1 << CS11)); // Start timer at Fcpu/8 TIMSK1 |= (1 << OCIE1A); // Enable CTC interrupt } /*-------------------------------------------------------------------------------------------------- Initialize PWM Timer interrupt --------------------------------------------------------------------------------------------------*/ void init_Timer2(void) { TCCR2A = 0x91; //|= (1<< COM1A1) | (1<< COM1B1) | (1<< COM1A0) | (1<< COM1B0) | (1<< WGM10)| (1<< WGM11) ;// 10 bit PWM phase correct TCCR2B |= (1<< CS20); // no prescaling OCR2A = 40; } /*-------------------------------------------------------------------------------------------------- Initialize External pin interrupt --------------------------------------------------------------------------------------------------*/ void init_ISR_PortA(void) { // interrupt sense control // on toggle EICRA |= (0<<ISC01) | (1<<ISC00); // turn interrupt on portA activ PCICR |= (1<<PCIE2) | (1<<PCIE0); } /*-------------------------------------------------------------------------------------------------- Initialize Register values --------------------------------------------------------------------------------------------------*/ void ResetMCURegister() { EECR = 0x00; // The EEPROM Control Register GPIOR0 = 0x00; // General Purpose I/O Register 0 GPIOR1 = 0x00; // General Purpose I/O Register 1 GPIOR2 = 0x00; // General Purpose I/O Register 2 // Disable change of Interrupt Vectors // MCUCR &= ~IVCE; // Move interrupts to Normal Flash section // MCUCR &= ~IVSEL; // PUD: Pull-up Disable // MCUCR &= ~PUD; // EICRA = 0x00; //External Interrupt Control Register A // EIMSK = 0x00; //External Interrupt Mask Register // EIFR = 0x00; //External Interrupt Flag Register PCICR = 0x00; //Pin Change Interrupt Control Register PCIFR = 0x00; //Pin Change Interrupt Flag Register PCMSK0 = 0x00; //Pin Change Mask Register 0 PCMSK1 = 0x00; //Pin Change Mask Register 1 PCMSK2 = 0x00; //Pin Change Mask Register 2 PORTB = 0x00; // Port Data DDRB = 0x00; PORTC = 0x00; DDRC = 0x00; PORTD = 0x00; DDRD = 0x00; TCCR0A = 0x00; // Timer 0 TCCR0B = 0x00; // TCCR0C = 0x00; TIMSK0 = 0x00; TCNT0 = 0x00; OCR0A = 0x00; OCR0B = 0x00; TIFR0 = 0x00; TCCR1A = 0x00; // Timer 1 TCCR1B = 0x00; TCCR1C = 0x00; TIMSK1 = 0x00; TCNT1H = 0x00; TCNT1L = 0x00; OCR1AH = 0x00; OCR1AL = 0x00; OCR1BH = 0x00; OCR1BL = 0x00; ICR1H = 0x00; ICR1L = 0x00; TIFR1 = 0x00; GTCCR = 0x00; //General Timer/Counter Control Register TCCR2A = 0x00; // Timer 3 TCCR2B = 0x00; // TCCR2C = 0x00; TIMSK2 = 0x00; TCNT2 = 0x00; OCR2A = 0x00; OCR2B = 0x00; TIFR2 = 0x00; ASSR = 0x00; UDR0 = 0x00; // USART I/O Data Register n UCSR0A = 0x00; // USART Control and Status Register 0 A UCSR0B = 0x00; // USART Control and Status Register 0 B UCSR0C = 0x00; // USART Control and Status Register 0 C UBRR0L = 0x00; // Baud Rate Registers UBRR0H = 0x00; // Baud Rate Registers ADCSRB = 0x00; //ADC Control and Status Register B ACSR = 0x00; //Analog Comparator Control and Status Register DIDR1 = 0x00; //Digital Input Disable Register 1 DIDR0 = 0x00; //Digital Input Disable Register 0 ADMUX = 0x00; // ADC Multiplexer Selection Register ADCSRA = 0x00; // ADC Control and Status Register A } /*-------------------------------------------------------------------------------------------------- End of file. --------------------------------------------------------------------------------------------------*/