@@ -50,9 +50,12 @@ mod prio {
50
50
use thread_priority:: * ;
51
51
52
52
pub fn realtime_priority ( ) -> Result < ( ) > {
53
+ let prio = ThreadPriorityValue :: try_from ( 10 )
54
+ . map_err ( |e| anyhow ! ( "Failed to choose realtime priority level 10: {e:?}" ) ) ?;
55
+
53
56
set_thread_priority_and_policy (
54
57
thread_native_id ( ) ,
55
- ThreadPriority :: Crossplatform ( ThreadPriorityValue :: try_from ( 10 ) . unwrap ( ) ) ,
58
+ ThreadPriority :: Crossplatform ( prio ) ,
56
59
ThreadSchedulePolicy :: Realtime ( RealtimeThreadSchedulePolicy :: Fifo ) ,
57
60
)
58
61
. map_err ( |e| anyhow ! ( "Failed to set up realtime priority {e:?}" ) )
@@ -260,10 +263,12 @@ fn turn_off_with_reason(
260
263
pwr_line : & LineHandle ,
261
264
discharge_line : & LineHandle ,
262
265
fail_state : & AtomicU8 ,
263
- ) {
264
- pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) . unwrap ( ) ;
265
- discharge_line. set_value ( DISCHARGE_LINE_ASSERTED ) . unwrap ( ) ;
266
+ ) -> Result < ( ) > {
267
+ pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) ? ;
268
+ discharge_line. set_value ( DISCHARGE_LINE_ASSERTED ) ? ;
266
269
fail_state. store ( reason as u8 , Ordering :: Relaxed ) ;
270
+
271
+ Ok ( ( ) )
267
272
}
268
273
269
274
/// Labgrid has a fixed assumption of how a REST based power port should work.
@@ -333,7 +338,7 @@ impl DutPwrThread {
333
338
// as well.
334
339
// Use a queue to notify the calling thread if the priority setup
335
340
// succeeded.
336
- let ( thread_res_tx , mut thread_res_rx ) = bounded ( 1 ) ;
341
+ let ( thread_tx , thread_rx ) = bounded ( 1 ) ;
337
342
338
343
// Spawn a high priority thread that handles the power status
339
344
// in a realtimey fashion.
@@ -348,24 +353,20 @@ impl DutPwrThread {
348
353
let mut volt_filter = MedianFilter :: < 4 > :: new ( ) ;
349
354
let mut curr_filter = MedianFilter :: < 4 > :: new ( ) ;
350
355
351
- let ( tick_weak, request, state) = match realtime_priority ( ) {
352
- Ok ( _) => {
353
- let tick = Arc :: new ( AtomicU32 :: new ( 0 ) ) ;
354
- let tick_weak = Arc :: downgrade ( & tick) ;
356
+ realtime_priority ( ) ?;
355
357
356
- let request = Arc :: new ( AtomicU8 :: new ( OutputRequest :: Idle as u8 ) ) ;
357
- let state = Arc :: new ( AtomicU8 :: new ( OutputState :: Off as u8 ) ) ;
358
+ let ( tick_weak, request, state) = {
359
+ let tick = Arc :: new ( AtomicU32 :: new ( 0 ) ) ;
360
+ let tick_weak = Arc :: downgrade ( & tick) ;
358
361
359
- thread_res_tx
360
- . try_send ( Ok ( ( tick, request. clone ( ) , state. clone ( ) ) ) )
361
- . unwrap ( ) ;
362
+ let request = Arc :: new ( AtomicU8 :: new ( OutputRequest :: Idle as u8 ) ) ;
363
+ let state = Arc :: new ( AtomicU8 :: new ( OutputState :: Off as u8 ) ) ;
362
364
363
- ( tick_weak, request, state)
364
- }
365
- Err ( e) => {
366
- thread_res_tx. try_send ( Err ( e) ) . unwrap ( ) ;
367
- panic ! ( )
368
- }
365
+ thread_tx
366
+ . try_send ( ( tick, request. clone ( ) , state. clone ( ) ) )
367
+ . expect ( "Queue that should be empty wasn't" ) ;
368
+
369
+ ( tick_weak, request, state)
369
370
} ;
370
371
371
372
// The grace period contains the number of loop iterations until
@@ -406,7 +407,7 @@ impl DutPwrThread {
406
407
& pwr_line,
407
408
& discharge_line,
408
409
& state,
409
- ) ;
410
+ ) ? ;
410
411
} else {
411
412
// We have a fresh ADC value. Signal "everything is well"
412
413
// to the watchdog task.
@@ -463,7 +464,7 @@ impl DutPwrThread {
463
464
& pwr_line,
464
465
& discharge_line,
465
466
& state,
466
- ) ;
467
+ ) ? ;
467
468
468
469
continue ;
469
470
}
@@ -474,7 +475,7 @@ impl DutPwrThread {
474
475
& pwr_line,
475
476
& discharge_line,
476
477
& state,
477
- ) ;
478
+ ) ? ;
478
479
479
480
continue ;
480
481
}
@@ -485,7 +486,7 @@ impl DutPwrThread {
485
486
& pwr_line,
486
487
& discharge_line,
487
488
& state,
488
- ) ;
489
+ ) ? ;
489
490
490
491
continue ;
491
492
}
@@ -496,34 +497,30 @@ impl DutPwrThread {
496
497
match req {
497
498
OutputRequest :: Idle => { }
498
499
OutputRequest :: On => {
499
- discharge_line
500
- . set_value ( 1 - DISCHARGE_LINE_ASSERTED )
501
- . unwrap ( ) ;
502
- pwr_line. set_value ( PWR_LINE_ASSERTED ) . unwrap ( ) ;
500
+ discharge_line. set_value ( 1 - DISCHARGE_LINE_ASSERTED ) ?;
501
+ pwr_line. set_value ( PWR_LINE_ASSERTED ) ?;
503
502
state. store ( OutputState :: On as u8 , Ordering :: Relaxed ) ;
504
503
}
505
504
OutputRequest :: Off => {
506
- discharge_line. set_value ( DISCHARGE_LINE_ASSERTED ) . unwrap ( ) ;
507
- pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) . unwrap ( ) ;
505
+ discharge_line. set_value ( DISCHARGE_LINE_ASSERTED ) ? ;
506
+ pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) ? ;
508
507
state. store ( OutputState :: Off as u8 , Ordering :: Relaxed ) ;
509
508
}
510
509
OutputRequest :: OffFloating => {
511
- discharge_line
512
- . set_value ( 1 - DISCHARGE_LINE_ASSERTED )
513
- . unwrap ( ) ;
514
- pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) . unwrap ( ) ;
510
+ discharge_line. set_value ( 1 - DISCHARGE_LINE_ASSERTED ) ?;
511
+ pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) ?;
515
512
state. store ( OutputState :: OffFloating as u8 , Ordering :: Relaxed ) ;
516
513
}
517
514
}
518
515
}
519
516
520
517
// Make sure to enter fail safe mode before leaving the thread
521
- turn_off_with_reason ( OutputState :: Off , & pwr_line, & discharge_line, & state) ;
518
+ turn_off_with_reason ( OutputState :: Off , & pwr_line, & discharge_line, & state) ? ;
522
519
523
520
Ok ( ( ) )
524
521
} ) ?;
525
522
526
- let ( tick, request, state) = thread_res_rx . next ( ) . await . unwrap ( ) ?;
523
+ let ( tick, request, state) = thread_rx . recv ( ) . await ?;
527
524
528
525
// The request and state topic use the same external path, this way one
529
526
// can e.g. publish "On" to the topic and be sure that the output is
0 commit comments