diff --git a/Firmware/Modulos/I2CService.h b/Firmware/Modulos/I2CService.h index 92ec7541c..212b3c4af 100644 --- a/Firmware/Modulos/I2CService.h +++ b/Firmware/Modulos/I2CService.h @@ -98,15 +98,16 @@ class I2CService { return false; } + idAtualUsandoI2C = idSensor; + tempoEntradaI2C = millis(); + // Troca canal se necessário if (canalMux > -1 && !SelecionarCanalMux(canalMux)) { + idAtualUsandoI2C = -1; xSemaphoreGive(i2cMutex); // Libera caso a troca de canal falhe return false; } - idAtualUsandoI2C = idSensor; - tempoEntradaI2C = millis(); - return true; } @@ -130,7 +131,7 @@ class I2CService { static bool WirePodeFinalizar() { pinMode(_pinoSCL, INPUT_PULLUP); pinMode(_pinoSDA, INPUT_PULLUP); - delay(1); // garante leitura estável + vTaskDelay(1); // garante leitura estável return (digitalRead(_pinoSCL) == HIGH && digitalRead(_pinoSDA) == HIGH); } @@ -227,7 +228,11 @@ class I2CService { if (canalMux != canalAtivo) { Wire.beginTransmission(endereco); Wire.write(1 << canalMux); - Wire.endTransmission(); + int err = Wire.endTransmission(); + if (err != 0) { + MostrarLog("MUX", "Falha ao trocar canal, err=" + String(err)); + return false; + } MostrarLog("MUX", "0x" + String(endereco) + " - Canal alterado de " + String(canalAtivo) + " para " + String(canalMux)); canalAtivo = canalMux; } @@ -251,7 +256,9 @@ class I2CService { } static int RealizarLeituraADS(int canalGlobal, int leituras, int idSensor, int canalMux) { - SolicitarAcessoI2C(idSensor, canalMux); + if (!SolicitarAcessoI2C(idSensor, canalMux)) { + return -1; + } int canal = SelecionarCanalADS(canalGlobal); if (canal > -1) { uint16_t l = 0; @@ -277,7 +284,14 @@ class I2CService { } MostrarLog("MUX", "Iniciando TCA9548A..."); if (!MuxIniciado) { - SolicitarAcessoI2C(); + if (!SolicitarAcessoI2C(idSensorMux)) { + MostrarLog("MUX", "TCA9548A nao Iniciado, falha ao solicitar acesso ao barramento I2C"); + if (Mandatorio) { + MostrarLog("MUX", "Conexão obrigatória, tentando novamente..."); + IniciarMUX(endereco, false); + } + return; + } MuxIniciado = VerificaEnderecoBarramento(endereco); if (!MuxIniciado) { MostrarLog("MUX", "TCA9548A nao Iniciado, endereco " + String(endereco) + " nao encontrado no barramento"); @@ -292,11 +306,9 @@ class I2CService { } else { canalAtivo = -1; // Nenhum canal ativo ainda - idAtualUsandoI2C = -1; - i2cMutex = xSemaphoreCreateMutex(); MostrarLog("MUX", "TCA9548A Iniciado"); } - LiberarAcessoI2C(); + LiberarAcessoI2C(idSensorMux); } else { MostrarLog("MUX", "TCA9548A ja Iniciado"); @@ -310,7 +322,14 @@ class I2CService { } MostrarLog("MCP", "Iniciando MCP23X17..."); if (!McpIniciado) { - SolicitarAcessoI2C(); + if (!SolicitarAcessoI2C(idSensorMcp)) { + MostrarLog("MCP", "MCP23C17 nao Iniciado, falha ao solicitar acesso ao barramento I2C"); + if (Mandatorio) { + MostrarLog("MCP", "Conexão obrigatória, tentando novamente..."); + IniciarMCP(endereco, false); + } + return; + } McpIniciado = mcp.begin_I2C(endereco, &Wire); if (!McpIniciado) { MostrarLog("MCP", "Erro ao iniciar MCP23X17!"); @@ -326,7 +345,7 @@ class I2CService { else { MostrarLog("MCP", "MCP23X17 Iniciado"); } - LiberarAcessoI2C(); + LiberarAcessoI2C(idSensorMcp); } else { MostrarLog("MCP", "MCP23X17 ja Iniciado"); @@ -366,6 +385,9 @@ class I2CService { } } + static int idSensorMux; + static int idSensorMcp; + static int _pinoSDA; static int _pinoSCL; static unsigned long tempoEntradaI2C; @@ -405,6 +427,8 @@ unsigned long I2CService::LimiteTempoI2C = 2000; bool I2CService::I2CIniciado = false; bool I2CService::MuxIniciado = false; bool I2CService::McpIniciado = false; +int I2CService::idSensorMux = 200; +int I2CService::idSensorMcp = 201; Adafruit_MCP23X17 I2CService::mcp; Adafruit_ADS1115 I2CService::ads; byte I2CService::ultimoEnderecoADS = 0xFF; diff --git a/Firmware/Modulos/Pinout.h b/Firmware/Modulos/Pinout.h index 1c4f4b53a..1aa4b314f 100644 --- a/Firmware/Modulos/Pinout.h +++ b/Firmware/Modulos/Pinout.h @@ -42,14 +42,16 @@ class PinoModel { } else if (barramento == TiposBarramentos::EXPANSOR_GPIO) { if (I2CService::McpIniciado) { - Conectado = true; - I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX); + if (!I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX)) { + return; + } if (tipo == _INPUT) { I2CService::mcp.pinMode(num, INPUT); } else if (tipo == _OUTPUT) { I2CService::mcp.pinMode(num, OUTPUT); - set(statusInicial); + set(statusInicial, false); } + Conectado = true; I2CService::LiberarAcessoI2C(idSensor); } } @@ -73,7 +75,9 @@ class PinoModel { } else if (barramento == EXPANSOR_GPIO) { if (I2CService::McpIniciado) { - I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX); + if (!I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX)) { + return; + } I2CService::mcp.pinMode(num, INPUT); I2CService::LiberarAcessoI2C(idSensor); } @@ -101,7 +105,9 @@ class PinoModel { } else if (barramento == TiposBarramentos::EXPANSOR_GPIO) { if (I2CService::McpIniciado) { - I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX); + if (!I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX)) { + return -1; + } int leitura = I2CService::mcp.digitalRead(num); I2CService::LiberarAcessoI2C(idSensor); return leitura; @@ -114,16 +120,20 @@ class PinoModel { return -1; } - void set(bool status) { + void set(bool status, bool reqMutex = true) { if (Conectado) { if (barramento == CONTROLADOR) { digitalWrite(num, status ? HIGH : LOW); } else if (barramento == EXPANSOR_GPIO) { if (I2CService::McpIniciado) { - I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX); + if (reqMutex && !I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX)) { + return; + } I2CService::mcp.digitalWrite(num, status ? HIGH : LOW); - I2CService::LiberarAcessoI2C(idSensor); + if (reqMutex) { + I2CService::LiberarAcessoI2C(idSensor); + } } } } diff --git a/Firmware/Modulos/SensorCorrenteModel_v2.h b/Firmware/Modulos/SensorCorrenteModel_v2.h index b4ba99460..eca615984 100644 --- a/Firmware/Modulos/SensorCorrenteModel_v2.h +++ b/Firmware/Modulos/SensorCorrenteModel_v2.h @@ -150,7 +150,7 @@ class SensorCorrente_v2 : public ComponenteCAN { } private: - bool DebugMode = true; + bool DebugMode = false; SimpleKalmanFilter filtro = SimpleKalmanFilter(10, 10, 0.05); Adafruit_INA219 sINA219; Adafruit_INA228 sINA228; @@ -187,11 +187,11 @@ class SensorCorrente_v2 : public ComponenteCAN { busVoltage = sINA228.getBusVoltage_V(); float leituraCorrente = sINA228.getCurrent_mA(); current = filtro.updateEstimate(leituraCorrente); - power = sINA228.getPower_mW(); + //power = sINA228.getPower_mW(); temperature = sINA228.readDieTemp(); - energy = sINA228.readEnergy(); - charge = sINA228.readCharge(); - codAlarme = (uint16_t)LerRegistradorINA(0x0B, 2); + //energy = sINA228.readEnergy(); + //charge = sINA228.readCharge(); + //codAlarme = (uint16_t)LerRegistradorINA(0x0B, 2); } I2CService::LiberarAcessoI2C(ID_Num); diff --git a/Firmware/Modulos/SensorTemperaturaNtcModel.h b/Firmware/Modulos/SensorTemperaturaNtcModel.h index d4b749f0d..6efe8dd94 100644 --- a/Firmware/Modulos/SensorTemperaturaNtcModel.h +++ b/Firmware/Modulos/SensorTemperaturaNtcModel.h @@ -18,7 +18,6 @@ class SensorTemperaturaNtc : public ComponenteCAN { double tensao = 0; bool Iniciado = false; - bool Checando = false; float OffsetTemperatura = 0.0f; @@ -99,7 +98,7 @@ class SensorTemperaturaNtc : public ComponenteCAN { } private: - bool DebugMode = true; + bool DebugMode = false; // SimpleKalmanFilter filtro = SimpleKalmanFilter(5, 15, 0.01); void MostrarLog(String mensagem) {