EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / flash using UART

flash using UART

|

Microcontroller › AVR › flash using UART

  • This topic has 0 replies, 1 voice, and was last updated 10 years, 6 months ago by niteesh.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • August 3, 2015 at 11:36 am #3796
    niteesh
    Participant
    #define F_CPU 1000000
    #include <avr/io.h>
    #include <util/delay.h>
    #include <avr/boot.h>
    #include <inttypes.h>
    #include <avr/interrupt.h>
    #include <avr/pgmspace.h>
    #include <stdio.h>
     
     
    //////////////////////////////////////////////////////////////////////////////////////
     
    #define BAUD 4800UL
     
    #define MY_UBRR ((F_CPU/ (16UL * BAUD))-1)
     
    void usart_init()
    {
     
    UBRRH = MY_UBRR>>8;
    UBRRL = MY_UBRR;
     
    UCSRB = (1<<RXEN)|(1<<TXEN);
     
    UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
     
      }
     
    void usart_write(unsigned char data)
    {
     
    while(!(UCSRA & (1<<UDRE)));
     
    UDR = data;
     
      }
     
    unsigned char usart_read()
    {
     
    while(!(UCSRA &(1<<RXC)));
     
    return UDR;
     
    }
     
    void usart_string(unsigned char *str)
    {
     
    unsigned char i = 0;
     
    while(str != '')
    {
      while(!(UCSRA & (1<<UDRE)));
      UDR = str;
      i++;
     }
     
     
    }
    //////////////////////////////////////////////////
     
    int main ( void )
    {int j=120;
    uint16_t i;
    unsigned char *A;
    uint8_t sreg;
    uint32_t page = 0;
    unsigned char *buf = *A;
    unsigned char ch;
    usart_init();
    DDRB|=(1<<PB0)|(1<<PB1)|(1<<PB7);
    PORTB|=(1<<PB0);
    ch=usart_read();
    if(ch=='X')
    {
    PORTB|=(1<<PB1);
     
    asm("jmp 0x0000");}
     
    if(ch=='P') // yes if host wants to program
    {usart_write('o'); //ack
    // storing the bytes from ith location of flash memory to ith variable of array A//
     
    for(i=0;i<15000;i++)
    {
    A=usart_read();
    if(A=='X')              // Check for ESC
    {PORTB|=(1<<PB7);
    goto SPM;} // jump to application programmed at 0x0000
     
    }
     
    // storing the bytes from ith location of flash memory to ith variable of array A//
    }
     
    //================================= SPM ==========================================//
           
    SPM:   
    while(1)
    {
    //==========================================================================//
    if(j)
    {
     
    sreg = SREG;
    cli(); // Disable interrupts.
    eeprom_busy_wait ();
    boot_page_erase (page);
    boot_spm_busy_wait ();      // Wait until the memory is erased.
     
    for (i=0; i<SPM_PAGESIZE; i+=2)
    {
        // Set up little-endian word.
      uint16_t w = *buf++;
       w += (*buf++) << 8;
     
       boot_page_fill (page + i, w);
    }
    boot_page_write (page);     // Store buffer in flash page.
    boot_spm_busy_wait();      // Wait until the memory is written.
    boot_rww_enable ();
    SREG = sreg;
    }
    else
    {
    asm ( "jmp 0x0000" );
    }
     
    j–;
    page = page + 128;
    //==============================================================================//
    }
    }
     
     
     
     
     
    this is my code for programing flash using UART.
    its not working plz help.
    'P' is programming flash & 'X' for exit from bootloading section.
  • Author
    Posts
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Buffer design? February 9, 2026
  • ANOTHER OLD PROJECT REDO February 9, 2026
  • BPF February 9, 2026
  • To couple or decouple February 9, 2026
  • Op amp shock February 9, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise