PIC32 Memory Access

Register and Memory Access

peek()
poke()
peeki()
pokei()
adr()

All microcontrollers have a rich memory set and it would be impractical to include every single option in some kind of BASIC statement. The peek/poke mechanism will allow access to all of the CPU registers to enable the user to set up unusual or complex configurations.

There are two sets of peek/poke, the first set will fetch or place a byte and the second set will fetch or place an integer which is 4 bytes (32 bits). Care must be taken when using the integer peek/poke not to read or write any memory addresses that don’t divide by 4 otherwise a general exception will occur.

peek()
peek(address)
a=peek(0xa0002000)
b=peek(b)

This statement returns the contents of the address given in brackets. The contents will be one byte.

poke()
poke(address)=value
poke(0xa0002000)=0
poke(b)=a

The address given in the brackets is set to value. The address is the address of one byte. If an attempt is made to write to Flash a general exception will be generated as Flash must be written in a special way, you simply can’t poke a byte into it. Likewise and attempt to write to a memory address that is not allowed by the microcontroller will generate an exception.

peeki()
peeki(address)
a=peeki(0xa0002000)
b=peeki(b)

At first sight there is little difference between peek and peeki, however peeki deals in word or 32 bit elements of data at a time. Most of the time this will be to retrieve integer values and so care must be exercised that the address is divisible by 4. Although this will not cause an exception if an odd address is read. The keyword is used to read processor registers as all of those are 32 bits wide.

pokei()
pokei(address)=value
pokei(0xa0002000)=0
pokei(b)=a

Places a 4 byte value at the given address, in this case the address must be within ram, or a register and must be divisible by 4. i.e. end in 0x0,0x4,0x8 or 0xc. A typical example for using these would be to read and write register addresses.

function rtc_time
dim rtctime=0xbf800220
    print "time register contents=";hex$(peeki(rtctime))
endf

In the above example the register that holds the RTC time is read. In this case all 4 bytes of the register are retrieved by using peeki. Although there are specific BASIC statements for most of the functionality, just about any of the functions can be reproduced by using this register access.

adr()
peek(address)
a=adr(j$)
b=adr(x[4])
c=adr(sub1) // see also fexists

Used for obtaining the address of variables or subroutines, it will return a pointer (address) of that variable or function as an integer. In the case of ‘b’ in the example this will return the address of the 4th element of the array. As arrays are contiguous the 5th element will be b+4 (an integer array occupies 4 bytes). This keyword will also return the address of a function, if it has been initialised, see 'fexists', otherwise it will return an error. Some keywords require an address rather then the actual variable itself, if this is the case, this keyword is used.