EENG 383

Lab 9 - Data acquisition

Requirements

Working in teams of two, read through the following lab activity and perform all the actions prescribed. You do not need to document bullet items. Make a record of your response to numbered items and turn them in a single copy as your teams solution on Canvas using the instructions posted there. Include the names of both team members at the top of your solutions. Use complete English sentences when answering questions. If the answer to a question is a table or other piece of art (like an oscilloscope trace or a figure), then include a sentence explaining the piece of art. Only include your answers, do not include the question-text unless it is absolutely needed.

Objective

To familiarize you with the SD Card and its SPI interface. To use the test and measurement equipment to measure the time required to complete read/write operations to the SD Card.

External Hardware

Kingston Technology is a large technology company that manufactures mainly storage products. Their microSDXC memory card Flash Storage Media document provides one of the best overviews of micro SD Cards and the associated SPI interface that I have found on the Internet. Download this PDF and use it to help you answer the following questions.
  1. Use Figure 1 from the Kingston PDF and the schematic of the development board to answer the following question.
    • The SD Card pin# column refers to Figure 1 of the Kingston PDF. The SPI Mode Name referrers to the SPI Mode Name column from Table 2 of the Kingston PDF.
    • The PIC pin column referrers to PIC pin (shown on the development board schematic) attached to the SD Card Holder pin in that row. If the pin in this row is not connected to the SD card holder, write "Not connected".
    • The PIC MCCP column refers to the MSSP function of the PIC pin in that row. You can get the MSSP function by looking in Table 2 of the PIC18(L)F2X/4XK22 Data Sheet posted on the class web page.
    SD Card pin # SPI Mode Name PIC pin PIC MCCP
    1 RSV Not connected  
    2      
    3     SDO2
    4      
    5 SCLK    
    6     Power supply
    7   RB2  
    8     Not connected
  2. How many Bytes of memory does our 128MByte SD Card hold, represented as a decimal number? The "Mega" in MB means 220. How many bits of address does the SD Card need to give each Byte a unique address?

Internal Subsystem

We will be relying heavily on the MSSP subsystem, configured to run in SPI mode, to send and received bits from the SD card. We will talk more about the SPI subsystem in coming lectures. To get an overview of the nature of the serial communication between the PIC and the SD Card, consult Figure 15-5 of the PIC 18F26K22 Technical documents posted on the class web page.

Firmware Organization

Let's start by creating, configuring and including all the code that you will need for this week's lab. To do this, complete the following steps. Note, this week's lab has two .c files and a .h file, so pay close attention to the instructions at the end.
Experiment with the code as you read through the following text.

The firmware provided in today's lab allows you to perform some basic operations. Let's look at these by first pressing "?" to get the menu.
-------------------------------------------------
SD card address:  0000:0000
-------------------------------------------------
?: help menu
o: k
Z: Reset processor
z: Clear the terminal
----------------SPI TEST-------------------------
t: send a Test character over SPI
--------------SD CARD TESTS----------------------
i: Initialize SD card
a/A decrease/increase read address
r: read a block of 512 bytes from SD card
w: write a block of 512 bytes to SD card
-------------------------------------------------
Now let's look at the code which does all this to understand better how the code operated and how you may use this code template to write your program for this week's lab.
  1. Look for the call to SDCARD_Initialize in main. What two functions are called just prior? It's important that you call these two function prior to calling SDCARD_Initialize.
  2. Look for the definition of SDCARD_Initialize in sdcard.c Using the comments in this function, what are the four main things that this function does?
  3. Look in main and find the name of the function that prints out the contents of the SD card buffer when you use the "r" function. What is the name of this function and what is the data type of the argument that main passes to this function (sdCardBuffer)?
  4. For this lab, you will write out a function to spool the contents to the terminal, one decimal value per line. For example, the first 16 entries of my SD Card, when spooled out to the terminal, look like:
    128
    159
    187
    213
    233
    248
    255
    255
    248
    233
    213
    187
    159
    128
    97
    69
    
    This question asks you to estimate the time required to print out a block and the entire 128MB SD Card contents.
    • How many character per line are printed out if a line contains a single byte value (represented as a decimal value) terminated with a carriage return and line feed. Make a reasonable assumption - only a single answer will be accepted.
    • How many total characters are printed out to display an entire block of 512 bytes, one decimal value per row? How many bits does this correspond to?
    • Baud rate tells you how many bits per second are transferred. At 9600 Baud, how long will it take to display an entire block of 512 bytes, one decimal value per row?
    • How long would it take to display the entire 128MB of the SD Card, one decimal value per row at 9600 Baud? Represent your answer in hours (3-significant figures).
  5. I made the sdCardBuffer array a global variable even though its only used in main. Let's explore why using section 5.5.2 of the XC8 Compiler User's Guide posted on the class web page.
    • What is the difference between an auto and non-auto variable? You should probably search the Internet for help with this question.
    • What is the size of a memory bank in the PIC? Look at section 5.4 in the PIC18(L)F2X/4XK22 Data Sheet posted on the class web page. What is the size of the available data memory on the PIC? I found the answer to this question on the first page of the data sheet.
    • Auto variables must fit into one data bank and non-auto variables can be as large as the available data memory in the PIC. Assume that your program has a declaration of a array that looks like:
      uint8_t array[SIZE];
      What is the largest value of SIZE if array is a local variable? What is the largest value of SIZE if array is a global variable?
  6. Look at the code in main.c and provide the names of the functions that read and write blocks from/to the SD card respectively.

