iSensor-SPI-Buffer  1.15
Firmware for the iSensor-SPI-Buffer board to enable full throughput buffered data capture on Analog Devices IMUs
Functions | Variables
sd_card.c File Reference

iSensor-SPI-Buffer SD card interfacing and script execution module More...

#include "reg.h"
#include "sd_card.h"
#include <stdio.h>
#include "script.h"
#include "fatfs.h"
#include "timer.h"
Include dependency graph for sd_card.c:

Functions

static bool OpenScriptFiles ()
 Mount SD card an open "script.txt" and "result.txt". More...
 
static bool SDCardAttached ()
 Check if SD card is attached. More...
 
static bool ParseScriptFile ()
 Parse script.txt into script element array (cmdList) More...
 
static bool CommandPostLoadProcess ()
 Process loaded command array. More...
 
static bool CreateResultFile ()
 Create new result file. More...
 
static void SPI3_Init (void)
 SPI3 Initialization Function (SD card master SPI port) More...
 
static void ParseReadBuffer (UINT bytesRead)
 Parse a script buffer read from the SD card. More...
 
void SD_Card_Tx_Handler (const uint8_t *buf, uint32_t count)
 SD card write handler function. More...
 
void SD_Card_Init ()
 Init SD card hardware interface and FATFs driver. More...
 
void SD_Card_Script_Autorun ()
 Run SD card script automatic execution process. More...
 
void SD_Card_Start_Script ()
 Start script run. More...
 
void SD_Card_Stop_Script ()
 Stop a running SD card script. More...
 
void SD_Card_Script_Step ()
 Step the script execution process. More...
 

Variables

SPI_HandleTypeDef g_spi3
 
static uint8_t sd_buf [STREAM_BUF_SIZE]
 
static script cmdList [SCRIPT_MAX_ENTRIES]
 
static uint32_t cmdIndex
 
static uint32_t numCmds
 
static uint32_t scriptRunning
 
static FIL cmdFile = {0}
 
static FIL outFile = {0}
 
static FATFS fs = {0}
 
static const uint8_t ScriptStart [] = "Script Starting...\r\n"
 

Detailed Description

iSensor-SPI-Buffer SD card interfacing and script execution module

Copyright (c) Analog Devices Inc, 2020 All Rights Reserved.

Date
7/28/2020
Author
A. Nolan (alex..nosp@m.nola.nosp@m.n@ana.nosp@m.log..nosp@m.com)

Function Documentation

◆ CommandPostLoadProcess()

static bool CommandPostLoadProcess ( )
static

Process loaded command array.

Returns
true if script is good, false otherwise

This function checks all commands for validity (argument and command). It also sets up all loop state variables, and links the end loop commands to the corresponding start loops.

Here is the caller graph for this function:

◆ CreateResultFile()

static bool CreateResultFile ( )
static

Create new result file.

Returns
true if file created, false otherwise

This function creates a unique result file for each script run. The result file number increments from 0 to 999.

◆ OpenScriptFiles()

static bool OpenScriptFiles ( )
static

Mount SD card an open "script.txt" and "result.txt".

Returns
true if files are opened successfully, false otherwise

This function first mounts an attached SD card, then populates the two file handles required for the application. If either file fails to open, both file handles are closed, and the SD card file system is unmounted.

◆ ParseReadBuffer()

static void ParseReadBuffer ( UINT  bytesRead)
static

Parse a script buffer read from the SD card.

Returns
void

The script read data to be parsed must be stored in sd_buf. This function iterates through the read array and calls the lower level ParseScriptElement() routine whenever a newline character is encountered.

Here is the caller graph for this function:

◆ ParseScriptFile()

static bool ParseScriptFile ( )
static

Parse script.txt into script element array (cmdList)

Returns
true if good script, false otherwise

This function loops through script.txt one read buffer at a time. Each read buffer is then parsed to find the command type and arguments. Any error flags set by ParseScriptElement will cause this function to return false. These flags can be set for invalid commands or arguments. In addition, any error found in command load post-process (which links all control flow related commands) will result in the script being cancelled.

