add putchar to support printf

This commit is contained in:
2016-09-06 15:28:49 +02:00
parent 6ed2143b98
commit 227f8c577b
2 changed files with 75 additions and 0 deletions

61
src/uart.c Normal file
View File

@ -0,0 +1,61 @@
#include <stdint.h>
#include <stdio.h>
#include <msp340g2553.h>
#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;
}

14
src/uart.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef UART_H_
#define UART_H_
#include <stdint.h>
#define UART_TX_BUFFER_SIZE 20
void uartInit(void *handleArg);
void uartExec(void *handleArg);
int putchar(int character);
#endif // UART_H_