Skip to content

Commit

Permalink
started T1 interface
Browse files Browse the repository at this point in the history
  • Loading branch information
AE9RB committed Apr 8, 2013
1 parent 562a791 commit e57e9e6
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 62 deletions.
62 changes: 3 additions & 59 deletions peaberry.cydsn/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,14 @@

#include <peaberry.h>

// IQ Reveral option
// IQ Reversal option
// 0: RX=norm TX=norm
// 1: RX=rev TX=norm
// 2: RX=norm TX=rev
// 3: RX=rev TX=rev
uint8 Audio_IQ_Channels;

#define SOF_CENTER 22000
#define FRAC_MIN (FracN_DEFAULT-100)
#define FRAC_MAX (FracN_DEFAULT+100)

// This is a simplified PID control. It works quite well. The full
// PID algorithm will be implemented some day for fun and learning.
void Audio_Buffer_Sync(void) {
static uint16 frac = FracN_DEFAULT;
static uint16 prev_pos;
uint16 pos;

pos = CY_GET_REG8(SyncSOF_FRAME_POS_LO__STATUS_REG);
if (pos & 0x01) {
pos += (uint16)CY_GET_REG8(SyncSOF_FRAME_POS_HI__STATUS_REG) << 8;

if (pos > SOF_CENTER) {
frac += (pos - SOF_CENTER) / 128;
} else {
frac -= (SOF_CENTER - pos) / 128;
}

if (prev_pos < pos) {
frac += (pos - prev_pos) / 2;
} else {
frac -= (prev_pos - pos) / 2;
}
prev_pos = pos;

if (frac > FRAC_MAX) frac = FRAC_MAX;
if (frac < FRAC_MIN) frac = FRAC_MIN;
FracN_Set(frac);
}

}


