@@ -55,14 +55,69 @@ use mpu6886::Mpu6886;
5555use spooky_core:: engine:: Engine ;
5656use shared_bus:: BusManagerSimple ;
5757
58- use embedded_hal:: digital:: v2:: OutputPin ;
58+ use embedded_hal:: {
59+ digital:: v2:: OutputPin ,
60+ blocking:: i2c:: { Read , Write , WriteRead }
61+ } ;
5962
60- use axp192:: { I2CPowerManagementInterface , Axp192 } ;
63+ use axp192:: { Axp192 } ;
6164
6265pub struct Universe < D > {
6366 pub engine : Engine < D > ,
6467}
6568
69+ // Source: https://github.com/rcloran/axp192-rs/blob/main/examples/m5stack-core2.rs
70+ fn m5sc2_init < I2C , E > ( axp : & mut axp192:: Axp192 < I2C > , delay : & mut Delay ) -> Result < ( ) , E >
71+ where
72+ I2C : Read < Error = E >
73+ + Write < Error = E >
74+ + WriteRead < Error = E > ,
75+ {
76+ // Default setup for M5Stack Core 2
77+ axp. set_dcdc1_voltage ( 3350 ) ?; // Voltage to provide to the microcontroller (this one!)
78+
79+ axp. set_ldo2_voltage ( 3300 ) ?; // Peripherals (LCD, ...)
80+ axp. set_ldo2_on ( true ) ?;
81+
82+ axp. set_ldo3_voltage ( 2000 ) ?; // Vibration motor
83+ axp. set_ldo3_on ( false ) ?;
84+
85+ axp. set_dcdc3_voltage ( 2800 ) ?; // LCD backlight
86+ axp. set_dcdc3_on ( true ) ?;
87+
88+ axp. set_gpio1_mode ( axp192:: GpioMode12 :: NmosOpenDrainOutput ) ?; // Power LED
89+ axp. set_gpio1_output ( false ) ?; // In open drain modes, state is opposite to what you might
90+ // expect
91+
92+ axp. set_gpio2_mode ( axp192:: GpioMode12 :: NmosOpenDrainOutput ) ?; // Speaker
93+ axp. set_gpio2_output ( true ) ?;
94+
95+ axp. set_key_mode (
96+ // Configure how the power button press will work
97+ axp192:: ShutdownDuration :: Sd4s ,
98+ axp192:: PowerOkDelay :: Delay64ms ,
99+ true ,
100+ axp192:: LongPress :: Lp1000ms ,
101+ axp192:: BootTime :: Boot512ms ,
102+ ) ?;
103+
104+ axp. set_gpio4_mode ( axp192:: GpioMode34 :: NmosOpenDrainOutput ) ?; // LCD reset control
105+
106+ axp. set_battery_voltage_adc_enable ( true ) ?;
107+ axp. set_battery_current_adc_enable ( true ) ?;
108+ axp. set_acin_current_adc_enable ( true ) ?;
109+ axp. set_acin_voltage_adc_enable ( true ) ?;
110+
111+ // Actually reset the LCD
112+ axp. set_gpio4_output ( false ) ?;
113+ axp. set_ldo3_on ( true ) ?; // Buzz the vibration motor while intializing ¯\_(ツ)_/¯
114+ delay. delay_ms ( 100u32 ) ;
115+ axp. set_gpio4_output ( true ) ?;
116+ axp. set_ldo3_on ( false ) ?;
117+ delay. delay_ms ( 100u32 ) ;
118+
119+ Ok ( ( ) )
120+ }
66121
67122#[ entry]
68123fn main ( ) -> ! {
@@ -93,11 +148,10 @@ fn main() -> ! {
93148
94149 let bus = BusManagerSimple :: new ( i2c_bus) ;
95150
96- let axp_interface = I2CPowerManagementInterface :: new ( bus. acquire_i2c ( ) ) ;
97- let mut axp = Axp192 :: new ( axp_interface) ;
98- axp. init ( ) . unwrap ( ) ;
151+ let mut axp = axp192:: Axp192 :: new ( bus. acquire_i2c ( ) ) ;
99152
100153 // M5Stack CORE 2 - https://docs.m5stack.com/en/core/core2
154+ m5sc2_init ( & mut axp, & mut delay) . unwrap ( ) ;
101155
102156 let lcd_sclk = io. pins . gpio18 ;
103157 let lcd_mosi = io. pins . gpio23 ;
0 commit comments