@@ -605,46 +605,52 @@ void furi_hal_serial_set_br(FuriHalSerialHandle* handle, uint32_t baud) {
605
605
}
606
606
}
607
607
608
- // Flash usage optimization: by checking that our enum members are equal to the
609
- // corresponding ST LL values, we can avoid having to convert between our
610
- // representation and the ST LL representation, thus shaving off about 120 bytes
611
- // (about half the size of this feature)
612
- static_assert (FuriHalSerialDataBits7 == LL_USART_DATAWIDTH_7B );
613
- static_assert (FuriHalSerialDataBits8 == LL_USART_DATAWIDTH_8B );
614
- static_assert (FuriHalSerialDataBits9 == LL_USART_DATAWIDTH_9B );
615
- static_assert (FuriHalSerialParityNone == LL_USART_PARITY_NONE );
616
- static_assert (FuriHalSerialParityEven == LL_USART_PARITY_EVEN );
617
- static_assert (FuriHalSerialParityOdd == LL_USART_PARITY_ODD );
618
- static_assert (FuriHalSerialStopBits0_5 == LL_USART_STOPBITS_0_5 );
619
- static_assert (FuriHalSerialStopBits1 == LL_USART_STOPBITS_1 );
620
- static_assert (FuriHalSerialStopBits1_5 == LL_USART_STOPBITS_1_5 );
621
- static_assert (FuriHalSerialStopBits2 == LL_USART_STOPBITS_2 );
608
+ // Avoid duplicating look-up tables between USART and LPUART
609
+ static_assert (LL_LPUART_DATAWIDTH_7B == LL_USART_DATAWIDTH_7B );
610
+ static_assert (LL_LPUART_DATAWIDTH_8B == LL_USART_DATAWIDTH_8B );
611
+ static_assert (LL_LPUART_DATAWIDTH_9B == LL_USART_DATAWIDTH_9B );
612
+ static_assert (LL_LPUART_PARITY_NONE == LL_USART_PARITY_NONE );
613
+ static_assert (LL_LPUART_PARITY_EVEN == LL_USART_PARITY_EVEN );
614
+ static_assert (LL_LPUART_PARITY_ODD == LL_USART_PARITY_ODD );
615
+ static_assert (LL_LPUART_STOPBITS_1 == LL_USART_STOPBITS_1 );
616
+ static_assert (LL_LPUART_STOPBITS_2 == LL_USART_STOPBITS_2 );
617
+
618
+ static const uint32_t serial_data_bits_lut [] = {
619
+ [FuriHalSerialDataBits7 ] = LL_USART_DATAWIDTH_7B ,
620
+ [FuriHalSerialDataBits8 ] = LL_USART_DATAWIDTH_8B ,
621
+ [FuriHalSerialDataBits9 ] = LL_USART_DATAWIDTH_9B ,
622
+ };
623
+
624
+ static const uint32_t serial_parity_lut [] = {
625
+ [FuriHalSerialParityNone ] = LL_USART_PARITY_NONE ,
626
+ [FuriHalSerialParityEven ] = LL_USART_PARITY_EVEN ,
627
+ [FuriHalSerialParityOdd ] = LL_USART_PARITY_ODD ,
628
+ };
629
+
630
+ static const uint32_t serial_stop_bits_lut [] = {
631
+ [FuriHalSerialStopBits0_5 ] = LL_USART_STOPBITS_0_5 ,
632
+ [FuriHalSerialStopBits1 ] = LL_USART_STOPBITS_1 ,
633
+ [FuriHalSerialStopBits1_5 ] = LL_USART_STOPBITS_1_5 ,
634
+ [FuriHalSerialStopBits2 ] = LL_USART_STOPBITS_2 ,
635
+ };
622
636
623
637
static void furi_hal_serial_usart_configure_framing (
624
638
FuriHalSerialDataBits data_bits ,
625
639
FuriHalSerialParity parity ,
626
640
FuriHalSerialStopBits stop_bits ) {
627
- LL_USART_SetDataWidth (USART1 , data_bits );
628
- LL_USART_SetParity (USART1 , parity );
629
- LL_USART_SetStopBitsLength (USART1 , stop_bits );
641
+ LL_USART_SetDataWidth (USART1 , serial_data_bits_lut [ data_bits ] );
642
+ LL_USART_SetParity (USART1 , serial_parity_lut [ parity ] );
643
+ LL_USART_SetStopBitsLength (USART1 , serial_stop_bits_lut [ stop_bits ] );
630
644
}
631
645
632
- static_assert (FuriHalSerialDataBits7 == LL_LPUART_DATAWIDTH_7B );
633
- static_assert (FuriHalSerialDataBits8 == LL_LPUART_DATAWIDTH_8B );
634
- static_assert (FuriHalSerialDataBits9 == LL_LPUART_DATAWIDTH_9B );
635
- static_assert (FuriHalSerialParityNone == LL_LPUART_PARITY_NONE );
636
- static_assert (FuriHalSerialParityEven == LL_LPUART_PARITY_EVEN );
637
- static_assert (FuriHalSerialParityOdd == LL_LPUART_PARITY_ODD );
638
- static_assert (FuriHalSerialStopBits1 == LL_LPUART_STOPBITS_1 );
639
- static_assert (FuriHalSerialStopBits2 == LL_LPUART_STOPBITS_2 );
640
-
641
646
static void furi_hal_serial_lpuart_configure_framing (
642
647
FuriHalSerialDataBits data_bits ,
643
648
FuriHalSerialParity parity ,
644
649
FuriHalSerialStopBits stop_bits ) {
645
- LL_LPUART_SetDataWidth (LPUART1 , data_bits );
646
- LL_LPUART_SetParity (LPUART1 , parity );
647
- LL_LPUART_SetStopBitsLength (LPUART1 , stop_bits );
650
+ LL_LPUART_SetDataWidth (LPUART1 , serial_data_bits_lut [data_bits ]);
651
+ LL_LPUART_SetParity (LPUART1 , serial_parity_lut [parity ]);
652
+ // Unsupported non-whole stop bit numbers have been furi_check'ed away
653
+ LL_LPUART_SetStopBitsLength (LPUART1 , serial_stop_bits_lut [stop_bits ]);
648
654
}
649
655
650
656
void furi_hal_serial_configure_framing (
@@ -659,14 +665,7 @@ void furi_hal_serial_configure_framing(
659
665
if (data_bits == FuriHalSerialDataBits6 ) furi_check (parity != FuriHalSerialParityNone );
660
666
661
667
// Extend data word to account for parity bit
662
- if (parity != FuriHalSerialParityNone ) {
663
- if (data_bits == FuriHalSerialDataBits6 )
664
- data_bits = FuriHalSerialDataBits7 ;
665
- else if (data_bits == FuriHalSerialDataBits7 )
666
- data_bits = FuriHalSerialDataBits8 ;
667
- else if (data_bits == FuriHalSerialDataBits8 )
668
- data_bits = FuriHalSerialDataBits9 ;
669
- }
668
+ if (parity != FuriHalSerialParityNone ) data_bits ++ ;
670
669
671
670
if (handle -> id == FuriHalSerialIdUsart ) {
672
671
if (LL_USART_IsEnabled (USART1 )) {
0 commit comments