Add correct handling of sd card, add reset command, add ls command

This commit is contained in:
Mario Hüttel 2020-04-26 21:23:25 +02:00
parent 6e07a363f4
commit 4df68880f0
8 changed files with 111 additions and 13 deletions

View File

@ -43,7 +43,7 @@ CFILES += rotary-encoder.c
CFILES += stack-check.c CFILES += stack-check.c
CFILES += fatfs/diskio.c fatfs/ff.c fatfs/ffsystem.c fatfs/ffunicode.c fatfs/shimatta_sdio_driver/shimatta_sdio.c CFILES += fatfs/diskio.c fatfs/ff.c fatfs/ffsystem.c fatfs/ffunicode.c fatfs/shimatta_sdio_driver/shimatta_sdio.c
CFILES += pid-controller.c oven-driver.c CFILES += pid-controller.c oven-driver.c
CFILES += settings/settings.c settings-sd-card.c CFILES += settings/settings.c settings/settings-sd-card.c
DEFINES += -DDEBUGBUILD DEFINES += -DDEBUGBUILD

View File

@ -32,9 +32,9 @@ static struct sd_info card_info; // = {.type = CARD_NONE};
* @brief checkNotInserted * @brief checkNotInserted
* @return return 0 if card is inserted, else 1 * @return return 0 if card is inserted, else 1
*/ */
static int sdio_check_inserted() { int sdio_check_inserted() {
#if SDIO_ENABLE_INS #if SDIO_ENABLE_INS
return ((INS_PORT->IDR & INS_PIN) == (INS_ACTIVE_LEVEL<<INS_PIN) ? 0 : 1); return ((INS_PORT->IDR & (1<<INS_PIN)) == (INS_ACTIVE_LEVEL<<INS_PIN) ? 0 : 1);
#else #else
return 0; // Assume Card is inserted return 0; // Assume Card is inserted
#endif #endif
@ -46,7 +46,7 @@ static int sdio_check_inserted() {
*/ */
static int sdio_check_write_protection() { static int sdio_check_write_protection() {
#if SDIO_ENABLE_WRITEPROT #if SDIO_ENABLE_WRITEPROT
return ((WRITEPROT_PORT->IDR & WRITEPROT_PIN) == (WRITEPROT_ACTIVE_LEVEL<<WRITEPROT_PIN) ? 1 : 0); return ((WRITEPROT_PORT->IDR & (1<<WRITEPROT_PIN)) == (WRITEPROT_ACTIVE_LEVEL<<WRITEPROT_PIN) ? 1 : 0);
#else #else
return 0; // Assume Card is not write protected return 0; // Assume Card is not write protected
#endif #endif

View File

@ -20,6 +20,8 @@ DRESULT sdio_disk_write(const BYTE *buff, DWORD sector, UINT count);
DRESULT sdio_disk_ioctl(BYTE cmd, void* buff); DRESULT sdio_disk_ioctl(BYTE cmd, void* buff);
DWORD get_fattime(); DWORD get_fattime();
int sdio_check_inserted();
//Defines for Card Status in struct _CardStatus //Defines for Card Status in struct _CardStatus
#define CURRENT_STATE_IDLE 0 #define CURRENT_STATE_IDLE 0

View File

@ -24,7 +24,7 @@
/* Port Definitions */ /* Port Definitions */
#define PORTCLKMASK (RCC_AHB1ENR_GPIODEN | RCC_AHB1ENR_GPIOCEN) #define PORTCLKMASK (RCC_AHB1ENR_GPIODEN | RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIOAEN)
#define ALTFUNC 12 #define ALTFUNC 12
@ -50,10 +50,10 @@
#define WRITEPROT_ACTIVE_LEVEL 0 #define WRITEPROT_ACTIVE_LEVEL 0
// Card inserted pin // Card inserted pin
#define SDIO_ENABLE_INS 0 #define SDIO_ENABLE_INS 1
#define INS_PORT GPIOD // Add this port to port clock mask! #define INS_PORT GPIOA // Add this port to port clock mask!
#define INS_PIN 0 #define INS_PIN 8
#define INS_PULLUP 0 #define INS_PULLUP 1
#define INS_ACTIVE_LEVEL 0 #define INS_ACTIVE_LEVEL 0

View File

@ -22,4 +22,6 @@
#ifndef __SETTINGS_SETTINGS_H__ #ifndef __SETTINGS_SETTINGS_H__
#define __SETTINGS_SETTINGS_H__ #define __SETTINGS_SETTINGS_H__
settings_save_calibration();
#endif /* __SETTINGS_SETTINGS_H__ */ #endif /* __SETTINGS_SETTINGS_H__ */

View File

@ -30,4 +30,6 @@ void shell_handle_input(shellmatta_handle_t shell, const char *data, size_t len)
void shell_print_string(shellmatta_handle_t shell, const char *string); void shell_print_string(shellmatta_handle_t shell, const char *string);
void shell_print_motd(shellmatta_handle_t shell);
#endif /* __SHELL_H__ */ #endif /* __SHELL_H__ */

View File

@ -34,6 +34,7 @@
#include <reflow-controller/shell.h> #include <reflow-controller/shell.h>
#include <reflow-controller/pid-controller.h> #include <reflow-controller/pid-controller.h>
#include <reflow-controller/digio.h> #include <reflow-controller/digio.h>
#include "fatfs/shimatta_sdio_driver/shimatta_sdio.h"
#include <reflow-controller/temp-converter.h> #include <reflow-controller/temp-converter.h>
#include <reflow-controller/rotary-encoder.h> #include <reflow-controller/rotary-encoder.h>
#include <stm-periph/stm32-gpio-macros.h> #include <stm-periph/stm32-gpio-macros.h>
@ -60,6 +61,9 @@ static uint32_t rot;
static volatile float pid_out; static volatile float pid_out;
static volatile float current_temperature; static volatile float current_temperature;
FATFS fs;
FATFS *fs_ptr = &fs;
static inline void uart_gpio_config() static inline void uart_gpio_config()
{ {
#ifdef DEBUGBUILD #ifdef DEBUGBUILD
@ -104,11 +108,32 @@ static inline void setup_sell_uart(struct stm_uart *uart)
NVIC_EnableIRQ(DMA2_Stream7_IRQn); NVIC_EnableIRQ(DMA2_Stream7_IRQn);
} }
static bool mount_sd_card_if_avail(bool mounted)
{
FRESULT res;
if (sdio_check_inserted() && mounted) {
memset(fs_ptr, 0, sizeof(FATFS));
return false;
}
if (!sdio_check_inserted() && !mounted) {
res = f_mount(fs_ptr, "0:/", 1);
if (res == FR_OK) {
return true;
} else {
return false;
}
}
return mounted;
}
const char *oven_controller_hello_world = "Hello world :)\n"; const char *oven_controller_hello_world = "Hello world :)\n";
int main() int main()
{ {
FATFS fs; bool sd_card_mounted = false;
FIL test_file; FIL test_file;
const char *uart_input; const char *uart_input;
size_t uart_input_len; size_t uart_input_len;
@ -132,8 +157,10 @@ int main()
setup_sell_uart(&shell_uart); setup_sell_uart(&shell_uart);
shell_handle = shell_init(write_shell_callback); shell_handle = shell_init(write_shell_callback);
shell_print_motd(shell_handle);
if (f_mount(&fs, "0:/", 1) == FR_OK) { if (f_mount(fs_ptr, "0:/", 1) == FR_OK) {
sd_card_mounted = true;
f_open(&test_file, "hello-world.txt", FA_OPEN_APPEND | FA_WRITE); f_open(&test_file, "hello-world.txt", FA_OPEN_APPEND | FA_WRITE);
f_write(&test_file, oven_controller_hello_world, strlen(oven_controller_hello_world), NULL); f_write(&test_file, oven_controller_hello_world, strlen(oven_controller_hello_world), NULL);
f_close(&test_file); f_close(&test_file);
@ -143,6 +170,9 @@ int main()
pid_zero(&pid); pid_zero(&pid);
while (1) { while (1) {
sd_card_mounted = mount_sd_card_if_avail(sd_card_mounted);
pt1000_value_status = adc_pt1000_get_current_resistance(&pt1000_value); pt1000_value_status = adc_pt1000_get_current_resistance(&pt1000_value);
if (pt1000_value_status >= 0) { if (pt1000_value_status >= 0) {

View File

@ -18,6 +18,8 @@
* If not, see <http://www.gnu.org/licenses/>. * If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stm32/stm32f4xx.h>
#include <cmsis/core_cm4.h>
#include <reflow-controller/shell.h> #include <reflow-controller/shell.h>
#include <stm-periph/uart.h> #include <stm-periph/uart.h>
#include <string.h> #include <string.h>
@ -30,7 +32,7 @@
#include <stm-periph/unique-id.h> #include <stm-periph/unique-id.h>
#include <reflow-controller/calibration.h> #include <reflow-controller/calibration.h>
#include <reflow-controller/temp-converter.h> #include <reflow-controller/temp-converter.h>
#include <fatfs/ff.h>
#include <reflow-controller/stack-check.h> #include <reflow-controller/stack-check.h>
#include <reflow-controller/rotary-encoder.h> #include <reflow-controller/rotary-encoder.h>
@ -290,6 +292,43 @@ static shellmatta_retCode_t shell_cmd_pt1000_res_loop(const shellmatta_handle_t
return SHELLMATTA_OK; return SHELLMATTA_OK;
} }
static shellmatta_retCode_t shell_cmd_ls(const shellmatta_handle_t handle, const char *arguments,
uint32_t length)
{
(void)length;
(void)arguments;
DIR dir;
FRESULT res;
FILINFO fno;
res = f_opendir(&dir, "/");
if (res != FR_OK) {
shellmatta_printf(handle, "Filesystem inaccessible. Is an SD Card inserted?\r\n");
return SHELLMATTA_OK;
}
while (f_readdir(&dir, &fno) == FR_OK) {
if (fno.fname[0] == 0)
break;
shellmatta_printf(handle, "%c\t%s\r\n", (fno.fattrib & AM_DIR ? 'd' : 'f'), fno.fname);
}
f_closedir(&dir);
return SHELLMATTA_OK;
}
static shellmatta_retCode_t shell_cmd_reset(const shellmatta_handle_t handle, const char *arguments,
uint32_t length)
{
(void)handle;
(void)length;
(void)arguments;
NVIC_SystemReset();
return SHELLMATTA_BUSY;
}
//typedef struct shellmatta_cmd //typedef struct shellmatta_cmd
//{ //{
// char *cmd; /**< command name */ // char *cmd; /**< command name */
@ -300,7 +339,7 @@ static shellmatta_retCode_t shell_cmd_pt1000_res_loop(const shellmatta_handle_t
// struct shellmatta_cmd *next; /**< pointer to next command or NULL */ // struct shellmatta_cmd *next; /**< pointer to next command or NULL */
//} shellmatta_cmd_t; //} shellmatta_cmd_t;
static shellmatta_cmd_t cmd[10] = { static shellmatta_cmd_t cmd[12] = {
{ {
.cmd = "version", .cmd = "version",
.cmdAlias = "ver", .cmdAlias = "ver",
@ -379,8 +418,25 @@ static shellmatta_cmd_t cmd[10] = {
.helpText = "Get current rotary encoder value", .helpText = "Get current rotary encoder value",
.usageText = "", .usageText = "",
.cmdFct = shell_cmd_rot, .cmdFct = shell_cmd_rot,
.next = &cmd[10],
},
{
.cmd = "ls",
.cmdAlias = NULL,
.helpText = "List filesystem contents",
.usageText = "",
.cmdFct = shell_cmd_ls,
.next = &cmd[11],
},
{
.cmd = "reset",
.cmdAlias = NULL,
.helpText = "Reset controller",
.usageText = "Resets the controller",
.cmdFct = shell_cmd_reset,
.next = NULL, .next = NULL,
} }
}; };
shellmatta_handle_t shell_init(shellmatta_write_t write_func) shellmatta_handle_t shell_init(shellmatta_write_t write_func)
@ -396,6 +452,12 @@ shellmatta_handle_t shell_init(shellmatta_write_t write_func)
return handle; return handle;
} }
void shell_print_motd(shellmatta_handle_t shell)
{
shellmatta_printf(shell, "\r\nShimatta 仕舞った Reflow Controller ready\r\n\r\n");
shell_cmd_ver(shell, NULL, 0UL);
shell_handle_input(shell, "\r\n", 2UL);
}
void shell_handle_input(shellmatta_handle_t shell, const char *data, size_t len) void shell_handle_input(shellmatta_handle_t shell, const char *data, size_t len)
{ {