From b224ffdf23c65ec76cddad37c9dcaa68d90cddf9 Mon Sep 17 00:00:00 2001 From: Bart Van Der Meerssche Date: Sun, 16 Jan 2011 12:00:41 +0100 Subject: [PATCH] [avr] optimize btoh and htob for 8-bit uc --- mote/v2/avr/ctrl.c | 8 ++++---- mote/v2/avr/encode.h | 27 +++++++++------------------ mote/v2/avr/main.c | 12 +++++------- 3 files changed, 18 insertions(+), 29 deletions(-) diff --git a/mote/v2/avr/ctrl.c b/mote/v2/avr/ctrl.c index fb6fb59..54a7175 100644 --- a/mote/v2/avr/ctrl.c +++ b/mote/v2/avr/ctrl.c @@ -128,7 +128,7 @@ uint8_t ctrlReadCharFromRxBuffer(uint8_t* pdata) uint8_t high_hex, low_hex; if (ctrlGetFromRxBuffer(&high_hex) && ctrlGetFromRxBuffer(&low_hex)) { - *pdata = htob(((uint16_t)high_hex << 8) + low_hex); + htob(high_hex, low_hex, pdata); return TRUE; } else { @@ -165,10 +165,10 @@ uint8_t ctrlReadLongFromRxBuffer(uint32_t* pdata) uint8_t ctrlWriteCharToTxBuffer(uint8_t data) { - uint16_t hex; + uint8_t high_hex, low_hex; - hex = btoh(data); - if (ctrlAddToTxBuffer((uint8_t)(hex >> 8)) && ctrlAddToTxBuffer((uint8_t)hex)) { + btoh(data, &high_hex, &low_hex); + if (ctrlAddToTxBuffer(high_hex) && ctrlAddToTxBuffer(low_hex)) { return TRUE; } else { diff --git a/mote/v2/avr/encode.h b/mote/v2/avr/encode.h index 1c504c5..6b87a5d 100644 --- a/mote/v2/avr/encode.h +++ b/mote/v2/avr/encode.h @@ -1,27 +1,18 @@ #include // hex to binary/byte decoding -static inline uint8_t htob(uint16_t hex) +static inline void htob(uint8_t high_hex, uint8_t low_hex, uint8_t *pbyte) { - uint8_t low_hex = (uint8_t) hex; - uint8_t high_hex = (uint8_t) (hex >> 8); - uint8_t byte; - - byte = (high_hex > 0x40) ? (high_hex & 0x0F) + 9 : high_hex & 0x0F; - byte = byte << 4; - byte |= (low_hex > 0x40) ? (low_hex & 0x0F) + 9 : low_hex & 0x0F; - return byte; + *pbyte = (high_hex > 0x40) ? (high_hex & 0x0F) + 9 : high_hex & 0x0F; + *pbyte = *pbyte << 4; + *pbyte |= (low_hex > 0x40) ? (low_hex & 0x0F) + 9 : low_hex & 0x0F; } // binary/byte to hex encoding -static inline uint16_t btoh(uint8_t byte) +static inline void btoh(uint8_t byte, uint8_t *phigh_hex, uint8_t *plow_hex) { - uint8_t low_nibble = (byte & 0x0F); - uint8_t high_nibble = (byte & 0xF0) >> 4; - uint16_t hex; - - hex = (high_nibble > 0x09) ? high_nibble - 9 + 0x60 : high_nibble + 0x30; - hex = hex << 8; - hex |= (low_nibble > 0x09) ? low_nibble - 9 + 0x60 : low_nibble + 0x30; - return hex; + *plow_hex = byte & 0x0F; + *plow_hex = (*plow_hex > 0x09) ? *plow_hex - 9 + 0x60 : *plow_hex + 0x30; + *phigh_hex = (byte & 0xF0) >> 4; + *phigh_hex = (*phigh_hex > 0x09) ? *phigh_hex - 9 + 0x60 : *phigh_hex + 0x30; } diff --git a/mote/v2/avr/main.c b/mote/v2/avr/main.c index e9f8fef..8afb83e 100644 --- a/mote/v2/avr/main.c +++ b/mote/v2/avr/main.c @@ -60,8 +60,7 @@ volatile struct time_struct time = {0, 0}; ISR(SPI_STC_vect) { - uint8_t spi_rx, rx, tx; - uint16_t spi_tx; + uint8_t spi_rx, spi_tx, rx, tx; DBG_ISR_BEGIN(); @@ -82,7 +81,7 @@ ISR(SPI_STC_vect) // are we in Tx mode? if (spi_status & SPI_TRANSMIT) { if (spi_status & SPI_HIGH_HEX) { - received_from_spi(spi_high_hex); + received_from_spi(spi_high_hex); /* actually low hex ! */ spi_status &= ~SPI_HIGH_HEX; return; } @@ -107,10 +106,9 @@ ISR(SPI_STC_vect) } } - spi_tx = btoh(tx); - spi_high_hex = (uint8_t)spi_tx; + btoh(tx, &spi_tx, (uint8_t *)&spi_high_hex); /* actually low hex ! */ spi_status |= SPI_HIGH_HEX; - received_from_spi((uint8_t)(spi_tx >> 8)); + received_from_spi(spi_tx); return; } @@ -136,7 +134,7 @@ ISR(SPI_STC_vect) break; default: if (spi_status & SPI_HIGH_HEX) { - rx = htob(((uint16_t)spi_high_hex << 8) + spi_rx); + htob(spi_high_hex, spi_rx, &rx); uartAddToTxBuffer(rx); } else {