@@ -54,6 +54,7 @@ pub struct PinConfig {
5454 direction : Option < Direction > ,
5555 value : Option < bool > ,
5656 name : Option < String > ,
57+ pint : Option < usize > ,
5758}
5859
5960#[ derive( Copy , Clone , Debug , Default , Deserialize ) ]
@@ -98,6 +99,53 @@ pub enum Opendrain {
9899 Opendrain ,
99100}
100101
102+ #[ derive( Copy , Clone , Debug , Deserialize , PartialEq ) ]
103+ #[ serde( rename_all = "lowercase" ) ]
104+ #[ repr( u32 ) ]
105+ pub enum PintSlot {
106+ None = 8 ,
107+ Slot0 = 0 ,
108+ Slot1 = 1 ,
109+ Slot2 = 2 ,
110+ Slot3 = 3 ,
111+ Slot4 = 4 ,
112+ Slot5 = 5 ,
113+ Slot6 = 6 ,
114+ Slot7 = 7 ,
115+ }
116+
117+ impl From < u32 > for PintSlot {
118+ fn from ( slot : u32 ) -> Self {
119+ match slot {
120+ 0 => PintSlot :: Slot0 ,
121+ 1 => PintSlot :: Slot1 ,
122+ 2 => PintSlot :: Slot2 ,
123+ 3 => PintSlot :: Slot3 ,
124+ 4 => PintSlot :: Slot4 ,
125+ 5 => PintSlot :: Slot5 ,
126+ 6 => PintSlot :: Slot6 ,
127+ 7 => PintSlot :: Slot7 ,
128+ _ => PintSlot :: None ,
129+ }
130+ }
131+ }
132+
133+ impl From < usize > for PintSlot {
134+ fn from ( slot : usize ) -> Self {
135+ match slot {
136+ 0 => PintSlot :: Slot0 ,
137+ 1 => PintSlot :: Slot1 ,
138+ 2 => PintSlot :: Slot2 ,
139+ 3 => PintSlot :: Slot3 ,
140+ 4 => PintSlot :: Slot4 ,
141+ 5 => PintSlot :: Slot5 ,
142+ 6 => PintSlot :: Slot6 ,
143+ 7 => PintSlot :: Slot7 ,
144+ _ => PintSlot :: None ,
145+ }
146+ }
147+ }
148+
101149#[ derive( Copy , Clone , Debug , Deserialize ) ]
102150#[ serde( rename_all = "lowercase" ) ]
103151pub enum Direction {
@@ -113,6 +161,25 @@ impl PinConfig {
113161
114162 self . alt
115163 }
164+
165+ fn get_pint_slot ( & self , used : & mut u8 ) -> PintSlot {
166+ if let Some ( pint_slot) = self . pint {
167+ if self . pin . port > 1 || self . pin . pin > 32 {
168+ panic ! ( "Invalid gpio pin for interrupt" ) ;
169+ }
170+ if pint_slot > 7 {
171+ panic ! ( "Invalid pint setting {} > 7" , pint_slot) ;
172+ }
173+ let mask = 1 << pint_slot;
174+ if ( * used & mask) != 0 {
175+ panic ! ( "Duplicate interrupt slot assignment: {:?}" , self . pin) ;
176+ }
177+ * used |= mask;
178+ PintSlot :: from ( pint_slot)
179+ } else {
180+ PintSlot :: None
181+ }
182+ }
116183}
117184
118185impl ToTokens for PinConfig {
@@ -140,21 +207,27 @@ impl ToTokens for PinConfig {
140207pub fn codegen ( pins : Vec < PinConfig > ) -> Result < ( ) > {
141208 let out_dir = build_util:: out_dir ( ) ;
142209 let dest_path = out_dir. join ( "pin_config.rs" ) ;
143- let mut file = std:: fs:: File :: create ( dest_path) ?;
144-
210+ let mut file = std:: fs:: File :: create ( & dest_path) ?;
145211 let mut buf = BufWriter :: new ( Vec :: new ( ) ) ;
212+ let mut used_slots = 0u8 ;
213+
146214 if pins. iter ( ) . any ( |p| p. name . is_some ( ) ) {
147- writeln ! ( & mut buf , "use drv_lpc55_gpio_api::Pin;" ) ?;
215+ writeln ! ( & mut file , "use drv_lpc55_gpio_api::Pin;" ) ?;
148216 }
149217 writeln ! (
150218 & mut file,
151- "fn setup_pins(task : TaskId) -> Result<(), ()> {{"
219+ "fn setup_pins(task : userlib:: TaskId) -> Result<(), ()> {{"
152220 ) ?;
153221 writeln ! ( & mut file, "use drv_lpc55_gpio_api::*;" ) ?;
154222 writeln ! ( & mut file, "let iocon = Pins::from(task);" ) ?;
155223 for p in pins {
156224 writeln ! ( & mut file, "iocon.iocon_configure(" ) ?;
157225 writeln ! ( & mut file, "{}" , p. to_token_stream( ) ) ?;
226+ writeln ! (
227+ & mut file,
228+ "PintSlot::{:?}" ,
229+ p. get_pint_slot( & mut used_slots)
230+ ) ?;
158231 writeln ! ( & mut file, ");" ) ?;
159232
160233 // Output pins can specify their value, which is set before configuring
@@ -183,21 +256,38 @@ pub fn codegen(pins: Vec<PinConfig>) -> Result<()> {
183256 }
184257 match p. name {
185258 None => ( ) ,
186- Some ( name) => {
259+ Some ( ref name) => {
187260 let pin = p. pin . get_port_pin ( ) ;
188261 writeln ! ( & mut buf, "#[allow(unused)]" ) ?;
189262 writeln ! (
190263 & mut buf,
191264 "const {}: Pin = Pin::PIO{}_{};" ,
192- name, pin. 0 , pin. 1
265+ & name, pin. 0 , pin. 1
193266 ) ?;
267+
268+ let mut ignore = 0u8 ;
269+ let slot = p. get_pint_slot ( & mut ignore) ;
270+ if slot != PintSlot :: None {
271+ writeln ! ( & mut buf, "#[allow(unused)]" ) ?;
272+ writeln ! (
273+ & mut buf,
274+ "pub const {}_PINT_MASK: u32 = 1 << {};" ,
275+ & name, slot as u32
276+ ) ?;
277+ }
194278 }
195279 }
196280 }
197281
198282 writeln ! ( & mut file, "Ok(())" ) ?;
199283 writeln ! ( & mut file, "}}" ) ?;
200- write ! ( file, "{}" , String :: from_utf8( buf. into_inner( ) ?) . unwrap( ) ) ?;
284+
285+ write ! (
286+ & mut file,
287+ "{}" ,
288+ String :: from_utf8( buf. into_inner( ) ?) . unwrap( )
289+ ) ?;
290+ call_rustfmt:: rustfmt ( & dest_path) ?;
201291
202292 Ok ( ( ) )
203293}
0 commit comments