diff --git a/src/uart.c b/src/uart.c new file mode 100644 index 0000000..5080d81 --- /dev/null +++ b/src/uart.c @@ -0,0 +1,61 @@ +#include +#include +#include + +#include "uart.h" +#include "PontCoopScheduler.h" + +uint8_t txBuffer[UART_TX_BUFFER_SIZE+5]; +uint8_t txBufferReadIdx = 0; +uint8_t txBufferWriteIdx = 0; + + +void uartInit(void *handleArg) { + UCA0CTL1 |= UCSWRST; + + P1SEL |= BIT1 | BIT2; + P1SEL2 |= BIT1 | BIT2; + UCA0CTL1 |= UCSSEL0; // ACLK + UCA0BR0 = 13; + UCA0BR1 = 0; + UCA0MCTL = UCBSR1 | UCBSR2; + + UCA0CTL1 &= ~UCSWRST; +} + +void uartExec(void *handleArg) { + if (txBufferReadIdx != txBufferWriteIdx) { + if ((IFG2 & UCA0TXIE) != 0) { + UCA0TXBUF = txBuffer[txBufferReadIdx]; + txBufferReadIdx++; + if (txBufferReadIdx > UART_TX_BUFFER_SIZE) { + txBufferReadIdx = 0; + } + } + } else { + // stop transmitting + schDel(uartExec, NULL); + } +} + +int putchar(int character) { + if (((txBufferWriteIdx == (UART_TX_BUFFER_SIZE - 1)) && (txBufferReadIdx == UART_TX_BUFFER_SIZE)) || + ((txBufferWriteIdx != (UART_TX_BUFFER_SIZE - 1)) && (txBufferReadIdx == (txBufferWriteIdx + 1)))) { + return EOF; + } + txBuffer[txBufferWriteIdx] = (unsigned char)character; + txBufferWriteIdx++; + + if (txBufferWriteIdx > UART_TX_BUFFER_SIZE) { + txBufferWriteIdx = 0; + } + + // start transmitting + schAdd(uartExec, NULL, 0, 5); + + return character; +} + + + + diff --git a/src/uart.h b/src/uart.h new file mode 100644 index 0000000..b07309a --- /dev/null +++ b/src/uart.h @@ -0,0 +1,14 @@ +#ifndef UART_H_ +#define UART_H_ + +#include + + +#define UART_TX_BUFFER_SIZE 20 + + +void uartInit(void *handleArg); +void uartExec(void *handleArg); +int putchar(int character); + +#endif // UART_H_