avec dfu
Fork of Utils by
Utils.cpp@19:2b5bac7f435e, 2018-05-24 (annotated)
- Committer:
- potless
- Date:
- Thu May 24 23:16:22 2018 +0000
- Revision:
- 19:2b5bac7f435e
- Parent:
- 18:522e18b47823
avec le flag pour le bootloader et l'id?e de d?clarer l usbserial uniquement si on n'est pas en phase de bootload
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
POTLESS_2 | 0:794c15bd30b0 | 1 | #include "mbed.h" |
POTLESS_2 | 0:794c15bd30b0 | 2 | #include "Utils.h" |
POTLESS_2 | 0:794c15bd30b0 | 3 | |
POTLESS_2 | 9:fa2818db80cf | 4 | //SDBlockDevice bd(Flash_MOSI, Flash_MISO, Flash_SCK, Flash_CS); |
POTLESS_2 | 9:fa2818db80cf | 5 | SPIFBlockDevice bd(Flash_MOSI, Flash_MISO, Flash_SCK, Flash_CS); |
POTLESS_2 | 11:a41267cffa61 | 6 | |
POTLESS_2 | 2:5af7a1fb1903 | 7 | FATFileSystem Root("Root"); |
POTLESS_2 | 0:794c15bd30b0 | 8 | |
POTLESS_2 | 3:01677db45b2a | 9 | int Exist_Val = 0; |
POTLESS_2 | 3:01677db45b2a | 10 | |
POTLESS_2 | 11:a41267cffa61 | 11 | //Init des PIN i2c pour l'eeprom |
POTLESS_2 | 11:a41267cffa61 | 12 | I2C I2C_EEPROM(SDA_PIN, SCL_PIN); |
POTLESS_2 | 11:a41267cffa61 | 13 | |
POTLESS_2 | 0:794c15bd30b0 | 14 | void return_error(int ret_val) |
POTLESS_2 | 0:794c15bd30b0 | 15 | { |
POTLESS_2 | 4:872676dd87a4 | 16 | Exist_Val = 0; |
POTLESS_2 | 6:8bae97034114 | 17 | if (ret_val) { |
POTLESS_2 | 12:9dd7c462a2c0 | 18 | DEEP_DEBUG(" Problème Flash = %d\r\n", ret_val); |
POTLESS_2 | 3:01677db45b2a | 19 | Exist_Val = 0; |
POTLESS_2 | 6:8bae97034114 | 20 | } else { |
POTLESS_2 | 12:9dd7c462a2c0 | 21 | DEEP_DEBUG(" Flash -> OK.\r\n"); |
POTLESS_2 | 3:01677db45b2a | 22 | Exist_Val = 1; |
POTLESS_2 | 6:8bae97034114 | 23 | } |
POTLESS_2 | 6:8bae97034114 | 24 | wait_ms(100); |
POTLESS_2 | 0:794c15bd30b0 | 25 | } |
POTLESS_2 | 0:794c15bd30b0 | 26 | |
POTLESS_2 | 0:794c15bd30b0 | 27 | void errno_error(void* ret_val) |
POTLESS_2 | 0:794c15bd30b0 | 28 | { |
POTLESS_2 | 4:872676dd87a4 | 29 | Exist_Val = 0; |
POTLESS_2 | 6:8bae97034114 | 30 | if (ret_val == NULL) { |
POTLESS_2 | 12:9dd7c462a2c0 | 31 | DEEP_DEBUG(" Problème Flash = %d \r\n", errno); |
POTLESS_2 | 3:01677db45b2a | 32 | Exist_Val = 0; |
POTLESS_2 | 6:8bae97034114 | 33 | } else { |
POTLESS_2 | 12:9dd7c462a2c0 | 34 | DEEP_DEBUG(" Flash -> OK.\r\n"); |
POTLESS_2 | 3:01677db45b2a | 35 | Exist_Val = 1; |
POTLESS_2 | 6:8bae97034114 | 36 | } |
POTLESS_2 | 6:8bae97034114 | 37 | wait_ms(100); |
POTLESS_2 | 0:794c15bd30b0 | 38 | } |
POTLESS_2 | 0:794c15bd30b0 | 39 | |
POTLESS_2 | 18:522e18b47823 | 40 | void UTILS::Flash_Infos(USBSerial *serial_port) |
POTLESS_2 | 8:23fd400c4edd | 41 | { |
POTLESS_2 | 16:74cb32bb162f | 42 | serial_port->printf("?\r\n"); |
POTLESS_2 | 16:74cb32bb162f | 43 | |
POTLESS_2 | 15:a8c19ad9c11d | 44 | serial_port->printf(" Flash size : %llu Mb\r\n", bd.size() / 1048576); |
POTLESS_2 | 15:a8c19ad9c11d | 45 | serial_port->printf(" Flash read size : %llu\r\n", bd.get_read_size()); |
POTLESS_2 | 15:a8c19ad9c11d | 46 | serial_port->printf(" Flash program size : %llu\r\n", bd.get_program_size()); |
POTLESS_2 | 15:a8c19ad9c11d | 47 | serial_port->printf(" Flash erase size : %llu\r\n", bd.get_erase_size()); |
POTLESS_2 | 8:23fd400c4edd | 48 | } |
POTLESS_2 | 8:23fd400c4edd | 49 | |
POTLESS_2 | 18:522e18b47823 | 50 | void UTILS::Get_File_Size(USBSerial *serial_port, char* File_Name) |
POTLESS_2 | 11:a41267cffa61 | 51 | { |
POTLESS_2 | 16:74cb32bb162f | 52 | serial_port->printf("?\r\n"); |
POTLESS_2 | 16:74cb32bb162f | 53 | |
POTLESS_2 | 15:a8c19ad9c11d | 54 | char filename[50]; |
POTLESS_2 | 8:23fd400c4edd | 55 | sprintf(filename, "/Root/%s", File_Name); |
POTLESS_2 | 8:23fd400c4edd | 56 | |
POTLESS_2 | 8:23fd400c4edd | 57 | FILE* fd = fopen(filename, "r"); |
POTLESS_2 | 11:a41267cffa61 | 58 | errno_error(fd); |
POTLESS_2 | 11:a41267cffa61 | 59 | |
POTLESS_2 | 8:23fd400c4edd | 60 | fseek(fd, 0, SEEK_END); |
POTLESS_2 | 8:23fd400c4edd | 61 | int size = ftell(fd); |
POTLESS_2 | 8:23fd400c4edd | 62 | fseek(fd, 0, SEEK_SET); |
POTLESS_2 | 11:a41267cffa61 | 63 | |
POTLESS_2 | 8:23fd400c4edd | 64 | fclose(fd); |
POTLESS_2 | 11:a41267cffa61 | 65 | |
POTLESS_2 | 15:a8c19ad9c11d | 66 | serial_port->printf(" Taille du fichier %s = %d Kb\r\n", filename, size / 1024); |
POTLESS_2 | 8:23fd400c4edd | 67 | } |
POTLESS_2 | 8:23fd400c4edd | 68 | |
POTLESS_2 | 0:794c15bd30b0 | 69 | void UTILS::Store_A_Val(float Val_To_Store, char* File_Name) |
POTLESS_2 | 0:794c15bd30b0 | 70 | { |
POTLESS_2 | 0:794c15bd30b0 | 71 | |
POTLESS_2 | 15:a8c19ad9c11d | 72 | char filename[50]; |
POTLESS_2 | 15:a8c19ad9c11d | 73 | sprintf(filename, "/Root/%s", File_Name); |
POTLESS_2 | 0:794c15bd30b0 | 74 | |
POTLESS_2 | 0:794c15bd30b0 | 75 | FILE* fd = fopen(filename, "w"); |
POTLESS_2 | 0:794c15bd30b0 | 76 | errno_error(fd); |
POTLESS_2 | 0:794c15bd30b0 | 77 | fprintf(fd, "%f\r\n", Val_To_Store); |
POTLESS_2 | 0:794c15bd30b0 | 78 | fclose(fd); |
POTLESS_2 | 0:794c15bd30b0 | 79 | |
POTLESS_2 | 12:9dd7c462a2c0 | 80 | DEEP_DEBUG(" \r\n %s sauvegardée = %f\r\n", filename, Val_To_Store); |
POTLESS_2 | 0:794c15bd30b0 | 81 | |
POTLESS_2 | 0:794c15bd30b0 | 82 | } |
POTLESS_2 | 0:794c15bd30b0 | 83 | |
POTLESS_2 | 0:794c15bd30b0 | 84 | float UTILS::Read_A_Val(char* File_Name) |
POTLESS_2 | 0:794c15bd30b0 | 85 | { |
POTLESS_2 | 0:794c15bd30b0 | 86 | char buffer[10] = {0}; |
POTLESS_2 | 0:794c15bd30b0 | 87 | char c = {0}; |
POTLESS_2 | 0:794c15bd30b0 | 88 | char *token; |
POTLESS_2 | 0:794c15bd30b0 | 89 | int i = 0; |
POTLESS_2 | 0:794c15bd30b0 | 90 | |
POTLESS_2 | 15:a8c19ad9c11d | 91 | char filename[50]; |
POTLESS_2 | 15:a8c19ad9c11d | 92 | sprintf(filename, "/Root/%s", File_Name); |
POTLESS_2 | 0:794c15bd30b0 | 93 | |
POTLESS_2 | 12:9dd7c462a2c0 | 94 | DEEP_DEBUG(" \n Récupération de %s...\r\n", filename); |
POTLESS_2 | 0:794c15bd30b0 | 95 | |
POTLESS_2 | 0:794c15bd30b0 | 96 | FILE* fd = fopen(filename, "r"); |
POTLESS_2 | 0:794c15bd30b0 | 97 | errno_error(fd); |
POTLESS_2 | 6:8bae97034114 | 98 | |
POTLESS_2 | 0:794c15bd30b0 | 99 | while ((c != '\n') && (i < 10)) { |
POTLESS_2 | 0:794c15bd30b0 | 100 | c = fgetc(fd); |
POTLESS_2 | 0:794c15bd30b0 | 101 | buffer[i] = c; |
POTLESS_2 | 0:794c15bd30b0 | 102 | i++; |
POTLESS_2 | 0:794c15bd30b0 | 103 | } |
POTLESS_2 | 0:794c15bd30b0 | 104 | |
POTLESS_2 | 0:794c15bd30b0 | 105 | token = strtok(buffer, "\n"); |
POTLESS_2 | 0:794c15bd30b0 | 106 | |
POTLESS_2 | 0:794c15bd30b0 | 107 | float val = atof(token); |
POTLESS_2 | 0:794c15bd30b0 | 108 | |
POTLESS_2 | 12:9dd7c462a2c0 | 109 | DEEP_DEBUG(" Valeur Récupérée = %f\r\n", val); |
POTLESS_2 | 0:794c15bd30b0 | 110 | |
POTLESS_2 | 0:794c15bd30b0 | 111 | fclose(fd); |
POTLESS_2 | 0:794c15bd30b0 | 112 | |
POTLESS_2 | 0:794c15bd30b0 | 113 | return val; |
POTLESS_2 | 0:794c15bd30b0 | 114 | } |
POTLESS_2 | 0:794c15bd30b0 | 115 | |
POTLESS_2 | 0:794c15bd30b0 | 116 | |
POTLESS_2 | 6:8bae97034114 | 117 | void UTILS::Write_Flash_File(char* To_Store, char* File_Name) |
POTLESS_2 | 0:794c15bd30b0 | 118 | { |
POTLESS_2 | 2:5af7a1fb1903 | 119 | char filename[50]; |
POTLESS_2 | 2:5af7a1fb1903 | 120 | sprintf(filename, "/Root/%s", (string)File_Name); |
POTLESS_2 | 6:8bae97034114 | 121 | |
POTLESS_2 | 0:794c15bd30b0 | 122 | FILE* fd = fopen(filename, "a"); |
POTLESS_2 | 6:8bae97034114 | 123 | |
POTLESS_2 | 0:794c15bd30b0 | 124 | errno_error(fd); |
POTLESS_2 | 0:794c15bd30b0 | 125 | fprintf(fd, "%s\r\n", To_Store); |
POTLESS_2 | 0:794c15bd30b0 | 126 | fclose(fd); |
POTLESS_2 | 0:794c15bd30b0 | 127 | |
POTLESS_2 | 12:9dd7c462a2c0 | 128 | DEEP_DEBUG(" Enregistrement de %s OK\r\n\r\n", filename); |
POTLESS_2 | 0:794c15bd30b0 | 129 | } |
POTLESS_2 | 0:794c15bd30b0 | 130 | |
POTLESS_2 | 6:8bae97034114 | 131 | /* |
POTLESS_2 | 6:8bae97034114 | 132 | void UTILS::Read_Flash_File(char* File_Name) |
POTLESS_2 | 0:794c15bd30b0 | 133 | { |
POTLESS_2 | 15:a8c19ad9c11d | 134 | char filename[50]; |
POTLESS_2 | 2:5af7a1fb1903 | 135 | sprintf(filename, "/Root/%s", File_Name); |
POTLESS_2 | 6:8bae97034114 | 136 | |
POTLESS_2 | 0:794c15bd30b0 | 137 | FILE* fd = fopen(filename, "r"); |
POTLESS_2 | 0:794c15bd30b0 | 138 | errno_error(fd); |
POTLESS_2 | 0:794c15bd30b0 | 139 | |
POTLESS_2 | 6:8bae97034114 | 140 | //printf(" Contenu du fichier :\r\n\r\n"); |
POTLESS_2 | 6:8bae97034114 | 141 | char buff[100] = {0}; |
POTLESS_2 | 11:a41267cffa61 | 142 | |
POTLESS_2 | 11:a41267cffa61 | 143 | while (!feof(fd)) { |
POTLESS_2 | 0:794c15bd30b0 | 144 | int size = fread(&buff[0], 1, 15, fd); |
POTLESS_2 | 11:a41267cffa61 | 145 | fwrite(&buff[0], 1, size, stdout); |
POTLESS_2 | 0:794c15bd30b0 | 146 | } |
POTLESS_2 | 6:8bae97034114 | 147 | //printf("\r\n Fin du fichier.\r\n"); |
POTLESS_2 | 0:794c15bd30b0 | 148 | fclose(fd); |
POTLESS_2 | 11:a41267cffa61 | 149 | |
POTLESS_2 | 6:8bae97034114 | 150 | } |
POTLESS_2 | 6:8bae97034114 | 151 | */ |
POTLESS_2 | 6:8bae97034114 | 152 | |
POTLESS_2 | 18:522e18b47823 | 153 | void UTILS::Read_Flash_File(USBSerial *serial_port, char* File_Name) |
POTLESS_2 | 15:a8c19ad9c11d | 154 | { |
POTLESS_2 | 16:74cb32bb162f | 155 | serial_port->printf("?\r\n"); |
POTLESS_2 | 16:74cb32bb162f | 156 | |
POTLESS_2 | 15:a8c19ad9c11d | 157 | if (File_Exist(File_Name)) { |
POTLESS_2 | 15:a8c19ad9c11d | 158 | char filename[50]; |
POTLESS_2 | 15:a8c19ad9c11d | 159 | sprintf(filename, "/Root/%s", File_Name); |
POTLESS_2 | 6:8bae97034114 | 160 | FILE* fd = fopen(filename, "r"); |
POTLESS_2 | 11:a41267cffa61 | 161 | errno_error(fd); |
POTLESS_2 | 6:8bae97034114 | 162 | char s1[100]; |
POTLESS_2 | 8:23fd400c4edd | 163 | printf("\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 164 | DEBUG(" Contenu du fichier :\r\n\r\n"); |
POTLESS_2 | 6:8bae97034114 | 165 | while (!feof(fd)) { |
POTLESS_2 | 6:8bae97034114 | 166 | memset(s1, 0, sizeof(s1)); |
POTLESS_2 | 6:8bae97034114 | 167 | fgets(s1, sizeof(s1), fd); |
POTLESS_2 | 16:74cb32bb162f | 168 | serial_port->printf("%s", s1); |
POTLESS_2 | 6:8bae97034114 | 169 | } |
POTLESS_2 | 11:a41267cffa61 | 170 | fclose(fd); |
POTLESS_2 | 15:a8c19ad9c11d | 171 | DEBUG("\r\n Fin du fichier.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 172 | } |
POTLESS_2 | 0:794c15bd30b0 | 173 | } |
POTLESS_2 | 0:794c15bd30b0 | 174 | |
POTLESS_2 | 15:a8c19ad9c11d | 175 | bool UTILS::File_Exist(char* File_Name) |
POTLESS_2 | 3:01677db45b2a | 176 | { |
POTLESS_2 | 15:a8c19ad9c11d | 177 | char filename[50]; |
POTLESS_2 | 15:a8c19ad9c11d | 178 | sprintf(filename, "/Root/%s", File_Name); |
POTLESS_2 | 6:8bae97034114 | 179 | |
POTLESS_2 | 3:01677db45b2a | 180 | FILE* fd = fopen(filename, "r"); |
POTLESS_2 | 3:01677db45b2a | 181 | errno_error(fd); |
POTLESS_2 | 6:8bae97034114 | 182 | |
POTLESS_2 | 6:8bae97034114 | 183 | if (Exist_Val == 0) { |
POTLESS_2 | 10:21f3ff5d6a3d | 184 | DEBUG(" Le fichier %s n'existe pas....\r\n", filename); |
POTLESS_2 | 4:872676dd87a4 | 185 | fclose(fd); |
POTLESS_2 | 3:01677db45b2a | 186 | return false; |
POTLESS_2 | 5:0f3b6e6677ef | 187 | } |
POTLESS_2 | 6:8bae97034114 | 188 | |
POTLESS_2 | 6:8bae97034114 | 189 | if (Exist_Val == 1) { |
POTLESS_2 | 3:01677db45b2a | 190 | fclose(fd); |
POTLESS_2 | 3:01677db45b2a | 191 | return true; |
POTLESS_2 | 6:8bae97034114 | 192 | } |
POTLESS_2 | 3:01677db45b2a | 193 | } |
POTLESS_2 | 3:01677db45b2a | 194 | |
POTLESS_2 | 6:8bae97034114 | 195 | void UTILS::Delete_Flash_File(char* File_Name) |
POTLESS_2 | 0:794c15bd30b0 | 196 | { |
POTLESS_2 | 15:a8c19ad9c11d | 197 | char filename[50]; |
POTLESS_2 | 6:8bae97034114 | 198 | sprintf(filename, "%s", File_Name); |
POTLESS_2 | 0:794c15bd30b0 | 199 | int error = 0; |
POTLESS_2 | 2:5af7a1fb1903 | 200 | error = Root.remove(filename); |
POTLESS_2 | 0:794c15bd30b0 | 201 | return_error(error); |
POTLESS_2 | 0:794c15bd30b0 | 202 | |
POTLESS_2 | 12:9dd7c462a2c0 | 203 | DEBUG(" Fichier %s effacé.\r\n", filename); |
POTLESS_2 | 0:794c15bd30b0 | 204 | } |
POTLESS_2 | 0:794c15bd30b0 | 205 | |
POTLESS_2 | 6:8bae97034114 | 206 | void UTILS::Clean_Flash() |
POTLESS_2 | 6:8bae97034114 | 207 | { |
POTLESS_2 | 12:9dd7c462a2c0 | 208 | DEBUG(" Nettoyage de la Flash.\r\n"); |
POTLESS_2 | 12:9dd7c462a2c0 | 209 | //float Saved_O2 = UTILS::Read_A_Val("Calibration_O2"); |
POTLESS_2 | 16:74cb32bb162f | 210 | int Saved_Motor_P = (int)UTILS::Read_A_Val("Servo_Poumon.sys"); |
POTLESS_2 | 16:74cb32bb162f | 211 | int Saved_Motor_F = (int)UTILS::Read_A_Val("Servo_Fuite.sys"); |
POTLESS_2 | 15:a8c19ad9c11d | 212 | int ARNSRS_ID = (int)UTILS::Read_A_Val("ARNSRS_ID.sys"); |
POTLESS_2 | 6:8bae97034114 | 213 | UTILS::UnMount_Flash(); |
POTLESS_2 | 6:8bae97034114 | 214 | UTILS::Format_Flash(); |
POTLESS_2 | 6:8bae97034114 | 215 | UTILS::Mount_Flash(); |
POTLESS_2 | 12:9dd7c462a2c0 | 216 | //UTILS::Store_A_Val(Saved_O2, "Calibration_O2"); |
POTLESS_2 | 16:74cb32bb162f | 217 | UTILS::Store_A_Val(Saved_Motor_P, "Servo_Poumon.sys"); |
POTLESS_2 | 16:74cb32bb162f | 218 | UTILS::Store_A_Val(Saved_Motor_F, "Servo_Fuite.sys"); |
POTLESS_2 | 15:a8c19ad9c11d | 219 | UTILS::Store_A_Val(ARNSRS_ID, "ARNSRS_ID.sys"); |
POTLESS_2 | 12:9dd7c462a2c0 | 220 | DEBUG(" Flash nettoyée.\r\n"); |
POTLESS_2 | 12:9dd7c462a2c0 | 221 | DEBUG(" Redémmarage code.\r\n"); |
POTLESS_2 | 13:ff0f36a6d800 | 222 | UTILS::UnMount_Flash(); |
POTLESS_2 | 11:a41267cffa61 | 223 | NVIC_SystemReset(); |
POTLESS_2 | 6:8bae97034114 | 224 | } |
POTLESS_2 | 6:8bae97034114 | 225 | |
POTLESS_2 | 15:a8c19ad9c11d | 226 | void UTILS::Clean_Flash_All() |
POTLESS_2 | 15:a8c19ad9c11d | 227 | { |
POTLESS_2 | 15:a8c19ad9c11d | 228 | DEBUG(" Nettoyage complet de la Flash.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 229 | UTILS::UnMount_Flash(); |
POTLESS_2 | 15:a8c19ad9c11d | 230 | UTILS::Format_Flash(); |
POTLESS_2 | 15:a8c19ad9c11d | 231 | NVIC_SystemReset(); |
POTLESS_2 | 15:a8c19ad9c11d | 232 | } |
POTLESS_2 | 15:a8c19ad9c11d | 233 | |
POTLESS_2 | 6:8bae97034114 | 234 | void UTILS::Rename_Flash_File(char* Old_File_Name, char* New_File_Name) |
POTLESS_2 | 0:794c15bd30b0 | 235 | { |
POTLESS_2 | 15:a8c19ad9c11d | 236 | char Oldfilename[50]; |
POTLESS_2 | 2:5af7a1fb1903 | 237 | sprintf(Oldfilename, "/Root/%s", Old_File_Name); |
POTLESS_2 | 15:a8c19ad9c11d | 238 | char Newfilename[50]; |
POTLESS_2 | 2:5af7a1fb1903 | 239 | sprintf(Newfilename, "/Root/%s", New_File_Name); |
POTLESS_2 | 0:794c15bd30b0 | 240 | |
POTLESS_2 | 0:794c15bd30b0 | 241 | int error = 0; |
POTLESS_2 | 2:5af7a1fb1903 | 242 | error = Root.rename(Oldfilename, Newfilename); |
POTLESS_2 | 0:794c15bd30b0 | 243 | return_error(error); |
POTLESS_2 | 0:794c15bd30b0 | 244 | |
POTLESS_2 | 12:9dd7c462a2c0 | 245 | DEBUG(" Fichier %s renommé en %s.\r\n", Oldfilename, Newfilename); |
POTLESS_2 | 0:794c15bd30b0 | 246 | } |
POTLESS_2 | 0:794c15bd30b0 | 247 | |
POTLESS_2 | 6:8bae97034114 | 248 | void UTILS::Mount_Flash() |
POTLESS_2 | 0:794c15bd30b0 | 249 | { |
POTLESS_2 | 6:8bae97034114 | 250 | //Montage Flash |
POTLESS_2 | 10:21f3ff5d6a3d | 251 | DEBUG(" Montage Flash \"/Root\". \r\n\r\n"); |
POTLESS_2 | 0:794c15bd30b0 | 252 | int error = 0; |
POTLESS_2 | 2:5af7a1fb1903 | 253 | error = Root.mount(&bd); |
POTLESS_2 | 0:794c15bd30b0 | 254 | return_error(error); |
POTLESS_2 | 1:a937433a95c8 | 255 | if (error > 0) { |
POTLESS_2 | 0:794c15bd30b0 | 256 | //On re format s'il n'y a as de file system...normalement une seul fois... |
POTLESS_2 | 10:21f3ff5d6a3d | 257 | DEBUG("Pas de File system, on format... "); |
POTLESS_2 | 17:af9dbae179c1 | 258 | UTILS::Format_Flash(); |
POTLESS_2 | 0:794c15bd30b0 | 259 | } |
POTLESS_2 | 0:794c15bd30b0 | 260 | } |
POTLESS_2 | 0:794c15bd30b0 | 261 | |
POTLESS_2 | 6:8bae97034114 | 262 | void UTILS::UnMount_Flash() |
POTLESS_2 | 0:794c15bd30b0 | 263 | { |
POTLESS_2 | 6:8bae97034114 | 264 | //De Montage Flash |
POTLESS_2 | 10:21f3ff5d6a3d | 265 | DEBUG(" Demontage Flash \"/Root\". \r\n\r\n"); |
POTLESS_2 | 0:794c15bd30b0 | 266 | int error = 0; |
POTLESS_2 | 2:5af7a1fb1903 | 267 | error = Root.unmount(); |
POTLESS_2 | 0:794c15bd30b0 | 268 | return_error(error); |
POTLESS_2 | 0:794c15bd30b0 | 269 | } |
POTLESS_2 | 0:794c15bd30b0 | 270 | |
POTLESS_2 | 6:8bae97034114 | 271 | void UTILS::Format_Flash() |
POTLESS_2 | 0:794c15bd30b0 | 272 | { |
POTLESS_2 | 6:8bae97034114 | 273 | //Formatage Flash |
POTLESS_2 | 10:21f3ff5d6a3d | 274 | DEBUG(" Formatage Flash\r\n\r\n"); |
POTLESS_2 | 0:794c15bd30b0 | 275 | int error = 0; |
POTLESS_2 | 4:872676dd87a4 | 276 | error = FATFileSystem::format(&bd); |
POTLESS_2 | 2:5af7a1fb1903 | 277 | return_error(error); |
POTLESS_2 | 2:5af7a1fb1903 | 278 | } |
POTLESS_2 | 2:5af7a1fb1903 | 279 | |
POTLESS_2 | 18:522e18b47823 | 280 | void UTILS::Dir_Flash(USBSerial *serial_port, char* Dir_Name) |
POTLESS_2 | 2:5af7a1fb1903 | 281 | { |
POTLESS_2 | 2:5af7a1fb1903 | 282 | int error = 0; |
POTLESS_2 | 3:01677db45b2a | 283 | int nb = 0; |
POTLESS_2 | 12:9dd7c462a2c0 | 284 | DEEP_DEBUG("\r\n Ouverture du répertoire %s\r\n", Dir_Name); |
POTLESS_2 | 15:a8c19ad9c11d | 285 | char Dirname[50]; |
POTLESS_2 | 2:5af7a1fb1903 | 286 | sprintf(Dirname, "/Root/%s", Dir_Name); |
POTLESS_2 | 6:8bae97034114 | 287 | |
POTLESS_2 | 2:5af7a1fb1903 | 288 | DIR* dir = opendir(Dirname); |
POTLESS_2 | 2:5af7a1fb1903 | 289 | |
POTLESS_2 | 2:5af7a1fb1903 | 290 | struct dirent* de; |
POTLESS_2 | 12:9dd7c462a2c0 | 291 | DEBUG(" Fichiers du répertoire :\r\n\r\n"); |
POTLESS_2 | 6:8bae97034114 | 292 | while((de = readdir(dir)) != NULL) { |
POTLESS_2 | 16:74cb32bb162f | 293 | serial_port->printf("%s\r\n", &(de->d_name)[0]); |
POTLESS_2 | 6:8bae97034114 | 294 | fflush(stdout); |
POTLESS_2 | 6:8bae97034114 | 295 | nb++; |
POTLESS_2 | 6:8bae97034114 | 296 | } |
POTLESS_2 | 16:74cb32bb162f | 297 | |
POTLESS_2 | 16:74cb32bb162f | 298 | serial_port->printf("!\r\n"); |
POTLESS_2 | 16:74cb32bb162f | 299 | |
POTLESS_2 | 10:21f3ff5d6a3d | 300 | DEBUG("\r\n Nombre de fichiers = %d\r\n", nb); |
POTLESS_2 | 12:9dd7c462a2c0 | 301 | DEEP_DEBUG(" Fermeture du répertoire.\r\n"); |
POTLESS_2 | 6:8bae97034114 | 302 | error = closedir(dir); |
POTLESS_2 | 0:794c15bd30b0 | 303 | return_error(error); |
POTLESS_2 | 0:794c15bd30b0 | 304 | } |
POTLESS_2 | 0:794c15bd30b0 | 305 | |
POTLESS_2 | 16:74cb32bb162f | 306 | |
POTLESS_2 | 3:01677db45b2a | 307 | int UTILS::File_Index() |
POTLESS_2 | 3:01677db45b2a | 308 | { |
POTLESS_2 | 3:01677db45b2a | 309 | int error = 0; |
POTLESS_2 | 3:01677db45b2a | 310 | int nb = 0; |
POTLESS_2 | 6:8bae97034114 | 311 | |
POTLESS_2 | 3:01677db45b2a | 312 | DIR* dir = opendir("/Root/"); |
POTLESS_2 | 3:01677db45b2a | 313 | struct dirent* de; |
POTLESS_2 | 6:8bae97034114 | 314 | while((de = readdir(dir)) != NULL) { |
POTLESS_2 | 6:8bae97034114 | 315 | nb++; |
POTLESS_2 | 3:01677db45b2a | 316 | } |
POTLESS_2 | 3:01677db45b2a | 317 | error = closedir(dir); |
POTLESS_2 | 3:01677db45b2a | 318 | return_error(error); |
POTLESS_2 | 6:8bae97034114 | 319 | |
POTLESS_2 | 3:01677db45b2a | 320 | return nb - 2; |
POTLESS_2 | 3:01677db45b2a | 321 | } |
POTLESS_2 | 3:01677db45b2a | 322 | |
POTLESS_2 | 0:794c15bd30b0 | 323 | //Remapping d'une valeur dans une autre échelle |
POTLESS_2 | 0:794c15bd30b0 | 324 | float UTILS::Remap(float x, float in_min, float in_max, float out_min, float out_max) |
POTLESS_2 | 0:794c15bd30b0 | 325 | { |
POTLESS_2 | 0:794c15bd30b0 | 326 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
POTLESS_2 | 0:794c15bd30b0 | 327 | } |
POTLESS_2 | 0:794c15bd30b0 | 328 | |
POTLESS_2 | 0:794c15bd30b0 | 329 | //Contraint une valeur entre deux limites |
POTLESS_2 | 0:794c15bd30b0 | 330 | float UTILS::constrain(float x, float a, float b) |
POTLESS_2 | 0:794c15bd30b0 | 331 | { |
POTLESS_2 | 0:794c15bd30b0 | 332 | if(x < a) { |
POTLESS_2 | 0:794c15bd30b0 | 333 | return a; |
POTLESS_2 | 0:794c15bd30b0 | 334 | } else if(b < x) { |
POTLESS_2 | 0:794c15bd30b0 | 335 | return b; |
POTLESS_2 | 0:794c15bd30b0 | 336 | } else |
POTLESS_2 | 0:794c15bd30b0 | 337 | return x; |
POTLESS_2 | 6:8bae97034114 | 338 | } |
POTLESS_2 | 6:8bae97034114 | 339 | |
POTLESS_2 | 18:522e18b47823 | 340 | void UTILS::Help(USBSerial *serial_port) |
POTLESS_2 | 6:8bae97034114 | 341 | { |
POTLESS_2 | 16:74cb32bb162f | 342 | serial_port->printf("?\r\n"); |
POTLESS_2 | 16:74cb32bb162f | 343 | |
POTLESS_2 | 15:a8c19ad9c11d | 344 | serial_port->printf(" COMMANDES GENERALES\r\n\r\n"); |
POTLESS_2 | 14:fbf4be649b6a | 345 | /* |
POTLESS_2 | 15:a8c19ad9c11d | 346 | serial_port->printf(" help = liste des commandes.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 347 | serial_port->printf(" start = démmarage des sorties moniteur.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 348 | serial_port->printf(" stop = arrêt des sorties moniteur.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 349 | serial_port->printf(" time X = met la RTC à l'heure, X en UNIX TIME.\n\r"); |
POTLESS_2 | 14:fbf4be649b6a | 350 | */ |
POTLESS_2 | 15:a8c19ad9c11d | 351 | serial_port->printf(" ARNSRS_ID X = entrée de l'identifiant de l'appareil X.\r\n"); |
POTLESS_2 | 14:fbf4be649b6a | 352 | /* |
POTLESS_2 | 15:a8c19ad9c11d | 353 | serial_port->printf(" sleep = mise en veille.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 354 | serial_port->printf(" reset = reset de la carte.\r\n\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 355 | serial_port->printf(" COMMANDES MEMOIRE\r\n\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 356 | serial_port->serial_port->printf(" rec = démarrage / arrêt enregistrement.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 357 | serial_port->printf(" flash_i = informations sur la flash.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 358 | serial_port->printf(" flash_u = démonte la flash. A faire avant de débrancher ou recharger un code.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 359 | serial_port->printf(" flash_m = monte la flash.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 360 | serial_port->printf(" flash_c = nettoyage flash, suppression des fichiers LOG.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 361 | serial_port->printf(" dir = liste des fichiers de la flash.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 362 | serial_port->printf(" del X = effacer le fichier LOG_X.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 363 | serial_port->printf(" get X = récupérer le contenu du fichier LOG_X.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 364 | serial_port->printf(" file_s X = récupérer la taille du fichier LOG_X.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 365 | //serial_port->printf(" file_i X = récupérer les informations sur le fichier LOG_X.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 366 | serial_port->printf(" COMMANDES MOTEURS\r\n\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 367 | serial_port->printf(" c_pou X = changement consigne volet poumon à X.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 368 | serial_port->printf(" c_fui X = changement consigne volet fuite à X.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 369 | serial_port->printf(" calib_p = calibration à 0 du volet poumon.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 370 | serial_port->printf(" calib_f = calibration à 0 du volet fuite.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 371 | serial_port->printf(" secu = mise des volets en mode sécu.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 372 | serial_port->printf(" check_F = sortie des valeurs 0 moteurs et ARNSRS_ID enregistrées.\r\n"); |
POTLESS_2 | 14:fbf4be649b6a | 373 | */ |
POTLESS_2 | 14:fbf4be649b6a | 374 | |
POTLESS_2 | 15:a8c19ad9c11d | 375 | serial_port->printf(" COMMANDES CAPTEURS\r\n\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 376 | serial_port->printf(" Head_ID X = Numérote le numéro X d'identification de la sensor Head dans l'eeprom.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 377 | serial_port->printf(" O2_1_ID X = Numérote le numéro X d'identification de la cellule O2 1 dans l'eeprom.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 378 | serial_port->printf(" O2_2_ID X = Numérote le numéro X d'identification de la cellule O2 2 dans l'eeprom.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 379 | serial_port->printf(" CO2_ID X = Numérote le numéro X d'identification du capteur CO2 dans l'eeprom.\r\n"); |
POTLESS_2 | 16:74cb32bb162f | 380 | //serial_port->printf(" check_E = sortie des valeurs de calibration enregistrées dans l'eeprom.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 381 | serial_port->printf(" calib_O2 X = calibration des cellules O2 dans l'air avec X itérations.\r\n"); |
POTLESS_2 | 12:9dd7c462a2c0 | 382 | |
POTLESS_2 | 15:a8c19ad9c11d | 383 | serial_port->printf(" Les commandes pour le Mini-r sont à entrer conformément à la doc. Les plus utilisée :\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 384 | serial_port->printf(" G = calibration du capteur CO2 dans l'air.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 385 | serial_port->printf(" K 0 = arrêt du capteur CO2.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 386 | serial_port->printf(" K 1 = capteur CO2 mode streaming.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 387 | serial_port->printf(" K 2 = capteur CO2 mode spooling.\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 388 | serial_port->printf(" A X = digi filter X. X égal à 32, 64, 128...\r\n"); |
potless | 19:2b5bac7f435e | 389 | serial_port->printf(""); |
potless | 19:2b5bac7f435e | 390 | serial_port->printf(" DFU = mise à jour firmware\r\n"); |
POTLESS_2 | 15:a8c19ad9c11d | 391 | //serial_port->printf("\r\n"); |
POTLESS_2 | 6:8bae97034114 | 392 | fflush(stdout); |
POTLESS_2 | 11:a41267cffa61 | 393 | } |
POTLESS_2 | 11:a41267cffa61 | 394 | |
POTLESS_2 | 11:a41267cffa61 | 395 | void UTILS::clean_line_EEPROM(unsigned int eeaddress, int address) |
POTLESS_2 | 11:a41267cffa61 | 396 | { |
POTLESS_2 | 12:9dd7c462a2c0 | 397 | DEEP_DEBUG(" Effacage de la ligne à l'adresse %d de l'eeprom.\r\n", eeaddress); |
POTLESS_2 | 11:a41267cffa61 | 398 | |
POTLESS_2 | 11:a41267cffa61 | 399 | int size = 64; |
POTLESS_2 | 11:a41267cffa61 | 400 | char deleteData[] = {" "}; |
POTLESS_2 | 11:a41267cffa61 | 401 | |
POTLESS_2 | 11:a41267cffa61 | 402 | char i2cBuffer[size + 2]; |
POTLESS_2 | 11:a41267cffa61 | 403 | i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB |
POTLESS_2 | 11:a41267cffa61 | 404 | i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF); // LSB |
POTLESS_2 | 11:a41267cffa61 | 405 | |
POTLESS_2 | 11:a41267cffa61 | 406 | for (int i = 0; i < size; i++) { |
POTLESS_2 | 11:a41267cffa61 | 407 | i2cBuffer[i + 2] = deleteData[i]; |
POTLESS_2 | 11:a41267cffa61 | 408 | } |
POTLESS_2 | 11:a41267cffa61 | 409 | |
POTLESS_2 | 11:a41267cffa61 | 410 | int result = I2C_EEPROM.write(address, i2cBuffer, size + 2, false); |
POTLESS_2 | 11:a41267cffa61 | 411 | wait_ms(6); |
POTLESS_2 | 11:a41267cffa61 | 412 | |
POTLESS_2 | 12:9dd7c462a2c0 | 413 | DEEP_DEBUG(" Ligne à l'adresse %d de l'eeprom effacée.\r\n", eeaddress); |
POTLESS_2 | 11:a41267cffa61 | 414 | } |
POTLESS_2 | 11:a41267cffa61 | 415 | |
POTLESS_2 | 11:a41267cffa61 | 416 | //Max 63 bit en écriture |
POTLESS_2 | 11:a41267cffa61 | 417 | void UTILS::write_EEPROM(char *data, unsigned int eeaddress, int address) |
POTLESS_2 | 11:a41267cffa61 | 418 | { |
POTLESS_2 | 11:a41267cffa61 | 419 | //Nettoyage de la ligne ou on va stocker la nouvelle data |
POTLESS_2 | 11:a41267cffa61 | 420 | clean_line_EEPROM(eeaddress); |
POTLESS_2 | 11:a41267cffa61 | 421 | |
POTLESS_2 | 11:a41267cffa61 | 422 | // On calcul de la taille de la chaine a enregistrer |
POTLESS_2 | 11:a41267cffa61 | 423 | char size = 0; |
POTLESS_2 | 11:a41267cffa61 | 424 | do { |
POTLESS_2 | 11:a41267cffa61 | 425 | size++; |
POTLESS_2 | 11:a41267cffa61 | 426 | } while (data[size]); |
POTLESS_2 | 11:a41267cffa61 | 427 | |
POTLESS_2 | 11:a41267cffa61 | 428 | char i2cBuffer[size + 2]; |
POTLESS_2 | 11:a41267cffa61 | 429 | i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB |
POTLESS_2 | 11:a41267cffa61 | 430 | i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF); // LSB |
POTLESS_2 | 11:a41267cffa61 | 431 | |
POTLESS_2 | 11:a41267cffa61 | 432 | for (int i = 0; i < size; i++) { |
POTLESS_2 | 11:a41267cffa61 | 433 | i2cBuffer[i + 2] = data[i]; |
POTLESS_2 | 11:a41267cffa61 | 434 | } |
POTLESS_2 | 11:a41267cffa61 | 435 | |
POTLESS_2 | 11:a41267cffa61 | 436 | int res = I2C_EEPROM.write(address, i2cBuffer, size + 2, false); |
POTLESS_2 | 11:a41267cffa61 | 437 | |
POTLESS_2 | 11:a41267cffa61 | 438 | wait_ms(6); |
POTLESS_2 | 11:a41267cffa61 | 439 | |
POTLESS_2 | 12:9dd7c462a2c0 | 440 | DEEP_DEBUG(" Chaine écrite à l'adresse %d de l'eeprom : %s\r\n", eeaddress , data); |
POTLESS_2 | 11:a41267cffa61 | 441 | } |
POTLESS_2 | 11:a41267cffa61 | 442 | |
POTLESS_2 | 12:9dd7c462a2c0 | 443 | char* UTILS::read_EEPROM(unsigned int eeaddress , int address) |
POTLESS_2 | 12:9dd7c462a2c0 | 444 | { |
POTLESS_2 | 12:9dd7c462a2c0 | 445 | //On lit toute la ligne |
POTLESS_2 | 12:9dd7c462a2c0 | 446 | int size = 17; |
POTLESS_2 | 12:9dd7c462a2c0 | 447 | static char data[17]; |
POTLESS_2 | 12:9dd7c462a2c0 | 448 | |
POTLESS_2 | 12:9dd7c462a2c0 | 449 | char i2cBuffer[2]; |
POTLESS_2 | 12:9dd7c462a2c0 | 450 | i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB |
POTLESS_2 | 12:9dd7c462a2c0 | 451 | i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF); // LSB |
POTLESS_2 | 12:9dd7c462a2c0 | 452 | |
POTLESS_2 | 12:9dd7c462a2c0 | 453 | // Reset eeprom pointer address |
POTLESS_2 | 12:9dd7c462a2c0 | 454 | int result = I2C_EEPROM.write(address, i2cBuffer, 2, false); |
POTLESS_2 | 12:9dd7c462a2c0 | 455 | wait_ms(6); |
POTLESS_2 | 12:9dd7c462a2c0 | 456 | |
POTLESS_2 | 12:9dd7c462a2c0 | 457 | // Read eeprom |
POTLESS_2 | 12:9dd7c462a2c0 | 458 | I2C_EEPROM.read(address, data, size); |
POTLESS_2 | 12:9dd7c462a2c0 | 459 | wait_ms(6); |
POTLESS_2 | 12:9dd7c462a2c0 | 460 | |
POTLESS_2 | 12:9dd7c462a2c0 | 461 | DEEP_DEBUG(" Char lu à l'adresse %d de l'eeprom : %s\r\n", eeaddress, &data); |
POTLESS_2 | 12:9dd7c462a2c0 | 462 | |
POTLESS_2 | 12:9dd7c462a2c0 | 463 | return data; |
POTLESS_2 | 12:9dd7c462a2c0 | 464 | } |
POTLESS_2 | 12:9dd7c462a2c0 | 465 | |
POTLESS_2 | 12:9dd7c462a2c0 | 466 | void UTILS::read_C_EEPROM(char *data, unsigned int eeaddress ,int address) |
POTLESS_2 | 12:9dd7c462a2c0 | 467 | { |
POTLESS_2 | 12:9dd7c462a2c0 | 468 | //On lit toute la ligne |
POTLESS_2 | 12:9dd7c462a2c0 | 469 | int size = 64; |
POTLESS_2 | 12:9dd7c462a2c0 | 470 | |
POTLESS_2 | 12:9dd7c462a2c0 | 471 | char i2cBuffer[2]; |
POTLESS_2 | 12:9dd7c462a2c0 | 472 | i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB |
POTLESS_2 | 12:9dd7c462a2c0 | 473 | i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF); // LSB |
POTLESS_2 | 12:9dd7c462a2c0 | 474 | |
POTLESS_2 | 12:9dd7c462a2c0 | 475 | // Reset eeprom pointer address |
POTLESS_2 | 12:9dd7c462a2c0 | 476 | int result = I2C_EEPROM.write(address, i2cBuffer, 2, false); |
POTLESS_2 | 12:9dd7c462a2c0 | 477 | wait_ms(6); |
POTLESS_2 | 12:9dd7c462a2c0 | 478 | |
POTLESS_2 | 12:9dd7c462a2c0 | 479 | // Read eeprom |
POTLESS_2 | 12:9dd7c462a2c0 | 480 | I2C_EEPROM.read(address, data, size); |
POTLESS_2 | 12:9dd7c462a2c0 | 481 | wait_ms(6); |
POTLESS_2 | 12:9dd7c462a2c0 | 482 | |
POTLESS_2 | 12:9dd7c462a2c0 | 483 | DEEP_DEBUG(" Char lu à l'adresse %d de l'eeprom : %s\r\n", eeaddress, data); |
POTLESS_2 | 12:9dd7c462a2c0 | 484 | } |
POTLESS_2 | 11:a41267cffa61 | 485 | |
POTLESS_2 | 11:a41267cffa61 | 486 | float UTILS::read_F_EEPROM(unsigned int eeaddress ,int address) |
POTLESS_2 | 11:a41267cffa61 | 487 | { |
POTLESS_2 | 11:a41267cffa61 | 488 | //On lit toute la ligne |
POTLESS_2 | 11:a41267cffa61 | 489 | int size = 64; |
POTLESS_2 | 11:a41267cffa61 | 490 | char data[64]; |
POTLESS_2 | 11:a41267cffa61 | 491 | |
POTLESS_2 | 11:a41267cffa61 | 492 | char i2cBuffer[2]; |
POTLESS_2 | 11:a41267cffa61 | 493 | i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB |
POTLESS_2 | 11:a41267cffa61 | 494 | i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF); // LSB |
POTLESS_2 | 11:a41267cffa61 | 495 | |
POTLESS_2 | 11:a41267cffa61 | 496 | int result = I2C_EEPROM.write(address, i2cBuffer, 2, false); |
POTLESS_2 | 11:a41267cffa61 | 497 | wait_ms(6); |
POTLESS_2 | 11:a41267cffa61 | 498 | |
POTLESS_2 | 11:a41267cffa61 | 499 | I2C_EEPROM.read(address, data, size); |
POTLESS_2 | 11:a41267cffa61 | 500 | wait_ms(6); |
POTLESS_2 | 11:a41267cffa61 | 501 | |
POTLESS_2 | 11:a41267cffa61 | 502 | float result_F = atof(data); |
POTLESS_2 | 12:9dd7c462a2c0 | 503 | DEEP_DEBUG(" Float lu à l'adresse %d de l'eeprom : %f\r\n", eeaddress, result_F); |
POTLESS_2 | 11:a41267cffa61 | 504 | return result_F; |
POTLESS_2 | 11:a41267cffa61 | 505 | } |
POTLESS_2 | 11:a41267cffa61 | 506 | |
POTLESS_2 | 11:a41267cffa61 | 507 | |
POTLESS_2 | 11:a41267cffa61 | 508 | int UTILS::read_I_EEPROM(unsigned int eeaddress ,int address) |
POTLESS_2 | 11:a41267cffa61 | 509 | { |
POTLESS_2 | 11:a41267cffa61 | 510 | //On lit toute la ligne |
POTLESS_2 | 11:a41267cffa61 | 511 | int size = 64; |
POTLESS_2 | 11:a41267cffa61 | 512 | char data[64]; |
POTLESS_2 | 11:a41267cffa61 | 513 | |
POTLESS_2 | 11:a41267cffa61 | 514 | char i2cBuffer[2]; |
POTLESS_2 | 11:a41267cffa61 | 515 | i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB |
POTLESS_2 | 11:a41267cffa61 | 516 | i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF); // LSB |
POTLESS_2 | 11:a41267cffa61 | 517 | |
POTLESS_2 | 11:a41267cffa61 | 518 | int result = I2C_EEPROM.write(address, i2cBuffer, 2, false); |
POTLESS_2 | 11:a41267cffa61 | 519 | wait_ms(6); |
POTLESS_2 | 11:a41267cffa61 | 520 | |
POTLESS_2 | 11:a41267cffa61 | 521 | I2C_EEPROM.read(address, data, size); |
POTLESS_2 | 11:a41267cffa61 | 522 | wait_ms(6); |
POTLESS_2 | 11:a41267cffa61 | 523 | |
POTLESS_2 | 11:a41267cffa61 | 524 | int result_I = atoi(data); |
POTLESS_2 | 12:9dd7c462a2c0 | 525 | DEEP_DEBUG(" Int lu à l'adresse %d l'eeprom : %d\r\n", eeaddress, result_I); |
POTLESS_2 | 11:a41267cffa61 | 526 | return result_I; |
POTLESS_2 | 11:a41267cffa61 | 527 | } |
POTLESS_2 | 11:a41267cffa61 | 528 | |
POTLESS_2 | 11:a41267cffa61 | 529 | void UTILS::clean_EEPROM(int address) |
POTLESS_2 | 11:a41267cffa61 | 530 | { |
POTLESS_2 | 11:a41267cffa61 | 531 | DEBUG(" Nettoyage total de l'Eeprom (64 colonnes, 4096 lignes).\r\n"); |
POTLESS_2 | 11:a41267cffa61 | 532 | int i; |
POTLESS_2 | 11:a41267cffa61 | 533 | int pointer; |
POTLESS_2 | 11:a41267cffa61 | 534 | char deleteData[] = {" "}; |
POTLESS_2 | 11:a41267cffa61 | 535 | for (i = 0; i < 4096; i++) { |
POTLESS_2 | 11:a41267cffa61 | 536 | pointer = 64 * i; |
POTLESS_2 | 11:a41267cffa61 | 537 | clean_line_EEPROM(pointer, address); |
POTLESS_2 | 11:a41267cffa61 | 538 | } |
POTLESS_2 | 11:a41267cffa61 | 539 | DEBUG("Eeprom néttoyée.\r\n"); |
potless | 19:2b5bac7f435e | 540 | } |
potless | 19:2b5bac7f435e | 541 | |
potless | 19:2b5bac7f435e | 542 | void UTILS::DFU() |
potless | 19:2b5bac7f435e | 543 | { |
potless | 19:2b5bac7f435e | 544 | DEEP_DEBUG(" mode DFU utils \r\n\r\n"); |
potless | 19:2b5bac7f435e | 545 | |
potless | 19:2b5bac7f435e | 546 | DEEP_DEBUG(" mode DFU deadbeef \r\n\r\n"); |
potless | 19:2b5bac7f435e | 547 | wait(1); |
potless | 19:2b5bac7f435e | 548 | |
potless | 19:2b5bac7f435e | 549 | |
potless | 19:2b5bac7f435e | 550 | |
potless | 19:2b5bac7f435e | 551 | void (*SysMemBootJump)(void); |
potless | 19:2b5bac7f435e | 552 | |
potless | 19:2b5bac7f435e | 553 | /** |
potless | 19:2b5bac7f435e | 554 | * Step: Set system memory address. |
potless | 19:2b5bac7f435e | 555 | * |
potless | 19:2b5bac7f435e | 556 | * For STM32F446, system memory is on 0x1FFF 0000 |
potless | 19:2b5bac7f435e | 557 | * For other families, check AN2606 document table 110 with descriptions of memory addresses |
potless | 19:2b5bac7f435e | 558 | */ |
potless | 19:2b5bac7f435e | 559 | volatile uint32_t addr = 0x1FFF0000; |
potless | 19:2b5bac7f435e | 560 | |
potless | 19:2b5bac7f435e | 561 | |
potless | 19:2b5bac7f435e | 562 | /** |
potless | 19:2b5bac7f435e | 563 | * Step: Disable RCC, set it to default (after reset) settings |
potless | 19:2b5bac7f435e | 564 | * Internal clock, no PLL, etc. |
potless | 19:2b5bac7f435e | 565 | */ |
potless | 19:2b5bac7f435e | 566 | HAL_RCC_DeInit(); |
potless | 19:2b5bac7f435e | 567 | DEEP_DEBUG(" mode DFU rcc deinit \r\n\r\n"); |
potless | 19:2b5bac7f435e | 568 | wait(1); |
potless | 19:2b5bac7f435e | 569 | |
potless | 19:2b5bac7f435e | 570 | __HAL_RCC_SYSCFG_CLK_ENABLE(); |
potless | 19:2b5bac7f435e | 571 | DEEP_DEBUG(" mode DFU clk enable \r\n\r\n"); |
potless | 19:2b5bac7f435e | 572 | wait(1); |
potless | 19:2b5bac7f435e | 573 | |
potless | 19:2b5bac7f435e | 574 | /** |
potless | 19:2b5bac7f435e | 575 | * Step: Disable systick timer and reset it to default values |
potless | 19:2b5bac7f435e | 576 | */ |
potless | 19:2b5bac7f435e | 577 | SysTick->CTRL = 0; |
potless | 19:2b5bac7f435e | 578 | SysTick->LOAD = 0; |
potless | 19:2b5bac7f435e | 579 | SysTick->VAL = 0; |
potless | 19:2b5bac7f435e | 580 | |
potless | 19:2b5bac7f435e | 581 | |
potless | 19:2b5bac7f435e | 582 | /** |
potless | 19:2b5bac7f435e | 583 | * Step: Disable all interrupts |
potless | 19:2b5bac7f435e | 584 | */ |
potless | 19:2b5bac7f435e | 585 | __disable_irq(); |
potless | 19:2b5bac7f435e | 586 | DEEP_DEBUG(" mode DFU irq disable \r\n\r\n"); |
potless | 19:2b5bac7f435e | 587 | wait(1); |
potless | 19:2b5bac7f435e | 588 | |
potless | 19:2b5bac7f435e | 589 | __DSB(); |
potless | 19:2b5bac7f435e | 590 | |
potless | 19:2b5bac7f435e | 591 | /** |
potless | 19:2b5bac7f435e | 592 | * Step: Remap system memory to address 0x0000 0000 in address space |
potless | 19:2b5bac7f435e | 593 | * For each family registers may be different. |
potless | 19:2b5bac7f435e | 594 | * Check reference manual for each family. |
potless | 19:2b5bac7f435e | 595 | * |
potless | 19:2b5bac7f435e | 596 | * For STM32F4xx, MEMRMP register in SYSCFG is used (bits[1:0]) |
potless | 19:2b5bac7f435e | 597 | * For STM32F0xx, CFGR1 register in SYSCFG is used (bits[1:0]) |
potless | 19:2b5bac7f435e | 598 | * For others, check family reference manual |
potless | 19:2b5bac7f435e | 599 | */ |
potless | 19:2b5bac7f435e | 600 | //SYSCFG->MEMRMP = 1; |
potless | 19:2b5bac7f435e | 601 | |
potless | 19:2b5bac7f435e | 602 | __HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH(); //Call HAL macro to do this for you |
potless | 19:2b5bac7f435e | 603 | |
potless | 19:2b5bac7f435e | 604 | DEEP_DEBUG(" mode DFU remap \r\n\r\n"); |
potless | 19:2b5bac7f435e | 605 | wait(1); |
potless | 19:2b5bac7f435e | 606 | |
potless | 19:2b5bac7f435e | 607 | /** |
potless | 19:2b5bac7f435e | 608 | * Step: Set jump memory location for system memory |
potless | 19:2b5bac7f435e | 609 | * Use address with 4 bytes offset which specifies jump location where program starts |
potless | 19:2b5bac7f435e | 610 | */ |
potless | 19:2b5bac7f435e | 611 | |
potless | 19:2b5bac7f435e | 612 | |
potless | 19:2b5bac7f435e | 613 | /* Remap is not visible at once. Execute some unrelated command! */ |
potless | 19:2b5bac7f435e | 614 | __DSB(); |
potless | 19:2b5bac7f435e | 615 | __ISB(); |
potless | 19:2b5bac7f435e | 616 | |
potless | 19:2b5bac7f435e | 617 | |
potless | 19:2b5bac7f435e | 618 | |
potless | 19:2b5bac7f435e | 619 | |
potless | 19:2b5bac7f435e | 620 | SysMemBootJump = (void (*)(void)) (*((uint32_t *)(addr + 4))); |
potless | 19:2b5bac7f435e | 621 | |
potless | 19:2b5bac7f435e | 622 | DEEP_DEBUG(" mode DFU jump \r\n\r\n"); |
potless | 19:2b5bac7f435e | 623 | wait(1); |
potless | 19:2b5bac7f435e | 624 | |
potless | 19:2b5bac7f435e | 625 | |
potless | 19:2b5bac7f435e | 626 | /** |
potless | 19:2b5bac7f435e | 627 | * Step: Set main stack pointer. |
potless | 19:2b5bac7f435e | 628 | * This step must be done last otherwise local variables in this function |
potless | 19:2b5bac7f435e | 629 | * don't have proper value since stack pointer is located on different position |
potless | 19:2b5bac7f435e | 630 | * |
potless | 19:2b5bac7f435e | 631 | * Set direct address location which specifies stack pointer in SRAM location |
potless | 19:2b5bac7f435e | 632 | */ |
potless | 19:2b5bac7f435e | 633 | __set_MSP(*(uint32_t *)addr); |
potless | 19:2b5bac7f435e | 634 | |
potless | 19:2b5bac7f435e | 635 | DEEP_DEBUG(" mode DFU set msp \r\n\r\n"); |
potless | 19:2b5bac7f435e | 636 | wait(1); |
potless | 19:2b5bac7f435e | 637 | |
potless | 19:2b5bac7f435e | 638 | /** |
potless | 19:2b5bac7f435e | 639 | * Step: Actually call our function to jump to set location |
potless | 19:2b5bac7f435e | 640 | * This will start system memory execution |
potless | 19:2b5bac7f435e | 641 | */ |
potless | 19:2b5bac7f435e | 642 | |
potless | 19:2b5bac7f435e | 643 | SysMemBootJump(); |
potless | 19:2b5bac7f435e | 644 | |
potless | 19:2b5bac7f435e | 645 | /** |
potless | 19:2b5bac7f435e | 646 | * Step: Connect USB<->UART converter to dedicated USART pins and test |
potless | 19:2b5bac7f435e | 647 | * and test with bootloader works with STM32 Flash Loader Demonstrator software |
potless | 19:2b5bac7f435e | 648 | */ |
potless | 19:2b5bac7f435e | 649 | |
potless | 19:2b5bac7f435e | 650 | while (1); |
potless | 19:2b5bac7f435e | 651 | |
potless | 19:2b5bac7f435e | 652 | |
POTLESS_2 | 15:a8c19ad9c11d | 653 | } |