working adc and wall power detection

custom
jacqueline 3 years ago
parent 112a43679f
commit b34b49aabc
  1. 33
      main/gay-ipod-fw.cpp
  2. 16
      main/gpio-expander.cpp
  3. 36
      main/gpio-expander.h

@ -1,18 +1,21 @@
#include <stdio.h> #include <stdio.h>
#include "gpio-expander.h" #include "driver/adc.h"
#include "esp_log.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "driver/i2c.h" #include "driver/i2c.h"
#include "driver/spi_common.h" #include "driver/spi_common.h"
#include "driver/spi_master.h" #include "driver/spi_master.h"
#include "esp_adc_cal.h"
#include "esp_log.h"
#include "gpio-expander.h" #include "gpio-expander.h"
#include "gpio-expander.h"
#include "hal/adc_types.h"
#include "hal/gpio_types.h" #include "hal/gpio_types.h"
#include "hal/spi_types.h" #include "hal/spi_types.h"
#define I2C_SDA_IO (GPIO_NUM_0) #define I2C_SDA_IO (GPIO_NUM_0)
#define I2C_SCL_IO (GPIO_NUM_4) #define I2C_SCL_IO (GPIO_NUM_4)
#define I2C_CLOCK_HZ (100000) #define I2C_CLOCK_HZ (400000)
#define SPI_SDI_IO (GPIO_NUM_19) #define SPI_SDI_IO (GPIO_NUM_19)
#define SPI_SDO_IO (GPIO_NUM_23) #define SPI_SDO_IO (GPIO_NUM_23)
@ -87,7 +90,29 @@ extern "C" void app_main(void)
ESP_LOGI(TAG, "Setting default GPIO state"); ESP_LOGI(TAG, "Setting default GPIO state");
gay_ipod::GpioExpander expander; gay_ipod::GpioExpander expander;
// for debugging usb ic
//expander.set_sd_mux(gay_ipod::GpioExpander::USB);
ESP_ERROR_CHECK(expander.Write()); ESP_ERROR_CHECK(expander.Write());
ESP_LOGI(TAG, "Idling."); ESP_LOGI(TAG, "Init ADC");
// Attentuate our battery voltage pin to read between 150mV and 2450mV.
esp_adc_cal_characteristics_t adc1_chars;
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 0, &adc1_chars);
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11);
while (1) {
int adc_raw = adc1_get_raw(ADC1_CHANNEL_6);
uint32_t adc_millivolts = esp_adc_cal_raw_to_voltage(adc_raw, &adc1_chars);
expander.Read();
ESP_LOGI(TAG, "raw adc: %d, millivolts: %d, wall power? %d", adc_raw, adc_millivolts, expander.charge_power_ok());
vTaskDelay(pdMS_TO_TICKS(1000));
ESP_ERROR_CHECK(expander.Write());
}
} }

@ -38,8 +38,8 @@ esp_err_t GpioExpander::Read() {
// it because that would indicate some really very badly wrong more generally. // it because that would indicate some really very badly wrong more generally.
i2c_master_start(handle); i2c_master_start(handle);
i2c_master_write_byte(handle, (PCA8575_ADDRESS << 1 | I2C_MASTER_READ), true); i2c_master_write_byte(handle, (PCA8575_ADDRESS << 1 | I2C_MASTER_READ), true);
i2c_master_read_byte(handle, &port_a_, I2C_MASTER_ACK); i2c_master_read_byte(handle, &input_a_, I2C_MASTER_ACK);
i2c_master_read_byte(handle, &port_b_, I2C_MASTER_LAST_NACK); i2c_master_read_byte(handle, &input_b_, I2C_MASTER_LAST_NACK);
i2c_master_stop(handle); i2c_master_stop(handle);
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, handle, PCA8575_TIMEOUT); esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, handle, PCA8575_TIMEOUT);
@ -62,4 +62,16 @@ uint8_t GpioExpander::key_states(void) {
return input_b_ & 0b00111111; return input_b_ & 0b00111111;
} }
void GpioExpander::set_sd_mux(SdMuxController controller) {
port_a_ &= (1 << 5);
}
void GpioExpander::set_sd_cs(bool high) {
port_a_ &= (1 << 6);
}
void GpioExpander::set_display_cs(bool high) {
port_a_ &= (1 << 7);
}
} // namespace gay_ipod } // namespace gay_ipod

@ -27,16 +27,44 @@ class GpioExpander {
bool headphone_detect(void); bool headphone_detect(void);
uint8_t key_states(void); uint8_t key_states(void);
enum SdMuxController {
ESP,
USB
};
void set_sd_mux(SdMuxController controller);
void set_sd_cs(bool high);
void set_display_cs(bool high);
// Not copyable or movable. // Not copyable or movable.
// TODO: maybe this could be movable? // TODO: maybe this could be movable?
GpioExpander(const GpioExpander&) = delete; GpioExpander(const GpioExpander&) = delete;
GpioExpander& operator=(const GpioExpander&) = delete; GpioExpander& operator=(const GpioExpander&) = delete;
private: private:
// All power switches high, PWR_OK low (input), both CS pins high. // Port A:
uint8_t port_a_ = uint8_t{0b00001000}; // 0 - audio power enable
// DAC mute output low, everything else is input and so low. // 1 - usb interface power enable
uint8_t port_b_ = uint8_t{0b11111111}; // 2 - display power enable
// 3 - sd card power enable
// 4 - charge power ok (active low)
// 5 - sd mux switch
// 6 - sd chip select
// 7 - display chip select
// All power switches low, chip selects high, active-low charge power high
uint8_t port_a_ = 0b11010000;
// Port B:
// 0 - 3.5mm jack detect (active low)
// 1 - dac soft mute switch
// 2 - GPIO
// 3 - GPIO
// 4 - GPIO
// 5 - GPIO
// 6 - GPIO
// 7 - GPIO
// DAC mute output low, everything else is active-low inputs.
uint8_t port_b_ = 0b11111101;
uint8_t input_a_ = 0; uint8_t input_a_ = 0;
uint8_t input_b_ = 0; uint8_t input_b_ = 0;

Loading…
Cancel
Save