@@ -158,12 +158,9 @@ mod async_io {
158
158
///
159
159
/// # Safety
160
160
///
161
- /// The `_token` enforces single use of gpios. Note that this makes it impossible to wait for
162
- /// more than one GPIO.
163
- ///
161
+ /// Safety of multiple GPIOs depends on the underlying controller.
164
162
pub unsafe fn wait_for_high (
165
163
& mut self ,
166
- _token : & mut GpioToken ,
167
164
) -> impl Future < Output = ( ) > + use < ' _ > {
168
165
GpioWait :: new ( self , 1 )
169
166
}
@@ -172,11 +169,9 @@ mod async_io {
172
169
///
173
170
/// # Safety
174
171
///
175
- /// The `_token` enforces single use of gpios. Note that this makes it impossible to wait
176
- /// for more than one GPIO.
172
+ /// Safety of multiple GPIOs depends on the underlying controller.
177
173
pub unsafe fn wait_for_low (
178
174
& mut self ,
179
- _token : & mut GpioToken ,
180
175
) -> impl Future < Output = ( ) > + use < ' _ > {
181
176
GpioWait :: new ( self , 0 )
182
177
}
@@ -245,35 +240,6 @@ mod async_io {
245
240
246
241
pub ( crate ) use async_io:: * ;
247
242
248
- /// Global instance to help make gpio in Rust slightly safer.
249
- ///
250
- /// # Safety
251
- ///
252
- /// To help with safety, the rust types use a global instance of a gpio-token. Methods will
253
- /// take a mutable reference to this, which will require either a single thread in the
254
- /// application code, or something like a mutex or critical section to manage. The operation
255
- /// methods are still unsafe, because we have no control over what happens with the gpio
256
- /// operations outside of Rust code, but this will help make the Rust usage at least better.
257
- pub struct GpioToken ( ( ) ) ;
258
-
259
- static GPIO_TOKEN : Unique = Unique :: new ( ) ;
260
-
261
- impl GpioToken {
262
- /// Retrieves the gpio token.
263
- ///
264
- /// # Safety
265
- /// This is unsafe because lots of code in zephyr operates on the gpio drivers. The user of the
266
- /// gpio subsystem, in general should either coordinate all gpio access across the system (the
267
- /// token coordinates this only within Rust code), or verify that the particular gpio driver and
268
- /// methods are thread safe.
269
- pub unsafe fn get_instance ( ) -> Option < GpioToken > {
270
- if !GPIO_TOKEN . once ( ) {
271
- return None ;
272
- }
273
- Some ( GpioToken ( ( ) ) )
274
- }
275
- }
276
-
277
243
/// A single instance of a zephyr device to manage a gpio controller. A gpio controller
278
244
/// represents a set of gpio pins, that are generally operated on by the same hardware block.
279
245
pub struct Gpio {
@@ -312,9 +278,7 @@ impl Gpio {
312
278
313
279
/// A GpioPin represents a single pin on a gpio device.
314
280
///
315
- /// This is a lightweight wrapper around the Zephyr `gpio_dt_spec` structure. Note that
316
- /// multiple pins may share a gpio controller, and as such, all methods on this are both unsafe,
317
- /// and require a mutable reference to the [`GpioToken`].
281
+ /// This is a lightweight wrapper around the Zephyr `gpio_dt_spec` structure.
318
282
#[ allow( dead_code) ]
319
283
pub struct GpioPin {
320
284
pub ( crate ) pin : raw:: gpio_dt_spec ,
@@ -366,10 +330,8 @@ impl GpioPin {
366
330
///
367
331
/// # Safety
368
332
///
369
- /// The `_token` enforces single threaded use of gpios from Rust code. However, many drivers
370
- /// within Zephyr use GPIOs, and to use gpios safely, the caller must ensure that there is
371
- /// either not simultaneous use, or the gpio driver in question is thread safe.
372
- pub unsafe fn configure ( & mut self , _token : & mut GpioToken , extra_flags : raw:: gpio_flags_t ) {
333
+ /// Concurrency safety is determined by the underlying driver.
334
+ pub fn configure ( & mut self , extra_flags : raw:: gpio_flags_t ) {
373
335
// TODO: Error?
374
336
unsafe {
375
337
raw:: gpio_pin_configure (
@@ -384,27 +346,29 @@ impl GpioPin {
384
346
///
385
347
/// # Safety
386
348
///
387
- /// The `_token` enforces single threaded use of gpios from Rust code. However, many drivers
388
- /// within Zephyr use GPIOs, and to use gpios safely, the caller must ensure that there is
389
- /// either not simultaneous use, or the gpio driver in question is thread safe.
390
- pub unsafe fn toggle_pin ( & mut self , _token : & mut GpioToken ) {
349
+ /// Concurrency safety is determined by the underlying driver.
350
+ pub fn toggle_pin ( & mut self ) {
391
351
// TODO: Error?
392
352
unsafe {
393
353
raw:: gpio_pin_toggle_dt ( & self . pin ) ;
394
354
}
395
355
}
396
356
397
357
/// Set the logical level of the pin.
398
- pub unsafe fn set ( & mut self , _token : & mut GpioToken , value : bool ) {
399
- raw:: gpio_pin_set_dt ( & self . pin , value as c_int ) ;
358
+ pub fn set ( & mut self , value : bool ) {
359
+ unsafe {
360
+ raw:: gpio_pin_set_dt ( & self . pin , value as c_int ) ;
361
+ }
400
362
}
401
363
402
364
/// Read the logical level of the pin.
403
- pub unsafe fn get ( & mut self , _token : & mut GpioToken ) -> bool {
404
- match raw:: gpio_pin_get_dt ( & self . pin ) {
405
- 0 => false ,
406
- 1 => true ,
407
- _ => panic ! ( "TODO: Handle gpio get error" ) ,
365
+ pub fn get ( & mut self ) -> bool {
366
+ unsafe {
367
+ match raw:: gpio_pin_get_dt ( & self . pin ) {
368
+ 0 => false ,
369
+ 1 => true ,
370
+ _ => panic ! ( "TODO: Handle gpio get error" ) ,
371
+ }
408
372
}
409
373
}
410
374
}
0 commit comments