diff --git a/ch32v/ch32v003-timer/nonlora.c b/ch32v/ch32v003-timer/nonlora.c index dc9b82a..451847c 100644 --- a/ch32v/ch32v003-timer/nonlora.c +++ b/ch32v/ch32v003-timer/nonlora.c @@ -184,11 +184,11 @@ void DMA1_Channel3_IRQHandler( void ) } // I tried using the DMA to do the copy - but that didn't work for some reason. - //while( DMA1_Channel2->CFGR & 1 ); -// DMA1_Channel2->CNTR = DMA_SIZE_WORDS/2; -// DMA1_Channel2->MADDR = (uint32_t)tsb; -// DMA1_Channel2->PADDR = (uint32_t)sb; -// DMA1_Channel2->CFGR |= DMA_CFGR1_EN; + //while( DMA1_Channel5->CFGR & 1 ); +// DMA1_Channel5->CNTR = DMA_SIZE_WORDS/2; +// DMA1_Channel5->MADDR = (uint32_t)tsb; +// DMA1_Channel5->PADDR = (uint32_t)sb; +// DMA1_Channel5->CFGR |= DMA_CFGR1_EN; int cpy = DMA_SIZE_WORDS/2; #ifdef TEST_TONE @@ -238,13 +238,13 @@ complete: void LoopFunction() __attribute__((section(".srodata"))); void LoopFunction() { - uint8_t * start = (uint8_t*)DMA1_Channel2->MADDR; - uint8_t * end = (uint8_t*)((uint32_t)DMA1_Channel2->MADDR + SENDBUFF_WORDS); + uint8_t * start = (uint8_t*)DMA1_Channel5->MADDR; + uint8_t * end = (uint8_t*)((uint32_t)DMA1_Channel5->MADDR + SENDBUFF_WORDS); uint8_t * here = start+ 8; uint32_t targ = 2000; uint32_t running = 0; - uint8_t * tail = end - DMA1_Channel2->CNTR; - uint32_t * cntr = DMA1_Channel2->CNTR; + uint8_t * tail = end - DMA1_Channel5->CNTR; + uint32_t * cntr = DMA1_Channel5->CNTR; uint32_t temp = 0; uint32_t temp2 = 0; @@ -282,7 +282,7 @@ skipreset:\n\ { int targ_f = 2000; //(frameno & 511)*9 + 1700; int run_f = 0; - uint8_t * tail = end - DMA1_Channel2->CNTR; + uint8_t * tail = end - DMA1_Channel5->CNTR; while( here != tail ) { @@ -317,6 +317,14 @@ skipreset:\n\ void LoopFunction2() __attribute__((aligned(256))) __attribute__((section(".srodata"))) __attribute__ ((noinline)); __attribute__((section(".sdata"))) __attribute__((aligned(256))) const uint32_t tablef[] = { + 0x06060606, + 0x06060607, + 0x06070607, + 0x07070706, + 0x07070707, + 0x07070708, + 0x07080708, + 0x08080807, 0x08080808, 0x08080809, 0x08090809, @@ -358,13 +366,13 @@ __attribute__((section(".sdata"))) __attribute__((aligned(256))) const uint32_t void LoopFunction2() { - uint32_t * start = (uint8_t*)DMA1_Channel2->MADDR; - uint32_t * end = (uint8_t*)((uint32_t)DMA1_Channel2->MADDR + SENDBUFF_WORDS); + uint32_t * start = (uint8_t*)DMA1_Channel5->MADDR; + uint32_t * end = (uint8_t*)((uint32_t)DMA1_Channel5->MADDR + SENDBUFF_WORDS); uint32_t * here = start; int run_f = 0; - volatile uint32_t * cntrptr = &DMA1_Channel2->CNTR; + volatile uint32_t * cntrptr = &DMA1_Channel5->CNTR; while(1) { @@ -376,9 +384,9 @@ void LoopFunction2() { #ifdef FM_TRANSMITTER_SWEEP // 97.7MHz FM Station - int notein = (SysTick->CNT>>4)&0x3fff; - if( notein > 0x1fff ) notein = 0x4000-notein; - uint32_t cp = (notein)+0x12900; + int notein = (SysTick->CNT>>3)&0x1fff; + if( notein > 0xfff ) notein = 0x2000-notein; + uint32_t cp = (notein)+0x20000; #else // 315MHz uint32_t cp = 0x1bfc3; @@ -456,7 +464,7 @@ int main() TIM1->PSC = 0x0000; // Auto Reload - sets period - TIM1->ATRLR = 20; + TIM1->ATRLR = 17; // Reload immediately TIM1->SWEVGR |= TIM_UG; @@ -479,10 +487,10 @@ int main() #endif // Compare 1 = for triggering - TIM1->CHCTLR1 = TIM_OC1M_2 | TIM_OC1M_1; + TIM1->CHCTLR1 = TIM_OC1M_2 | TIM_OC1M_1 | TIM_OC1FE; // Set the Capture Compare Register value to 50% initially - TIM1->CH3CVR = 5; // ACTUALLY Ignored typically it seems. + TIM1->CH3CVR = 4; // ACTUALLY Ignored typically it seems. TIM1->CH1CVR = 0; // This triggers DMA. // Enable TIM1 outputs @@ -491,14 +499,28 @@ int main() // Enable TIM1 TIM1->CTLR1 |= TIM_CEN; - TIM1->DMAINTENR = TIM_TDE | TIM_COMDE | TIM_CC1DE | TIM_UDE; + TIM1->DMAINTENR = TIM_TDE | TIM_UDE; // Outputs DMA1_Channel5 +#if 0 + // Another try - use TIM2 to trigger DMA? + RCC->APB1PCENR |= RCC_APB1Periph_TIM2; + RCC->APB1PRSTR |= RCC_APB1Periph_TIM2; + RCC->APB1PRSTR &= ~RCC_APB1Periph_TIM2; - //DMA1_Channel3 is for SPI1TX - DMA1_Channel2->PADDR = (uint32_t)&TIM1->ATRLR; - DMA1_Channel2->MADDR = (uint32_t)sendbuff; - DMA1_Channel2->CNTR = 0;// sizeof( bufferset )/2; // Number of unique copies. (Don't start, yet!) - DMA1_Channel2->CFGR = + TIM2->PSC = 0x0000; + TIM2->ATRLR = 37; + TIM2->SWEVGR |= TIM_UG; + TIM2->BDTR |= TIM_MOE; + TIM2->CHCTLR1 = TIM_OC1M_2 | TIM_OC1M_1 | TIM_OC1FE; + TIM2->DMAINTENR |= TIM_UDE | TIM_TDE | TIM_TDE | TIM_CC1DE; + TIM2->CTLR1 |= TIM_CEN; + // TIM2_UP = DMA Channel 2. +#endif + + DMA1_Channel5->PADDR = (uint32_t)&TIM1->CH3CVR; + DMA1_Channel5->MADDR = (uint32_t)sendbuff; + DMA1_Channel5->CNTR = 0;// sizeof( bufferset )/2; // Number of unique copies. (Don't start, yet!) + DMA1_Channel5->CFGR = DMA_M2M_Disable | DMA_Priority_VeryHigh | DMA_PeripheralDataSize_HalfWord | @@ -519,9 +541,10 @@ int main() // Enter critical section. - DMA1_Channel2->MADDR = (uint32_t)sendbuff; - DMA1_Channel2->CNTR = SENDBUFF_WORDS; // Number of unique uint16_t entries. - DMA1_Channel2->CFGR |= DMA_CFGR1_EN; + DMA1_Channel5->MADDR = (uint32_t)sendbuff; + DMA1_Channel5->CNTR = SENDBUFF_WORDS; // Number of unique uint16_t entries. + DMA1_Channel5->CFGR |= DMA_CFGR1_EN; + LoopFunction2(); } diff --git a/ch32v/ch32v203-goertzel/adcgoertzel.c b/ch32v/ch32v203-goertzel/adcgoertzel.c index 09ecfa3..183f62c 100644 --- a/ch32v/ch32v203-goertzel/adcgoertzel.c +++ b/ch32v/ch32v203-goertzel/adcgoertzel.c @@ -64,7 +64,7 @@ SOFTWARE. #define SH1107_128x128 -#define PWM_OUTPUT +//#define PWM_OUTPUT #define ENABLE_OLED #include "ssd1306_i2c.h" #include "ssd1306.h" @@ -89,7 +89,7 @@ const int32_t g_goertzel_coefficient_s = 1580594514; #if 0 #define PWM_PERIOD (30-1) -#define GOERTZEL_BUFFER (420) +#define GOERTZEL_BUFFER (180) const int32_t g_goertzel_omega_per_sample = 5509657063; // 0.816667 of whole per step / 0.880000MHz const int32_t g_goertzel_coefficient = 873460290; const int32_t g_goertzel_coefficient_s = -1961823932; @@ -109,7 +109,6 @@ const int32_t g_goertzel_coefficient_s = 2147233926; 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; - #endif #if 0 @@ -118,10 +117,9 @@ const int32_t g_goertzel_coefficient_s = 1984016189; const int32_t g_goertzel_omega_per_sample = 990894956; // 0.146875 of whole per step / 101.505000MHz const int32_t g_goertzel_coefficient = 1296126516; const int32_t g_goertzel_coefficient_s = 1712233066; - #endif -#if 1 +#if 0 #define PWM_PERIOD (30-1) #define GOERTZEL_BUFFER (384) const int32_t g_goertzel_omega_per_sample = 4251712402; // 0.630208 of whole per step / 27.025000MHz @@ -129,6 +127,15 @@ const int32_t g_goertzel_coefficient = -1468003291; const int32_t g_goertzel_coefficient_s = -1567371161; #endif +#if 1 +#define PWM_PERIOD (30-1) +#define GOERTZEL_BUFFER (336) +const int32_t g_goertzel_omega_per_sample = 1827182189; // 0.270833 of whole per step / 89.900000MHz +const int32_t g_goertzel_coefficient = -280302863; +const int32_t g_goertzel_coefficient_s = 2129111628; +#endif + + int intensity_max = 1; #define LOG_GOERTZEL_LIST 512 @@ -226,7 +233,7 @@ static void SetupTimer1() #ifdef PWM_OUTPUT // PA9 = T1CH2. GPIOA->CFGHR &= ~(0xf<<(4*1)); - GPIOA->CFGHR |= (GPIO_Speed_10MHz | GPIO_CNF_OUT_PP_AF)<<(4*1); + GPIOA->CFGHR |= (GPIO_Speed_2MHz | GPIO_CNF_OUT_PP_AF)<<(4*1); TIM1->CCER = TIM_CC2E | TIM_CC2P; TIM1->CHCTLR1 |= TIM_OC2M_2 | TIM_OC2M_1 | TIM_OC2FE; @@ -358,25 +365,28 @@ void DMA1_Channel1_IRQHandler( void ) gertzellogs[gertzellogs_head++] = g_goertzelp2_store; gertzellogs_head = gertzellogs_head & ((LOG_GOERTZEL_LIST*2)-1); - - - #ifdef PWM_OUTPUT int32_t zp = g_goertzelp_store; int32_t zp2 = g_goertzelp2_store; int32_t rr = (((int64_t)(g_goertzel_coefficient ) * (int64_t)zp<<1)>>32) - (zp2); int32_t ri = (((int64_t)(g_goertzel_coefficient_s) * (int64_t)zp<<1)>>32); - //rr>>=1; - //ri>>=1; - rr = rr * PWM_PERIOD / (intensity_max>>7); - ri = ri * PWM_PERIOD / (intensity_max>>7); + rr>>=2; + ri>>=2; + int s = rr * rr + ri * ri; int intensity = 1<<( ( 32 - __builtin_clz(s) )/2); intensity = (intensity + s/intensity)/2; intensity = (intensity + s/intensity)/2; - - if( intensity >= PWM_PERIOD ) intensity = PWM_PERIOD-1; - if( intensity < 0 ) intensity = 0; + + intensity_max = intensity_max - (intensity_max>>10) + (intensity>>2); + + + #ifdef PWM_OUTPUT + + intensity = intensity * PWM_PERIOD / (intensity_max>>7); + + if( intensity >= PWM_PERIOD-1 ) intensity = PWM_PERIOD-2; + if( intensity < 1 ) intensity = 1; TIM1->CH2CVR = intensity; // Actual duty cycle (Off to begin with) #endif @@ -425,6 +435,7 @@ void InnerLoop() int glread = gertzellogs_head+LOG_GOERTZEL_LIST*2/2; + int intensity = 0; for( pxa = 0; pxa < LOG_GOERTZEL_LIST/2; pxa++ ) { @@ -437,15 +448,13 @@ void InnerLoop() //rr>>=1; //ri>>=1; - int s = rr * rr + ri * ri; - int intensity = 1<<( ( 32 - __builtin_clz(s) )/2); - intensity = (intensity + s/intensity)/2; - intensity = (intensity + s/intensity)/2; -// if( intensity > intensity_max ) intensity_max = intensity; - intensity_max = intensity_max - (intensity_max>>8) + intensity; + //int s = rr * rr + ri * ri; + //intensity = 1<<( ( 32 - __builtin_clz(s) )/2); + //intensity = (intensity + s/intensity)/2; + //intensity = (intensity + s/intensity)/2; - rr = rr * 64 / (intensity_max>>7); - ri = ri * 64 / (intensity_max>>7); + rr = rr * 100 / (intensity_max>>4); + ri = ri * 100 / (intensity_max>>4); rr += 64; ri += 64; @@ -464,16 +473,8 @@ void InnerLoop() ssd1306_refresh(); ssd1306_setbuf(0); - int32_t rr = (((int64_t)(g_goertzel_coefficient ) * (int64_t)g_goertzelp_store<<1)>>32) - (g_goertzelp2_store); \ - int32_t ri = (((int64_t)(g_goertzel_coefficient_s) * (int64_t)g_goertzelp_store<<1)>>32); \ - - int s = rr * rr + ri * ri; - int x = 1<<( ( 32 - __builtin_clz(s) )/2); - x = (x + s/x)/2; - x = (x + s/x)/2; - char cts[32]; - snprintf( cts, 32, "%6d", x ); + snprintf( cts, 32, "%d %d", intensity_max, intensity ); ssd1306_drawstr( 0, 0, cts, 1 ); #else @@ -494,9 +495,6 @@ int main() SysTick->CTLR = (1<<2) | 1; // HCLK Delay_Ms(100); - - printf( "System On\n" ); - RCC->CTLR |= RCC_HSEON; while( ! ( RCC->CTLR & RCC_HSERDY ) ); @@ -530,11 +528,12 @@ int main() SetupADC(); - printf( "Setting up OLED.\n" ); +#ifdef ENABLE_OLED ssd1306_i2c_setup(); uint8_t ret = ssd1306_i2c_init(); ssd1306_init(); ssd1306_setbuf(0); +#endif #if 0 int i = 0; @@ -564,12 +563,7 @@ int main() EXTEN->EXTEN_CTR |= EXTEN_OPA_NSEL; #endif - - printf( "ADC Setup\n" ); - SetupTimer1(); - printf( "Timer 1 setup\n" ); - InnerLoop(); }