enjoy-digital / litex

Build your hardware, easily!
Other
2.89k stars 556 forks source link

Litex timer settings are not working correctly #1711

Closed fdsgdshn closed 1 year ago

fdsgdshn commented 1 year ago

"Hello, I'm trying to implement a timer application on the Litex board. For this purpose, I'm using the Litex VexRiscv processor. It is defined in the following way in the platform file of Litex: https://github.com/enjoy-digital/litex/blob/master/litex/soc/cores/timer.py

"To use the Timer in Periodic mode, the user needs to:

Disable the Timer Set the 'load' register to 0 Set the 'reload' register to the expected period Enable the Timer " Despite applying these steps, the timer is not working. Can you help me? Thank you very much. Here is my code:

const uint32_t frequency = 100000000; // 100 MHz
const uint32_t period = 5 * frequency; // 5 seconds

// in Main function // Configure the LED control register *LED_CTRL_ADDR = 0; // Turn off the LED initially

// Configure the timer
*TIMER_EN = 0; // Disable the timer
*TIMER_LOAD = 0; 
*TIMER_RELOAD = period; // Set the reload register to the desired period
*TIMER_EN_ = 1; // Enable the timer

while (1) {
    // Wait until the timer reaches zero
    while (*TIMER_VALUE!= 0) {
        // Wait
    }

    // Toggle the LED control register
    *LED_CTRL_ADDR ^= 1;
}

Despite implementing this, the timer is not functioning. How to use timers in Litex? Can you assist me?

Thanks

JamesTimothyMeech commented 1 year ago

This example of using the timers has worked for me https://github.com/BrunoLevy/learn-fpga/discussions/105

fdsgdshn commented 1 year ago

This example of using the timers has worked for me BrunoLevy/learn-fpga#105

@JamesTimothyMeech Thank you, it works! Do you have an example related to the usage of timer0_interrupt in LiteX? I have searched extensively, but unfortunately, I couldn't find any. I have created a code block in the following way, but it's not working as expected. Thank you for your help and support!

void init_interrupts(void) { unsigned int mask = irq_getmask(); mask |= (1<< TIMER0_INTERRUPT); irq_setmask(mask);

unsigned int irqs = irq_pending() & irq_getmask();

if (irqs & (1 << TIMER0_INTERRUPT))
    timer0_isr();

}

int main(void) { // Initialize interrupts init_interrupts(); ...

}

JamesTimothyMeech commented 1 year ago

Unfortunately I haven't used timer interrupts but there could be some clues here: https://github.com/enjoy-digital/litex/blob/c58f46bb7981b935f44d1e01fc92623492ee1a15/litex/soc/interconnect/csr_eventmanager.py#L22

You can also look in the LiteX generated soc.h and csr.h files to see what LiteX generated functionality you have access to. Apologies I can't be more helpful but I am still learning about this myself.