From 07ce2df5da6265702e1d99556f46139f8b5b8611 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Sun, 24 Jan 2016 20:02:30 +0100 Subject: write/verify scratchpad. proper formatting. warning comment. --- avr/main.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 9 deletions(-) (limited to 'avr/main.c') diff --git a/avr/main.c b/avr/main.c index 6774d6e..15d98d3 100644 --- a/avr/main.c +++ b/avr/main.c @@ -55,6 +55,8 @@ void jump_to_bootloader(void) } static uchar replyBuffer[130]="Hello world initial"; +static uchar buf[33]; + usbMsgLen_t usbFunctionSetup(uchar data[8]) { usbRequest_t *rq = (void *) data; @@ -152,28 +154,75 @@ int main(void) sei(); // Enable interrupts after re-enumeration + uint8_t error = 0; while (1) { wdt_reset(); // keep the watchdog happy usbPoll(); - if (++c % (got_data ? 1000 : 3000) == 0) + if (++c % (error ? 12000 : got_data ? 1000 : 3000) == 0) { PORTC^=LED_BLUE; PORTC |= LED_RED | LED_GREEN; } if (c % 12000 == 0) { - uint8_t result = w1_reset(); - if (result == 0) // device detected + if (!got_data) // we want to read the token + { + uint8_t result = w1_reset(); + if (result == 0) // device detected + { + PORTC &= ~LED_GREEN; + ds1992_read(0, replyBuffer, 128); + } + else if (result == 1) // no presence pulse == no device detected + PORTC &= ~LED_RED; + else // short circuit detected + PORTC &= ~(LED_RED | LED_GREEN); + } + else // we want to WRITE the token (*shudder*!) { - PORTC &= ~LED_GREEN; - ds1992_read(0, replyBuffer, 128); + uint8_t result = w1_reset(); + if (result == 0) // device detected + { + PORTC &= ~LED_GREEN; + + cli(); + memcpy(buf, replyBuffer, 33); + got_data = 0; + sei(); + + uint16_t writepos = ((uint16_t)buf[32]) * 32; + ds1992_scratch_write(writepos, buf, 32); + + result = w1_reset(); + if (result != 0) + { + replyBuffer[0] = 0x01; + error = 1; + goto write_failed; + } + + uint8_t es_reg; + result = ds1992_scratch_verify(writepos, buf, 32, &es_reg); + if (result != 0) + { + replyBuffer[0] = 0x02; + error = 1; + goto write_failed; + } + + replyBuffer[0] = 0xFF; // success + error = 0; + + write_failed: + 1; + } + else if (result == 1) // no presence pulse == no device detected + PORTC &= ~LED_RED; + else // short circuit detected + PORTC &= ~(LED_RED | LED_GREEN); } - else if (result == 1) // no presence pulse == no device detected - PORTC &= ~LED_RED; - else // short circuit detected - PORTC &= ~(LED_RED | LED_GREEN); } } -- cgit v1.2.1