chipKIT® Development Platform

Inspired by Arduino™

CoreTimerService

Created Sun, 06 May 2012 08:53:17 +0000 by radiosky


radiosky

Sun, 06 May 2012 08:53:17 +0000

I need very fast interrupts in microseconds range. The wiki below describes functions to achieve this, but I have not been able to find other information. Anyone have examples of how to use these high res timers? I would think that they would be mentioned commonly but I only am finding hardware pin interrupts.

[url]http://www.chipkit.cc/wiki/index.php?title=Documentation[/url]

Thanks


EmbeddedMan

Sun, 06 May 2012 16:15:28 +0000

radiosky,

Yes, you've found a new feature in recent MPIDE test builds that's so new, there aren't really many good examples of it yet! Gene Apperson and Keith Vogel from Digilent are adding so many cool new features to the MPIDE core files that it's hard for us to keep up with them. But this new core timer service is my absolutely favorite feature that's been added recently. It's really awesome, and is very robust (thanks to some great code reviews by several members of this forum as well as Gene and Keith).

So here's a really quick and simple example I whipped up:

/* CoreTimer demo1 : demonstrates a simple callback scheduled at
a single frequency (10KHz). This example code is in the public domain. */

void setup() {
  pinMode(4, OUTPUT); // Use IO pin 4 to show operation of callback
  attachCoreTimerService(MyCallback);
}

void loop() {
  // We don't do anything in the main loop
}

// For the core timer callback, just toggle the output high and low
// and schedule us for another 100uS in the future. CORE_TICK_RATE
// is the number of core timer counts in 1 millisecond. So if we 
// want this callback to be called every 100uS, we just divide 
// the CORE_TICK_RATE by 10, and add it to the current time.
uint32_t MyCallback(uint32_t currentTime) {
  digitalWrite(4, HIGH);
  digitalWrite(4, LOW);
  return (currentTime + CORE_TICK_RATE/10);
}

All it does is creates a new callback, which then gets registered with the core timer service. This callback gets called, and must return a new uint32 value that is the time, in the future, that it next wants to get called. The units are 1/40MHz seconds, and we use the CORE_TICK_RATE constant as an easy way to figure out when we want to be scheduled to run in the future. There are CORE_TICK_RATE counts in 1 millisecond. We set a pin high and then low inside our callback. On my scope, I can see pin 4 pulse every 100uS. You can obviously update the value returned by the callback to whatever value you want, and the core timer service will attempt to schedule the callback to be called at that time in the future.

Is this enough to get you started? I can provide other examples if you suggest to me what you'd like to see.

*Brian


radiosky

Sun, 06 May 2012 23:18:40 +0000

Thanks Brian, that is extremely helpful. What I am doing is a data collection program that sequentially tunes a DDS controlled radio and measures the output, a spectrometer of sorts. The data will be passed via Ethernet to the PC. I have done this using a PIC 16F873 and a serial connection in assembler but now we need more speed.

You have no main loop in the program but I will need one I believe. I would like to send a buffer of 200 to 1600 16 bit samples back to the PC each time I loop through all of the frequencies. So I guess a main loop with the EThernet communications (TCP server) would watch for PC commands and send a buffers when full. It would have to send that buffer in less than the sampling period.

Now I need to figure out how to get the TCP server to send a buffer of 2 byte integers (the ADC reading). But I should put that query in another thread.


RobertCarlRice

Tue, 08 May 2012 07:00:19 +0000

I have Timer 2 interrupts working, but I would like to try using attachCoreTimerService. Where is it defined? I get attachCoreTimerService is not defined in this scope.

Thanks, Bob Rice


EmbeddedMan

Tue, 08 May 2012 18:36:17 +0000

Bob,

Are you using the very latest test build? This code is not in any of the released versions yet, just the test builds.

*Brian


RobertCarlRice

Tue, 08 May 2012 20:11:47 +0000

Hi Brian,

Thanks. I have upgraded to the latest test release and attachCoreTimerService is now defined.

I assume that the CoreTimerService runs at a high priority. I may need a low priority timer to not interfere with serial communications.

Has anyone used the non-blocking wire library with the Uno32? I want to use the Centipede shield in my project but my pin timing is critical so I want to be able to start an I2C transfer from my timer interrupt. The standard (blocking) wire library works in loop() but kills my timer 2 interrupts when I try to initiate a transfer from the isr.

Thanks, Bob Rice