Firmware Experiments

There are a variety of reasons that the timer 0 interrupt service routine might get delayed from running, or accumulate unanticipated delays while executing. In the example code provided, I forced the ISR to have a big-ole-waste-of-time inside the ISR. As designed, the intention of the ISR was to have it called once every 100µs and set RA6 high while the CPU is in the TMR0 ISR. To check how well this is happening, measure the frequency of ISR calls using an oscilloscope configured as follows.
Ch1 probe RA6
Ch1 ground clip Dev board ground loop
Horizontal (scale) 25 us
Ch1 (scale) 1V
Trigger mode Auto
Trigger source 1
Trigger slope
Trigger level 1.5V
Make sure to:
  1. Measure the period between consecutive interrupts using the ISR as configured. Next make the following changes:
    • In main.c - comment out TMR0_WriteTimer(0x10000 - RATE);
    • In main.c - remove comment TMR0_WriteTimer(TMR0_ReadTimer() + (0x10000 - RATE));
    Now measure the interrupt period again. Put your finding in the table below.
    ISR timer adjustment Interrupt period
    TMR0_WriteTimer(0x10000 - RATE); 
    TMR0_WriteTimer(TMR0_ReadTimer() + (0x10000 - RATE));  
  2. Explain in your own words what causes the second form to be so much more accurate.

    An important note, you MUST configure timer 0 in MCC so that the TMR0H:TMR0L is 0 when you use the TMR0_WriteTimer(TMR0_ReadTimer() + (0x10000 - RATE)); form to set the duration to the next timer. This is because the timer is set to the Requested Period (from MCC) before your myTMR0ISR is called.

SD Card Read Block Interface

Let's look at how long it takes to read a block of 512 bytes from the SD card using the oscilloscope configured as follows.
Ch1 probe RC5
Ch1 ground clip Dev board ground loop
Horizontal (scale) 500us
Ch1 (scale) 1V
Trigger mode Auto
Trigger source 1
Trigger slope
Trigger level 1.5V
  1. How long does it take SDCARD_ReadBlock to read 512 bytes from the SD card? Divide this time by 512 to determine the effective read time per Byte. Represent your answer in microseconds.

SD Card Block Write

Let's look at how long it takes to write a block of 512 bytes to the SD card using the oscilloscope configured as follows.
Ch1 probe RC4
Ch1 ground clip Dev board ground loop
Horizontal (scale) 500us
Ch1 (scale) 1V
Trigger mode Auto
Trigger source 1
Trigger slope
Trigger level 1.5V
  1. How long does it take SDCARD_ReadBlock to write 512 bytes to the SD card? Divide this time by 512 to determine the effective write time per byte. Represent your answer in microseconds.