Skip to content

Commit

Permalink
fix(PeriphDrivers): Fix UART clock source issues (#1168)
Browse files Browse the repository at this point in the history
Signed-off-by: Furkan Akkiz <[email protected]>
  • Loading branch information
hfakkiz authored Sep 24, 2024
1 parent 822491e commit df04760
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 183 deletions.
9 changes: 9 additions & 0 deletions Libraries/PeriphDrivers/Include/MAX32670/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ int MXC_UART_SetFlowCtrl(mxc_uart_regs_t *uart, mxc_uart_flow_t flowCtrl, int rt
*/
int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock);

/**
* @brief Gets the clock source used for the UART instance
*
* @param uart Pointer to UART registers (selects the UART block used.)
*
* @return The selected clock source for the UART instance
*/
mxc_uart_clock_t MXC_UART_GetClockSource(mxc_uart_regs_t *uart);

/* ************************************************************************* */
/* Low-level functions */
/* ************************************************************************* */
Expand Down
16 changes: 12 additions & 4 deletions Libraries/PeriphDrivers/Include/MAX32672/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ typedef enum {
MXC_UART_EXT_CLK = 1,
/*8M (IBRO) and 32M (EFRO) clocks can be used for UARTs 0,1 and 2*/
MXC_UART_IBRO_CLK = 2,
MXC_UART_ERFO_CLK = 3,
/*32K (ERTCO) and 80K (INRO) clocks can only be used for UART3*/
MXC_UART_ERTCO_CLK = 4,
MXC_UART_INRO_CLK = 5,
MXC_UART_AOD_CLK = 6
MXC_UART_ERTCO_CLK = 3,
MXC_UART_INRO_CLK = 4,
MXC_UART_AOD_CLK = 5
} mxc_uart_clock_t;

/**
Expand Down Expand Up @@ -277,6 +276,15 @@ int MXC_UART_SetFlowCtrl(mxc_uart_regs_t *uart, mxc_uart_flow_t flowCtrl, int rt
*/
int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock);

/**
* @brief Gets the clock source used for the UART instance
*
* @param uart Pointer to UART registers (selects the UART block used.)
*
* @return The selected clock source for the UART instance
*/
mxc_uart_clock_t MXC_UART_GetClockSource(mxc_uart_regs_t *uart);

