Skip to content

Commit

Permalink
Add support for parity on serial port.
Browse files Browse the repository at this point in the history
Signed-off-by: Kari Hautio <[email protected]>
  • Loading branch information
kh4 committed Apr 17, 2016
1 parent 32f1ae3 commit ccd9a45
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
3 changes: 3 additions & 0 deletions binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// 2 -- 19200bps, medium range
#define DEFAULT_DATARATE 2
#define DEFAULT_BAUDRATE 115200
#define DEFAULT_SMODE 0
#define MAX_PACKET_SIZE 49
#define MIN_PACKET_SIZE 9
#define DEFAULT_PACKET_SIZE 33
Expand Down Expand Up @@ -65,6 +66,7 @@ static uint8_t default_hop_list[] = {DEFAULT_HOPLIST};
struct bind_data {
uint8_t version;
uint32_t serial_baudrate;
uint8_t serial_mode;
uint32_t rf_frequency;
uint32_t rf_magic;
uint8_t rf_power;
Expand Down Expand Up @@ -173,6 +175,7 @@ void bindInitDefaults(void)
{
bind_data.version = BINDING_VERSION;
bind_data.serial_baudrate = DEFAULT_BAUDRATE;
bind_data.serial_mode = DEFAULT_SMODE;
bind_data.rf_power = DEFAULT_RF_POWER;
bind_data.rf_frequency = DEFAULT_CARRIER_FREQUENCY;
bind_data.rf_channel_spacing = DEFAULT_CHANNEL_SPACING;
Expand Down
13 changes: 11 additions & 2 deletions cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ void show()
printStr(" module type ");
printULLn(bind_data.rfmType);
printStr("a) serial baudrate: ");
printULLn(bind_data.serial_baudrate);
printUL(bind_data.serial_baudrate);
printStr(" t) serial mode: ");
printStrLn(serial_bits_str[bind_data.serial_mode]);
printStr("b) base frequency: ");
printULLn(bind_data.rf_frequency);
printStr("c) max frequency: ");
Expand Down Expand Up @@ -179,6 +181,13 @@ void handleCLI()
bind_data.flags ^= PACKETCRC_MODE;
valid = 1;
break;
case 't':
bind_data.serial_mode++;
if (bind_data.serial_mode > 5) {
bind_data.serial_mode=0;
}
valid = 1;
break;
case 'l': {
printStrLn("Enter channels one by one (1-254), invalid value or just enter to finish");
uint8_t i=0;
Expand Down Expand Up @@ -237,7 +246,7 @@ void handleCLI()
case 's':
bindWriteEeprom();
printStrLn("SAVED!!");
// fallthru
// fallthru
case 'q':
bindReadEeprom();
cliActive = false;
Expand Down
6 changes: 3 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ void setup(void)
digitalWrite(SLAVE_SELECT, HIGH); // enable pullup for TX:s with open collector output
buzzerInit();

serialInit(115200);
serialInit(115200,SERIAL_8N1);

checkOperatingMode();

Expand Down Expand Up @@ -276,7 +276,7 @@ void setup(void)
printStrLn("Entering normal mode");

serialFlush();
serialInit(bind_data.serial_baudrate);
serialInit(bind_data.serial_baudrate, bind_data.serial_mode);

Red_LED_OFF;
buzzerOff();
Expand Down Expand Up @@ -436,7 +436,7 @@ void slaveLoop()
case 1: // waiting TX completion
switch (tx_done()) {
case 2: // tx timeout
// rfm init ??
// rfm init ??
case 1: // ok
Green_LED_OFF;
state = 0;
Expand Down
21 changes: 19 additions & 2 deletions serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#define FIFOSIZE 128

const char* serial_bits_str[] = {"8N1","8N2","8E1","8E2","8O1","8O2"};

struct fifo {
uint8_t head;
uint8_t tail;
Expand Down Expand Up @@ -38,6 +40,9 @@ bool txActive = false;
#define _RXCIEx RXCIE1
#define _UCSZx1 UCSZ11
#define _UCSZx0 UCSZ10
#define _USBSx USBS1
#define _UPMx0 UPM10
#define _UPMx1 UPM11

#else
#define _USART_RX_vect USART_RX_vect
Expand All @@ -59,7 +64,9 @@ bool txActive = false;
#define _RXCIEx RXCIE0
#define _UCSZx1 UCSZ01
#define _UCSZx0 UCSZ00

#define _USBSx USBS0
#define _UPMx0 UPM00
#define _UPMx1 UPM01
#endif

ISR(_USART_RX_vect)
Expand Down Expand Up @@ -139,7 +146,7 @@ void serialWriteSync(uint8_t c)
while (serialWrite(c));
}

void serialInit(uint32_t baudrate)
void serialInit(uint32_t baudrate, uint8_t bits)
{
bool u2x = 1;
uint16_t brd;
Expand All @@ -157,5 +164,15 @@ void serialInit(uint32_t baudrate)
_UCSRxB = (1<<_RXCIEx) | (1<<_RXENx) | (1<<_TXENx);
_UCSRxC = (1<<_UCSZx1) | (1<<_UCSZx0);

// bits == 0=8N1 1=8N2 2=8E1 3=8E2 4=8O1 5=8O2, rest undef.
if (bits & 1) { // 2 stop bit mode
_UCSRxC |= _USBSx;
}
if (bits > 2) { //parity enabled
_UCSRxC |= _UPMx1;
if (bits > 4) { // odd parity
_UCSRxC |= _UPMx0;
}
}
}

11 changes: 10 additions & 1 deletion serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,14 @@ uint8_t serialRead();

void serialFlush();

void serialInit(uint32_t baudrate);
extern const char* serial_bits_str[];

#define SERIAL_8N1 0x00
#define SERIAL_8N2 0x01
#define SERIAL_8E1 0x02
#define SERIAL_8E2 0x03
#define SERIAL_8O1 0x04
#define SERIAL_8O2 0x05

void serialInit(uint32_t baudrate, uint8_t bits);

0 comments on commit ccd9a45

Please sign in to comment.