add uart for debug
This commit is contained in:
@ -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;
|
||||||
|
37
src/hmi.cpp
37
src/hmi.cpp
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "hmi.h"
|
#include "hmi.h"
|
||||||
#include "engine.h"
|
#include "engine.h"
|
||||||
|
#include "uart.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -16,6 +17,7 @@ public:
|
|||||||
e_Value result = e_NIL;
|
e_Value result = e_NIL;
|
||||||
switch (m_state) {
|
switch (m_state) {
|
||||||
case 0:
|
case 0:
|
||||||
|
//uartWrite('0');
|
||||||
if (getInValue()) {
|
if (getInValue()) {
|
||||||
m_state = 1;
|
m_state = 1;
|
||||||
} else {
|
} else {
|
||||||
@ -23,6 +25,7 @@ public:
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
|
//uartWrite('1');
|
||||||
if (m_cnt > LONG_THRESHOLD) {
|
if (m_cnt > LONG_THRESHOLD) {
|
||||||
result = e_LONG;
|
result = e_LONG;
|
||||||
m_state = 2;
|
m_state = 2;
|
||||||
@ -34,6 +37,7 @@ public:
|
|||||||
m_cnt = 0;
|
m_cnt = 0;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
|
//uartWrite('2');
|
||||||
if (getInValue()) {
|
if (getInValue()) {
|
||||||
m_cnt++;
|
m_cnt++;
|
||||||
} else {
|
} else {
|
||||||
@ -51,17 +55,24 @@ public:
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
protected:
|
protected:
|
||||||
virtual bool getInValue() =0;
|
virtual bool getInValue() {
|
||||||
|
//uartWrite('x');
|
||||||
|
return true;
|
||||||
|
};
|
||||||
uint8_t m_inRegister;
|
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) {
|
||||||
@ -71,7 +82,9 @@ public:
|
|||||||
};
|
};
|
||||||
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 {
|
||||||
@ -83,15 +96,16 @@ public:
|
|||||||
};
|
};
|
||||||
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;
|
||||||
@ -110,6 +124,7 @@ void hmiExec() {
|
|||||||
if (intensity > maxIntensity) {
|
if (intensity > maxIntensity) {
|
||||||
intensity = maxIntensity;
|
intensity = maxIntensity;
|
||||||
}
|
}
|
||||||
|
|
||||||
engineSetPwmValue(intensity);
|
engineSetPwmValue(intensity);
|
||||||
} else if (SwitchPort::e_LONG == intensitySwitch.get()) {
|
} else if (SwitchPort::e_LONG == intensitySwitch.get()) {
|
||||||
intensity -= intensityStep;
|
intensity -= intensityStep;
|
||||||
|
13
src/main.cpp
13
src/main.cpp
@ -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
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