From dd91973f9b54cb6b766e73773c3cc2350eb5c293 Mon Sep 17 00:00:00 2001 From: Empire Phoenix Date: Wed, 1 May 2024 18:06:20 +0200 Subject: [PATCH] i2c adjust and pin downgrade --- rust/Cargo.toml | 6 ++-- rust/sdkconfig.defaults | 4 ++- rust/src/plant_hal.rs | 65 ++++++++++++++++++++--------------------- 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 46d1685..667f6ce 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -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" } diff --git a/rust/sdkconfig.defaults b/rust/sdkconfig.defaults index f9edd2d..6ae7e19 100644 --- a/rust/sdkconfig.defaults +++ b/rust/sdkconfig.defaults @@ -5,4 +5,6 @@ CONFIG_ESP_MAIN_TASK_STACK_SIZE=25000 # This allows to use 1 ms granuality for thread sleeps (10 ms by default). CONFIG_FREERTOS_HZ=1000 -CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y \ No newline at end of file +CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y +CONFIG_I2C_ENABLE_DEBUG_LOG=y +DEBUG_LEVEL=5 \ No newline at end of file diff --git a/rust/src/plant_hal.rs b/rust/src/plant_hal.rs index 1c7fde7..75b0df8 100644 --- a/rust/src/plant_hal.rs +++ b/rust/src/plant_hal.rs @@ -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>, mqtt_client: Option>, @@ -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); - - //esp!(unsafe { esp_idf_sys::i2c_set_timeout(i2c_port, 1)}).unwrap(); + println!("init i2c timeout is {}", timeout); + + loop { + unsafe { vTaskDelay(1) } + } + //esp!(unsafe { esp_idf_sys::i2c_set_timeout(i2c_port, 22)}).unwrap(); let mut battery_driver: Bq34z100g1Driver = 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)