[avr] optimize btoh and htob for 8-bit uc

This commit is contained in:
Bart Van Der Meerssche 2011-01-16 12:00:41 +01:00
parent 0af40cf27d
commit b224ffdf23
3 changed files with 18 additions and 29 deletions

View File

@ -128,7 +128,7 @@ uint8_t ctrlReadCharFromRxBuffer(uint8_t* pdata)
uint8_t high_hex, low_hex; uint8_t high_hex, low_hex;
if (ctrlGetFromRxBuffer(&high_hex) && ctrlGetFromRxBuffer(&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; return TRUE;
} }
else { else {
@ -165,10 +165,10 @@ uint8_t ctrlReadLongFromRxBuffer(uint32_t* pdata)
uint8_t ctrlWriteCharToTxBuffer(uint8_t data) uint8_t ctrlWriteCharToTxBuffer(uint8_t data)
{ {
uint16_t hex; uint8_t high_hex, low_hex;
hex = btoh(data); btoh(data, &high_hex, &low_hex);
if (ctrlAddToTxBuffer((uint8_t)(hex >> 8)) && ctrlAddToTxBuffer((uint8_t)hex)) { if (ctrlAddToTxBuffer(high_hex) && ctrlAddToTxBuffer(low_hex)) {
return TRUE; return TRUE;
} }
else { else {

View File

@ -1,27 +1,18 @@
#include <stdint.h> #include <stdint.h>
// hex to binary/byte decoding // 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; *pbyte = (high_hex > 0x40) ? (high_hex & 0x0F) + 9 : high_hex & 0x0F;
uint8_t high_hex = (uint8_t) (hex >> 8); *pbyte = *pbyte << 4;
uint8_t byte; *pbyte |= (low_hex > 0x40) ? (low_hex & 0x0F) + 9 : low_hex & 0x0F;
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;
} }
// binary/byte to hex encoding // 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); *plow_hex = byte & 0x0F;
uint8_t high_nibble = (byte & 0xF0) >> 4; *plow_hex = (*plow_hex > 0x09) ? *plow_hex - 9 + 0x60 : *plow_hex + 0x30;
uint16_t hex; *phigh_hex = (byte & 0xF0) >> 4;
*phigh_hex = (*phigh_hex > 0x09) ? *phigh_hex - 9 + 0x60 : *phigh_hex + 0x30;
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;
} }

View File

@ -60,8 +60,7 @@ volatile struct time_struct time = {0, 0};
ISR(SPI_STC_vect) ISR(SPI_STC_vect)
{ {
uint8_t spi_rx, rx, tx; uint8_t spi_rx, spi_tx, rx, tx;
uint16_t spi_tx;
DBG_ISR_BEGIN(); DBG_ISR_BEGIN();
@ -82,7 +81,7 @@ ISR(SPI_STC_vect)
// are we in Tx mode? // are we in Tx mode?
if (spi_status & SPI_TRANSMIT) { if (spi_status & SPI_TRANSMIT) {
if (spi_status & SPI_HIGH_HEX) { 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; spi_status &= ~SPI_HIGH_HEX;
return; return;
} }
@ -107,10 +106,9 @@ ISR(SPI_STC_vect)
} }
} }
spi_tx = btoh(tx); btoh(tx, &spi_tx, (uint8_t *)&spi_high_hex); /* actually low hex ! */
spi_high_hex = (uint8_t)spi_tx;
spi_status |= SPI_HIGH_HEX; spi_status |= SPI_HIGH_HEX;
received_from_spi((uint8_t)(spi_tx >> 8)); received_from_spi(spi_tx);
return; return;
} }
@ -136,7 +134,7 @@ ISR(SPI_STC_vect)
break; break;
default: default:
if (spi_status & SPI_HIGH_HEX) { 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); uartAddToTxBuffer(rx);
} }
else { else {