aboutsummaryrefslogtreecommitdiffstats
path: root/src/lpc176x/serial.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2021-05-25 12:31:42 -0400
committerKevin O'Connor <kevin@koconnor.net>2021-05-25 12:44:13 -0400
commitce8fe615edbe06d840e66ba111079b2897c8a9fd (patch)
tree985065ba6c56eb3cb7cecb7c24b8289909d426ec /src/lpc176x/serial.c
parent6a3c357a50e7ce4f612a66dfdc5e03e71433eabf (diff)
downloadkutter-ce8fe615edbe06d840e66ba111079b2897c8a9fd.tar.gz
kutter-ce8fe615edbe06d840e66ba111079b2897c8a9fd.tar.xz
kutter-ce8fe615edbe06d840e66ba111079b2897c8a9fd.zip
lpc176x: Move pin definitions together in serial.c
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/lpc176x/serial.c')
-rw-r--r--src/lpc176x/serial.c50
1 files changed, 28 insertions, 22 deletions
diff --git a/src/lpc176x/serial.c b/src/lpc176x/serial.c
index 63a2bd7d..65f08ba7 100644
--- a/src/lpc176x/serial.c
+++ b/src/lpc176x/serial.c
@@ -12,33 +12,41 @@
#include "internal.h" // gpio_peripheral
#include "sched.h" // DECL_INIT
+DECL_CONSTANT_STR("RESERVE_PINS_serial", "P0.3,P0.2");
+#define GPIO_Rx GPIO(0, 3)
+#define GPIO_Tx GPIO(0, 2)
+#define GPIO_FUNCTION_UARTx 1
+#define LPC_UARTx LPC_UART0
+#define UARTx_IRQn UART0_IRQn
+#define PCLK_UARTx PCLK_UART0
+
// Write tx bytes to the serial port
static void
kick_tx(void)
{
for (;;) {
- if (!(LPC_UART0->LSR & (1<<5))) {
+ if (!(LPC_UARTx->LSR & (1<<5))) {
// Output fifo full - enable tx irq
- LPC_UART0->IER = 0x03;
+ LPC_UARTx->IER = 0x03;
break;
}
uint8_t data;
int ret = serial_get_tx_byte(&data);
if (ret) {
// No more data to send - disable tx irq
- LPC_UART0->IER = 0x01;
+ LPC_UARTx->IER = 0x01;
break;
}
- LPC_UART0->THR = data;
+ LPC_UARTx->THR = data;
}
}
void
-UART0_IRQHandler(void)
+UARTx_IRQHandler(void)
{
- uint32_t iir = LPC_UART0->IIR, status = iir & 0x0f;
+ uint32_t iir = LPC_UARTx->IIR, status = iir & 0x0f;
if (status == 0x04)
- serial_rx_byte(LPC_UART0->RBR);
+ serial_rx_byte(LPC_UARTx->RBR);
else if (status == 0x02)
kick_tx();
}
@@ -46,37 +54,35 @@ UART0_IRQHandler(void)
void
serial_enable_tx_irq(void)
{
- if (LPC_UART0->LSR & (1<<5)) {
+ if (LPC_UARTx->LSR & (1<<5)) {
irqstatus_t flag = irq_save();
kick_tx();
irq_restore(flag);
}
}
-DECL_CONSTANT_STR("RESERVE_PINS_serial", "P0.3,P0.2");
-
void
serial_init(void)
{
// Setup baud
- LPC_UART0->LCR = (1<<7); // set DLAB bit
- enable_pclock(PCLK_UART0);
- uint32_t pclk = get_pclock_frequency(PCLK_UART0);
+ LPC_UARTx->LCR = (1<<7); // set DLAB bit
+ enable_pclock(PCLK_UARTx);
+ uint32_t pclk = get_pclock_frequency(PCLK_UARTx);
uint32_t div = pclk / (CONFIG_SERIAL_BAUD * 16);
- LPC_UART0->DLL = div & 0xff;
- LPC_UART0->DLM = (div >> 8) & 0xff;
- LPC_UART0->FDR = 0x10;
- LPC_UART0->LCR = 3; // 8N1 ; clear DLAB bit
+ LPC_UARTx->DLL = div & 0xff;
+ LPC_UARTx->DLM = (div >> 8) & 0xff;
+ LPC_UARTx->FDR = 0x10;
+ LPC_UARTx->LCR = 3; // 8N1 ; clear DLAB bit
// Enable fifo
- LPC_UART0->FCR = 0x01;
+ LPC_UARTx->FCR = 0x01;
// Setup pins
- gpio_peripheral(GPIO(0, 3), 1, 0);
- gpio_peripheral(GPIO(0, 2), 1, 0);
+ gpio_peripheral(GPIO_Rx, GPIO_FUNCTION_UARTx, 0);
+ gpio_peripheral(GPIO_Tx, GPIO_FUNCTION_UARTx, 0);
// Enable receive irq
- armcm_enable_irq(UART0_IRQHandler, UART0_IRQn, 0);
- LPC_UART0->IER = 0x01;
+ armcm_enable_irq(UARTx_IRQHandler, UARTx_IRQn, 0);
+ LPC_UARTx->IER = 0x01;
}
DECL_INIT(serial_init);