Go Down

Topic: 5.1 Organization of Flash (Code Memory/Program Memory) of ATmega328 MCU (Read 1 time) previous topic - next topic

GolamMostafa

***Revised on 10/5/17 as per Post#1 of CrossRoads***
5.1  Organization of Flash (Code Memory/Program Memory) of ATmega328 MCU
(1)  In the Lab using Arduino IDE, we have compiled our application programs (the sketches) to
      obtain binary codes (machine codes). However, we have not yet seen these binary codes
      which the MCU executes to produce the desired result like blink L (built-in LED of Arduino
      UNO Kit).

(2)  We transferred (uploaded) the binary codes of Step-1 to the ATmega328 Microcontroller (MCU)
      of the Arduino Kit. These binary codes get stored into a block of memory space of the MCU. This
      block of memory space is known as Flash (Code Memory = Program Memory).
      Why is it called Flash?
      "The term flash was chosen because a large chunk of memory could be erased at one time.
       The name, therefore, distinguishes flash devices from EEPROMs, where each byte is erased
        individually."

(3)  Conceptual View of the Code Memory (Flash) of ATmega328




(4)  Inside the Code Memory, there are small slots (bins) called Memory Locations.

(5)  Every memory location has a 16-bit (4 digit-hex) identification code called Address (adr).

(6)  There are in total 16x1024 (16384) locations inside the code memory of ATmega328. The
      address of the 1st location is 0 ($0000) and the address of the last location is 16383 ($3FFF).
      $ is the notation for hexadecimal number; "0x notation for hexdecimal number is preferred
      while coding in assembly."

(7)  (a) Every memory location can hold 16-bit (word = 2-byte) data/code. The 16-bit data is
       split as Low Byte and High Byte.

(8)  (a) The Code Memory of ATmega328 of Arduino Kit has three sections:
     (i)     Interrupt Vector Section,
     (ii)    Application Section, and
     (iii)   Boot Section.

      (b) This is the application section into which the machine codes of our sketches are stored
            during uploading.

      (c)  The machine codes of an application program have two parts: (i) Executable
             Codes and (ii) Data on which the Executable Codes work. The executable codes
             are stored in the 'Application section' of the Code Memory and the data are stored in
             the 'SRAM = Static RAM Memory' area of the MCU." The SRAM would be discussed in a
             separate post. It might happen that data (variable) are stored into Code Memory to
             save SRAM space.

      (d)  The boundary addresses and operational modes of the memory sections are determined
             by Fuse Bits.
      (e)  The access rights to Boot and Application sections are controlled by:
             (i)    Boot Lock Bits,
             (ii)   Application Lock Bits,
     
(9) The Code Memory is a non-volatile EEPROM type memory. Volatile memory loses its
      information (data) when it is disconnected from power supply. EEPROM stands for
       Electrically Erasable Electrically Programmable Random Access Ream Only Memory.

       "Flash is one type of EEPROM, with a limited number of re-write cycles (10,000 times). The
        EEPROM is a different type, with more re-write cycles (100,000). RAM is Random Access
        Memory that can be read and written. (Vs what you spelled out, RARWM). '328P and other
        AVR devices use Static RAM (SRAM) (vs Dynamic RAM, or DRAM, such as is found in a PC).
        SRAM can be written and retains its data as long as power is available. DRAM must be
        continuously refreshed to retain the data."

(10) Calculation of Code Memory Capacity
        (i)            16x1024 word locations
                      ==> 16 K word locations    (in Computer Science, 1024 stands for 1 K)
        Word locations are individually accessible by the processor during instruction fetching and
        execution.

        (ii)           16x1024  2-byte locations
                      ==> 16x102x2  byte locations
                      ==> 32 K byte locations
       Byte locations can also be individually accessed during Paralle/Serial Programming. 
       
       (iii)           32x1024x8 bit locations (8-byte = 8-bit)
                        ==> 262 144 bits
        The bit locations can not accessed individually. In 8051, there are a good number of bit
         addressable locations in SRAM.
     
        The Code Memory contains limited amout of memory space. So, we must exercise hard to
        write program with minimum number of instructions in order consume less memory space.

