@@ -47,16 +47,38 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
47
47
obj -> index = pinmap_merge (uart_tx , uart_rx );
48
48
MBED_ASSERT ((int )obj -> index != NC );
49
49
50
- /* Set the LPUART clock source */
51
- CLOCK_SetLpuartClock (1U );
50
+ // since the LPuart initialization depends very much on the source clock and its
51
+ // frequency, we do a check here and retrieve the frequency accordingly
52
+ // The CLOCK_SetLpuartSrc() is already done during clock init.
53
+ uint32_t lpuart_src_freq ;
54
+ switch (SIM -> SOPT2 & SIM_SOPT2_LPUARTSRC_MASK ) {
55
+ case SIM_SOPT2_LPUARTSRC (3U ): {
56
+ lpuart_src_freq = CLOCK_GetInternalRefClkFreq ();
57
+ break ;
58
+ }
59
+ case SIM_SOPT2_LPUARTSRC (2U ): {
60
+ lpuart_src_freq = CLOCK_GetOsc0ErClkFreq ();
61
+ break ;
62
+ }
63
+ case SIM_SOPT2_LPUARTSRC (1U ): {
64
+ lpuart_src_freq = CLOCK_GetPllFllSelClkFreq ();
65
+ break ;
66
+ }
67
+ default : {
68
+ /* Set the LPUART clock source */
69
+ CLOCK_SetLpuartClock (1U );
70
+ lpuart_src_freq = CLOCK_GetFreq (uart_clocks [obj -> index ]);
71
+ break ;
72
+ }
73
+ }
52
74
53
75
lpuart_config_t config ;
54
76
LPUART_GetDefaultConfig (& config );
55
77
config .baudRate_Bps = 9600 ;
56
78
config .enableTx = false;
57
79
config .enableRx = false;
58
80
59
- LPUART_Init (uart_addrs [obj -> index ], & config , CLOCK_GetFreq ( uart_clocks [ obj -> index ]) );
81
+ LPUART_Init (uart_addrs [obj -> index ], & config , lpuart_src_freq );
60
82
61
83
pinmap_pinout (tx , PinMap_UART_TX );
62
84
pinmap_pinout (rx , PinMap_UART_RX );
0 commit comments