Skip to content

Commit

Permalink
Updated code to not use sd task, implemented stub for mounting comman…
Browse files Browse the repository at this point in the history
…d, and added bugfix for console
  • Loading branch information
danieldegrasse committed Aug 11, 2020
1 parent 0aab00d commit 32f3a28
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 38 deletions.
3 changes: 2 additions & 1 deletion cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include "cli.h"
#include "commands.h"
Expand Down Expand Up @@ -109,6 +108,8 @@ void start_cli(CLIContext *context) {
break;
case '\x1b':
cli_handle_esc(context);
// Required because some escape sequences update the line idx.
current_line = &(context->lines[context->line_idx]);
break;
default:
if (current_line->len >= CLI_MAX_LINE - 1) {
Expand Down
33 changes: 30 additions & 3 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* @file commands.c
* Implements CLI command handlers.
*/

#include <string.h>

#include "cli.h"

typedef struct {
Expand All @@ -10,8 +13,6 @@ typedef struct {
char *cmd_help;
} CmdEntry;

#include <string.h>

/*
* Maximum number of arguments that the parser will handle.
* Includes command name.
Expand All @@ -23,7 +24,8 @@ typedef struct {
*/
#define DELIMETER " "

static int help(CLIContext *cxt, char **argv, int argc);
static int help(CLIContext *ctx, char **argv, int argc);
static int sdcard(CLIContext *ctx, char **argv, int argc);

/**
* Declaration of commands. Syntax is as follows:
Expand All @@ -38,6 +40,8 @@ const CmdEntry COMMANDS[] = {
{"help", help,
"Prints help for this commandline.\r\n"
"supply the name of a command after \"help\" for help with that command"},
{"sdcard", sdcard,
"Manages the sdcard. Use \"sdcard mount\" to mount the sdcard"},
// Add more entries here.
{NULL, NULL, NULL}};

Expand Down Expand Up @@ -108,3 +112,26 @@ static int help(CLIContext *ctx, char **argv, int argc) {
cli_printf(ctx, "Unsupported number of arguments\r\n");
return 255;
}

/**
* SD card handler function. Allows the console to control the state of the
* SD card by mounting or unmounting it.
* @param ctx: CLI context to print to
* @param argv list of arguments
* @param argc argument count
* @return 0 on success, or another value on failure
*/
static int sdcard(CLIContext *ctx, char **argv, int argc) {
if (argc != 2) {
cli_printf(ctx, "Unsupported number of arguments\r\n");
return 255;
}
if (strncmp(argv[1], "mount", 5) == 0) {
cli_printf(ctx, "Attempting to mount sdcard...");
cli_printf(ctx, "Success!\r\n");
return 0;
} else {
cli_printf(ctx, "Unknown command %s. Try \"help sdcard\"\r\n", argv[1]);
return 255;
}
}
113 changes: 113 additions & 0 deletions sd_card.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* @file sd_card.c
* Implements all code required to managed the I/O as well as mounting and
* unmounting of the SD card.
*
* ============== Required Pins ==================:
* The SD card is driven via SPI. The following pins are used:
* CLK- PB4
* MISO- PB6
* MOSI- PB7
* CS- PA5
*/

/* XDCtools Header files */
#include <xdc/runtime/System.h>
#include <xdc/std.h>

/* Pthread support */
#include <ti/sysbios/posix/pthread.h>

/* TI-RTOS driver files */
#include <ti/drivers/SDSPI.h>

/* SD SPI driver */
#include <ti/mw/fatfs/ff.h>

#include <stdbool.h>

// Board header file
#include "Board.h"

// Drive number, as well as macros to convert it to a string
#define DRIVE_NUM 0
#define STR(n) #n


// Global variables.
pthread_cond_t sd_card_mounted;
pthread_mutex_t sd_card_access_mutex;

static bool sd_online(const char *drive_num, FATFS **fs);

/**
* Sets up required mutex and condition variables for SD card management.
* Should be called before BIOS starts.
*/
void sd_pthread_setup(void) {
pthread_cond_init(&sd_card_mounted, NULL);
if (pthread_mutex_init(&sd_card_access_mutex, NULL) != 0) {
System_abort("Failed to create SD write mutex\n");
}
}

/**
* Attempts to mount the SD card. If the mount succeeds, notifies any tasks
* waiting for the SD card to be mounted that it has been.
*/
bool attempt_sd_mount(void) {
// First, lock the sd card access mutex.
if (pthread_mutex_lock(&sd_card_access_mutex) != 0) {
System_abort("could not lock sd card mutex");
}
// Set up SPI bus.
Board_initSDSPI();
SDSPI_Handle sdspi_handle;
SDSPI_Params sdspi_params;
FIL logfile;
/* Try to mount the SD card. */
SDSPI_Params_init(&sdspi_params);
sdspi_handle = SDSPI_open(Board_SDSPI0, DRIVE_NUM, &sdspi_params);
if (sdspi_handle == NULL) {
System_abort("Error starting the SD card\n");
} else {
System_printf("SPI Bus for Drive %u started\n", DRIVE_NUM);
}
if (sd_online(STR(DRIVE_NUM), &(logfile.fs))) {
// Sd card did mount. Signal waiting tasks, and return true.
pthread_cond_broadcast(&sd_card_mounted);
return true;
} else {
// Sd card is not mounted. Undo SPI bus initialization.
SDSPI_close(sdspi_handle);
return false;
}
}

/**
* Checks if the SD card is online by attempting to check the free cluster
* count.
* @param drive_num: drive number as a string, use STR(DRIVE_NUM)
* @param fs: FATFS instance
*/
static bool sd_online(const char *drive_num, FATFS **fs) {
// Verify the SD card is online by getting the free cluster count.
FRESULT fresult;
DWORD free_cluster_count;
fresult = f_getfree(STR(DRIVE_NUM), &free_cluster_count, fs);
if (fresult) {
/*
* Failed to get the free cluster count.
* Assume SD card is not connected.
*/
System_printf("Failed to get free cluster count. Assuming SD card"
" is offline\n");
System_flush();
return false;
} else {
System_printf("SD card is online with %lu free clusters\n",
free_cluster_count);
System_flush();
return true;
}
}
23 changes: 23 additions & 0 deletions sd_card.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @file sd_card.h
* Implements all code required to managed the I/O as well as mounting and
* unmounting of the SD card.
*
* ============== Required Pins ==================:
* The SD card is driven via SPI. The following pins are used:
* CLK- PB4
* MISO- PB6
* MOSI- PB7
* CS- PA5
*/

#ifndef SD_CARD_H
#define SD_CARD_H

/**
* Sets up required mutex and condition variables for SD card management.
* Should be called before BIOS starts.
*/
void sd_pthread_setup(void);

#endif
5 changes: 4 additions & 1 deletion sd_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Board.h"

#include "uart_console_task.h"
#include "sd_card.h"



Expand All @@ -38,7 +39,9 @@ int main(void)
// Board_initUSB(Board_USBDEVICE);
// Board_initWatchdog();
// Board_initWiFi();
uart_prebios();
uart_task_prebios();
// Setup required pthread variables for the SD card.
sd_pthread_setup();
/* Turn on user LED */
GPIO_write(Board_LED0, Board_LED_ON);

Expand Down
14 changes: 9 additions & 5 deletions sd_logger.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ var mwConfig = xdc.useModule('ti.mw.Config');



/* ================ TI-RTOS drivers' configuration ================ */
/* ================ TI-RTOS drivers configuration ================ */
var driversConfig = xdc.useModule('ti.drivers.Config');
/*
* Include TI-RTOS drivers
Expand All @@ -500,6 +500,9 @@ driversConfig.libType = driversConfig.LibType_NonInstrumented;
//driversConfig.libType = driversConfig.LibType_Instrumented;


/* =============== Pthread support module =========== */
var Settings = xdc.useModule('ti.sysbios.posix.Settings');


/* ================ Task creation ================ */
/* Heartbeat task blinks the LED, simply as a sign of life. */
Expand All @@ -513,7 +516,8 @@ task1Params.instance.name = "uart_console";
// Stack must be larger to hold CLI history buffer.
task1Params.stackSize = 2048;
Program.global.uart_console = Task.create("&uart_task_entry", task1Params);
/* SD Task handles all management of the SD Card, included I/O and mounting */
var task2Params = new Task.Params();
task2Params.instance.name = "sd_task";
Program.global.sd_task = Task.create("&sd_task_entry", task2Params);


/* ================= Required Modules for FatFS support ============ */
var FatFS = xdc.useModule('ti.mw.fatfs.FatFS');
var Timestamp = xdc.useModule('xdc.runtime.Timestamp');
26 changes: 0 additions & 26 deletions sd_task.c

This file was deleted.

2 changes: 1 addition & 1 deletion uart_console_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static int uart_write(char* out, int n);
* UART device on linux will be ttyACM0.
* This code MUST be called before the BIOS is started.
*/
void uart_prebios(void)
void uart_task_prebios(void)
{
Board_initUART();
System_printf("Setup UART Device\n");
Expand Down
2 changes: 1 addition & 1 deletion uart_console_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
* UART device on linux will be ttyACM0.
* This code MUST be called before the BIOS is started.
*/
void uart_prebios(void);
void uart_task_prebios(void);

0 comments on commit 32f3a28

Please sign in to comment.