i2c adjust and pin downgrade

This commit is contained in:
Empire Phoenix 2024-05-01 18:06:20 +02:00
parent bfb19321fd
commit dd91973f9b
3 changed files with 38 additions and 37 deletions

View File

@ -8,7 +8,7 @@ rust-version = "1.71"
[profile.dev]
# Explicitly disable LTO which the Xtensa codegen backend has issues
lto = true
lto = false
strip = false
debug = true
overflow-checks = true
@ -69,10 +69,10 @@ strum = { version = "0.26.1", features = ["derive"] }
once_cell = "1.19.0"
measurements = "0.11.0"
bq34z100 = "0.2.1"
bitbang-hal = "0.3.2"
[patch.crates-io]
esp-idf-hal = { git = "https://github.com/esp-rs/esp-idf-hal.git" }
#esp-idf-hal = { git = "https://github.com/esp-rs/esp-idf-hal.git" }
esp-idf-hal = { git = "https://github.com/empirephoenix/esp-idf-hal.git" }
#esp-idf-sys = { git = "https://github.com/empirephoenix/esp-idf-sys.git" }
#esp-idf-sys = { git = "https://github.com/esp-rs/esp-idf-sys.git" }
#esp-idf-svc = { git = "https://github.com/esp-rs/esp-idf-svc.git" }

View File

@ -6,3 +6,5 @@ CONFIG_ESP_MAIN_TASK_STACK_SIZE=25000
CONFIG_FREERTOS_HZ=1000
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
CONFIG_I2C_ENABLE_DEBUG_LOG=y
DEBUG_LEVEL=5

View File

