Chapter 6 - Memory

16
Chapter 6 - Memory Chapter 6 - Memory How to allocate variables and constants in RAM and FLASH memory

description

Chapter 6 - Memory. How to allocate variables and constants in RAM and FLASH memory. Strings.c Example. /* ** Strings */ #include #include // 1. variable declarations const char a[] = "Exploring the PIC32"; char b[100] = "Initialized"; // 2. main program main() - PowerPoint PPT Presentation

Transcript of Chapter 6 - Memory

Page 1: Chapter 6 - Memory

Chapter 6 - MemoryChapter 6 - Memory

How to allocate variables and constants in RAM and FLASH

memory

Page 2: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

Strings.c ExampleStrings.c Example/*** Strings*/#include <p32xxxx.h>#include <string.h>// 1. variable declarationsconst char a[] = "Exploring the PIC32";char b[100] = "Initialized";// 2. main programmain(){ strcpy( b, "MPLAB C32"); // assign new content to b} // main

Page 3: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

Using the Watch WindowUsing the Watch Window

Page 4: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

Setting the Watch PropertiesSetting the Watch Properties

Page 5: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

String “b” after C0 initializationString “b” after C0 initialization

Page 6: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

Disassembly ListingDisassembly Listing14: // 2. main program15: main()16: {9D000018 27BDFFE8 addiu sp,sp,-249D00001C AFBF0014 sw ra,20(sp)9D000020 AFBE0010 sw s8,16(sp)9D000024 03A0F021 addu s8,sp,zero17: strcpy( b, "MPLAB C32"); // assign new content to b9D000028 3C02A000 lui v0,0xa0009D00002C 24440000 addiu a0,v0,09D000030 3C029D00 lui v0,0x9d009D000034 2445074C addiu a1,v0,18689D000038 0F400016 jal 0x9d0000589D00003C 00000000 nop 18: } // main9D000040 03C0E821 addu sp,s8,zero9D000044 8FBF0014 lw ra,20(sp)9D000048 8FBE0010 lw s8,16(sp)9D00004C 27BD0018 addiu sp,sp,249D000050 03E00008 jr ra9D000054 00000000 nop

Page 7: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

Looking at the “Map”Looking at the “Map”

C:/Program Files/Microchip/../pic32mx/lib\libc.a(strcpy.o) Strings.o (strcpy)

C:/Program Files/Microchip/../pic32mx/lib\libc.a(strcpy.o) Strings.o (strcpy)

Memory ConfigurationName Origin Length Attributeskseg0_program_mem 0x9d000000 0x00080000 xrkseg0_boot_mem 0x9fc00490 0x00000970exception_mem 0x9fc01000 0x00001000kseg1_boot_mem 0xbfc00000 0x00000490debug_exec_mem 0xbfc02000 0x00000ff0config3 0xbfc02ff0 0x00000004config2 0xbfc02ff4 0x00000004config1 0xbfc02ff8 0x00000004config0 0xbfc02ffc 0x00000004kseg1_data_mem 0xa0000000 0x00008000 w !xsfrs 0xbf800000 0x00100000*default* 0x00000000 0xffffffff

Memory ConfigurationName Origin Length Attributeskseg0_program_mem 0x9d000000 0x00080000 xrkseg0_boot_mem 0x9fc00490 0x00000970exception_mem 0x9fc01000 0x00001000kseg1_boot_mem 0xbfc00000 0x00000490debug_exec_mem 0xbfc02000 0x00000ff0config3 0xbfc02ff0 0x00000004config2 0xbfc02ff4 0x00000004config1 0xbfc02ff8 0x00000004config0 0xbfc02ffc 0x00000004kseg1_data_mem 0xa0000000 0x00008000 w !xsfrs 0xbf800000 0x00100000*default* 0x00000000 0xffffffff

Page 8: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

Memory Map SectionsMemory Map Sections .reset section, containing the code that will be placed by the linker at the reset vector. This is

normally filled with a default handler (_reset()). .reset 0xbfc00000 0x10 C:/.../pic32mx/lib/crt0.o 0xbfc00000 _reset .vector_x sections, there are 64 of them each associated to the corresponding interrupt handler.

They will be empty unless your program is using the specific interrupt handler..vector_0 0x9fc01200 0x0 .startup section, where the C0 initialization code is placed. .startup 0x9fc00490 0x1e0 C:/.../lib/crt0.o .text sections, you will find many of them, where all the code generated by the MPLAB C32

compiler from your source files is placed. Here is the specific part produced by our main() function: .text 0x9d000018 0x40 Strings.o 0x9d000018 main .rodata section, where read only (constant) data is placed in program memory space. Here we can

find space for our constant string “a” for example: .rodata 0x9d000738 0x20 Strings.o 0x9d000738 a .data section, where RAM memory is allocated for global variables. .data 0xa0000000 0x64 Strings.o 0xa0000000 b .data1 section, where the initialization value, ready for the C0 code to load into the “b” variable is

placed, once more, in program memory space. *(.data1) 0x9d00076c _data_image_begin=LOADADDR(data)

Page 9: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

The Memory WindowThe Memory Window

Address 00 04 08 0C ASCII 1D00_0760 9D0003AC 9D0004F4 9D000578 74696E49 ........ x...Init1D00_0770 696C6169 0064657A 00000000 00000000 ialized. ........

Page 10: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

PointersPointersint *pi; // define a pointer to an integerint i; // index/counterint a[10]; // the array of integers

// 1. sequential access using array indexingfor( i=0; i<10; i++) a[ i] = i;

// 2. sequential access using a pointerpi = a;for( i=0; i<10; i++){ *pi = i; pi++;}

Page 11: Chapter 6 - Memory

Advanced MaterialAdvanced MaterialFixed Translation Map

User and Kernel Virtual Memory Maps

Page 12: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

The PIC32 Fixed Translation MapThe PIC32 Fixed Translation Map

Page 13: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

Phisical Addressing SpacePhisical Addressing Space

RAMRAM FLASHFLASHSFR

SFR

0x00000000 0x1D000000 0x1F800000

BOOT

BOOT

0x1FC00000 0xFFFFFFFF

Page 14: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

RAM PartitioningRAM Partitioning

Kernel RAM(Data)

Kernel RAM(Data)

0x00000000 BMXDUDBA 0xBF000000+BMXDUDBA

User RAM(Data)

User RAM(Data)

0xFFFFFFFF

User RAM (Prog)

User RAM (Prog)

KernelRAM(Prog)

KernelRAM(Prog)

BMXDKDBA 0xBF000000+BMXDUPBA

Page 15: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

User Mode Virtual Memory MapUser Mode Virtual Memory Map

0x7F000000+BMXDUDBA

User RAM(Data)

User RAM(Data)

0x7F000000+BMXDUPBA

User RAM(Prog)

User RAM(Prog)

0x80000000

User Space Kernel Space

User FLASH

User FLASH

Generate an immediate exception if access is

attempted!

0xFFFFFFFF

0x00000000

0x7D000000

Page 16: Chapter 6 - Memory

Di Jasio - Programming 32-bit Microcontrollers in C

Kernel Mode Virtual Memory MapKernel Mode Virtual Memory Map

0x80000000+BMXDKPBA

User Space Kernel Space

FLASHCached(Kseg0)

FLASHCached(Kseg0)

0xFFFFFFFF0x00000000 0x9D000000

RAM(Data)

RAM(Data)

RAM(Prog)

RAM(Prog)

FLASHUn-

Cached(Kseg1)

FLASHUn-

Cached(Kseg1)

0xBD0000000x80000000

SFR

SFR

0xBF800000

BOOT

BOOT

0xBFC00000