diff --git a/src/IOManagement.cpp b/src/IOManagement.cpp index 518fdff..9365dfa 100644 --- a/src/IOManagement.cpp +++ b/src/IOManagement.cpp @@ -18,21 +18,39 @@ void initIO() { pinMode(BATT_POS_CONT_MCU, INPUT); pinMode(PPC1_SUPP_INVALID, INPUT); pinMode(PPC1_DCDC_INVALID, INPUT); + + // Initialize MCU_BATT_EN as push-pull output with explicit mode pinMode(MCU_BATT_EN, OUTPUT); + digitalWrite(MCU_BATT_EN, 1); + pinMode(MPPT_CONT_MCU, INPUT); pinMode(MC_CONT_MCU, INPUT); - // Initialize analog pins + // Initialize analog pins AFTER digital outputs are set + // This prevents ADC initialization from affecting GPIO state initADC(ADC1); if (IOTimer.attachInterruptInterval(IO_UPDATE_PERIOD, readIO)) { printf("IO timer started\n"); + // Ensure the timer interrupt has lower priority than other operations + // to prevent it from interrupting critical GPIO writes + NVIC_SetPriority(TIM2_IRQn, 3); } else { printf("Failed to start IO timer\n"); } } void readIO() { + // Debug: log when ADC reads happen + static unsigned long lastReadTime = 0; + unsigned long currentTime = millis(); + if (currentTime - lastReadTime > 50) { // Only print occasionally to avoid spam + Serial.print("[ADC_READ @ "); + Serial.print(currentTime); + Serial.println("ms]"); + lastReadTime = currentTime; + } + digital_data.batt_neg_cont = digitalRead(BATT_NEG_CONT_MCU); digital_data.estop_mcu = digitalRead(ESTOP_MCU); digital_data.batt_pos_cont = digitalRead(BATT_POS_CONT_MCU); diff --git a/src/main.cpp b/src/main.cpp index cb9fbba..b55496f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,15 +1,27 @@ #include +#include "IOManagement.h" // put function declarations here: int myFunction(int, int); void setup() { // put your setup code here, to run once: - int result = myFunction(2, 3); + Serial.begin(115200); + delay(100); + + Serial.println("Starting setup..."); + + pinMode(MCU_BATT_EN, OUTPUT); + digitalWrite(MCU_BATT_EN, HIGH); + + Serial.println("Setup complete. MCU_BATT_EN is now HIGH and should stay HIGH"); } void loop() { // put your main code here, to run repeatedly: + // Keep the pin HIGH at all times + digitalWrite(MCU_BATT_EN, HIGH); + delay(100); } // put function definitions here: