@@ -13,6 +13,73 @@ use embassy_imxrt::peripherals::ESPI;
1313use espi_service:: espi_service;
1414use { defmt_rtt as _, panic_probe as _} ;
1515
16+ // Mock battery service
17+ mod battery_service {
18+ use defmt:: info;
19+ use embassy_sync:: blocking_mutex:: raw:: NoopRawMutex ;
20+ use embassy_sync:: once_lock:: OnceLock ;
21+ use embassy_sync:: signal:: Signal ;
22+ use embedded_services:: comms:: { self , EndpointID , External , Internal } ;
23+
24+ struct Service {
25+ endpoint : comms:: Endpoint ,
26+
27+ // This is can be an Embassy signal or channel or whatever Embassy async notification construct
28+ signal : Signal < NoopRawMutex , espi_service:: BatteryMessage > ,
29+ }
30+
31+ impl Service {
32+ fn new ( ) -> Self {
33+ Service {
34+ endpoint : comms:: Endpoint :: uninit ( EndpointID :: Internal ( Internal :: Battery ) ) ,
35+ signal : Signal :: new ( ) ,
36+ }
37+ }
38+ }
39+
40+ impl comms:: MailboxDelegate for Service {
41+ fn receive ( & self , message : & comms:: Message ) {
42+ if let Some ( msg) = message. data . get :: < espi_service:: BatteryMessage > ( ) {
43+ self . signal . signal ( * msg) ;
44+ }
45+ }
46+ }
47+
48+ static BATTERY_SERVICE : OnceLock < Service > = OnceLock :: new ( ) ;
49+
50+ // Initialize battery service
51+ pub async fn init ( ) {
52+ let battery_service = BATTERY_SERVICE . get_or_init ( Service :: new) ;
53+
54+ comms:: register_endpoint ( battery_service, & battery_service. endpoint )
55+ . await
56+ . unwrap ( ) ;
57+ }
58+
59+ // Service to update the battery value in the memory map periodically
60+ #[ embassy_executor:: task]
61+ pub async fn battery_update_service ( ) {
62+ let battery_service = BATTERY_SERVICE . get ( ) . await ;
63+
64+ let mut battery_remain_cap = u32:: MAX ;
65+
66+ loop {
67+ battery_service
68+ . endpoint
69+ . send (
70+ EndpointID :: External ( External :: Host ) ,
71+ & espi_service:: BatteryMessage :: RemainCap ( battery_remain_cap) ,
72+ )
73+ . await
74+ . unwrap ( ) ;
75+ info ! ( "Sending updated battery status to espi service" ) ;
76+ battery_remain_cap -= 1 ;
77+
78+ embassy_time:: Timer :: after_secs ( 1 ) . await ;
79+ }
80+ }
81+ }
82+
1683bind_interrupts ! ( struct Irqs {
1784 ESPI => InterruptHandler <ESPI >;
1885} ) ;
@@ -76,6 +143,10 @@ async fn main(spawner: Spawner) {
76143
77144 spawner. must_spawn ( espi_service:: espi_service ( espi, memory_map_buffer) ) ;
78145
146+ battery_service:: init ( ) . await ;
147+
148+ spawner. spawn ( battery_service:: battery_update_service ( ) ) . unwrap ( ) ;
149+
79150 loop {
80151 embassy_time:: Timer :: after_secs ( 10 ) . await ;
81152 info ! ( "The uptime is {} secs" , embassy_time:: Instant :: now( ) . as_secs( ) ) ;
0 commit comments