capsules_core/driver.rs
1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Mapping of capsules to their syscall driver number.
6
7use enum_primitive::cast::FromPrimitive;
8use enum_primitive::enum_from_primitive;
9
10/// Create a `DRIVER_NUM` for a second (or third, etc.) instance of a
11/// [`SyscallDriver`](kernel::syscall::SyscallDriver).
12///
13/// Creating additional driver numbers for known drivers uses the second most
14/// significant nibble of the 32-bit driver number. This allows creating up to
15/// 15 additional driver numbers for the same driver. The original driver of
16/// course has 0 in this field.
17///
18/// The format is like this:
19///
20/// ```text
21/// 32 28 24 0
22/// ┌──────────┬──────────┬────────────────────────────────────────┐
23/// │ 0 │ Instance │ Driver Number │
24/// │ │ Number │ │
25/// └──────────┴──────────┴────────────────────────────────────────┘
26/// ```
27pub const fn dup_driver_num(driver_num: usize, instance_num: usize) -> usize {
28 assert!(
29 instance_num < 16,
30 "This mechanism only supports up to 15 additional driver numbers."
31 );
32 assert!(
33 (driver_num & 0x0f00_0000) == 0,
34 "Must use the base driver number to create a duplicate"
35 );
36 assert!(
37 instance_num > 0,
38 "This mechanism is for new instance indices above the base driver number."
39 );
40
41 (instance_num << 24) | driver_num
42}
43
44enum_from_primitive! {
45#[derive(Debug, PartialEq)]
46// syscall driver numbers
47pub enum NUM {
48 // Base
49 Alarm = 0x00000,
50 Console = 0x00001,
51 Led = 0x00002,
52 Button = 0x00003,
53 Gpio = 0x00004,
54 Adc = 0x00005,
55 Dac = 0x00006,
56 AnalogComparator = 0x00007,
57 LowLevelDebug = 0x00008,
58 ReadOnlyState = 0x00009,
59 Pwm = 0x00010,
60
61 // Kernel
62 Ipc = 0x10000,
63 AppLoader = 0x10001,
64 ProcessInfo = 0x10002,
65
66 // HW Buses
67 Spi = 0x20001,
68 SpiPeripheral = 0x20002,
69 I2cMaster = 0x20003,
70 UsbUser = 0x20005,
71 I2cMasterSlave = 0x20006,
72 Can = 0x20007,
73
74 // Networking
75 BleAdvertising = 0x30000,
76 Ieee802154 = 0x30001,
77 Udp = 0x30002,
78 LoRaPhySPI = 0x30003,
79 LoRaPhyGPIO = 0x30004,
80 Thread = 0x30005,
81 Eui64 = 0x30006,
82 EthernetTap = 0x30007,
83 Wifi = 0x30008,
84
85 // Cryptography
86 Rng = 0x40001,
87 Crc = 0x40002,
88 Hmac = 0x40003,
89 CtapHid = 0x40004,
90 Sha = 0x40005,
91 Aes = 0x40006,
92
93 // Storage
94 AppFlash = 0x50000,
95 NvmStorage = 0x50001,
96 SdCard = 0x50002,
97 Kv = 0x50003,
98 IsolatedNvmStorage = 0x50004,
99
100 // Sensors
101 Temperature = 0x60000,
102 Humidity = 0x60001,
103 AmbientLight = 0x60002,
104 NINEDOF = 0x60004,
105 Proximity = 0x60005,
106 SoundPressure = 0x60006,
107 AirQuality = 0x60007,
108 Pressure = 0x60008,
109 Distance = 0x60009,
110 Moisture = 0x6000A,
111 RainFall = 0x6000B,
112
113 // Sensor ICs
114 Tsl2561 = 0x70000,
115 Tmp006 = 0x70001,
116 Lps25hb = 0x70004,
117 L3gd20 = 0x70005,
118 Lsm303dlch = 0x70006,
119 Mlx90614 = 0x70007,
120 Lsm6dsoxtr = 0x70008,
121
122 // Other ICs
123 Ltc294x = 0x80000,
124 Max17205 = 0x80001,
125 Pca9544a = 0x80002,
126 GpioAsync = 0x80003,
127 Nrf51822Serialization = 0x80004,
128
129 // Misc
130 Buzzer = 0x90000,
131 Screen = 0x90001,
132 Touch = 0x90002,
133 TextScreen = 0x90003,
134 SevenSegment = 0x90004,
135 KeyboardHid = 0x90005,
136 DateTime = 0x90007,
137 CycleCount = 0x90008,
138 Servo = 0x90009,
139
140 // Testing
141 SyscallReturnTest = 0xB0000,
142}
143}