Timer and interrupt

Hi everyone. I did write time handler function, direct mode, and test it out by toggling two led inside the timer interrupt handler. the pins got toggled. however, it misbehaves and the time between the toggle is not constant. Also, when I debug my code, and before executing any instruction. I found that the timer is enabled in mie and mcause had the code of time interrupt,0x80000007. I was hoping if anyone could help me and check my code. Thank you very much.

I wonder if it’s because of overflow: mtime and mtimecmp are both 64 bit registers. You’d need to read mtimeh and set mtimecmph as well.

This can get tricky since it takes two instructions to read/set these and the 32bit portion may overflow while you do so, or trigger the timer interrupt with the intermediate value for mtimecmp

I think the recommended procedure it to:

Read mtimeh
Read mtime
Read mtimeh again
Re-run if mtimeh is not equal to previously read value.

Then for setting mtimecmp:
Set mtimecmph to 0xFFFFFFFF (maximum time)
Set mtimecmp to low word of desired time
Set mtimecmph to high word of desired time

1 Like
1 Like

thanks very much. the steps you said it does make sense and I will use it. However, my issue happen only if I wrote any code inside the while loop. Otherwise, if there is nothing inside the while loop the timer work more than fine. it took me along time to figure that. however, I still do not know where is the issue. Thanks very much again and hopefully you can give it a second look.

good evening, I used the code that you post and I did not find the micro so I wrote them. hopefully, I did it the right way. however, the timer issue still exists. and I noticed that the issue only exists if I wrote code inside the while(1), otherwise it works more than fine. I hope you can give my code a second look. Thank you very much.

Good after noon every one. I would like to mention that my code working fine now after I wrote the instruction ( asm volatile (“wfi”); ) after enabling the interrupt. hopefully this will be helpful for anyone who is checking this conversation. I am not totally sure what is the important of this instruction but I will search it. if any one have a good explanation, please enlighten me. Thanks very much!

1 Like

I guess that makes sense, as the wfi instructions should stall the CPU at that point. I wonder if the inconsistent timing behavior is because what’s in ASSIGN_PIN takes more than one instruction? As in it could be partially executing ASSIGN_PIN, then interrupt fires, toggles the pin states, but then on return from the interrupt the ASSIGN_PIN work finishes and re-enables the pin before the toggled state can be seen.

I’d be curious if you removed the wfi and moved the ASSIGN_PIN outside of the while loop (effectively making it a busy loop instead) if that would “work” as well (but, wfi seems better from a power perspective at least).

Edit: one of the mistakes was doing is if(mcause==0x80000007) and assuming that the reserved bit always 0. Also, writing to the whole register mstatus. After, I fixed these two issues the code started to work fine without using WFI instruction. so far a lot of problems are solved. now I will try to figure out why the timer interrupt does not work if I enabled mie_MEIE, machine interrupt bit in mie register :slight_smile: :thinking:

1 Like