void Audio_Detect_Change() {
void Audio_Main() {
switch (Si570_LO) {
case 0xA8AAAA10: Audio_IQ_Channels = 0; break; // 33.333333 MHz
case 0x8AE3B810: Audio_IQ_Channels = 1; break; // 33.444444 MHz
Expand Down Expand Up @@ -111,10 +75,6 @@ void Audio_USB_ReadOutEP(uint8 epNumber, uint8 *pData, uint16 length)
CyDmaChSetInitialTd(USBFS_DmaChan[epNumber], USBFS_DmaTd[epNumber]);
CyDmaChEnable(USBFS_DmaChan[epNumber], 1);
}


uint8 TX_Enabled = 0;

void Audio_Start(void) {
if(USBFS_DmaTd[RX_ENDPOINT] == DMA_INVALID_TD)
USBFS_InitEP_DMA(RX_ENDPOINT, PCM3060_RxBuf());
Expand All @@ -123,22 +83,7 @@ void Audio_Start(void) {
Audio_USB_ReadOutEP(TX_ENDPOINT, PCM3060_TxBuf(), 0);
}

void Audio_Main(void) {
Audio_Buffer_Sync();
Audio_Detect_Change();

if (USBFS_GetInterfaceSetting(TX_INTERFACE)) {
if (!TX_Enabled) {
USBFS_EnableOutEP(TX_ENDPOINT);
TX_Enabled = 1;
}
} else {
if (TX_Enabled) {
USBFS_DisableOutEP(TX_ENDPOINT);
TX_Enabled = 0;
}
}

void Audio_USB(void) {
if(USBFS_IsConfigurationChanged()) {
USBFS_EnableOutEP(TX_ENDPOINT);
}
Expand All @@ -151,5 +96,4 @@ void Audio_Main(void) {
if (USBFS_GetEPState(RX_ENDPOINT) == USBFS_IN_BUFFER_EMPTY) {
Audio_USB_LoadInEP(RX_ENDPOINT, PCM3060_RxBuf(), I2S_BUF_SIZE);
}

}
12 changes: 9 additions & 3 deletions peaberry.cydsn/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ void main()
Control_Write(Control_Read() & ~CONTROL_LED);

for(;;) {
// Audio is high priority
Audio_Main();
// USB Audio is very high priority
Audio_USB();
Sync_USB();
// Everything else runs twice per millisecond
// Keep T1 first for timing accuracy
i = Status_Read() & STATUS_BEAT;
if (beat != i) {
switch(beater++) {
case 0:
main_usb_vbus();
T1_Main();
break;
case 1:
TX_Main();
Expand All @@ -66,7 +68,11 @@ void main()
case 3:
Si570_Main();
break;
case 4:
Audio_Main();
break;
default:
main_usb_vbus();
beater = 0;
beat = i;
}
Expand Down
Binary file modified peaberry.cydsn/peaberry.cydwr
Binary file not shown.
14 changes: 14 additions & 0 deletions peaberry.cydsn/peaberry.cyprj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@
<build_action v="C_FILE" />
<PropertyDeltas />
</CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b>
<CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFile" version="3" xml_contents_version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItem" version="2" name="t1.c" persistent=".\t1.c">
<Hidden v="False" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<build_action v="C_FILE" />
<PropertyDeltas />
</CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b>
<CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtFile" version="3" xml_contents_version="1">
<CyGuid_31768f72-0253-412b-af77-e7dba74d1330 type_name="CyDesigner.Common.ProjMgmt.Model.CyPrjMgmtItem" version="2" name="sync.c" persistent=".\sync.c">
<Hidden v="False" />
</CyGuid_31768f72-0253-412b-af77-e7dba74d1330>
<build_action v="C_FILE" />
<PropertyDeltas />
</CyGuid_8b8ab257-35d3-4473-b57b-36315200b38b>
</dependencies>
</CyGuid_0820c2e7-528d-4137-9a08-97257b946089>
</CyGuid_2f73275c-45bf-46ba-b3b1-00a2fe0c8dd8>
Expand Down
6 changes: 6 additions & 0 deletions peaberry.cydsn/peaberry.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ void Morse_Main(char* msg);
extern uint8 Audio_IQ_Channels;
void Audio_Start(void);
void Audio_Main(void);
void Audio_USB(void);

// sync.c
void Sync_USB(void);

// si570.c
#define SI570_STARTUP_FREQ 56.32
Expand All @@ -89,5 +93,7 @@ void Settings_Main(void);
extern uint8 TX_Request;
void TX_Main(void);

// t1.c
void T1_Main(void);

#endif //PEABERRY_H
50 changes: 50 additions & 0 deletions peaberry.cydsn/sync.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2013 David Turnbull AE9RB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <peaberry.h>

#define SOF_CENTER 22000
#define FRAC_MIN (FracN_DEFAULT-100)
#define FRAC_MAX (FracN_DEFAULT+100)

// This is a simplified PID control. It works quite well. The full
// PID algorithm will be implemented some day for fun and learning.
void Sync_USB(void) {
static uint16 frac = FracN_DEFAULT;
static uint16 prev_pos;
uint16 pos;

pos = CY_GET_REG8(SyncSOF_FRAME_POS_LO__STATUS_REG);
if (pos & 0x01) {
pos += (uint16)CY_GET_REG8(SyncSOF_FRAME_POS_HI__STATUS_REG) << 8;

if (pos > SOF_CENTER) {
frac += (pos - SOF_CENTER) / 128;
} else {
frac -= (SOF_CENTER - pos) / 128;
}

if (prev_pos < pos) {
frac += (pos - prev_pos) / 2;
} else {
frac -= (prev_pos - pos) / 2;
}
prev_pos = pos;

if (frac > FRAC_MAX) frac = FRAC_MAX;
if (frac < FRAC_MIN) frac = FRAC_MIN;
FracN_Set(frac);
}

}
74 changes: 74 additions & 0 deletions peaberry.cydsn/t1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2013 David Turnbull AE9RB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <peaberry.h>

#define T1_STATE_IDLE 0

uint8 T1_Band, T1_Tune_Timer;
uint32 T1_LO;

// type 0 is wakeup for bandswitch, type 1 engages auto-tune
void T1_Tune(uint8 type) {
uint16 timer;
if (type) timer = 1000;
else timer = 10;
if (timer > T1_Tune_Timer) T1_Tune_Timer = timer;
Control_Write(Control_Read() | CONTROL_ATU_1);
}

void T1_LO_Watch(void) {
uint8 band;
uint32 i;

if (Si570_LO != T1_LO) {
T1_LO = Si570_LO;
i = swap32(Si570_LO);
if (i > 0x1b800000) band = 0; // 55.000 MHz
else if (i > 0xf800000) band = 11; // 31.000 MHz
else if (i > 0xd000000) band = 10; // 26.000 MHz
else if (i > 0xb000000) band = 9; // 22.000 MHz
else if (i > 0x9800000) band = 8; // 19.000 MHz
else if (i > 0x8000000) band = 7; // 16.000 MHz
else if (i > 0x6000000) band = 6; // 12.000 MHz
else if (i > 0x4800000) band = 5; // 9.000 MHz
else if (i > 0x3000000) band = 4; // 6.000 MHz
else if (i > 0x2800000) band = 3; // 5.000 MHz
else if (i > 0x1800000) band = 2; // 3.000 MHz
else if (i > 0x0c00000) band = 1; // 1.500 MHz
else band = 0;
if (T1_Band != band) {
T1_Band = band;
T1_Tune(0);
}
}
}

void T1_Main(void) {
static uint8 T1_State = 0;

if (T1_Tune_Timer) {
T1_Tune_Timer--;
if (!T1_Tune_Timer) {
Control_Write(Control_Read() & ~CONTROL_ATU_1);
}
}
T1_LO_Watch();

switch(T1_State) {
case T1_STATE_IDLE:
break;
}

}

0 comments on commit e57e9e6

Please sign in to comment.