weird BootLoader problem

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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.
--------------------------------------------------------------------------------------------------*/


  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you post your interrupt handlers too?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sure

the ? error on output string was UDR0 = 0x00;
if a removed that line then it was ok.

but it its still som char missing in som strings when a send somthing from MCU on serial.

and im testing back and fort with bootloader and without.

ISR.c


/*--------------------------------------------------------------------------------------------------
                                ISR File
--------------------------------------------------------------------------------------------------*/
// Main includes 
#include "ISR.h"  
/*--------------------------------------------------------------------------------------------------*/

void Comp_String(void);
void send_command(char * command, char newline);
void ItoH(unsigned int num, char *str);
int HtoI (unsigned int ch);
void PortSettingsInit (unsigned char PortSetting, unsigned char PortNumber, unsigned char PortFunction, unsigned int Data1, unsigned int data2);
void Activate_Function();

void SetBisySignal();

void Read_AD(void);


ISR(TIMER1_COMPA_vect);
ISR(PCINT0_vect);
ISR(PCINT2_vect);

/*--------------------------------------------------------------------------------------------------

  Name         :  RS232 Interuppt vector

  Description  :  triggers on RS232 activity


--------------------------------------------------------------------------------------------------*/
ISR(USART_RX_vect)
{


//	unsigned char ReceivedByte2;

//   	ReceivedByte2 = UDR0; // Fetch the recieved byte value into the variable "ByteReceived"
//   UDR0 = ReceivedByte2; // Echo back the received byte back to the computer



	unsigned int TempH;
	unsigned char Tid1;



//	PORTD |= RX232_CTS;								// Set CTS to low to indicate bisy


	temp_udr=UDR0;
	if(temp_udr == '$'){
		recived_bytes_PC=0;
		
	}
	else if(temp_udr == '\n')
	{
		

	
		// *********** Rensar gammal data
		Tid1 = recived_bytes_PC;
		while(Tid1<10)
		{
			received_label[Tid1] = 0;
			Tid1 = Tid1++;
		}
		//*******************************

		
	//	send_command(received_label,1);					
		

		received_value = ReadConvert(received_label);						// sparar värdet som är i slutet på komandoraden


	// ->send_command(HtoI(received_value),1);


		//ItoH(received_value,6);
	




		Comp_String();
		recived_bytes_PC = 0;	

	}
	else if(recived_bytes_PC<11){
		received_label[recived_bytes_PC]=temp_udr;
		recived_bytes_PC++;
		}
	else if(recived_bytes_PC>10) {
		send_command("StringError",1);
		recived_bytes_PC++;
		}





}
/*--------------------------------------------------------------------------------------------------
Timer interrupt

--------------------------------------------------------------------------------------------------*/

