SD card: initial shoehorning of SD code, part 1.
Part 1 is, implement - M20: List SD card. - M21: Initialize SD card (has to be done before listing). - M22: Release SD card. Do all this in one chunk, splitting this up wouldn't allow to test the result.
This commit is contained in:
parent
03e720d2ac
commit
8f2e1e59ac
|
|
@ -26,6 +26,8 @@
|
||||||
#include "clock.h"
|
#include "clock.h"
|
||||||
#include "config_wrapper.h"
|
#include "config_wrapper.h"
|
||||||
#include "home.h"
|
#include "home.h"
|
||||||
|
#include "sd.h"
|
||||||
|
|
||||||
|
|
||||||
/// the current tool
|
/// the current tool
|
||||||
uint8_t tool;
|
uint8_t tool;
|
||||||
|
|
@ -374,6 +376,30 @@ void process_gcode_command() {
|
||||||
tool = next_tool;
|
tool = next_tool;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
#ifdef SD
|
||||||
|
case 20:
|
||||||
|
//? --- M20: list SD card. ---
|
||||||
|
sd_list("/");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 21:
|
||||||
|
//? --- M21: initialise SD card. ---
|
||||||
|
//?
|
||||||
|
//? Has to be done before doing any other operation, including M20.
|
||||||
|
sd_mount();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 22:
|
||||||
|
//? --- M22: release SD card. ---
|
||||||
|
//?
|
||||||
|
//? Not mandatory. Just removing the card is fine, but results in
|
||||||
|
//? odd behaviour when trying to read from the card anyways. M22
|
||||||
|
//? makes also sure SD card printing is disabled, even with the card
|
||||||
|
//? inserted.
|
||||||
|
sd_unmount();
|
||||||
|
break;
|
||||||
|
#endif /* SD */
|
||||||
|
|
||||||
case 82:
|
case 82:
|
||||||
//? --- M82 - Set E codes absolute ---
|
//? --- M82 - Set E codes absolute ---
|
||||||
//?
|
//?
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
/---------------------------------------------------------------------------*/
|
/---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
#define _USE_READ 1 /* Enable pf_read() function */
|
#define _USE_READ 1 /* Enable pf_read() function */
|
||||||
#define _USE_DIR 0 /* Enable pf_opendir() and pf_readdir() function */
|
#define _USE_DIR 1 /* Enable pf_opendir() and pf_readdir() function */
|
||||||
#define _USE_LSEEK 0 /* Enable pf_lseek() function */
|
#define _USE_LSEEK 0 /* Enable pf_lseek() function */
|
||||||
#define _USE_WRITE 0 /* Enable pf_write() function */
|
#define _USE_WRITE 0 /* Enable pf_write() function */
|
||||||
|
|
||||||
|
|
|
||||||
57
sd.c
57
sd.c
|
|
@ -6,10 +6,67 @@
|
||||||
|
|
||||||
#ifdef SD
|
#ifdef SD
|
||||||
|
|
||||||
|
#include "delay.h"
|
||||||
|
#include "serial.h"
|
||||||
|
#include "sersendf.h"
|
||||||
|
|
||||||
|
|
||||||
|
static FATFS sdfile;
|
||||||
|
static FRESULT result;
|
||||||
|
|
||||||
|
/** Initialize SPI for SD card reading.
|
||||||
|
*/
|
||||||
void sd_init(void) {
|
void sd_init(void) {
|
||||||
WRITE(SD_CARD_SELECT_PIN, 1);
|
WRITE(SD_CARD_SELECT_PIN, 1);
|
||||||
SET_OUTPUT(SD_CARD_SELECT_PIN);
|
SET_OUTPUT(SD_CARD_SELECT_PIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Mount the SD card.
|
||||||
|
*/
|
||||||
|
void sd_mount(void) {
|
||||||
|
result = pf_mount(&sdfile);
|
||||||
|
if (result != FR_OK)
|
||||||
|
sersendf_P(PSTR("E: SD init failed. (%su)"), result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Unmount the SD card.
|
||||||
|
|
||||||
|
This makes just sure subsequent reads to the card do nothing, instead of
|
||||||
|
trying and failing. Not mandatory, just removing the card is fine, as well
|
||||||
|
as inserting and mounting another one without previous unmounting.
|
||||||
|
*/
|
||||||
|
void sd_unmount(void) {
|
||||||
|
pf_unmount(&sdfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 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) {
|
||||||
|
FILINFO fno;
|
||||||
|
DIR dir;
|
||||||
|
|
||||||
|
serial_writechar('\n');
|
||||||
|
result = pf_opendir(&dir, path);
|
||||||
|
if (result == FR_OK) {
|
||||||
|
for (;;) {
|
||||||
|
result = pf_readdir(&dir, &fno);
|
||||||
|
if (result != FR_OK || fno.fname[0] == 0)
|
||||||
|
break;
|
||||||
|
serial_writestr((uint8_t *)fno.fname);
|
||||||
|
if (fno.fattrib & AM_DIR)
|
||||||
|
serial_writechar('/');
|
||||||
|
serial_writechar('\n');
|
||||||
|
delay_ms(2); // Time for sending the characters.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sersendf_P(PSTR("E: failed to open dir. (%su)"), result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* SD */
|
#endif /* SD */
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue