teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Fri Jun 08 22:14:21 2018 +0000
Revision:
38:07d3907b74e5
Parent:
37:0e95c85f0160
teste de publish para compilar no mbed-cli

Who changed what in which revision?

UserRevisionLine numberNew contents of line
brunofgc 0:1c0a769988ee 1 #include "mbed.h" //API mBed com diversos drivers para os periféricos LPC1768
brunofgc 0:1c0a769988ee 2 #include "stdio.h"
brunofgc 0:1c0a769988ee 3 #include "cmsis_os.h" //Nucleo do Real Time Operational System
brunofgc 0:1c0a769988ee 4 #include "main.h" //Inicializações de maquina e demais ajustes de hardware
brunofgc 0:1c0a769988ee 5 #include "funcoesSDCard.h" //Funções para manipulação do SDCard que incluem rotinas de inicialização, setar pastas e arquivos e demais usos
brunofgc 0:1c0a769988ee 6 #include "RTC.h" //API de uso das interrupções do Real Time Clock
brunofgc 0:1c0a769988ee 7 #include "serialPC.h" //Funções de comunicação entre RAD e PC via usb
brunofgc 0:1c0a769988ee 8 #include "modbusMaster1.h"
brunofgc 13:b9183b4bc049 9 //#include "lpc17xx_nvic.h"
brunofgc 13:b9183b4bc049 10
brunofgc 0:1c0a769988ee 11
brunofgc 7:ae9c47f62946 12 //Definições de Hardware acessíveis em qualquer modulo
brunofgc 7:ae9c47f62946 13 /*LEDs de informação visual*/
brunofgc 7:ae9c47f62946 14 DigitalOut ledLigado(P1_0);
brunofgc 7:ae9c47f62946 15 DigitalOut ledEmComunicacao(P1_1);
brunofgc 7:ae9c47f62946 16 DigitalOut ledConectadoInternet(P1_4);
brunofgc 7:ae9c47f62946 17 DigitalOut ledUsoGeral(P1_8);
brunofgc 7:ae9c47f62946 18 /*LEDs de informação visual*/
brunofgc 7:ae9c47f62946 19
brunofgc 7:ae9c47f62946 20 /*Pinos de controle do modulo WiFi*/
brunofgc 0:1c0a769988ee 21 DigitalOut ESP_CH_PD(P0_6);
brunofgc 0:1c0a769988ee 22 DigitalOut ESP_RESET(P0_0);
brunofgc 0:1c0a769988ee 23 DigitalOut ESP_ESPGPIO_2(P0_1);
brunofgc 7:ae9c47f62946 24 /*Pinos de controle do modulo WiFi*/
brunofgc 0:1c0a769988ee 25
brunofgc 7:ae9c47f62946 26 /*Interrupções externas para contagem de pulsos rapidos*/
brunofgc 6:d4ebbaaba295 27 InterruptIn eInt1(P2_11);
brunofgc 6:d4ebbaaba295 28 InterruptIn eInt2(P2_12);
brunofgc 6:d4ebbaaba295 29 InterruptIn eInt3(P2_13);
brunofgc 7:ae9c47f62946 30 /*Interrupções externas para contagem de pulsos rapidos*/
brunofgc 6:d4ebbaaba295 31
brunofgc 21:b9315cdd9275 32 /*Leituras analógicas*/
brunofgc 21:b9315cdd9275 33 /*AnalogIn ain1(p17);
brunofgc 21:b9315cdd9275 34 AnalogIn ain2(p18);
brunofgc 21:b9315cdd9275 35 AnalogIn ain3(p19);
brunofgc 21:b9315cdd9275 36 AnalogIn ain4(p20);*/
brunofgc 21:b9315cdd9275 37 /*Leituras analógicas*/
brunofgc 21:b9315cdd9275 38
brunofgc 7:ae9c47f62946 39 //Função para reset do ARM
brunofgc 0:1c0a769988ee 40 extern "C" void mbed_reset();
brunofgc 0:1c0a769988ee 41
brunofgc 30:8a06a85d8807 42 //Devices
brunofgc 0:1c0a769988ee 43 uint8_t devices=0;
brunofgc 30:8a06a85d8807 44 uint16_t alarmes=0;
brunofgc 30:8a06a85d8807 45 uint16_t leituras=0;
brunofgc 30:8a06a85d8807 46 uint16_t qtdSchedules=0;
brunofgc 30:8a06a85d8807 47 uint16_t qtdScheduleExceptions=0;
brunofgc 0:1c0a769988ee 48 //Devices
brunofgc 0:1c0a769988ee 49
brunofgc 0:1c0a769988ee 50 //Variáveis para envio de arquivo em qualquer lugar do código
brunofgc 0:1c0a769988ee 51 extern arquivoSD *arquivoEnvioPointer = NULL;
brunofgc 0:1c0a769988ee 52
brunofgc 7:ae9c47f62946 53 uint32_t act;
brunofgc 7:ae9c47f62946 54 bool boolExecAct;
brunofgc 7:ae9c47f62946 55 bool inicializaModemBool;
brunofgc 37:0e95c85f0160 56 bool debug = false;
brunofgc 37:0e95c85f0160 57 bool xeretaModem = false;
brunofgc 0:1c0a769988ee 58 bool hardwareReset = false;
brunofgc 30:8a06a85d8807 59 char firmVersion[20];
brunofgc 33:a60abda630f7 60 char resetCauses[30];
brunofgc 0:1c0a769988ee 61
brunofgc 0:1c0a769988ee 62
brunofgc 0:1c0a769988ee 63 MediaMovel FiltroAI4(5);
brunofgc 0:1c0a769988ee 64 MediaMovel FiltroAI3(5);
brunofgc 0:1c0a769988ee 65 MediaMovel FiltroAI2(5);
brunofgc 0:1c0a769988ee 66 MediaMovel FiltroAI1(5);
brunofgc 0:1c0a769988ee 67
brunofgc 0:1c0a769988ee 68 uint16_t aiFiltrada[4];
brunofgc 0:1c0a769988ee 69 bool entradasDigitais[9];
brunofgc 0:1c0a769988ee 70
brunofgc 37:0e95c85f0160 71 CircularBuffer bufModem(maxBufInModem,0);// __attribute__ ((section("AHBSRAM0")));
brunofgc 37:0e95c85f0160 72 CircularBuffer bufPC(128,0);
brunofgc 37:0e95c85f0160 73 CircularBuffer sdCardBuf(1024,0);
brunofgc 0:1c0a769988ee 74
brunofgc 0:1c0a769988ee 75 uint8_t enviaDadosPorAlarme = 255;
brunofgc 2:55b7b466e742 76 //bool reportaMudancaAoServer;
brunofgc 0:1c0a769988ee 77 bool executaComandoServer;
brunofgc 7:ae9c47f62946 78
brunofgc 0:1c0a769988ee 79 //typeConfig flashConfig;
brunofgc 0:1c0a769988ee 80
brunofgc 7:ae9c47f62946 81 //Instancia das portas Seriais do ARM
brunofgc 0:1c0a769988ee 82 Serial pc(USBTX, USBRX); //Instancia de recurso Serial uart pc
brunofgc 7:ae9c47f62946 83 Serial modem(P0_10,P0_11); //Instancia de recurso Serial uart Modem
brunofgc 0:1c0a769988ee 84 DigitalOut max_de(P2_7); //Pino de habilitação max485 para porta Energy Metering ModBus
brunofgc 0:1c0a769988ee 85 Serial modBusMaster1Serial(P2_0,P2_1); //Recurso serial para porta Energy Metering ModBus
brunofgc 0:1c0a769988ee 86
brunofgc 0:1c0a769988ee 87 /*
brunofgc 0:1c0a769988ee 88 //Interrupções externas e botões
brunofgc 0:1c0a769988ee 89 InterruptIn int_wh(P2_11);
brunofgc 0:1c0a769988ee 90 InterruptIn int_varh(P2_12);
brunofgc 0:1c0a769988ee 91
brunofgc 0:1c0a769988ee 92 void whCount(void){
brunofgc 0:1c0a769988ee 93 whled=1;
brunofgc 0:1c0a769988ee 94 wait(0.2);
brunofgc 0:1c0a769988ee 95 whled=0;
brunofgc 0:1c0a769988ee 96 }
brunofgc 0:1c0a769988ee 97
brunofgc 0:1c0a769988ee 98 void varhCount(void){
brunofgc 0:1c0a769988ee 99 varhled=1;
brunofgc 0:1c0a769988ee 100 wait(0.2);
brunofgc 0:1c0a769988ee 101 varhled=0;
brunofgc 0:1c0a769988ee 102 }*/
brunofgc 0:1c0a769988ee 103 //Definições de Hardware acessíveis em qualquer modulo
brunofgc 0:1c0a769988ee 104
brunofgc 0:1c0a769988ee 105 /*
brunofgc 0:1c0a769988ee 106 void loadStandardConfig(){
brunofgc 0:1c0a769988ee 107 strcpy(flashConfig.apnList[0],"claro.com.br");
brunofgc 0:1c0a769988ee 108 strcpy(flashConfig.apnList[1],"zap.vivo.com.br");
brunofgc 0:1c0a769988ee 109 strcpy(flashConfig.apnList[2],"tim.br");
brunofgc 0:1c0a769988ee 110 strcpy(flashConfig.apnList[3],"generica.oi.com.br");
brunofgc 0:1c0a769988ee 111
brunofgc 0:1c0a769988ee 112 strcpy(flashConfig.login[0],"claro");
brunofgc 0:1c0a769988ee 113 strcpy(flashConfig.login[1],"vivo");
brunofgc 0:1c0a769988ee 114 strcpy(flashConfig.login[2],"tim");
brunofgc 0:1c0a769988ee 115 strcpy(flashConfig.login[3],"oi");
brunofgc 0:1c0a769988ee 116
brunofgc 0:1c0a769988ee 117 strcpy(flashConfig.senha[0],"claro");
brunofgc 0:1c0a769988ee 118 strcpy(flashConfig.senha[1],"vivo");
brunofgc 0:1c0a769988ee 119 strcpy(flashConfig.senha[2],"tim");
brunofgc 0:1c0a769988ee 120 strcpy(flashConfig.senha[3],"oi");
brunofgc 0:1c0a769988ee 121
brunofgc 2:55b7b466e742 122 strcpy(flashConfig.serverAddress,serverAddress);
brunofgc 0:1c0a769988ee 123
brunofgc 0:1c0a769988ee 124 strcpy(flashConfig.serverPort,"80");
brunofgc 0:1c0a769988ee 125
brunofgc 0:1c0a769988ee 126 flashConfig.periodoDeEnvioDeDadosMinutos=15; //Padrão 15 minutos
brunofgc 0:1c0a769988ee 127 flashConfig.ultimoEstadoLigadoDesligado=0; //Padrão de fábrica desligado
brunofgc 0:1c0a769988ee 128 }
brunofgc 0:1c0a769988ee 129 */
brunofgc 0:1c0a769988ee 130
brunofgc 0:1c0a769988ee 131 void leiturasDigitais(){
brunofgc 0:1c0a769988ee 132 entradasDigitais[0] = ED1;
brunofgc 0:1c0a769988ee 133 entradasDigitais[1] = ED2;
brunofgc 0:1c0a769988ee 134 entradasDigitais[2] = ED3;
brunofgc 0:1c0a769988ee 135 entradasDigitais[3] = ED4;
brunofgc 0:1c0a769988ee 136 entradasDigitais[4] = ED5;
brunofgc 0:1c0a769988ee 137 entradasDigitais[5] = ED6;
brunofgc 0:1c0a769988ee 138 entradasDigitais[6] = ED7;
brunofgc 0:1c0a769988ee 139 entradasDigitais[7] = ED8;
brunofgc 0:1c0a769988ee 140 entradasDigitais[8] = ED9;
brunofgc 0:1c0a769988ee 141 }
brunofgc 0:1c0a769988ee 142
brunofgc 21:b9315cdd9275 143 HighSpeedAnalogIn ain(p17, p18, p19, p20);
brunofgc 0:1c0a769988ee 144
brunofgc 0:1c0a769988ee 145 void leiturasAnalogicas(){
brunofgc 21:b9315cdd9275 146 static uint8_t leituraAtual;
brunofgc 21:b9315cdd9275 147 switch(leituraAtual){
brunofgc 21:b9315cdd9275 148 case 0:
brunofgc 21:b9315cdd9275 149 aiFiltrada[0] = FiltroAI1.get(ain.read_u16(p17));
brunofgc 21:b9315cdd9275 150 break;
brunofgc 21:b9315cdd9275 151 case 1:
brunofgc 21:b9315cdd9275 152 aiFiltrada[1] = FiltroAI2.get(ain.read_u16(p18));
brunofgc 21:b9315cdd9275 153 break;
brunofgc 21:b9315cdd9275 154 case 2:
brunofgc 21:b9315cdd9275 155 aiFiltrada[2] = FiltroAI3.get(ain.read_u16(p19));
brunofgc 21:b9315cdd9275 156 break;
brunofgc 21:b9315cdd9275 157 case 3:
brunofgc 21:b9315cdd9275 158 aiFiltrada[3] = FiltroAI4.get(ain.read_u16(p20));
brunofgc 21:b9315cdd9275 159 break;
brunofgc 0:1c0a769988ee 160 }
brunofgc 21:b9315cdd9275 161 if(leituraAtual<3){leituraAtual++;}
brunofgc 21:b9315cdd9275 162 else{leituraAtual=0;}
brunofgc 0:1c0a769988ee 163 }
brunofgc 0:1c0a769988ee 164
brunofgc 21:b9315cdd9275 165 void verificaSaidasDigitais(){
brunofgc 21:b9315cdd9275 166 SD_OE_R = 1;
brunofgc 21:b9315cdd9275 167 /*if(SD_FAULT){
brunofgc 30:8a06a85d8807 168 if(debug)pc.printf("Fault\r\n");
brunofgc 21:b9315cdd9275 169 }else{
brunofgc 30:8a06a85d8807 170 if(debug)pc.printf("No Fault\r\n");
brunofgc 21:b9315cdd9275 171 }*/
brunofgc 21:b9315cdd9275 172 }
brunofgc 4:13ff9c81dc10 173
brunofgc 0:1c0a769988ee 174 //Thread timers é utilizada para dar start nos timers de dentro das ISRs de comunicação serial porque não é permitido inicializar osTimers de dentro de ISRs.
brunofgc 0:1c0a769988ee 175 osThreadId idThreadTimers;
brunofgc 0:1c0a769988ee 176 void threadTimers(void const *args){
brunofgc 0:1c0a769988ee 177 while(true){
brunofgc 30:8a06a85d8807 178 osSignalWait(0x1,1000); //Executo compulsoriamente mesmo que não haja sinal num periodo de 1 segundo
brunofgc 30:8a06a85d8807 179 //if(enviaDadosPorAlarme==255){//debug
brunofgc 30:8a06a85d8807 180 if(serialPC::startTimer_pacote){
brunofgc 30:8a06a85d8807 181 serialPC::startTimer_pacote = false;
brunofgc 30:8a06a85d8807 182 osTimerStart(serialPC::timer_pacote,100);
brunofgc 30:8a06a85d8807 183 }
brunofgc 30:8a06a85d8807 184 if(serialModem::startTimer_pacote){
brunofgc 30:8a06a85d8807 185 serialModem::startTimer_pacote = false;
brunofgc 30:8a06a85d8807 186 osTimerStart(serialModem::timer_pacote,modemCom::timeOutModem);
brunofgc 30:8a06a85d8807 187 }
brunofgc 30:8a06a85d8807 188 //}
brunofgc 0:1c0a769988ee 189 if(modBusMaster1::startThreadModBusMaster){
brunofgc 0:1c0a769988ee 190 modBusMaster1::startThreadModBusMaster = false;
brunofgc 0:1c0a769988ee 191 osTimerStart(modBusMaster1::timer_pacote,4);
brunofgc 4:13ff9c81dc10 192 }
brunofgc 0:1c0a769988ee 193 }
brunofgc 0:1c0a769988ee 194 }
brunofgc 9:cf406384efd9 195 osThreadDef(threadTimers, osPriorityNormal, 128);
brunofgc 29:823a9da3696b 196
brunofgc 0:1c0a769988ee 197
brunofgc 0:1c0a769988ee 198 osThreadId idThreadProcessaPacote;
brunofgc 0:1c0a769988ee 199 void threadProcessaPacote(void const *args){
brunofgc 0:1c0a769988ee 200 while(true){
brunofgc 29:823a9da3696b 201 osSignalWait(0x1,100); //Executo compulsoriamente mesmo que não haja sinal num periodo de 1 segundo
brunofgc 4:13ff9c81dc10 202 if(serialModem::processarPacote){
brunofgc 4:13ff9c81dc10 203 serialModem::processarPacote = false;
brunofgc 4:13ff9c81dc10 204 modemCom::processaPacote(NULL);
brunofgc 21:b9315cdd9275 205 }
brunofgc 4:13ff9c81dc10 206 }
brunofgc 0:1c0a769988ee 207 }
brunofgc 23:f3c87afb6548 208 osThreadDef(threadProcessaPacote, osPriorityNormal, DEFAULT_STACK_SIZE);
brunofgc 0:1c0a769988ee 209
brunofgc 0:1c0a769988ee 210
brunofgc 7:ae9c47f62946 211
brunofgc 2:55b7b466e742 212 //Thread que gerencia as comunicacoes (DROME - SERVER)
brunofgc 0:1c0a769988ee 213 osThreadId idThreadComunicacoes;
brunofgc 0:1c0a769988ee 214 void threadComunicacoes(void const *args){
brunofgc 0:1c0a769988ee 215 //Esta thread faz todo o tratamento de comunicação.
brunofgc 0:1c0a769988ee 216
brunofgc 7:ae9c47f62946 217 //char aux[30];
brunofgc 7:ae9c47f62946 218 while(true){
brunofgc 29:823a9da3696b 219 osSignalWait(0x1,1000); //Libero processamento e executo compulsoriamente mesmo que não haja sinal num periodo de um segundo
brunofgc 29:823a9da3696b 220 if(inicializaModemBool){
brunofgc 29:823a9da3696b 221 inicializaModemBool = false;
brunofgc 29:823a9da3696b 222 modemCom::conectaWiFi();
brunofgc 29:823a9da3696b 223 }
brunofgc 37:0e95c85f0160 224
brunofgc 29:823a9da3696b 225 if(eventosRTC::rotinaEnvioDeDados){
brunofgc 30:8a06a85d8807 226 //Faço aqui o que for devido.
brunofgc 30:8a06a85d8807 227 if((!modemCom::status.recebendoArquivoDoServer)&&(enviaDadosPorAlarme==255)){
brunofgc 33:a60abda630f7 228 if(!modemCom::timeOutEnvioDados){
brunofgc 33:a60abda630f7 229 modemCom::timeOutEnvioDados=maxTimeEnvioDados;
brunofgc 33:a60abda630f7 230 }
brunofgc 30:8a06a85d8807 231 if(debug){pc.printf("Enviando dados de hora completa ao server.\n");}
brunofgc 30:8a06a85d8807 232 modemCom::status.emComunicacao = true;
brunofgc 29:823a9da3696b 233 eventosRTC::rotinaEnvioDeDados--;
brunofgc 29:823a9da3696b 234 modemCom::status.periodo = 900;
brunofgc 35:11a51a36c494 235 diversos::wdt.kick(180);
brunofgc 29:823a9da3696b 236 if(modemCom::verificaConexao()){
brunofgc 33:a60abda630f7 237 //Estou conectado
brunofgc 29:823a9da3696b 238 if(modemCom::enviaDados()){
brunofgc 29:823a9da3696b 239 eventosRTC::rotinaEnvioDeDados=0;
brunofgc 33:a60abda630f7 240 modemCom::timeOutEnvioDados=0;
brunofgc 29:823a9da3696b 241 modemCom::status.periodo = modemCom::status.periodoConfiguracao; //Restaurando o período configurado no sistema
brunofgc 25:a6da63ed025b 242 }
brunofgc 29:823a9da3696b 243 }
brunofgc 30:8a06a85d8807 244 modemCom::status.emComunicacao = false;
brunofgc 25:a6da63ed025b 245 }
brunofgc 30:8a06a85d8807 246
brunofgc 29:823a9da3696b 247 }
brunofgc 29:823a9da3696b 248
brunofgc 29:823a9da3696b 249 if(arquivoEnvioPointer!=NULL){
brunofgc 30:8a06a85d8807 250 if(debug){pc.printf("Enviando o arquivo <%s> ao server.\n",arquivoEnvioPointer->nome);}
brunofgc 29:823a9da3696b 251 if(modemCom::postFileCommandMode(modemCom::status.serverIP,modemCom::status.host,"/drome/parser/index.php",arquivoEnvioPointer)){
brunofgc 29:823a9da3696b 252 //if(modemCom::postFileCommandMode(modemCom::status.serverIP,modemCom::status.host,"/sistema/Parser/index.php",arquivoEnvioPointer)){
brunofgc 30:8a06a85d8807 253 if(debug){pc.printf("Enviado o arquivo <%s>.\n",arquivoEnvioPointer->nome);}
brunofgc 29:823a9da3696b 254 arquivoEnvioPointer = NULL;
brunofgc 29:823a9da3696b 255 }else{
brunofgc 30:8a06a85d8807 256 if(debug){pc.printf("Erro enviando o arquivo <%s>.\n",arquivoEnvioPointer->nome);}
brunofgc 29:823a9da3696b 257 }
brunofgc 7:ae9c47f62946 258 }
brunofgc 29:823a9da3696b 259
brunofgc 29:823a9da3696b 260 if(modemCom::status.SRINGsockEntrante){
brunofgc 29:823a9da3696b 261 modemCom::status.SRINGsockEntrante=false;
brunofgc 29:823a9da3696b 262 modemCom::atendeSRING(modemCom::status.connIDServerCommand);
brunofgc 29:823a9da3696b 263 }
brunofgc 29:823a9da3696b 264 modemCom::timeOutModem = 30;
brunofgc 29:823a9da3696b 265 }
brunofgc 0:1c0a769988ee 266 }
brunofgc 29:823a9da3696b 267 //osThreadDef(threadComunicacoes, osPriorityNormal, 3072);
brunofgc 29:823a9da3696b 268 osThreadDef(threadComunicacoes, osPriorityNormal, (DEFAULT_STACK_SIZE*2));
brunofgc 0:1c0a769988ee 269
brunofgc 0:1c0a769988ee 270 void RTC_POR_SEGUNDO(void){
brunofgc 0:1c0a769988ee 271 time_t seconds;
brunofgc 0:1c0a769988ee 272 seconds = time(NULL);
brunofgc 29:823a9da3696b 273 //osSignalSet(idThreadComunicacoes, 0x1);
brunofgc 0:1c0a769988ee 274 eventosRTC::rotina1segundo=true;
brunofgc 0:1c0a769988ee 275
brunofgc 7:ae9c47f62946 276 if(((seconds+5)%10)==0){
brunofgc 19:5559f7570e6f 277 eventosRTC::rotina10Segundos = true;
brunofgc 0:1c0a769988ee 278 }
brunofgc 0:1c0a769988ee 279
brunofgc 0:1c0a769988ee 280 if((seconds%60)==0){
brunofgc 0:1c0a769988ee 281 eventosRTC::minutos = true;
brunofgc 0:1c0a769988ee 282 }
brunofgc 0:1c0a769988ee 283
brunofgc 33:a60abda630f7 284 if(((seconds-15)%modemCom::status.periodo)==0){
brunofgc 9:cf406384efd9 285 //if((seconds%300)==0){
brunofgc 21:b9315cdd9275 286 eventosRTC::rotinaEnvioDeDados = maxTentativasEnvioDados;
brunofgc 29:823a9da3696b 287 osSignalSet(idThreadComunicacoes, 0x1);
brunofgc 29:823a9da3696b 288 }
brunofgc 0:1c0a769988ee 289 }
brunofgc 0:1c0a769988ee 290
brunofgc 0:1c0a769988ee 291 void loadDevices(){
brunofgc 0:1c0a769988ee 292 if(sdCard::abreArquivo(&sdCard::devices,"r")){
brunofgc 0:1c0a769988ee 293 criaDevices(sdCard::devices.fp);
brunofgc 0:1c0a769988ee 294 if(devices){
brunofgc 0:1c0a769988ee 295 configuraDevices(sdCard::devices.fp);
brunofgc 2:55b7b466e742 296 printf("Dispositivos configurados.\n");
brunofgc 0:1c0a769988ee 297 }
brunofgc 21:b9315cdd9275 298 sdCard::fechaArquivo(&sdCard::devices);
brunofgc 2:55b7b466e742 299 if(devices){testaTudoDevices();}
brunofgc 0:1c0a769988ee 300 }
brunofgc 0:1c0a769988ee 301 }
brunofgc 0:1c0a769988ee 302
brunofgc 0:1c0a769988ee 303 void inicializaMaquina(){
brunofgc 38:07d3907b74e5 304 //char aux[15];
brunofgc 36:fd239680e427 305 time_t seconds;
brunofgc 36:fd239680e427 306 seconds = time(NULL);
brunofgc 33:a60abda630f7 307 //Resetei pq?
brunofgc 36:fd239680e427 308 sprintf(resetCauses,"TSOn:%lu",seconds);
brunofgc 33:a60abda630f7 309 if((LPC_WDT->WDMOD >> 2) & 1){
brunofgc 37:0e95c85f0160 310 strcat(resetCauses,",WDT");
brunofgc 37:0e95c85f0160 311 }else{strcat(resetCauses,",NOW");}
brunofgc 33:a60abda630f7 312
brunofgc 33:a60abda630f7 313 diversos::wdt.kick(90.0);
brunofgc 33:a60abda630f7 314
brunofgc 14:c0162ab2a951 315 pwmPeriod = 100000;
brunofgc 0:1c0a769988ee 316 SD1.period_us(pwmPeriod);
brunofgc 0:1c0a769988ee 317 modemCom::atendendoWebServer = false;
brunofgc 34:348991c54ff3 318
brunofgc 30:8a06a85d8807 319 printf("Versao: %s.\r\n",firmVersion);
brunofgc 33:a60abda630f7 320 printf("Reset Causes: %s.\r\n",resetCauses);
brunofgc 0:1c0a769988ee 321 modemCom::timeOutModem = 30;
brunofgc 14:c0162ab2a951 322 serialModem::serial_init();
brunofgc 14:c0162ab2a951 323 numeroDeBytesPorEnvio = 768;
brunofgc 14:c0162ab2a951 324 modBusMaster1::modBusMaster(&modBusMaster1Serial,19200,&max_de);
brunofgc 14:c0162ab2a951 325 inicializaSensoresTemperatura();
brunofgc 0:1c0a769988ee 326
brunofgc 0:1c0a769988ee 327 if(sdCard::init()){
brunofgc 30:8a06a85d8807 328 printf("SD Card inicializado com sucesso.\n");
brunofgc 0:1c0a769988ee 329 //Carregando a lista de dispositivos
brunofgc 0:1c0a769988ee 330 loadDevices();
brunofgc 0:1c0a769988ee 331 }else{
brunofgc 30:8a06a85d8807 332 printf("Nao foi possivel inicializar o SD Card.\n");
brunofgc 13:b9183b4bc049 333 }
brunofgc 13:b9183b4bc049 334
brunofgc 13:b9183b4bc049 335
brunofgc 7:ae9c47f62946 336 //Tick Timer por segundo
brunofgc 0:1c0a769988ee 337 RTC::attach(&RTC_POR_SEGUNDO, RTC::Second);
brunofgc 7:ae9c47f62946 338
brunofgc 7:ae9c47f62946 339 /*Chamada de threads*/
brunofgc 7:ae9c47f62946 340
brunofgc 7:ae9c47f62946 341 //Thread timers
brunofgc 0:1c0a769988ee 342 idThreadTimers = osThreadCreate(osThread(threadTimers), NULL);
brunofgc 7:ae9c47f62946 343 //Thread comunicacoes
brunofgc 0:1c0a769988ee 344 idThreadComunicacoes = osThreadCreate(osThread(threadComunicacoes), NULL);
brunofgc 7:ae9c47f62946 345 //Thread processaPacote
brunofgc 0:1c0a769988ee 346 idThreadProcessaPacote = osThreadCreate(osThread(threadProcessaPacote), NULL);
brunofgc 7:ae9c47f62946 347
brunofgc 33:a60abda630f7 348 /*Chamada de threads*/
brunofgc 33:a60abda630f7 349 modemCom::status.connIDServerCommand = 255;
brunofgc 33:a60abda630f7 350 modemCom::status.connIDWebServer = 255;
brunofgc 33:a60abda630f7 351 modemCom::status.connIDSendData = 255;
brunofgc 0:1c0a769988ee 352 }
brunofgc 0:1c0a769988ee 353
brunofgc 0:1c0a769988ee 354 //Inicio do programa;
brunofgc 34:348991c54ff3 355 int main() {
brunofgc 38:07d3907b74e5 356 //uint8_t i;
brunofgc 37:0e95c85f0160 357 ESP_CH_PD = 0;
brunofgc 37:0e95c85f0160 358 ESP_ESPGPIO_2 = 0;
brunofgc 37:0e95c85f0160 359 ESP_RESET = 0;
brunofgc 37:0e95c85f0160 360
brunofgc 34:348991c54ff3 361 flashPrepare();
brunofgc 34:348991c54ff3 362 firmDescToRam();
brunofgc 34:348991c54ff3 363 serialPC::serialPC_init();
brunofgc 34:348991c54ff3 364 pc.printf("firmDesc: nome <%s> tamFirmware = %lu executaBootLoader = %u.\r\n",(char*)&memBufConfigBootLoader[5],tamFirmware,executaBootLoader);
brunofgc 34:348991c54ff3 365
brunofgc 34:348991c54ff3 366 if(executaBootLoader==1){
brunofgc 34:348991c54ff3 367 executaBootLoader=0;
brunofgc 34:348991c54ff3 368 pc.printf("Chamando o bootloader logo apos dar reset.\r\n");
brunofgc 34:348991c54ff3 369 while( ((LPC_UART0->LSR >> 6) &0x1) == 0 );
brunofgc 34:348991c54ff3 370 //__disable_irq();
brunofgc 34:348991c54ff3 371 SysTick->CTRL &= ~0x00000002; // disable SysTick interrupt
brunofgc 34:348991c54ff3 372 NVIC_DeInit();
brunofgc 34:348991c54ff3 373 firmDescToFlash();
brunofgc 34:348991c54ff3 374 bootLoader();
brunofgc 34:348991c54ff3 375 }
brunofgc 34:348991c54ff3 376
brunofgc 38:07d3907b74e5 377 sprintf(firmVersion,"08/06/18(BL)");
brunofgc 30:8a06a85d8807 378
brunofgc 33:a60abda630f7 379
brunofgc 6:d4ebbaaba295 380 eInt1.rise(&diversos::processaPulsosEDs);
brunofgc 6:d4ebbaaba295 381 eInt2.rise(&diversos::processaPulsosEDs);
brunofgc 33:a60abda630f7 382 eInt3.rise(&diversos::processaPulsosEDs);
brunofgc 0:1c0a769988ee 383
brunofgc 0:1c0a769988ee 384 modem.baud(76800);
brunofgc 37:0e95c85f0160 385
brunofgc 0:1c0a769988ee 386 ESP_CH_PD = 1;
brunofgc 0:1c0a769988ee 387 ESP_ESPGPIO_2 = 1;
brunofgc 37:0e95c85f0160 388 ESP_RESET = 1;
brunofgc 0:1c0a769988ee 389
brunofgc 37:0e95c85f0160 390 wait(5);
brunofgc 33:a60abda630f7 391
brunofgc 33:a60abda630f7 392 inicializaMaquina();
brunofgc 0:1c0a769988ee 393 modemCom::inicializaModem();
brunofgc 33:a60abda630f7 394 /*if(!modemCom::verificaConexao()){
brunofgc 21:b9315cdd9275 395 modemCom::conectaWiFi();
brunofgc 33:a60abda630f7 396 }*/
brunofgc 33:a60abda630f7 397
brunofgc 33:a60abda630f7 398 //Agendando primeiro envio de cara.
brunofgc 33:a60abda630f7 399 eventosRTC::rotinaEnvioDeDados=maxTentativasEnvioDados;
brunofgc 33:a60abda630f7 400 osSignalSet(idThreadComunicacoes, 0x1);
brunofgc 33:a60abda630f7 401
brunofgc 34:348991c54ff3 402
brunofgc 21:b9315cdd9275 403
brunofgc 29:823a9da3696b 404 while(true){
brunofgc 29:823a9da3696b 405 osDelay(100);
brunofgc 13:b9183b4bc049 406 if(callBootLoader){
brunofgc 35:11a51a36c494 407 diversos::wdt.kick(180.0);
brunofgc 13:b9183b4bc049 408 SysTick->CTRL &= ~0x00000002; // disable SysTick interrupt
brunofgc 34:348991c54ff3 409 NVIC_DeInit();
brunofgc 33:a60abda630f7 410
brunofgc 33:a60abda630f7 411 //Bloco novo
brunofgc 33:a60abda630f7 412 //SysTick->CTRL = 0;
brunofgc 33:a60abda630f7 413 //SysTick->LOAD = 0;
brunofgc 33:a60abda630f7 414 //SysTick->VAL = 0;
brunofgc 33:a60abda630f7 415
brunofgc 33:a60abda630f7 416 /**
brunofgc 33:a60abda630f7 417 * Step: Disable all interrupts
brunofgc 33:a60abda630f7 418 */
brunofgc 33:a60abda630f7 419 //__disable_irq();
brunofgc 33:a60abda630f7 420
brunofgc 33:a60abda630f7 421 /* ARM Cortex-M Programming Guide to Memory Barrier Instructions.*/
brunofgc 33:a60abda630f7 422 //__DSB();
brunofgc 33:a60abda630f7 423 //Bloco novo
brunofgc 13:b9183b4bc049 424 //__disable_irq();
brunofgc 34:348991c54ff3 425 //__disable_irq();
brunofgc 13:b9183b4bc049 426 bootloader_fillUpFlash(&sdCard::newFirmware);
brunofgc 34:348991c54ff3 427 pc.printf("Logo depois de fillUpFlash firmDesc: nome <%s> tamFirmware = %lu\r\n",(char*)&memBufConfigBootLoader[5],tamFirmware);
brunofgc 34:348991c54ff3 428
brunofgc 34:348991c54ff3 429
brunofgc 34:348991c54ff3 430 NVIC_SystemReset();
brunofgc 34:348991c54ff3 431 /*while( ((LPC_UART0->LSR >> 6) &0x1) == 0 );
brunofgc 13:b9183b4bc049 432 //__enable_irq();
brunofgc 34:348991c54ff3 433 bootLoader();*/
brunofgc 13:b9183b4bc049 434 }
brunofgc 29:823a9da3696b 435
brunofgc 33:a60abda630f7 436 /*---------------------Bloco de prevenção de bloqueio-------------------------------*/
brunofgc 29:823a9da3696b 437 if((!modemCom::status.recebendoArquivoDoServer)&&(!modemCom::status.emComunicacao)){
brunofgc 35:11a51a36c494 438 diversos::wdt.kick(180.0);
brunofgc 33:a60abda630f7 439 }else{
brunofgc 33:a60abda630f7 440 /*-----------------Bloco temporizador de recepção de arquivo--------------*/
brunofgc 29:823a9da3696b 441 if(modemCom::status.recebendoArquivoDoServer){
brunofgc 33:a60abda630f7 442 if(modemCom::status.recebendoArquivoDoServer==1){
brunofgc 33:a60abda630f7 443 modemCom::closeConnection(&modemCom::status.connIDServerCommand);
brunofgc 33:a60abda630f7 444 //Insere erro no arquivo.
brunofgc 33:a60abda630f7 445 }
brunofgc 33:a60abda630f7 446 modemCom::status.recebendoArquivoDoServer--;
brunofgc 33:a60abda630f7 447 if(xeretaModem){pc.printf("recebendoArquivoDoServer = %lu.\r\n",modemCom::status.recebendoArquivoDoServer);}
brunofgc 33:a60abda630f7 448 }
brunofgc 33:a60abda630f7 449 /*-----------------Bloco temporizador de recepção de arquivo--------------*/
brunofgc 33:a60abda630f7 450
brunofgc 33:a60abda630f7 451 /*-----------------Bloco temporizador de recepção de envio de dados-------*/
brunofgc 33:a60abda630f7 452 if(modemCom::status.emComunicacao){
brunofgc 33:a60abda630f7 453 if(modemCom::timeOutEnvioDados==1){
brunofgc 33:a60abda630f7 454 modemCom::status.emComunicacao=0;
brunofgc 33:a60abda630f7 455 modemCom::closeConnection(&modemCom::status.connIDServerCommand);
brunofgc 33:a60abda630f7 456 modemCom::closeConnection(&modemCom::status.connIDWebServer);
brunofgc 33:a60abda630f7 457 modemCom::closeConnection(&modemCom::status.connIDSendData);
brunofgc 33:a60abda630f7 458 //Insere erro no arquivo.
brunofgc 29:823a9da3696b 459 }
brunofgc 33:a60abda630f7 460 modemCom::timeOutEnvioDados--;
brunofgc 33:a60abda630f7 461 if(xeretaModem){pc.printf("timeOutEnvioDados = %lu.\r\n",modemCom::timeOutEnvioDados);}
brunofgc 29:823a9da3696b 462 }
brunofgc 33:a60abda630f7 463 /*-----------------Bloco temporizador de recepção de envio de dados-------*/
brunofgc 29:823a9da3696b 464 }
brunofgc 33:a60abda630f7 465 /*---------------------Bloco de prevenção de bloqueio-------------------------------*/
brunofgc 29:823a9da3696b 466
brunofgc 33:a60abda630f7 467 if(executaComandoServer){
brunofgc 33:a60abda630f7 468 executaComandoServer = false;
brunofgc 33:a60abda630f7 469
brunofgc 33:a60abda630f7 470 commands::exec(modemCom::status.connIDServerCommand);
brunofgc 33:a60abda630f7 471 if(!modemCom::status.recebendoArquivoDoServer){
brunofgc 33:a60abda630f7 472 if(debug){pc.printf("Fechando socket chave 4.\r\n");}
brunofgc 33:a60abda630f7 473 modemCom::closeConnection(&modemCom::status.connIDServerCommand);
brunofgc 33:a60abda630f7 474 }
brunofgc 33:a60abda630f7 475 }
brunofgc 33:a60abda630f7 476
brunofgc 33:a60abda630f7 477 if(sdCard::deleteSentFiles){
brunofgc 33:a60abda630f7 478 printf("Recebido comando para deletar arquivos enviados.\n");
brunofgc 33:a60abda630f7 479 sdCard::deleteBanks(sdCard::currentBankSending);
brunofgc 33:a60abda630f7 480 if(sdCard::modificaCurrentBank(sdCard::currentBank,!sdCard::currentBankSending)){
brunofgc 33:a60abda630f7 481 sdCard::deleteSentFiles=false;
brunofgc 33:a60abda630f7 482 }
brunofgc 33:a60abda630f7 483 }
brunofgc 33:a60abda630f7 484
brunofgc 33:a60abda630f7 485 if(modemCom::atendendoWebServer){
brunofgc 33:a60abda630f7 486 modemCom::atendendoWebServer = false;
brunofgc 33:a60abda630f7 487 if(debug){pc.printf("Atendendo webServer na conexao %u.\r\n",modemCom::status.connIDWebServer);}
brunofgc 33:a60abda630f7 488 modemCom::status.emComunicacao=true;
brunofgc 33:a60abda630f7 489 modemCom::webServer(modemCom::status.connIDWebServer);
brunofgc 33:a60abda630f7 490 modemCom::status.emComunicacao=false;
brunofgc 33:a60abda630f7 491 }
brunofgc 33:a60abda630f7 492
brunofgc 33:a60abda630f7 493 if(boolExecAct){
brunofgc 33:a60abda630f7 494 execAct(act);
brunofgc 33:a60abda630f7 495 boolExecAct = false;
brunofgc 33:a60abda630f7 496 }
brunofgc 30:8a06a85d8807 497
brunofgc 30:8a06a85d8807 498 if((!modemCom::status.recebendoArquivoDoServer)&&(!modemCom::status.emComunicacao)){
brunofgc 2:55b7b466e742 499 if(eventosRTC::rotina1segundo){
brunofgc 29:823a9da3696b 500 eventosRTC::rotina1segundo = false;
brunofgc 2:55b7b466e742 501 if(enviaDadosPorAlarme!=255){
brunofgc 2:55b7b466e742 502 enviaDadosPorAlarme--;
brunofgc 2:55b7b466e742 503 if(enviaDadosPorAlarme==0){
brunofgc 18:1eefda1f7736 504 eventosRTC::rotinaEnvioDeDados=maxTentativasEnvioDados;
brunofgc 30:8a06a85d8807 505 enviaDadosPorAlarme=255;
brunofgc 2:55b7b466e742 506 }
brunofgc 29:823a9da3696b 507 }
brunofgc 21:b9315cdd9275 508 leiturasAnalogicas();
brunofgc 21:b9315cdd9275 509 verificaSaidasDigitais();
brunofgc 26:c246eacf6815 510 leiturasDigitais();
brunofgc 30:8a06a85d8807 511
brunofgc 21:b9315cdd9275 512 if(debug){
brunofgc 21:b9315cdd9275 513 pc.printf("\nMemoria RAM disponivel = %lu\n",diversos::memAvailable());
brunofgc 38:07d3907b74e5 514 //pc.printf("\nEntrada 4 = %lu.\nEntrada 3 = %lu.\nEntrada 2 = %lu.\nEntrada 1 = %lu.\r\n",aiFiltrada[3],aiFiltrada[2],aiFiltrada[1],aiFiltrada[0]);
brunofgc 38:07d3907b74e5 515 //pc.printf("Pulsos <%lu><%lu><%lu>.\r\n",pulsosEDs[0],pulsosEDs[1],pulsosEDs[2]);
brunofgc 33:a60abda630f7 516 }
brunofgc 2:55b7b466e742 517 time_t seconds = time(NULL);
brunofgc 38:07d3907b74e5 518 pc.printf("Drome %s, %s\n",firmVersion,ctime(&seconds));
brunofgc 37:0e95c85f0160 519 }
brunofgc 2:55b7b466e742 520 if(eventosRTC::rotina10Segundos){
brunofgc 25:a6da63ed025b 521 //Executo abaixo a rotina de 10 segundos
brunofgc 26:c246eacf6815 522 eventosRTC::rotina10Segundos = false;
brunofgc 38:07d3907b74e5 523 verifySchedules();
brunofgc 38:07d3907b74e5 524 verifyAlarms();
brunofgc 38:07d3907b74e5 525 }
brunofgc 38:07d3907b74e5 526 if((eventosRTC::minutos)){//&&(!modemCom::status.recebendoArquivoDoServer)){
brunofgc 38:07d3907b74e5 527 //Executo abaixo a rotina de 1 minuto
brunofgc 38:07d3907b74e5 528 eventosRTC::minutos = false;
brunofgc 38:07d3907b74e5 529 refreshSensoresTemperatura();
brunofgc 38:07d3907b74e5 530 writeReadingsToSD();
brunofgc 38:07d3907b74e5 531 }
brunofgc 38:07d3907b74e5 532 }
brunofgc 37:0e95c85f0160 533
brunofgc 37:0e95c85f0160 534 if(hardwareReset){
brunofgc 37:0e95c85f0160 535 //Resetando
brunofgc 37:0e95c85f0160 536 pc.printf("hardwareReset = true.\r\n");
brunofgc 37:0e95c85f0160 537 osDelay(50); //Para dar tempo de enviar todo o printf.
brunofgc 37:0e95c85f0160 538 //diversos::wdt.kick(0.1);
brunofgc 37:0e95c85f0160 539 NVIC_SystemReset();
brunofgc 37:0e95c85f0160 540 while(true);
brunofgc 37:0e95c85f0160 541 }
brunofgc 0:1c0a769988ee 542 }
brunofgc 0:1c0a769988ee 543 }