@@ -37,7 +37,7 @@ mod async_io {
37
37
38
38
use crate :: sync:: atomic:: { AtomicBool , AtomicU32 } ;
39
39
40
- use super :: { GpioPin , GpioToken } ;
40
+ use super :: GpioPin ;
41
41
42
42
pub ( crate ) struct GpioStatic {
43
43
/// The wakers for each of the gpios.
@@ -158,26 +158,17 @@ 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
- ///
164
- pub unsafe fn wait_for_high (
165
- & mut self ,
166
- _token : & mut GpioToken ,
167
- ) -> impl Future < Output = ( ) > + use < ' _ > {
161
+ /// Safety of multiple GPIOs depends on the underlying controller.
162
+ pub unsafe fn wait_for_high ( & mut self ) -> impl Future < Output = ( ) > + use < ' _ > {
168
163
GpioWait :: new ( self , 1 )
169
164
}
170
165
171
166
/// Asynchronously wait for a gpio pin to become low.
172
167
///
173
168
/// # Safety
174
169
///
175
- /// The `_token` enforces single use of gpios. Note that this makes it impossible to wait
176
- /// for more than one GPIO.
177
- pub unsafe fn wait_for_low (
178
- & mut self ,
179
- _token : & mut GpioToken ,
180
- ) -> impl Future < Output = ( ) > + use < ' _ > {
170
+ /// Safety of multiple GPIOs depends on the underlying controller.
171
+ pub unsafe fn wait_for_low ( & mut self ) -> impl Future < Output = ( ) > + use < ' _ > {
181
172
GpioWait :: new ( self , 0 )
182
173
}
183
174
}
@@ -245,35 +236,6 @@ mod async_io {
245
236
246
237
pub ( crate ) use async_io:: * ;
247
238
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
239
/// A single instance of a zephyr device to manage a gpio controller. A gpio controller
278
240
/// represents a set of gpio pins, that are generally operated on by the same hardware block.
279
241
pub struct Gpio {
@@ -312,9 +274,7 @@ impl Gpio {
312
274
313
275
/// A GpioPin represents a single pin on a gpio device.
314
276
///
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`].
277
+ /// This is a lightweight wrapper around the Zephyr `gpio_dt_spec` structure.
318
278
#[ allow( dead_code) ]
319
279
pub struct GpioPin {
320
280
pub ( crate ) pin : raw:: gpio_dt_spec ,
@@ -366,10 +326,8 @@ impl GpioPin {
366
326
///
367
327
/// # Safety
368
328
///
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 ) {
329
+ /// Concurrency safety is determined by the underlying driver.
330
+ pub fn configure ( & mut self , extra_flags : raw:: gpio_flags_t ) {
373
331
// TODO: Error?
374
332
unsafe {
375
333
raw:: gpio_pin_configure (
@@ -384,27 +342,29 @@ impl GpioPin {
384
342
///
385
343
/// # Safety
386
344
///
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 ) {
345
+ /// Concurrency safety is determined by the underlying driver.
346
+ pub fn toggle_pin ( & mut self ) {
391
347
// TODO: Error?
392
348
unsafe {
393
349
raw:: gpio_pin_toggle_dt ( & self . pin ) ;
394
350
}
395
351
}
396
352
397
353
/// 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 ) ;
354
+ pub fn set ( & mut self , value : bool ) {
355
+ unsafe {
356
+ raw:: gpio_pin_set_dt ( & self . pin , value as c_int ) ;
357
+ }
400
358
}
401
359
402
360
/// 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" ) ,
361
+ pub fn get ( & mut self ) -> bool {
362
+ unsafe {
363
+ match raw:: gpio_pin_get_dt ( & self . pin ) {
364
+ 0 => false ,
365
+ 1 => true ,
366
+ _ => panic ! ( "TODO: Handle gpio get error" ) ,
367
+ }
408
368
}
409
369
}
410
370
}
0 commit comments