/* * Terminal.c * * Created on: Jul 13, 2021 * Author: angoosh */ #include "Terminal.h" #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; uint8_t msgOk[] = {"\nOK\n"}; uint8_t msgBlank[] = {"> "}; uint8_t msgNoGpioPort[] = {"\nNo such GPIO port\n"}; uint8_t msgUnknownParameter[] = {"\nUnknown parameter\n"}; uint8_t msgNL = '\n'; /** * @brief Callback for uart byte by byte receive */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){ termRxBuffer[pointr] = RX[0]; pointr++; HAL_UART_Receive_IT(&huart3, RX, 1); } /** * @brief Readout whole receive buffer, for debug */ void PrintRxBuffer(){ for(int l = 0; l ", 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; } } /** * @brief Initialize terminal */ void TerminalInit(){ uint8_t msgWelcome[] = {"\nWelcome to terminal utility by Angoosh\nLIBRARY VERSION: "}; uint8_t msgTermVersion[] = {LIB_VERSION}; uint8_t msgSwVerHeader[] = {"\nSOFTWARE VERSION: "}; #ifdef SOFTWARE_VERSION uint8_t msgSoftwareVersion[] = ; #else uint8_t msgSoftwareVersion[] = {"unknown"}; #endif uint8_t msgTalk[] = {"\n\nFor list of avaiable commands type help"}; HAL_UART_Transmit(&huart3, msgWelcome, sizeof(msgWelcome)-1, 0xFFFF); HAL_UART_Transmit(&huart3, msgTermVersion, sizeof(msgTermVersion)-1, 0xFFFF); HAL_UART_Transmit(&huart3, msgSwVerHeader, sizeof(msgSwVerHeader)-1, 0xFFFF); HAL_UART_Transmit(&huart3, msgSoftwareVersion, sizeof(msgSoftwareVersion)-1, 0xFFFF); HAL_UART_Transmit(&huart3, msgTalk, sizeof(msgTalk)-1, 0xFFFF); HAL_UART_Transmit(&huart3, &msgNL, sizeof(msgNL), 0xFFFF); HAL_UART_Transmit(&huart3, &msgNL, sizeof(msgNL), 0xFFFF); HAL_UART_Transmit(&huart3, msgBlank, sizeof(msgBlank)-1, 0xFFFF); } //FATFS /** * @brief list files */ 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; }