add uart for debug

This commit is contained in:
hg
2016-01-26 20:23:24 +01:00
parent 6b4849f086
commit b96045e0c9
5 changed files with 262 additions and 90 deletions

View File

@ -72,7 +72,7 @@ void engineInit() {
P1SEL |= BIT6; P1SEL |= BIT6;
TACCR0 = TICK; TACCR0 = TICK;
TACCR1 = pwmVal; TACCR1 = 0;
TACCTL0 = CCIE; TACCTL0 = CCIE;
TACCTL1 = OUTMOD_7; TACCTL1 = OUTMOD_7;

View File

@ -3,95 +3,109 @@
#include "hmi.h" #include "hmi.h"
#include "engine.h" #include "engine.h"
#include "uart.h"
class SwitchPort { class SwitchPort {
public: public:
enum e_Value { e_NIL, e_SHORT, e_LONG }; enum e_Value { e_NIL, e_SHORT, e_LONG };
SwitchPort(uint16_t bit, uint16_t inRegister) : m_bit(bit), m_state(0), m_cnt(0), m_inRegister(inRegister) { SwitchPort(uint16_t bit, uint16_t inRegister) : m_bit(bit), m_state(0), m_cnt(0), m_inRegister(inRegister) {
}; };
e_Value get() { e_Value get() {
e_Value result = e_NIL; e_Value result = e_NIL;
switch (m_state) { switch (m_state) {
case 0: case 0:
if (getInValue()) { //uartWrite('0');
m_state = 1; if (getInValue()) {
} else { m_state = 1;
m_cnt++; } else {
} m_cnt++;
break; }
case 1: break;
if (m_cnt > LONG_THRESHOLD) { case 1:
result = e_LONG; //uartWrite('1');
m_state = 2; if (m_cnt > LONG_THRESHOLD) {
} else if (m_cnt > SHORT_THRESHOLD) { result = e_LONG;
result = e_SHORT; m_state = 2;
} else { } else if (m_cnt > SHORT_THRESHOLD) {
m_state = 0; result = e_SHORT;
} } else {
m_cnt = 0; m_state = 0;
break; }
case 2: m_cnt = 0;
if (getInValue()) { break;
m_cnt++; case 2:
} else { //uartWrite('2');
m_cnt = 0; if (getInValue()) {
} m_cnt++;
if (m_cnt > COOLDOWN_THRESHOLD) { } else {
m_state = 0; m_cnt = 0;
m_cnt = 0; }
} if (m_cnt > COOLDOWN_THRESHOLD) {
break; m_state = 0;
default: m_cnt = 0;
m_state = 0; }
break; break;
} default:
return result; m_state = 0;
}; break;
}
return result;
};
protected: protected:
virtual bool getInValue() =0; virtual bool getInValue() {
uint8_t m_inRegister; //uartWrite('x');
return true;
};
uint8_t m_inRegister;
private: private:
const uint16_t SHORT_THRESHOLD = 100; static const uint16_t SHORT_THRESHOLD;
const uint16_t LONG_THRESHOLD = 300; static const uint16_t LONG_THRESHOLD;
const uint16_t COOLDOWN_THRESHOLD = 20; static const uint16_t COOLDOWN_THRESHOLD;
uint16_t m_bit; uint16_t m_bit;
uint8_t m_state; uint8_t m_state;
uint16_t m_cnt; uint16_t m_cnt;
}; };
const uint16_t SwitchPort::SHORT_THRESHOLD = 100;
const uint16_t SwitchPort::LONG_THRESHOLD = 300;
const uint16_t SwitchPort::COOLDOWN_THRESHOLD = 20;
class SwitchPort2 : public SwitchPort { class SwitchPort2 : public SwitchPort {
public: public:
SwitchPort2(uint16_t bit) : SwitchPort(bit, 2) { SwitchPort2(uint16_t bit) : SwitchPort(bit, 2) {
P2REN |= bit; P2REN |= bit;
P2DIR &= ~bit; P2DIR &= ~bit;
P2SEL &= ~bit; P2SEL &= ~bit;
}; };
protected: protected:
virtual bool getInValue() { virtual bool getInValue() {
return P2IN & m_inRegister; bool r = P2IN & m_inRegister;
}; if (! r) uartWrite('-');
return r;
};
}; };
class SwitchPort1 : public SwitchPort { class SwitchPort1 : public SwitchPort {
public: public:
SwitchPort1(uint16_t bit) : SwitchPort(bit, 1) { SwitchPort1(uint16_t bit) : SwitchPort(bit, 1) {
P1REN |= bit; P1REN |= bit;
P1DIR &= ~bit; P1DIR &= ~bit;
P1SEL &= ~bit; P1SEL &= ~bit;
}; };
protected: protected:
virtual bool getInValue(uint8_t bit) { virtual bool getInValue(uint8_t bit) {
return P1IN & m_inRegister; bool r = P1IN & m_inRegister;
}; if (! r) uartWrite('-');
}; return r;
};
}; };
SwitchPort intensitySwitch = SwitchPort2(BIT0); SwitchPort intensitySwitch = SwitchPort2(BIT1);
SwitchPort onTimeSwitch = SwitchPort2(BIT1); SwitchPort onTimeSwitch = SwitchPort2(BIT2);
SwitchPort offTimeSwitch = SwitchPort2(BIT2); SwitchPort offTimeSwitch = SwitchPort2(BIT3);
SwitchPort modeSwitch = SwitchPort2(BIT3); SwitchPort modeSwitch = SwitchPort2(BIT4);
int16_t intensity = 0; int16_t intensity = 0;
uint16_t maxIntensity = 0; uint16_t maxIntensity = 0;
@ -100,22 +114,23 @@ const uint16_t intensityStepCnt = 8;
void hmiInit() { void hmiInit() {
maxIntensity = engineMaxPwmValue(); maxIntensity = engineMaxPwmValue();
intensityStep = maxIntensity / intensityStepCnt; intensityStep = maxIntensity / intensityStepCnt;
} }
void hmiExec() { void hmiExec() {
if (SwitchPort::e_SHORT == intensitySwitch.get()) { if (SwitchPort::e_SHORT == intensitySwitch.get()) {
intensity += intensityStep; intensity += intensityStep;
if (intensity > maxIntensity) { if (intensity > maxIntensity) {
intensity = maxIntensity; intensity = maxIntensity;
} }
engineSetPwmValue(intensity);
} else if (SwitchPort::e_LONG == intensitySwitch.get()) { engineSetPwmValue(intensity);
intensity -= intensityStep; } else if (SwitchPort::e_LONG == intensitySwitch.get()) {
if (intensity < 0) { intensity -= intensityStep;
intensity = 0; if (intensity < 0) {
} intensity = 0;
engineSetPwmValue(intensity); }
} engineSetPwmValue(intensity);
}
} }

View File

@ -11,19 +11,26 @@
#include <isr_compat.h> #include <isr_compat.h>
#include "engine.h" #include "engine.h"
#include "hmi.h"
#include "uart.h"
int main() { int main() {
WDTCTL = WDTPW | WDTHOLD; WDTCTL = WDTPW | WDTHOLD;
engineInit(); //engineInit();
hmiInit();
uartInit();
__enable_interrupt(); __enable_interrupt();
uartWrite('W');
while (1) { while (1) {
hmiExec();
//uartWrite('.');
} }
} }

128
src/uart.cpp Normal file
View 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
View 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_ */