avec dfu

Fork of Utils by POTLESS

Files at this revision

API Documentation at this revision

Comitter:
POTLESS_2
Date:
Tue Feb 20 16:48:46 2018 +0000
Parent:
5:0f3b6e6677ef
Child:
7:1bd8b236feb1
Commit message:
petits changements

Changed in this revision

Utils.cpp Show annotated file Show diff for this revision Revisions of this file
Utils.h Show annotated file Show diff for this revision Revisions of this file
--- a/Utils.cpp	Tue Feb 13 16:54:00 2018 +0000
+++ b/Utils.cpp	Tue Feb 20 16:48:46 2018 +0000
@@ -1,7 +1,9 @@
 #include "mbed.h"
 #include "Utils.h"
 
-SDBlockDevice bd(SD_MOSI, SD_MISO, SD_SCK, SD_CS);
+SDBlockDevice bd(Flash_MOSI, Flash_MISO, Flash_SCK, Flash_CS);
+//SPIFBlockDevice bd(Flash_MOSI, Flash_MISO, Flash_SCK, Flash_CS);
+
 FATFileSystem Root("Root");
 
 int Exist_Val = 0;
@@ -9,25 +11,27 @@
 void return_error(int ret_val)
 {
     Exist_Val = 0;
-    if (ret_val){
-        printf("  Problème carte SD = %d\r\n", ret_val);
+    if (ret_val) {
+        DEBUG_U("  Problème Flash = %d\r\n", ret_val);
         Exist_Val = 0;
-    }else{
-        printf("  SD -> OK.\r\n");
+    } else {
+        DEBUG_U("  Flash -> OK.\r\n");
         Exist_Val = 1;
-        }
+    }
+    wait_ms(100);
 }
 
 void errno_error(void* ret_val)
 {
     Exist_Val = 0;
-    if (ret_val == NULL){
-        printf("  Problème carte SD = %d \r\n", errno);
+    if (ret_val == NULL) {
+        DEBUG_U("  Problème Flash = %d \r\n", errno);
         Exist_Val = 0;
-    }else{
-        printf("  SD -> OK.\r\n");
+    } else {
+        DEBUG_U("  Flash -> OK.\r\n");
         Exist_Val = 1;
-        }
+    }
+    wait_ms(100);
 }
 
 void UTILS::Store_A_Val(float Val_To_Store, char* File_Name)
@@ -41,7 +45,7 @@
     fprintf(fd, "%f\r\n", Val_To_Store);
     fclose(fd);
 
-    printf("  \r\n  %s sauvegardée = %f\r\n", filename, Val_To_Store);
+    DEBUG_U("  \r\n  %s sauvegardée = %f\r\n", filename, Val_To_Store);
 
 }
 
@@ -55,11 +59,11 @@
     char filename[20];
     sprintf(filename, "/Root/%s.txt", File_Name);
 
-    printf("  \n  Récupération de %s...\r\n", filename);
+    DEBUG_U("  \n  Récupération de %s...\r\n", filename);
 
     FILE* fd = fopen(filename, "r");
     errno_error(fd);
-    
+
     while ((c != '\n') && (i < 10)) {
         c = fgetc(fd);
         buffer[i] = c;
@@ -70,7 +74,7 @@
 
     float val = atof(token);
 
-    printf("  Valeur Récupérée = %f\r\n", val);
+    DEBUG_U("  Valeur Récupérée = %f\r\n", val);
 
     fclose(fd);
 
@@ -78,70 +82,108 @@
 }
 
 
-void UTILS::Write_SD_File(char* To_Store, char* File_Name)
+void UTILS::Write_Flash_File(char* To_Store, char* File_Name)
 {
     char filename[50];
     sprintf(filename, "/Root/%s", (string)File_Name);
-    
+
     FILE* fd = fopen(filename, "a");
-    
+
     errno_error(fd);
     fprintf(fd, "%s\r\n", To_Store);
     fclose(fd);
 
-    //printf("  Sauvegarde OK\r\n\r\n");
+    DEBUG_U("  Enregistrement de %s OK\r\n\r\n", filename);
 }
 
