Terminal base finished

This commit is contained in:
Angoosh Leviocki 2021-07-15 15:48:16 +02:00
parent 0758e3fac8
commit 3d4681f82d
2 changed files with 263 additions and 2 deletions

View File

@ -8,11 +8,21 @@
#include "main.h"
void myprintf(const char *fmt, ...);
FRESULT ls_files (char*);
uint8_t pointr = 0;
uint8_t RX[1];
uint8_t termRxBuffer[TERM_BUFFER_SIZE];
FATFS FatFs; //Fatfs handle
FIL fil; //File handle
DIR dir; //Directory handle
FRESULT fres; //Result after operations
char currentDir[128];
char lastDir[128];
UART_HandleTypeDef huart3;
RTC_HandleTypeDef hrtc;
@ -43,10 +53,21 @@ void PrintRxBuffer(){
pointr = 0;
}
void myprintf(const char *fmt, ...) {
static char buffer[256];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
int len = strlen(buffer);
HAL_UART_Transmit(&huart3, (uint8_t*)buffer, len, -1);
}
char dateChar[9];
char timeChar[9];
void get_time(void)
void get_time(char* timeChar, char* dateChar)
{
RTC_DateTypeDef gDate;
RTC_TimeTypeDef gTime;
@ -63,14 +84,18 @@ void get_time(void)
* @brief Read rx buffer and parse commands
*
* commands:
* mount/umount - mount unmount sd card
* ls - list fatfs drive
* mkdir - make fatfs directory
* touch - create fatfs file
* rm - remove file
* echo - print contents of file / write to file
* cd - change directory
* reset - reset MCU
* gpio - gpio operations as set, reset, input, output
* time - print local time
* errls - list all errors
* errclr - clear all errors
* help - print all commands
*/
void ReadBuffer(){
@ -79,18 +104,31 @@ void ReadBuffer(){
char gpio[] = {"gpio"};
char help[] = {"help"};
char time[] = {"time"};
char ls[] = {"ls"};
char mount[] = {"mount"};
char umount[] = {"umount"};
char touch[] = {"touch"};
char mkdir[] = {"mkdir"};
char cd[] = {"cd"};
char rm[] = {"rm"};
char echo[] = {"echo"};
for(int i = 0; i < TERM_BUFFER_SIZE; i++){
char chachar[TERM_BUFFER_SIZE];
for(int i = 0; i < TERM_BUFFER_SIZE; i++){
chachar[i] = 0;
}
for(int j = 0; j < (TERM_BUFFER_SIZE - i); j++){
chachar[j] = termRxBuffer[j+i];
}
if(chachar[0] == '\n'){}
else if(strncmp(reset, chachar, 5) == 0){
i+=5;
HAL_UART_Transmit(&huart3, msgOk, 3, 0xFFFF);
HAL_NVIC_SystemReset();
}
else if(strncmp(gpio, chachar, 4) == 0){
i+=4;
char pinStr[2];
uint pin;
uint pinBinary = 1;
@ -278,12 +316,15 @@ void ReadBuffer(){
HAL_UART_Transmit(&huart3, msgBlank, sizeof(msgBlank)-1, 0xFFFF);
}
else if(strncmp(help, chachar, 4) == 0){
i+=4;
uint8_t msgHelp[] = {"\ncommands:\n\
* mount/umount - mount unmount sd card\n\
* ls - list fatfs drive\n\
* mkdir - make fatfs directory\n\
* touch - create fatfs file\n\
* rm - remove file\n\
* echo - print contents of file / write to file\n\
* cd - change directory\n\
* reset - reset MCU\n\
* gpio - gpio operations as set, reset, input, output\n\
* time - print local time\n\
@ -294,10 +335,11 @@ void ReadBuffer(){
HAL_UART_Transmit(&huart3, msgBlank, sizeof(msgBlank)-1, 0xFFFF);
}
else if(strncmp(time, chachar, 4) == 0){
i+=4;
uint8_t msgSystime[] = {"\nSystem time: "};
uint8_t msgSysdate[] = {"\nSystem date: "};
get_time();
get_time(timeChar, dateChar);
HAL_UART_Transmit(&huart3, msgSystime, sizeof(msgSystime)-1, 0xFFFF);
HAL_UART_Transmit(&huart3, timeChar, sizeof(timeChar)-1, 0xFFFF);
HAL_UART_Transmit(&huart3, msgSysdate, sizeof(msgSysdate)-1, 0xFFFF);
@ -305,6 +347,188 @@ void ReadBuffer(){
HAL_UART_Transmit(&huart3, &msgNL, sizeof(msgNL), 0xFFFF);
HAL_UART_Transmit(&huart3, msgBlank, sizeof(msgBlank)-1, 0xFFFF);
}
else if(strncmp(mount, chachar, 5) == 0){
i+=5;
strcpy(currentDir, "/");
fres = f_mount(&FatFs, "/", 1); //1=mount now
if (fres != FR_OK) {
myprintf("\nf_mount error (%i)\r\n> ", fres);
}
else{
myprintf("\nMMC mounted\n> ");
}
}
else if(strncmp(umount, chachar, 6) == 0){
i+=6;
f_mount(NULL, "", 0);
myprintf("\nMMC unmounted\n> ");
}
else if(strncmp(ls, chachar, 2) == 0){
i+=2;
myprintf("\n");
char buff[256];
strcpy(buff, currentDir);
fres = ls_files(buff);
myprintf("\n> ");
}
else if(strncmp(touch, chachar, 5) == 0){
i+=5;
char filename[64];
char filebuff[64];
for(int j = 0; j < 64; j++){
filename[j] = 0;
filebuff[j] = 0;
}
for(int j = 0; j < 64; j++){
if(chachar[6+j] != 0){
filebuff[j] = chachar[6+j];
}
}
sscanf(filebuff, "%s", filename);
fres = f_open(&fil, filename, FA_CREATE_NEW);
myprintf("\nFile created\n> ");
f_close(&fil);
}
else if(strncmp(mkdir, chachar, 5) == 0){
i+=5;
char dirname[64];
char dirbuff[64];
for(int j = 0; j < 64; j++){
dirname[j] = 0;
dirbuff[j] = 0;
}
for(int j = 0; j < 64; j++){
if(chachar[6+j] != 0){
dirbuff[j] = chachar[6+j];
}
}
sscanf(dirbuff, "%s", dirname);
fres = f_mkdir(dirname);
myprintf("\nDirectory created\n> ");
}
else if(strncmp(cd, chachar, 2) == 0){
i+=2;
char path[64];
char pathbuff[64];
for(int j = 0; j < 64; j++){
path[j] = 0;
pathbuff[j] = 0;
}
for(int j = 0; j < 64; j++){
if(chachar[2+j] != 0){
pathbuff[j] = chachar[2+j];
}
}
sscanf(pathbuff, "%s", path);
fres = f_chdir(path);
if(fres == FR_OK){
myprintf("\nDirectory changed\n> ");
strcpy(currentDir, path);
}
else{
myprintf("\nf_mount error (%i)\r\n> ", fres);
}
}
else if(strncmp(rm, chachar, 2) == 0){
i+=2;
char path[64];
char pathbuff[64];
for(int j = 0; j < 64; j++){
path[j] = 0;
pathbuff[j] = 0;
}
for(int j = 0; j < 64; j++){
if(chachar[2+j] != 0){
pathbuff[j] = chachar[2+j];
}
}
sscanf(pathbuff, "%s", path);
fres = f_unlink(path);
if(fres == FR_OK){
myprintf("\nRemoved\n> ");
}
else{
myprintf("\nf_mount error (%i)\r\n> ", fres);
}
}
else if(strncmp(echo, chachar, 4) == 0){
char srcbuff[256];
char destbuff[256];
char dest[256];
char src[256];
uint8_t srcSize = 0;
for(int j = 0; j < 256; j++){
srcbuff[j] = 0;
destbuff[j] = 0;
}
if(chachar[5] != 0){
for(int j = 0; j < 250; j++){
if(chachar[6+j] != '"'){
srcbuff[j] = chachar[6+j];
srcSize++;
}
else{
break;
}
}
for(int j = 0; j < (251-3-srcSize); j++){
if(chachar[6+srcSize+j] != 0){
destbuff[j] = chachar[6+3+srcSize+j];
}
}
sscanf(destbuff, "%s", dest);
if(chachar[6+srcSize+2] == '>'){
fres = f_open(&fil, dest, FA_OPEN_APPEND | FA_WRITE);
if(fres == FR_OK){
int len = strlen(srcbuff);
uint bytesWrote;
fres = f_puts(srcbuff, &fil);
if(fres > 0){
myprintf("\nSuccess\n> ");
fres = f_putc('\n', &fil);
f_close(&fil);
}
else{
myprintf("\f_write error (%i)\r\n> ", fres);
}
}
else{
myprintf("\nf_open error (%i)\r\n> ", fres);
}
}
else if(chachar[6+srcSize+2] == 0){
sscanf(srcbuff, "%s", src);
fres = f_open(&fil, src, FA_READ);
if(fres == FR_OK){
char readBuffer[256];
for(int j = 0; j < 256; j++){
readBuffer[j] = 0;
}
TCHAR * ptrchck = 1;
while(ptrchck != NULL){
ptrchck = f_gets((TCHAR*)readBuffer,256,&fil);
myprintf("%s", readBuffer);
}
f_close(&fil);
myprintf("\n> ");
}
else{
myprintf("\nFile: %s", src);
myprintf("\nf_open error (%i)\r\n> ", fres);
}
}
else{
myprintf("\n %d %d", chachar[6+srcSize+2], srcSize);
myprintf("\nNothing to echo\n> ");
}
}
else{
myprintf("\nNothing to echo\n> ");
}
}
}
for(int i = 0; i < TERM_BUFFER_SIZE; i++){
termRxBuffer[i] = 0;
@ -331,3 +555,36 @@ void TerminalInit(){
HAL_UART_Transmit(&huart3, &msgNL, sizeof(msgNL), 0xFFFF);
HAL_UART_Transmit(&huart3, msgBlank, sizeof(msgBlank)-1, 0xFFFF);
}
//FATFS
FRESULT ls_files (
char* path /* Start node to be scanned (***also used as work area***) */
)
{
FRESULT res;
DIR dir;
UINT i;
static FILINFO fno;
res = f_opendir(&dir, path); /* Open the directory */
if (res == FR_OK) {
for (;;) {
res = f_readdir(&dir, &fno); /* Read a directory item */
if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */
if (fno.fattrib & AM_DIR) { /* It is a directory */
i = strlen(path);
sprintf(&path[i], "/%s", fno.fname);
res = ls_files(path); /* Enter the directory */
if (res != FR_OK) break;
path[i] = 0;
} else { /* It is a file. */
myprintf("%s/%s\n", path, fno.fname);
}
}
f_closedir(&dir);
}
return res;
}

View File

@ -11,6 +11,8 @@
//#include "main.h"
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include "fatfs.h"
#define LIB_VERSION "0.1"
@ -20,6 +22,8 @@
void PrintRxBuffer();
void ReadBuffer();
void TerminalInit();
void myprintf(const char *fmt, ...);
void get_time(char* timeChar, char* dateChar);
#endif /* INC_TERMINAL_H_ */