ISR(TIMER1_COMPA_vect)
{







		SetBisySignal();


		//**** Step Function

		if(Step == 0x01)							// If step enable Step at given Dir
		{
			if(Direction == 0x01)
			{
				if(P_HomeSens == 0x00)				// If home sene = true, stop motor at that direction
				{
				Step_Counter++;
				PORTB ^= A_STEP;
				}
			}
			else
			{
				if(N_HomeSens == 0x00)				// If home sene = true, stop motor at that direction
				{
				Step_Counter--;
				PORTB ^= A_STEP;
				}

			}
		
		}


		//**** +Continuous Function

		if((P_Continuous == 0x01) && (P_HomeSens == 0x00))		// If +Continuous enable Step at + Dir
		{
		Step_Counter++;
		PORTB |= A_DIR;								// Set Dir to Posetiv
		PORTB ^= A_STEP;							// Make a step

		}
		//**** -Continuous Function

		if((N_Continuous == 0x01) && (N_HomeSens == 0x00))		// If +Continuous enable Step at - Dir
		{
		Step_Counter--;
		PORTB &= ~A_DIR;								// Set Dir to Negativ
		PORTB ^= A_STEP;							// Make a step

		}

		//**** Get Home By count


		Read_AD();
		Step_Home = ADC_CH1 * 10;

		Home_Set = 0x01;


		if(Home_Set == 0x01)						// Get home by count
		{
					

							if(Step_Counter == Step_Home)			// Check if counter == home
							{
							Home_Set = 0x00;						// Done
							BisySignal = 0x00;						// Disable Bisy_Signal
							}
							else
							{
								if(Step_Counter < Step_Home)
								{
										if(P_HomeSens == 0x00)		// If P home sens is activ ABORT !! and set bisy to normal
										{
										Step_Counter++;
										PORTB |= A_DIR;
										PORTB ^= A_STEP;
										}
										else
										{
										Home_Set = 0x00;						// Done
										BisySignal = 0x00;						// Disable Bisy_Signal					
										}
								}
								else
								{

										if(N_HomeSens == 0x00)		// If N home sens is activ ABORT !! and set bisy to normal
										{
										Step_Counter--;
										PORTB &= ~A_DIR;
										PORTB ^= A_STEP;
										}
										else
										{
										Home_Set = 0x00;						// Done
										BisySignal = 0x00;						// Disable Bisy_Signal					
										}

								}
	
								
							}

				
		}


		//**** + Step with xxx steps

		if(P_Step_Set != 0x00)						// If P_step other then zero it still has steps to go
		{

				if(P_HomeSens == 0x00) 				// If P home sens is activ ABORT !! and set bisy to normal
				{

						P_Step_Set--;	

						Step_Counter++;
						PORTB |= A_DIR;
	
						PORTB ^= A_STEP;
		
							if(P_Step_Set == 0x00) 					// if it was the last step
							{
							BisySignal = 0x00;						// Disable Bisy_Signal
							}
				}
				else
				{
				P_Step_Set = 0x00;						// On Home sens activ END and reset 
				BisySignal = 0x00;						// Disable Bisy_Signal
				}
		}


		//**** - Step with xxx steps

		if(N_Step_Set != 0x00)						// If N_step other then zero it still has steps to go
		{

				if(N_HomeSens == 0x00)				// If N home sens is activ ABORT !! and set bisy to normal
				{

						N_Step_Set--;	

						Step_Counter--;
						PORTB &= ~A_DIR;
	
						PORTB ^= A_STEP;

							if(N_Step_Set == 0x00) 					// if it was the last step
							{
							BisySignal = 0x00;						// Disable Bisy_Signal
							}		
				}
				else
				{
				N_Step_Set = 0x00;						// On Home sens activ END and reset 
				BisySignal = 0x00;						// Disable Bisy_Signal
				}
		}
		




}
/*--------------------------------------------------------------------------------------------------
PortB interrupt

--------------------------------------------------------------------------------------------------*/
ISR(PCINT0_vect)
{
	
	ScanPortFunctions();
	Activate_Function();
//	ItoH(Microstep,6);
//	ItoH(OCR1A,6);
} 
/*--------------------------------------------------------------------------------------------------
PortD interrupt

--------------------------------------------------------------------------------------------------*/
ISR(PCINT2_vect)
{
	
	ScanPortFunctions();
	Activate_Function();
//	ItoH(Microstep,6);
//	ItoH(OCR1A,6);
} 


ISR.h

/*--------------------------------------------------------------------------------------------------
                                Uart Header File
--------------------------------------------------------------------------------------------------*/
// Main includes 
#include 
#include 
#include 
/*--------------------------------------------------------------------------------------------------*/


//   PC RX
volatile unsigned char temp_udr;
volatile unsigned char recived_bytes_PC;
volatile char received_label[10];
volatile unsigned int received_value;



//** Allegro krets

#define A_MS2					   			_BV(PORTC0)
#define A_MS1					   			_BV(PORTC1)
#define A_ENABLE					   		_BV(PORTB5)
#define A_DIR					   			_BV(PORTB4)
#define A_STEP					   			_BV(PORTB2)

#define LED					   				_BV(PORTC5)



//  Step counter 
volatile int Step_Counter;
volatile int Step_Home;


//Global Bisy signal
unsigned char BisySignal;

// ADC Channels
volatile int ADC_CH1;
volatile int ADC_CH2;

