|
|
|
@ -26,20 +26,13 @@ uint8_t sesc;
|
|
|
|
|
uint32_t saddress;
|
|
|
|
|
uint8_t slen;
|
|
|
|
|
uint8_t smessage[62];
|
|
|
|
|
uint8_t slocked;
|
|
|
|
|
uint8_t sbusy;
|
|
|
|
|
|
|
|
|
|
volatile uint16_t sdelay;
|
|
|
|
|
uint16_t sdelay;
|
|
|
|
|
|
|
|
|
|
uint8_t waitack;
|
|
|
|
|
uint32_t aaddress;
|
|
|
|
|
uint8_t actl;
|
|
|
|
|
uint8_t alen;
|
|
|
|
|
uint8_t alink;
|
|
|
|
|
uint8_t amessage[62];
|
|
|
|
|
|
|
|
|
|
uint8_t asent;
|
|
|
|
|
uint8_t dsent;
|
|
|
|
|
uint8_t gotack;
|
|
|
|
|
|
|
|
|
|
uint8_t configled;
|
|
|
|
@ -67,6 +60,7 @@ static uint16_t crc16shift(uint16_t crc, uint8_t data)
|
|
|
|
|
|
|
|
|
|
static void readaddr(void)
|
|
|
|
|
{
|
|
|
|
|
// my_address=eeprom_read_dword((const uint32_t *)EESIZE-4);
|
|
|
|
|
((uint8_t*)&my_address)[0]=eeprom_read_byte((const uint8_t *)EESIZE-4);
|
|
|
|
|
((uint8_t*)&my_address)[1]=eeprom_read_byte((const uint8_t *)EESIZE-3);
|
|
|
|
|
((uint8_t*)&my_address)[2]=eeprom_read_byte((const uint8_t *)EESIZE-2);
|
|
|
|
@ -203,18 +197,16 @@ static uint8_t senddsc(void)
|
|
|
|
|
UDR0 = 0xFE;
|
|
|
|
|
sind = 1;
|
|
|
|
|
sesc = 0;
|
|
|
|
|
dsent = 1;
|
|
|
|
|
sdisc = 1;
|
|
|
|
|
sdelay = 1000 + (rand() & 0x1F);
|
|
|
|
|
return ~0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint8_t is_bus_free(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(sdelay || slocked)
|
|
|
|
|
return 0;
|
|
|
|
|
return 1;
|
|
|
|
|
if(sdelay - hbw_timer > 0x7FFF)
|
|
|
|
|
return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void announce(void)
|
|
|
|
@ -241,9 +233,9 @@ static void announce(void)
|
|
|
|
|
smessage[13] = HBWSERIAL7;
|
|
|
|
|
smessage[14] = HBWSERIAL8;
|
|
|
|
|
smessage[15] = HBWSERIAL9;
|
|
|
|
|
sendmsg();
|
|
|
|
|
|
|
|
|
|
if (sendmsg())
|
|
|
|
|
asent = 1;
|
|
|
|
|
asent = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void hbw_init(void)
|
|
|
|
@ -269,104 +261,30 @@ void hbw_init(void)
|
|
|
|
|
UCSR0B = (1 << TXEN0) | (1 << RXEN0) | (1 << RXCIE0);
|
|
|
|
|
UCSR0C = (1 << UPM01) | (1 << UCSZ01) | (1 << UCSZ00);
|
|
|
|
|
|
|
|
|
|
OCR2A = 249;
|
|
|
|
|
OCR2A = 0xF9;
|
|
|
|
|
TIMSK2 |= (1 << OCIE2A);
|
|
|
|
|
TCCR2A |= (1 << WGM21);
|
|
|
|
|
TCCR2B = TCCR2B | (1 << CS22);
|
|
|
|
|
TCCR2B = TCCR2B | (1 << CS21) | (1 << CS20);
|
|
|
|
|
|
|
|
|
|
rind = 0;
|
|
|
|
|
sind = 0;
|
|
|
|
|
sdisc = 0;
|
|
|
|
|
rready = 0;
|
|
|
|
|
asent = 0;
|
|
|
|
|
dsent = 0;
|
|
|
|
|
hbw_timer = 0;
|
|
|
|
|
gotack = 0;
|
|
|
|
|
waitack = 0;
|
|
|
|
|
configled = 0;
|
|
|
|
|
configbutton = 0;
|
|
|
|
|
configstate = 0;
|
|
|
|
|
slocked = 0;
|
|
|
|
|
sbusy = 0;
|
|
|
|
|
alink = 0;
|
|
|
|
|
|
|
|
|
|
readaddr();
|
|
|
|
|
hbw_read_config();
|
|
|
|
|
srand(my_address);
|
|
|
|
|
sdelay = 1000 + (rand() & 0x1F);
|
|
|
|
|
sei();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void snext(void)
|
|
|
|
|
{
|
|
|
|
|
uint8_t linkchannel;
|
|
|
|
|
|
|
|
|
|
if (amessage[0] != 'K') {
|
|
|
|
|
alink = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (alink < HBW_MAXLINKS) {
|
|
|
|
|
linkchannel = eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS);
|
|
|
|
|
if (amessage[1] == linkchannel) {
|
|
|
|
|
((uint8_t*)&aaddress)[0]=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+1);
|
|
|
|
|
((uint8_t*)&aaddress)[1]=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+2);
|
|
|
|
|
((uint8_t*)&aaddress)[2]=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+3);
|
|
|
|
|
((uint8_t*)&aaddress)[3]=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+4);
|
|
|
|
|
linkchannel=eeprom_read_byte((const uint8_t *)(alink*6)+HBW_LINKOFFS+5);
|
|
|
|
|
if(aaddress == my_address) {
|
|
|
|
|
hbw_receive_key(linkchannel, amessage[3] >> 2, amessage[3] & 0x3);
|
|
|
|
|
} else {
|
|
|
|
|
amessage[2] = linkchannel;
|
|
|
|
|
sbusy = 1;
|
|
|
|
|
alink++;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
alink++;
|
|
|
|
|
}
|
|
|
|
|
alink = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sprocess(void)
|
|
|
|
|
{
|
|
|
|
|
if (gotack) {
|
|
|
|
|
gotack = 0;
|
|
|
|
|
waitack = 0;
|
|
|
|
|
sbusy= 0;
|
|
|
|
|
snext();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!is_bus_free())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if(waitack > 2) {
|
|
|
|
|
waitack = 0;
|
|
|
|
|
sbusy= 0;
|
|
|
|
|
snext();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saddress = aaddress;
|
|
|
|
|
sctl = actl;
|
|
|
|
|
slen = alen;
|
|
|
|
|
memcpy(smessage,amessage,slen);
|
|
|
|
|
|
|
|
|
|
if (!sendmsg())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (aaddress == 0xFFFFFFFF) {
|
|
|
|
|
sbusy = 0;
|
|
|
|
|
snext();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
waitack++;
|
|
|
|
|
gotack = 0;
|
|
|
|
|
sdelay = 100 + (rand() & 0x1F);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void rprocess(void)
|
|
|
|
|
static void process(void)
|
|
|
|
|
{
|
|
|
|
|
uint8_t seq,i;
|
|
|
|
|
uint16_t addr;
|
|
|
|
@ -424,6 +342,8 @@ static void rprocess(void)
|
|
|
|
|
sendmsg();
|
|
|
|
|
break;
|
|
|
|
|
case 'v':
|
|
|
|
|
if (rlen != 2)
|
|
|
|
|
return;
|
|
|
|
|
smessage[0] = HBWMAJOR;
|
|
|
|
|
smessage[1] = HBWMINOR;
|
|
|
|
|
slen = 2;
|
|
|
|
@ -479,20 +399,6 @@ static void rprocess(void)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ((rdest == 0xFFFFFFFF) &&
|
|
|
|
|
((rctl & 0x11) == 0x10)) {
|
|
|
|
|
if(!rlen)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
switch (rmessage[0]) {
|
|
|
|
|
case 'z':
|
|
|
|
|
slocked = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 'Z':
|
|
|
|
|
slocked = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void eeprom_clear(void)
|
|
|
|
@ -507,21 +413,12 @@ void hbw_loop(void)
|
|
|
|
|
{
|
|
|
|
|
rloop();
|
|
|
|
|
if (rready) {
|
|
|
|
|
rprocess();
|
|
|
|
|
process();
|
|
|
|
|
rready = 0;
|
|
|
|
|
}
|
|
|
|
|
if (sbusy) {
|
|
|
|
|
sprocess();
|
|
|
|
|
} else {
|
|
|
|
|
if (!dsent) {
|
|
|
|
|
if (is_bus_free()) {
|
|
|
|
|
senddsc();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!asent) {
|
|
|
|
|
if (is_bus_free()) {
|
|
|
|
|
announce();
|
|
|
|
|
}
|
|
|
|
|
if (!asent) {
|
|
|
|
|
if (is_bus_free()) {
|
|
|
|
|
announce();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -553,14 +450,14 @@ void hbw_loop(void)
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
configled = (hbw_timer & 128)?0:1;
|
|
|
|
|
configled = ((hbw_timer % 400) > 200)?0:1;
|
|
|
|
|
if (bit_is_set(PIND, ConfigButton)) {
|
|
|
|
|
configstate = 4;
|
|
|
|
|
configtimer = hbw_timer;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
configled = (hbw_timer & 128)?0:1;
|
|
|
|
|
configled = ((hbw_timer % 400) > 200)?0:1;
|
|
|
|
|
if (((hbw_timer - configtimer) > 2000) &&
|
|
|
|
|
(bit_is_set(PIND, ConfigButton))) {
|
|
|
|
|
configstate = 0;
|
|
|
|
@ -570,13 +467,12 @@ void hbw_loop(void)
|
|
|
|
|
(bit_is_clear(PIND, ConfigButton))) {
|
|
|
|
|
configstate = 5;
|
|
|
|
|
configled = 0;
|
|
|
|
|
eeprom_clear();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 5:
|
|
|
|
|
configled = (hbw_timer & 512)?0:1;
|
|
|
|
|
if (((hbw_timer - configtimer) > 20) &&
|
|
|
|
|
(bit_is_set(PIND, ConfigButton))) {
|
|
|
|
|
eeprom_clear();
|
|
|
|
|
configstate = 0;
|
|
|
|
|
configled = 0;
|
|
|
|
|
asent = 0;
|
|
|
|
@ -592,54 +488,52 @@ void hbw_loop(void)
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t hbw_send_key(uint8_t channel, uint8_t count, uint8_t flag)
|
|
|
|
|
uint8_t hbw_send_channel(uint8_t channel, uint8_t length, uint8_t const * const data, uint32_t target_address)
|
|
|
|
|
{
|
|
|
|
|
if (sbusy)
|
|
|
|
|
return 0;
|
|
|
|
|
if (gotack) {
|
|
|
|
|
gotack = 0;
|
|
|
|
|
waitack = 0;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (alink)
|
|
|
|
|
if (!is_bus_free())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
alen = 4;
|
|
|
|
|
actl = 0xF8;
|
|
|
|
|
amessage[0] = 'K';
|
|
|
|
|
amessage[1] = channel;
|
|
|
|
|
amessage[2] = 0;
|
|
|
|
|
amessage[3] = (flag & 3) | (count << 2);
|
|
|
|
|
|
|
|
|
|
aaddress = 0xFFFFFFFF;
|
|
|
|
|
sbusy = 1;
|
|
|
|
|
alink = 0;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if(waitack > 2) {
|
|
|
|
|
waitack = 0;
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t hbw_send_channel(uint8_t channel, uint8_t length, uint8_t const * const data)
|
|
|
|
|
{
|
|
|
|
|
if (sbusy)
|
|
|
|
|
return 0;
|
|
|
|
|
slen = length + 2;
|
|
|
|
|
sctl = 0xF8;
|
|
|
|
|
aaddress = target_address;
|
|
|
|
|
if(aaddress == 0) {
|
|
|
|
|
((uint8_t*)&aaddress)[0]=eeprom_read_byte((const uint8_t *)2);
|
|
|
|
|
((uint8_t*)&aaddress)[1]=eeprom_read_byte((const uint8_t *)3);
|
|
|
|
|
((uint8_t*)&aaddress)[2]=eeprom_read_byte((const uint8_t *)4);
|
|
|
|
|
((uint8_t*)&aaddress)[3]=eeprom_read_byte((const uint8_t *)5);
|
|
|
|
|
// aaddress = eeprom_read_dword((const uint32_t *)2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (alink)
|
|
|
|
|
return 0;
|
|
|
|
|
saddress = aaddress;
|
|
|
|
|
smessage[0] = 'i';
|
|
|
|
|
smessage[1] = channel;
|
|
|
|
|
memcpy(&(smessage[2]), data, length);
|
|
|
|
|
sendmsg();
|
|
|
|
|
if (aaddress == 0xFFFFFFFF)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
alen = length + 2;
|
|
|
|
|
actl = 0xF8;
|
|
|
|
|
((uint8_t*)&aaddress)[0]=eeprom_read_byte((const uint8_t *)2);
|
|
|
|
|
((uint8_t*)&aaddress)[1]=eeprom_read_byte((const uint8_t *)3);
|
|
|
|
|
((uint8_t*)&aaddress)[2]=eeprom_read_byte((const uint8_t *)4);
|
|
|
|
|
((uint8_t*)&aaddress)[3]=eeprom_read_byte((const uint8_t *)5);
|
|
|
|
|
|
|
|
|
|
amessage[0] = 'i';
|
|
|
|
|
amessage[1] = channel;
|
|
|
|
|
memcpy(&(amessage[2]), data, length);
|
|
|
|
|
sbusy = 1;
|
|
|
|
|
return 1;
|
|
|
|
|
waitack++;
|
|
|
|
|
gotack = 0;
|
|
|
|
|
sdelay = hbw_timer + 100 + (rand() & 0x1F);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ISR(USART_RX_vect)
|
|
|
|
|
{
|
|
|
|
|
uint8_t c;
|
|
|
|
|
|
|
|
|
|
sdelay = 100 + (rand() & 0x1F);
|
|
|
|
|
sdelay = hbw_timer + 100 + (rand() & 0x1F);
|
|
|
|
|
|
|
|
|
|
c = UDR0;
|
|
|
|
|
if (c == 0xFE) {
|
|
|
|
@ -706,7 +600,5 @@ ISR(TIMER2_COMPA_vect)
|
|
|
|
|
{
|
|
|
|
|
cli();
|
|
|
|
|
hbw_timer++;
|
|
|
|
|
if (sdelay)
|
|
|
|
|
sdelay--;
|
|
|
|
|
sei();
|
|
|
|
|
}
|
|
|
|
|