(11)  Programming the Code Memory refers to the processes of Erasing, Writing, and Verifying
        its contents. Verification is done to ensue that the programmer has correctly written
        source data into the designated locations; this is done by reading back the fused data
        and comparing it with the original source data. Some programmers avoid verification to 
        speed up the programming process.

(12)  Erasing of the Code Memory refers to turning all the bits of the memory into LH states. The
        erasing is done by one of the following methods:
        (i)    Parallel Programming and
        (ii)   Serial Programming (In-system Programming = ISP).

(13)  New information (code/data) can be written into code memory using one of the following
        methods:
        (i)   Parallel Programming,
        (ii)  Serial Programming (In-system Programming = ISP), and
        (iii) Self-programming.

(14) The numerical values of the addresses of the code memory and data memory (EEPROM)
        locations come into picture only when we write programs using Assembly Language. The
        High Level Language like C/C++ hides to the user all kinds of low level activities. For
        example:
           
Code: [Select]

        byte myArray[2] = {0x3F, 0x66};       // HLL Code

        //---ASM Code------
        ldi    r16, 0x3F
        sts    $0600, r16     //$0600 is the address of a Data Space (not shown here)


(15)  Binary Code (of an application program) storage organization into Code Memory
        Assume that the following machine codes (written as hex bytes as a matter of
        convenience) represent some actions to be carried out by the processor (the
        microcontroller). These codes must be loaded first into code memory. The arrangement of
        storage is shown in Fig-5.1.

        000040 e50f       ldi r16, 0x5F ; stack initialize
        000041 bf0d       out spl, r16
        000042 e008       ldi r16, 0x08
        000043 bf0e       out sph, r16

5.2  Home Works
(1)  Download data sheets for ATmega328 and ATTiny85 microcontrollers.
(2)  Draw conceptual diagram for the Code Memory of ATTiny85 MCU.

CrossRoads

Some comments:
#2 "Why is it called Flash?" You  won't find that in the datasheet 8) Students will need to do some online research.
"Flash memory technology is a mix of EPROM and EEPROM technologies. The term flash was
chosen because a large chunk of memory could be erased at one time. The name, therefore, distinguishes
flash devices from EEPROMs, where each byte is erased individually."
smithsonianchips.si.edu/ice/cd/MEMORY97/SEC10.PDF

#6, hex is referred with 0x3FFF when coding tho.
#8, probably want to combine #8 & #9.
I believe registers/stack/heap actually take up part of the SRAM.
Statement for #9 only applies to (iii) Fuse bytes. The Lock bits just limit access to the bootloader (if used) & flash. -> #11
#10. Terminoloy: I would not put in EPROM (UVEPROM), it's not part of the chip. Maybe as historical footnote, with OTPROM. Flash is one type of EEPROM, with a limited number of re-write cycles (10,000 times). The EEPROM is a different type, with more re-write cycles (100,000).
RAM is Random Access Memory that can be read and written. (Vs what you spelled out, RARWM). '328P and other AVR devices use Static RAM (SRAM) (vs Dynamic RAM, or DRAM, such as is found in a PC). SRAM can be written and retains its data as long as power is available. DRAM must be continuously refreshed to retain the data.
#12, the bits are not individually accessible, only bytes are addressable. The code lets a byte be read, manipulated, and rewritten so it looks like 1 bit was accessed. Or for reading, the state of one bit can be reported.
#13, reading the written code is not required, it's only done if one wants to verify the code after it is written. Some methods speed up programming by not verifying.
#14, I believe Serial programming also erases a memory location before writing it.
#16, specific addressing also comes into play when writing & reading the EEPROM.



Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

GolamMostafa

@CrossRoads

I thank myself for the courage of posting my lecture notes which have received so much constructive and necessary amendments that I could never ever realise being idle. The post will very soon undergo necessary revisions and distributions in compliance with the comments.

What's about those who have taken time and pain to go through every line of the poorly formatted posts and have laid down comments based on their academic intellect and field training? They deserve respects from all knowledge seekers.

All the scriptures of the monotheistic religions have given utmost emphasis to the citizens to engage appropriate efforts for the acquisition of the most up dated knowledge where-ever it is and shape their lives and the society for better living and longer survival.   


Go Up