aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/ports/TIVA/LLD/UART/hal_serial_lld.c
diff options
context:
space:
mode:
Diffstat (limited to 'os/hal/ports/TIVA/LLD/UART/hal_serial_lld.c')
-rw-r--r--os/hal/ports/TIVA/LLD/UART/hal_serial_lld.c207
1 files changed, 112 insertions, 95 deletions
diff --git a/os/hal/ports/TIVA/LLD/UART/hal_serial_lld.c b/os/hal/ports/TIVA/LLD/UART/hal_serial_lld.c
index 89d29da..2e3b213 100644
--- a/os/hal/ports/TIVA/LLD/UART/hal_serial_lld.c
+++ b/os/hal/ports/TIVA/LLD/UART/hal_serial_lld.c
@@ -34,58 +34,42 @@
/* Driver exported variables. */
/*===========================================================================*/
-/**
- * @brief UART0 serial driver identifier.
- */
+/** @brief UART0 serial driver identifier.*/
#if TIVA_SERIAL_USE_UART0 || defined(__DOXYGEN__)
SerialDriver SD1;
#endif
-/**
- * @brief UART1 serial driver identifier.
- */
+/** @brief UART1 serial driver identifier.*/
#if TIVA_SERIAL_USE_UART1 || defined(__DOXYGEN__)
SerialDriver SD2;
#endif
-/**
- * @brief UART2 serial driver identifier.
- */
+/** @brief UART2 serial driver identifier.*/
#if TIVA_SERIAL_USE_UART2 || defined(__DOXYGEN__)
SerialDriver SD3;
#endif
-/**
- * @brief UART3 serial driver identifier.
- */
+/** @brief UART3 serial driver identifier.*/
#if TIVA_SERIAL_USE_UART3 || defined(__DOXYGEN__)
SerialDriver SD4;
#endif
-/**
- * @brief UART4 serial driver identifier.
- */
+/** @brief UART4 serial driver identifier.*/
#if TIVA_SERIAL_USE_UART4 || defined(__DOXYGEN__)
SerialDriver SD5;
#endif
-/**
- * @brief UART5 serial driver identifier.
- */
+/** @brief UART5 serial driver identifier.*/
#if TIVA_SERIAL_USE_UART5 || defined(__DOXYGEN__)
SerialDriver SD6;
#endif
-/**
- * @brief UART6 serial driver identifier.
- */
+/** @brief UART6 serial driver identifier.*/
#if TIVA_SERIAL_USE_UART6 || defined(__DOXYGEN__)
SerialDriver SD7;
#endif
-/**
- * @brief UART7 serial driver identifier.
- */
+/** @brief UART7 serial driver identifier.*/
#if TIVA_SERIAL_USE_UART7 || defined(__DOXYGEN__)
SerialDriver SD8;
#endif
@@ -94,14 +78,14 @@ SerialDriver SD8;
/* Driver local variables. */
/*===========================================================================*/
-/**
- * @brief Driver default configuration.
- */
+/** @brief Driver default configuration.*/
static const SerialConfig sd_default_config =
{
SERIAL_DEFAULT_BITRATE,
- TIVA_LCRH_FEN | TIVA_LCRH_WLEN_8,
- TIVA_IFLS_TXIFLSEL_1_8_F | TIVA_IFLS_RXIFLSEL_1_8_E
+ 0,
+ UART_LCRH_FEN | UART_LCRH_WLEN_8,
+ UART_IFLS_TX4_8 | UART_IFLS_RX7_8,
+ UART_CC_CS_SYSCLK
};
/*===========================================================================*/
@@ -111,23 +95,55 @@ static const SerialConfig sd_default_config =
/**
* @brief UART initialization.
*
- * @param[in] sdp communication channel associated to the UART
+ * @param[in] sdp pointer to a @p SerialDriver object
* @param[in] config the architecture-dependent serial driver configuration
*/
static void uart_init(SerialDriver *sdp, const SerialConfig *config)
{
- UART_TypeDef *u = sdp->uart;
- uint32_t div; /* baud rate divisor */
-
- /* disable the UART before any of the control registers are reprogrammed */
- u->CTL &= ~TIVA_CTL_UARTEN;
- div = (((TIVA_SYSCLK * 8) / config->sc_speed) + 1) / 2;
- u->IBRD = div / 64; /* integer portion of the baud rate divisor */
- u->FBRD = div % 64; /* fractional portion of the baud rate divisor */
- u->LCRH = config->sc_lcrh; /* set data format */
- u->IFLS = config->sc_ifls;
- u->CTL |= TIVA_CTL_TXE | TIVA_CTL_RXE | TIVA_CTL_UARTEN;
- u->IM |= TIVA_IM_RXIM | TIVA_IM_TXIM | TIVA_IM_RTIM; /* interrupts enable */
+ uint32_t u = sdp->uart;
+ uint32_t brd;
+ uint32_t speed = config->speed;
+ uint32_t clock_source;
+
+ if (config->ctl & UART_CTL_HSE) {
+ /* High speed mode is enabled, half the baud rate to compensate
+ * for high speed mode.*/
+ speed = (speed + 1) / 2;
+ }
+
+ if ((config->cc & UART_CC_CS_SYSCLK) == UART_CC_CS_SYSCLK) {
+ /* UART is clocked using the SYSCLK.*/
+ clock_source = TIVA_SYSCLK * 8;
+ }
+ else {
+ /* UART is clocked using the PIOSC.*/
+ clock_source = 16000000 * 8;
+ }
+
+ /* Calculate the baud rate divisor */
+ brd = ((clock_source / speed) + 1) / 2;
+
+ /* Disable UART.*/
+ HWREG(u + UART_O_CTL) &= ~UART_CTL_UARTEN;
+
+ /* Set baud rate.*/
+ HWREG(u + UART_O_IBRD) = brd / 64;
+ HWREG(u + UART_O_FBRD) = brd % 64;
+
+ /* Line control/*/
+ HWREG(u + UART_O_LCRH) = config->lcrh;
+
+ /* Select clock source.*/
+ HWREG(u + UART_O_CC) = config->cc & UART_CC_CS_M;
+
+ /* FIFO configuration.*/
+ HWREG(u + UART_O_IFLS) = config->ifls & (UART_IFLS_RX_M | UART_IFLS_TX_M);
+
+ /* Note that some bits are enforced.*/
+ HWREG(u + UART_O_CTL) = config->ctl | UART_CTL_RXE | UART_CTL_TXE | UART_CTL_UARTEN;
+
+ /* Enable interrupts.*/
+ HWREG(u + UART_O_IM) = UART_IM_RXIM | UART_IM_TXIM | UART_IM_RTIM;
}
/**
@@ -135,9 +151,9 @@ static void uart_init(SerialDriver *sdp, const SerialConfig *config)
*
* @param[in] u pointer to an UART I/O block
*/
-static void uart_deinit(UART_TypeDef *u)
+static void uart_deinit(uint32_t u)
{
- u->CTL &= ~TIVA_CTL_UARTEN;
+ HWREG(u + UART_O_CTL) &= ~UART_CTL_UARTEN;
}
/**
@@ -150,13 +166,13 @@ static void set_error(SerialDriver *sdp, uint16_t err)
{
eventflags_t sts = 0;
- if (err & TIVA_MIS_FEMIS)
+ if (err & UART_MIS_FEMIS)
sts |= SD_FRAMING_ERROR;
- if (err & TIVA_MIS_PEMIS)
+ if (err & UART_MIS_PEMIS)
sts |= SD_PARITY_ERROR;
- if (err & TIVA_MIS_BEMIS)
+ if (err & UART_MIS_BEMIS)
sts |= SD_BREAK_DETECTED;
- if (err & TIVA_MIS_OEMIS)
+ if (err & UART_MIS_OEMIS)
sts |= SD_OVERRUN_ERROR;
osalSysLockFromISR();
chnAddFlagsI(sdp, sts);
@@ -174,44 +190,44 @@ static void set_error(SerialDriver *sdp, uint16_t err)
*/
static void serial_serve_interrupt(SerialDriver *sdp)
{
- UART_TypeDef *u = sdp->uart;
- uint16_t mis = u->MIS;
+ uint32_t u = sdp->uart;
+ uint16_t mis = HWREG(u + UART_O_MIS);
- u->ICR = mis; /* clear interrupts */
+ HWREG(u + UART_O_ICR) = mis; /* clear interrupts */
- if (mis & (TIVA_MIS_FEMIS | TIVA_MIS_PEMIS | TIVA_MIS_BEMIS | TIVA_MIS_OEMIS)) {
+ if (mis & (UART_MIS_FEMIS | UART_MIS_PEMIS | UART_MIS_BEMIS | UART_MIS_OEMIS)) {
set_error(sdp, mis);
}
- if ((mis & TIVA_MIS_RXMIS) || (mis & TIVA_MIS_RTMIS)) {
+ if ((mis & UART_MIS_RXMIS) || (mis & UART_MIS_RTMIS)) {
osalSysLockFromISR();
if (iqIsEmptyI(&sdp->iqueue)) {
chnAddFlagsI(sdp, CHN_INPUT_AVAILABLE);
}
osalSysUnlockFromISR();
- while ((u->FR & TIVA_FR_RXFE) == 0) {
+ while ((HWREG(u + UART_O_FR) & UART_FR_RXFE) == 0) {
osalSysLockFromISR();
- if (iqPutI(&sdp->iqueue, u->DR) < Q_OK) {
+ if (iqPutI(&sdp->iqueue, HWREG(u + UART_O_DR)) < Q_OK) {
chnAddFlagsI(sdp, SD_OVERRUN_ERROR);
}
osalSysUnlockFromISR();
}
}
- if (mis & TIVA_MIS_TXMIS) {
- while ((u->FR & TIVA_FR_TXFF) == 0) {
+ if (mis & UART_MIS_TXMIS) {
+ while ((HWREG(u + UART_O_FR) & UART_FR_TXFF) == 0) {
msg_t b;
osalSysLockFromISR();
b = oqGetI(&sdp->oqueue);
osalSysUnlockFromISR();
if (b < Q_OK) {
- u->IM &= ~TIVA_IM_TXIM;
+ HWREG(u + UART_O_IM) &= ~UART_IM_TXIM;
osalSysLockFromISR();
chnAddFlagsI(sdp, CHN_OUTPUT_EMPTY);
osalSysUnlockFromISR();
break;
}
- u->DR = b;
+ HWREG(u + UART_O_DR) = b;
}
}
}
@@ -221,17 +237,18 @@ static void serial_serve_interrupt(SerialDriver *sdp)
*/
static void fifo_load(SerialDriver *sdp)
{
- UART_TypeDef *u = sdp->uart;
+ uint32_t u = sdp->uart;
- while ((u->FR & TIVA_FR_TXFF) == 0) {
+ while ((HWREG(u + UART_O_FR) & UART_FR_TXFF) == 0) {
msg_t b = oqGetI(&sdp->oqueue);
if (b < Q_OK) {
chnAddFlagsI(sdp, CHN_OUTPUT_EMPTY);
return;
}
- u->DR = b;
+ HWREG(u + UART_O_DR) = b;
}
- u->IM |= TIVA_IM_TXIM; /* transmit interrupt enable */
+
+ HWREG(u + UART_O_IM) |= UART_IM_TXIM; /* transmit interrupt enable */
}
/**
@@ -452,42 +469,42 @@ void sd_lld_init(void)
{
#if TIVA_SERIAL_USE_UART0
sdObjectInit(&SD1, NULL, notify1);
- SD1.uart = UART0;
+ SD1.uart = UART0_BASE;
#endif
#if TIVA_SERIAL_USE_UART1
sdObjectInit(&SD2, NULL, notify2);
- SD2.uart = UART1;
+ SD2.uart = UART1_BASE;
#endif
#if TIVA_SERIAL_USE_UART2
sdObjectInit(&SD3, NULL, notify3);
- SD3.uart = UART2;
+ SD3.uart = UART2_BASE;
#endif
#if TIVA_SERIAL_USE_UART3
sdObjectInit(&SD4, NULL, notify4);
- SD4.uart = UART3;
+ SD4.uart = UART3_BASE;
#endif
#if TIVA_SERIAL_USE_UART4
sdObjectInit(&SD5, NULL, notify5);
- SD5.uart = UART4;
+ SD5.uart = UART4_BASE;
#endif
#if TIVA_SERIAL_USE_UART5
sdObjectInit(&SD6, NULL, notify6);
- SD6.uart = UART5;
+ SD6.uart = UART5_BASE;
#endif
#if TIVA_SERIAL_USE_UART6
sdObjectInit(&SD7, NULL, notify7);
- SD7.uart = UART6;
+ SD7.uart = UART6_BASE;
#endif
#if TIVA_SERIAL_USE_UART7
sdObjectInit(&SD8, NULL, notify8);
- SD8.uart = UART7;
+ SD8.uart = UART7_BASE;
#endif
}
@@ -507,9 +524,9 @@ void sd_lld_start(SerialDriver *sdp, const SerialConfig *config)
if (sdp->state == SD_STOP) {
#if TIVA_SERIAL_USE_UART0
if (&SD1 == sdp) {
- SYSCTL->RCGCUART |= (1 << 0);
+ HWREG(SYSCTL_RCGCUART) |= (1 << 0);
- while (!(SYSCTL->PRUART & (1 << 0)))
+ while (!(HWREG(SYSCTL_PRUART) & (1 << 0)))
;
nvicEnableVector(TIVA_UART0_NUMBER, TIVA_SERIAL_UART0_PRIORITY);
@@ -517,9 +534,9 @@ void sd_lld_start(SerialDriver *sdp, const SerialConfig *config)
#endif
#if TIVA_SERIAL_USE_UART1
if (&SD2 == sdp) {
- SYSCTL->RCGCUART |= (1 << 1);
+ HWREG(SYSCTL_RCGCUART) |= (1 << 1);
- while (!(SYSCTL->PRUART & (1 << 1)))
+ while (!(HWREG(SYSCTL_PRUART) & (1 << 1)))
;
nvicEnableVector(TIVA_UART1_NUMBER, TIVA_SERIAL_UART1_PRIORITY);
@@ -527,9 +544,9 @@ void sd_lld_start(SerialDriver *sdp, const SerialConfig *config)
#endif
#if TIVA_SERIAL_USE_UART2
if (&SD3 == sdp) {
- SYSCTL->RCGCUART |= (1 << 2);
+ HWREG(SYSCTL_RCGCUART) |= (1 << 2);
- while (!(SYSCTL->PRUART & (1 << 2)))
+ while (!(HWREG(SYSCTL_PRUART) & (1 << 2)))
;
nvicEnableVector(TIVA_UART2_NUMBER, TIVA_SERIAL_UART2_PRIORITY);
@@ -537,9 +554,9 @@ void sd_lld_start(SerialDriver *sdp, const SerialConfig *config)
#endif
#if TIVA_SERIAL_USE_UART3
if (&SD4 == sdp) {
- SYSCTL->RCGCUART |= (1 << 3);
+ HWREG(SYSCTL_RCGCUART) |= (1 << 3);
- while (!(SYSCTL->PRUART & (1 << 3)))
+ while (!(HWREG(SYSCTL_PRUART) & (1 << 3)))
;
nvicEnableVector(TIVA_UART3_NUMBER, TIVA_SERIAL_UART3_PRIORITY);
@@ -547,9 +564,9 @@ void sd_lld_start(SerialDriver *sdp, const SerialConfig *config)
#endif
#if TIVA_SERIAL_USE_UART4
if (&SD5 == sdp) {
- SYSCTL->RCGCUART |= (1 << 4);
+ HWREG(SYSCTL_RCGCUART) |= (1 << 4);
- while (!(SYSCTL->PRUART & (1 << 4)))
+ while (!(HWREG(SYSCTL_PRUART) & (1 << 4)))
;
nvicEnableVector(TIVA_UART4_NUMBER, TIVA_SERIAL_UART4_PRIORITY);
@@ -557,9 +574,9 @@ void sd_lld_start(SerialDriver *sdp, const SerialConfig *config)
#endif
#if TIVA_SERIAL_USE_UART5
if (&SD6 == sdp) {
- SYSCTL->RCGCUART |= (1 << 5);
+ HWREG(SYSCTL_RCGCUART) |= (1 << 5);
- while (!(SYSCTL->PRUART & (1 << 5)))
+ while (!(HWREG(SYSCTL_PRUART) & (1 << 5)))
;
nvicEnableVector(TIVA_UART5_NUMBER, TIVA_SERIAL_UART5_PRIORITY);
@@ -567,9 +584,9 @@ void sd_lld_start(SerialDriver *sdp, const SerialConfig *config)
#endif
#if TIVA_SERIAL_USE_UART6
if (&SD7 == sdp) {
- SYSCTL->RCGCUART |= (1 << 6);
+ HWREG(SYSCTL_RCGCUART) |= (1 << 6);
- while (!(SYSCTL->PRUART & (1 << 6)))
+ while (!(HWREG(SYSCTL_PRUART) & (1 << 6)))
;
nvicEnableVector(TIVA_UART6_NUMBER, TIVA_SERIAL_UART6_PRIORITY);
@@ -577,9 +594,9 @@ void sd_lld_start(SerialDriver *sdp, const SerialConfig *config)
#endif
#if TIVA_SERIAL_USE_UART7
if (&SD8 == sdp) {
- SYSCTL->RCGCUART |= (1 << 7);
+ HWREG(SYSCTL_RCGCUART) |= (1 << 7);
- while (!(SYSCTL->PRUART & (1 << 7)))
+ while (!(HWREG(SYSCTL_PRUART) & (1 << 7)))
;
nvicEnableVector(TIVA_UART7_NUMBER, TIVA_SERIAL_UART7_PRIORITY);
@@ -602,56 +619,56 @@ void sd_lld_stop(SerialDriver *sdp)
uart_deinit(sdp->uart);
#if TIVA_SERIAL_USE_UART0
if (&SD1 == sdp) {
- SYSCTL->RCGCUART &= ~(1 << 0); /* disable UART0 module */
+ HWREG(SYSCTL_RCGCUART) &= ~(1 << 0); /* disable UART0 module */
nvicDisableVector(TIVA_UART0_NUMBER);
return;
}
#endif
#if TIVA_SERIAL_USE_UART1
if (&SD2 == sdp) {
- SYSCTL->RCGCUART &= ~(1 << 1); /* disable UART1 module */
+ HWREG(SYSCTL_RCGCUART) &= ~(1 << 1); /* disable UART1 module */
nvicDisableVector(TIVA_UART1_NUMBER);
return;
}
#endif
#if TIVA_SERIAL_USE_UART2
if (&SD3 == sdp) {
- SYSCTL->RCGCUART &= ~(1 << 2); /* disable UART2 module */
+ HWREG(SYSCTL_RCGCUART) &= ~(1 << 2); /* disable UART2 module */
nvicDisableVector(TIVA_UART2_NUMBER);
return;
}
#endif
#if TIVA_SERIAL_USE_UART3
if (&SD4 == sdp) {
- SYSCTL->RCGCUART &= ~(1 << 3); /* disable UART3 module */
+ HWREG(SYSCTL_RCGCUART) &= ~(1 << 3); /* disable UART3 module */
nvicDisableVector(TIVA_UART3_NUMBER);
return;
}
#endif
#if TIVA_SERIAL_USE_UART4
if (&SD5 == sdp) {
- SYSCTL->RCGCUART &= ~(1 << 4); /* disable UART4 module */
+ HWREG(SYSCTL_RCGCUART) &= ~(1 << 4); /* disable UART4 module */
nvicDisableVector(TIVA_UART4_NUMBER);
return;
}
#endif
#if TIVA_SERIAL_USE_UART5
if (&SD6 == sdp) {
- SYSCTL->RCGCUART &= ~(1 << 5); /* disable UART5 module */
+ HWREG(SYSCTL_RCGCUART) &= ~(1 << 5); /* disable UART5 module */
nvicDisableVector(TIVA_UART5_NUMBER);
return;
}
#endif
#if TIVA_SERIAL_USE_UART6
if (&SD7 == sdp) {
- SYSCTL->RCGCUART &= ~(1 << 6); /* disable UART6 module */
+ HWREG(SYSCTL_RCGCUART) &= ~(1 << 6); /* disable UART6 module */
nvicDisableVector(TIVA_UART6_NUMBER);
return;
}
#endif
#if TIVA_SERIAL_USE_UART7
if (&SD8 == sdp) {
- SYSCTL->RCGCUART &= ~(1 << 7); /* disable UART7 module */
+ HWREG(SYSCTL_RCGCUART) &= ~(1 << 7); /* disable UART7 module */
nvicDisableVector(TIVA_UART7_NUMBER);
return;
}