Hello,
In my project I need my Portenta to generate a 20 MHz clock for a stepper motor controller.
I tried using the library from khoi_prog PortentaH7_TimerInterrupt but with this library you cannot generate signals higher than 100 kHz.
I tried modifying the library but it seems like the call to the ISR is not really compatible with the frequency I want to reach, when modifying the value at which the interrupt happens, the portenta gets stuck somewhere in the code.
From the portenta complete pinout it seems like some timers channel can generate signals on some pins (TIM1_CH2 on pin D14, TIM1_CH3 on pin D13).
I then tried to setup the timers so that they would toggle the pins at every interrupts but it doesn't seem to work.
Here is my code:
#define CLK16_PIN D14 //CLK16_PIN Timer pin
void setup()
{
Serial.begin(115200); //init serial port and set baudrate
while(!Serial); //wait for serial port to connect (needed for Leonardo only)
Serial.println("\nStart...");
//set pins
pinMode(CLK16_PIN,OUTPUT);
pinMode(D13,OUTPUT);
TIM1->SR = 0; //Clear flags of status register
TIM1->PSC = 0; //Set prescaler to 1
TIM1->ARR = 1000; //Interrupt every 1000 clock cycle -> 200kHz
TIM1->DIER |= 0x000f;//Enable interrupt on channels 1,2,3
//TIM1->EGR |= 0x06;//Generate an interrupt on channel 1 and 2
/*
TIM1->CCMR1 |= TIM_CCMR1_OC1M_0 | TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2;
TIM1->CCR1 = TIM1->ARR / 2;
TIM1->CCER |= TIM_CCER_CC1E;*/
TIM1->CCMR1 |= 0x3030;//Output compare on toggle, should toggle the pin for channel 1 and 2
TIM1->CCMR2 |= 0x0030;//toggle output channel 3
TIM1->CCER |= 0x0111; //Enable channels 1-3
TIM1->CCR1 |= 10; //Not sure what those registers do, seems like they also determine the freq of the interrupt
TIM1->CCR2 |= 1000;
TIM1->CCR3 |= 100;
TIM1->CR1 |= TIM_CR1_CEN; //Enable the timer
Serial.println("Setup end");
}
void loop()
{
digitalWrite(LEDR,HIGH);
delay(500);
digitalWrite(LEDR,LOW);
delay(500);
}
I am checking the pins with a logic analyzer, but it doesn't seem to produce any signals.
Other "strange" facts. From the PortentaH7_TimerInterrupt it seems that my portenta is running at 200MHz. The timer also seem to operate at this frequency, from the result I obtained by setting the ARR value directly and checking the signal.
Any help is appreciated.