-void UTILS::Read_SD_File(char* File_Name)
+/*
+void UTILS::Read_Flash_File(char* File_Name)
 {
     char filename[20];
     sprintf(filename, "/Root/%s", File_Name);
-    
+
     FILE* fd = fopen(filename, "r");
     errno_error(fd);
 
-    printf("  Contenu du fichier :\r\n\r\n");
-    char buff[16] = {0};
-    while (!feof(fd)) {
+    //printf("  Contenu du fichier :\r\n\r\n");
+    char buff[100] = {0};
+       
+    while (!feof(fd)) {        
         int size = fread(&buff[0], 1, 15, fd);
-        fwrite(&buff[0], 1, size, stdout);
+        fwrite(&buff[0], 1, size, stdout);        
     }
-    printf("\r\n  Fin du fichier.\r\n");
+    //printf("\r\n  Fin du fichier.\r\n");
     fclose(fd);
+    
+}
+*/
+
+void UTILS::Read_Flash_File(char* File_Name)
+{
+    char filename[20];
+    sprintf(filename, "/Root/%s", File_Name);
+
+    FILE* fd = fopen(filename, "r");
+    errno_error(fd);   
+    char s1[100];
+    printf("  Contenu du fichier :\r\n\r\n");
+    while (!feof(fd)) {
+        memset(s1, 0, sizeof(s1));
+        fgets(s1, sizeof(s1), fd);
+        printf("%s", s1);
+        fflush(stdout);
+    }
+   fclose(fd);
+   printf("\r\n  Fin du fichier.\r\n");
 }
 
 bool UTILS::File_Exist(string File_Name)
 {
     char filename[20];
     sprintf(filename, "/Root/%s.txt", File_Name);
-    
+
     FILE* fd = fopen(filename, "r");
     errno_error(fd);
-    
-    if (Exist_Val == 0){
-        printf("  Le fichier %s n'existe pas....\r\n", filename);
+
+    if (Exist_Val == 0) {
+        DEBUG_U("  Le fichier %s n'existe pas....\r\n", filename);
         fclose(fd);
         return false;
     }
-    
-    if (Exist_Val == 1){
+
+    if (Exist_Val == 1) {
         fclose(fd);
         return true;
-    }   
+    }
 }
 
-void UTILS::Delete_SD_File(char* File_Name)
+void UTILS::Delete_Flash_File(char* File_Name)
 {
     char filename[20];
-    sprintf(filename, "/Root/%s", File_Name);
+    sprintf(filename, "%s", File_Name);
     int error = 0;
     error = Root.remove(filename);
     return_error(error);
 
-    printf("Fichier effacé.\n");
+    DEBUG_U("Fichier %s effacé.\r\n", filename);
 }
 
-void UTILS::Rename_SD_File(char* Old_File_Name, char* New_File_Name)
+void UTILS::Clean_Flash()
+{
+    DEBUG_U("Nettoyage de la Flash.\r\n");
+    float Saved_O2 = UTILS::Read_A_Val("Calibration_O2");
+    int Saved_Motor = (int)UTILS::Read_A_Val("Servo_Poumon");
+    UTILS::UnMount_Flash();
+    UTILS::Format_Flash();
+    UTILS::Mount_Flash();
+    UTILS::Store_A_Val(Saved_O2, "Calibration_O2");
+    UTILS::Store_A_Val(Saved_Motor, "Servo_Poumon");
+    DEBUG_U("Flash nettoyée.\r\n");
+    DEBUG_U("Redémmarage code.\r\n");
+    NVIC_SystemReset();   
+}
+
+void UTILS::Rename_Flash_File(char* Old_File_Name, char* New_File_Name)
 {
     char Oldfilename[20];
     sprintf(Oldfilename, "/Root/%s", Old_File_Name);
@@ -152,63 +194,61 @@
     error = Root.rename(Oldfilename, Newfilename);
     return_error(error);
 
-    printf("Fichier renommé.\n");
+    DEBUG_U("Fichier %s renommé en %s.\r\n", Oldfilename, Newfilename);
 }
 
-void UTILS::Mount_SD()
+void UTILS::Mount_Flash()
 {
-    //Montage carte SD
-    printf("  Montage carte SD \"/Root\". \r\n\r\n");
+    //Montage Flash
+    DEBUG_U("  Montage Flash \"/Root\". \r\n\r\n");
     int error = 0;
     error = Root.mount(&bd);
     return_error(error);
     if (error > 0) {
         //On re format s'il n'y a as de file system...normalement une seul fois...
-        printf("Pas de File system, on format... ");
-        Format_SD();
+        DEBUG_U("Pas de File system, on format... ");
+        Format_Flash();
     }
 }
 
-void UTILS::UnMount_SD()
+void UTILS::UnMount_Flash()
 {
-    //Montage carte SD
-    printf("  Demontage carte SD \"/Root\". \r\n\r\n");
+    //De Montage Flash
+    DEBUG_U("  Demontage Flash \"/Root\". \r\n\r\n");
     int error = 0;
     error = Root.unmount();
     return_error(error);
 }
 
-void UTILS::Format_SD()
+void UTILS::Format_Flash()
 {
-    //Formatage carte SD
-    printf("  Formatage carte SD\r\n\r\n");
+    //Formatage Flash
+    DEBUG_U("  Formatage Flash\r\n\r\n");
     int error = 0;
     error = FATFileSystem::format(&bd);
     return_error(error);
 }
 
-void UTILS::Dir_SD(char* Dir_Name)
+void UTILS::Dir_Flash(char* Dir_Name)
 {
     int error = 0;
     int nb = 0;
-    printf("\r\n  Ouverture du répertoire %s\r\n", Dir_Name);
+    DEBUG_U("\r\n  Ouverture du répertoire %s\r\n", Dir_Name);
     char Dirname[20];
     sprintf(Dirname, "/Root/%s", Dir_Name);
-    
+
     DIR* dir = opendir(Dirname);
-    //errno_error(fd);
 
     struct dirent* de;
-    printf("  Fichier du répertoire :\r\n\r\n");
-    while((de = readdir(dir)) != NULL)
-    {
-    printf("  %s\r\n", &(de->d_name)[0]);
-    nb++;
-    //wait_ms(100);
-  }
-    printf("\r\n  Nombre de fichiers = %d\r\n", nb);
-    printf("  Fermeture du répertoire.\r\n");
-    //error = closedir(dir);
+    DEBUG_U("  Fichier du répertoire :\r\n\r\n");
+    while((de = readdir(dir)) != NULL) {
+        printf("  %s\r\n", &(de->d_name)[0]);
+        fflush(stdout);
+        nb++;
+    }
+    DEBUG_U("\r\n  Nombre de fichiers = %d\r\n", nb);
+    DEBUG_U("  Fermeture du répertoire.\r\n");
+    error = closedir(dir);
     return_error(error);
 }
 
@@ -216,16 +256,15 @@
 {
     int error = 0;
     int nb = 0;
-    
+
     DIR* dir = opendir("/Root/");
     struct dirent* de;
-    while((de = readdir(dir)) != NULL)
-    {
-    nb++;
+    while((de = readdir(dir)) != NULL) {
+        nb++;
     }
     error = closedir(dir);
     return_error(error);
-    
+
     return nb - 2;
 }
 
@@ -244,4 +283,27 @@
         return b;
     } else
         return x;
+}
+
+void UTILS::Help()
+{
+    printf("\r\n");
+    printf("  Commandes à entrer dans le moniteur :\r\n\r\n");
+    printf("  help             =    liste des commandes\r\n");
+    printf("  start            =    démmarage des sorties moniteur\r\n");
+    printf("  stop             =    arrêt des sorties moniteur\r\n");
+    printf("  clean            =    nettoyage flash, suppression des fichiers LOG\r\n");
+    printf("  dir              =    liste des fichiers de la flash\r\n");
+    printf("  del X            =    effacer le fichier LOG_X\n\r");
+    printf("  get X            =    récupérer le contenu du fichier LOG_X\n\r");
+    printf("  time X           =    met la RTC à l'heure, X en UNIX TIME.\n\r");
+    printf("  c_pou X          =    changement consigne volet poumon à X\n\r");
+    printf("  c_fui X          =    changement consigne volet fuite à X\n\r");
+    printf("  calib_p          =    calibration à 0 du volet poumon\n\r");
+    printf("  calib_f          =    calibration à 0 du volet fuite\n\r");
+    printf("  sleep            =    mise en veille\n\r");
+    printf("  reset            =    reset de la carte\n\r\n\r");
+    printf("  Les commandes pour le Mini-r sont à entrer conformément à la doc.\n\r");
+    printf("\r\n\r\n");
+    fflush(stdout);
 }
