From cdf900a5b6abd1e0c8807a5287159189cfb5816b Mon Sep 17 00:00:00 2001 From: linuswck Date: Tue, 16 Jan 2024 17:36:16 +0800 Subject: [PATCH] gpio: Add AD7172 phy gpio init - Will not function on rev0_2 kirdy due to wrong hardware connection --- src/device/boot.rs | 3 ++- src/device/gpio.rs | 21 +++++++++++++++++++-- src/thermostat/ad7172/adc.rs | 21 ++++++++++++++++++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/device/boot.rs b/src/device/boot.rs index e0f05b0..1d4a496 100644 --- a/src/device/boot.rs +++ b/src/device/boot.rs @@ -41,7 +41,7 @@ pub fn bootup( sys_timer::setup(core_perif.SYST, clocks); - let (_eth_pins, usb, current_source_phy, max1968_phy) = gpio::setup( + let (_eth_pins, usb, current_source_phy, _ad7172_phy, max1968_phy) = gpio::setup( clocks, perif.TIM4, perif.GPIOA, @@ -51,6 +51,7 @@ pub fn bootup( perif.GPIOG, perif.SPI1, perif.SPI2, + perif.SPI3, perif.OTG_FS_GLOBAL, perif.OTG_FS_DEVICE, perif.OTG_FS_PWRCLK, diff --git a/src/device/gpio.rs b/src/device/gpio.rs index ffb8374..794578a 100644 --- a/src/device/gpio.rs +++ b/src/device/gpio.rs @@ -2,12 +2,13 @@ use crate::laser_diode::current_sources::{self, CurrentSourcePhy}; use crate::laser_diode::max5719; use crate::thermostat::ad5680; use crate::thermostat::max1968::{self, MAX1968PinSet, MAX1968Phy, PWM_FREQ_KHZ}; +use crate::thermostat::ad7172; use stm32_eth::EthPins; use stm32f4xx_hal::{ gpio::{gpioa::*, gpiob::*, gpioc::*, gpiog::*, GpioExt, Input}, otg_fs::USB, pac::{ - GPIOA, GPIOB, GPIOC, GPIOD, GPIOG, OTG_FS_DEVICE, OTG_FS_GLOBAL, OTG_FS_PWRCLK, SPI1, SPI2, + GPIOA, GPIOB, GPIOC, GPIOD, GPIOG, OTG_FS_DEVICE, OTG_FS_GLOBAL, OTG_FS_PWRCLK, SPI1, SPI2, SPI3, TIM4, }, rcc::Clocks, @@ -28,6 +29,7 @@ pub fn setup( gpiog: GPIOG, spi1: SPI1, spi2: SPI2, + spi3: SPI3, otg_fs_global: OTG_FS_GLOBAL, otg_fs_device: OTG_FS_DEVICE, otg_fs_pwrclk: OTG_FS_PWRCLK, @@ -35,6 +37,7 @@ pub fn setup( EthernetPins, USB, CurrentSourcePhy, + ad7172::AdcPhy, MAX1968Phy, // thermostat_phy ) { @@ -118,5 +121,19 @@ pub fn setup( max_i_neg: max_i_neg0, }); - (eth_pins, usb, current_source_phy, max1968_phy) + let ad7172_phy = ad7172::Adc::new( + Spi::new(spi3, + ( + gpioc.pc10.into_alternate(), + gpioc.pc11.into_alternate(), + gpioc.pc12.into_alternate(), + ), + ad7172::SPI_MODE, + ad7172::SPI_CLOCK_MHZ.convert(), + &clocks + ), + gpiod.pd0.into_push_pull_output(), + ).unwrap(); + + (eth_pins, usb, current_source_phy, ad7172_phy, max1968_phy) } diff --git a/src/thermostat/ad7172/adc.rs b/src/thermostat/ad7172/adc.rs index 68ebab3..f4e9cf1 100644 --- a/src/thermostat/ad7172/adc.rs +++ b/src/thermostat/ad7172/adc.rs @@ -1,8 +1,14 @@ use core::fmt; use log::{info, warn}; -use stm32f4xx_hal::hal::{ - blocking::spi::Transfer, - digital::v2::OutputPin, +use stm32f4xx_hal:: + { + spi::{Spi, TransferModeNormal}, + pac::SPI3, + gpio::{PC10, PC11, PC12, PD0, Alternate, Output, PushPull}, + hal::{ + blocking::spi::Transfer, + digital::v2::OutputPin, + }, }; use uom::si::{ f64::ElectricPotential, @@ -23,7 +29,16 @@ pub struct Adc, NSS: OutputPin> { checksum_mode: ChecksumMode, } +type AdcSpi = Spi>, PC11>, PC12>), TransferModeNormal>; +type AdcNss = PD0>; +pub type AdcPhy = Adc; + impl, NSS: OutputPin, E: fmt::Debug> Adc { + #[deprecated(note= + "To be removed when rev0_3 has arrived. + Rev0_2 has wrong SPI hardware connection bug. + AD7172-2 is not accessible. + ")] pub fn new(spi: SPI, mut nss: NSS) -> Result { let _ = nss.set_high(); let mut adc = Adc {