//***** 
volatile unsigned char Home;
volatile unsigned char HomeSens;
volatile unsigned char P_HomeSens;
volatile unsigned char N_HomeSens;
volatile unsigned char P_Step;
volatile unsigned char N_Step;
volatile unsigned char Step;
volatile unsigned char P_Continuous;
volatile unsigned char N_Continuous;
volatile unsigned char Direction;
volatile unsigned char Speed;
volatile unsigned char Microstep;
volatile unsigned char Current;
volatile unsigned char Stop_Emergency;
volatile unsigned char Reset_Emergency;
volatile unsigned char Wait_Untill;
volatile unsigned char Abort_IF_HL;
volatile unsigned char Current_Standby;


//***** 
volatile unsigned char Home_Set;
volatile unsigned char HomeSens_Set;
volatile unsigned char P_HomeSens_Set;
volatile unsigned char N_HomeSens_Set;
volatile unsigned int  P_Step_Set;
volatile unsigned int  N_Step_Set;
volatile unsigned char Step_Set;
volatile unsigned char P_Continuous_Set;
volatile unsigned char N_Continuous_Set;
volatile unsigned char Wait_Untill_Set;

Functions.c

/*--------------------------------------------------------------------------------------------------
                                Function Main File
--------------------------------------------------------------------------------------------------*/
// Main includes 
#include "Function.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 Comp_String(void);
void Activate_Function();
void ItoH(unsigned int num, char *str);
void Set_Microstep(unsigned char Data_P);

void Read_AD(void);

void Write_eeprom_int (unsigned int addres, unsigned int data);
void Write_eeprom_byte (unsigned int addres, unsigned char data);
/*--------------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

  Name         :  Main UART Functions 

  Description  :  compair incomming function from rs232

  Argument(s)  :  

--------------------------------------------------------------------------------------------------*/
void Comp_String(void)
{
unsigned char Data_Temp_1;





	if(!strncmp(received_label,"CUR",3))		// 
	{

			Read_AD();
			send_command("ADC_AVR",1);			// 
			ItoH(ADC_CH1,6);
			ItoH(ADC_CH2,6);


	}
	else if(!strncmp(received_label,"SAM",3)){ 	// 
				// 
		send_command("CBisy",1);
		ItoH(Microstep,6);
		ItoH(OCR1A,6);
	//	BisySignal = 0x00;				// Sets Bisy Signal Active

	}
	else if(!strncmp(received_label,"CHECK",5)){ // Alive Respons 

	
		send_command("RESPONSOK",1);

	}
	else if(!strncmp(received_label,"VERSI",5)){ // returns SW Version

		
		send_command(HtoI(SWVersionMinor),1);
		send_command("VERX:",1);
	

	}
	else if(!strncmp(received_label,"STEP",4)){ // Step Function
	
	Step = received_value;
	send_command("Step=",0);
	ItoH(Step,6);
	}
	else if(!strncmp(received_label,"SPEE",4)){ // Respons from AutoCom Function 
	
	OCR1A = received_value;
	send_command("OCR1A=",0);
	ItoH(OCR1A,6);
	}

	else if(!strncmp(received_label,"EeSA",4)){ // Set EEprom adress
	
		Eeprom_Adress = received_value;

	}

	else if(!strncmp(received_label,"EeWB",4)){ // Write Byte
	

		Write_eeprom_byte(Eeprom_Adress, received_value);
		Eeprom_Adress++;									// jump to next adress

	}

	else if(!strncmp(received_label,"EeWI",4)){ // Write Word
	
		Write_eeprom_int(Eeprom_Adress, received_value);
		Eeprom_Adress++;
		Eeprom_Adress++;

	}

	else if(!strncmp(received_label,"EeRB",4)){ // Read byte
	

		send_command(HtoI(eeprom_read_byte(Eeprom_Adress)),1);
		Eeprom_Adress++;									// jump to next adress

	}

	else if(!strncmp(received_label,"EeRI",4)){ // Read Word
	
		send_command(HtoI(eeprom_read_word(Eeprom_Adress)),1);
		Eeprom_Adress++;
		Eeprom_Adress++;

	}

	else if(!strncmp(received_label,"EeER",4)){ // Erase EErpom memmory
	

		unsigned int tid;

		tid = 0x00;

		Eeprom_Adress = 0x00;
	
	
		while(tid<255)
		{
			Write_eeprom_int(Eeprom_Adress, 0xFFFF);
		
			Eeprom_Adress++;
			Eeprom_Adress++;



			tid = tid++;
			
		}

		send_command("Done",1);

	}

	else if(!strncmp(received_label,"TEST",4)){ // Send the profile data 
	

		unsigned int tid;

		tid = 0x00;

		Eeprom_Adress = 0x00;
	
	
		while(tid<10)
		{
			send_command(HtoI(eeprom_read_byte(Eeprom_Adress)),1);				// 00 Portnumber 		8bit
			Eeprom_Adress++;
			Delay(10);

			send_command(HtoI(eeprom_read_byte(Eeprom_Adress)),1);				// 01 Function data 	8bit
			Eeprom_Adress++;
			Delay(10);

			send_command(HtoI(eeprom_read_byte(Eeprom_Adress)),1);				// 02 Setting		 	8bit
			Eeprom_Adress++;
			Delay(10);

			send_command(HtoI(eeprom_read_word(Eeprom_Adress)),1);				// 03 DATA1			 	16bit
			Eeprom_Adress++;
			Eeprom_Adress++;
			Delay(10);

			send_command(HtoI(eeprom_read_word(Eeprom_Adress)),1);				// 05 DATA2			 	16bit
			Eeprom_Adress++;
			Eeprom_Adress++;
			Delay(10);

			send_command(HtoI(eeprom_read_byte(Eeprom_Adress)),1);				// 07 DATA3			 	8bit
			Eeprom_Adress++;
			Delay(10);




			tid = tid++;
			
		}

		send_command("Done",1);

	}



}

