Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Lpc17xx40xxUart.cxx
Go to the documentation of this file.
1
35#include "Lpc17xx40xxUart.hxx"
36
37#include <algorithm>
38
39#if defined (CHIP_LPC177X_8X) || defined (CHIP_LPC407X_8X)
40LpcUart *LpcUart::instances[5] = {NULL};
41#else
42LpcUart *LpcUart::instances[4] = {NULL};
43#endif
44
50LpcUart::LpcUart(const char *name, LPC_USART_T *base, IRQn_Type interrupt)
51 : Serial(name)
52 , base(base)
53 , interrupt(interrupt)
54{
55 switch (interrupt)
56 {
57 default:
58 HASSERT(0);
59 case UART0_IRQn:
60 instances[0] = this;
61 break;
62 case UART1_IRQn:
63 instances[1] = this;
64 break;
65 case UART2_IRQn:
66 instances[2] = this;
67 break;
68 case UART3_IRQn:
69 instances[3] = this;
70 break;
71#if defined (CHIP_LPC177X_8X) || defined (CHIP_LPC407X_8X)
72 case UART4_IRQn:
73 instances[4] = this;
74 break;
75#endif
76 }
77
78 /* should already be disabled, but just in case */
79 NVIC_DisableIRQ(interrupt);
80 /* We set the priority so that it is slightly lower than the highest needed
81 * for FreeRTOS compatibility. This will ensure that CAN interrupts take
82 * precedence over UART. */
83 NVIC_SetPriority(interrupt,
84 std::min(0xff, configKERNEL_INTERRUPT_PRIORITY + 0x20));
85}
86
90{
91 Chip_UART_Init(base);
92 Chip_UART_SetBaud(base, 115200);
93 Chip_UART_ConfigData(base, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT));
94 Chip_UART_SetupFIFOS(base, (UART_FCR_FIFO_EN | UART_FCR_RX_RS |
95 UART_FCR_TX_RS | UART_FCR_TRG_LEV0));
96 Chip_UART_TXEnable(base);
97 Chip_UART_IntEnable(base, (UART_IER_RBRINT | UART_IER_RLSINT));
98 NVIC_EnableIRQ(interrupt);
99}
100
104{
105 NVIC_DisableIRQ(interrupt);
106 Chip_UART_IntDisable(base, (UART_IER_RBRINT | UART_IER_RLSINT));
107 Chip_UART_TXDisable(base);
108 Chip_UART_DeInit(base);
109}
110
114{
115 if ((Chip_UART_GetIntsEnabled(base) & UART_IER_THREINT) == 0)
116 {
117 uint8_t data = 0;
118 if (txBuf->get(&data, 1))
119 {
120 Chip_UART_SendByte(base, (uint8_t)data);
121 Chip_UART_IntEnable(base, UART_IER_THREINT);
123 }
124 }
125}
126
130{
134 /* receive charaters as long as we can */
135 while (Chip_UART_ReadLineStatus(base) & UART_LSR_RDR)
136 {
137 uint8_t data = Chip_UART_ReadByte(base);
138 if (rxBuf->put(&data, 1) == 0)
139 {
140 overrunCount++;
141 }
143 }
144
145 /* tranmit a character if we have pending tx data */
146 if (Chip_UART_GetIntsEnabled(base) & UART_IER_THREINT)
147 {
151 while (Chip_UART_ReadLineStatus(base) & UART_LSR_THRE)
152 {
153 uint8_t data;
154 if (txBuf->get(&data, 1) != 0)
155 {
156 Chip_UART_SendByte(base, (uint8_t)data);
158 }
159 else
160 {
161 /* no more data pending */
162 Chip_UART_IntDisable(base, UART_IER_THREINT);
163 break;
164 }
165 }
166 }
167}
168
169extern "C" {
176
183
190
197
198#if defined (CHIP_LPC177X_8X) || defined (CHIP_LPC407X_8X)
202{
204}
205#endif
206
207} // extern C
void uart2_interrupt_handler(void)
UART2 interrupt handler.
void uart0_interrupt_handler(void)
UART0 interrupt handler.
void uart3_interrupt_handler(void)
UART3 interrupt handler.
void uart1_interrupt_handler(void)
UART1 interrupt handler.
void uart4_interrupt_handler(void)
UART4 interrupt handler.
Definition TivaUart.cxx:421
void signal_condition()
Signal the wakeup condition.
void signal_condition_from_isr()
Signal the wakeup condition from an ISR context.
size_t get(T *buf, size_t items)
remove a number of items from the buffer.
size_t put(const T *buf, size_t items)
Insert a number of items to the buffer.
Specialization of Serial driver for LPC17xx and LPC40xx UART.
LpcUart()
Default constructor.
void disable() override
function to disable device
static LpcUart * instances[4]
Instance pointers help us get context from the interrupt handler(s)
LPC_USART_T * base
base address of this device
IRQn_Type interrupt
interrupt of this device
void tx_char() override
Try and transmit a message.
void enable() override
function to enable device
void interrupt_handler()
handle an interrupt.
Private data for a serial device.
Definition Serial.hxx:46
DeviceBuffer< uint8_t > * rxBuf
receive buffer
Definition Serial.hxx:85
DeviceBuffer< uint8_t > * txBuf
transmit buffer
Definition Serial.hxx:84
unsigned int overrunCount
overrun count
Definition Serial.hxx:86
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138