add uart for debug
This commit is contained in:
parent
6b4849f086
commit
b96045e0c9
@ -72,7 +72,7 @@ void engineInit() {
|
||||
P1SEL |= BIT6;
|
||||
|
||||
TACCR0 = TICK;
|
||||
TACCR1 = pwmVal;
|
||||
TACCR1 = 0;
|
||||
|
||||
TACCTL0 = CCIE;
|
||||
TACCTL1 = OUTMOD_7;
|
||||
|
187
src/hmi.cpp
187
src/hmi.cpp
@ -3,95 +3,109 @@
|
||||
|
||||
#include "hmi.h"
|
||||
#include "engine.h"
|
||||
#include "uart.h"
|
||||
|
||||
|
||||
|
||||
|
||||
class SwitchPort {
|
||||
public:
|
||||
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) {
|
||||
};
|
||||
e_Value get() {
|
||||
e_Value result = e_NIL;
|
||||
switch (m_state) {
|
||||
case 0:
|
||||
if (getInValue()) {
|
||||
m_state = 1;
|
||||
} else {
|
||||
m_cnt++;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (m_cnt > LONG_THRESHOLD) {
|
||||
result = e_LONG;
|
||||
m_state = 2;
|
||||
} else if (m_cnt > SHORT_THRESHOLD) {
|
||||
result = e_SHORT;
|
||||
} else {
|
||||
m_state = 0;
|
||||
}
|
||||
m_cnt = 0;
|
||||
break;
|
||||
case 2:
|
||||
if (getInValue()) {
|
||||
m_cnt++;
|
||||
} else {
|
||||
m_cnt = 0;
|
||||
}
|
||||
if (m_cnt > COOLDOWN_THRESHOLD) {
|
||||
m_state = 0;
|
||||
m_cnt = 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
m_state = 0;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
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) {
|
||||
};
|
||||
e_Value get() {
|
||||
e_Value result = e_NIL;
|
||||
switch (m_state) {
|
||||
case 0:
|
||||
//uartWrite('0');
|
||||
if (getInValue()) {
|
||||
m_state = 1;
|
||||
} else {
|
||||
m_cnt++;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
//uartWrite('1');
|
||||
if (m_cnt > LONG_THRESHOLD) {
|
||||
result = e_LONG;
|
||||
m_state = 2;
|
||||
} else if (m_cnt > SHORT_THRESHOLD) {
|
||||
result = e_SHORT;
|
||||
} else {
|
||||
m_state = 0;
|
||||
}
|
||||
m_cnt = 0;
|
||||
break;
|
||||
case 2:
|
||||
//uartWrite('2');
|
||||
if (getInValue()) {
|
||||
m_cnt++;
|
||||
} else {
|
||||
m_cnt = 0;
|
||||
}
|
||||
if (m_cnt > COOLDOWN_THRESHOLD) {
|
||||
m_state = 0;
|
||||
m_cnt = 0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
m_state = 0;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
protected:
|
||||
virtual bool getInValue() =0;
|
||||
uint8_t m_inRegister;
|
||||
virtual bool getInValue() {
|
||||
//uartWrite('x');
|
||||
return true;
|
||||
};
|
||||
uint8_t m_inRegister;
|
||||
private:
|
||||
const uint16_t SHORT_THRESHOLD = 100;
|
||||
const uint16_t LONG_THRESHOLD = 300;
|
||||
const uint16_t COOLDOWN_THRESHOLD = 20;
|
||||
uint16_t m_bit;
|
||||
uint8_t m_state;
|
||||
uint16_t m_cnt;
|
||||
static const uint16_t SHORT_THRESHOLD;
|
||||
static const uint16_t LONG_THRESHOLD;
|
||||
static const uint16_t COOLDOWN_THRESHOLD;
|
||||
uint16_t m_bit;
|
||||
uint8_t m_state;
|
||||
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 {
|
||||
public:
|
||||
SwitchPort2(uint16_t bit) : SwitchPort(bit, 2) {
|
||||
P2REN |= bit;
|
||||
P2DIR &= ~bit;
|
||||
P2SEL &= ~bit;
|
||||
};
|
||||
SwitchPort2(uint16_t bit) : SwitchPort(bit, 2) {
|
||||
P2REN |= bit;
|
||||
P2DIR &= ~bit;
|
||||
P2SEL &= ~bit;
|
||||
};
|
||||
protected:
|
||||
virtual bool getInValue() {
|
||||
return P2IN & m_inRegister;
|
||||
};
|
||||
virtual bool getInValue() {
|
||||
bool r = P2IN & m_inRegister;
|
||||
if (! r) uartWrite('-');
|
||||
return r;
|
||||
};
|
||||
};
|
||||
class SwitchPort1 : public SwitchPort {
|
||||
public:
|
||||
SwitchPort1(uint16_t bit) : SwitchPort(bit, 1) {
|
||||
P1REN |= bit;
|
||||
P1DIR &= ~bit;
|
||||
P1SEL &= ~bit;
|
||||
};
|
||||
SwitchPort1(uint16_t bit) : SwitchPort(bit, 1) {
|
||||
P1REN |= bit;
|
||||
P1DIR &= ~bit;
|
||||
P1SEL &= ~bit;
|
||||
};
|
||||
protected:
|
||||
virtual bool getInValue(uint8_t bit) {
|
||||
return P1IN & m_inRegister;
|
||||
};
|
||||
};
|
||||
virtual bool getInValue(uint8_t bit) {
|
||||
bool r = P1IN & m_inRegister;
|
||||
if (! r) uartWrite('-');
|
||||
return r;
|
||||
};
|
||||
};
|
||||
|
||||
SwitchPort intensitySwitch = SwitchPort2(BIT0);
|
||||
SwitchPort onTimeSwitch = SwitchPort2(BIT1);
|
||||
SwitchPort offTimeSwitch = SwitchPort2(BIT2);
|
||||
SwitchPort modeSwitch = SwitchPort2(BIT3);
|
||||
SwitchPort intensitySwitch = SwitchPort2(BIT1);
|
||||
SwitchPort onTimeSwitch = SwitchPort2(BIT2);
|
||||
SwitchPort offTimeSwitch = SwitchPort2(BIT3);
|
||||
SwitchPort modeSwitch = SwitchPort2(BIT4);
|
||||
|
||||
int16_t intensity = 0;
|
||||
uint16_t maxIntensity = 0;
|
||||
@ -100,22 +114,23 @@ const uint16_t intensityStepCnt = 8;
|
||||
|
||||
|
||||
void hmiInit() {
|
||||
maxIntensity = engineMaxPwmValue();
|
||||
intensityStep = maxIntensity / intensityStepCnt;
|
||||
maxIntensity = engineMaxPwmValue();
|
||||
intensityStep = maxIntensity / intensityStepCnt;
|
||||
}
|
||||
|
||||
void hmiExec() {
|
||||
if (SwitchPort::e_SHORT == intensitySwitch.get()) {
|
||||
intensity += intensityStep;
|
||||
if (intensity > maxIntensity) {
|
||||
intensity = maxIntensity;
|
||||
}
|
||||
engineSetPwmValue(intensity);
|
||||
} else if (SwitchPort::e_LONG == intensitySwitch.get()) {
|
||||
intensity -= intensityStep;
|
||||
if (intensity < 0) {
|
||||
intensity = 0;
|
||||
}
|
||||
engineSetPwmValue(intensity);
|
||||
}
|
||||
}
|
||||
if (SwitchPort::e_SHORT == intensitySwitch.get()) {
|
||||
intensity += intensityStep;
|
||||
if (intensity > maxIntensity) {
|
||||
intensity = maxIntensity;
|
||||
}
|
||||
|
||||
engineSetPwmValue(intensity);
|
||||
} else if (SwitchPort::e_LONG == intensitySwitch.get()) {
|
||||
intensity -= intensityStep;
|
||||
if (intensity < 0) {
|
||||
intensity = 0;
|
||||
}
|
||||
engineSetPwmValue(intensity);
|
||||
}
|
||||
}
|
||||
|
13
src/main.cpp
13
src/main.cpp
@ -11,19 +11,26 @@
|
||||
#include <isr_compat.h>
|
||||
|
||||
#include "engine.h"
|
||||
|
||||
#include "hmi.h"
|
||||
#include "uart.h"
|
||||
|
||||
|
||||
int main() {
|
||||
WDTCTL = WDTPW | WDTHOLD;
|
||||
|
||||
|
||||
engineInit();
|
||||
|
||||
//engineInit();
|
||||
hmiInit();
|
||||
uartInit();
|
||||
|
||||
__enable_interrupt();
|
||||
|
||||
|
||||
uartWrite('W');
|
||||
|
||||
while (1) {
|
||||
hmiExec();
|
||||
//uartWrite('.');
|
||||
}
|
||||
}
|
||||
|
||||
|
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_ */
|
Loading…
x
Reference in New Issue
Block a user