esp32c3 timer registers

Nikita71
Posts: 5
Joined: Thu May 27, 2021 7:08 pm

esp32c3 timer registers

Postby Nikita71 » Fri May 05, 2023 8:01 am

Hello! I want to create a project where I need to use timer 0 to count an unlimited number of bars (time) and vice versa to count down a certain amount of time with a interupt at the end. these regimes must change quite quickly and often.

tried to do it through the idf function, but it didn't work properly, so I switched to registers.

I managed to start the timer itself, but now I stopped at 2 problems:

https://anonymfile.com/eRBZO/photo-2023 ... -49-25.jpg
Problem number 1, I set the timer to reset after 4,000,000 years, but for some reason this does not happen ...

https://anonymfile.com/59WLW/photo-2023 ... -49-30.jpg
Problem number 2 is more important, I still don't know where to add the handler interrupt

Code: Select all

#include <Wire.h>
#include <U8g2lib.h>
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g(U8G2_R0, U8X8_PIN_NONE, 6, 7);


#define TIMER0_BASE_ADDR 0x6001F000
volatile uint32_t TIMG_T0LO_REG, TIMG_T0HI_REG;
uint64_t TIMG_T0_REG;

void IRAM_ATTR timer_group0_isr() { 

}

void setup() {

  u8g.begin();
  u8g.enableUTF8Print();
  u8g.setFont(u8g2_font_unifont_t_cyrillic);
  u8g.setFontDirection(0);
  
  *(volatile uint32_t *)(TIMER0_BASE_ADDR + 13) = 1; // Configure the prescaler 40MHz
  *(volatile uint32_t *) TIMER0_BASE_ADDR |= (1 << 30); // Count up
  *(volatile uint32_t *) TIMER0_BASE_ADDR |= (1 << 29); // AUTORELOAD EN

  /* reload value */
  *(volatile uint32_t *)(TIMER0_BASE_ADDR + 0x0018) = 4000000; // TIMG_T0_LOAD 32 bit
  *(volatile uint32_t *)(TIMER0_BASE_ADDR + 0x001C) = 0; // TIMG_T0LOADHI_REG 22 bit
  /* alarm value */
  *(volatile uint32_t *)(TIMER0_BASE_ADDR + 0x0010) = 30000; // TIMG_T0ALARMLO_REG 32 bit
  *(volatile uint32_t *)(TIMER0_BASE_ADDR + 0x0014) = 0; // TIMG_T0ALARMHI_REG 22 bit


  *(volatile uint32_t *) TIMER0_BASE_ADDR |= (1 << 10); // Alarm enable
  
  *(volatile uint32_t *)(TIMER0_BASE_ADDR + 0x0070)  |= (1 << 0); // enable TIMG_T0_INT_ENA interrupt
  *(volatile uint32_t *)(TIMER0_BASE_ADDR + 0x0078)  |= (1 << 0); // masked TIMG_T0_INT interrupt

 // *(volatile uint32_t *)(TIMER0_BASE_ADDR + 0x007C) |= (0 << 0); // Clear the interrupt

  *(volatile uint32_t *)(TIMER0_BASE_ADDR + 0x0020) = 1; // Timer 0 time-base counter reload.
  *(volatile uint32_t *) TIMER0_BASE_ADDR |= (1 << 31); // Enable Timer 0
  

}
void loop() {

  u8g.clearBuffer();

  *(volatile uint32_t *)(TIMER0_BASE_ADDR + 0x000C) |= (1 << 30); // UPDATE 
  TIMG_T0LO_REG = *(volatile uint32_t *)(TIMER0_BASE_ADDR + 0x0004); // ticks LO_REG
  TIMG_T0HI_REG = *(volatile uint32_t *)(TIMER0_BASE_ADDR + 0x0008); // ticks HI_REG
  TIMG_T0_REG = (uint64_t)TIMG_T0HI_REG << 32 | TIMG_T0LO_REG; // 32 + 22 = 54 bit

  u8g.setCursor(35, 20);
  u8g.print(TIMG_T0_REG);   
  u8g.sendBuffer();
  
    
  delay(1000);

}



ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: esp32c3 timer registers

Postby ESP_Sprite » Fri May 05, 2023 8:38 am

Let's start from the beginning: why didn't the ESP-IDF (or Arduino, for that matter) drivers work for you?

Nikita71
Posts: 5
Joined: Thu May 27, 2021 7:08 pm

Re: esp32c3 timer registers

Postby Nikita71 » Fri May 05, 2023 11:02 am

I think that after an interrupt occurs, the timer value is not reset to 0, but the alarm value will simply increase, and I really don't like this work logic. tried to manually reset the value to 0 through the registers, and then set the new value, but it did not work.

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: esp32c3 timer registers

Postby ESP_Sprite » Sun May 07, 2023 8:09 am

Why do you think that? As clarified in the docs, when a timer triggers an interrupt by means of an alarm, it can automatically reload the timer to a set value. That value also can be zero.

Who is online

Users browsing this forum: No registered users and 59 guests