Skip to content

Commit

Permalink
speeding up HDLC framing by inlining and avoiding modulo operator
Browse files Browse the repository at this point in the history
  • Loading branch information
twatteyne committed Jan 30, 2013
1 parent 337c8c1 commit b1ac41a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 36 deletions.
54 changes: 20 additions & 34 deletions firmware/openos/drivers/common/openserial.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ openserial_vars_t openserial_vars;

//=========================== prototypes ======================================

uint8_t IncrOutputBufIdxW();
uint8_t IncrOutputBufIdxR();
error_t openserial_printInfoErrorCritical(
char severity,
uint8_t calling_component,
Expand All @@ -62,6 +60,10 @@ error_t openserial_printInfoErrorCritical(
void outputHdlcOpen();
void outputHdlcWrite(uint8_t b);
void outputHdlcClose();
// HDLC input
void inputHdlcOpen();
void inputHdlcWrite(uint8_t b);
void inputHdlcClose();

//=========================== public ==========================================

Expand Down Expand Up @@ -344,7 +346,7 @@ void openserial_startOutput() {
DISABLE_INTERRUPTS();
openserial_vars.mode=MODE_OUTPUT;
if (openserial_vars.outputBufFilled) {
uart_writeByte(openserial_vars.outputBuf[IncrOutputBufIdxR()]);
uart_writeByte(openserial_vars.outputBuf[openserial_vars.outputBufIdxR++]);
} else {
openserial_stop();
}
Expand Down Expand Up @@ -429,73 +431,57 @@ bool debugPrint_outBufferIndexes() {

//=========================== private =========================================

//===== increment indexes

/**
\pre Disable interrupts before calling this function.
*/
uint8_t IncrOutputBufIdxW() {
openserial_vars.outputBufIdxW = (openserial_vars.outputBufIdxW+1)%SERIAL_OUTPUT_BUFFER_SIZE;
return openserial_vars.outputBufIdxW;
}

/**
\pre Disable interrupts before calling this function.
*/
uint8_t IncrOutputBufIdxR() {
openserial_vars.outputBufIdxR = (openserial_vars.outputBufIdxR+1)%SERIAL_OUTPUT_BUFFER_SIZE;
return openserial_vars.outputBufIdxR;
}

//===== hdlc (output)

/**
\brief Start an HDLC frame in the output buffer.
*/
void outputHdlcOpen() {
inline void outputHdlcOpen() {
// initialize the value of the CRC
openserial_vars.outputCrc = HDLC_CRCINIT;

// write the opening HDLC flag
openserial_vars.outputBuf[IncrOutputBufIdxW()] = HDLC_FLAG;
openserial_vars.outputBuf[openserial_vars.outputBufIdxW++] = HDLC_FLAG;
}
/**
\brief Add a byte to the outgoing HDLC frame being built.
\todo escape 0x7e and 0x7d.
*/
void outputHdlcWrite(uint8_t b) {
inline void outputHdlcWrite(uint8_t b) {

// iterate through CRC calculator
openserial_vars.outputCrc = crcIteration(openserial_vars.outputCrc,b);

// add byte to buffer
if (b==HDLC_FLAG || b==HDLC_ESCAPE) {
openserial_vars.outputBuf[IncrOutputBufIdxW()] = HDLC_ESCAPE;
openserial_vars.outputBuf[openserial_vars.outputBufIdxW++] = HDLC_ESCAPE;
b = b^HDLC_ESCAPE_MASK;
}
openserial_vars.outputBuf[IncrOutputBufIdxW()] = b;
openserial_vars.outputBuf[openserial_vars.outputBufIdxW++] = b;

}
/**
\brief Finalize the outgoing HDLC frame.
*/
void outputHdlcClose() {
inline void outputHdlcClose() {
// finalize the calculation of the CRC
openserial_vars.outputCrc = ~openserial_vars.outputCrc;

// write the CRC value
openserial_vars.outputBuf[IncrOutputBufIdxW()] = (openserial_vars.outputCrc>>0)&0xff;
openserial_vars.outputBuf[IncrOutputBufIdxW()] = (openserial_vars.outputCrc>>8)&0xff;
openserial_vars.outputBuf[openserial_vars.outputBufIdxW++] = (openserial_vars.outputCrc>>0)&0xff;
openserial_vars.outputBuf[openserial_vars.outputBufIdxW++] = (openserial_vars.outputCrc>>8)&0xff;

// write the closing HDLC flag
openserial_vars.outputBuf[IncrOutputBufIdxW()] = HDLC_FLAG;
openserial_vars.outputBuf[openserial_vars.outputBufIdxW++] = HDLC_FLAG;
}

//===== hdlc (input)

/**
\brief Start an HDLC frame in the input buffer.
*/
void inputHdlcOpen() {
inline void inputHdlcOpen() {
// reset the input buffer index
openserial_vars.inputBufFill = 0;

Expand All @@ -507,7 +493,7 @@ void inputHdlcOpen() {
\todo escape 0x7e and 0x7d.
*/
void inputHdlcWrite(uint8_t b) {
inline void inputHdlcWrite(uint8_t b) {
if (b==HDLC_ESCAPE) {
openserial_vars.inputEscaping = TRUE;
} else {
Expand All @@ -527,7 +513,7 @@ void inputHdlcWrite(uint8_t b) {
/**
\brief Finalize the incoming HDLC frame.
*/
void inputHdlcClose() {
inline void inputHdlcClose() {

// verify the validity of the frame
if (openserial_vars.inputCrc==HDLC_CRCGOOD) {
Expand Down Expand Up @@ -559,7 +545,7 @@ void isr_openserial_tx() {
openserial_vars.outputBufFilled = FALSE;
}
if (openserial_vars.outputBufFilled) {
uart_writeByte(openserial_vars.outputBuf[IncrOutputBufIdxR()]);
uart_writeByte(openserial_vars.outputBuf[openserial_vars.outputBufIdxR++]);
}
break;
case MODE_OFF:
Expand Down
5 changes: 4 additions & 1 deletion firmware/openos/drivers/common/openserial.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@

/**
\brief Number of bytes of the serial output buffer, in bytes.
\warning should be exactly 256 so wrap-around on the index does not require
the use of a slow modulo operator.
*/
#define SERIAL_OUTPUT_BUFFER_SIZE 200
#define SERIAL_OUTPUT_BUFFER_SIZE 256 // leave at 256!

/**
\brief Number of bytes of the serial input buffer, in bytes.
Expand Down
2 changes: 1 addition & 1 deletion firmware/openos/openwsn/openwsn.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static const uint8_t infoStackName[] = "OpenWSN ";
#define OPENWSN_VERSION_PATCH 1

// enter the last byte of your mote's address if you want it to be an LBR
#define DEBUG_MOTEID_MASTER 0x3b
#define DEBUG_MOTEID_MASTER 0x6f

#ifndef TRUE
#define TRUE 1
Expand Down

0 comments on commit b1ac41a

Please sign in to comment.