1 use super::{driver::Driver, Device, DeviceState, IdTable}; 2 use crate::libs::spinlock::SpinLock; 3 use alloc::{collections::BTreeMap, sync::Arc}; 4 use core::fmt::Debug; 5 use lazy_static::lazy_static; 6 7 /// @brief: 总线状态 8 #[derive(Debug, Copy, Clone)] 9 pub enum BusState { 10 NotInitialized = 0, // 未初始化 11 Initialized = 1, // 已初始化 12 UnDefined = 2, // 未定义的 13 } 14 15 /// @brief: 将u32类型转换为总线状态类型 16 impl From<u32> for BusState { 17 fn from(state: u32) -> Self { 18 match state { 19 0 => BusState::NotInitialized, 20 1 => BusState::Initialized, 21 _ => BusState::UnDefined, 22 } 23 } 24 } 25 26 /// @brief: 将总线状态类型转换为u32类型 27 impl From<DeviceState> for BusState { 28 fn from(state: DeviceState) -> Self { 29 match state { 30 DeviceState::Initialized => BusState::Initialized, 31 DeviceState::NotInitialized => BusState::NotInitialized, 32 DeviceState::UnDefined => BusState::UnDefined, 33 } 34 } 35 } 36 37 /// @brief: 将总线状态类型转换为设备状态类型 38 impl From<BusState> for DeviceState { 39 fn from(state: BusState) -> Self { 40 match state { 41 BusState::Initialized => DeviceState::Initialized, 42 BusState::NotInitialized => DeviceState::NotInitialized, 43 BusState::UnDefined => DeviceState::UnDefined, 44 } 45 } 46 } 47 48 /// @brief: 总线驱动trait,所有总线驱动都应实现该trait 49 pub trait BusDriver: Driver { 50 /// @brief: 判断总线是否为空 51 /// @parameter: None 52 /// @return: 如果总线上设备和驱动的数量都为0,则返回true,否则,返回false 53 fn is_empty(&self) -> bool; 54 } 55 56 /// @brief: 总线设备trait,所有总线都应实现该trait 57 pub trait Bus: Device {} 58 59 /// @brief: 总线管理结构体 60 #[derive(Debug, Clone)] 61 pub struct BusManager { 62 buses: BTreeMap<IdTable, Arc<dyn Bus>>, // 总线设备表 63 bus_drvs: BTreeMap<IdTable, Arc<dyn BusDriver>>, // 总线驱动表 64 } 65 66 /// @brief: 总线管理结构体加锁 67 pub struct BusManagerLock(SpinLock<BusManager>); 68 69 /// @brief: 总线管理方法集 70 impl BusManagerLock { 71 /// @brief: 创建总线管理实例 72 /// @parameter: None 73 /// @return: 总线管理实例 74 #[inline] 75 #[allow(dead_code)] 76 pub fn new() -> Self { 77 BusManagerLock(SpinLock::new(BusManager { 78 buses: BTreeMap::new(), 79 bus_drvs: BTreeMap::new(), 80 })) 81 } 82 83 /// @brief: 添加总线 84 /// @parameter id_table: 总线标识符,用于唯一标识该总线 85 /// @parameter bus_dev: 总线实例 86 /// @return: None 87 #[inline] 88 #[allow(dead_code)] 89 pub fn add_bus(&self, id_table: IdTable, bus_dev: Arc<dyn Bus>) { 90 let mut bus_manager = self.0.lock(); 91 bus_manager.buses.insert(id_table, bus_dev); 92 } 93 94 /// @brief: 添加总线驱动 95 /// @parameter id_table: 总线驱动标识符,用于唯一标识该总线驱动 96 /// @parameter bus_dev: 总线驱动实例 97 /// @return: None 98 #[inline] 99 #[allow(dead_code)] 100 pub fn add_bus_driver(&self, id_table: IdTable, bus_drv: Arc<dyn BusDriver>) { 101 let mut bus_manager = self.0.lock(); 102 bus_manager.bus_drvs.insert(id_table, bus_drv); 103 } 104 105 /// @brief: 卸载总线 106 /// @parameter id_table: 总线标识符,用于唯一标识该总线 107 /// @return: None 108 #[inline] 109 #[allow(dead_code)] 110 pub fn remove_bus(&self, id_table: &IdTable) { 111 let mut bus_manager = self.0.lock(); 112 bus_manager.buses.remove(id_table); 113 } 114 115 /// @brief: 卸载总线驱动 116 /// @parameter id_table: 总线驱动标识符,用于唯一标识该总线驱动 117 /// @return: None 118 #[inline] 119 #[allow(dead_code)] 120 pub fn remove_bus_driver(&self, id_table: &IdTable) { 121 let mut bus_manager = self.0.lock(); 122 bus_manager.bus_drvs.remove(id_table); 123 } 124 125 /// @brief: 获取总线设备 126 /// @parameter id_table: 总线标识符,用于唯一标识该总线 127 /// @return: 总线设备实例 128 #[inline] 129 #[allow(dead_code)] 130 pub fn get_bus(&self, id_table: &IdTable) -> Option<Arc<dyn Bus>> { 131 let bus_manager = self.0.lock(); 132 bus_manager.buses.get(id_table).cloned() 133 } 134 135 /// @brief: 获取总线驱动 136 /// @parameter id_table: 总线驱动标识符,用于唯一标识该总线驱动 137 /// @return: 总线驱动实例 138 #[inline] 139 #[allow(dead_code)] 140 pub fn get_bus_driver(&self, id_table: &IdTable) -> Option<Arc<dyn BusDriver>> { 141 let bus_manager = self.0.lock(); 142 return bus_manager.bus_drvs.get(id_table).cloned(); 143 } 144 } 145 146 lazy_static! { 147 pub static ref BUS_MANAGER: Arc<BusManagerLock> = Arc::new(BusManagerLock::new()); 148 } 149