From c906725a0c809ac27f7e2bbc8b2e9a00570de1b0 Mon Sep 17 00:00:00 2001 From: Allen Hill Date: Tue, 24 Oct 2023 16:39:39 -0400 Subject: [PATCH] Move RTC PIT to avclandrv and add CD status struct and answerreq --- src/avclandrv.c | 89 +++++++++++++++++++++++++++++++++++++++---------- src/avclandrv.h | 46 ++++++++++++++++++++----- src/sniffer.c | 52 ++--------------------------- 3 files changed, 111 insertions(+), 76 deletions(-) diff --git a/src/avclandrv.c b/src/avclandrv.c index a62244e..1df970a 100644 --- a/src/avclandrv.c +++ b/src/avclandrv.c @@ -134,9 +134,11 @@ uint8_t printBinary; uint8_t playMode; -uint8_t cd_Track; -uint8_t cd_Time_Min; -uint8_t cd_Time_Sec; +AVCLAN_CD_Status_t cd_status; + +uint8_t *cd_Track; +uint8_t *cd_Time_Min; +uint8_t *cd_Time_Sec; uint8_t answerReq; @@ -299,6 +301,14 @@ void AVCLAN_init() { TCB1.CCMP = 0xFFFF; TCB1.CTRLA = TCB_CLKSEL | TCB_ENABLE_bm; + // Setup RTC as 1 sec periodic timer + 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; + // Set PA4 and PC0 as outputs PORTA.DIRSET = PIN4_bm; PORTC.DIRSET = PIN0_bm; @@ -308,13 +318,56 @@ void AVCLAN_init() { answerReq = cm_Null; - cd_Track = 1; - cd_Time_Min = 0; - cd_Time_Sec = 0; + cd_status.cd1 = 1; + cd_status.disc = 1; + cd_status.cd2 = cd_status.cd3 = cd_status.cd4 = cd_status.cd5 = + cd_status.cd6 = 0; + cd_status.state = cd_LOADING; + cd_status.disk_random = 0; + cd_status.random = 0; + cd_status.disk_repeat = 0; + cd_status.repeat = 0; + cd_status.scan = 0; + + cd_status.track = 1; + cd_status.mins = 0; + cd_status.secs = 0; + + cd_Track = &cd_status.track; + cd_Time_Min = &cd_status.mins; + cd_Time_Sec = &cd_status.secs; + playMode = 0; CD_Mode = stStop; } +/* Increment packed 2-digit BCD number. + WARNING: Overflow behavior is incorrect (e.g. `incBCD(0x99) != 0x00`) */ +uint8_t incBCD(uint8_t data) { + if ((data & 0x9) == 0x9) + return (data + 7); + + return (data + 1); +} + +// Periodic interrupt with a 1 sec period +ISR(RTC_PIT_vect) { + if (CD_Mode == stPlay) { + uint8_t sec = *cd_Time_Sec; + uint8_t min = *cd_Time_Min; + sec = incBCD(sec); + if (sec == 0x60) { + *cd_Time_Sec = 0; + min = incBCD(min); + if (min == 0xA0) { + *cd_Time_Min = 0x0; + } + } + answerReq = cm_CDStatus; + } + RTC.PITINTFLAGS |= RTC_PI_bm; +} + void set_AVC_logic_for(uint8_t val, uint16_t period) { TCB1.CNT = 0; if (val) { @@ -878,6 +931,8 @@ uint8_t AVCLAN_sendframe(const AVCLAN_frame_t *frame) { return 0; } +uint8_t AVCLAN_responseNeeded() { return (answerReq != 0); } + void AVCLAN_printframe(const AVCLAN_frame_t *frame, uint8_t binary) { if (binary) { RS232_SendByte(0x10); // Data Link Escape, signaling binary data forthcoming @@ -990,9 +1045,9 @@ uint8_t AVCLan_SendInitCommands() { void AVCLan_Send_Status() { uint8_t STATUS[] = {0x63, 0x31, 0xF1, 0x01, 0x10, 0x01, 0x01, 0x00, 0x00, 0x00, 0x80}; - STATUS[6] = cd_Track; - STATUS[7] = cd_Time_Min; - STATUS[8] = cd_Time_Sec; + STATUS[6] = *cd_Track; + STATUS[7] = *cd_Time_Min; + STATUS[8] = *cd_Time_Sec; STATUS[9] = 0; AVCLAN_frame_t status = {.broadcast = UNICAST, @@ -1083,14 +1138,14 @@ uint8_t AVCLan_SendAnswer() { frame.broadcast = CMD_PLAY_OK4.broadcast; frame.length = CMD_PLAY_OK4.length; frame.data = (uint8_t *)&CMD_PLAY_OK4.data[0]; - CMD_PLAY_OK4.data[8] = cd_Track; - CMD_PLAY_OK4.data[9] = cd_Time_Min; - CMD_PLAY_OK4.data[10] = cd_Time_Sec; + CMD_PLAY_OK4.data[8] = *cd_Track; + CMD_PLAY_OK4.data[9] = *cd_Time_Min; + CMD_PLAY_OK4.data[10] = *cd_Time_Sec; r = AVCLAN_sendframe(&frame); - + CD_Mode = stPlay; + case cm_CDStatus: if (!r) AVCLan_Send_Status(); - CD_Mode = stPlay; break; case cm_StopReq: case cm_StopReq2: @@ -1101,9 +1156,9 @@ uint8_t AVCLan_SendAnswer() { frame.data = (uint8_t *)&CMD_STOP1.data[0]; r = AVCLAN_sendframe(&frame); - CMD_STOP2.data[8] = cd_Track; - CMD_STOP2.data[9] = cd_Time_Min; - CMD_STOP2.data[10] = cd_Time_Sec; + CMD_STOP2.data[8] = *cd_Track; + CMD_STOP2.data[9] = *cd_Time_Min; + CMD_STOP2.data[10] = *cd_Time_Sec; frame.broadcast = CMD_STOP2.broadcast; frame.length = CMD_STOP2.length; frame.data = (uint8_t *)&CMD_STOP2.data[0]; diff --git a/src/avclandrv.h b/src/avclandrv.h index a2629ab..5123d25 100644 --- a/src/avclandrv.h +++ b/src/avclandrv.h @@ -73,10 +73,43 @@ typedef enum { cm_PrevDisc = 123, cm_ScanModeOn = 130, cm_ScanModeOff = 131, + cm_CDStatus, } commands; +typedef enum { + cd_OPEN = 0x01, + cd_ERR1 = 0x02, + cd_SEEKING = 0x08, + cd_PLAYBACK = 0x10, + cd_SEEKING_TRACK = 0x20, + cd_LOADING = 0x80, +} cd_state; + +typedef struct AVCLAN_CD_Status { + _Bool cd1 : 1; + _Bool cd2 : 1; + _Bool cd3 : 1; + _Bool cd4 : 1; + _Bool cd5 : 1; + _Bool cd6 : 1; + int : 2; + cd_state state; + uint8_t disc; + uint8_t track; + uint8_t mins; + uint8_t secs; + int : 1; + _Bool disk_random : 1; + _Bool random : 1; + _Bool disk_repeat : 1; + _Bool repeat : 1; + _Bool disk_scan : 1; + _Bool scan : 1; + int : 2; + uint8_t flags2; +} AVCLAN_CD_Status_t; + typedef enum { stStop = 0, stPlay = 1 } cd_modes; -extern cd_modes CD_Mode; typedef enum MSG_TYPE { BROADCAST = 0, UNICAST = 1 } MSG_TYPE_t; @@ -98,6 +131,9 @@ typedef struct AVCLAN_frame_struct { uint8_t AVCLAN_readframe(); uint8_t AVCLAN_sendframe(const AVCLAN_frame_t *frame); + +uint8_t AVCLAN_responseNeeded(); + void AVCLAN_printframe(const AVCLAN_frame_t *frame, uint8_t binary); AVCLAN_frame_t *AVCLAN_parseframe(const uint8_t *bytes, uint8_t len); @@ -106,14 +142,6 @@ void AVCLan_Send_Status(); void AVCLan_Register(); uint8_t AVCLan_SendAnswer(); -extern uint8_t cd_Track; -extern uint8_t cd_Time_Min; -extern uint8_t cd_Time_Sec; - -extern uint8_t playMode; - -extern uint8_t answerReq; - #ifdef SOFTWARE_DEBUG void AVCLan_Measure(); #endif diff --git a/src/sniffer.c b/src/sniffer.c index 9b1ea0c..340ec3f 100644 --- a/src/sniffer.c +++ b/src/sniffer.c @@ -30,10 +30,6 @@ #include "avclandrv.h" #include "com232.h" -#define EV_NOTHING 0 -#define EV_STATUS 4 - -uint8_t Event; uint8_t echoCharacters; uint8_t readBinary; uint8_t readkey; @@ -67,18 +63,10 @@ int main() { AVCLAN_readframe(); } else { // check command from HU - if (answerReq != 0) + if (AVCLAN_responseNeeded()) AVCLan_SendAnswer(); } - // HandleEvent - switch (Event) { - case EV_STATUS: - Event &= ~EV_STATUS; - AVCLan_Send_Status(); - break; - } - // Key handler if (RS232_RxCharEnd) { cli(); @@ -157,7 +145,7 @@ int main() { break; case 'b': case 'B': // Beep - answerReq = cm_Beep; + // answerReq = cm_Beep; AVCLan_SendAnswer(); break; case 'e': // Beep @@ -250,20 +238,9 @@ void Setup() { _PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, (CLK_PRESCALE | CLK_PRESCALE_DIV)); general_GPIO_init(); - - // Setup RTC as 1 sec periodic timer - 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(); - AVCLAN_init(); - Event = EV_NOTHING; sei(); } @@ -314,28 +291,3 @@ void print_help() { #endif "? - Print this message\n"); } - -/* Increment packed 2-digit BCD number. - WARNING: Overflow behavior is incorrect (e.g. `incBCD(0x99) != 0x00`) */ -uint8_t incBCD(uint8_t data) { - if ((data & 0x9) == 0x9) - return (data + 7); - - return (data + 1); -} - -// 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; - } - } - Event |= EV_STATUS; - } - RTC.PITINTFLAGS |= RTC_PI_bm; -}