-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
156 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |