initial
This commit is contained in:
49
src/main.cpp
Normal file
49
src/main.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* main.cpp
|
||||
*
|
||||
* Created on: 08. 02. 2015
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#include <msp430g2553.h>
|
||||
#include <stdint.h>
|
||||
#include <intrinsics.h>
|
||||
#include <isr_compat.h>
|
||||
|
||||
#include "uart.h"
|
||||
#include "time.h"
|
||||
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
WDTCTL = WDTPW | WDTHOLD;
|
||||
|
||||
// P1DIR |= BIT6;
|
||||
// P1SEL |= BIT6;
|
||||
// P1SEL2 &= ~BIT6;
|
||||
//
|
||||
// TACCR0 = 1024;
|
||||
// TACCR1 = 128;
|
||||
// TACCTL0 = CCIE | OUTMOD_7;
|
||||
// TACTL = MC_1 | ID_0 | TASSEL_1 | TACLR;
|
||||
|
||||
uartInit();
|
||||
timeInit();
|
||||
|
||||
__enable_interrupt();
|
||||
|
||||
uartWrite('W');
|
||||
|
||||
|
||||
uint32_t c = 0;
|
||||
while (1) {
|
||||
c++;
|
||||
if (c >= 100000) {
|
||||
c = 0;
|
||||
uartWrite('x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
53
src/time.cpp
Normal file
53
src/time.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* time.c
|
||||
*
|
||||
* Created on: 20.05.2014
|
||||
* Author: wn
|
||||
*/
|
||||
#include <msp430g2553.h>
|
||||
#include <isr_compat.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "time.h"
|
||||
|
||||
|
||||
volatile unsigned long timestamp;
|
||||
|
||||
//ISR(TIMER0_A0, TA0_ISR) {
|
||||
// static uint8_t state = 0;
|
||||
//
|
||||
// timestamp++;
|
||||
//
|
||||
// switch (state) {
|
||||
// case 0:
|
||||
// P1OUT = BIT0;
|
||||
// state = 1;
|
||||
// break;
|
||||
// case 1:
|
||||
// P1OUT = BIT6;
|
||||
// state = 0;
|
||||
// break;
|
||||
// default:
|
||||
// state = 0;
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
void timeInit() {
|
||||
timestamp = 0;
|
||||
|
||||
P1DIR |= BIT6;
|
||||
P1SEL |= BIT6;
|
||||
P1OUT = 0;
|
||||
|
||||
TACCR0 = 1024;
|
||||
TACCR1 = 768;
|
||||
TACCTL1 = OUTMOD_7;
|
||||
TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR;
|
||||
}
|
||||
|
||||
|
||||
unsigned long millis() {
|
||||
return timestamp;
|
||||
}
|
||||
|
19
src/time.h
Normal file
19
src/time.h
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* time.h
|
||||
*
|
||||
* Created on: 20.05.2014
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef TIME_H_
|
||||
#define TIME_H_
|
||||
|
||||
|
||||
|
||||
void timeInit();
|
||||
unsigned long millis();
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* TIME_H_ */
|
128
src/uart.cpp
Normal file
128
src/uart.cpp
Normal file
@ -0,0 +1,128 @@
|
||||
#include <msp430g2553.h>
|
||||
#include <stdio.h>
|
||||
#include <isr_compat.h>
|
||||
|
||||
#include "uart.h"
|
||||
|
||||
|
||||
|
||||
volatile uint8_t txBuffer[UART_TX_BUFFER_SIZE+5];
|
||||
|
||||
volatile uint8_t txBufferReadIdx = 0;
|
||||
volatile uint8_t txBufferWriteIdx = 0;
|
||||
|
||||
volatile uint8_t rxBuffer[UART_RX_BUFFER_SIZE+5];
|
||||
volatile uint8_t rxBufferReadIdx = 0;
|
||||
volatile uint8_t rxBufferWriteIdx = 0;
|
||||
|
||||
|
||||
static inline void enableDataRegisterEmptyInterrupt() {
|
||||
IE2 |= UCA0TXIE;
|
||||
}
|
||||
|
||||
static inline void disableDataRegisterEmptyInterrupt() {
|
||||
IE2 &= ~UCA0TXIE;
|
||||
}
|
||||
|
||||
void uartInit() {
|
||||
UCA0CTL1 |= UCSWRST;
|
||||
|
||||
P1SEL = BIT1 + BIT2; // select secondary function TX, RX
|
||||
P1SEL2 = BIT1 + BIT2; // dti
|
||||
|
||||
UCA0CTL0 |= UCPEN | UCPAR; // even parity
|
||||
UCA0CTL1 |= UCSSEL0; // ACLK
|
||||
|
||||
UCA0BR0 = 13; // divider for 2400@32768
|
||||
UCA0BR1 = 0;
|
||||
|
||||
UCA0MCTL = UCBRS1 | UCBRS2; // modulator for 2400@32768
|
||||
|
||||
UCA0CTL1 &= ~UCSWRST;
|
||||
|
||||
IE2 |= UCA0RXIE;
|
||||
}
|
||||
|
||||
|
||||
void uartWrite(uint8_t o) {
|
||||
if (txBufferWriteIdx == (UART_TX_BUFFER_SIZE - 1)) {
|
||||
while (txBufferReadIdx == UART_TX_BUFFER_SIZE);
|
||||
} else {
|
||||
while (txBufferReadIdx == (txBufferWriteIdx + 1));
|
||||
}
|
||||
|
||||
txBuffer[txBufferWriteIdx] = o;
|
||||
txBufferWriteIdx++;
|
||||
|
||||
if (txBufferWriteIdx > UART_TX_BUFFER_SIZE) {
|
||||
txBufferWriteIdx = 0;
|
||||
}
|
||||
|
||||
enableDataRegisterEmptyInterrupt();
|
||||
}
|
||||
|
||||
|
||||
//int uartPutchar(char c, FILE *stream) {
|
||||
// if (c == '\n')
|
||||
// uartPutchar('\r', stream);
|
||||
// uartWrite((uint8_t) c);
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
|
||||
|
||||
ISR(USCIAB0TX, UART_TX_ISR) {
|
||||
if ((IFG2 & UCA0TXIE) != 0) {
|
||||
if (txBufferReadIdx != txBufferWriteIdx) {
|
||||
UCA0TXBUF = txBuffer[txBufferReadIdx];
|
||||
txBufferReadIdx++;
|
||||
if (txBufferReadIdx > UART_TX_BUFFER_SIZE) {
|
||||
txBufferReadIdx = 0;
|
||||
}
|
||||
} else {
|
||||
disableDataRegisterEmptyInterrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ISR(USCIAB0RX, UART_RX_ISR) {
|
||||
while ((IFG2 & UCA0RXIE) != 0) {
|
||||
if (rxBufferWriteIdx == UART_RX_BUFFER_SIZE - 1) {
|
||||
if (rxBufferReadIdx == UART_RX_BUFFER_SIZE) {
|
||||
// rx buffer overflow
|
||||
}
|
||||
} else {
|
||||
if (rxBufferReadIdx == rxBufferWriteIdx + 1) {
|
||||
// rx buffer overflow
|
||||
}
|
||||
}
|
||||
|
||||
rxBuffer[rxBufferWriteIdx] = UCA0RXBUF;
|
||||
rxBufferWriteIdx++;
|
||||
|
||||
if (rxBufferWriteIdx > UART_RX_BUFFER_SIZE) {
|
||||
rxBufferWriteIdx = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t uartHasChar() {
|
||||
return rxBufferWriteIdx != rxBufferReadIdx;
|
||||
}
|
||||
|
||||
uint8_t uartGetChar() {
|
||||
uint8_t c = rxBuffer[rxBufferReadIdx];
|
||||
rxBufferReadIdx++;
|
||||
if (rxBufferReadIdx > UART_RX_BUFFER_SIZE) {
|
||||
rxBufferReadIdx = 0;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
int uartRead() {
|
||||
int res = -1;
|
||||
if (uartHasChar()) {
|
||||
res = uartGetChar();
|
||||
}
|
||||
return res;
|
||||
}
|
22
src/uart.h
Normal file
22
src/uart.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef UART_H_
|
||||
#define UART_H_
|
||||
|
||||
#include <stdint.h>
|
||||
// #include <stdio.h>
|
||||
|
||||
|
||||
|
||||
#define UART_TX_BUFFER_SIZE 32
|
||||
#define UART_RX_BUFFER_SIZE 16
|
||||
|
||||
|
||||
void uartInit();
|
||||
// int uartPutchar(char c, FILE *stream);
|
||||
void uartWrite(uint8_t o);
|
||||
|
||||
uint8_t uartHasChar();
|
||||
uint8_t uartGetChar();
|
||||
|
||||
int uartRead();
|
||||
|
||||
#endif /* UART_H_ */
|
Reference in New Issue
Block a user