From 6c7adbdcc28c325cbd9746b3c8c9632e22188fe9 Mon Sep 17 00:00:00 2001 From: cnlohr Date: Tue, 9 Jul 2024 03:18:12 -0700 Subject: [PATCH] Making good progress. --- ch32v/ch32v203-goertzel/adcgoertzel.c | 28 ++++++++++++++++++++------ ch32v/lib/calculator.html | 20 +++++++++++------- ch32v/lib/webhidcontrol.js | 29 +++++++++++++++++++-------- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/ch32v/ch32v203-goertzel/adcgoertzel.c b/ch32v/ch32v203-goertzel/adcgoertzel.c index efac46c..5c5c862 100644 --- a/ch32v/ch32v203-goertzel/adcgoertzel.c +++ b/ch32v/ch32v203-goertzel/adcgoertzel.c @@ -89,7 +89,7 @@ int32_t g_goertzel_coefficient = -1453756170; int32_t g_goertzel_coefficient_s = 1580594514; #endif -#if 1 +#if 0 int g_pwm_period = (30-1); int g_goertzel_buffer = (180); int32_t g_goertzel_omega_per_sample = 5509657063; // 0.816667 of whole per step / 0.880000MHz @@ -105,12 +105,12 @@ const int32_t g_goertzel_coefficient = 32748822; const int32_t g_goertzel_coefficient_s = 2147233926; #endif -#if 0 +#if 1 int g_pwm_period = (30-1); int g_goertzel_buffer = (576); -const int32_t g_goertzel_omega_per_sample = 1264972285; // 0.187500 of whole per step / 90.300000MHz -const int32_t g_goertzel_coefficient = 821806413; -const int32_t g_goertzel_coefficient_s = 1984016189; +int32_t g_goertzel_omega_per_sample = 1264972285; // 0.187500 of whole per step / 90.300000MHz +int32_t g_goertzel_coefficient = 821806413; +int32_t g_goertzel_coefficient_s = 1984016189; #endif #if 0 @@ -683,7 +683,23 @@ void HandleHidUserReportOutComplete( struct _USBState * ctx ) { if( g_isConfigurePacket ) { - printf( "Is Configure Packet\n" ); + + uint32_t * configs = (uint32_t*)scratchpad; + // Note: configs[0] == 0xac (command type) + + printf( "Is Configure Packet %08x\n", configs[1] ); + + int numconfigs = configs[1]; + if( numconfigs > 0) g_pwm_period = configs[2]; + if( numconfigs > 1) g_goertzel_buffer = configs[3]; + if( numconfigs > 2) g_goertzel_omega_per_sample = configs[4]; // 0.816667 of whole per step / 0.880000MHz + if( numconfigs > 3) g_goertzel_coefficient = configs[5]; + if( numconfigs > 4) g_goertzel_coefficient_s = configs[6]; + + // Need to reset so we don't blast by. + g_goertzel_samples = 0; + TIM1->ATRLR = g_pwm_period; + g_isConfigurePacket = 0; } return; diff --git a/ch32v/lib/calculator.html b/ch32v/lib/calculator.html index 2c312ed..812025b 100644 --- a/ch32v/lib/calculator.html +++ b/ch32v/lib/calculator.html @@ -33,18 +33,24 @@ function Goertz( n, mhz, fr, brf ) { let omega = fr * 3.1415926535*2.0; var textarea = document.getElementById("goertzeloutput"); + + var g_goertzel_omega_per_sample = Math.round( ( omega*2*(1<<29)) ); + var g_goertzel_coefficient = Math.round( (2 * Math.cos( omega ) * (1<<30)) ); + var g_goertzel_coefficient_s = Math.round( (2 * Math.sin( omega ) * (1<<30)) ); textarea.value = "int g_pwm_period = ("+n+"-1);\n" + "int g_goertzel_buffer = ("+brf+");\n" + - "int32_t g_goertzel_omega_per_sample = " + ( omega*2*(1<<29)).toFixed(0) + "; // " + ( omega / (3.1415926535*2.0)).toFixed(6) + " of whole per step / " + mhz.toFixed(6) + "MHz\n" + - "int32_t g_goertzel_coefficient = " + (2 * Math.cos( omega ) * (1<<30)).toFixed(0) + ";\n" + - "int32_t g_goertzel_coefficient_s = "+ (2 * Math.sin( omega ) * (1<<30)).toFixed(0) + ";\n"; + "int32_t g_goertzel_omega_per_sample = " + g_goertzel_coefficient.toFixed(0) + "; // " + ( omega / (3.1415926535*2.0)).toFixed(6) + " of whole per step / " + mhz.toFixed(6) + "MHz\n" + + "int32_t g_goertzel_coefficient = " + g_goertzel_coefficient.toFixed(0) + ";\n" + + "int32_t g_goertzel_coefficient_s = " + g_goertzel_coefficient_s.toFixed(0) + ";\n"; - // Highlight its content - textarea.select(); + // Highlight its content + textarea.select(); - // Copy the highlighted text - document.execCommand("copy"); + // Copy the highlighted text + document.execCommand("copy"); + + updateWebHidDeviceWithParameters( [ (n-1)|0, brf|0, g_goertzel_omega_per_sample|0, g_goertzel_coefficient|0, g_goertzel_coefficient_s|0 ] ); } function computeTable() diff --git a/ch32v/lib/webhidcontrol.js b/ch32v/lib/webhidcontrol.js index 5427d47..5c3d5e8 100644 --- a/ch32v/lib/webhidcontrol.js +++ b/ch32v/lib/webhidcontrol.js @@ -136,10 +136,30 @@ async function sendLoopError( e ) setStatusError( e ); } +function updateWebHidDeviceWithParameters( paramlist ) +{ + var i = 0|0; + var arraySend = new Uint8Array(63); + for( var i = 0|0; i < paramlist.length|0; i++ ) + { + var vv = paramlist[i] | 0; + arraySend[i*4+7] = (vv>>0)&0xff; + arraySend[i*4+8] = (vv>>8)&0xff; + arraySend[i*4+9] = (vv>>16)&0xff; + arraySend[i*4+10] = (vv>>24)&0xff; + } + arraySend[3] = paramlist.length | 0; + + console.log( arraySend ); + + sendReport = dev.sendFeatureReport( 0xAC, arraySend ).catch( sendLoopError ); + if( !sendReport ) sendLoopError( "error creating sendFeatureReport" ); +} + + async function sendLoop() { const sleep = ms => new Promise(r => setTimeout(r, ms)); - //var arraySend = new Uint8Array(255); var frameNo = 0|0; var lastTime = performance.now(); let goodCount = 0; @@ -151,13 +171,6 @@ async function sendLoop() { if( dev && !loopAbort ) { - //var i = 0|0; - //for( var i = 0|0; i < 255|0; i++ ) - // arraySend[i] = (Math.random()*256)|0; - - //sendReport = dev.sendFeatureReport( 0xAA, arraySend ).catch( sendLoopError ); - //if( !sendReport ) sendLoopError( "error creating sendFeatureReport" ); - receiveReport = dev.receiveFeatureReport( 0xAD ).catch( sendLoopError ); if( !receiveReport ) sendLoopError( "error creating receiveReport" );