From fe742e35d3fa6d69125e9f463cbbe1c6032969ca Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Sun, 29 Nov 2015 18:15:21 +0100 Subject: meeh --- avr/1wire.c | 239 ++++++++++++++++++++++++++++++++------------------- avr/1wire.h | 4 +- avr/Makefile | 2 +- avr/main.c | 24 +++++- avr/main_jtag.c.orig | 196 ------------------------------------------ pc/a.out | Bin 12696 -> 12744 bytes pc/usbtest.c | 5 +- 7 files changed, 180 insertions(+), 290 deletions(-) delete mode 100644 avr/main_jtag.c.orig diff --git a/avr/1wire.c b/avr/1wire.c index 237abb9..5dbca82 100644 --- a/avr/1wire.c +++ b/avr/1wire.c @@ -9,122 +9,185 @@ #include #include #include +#include "1wire.h" #ifndef W1_PIN -#define W1_PIN PA0 -#define W1_IN PINA -#define W1_OUT PORTA -#define W1_DDR DDRA +#define W1_PIN PB4 +#define W1_IN PINB +#define W1_OUT PORTB +#define W1_DDR DDRB #endif uint8_t buff[2]; + +#define hard_gnd do { W1_OUT &= ~(1<>= 1; - if( j ) - b |= 0x80; - }while( --i ); - return b; + uint8_t i = 8, j; + do + { + j = w1_bit_io( b & 1 ); + b >>= 1; + if( j ) + b |= 0x80; + } + while( --i ); + return b; } -uint w1_byte_rd( void ) +uint8_t w1_byte_rd( void ) { - return w1_byte_wr( 0xFF ); + return w1_byte_wr( 0xFF ); } -uint8_t w1_rom_search( uint8_t diff, uint8_t idata *id ) +uint8_t w1_rom_search( uint8_t diff, uint8_t *id ) { - uint8_t i, j, next_diff; - uint8_t b; - - if( w1_reset() ) - return PRESENCE_ERR; // error, no device found - w1_byte_wr( SEARCH_ROM ); // ROM search command - next_diff = LAST_DEVICE; // unchanged on last device - i = 8 * 8; // 8 bytes - do{ - j = 8; // 8 bits - do{ - b = w1_bit_io( 1 ); // read bit - if( w1_bit_io( 1 ) ){ // read complement bit - if( b ) // 11 - return DATA_ERR; // data error - }else{ - if( !b ){ // 00 = 2 devices - if( diff > i || - ((*id & 1) && diff != i) ){ - b = 1; // now 1 - next_diff = i; // next pass 0 - } + uint8_t i, j, next_diff; + uint8_t b; + + if( w1_reset() ) + return PRESENCE_ERR; // error, no device found + w1_byte_wr( SEARCH_ROM ); // ROM search command + next_diff = LAST_DEVICE; // unchanged on last device + i = 8 * 8; // 8 bytes + do + { + j = 8; // 8 bits + do + { + b = w1_bit_io( 1 ); // read bit + if( w1_bit_io( 1 ) ) // read complement bit + { + if( b ) // 11 + return DATA_ERR; // data error + } + else + { + if( !b ) // 00 = 2 devices + { + if( diff > i || + ((*id & 1) && diff != i) ) + { + b = 1; // now 1 + next_diff = i; // next pass 0 + } + } + } + w1_bit_io( b ); // write bit + *id >>= 1; + if( b ) // store bit + *id |= 0x80; + i--; + } + while( --j ); + id++; // next byte } - } - w1_bit_io( b ); // write bit - *id >>= 1; - if( b ) // store bit - *id |= 0x80; - i--; - }while( --j ); - id++; // next byte - }while( i ); - return next_diff; // to continue search + while( i ); + return next_diff; // to continue search } +void ds1992_read(uint16_t addr, uint8_t* buf, uint8_t len) +{ + w1_byte_wr(SKIP_ROM); + w1_byte_wr(0xF0); // read + w1_byte_wr((addr & 0x00FF)); + w1_byte_wr((addr & 0xFF00) >> 8); + for (int i=0; i < len; i++) + buf[i] = w1_byte_rd(); +} -void w1_command( uint8_t command, uint8_t idata *id ) +void w1_command( uint8_t command, uint8_t *id ) { - uint8_t i; - w1_reset(); - if( id ){ - w1_byte_wr( MATCH_ROM ); // to a single device - i = 8; - do{ - w1_byte_wr( *id ); - id++; - }while( --i ); - }else{ - w1_byte_wr( SKIP_ROM ); // to all devices - } - w1_byte_wr( command ); + uint8_t i; + w1_reset(); + if( id ) + { + w1_byte_wr( MATCH_ROM ); // to a single device + i = 8; + do + { + w1_byte_wr( *id ); + id++; + } + while( --i ); + } + else + { + w1_byte_wr( SKIP_ROM ); // to all devices + } + w1_byte_wr( command ); } diff --git a/avr/1wire.h b/avr/1wire.h index d5657ad..accb132 100644 --- a/avr/1wire.h +++ b/avr/1wire.h @@ -21,7 +21,7 @@ uint8_t w1_reset(void); uint8_t w1_byte_wr( uint8_t b ); uint8_t w1_byte_rd( void ); -uint8_t w1_rom_search( uint8_t diff, uint8_t idata *id ); +uint8_t w1_rom_search( uint8_t diff, uint8_t*id ); -void w1_command( uint8_t command, uint8_t idata *id ); +void w1_command( uint8_t command, uint8_t *id ); #endif diff --git a/avr/Makefile b/avr/Makefile index 13bdd94..c5b62a8 100644 --- a/avr/Makefile +++ b/avr/Makefile @@ -61,7 +61,7 @@ OBJDIR = obj # List C source files here. (C dependencies are automatically generated.) -SRC = $(TARGET).c usbdrv/usbdrv.c usbdrv/oddebug.c +SRC = $(TARGET).c 1wire.c usbdrv/usbdrv.c usbdrv/oddebug.c # List C++ source files here. (C dependencies are automatically generated.) diff --git a/avr/main.c b/avr/main.c index d350637..5c9742f 100644 --- a/avr/main.c +++ b/avr/main.c @@ -30,7 +30,7 @@ #include #include "usbdrv/usbdrv.h" - +#include "1wire.h" #define LED_BLUE (1<<5) #define LED_RED (1<<4) @@ -125,6 +125,8 @@ int main(void) PORTB &= ~0x0C; PORTB |= 0x30; // enable pullups for PB4 and 5 + replyBuffer[129] = 0; + cli(); wdt_enable(WDTO_1S); // enable 1s watchdog timer @@ -146,7 +148,25 @@ int main(void) wdt_reset(); // keep the watchdog happy usbPoll(); - if (++c % 3000 == 0) PORTC^=LED_BLUE; + if (++c % 3000 == 0) + { + PORTC^=LED_BLUE; + PORTC |= LED_RED | LED_GREEN; + } + if (c % 12000 == 0) + { + uint8_t result = w1_reset(); + if (result == 0) + { + PORTC &= ~LED_GREEN; + ds1992_read(0x00, replyBuffer, 128); + } + else if (result == 1) + PORTC &= ~LED_RED; + else + PORTC &= ~(LED_RED | LED_GREEN); + + } /*if ( (c / 3000) % 40 == 0) { PORTC &= ~LED_RED; diff --git a/avr/main_jtag.c.orig b/avr/main_jtag.c.orig deleted file mode 100644 index 70cccb7..0000000 --- a/avr/main_jtag.c.orig +++ /dev/null @@ -1,196 +0,0 @@ -/************************************************************************************************ - * Project: USB AVR-ISP - * Author: Christian Ulrich - * Contact: christian at ullihome dot de - * - * Creation Date: 2008-12-10 - * Copyright: (c) 2008 by Christian Ulrich - * License: GPLv2 for private use - * commercial use prohibited - * - * Changes: - ***********************************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "usbdrv.h" -#include "usbconfig.h" -#include "led.h" -#include "timer.h" -#include "main.h" -#include "jtag.h" - -#define FUNC_READ_CHAIN 1 -#define FUNC_WRITE_CHAIN 2 -#define FUNC_TAP_STATE 3 -#define FUNC_GET_TAP_STATE 4 -#define FUNC_OPEN 5 -#define FUNC_CLOSE 6 -#define FUNC_SHIFTBITS 7 -#define FUNC_CHAINSIZE 8 -#define FUNC_EXECCHAIN 9 - -#define FUNC_START_BOOTLOADER 30 -#define FUNC_GET_TYPE 0xFE - -#define STATE_IDLE 0 -#define STATE_READ 1 -#define STATE_WRITE 2 - -uint8_t usb_state = STATE_IDLE; - -#ifndef USBASP_COMPATIBLE -led_t leds[] = {{4,LED_OFF,LED_OFF}, - {3,LED_OFF,LED_OFF}, - {5,LED_OFF,LED_OFF}}; -#else -led_t leds[] = {{0,LED_OFF,LED_OFF}, - {1,LED_OFF,LED_OFF}, - {3,LED_OFF,LED_OFF}}; -#endif -const uint8_t led_count = sizeof(leds)/sizeof(led_t); - - -#define MAX_CHAINSIZE 900 -unsigned char JTAG_CHAIN[MAX_CHAINSIZE]; -unsigned int chainpos = 0; - -int main(void) -{ - extern uchar usbNewDeviceAddr; - uint8_t i; - PORTC |= (1<>8; - len = 2; - } - else if (data[1] == FUNC_EXECCHAIN) - { -/* unsigned char i; - for (i = 0; i < len / 2; i++) - { - unsigned char z = JTAG_CHAIN[i]; - JTAG_CHAIN[i] = JTAG_CHAIN[len - i - 1]; - JTAG_CHAIN[len - i - 1] = z; - }*/ - JTAG_shift_bytes(JTAG_CHAIN,chainpos); - leds[LED_GREEN].counter = 10; - leds[LED_GREEN].frequency = LED_FLASH_NEG; - } - leds[LED_BLUE].counter = 10; - leds[LED_BLUE].frequency = LED_FLASH_NEG; - usbMsgPtr = replyBuffer; - return len; -} - -uint8_t usbFunctionRead( uint8_t *data, uint8_t len ) -{ - if (usb_state != STATE_READ) - return 0xff; - uint8_t asize = 0; - memcpy(data,&JTAG_CHAIN[chainpos],len); - chainpos += len; - asize+=len; - return asize; -} - -uint8_t usbFunctionWrite( uint8_t *data, uint8_t len ) -{ - if (usb_state != STATE_WRITE) - return 0xff; - memcpy(&JTAG_CHAIN[chainpos],data,len); - chainpos += len; - return len; -} diff --git a/pc/a.out b/pc/a.out index f1b6c1b..fbba741 100755 Binary files a/pc/a.out and b/pc/a.out differ diff --git a/pc/usbtest.c b/pc/usbtest.c index 002add4..148fc16 100644 --- a/pc/usbtest.c +++ b/pc/usbtest.c @@ -134,7 +134,10 @@ int main(int argc, char **argv) nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 0x21, 0, 0, (char *)buffer, sizeof(buffer), 1000); - printf("Got %d bytes: %s;\n", nBytes, buffer); + printf("Got %d bytes: %s = \n", nBytes, buffer); + for (int i=0; i