\ No newline at end of file
--- a/Utils.h	Tue Feb 13 16:54:00 2018 +0000
+++ b/Utils.h	Tue Feb 20 16:48:46 2018 +0000
@@ -6,14 +6,23 @@
 
 #include "FATFileSystem.h"
 #include "SDBlockDevice.h"
+//#include "SPIFBlockDevice.h"
 #include <stdio.h>
 #include <errno.h>
 
-//PIN OUT carte SD
-#define SD_MOSI PA_7
-#define SD_MISO PA_6
-#define SD_SCK PA_5
-#define SD_CS PB_6
+//PIN OUT Flash
+#define Flash_MOSI PA_7
+#define Flash_MISO PA_6
+#define Flash_SCK PA_5
+#define Flash_CS PB_6
+
+#define DEBUG_UTILS 0
+
+#if DEBUG_UTILS
+#define DEBUG_U(...) { printf(__VA_ARGS__); fflush(stdout);}
+#else
+#define DEBUG_U(...)
+#endif
 
 /** Utils class.
  *  Rassemblant des fonctions annexes pour le fonctionnement de l'ARNSRS.
@@ -22,21 +31,63 @@
  *
  *  -  FATFileSystem
  *
- *  -  SDBlockDevice
+ *  -  SPIFBlockDevice
  *
  *
  *  Constantes de l'application :
  *
- * Pin out carte SD :
+ * Pin out Flash :
+ *
+ * -  Flash_MOSI PA_7
+ *
+ * -  Flash_MISO PA_6
+ *
+ * -  Flash_SCK PA_5
+ *
+ * -  Flash_CS PB_6
+ *
+ * Liste des erreurs possibles sur les fonctions Flash :
  *
- * -  SD_MOSI PA_7
+ *
+ * (0) no error
+ * 
+ * (1) A hard error occurred in the low level disk I/O layer
+ *
+ * (2) Assertion failed 
+ *
+ * (3) The physical drive cannot work 
+ *
+ * (4) Could not find the file
+ *
+ * (5) Could not find the path
  *
- * -  SD_MISO PA_6
+ * (6) The path name format is invalid
+ *
+ * (7) Access denied due to prohibited access or directory full, Permission denied
+ *
+ * (8) Access denied due to prohibited access, File exists
+ *
+ * (9) The file/directory object is invalid, Bad address 
+ *
+ * (10) The physical drive is write protected
+ *
+ * (11) The logical drive number is invalid
+ *
+ * (12) The volume has no work area, No such device or address
  *
- * -  SD_SCK PA_5
+ * (13) There is no valid FAT volume, No such file or directory
+ *
+ * (14) The f_mkfs() aborted due to any parameter error
+ *
+ * (15) Could not get a grant to access the volume within defined period, Bad file number
  *
- * -  SD_CS PB_6
+ * (16) The operation is rejected according to the file sharing policy, Permission denied
+ *
+ * (17) LFN working buffer could not be allocated, Not enough space
  *
+ * (18) Number of open files > _FS_LOCK, Too many open files in system
+ *
+ * (19) Given parameter is invalid, Exec format error 
 */
 
 class UTILS