@ -4,7 +4,7 @@ use chrono_tz::Europe::Berlin;
use embedded_svc::wifi::{
AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration,
};
use esp_idf_hal::i2c::{I2cConfig, I2cDriver, I2cError};
use esp_idf_hal::i2c::{APBTickType, I2cConfig, I2cDriver, I2cError};
use esp_idf_hal::units::FromValueType;
use esp_idf_svc::eventloop::EspSystemEventLoop;
use esp_idf_svc::ipv4::IpInfo;
@ -34,7 +34,7 @@ use std::time::Duration;
use embedded_hal::digital::OutputPin;
use esp_idf_hal::adc::{attenuation, AdcChannelDriver, AdcDriver};
use esp_idf_hal::delay::Delay;
use esp_idf_hal::gpio::{AnyInputPin, Gpio18, Gpio5, InputOutput, Level, PinDriver, Pull};
use esp_idf_hal::gpio::{AnyInputPin, Gpio18, Gpio5, IOPin, InputOutput, Level, PinDriver, Pull};
use esp_idf_hal::pcnt::{
PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex,
};
@ -45,8 +45,6 @@ use esp_idf_svc::systime::EspSystemTime;
use esp_idf_sys::{esp, gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError};
use one_wire_bus::OneWire;
use bitbang_hal;
use crate::config::{self, Config, WifiConfig};
use crate::STAY_ALIVE;
@ -159,19 +157,19 @@ pub struct PlantHal {}
pub struct PlantCtrlBoard<'a> {
shift_register: ShiftRegister40<
PinDriver<'a, esp_idf_hal::gpio::Gpio15, InputOutput>,
PinDriver<'a, esp_idf_hal::gpio::Gpio3, InputOutput>,
PinDriver<'a, esp_idf_hal::gpio::Gpio23, InputOutput>,
PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
>,
tank_driver: AdcDriver<'a, esp_idf_hal::adc::ADC1>,
tank_channel: esp_idf_hal::adc::AdcChannelDriver<'a, { attenuation::DB_11 }, Gpio5>,
solar_is_day: PinDriver<'a, esp_idf_hal::gpio::Gpio8, esp_idf_hal::gpio::Input>,
boot_button: PinDriver<'a, esp_idf_hal::gpio::Gpio9, esp_idf_hal::gpio::Input>,
solar_is_day: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>,
boot_button: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, esp_idf_hal::gpio::Input>,
signal_counter: PcntDriver<'a>,
light: PinDriver<'a, esp_idf_hal::gpio::Gpio10, InputOutput>,
main_pump: PinDriver<'a, esp_idf_hal::gpio::Gpio2, InputOutput>,
tank_power: PinDriver<'a, esp_idf_hal::gpio::Gpio11, InputOutput>,
general_fault: PinDriver<'a, esp_idf_hal::gpio::Gpio6, InputOutput>,
light: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
main_pump: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
tank_power: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
general_fault: PinDriver<'a, esp_idf_hal::gpio::AnyIOPin, InputOutput>,
pub wifi_driver: EspWifi<'a>,
one_wire_bus: OneWire<PinDriver<'a, Gpio18, esp_idf_hal::gpio::InputOutput>>,
mqtt_client: Option<EspMqttClient<'a>>,
@ -661,7 +659,6 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
data,
details: _,
} => {
println!("Received something");
let data = String::from_utf8_lossy(data);
if let Some(topic) = topic {
//todo use enums
@ -754,7 +751,6 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
}
fn mqtt_publish(&mut self, config: &Config, subtopic: &str, message: &[u8]) -> Result<()> {
println!("Publishing mqtt {} content {:?}", subtopic, message);
if !subtopic.starts_with("/") {
println!("Subtopic without / at start {}", subtopic);
bail!("Subtopic without / at start {}", subtopic);
@ -958,21 +954,24 @@ impl CreatePlantHal<'_> for PlantHal {
let i2c = peripherals.i2c0;
let config = I2cConfig::new()
.scl_enable_pullup(false)
.sda_enable_pullup(false)
.scl_enable_pullup(true)
.sda_enable_pullup(true)
.baudrate(1_u32.kHz().into())
.timeout(Duration::from_millis(10).into());
let scl = peripherals.pins.gpio19;
let sda = peripherals.pins.gpio20;
.timeout(APBTickType::from(Duration::from_millis(100)));
let scl = peripherals.pins.gpio19.downgrade();
let sda = peripherals.pins.gpio20.downgrade();
let driver = I2cDriver::new(i2c, sda, scl, &config).unwrap();
let i2c_port = driver.port();
let mut timeout:i32 = 0;
esp!(unsafe { esp_idf_sys::i2c_get_timeout(i2c_port, &mut timeout)}).unwrap();
println!("Default i2c timeout is {}", timeout);
println!("init i2c timeout is {}", timeout);
//esp!(unsafe { esp_idf_sys::i2c_set_timeout(i2c_port, 1)}).unwrap();
loop {
unsafe { vTaskDelay(1) }
}
//esp!(unsafe { esp_idf_sys::i2c_set_timeout(i2c_port, 22)}).unwrap();
let mut battery_driver: Bq34z100g1Driver<I2cDriver, Delay> = Bq34z100g1Driver {
i2c: driver,
@ -980,11 +979,11 @@ impl CreatePlantHal<'_> for PlantHal {
flash_block_data: [0; 32],
};
let mut clock = PinDriver::input_output(peripherals.pins.gpio15)?;
let mut clock = PinDriver::input_output(peripherals.pins.gpio15.downgrade())?;
clock.set_pull(Pull::Floating).unwrap();
let mut latch = PinDriver::input_output(peripherals.pins.gpio3)?;
let mut latch = PinDriver::input_output(peripherals.pins.gpio3.downgrade())?;
latch.set_pull(Pull::Floating).unwrap();
let mut data = PinDriver::input_output(peripherals.pins.gpio23)?;
let mut data = PinDriver::input_output(peripherals.pins.gpio23.downgrade())?;
data.set_pull(Pull::Floating).unwrap();
let shift_register = ShiftRegister40::new(clock.into(), latch.into(), data.into());
for mut pin in shift_register.decompose() {
@ -1082,27 +1081,27 @@ impl CreatePlantHal<'_> for PlantHal {
let adc_config = esp_idf_hal::adc::config::Config {
resolution: esp_idf_hal::adc::config::Resolution::Resolution12Bit,
calibration: true,
calibration: true
};
let tank_driver = AdcDriver::new(peripherals.adc1, &adc_config)?;
let tank_channel: AdcChannelDriver<'_, { attenuation::DB_11 }, Gpio5> =
AdcChannelDriver::new(peripherals.pins.gpio5)?;
let mut solar_is_day = PinDriver::input(peripherals.pins.gpio8)?;
let mut solar_is_day = PinDriver::input(peripherals.pins.gpio8.downgrade())?;
solar_is_day.set_pull(Pull::Floating)?;
let mut boot_button = PinDriver::input(peripherals.pins.gpio9)?;
let mut boot_button = PinDriver::input(peripherals.pins.gpio9.downgrade())?;
boot_button.set_pull(Pull::Floating)?;
let mut light = PinDriver::input_output(peripherals.pins.gpio10)?;
let mut light = PinDriver::input_output(peripherals.pins.gpio10.downgrade())?;
light.set_pull(Pull::Floating).unwrap();
let mut main_pump = PinDriver::input_output(peripherals.pins.gpio2)?;
let mut main_pump = PinDriver::input_output(peripherals.pins.gpio2.downgrade())?;
main_pump.set_pull(Pull::Floating)?;
main_pump.set_low()?;
let mut tank_power = PinDriver::input_output(peripherals.pins.gpio11)?;
let mut tank_power = PinDriver::input_output(peripherals.pins.gpio11.downgrade())?;
tank_power.set_pull(Pull::Floating)?;
let mut general_fault = PinDriver::input_output(peripherals.pins.gpio6)?;
let mut general_fault = PinDriver::input_output(peripherals.pins.gpio6.downgrade())?;
general_fault.set_pull(Pull::Floating)?;
general_fault.set_low()?;
let one_wire_bus = OneWire::new(one_wire_pin)