Uart.c



/*--------------------------------------------------------------------------------------------------
                                Uart File
--------------------------------------------------------------------------------------------------*/
// Main includes 
#include "Uart.h"  
/*--------------------------------------------------------------------------------------------------*/




/*--------------------------------------------------------------------------------------------------
                                Private function prototypes
--------------------------------------------------------------------------------------------------*/
//  Function prototypes are mandatory otherwise the compiler generates unreliable code.
int ReadConvert(char *v);
void send_command(char * command, char newline);
int hexatoi(uint8_t * addr);
void ItoH(unsigned int num, char *str);
int HtoI (unsigned int ch);
/*--------------------------------------------------------------------------------------------------*/






/*--------------------------------------------------------------------------------------------------

  Name         :  SerialInit

  Description  :  Init RS232

  Argument(s)  :  None.

  Return value :  None.
--------------------------------------------------------------------------------------------------*/
void SerialInit ()
{
 
   UCSR0B |= (0 << RXCIE0) | (0 << TXCIE0) | (0 << UDRIE0) | (1 << RXEN0) | (1 << TXEN0) | (0 << UCSZ02) | (0 << RXB80) | (0 << TXB80);   // Turn on the transmission and reception circuitry
   
   UCSR0C |= (0 << UMSEL00) | (0 << UMSEL01) | (0 << UPM01) | (0 << UPM00)  | (0 << USBS0) |(1 << UCSZ00) | (1 << UCSZ01) | (0 << UCPOL0); // Use 8-bit character sizes

   UBRR0L = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
   UBRR0H = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register

   UCSR0B |= (1 << RXCIE0); // Enable the USART Recieve Complete interrupt (USART_RXC)
   sei(); // Enable the Global Interrupt Enable flag so that interrupts can be processed

}

/*--------------------------------------------------------------------------------------------------

  Name         :  send_single_byte

  Description  :  Sends a single Byte

  Argument(s)  :  bite to send.

--------------------------------------------------------------------------------------------------*/
void send_single_byte(unsigned char data){
	while (!( UCSR0A & (1<<UDRE0)));
	UDR0=data;
}

/*--------------------------------------------------------------------------------------------------

  Name         :  send_command

  Description  :  Sends a ´string

  Argument(s)  :  string to send., newline = if the string shuld end with "/n" 

--------------------------------------------------------------------------------------------------*/
void send_command(char * command, char newline){

	unsigned char i=0;

	while(*(command+i)!='\0'){
		send_single_byte(*(command+i));
		i++;
	}

	if(newline == 0x01)
	{
	//	send_single_byte(0x0D);				// "\r"
		send_single_byte(0x0A);				// "\n"
	}


}