@@ -60,57 +111,61 @@
     */
     static float constrain(float x, float a, float b);
 
-    /**Fonction de stockage valeur sur la carte SD.
+    /**Fonction de stockage valeur sur la Flash.
     * @param float Val_To_Store, la valeur à stocker
     * @param char* Nom du fichier ou stocker cette valeur
     */
     static void Store_A_Val(float Val_To_Store, char* File_Name);
 
-    /**Fonction de récupération d'une valeur stockée sur la carte SD.
+    /**Fonction de récupération d'une valeur stockée sur la Flash.
     * @param char* Nom du fichier ou stocker cette valeur
     * @returns
     *   valeur de calibration des capteurs O2
     */
     static float Read_A_Val(char* File_Name);
 
-    /**Fonction d'enregistrement d'une chaine de charatères sur la carte SD.
+    /**Fonction d'enregistrement d'une chaine de charatères sur la Flash.
     * @param char* To_Store, chaine de charactères
     * @param char* Nom du fichier, s'il existe il est ouvert, sinon il est créé
     */
-    static void Write_SD_File(char* To_Store, char* File_Name);
+    static void Write_Flash_File(char* To_Store, char* File_Name);
 
-    /**Fonction de lecture d'un fichier sur la carte SD.
+    /**Fonction de lecture d'un fichier sur la Flash.
     * @param char* Nom du fichier
     */