Here is the call graph for this function:

◆ SD_Card_Init()

void SD_Card_Init ( )

Init SD card hardware interface and FATFs driver.

Returns
void

This function configures the SPI3 port for use with an SD card and inits all GPIO as needed. This function is called directly by the application on startup. It does not attempt to connect to the SD card in any way.lked right into tha

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SD_Card_Script_Autorun()

void SD_Card_Script_Autorun ( )

Run SD card script automatic execution process.

Returns
void

This function is intended to be called at the end of the initialization portion of main, just before entering the cyclic executive. If the SD card autorun bit is set in USB_CONFIG and the watchdog reset bit is not set in STATUS, the script will be run. The watchdog reset check is in place to remove the potential for a watchdog reset loop.

Here is the caller graph for this function:

◆ SD_Card_Script_Step()

void SD_Card_Script_Step ( )

Step the script execution process.

Returns
void

This function is called from the main loop. It is responsible for performing the actual script execution, and writing the output to the SD card (via Script_Run_Element with isUSB set to false)

Here is the call graph for this function:

◆ SD_Card_Start_Script()

void SD_Card_Start_Script ( )

Start script run.

Returns
void

This function checks if an SD card is connected, then sets up the FAT file system if there is a card. Once the file system is set up, "script.txt" is read and parsed, if it exists. Once the script has been processed, the script execution state variables are initialized, and the script is kicked off. The actual script execution work occurs in ScriptStep(), which is called periodically from the main cyclic executive loop.

◆ SD_Card_Stop_Script()

void SD_Card_Stop_Script ( )

Stop a running SD card script.

Returns
void

This function can be invoked by the user (via the script cancel command), or will run automatically once the script execution process has finished. It clears the script running flags and the script active STATUS register bit. It then closes the output text file and unmounts from the SD card.

Here is the caller graph for this function:

◆ SD_Card_Tx_Handler()

void SD_Card_Tx_Handler ( const uint8_t *  buf,
uint32_t  count 
)

SD card write handler function.

Parameters
bufBuffer containing data to write
countNumber of bytes to write
Returns
void

This function is called by the script execution routines, when a script object is executed from a SD card script context.

Here is the caller graph for this function:

◆ SDCardAttached()

static bool SDCardAttached ( )
static

Check if SD card is attached.

Returns
true if card is attached, false otherwise

This function makes use of the SD card detect pin to determine if an SD card is inserted without communicating to the SD card directly (floating if no SD card, pulled to ground if there is an SD card).

Here is the call graph for this function:

◆ SPI3_Init()

static void SPI3_Init ( void  )
static

SPI3 Initialization Function (SD card master SPI port)

Returns
void

Configures SPI 3 as master SPI port for SD card SPI mode: 0 SCLK Freq: 4.25MHz SPI word size: 8 bits

Here is the caller graph for this function:

Variable Documentation

◆ cmdFile

FIL cmdFile = {0}
static

Handle for command file (cmd.txt in top level directory)

◆ cmdIndex

uint32_t cmdIndex
static

Track index within the command list currently being executed

◆ cmdList

script cmdList[SCRIPT_MAX_ENTRIES]
static

Command list. Loaded from cmd.txt on the SD card

◆ fs

FATFS fs = {0}
static

File system object

◆ g_spi3

SPI_HandleTypeDef g_spi3

SPI handle for SD card master port (global scope)

◆ numCmds

uint32_t numCmds
static

Number of commands within the current script

◆ outFile

FIL outFile = {0}
static

Handle for output file (result.txt in top level directory)

◆ scriptRunning

uint32_t scriptRunning
static

Track if a script is actively running

◆ ScriptStart

const uint8_t ScriptStart[] = "Script Starting...\r\n"
static

String literal script start message

◆ sd_buf

uint8_t sd_buf[STREAM_BUF_SIZE]
static

Buffer for SD card read/writes