#avr Logs

Feb 07 2021

#avr Calendar

04:34 PM joakimk: So, I've got Timer1 up and running on an ATMEGA88, in CTC mode (I believe), and I'm trying to do the same with Timer0. But I just can't get Timer0 to "respond" properly: https://pastebin.com/9g22P672
04:41 PM LeoNerd: timer0 and timer1 are quite different
04:41 PM joakimk: yes... 8 and 16 bit?
04:41 PM LeoNerd: Honestly, once you get used to the newer chips (ATmega 0-series, ATtiny 1-series) you realise how much of a stupid ugly pain the older ones are
04:41 PM LeoNerd: No feature moreso than the timers
04:41 PM joakimk: thanks for the tip:)  However, with this hardware I've got, what I want is to have Timer0 trigger the ISR at some fixed interval -- e.g. every 100ms
04:43 PM joakimk: The ISR should "send" (write) a UDP package... That's surlely going to take some time, right? So how often would something like that make sense to do? Maybe 100ms is WAY to rapid?
04:44 PM joakimk: I mean, what happens if the ISR is set up to go every 100ms, but requires 1s to finish?
04:44 PM LeoNerd: I'd suggest not doing that ;)
04:44 PM joakimk: ah -- cli will prevent the other 10
04:44 PM LeoNerd: ISRs really ought to return very quickly
04:44 PM LeoNerd: Personally I basically just set task flags for my scheduler, which then runs the actual functions outside of an ISR context
04:45 PM joakimk: but you have the main thread (loop), and the ISRs... What else is there?
04:45 PM LeoNerd: ?
04:46 PM joakimk: I mean, what other "threads" of execution can run anything, other than main() or an ISR?
04:46 PM joakimk: what's the scheduler, as you say? :)
04:47 PM LeoNerd: My scheduler is basically a little while(1) loop that iterates over a list of tasks (stored in a static array). each task has a function pointer and a runnable flag
04:47 PM LeoNerd: ISRs basically just set the flag. scheduler then wakes up after interrupt, runs the runnable tasks (in order), then goes back to sleep awaiting more interrupts
04:47 PM LeoNerd: Fairly standard stuff ;)
04:48 PM joakimk: so the scheduler is the main while(1) loop?
04:49 PM LeoNerd: yah
04:49 PM joakimk: I mean, AVRs do some setup (timers etc), and then go into some eternal loop - - which typically is 99% idle
04:49 PM LeoNerd: Yup
04:50 PM LeoNerd: That's basically what I do. A per-project file of mine never actually defines a main() function. I just link in my usual scheduler
04:50 PM joakimk: so what you're saying is, have the ISR for timer0 set some (volatile) flag, which the main loop checks -- every now and then -- (say in between sleep(50) or something)
04:50 PM LeoNerd: The toplevel .c file probably defines a setup(), and a bunch of tasks which sit in a toplevel array called tasks[]
04:50 PM LeoNerd: Not between sleeps, no. that would be very silly
04:50 PM LeoNerd: Between "waiting for interrupts"
04:52 PM joakimk: do you have some links to some more info on this?
04:52 PM LeoNerd: No
04:52 PM joakimk: I don't see how main "waits for interrupts"
04:53 PM LeoNerd: by sleeping
04:53 PM joakimk: so.. while(1) {  sleep(50); if (doWorkA) { doWorkA(); }  }
04:53 PM LeoNerd: ... no
04:53 PM joakimk: something like that
04:53 PM LeoNerd: sleep()
04:53 PM LeoNerd: The real sleep
04:53 PM LeoNerd: The CPU instruction that says "wait for interrupts"
04:53 PM LeoNerd: and powers down the CPU
04:53 PM LeoNerd: and savess battery power
04:54 PM LeoNerd: Not that stupid busy-wait for some microseconds nonsesnse
04:54 PM joakimk: I see...
04:54 PM joakimk: so not `_delay_ms()`
04:55 PM joakimk: but, replacing the "sleep" method in the above pseudo code, something slightly in that direction then?
04:57 PM LeoNerd: https://paste.debian.net/1184459/ -- here is my actual sched.c
04:57 PM LeoNerd: It's long and full of lots of "try to work on like 20 different AVR chips" portability hacks, and lots of features I use only in one or two projects
04:58 PM LeoNerd: lines 156 onwards are the main core of the thing
04:59 PM joakimk: thanks! I see the sleep_cpu in the ppoll method
05:00 PM LeoNerd: Yup
05:00 PM LeoNerd: Ohright, yes it's called sleep_cpu() despite that the actual AVR instruction is just `SLEEP`
05:04 PM joakimk: did you have a chance to look at my paste though?
05:05 PM LeoNerd: I didn't
05:07 PM joakimk: well, I won't take up more of your time:)  Thanks for the advice on the scheduler design! I'll try some light-weight version of that (using busy-wait) and send the UDP package from the main loop/thread, rather than within the ISR
05:08 PM joakimk: (this also means I could just use Timer1 to "trigger" the UDP packages, as well as blinking the LEDs) ;)
05:09 PM joakimk: it's (maybe) similar to what you said: Use one timer to trigger two (or more) tasks
05:09 PM LeoNerd: Yeah.. I usually end up with a ticking timer that counts some small time interval and maintain my own counters on tasks
05:09 PM LeoNerd: my task interface has delay slots
05:09 PM joakimk: very nice :)
05:09 PM LeoNerd: the timer ISR just decrements all the ticks if they're above zero,a nd if they become zero will schedule the task to run
05:10 PM LeoNerd: So you can have an e.g. once every second task by just rescheduling itself in 1000msec, at the start
05:10 PM joakimk: yeah! this actually solves my need. But it sure bugs me that I wasn't able to figure out the setup for timer0;)  I'll have another look at that some other day
05:11 PM LeoNerd: timer0 is weeeird
05:11 PM LeoNerd: Across many different AVR chips, its design often differs here and there, different features on different chips
05:11 PM LeoNerd: It's generally horrible
05:12 PM LeoNerd: This is why I like the new chips. On every single mega0 or tiny1, every instance of the same type of timer is identical. They /all/ have an RTC, a TCA and a TCB. Same across every chips
05:15 PM joakimk: maybe I should've used Timer2 then? But I think I'll be fine just sticking everything in Timer1 :)
05:16 PM LeoNerd: Well, on the new chips I use RTC for the internal "hidden" scheduler timer, because that's what it's for. It doesn't have an IO pins
05:16 PM LeoNerd: That leaves TCA free for PWM [because it has up to 6 output channels], or TCB for signal measuring [because it has lots of input measurement modes]
05:16 PM LeoNerd: Overall they're just much nicer chips. Did I say that already? ;)
05:21 PM joakimk: hehe :)
05:22 PM joakimk: so far I've played around with my old chips, and my STK500. Maybe time to upgrade...
05:23 PM joakimk: but this is all only fun little hobby projects
05:24 PM joakimk: right, thanks again!:)  Got to go
05:57 PM vmt: what? talk?!
05:58 PM LeoNerd: I was bored and waiting for my unit tests to run :P
11:39 PM fengshaun_ is now known as fengshaun