Introduction: Linkit One: LTask Blink
The Linkit One board by MediaTek runs on the MT2502 Aster, system on a chip. There are some hidden implementation details about how the OS is running the Arduino code, but the "vm" prefixed functions seem to offer the most insight into the threading on the board.
While traditional Arduino code is not threaded, the Linkit One system seems to allow threading via the LTask class. The following Ible will explore the LTask and vm_timer, to blink an LED without using the loop() function.
Step 1: Circuit Setup
The circuit is a simple LED and resistor. I'm using pin 8 as my digital output to turn the LED on.
It should be noted that the digital output pins on the Linkit One are 3.3v with low driving current ability. I'm using a white LED with 3-3.2 forward voltage, at 20ma.
I'm using a 12 ohm resistor, since that's what I had lying around.
The circuit is as follows:
Digital Pin 8 -> Resistor -> LED -> Ground.
Step 2: Code
To setup the code, you will need to first include the LTask and vmtimer headers. I also specify my pin for digital output.
#include "LTask.h"
#include "vmtimer.h" int pin = 8;
The LTask class handles threading for your Arduino code, to the Linkit One OS. The main method you will be accessing in this class is the remoteCall function, which is defined as:
remoteCall(remote_call_ptr func, void* userdata)
where remote_call_ptr is a typedef:
typedef boolean (*remote_call_ptr)(void* user_data);
and userdata is any object you want to pass to your callback function.
The vmtimer header defines the vm_create_timer function, which we will be using to create a looping timer that triggers our function callback that we register.
VMINT vm_create_timer(VMUINT32 millisec, VM_TIMERPROC_T timerproc);
The parameters are the milliseconds between timer callbacks and the function to callback. vm_create_timer returns the id of the timer that the Linkit One system creates. It will be less than zero if the system could not create a timer.
NOTE: there can only be a max of 10 of these timers at any time in your code, so if you plan on using multiple you should make sure you delete timers when you finish with them.
void setup()
Start your serial, set your pin to digital output, and I personally turn off the LED at the beginning. Most importantly we will be using LTask remoteCall to register a function "createTimer" to have the system call when it's ready to start a thread.
LTask.remoteCall((remote_call_ptr)createTimer, NULL);
void loop()
Do nothing, we will not be using the loop for this example
In createTimer()
This is called by the system, since we've registered this function with LTask.remoteCall(). We need to use remoteCall before we can setup the vmtimer, which will act as our loop().
VMINT timerId = vm_create_timer(1000, (VM_TIMERPROC_T)timerTick);
VMINT is an integer. vm_create_timer takes two parameters, the time in milliseconds that you want the timer to wait before calling the function specified in the second parameter. The function must match the VM_TIMER_PROC_T signature, which is defined as a function that takes an integer parameter (the task id)
typedef void (*VM_TIMERPROC_T)(VMINT tid);
void timerTick(VMINT tid)
This is our function that will be called each time the vmtimer triggers. For this example, I just toggle the LED on or off with the digitalWrite() as normal.
if (blinkCount & 1) { digitalWrite(pin, HIGH); Serial.println("LED On"); } else { digitalWrite(pin, LOW); Serial.println("LED Off"); }
As was stated before, there can only ever be 10 vmtimer's active. So it's good practice to delete them when you're done. After 10 iterations on the timerTick, I remove the timer.
vm_delete_timer(tid);
And that's it!
I hope this has helped anyone looking into using the timer's provided by the Linkit One.
4 Comments
2 years ago on Step 1
How did you turn on the led? Linkit one digital pin max current is 3mA but led current is 20mA.
5 years ago
Where do I find #include "LTask.h"?
Reply 5 years ago
LTask.h is included in the Linkit One SDK.
https://labs.mediatek.com/en/platform/linkit-one#SDK
There's also an instructable here on how to install it for mac os x.
https://www.instructables.com/id/Install-LinkIt-ONE-SDK-on-Mac-OS-X/
7 years ago
Very nice. Thanks for sharing!