Ok...the full program is below.
The two pins I'm seeking to reset are 22 and 23 which are both on FlexPWM4.
The attempt to resync PWM happens after this line:
if((command_buf & 0x000000000000FF00) == 0x0000000000000000){ //resync CW
I can see the test output 'Y' in the serial, indicating that the if statement is entered.
I have my test code for pinmode, analogwrite, and analogwrite frequency present...but all seem to do nothing.
With a scope, I can see the PWM pattern 'scroll' by WRT to the sync command very steadily. This is just the expected clock skew and no resyncing is observed.
#include<ADC.h>
#include<ADC_Module.h>
#include<i2c_t3.h>
#include<SPI.h>
#include<stdio.h>
//#include "filter_coefficients.h"
long int CPU_MARK;
long int CPU_CYCS;
char buf[20];
int sample_count=-1;
int tone_count=-1;
IntervalTimer piezoTimer;
IntervalTimer pingTimer;
IntervalTimer US_Tx_Timer;
//working
//reserve pin1 for future return channel
const int BEAT_ACK_PIN = 2;
const int RX_433_PIN = 0;
unsigned int byte_433=0;
int loop_count=0;
long int beat_count = 0;
int isr_count = 0;
unsigned long long command_buf=0;
unsigned long long command_long=0;
unsigned int command_byte=0;
unsigned int recieved_bytes=0;
int sampling_rate=200000; //ideally a multiple of both tone_freq and tone_freq+diff_freq
const int LUT_size=12500;//at least 1/32 of a sec long = 6250 samples
int COS_LUT[12500];
int tone_freq;
int tone_freqs[2]={6240, 2240};//must be a multiple of diff freq. set for targets =20.
int diff_freq=32;
int begin_count=0;
int ping_count=0;
int address_pin[4]={2,3,4,5};
unsigned long long target_address=0;
int US_Tx_PINs=4;
int US_Tx_PIN[]={23,22,14,15};
uint32_t US_Tx_Freq[]={40000,40032,39488,39456};
void setup() {
ARM_DEMCR |= ARM_DEMCR_TRCENA;
ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
for(int i=0;i<4;i++){
pinMode(address_pin,INPUT_PULLUP);
target_address=(target_address<<1)+digitalRead(address_pin);
}
pinMode(RX_433_PIN,INPUT);
pinMode(BEAT_ACK_PIN,OUTPUT);
for(int i=0;i<US_Tx_PINs;i++){
pinMode(US_Tx_PIN,OUTPUT);
analogWriteFrequency(US_Tx_PIN,US_Tx_Freq);
}
tone_freq=tone_freqs[digitalRead(address_pin[3])];
compute_LUT();
Serial.begin(9600);
Serial1.begin(4800);
sprintf(buf,"%08llX ",target_address);
Serial.println(buf);
Serial.println(tone_freq);
analogWriteResolution(8);
//digitalWrite(US_TX_PIN,1);
piezoTimer.begin(SAMPLE_ISR, 1000000/(sampling_rate));
US_Tx_Timer.begin(US_Tx_ISR, 1000000/80000);
for(int i=0;i<US_Tx_PINs;i++){
// analogWrite(US_Tx_PIN,128);
}
}
void US_Tx_ISR(void){
if(ping_count>0){
if(ping_count--==0){
digitalWrite(US_Tx_PIN[0],0);
}
else{
digitalWrite(US_Tx_PIN[0],ping_count%2);
}
}
if(tone_count>0){
if(tone_count--==0){
//stop tone
}
}
}
void SAMPLE_ISR(){
CPU_CYCS = ARM_DWT_CYCCNT-CPU_MARK;
CPU_MARK = ARM_DWT_CYCCNT;
//change this...
if (sample_count < LUT_size*100 && (sample_count!=-1)){ //~3 seconds
// analogWrite(DAC_PIN0,COS_LUT[(sample_count)%LUT_size]);
sample_count++;
}
}
void loop() {
loop_count++;
if(Serial1.available()){
while(Serial1.available()){
byte_433=(unsigned int)Serial1.read();
command_buf=command_buf<<8;
command_buf=command_buf+byte_433;
//Serial.println(byte_433);
command_byte++;
recieved_bytes++;
}
if(command_buf == (0xFFFFFFFFFFFFFFFF)){//start byte
command_byte=0;
command_long=command_buf;
digitalWrite(BEAT_ACK_PIN,HIGH);
begin_count++;
}
}
if(command_byte>7) {
digitalWrite(BEAT_ACK_PIN,HIGH);
//process command
if(command_buf!=0x00){
sprintf(buf,"%08llX ",command_buf);
// Serial.println(buf);
if((command_buf & 0x00000000000000FF) == target_address){ //found this target's address...
beat_count++;
if((command_buf & 0x000000000000FF00) == 0x0000000000000000){ //resync CW
tone_count=3*80000;
//PWM
for(int i=0;i<US_Tx_PINs;i++){
pinMode(US_Tx_PIN,INPUT);///how to make the timer reset???
pinMode(US_Tx_PIN,OUTPUT);
analogWrite(US_Tx_PIN,128);
analogWriteFrequency(US_Tx_PIN,US_Tx_Freq);
}
Serial.println('Y');
}
else if((command_buf & 0x000000000000FF00) == 0x0000000000000100){ //stop CW
sample_count=-1;
}
else if((command_buf & 0x000000000000FF00) == 0x0000000000001000){ //begin ping
Serial.println('X');
//tone(US_Tx_PIN[0],40000,1); //this line is not working.
sample_count=-1;
ping_count=20;
}
command_long=command_buf;
}
}
command_byte=0;
}
else{
digitalWrite(BEAT_ACK_PIN,LOW);
}
if(loop_count % 500000 ==0){
Serial.print(begin_count);
Serial.print(' ');
Serial.print(beat_count);
Serial.print(' ');
sprintf(buf,"%08llX ",command_long);
Serial.print(buf);
//Serial.print("%llux",command_long);
Serial.print(' ');
Serial.println(beat_count);
}
}
void compute_LUT(){
for(int i=0;i<LUT_size;i++){
//COS_LUT=2047+floor(1024*cos(2*PI*i*(tone_freq)/sampling_rate))+floor(1024*cos(2*PI*i*(tone_freq)/sampling_rate));
COS_LUT=1600+floor(350*cos(2*PI*i*(tone_freqs[0])/sampling_rate))+
floor(350*cos(2*PI*i*(tone_freqs[0]+diff_freq)/sampling_rate))+
floor(350*cos(2*PI*i*(tone_freqs[1])/sampling_rate))+
floor(350*cos(2*PI*i*(tone_freqs[1]+diff_freq)/sampling_rate));
//COS_LUT=2047+floor(2047*cos(2*PI*i*6880/44100));
}
}
unsigned int interpolate(int val){
int inval=val;
int outval;
outval=0;
for(int i=0;i<16;i++){
outval=outval+((inval & 0x00000001)<<(2*i));
outval=outval+((inval & 0x00000001)<<(2*i+1));
inval=inval>>1;
}
return outval;
}