Close

The Timer

A project log for Setting up the LinkIt C cross-compiler

Are you a "dyed in the wool" C programmer. Love your Arduino but want something more powerful. The LinkIt Smart MT7688 board may for you!

agpcooperagp.cooper 10/29/2017 at 02:490 Comments

The Timer

The MT7688 has a timer but I cannot find documentation how to access it (even if that is appropriate). So I have written a simple timer (uTime.c) using <signal.h> and <sys/time.h>:

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>

void timer_handler (int signum) {
  static int count = 0;
  printf ("timer expired %d times\n", ++count);
}

int main () {
  struct sigaction sa;
  struct itimerval timer;

  // Install timer_handler as the signal handler for SIGVTALRM
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = &timer_handler;
  sigaction (SIGVTALRM, &sa, NULL);

  // Configure the timer to expire after 1 second
  timer.it_value.tv_sec = 1;
  timer.it_value.tv_usec = 0;
  // and every 1 second after that
  timer.it_interval.tv_sec = 1;
  timer.it_interval.tv_usec = 0;
  // Start a virtual timer
  //   It counts down whenever this process is executing
  setitimer (ITIMER_VIRTUAL, &timer, NULL);

  // Do something that can be interrupted
  while (1);
 }

The code should be good for microsecond timing but I have not tested it.

Example Run

alanx@alanx ~/LinkIt-cc/uTimer $ make

/home/alanx/LinkIt-cc/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-uclibc-gcc -I. -I/home/alanx/LinkIt-cc/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/usr/include -I/home/alanx/LinkIt-cc/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/include -o uTimer.run uTimer.c -L. -L/home/alanx/LinkIt-cc/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/usr/lib -L/home/alanx/LinkIt-cc/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/lib -lmraa -pthread -ldl

alanx@alanx ~/LinkIt-cc/uTimer $ ls -lah

total 28K
drwxrwxr-x 2 alanx alanx 4.0K Oct 29 09:53 .
drwxr-xr-x 8 alanx alanx 4.0K Oct 29 09:02 ..
-rw-rw-r-- 1 alanx alanx  829 Oct 29 09:03 Makefile
-rw-rw-r-- 1 alanx alanx  295 Oct 29 09:11 uTimer01.c
-rw-rw-r-- 1 alanx alanx  972 Oct 29 09:49 uTimer.c
-rwxrwxr-x 1 alanx alanx 7.4K Oct 29 09:53 uTimer.run

alanx@alanx ~/LinkIt-cc/uTimer $ scp uTimer.run root@mylinkit.local:~/Code/uTimer
root@mylinkit.local's password:

uTimer.run                                    100% 7534     7.4KB/s   00:00    

alanx@alanx ~/LinkIt-cc/uTimer $ ssh root@mylinkit.local
root@mylinkit.local's password:

BusyBox v1.23.2 (2015-11-18 16:34:33 CET) built-in shell (ash)

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 CHAOS CALMER (15.05+linkit, r47501)
 -----------------------------------------------------
  * 1 1/2 oz Gin            Shake with a glassful
  * 1/4 oz Triple Sec       of broken ice and pour
  * 3/4 oz Lime Juice       unstrained into a goblet.
  * 1 1/2 oz Orange Juice
  * 1 tsp. Grenadine Syrup
 -----------------------------------------------------

root@mylinkit:~# cd Code/uTimer

root@mylinkit:~/Code/uTimer# ls -lah

drwxr-xr-x    2 root     root        4.0K Oct 29 01:54 .
drwxr-xr-x    6 root     root        4.0K Oct 29 01:52 ..
-rwxr-xr-x    1 root     root        7.4K Oct 29 01:54 uTimer.run

root@mylinkit:~/Code/uTimer# ./uTimer.run
timer expired 1 times
timer expired 2 times
timer expired 3 times
timer expired 4 times
timer expired 5 times
timer expired 6 times
timer expired 7 times
^C

root@mylinkit:~/Code/uTimer# exit

Connection to mylinkit.local closed.

alanx@alanx ~/LinkIt-cc/uTimer $

AlanX

Discussions