Skip to content

Commit

Permalink
fix(PeriphDrivers): Add UART Clock Setter API for all parts using UAR…
Browse files Browse the repository at this point in the history
…T RevB Drivers (#1113)

Co-authored-by: sihyung-maxim <[email protected]>
  • Loading branch information
sihyung-maxim and sihyung-maxim authored Aug 15, 2024
1 parent 608acf3 commit 3c1cf09
Show file tree
Hide file tree
Showing 10 changed files with 410 additions and 155 deletions.
14 changes: 9 additions & 5 deletions Libraries/PeriphDrivers/Include/MAX32572/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,9 @@ typedef enum {
/**
* @brief Clock settings */
typedef enum {
/*For UART3 APB clock source is the 8MHz clock*/
MXC_UART_APB_CLK = 0,
MXC_UART_EXT_CLK = 1,
/*IBRO and ERFO clock can only be used for UART 0, 1 & 2*/
MXC_UART_IBRO_CLK = 2,
MXC_UART_ERFO_CLK = 3,
/*ERTCO clock can only be used for UART3*/
MXC_UART_ERTCO_CLK = 4,
} mxc_uart_clock_t;

/**
Expand Down Expand Up @@ -254,6 +249,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
9 changes: 9 additions & 0 deletions Libraries/PeriphDrivers/Include/MAX32655/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,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
9 changes: 9 additions & 0 deletions Libraries/PeriphDrivers/Include/MAX32662/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,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
7 changes: 7 additions & 0 deletions Libraries/PeriphDrivers/Include/MAX32690/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ 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);

/**
Expand Down
9 changes: 9 additions & 0 deletions Libraries/PeriphDrivers/Include/MAX78000/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,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
7 changes: 7 additions & 0 deletions Libraries/PeriphDrivers/Include/MAX78002/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ 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);

/**
Expand Down
162 changes: 111 additions & 51 deletions Libraries/PeriphDrivers/Source/UART/uart_ai85.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,10 @@ int MXC_UART_Init(mxc_uart_regs_t *uart, unsigned int baud, mxc_uart_clock_t clo
int retval;

retval = MXC_UART_Shutdown(uart);

if (retval) {
return retval;
}

switch (clock) {
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;

default:
break;
}

switch (MXC_UART_GET_IDX(uart)) {
case 0:
MXC_GPIO_Config(&gpio_cfg_uart0);
Expand All @@ -90,6 +76,11 @@ int MXC_UART_Init(mxc_uart_regs_t *uart, unsigned int baud, mxc_uart_clock_t clo
return E_BAD_PARAM;
}

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);
}

Expand Down Expand Up @@ -130,59 +121,53 @@ int MXC_UART_ReadyForSleep(mxc_uart_regs_t *uart)

int MXC_UART_SetFrequency(mxc_uart_regs_t *uart, unsigned int baud, mxc_uart_clock_t clock)
{
int frequency, clkDiv = 0, mod = 0;
int freq;
uint32_t clock_freq = 0;

if (MXC_UART_GET_IDX(uart) < 0) {
return E_BAD_PARAM;
}

// check if the uart is LPUART
if (uart == MXC_UART3) {
// OSR default value
uart->osr = 5;

switch (clock) {
case MXC_UART_APB_CLK:
case MXC_UART_IBRO_CLK:
clkDiv = ((IBRO_FREQ) / baud);
mod = ((IBRO_FREQ) % baud);
break;
switch (clock) {
case MXC_UART_APB_CLK:
clock_freq = SystemCoreClock / 2;
break;

case MXC_UART_ERTCO_CLK:
uart->ctrl |= MXC_S_UART_CTRL_BCLKSRC_EXTERNAL_CLOCK;
uart->ctrl |= MXC_F_UART_CTRL_FDM;
clkDiv = ((ERTCO_FREQ * 2) / baud);
mod = ((ERTCO_FREQ * 2) % baud);

if (baud > 2400) {
uart->osr = 0;
} else {
uart->osr = 1;
}
break;
case MXC_UART_IBRO_CLK:
clock_freq = IBRO_FREQ;
break;

default:
case MXC_UART_ERTCO_CLK:
// Only UART3 (LPUART0) supports ERTCO clock source.
if (uart != MXC_UART3) {
return E_BAD_PARAM;
}
if (!clkDiv || mod > (baud / 2)) {
clkDiv++;
}
uart->clkdiv = clkDiv;
frequency = MXC_UART_GetFrequency(uart);
} else {
if (clock == MXC_UART_ERTCO_CLK) {
return E_BAD_PARAM;

uart->ctrl |= MXC_S_UART_CTRL_BCLKSRC_EXTERNAL_CLOCK;
uart->ctrl |= MXC_F_UART_CTRL_FDM;

if (baud > 2400) {
uart->osr = 0;
} else {
uart->osr = 1;
}

frequency = MXC_UART_RevB_SetFrequency((mxc_uart_revb_regs_t *)uart, baud, clock);
clock_freq = ERTCO_FREQ * 2; // x2 to account for FDM.
break;

default:
return E_BAD_PARAM;
}

if (frequency > 0) {
freq = MXC_UART_RevB_SetFrequency((mxc_uart_revb_regs_t *)uart, clock_freq, baud);

if (freq > 0) {
// Enable baud clock and wait for it to become ready.
uart->ctrl |= MXC_F_UART_CTRL_BCLKEN;
while (((uart->ctrl & MXC_F_UART_CTRL_BCLKRDY) >> MXC_F_UART_CTRL_BCLKRDY_POS) == 0) {}
}

return frequency;
return freq;
}

int MXC_UART_GetFrequency(mxc_uart_regs_t *uart)
Expand Down Expand Up @@ -251,7 +236,82 @@ 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)
{
return MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, clock);
int error = E_NO_ERROR;

switch (MXC_UART_GET_IDX(uart)) {
case 0:
case 1:
case 2:
// UART0-2 support PCLK and IBRO
switch (clock) {
case MXC_UART_APB_CLK:
MXC_UART_RevB_SetClockSource((mxc_uart_revb_regs_t *)uart, 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);
break;

default:
return E_BAD_PARAM;
}
break;

case 3:
// 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, 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, 3);
break;

default:
return E_BAD_PARAM;
}
break;

default:
return E_BAD_PARAM;
}

return error;
}

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 2:
return MXC_UART_IBRO_CLK;
default:
return E_BAD_STATE;
}
break;
case 3:
switch (clock_option) {
case 0:
return MXC_UART_IBRO_CLK;
case 1:
return MXC_UART_ERTCO_CLK;
default:
return E_BAD_STATE;
}
break;
default:
return E_BAD_PARAM;
}
}

int MXC_UART_GetActive(mxc_uart_regs_t *uart)
Expand Down
Loading

0 comments on commit 3c1cf09

Please sign in to comment.