AVR BOOT

Posted gyang_iconergy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AVR BOOT相关的知识,希望对你有一定的参考价值。

  1 #include <string.h>
  2 #include <iom128v.h>
  3 #include "main.h"
  4 #include <macros.h>
  5 //#include "assembly.h"
  6 
  7 static unsigned long timer_1ms = 0;
  8 static unsigned char sys_state = 0;
  9 static unsigned char uart0_rx_buf[UART0_BUF_SIZE];
 10 static unsigned long flash_addr = 0;
 11 void sys_set_state(unsigned char state)
 12 {
 13  sys_state = state;
 14 }
 15 unsigned long get_sys_time_ms(void)
 16 {
 17  return timer_1ms;
 18 }
 19 extern int _textmode;
 20  
 21 int putchar(char c)
 22     {
 23     //if (_textmode && c == \'\\n\')
 24     //    putchar(\'\\r\');
 25     /* Wait for empty transmit buffer */
 26     while ( !(UCSR0A & (1<<UDRE0)) )
 27         ;                     
 28     /* Putting data into buffer , sends the data */
 29     UDR0 = c;  
 30     return c;
 31 }
 32 int getchar(void)
 33 {
 34  unsigned char dat = 0;
 35  unsigned char status = 0;
 36  WDR();
 37  if(((UCSR0A & 0x80)==0)){
 38   return -1;
 39  }
 40  status = UCSR0A;
 41  dat = UDR0;
 42  if(status & 0x1C){
 43   return -2;
 44  }
 45  
 46  return dat; 
 47 }
 48 void sendStr(char *str)
 49 {
 50  int len = 0;
 51  unsigned char i = 0;
 52  len = strlen(str);
 53  for(i = 0; i < len; i++)
 54   putchar(str[i]);
 55  while ( !(UCSR0A & (1<<UDRE0)) );
 56 }
 57 void port_init(void)
 58 {
 59 // MCUCSR |= 0x80;
 60 // MCUCSR |= 0x80;
 61   PORTA = 0x07;    //PA3~PA7 IS LOW,PA2~PA0 IS HIGH
 62   DDRA  = 0xFF;
 63   PORTB = 0xFb;    //MOSI(PB2) is low *****
 64   DDRB  = 0xF7;    //PB3 IS INPUT
 65   PORTC = 0xFF;     //m103 output only
 66   DDRC  = 0xFF;
 67   PORTD = 0xFF;
 68   DDRD  = 0x00;    //PD0~7 IS INPUT
 69   PORTE = 0xFF;
 70   DDRE  = 0xFE;
 71   PORTF = 0xFF;
 72   DDRF  = 0x00;
 73   PORTG = 0x1F;
 74   DDRG  = 0x00;
 75 }
 76 void uart0_init(void)
 77 {
 78   UCSR0B = 0x00;   //disable while setting baud rate
 79   UCSR0A = 0x00;
 80   UCSR0C = 0x06;
 81  //6MHz 
 82   UBRR0L = 0x26;   //set baud rate lo
 83   //14.7456MHz 9600bps
 84 //  UBRR0L = 0x5F;   //set baud rate lo
 85   //14.7456MHz 115200bps
 86 //  UBRR0L = 0x07;   //set baud rate lo
 87   UBRR0H = 0x00;   //set baud rate hi
 88 //  UCSR0B = 0xD8;  //0xB8
 89 //  UCSR0B = 0xB8;  //0xD8
 90   //UCSR0B = 0x98;  //0xD8,0xB8
 91   UCSR0B = 0x18;  //0xD8,0xB8,disable rcv interrupt
 92 }
 93 // OSC:6MHz, prescale:1024
 94 void timer0_init(void)
 95 {
 96   TCCR0 = 0x00; //stop
 97   ASSR  = 0x00; //set async mode
 98   TCNT0 = 0xf9; //set count
 99   OCR0  = 0xD7;
100   TCCR0 = 0x07; //start timer
101 }
102 #pragma interrupt_handler timer0_ovf_isr:17
103 void timer0_ovf_isr(void)
104 {
105   TCNT0 = 0xf9; //reload counter value
106   timer_1ms++;
107 }
108 void watchdog_off(void)
109 {
110 // Write logical one to WDCE and WDE
111  WDTCR = (1<<WDCE) | (1<<WDE);
112 // Turn off WDT
113  WDTCR = 0x00;
114 }
115 void watchdog_init(unsigned char time)
116 {
117   WDR();     //this prevents a timout on enabling
118 // Write logical one to WDCE and WDE
119   WDTCR = (1<<WDCE) | (1<<WDE);
120   time&=0x07;   // prescale: n cycles  time=0~7,WDCE=0
121   time|=0x08;   // prescale: n cycles  time=0~7,WDE=1
122  WDTCR=time;   //WATCHDOG ENABLED - dont forget to issue WDRs
123 }
124 #if 0
125 void delay_1ms(void)
126 {
127     unsigned int i;
128     for(i=1;i<(unsigned int)(XTAL*143-20);i++)
129     {
130      WDR();
131     }
132 }
133 void delay_ms(unsigned int n)
134 {
135     unsigned int i=0;
136     while(i<n)
137     {
138          delay_1ms();
139          i++;
140     }
141 }
142 #endif
143 void init_devices(void)
144 {
145   CLI(); //disable all interrupts
146   XDIV  = 0x00;   //xtal divider
147   XMCRA = 0x00;   //external memory
148   port_init();
149   uart0_init();
150  timer0_init();
151  
152   MCUCR = 0x00;
153   EICRA = 0x00;   //extended ext ints
154   sbi(TIMSK,TOIE0);
155 //  sbi(TIMSK,TOIE1); //允许定时器1溢出中断
156 //  sbi(TIMSK,TOIE2); //允许定时器2溢出中断
157  watchdog_init(7);
158   //SEI();     //re-enable interrupts
159  //all peripherals are now initialised
160 }
161 #if 0
162 void EEPROMwrite(unsigned int location, unsigned char byte)
163 //unsigned int eepromwrite(unsigned int location, unsigned char byte)
164 {
165  unsigned char oldSREG;
166  oldSREG = SREG;
167  SREG &= ~0x80;   // disable interrupt
168 // Wait for completion of previous write
169  while(EECR & (1<<EEWE)) ;
170 // Set up address and data registers
171  EEAR = location;
172  EEDR = byte;
173 // Write logical one to EEMWE
174  EECR |= (1<<EEMWE);
175 // Start eeprom write by setting EEWE
176  EECR |= (1<<EEWE);
177  
178  SREG = oldSREG;
179 // return 0;                           // return Success.  ?
180 }                   
181 unsigned char EEPROMread(unsigned int location)
182 //unsigned char eepromread(unsigned int location)
183 {
184  unsigned char oldSREG;
185  oldSREG = SREG;
186  SREG &= ~0x80;   // disable interrupt
187 // Wait for completion of previous write
188  while(EECR & (1<<EEWE)) ;
189 // Set up address register
190  EEAR = location;
191 // Start eeprom read by writing EERE
192  EECR |= (1<<EERE);
193  
194  SREG = oldSREG;
195 // Return data from data register
196  return EEDR;
197 }
198 #endif
199 #if 1
200 void wait_page_rw_ok(void)
201 {
202  //while(SPMCSR & 0x40){
203  {
204   while(SPMCSR & 0x01);
205   //SPMCSR = 0x11;
206   asm("spm\\n");
207  }
208 }
209 // code:0x03:erase, code:0x05:write a page
210 void boot_page_ew(unsigned long addr,unsigned char code)
211 {
212  wait_page_rw_ok();
213  asm("mov r30,r16\\n"
214      "mov r31,r17\\n"
215      "out 0x3b,r18\\n"
216   ); // put addr into Z register and RAMPZ\'s BIT0
217  SPMCSR = code; // set operate code
218  asm("spm\\n"); // operate flash page
219  wait_page_rw_ok();
220 }
221 // fill one word into a page
222 void boot_page_fill(unsigned int pg_addr,unsigned int data)
223 {
224  pg_addr = pg_addr;
225  data = data;
226  wait_page_rw_ok();
227  asm("mov r30,r16\\n"
228      "mov r31,r17\\n" // Z register is page addr
229      "mov r0,r18\\n"
230      "mov r1,r19\\n" // R0R1 operate word 
231      );
232  SPMCSR = 0x01;
233  asm("spm\\n");  // operate flash page
234 }
235 void write_one_page(unsigned long f_addr)
236 {
237  int i = 0;
238  for(i = 0; i < FLASH_PG_SIZE;i+=2){
239   boot_page_fill(i,uart0_rx_buf[i]|(uart0_rx_buf[i+1]<<8));
240  }
241  boot_page_ew(flash_addr,0x03);
242  boot_page_ew(flash_addr,0x05);
243 }
244 int check_one_page_ok(unsigned long f_addr)
245 {
246  unsigned char dat = 0;
247  int i = 0;
248  #if 0
249  asm("mov r30,r16\\n"
250      "mov r31,r17\\n"
251      "out 0x3b,r18\\n"
252   ); // put addr into Z register and RAMPZ\'s BIT0
253  for(i = 0; i < FLASH_PG_SIZE; i++){
254   asm("elpm r0,Z+");
255   asm("mov %dat, r0");
256   //if(dat != uart0_rx_buf[i])
257   // return 0;
258  }
259  #endif
260  return 1;
261 }
262 #else
263 void WriteFlash(void)
264   {
265     unsigned int i;
266     unsigned int TempInt;
267     for (i=0;i<FLASH_PG_SIZE;i+=2)
268       {
269        TempInt=uart0_rx_buf[i]+(uart0_rx_buf[i+1]<<8);
270        fill_temp_buffer(TempInt,i);    //call asm routine.
271       } 
272     write_page(flash_addr,0x03);       //擦除页
273     write_page(flash_addr,0x05);       //写页数据
274    
275     enableRWW();
276   }
277 #endif
278 #if 0
279 #pragma interrupt_handler uart0_rxc_isr:19  //iv_USART0_RX
280 void uart0_rxc_isr( void )
281 {
282  #if 0
283   unsigned char data;
284  unsigned char index;
285  data = UDR0; 
286  #endif
287 }
288 #endif
289 void sys_choice_process(void)
290 {
291  unsigned long time_enter = 0;
292  int dat = 0;
293  time_enter = get_sys_time_ms();
294  //sendStr("press \'d\' to download fw:");
295  while(1){
296   dat = getchar();
297   //if(\'d\' == dat){
298   {
299    putchar(XMODEM_NAK);
300    sys_set_state(SYS_STATE_READ_DAT);
301    return;
302   }
303   #if 0
304   if(get_sys_time_ms() - time_enter >= SYS_WAIT_TIME){
305    sys_set_state(SYS_STATE_ERROR);
306    return;
307   }
308   #endif
309  }
310  
311  return;
312 }
313 #if 0
314 void sys_ack_process(void)
315 {
316  // Nothing, put ack to read data process
317  return;
318 }
319 #endif
320 unsigned char read_start = 0;
321 void sys_read_data_process(void)
322 {
323  int dat = 0;
324  int count = 0;
325  unsigned int i = 0,ee_addr = 0,err_count = 0,cancel_flag = 0;
326  unsigned char checksum = 0;
327  //unsigned long flash_addr = 0;
328  unsigned char packet_sn = 0,block = 0;
329  unsigned long timer = 0;
330  read_start = 0;
331  while(1){
332   #if 1
333   if(0 == read_start){
334    timer++;
335    if((timer > 600000) ){
336     sys_set_state(SYS_STATE_END);
337     return;
338    }
339   }
340   #endif
341   dat = getchar();
342   if(dat >= 0){
343    
344    if(0 == count){
345     if(XMODEM_EOT == dat){
346      read_start = 1;
347      if(1 == block){
348       write_one_page(flash_addr);
349      }
350      putchar(XMODEM_ACK);
351      sys_set_state(SYS_STATE_END);
352      return;
353     }else if(XMODEM_SOH == dat){  // start flag
354      cancel_flag = 0;
355     }else if(XMODEM_CAN == dat){  // cancel flag
356      cancel_flag = 1;
357     }
358     
359    }else if(1 == count){
360     if(1 == cancel_flag){
361      if(XMODEM_CAN == dat){
362       cancel_flag = 2;
363      }else{
364       putchar(\'1\');
365       goto error;
366      }
367     }
368     //  Note:when packet number reach to 255, it reset to zero,not 1
369     if((dat - packet_sn == 1)|| ((0x0 == dat) &&(0xff == packet_sn))){  // sn ok
370      packet_sn = dat;
371     }else{     // sn error
372      putchar(\'2\');
373      goto error;
374     }
375    }else if(2 == count){
376     if(2 == cancel_flag){
377      if(XMODEM_CAN == dat){
378       sys_set_state(SYS_STATE_CANCEL);
379       return;
380      }else{
381       putchar(用 AVR 计算七段数字

Spring boot:thymeleaf 没有正确渲染片段

解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE(转)(代码片段

AVR - 高速中断驱动的 UART 代码不工作

AVR 示例代码解释请

为avr构建代码时出现cmake构建问题