/*--------------------------------------------------------------------------------------------------
 
  Name         :  ReadConvert

  Description  :  läser av str och gör tar bort allt före och : och konverterar om värdet efter det
  				  och returnerar 

--------------------------------------------------------------------------------------------------*/

int ReadConvert(char *v)
{

		int ReadConvert_Value;

		v = strchr(v, ':');
		v++; // point at character AFTER the ':'
		ReadConvert_Value = atoi(v); // will stop converting at ';'
		return ReadConvert_Value;


}

/*--------------------------------------------------------------------------------------------------
 
  Name         :  hexatoi

  Description  : 


--------------------------------------------------------------------------------------------------*/

int hexatoi(uint8_t * addr)
{
   uint8_t   *p;
   uint8_t n;
   uint16_t addr_val = 0;



   if (addr[0] != '\0') {
      p = addr;
      addr_val = 0;
      while (*p != '\0') {
              addr_val <<= 4;
              n = *p;
              if ((n >= '0') && (n <= '9')) {
                      n = n - '0';
              }
              else if ((n >= 'a') && (n <= 'f')) {
                      n = n - 'a' + 10;
              }
              else if ((n >= 'A') && (n <= 'F')) {
                      n = n - 'A' + 10;
              }
              else {
                      n = 0;
              }
              addr_val += n;
              p++;
      }
   }
   return addr_val;
}
/*--------------------------------------------------------------------------------------------------
 
  Name         :  ItoH

  Description  :  Converts int value to Hex and send it to UART in ascci format

  Return value :  char value.

--------------------------------------------------------------------------------------------------*/


void ItoH(unsigned int num, char *str) {

unsigned char i,val;

str[4]='\0';
for (i=3;i<4;i--) {
    val=num%16;
    if (val<10) str[i]='0'+val;
    else str[i]='A'+val-10;
    num=num>>4;
    }

	send_command(str,1);
}

/*--------------------------------------------------------------------------------------------------
 
  Name         :  HtoI 

  Description  :  Converts hex to int and Returns it in ascci format

  Return value :  int value.

--------------------------------------------------------------------------------------------------*/

int HtoI (unsigned int ch)
{
	unsigned int dispStr[ 11 ];

		ltoa(ch, dispStr, 10);
		return dispStr;
}


Usart.h

/*--------------------------------------------------------------------------------------------------
                                Uart Header File
--------------------------------------------------------------------------------------------------*/
// Main includes 
#include 
#include 
#include 
#include 
/*--------------------------------------------------------------------------------------------------*/


/* define CPU frequency in Mhz here if not defined in Makefile */
#ifndef F_CPU
#define F_CPU 20000000UL
#endif

#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)






//***** buffer
volatile unsigned char ItoHstring[2];
volatile unsigned char ReceivedByte[50];


void send_single_byte(unsigned char data);
void send_command(char * command, char newline); 

//   PC RX
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

lizerdboy wrote:
sure
but it its still som char missing in som strings when a send somthing from MCU on serial.

and im testing back and fort with bootloader and without.

One suggestion I would make is to set flags in your interrupt handlers and then call the appropriate functions in your main loop when they are set. For example, on the serial receive interrupt, I usually store the received value in a volatile buffer, increment index into the buffer, then set a flag when I see the terminating character. I really try to avoid calling functions inside of interrupt handlers.

As for why it works fine without bootloader, not sure about that... Might be something to do with interrupts being enabled in the bootloader? Or maybe the program looses control of the PC and wraps around to the boot vector when there is no bootloader? Do you see the send_command("MCU Reset") being executed multiple times with no bootloader?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The MCU Reset is launched only once, and thats on bootup.

I try to change and make the function outside the ISR.

how can a disable all interrupts just to be sure the are ok ??

Many and Big thanks telliott

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
BODLEVEL = 4,3V
BOOTSZ = size=2048words and start=$3800
BOOTRST = checked
Ext Crystal 16K +65ms

Hi Lizerd
Check your BOOTSZ. According to the Infosheet that comes with the chip45boot2 it should be 1024 words

Cheers
Dave