24 #define SER_BUF_SIZE (128) // serial buffer in bytes (power 2)
25 #define SER_BUF_MASK (SER_BUF_SIZE-1ul) // buffer size mask
28 #define SER_BUF_RESET(serBuf) (serBuf.rdIdx = serBuf.wrIdx = 0)
29 #define SER_BUF_WR(serBuf, dataIn) (serBuf.data[SER_BUF_MASK & serBuf.wrIdx++] = (dataIn))
30 #define SER_BUF_RD(serBuf) (serBuf.data[SER_BUF_MASK & serBuf.rdIdx++])
31 #define SER_BUF_EMPTY(serBuf) (serBuf.rdIdx == serBuf.wrIdx)
32 #define SER_BUF_FULL(serBuf) (serBuf.rdIdx == serBuf.wrIdx+1)
33 #define SER_BUF_COUNT(serBuf) (SER_BUF_MASK & (serBuf.wrIdx - serBuf.rdIdx))
37 unsigned char data[SER_BUF_SIZE];
42 unsigned long ser_txRestart;
43 unsigned short ser_lineState;
50 void ser_OpenPort (
void) {
52 NVIC_DisableIRQ(UART_IRQn);
54 LPC_IOCON->PIO1_6 &= ~0x07;
55 LPC_IOCON->PIO1_6 |= 0x01;
56 LPC_IOCON->PIO1_7 &= ~0x07;
57 LPC_IOCON->PIO1_7 |= 0x01;
59 LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
60 LPC_SYSCON->UARTCLKDIV = 0x1;
67 void ser_ClosePort (
void) {
68 LPC_IOCON->PIO1_6 &= ~0x07;
69 LPC_IOCON->PIO1_7 &= ~0x07;
73 NVIC_DisableIRQ(UART_IRQn);
80 void ser_InitPort (
unsigned long baudrate,
unsigned int databits,
81 unsigned int parity,
unsigned int stopbits) {
83 uint8_t lcr_p, lcr_s, lcr_d;
133 SER_BUF_RESET(ser_out);
134 SER_BUF_RESET(ser_in);
138 Fdiv = LPC_SYSCON->UARTCLKDIV;
139 dll = (((SystemCoreClock/LPC_SYSCON->SYSAHBCLKDIV)/Fdiv)/16)/baudrate ;
141 LPC_UART->LCR = 0x80 | lcr_d | lcr_p | lcr_s;
143 LPC_UART->DLM = (dll >> 8);
144 LPC_UART->LCR = 0x00 | lcr_d | lcr_p | lcr_s;
145 LPC_UART->IER = 0x03;
147 LPC_UART->FCR = 0x07;
151 NVIC_EnableIRQ(UART_IRQn);
158 int ser_Read (
char *buffer,
const int *length) {
159 int bytesToRead, bytesRead;
162 bytesToRead = *length;
163 bytesToRead = (bytesToRead < (*length)) ? bytesToRead : (*length);
164 bytesRead = bytesToRead;
166 while (bytesToRead--) {
167 while (SER_BUF_EMPTY(ser_in));
168 *buffer++ = SER_BUF_RD(ser_in);
176 int ser_Write (
const char *buffer,
int *length) {
177 int bytesToWrite, bytesWritten;
180 bytesToWrite = *length;
181 bytesWritten = bytesToWrite;
183 while (!SER_BUF_EMPTY(ser_out));
184 while (bytesToWrite) {
185 SER_BUF_WR(ser_out, *buffer++);
191 LPC_UART->THR = SER_BUF_RD(ser_out);
194 return (bytesWritten);
200 void ser_AvailChar (
int *availChar) {
202 *availChar = SER_BUF_COUNT(ser_in);
209 void ser_LineState (
unsigned short *lineState) {
211 *lineState = ser_lineState;