teste de publish
Dependencies: DS1820 HighSpeedAnalogIn devices mbed
funcoesSDCard.cpp@13:b9183b4bc049, 2017-10-23 (annotated)
- Committer:
- brunofgc
- Date:
- Mon Oct 23 15:35:18 2017 +0000
- Revision:
- 13:b9183b4bc049
- Parent:
- 11:631bea162800
- Child:
- 14:c0162ab2a951
vers?o com rename de arquivo ainda gerando eventuais erros; esta vers?o j? faz bootloader pela internet sem usar memoria AHB extra
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brunofgc | 0:1c0a769988ee | 1 | #include "funcoesSDCard.h" |
brunofgc | 0:1c0a769988ee | 2 | #include "serialPC.h" |
brunofgc | 0:1c0a769988ee | 3 | |
brunofgc | 10:263c093f8977 | 4 | |
brunofgc | 10:263c093f8977 | 5 | arquivoSD sdCard::bank0; |
brunofgc | 10:263c093f8977 | 6 | arquivoSD sdCard::bank1; |
brunofgc | 10:263c093f8977 | 7 | arquivoSD sdCard::currentBankFile; |
brunofgc | 0:1c0a769988ee | 8 | arquivoSD sdCard::config; |
brunofgc | 0:1c0a769988ee | 9 | arquivoSD sdCard::devices; |
brunofgc | 0:1c0a769988ee | 10 | arquivoSD sdCard::status; |
brunofgc | 0:1c0a769988ee | 11 | arquivoSD sdCard::tempFile; |
brunofgc | 13:b9183b4bc049 | 12 | arquivoSD sdCard::newFirmware; |
brunofgc | 13:b9183b4bc049 | 13 | uint32_t sdCard::nBytesArquivoRecebidos; |
brunofgc | 10:263c093f8977 | 14 | uint8_t sdCard::currentBank; |
brunofgc | 11:631bea162800 | 15 | uint8_t sdCard::currentBankSending; |
brunofgc | 7:ae9c47f62946 | 16 | bool sdCard::arquivosAbertos=false; |
brunofgc | 0:1c0a769988ee | 17 | |
brunofgc | 0:1c0a769988ee | 18 | char sdCard::nomeArquivoEmRecebimento[30]; |
brunofgc | 0:1c0a769988ee | 19 | |
brunofgc | 0:1c0a769988ee | 20 | |
brunofgc | 0:1c0a769988ee | 21 | bool sdCard::deleteSentFiles; |
brunofgc | 0:1c0a769988ee | 22 | |
brunofgc | 0:1c0a769988ee | 23 | SDFileSystem sd(p11, p12, p13, p14, "sd"); |
brunofgc | 0:1c0a769988ee | 24 | |
brunofgc | 0:1c0a769988ee | 25 | |
brunofgc | 0:1c0a769988ee | 26 | bool sdCard::abreArquivo(arquivoSD *arquivo,char *mode){ |
brunofgc | 0:1c0a769988ee | 27 | //Struct do arquivo e seu nome |
brunofgc | 7:ae9c47f62946 | 28 | /*uint32_t timeOut=30000; |
brunofgc | 7:ae9c47f62946 | 29 | while(timeOut && arquivo->aberto){ |
brunofgc | 7:ae9c47f62946 | 30 | timeOut--; |
brunofgc | 7:ae9c47f62946 | 31 | osDelay(1); |
brunofgc | 0:1c0a769988ee | 32 | } |
brunofgc | 7:ae9c47f62946 | 33 | if(arquivo->aberto){return false;} |
brunofgc | 7:ae9c47f62946 | 34 | */ |
brunofgc | 10:263c093f8977 | 35 | //if(!sdCard::arquivoAberto(15000)){return false;} |
brunofgc | 7:ae9c47f62946 | 36 | |
brunofgc | 7:ae9c47f62946 | 37 | //pc.printf("Abrindo um arquivo de nome <%s> em modo <%s>.\r\n",arquivo->nome,mode); |
brunofgc | 7:ae9c47f62946 | 38 | |
brunofgc | 0:1c0a769988ee | 39 | //pc.printf("Abrindo arquivo <%s>.\n",arquivo->nome); |
brunofgc | 0:1c0a769988ee | 40 | if((arquivo->fp = fopen(arquivo->nome,mode))!=NULL){ |
brunofgc | 0:1c0a769988ee | 41 | arquivo->aberto = true; |
brunofgc | 0:1c0a769988ee | 42 | return true; |
brunofgc | 0:1c0a769988ee | 43 | }else{ |
brunofgc | 0:1c0a769988ee | 44 | return false; |
brunofgc | 0:1c0a769988ee | 45 | } |
brunofgc | 0:1c0a769988ee | 46 | } |
brunofgc | 0:1c0a769988ee | 47 | |
brunofgc | 0:1c0a769988ee | 48 | uint16_t sdCard::calcCheckSum16BITFile(char *nome){ |
brunofgc | 0:1c0a769988ee | 49 | FILE *fp; |
brunofgc | 0:1c0a769988ee | 50 | char c; |
brunofgc | 0:1c0a769988ee | 51 | uint16_t sum = 0; |
brunofgc | 0:1c0a769988ee | 52 | |
brunofgc | 0:1c0a769988ee | 53 | fp = fopen (nome,"r"); |
brunofgc | 0:1c0a769988ee | 54 | if(fp != NULL){ |
brunofgc | 0:1c0a769988ee | 55 | do{ |
brunofgc | 0:1c0a769988ee | 56 | c = fgetc(fp); |
brunofgc | 0:1c0a769988ee | 57 | if(!feof(fp)){sum+=c;} |
brunofgc | 0:1c0a769988ee | 58 | }while(!feof(fp)); |
brunofgc | 0:1c0a769988ee | 59 | fclose(fp); |
brunofgc | 0:1c0a769988ee | 60 | } |
brunofgc | 0:1c0a769988ee | 61 | return sum; |
brunofgc | 0:1c0a769988ee | 62 | } |
brunofgc | 0:1c0a769988ee | 63 | |
brunofgc | 7:ae9c47f62946 | 64 | /*bool sdCard::insereDadosArquivoPeloNome(char *nome, char *dados,uint16_t tam){ |
brunofgc | 0:1c0a769988ee | 65 | FILE *fp; |
brunofgc | 0:1c0a769988ee | 66 | fp = fopen(nome,"a"); |
brunofgc | 0:1c0a769988ee | 67 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 68 | if(fp!=NULL){ |
brunofgc | 7:ae9c47f62946 | 69 | sdCard::arquivoAberto = true; |
brunofgc | 0:1c0a769988ee | 70 | for(i=0;i<tam;i++){ |
brunofgc | 0:1c0a769988ee | 71 | fprintf(fp,"%c",dados[i]); |
brunofgc | 0:1c0a769988ee | 72 | fflush(fp); |
brunofgc | 0:1c0a769988ee | 73 | } |
brunofgc | 0:1c0a769988ee | 74 | fclose(fp); |
brunofgc | 7:ae9c47f62946 | 75 | sdCard::arquivoAberto = false; |
brunofgc | 0:1c0a769988ee | 76 | }else{ |
brunofgc | 0:1c0a769988ee | 77 | return false; |
brunofgc | 0:1c0a769988ee | 78 | } |
brunofgc | 0:1c0a769988ee | 79 | return true; |
brunofgc | 7:ae9c47f62946 | 80 | }*/ |
brunofgc | 0:1c0a769988ee | 81 | |
brunofgc | 11:631bea162800 | 82 | void sdCard::verificaCurrentBank(void){ |
brunofgc | 10:263c093f8977 | 83 | if(sdCard::abreArquivo(&sdCard::currentBankFile,"r")){ |
brunofgc | 11:631bea162800 | 84 | pc.printf("sdCard::verificaCurrentBank[1]\r\n"); |
brunofgc | 11:631bea162800 | 85 | sdCard::currentBank = 3; |
brunofgc | 11:631bea162800 | 86 | sdCard::currentBankSending = 3; |
brunofgc | 10:263c093f8977 | 87 | pc.printf("sdCard::verificaCurrentBank[2]\r\n"); |
brunofgc | 13:b9183b4bc049 | 88 | sdCard::currentBank = fgetc(sdCard::currentBankFile.fp); |
brunofgc | 13:b9183b4bc049 | 89 | sdCard::currentBank = sdCard::currentBank - 48; |
brunofgc | 13:b9183b4bc049 | 90 | sdCard::currentBankSending = fgetc(sdCard::currentBankFile.fp); |
brunofgc | 13:b9183b4bc049 | 91 | sdCard::currentBankSending = sdCard::currentBankSending -48; |
brunofgc | 13:b9183b4bc049 | 92 | sdCard::fechaArquivo(&sdCard::currentBankFile); |
brunofgc | 13:b9183b4bc049 | 93 | } |
brunofgc | 13:b9183b4bc049 | 94 | if((sdCard::currentBankSending > 1)||(sdCard::currentBank > 1)){ |
brunofgc | 13:b9183b4bc049 | 95 | pc.printf("sdCard::verificaCurrentBank[3]\r\n"); |
brunofgc | 10:263c093f8977 | 96 | //Criando bank 0 |
brunofgc | 10:263c093f8977 | 97 | if(sdCard::abreArquivo(&sdCard::bank0,"w")){ |
brunofgc | 10:263c093f8977 | 98 | sdCard::fechaArquivo(&sdCard::bank0); |
brunofgc | 10:263c093f8977 | 99 | }else{ |
brunofgc | 13:b9183b4bc049 | 100 | pc.printf("sdCard::verificaCurrentBank[4]\r\n"); |
brunofgc | 11:631bea162800 | 101 | return; |
brunofgc | 10:263c093f8977 | 102 | } |
brunofgc | 10:263c093f8977 | 103 | //Criando bank 1 |
brunofgc | 10:263c093f8977 | 104 | if(sdCard::abreArquivo(&sdCard::bank1,"w")){ |
brunofgc | 10:263c093f8977 | 105 | sdCard::fechaArquivo(&sdCard::bank1); |
brunofgc | 10:263c093f8977 | 106 | }else{ |
brunofgc | 13:b9183b4bc049 | 107 | pc.printf("sdCard::verificaCurrentBank[5]\r\n"); |
brunofgc | 11:631bea162800 | 108 | return; |
brunofgc | 10:263c093f8977 | 109 | } |
brunofgc | 10:263c093f8977 | 110 | //Criando currentBank |
brunofgc | 10:263c093f8977 | 111 | if(sdCard::abreArquivo(&sdCard::currentBankFile,"w")){ |
brunofgc | 10:263c093f8977 | 112 | fputc('0',sdCard::currentBankFile.fp); |
brunofgc | 11:631bea162800 | 113 | fputc('0',sdCard::currentBankFile.fp); |
brunofgc | 13:b9183b4bc049 | 114 | //fflush(sdCard::currentBankFile.fp); |
brunofgc | 10:263c093f8977 | 115 | sdCard::fechaArquivo(&sdCard::currentBankFile); |
brunofgc | 11:631bea162800 | 116 | sdCard::currentBank = 0; |
brunofgc | 11:631bea162800 | 117 | sdCard::currentBankSending = 0; |
brunofgc | 10:263c093f8977 | 118 | }else{ |
brunofgc | 13:b9183b4bc049 | 119 | pc.printf("sdCard::verificaCurrentBank[6]\r\n"); |
brunofgc | 11:631bea162800 | 120 | return; |
brunofgc | 10:263c093f8977 | 121 | } |
brunofgc | 13:b9183b4bc049 | 122 | } |
brunofgc | 11:631bea162800 | 123 | pc.printf("sdCard::verificaCurrentBank[7]\r\n"); |
brunofgc | 10:263c093f8977 | 124 | } |
brunofgc | 10:263c093f8977 | 125 | |
brunofgc | 11:631bea162800 | 126 | uint8_t sdCard::modificaCurrentBank(uint8_t newCurrentBank,uint8_t newCurrentBankSending){ |
brunofgc | 11:631bea162800 | 127 | //sdCard::verificaCurrentBank(); |
brunofgc | 11:631bea162800 | 128 | if((sdCard::currentBank == newCurrentBank)&&(sdCard::currentBankSending == newCurrentBankSending)){ |
brunofgc | 10:263c093f8977 | 129 | return 1; |
brunofgc | 10:263c093f8977 | 130 | }else{ |
brunofgc | 11:631bea162800 | 131 | //if(!sdCard::excluiArquivo(&sdCard::currentBankFile)){return 0;} |
brunofgc | 10:263c093f8977 | 132 | if(sdCard::abreArquivo(&sdCard::currentBankFile,"w")){ |
brunofgc | 10:263c093f8977 | 133 | if(newCurrentBank){fputc('1',sdCard::currentBankFile.fp);fflush(sdCard::currentBankFile.fp);} |
brunofgc | 10:263c093f8977 | 134 | else{fputc('0',sdCard::currentBankFile.fp);fflush(sdCard::currentBankFile.fp);} |
brunofgc | 11:631bea162800 | 135 | if(newCurrentBankSending){fputc('1',sdCard::currentBankFile.fp);fflush(sdCard::currentBankFile.fp);} |
brunofgc | 11:631bea162800 | 136 | else{fputc('0',sdCard::currentBankFile.fp);fflush(sdCard::currentBankFile.fp);} |
brunofgc | 11:631bea162800 | 137 | sdCard::fechaArquivo(&sdCard::currentBankFile); |
brunofgc | 11:631bea162800 | 138 | sdCard::currentBank = newCurrentBank; |
brunofgc | 11:631bea162800 | 139 | sdCard::currentBankSending = newCurrentBankSending; |
brunofgc | 10:263c093f8977 | 140 | }else{ |
brunofgc | 11:631bea162800 | 141 | return 0; |
brunofgc | 10:263c093f8977 | 142 | } |
brunofgc | 10:263c093f8977 | 143 | } |
brunofgc | 10:263c093f8977 | 144 | return 1; |
brunofgc | 10:263c093f8977 | 145 | } |
brunofgc | 10:263c093f8977 | 146 | |
brunofgc | 10:263c093f8977 | 147 | bool sdCard::insereDadosBank(char* data,uint16_t tam){ |
brunofgc | 10:263c093f8977 | 148 | //Struct do arquivo, dados a inserir e tamanho deles |
brunofgc | 10:263c093f8977 | 149 | uint16_t i; |
brunofgc | 10:263c093f8977 | 150 | arquivoSD *arquivo; |
brunofgc | 11:631bea162800 | 151 | //verificaCurrentBank(); |
brunofgc | 11:631bea162800 | 152 | if(sdCard::currentBank > 1){ |
brunofgc | 10:263c093f8977 | 153 | pc.printf("sdCard::insereDadosBank[1]\r\n"); |
brunofgc | 10:263c093f8977 | 154 | return false; |
brunofgc | 10:263c093f8977 | 155 | } |
brunofgc | 10:263c093f8977 | 156 | |
brunofgc | 10:263c093f8977 | 157 | if(sdCard::currentBank){ |
brunofgc | 10:263c093f8977 | 158 | arquivo = &sdCard::bank1; |
brunofgc | 10:263c093f8977 | 159 | }else{ |
brunofgc | 10:263c093f8977 | 160 | arquivo = &sdCard::bank0; |
brunofgc | 10:263c093f8977 | 161 | } |
brunofgc | 10:263c093f8977 | 162 | |
brunofgc | 10:263c093f8977 | 163 | if(!sdCard::abreArquivo(arquivo,"a")){ |
brunofgc | 10:263c093f8977 | 164 | sdCard::fechaArquivo(arquivo); |
brunofgc | 10:263c093f8977 | 165 | pc.printf("sdCard::insereDadosBank[2]\r\n"); |
brunofgc | 10:263c093f8977 | 166 | return false; |
brunofgc | 10:263c093f8977 | 167 | } |
brunofgc | 10:263c093f8977 | 168 | |
brunofgc | 10:263c093f8977 | 169 | for(i=0;i<tam;i++){ |
brunofgc | 10:263c093f8977 | 170 | //fprintf(arquivo->fp,"%c",data[i]); |
brunofgc | 10:263c093f8977 | 171 | fputc(data[i],arquivo->fp); |
brunofgc | 10:263c093f8977 | 172 | fflush(arquivo->fp); |
brunofgc | 10:263c093f8977 | 173 | } |
brunofgc | 10:263c093f8977 | 174 | sdCard::fechaArquivo(arquivo); |
brunofgc | 10:263c093f8977 | 175 | pc.printf("sdCard::insereDadosBank[3]\r\n"); |
brunofgc | 10:263c093f8977 | 176 | return true; |
brunofgc | 10:263c093f8977 | 177 | } |
brunofgc | 10:263c093f8977 | 178 | |
brunofgc | 0:1c0a769988ee | 179 | bool sdCard::insereDadosArquivo(arquivoSD *arquivo, char* data,uint16_t tam){ |
brunofgc | 0:1c0a769988ee | 180 | //Struct do arquivo, dados a inserir e tamanho deles |
brunofgc | 7:ae9c47f62946 | 181 | uint16_t i; |
brunofgc | 7:ae9c47f62946 | 182 | if(!sdCard::abreArquivo(arquivo,"a")){ |
brunofgc | 7:ae9c47f62946 | 183 | sdCard::fechaArquivo(arquivo); |
brunofgc | 7:ae9c47f62946 | 184 | return false; |
brunofgc | 0:1c0a769988ee | 185 | } |
brunofgc | 0:1c0a769988ee | 186 | |
brunofgc | 0:1c0a769988ee | 187 | for(i=0;i<tam;i++){ |
brunofgc | 7:ae9c47f62946 | 188 | //fprintf(arquivo->fp,"%c",data[i]); |
brunofgc | 7:ae9c47f62946 | 189 | fputc(data[i],arquivo->fp); |
brunofgc | 0:1c0a769988ee | 190 | fflush(arquivo->fp); |
brunofgc | 7:ae9c47f62946 | 191 | } |
brunofgc | 7:ae9c47f62946 | 192 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 193 | |
brunofgc | 0:1c0a769988ee | 194 | return true; |
brunofgc | 0:1c0a769988ee | 195 | } |
brunofgc | 13:b9183b4bc049 | 196 | |
brunofgc | 13:b9183b4bc049 | 197 | uint32_t hextobin(char *hex,uint32_t len){ |
brunofgc | 13:b9183b4bc049 | 198 | int i; |
brunofgc | 13:b9183b4bc049 | 199 | uint32_t bin=0; |
brunofgc | 13:b9183b4bc049 | 200 | for(i=len;i>0;i--){ |
brunofgc | 13:b9183b4bc049 | 201 | if(hex[len-i]<65){ |
brunofgc | 13:b9183b4bc049 | 202 | bin+=((hex[len-i]-48)<<(4*(i-1))); |
brunofgc | 13:b9183b4bc049 | 203 | }else{ |
brunofgc | 13:b9183b4bc049 | 204 | bin+=((hex[len-i]-55)<<(4*(i-1))); |
brunofgc | 13:b9183b4bc049 | 205 | } |
brunofgc | 13:b9183b4bc049 | 206 | } |
brunofgc | 13:b9183b4bc049 | 207 | return bin; |
brunofgc | 13:b9183b4bc049 | 208 | } |
brunofgc | 13:b9183b4bc049 | 209 | |
brunofgc | 13:b9183b4bc049 | 210 | bool sdCard::insereDadosArquivoHex(arquivoSD *arquivo, char* data,uint16_t tam){ |
brunofgc | 13:b9183b4bc049 | 211 | //Struct do arquivo, dados a inserir e tamanho deles |
brunofgc | 13:b9183b4bc049 | 212 | uint16_t i; |
brunofgc | 13:b9183b4bc049 | 213 | char c,hex[3]; |
brunofgc | 13:b9183b4bc049 | 214 | if(!sdCard::abreArquivo(arquivo,"a")){ |
brunofgc | 13:b9183b4bc049 | 215 | sdCard::fechaArquivo(arquivo); |
brunofgc | 13:b9183b4bc049 | 216 | return false; |
brunofgc | 13:b9183b4bc049 | 217 | } |
brunofgc | 13:b9183b4bc049 | 218 | |
brunofgc | 13:b9183b4bc049 | 219 | for(i=0;i<tam;i+=2){ |
brunofgc | 13:b9183b4bc049 | 220 | //fprintf(arquivo->fp,"%c",data[i]); |
brunofgc | 13:b9183b4bc049 | 221 | |
brunofgc | 13:b9183b4bc049 | 222 | hex[0]=data[i]; |
brunofgc | 13:b9183b4bc049 | 223 | hex[1]=data[i+1]; |
brunofgc | 13:b9183b4bc049 | 224 | c = hextobin(hex,2); |
brunofgc | 13:b9183b4bc049 | 225 | |
brunofgc | 13:b9183b4bc049 | 226 | fputc(c,arquivo->fp); |
brunofgc | 13:b9183b4bc049 | 227 | fflush(arquivo->fp); |
brunofgc | 13:b9183b4bc049 | 228 | } |
brunofgc | 13:b9183b4bc049 | 229 | sdCard::fechaArquivo(arquivo); |
brunofgc | 13:b9183b4bc049 | 230 | |
brunofgc | 13:b9183b4bc049 | 231 | return true; |
brunofgc | 13:b9183b4bc049 | 232 | } |
brunofgc | 13:b9183b4bc049 | 233 | |
brunofgc | 0:1c0a769988ee | 234 | bool sdCard::fechaArquivo(arquivoSD* arquivo){ |
brunofgc | 11:631bea162800 | 235 | //Struct do arquivo |
brunofgc | 0:1c0a769988ee | 236 | fclose(arquivo->fp); |
brunofgc | 0:1c0a769988ee | 237 | arquivo->aberto = false; |
brunofgc | 0:1c0a769988ee | 238 | return true; |
brunofgc | 0:1c0a769988ee | 239 | } |
brunofgc | 0:1c0a769988ee | 240 | |
brunofgc | 10:263c093f8977 | 241 | bool sdCard::excluiArquivo(arquivoSD *arquivo){ |
brunofgc | 0:1c0a769988ee | 242 | if(arquivo->aberto){ |
brunofgc | 11:631bea162800 | 243 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 244 | } |
brunofgc | 7:ae9c47f62946 | 245 | if(!remove(arquivo->nome)){return false;} |
brunofgc | 13:b9183b4bc049 | 246 | arquivo->bytes = 0; |
brunofgc | 7:ae9c47f62946 | 247 | return true; |
brunofgc | 0:1c0a769988ee | 248 | } |
brunofgc | 0:1c0a769988ee | 249 | |
brunofgc | 0:1c0a769988ee | 250 | bool sdCard::exibeArquivo(arquivoSD *arquivo){ |
brunofgc | 7:ae9c47f62946 | 251 | char c; |
brunofgc | 7:ae9c47f62946 | 252 | if(!sdCard::arquivoAberto(2000)){return false;} |
brunofgc | 0:1c0a769988ee | 253 | |
brunofgc | 0:1c0a769988ee | 254 | if(!sdCard::abreArquivo(arquivo,"r")){ |
brunofgc | 0:1c0a769988ee | 255 | return false; |
brunofgc | 7:ae9c47f62946 | 256 | } |
brunofgc | 0:1c0a769988ee | 257 | |
brunofgc | 0:1c0a769988ee | 258 | pc.printf("\n\nInicio do arquivo %s.\n\n",arquivo->nome); |
brunofgc | 0:1c0a769988ee | 259 | |
brunofgc | 0:1c0a769988ee | 260 | do{ |
brunofgc | 0:1c0a769988ee | 261 | c = fgetc(arquivo->fp); |
brunofgc | 0:1c0a769988ee | 262 | if(!feof(arquivo->fp)){pc.printf("%c",c);} |
brunofgc | 0:1c0a769988ee | 263 | }while(!feof(arquivo->fp)); |
brunofgc | 0:1c0a769988ee | 264 | pc.printf("\n\nFim do arquivo %s.\n",arquivo->nome); |
brunofgc | 0:1c0a769988ee | 265 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 266 | return true; |
brunofgc | 0:1c0a769988ee | 267 | } |
brunofgc | 0:1c0a769988ee | 268 | |
brunofgc | 0:1c0a769988ee | 269 | int sdCard::init(){ |
brunofgc | 0:1c0a769988ee | 270 | //Inicializando SDCard |
brunofgc | 0:1c0a769988ee | 271 | |
brunofgc | 10:263c093f8977 | 272 | strcpy(sdCard::bank0.nome,"/sd/RAD/bank0.txt"); |
brunofgc | 10:263c093f8977 | 273 | strcpy(sdCard::bank1.nome,"/sd/RAD/bank1.txt"); |
brunofgc | 10:263c093f8977 | 274 | strcpy(sdCard::currentBankFile.nome,"/sd/RAD/currentBank.txt"); |
brunofgc | 0:1c0a769988ee | 275 | strcpy(sdCard::config.nome,"/sd/RAD/config.bin"); |
brunofgc | 0:1c0a769988ee | 276 | strcpy(sdCard::devices.nome,"/sd/RAD/devices.cfg"); |
brunofgc | 0:1c0a769988ee | 277 | strcpy(sdCard::status.nome,"/sd/RAD/status.txt"); |
brunofgc | 10:263c093f8977 | 278 | strcpy(sdCard::tempFile.nome,"/sd/RAD/tempFile.bin"); |
brunofgc | 13:b9183b4bc049 | 279 | strcpy(sdCard::newFirmware.nome,"/sd/RAD/firmware.bin"); |
brunofgc | 13:b9183b4bc049 | 280 | |
brunofgc | 0:1c0a769988ee | 281 | |
brunofgc | 0:1c0a769988ee | 282 | if(!sdCard::abreArquivo(&sdCard::devices,"r")){ |
brunofgc | 0:1c0a769988ee | 283 | pc.printf("Remota nao parametrizada!!!\n"); |
brunofgc | 0:1c0a769988ee | 284 | }else{ |
brunofgc | 0:1c0a769988ee | 285 | fechaArquivo(&sdCard::devices); |
brunofgc | 10:263c093f8977 | 286 | } |
brunofgc | 11:631bea162800 | 287 | sdCard::verificaCurrentBank(); |
brunofgc | 0:1c0a769988ee | 288 | return 1; |
brunofgc | 0:1c0a769988ee | 289 | } |
brunofgc | 0:1c0a769988ee | 290 | |
brunofgc | 10:263c093f8977 | 291 | bool sdCard::deleteBanks(uint8_t bank){ |
brunofgc | 10:263c093f8977 | 292 | switch(bank){ |
brunofgc | 10:263c093f8977 | 293 | case 0: |
brunofgc | 10:263c093f8977 | 294 | sdCard::excluiArquivo(&sdCard::bank0); |
brunofgc | 11:631bea162800 | 295 | /*sdCard::abreArquivo(&sdCard::bank0,"w"); |
brunofgc | 11:631bea162800 | 296 | sdCard::fechaArquivo(&sdCard::bank0);*/ |
brunofgc | 10:263c093f8977 | 297 | break; |
brunofgc | 10:263c093f8977 | 298 | case 1: |
brunofgc | 10:263c093f8977 | 299 | sdCard::excluiArquivo(&sdCard::bank1); |
brunofgc | 11:631bea162800 | 300 | /*sdCard::abreArquivo(&sdCard::bank1,"w"); |
brunofgc | 11:631bea162800 | 301 | sdCard::fechaArquivo(&sdCard::bank1);*/ |
brunofgc | 10:263c093f8977 | 302 | break; |
brunofgc | 10:263c093f8977 | 303 | case 2: |
brunofgc | 10:263c093f8977 | 304 | sdCard::excluiArquivo(&sdCard::bank0); |
brunofgc | 11:631bea162800 | 305 | /*sdCard::abreArquivo(&sdCard::bank0,"w"); |
brunofgc | 11:631bea162800 | 306 | sdCard::fechaArquivo(&sdCard::bank0);*/ |
brunofgc | 10:263c093f8977 | 307 | sdCard::excluiArquivo(&sdCard::bank1); |
brunofgc | 11:631bea162800 | 308 | /*sdCard::abreArquivo(&sdCard::bank1,"w"); |
brunofgc | 11:631bea162800 | 309 | sdCard::fechaArquivo(&sdCard::bank1); */ |
brunofgc | 10:263c093f8977 | 310 | sdCard::excluiArquivo(&sdCard::currentBankFile); |
brunofgc | 10:263c093f8977 | 311 | if(sdCard::abreArquivo(&sdCard::currentBankFile,"w")){ |
brunofgc | 10:263c093f8977 | 312 | fputc('0',sdCard::currentBankFile.fp); |
brunofgc | 11:631bea162800 | 313 | fputc('0',sdCard::currentBankFile.fp); |
brunofgc | 10:263c093f8977 | 314 | fflush(sdCard::currentBankFile.fp); |
brunofgc | 10:263c093f8977 | 315 | sdCard::fechaArquivo(&sdCard::currentBankFile); |
brunofgc | 10:263c093f8977 | 316 | } |
brunofgc | 10:263c093f8977 | 317 | break; |
brunofgc | 10:263c093f8977 | 318 | } |
brunofgc | 10:263c093f8977 | 319 | return true; |
brunofgc | 10:263c093f8977 | 320 | } |
brunofgc | 10:263c093f8977 | 321 | |
brunofgc | 0:1c0a769988ee | 322 | int sdCard::preparaEnvio(void){ |
brunofgc | 7:ae9c47f62946 | 323 | //Verifico se existe arquivo pendente de envio. |
brunofgc | 10:263c093f8977 | 324 | //if(!sdCard::arquivoAberto(5000)){return 2;} |
brunofgc | 7:ae9c47f62946 | 325 | //Incluindo o status no envio |
brunofgc | 10:263c093f8977 | 326 | modemCom::writeStatusToSD(); |
brunofgc | 0:1c0a769988ee | 327 | return 1; |
brunofgc | 0:1c0a769988ee | 328 | } |
brunofgc | 0:1c0a769988ee | 329 | |
brunofgc | 0:1c0a769988ee | 330 | bool sdCard::getFileTam(arquivoSD *arquivo){ |
brunofgc | 0:1c0a769988ee | 331 | uint32_t tam=0; |
brunofgc | 7:ae9c47f62946 | 332 | uint8_t timeOut = timeOutArquivoAberto; |
brunofgc | 7:ae9c47f62946 | 333 | if(!sdCard::arquivoAberto(5000)){return false;} |
brunofgc | 0:1c0a769988ee | 334 | |
brunofgc | 0:1c0a769988ee | 335 | if(arquivo->aberto){ |
brunofgc | 0:1c0a769988ee | 336 | fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 337 | } |
brunofgc | 0:1c0a769988ee | 338 | |
brunofgc | 13:b9183b4bc049 | 339 | if(sdCard::abreArquivo(arquivo,"r")){ |
brunofgc | 2:55b7b466e742 | 340 | fseek(arquivo->fp, 0, SEEK_END); // seek to end of file |
brunofgc | 2:55b7b466e742 | 341 | tam = ftell(arquivo->fp); // get current file pointer |
brunofgc | 13:b9183b4bc049 | 342 | fseek(arquivo->fp, 0, SEEK_SET); // seek back to beginning of file |
brunofgc | 13:b9183b4bc049 | 343 | |
brunofgc | 13:b9183b4bc049 | 344 | /*while(fgetc(arquivo->fp)!=EOF){ |
brunofgc | 13:b9183b4bc049 | 345 | tam++; |
brunofgc | 13:b9183b4bc049 | 346 | }*/ |
brunofgc | 13:b9183b4bc049 | 347 | |
brunofgc | 0:1c0a769988ee | 348 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 349 | }else{ |
brunofgc | 0:1c0a769988ee | 350 | return false; |
brunofgc | 0:1c0a769988ee | 351 | } |
brunofgc | 0:1c0a769988ee | 352 | arquivo->bytes = tam; |
brunofgc | 0:1c0a769988ee | 353 | return true; |
brunofgc | 0:1c0a769988ee | 354 | } |
brunofgc | 0:1c0a769988ee | 355 | |
brunofgc | 7:ae9c47f62946 | 356 | bool sdCard::arquivoAberto(uint16_t timeOut){ |
brunofgc | 7:ae9c47f62946 | 357 | while( |
brunofgc | 10:263c093f8977 | 358 | (sdCard::bank0.aberto||sdCard::bank1.aberto||sdCard::currentBankFile.aberto||sdCard::config.aberto||sdCard::devices.aberto||sdCard::status.aberto||sdCard::tempFile.aberto||sdCard::arquivosAbertos) |
brunofgc | 7:ae9c47f62946 | 359 | && |
brunofgc | 7:ae9c47f62946 | 360 | timeOut){ |
brunofgc | 7:ae9c47f62946 | 361 | timeOut--; |
brunofgc | 7:ae9c47f62946 | 362 | if((timeOut%1000)==0){pc.printf("Timeout em arquivoAberto()<%lu>.\r\n",timeOut);} |
brunofgc | 7:ae9c47f62946 | 363 | osDelay(1); |
brunofgc | 7:ae9c47f62946 | 364 | } |
brunofgc | 7:ae9c47f62946 | 365 | if(timeOut){return true;} |
brunofgc | 7:ae9c47f62946 | 366 | else{return false;} |
brunofgc | 0:1c0a769988ee | 367 | } |
brunofgc | 0:1c0a769988ee | 368 | |
brunofgc | 0:1c0a769988ee | 369 | |
brunofgc | 9:cf406384efd9 | 370 | /*//*********************************************************** |
brunofgc | 0:1c0a769988ee | 371 | // file_rename: renames a file (via copy & delete). |
brunofgc | 0:1c0a769988ee | 372 | // Moves data instead of adjusting the file name in the |
brunofgc | 0:1c0a769988ee | 373 | // file directory. Checks to insure the file was renamed. |
brunofgc | 9:cf406384efd9 | 374 | // Returns 0 = error; 1 = success |
brunofgc | 0:1c0a769988ee | 375 | //*********************************************************** |
brunofgc | 7:ae9c47f62946 | 376 | uint8_t sdCard::file_rename(const char *oldfname, const char *newfname) { |
brunofgc | 7:ae9c47f62946 | 377 | //rename(oldfname,newfname); |
brunofgc | 0:1c0a769988ee | 378 | int retval = 0; |
brunofgc | 0:1c0a769988ee | 379 | int ch; |
brunofgc | 7:ae9c47f62946 | 380 | sdCard::envio.bytes=0; |
brunofgc | 7:ae9c47f62946 | 381 | |
brunofgc | 7:ae9c47f62946 | 382 | if(!sdCard::arquivoAberto(30000)){return false;} |
brunofgc | 7:ae9c47f62946 | 383 | |
brunofgc | 7:ae9c47f62946 | 384 | |
brunofgc | 0:1c0a769988ee | 385 | FILE *fpold = fopen(oldfname, "r"); // src file |
brunofgc | 7:ae9c47f62946 | 386 | |
brunofgc | 0:1c0a769988ee | 387 | if(fpold == NULL){ |
brunofgc | 7:ae9c47f62946 | 388 | pc.printf("file_rename 4.\r\n"); |
brunofgc | 7:ae9c47f62946 | 389 | fclose(fpold); |
brunofgc | 0:1c0a769988ee | 390 | return 0; |
brunofgc | 0:1c0a769988ee | 391 | } |
brunofgc | 0:1c0a769988ee | 392 | |
brunofgc | 0:1c0a769988ee | 393 | FILE *fpnew = fopen(newfname, "w"); // dest file |
brunofgc | 7:ae9c47f62946 | 394 | |
brunofgc | 7:ae9c47f62946 | 395 | if(fpnew == NULL){ |
brunofgc | 7:ae9c47f62946 | 396 | |
brunofgc | 7:ae9c47f62946 | 397 | fclose(fpnew); |
brunofgc | 0:1c0a769988ee | 398 | return 0; |
brunofgc | 0:1c0a769988ee | 399 | } |
brunofgc | 0:1c0a769988ee | 400 | |
brunofgc | 0:1c0a769988ee | 401 | pc.printf("Iniciando copia.\n"); |
brunofgc | 0:1c0a769988ee | 402 | |
brunofgc | 0:1c0a769988ee | 403 | sdCard::envio.bytes=0; |
brunofgc | 0:1c0a769988ee | 404 | ch = fgetc(fpold); |
brunofgc | 0:1c0a769988ee | 405 | while(ch != EOF){ |
brunofgc | 0:1c0a769988ee | 406 | fputc(ch, fpnew); |
brunofgc | 0:1c0a769988ee | 407 | ch = fgetc(fpold); |
brunofgc | 0:1c0a769988ee | 408 | sdCard::envio.bytes++; |
brunofgc | 0:1c0a769988ee | 409 | } |
brunofgc | 0:1c0a769988ee | 410 | |
brunofgc | 0:1c0a769988ee | 411 | fclose(fpnew); |
brunofgc | 0:1c0a769988ee | 412 | fclose(fpold); |
brunofgc | 0:1c0a769988ee | 413 | |
brunofgc | 0:1c0a769988ee | 414 | fpnew = fopen(newfname, "r"); // Reopen dest to insure |
brunofgc | 0:1c0a769988ee | 415 | if(fpnew == NULL) { // that it was created. |
brunofgc | 0:1c0a769988ee | 416 | retval = (0); // Return Error. |
brunofgc | 0:1c0a769988ee | 417 | } |
brunofgc | 0:1c0a769988ee | 418 | else { |
brunofgc | 0:1c0a769988ee | 419 | fclose(fpnew); |
brunofgc | 0:1c0a769988ee | 420 | remove(oldfname); // Remove original file. |
brunofgc | 0:1c0a769988ee | 421 | retval = (1); // Return Success. |
brunofgc | 0:1c0a769988ee | 422 | } |
brunofgc | 7:ae9c47f62946 | 423 | sdCard::arquivosAbertos=false; |
brunofgc | 7:ae9c47f62946 | 424 | pc.printf("file_rename retval <%lu>.\r\n",retval); |
brunofgc | 0:1c0a769988ee | 425 | return (retval); |
brunofgc | 9:cf406384efd9 | 426 | }*/ |
brunofgc | 9:cf406384efd9 | 427 | |
brunofgc | 10:263c093f8977 | 428 | |
brunofgc | 10:263c093f8977 | 429 | |
brunofgc | 9:cf406384efd9 | 430 | //*********************************************************** |
brunofgc | 9:cf406384efd9 | 431 | // file_rename: renames a file (via copy & delete). |
brunofgc | 9:cf406384efd9 | 432 | // Moves data instead of adjusting the file name in the |
brunofgc | 9:cf406384efd9 | 433 | // file directory. Checks to insure the file was renamed. |
brunofgc | 9:cf406384efd9 | 434 | // Returns 0 = error; 1 = success |
brunofgc | 9:cf406384efd9 | 435 | //*********************************************************** |
brunofgc | 10:263c093f8977 | 436 | |
brunofgc | 10:263c093f8977 | 437 | |
brunofgc | 10:263c093f8977 | 438 | |
brunofgc | 9:cf406384efd9 | 439 | uint8_t sdCard::file_rename(const char *oldfname, const char *newfname) { |
brunofgc | 9:cf406384efd9 | 440 | //rename(oldfname,newfname); |
brunofgc | 9:cf406384efd9 | 441 | #define maxTamFileCopy 128 |
brunofgc | 9:cf406384efd9 | 442 | char bufCopy[maxTamFileCopy]; |
brunofgc | 9:cf406384efd9 | 443 | uint8_t retval = 0; |
brunofgc | 9:cf406384efd9 | 444 | uint32_t fileSize = 0; |
brunofgc | 9:cf406384efd9 | 445 | uint32_t fileNumParts = 0; |
brunofgc | 9:cf406384efd9 | 446 | uint32_t fileEnding=0; |
brunofgc | 9:cf406384efd9 | 447 | char ch; |
brunofgc | 10:263c093f8977 | 448 | uint32_t i,j; |
brunofgc | 10:263c093f8977 | 449 | if(!sdCard::arquivoAberto(30000)){return false;} |
brunofgc | 9:cf406384efd9 | 450 | FILE *fpold = fopen(oldfname, "r"); // src file |
brunofgc | 9:cf406384efd9 | 451 | |
brunofgc | 9:cf406384efd9 | 452 | if(fpold == NULL){ |
brunofgc | 9:cf406384efd9 | 453 | fclose(fpold); |
brunofgc | 9:cf406384efd9 | 454 | return 0; |
brunofgc | 9:cf406384efd9 | 455 | } |
brunofgc | 9:cf406384efd9 | 456 | |
brunofgc | 9:cf406384efd9 | 457 | sdCard::arquivosAbertos = true; |
brunofgc | 9:cf406384efd9 | 458 | |
brunofgc | 9:cf406384efd9 | 459 | FILE *fpnew = fopen(newfname, "w"); // dest file |
brunofgc | 9:cf406384efd9 | 460 | |
brunofgc | 9:cf406384efd9 | 461 | if(fpnew == NULL){ |
brunofgc | 9:cf406384efd9 | 462 | fclose(fpnew); |
brunofgc | 9:cf406384efd9 | 463 | return 0; |
brunofgc | 9:cf406384efd9 | 464 | sdCard::arquivosAbertos = false; |
brunofgc | 9:cf406384efd9 | 465 | } |
brunofgc | 9:cf406384efd9 | 466 | |
brunofgc | 9:cf406384efd9 | 467 | |
brunofgc | 9:cf406384efd9 | 468 | pc.printf("Iniciando copia.\n"); |
brunofgc | 9:cf406384efd9 | 469 | |
brunofgc | 9:cf406384efd9 | 470 | |
brunofgc | 9:cf406384efd9 | 471 | fseek(fpold, 0, SEEK_END); // seek to end of file |
brunofgc | 9:cf406384efd9 | 472 | fileSize = ftell(fpold); // get current file pointer |
brunofgc | 10:263c093f8977 | 473 | fseek(fpold, 0, SEEK_SET); // seek back to beginning of file |
brunofgc | 9:cf406384efd9 | 474 | |
brunofgc | 9:cf406384efd9 | 475 | |
brunofgc | 9:cf406384efd9 | 476 | fileNumParts = fileSize/maxTamFileCopy; |
brunofgc | 9:cf406384efd9 | 477 | fileEnding = fileSize%maxTamFileCopy; |
brunofgc | 9:cf406384efd9 | 478 | for(i=0;i<fileNumParts;i++){ |
brunofgc | 9:cf406384efd9 | 479 | for(j=0;j<maxTamFileCopy;j++){ |
brunofgc | 9:cf406384efd9 | 480 | bufCopy[j]=fgetc(fpold); |
brunofgc | 9:cf406384efd9 | 481 | } |
brunofgc | 9:cf406384efd9 | 482 | for(j=0;j<maxTamFileCopy;j++){ |
brunofgc | 9:cf406384efd9 | 483 | fputc(bufCopy[j],fpnew); |
brunofgc | 9:cf406384efd9 | 484 | } |
brunofgc | 9:cf406384efd9 | 485 | } |
brunofgc | 9:cf406384efd9 | 486 | for(j=0;j<fileEnding;j++){ |
brunofgc | 9:cf406384efd9 | 487 | bufCopy[j]=fgetc(fpold); |
brunofgc | 9:cf406384efd9 | 488 | } |
brunofgc | 9:cf406384efd9 | 489 | for(j=0;j<fileEnding;j++){ |
brunofgc | 9:cf406384efd9 | 490 | fputc(bufCopy[j],fpnew); |
brunofgc | 9:cf406384efd9 | 491 | } |
brunofgc | 9:cf406384efd9 | 492 | fclose(fpnew); |
brunofgc | 9:cf406384efd9 | 493 | fclose(fpold); |
brunofgc | 9:cf406384efd9 | 494 | |
brunofgc | 9:cf406384efd9 | 495 | fpnew = fopen(newfname, "r"); // Reopen dest to insure |
brunofgc | 9:cf406384efd9 | 496 | if(fpnew == NULL) { // that it was created. |
brunofgc | 9:cf406384efd9 | 497 | retval = (0); // Return Error. |
brunofgc | 9:cf406384efd9 | 498 | } |
brunofgc | 9:cf406384efd9 | 499 | else { |
brunofgc | 9:cf406384efd9 | 500 | fclose(fpnew); |
brunofgc | 9:cf406384efd9 | 501 | remove(oldfname); // Remove original file. |
brunofgc | 9:cf406384efd9 | 502 | retval = (1); // Return Success. |
brunofgc | 9:cf406384efd9 | 503 | } |
brunofgc | 9:cf406384efd9 | 504 | sdCard::arquivosAbertos=false; |
brunofgc | 9:cf406384efd9 | 505 | pc.printf("file_rename retval <%lu>.\r\n",retval); |
brunofgc | 9:cf406384efd9 | 506 | return (retval); |
brunofgc | 0:1c0a769988ee | 507 | } |