/* ************************************************************************* */
/* Low-level functions */
/* ************************************************************************* */
Expand Down
5 changes: 1 addition & 4 deletions Libraries/PeriphDrivers/Include/MAX32675/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,9 @@ typedef enum {
typedef enum {
MXC_UART_APB_CLK = 0,
MXC_UART_EXT_CLK = 1,
/*8M (IBRO) and 32M (EFRO) clocks can be used for UARTs 0,1 and 2*/
/*8M (IBRO) and 32M (EFRO) clocks can be used for UARTs 0 and 2*/
MXC_UART_IBRO_CLK = 2,
MXC_UART_ERFO_CLK = 3,
/*32K (ERTCO) clock can only be used for UART3*/
MXC_UART_AOD_CLK = 4,
MXC_UART_INRO_CLK = 5
} mxc_uart_clock_t;

/**
Expand Down
37 changes: 24 additions & 13 deletions Libraries/PeriphDrivers/Source/UART/uart_ai85.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int MXC_UART_AsyncStop(mxc_uart_regs_t *uart)
int MXC_UART_Init(mxc_uart_regs_t *uart, unsigned int baud, mxc_uart_clock_t clock)
{
int retval;

#ifndef MSDK_NO_GPIO_CLK_INIT
retval = MXC_UART_Shutdown(uart);
if (retval) {
return retval;
Expand Down Expand Up @@ -75,13 +75,14 @@ int MXC_UART_Init(mxc_uart_regs_t *uart, unsigned int baud, mxc_uart_clock_t clo
default:
return E_BAD_PARAM;
}
#endif // MSDK_NO_GPIO_CLK_INIT

retval = MXC_UART_SetClockSource(uart, clock);
if (retval != E_NO_ERROR) {
return retval;
}

return MXC_UART_RevB_Init((mxc_uart_revb_regs_t *)uart, baud, clock);
return MXC_UART_RevB_Init((mxc_uart_revb_regs_t *)uart, baud, MXC_UART_GetClockSource(uart));
}

int MXC_UART_Shutdown(mxc_uart_regs_t *uart)
Expand Down Expand Up @@ -240,7 +241,8 @@ int MXC_UART_SetFlowCtrl(mxc_uart_regs_t *uart, mxc_uart_flow_t flowCtrl, int rt

int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock)
{
int error = E_NO_ERROR;
int retval = E_NO_ERROR;
uint8_t clock_option = 0;

switch (MXC_UART_GET_IDX(uart)) {
case 0:
Expand All @@ -249,12 +251,14 @@ int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock)
// UART0-2 support PCLK and IBRO
switch (clock) {
case MXC_UART_APB_CLK:
MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, 0);
clock_option = 0;
break;

case MXC_UART_IBRO_CLK:
error = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_IBRO);
MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, 2);
#ifndef MSDK_NO_GPIO_CLK_INIT
retval = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_IBRO);
#endif // MSDK_NO_GPIO_CLK_INIT
clock_option = 2;
break;

default:
Expand All @@ -266,13 +270,17 @@ int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock)
// UART3 (LPUART0) supports IBRO and ERTCO
switch (clock) {
case MXC_UART_IBRO_CLK:
error = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_IBRO);
MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, 0);
#ifndef MSDK_NO_GPIO_CLK_INIT
retval = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_IBRO);
#endif // MSDK_NO_GPIO_CLK_INIT
clock_option = 2;
break;

case MXC_UART_ERTCO_CLK:
error = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERTCO);
MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, 1);
#ifndef MSDK_NO_GPIO_CLK_INIT
retval = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERTCO);
#endif // MSDK_NO_GPIO_CLK_INIT
clock_option = 3;
break;

default:
Expand All @@ -284,7 +292,10 @@ int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock)
return E_BAD_PARAM;
}

return error;
if (retval)
return retval;

return MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, clock_option);
}

mxc_uart_clock_t MXC_UART_GetClockSource(mxc_uart_regs_t *uart)
Expand All @@ -305,9 +316,9 @@ mxc_uart_clock_t MXC_UART_GetClockSource(mxc_uart_regs_t *uart)
break;
case 3:
switch (clock_option) {
case 0:
case 2:
return MXC_UART_IBRO_CLK;
case 1:
case 3:
return MXC_UART_ERTCO_CLK;
default:
return E_BAD_STATE;
Expand Down
23 changes: 17 additions & 6 deletions Libraries/PeriphDrivers/Source/UART/uart_ai87.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void MXC_UART_UnlockClockSource(mxc_uart_regs_t *uart)
int MXC_UART_Init(mxc_uart_regs_t *uart, unsigned int baud, mxc_uart_clock_t clock)
{
int err;

#ifndef MSDK_NO_GPIO_CLK_INIT
err = MXC_UART_Shutdown(uart);

if (err) {
Expand Down Expand Up @@ -92,6 +92,7 @@ int MXC_UART_Init(mxc_uart_regs_t *uart, unsigned int baud, mxc_uart_clock_t clo
default:
return E_BAD_PARAM;
}
#endif // MSDK_NO_GPIO_CLK_INIT

err = MXC_UART_SetClockSource(uart, clock);
if (err)
Expand Down Expand Up @@ -261,6 +262,7 @@ int MXC_UART_SetFlowCtrl(mxc_uart_regs_t *uart, mxc_uart_flow_t flowCtrl, int rt
int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock)
{
int err = E_NO_ERROR;
uint8_t clock_option = 0;
int idx = MXC_UART_GET_IDX(uart);
if (idx < 0)
return E_BAD_PARAM;
Expand All @@ -277,12 +279,14 @@ int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock)
// UART0-2 support PCLK and IBRO
switch (clock) {
case MXC_UART_APB_CLK:
MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, 0);
clock_option = 0;
break;

case MXC_UART_IBRO_CLK:
#ifndef MSDK_NO_GPIO_CLK_INIT
err = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_IBRO);
MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, 2);
#endif // MSDK_NO_GPIO_CLK_INIT
clock_option = 2;
break;

default:
Expand All @@ -293,13 +297,17 @@ int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock)
// UART3 (LPUART0) supports IBRO and ERTCO
switch (clock) {
case MXC_UART_IBRO_CLK:
#ifndef MSDK_NO_GPIO_CLK_INIT
err = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_IBRO);
MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, 0);
#endif // MSDK_NO_GPIO_CLK_INIT
clock_option = 0;
break;

case MXC_UART_ERTCO_CLK:
#ifndef MSDK_NO_GPIO_CLK_INIT
err = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERTCO);
MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, 1);
#endif // MSDK_NO_GPIO_CLK_INIT
clock_option = 1;
break;

default:
Expand All @@ -308,7 +316,10 @@ int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock)
break;
}

return err;
if (err)
return err;

return MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, clock_option);
}

mxc_uart_clock_t MXC_UART_GetClockSource(mxc_uart_regs_t *uart)
Expand Down
6 changes: 6 additions & 0 deletions Libraries/PeriphDrivers/Source/UART/uart_me12.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,23 @@ int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock)
break;

case MXC_UART_EXT_CLK:
#ifndef MSDK_NO_GPIO_CLK_INIT
error = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_EXTCLK);
#endif // MSDK_NO_GPIO_CLK_INIT
MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, 1);
break;

case MXC_UART_IBRO_CLK:
#ifndef MSDK_NO_GPIO_CLK_INIT
error = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_IBRO);
#endif // MSDK_NO_GPIO_CLK_INIT
MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, 2);
break;

case MXC_UART_ERFO_CLK:
#ifndef MSDK_NO_GPIO_CLK_INIT
error = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERFO);
#endif // MSDK_NO_GPIO_CLK_INIT
MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, 3);
break;

Expand Down
83 changes: 53 additions & 30 deletions Libraries/PeriphDrivers/Source/UART/uart_me15.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,6 @@ int MXC_UART_Init(mxc_uart_regs_t *uart, unsigned int baud, mxc_uart_clock_t clo
return retval;
}

switch (clock) {
case MXC_UART_EXT_CLK:
if (uart == MXC_UART3) {
MXC_GPIO_Config(&gpio_cfg_lpextclk);
} else {
MXC_GPIO_Config(&gpio_cfg_hfextclk);
}
break;

case MXC_UART_ERTCO_CLK:
MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERTCO);
break;

case MXC_UART_IBRO_CLK:
MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_IBRO);
break;

case MXC_UART_ERFO_CLK:
MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERFO);
break;

case MXC_UART_INRO_CLK:
MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_INRO);
break;

default:
break;
}

switch (MXC_UART_GET_IDX(uart)) {
case 0:
MXC_GPIO_Config(&gpio_cfg_uart0);
Expand Down Expand Up @@ -115,7 +86,7 @@ int MXC_UART_Init(mxc_uart_regs_t *uart, unsigned int baud, mxc_uart_clock_t clo
if (retval)
return retval;

return MXC_UART_RevB_Init((mxc_uart_revb_regs_t *)uart, baud, clock);
return MXC_UART_RevB_Init((mxc_uart_revb_regs_t *)uart, baud, MXC_UART_GetClockSource(uart));
}

int MXC_UART_Shutdown(mxc_uart_regs_t *uart)
Expand Down Expand Up @@ -346,14 +317,21 @@ int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock)
clock_option = 0;
break;
case MXC_UART_EXT_CLK:
#ifndef MSDK_NO_GPIO_CLK_INIT
MXC_GPIO_Config(&gpio_cfg_hfextclk);
#endif // MSDK_NO_GPIO_CLK_INIT
clock_option = 1;
break;
case MXC_UART_IBRO_CLK:
#ifndef MSDK_NO_GPIO_CLK_INIT
MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_IBRO);
#endif // MSDK_NO_GPIO_CLK_INIT
clock_option = 2;
break;
case MXC_UART_ERFO_CLK:
#ifndef MSDK_NO_GPIO_CLK_INIT
MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERFO);
#endif // MSDK_NO_GPIO_CLK_INIT
clock_option = 3;
break;
default:
Expand All @@ -366,9 +344,15 @@ int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock)
clock_option = 0;
break;
case MXC_UART_EXT_CLK:
#ifndef MSDK_NO_GPIO_CLK_INIT
MXC_GPIO_Config(&gpio_cfg_lpextclk);
#endif // MSDK_NO_GPIO_CLK_INIT
clock_option = 1;
break;
case MXC_UART_ERTCO_CLK:
#ifndef MSDK_NO_GPIO_CLK_INIT
MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERTCO);
#endif // MSDK_NO_GPIO_CLK_INIT
clock_option = 2;
break;
case MXC_UART_INRO_CLK:
Expand All @@ -382,6 +366,45 @@ int MXC_UART_SetClockSource(mxc_uart_regs_t *uart, mxc_uart_clock_t clock)
return MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, clock_option);
}

mxc_uart_clock_t MXC_UART_GetClockSource(mxc_uart_regs_t *uart)
{
unsigned int clock_option = MXC_UART_RevB_GetClockSource((mxc_uart_revb_regs_t *)uart);
switch (MXC_UART_GET_IDX(uart)) {
case 0:
case 1:
case 2:
switch (clock_option) {
case 0:
return MXC_UART_APB_CLK;
case 1:
return MXC_UART_EXT_CLK;
case 2:
return MXC_UART_IBRO_CLK;
case 3:
return MXC_UART_ERFO_CLK;
default:
return E_BAD_STATE;
}
break;
case 3:
switch (clock_option) {
case 0:
return MXC_UART_AOD_CLK;
case 1:
return MXC_UART_EXT_CLK;
case 2:
return MXC_UART_ERTCO_CLK;
case 3:
return MXC_UART_INRO_CLK;
default:
return E_BAD_STATE;
}
break;
default:
return E_BAD_PARAM;
}
}

int MXC_UART_GetActive(mxc_uart_regs_t *uart)
{
return MXC_UART_RevB_GetActive((mxc_uart_revb_regs_t *)uart);
Expand Down
Loading

0 comments on commit df04760

Please sign in to comment.