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"
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)
◆ 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.
◆ 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.
◆ 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.
◆ 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
◆ 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.
◆ 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)
◆ 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.
◆ SD_Card_Tx_Handler()
void SD_Card_Tx_Handler |
( |
const uint8_t * |
buf, |
|
|
uint32_t |
count |
|
) |
| |
SD card write handler function.
- Parameters
-
buf | Buffer containing data to write |
count | Number 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.
◆ 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).
◆ 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
◆ cmdFile
Handle for command file (cmd.txt in top level directory)
◆ cmdIndex
Track index within the command list currently being executed
◆ cmdList
Command list. Loaded from cmd.txt on the SD card
◆ fs
◆ g_spi3
SPI handle for SD card master port (global scope)
◆ numCmds
Number of commands within the current script
◆ outFile
Handle for output file (result.txt in top level directory)
◆ scriptRunning
Track if a script is actively running
◆ ScriptStart
const uint8_t ScriptStart[] = "Script Starting...\r\n" |
|
static |
String literal script start message
◆ sd_buf
Buffer for SD card read/writes