1
0
mirror of https://github.com/halleysfifthinc/Toyota-AVC-LAN synced 2025-07-26 11:16:47 +00:00

Reformat using clang format

This commit is contained in:
Allen Hill 2023-08-18 21:27:32 -04:00
parent e8d7f5b297
commit 1da1a52c68
8 changed files with 1193 additions and 1127 deletions

6
.clang-format Normal file
View File

@ -0,0 +1,6 @@
---
BasedOnStyle: LLVM
UseCRLF: false
BreakBeforeBraces: Attach
AllowShortBlocksOnASingleLine: Empty
...

View File

@ -26,7 +26,6 @@
#include "GlobalDef.h"
// max 10 events in fifo
byte EventCount;
byte EventCmd[10];

View File

@ -27,22 +27,22 @@
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#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;
@ -110,7 +109,6 @@ 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};
@ -124,20 +122,20 @@ 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_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_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 };
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};
void AVC_HoldLine()
{
void AVC_HoldLine() {
STOPEvent;
// wait for free line
@ -146,12 +144,14 @@ void AVC_HoldLine()
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;
/* 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;
if (TCB1.CNT > 864)
line_busy = 0;
} while (line_busy);
// switch to out mode
@ -161,21 +161,19 @@ void AVC_HoldLine()
STARTEvent;
}
void AVC_ReleaseLine()
{
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
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
// Analog comparator
AC2.CTRLA = AC_OUTEN_bm | AC_HYSMODE_25mV_gc | AC_ENABLE_bm;
@ -197,23 +195,22 @@ void AVCLan_Init()
randomMode = 0;
playMode = 0;
CD_Mode = stStop;
}
byte AVCLan_Read_Byte(byte length)
{
byte AVCLan_Read_Byte(byte length) {
byte bite = 0;
while (1) {
while (INPUT_IS_CLEAR);
while (INPUT_IS_CLEAR) {}
TCB1.CNT = 0;
while (INPUT_IS_SET); // If input was set for less than 26 us
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;
if (!length)
return bite;
bite = bite << 1;
}
}
@ -230,49 +227,48 @@ void set_AVC_logic_for(uint8_t val, uint16_t period) {
TCB1_INTFLAGS = 1;
}
byte AVCLan_Send_StartBit()
{
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()
{
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()
{
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()
{
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
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);
while (INPUT_IS_SET) {}
AVC_OUT_EN(); // back to write mode
return 0;
}
byte AVCLan_Send_ACK()
{
byte AVCLan_Send_ACK() {
TCB1.CNT = 0;
while (INPUT_IS_CLEAR) {
if (TCB1.CNT >= 900) return 0; // max wait time
if (TCB1.CNT >= 900)
return 0; // max wait time
}
AVC_OUT_EN();
@ -285,8 +281,7 @@ byte AVCLan_Send_ACK()
return 1;
}
byte AVCLan_Send_Byte(byte bite, byte len)
{
byte AVCLan_Send_Byte(byte bite, byte len) {
byte b;
if (len == 8) {
b = bite;
@ -308,11 +303,9 @@ byte AVCLan_Send_Byte(byte bite, byte len)
}
b = b << 1;
}
}
byte AVCLan_Send_ParityBit()
{
byte AVCLan_Send_ParityBit() {
if ((parity_bit & 1) != 0) {
AVCLan_Send_Bit1();
// parity_bit++;
@ -323,8 +316,7 @@ byte AVCLan_Send_ParityBit()
return 1;
}
byte CheckCmd(byte *cmd)
{
byte CheckCmd(byte *cmd) {
byte i;
byte *c;
byte l;
@ -333,14 +325,14 @@ byte CheckCmd(byte *cmd)
l = *c++;
for (i = 0; i < l; i++) {
if (message[i] != *c) return 0;
if (message[i] != *c)
return 0;
c++;
}
return 1;
}
byte AVCLan_Read_Message()
{
byte AVCLan_Read_Message() {
STOPEvent; // disable timer1 interrupt
byte i;
@ -391,8 +383,10 @@ byte AVCLan_Read_Message()
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
@ -400,8 +394,10 @@ byte AVCLan_Read_Message()
STARTEvent;
return 0;
}
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;
message_len = AVCLan_Read_Byte(8);
@ -409,8 +405,10 @@ byte AVCLan_Read_Message()
STARTEvent;
return 0;
}
if (for_me) AVCLan_Send_ACK();
else AVCLan_Read_Byte(1);
if (for_me)
AVCLan_Send_ACK();
else
AVCLan_Read_Byte(1);
if (message_len > MAXMSGLEN) {
// RS232_Print_P(PSTR("LAN> Command error"));
@ -432,45 +430,81 @@ byte AVCLan_Read_Message()
}
}
STARTEvent;
if (showLog) ShowInMessage();
if (showLog)
ShowInMessage();
if (for_me) {
if (CheckCmd((byte*)stat1)) { answerReq = cmStatus1; return 1; }
if (CheckCmd((byte*)stat2)) { answerReq = cmStatus2; return 1; }
if (CheckCmd((byte*)stat3)) { answerReq = cmStatus3; return 1; }
if (CheckCmd((byte*)stat4)) { answerReq = cmStatus4; return 1; }
if (CheckCmd((byte *)stat1)) {
answerReq = cmStatus1;
return 1;
}
if (CheckCmd((byte *)stat2)) {
answerReq = cmStatus2;
return 1;
}
if (CheckCmd((byte *)stat3)) {
answerReq = cmStatus3;
return 1;
}
if (CheckCmd((byte *)stat4)) {
answerReq = cmStatus4;
return 1;
}
// if (CheckCmd((byte*)stat5)) { answerReq = cmStatus5; return 1; }
if (CheckCmd((byte*)play_req1)) { answerReq = cmPlayReq1; return 1; }
if (CheckCmd((byte*)play_req2)) { answerReq = cmPlayReq2; return 1; }
if (CheckCmd((byte*)play_req3)) { answerReq = cmPlayReq3; return 1; }
if (CheckCmd((byte*)stop_req)) { answerReq = cmStopReq; return 1; }
if (CheckCmd((byte*)stop_req2)) { answerReq = cmStopReq2; return 1; }
if (CheckCmd((byte *)play_req1)) {
answerReq = cmPlayReq1;
return 1;
}
if (CheckCmd((byte *)play_req2)) {
answerReq = cmPlayReq2;
return 1;
}
if (CheckCmd((byte *)play_req3)) {
answerReq = cmPlayReq3;
return 1;
}
if (CheckCmd((byte *)stop_req)) {
answerReq = cmStopReq;
return 1;
}
if (CheckCmd((byte *)stop_req2)) {
answerReq = cmStopReq2;
return 1;
}
} else { // broadcast check
if (CheckCmd((byte*)lan_playit)) { answerReq = cmPlayIt; return 1; }
if (CheckCmd((byte *)lan_playit)) {
answerReq = cmPlayIt;
return 1;
}
if (CheckCmd((byte *)lan_check)) {
answerReq = cmCheck;
CMD_CHECK[6] = message[3];
return 1;
}
if (CheckCmd((byte*)lan_reg)) { answerReq = cmRegister; return 1; }
if (CheckCmd((byte*)lan_init)) { answerReq = cmInit; return 1; }
if (CheckCmd((byte*)lan_stat1)) { answerReq = cmStatus1; return 1; }
if (CheckCmd((byte *)lan_reg)) {
answerReq = cmRegister;
return 1;
}
if (CheckCmd((byte *)lan_init)) {
answerReq = cmInit;
return 1;
}
if (CheckCmd((byte *)lan_stat1)) {
answerReq = cmStatus1;
return 1;
}
}
answerReq = cmNull;
return 1;
}
byte AVCLan_SendData()
{
byte AVCLan_SendData() {
byte i;
STOPEvent;
@ -481,19 +515,19 @@ byte AVCLan_SendData()
TCB1.CNT = 0;
do {
while (INPUT_IS_CLEAR) {
if ( TCB1.CNT >= 900 ) break;
if (TCB1.CNT >= 900)
break;
}
if ( TCB1.CNT > 864 ) line_busy=0;
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);
@ -511,7 +545,6 @@ byte AVCLan_SendData()
return 1;
}
AVCLan_Send_Byte(0xF, 4); // 0xf - control -> COMMAND WRITE
AVCLan_Send_ParityBit();
if (AVCLan_Read_ACK()) {
@ -547,12 +580,12 @@ byte AVCLan_SendData()
AVC_OUT_DIS();
STARTEvent;
if (showLog) ShowOutMessage();
if (showLog)
ShowOutMessage();
return 0;
}
byte AVCLan_SendDataBroadcast()
{
byte AVCLan_SendDataBroadcast() {
byte i;
STOPEvent;
@ -563,12 +596,13 @@ byte AVCLan_SendDataBroadcast()
TCB1.CNT = 0;
do {
while (INPUT_IS_CLEAR) {
if ( TCB1.CNT >= 900 ) break;
if (TCB1.CNT >= 900)
break;
}
if ( TCB1.CNT > 864 ) line_busy=0;
if (TCB1.CNT > 864)
line_busy = 0;
} while (line_busy);
AVC_OUT_EN();
AVCLan_Send_StartBit();
@ -600,12 +634,12 @@ byte AVCLan_SendDataBroadcast()
AVC_OUT_DIS();
STARTEvent;
if (showLog) ShowOutMessage();
if (showLog)
ShowOutMessage();
return 0;
}
byte AVCLan_SendAnswerFrame(byte *cmd)
{
byte AVCLan_SendAnswerFrame(byte *cmd) {
byte i;
byte *c;
byte b;
@ -624,8 +658,7 @@ byte AVCLan_SendAnswerFrame(byte *cmd)
return AVCLan_SendDataBroadcast();
}
byte AVCLan_SendMyData(byte *data_tmp, byte s_len)
{
byte AVCLan_SendMyData(byte *data_tmp, byte s_len) {
byte i;
byte *c;
@ -639,12 +672,10 @@ byte AVCLan_SendMyData(byte *data_tmp, byte s_len)
return AVCLan_SendData();
}
byte AVCLan_SendMyDataBroadcast(byte *data_tmp, byte s_len)
{
byte AVCLan_SendMyDataBroadcast(byte *data_tmp, byte s_len) {
byte i;
byte *c;
c = data_tmp;
data_control = 0xF;
@ -655,41 +686,61 @@ byte AVCLan_SendMyDataBroadcast(byte *data_tmp, byte s_len)
return AVCLan_SendDataBroadcast();
}
byte AVCLan_SendInitCommands()
{
byte AVCLan_SendInitCommands() {
byte r;
const byte c1[] = { 0x0, 0x0B, 0x63, 0x31, 0xF1, 0x00, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x80 };
const byte c2[] = { 0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x02 };
const byte c3[] = { 0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00, 0x3F, 0x00, 0x01, 0x00, 0x01, 0x02 };
const byte c4[] = { 0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00, 0x3D, 0x00, 0x01, 0x00, 0x01, 0x02 };
const byte c5[] = { 0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00, 0x39, 0x00, 0x01, 0x00, 0x01, 0x02 };
const byte c6[] = { 0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00, 0x31, 0x00, 0x01, 0x00, 0x01, 0x02 };
const byte c7[] = { 0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00, 0x21, 0x00, 0x01, 0x00, 0x01, 0x02 };
const byte c8[] = { 0x0, 0x0B, 0x63, 0x31, 0xF1, 0x00, 0x90, 0x01, 0xFF, 0xFF, 0xFF, 0x00, 0x80 };
const byte c9[] = { 0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x02 };
const byte cA[] = { 0x0, 0x0B, 0x63, 0x31, 0xF1, 0x00, 0x30, 0x01, 0xFF, 0xFF, 0xFF, 0x00, 0x80 };
const byte c1[] = {0x0, 0x0B, 0x63, 0x31, 0xF1, 0x00, 0x80,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x80};
const byte c2[] = {0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00,
0x3F, 0x00, 0x00, 0x00, 0x00, 0x02};
const byte c3[] = {0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00,
0x3F, 0x00, 0x01, 0x00, 0x01, 0x02};
const byte c4[] = {0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00,
0x3D, 0x00, 0x01, 0x00, 0x01, 0x02};
const byte c5[] = {0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00,
0x39, 0x00, 0x01, 0x00, 0x01, 0x02};
const byte c6[] = {0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00,
0x31, 0x00, 0x01, 0x00, 0x01, 0x02};
const byte c7[] = {0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00,
0x21, 0x00, 0x01, 0x00, 0x01, 0x02};
const byte c8[] = {0x0, 0x0B, 0x63, 0x31, 0xF1, 0x00, 0x90,
0x01, 0xFF, 0xFF, 0xFF, 0x00, 0x80};
const byte c9[] = {0x0, 0x0A, 0x63, 0x31, 0xF3, 0x00,
0x01, 0x00, 0x01, 0x00, 0x01, 0x02};
const byte cA[] = {0x0, 0x0B, 0x63, 0x31, 0xF1, 0x00, 0x30,
0x01, 0xFF, 0xFF, 0xFF, 0x00, 0x80};
r = AVCLan_SendAnswerFrame((byte *)c1);
if (!r) r = AVCLan_SendAnswerFrame((byte*)c2);
if (!r) r = AVCLan_SendAnswerFrame((byte*)c3);
if (!r) r = AVCLan_SendAnswerFrame((byte*)c4);
if (!r) r = AVCLan_SendAnswerFrame((byte*)c5);
if (!r) r = AVCLan_SendAnswerFrame((byte*)c6);
if (!r) r = AVCLan_SendAnswerFrame((byte*)c7);
if (!r) r = AVCLan_SendAnswerFrame((byte*)c8);
if (!r) r = AVCLan_SendAnswerFrame((byte*)c9);
if (!r) r = AVCLan_SendAnswerFrame((byte*)cA);
if (!r)
r = AVCLan_SendAnswerFrame((byte *)c2);
if (!r)
r = AVCLan_SendAnswerFrame((byte *)c3);
if (!r)
r = AVCLan_SendAnswerFrame((byte *)c4);
if (!r)
r = AVCLan_SendAnswerFrame((byte *)c5);
if (!r)
r = AVCLan_SendAnswerFrame((byte *)c6);
if (!r)
r = AVCLan_SendAnswerFrame((byte *)c7);
if (!r)
r = AVCLan_SendAnswerFrame((byte *)c8);
if (!r)
r = AVCLan_SendAnswerFrame((byte *)c9);
if (!r)
r = AVCLan_SendAnswerFrame((byte *)cA);
//const byte c1[] = { 0x0, 0x0B, 0x63, 0x31, 0xF1, 0x00, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x80 };
//r = AVCLan_SendAnswerFrame((byte*)c1);
// const byte c1[] = { 0x0, 0x0B, 0x63, 0x31, 0xF1, 0x00, 0x80,
// 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x80 }; r =
// AVCLan_SendAnswerFrame((byte*)c1);
return r;
}
void AVCLan_Send_Status()
{
// disc track t_min t_sec
byte STATUS[] = {0x0, 0x0B, 0x63, 0x31, 0xF1, 0x01, 0x10, 0x01, 0x01, 0x00, 0x00, 0x00, 0x80 };
void AVCLan_Send_Status() {
// disc track t_min
// t_sec
byte STATUS[] = {0x0, 0x0B, 0x63, 0x31, 0xF1, 0x01, 0x10,
0x01, 0x01, 0x00, 0x00, 0x00, 0x80};
STATUS[7] = cd_Disc;
STATUS[8] = cd_Track;
@ -701,50 +752,61 @@ void AVCLan_Send_Status()
AVCLan_SendAnswerFrame((byte *)STATUS);
}
byte AVCLan_SendAnswer()
{
byte AVCLan_SendAnswer() {
byte r = 0;
switch (answerReq) {
case cmStatus1: r = AVCLan_SendAnswerFrame((byte*)CMD_STATUS1);
case cmStatus1:
r = AVCLan_SendAnswerFrame((byte *)CMD_STATUS1);
break;
case cmStatus2: r = AVCLan_SendAnswerFrame((byte*)CMD_STATUS2);
case cmStatus2:
r = AVCLan_SendAnswerFrame((byte *)CMD_STATUS2);
break;
case cmStatus3: r = AVCLan_SendAnswerFrame((byte*)CMD_STATUS3);
case cmStatus3:
r = AVCLan_SendAnswerFrame((byte *)CMD_STATUS3);
break;
case cmStatus4: r = AVCLan_SendAnswerFrame((byte*)CMD_STATUS4);
case cmStatus4:
r = AVCLan_SendAnswerFrame((byte *)CMD_STATUS4);
break;
case cmRegister: r = AVCLan_SendAnswerFrame((byte*)CMD_REGISTER);
case cmRegister:
r = AVCLan_SendAnswerFrame((byte *)CMD_REGISTER);
break;
case cmInit: // RS232_Print_P(PSTR("INIT\n"));
r = AVCLan_SendInitCommands();
break;
case cmCheck: r = AVCLan_SendAnswerFrame((byte*)CMD_CHECK);
case cmCheck:
r = AVCLan_SendAnswerFrame((byte *)CMD_CHECK);
check_timeout = 0;
CMD_CHECK[6]++;
RS232_Print_P(PSTR("AVCCHK\n"));
break;
case cmPlayReq1: playMode = 0;
case cmPlayReq1:
playMode = 0;
r = AVCLan_SendAnswerFrame((byte *)CMD_PLAY_OK1);
break;
case cmPlayReq2:
case cmPlayReq3: playMode = 0;
case cmPlayReq3:
playMode = 0;
r = AVCLan_SendAnswerFrame((byte *)CMD_PLAY_OK2);
if (!r) r = AVCLan_SendAnswerFrame((byte*)CMD_PLAY_OK3);
if (!r)
r = AVCLan_SendAnswerFrame((byte *)CMD_PLAY_OK3);
CD_Mode = stPlay;
break;
case cmPlayIt: playMode = 1;
case cmPlayIt:
playMode = 1;
RS232_Print_P(PSTR("PLAY\n"));
CMD_PLAY_OK4[7] = cd_Disc;
CMD_PLAY_OK4[8] = cd_Track;
CMD_PLAY_OK4[9] = cd_Time_Min;
CMD_PLAY_OK4[10] = cd_Time_Sec;
r = AVCLan_SendAnswerFrame((byte *)CMD_PLAY_OK4);
if (!r) AVCLan_Send_Status();
if (!r)
AVCLan_Send_Status();
CD_Mode = stPlay;
break;
case cmStopReq:
case cmStopReq2: CD_Mode = stStop;
case cmStopReq2:
CD_Mode = stStop;
playMode = 0;
r = AVCLan_SendAnswerFrame((byte *)CMD_STOP1);
@ -754,7 +816,8 @@ byte AVCLan_SendAnswer()
CMD_STOP2[10] = cd_Time_Sec;
r = AVCLan_SendAnswerFrame((byte *)CMD_STOP2);
break;
case cmBeep: AVCLan_SendAnswerFrame((byte*)CMD_BEEP);
case cmBeep:
AVCLan_SendAnswerFrame((byte *)CMD_BEEP);
break;
}
@ -762,8 +825,7 @@ byte AVCLan_SendAnswer()
return r;
}
void AVCLan_Register()
{
void AVCLan_Register() {
RS232_Print_P(PSTR("REG_ST\n"));
AVCLan_SendAnswerFrame((byte *)CMD_REGISTER);
RS232_Print_P(PSTR("REG_END\n"));
@ -771,8 +833,7 @@ void AVCLan_Register()
AVCLan_Command(cmInit);
}
byte AVCLan_Command(byte command)
{
byte AVCLan_Command(byte command) {
byte r;
answerReq = command;
@ -787,25 +848,25 @@ byte AVCLan_Command(byte command)
/* Increment packed 2-digit BCD number.
WARNING: Overflow behavior is incorrect (e.g. `incBCD(0x99) != 0x00`) */
byte incBCD(byte data)
{
byte incBCD(byte data) {
if ((data & 0x9) == 0x9)
return (data + 7);
return (data + 1);
}
void ShowInMessage()
{
if (message_len==0) return;
void ShowInMessage() {
if (message_len == 0)
return;
AVC_HoldLine();
RS232_Print_P(PSTR("HU < ("));
if (broadcast==0) RS232_Print_P(PSTR("bro) "));
else RS232_Print_P(PSTR("dir) "));
if (broadcast == 0)
RS232_Print_P(PSTR("bro) "));
else
RS232_Print_P(PSTR("dir) "));
RS232_PrintHex4(master1);
RS232_PrintHex8(master2);
@ -824,8 +885,7 @@ void ShowInMessage()
AVC_ReleaseLine();
}
void ShowOutMessage()
{
void ShowOutMessage() {
byte i;
AVC_HoldLine();
@ -843,8 +903,7 @@ void ShowOutMessage()
#ifdef SOFTWARE_DEBUG
word temp_b[100];
void AVCLan_Measure()
{
void AVCLan_Measure() {
STOPEvent;
// word tmp, tmp1, tmp2, bit0, bit1;
@ -856,34 +915,33 @@ void ShowOutMessage()
char str[5];
while ( n < 100 )
{
while (n < 100) {
temp_b[n] = TCNT1;
while(INPUT_IS_CLEAR);
while (INPUT_IS_CLEAR) {}
temp_b[n + 1] = TCNT1;
while(INPUT_IS_SET);
while (INPUT_IS_SET) {}
temp_b[n + 2] = TCNT1;
while(INPUT_IS_CLEAR);
while (INPUT_IS_CLEAR) {}
temp_b[n + 3] = TCNT1;
while(INPUT_IS_SET);
while (INPUT_IS_SET) {}
temp_b[n + 4] = TCNT1;
while(INPUT_IS_CLEAR);
while (INPUT_IS_CLEAR) {}
temp_b[n + 5] = TCNT1;
while(INPUT_IS_SET);
while (INPUT_IS_SET) {}
temp_b[n + 6] = TCNT1;
while(INPUT_IS_CLEAR);
while (INPUT_IS_CLEAR) {}
temp_b[n + 7] = TCNT1;
while(INPUT_IS_SET);
while (INPUT_IS_SET) {}
temp_b[n + 8] = TCNT1;
while(INPUT_IS_CLEAR);
while (INPUT_IS_CLEAR) {}
temp_b[n + 9] = TCNT1;
while(INPUT_IS_SET);
while (INPUT_IS_SET) {}
//
// while(INPUT_IS_CLEAR);
// while (INPUT_IS_CLEAR) {}
//
// tmp1 = TCNT1;
//
// while(INPUT_IS_SET);
// while (INPUT_IS_SET) {}
//
// tmp2 = TCNT1;
//
@ -900,8 +958,7 @@ void ShowOutMessage()
n += 10;
}
for(byte i =0; i < 100; i++)
{
for (byte i = 0; i < 100; i++) {
itoa(temp_b[i], str);
if (i & 1) {
RS232_Print_P(PSTR("High,"));
@ -927,10 +984,10 @@ void ShowOutMessage()
word n = 60000;
TCNT1 = 0;
AVC_SET_LOGICAL_1();
while ( TCNT1 < n );
while (TCNT1 < n) {}
TCNT1 = 0;
AVC_SET_LOGICAL_0();
while ( TCNT1 < n );
while (TCNT1 < n) {}
cbi(TCCR1B, CS10);
AVC_OUT_DIS();
}

View File

@ -27,17 +27,21 @@
*/
#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();
@ -51,7 +55,6 @@ 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
@ -62,7 +65,6 @@ extern byte CD_ID_2; // 0x60
#define cmStatus3 3
#define cmStatus4 4
#define cmRegister 100
#define cmInit 101
#define cmCheck 102
@ -86,7 +88,6 @@ extern byte CD_ID_2; // 0x60
typedef enum { stStop = 0, stPlay = 1 } cd_modes;
extern cd_modes CD_Mode;
extern byte broadcast;
extern byte master1;
extern byte master2;

View File

@ -24,16 +24,15 @@
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "com232.h"
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
byte RS232_RxCharBuffer[25], RS232_RxCharBegin, RS232_RxCharEnd;
byte readkey;
void RS232_Init(void)
{
void RS232_Init(void) {
// // init LED
// sbi(DDRB, 5);
// cbi(PORTB, 5);
@ -41,47 +40,47 @@ void RS232_Init(void)
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.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
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
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)
{
void RS232_Print_P(const char *str_addr) {
register byte c;
while ( (c = pgm_read_byte(str_addr++) ) )
{
while ((c = pgm_read_byte(str_addr++))) {
if (c == '\n')
RS232_SendByte('\r');
RS232_SendByte(c);
}
}
void RS232_Print(const char *pBuf)
{
void RS232_Print(const char *pBuf) {
register byte c;
while ((c = *pBuf++))
{
while ((c = *pBuf++)) {
if (c == '\n')
RS232_SendByte('\r');
RS232_SendByte(c);
}
}
void RS232_PrintHex4(byte Data)
{
void RS232_PrintHex4(byte Data) {
byte Character = Data & 0x0f;
Character += '0';
if (Character > '9')
@ -89,14 +88,12 @@ void RS232_PrintHex4(byte Data)
RS232_SendByte(Character);
}
void RS232_PrintHex8(byte Data)
{
void RS232_PrintHex8(byte Data) {
RS232_PrintHex4(Data >> 4);
RS232_PrintHex4(Data);
}
void RS232_PrintDec(byte Data)
{
void RS232_PrintDec(byte Data) {
if (Data > 99) {
RS232_SendByte('*');
return;
@ -114,9 +111,9 @@ void RS232_PrintDec(byte Data)
RS232_SendByte(c);
}
void RS232_PrintDec2(byte Data)
{
if (Data<10) RS232_SendByte('0');
void RS232_PrintDec2(byte Data) {
if (Data < 10)
RS232_SendByte('0');
RS232_PrintDec(Data);
}

View File

@ -42,8 +42,4 @@ 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

View File

@ -24,13 +24,13 @@
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "GlobalDef.h"
#include "com232.h"
#include "avclandrv.h"
#include "com232.h"
void Setup();
@ -38,8 +38,7 @@ byte rcv_command[5];
byte rcv_pos = 0;
byte rcv_time_clr = 0;
int main()
{
int main() {
byte readSeq = 0;
byte s_len = 0;
byte s_dig = 0;
@ -51,7 +50,8 @@ int main()
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("\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"));
@ -69,59 +69,70 @@ int main()
} else {
// LED_OFF();
// check command from HU
if (answerReq != 0) AVCLan_SendAnswer();
if (answerReq != 0)
AVCLan_SendAnswer();
}
// HandleEvent
switch (Event) {
case EV_STATUS: Event &= ~EV_STATUS;
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
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;
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;
case 'W':
showLog = 1;
readSeq = 0;
AVCLan_SendMyData(data_tmp, s_len);
break;
case 'Q' : showLog = 1;
case 'Q':
showLog = 1;
readSeq = 0;
AVCLan_SendMyDataBroadcast(data_tmp, s_len);
break;
case 'R': RS232_Print_P(PSTR("REGIST:\n"));
case 'R':
RS232_Print_P(PSTR("REGIST:\n"));
AVCLan_Command(cmRegister);
TCB1.CNT = 0;
while( TCB1.CNT < 540 );
while (TCB1.CNT < 540) {}
CHECK_AVC_LINE;
break;
case 'r': AVCLan_Register();
case 'r':
AVCLan_Register();
break;
case 'l': RS232_Print_P(PSTR("Log OFF\n"));
case 'l':
RS232_Print_P(PSTR("Log OFF\n"));
showLog = 0;
break;
case 'L': RS232_Print_P(PSTR("Log ON\n"));
case 'L':
RS232_Print_P(PSTR("Log ON\n"));
showLog = 1;
break;
case 'k': RS232_Print_P(PSTR("str OFF\n"));
case 'k':
RS232_Print_P(PSTR("str OFF\n"));
showLog2 = 0;
break;
case 'K': RS232_Print_P(PSTR("str ON\n"));
case 'K':
RS232_Print_P(PSTR("str ON\n"));
showLog2 = 1;
break;
case 'B':
@ -160,11 +171,15 @@ int main()
s_dig++;
if (s_dig == 2) {
if (s_c[0]<':') s_c[0] -= 48;
else s_c[0] -= 55;
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;
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;
@ -185,8 +200,7 @@ int main()
return 0;
}
void Setup()
{
void Setup() {
CD_ID_1 = 0x03;
CD_ID_2 = 0x60;
@ -206,10 +220,8 @@ void Setup()
loop_until_bit_is_clear(RTC_PITSTATUS, RTC_CTRLBUSY_bp);
RTC.PITCTRLA = RTC_PERIOD_CYC32768_gc | RTC_PITEN_bm;
RS232_Init();
AVCLan_Init();
Event = EV_NOTHING;
@ -217,10 +229,8 @@ void Setup()
}
// Periodic interrupt with a 1 sec period
ISR(RTC_PIT_vect)
{
if (CD_Mode==stPlay)
{
ISR(RTC_PIT_vect) {
if (CD_Mode == stPlay) {
cd_Time_Sec = incBCD(cd_Time_Sec);
if (cd_Time_Sec == 0x60) {
cd_Time_Sec = 0;