I am cascading a QTimer into a 64-bit counter, and it works. But I am having a strange thing happen on startup, that I would like to understand.
I am posting the full code at the end, but the most important thing are the the CNTR initializations:
Using there, here is what I get from the serial monitor:
so, at first the values are bad, then they turn fine and work as intended. I can also just add a 100ms delay after the timer setup and then the values are ok in the main program.
If I set all CNTR to 0, then the 3rd and 4th values start out at 0 as expected, but then change to 1, like this:
I guess a carry over bit needs to be specified or something...
The 0xffff and wait a few ms workaround functions, but I do want to understand what is going on though, so does anyone here know?
I am posting the full code at the end, but the most important thing are the the CNTR initializations:
Code:
TMRx->CH[0].CNTR = 0; // set count to 0
TMRx->CH[1].CNTR = 0; // set count to 0
TMRx->CH[2].CNTR = 0xffff; // set count to 0xffff
TMRx->CH[3].CNTR = 0; // set count to 0
Code:
0
0
65535
0
17897
49
0
0
so, at first the values are bad, then they turn fine and work as intended. I can also just add a 100ms delay after the timer setup and then the values are ok in the main program.
If I set all CNTR to 0, then the 3rd and 4th values start out at 0 as expected, but then change to 1, like this:
Code:
0
0
0
0
3397
49
1
1
I guess a carry over bit needs to be specified or something...
The 0xffff and wait a few ms workaround functions, but I do want to understand what is going on though, so does anyone here know?
Code:
#include <U8x8lib.h>
U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/* reset=*/U8X8_PIN_NONE);
IMXRT_TMR_t * TMRx = (IMXRT_TMR_t *)&IMXRT_TMR4;
void setup()
{
Serial.begin(9600);
// initialize and clear display
u8x8.begin();
u8x8.setPowerSave(0);
u8x8.setFont(u8x8_font_chroma48medium8_r);
u8x8.drawString(0, 0, "Started !");
CCM_CCGR6 |= CCM_CCGR6_QTIMER4(CCM_CCGR_ON); // enable QTMR4
IOMUXC_SW_MUX_CTL_PAD_GPIO_B0_11 = 1; // QT4 Timer2 on pin 9
TMRx->CH[0].CTRL =0; // stop
TMRx->CH[1].CTRL =0; // stop
TMRx->CH[2].CTRL =0; // stop
TMRx->CH[3].CTRL =0; // stop
TMRx->CH[0].CNTR = 0; // set count to 0
TMRx->CH[1].CNTR = 0; // set count to 0
TMRx->CH[2].CNTR = 0xffff; // set count to 0xffff
TMRx->CH[3].CNTR = 0; // set count to 0
TMRx->CH[0].SCTRL = 0; // clear all overflow etc flags
TMRx->CH[1].SCTRL = 0; // clear all overflow etc flags
TMRx->CH[2].SCTRL = 0; // clear all overflow etc flags
TMRx->CH[3].SCTRL = 0; // clear all overflow etc flags
TMRx->CH[3].CTRL = TMR_CTRL_CM (7); // Count Mode: Cascaded counter mode
TMRx->CH[3].CTRL |= TMR_CTRL_PCS(6); // Primary Count Source: CH[2] output
TMRx->CH[2].CTRL = TMR_CTRL_CM (7); // Count Mode: Cascaded counter mode
TMRx->CH[2].CTRL |= TMR_CTRL_PCS(5); // Primary Count Source: CH[1] output
TMRx->CH[1].CTRL = TMR_CTRL_CM (7); // Count Mode: Cascaded counter mode
TMRx->CH[1].CTRL |= TMR_CTRL_PCS(4); // Primary Count Source: CH[0] output
TMRx->CH[0].CTRL = TMR_CTRL_CM (1); // Count Mode: Count rising edges of primary source
TMRx->CH[0].CTRL |= TMR_CTRL_PCS(2); // Primary Count Source: Counter 2 input pin
//delay(100); // settle timer???
}
char buffer[100];
void loop()
{
sprintf(buffer, "%u", TMRx->CH[0].CNTR); // regular read of CH CNTR causes HOLD registers to be set with corresponding values of other channels;
u8x8.drawString(0, 2, " ");
u8x8.drawString(0, 2, buffer);
Serial.println(buffer);
sprintf(buffer, "%u", TMRx->CH[1].HOLD); // regular read of CH CNTR causes HOLD registers to be set with corresponding values of other channels;
u8x8.drawString(0, 3, " ");
u8x8.drawString(0, 3, buffer);
Serial.println(buffer);
sprintf(buffer, "%u", TMRx->CH[2].HOLD); // regular read of CH CNTR causes HOLD registers to be set with corresponding values of other channels;
u8x8.drawString(0, 4, " ");
u8x8.drawString(0, 4, buffer);
Serial.println(buffer);
sprintf(buffer, "%u", TMRx->CH[3].HOLD); // regular read of CH CNTR causes HOLD registers to be set with corresponding values of other channels;
u8x8.drawString(0, 5, " ");
u8x8.drawString(0, 5, buffer);
Serial.println(buffer);
Serial.println();
delay(1000);
}