Does FemtoRV32 include mtime and mtimecmp? #117
Replies: 8 comments 6 replies
-
Great to see a FreeRTOS port for the FemtoRV! The issue you are encountering is that the mtime and mtimecmp CSRs are not implemented in FemtoRV, for timekeeping, only cycles and cyclesh are available: |
Beta Was this translation helpful? Give feedback.
-
I think FreeRTOS support is a very solid enhancement for the tutorial; time to reach out to @BrunoLevy if he is interested in officially adding a FreeRTOS chapter in the learn-fpga coursework along with the necessary logic! |
Beta Was this translation helpful? Give feedback.
-
Hello @jimmylu890303 and @Mecrisp, yes it will be a fantastic addition to the tutorial series !
For the tutorial, I'd like to continue with the "step by step incremental modification" approach of episode I, so the "scenario" could be something like (first draft):
|
Beta Was this translation helpful? Give feedback.
-
As far as I know, the flow of handling interrupts in RISC-V is as follows. When a trap occurs, the flow of handling interrupts in RISC-V
In the case of gracilis, it only supports mepc, mtvec, mstatus, and mcause.Moreover, the implementation of mstatus and mcause is too simple. Both of them only have one bit, but in the spec, there are 32 bits, and each bit represents a different meaning. So, the first thing I think we need to do is to implement complete Control and Status Registers (CSRs) |
Beta Was this translation helpful? Give feedback.
-
The second thing I think we need to do is to implement a CLINT module. I have referenced the srv32 project, and CLINT is responsible for pending interrupts to the processor. It has mtime and mtimecmp registers. The provided code is part of the CLINT module in the srv32 project.
|
Beta Was this translation helpful? Give feedback.
-
Third thing I think we need to do is to implement a complete interrupt mechanism. When an interrupt request is pending, the core should perform different operations based on the type of interrupt. The provided code is part of the RISC-V core in the srv32 project. In the core design, it is evident that different actions are taken based on the type of interrupt. For instance, if a software interrupt (sw_irq) occurs, the mcause register will be updated to INT_MSI. Handling this type of interrupt involves performing specific operations with CSR (Control and Status Register) registers. The code demonstrates the logic and behavior associated with interrupt handling in the RISC-V core within the srv32 project.
|
Beta Was this translation helpful? Give feedback.
-
And then, I believe that porting FreeRTOS on FemtoRV could work correctly. |
Beta Was this translation helpful? Give feedback.
-
You are embarking on the route to full support of everything, and who am I to stop you! But before you set sails, I want to give you a hint. You came to FemtoRV in the first place probably because Bruno and me extracted what is necessary to understand, and presented it in a -hopefully- readable and concise way. There are other cores that are much better in full support of everything! First, think of what FreeRTOS absolutely needs. And then how to minimally implement it. Start with that. As far as I understand, FreeRTOS wants a 64 bit register that counts every clock cycle, and another 64 bit register that compares to that. Interrupt triggers when counter is greater or equal to the compare value. Now on to how FemtoRV interrupts work. They trigger an interrupt response when they see the interrupt line being high for one clock cycle, even if the CPU already is in an interrupt handler at that moment. With a cycle counter incrementing every clock cycle, iff the timer is the only interrupt source, you can go with this: If counter and compare are equal, pulse interrupt line. You can directly wire a test for equality into the interrupt wire. If compare register is written and the new value is greater or equal to counter register, pulse interrupt line also. OR both together (and route it through a flipflop because of critical path). This way you get the full "greater or equal" behaviour which FreeRTOS expects with the one-cycle-pulsed interrupt of the FemtoRV. There you go. Two registers in IO space, and wiring it to the interrupt line. Done. FreeRTOS should work. For writing the 64 bit compare register atomically, I recommend that you have a look which part is written first, store that in a temporary 32 bit shadow register, and upon writing the other half, the complete 64 bit compare register is updated at once. This way you circumvent triggering interrupts that might occur when writing high and low 32 bits separately. Similar is possible for read access -- when reading the first half of the counter, freeze the other half in a shadow register in logic and deliver this later. |
Beta Was this translation helpful? Give feedback.
-
I am currently learning how to port FreeRTOS on FemtoRV, and I am referencing the FreeRTOS port on the srv32 repository. I have successfully run FreeRTOS on FemtoRV, but encountered some issues. Specifically, the
vTaskStartScheduler
method in FreeRTOS isn't behaving as expected. I suspect this is due to incorrect configuration of configMTIME_BASE_ADDRESS and configMTIMECMP_BASE_ADDRESS. In order to resolve this, I am trying to determine the correct address of mtime on FemtoRV. Unfortunately, I have been unable to find this information on my own, so I am seeking assistance. Thank you.The code above corresponds to
vPortSetupTimerInterrupt
in FreeRTOS. I believe that the issue might be related to the vPortSetupTimerInterrupt function not working correctly. Consequently, vTaskStartScheduler is also affected and not functioning as intended.Beta Was this translation helpful? Give feedback.
All reactions