@@ -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.
@@ -318,7 +323,7 @@ impl DutPwrThread {
318
323
// as well.
319
324
// Use a queue to notify the calling thread if the priority setup
320
325
// succeeded.
321
- let ( thread_res_tx , mut thread_res_rx ) = bounded ( 1 ) ;
326
+ let ( thread_tx , thread_rx ) = bounded ( 1 ) ;
322
327
323
328
// Spawn a high priority thread that handles the power status
324
329
// in a realtimey fashion.
@@ -333,24 +338,20 @@ impl DutPwrThread {
333
338
let mut volt_filter = MedianFilter :: < 4 > :: new ( ) ;
334
339
let mut curr_filter = MedianFilter :: < 4 > :: new ( ) ;
335
340
336
- let ( tick_weak, request, state) = match realtime_priority ( ) {
337
- Ok ( _) => {
338
- let tick = Arc :: new ( AtomicU32 :: new ( 0 ) ) ;
339
- let tick_weak = Arc :: downgrade ( & tick) ;
341
+ realtime_priority ( ) ?;
340
342
341
- let request = Arc :: new ( AtomicU8 :: new ( OutputRequest :: Idle as u8 ) ) ;
342
- let state = Arc :: new ( AtomicU8 :: new ( OutputState :: Off as u8 ) ) ;
343
+ let ( tick_weak, request, state) = {
344
+ let tick = Arc :: new ( AtomicU32 :: new ( 0 ) ) ;
345
+ let tick_weak = Arc :: downgrade ( & tick) ;
343
346
344
- thread_res_tx
345
- . try_send ( Ok ( ( tick, request. clone ( ) , state. clone ( ) ) ) )
346
- . unwrap ( ) ;
347
+ let request = Arc :: new ( AtomicU8 :: new ( OutputRequest :: Idle as u8 ) ) ;
348
+ let state = Arc :: new ( AtomicU8 :: new ( OutputState :: Off as u8 ) ) ;
347
349
348
- ( tick_weak, request, state)
349
- }
350
- Err ( e) => {
351
- thread_res_tx. try_send ( Err ( e) ) . unwrap ( ) ;
352
- panic ! ( )
353
- }
350
+ thread_tx
351
+ . try_send ( ( tick, request. clone ( ) , state. clone ( ) ) )
352
+ . expect ( "Queue that should be empty wasn't" ) ;
353
+
354
+ ( tick_weak, request, state)
354
355
} ;
355
356
356
357
// Run as long as there is a strong reference to `tick`.
@@ -385,7 +386,7 @@ impl DutPwrThread {
385
386
& pwr_line,
386
387
& discharge_line,
387
388
& state,
388
- ) ;
389
+ ) ? ;
389
390
} else {
390
391
// We have a fresh ADC value. Signal "everything is well"
391
392
// to the watchdog task.
@@ -422,7 +423,7 @@ impl DutPwrThread {
422
423
& pwr_line,
423
424
& discharge_line,
424
425
& state,
425
- ) ;
426
+ ) ? ;
426
427
427
428
continue ;
428
429
}
@@ -436,7 +437,7 @@ impl DutPwrThread {
436
437
& pwr_line,
437
438
& discharge_line,
438
439
& state,
439
- ) ;
440
+ ) ? ;
440
441
441
442
continue ;
442
443
}
@@ -449,7 +450,7 @@ impl DutPwrThread {
449
450
& pwr_line,
450
451
& discharge_line,
451
452
& state,
452
- ) ;
453
+ ) ? ;
453
454
454
455
continue ;
455
456
}
@@ -459,34 +460,30 @@ impl DutPwrThread {
459
460
match req {
460
461
OutputRequest :: Idle => { }
461
462
OutputRequest :: On => {
462
- discharge_line
463
- . set_value ( 1 - DISCHARGE_LINE_ASSERTED )
464
- . unwrap ( ) ;
465
- pwr_line. set_value ( PWR_LINE_ASSERTED ) . unwrap ( ) ;
463
+ discharge_line. set_value ( 1 - DISCHARGE_LINE_ASSERTED ) ?;
464
+ pwr_line. set_value ( PWR_LINE_ASSERTED ) ?;
466
465
state. store ( OutputState :: On as u8 , Ordering :: Relaxed ) ;
467
466
}
468
467
OutputRequest :: Off => {
469
- discharge_line. set_value ( DISCHARGE_LINE_ASSERTED ) . unwrap ( ) ;
470
- pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) . unwrap ( ) ;
468
+ discharge_line. set_value ( DISCHARGE_LINE_ASSERTED ) ? ;
469
+ pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) ? ;
471
470
state. store ( OutputState :: Off as u8 , Ordering :: Relaxed ) ;
472
471
}
473
472
OutputRequest :: OffFloating => {
474
- discharge_line
475
- . set_value ( 1 - DISCHARGE_LINE_ASSERTED )
476
- . unwrap ( ) ;
477
- pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) . unwrap ( ) ;
473
+ discharge_line. set_value ( 1 - DISCHARGE_LINE_ASSERTED ) ?;
474
+ pwr_line. set_value ( 1 - PWR_LINE_ASSERTED ) ?;
478
475
state. store ( OutputState :: OffFloating as u8 , Ordering :: Relaxed ) ;
479
476
}
480
477
}
481
478
}
482
479
483
480
// Make sure to enter fail safe mode before leaving the thread
484
- turn_off_with_reason ( OutputState :: Off , & pwr_line, & discharge_line, & state) ;
481
+ turn_off_with_reason ( OutputState :: Off , & pwr_line, & discharge_line, & state) ? ;
485
482
486
483
Ok ( ( ) )
487
484
} ) ?;
488
485
489
- let ( tick, request, state) = thread_res_rx . next ( ) . await . unwrap ( ) ?;
486
+ let ( tick, request, state) = thread_rx . recv ( ) . await ?;
490
487
491
488
// The request and state topic use the same external path, this way one
492
489
// can e.g. publish "On" to the topic and be sure that the output is
0 commit comments