diff --git a/variants/nano_33_iot/variant.cpp b/variants/nano_33_iot/variant.cpp index 82c5b9e52..4be316e91 100644 --- a/variants/nano_33_iot/variant.cpp +++ b/variants/nano_33_iot/variant.cpp @@ -167,7 +167,86 @@ extern "C" { const void* g_apTCInstances[TCC_INST_NUM+TC_INST_NUM]={ TCC0, TCC1, TCC2, TC3, TC4, TC5 } ; +#if defined(__ARDUINOMOTORSHIELD__) +#include "wiring_private.h" + +#define PMIC_ADDRESS 0x6B +#define PMIC_REG01 0x01 +#define PMIC_REG05 0x05 +#define PMIC_REG07 0x07 + +static inline void enable_battery_charging() { + PERIPH_WIRE.initMasterWIRE(100000); + PERIPH_WIRE.enableWIRE(); + pinPeripheral(PIN_WIRE_SDA, g_APinDescription[PIN_WIRE_SDA].ulPinType); + pinPeripheral(PIN_WIRE_SCL, g_APinDescription[PIN_WIRE_SCL].ulPinType); + + PERIPH_WIRE.startTransmissionWIRE( PMIC_ADDRESS, WIRE_WRITE_FLAG ); + PERIPH_WIRE.sendDataMasterWIRE(PMIC_REG01); + PERIPH_WIRE.sendDataMasterWIRE(0x1B); // Charge Battery + Minimum System Voltage 3.5V + PERIPH_WIRE.prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); + + PERIPH_WIRE.disableWIRE(); +} + +static inline void disable_battery_fet(bool disabled) { + PERIPH_WIRE.initMasterWIRE(100000); + PERIPH_WIRE.enableWIRE(); + pinPeripheral(PIN_WIRE_SDA, g_APinDescription[PIN_WIRE_SDA].ulPinType); + pinPeripheral(PIN_WIRE_SCL, g_APinDescription[PIN_WIRE_SCL].ulPinType); + + PERIPH_WIRE.startTransmissionWIRE( PMIC_ADDRESS, WIRE_WRITE_FLAG ); + PERIPH_WIRE.sendDataMasterWIRE(PMIC_REG07); + // No D+/D– detection + Safety timer not slowed by 2X during input DPM or thermal regulation + + // BAT fet disabled/enabled + charge and bat fault INT + PERIPH_WIRE.sendDataMasterWIRE(0x0B | (disabled ? 0x20 : 0x00)); + PERIPH_WIRE.prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); + + PERIPH_WIRE.disableWIRE(); +} + +void disable_watchdog() { + PERIPH_WIRE.initMasterWIRE(100000); + PERIPH_WIRE.enableWIRE(); + pinPeripheral(PIN_WIRE_SDA, g_APinDescription[PIN_WIRE_SDA].ulPinType); + pinPeripheral(PIN_WIRE_SCL, g_APinDescription[PIN_WIRE_SCL].ulPinType); + + PERIPH_WIRE.startTransmissionWIRE( PMIC_ADDRESS, WIRE_WRITE_FLAG ); + PERIPH_WIRE.sendDataMasterWIRE(PMIC_REG05); + PERIPH_WIRE.sendDataMasterWIRE(0x8A); // disable wahcdog + PERIPH_WIRE.prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); + + PERIPH_WIRE.disableWIRE(); + + int DATA = readRegister(CHARGE_TIMER_CONTROL_REGISTER); + + if (DATA == -1) { + return 0; + } + + return writeRegister(CHARGE_TIMER_CONTROL_REGISTER, (DATA & 0b11001110)); +} + +#endif + + void initVariant() { +#if defined(__ARDUINOMOTORSHIELD__) + pinMode(ADC_BATTERY, OUTPUT); + digitalWrite(ADC_BATTERY, LOW); + delay(10); + pinMode(ADC_BATTERY, INPUT); + delay(100); + + bool batteryPresent = analogRead(ADC_BATTERY) > 600; + if (batteryPresent) { + disable_watchdog(); + enable_battery_charging(); + } + disable_battery_fet(!batteryPresent); +#endif + + // NINA - SPI boot pinMode(NINA_GPIO0, OUTPUT); digitalWrite(NINA_GPIO0, HIGH); @@ -185,6 +264,8 @@ SERCOM sercom3( SERCOM3 ) ; SERCOM sercom4( SERCOM4 ) ; SERCOM sercom5( SERCOM5 ) ; + + Uart Serial1( &sercom5, PIN_SERIAL1_RX, PIN_SERIAL1_TX, PAD_SERIAL1_RX, PAD_SERIAL1_TX ) ; void SERCOM5_Handler()