From ad2a76b2df71ab9e3ab05b36483a724c4863f444 Mon Sep 17 00:00:00 2001 From: Phil Hord Date: Mon, 13 Jul 2015 13:53:35 +0200 Subject: [PATCH] Add simulator SD card (and SPI, PFF) support. Add simulator for SD card driver, plus add stubs for SPI and PFF primarily to prevent the on-device code from being picked up by the makefiles for the simulator. --- pff.h | 3 +- simulator/pff_diskio_sim.c | 0 simulator/pff_sim.c | 0 simulator/sd_sim.c | 118 +++++++++++++++++++++++++++++++++++++ simulator/spi_sim.c | 9 +++ spi.h | 3 + 6 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 simulator/pff_diskio_sim.c create mode 100644 simulator/pff_sim.c create mode 100644 simulator/sd_sim.c create mode 100644 simulator/spi_sim.c diff --git a/pff.h b/pff.h index 168d3e5..5a61bca 100644 --- a/pff.h +++ b/pff.h @@ -18,6 +18,7 @@ #include "sd.h" #ifdef SD +#ifndef SIMULATOR #ifndef _PFATFS #define _PFATFS 4004 /* Revision ID */ @@ -164,5 +165,5 @@ FRESULT pf_readdir (DIR* dj, FILINFO* fno); /* Read a directory #endif #endif /* _PFATFS */ - +#endif /* SIMULATOR */ #endif /* SD */ diff --git a/simulator/pff_diskio_sim.c b/simulator/pff_diskio_sim.c new file mode 100644 index 0000000..e69de29 diff --git a/simulator/pff_sim.c b/simulator/pff_sim.c new file mode 100644 index 0000000..e69de29 diff --git a/simulator/sd_sim.c b/simulator/sd_sim.c new file mode 100644 index 0000000..7d23f83 --- /dev/null +++ b/simulator/sd_sim.c @@ -0,0 +1,118 @@ + +/** \file SD card simulator +*/ + +#include "sd.h" + +#ifdef SD + +#include "delay.h" +#include "serial.h" +#include "sersendf.h" +#include "gcode_parse.h" + +#include +#include +#include +#include + +/** Initialize SPI for SD card reading. +*/ +void sd_init(void) { + sim_info("SD card mounted (simulated)."); +} + +void sd_mount(void) {} + +void sd_unmount(void) {} + +/** List a given directory. + + \param path The path to list. Toplevel path is "/". + + A slash is added to directory names, to make it easier for users to + recognize them. +*/ +void sd_list(const char* path) { + DIR *dir; + + dir = opendir(path); + serial_writechar('\n'); + if (dir) { + for (;;) { + struct dirent *de = readdir(dir); + if (!de) + break; + serial_writestr((uint8_t *)de->d_name); + #ifdef _DIRENT_HAVE_D_TYPE + if (de->d_type & DT_DIR) + serial_writechar('/'); + #endif + serial_writechar('\n'); + } + } + else { + sim_info("E: failed to open dir. (%s, %u)", path, errno); + } +} + +/** Open a file for reading. + + \param filename Name of the file to open and to read G-code from. +*/ +static FILE *f; +void sd_open(const char* filename) { + if (f) fclose(f); + f = fopen(filename, "rb"); + if (!f) { + sim_info("E: failed to open file. (%s, %u)", filename, errno); + } +} + +uint8_t gcode_parse_char_sd(uint8_t c) { + return gcode_parse_char(c); +} + +/** Read a line of G-code from a file. + + \param A pointer to the parser function. This function should accept an + uint8_t with the character to parse and return an uint8_t whether + end of line (EOL) was reached. + + \return Whether end of line (EOF) was reached or an error happened. +*/ +uint8_t sd_read_gcode_line(void) { + char str[1024] ; + if (!f) { + sim_info("E: file not open."); + return 1; + } + char *s = fgets(str, sizeof(str), f); + if (!s) { + sim_info("SD card: reached EOF."); + return 1; + } + + while (*s) + gcode_parse_char_sd((uint8_t)*s++); + return 0; +} + +/** Read a character from a file. + + \return The character read, or zero if there is no such character (e.g. EOF). +*/ +uint8_t sd_read_char(void) { + if (!f) { + sim_info("E: file not open"); + return 1; + } + char ch = fgetc(f); + if (ch == EOF) { + sim_info("SD card: reached EOF"); + return 0; + } + return (uint8_t) ch; +} + +#endif /* SD */ diff --git a/simulator/spi_sim.c b/simulator/spi_sim.c new file mode 100644 index 0000000..d1f50ef --- /dev/null +++ b/simulator/spi_sim.c @@ -0,0 +1,9 @@ + +/** \file + \brief Simulated SPI subsystem +*/ +#include "spi.h" +#include "arduino.h" + +void spi_init() { +} diff --git a/spi.h b/spi.h index a301296..fb5faec 100644 --- a/spi.h +++ b/spi.h @@ -12,6 +12,8 @@ */ void spi_init(void); +#ifndef SIMULATOR /* Avoid inlining code on simulator. */ + /** SPI device selection. Because out famous WRITE() macro works with constant pins, only, we define @@ -99,4 +101,5 @@ inline uint8_t spi_rw(uint8_t byte) { return SPDR; } +#endif /* SIMULATOR */ #endif /* _SPI_H */