avr: move SPI_... defs to header file, change value into counter name

This commit is contained in:
Bart Van Der Meerssche 2010-12-28 14:34:56 +01:00
parent 263d3d107a
commit b63f36cba0
3 changed files with 97 additions and 46 deletions

View File

@ -232,11 +232,11 @@ void ctrlCmdGet(uint8_t cmd)
}
break;
case 'v':
case 'c':
ctrlReadCharFromRxBuffer(&i);
cli();
tmp32 = sensor[i].value;
tmp32 = sensor[i].counter;
sei();
ctrlWriteLongToTxBuffer(tmp32);
@ -274,12 +274,12 @@ void ctrlCmdSet(uint8_t cmd)
}
break;
case 'v':
case 'c':
ctrlReadCharFromRxBuffer(&i);
ctrlReadLongFromRxBuffer(&tmp32);
cli();
sensor[i].value = tmp32;
sensor[i].counter = tmp32;
sei();
break;

View File

@ -32,22 +32,9 @@
#include "global.h"
#include "encode.h"
#define NO_OP_1 1
#define NO_OP_2 2
#define START_TX 4
#define TRANSMIT 8
#define HIGH_HEX 16
#define TO_FROM_UART 32
#define NEW_CTRL_MSG 64
volatile uint8_t spi_status, spi_high_hex;
#define SPI_END_OF_TX 0x00
#define SPI_END_OF_MESSAGE '.'
#define SPI_FORWARD_TO_UART_PORT 'u'
#define SPI_FORWARD_TO_CTRL_PORT 'l' // 'l'ocal port
volatile uint8_t spi_status, high_hex;
uint8_t EEMEM first_EEPROM_byte_not_used_to_protect_from_brownout_corruption = 0x00;
uint8_t EEMEM first_EEPROM_byte_not_used_to_protect_from_brownout_corruption = 0xab;
volatile struct event_struct EEMEM EEPROM_event = {0, 0};
volatile struct event_struct event;
@ -58,37 +45,39 @@ uint8_t phy_to_log[MAX_SENSORS];
volatile struct sensor_struct EEMEM EEPROM_sensor[MAX_SENSORS];
volatile struct sensor_struct sensor[MAX_SENSORS];
volatile struct state_struct state[MAX_SENSORS];
ISR(SPI_STC_vect)
{
uint8_t spi_rx, rx, tx;
uint16_t spi_tx;
// the SPI is double-buffered, requiring two NO_OPs when switching from Tx to Rx
if (spi_status & (NO_OP_1 | NO_OP_2)) {
if (spi_status & (SPI_NO_OP_1 | SPI_NO_OP_2)) {
spi_status--;
return;
}
// do we have to transmit the first byte?
if (spi_status & START_TX) {
if (spi_status & SPI_START_TX) {
received_from_spi(SPI_FORWARD_TO_CTRL_PORT);
spi_status &= ~START_TX;
spi_status &= ~SPI_START_TX;
return;
}
// are we in Tx mode?
if (spi_status & TRANSMIT) {
if (spi_status & HIGH_HEX) {
received_from_spi(high_hex);
spi_status &= ~HIGH_HEX;
if (spi_status & SPI_TRANSMIT) {
if (spi_status & SPI_HIGH_HEX) {
received_from_spi(spi_high_hex);
spi_status &= ~SPI_HIGH_HEX;
return;
}
if (spi_status & TO_FROM_UART) {
if (spi_status & SPI_TO_FROM_UART) {
if (!uartReceiveByte(&tx)) {
received_from_spi(SPI_END_OF_TX);
spi_status &= ~TRANSMIT;
spi_status |= NO_OP_2;
spi_status &= ~SPI_TRANSMIT;
spi_status |= SPI_NO_OP_2;
return;
}
}
@ -99,14 +88,14 @@ ISR(SPI_STC_vect)
}
else {
received_from_spi(SPI_FORWARD_TO_UART_PORT);
spi_status |= TO_FROM_UART;
spi_status |= SPI_TO_FROM_UART;
return;
}
}
spi_tx = btoh(tx);
high_hex = (uint8_t)spi_tx;
spi_status |= HIGH_HEX;
spi_high_hex = (uint8_t)spi_tx;
spi_status |= SPI_HIGH_HEX;
received_from_spi((uint8_t)(spi_tx >> 8));
return;
}
@ -114,29 +103,29 @@ ISR(SPI_STC_vect)
// we're in Rx mode
switch (spi_rx = received_from_spi(0x00)) {
case SPI_END_OF_TX:
spi_status |= TRANSMIT | START_TX;
spi_status &= ~(HIGH_HEX | TO_FROM_UART);
spi_status |= SPI_TRANSMIT | SPI_START_TX;
spi_status &= ~(SPI_HIGH_HEX | SPI_TO_FROM_UART);
break;
case SPI_END_OF_MESSAGE:
if (!(spi_status & TO_FROM_UART)) {
if (!(spi_status & SPI_TO_FROM_UART)) {
ctrlAddToRxBuffer(spi_rx);
spi_status |= NEW_CTRL_MSG;
spi_status |= SPI_NEW_CTRL_MSG;
}
break;
case SPI_FORWARD_TO_UART_PORT:
spi_status |= TO_FROM_UART;
spi_status |= SPI_TO_FROM_UART;
break;
case SPI_FORWARD_TO_CTRL_PORT:
spi_status &= ~TO_FROM_UART;
spi_status &= ~SPI_TO_FROM_UART;
break;
default:
if (spi_status & HIGH_HEX) {
rx = htob(((uint16_t)high_hex << 8) + spi_rx);
if (spi_status & SPI_HIGH_HEX) {
rx = htob(((uint16_t)spi_high_hex << 8) + spi_rx);
uartAddToTxBuffer(rx);
}
else {
if (spi_status & TO_FROM_UART) {
high_hex = spi_rx;
if (spi_status & SPI_TO_FROM_UART) {
spi_high_hex = spi_rx;
}
else {
ctrlAddToRxBuffer(spi_rx);
@ -145,7 +134,7 @@ ISR(SPI_STC_vect)
}
// toggle the HEX bit in spi_status
spi_status ^= HIGH_HEX;
spi_status ^= SPI_HIGH_HEX;
}
}
@ -248,10 +237,10 @@ int main(void)
for(;;) {
if (spi_status & NEW_CTRL_MSG) {
if (spi_status & SPI_NEW_CTRL_MSG) {
//ctrlRxToTxLoop();
ctrlDecode();
spi_status &= ~NEW_CTRL_MSG;
spi_status &= ~SPI_NEW_CTRL_MSG;
}
// toggle the LED=PB0 pin

View File

@ -1,10 +1,72 @@
#define SPI_NO_OP_1 1
#define SPI_NO_OP_2 2
#define SPI_START_TX 4
#define SPI_TRANSMIT 8
#define SPI_HIGH_HEX 16
#define SPI_TO_FROM_UART 32
#define SPI_NEW_CTRL_MSG 64
#define SPI_END_OF_TX 0x00
#define SPI_END_OF_MESSAGE '.'
#define SPI_FORWARD_TO_UART_PORT 'u'
#define SPI_FORWARD_TO_CTRL_PORT 'l' // 'l'ocal port
struct event_struct {
uint16_t wdt;
uint16_t brown_out;
};
struct sensor_struct {
uint32_t value;
uint32_t counter;
uint16_t meterconst;
};
#define STATE_PULSE = 1
#define STATE_TOGGLE = 2
#define STATE_POWER = 4
struct state_struct {
uint8_t flags;
uint32_t nano;
uint32_t nano_start;
uint32_t nano_end;
uint8_t pulse_count;
uint8_t pulse_count_final;
uint32_t power;
uint32_t timestamp;
};
/*
* This macro performs a 16x16 -> 32 unsigned MAC in 37 cycles with operands and results in memory
* based on http://www2.ife.ee.ethz.ch/~roggend/publications/wear/DSPMic_v1.1.pdf par 3.4 and table 31.
*/
#define MacU16X16to32(uint_32Acc, uint_16In1, uint_16In2) \
asm volatile ( \
"clr r2 \n\t" \
"mul %B2, %B1 \n\t" \
"movw r4, r0 \n\t" \
"mul %A2, %A1 \n\t" \
"add %A0, r0 \n\t" \
"adc %B0, r1 \n\t" \
"adc %C0, r4 \n\t" \
"adc %D0, r5 \n\t" \
"mul %B2, %A1 \n\t" \
"add %B0, r0 \n\t" \
"adc %C0, r1 \n\t" \
"adc %D0, r2 \n\t" \
"mul %A2, %B1 \n\t" \
"add %B0, r0 \n\t" \
"adc %C0, r1 \n\t" \
"adc %D0, r2 \n\t" \
"clr r1 \n\t" \
: \
"+r" (uint_32Acc) \
: \
"a" (uint_16In1), \
"a" (uint_16In2) \
: \
"r2", "r4", "r5" \
)