-    static void Read_SD_File(char* File_Name);
+    static void Read_Flash_File(char* File_Name);
 
-    /**Fonction d'éffaçage d'un fichier sur la carte SD.
+    /**Fonction d'éffaçage d'un fichier sur la Flash.
     * @param char* Nom du fichier
     */
-    static void Delete_SD_File(char* File_Name);
+    static void Delete_Flash_File(char* File_Name);
 
-    /**Fonction d'éffaçage d'un fichier sur la carte SD.
+    /**Fonction nettoyage de la Flash.
+    */
+    static void Clean_Flash();
+    
+    /**Fonction d'éffaçage d'un fichier sur la Flash.
     * @param char* Nom du fichier à effacer
     * @param char* Nouveau Nom du fichier
     */
-    static void Rename_SD_File(char* Old_File_Name, char* New_File_Name);
+    static void Rename_Flash_File(char* Old_File_Name, char* New_File_Name);
 
-    /**Fonction montage de la carte SD.
+    /**Fonction montage de la Flash.
     */
-    static void Mount_SD();
+    static void Mount_Flash();
 
-    /**Fonction demontage de la carte SD.
+    /**Fonction demontage de la Flash.
     */
-    static void UnMount_SD();
+    static void UnMount_Flash();
 
-    /**Fonction formatage de la carte SD.
+    /**Fonction formatage de la Flash.
     */
-    static void Format_SD();
+    static void Format_Flash();
     
     /**Fonction DIR.
     * @param char* Nom du répertoire
     */
-    static void Dir_SD(char* Dir_Name = "");
+    static void Dir_Flash(char* Dir_Name = "");
     
     /**Fonction de numérotage des fichiers log.
     * @returns
@@ -124,6 +179,10 @@
     *   true si le fichier existe, false s'il n'existe pas
     */
     static bool File_Exist(string File_Name);
+    
+    /**Fonction d'affichage du menu d'aide.
+    */
+    static void Help();
 
 
 private: