@@ -49,9 +49,12 @@ mod prio {
49
49
use thread_priority:: * ;
50
50
51
51
pub fn realtime_priority ( ) -> Result < ( ) > {
52
+ let prio = ThreadPriorityValue :: try_from ( 10 )
53
+ . map_err ( |e| anyhow ! ( "Failed to choose realtime priority level 10: {e:?}" ) ) ?;
54
+
52
55
set_thread_priority_and_policy (
53
56
thread_native_id ( ) ,
54
- ThreadPriority :: Crossplatform ( ThreadPriorityValue :: try_from ( 10 ) . unwrap ( ) ) ,
57
+ ThreadPriority :: Crossplatform ( prio ) ,
55
58
ThreadSchedulePolicy :: Realtime ( RealtimeThreadSchedulePolicy :: Fifo ) ,
56
59
)
57
60
. map_err ( |e| anyhow ! ( "Failed to set up realtime priority {e:?}" ) )
@@ -237,10 +240,12 @@ fn turn_off_with_reason(
237
240
pwr_line : & LineHandle ,
238
241
discharge_line : & LineHandle ,
239
242
fail_state : & AtomicU8 ,
240
- ) {
241
- pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) . unwrap ( ) ;
242
- discharge_line. set_value ( DISCHARGE_LINE_ASSERTED ) . unwrap ( ) ;
243
+ ) -> Result < ( ) > {
244
+ pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) ? ;
245
+ discharge_line. set_value ( DISCHARGE_LINE_ASSERTED ) ? ;
243
246
fail_state. store ( reason as u8 , Ordering :: Relaxed ) ;
247
+
248
+ Ok ( ( ) )
244
249
}
245
250
246
251
/// Labgrid has a fixed assumption of how a REST based power port should work.
@@ -316,7 +321,7 @@ impl DutPwrThread {
316
321
// as well.
317
322
// Use a queue to notify the calling thread if the priority setup
318
323
// succeeded.
319
- let ( thread_res_tx , mut thread_res_rx ) = bounded ( 1 ) ;
324
+ let ( thread_tx , thread_rx ) = bounded ( 1 ) ;
320
325
321
326
// Spawn a high priority thread that handles the power status
322
327
// in a realtimey fashion.
@@ -331,24 +336,20 @@ impl DutPwrThread {
331
336
let mut volt_filter = MedianFilter :: < 4 > :: new ( ) ;
332
337
let mut curr_filter = MedianFilter :: < 4 > :: new ( ) ;
333
338
334
- let ( tick_weak, request, state) = match realtime_priority ( ) {
335
- Ok ( _) => {
336
- let tick = Arc :: new ( AtomicU32 :: new ( 0 ) ) ;
337
- let tick_weak = Arc :: downgrade ( & tick) ;
339
+ realtime_priority ( ) ?;
338
340
339
- let request = Arc :: new ( AtomicU8 :: new ( OutputRequest :: Idle as u8 ) ) ;
340
- let state = Arc :: new ( AtomicU8 :: new ( OutputState :: Off as u8 ) ) ;
341
+ let ( tick_weak, request, state) = {
342
+ let tick = Arc :: new ( AtomicU32 :: new ( 0 ) ) ;
343
+ let tick_weak = Arc :: downgrade ( & tick) ;
341
344
342
- thread_res_tx
343
- . try_send ( Ok ( ( tick, request. clone ( ) , state. clone ( ) ) ) )
344
- . unwrap ( ) ;
345
+ let request = Arc :: new ( AtomicU8 :: new ( OutputRequest :: Idle as u8 ) ) ;
346
+ let state = Arc :: new ( AtomicU8 :: new ( OutputState :: Off as u8 ) ) ;
345
347
346
- ( tick_weak, request, state)
347
- }
348
- Err ( e) => {
349
- thread_res_tx. try_send ( Err ( e) ) . unwrap ( ) ;
350
- panic ! ( )
351
- }
348
+ thread_tx
349
+ . try_send ( ( tick, request. clone ( ) , state. clone ( ) ) )
350
+ . expect ( "Queue that should be empty wasn't" ) ;
351
+
352
+ ( tick_weak, request, state)
352
353
} ;
353
354
354
355
// Run as long as there is a strong reference to `tick`.
@@ -383,7 +384,7 @@ impl DutPwrThread {
383
384
& pwr_line,
384
385
& discharge_line,
385
386
& state,
386
- ) ;
387
+ ) ? ;
387
388
} else {
388
389
// We have a fresh ADC value. Signal "everything is well"
389
390
// to the watchdog task.
@@ -420,7 +421,7 @@ impl DutPwrThread {
420
421
& pwr_line,
421
422
& discharge_line,
422
423
& state,
423
- ) ;
424
+ ) ? ;
424
425
425
426
continue ;
426
427
}
@@ -434,7 +435,7 @@ impl DutPwrThread {
434
435
& pwr_line,
435
436
& discharge_line,
436
437
& state,
437
- ) ;
438
+ ) ? ;
438
439
439
440
continue ;
440
441
}
@@ -447,7 +448,7 @@ impl DutPwrThread {
447
448
& pwr_line,
448
449
& discharge_line,
449
450
& state,
450
- ) ;
451
+ ) ? ;
451
452
452
453
continue ;
453
454
}
@@ -457,34 +458,30 @@ impl DutPwrThread {
457
458
match req {
458
459
OutputRequest :: Idle => { }
459
460
OutputRequest :: On => {
460
- discharge_line
461
- . set_value ( 1 - DISCHARGE_LINE_ASSERTED )
462
- . unwrap ( ) ;
463
- pwr_line. set_value ( PWR_LINE_ASSERTED ) . unwrap ( ) ;
461
+ discharge_line. set_value ( 1 - DISCHARGE_LINE_ASSERTED ) ?;
462
+ pwr_line. set_value ( PWR_LINE_ASSERTED ) ?;
464
463
state. store ( OutputState :: On as u8 , Ordering :: Relaxed ) ;
465
464
}
466
465
OutputRequest :: Off => {
467
- discharge_line. set_value ( DISCHARGE_LINE_ASSERTED ) . unwrap ( ) ;
468
- pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) . unwrap ( ) ;
466
+ discharge_line. set_value ( DISCHARGE_LINE_ASSERTED ) ? ;
467
+ pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) ? ;
469
468
state. store ( OutputState :: Off as u8 , Ordering :: Relaxed ) ;
470
469
}
471
470
OutputRequest :: OffFloating => {
472
- discharge_line
473
- . set_value ( 1 - DISCHARGE_LINE_ASSERTED )
474
- . unwrap ( ) ;
475
- pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) . unwrap ( ) ;
471
+ discharge_line. set_value ( 1 - DISCHARGE_LINE_ASSERTED ) ?;
472
+ pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) ?;
476
473
state. store ( OutputState :: OffFloating as u8 , Ordering :: Relaxed ) ;
477
474
}
478
475
}
479
476
}
480
477
481
478
// Make sure to enter fail safe mode before leaving the thread
482
- turn_off_with_reason ( OutputState :: Off , & pwr_line, & discharge_line, & state) ;
479
+ turn_off_with_reason ( OutputState :: Off , & pwr_line, & discharge_line, & state) ? ;
483
480
484
481
Ok ( ( ) )
485
482
} ) ?;
486
483
487
- let ( tick, request, state) = thread_res_rx . next ( ) . await . unwrap ( ) ?;
484
+ let ( tick, request, state) = thread_rx . recv ( ) . await ?;
488
485
489
486
// The request and state topic use the same external path, this way one
490
487
// can e.g. publish "On" to the topic and be sure that the output is
0 commit comments