Mbed BK Ed2 CH 10
Mbed BK Ed2 CH 10
If you use or reference these slides or the associated textbook, please cite the original authors’ work as follows:
Toulson, R. & Wilmshurst, T. (2016). Fast and Effective Embedded Systems Design - Applying the ARM mbed
(2nd edition), Newnes, Oxford, ISBN: 978-0-08-100880-5.
www.embedded-knowhow.co.uk
Memory function types
A microprocessor needs memory for two reasons: to hold its program, and to hold
the data that it is working with; we often call these program memory and data
memory. To meet these needs there are a number of different semiconductor
memory technologies available, which can be embedded on the microcontroller
chip.
Memory technology is divided broadly into two types: volatile and non-volatile.
• Non-volatile memory retains its data when power is removed, but tends to be
more complex to write to in the first place. For historical reasons it is still often
called ROM (Read Only Memory). Non-volatile memory is generally required
for program memory, so that the program data is there and ready when the
processor is powered up.
• Volatile memory loses all data when power is removed, but is easy to write to.
Volatile memory is traditionally used for data memory; it’s essential to be able
to write to memory easily, and there is little expectation for data to be retained
when the product is switched off. For historical reasons it is often called RAM
(Random Access Memory).
Essential electronic memory types
• In any electronic memory we want to be able to store all the 1s and 0s which
make up our data.
• A simple one-bit memory is a coin. It is stable in two positions, with either
“heads” facing up, or “tails”.
• We can try to balance the coin on its edge, but it would pretty soon fall over.
• We recognise that the coin is stable in two states, we call this bistable.
• It could be said that “heads” represents logic 1, and “tails” logic 0. With 8
coins, an 8-bit number can be represented and stored.
• If we had 10 million coins, we could store the data that makes up one
photograph of good resolution, but that would take up a lot of space indeed!
• There are a number of electronic alternatives to the coin, which take up much
less space.
• One is to use an electronic bistable (or “flip-flop”) circuit.
Essential electronic memory types
The two logic circuits shown are stable in only two states, and each can be used
to store one bit of data.
VCC
Essential electronic memory types
There are a number of different types of volatile and non-volatile memory:
The specific address of a data variable, can also be assigned to a pointer by using the &
operator, for example
We can also use the * operator in a program to get the data from a pointer address, for
example:
We can also use pointers with arrays, because an array is really just a number of data
values stored at consecutive memory locations; for example:
int main() {
dataptr=&data[0]; // point to address of the first array element
average = CalculateAverage(dataptr, sizeof(data)); // call function
pc.printf("\n\rdata = ");
for (char i=0; i<sizeof(data); i++) { // loop for each data value
pc.printf("%d ",data[i]); // display all the data values
}
pc.printf("\n\raverage = %.3f",average); // display average value
}
FILE *fopen(const char *filename, const char opens the file of name
fopen
*mode); filename
int fseek(FILE *stream, long int offset, int moves file pointer to
fseek
origin); specified location
To implement, simply add the following line to the declarations section of a program:
A file stored on the mbed (in this example called “datafile.txt”) can therefore be opened with
the following command:
When we have finished using a file for reading or writing it is essential to close it, for
example using
fclose(pFile);
Example mbed data file access
int main ()
{
FILE* File1 = fopen("/local/datafile.txt","w"); // open file
write_var=0x23; // example data
fputc(write_var, File1); // put char (data value) into file
fclose(File1); // close file
int main ()
{
FILE* File1 = fopen("/local/textfile.txt","w"); // open file access
fputs("lots and lots of words and letters", File1);// put text into file
fclose(File1); // close file
Read further:
Writing formatted data,
Section 10.4.3
Using external memory with the mbed
A flash SD (Secure Digital) card can be used with the mbed via the SPI protocol
Using a micro SD card with a card holder cradle (as shown) it is possible to access the SD
card as an external memory
The SD card can be connected to the mbed as shown in the following wiring table:
int main() {
FILE *File = fopen("/sd/sdfile.txt", "w"); // open file
if(File == NULL) { // check for file pointer
pc.printf("Could not open file for write\n"); // error if no pointer
}
else{
pc.printf("SD card file successfully opened\n"); // if pointer ok
}
fprintf(File, "Here's some sample text on the SD card"); // write data
fclose(File); // close file
}
Using external USB flash memory with the mbed
Program Example 10.5 /* Program Example 10.5: writing data to an USB flash storage device
implements the USB flash */
#include "mbed.h"
storage. #include "USBHostMSD.h"