summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Jung <flo@windfisch.org>2015-11-29 18:15:21 +0100
committerFlorian Jung <flo@windfisch.org>2015-11-29 18:15:21 +0100
commitfe742e35d3fa6d69125e9f463cbbe1c6032969ca (patch)
treec51a579de6498295cdb2e80edcdb9a162305e81a
parent84b980994934d54b3dad6dd2184830f4464b9933 (diff)
meeh
-rw-r--r--avr/1wire.c239
-rw-r--r--avr/1wire.h4
-rw-r--r--avr/Makefile2
-rw-r--r--avr/main.c24
-rw-r--r--avr/main_jtag.c.orig196
-rwxr-xr-xpc/a.outbin12696 -> 12744 bytes
-rw-r--r--pc/usbtest.c5
7 files changed, 180 insertions, 290 deletions
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 <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
+#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<<W1_PIN); W1_DDR |= 1<<W1_PIN; } while(0)
+#define hard_vcc do { W1_OUT |= 1<<W1_PIN; W1_DDR |= 1<<W1_PIN; } while(0)
+#define soft_vcc do { W1_DDR &= ~(1<<W1_PIN); W1_OUT |= 1<<W1_PIN; } while(0)
+#define tristate do { W1_DDR &= ~(1<<W1_PIN); W1_OUT &= ~(1<<W1_PIN); } while(0)
+
+/*uint8_t w1_reset(void)
+{
+ uint8_t err = 0;
+
+ hard_gnd;
+
+ _delay_us(480 ); // 480 us
+ cli();
+
+ soft_vcc;
+
+ _delay_us( 66 );
+ if (W1_IN & (1<<W1_PIN)) // no presence detect
+ err = 1;
+ sei();
+ _delay_us( 480 - 66 );
+ if( (W1_IN & (1<<W1_PIN)) == 0 ) // short circuit
+ err = 2;
+ return err;
+}*/
+
uint8_t w1_reset(void)
{
- uint8_t err;
-
- W1_OUT &= ~(1<<W1_PIN);
- W1_DDR |= 1<<W1_PIN;
- _delay_us(480 ); // 480 us
- cli();
- W1_DDR &= ~(1<<W1_PIN);
- _delay_us( 66 );
- err = W1_IN & (1<<W1_PIN); // no presence detect
- sei();
- _delay_us( 480 - 66 );
- if( (W1_IN & (1<<W1_PIN)) == 0 ) // short circuit
- err = 1;
- return err;
+ uint8_t err = 0;
+
+ hard_gnd;
+
+ _delay_us(480 ); // 480 us
+ cli();
+
+ hard_vcc;
+ _delay_us(33);
+ soft_vcc;
+
+ _delay_us( 66-33 );
+ if (W1_IN & (1<<W1_PIN)) // no presence detect
+ err = 1;
+ sei();
+ _delay_us( 480 - 66 );
+ if( (W1_IN & (1<<W1_PIN)) == 0 ) // short circuit
+ err = 2;
+ return err;
}
uint8_t w1_bit_io( uint8_t b )
{
- cli();
- W1_DDR |= 1<<W1_PIN;
- _delay_us( 1 );
- if( b )
- W1_DDR &= ~(1<<W1_PIN);
- _delay_us( 15 - 1 );
- if( (W1_IN & (1<<W1_PIN)) == 0 )
- b = 0;
- _delay_us( 60 - 15 );
- W1_DDR &= ~(1<<W1_PIN);
- sei();
- return b;
+ cli();
+ hard_gnd;
+
+ _delay_us( 1 );
+ if( b )
+ {
+ //hard_vcc;
+ //asm volatile("nop" ::: );
+ soft_vcc;
+ }
+ _delay_us( 15 - 1 );
+ if( (W1_IN & (1<<W1_PIN)) == 0 )
+ b = 0;
+ _delay_us( 60 - 15 );
+ soft_vcc;
+ sei();
+ return b;
}
-uint w1_byte_wr( uint8_t b )
+uint8_t w1_byte_wr( uint8_t b )
{
- uint8_t i = 8, j;
- do{
- j = w1_bit_io( b & 1 );
- b >>= 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 <string.h>
#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 <avr/io.h>
-#include <avr/interrupt.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <avr/pgmspace.h>
-#include <avr/eeprom.h>
-#include <util/delay.h>
-#include <avr/wdt.h>
-#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<<PC2);
-//Reconnect USB
- usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
- i = 0;
- while(--i)
- _delay_ms(2);
- usbDeviceConnect();
- usbInit();
- sei();
-
-
- leds[LED_RED].frequency = LED_ON;
- LED_init();
- for (i=0;i<3;i++)
- TIMER_delay(250);
- leds[LED_RED].frequency = LED_OFF;
-
- while(1)
- {
- usbPoll();
- LED_poll();
- if(usbNewDeviceAddr)
- leds[LED_BLUE].frequency = LED_ON;
- }
-}
-
-static uchar replyBuffer[8];
-
-uint8_t usbFunctionSetup(uint8_t data[8])
-{
- uchar len = 0;
-
- usb_state = STATE_IDLE;
- if(data[1] == FUNC_GET_TYPE)
- {
- replyBuffer[0] = 10;
- len = 1;
- }
- else if(data[1] == FUNC_START_BOOTLOADER)
- {
- cli();
- wdt_enable(WDTO_15MS);
- while(1);
- len = 0;
- }
- else if(data[1] == FUNC_READ_CHAIN)
- {
- chainpos = 0;
- usb_state = STATE_READ;
- len = 0xff;
- }
- else if(data[1] == FUNC_WRITE_CHAIN)
- {
- chainpos = 0;
- usb_state = STATE_WRITE;
- len = 0xff;
- }
- else if(data[1] == FUNC_TAP_STATE)
- {
- JTAG_goto_tap_state(data[2]);
- len = 0;
- }
- else if(data[1] == FUNC_GET_TAP_STATE)
- {
- replyBuffer[0] = JTAG_TAP_STATE;
- len = 1;
- }
- else if(data[1] == FUNC_OPEN)
- {
- leds[LED_BLUE].frequency = LED_ON;
- JTAG_attatch();
- len = 0;
- }
- else if(data[1] == FUNC_CLOSE)
- {
- leds[LED_BLUE].frequency = LED_OFF;
- JTAG_detatch();
- len = 0;
- }
- else if(data[1] == FUNC_SHIFTBITS)
- {
- replyBuffer[0] = JTAG_shift_bits(data[2],data[3]);
- len = 1;
- }
- else if(data[1] == FUNC_CHAINSIZE)
- {
- replyBuffer[0] = chainpos;
- replyBuffer[1] = chainpos>>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
--- a/pc/a.out
+++ b/pc/a.out
Binary files 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<nBytes; i++)
+ printf("%.2X ",buffer[i] & 0xFF);
+ printf("\n");
// write to device
nBytes = usb_control_msg(handle,