Skip to content

Commit

Permalink
+MilliHertz resolution. +New tests. +compiled Uf2s. v0.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
RPiks committed Nov 13, 2023
1 parent d7f92d4 commit 0e5f208
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 28 deletions.
22 changes: 11 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,38 @@ if (PICO_SDK_VERSION_STRING VERSION_LESS "1.4.0")
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.4.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()

project(pico-DCO-test C CXX ASM)
project(pico-hf-oscillator-test C CXX ASM)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()

# Add executable. Default name is the project name, version 0.1

add_executable(pico-DCO-test)
add_executable(pico-hf-oscillator-test)

pico_generate_pio_header(pico-DCO-test ${CMAKE_CURRENT_LIST_DIR}/piodco/dco.pio)
pico_generate_pio_header(pico-hf-oscillator-test ${CMAKE_CURRENT_LIST_DIR}/piodco/dco.pio)

target_sources(pico-DCO-test PUBLIC
target_sources(pico-hf-oscillator-test PUBLIC
${CMAKE_CURRENT_LIST_DIR}/lib/assert.c
${CMAKE_CURRENT_LIST_DIR}/piodco/piodco.c
${CMAKE_CURRENT_LIST_DIR}/test.c
)

pico_set_program_name(pico-DCO-test "pico-DCO-test")
pico_set_program_version(pico-DCO-test "0.9")
pico_set_program_name(pico-hf-oscillator-test "pico-hf-oscillator-test")
pico_set_program_version(pico-hf-oscillator-test "0.9")

pico_enable_stdio_uart(pico-DCO-test 1)
pico_enable_stdio_usb(pico-DCO-test 0)
pico_enable_stdio_uart(pico-hf-oscillator-test 1)
pico_enable_stdio_usb(pico-hf-oscillator-test 0)

# Add the standard include files to the build
target_include_directories(pico-DCO-test PRIVATE
target_include_directories(pico-hf-oscillator-test PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/.. # for our common lwipopts or any other standard includes, if required
)

# Add any user requested libraries
target_link_libraries(
pico-DCO-test
pico-hf-oscillator-test
pico_stdlib
pico_sync
pico_multicore
Expand All @@ -61,4 +61,4 @@ target_link_libraries(
hardware_vreg
)

pico_add_extra_outputs(pico-DCO-test)
pico_add_extra_outputs(pico-hf-oscillator-test)
17 changes: 10 additions & 7 deletions piodco/piodco.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,25 @@ int PioDCOInit(PioDco *pdco, int gpio, int cpuclkhz)
return 0;
}

/// @brief Sets DCO working frequency in Hz.
/// @brief Sets DCO working frequency in Hz: Fout = ui32_frq_hz + ui32_frq_millihz * 1e-3.
/// @param pdco Ptr to DCO context.
/// @param ui32_frq_hz The frequency [Hz].
/// @param ui32_frq_hz The `coarse` part of frequency [Hz].
/// @param ui32_frq_millihz The `fine` part of frequency [Hz].
/// @return 0 if OK. -1 invalid freq.
/// @attention The func can be called while DCO running.
int PioDCOSetFreq(PioDco *pdco, uint32_t ui32_frq_hz)
int PioDCOSetFreq(PioDco *pdco, uint32_t ui32_frq_hz, uint32_t ui32_frq_millihz)
{
assert_(pdco);
assert(pdco->_clkfreq_hz);

ui32_frq_hz <<= 1;

/* RPix: Calculate an accurate value of phase increment of the freq
per 1 tick of CPU clock, here 2^24 is scaling coefficient. */
pdco->_frq_cycles_per_pi = (int32_t)(((int64_t)pdco->_clkfreq_hz * (int64_t)(1<<24)
+ (ui32_frq_hz>>1)) / (int64_t)ui32_frq_hz);
const int64_t i64denominator = 2000LL * (int64_t)ui32_frq_hz + (int64_t)ui32_frq_millihz;
pdco->_frq_cycles_per_pi = (int32_t)(((int64_t)pdco->_clkfreq_hz * (int64_t)(1<<24) * 1000LL
+(i64denominator>>1)) / i64denominator);

//pdco->_frq_cycles_per_pi = (int32_t)(((int64_t)pdco->_clkfreq_hz * (int64_t)(1<<24)
//+ (ui32_frq_hz>>1)) / (int64_t)ui32_frq_hz);

si32precise_cycles = pdco->_frq_cycles_per_pi;

Expand Down
3 changes: 2 additions & 1 deletion piodco/piodco.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
// REVISION HISTORY
//
// Rev 0.1 05 Nov 2023
// Rev 0.2 18 Nov 2023
// Initial release.
//
// LICENCE
Expand Down Expand Up @@ -87,7 +88,7 @@ typedef struct
} PioDco;

int PioDCOInit(PioDco *pdco, int gpio, int cpuclkhz);
int PioDCOSetFreq(PioDco *pdco, uint32_t ui32_frq_hz);
int PioDCOSetFreq(PioDco *pdco, uint32_t ui32_frq_hz, uint32_t ui32_frq_millihz);

void PioDCOStart(PioDco *pdco);
void PioDCOStop(PioDco *pdco);
Expand Down
56 changes: 47 additions & 9 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,21 @@ void core1_entry()
PioDCOStart(&DCO);

/* Set initial freq. */
assert_(0 == PioDCOSetFreq(&DCO, freq_hz));
assert_(0 == PioDCOSetFreq(&DCO, freq_hz, 0u));

/* Run the main DCO algorithm. It spins forever. */
PioDCOWorker(&DCO);
}

void RAM (SpinnerMFSKTest)(void)
{
int i = 0;
uint32_t rndval = 77777777;
for(;;)
{
/* This example sets new RND frequency every ~250 ms.
Frequency shift is 5 Hz for each step.
*/
PioDCOSetFreq(&DCO, GEN_FRQ_HZ - 5*(rndval & 7));
PioDCOSetFreq(&DCO, GEN_FRQ_HZ - 5*(rndval & 7), 0u);

/* LED signal */
gpio_put(PICO_DEFAULT_LED_PIN, 1);
Expand All @@ -130,7 +129,7 @@ void RAM (SpinnerSweepTest)(void)
/* This example sets new frequency every ~250 ms.
Frequency shift is 5 Hz for each step.
*/
PioDCOSetFreq(&DCO, GEN_FRQ_HZ - 5*i);
PioDCOSetFreq(&DCO, GEN_FRQ_HZ - 5*i, 0u);

/* LED signal */
gpio_put(PICO_DEFAULT_LED_PIN, 1);
Expand All @@ -146,16 +145,14 @@ void RAM (SpinnerSweepTest)(void)

void RAM (SpinnerRTTYTest)(void)
{
int i = 0;
int32_t df = 170; /* 170 Hz freq diff. */
uint32_t rndval = 77777777;
for(;;)
{
/* This example sets new PRN frequency every ~22 ms.
Note: You should use precise timing while building a real transmitter.
*/

PioDCOSetFreq(&DCO, GEN_FRQ_HZ - df*(rndval & 1));
PioDCOSetFreq(&DCO, GEN_FRQ_HZ - df*(rndval & 1), 0u);

/* LED signal */
gpio_put(PICO_DEFAULT_LED_PIN, 1);
Expand All @@ -167,6 +164,45 @@ void RAM (SpinnerRTTYTest)(void)
}
}

void RAM (SpinnerMilliHertzTest)(void)
{
int i = 0;
for(;;)
{
/* This example sets new frequency every ~1s.
Frequency shift is 0.99 Hz for each step.
*/
PioDCOSetFreq(&DCO, GEN_FRQ_HZ, 990*(++i&1));

/* LED signal */
gpio_put(PICO_DEFAULT_LED_PIN, 1);
sleep_ms(1000);
gpio_put(PICO_DEFAULT_LED_PIN, 0);
sleep_ms(1000);
}
}

void RAM (SpinnerWide4FSKTest)(void)
{
int32_t df = 100; /* 100 Hz freq diff * 4 = 400 Hz. */
uint32_t rndval = 77777777;
for(;;)
{
/* This example sets new PRN frequency every ~20 ms.
Note: You should use precise timing while building a real transmitter.
*/
PioDCOSetFreq(&DCO, GEN_FRQ_HZ - df*(rndval & 3), 0u);

/* LED signal */
gpio_put(PICO_DEFAULT_LED_PIN, 1);
sleep_ms(20);
gpio_put(PICO_DEFAULT_LED_PIN, 0);
sleep_ms(20);

PRN32(&rndval);
}
}

int main()
{
const uint32_t clkhz = PLL_SYS_MHZ * 1000000L;
Expand All @@ -178,8 +214,10 @@ int main()
multicore_launch_core1(core1_entry);

//SpinnerSweepTest();
//SpinnerMFSKTest();
SpinnerRTTYTest();
SpinnerMFSKTest();
//SpinnerRTTYTest();
//SpinnerMilliHertzTest();
//SpinnerWide4FSKTest();
}

void PRN32(uint32_t *val)
Expand Down
Binary file added test/pico-MFSK-9M4-test.uf2
Binary file not shown.
Binary file added test/pico-MilliHertz-9M4-test.uf2
Binary file not shown.
Binary file added test/pico-RTTY-9M4-test.uf2
Binary file not shown.
Binary file added test/pico-Wide4FSK-9M4-test.uf2
Binary file not shown.
Binary file added test/pico-sweep-9M4-test.uf2
Binary file not shown.

0 comments on commit 0e5f208

Please sign in to comment.