From 1da1a52c682f95a7a998711c53aa887741662e51 Mon Sep 17 00:00:00 2001 From: Allen Hill Date: Fri, 18 Aug 2023 21:27:32 -0400 Subject: [PATCH] Reformat using clang format --- .clang-format | 6 + GlobalDef.c | 71 ++- GlobalDef.h | 46 +- avclandrv.c | 1507 +++++++++++++++++++++++++------------------------ avclandrv.h | 89 +-- com232.c | 179 +++--- com232.h | 94 ++- sniffer.c | 328 +++++------ 8 files changed, 1193 insertions(+), 1127 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..ebd5f69 --- /dev/null +++ b/.clang-format @@ -0,0 +1,6 @@ +--- +BasedOnStyle: LLVM +UseCRLF: false +BreakBeforeBraces: Attach +AllowShortBlocksOnASingleLine: Empty +... diff --git a/GlobalDef.c b/GlobalDef.c index b32c327..4c4f8bf 100644 --- a/GlobalDef.c +++ b/GlobalDef.c @@ -1,36 +1,35 @@ -/* - Copyright (C) 2006 Marcin Slonicki . - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - - ----------------------------------------------------------------------- - this file is a part of the TOYOTA Corolla MP3 Player Project - ----------------------------------------------------------------------- - http://www.softservice.com.pl/corolla/avc - - May 28 / 2009 - version 2 - -*/ - -#include "GlobalDef.h" - - -// max 10 events in fifo -byte EventCount; -byte EventCmd[10]; -byte Event; - -byte showLog; -byte showLog2; +/* + Copyright (C) 2006 Marcin Slonicki . + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + ----------------------------------------------------------------------- + this file is a part of the TOYOTA Corolla MP3 Player Project + ----------------------------------------------------------------------- + http://www.softservice.com.pl/corolla/avc + + May 28 / 2009 - version 2 + +*/ + +#include "GlobalDef.h" + +// max 10 events in fifo +byte EventCount; +byte EventCmd[10]; +byte Event; + +byte showLog; +byte showLog2; diff --git a/GlobalDef.h b/GlobalDef.h index 0232632..bc14f48 100644 --- a/GlobalDef.h +++ b/GlobalDef.h @@ -12,49 +12,49 @@ #include -#define FALSE 0 -#define TRUE (!FALSE) +#define FALSE 0 +#define TRUE (!FALSE) // AVC LAN bus on AC2 (PA6/7) // PA6 AINP0 + // PA7 AINN1 - -#define INPUT_IS_SET ( bit_is_set( AC2_STATUS, AC_STATE_bp ) ) -#define INPUT_IS_CLEAR ( bit_is_clear( AC2_STATUS, AC_STATE_bp ) ) +#define INPUT_IS_SET (bit_is_set(AC2_STATUS, AC_STATE_bp)) +#define INPUT_IS_CLEAR (bit_is_clear(AC2_STATUS, AC_STATE_bp)) -#define LED_DDR DDRB -#define LED_PORT PORTB -#define LEDOUT _BV(PORT5) +#define LED_DDR DDRB +#define LED_PORT PORTB +#define LEDOUT _BV(PORT5) #define sbi(port, bit) (port) |= (1 << (bit)) // Set bit (i.e. to 1) #define cbi(port, bit) (port) &= ~(1 << (bit)) // Clear bit (i.e. set bit to 0) -typedef unsigned char byte; -typedef unsigned int word; +typedef unsigned char byte; +typedef unsigned int word; -inline void LedOff( void ); -inline void LedOn( void ); +inline void LedOff(void); +inline void LedOn(void); // max 10 events in fifo extern byte EventCount; extern byte EventCmd[10]; extern byte Event; -#define EV_NOTHING 0 -#define EV_DISPLAY 1 -#define EV_STATUS 4 +#define EV_NOTHING 0 +#define EV_DISPLAY 1 +#define EV_STATUS 4 // const -#define smYear 1 -#define smMonth 2 -#define smDay 3 -#define smHour 4 -#define smMin 5 -#define smWDay 6 +#define smYear 1 +#define smMonth 2 +#define smDay 3 +#define smHour 4 +#define smMin 5 +#define smWDay 6 -//#define STOPEvent cbi(TIMSK, TOIE1); cbi(UCSRB, RXCIE); -//#define STARTEvent sbi(TIMSK, TOIE1); sbi(UCSRB, RXCIE); +// #define STOPEvent cbi(TIMSK, TOIE1); cbi(UCSRB, RXCIE); +// #define STARTEvent sbi(TIMSK, TOIE1); sbi(UCSRB, RXCIE); extern byte showLog; extern byte showLog2; -#endif // _GLOBALDEF_H_ +#endif // _GLOBALDEF_H_ diff --git a/avclandrv.c b/avclandrv.c index cdc1ae1..b77d821 100644 --- a/avclandrv.c +++ b/avclandrv.c @@ -27,22 +27,22 @@ */ -#include #include +#include #include +#include "GlobalDef.h" #include "avclandrv.h" #include "com232.h" -#include "GlobalDef.h" - - -#define AVC_OUT_EN() cbi(AC2_CTRLA, AC_ENABLE_bp); sbi(VPORTA_DIR, 6); // Write mode -#define AVC_OUT_DIS() cbi(VPORTA_DIR, 6); sbi(AC2_CTRLA, AC_ENABLE_bp); // Read mode -#define AVC_SET_LOGICAL_1() \ - __asm__ __volatile__ ("sbi VPORTA_OUT, 6;"); -#define AVC_SET_LOGICAL_0() \ - __asm__ __volatile__ ("cbi VPORTA_OUT, 6;"); +#define AVC_OUT_EN() \ + cbi(AC2_CTRLA, AC_ENABLE_bp); \ + sbi(VPORTA_DIR, 6); // Write mode +#define AVC_OUT_DIS() \ + cbi(VPORTA_DIR, 6); \ + sbi(AC2_CTRLA, AC_ENABLE_bp); // Read mode +#define AVC_SET_LOGICAL_1() __asm__ __volatile__("sbi VPORTA_OUT, 6;"); +#define AVC_SET_LOGICAL_0() __asm__ __volatile__("cbi VPORTA_OUT, 6;"); byte CD_ID_1; byte CD_ID_2; @@ -66,7 +66,6 @@ byte answerReq; cd_modes CD_Mode; - byte broadcast; byte master1; byte master2; @@ -83,855 +82,913 @@ byte data[MAXMSGLEN]; // when is more then 1 min, FORCE answer. byte check_timeout; -#define SW_ID 0x11 // 11 For my stereo +#define SW_ID 0x11 // 11 For my stereo // commands -const byte stat1[] = { 0x4, 0x00, 0x00, 0x01, 0x0A }; -const byte stat2[] = { 0x4, 0x00, 0x00, 0x01, 0x08 }; -const byte stat3[] = { 0x4, 0x00, 0x00, 0x01, 0x0D }; -const byte stat4[] = { 0x4, 0x00, 0x00, 0x01, 0x0C }; +const byte stat1[] = {0x4, 0x00, 0x00, 0x01, 0x0A}; +const byte stat2[] = {0x4, 0x00, 0x00, 0x01, 0x08}; +const byte stat3[] = {0x4, 0x00, 0x00, 0x01, 0x0D}; +const byte stat4[] = {0x4, 0x00, 0x00, 0x01, 0x0C}; // broadcast -const byte lan_stat1[] = { 0x3, 0x00, 0x01, 0x0A }; -const byte lan_reg[] = { 0x3, SW_ID, 0x01, 0x00 }; -const byte lan_init[] = { 0x3, SW_ID, 0x01, 0x01 }; -const byte lan_check[] = { 0x3, SW_ID, 0x01, 0x20 }; -const byte lan_playit[] = { 0x4, SW_ID, 0x01, 0x45, 0x63 }; +const byte lan_stat1[] = {0x3, 0x00, 0x01, 0x0A}; +const byte lan_reg[] = {0x3, SW_ID, 0x01, 0x00}; +const byte lan_init[] = {0x3, SW_ID, 0x01, 0x01}; +const byte lan_check[] = {0x3, SW_ID, 0x01, 0x20}; +const byte lan_playit[] = {0x4, SW_ID, 0x01, 0x45, 0x63}; -const byte play_req1[] = { 0x4, 0x00, 0x25, 0x63, 0x80 }; +const byte play_req1[] = {0x4, 0x00, 0x25, 0x63, 0x80}; #ifdef __AVENSIS__ - const byte play_req2[] = { 0x6, 0x00, SW_ID, 0x63, 0x42 }; +const byte play_req2[] = {0x6, 0x00, SW_ID, 0x63, 0x42}; #else - const byte play_req2[] = { 0x6, 0x00, SW_ID, 0x63, 0x42, 0x01, 0x00 }; +const byte play_req2[] = {0x6, 0x00, SW_ID, 0x63, 0x42, 0x01, 0x00}; #endif -const byte play_req3[] = { 0x5, 0x00, SW_ID, 0x63, 0x42, 0x41 }; -const byte stop_req[] = { 0x5, 0x00, SW_ID, 0x63, 0x43, 0x01 }; -const byte stop_req2[] = { 0x5, 0x00, SW_ID, 0x63, 0x43, 0x41 }; - +const byte play_req3[] = {0x5, 0x00, SW_ID, 0x63, 0x42, 0x41}; +const byte stop_req[] = {0x5, 0x00, SW_ID, 0x63, 0x43, 0x01}; +const byte stop_req2[] = {0x5, 0x00, SW_ID, 0x63, 0x43, 0x41}; // answers -const byte CMD_REGISTER[] = {0x1, 0x05, 0x00, 0x01, SW_ID, 0x10, 0x63 }; -const byte CMD_STATUS1[] = {0x1, 0x04, 0x00, 0x01, 0x00, 0x1A }; -const byte CMD_STATUS2[] = {0x1, 0x04, 0x00, 0x01, 0x00, 0x18 }; -const byte CMD_STATUS3[] = {0x1, 0x04, 0x00, 0x01, 0x00, 0x1D }; -const byte CMD_STATUS4[] = {0x1, 0x05, 0x00, 0x01, 0x00, 0x1C, 0x00 }; -byte CMD_CHECK[] = {0x1, 0x06, 0x00, 0x01, SW_ID, 0x30, 0x00, 0x00 }; +const byte CMD_REGISTER[] = {0x1, 0x05, 0x00, 0x01, SW_ID, 0x10, 0x63}; +const byte CMD_STATUS1[] = {0x1, 0x04, 0x00, 0x01, 0x00, 0x1A}; +const byte CMD_STATUS2[] = {0x1, 0x04, 0x00, 0x01, 0x00, 0x18}; +const byte CMD_STATUS3[] = {0x1, 0x04, 0x00, 0x01, 0x00, 0x1D}; +const byte CMD_STATUS4[] = {0x1, 0x05, 0x00, 0x01, 0x00, 0x1C, 0x00}; +byte CMD_CHECK[] = {0x1, 0x06, 0x00, 0x01, SW_ID, 0x30, 0x00, 0x00}; -const byte CMD_STATUS5[] = {0x1, 0x05, 0x00, 0x5C, 0x12, 0x53, 0x02 }; -const byte CMD_STATUS5A[] = {0x0, 0x05, 0x5C, 0x31, 0xF1, 0x00, 0x00 }; +const byte CMD_STATUS5[] = {0x1, 0x05, 0x00, 0x5C, 0x12, 0x53, 0x02}; +const byte CMD_STATUS5A[] = {0x0, 0x05, 0x5C, 0x31, 0xF1, 0x00, 0x00}; -const byte CMD_STATUS6[] = {0x1, 0x06, 0x00, 0x5C, 0x32, 0xF0, 0x02, 0x00 }; +const byte CMD_STATUS6[] = {0x1, 0x06, 0x00, 0x5C, 0x32, 0xF0, 0x02, 0x00}; +const byte CMD_PLAY_OK1[] = {0x1, 0x05, 0x00, 0x63, SW_ID, 0x50, 0x01}; +const byte CMD_PLAY_OK2[] = {0x1, 0x05, 0x00, 0x63, SW_ID, 0x52, 0x01}; +const byte CMD_PLAY_OK3[] = {0x0, 0x0B, 0x63, 0x31, 0xF1, 0x01, 0x00, + 0x01, 0xFF, 0xFF, 0xFF, 0x00, 0x80}; +byte CMD_PLAY_OK4[] = {0x0, 0x0B, 0x63, 0x31, 0xF1, 0x01, 0x28, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}; -const byte CMD_PLAY_OK1[] = {0x1, 0x05, 0x00, 0x63, SW_ID, 0x50, 0x01 }; -const byte CMD_PLAY_OK2[] = {0x1, 0x05, 0x00, 0x63, SW_ID, 0x52, 0x01 }; -const byte CMD_PLAY_OK3[] = {0x0, 0x0B, 0x63, 0x31, 0xF1, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0x00, 0x80 }; -byte CMD_PLAY_OK4[] = {0x0, 0x0B, 0x63, 0x31, 0xF1, 0x01, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 }; +const byte CMD_STOP1[] = {0x1, 0x05, 0x00, 0x63, SW_ID, 0x53, 0x01}; +byte CMD_STOP2[] = {0x0, 0x0B, 0x63, 0x31, 0xF1, 0x00, 0x30, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}; -const byte CMD_STOP1[] = {0x1, 0x05, 0x00, 0x63, SW_ID, 0x53, 0x01 }; -byte CMD_STOP2[] = {0x0, 0x0B, 0x63, 0x31, 0xF1, 0x00, 0x30, 0x00, 0x00,0x00, 0x00, 0x00, 0x80 }; +const byte CMD_BEEP[] = {0x1, 0x05, 0x00, 0x63, 0x29, 0x60, 0x02}; -const byte CMD_BEEP[] = {0x1, 0x05, 0x00, 0x63, 0x29, 0x60, 0x02 }; +void AVC_HoldLine() { + STOPEvent; + // wait for free line + byte line_busy = 1; -void AVC_HoldLine() -{ - STOPEvent; + TCB1.CNT = 0; + do { + while (INPUT_IS_CLEAR) { + /* The comparison value was originally 25 with CK64 (tick period + of 4.34 us) at a clock frequency 14.7456MHz. For a more accurate tick + period of .5 us at 16MHz, the value should be approximately 225*/ + if (TCB1.CNT >= 900) + break; + } + if (TCB1.CNT > 864) + line_busy = 0; + } while (line_busy); - // wait for free line - byte line_busy = 1; + // switch to out mode + AVC_OUT_EN(); + AVC_SET_LOGICAL_1(); - TCB1.CNT = 0; - do { - while (INPUT_IS_CLEAR) { - /* The comparison value was originally 25 with CK64 (tick period of 4.34 us) - at a clock frequency 14.7456MHz. - For a more accurate tick period of .5 us at 16MHz, the value should be approximately 225*/ - if (TCB1.CNT >= 900) break; - } - if (TCB1.CNT > 864) line_busy=0; - } while (line_busy); - - // switch to out mode - AVC_OUT_EN(); - AVC_SET_LOGICAL_1(); - - STARTEvent; + STARTEvent; } -void AVC_ReleaseLine() -{ - AVC_SET_LOGICAL_0(); - AVC_OUT_DIS(); +void AVC_ReleaseLine() { + AVC_SET_LOGICAL_0(); + AVC_OUT_DIS(); } -void AVCLan_Init() -{ - PORTA.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc; // Disable input buffer; recommended when using AC - PORTA.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc; +void AVCLan_Init() { + PORTA.PIN6CTRL = PORT_ISC_INPUT_DISABLE_gc; // Disable input buffer; + // recommended when using AC + PORTA.PIN7CTRL = PORT_ISC_INPUT_DISABLE_gc; - // Pull-ups are disabled by default - VPORTA.DIR &= ~(PIN6_bm | PIN7_bm); // Zero pin 6 and 7 to set as input + // Pull-ups are disabled by default + VPORTA.DIR &= ~(PIN6_bm | PIN7_bm); // Zero pin 6 and 7 to set as input + // Analog comparator + AC2.CTRLA = AC_OUTEN_bm | AC_HYSMODE_25mV_gc | AC_ENABLE_bm; - // Analog comparator - AC2.CTRLA = AC_OUTEN_bm | AC_HYSMODE_25mV_gc | AC_ENABLE_bm; + TCB1.CTRLB = TCB_ASYNC_bm | TCB_CNTMODE_SINGLE_gc; + TCB1.EVCTRL = TCB_CAPTEI_bm; + TCB1.INTCTRL = TCB_CAPT_bm; + EVSYS.ASYNCUSER0 = EVSYS_ASYNCUSER0_ASYNCCH0_gc; + TCB1.CTRLA = TCB_CLKSEL_CLKDIV2_gc | TCB_ENABLE_bm; - TCB1.CTRLB = TCB_ASYNC_bm | TCB_CNTMODE_SINGLE_gc; - TCB1.EVCTRL = TCB_CAPTEI_bm; - TCB1.INTCTRL = TCB_CAPT_bm; - EVSYS.ASYNCUSER0 = EVSYS_ASYNCUSER0_ASYNCCH0_gc; - TCB1.CTRLA = TCB_CLKSEL_CLKDIV2_gc | TCB_ENABLE_bm; - - message_len = 0; - answerReq = cmNull; - check_timeout = 0; - - cd_Disc = 1; - cd_Track = 1; - cd_Time_Min = 0; - cd_Time_Sec = 0; - repeatMode = 0; - randomMode = 0; - playMode = 0; - CD_Mode = stStop; + message_len = 0; + answerReq = cmNull; + check_timeout = 0; + cd_Disc = 1; + cd_Track = 1; + cd_Time_Min = 0; + cd_Time_Sec = 0; + repeatMode = 0; + randomMode = 0; + playMode = 0; + CD_Mode = stStop; } -byte AVCLan_Read_Byte(byte length) -{ - byte bite = 0; +byte AVCLan_Read_Byte(byte length) { + byte bite = 0; - while (1) { - while (INPUT_IS_CLEAR); - TCB1.CNT = 0; - while (INPUT_IS_SET); // If input was set for less than 26 us - if ( TCB1.CNT < 208 ) { // (a generous half period), bit was a 1 + while (1) { + while (INPUT_IS_CLEAR) {} + TCB1.CNT = 0; + while (INPUT_IS_SET) {} // If input was set for less than 26 us + if (TCB1.CNT < 208) { // (a generous half period), bit was a 1 bite++; parity_bit++; - } - length--; - if (!length) return bite; - bite = bite << 1; - } + } + length--; + if (!length) + return bite; + bite = bite << 1; + } } void set_AVC_logic_for(uint8_t val, uint16_t period) { - if (val == 1) { - AVC_SET_LOGICAL_1(); + if (val == 1) { + AVC_SET_LOGICAL_1(); + } else { + AVC_SET_LOGICAL_0(); + } + TCB1.CCMP = period; + EVSYS.ASYNCSTROBE = EVSYS_ASYNCCH00_bm; + loop_until_bit_is_set(TCB1_INTFLAGS, 0); + TCB1_INTFLAGS = 1; +} + +byte AVCLan_Send_StartBit() { + set_AVC_logic_for(1, 1328); // 166 us @ 125 ns tick (for F_CPU = 16MHz) + set_AVC_logic_for(0, 152); // 19 us @ 125 ns tick (for F_CPU = 16MHz) + + return 1; +} + +void AVCLan_Send_Bit1() { + set_AVC_logic_for(1, 164); // 20.5 us @ 125 ns tick (for F_CPU = 16MHz) + set_AVC_logic_for(0, 152); // 19 us @ 125 ns tick (for F_CPU = 16MHz) +} + +void AVCLan_Send_Bit0() { + set_AVC_logic_for(1, 272); // 34 us @ 125 ns tick (for F_CPU = 16MHz) + set_AVC_logic_for(0, 44); // 5.5 us @ 125 ns tick (for F_CPU = 16MHz) +} + +byte AVCLan_Read_ACK() { + set_AVC_logic_for(1, 152); // 34 us @ 125 ns tick (for F_CPU = 16MHz) + AVC_SET_LOGICAL_0(); // Replace with AVC_ReleaseLine? + AVC_OUT_DIS(); // switch to read mode + + TCB1.CNT = 0; + while (1) { + if (INPUT_IS_SET && (TCB1.CNT > 208)) + break; // Make sure INPUT is not still set from us + // Line of experimentation: Try changing TCNT0 comparison value or remove + // check entirely + if (TCB1.CNT > 300) + return 1; // Not sure if this fix is intent correct + } + + while (INPUT_IS_SET) {} + AVC_OUT_EN(); // back to write mode + return 0; +} + +byte AVCLan_Send_ACK() { + TCB1.CNT = 0; + while (INPUT_IS_CLEAR) { + if (TCB1.CNT >= 900) + return 0; // max wait time + } + + AVC_OUT_EN(); + + set_AVC_logic_for(1, 272); // 34 us @ 125 ns tick (for F_CPU = 16MHz) + set_AVC_logic_for(0, 44); // 5.5 us @ 125 ns tick (for F_CPU = 16MHz) + + AVC_OUT_DIS(); + + return 1; +} + +byte AVCLan_Send_Byte(byte bite, byte len) { + byte b; + if (len == 8) { + b = bite; + } else { + b = bite << (8 - len); + } + + while (1) { + if ((b & 128) != 0) { + AVCLan_Send_Bit1(); + parity_bit++; } else { - AVC_SET_LOGICAL_0(); + AVCLan_Send_Bit0(); } - TCB1.CCMP = period; - EVSYS.ASYNCSTROBE = EVSYS_ASYNCCH00_bm; - loop_until_bit_is_set(TCB1_INTFLAGS, 0); - TCB1_INTFLAGS = 1; -} - -byte AVCLan_Send_StartBit() -{ - set_AVC_logic_for(1, 1328); // 166 us @ 125 ns tick (for F_CPU = 16MHz) - set_AVC_logic_for(0, 152); // 19 us @ 125 ns tick (for F_CPU = 16MHz) - - return 1; -} - -void AVCLan_Send_Bit1() -{ - set_AVC_logic_for(1, 164); // 20.5 us @ 125 ns tick (for F_CPU = 16MHz) - set_AVC_logic_for(0, 152); // 19 us @ 125 ns tick (for F_CPU = 16MHz) -} - -void AVCLan_Send_Bit0() -{ - set_AVC_logic_for(1, 272); // 34 us @ 125 ns tick (for F_CPU = 16MHz) - set_AVC_logic_for(0, 44); // 5.5 us @ 125 ns tick (for F_CPU = 16MHz) -} - -byte AVCLan_Read_ACK() -{ - set_AVC_logic_for(1, 152); // 34 us @ 125 ns tick (for F_CPU = 16MHz) - AVC_SET_LOGICAL_0(); // Replace with AVC_ReleaseLine? - AVC_OUT_DIS(); // switch to read mode - - TCB1.CNT = 0; - while(1) { - if (INPUT_IS_SET && (TCB1.CNT > 208 )) break; // Make sure INPUT is not still set from us - // Line of experimentation: Try changing TCNT0 comparison value or remove check entirely - if (TCB1.CNT > 300 ) return 1; // Not sure if this fix is intent correct + len--; + if (!len) { + // if (INPUT_IS_SET) RS232_Print_P(PSTR("SBER\n")); // Send Bit ERror + return 1; } - - while(INPUT_IS_SET); - AVC_OUT_EN(); // back to write mode - return 0; + b = b << 1; + } } -byte AVCLan_Send_ACK() -{ - TCB1.CNT = 0; - while (INPUT_IS_CLEAR) { - if (TCB1.CNT >= 900) return 0; // max wait time - } - - AVC_OUT_EN(); - - set_AVC_logic_for(1, 272); // 34 us @ 125 ns tick (for F_CPU = 16MHz) - set_AVC_logic_for(0, 44); // 5.5 us @ 125 ns tick (for F_CPU = 16MHz) - - AVC_OUT_DIS(); - - return 1; +byte AVCLan_Send_ParityBit() { + if ((parity_bit & 1) != 0) { + AVCLan_Send_Bit1(); + // parity_bit++; + } else { + AVCLan_Send_Bit0(); + } + parity_bit = 0; + return 1; } -byte AVCLan_Send_Byte(byte bite, byte len) -{ - byte b; - if (len==8) { - b = bite; - } else { - b = bite << (8-len); - } +byte CheckCmd(byte *cmd) { + byte i; + byte *c; + byte l; - while (1) { - if ( (b & 128)!=0 ) { - AVCLan_Send_Bit1(); - parity_bit++; - } else { - AVCLan_Send_Bit0(); - } - len--; - if (!len) { - //if (INPUT_IS_SET) RS232_Print_P(PSTR("SBER\n")); // Send Bit ERror - return 1; - } - b = b << 1; - } + c = cmd; + l = *c++; -} - -byte AVCLan_Send_ParityBit() -{ - if ( (parity_bit & 1)!=0 ) { - AVCLan_Send_Bit1(); - //parity_bit++; - } else { - AVCLan_Send_Bit0(); - } - parity_bit=0; - return 1; -} - -byte CheckCmd(byte *cmd) -{ - byte i; - byte *c; - byte l; - - c = cmd; - l = *c++; - - for (i=0; i 255 ) { // 170 us - // // TCCR1B = 0; - // // TCCR1B |= (1 << WGM12)|(1 << CS12); // Set CTC, prescaler at 256 - // STARTEvent; - // RS232_Print_P(PSTR("LAN>T1\n")); - // return 0; - // } - // } - // - // if ( TCNT0 < 20 ) { // 20 us - // // TCCR1B = 0; - // // TCCR1B |= (1 << WGM12)|(1 << CS12); - // STARTEvent; - // RS232_Print_P(PSTR("LAN>T2\n")); - // return 0; - // } + // RS232_Print_P(PSTR("$ ")); + // TCCR1B |= (1 << CS11)|(1 << CS10); // Timer1 prescaler at 64 + // TCNT1 = 0; + // TCNT0 = 0; + // while (INPUT_IS_SET) { + // if ( TCNT0 > 255 ) { // 170 us + // // TCCR1B = 0; + // // TCCR1B |= (1 << WGM12)|(1 << CS12); // Set CTC, prescaler at 256 + // STARTEvent; + // RS232_Print_P(PSTR("LAN>T1\n")); + // return 0; + // } + // } + // + // if ( TCNT0 < 20 ) { // 20 us + // // TCCR1B = 0; + // // TCCR1B |= (1 << WGM12)|(1 << CS12); + // STARTEvent; + // RS232_Print_P(PSTR("LAN>T2\n")); + // return 0; + // } + AVCLan_Read_Byte(1); + + broadcast = AVCLan_Read_Byte(1); + + parity_bit = 0; + master1 = AVCLan_Read_Byte(4); + master2 = AVCLan_Read_Byte(8); + if ((parity_bit & 1) != AVCLan_Read_Byte(1)) { + STARTEvent; + return 0; + } + + parity_bit = 0; + slave1 = AVCLan_Read_Byte(4); + slave2 = AVCLan_Read_Byte(8); + if ((parity_bit & 1) != AVCLan_Read_Byte(1)) { + STARTEvent; + return 0; + } + // is this command for me ? + if ((slave1 == CD_ID_1) && (slave2 == CD_ID_2)) { + for_me = 1; + } + + if (for_me) + AVCLan_Send_ACK(); + else AVCLan_Read_Byte(1); - broadcast = AVCLan_Read_Byte(1); - - parity_bit = 0; - master1 = AVCLan_Read_Byte(4); - master2 = AVCLan_Read_Byte(8); - if ((parity_bit&1)!=AVCLan_Read_Byte(1)) { + parity_bit = 0; + AVCLan_Read_Byte(4); // control - always 0xF + if ((parity_bit & 1) != AVCLan_Read_Byte(1)) { STARTEvent; return 0; - } + } + if (for_me) + AVCLan_Send_ACK(); + else + AVCLan_Read_Byte(1); - parity_bit = 0; - slave1 = AVCLan_Read_Byte(4); - slave2 = AVCLan_Read_Byte(8); - if ((parity_bit&1)!=AVCLan_Read_Byte(1)) { + parity_bit = 0; + message_len = AVCLan_Read_Byte(8); + if ((parity_bit & 1) != AVCLan_Read_Byte(1)) { STARTEvent; return 0; - } - // is this command for me ? - if ((slave1==CD_ID_1)&&(slave2==CD_ID_2)) { - for_me=1; - } + } + if (for_me) + AVCLan_Send_ACK(); + else + AVCLan_Read_Byte(1); - if (for_me) AVCLan_Send_ACK(); - else AVCLan_Read_Byte(1); - - parity_bit = 0; - AVCLan_Read_Byte(4); // control - always 0xF - if ((parity_bit&1)!=AVCLan_Read_Byte(1)) { - STARTEvent; - return 0; - } - if (for_me) AVCLan_Send_ACK(); - else AVCLan_Read_Byte(1); - - parity_bit = 0; - message_len = AVCLan_Read_Byte(8); - if ((parity_bit&1)!=AVCLan_Read_Byte(1)) { - STARTEvent; - return 0; - } - if (for_me) AVCLan_Send_ACK(); - else AVCLan_Read_Byte(1); - - if (message_len > MAXMSGLEN) { + if (message_len > MAXMSGLEN) { // RS232_Print_P(PSTR("LAN> Command error")); STARTEvent; return 0; - } + } - for (i=0; i= 900) + break; + } + if (TCB1.CNT > 864) + line_busy = 0; + } while (line_busy); + + // switch to output mode + AVC_OUT_EN(); + + AVCLan_Send_StartBit(); + AVCLan_Send_Byte(0x1, 1); // regular communication + + parity_bit = 0; + AVCLan_Send_Byte(CD_ID_1, 4); // CD Changer ID as master + AVCLan_Send_Byte(CD_ID_2, 8); + AVCLan_Send_ParityBit(); + + AVCLan_Send_Byte(HU_ID_1, 4); // HeadUnit ID as slave + AVCLan_Send_Byte(HU_ID_2, 8); + + AVCLan_Send_ParityBit(); + + if (AVCLan_Read_ACK()) { + AVC_OUT_DIS(); + STARTEvent; + RS232_Print_P(PSTR("Error ACK 1 (Transmission ACK)\n")); return 1; -} + } -byte AVCLan_SendData() -{ - byte i; - - STOPEvent; - - // wait for free line - byte line_busy = 1; - - TCB1.CNT = 0; - do { - while (INPUT_IS_CLEAR) { - if ( TCB1.CNT >= 900 ) break; - } - if ( TCB1.CNT > 864 ) line_busy=0; - } while (line_busy); - - - // switch to output mode - AVC_OUT_EN(); - - AVCLan_Send_StartBit(); - AVCLan_Send_Byte(0x1, 1); // regular communication - - - parity_bit = 0; - AVCLan_Send_Byte(CD_ID_1, 4); // CD Changer ID as master - AVCLan_Send_Byte(CD_ID_2, 8); - AVCLan_Send_ParityBit(); - - AVCLan_Send_Byte(HU_ID_1, 4); // HeadUnit ID as slave - AVCLan_Send_Byte(HU_ID_2, 8); - - AVCLan_Send_ParityBit(); - - if (AVCLan_Read_ACK()) { - AVC_OUT_DIS(); - STARTEvent; - RS232_Print_P(PSTR("Error ACK 1 (Transmission ACK)\n")); - return 1; - } - - - AVCLan_Send_Byte(0xF, 4); // 0xf - control -> COMMAND WRITE - AVCLan_Send_ParityBit(); - if (AVCLan_Read_ACK()) { - AVC_OUT_DIS(); - STARTEvent; - RS232_Print_P(PSTR("Error ACK 2 (COMMMAND WRITE)\n")); - return 2; - } - - AVCLan_Send_Byte(data_len, 8);// data length - AVCLan_Send_ParityBit(); - if (AVCLan_Read_ACK()) { - AVC_OUT_DIS(); - STARTEvent; - RS232_Print_P(PSTR("Error ACK 3 (Data Length)\n")); - return 3; - } - - for (i=0;i COMMAND WRITE + AVCLan_Send_ParityBit(); + if (AVCLan_Read_ACK()) { AVC_OUT_DIS(); - STARTEvent; - if (showLog) ShowOutMessage(); - return 0; -} + RS232_Print_P(PSTR("Error ACK 2 (COMMMAND WRITE)\n")); + return 2; + } -byte AVCLan_SendDataBroadcast() -{ - byte i; + AVCLan_Send_Byte(data_len, 8); // data length + AVCLan_Send_ParityBit(); + if (AVCLan_Read_ACK()) { + AVC_OUT_DIS(); + STARTEvent; + RS232_Print_P(PSTR("Error ACK 3 (Data Length)\n")); + return 3; + } - STOPEvent; - - // wait for free line - byte line_busy = 1; - - TCB1.CNT = 0; - do { - while (INPUT_IS_CLEAR) { - if ( TCB1.CNT >= 900 ) break; - } - if ( TCB1.CNT > 864 ) line_busy=0; - } while (line_busy); - - - AVC_OUT_EN(); - - AVCLan_Send_StartBit(); - AVCLan_Send_Byte(0x0, 1); // broadcast - - parity_bit = 0; - AVCLan_Send_Byte(CD_ID_1, 4); // CD Changer ID as master - AVCLan_Send_Byte(CD_ID_2, 8); - AVCLan_Send_ParityBit(); - - AVCLan_Send_Byte(0x1, 4); // all audio devices - AVCLan_Send_Byte(0xFF, 8); - AVCLan_Send_ParityBit(); - AVCLan_Send_Bit1(); - - AVCLan_Send_Byte(0xF, 4); // 0xf - control -> COMMAND WRITE - AVCLan_Send_ParityBit(); - AVCLan_Send_Bit1(); - - AVCLan_Send_Byte(data_len, 8); // data lenght - AVCLan_Send_ParityBit(); - AVCLan_Send_Bit1(); - - for (i=0;i= 900) + break; + } + if (TCB1.CNT > 864) + line_busy = 0; + } while (line_busy); + + AVC_OUT_EN(); + + AVCLan_Send_StartBit(); + AVCLan_Send_Byte(0x0, 1); // broadcast + + parity_bit = 0; + AVCLan_Send_Byte(CD_ID_1, 4); // CD Changer ID as master + AVCLan_Send_Byte(CD_ID_2, 8); + AVCLan_Send_ParityBit(); + + AVCLan_Send_Byte(0x1, 4); // all audio devices + AVCLan_Send_Byte(0xFF, 8); + AVCLan_Send_ParityBit(); + AVCLan_Send_Bit1(); + + AVCLan_Send_Byte(0xF, 4); // 0xf - control -> COMMAND WRITE + AVCLan_Send_ParityBit(); + AVCLan_Send_Bit1(); + + AVCLan_Send_Byte(data_len, 8); // data lenght + AVCLan_Send_ParityBit(); + AVCLan_Send_Bit1(); + + for (i = 0; i < data_len; i++) { + AVCLan_Send_Byte(data[i], 8); // data byte + AVCLan_Send_ParityBit(); AVCLan_Send_Bit1(); - } + } - AVC_OUT_DIS(); - STARTEvent; - if (showLog) ShowOutMessage(); - return 0; + AVC_OUT_DIS(); + STARTEvent; + if (showLog) + ShowOutMessage(); + return 0; } -byte AVCLan_SendAnswerFrame(byte *cmd) -{ - byte i; - byte *c; - byte b; +byte AVCLan_SendAnswerFrame(byte *cmd) { + byte i; + byte *c; + byte b; - c = cmd; + c = cmd; - b = *c++; - data_control = 0xF; - data_len = *c++; - for (i=0; i ")); - for (i=0; i ")); + for (i = 0; i < data_len; i++) { + RS232_PrintHex8(data[i]); + RS232_SendByte(' '); + } + RS232_Print_P(PSTR("\n")); - AVC_ReleaseLine(); + AVC_ReleaseLine(); } #ifdef SOFTWARE_DEBUG - word temp_b[100]; +word temp_b[100]; - void AVCLan_Measure() - { - STOPEvent; +void AVCLan_Measure() { + STOPEvent; - //word tmp, tmp1, tmp2, bit0, bit1; - byte n = 0; + // word tmp, tmp1, tmp2, bit0, bit1; + byte n = 0; - cbi(TCCR1B,CS12); - TCCR1B = _BV(CS10); - TCNT1 = 0; + cbi(TCCR1B, CS12); + TCCR1B = _BV(CS10); + TCNT1 = 0; - char str[5]; + char str[5]; - while ( n < 100 ) - { - temp_b[n] = TCNT1; - while(INPUT_IS_CLEAR); - temp_b[n+1] = TCNT1; - while(INPUT_IS_SET); - temp_b[n+2] = TCNT1; - while(INPUT_IS_CLEAR); - temp_b[n+3] = TCNT1; - while(INPUT_IS_SET); - temp_b[n+4] = TCNT1; - while(INPUT_IS_CLEAR); - temp_b[n+5] = TCNT1; - while(INPUT_IS_SET); - temp_b[n+6] = TCNT1; - while(INPUT_IS_CLEAR); - temp_b[n+7] = TCNT1; - while(INPUT_IS_SET); - temp_b[n+8] = TCNT1; - while(INPUT_IS_CLEAR); - temp_b[n+9] = TCNT1; - while(INPUT_IS_SET); - // - // while(INPUT_IS_CLEAR); - // - // tmp1 = TCNT1; - // - // while(INPUT_IS_SET); - // - // tmp2 = TCNT1; - // - // bit0 = tmp1-tmp; - // bit1 = tmp2-tmp1; - // - // RS232_Print_P(PSTR("1,")); - // RS232_PrintDec(bit1); - // RS232_Print_P(PSTR("\n")); - // - // RS232_Print_P(PSTR("0,")); - // RS232_PrintDec(bit0); - // RS232_Print_P(PSTR("\n")); - n += 10; - } + while (n < 100) { + temp_b[n] = TCNT1; + while (INPUT_IS_CLEAR) {} + temp_b[n + 1] = TCNT1; + while (INPUT_IS_SET) {} + temp_b[n + 2] = TCNT1; + while (INPUT_IS_CLEAR) {} + temp_b[n + 3] = TCNT1; + while (INPUT_IS_SET) {} + temp_b[n + 4] = TCNT1; + while (INPUT_IS_CLEAR) {} + temp_b[n + 5] = TCNT1; + while (INPUT_IS_SET) {} + temp_b[n + 6] = TCNT1; + while (INPUT_IS_CLEAR) {} + temp_b[n + 7] = TCNT1; + while (INPUT_IS_SET) {} + temp_b[n + 8] = TCNT1; + while (INPUT_IS_CLEAR) {} + temp_b[n + 9] = TCNT1; + while (INPUT_IS_SET) {} + // + // while (INPUT_IS_CLEAR) {} + // + // tmp1 = TCNT1; + // + // while (INPUT_IS_SET) {} + // + // tmp2 = TCNT1; + // + // bit0 = tmp1-tmp; + // bit1 = tmp2-tmp1; + // + // RS232_Print_P(PSTR("1,")); + // RS232_PrintDec(bit1); + // RS232_Print_P(PSTR("\n")); + // + // RS232_Print_P(PSTR("0,")); + // RS232_PrintDec(bit0); + // RS232_Print_P(PSTR("\n")); + n += 10; + } - for(byte i =0; i < 100; i++) - { - itoa(temp_b[i],str); - if(i & 1) { - RS232_Print_P(PSTR("High,")); - } else { - RS232_Print_P(PSTR("Low,")); - } - RS232_Print(str); - RS232_Print_P(PSTR("\n")); - } - RS232_Print_P(PSTR("\nDone.\n")); - - cbi(TCCR1B,CS10); - TCCR1B = _BV(CS12); - - STARTEvent; + for (byte i = 0; i < 100; i++) { + itoa(temp_b[i], str); + if (i & 1) { + RS232_Print_P(PSTR("High,")); + } else { + RS232_Print_P(PSTR("Low,")); } + RS232_Print(str); + RS232_Print_P(PSTR("\n")); + } + RS232_Print_P(PSTR("\nDone.\n")); + + cbi(TCCR1B, CS10); + TCCR1B = _BV(CS12); + + STARTEvent; +} #endif #ifdef HARDWARE_DEBUG - void SetHighLow() { - AVC_OUT_EN(); - sbi(TCCR1B, CS10); - word n = 60000; - TCNT1 = 0; - AVC_SET_LOGICAL_1(); - while ( TCNT1 < n ); - TCNT1 = 0; - AVC_SET_LOGICAL_0(); - while ( TCNT1 < n ); - cbi(TCCR1B, CS10); - AVC_OUT_DIS(); - } +void SetHighLow() { + AVC_OUT_EN(); + sbi(TCCR1B, CS10); + word n = 60000; + TCNT1 = 0; + AVC_SET_LOGICAL_1(); + while (TCNT1 < n) {} + TCNT1 = 0; + AVC_SET_LOGICAL_0(); + while (TCNT1 < n) {} + cbi(TCCR1B, CS10); + AVC_OUT_DIS(); +} #endif diff --git a/avclandrv.h b/avclandrv.h index 255c449..dd34405 100644 --- a/avclandrv.h +++ b/avclandrv.h @@ -27,66 +27,67 @@ */ - #ifndef __AVCLANDRV_H #define __AVCLANDRV_H #include "GlobalDef.h" -#define STOPEvent cbi(RTC.PITINTCTRL, RTC_PI_bp); cbi(USART0.CTRLA, USART_RXCIE_bp); -#define STARTEvent sbi(RTC.PITINTCTRL, RTC_PI_bp); sbi(USART0.CTRLA, USART_RXCIE_bp); +#define STOPEvent \ + cbi(RTC.PITINTCTRL, RTC_PI_bp); \ + cbi(USART0.CTRLA, USART_RXCIE_bp); +#define STARTEvent \ + sbi(RTC.PITINTCTRL, RTC_PI_bp); \ + sbi(USART0.CTRLA, USART_RXCIE_bp); - -#define CHECK_AVC_LINE if (INPUT_IS_SET) AVCLan_Read_Message(); +#define CHECK_AVC_LINE \ + if (INPUT_IS_SET) \ + AVCLan_Read_Message(); void AVC_HoldLine(); void AVC_ReleaseLine(); -#define MAXMSGLEN 32 +#define MAXMSGLEN 32 // Head Unid ID -extern byte HU_ID_1; // 0x01 -extern byte HU_ID_2; // 0x40 - -extern byte CD_ID_1; // 0x03 -extern byte CD_ID_2; // 0x60 +extern byte HU_ID_1; // 0x01 +extern byte HU_ID_2; // 0x40 +extern byte CD_ID_1; // 0x03 +extern byte CD_ID_2; // 0x60 // DVD CHANGER -//#define CD_ID_1 0x02 -//#define CD_ID_2 0x50 +// #define CD_ID_1 0x02 +// #define CD_ID_2 0x50 -#define cmNull 0 -#define cmStatus1 1 -#define cmStatus2 2 -#define cmStatus3 3 -#define cmStatus4 4 +#define cmNull 0 +#define cmStatus1 1 +#define cmStatus2 2 +#define cmStatus3 3 +#define cmStatus4 4 +#define cmRegister 100 +#define cmInit 101 +#define cmCheck 102 +#define cmPlayIt 103 +#define cmBeep 110 -#define cmRegister 100 -#define cmInit 101 -#define cmCheck 102 -#define cmPlayIt 103 -#define cmBeep 110 +#define cmNextTrack 120 +#define cmPrevTrack 121 +#define cmNextDisc 122 +#define cmPrevDisc 123 -#define cmNextTrack 120 -#define cmPrevTrack 121 -#define cmNextDisc 122 -#define cmPrevDisc 123 +#define cmScanModeOn 130 +#define cmScanModeOff 131 -#define cmScanModeOn 130 -#define cmScanModeOff 131 +#define cmPlayReq1 5 +#define cmPlayReq2 6 +#define cmPlayReq3 7 +#define cmStopReq 8 +#define cmStopReq2 9 -#define cmPlayReq1 5 -#define cmPlayReq2 6 -#define cmPlayReq3 7 -#define cmStopReq 8 -#define cmStopReq2 9 - -typedef enum { stStop=0, stPlay=1 } cd_modes; +typedef enum { stStop = 0, stPlay = 1 } cd_modes; extern cd_modes CD_Mode; - extern byte broadcast; extern byte master1; extern byte master2; @@ -104,12 +105,12 @@ void AVCLan_Send_Status(); void AVCLan_Init(); void AVCLan_Register(); -byte AVCLan_SendData(); -byte AVCLan_SendAnswer(); -byte AVCLan_SendDataBroadcast(); -byte AVCLan_Command(byte command); +byte AVCLan_SendData(); +byte AVCLan_SendAnswer(); +byte AVCLan_SendDataBroadcast(); +byte AVCLan_Command(byte command); -byte incBCD(byte data); +byte incBCD(byte data); extern byte check_timeout; @@ -127,10 +128,10 @@ void ShowInMessage(); void ShowOutMessage(); #ifdef SOFTWARE_DEBUG - void AVCLan_Measure(); +void AVCLan_Measure(); #endif #ifdef HARDWARE_DEBUG - void SetHighLow(); +void SetHighLow(); #endif extern byte answerReq; diff --git a/com232.c b/com232.c index 0472d3d..758db56 100644 --- a/com232.c +++ b/com232.c @@ -16,126 +16,123 @@ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ----------------------------------------------------------------------- - this file is a part of the TOYOTA Corolla MP3 Player Project + this file is a part of the TOYOTA Corolla MP3 Player Project ----------------------------------------------------------------------- - http://www.softservice.com.pl/corolla/avc + http://www.softservice.com.pl/corolla/avc May 28 / 2009 - version 2 */ -#include -#include -#include #include "com232.h" +#include +#include +#include byte RS232_RxCharBuffer[25], RS232_RxCharBegin, RS232_RxCharEnd; byte readkey; -void RS232_Init(void) -{ -// // init LED -// sbi(DDRB, 5); -// cbi(PORTB, 5); +void RS232_Init(void) { + // // init LED + // sbi(DDRB, 5); + // cbi(PORTB, 5); - RS232_RxCharBegin = RS232_RxCharEnd = 0; + RS232_RxCharBegin = RS232_RxCharEnd = 0; - USART0.CTRLA = USART_RXCIE_bm; // Enable receive interrupts - USART0.CTRLB = USART_RXEN_bm | USART_TXEN_bm | USART_RXMODE_NORMAL_gc; // Enable Rx/Tx and set receive mode normal - USART0.CTRLC = USART_CMODE_ASYNCHRONOUS_gc | USART_PMODE_DISABLED_gc | USART_CHSIZE_8BIT_gc | USART_SBMODE_1BIT_gc; // Async UART with 8N1 config - USART0.BAUD = 256; // 250k baud rate (64*F_CPU/(16*250k)) for F_CPU = 16MHz + USART0.CTRLA = USART_RXCIE_bm; // Enable receive interrupts + USART0.CTRLB = USART_RXEN_bm | USART_TXEN_bm | + USART_RXMODE_NORMAL_gc; // Enable Rx/Tx and set receive mode + // normal + USART0.CTRLC = USART_CMODE_ASYNCHRONOUS_gc | USART_PMODE_DISABLED_gc | + USART_CHSIZE_8BIT_gc | + USART_SBMODE_1BIT_gc; // Async UART with 8N1 config + USART0.BAUD = 256; // 250k baud rate (64*F_CPU/(16*250k)) for F_CPU = 16MHz } -ISR(USART0_RXC_vect) -{ - RS232_RxCharBuffer[RS232_RxCharEnd] = USART0_RXDATAL; // Store received character to the End of Buffer - RS232_RxCharEnd++; +ISR(USART0_RXC_vect) { + RS232_RxCharBuffer[RS232_RxCharEnd] = USART0_RXDATAL; // Store received + // character to the End + // of Buffer + RS232_RxCharEnd++; } -void RS232_SendByte(byte Data) -{ - loop_until_bit_is_set(USART0_STATUS, USART_DREIF_bp); // wait for UART to become available - USART0_TXDATAL = Data; // send character +void RS232_SendByte(byte Data) { + loop_until_bit_is_set(USART0_STATUS, + USART_DREIF_bp); // wait for UART to become available + USART0_TXDATAL = Data; // send character } -void RS232_Print_P(const char * str_addr) -{ - register byte c; - while ( (c = pgm_read_byte(str_addr++) ) ) - { - if (c == '\n') - RS232_SendByte('\r'); - RS232_SendByte(c); - } +void RS232_Print_P(const char *str_addr) { + register byte c; + while ((c = pgm_read_byte(str_addr++))) { + if (c == '\n') + RS232_SendByte('\r'); + RS232_SendByte(c); + } } -void RS232_Print(const char *pBuf) -{ - register byte c; - while ((c = *pBuf++)) - { - if (c == '\n') - RS232_SendByte('\r'); - RS232_SendByte(c); - } +void RS232_Print(const char *pBuf) { + register byte c; + while ((c = *pBuf++)) { + if (c == '\n') + RS232_SendByte('\r'); + RS232_SendByte(c); + } } -void RS232_PrintHex4(byte Data) -{ - byte Character = Data & 0x0f; - Character += '0'; - if (Character > '9') - Character += 'A' - '0' - 10; - RS232_SendByte(Character); +void RS232_PrintHex4(byte Data) { + byte Character = Data & 0x0f; + Character += '0'; + if (Character > '9') + Character += 'A' - '0' - 10; + RS232_SendByte(Character); } -void RS232_PrintHex8(byte Data) -{ - RS232_PrintHex4(Data >> 4); - RS232_PrintHex4(Data); +void RS232_PrintHex8(byte Data) { + RS232_PrintHex4(Data >> 4); + RS232_PrintHex4(Data); } -void RS232_PrintDec(byte Data) -{ - if (Data>99) { - RS232_SendByte('*'); - return; - } - if (Data<10) { - RS232_SendByte('0'+Data); - return; - } - byte c; - unsigned short v,v1; - v = Data; - v1 = v/10; - c = '0' + (v-v1*10); - RS232_SendByte('0'+v1); - RS232_SendByte(c); +void RS232_PrintDec(byte Data) { + if (Data > 99) { + RS232_SendByte('*'); + return; + } + if (Data < 10) { + RS232_SendByte('0' + Data); + return; + } + byte c; + unsigned short v, v1; + v = Data; + v1 = v / 10; + c = '0' + (v - v1 * 10); + RS232_SendByte('0' + v1); + RS232_SendByte(c); } -void RS232_PrintDec2(byte Data) -{ - if (Data<10) RS232_SendByte('0'); - RS232_PrintDec(Data); +void RS232_PrintDec2(byte Data) { + if (Data < 10) + RS232_SendByte('0'); + RS232_PrintDec(Data); } -char* itoa(int i, char b[]){ - char const digit[] = "0123456789"; - char* p = b; - if(i<0){ - *p++ = '-'; - i *= -1; - } - int shifter = i; - do{ //Move to where representation ends - ++p; - shifter = shifter/10; - }while(shifter); - *p = '\0'; - do{ //Move back, inserting digits as u go - *--p = digit[i%10]; - i = i/10; - }while(i); - return b; +char *itoa(int i, char b[]) { + char const digit[] = "0123456789"; + char *p = b; + if (i < 0) { + *p++ = '-'; + i *= -1; + } + int shifter = i; + do { // Move to where representation ends + ++p; + shifter = shifter / 10; + } while (shifter); + *p = '\0'; + do { // Move back, inserting digits as u go + *--p = digit[i % 10]; + i = i / 10; + } while (i); + return b; } diff --git a/com232.h b/com232.h index 215f3c6..f67aff2 100644 --- a/com232.h +++ b/com232.h @@ -1,49 +1,45 @@ -/* - Copyright (C) 2006 Marcin Slonicki . - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - - ----------------------------------------------------------------------- - this file is a part of the TOYOTA Corolla MP3 Player Project - ----------------------------------------------------------------------- - http://www.softservice.com.pl/corolla/avc - - May 28 / 2009 - version 2 - -*/ - -#ifndef __COM232_H -#define __COM232_H - -#include "GlobalDef.h" - -extern byte RS232_RxCharBuffer[25], RS232_RxCharBegin, RS232_RxCharEnd; -extern byte readkey; - -void RS232_Init(void); -extern void RS232_Print_P(const char * str_addr); -extern void RS232_SendByte(byte Data); -extern void RS232_Print(const char *pBuf); -extern void RS232_PrintHex4(byte Data); -extern void RS232_PrintHex8(byte Data); -extern void RS232_PrintDec(byte Data); -extern void RS232_PrintDec2(byte Data); -extern char* itoa(int i, char b[]); - -// LED -#define LED_ON() cbi(PORTB, 5) -#define LED_OFF() sbi(PORTB, 5) - -#endif // __COM232_H +/* + Copyright (C) 2006 Marcin Slonicki . + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + ----------------------------------------------------------------------- + this file is a part of the TOYOTA Corolla MP3 Player Project + ----------------------------------------------------------------------- + http://www.softservice.com.pl/corolla/avc + + May 28 / 2009 - version 2 + +*/ + +#ifndef __COM232_H +#define __COM232_H + +#include "GlobalDef.h" + +extern byte RS232_RxCharBuffer[25], RS232_RxCharBegin, RS232_RxCharEnd; +extern byte readkey; + +void RS232_Init(void); +extern void RS232_Print_P(const char *str_addr); +extern void RS232_SendByte(byte Data); +extern void RS232_Print(const char *pBuf); +extern void RS232_PrintHex4(byte Data); +extern void RS232_PrintHex8(byte Data); +extern void RS232_PrintDec(byte Data); +extern void RS232_PrintDec2(byte Data); +extern char *itoa(int i, char b[]); + +#endif // __COM232_H diff --git a/sniffer.c b/sniffer.c index 27b9fbd..f8c0b5f 100644 --- a/sniffer.c +++ b/sniffer.c @@ -24,13 +24,13 @@ */ -#include #include +#include #include #include "GlobalDef.h" -#include "com232.h" #include "avclandrv.h" +#include "com232.h" void Setup(); @@ -38,198 +38,208 @@ byte rcv_command[5]; byte rcv_pos = 0; byte rcv_time_clr = 0; -int main() -{ - byte readSeq = 0; - byte s_len = 0; - byte s_dig = 0; - byte s_c[2]; - byte i; - byte data_tmp[32]; +int main() { + byte readSeq = 0; + byte s_len = 0; + byte s_dig = 0; + byte s_c[2]; + byte i; + byte data_tmp[32]; - Setup(); + Setup(); - RS232_Print("AVCLan reader 1.00\nReady\n\n"); -// LED_OFF(); - RS232_Print_P(PSTR("\nS - read sequence\nW - send command\nQ - send broadcast\nL/l - log on/off\nK/k - seq. echo on/off\n")); - RS232_Print_P(PSTR("R/r - register device\nB - Beep\n")); + RS232_Print("AVCLan reader 1.00\nReady\n\n"); + // LED_OFF(); + RS232_Print_P(PSTR("\nS - read sequence\nW - send command\nQ - send " + "broadcast\nL/l - log on/off\nK/k - seq. echo on/off\n")); + RS232_Print_P(PSTR("R/r - register device\nB - Beep\n")); #ifdef HARDWARE_DEBUG - RS232_Print_P(PSTR("1 - Hold High/low\nE - Print line status\n")); + RS232_Print_P(PSTR("1 - Hold High/low\nE - Print line status\n")); #endif #ifdef SOFTWARE_DEBUG - RS232_Print_P(PSTR("M - Measure high and low lengths\n")); + RS232_Print_P(PSTR("M - Measure high and low lengths\n")); #endif - while (1) { + while (1) { - if (INPUT_IS_SET) { // if message from some device on AVCLan begin - // LED_ON(); - AVCLan_Read_Message(); - // show message + if (INPUT_IS_SET) { // if message from some device on AVCLan begin + // LED_ON(); + AVCLan_Read_Message(); + // show message } else { - // LED_OFF(); - // check command from HU - if (answerReq != 0) AVCLan_SendAnswer(); + // LED_OFF(); + // check command from HU + if (answerReq != 0) + AVCLan_SendAnswer(); } // HandleEvent switch (Event) { - case EV_STATUS: Event &= ~EV_STATUS; - AVCLan_Send_Status(); - break; + case EV_STATUS: + Event &= ~EV_STATUS; + AVCLan_Send_Status(); + break; } - // Key handler if (RS232_RxCharEnd) { - cbi(USART0.CTRLA, USART_RXCIE_bp); // disable RX complete interrupt - readkey = RS232_RxCharBuffer[RS232_RxCharBegin]; // read begin of received Buffer - RS232_RxCharBegin++; - if (RS232_RxCharBegin == RS232_RxCharEnd) // if Buffer is empty - RS232_RxCharBegin = RS232_RxCharEnd = 0; // reset Buffer - sbi(USART0.CTRLA, USART_RXCIE_bp); // enable RX complete interrupt - switch (readkey) { - case 'S': showLog = 0; - RS232_Print_P(PSTR("READ SEQUENCE > \n")); - readSeq = 1; - s_len=0; - s_dig=0; - s_c[0]=s_c[1]=0; - break; - case 'W' : showLog = 1; - readSeq=0; - AVCLan_SendMyData(data_tmp, s_len); - break; - case 'Q' : showLog = 1; - readSeq=0; - AVCLan_SendMyDataBroadcast(data_tmp, s_len); - break; - case 'R': RS232_Print_P(PSTR("REGIST:\n")); - AVCLan_Command( cmRegister ); - TCB1.CNT = 0; - while( TCB1.CNT < 540 ); - CHECK_AVC_LINE; - break; - case 'r': AVCLan_Register(); - break; - case 'l': RS232_Print_P(PSTR("Log OFF\n")); - showLog = 0; - break; - case 'L': RS232_Print_P(PSTR("Log ON\n")); - showLog = 1; - break; - case 'k': RS232_Print_P(PSTR("str OFF\n")); - showLog2 = 0; - break; - case 'K': RS232_Print_P(PSTR("str ON\n")); - showLog2 = 1; - break; - case 'B': - data_tmp[0] = 0x00; - data_tmp[1] = 0x5E; - data_tmp[2] = 0x29; - data_tmp[3] = 0x60; - data_tmp[4] = 0x01; - s_len = 5; - AVCLan_SendMyData(data_tmp, s_len); - break; + cbi(USART0.CTRLA, USART_RXCIE_bp); // disable RX complete interrupt + readkey = RS232_RxCharBuffer[RS232_RxCharBegin]; // read begin of received + // Buffer + RS232_RxCharBegin++; + if (RS232_RxCharBegin == RS232_RxCharEnd) // if Buffer is empty + RS232_RxCharBegin = RS232_RxCharEnd = 0; // reset Buffer + sbi(USART0.CTRLA, USART_RXCIE_bp); // enable RX complete interrupt + switch (readkey) { + case 'S': + showLog = 0; + RS232_Print_P(PSTR("READ SEQUENCE > \n")); + readSeq = 1; + s_len = 0; + s_dig = 0; + s_c[0] = s_c[1] = 0; + break; + case 'W': + showLog = 1; + readSeq = 0; + AVCLan_SendMyData(data_tmp, s_len); + break; + case 'Q': + showLog = 1; + readSeq = 0; + AVCLan_SendMyDataBroadcast(data_tmp, s_len); + break; + case 'R': + RS232_Print_P(PSTR("REGIST:\n")); + AVCLan_Command(cmRegister); + TCB1.CNT = 0; + while (TCB1.CNT < 540) {} + CHECK_AVC_LINE; + break; + case 'r': + AVCLan_Register(); + break; + case 'l': + RS232_Print_P(PSTR("Log OFF\n")); + showLog = 0; + break; + case 'L': + RS232_Print_P(PSTR("Log ON\n")); + showLog = 1; + break; + case 'k': + RS232_Print_P(PSTR("str OFF\n")); + showLog2 = 0; + break; + case 'K': + RS232_Print_P(PSTR("str ON\n")); + showLog2 = 1; + break; + case 'B': + data_tmp[0] = 0x00; + data_tmp[1] = 0x5E; + data_tmp[2] = 0x29; + data_tmp[3] = 0x60; + data_tmp[4] = 0x01; + s_len = 5; + AVCLan_SendMyData(data_tmp, s_len); + break; #ifdef HARDWARE_DEBUG - case '1': - SetHighLow(); - break; - case 'E': - if(INPUT_IS_SET) { - RS232_Print_P(PSTR("Set/High/1\n")); - } else if (INPUT_IS_CLEAR) { - RS232_Print_P(PSTR("Unset/Low/0\n")); - } else { - RS232_Print_P(PSTR("WTF?\n")); - } - break; + case '1': + SetHighLow(); + break; + case 'E': + if (INPUT_IS_SET) { + RS232_Print_P(PSTR("Set/High/1\n")); + } else if (INPUT_IS_CLEAR) { + RS232_Print_P(PSTR("Unset/Low/0\n")); + } else { + RS232_Print_P(PSTR("WTF?\n")); + } + break; #endif #ifdef SOFTWARE_DEBUG - case 'M': - AVCLan_Measure(); - break; + case 'M': + AVCLan_Measure(); + break; #endif - default : - if (readSeq==1) { - s_c[s_dig]=readkey; + default: + if (readSeq == 1) { + s_c[s_dig] = readkey; - s_dig++; - if (s_dig==2) { - if (s_c[0]<':') s_c[0] -= 48; - else s_c[0] -= 55; - data_tmp[s_len] = 16 * s_c[0]; - if (s_c[1]<':') s_c[1] -= 48; - else s_c[1] -= 55; - data_tmp[s_len] += s_c[1]; - s_len++; - s_dig=0; - s_c[0]=s_c[1]=0; - } - if (showLog2) { - RS232_Print_P(PSTR("CURRENT SEQUENCE > ")); - for (i=0; i ")); + for (i = 0; i < s_len; i++) { + RS232_PrintHex8(data_tmp[i]); + RS232_SendByte(' '); + } + RS232_Print_P(PSTR("\n")); + } + } + } // switch (readkey) + } // if (RS232_RxCharEnd) + } + return 0; } -void Setup() -{ - CD_ID_1 = 0x03; - CD_ID_2 = 0x60; +void Setup() { + CD_ID_1 = 0x03; + CD_ID_2 = 0x60; - HU_ID_1 = 0x01; - HU_ID_2 = 0x90; + HU_ID_1 = 0x01; + HU_ID_2 = 0x90; - showLog = 1; - showLog2 = 1; + showLog = 1; + showLog2 = 1; -// Default is zero; resetting/zeroing unnecessary -// MCUCR = 0; + // Default is zero; resetting/zeroing unnecessary + // MCUCR = 0; - loop_until_bit_is_clear(RTC_STATUS, RTC_CTRLABUSY_bp); - RTC.CTRLA = RTC_PRESCALER_DIV1_gc; - RTC.CLKSEL = RTC_CLKSEL_INT32K_gc; - RTC.PITINTCTRL = RTC_PI_bm; - loop_until_bit_is_clear(RTC_PITSTATUS, RTC_CTRLBUSY_bp); - RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc | RTC_PITEN_bm; + loop_until_bit_is_clear(RTC_STATUS, RTC_CTRLABUSY_bp); + RTC.CTRLA = RTC_PRESCALER_DIV1_gc; + RTC.CLKSEL = RTC_CLKSEL_INT32K_gc; + RTC.PITINTCTRL = RTC_PI_bm; + loop_until_bit_is_clear(RTC_PITSTATUS, RTC_CTRLBUSY_bp); + RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc | RTC_PITEN_bm; + RS232_Init(); - RS232_Init(); + AVCLan_Init(); - - AVCLan_Init(); - - Event = EV_NOTHING; - sei(); + Event = EV_NOTHING; + sei(); } // Periodic interrupt with a 1 sec period -ISR(RTC_PIT_vect) -{ - if (CD_Mode==stPlay) - { - cd_Time_Sec=incBCD(cd_Time_Sec); - if (cd_Time_Sec==0x60) { - cd_Time_Sec = 0; - cd_Time_Min=incBCD(cd_Time_Min); - if (cd_Time_Min==0xA0) { - cd_Time_Min=0x0; - } - } +ISR(RTC_PIT_vect) { + if (CD_Mode == stPlay) { + cd_Time_Sec = incBCD(cd_Time_Sec); + if (cd_Time_Sec == 0x60) { + cd_Time_Sec = 0; + cd_Time_Min = incBCD(cd_Time_Min); + if (cd_Time_Min == 0xA0) { + cd_Time_Min = 0x0; + } } - Event |= EV_STATUS; - RTC.PITINTFLAGS |= RTC_PI_bm; + } + Event |= EV_STATUS; + RTC.PITINTFLAGS |= RTC_PI_bm; }