From 42947cd6b0e21078f3f6ae12a2514e849653345b Mon Sep 17 00:00:00 2001 From: Michael Waiblinger Date: Tue, 31 May 2016 01:38:27 +0200 Subject: [PATCH] Latest --- src/CANode.cpp | 183 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 151 insertions(+), 32 deletions(-) diff --git a/src/CANode.cpp b/src/CANode.cpp index 3bcf6fb..3e4aacd 100644 --- a/src/CANode.cpp +++ b/src/CANode.cpp @@ -12,13 +12,61 @@ #include #include - +#include +// Configurations +// Misc +#define DEBUG 0 +#define LED 13 +// Metro +#define METRO_CAN_tick 1 // 20 millisecond +#define METRO_OW_read_tick 20 // 20 millisecond +#define METRO_OW_search_tick 1000 // 1000 millisecond +// CAN bus +#define CAN_speed 125000 #define CAN_RS_PIN 2 +// OneWire +#define OW_pin 14 + +#define TEST_ADDR "125B275000000026" + +#define DS2406_FAMILY 0x12 +#define DS2406_WRITE_STATUS 0x55 +#define DS2406_READ_STATUS 0xaa +#define DS2406_CHANNEL_ACCESS 0xf5 +#define DS2406_CHANNEL_CONTROL1 0x4c // 01001100 - Read Both Pins +#define DS2406_CHANNEL_CONTROL2 0xff // for future dev +#define SKIP_ROM 0xCC +#define PIO_A 0x20 +#define PIO_B 0x40 + +#define DS2406_BUF_LEN 10 + +// Initialisation +// Misc +#if DEBUG + #define DEBUG_PRINT(a) Serial.print(a) + #define DEBUG_WRITE(a) Serial.write(a) +#else + #define DEBUG_PRINT(a) + #define DEBUG_WRITE(a) +#endif /* DEBUG */ + +int led = LED; +// Metro +Metro METRO_CAN = Metro(METRO_CAN_tick); +Metro METRO_OW_read = Metro(METRO_OW_read_tick); +Metro METRO_OW_search = Metro(METRO_OW_search_tick); +// CAN bus +FlexCAN CANbus(CAN_speed); +// OneWire +uint8_t addr[8]; +uint8_t buffer[DS2406_BUF_LEN]; +uint8_t tmp; +OneWire OW_1(OW_pin); + + -Metro sysTimer = Metro(1);// milliseconds -int led = 13; -FlexCAN CANbus(125000); static CAN_message_t msg,rxmsg; static uint8_t hex[17] = "0123456789abcdef"; @@ -26,33 +74,68 @@ int txCount,rxCount; unsigned int txTimer,rxTimer; -// ------------------------------------------------------------- -static void hexDump(uint8_t dumpLen, uint8_t *bytePtr) -{ - uint8_t working; - while( dumpLen-- ) { - working = *bytePtr++; - Serial.write( hex[ working>>4 ] ); - Serial.write( hex[ working&15 ] ); +// Functions ------------------------------------------------------- +void PrintBytes(uint8_t* addr, uint8_t count, bool newline=0) { + for (uint8_t i = 0; i < count; i++) { + Serial.print(addr[i]>>4, HEX); + Serial.print(addr[i]&0x0f, HEX); } - Serial.write('\r'); - Serial.write('\n'); + if (newline) + Serial.println(); +} +void DEBUG_Bytes(uint8_t* addr, uint8_t count, bool newline=0) { + #if DEBUG + for (uint8_t i = 0; i < count; i++) { + Serial.print(addr[i]>>4, HEX); + Serial.print(addr[i]&0x0f, HEX); + } + if (newline) + Serial.println(); + #endif +} +void print_OW_Device(uint8_t *addr) { + for(int i = 0; i < 8; i++) { + if(i != 0) { + Serial.print(":"); + } + Serial.print(addr[i], HEX); + } +} +uint8_t read_DS2406(uint8_t* addr) { + // We're using a temporary switch object here only because we're + // discovering them dynamically. Depending on your app, you may + // already know the serial number of the device you intend to + // control and will use it there. + OW_1.reset(); + OW_1.select(addr); + OW_1.write(DS2406_CHANNEL_ACCESS,1); + OW_1.write(DS2406_CHANNEL_CONTROL1,1); + OW_1.write(DS2406_CHANNEL_CONTROL2,1); + tmp = OW_1.read(); + OW_1.reset(); + #if DEBUG + PrintBytes(&tmp,1,1); + #endif /* DEBUG */ + return tmp; } - - // ------------------------------------------------------------- void setup(void) { - pinMode(CAN_RS_PIN,OUTPUT); - digitalWrite(CAN_RS_PIN,0); - CANbus.begin(); + // Misc + Serial.begin(115200); pinMode(led, OUTPUT); digitalWrite(led, 1); - delay(1000); - Serial.println(F("Hello Teensy 3.1 CAN Test.")); + //CAN bus + pinMode(CAN_RS_PIN,OUTPUT); + digitalWrite(CAN_RS_PIN,0); + CANbus.begin(); - sysTimer.reset(); + delay(100); + Serial.println(F("Hello Teensy 3.2 CANode awakes.")); + METRO_CAN.reset(); + METRO_OW_read.reset(); + METRO_OW_search.reset(); } @@ -60,7 +143,42 @@ void setup(void) void loop(void) { // service software timers based on Metro tick - if ( sysTimer.check() ) { + if ( METRO_OW_search.check() ) { + //Serial.print("Beginning Search...\n"); + OW_1.reset_search(); + // Assume we can't find something until we prove otherwise. +// bool foundDevice = false; + + while(OW_1.search(addr) == 1) { + if ( OneWire::crc8( addr, 7) != addr[7]) { + Serial.print("CRC is not valid!\n"); + delay(100); + return; + } + + Serial.print("Found a device: "); + print_OW_Device(addr); + Serial.println(""); + + + // At this point, we know we have a DS2406. Blink it. + if(addr[0] == DS2406_FAMILY) { +// foundDevice = true; + // digitalWrite(13, HIGH); + tmp = read_DS2406(addr); + bool pioA = tmp & 0x04; + bool pioB = tmp & 0x08; + digitalWrite(led, pioA); + } + } + } + if (METRO_OW_read.check() ) { + + } + + + + if ( METRO_CAN.check() ) { if ( txTimer ) { --txTimer; } @@ -73,7 +191,7 @@ void loop(void) if ( !rxTimer ) { while ( CANbus.read(rxmsg) ) { //hexDump( sizeof(rxmsg), (uint8_t *)&rxmsg ); - Serial.write(rxmsg.buf[0]); + DEBUG_WRITE(rxmsg.buf[0]); rxCount++; } } @@ -82,10 +200,12 @@ void loop(void) if ( !txTimer ) { // if frames were received, print the count if ( rxCount ) { - Serial.write('='); - Serial.print(rxCount); - Serial.print(",Hello="); - Serial.print(rxmsg.id,HEX); + #if DEBUG + Serial.write('='); + Serial.print(rxCount); + Serial.print(",Hello="); + Serial.print(rxmsg.id,HEX); + #endif rxCount = 0; } txTimer = 100;//milliseconds @@ -96,16 +216,15 @@ void loop(void) } // send 6 at a time to force tx buffering txCount = 16; - digitalWrite(led, 1); - Serial.println("."); + //digitalWrite(led, 1); +// Serial.println("."); while ( txCount-- ) { CANbus.write(msg); msg.buf[0]++; } - digitalWrite(led, 0); + // digitalWrite(led, 0); // time delay to force some rx data queue use rxTimer = 3;//milliseconds } } -