Terra customization: Arduino Mega Basic customization

In this page we present a customization, called TerraIno, that is a very light customization for Arduino Mega Board using only simple radio communications and GPIO.

TerraIno - Basic Operations

This Terra customization mainly provides only very basic "send" and "receive" events, GPIO operations, and a specific DHT11 Temperature/Humidity Sensor interface. Table 1 presents the current TerraIno events interface and Table 2 presents the functions interface.

Some code examples are presented at the end.

The local operations comprises operations to access input and output devices of the microcontroller. Terra encapsulates all these operations in a component called Local Operations providing them as input and/or output events. Timers, on the other hand, are handled directly by the Céu-T language with the await time command.

The SEND() command is a basic send command where only the nodes in the radio range may receive the message. If the field target is set to BROADCAST value, all nodes in the range will receive the message. On the other hand, if this field is set to a specific node identifier, only this node will receive the message (if the node is in the radio range). A SEND_DONE() event indicates that the send request was processed by the radio. The RECEIVE event returns a received message. A variation of the SEND() command is the SEND_ACK() command that requests an acknowledgement from the target mote. In this variation the SEND_DONE_ACK() event return a boolean value indicating the acknowledgement. Additionally, TerraIno implements a simple message queue to support message buffering needs.

The user must define the radio message data structure before create a message variable.

Events definitions


Table 1 - TerraIno Events interface
Description Output event (emit) Input event (await)
Event identifier Argument type Returned type Event identifier Argument type
Set all three leds - The less three significative bits of the argument will set the leds. LEDS ubyte
Set LED0 - Argument can be ON, OFF, or TOGGLE. LED0 ubyte
Set LED1 - Argument can be ON, OFF, or TOGGLE. LED1 ubyte
Set LED2 - Argument can be ON, OFF, or TOGGLE. LED2 ubyte
Custom event - This is a internal loopback event that allows to pass a integer value. The await command may define or not which value to wait. REQ_CUSTOM_A ubyte ubyte CUSTOM_A void or ubyte
Send radio message - Sends a radioMsg variable (broadcast or to a specific target node). SEND radioMsg ubyte SEND_DONE void or ubyte
Send radio message (with Acknowledgement) - Sends a radioMsg variable to a specific target node requesting an acknowledgment. The confirmation (TRUE or FALSE) is returned in the SEND_DONE_ACK. SEND_ACK radioMsg ubyte SEND_DONE_ACK void or ubyte
Receive radio message - returns a radioMsg variable received by the radio. The argument may be used to wait only a specific message type identifier. radioMsg RECEIVE void or ubyte
Message queue - This event indicates that the msg queue got a new element when it was empty. ubyte Q_READY void
Error trap - Indicates a internal errors like E_DIVZERO and E_IDXOVF, respectively division by zero and array index overflow. ubyte ERROR void or ubyte
DTH Sensor - Requests and receives the sensor value. The received value is a 8bit reading. The request argument defines which interrupt pin the sensor data pin is connected to. Can range from 0 for Int0/D21 to 3 for Int3/D18. The received event return a dhtData_t data structure with stat, hum, temp fields. REQ_DHT ubyte dhtData_t DHT void
Analog Input - Requests and receives the analog input value. The received value is a 10bit reading from Analog to Digital converter. Four input are available in ANA0 to ANA3. ANA0_READ void ushort ANA0_READ_DONE void
External Interrupt fired: Int0 -- Int3. Must be configured by specific functions. Returns the interrupt ID (0..3) and the argument indicates which ID will be triggered. ubyte INT_FIRED ubyte
Change Level Interrupt fired: PCINT0 -- PCINT2. Must be configured by specific functions. Returns the interrupt ID (0..2) and the argument indicates which ID will be triggered. ubyte PCINT_FIRED ubyte

Available functions


Table 2 - TerraIno functions interface
Description Returned type Function name arguments
Return the node identifier ushort getNodeId void
Return a 16bits random number ushort random void
Return the internal timer counter ulong getClockMicro void
Put a radioMsg data into the queue. The message structure is passed as argument. Returns FAIL if the queue is full, otherwise returns SUCCESS. ubyte qPut radioMsg msgVar
Get a radioMsg data from the queue. The message structure is passed as argument. Returns FAIL if the queue is empty, otherwise returns SUCCESS. ubyte qGet radioMsg msgVar
Return the queue size ubyte qSize void
Clear all queue entries. Always returns SUCCESS. ubyte qClear void
Set the radio transmit power level. Always returns SUCCESS. ubyte setRFPower ubyte level
Set the pin mode INPUT, OUTPUT, INPUT_PULLUP. Always returns SUCCESS. The three arguments are: port_id, pin_id, mode. See constants definitions for more details. ubyte pinMode ubyte,ubyte,ubyte
Writes LOW or HIGH to a pin. Always returns SUCCESS. The three arguments are: port_id, pin_id, value. See constants definitions for more details. ubyte pinWrite ubyte,ubyte,ubyte
Returns the input value of a pin. The tow arguments are: port_id, pin_id. See constants definitions for more details. ubyte pinRead ubyte,ubyte
Toggle the pin value. Always returns SUCCESS. The two arguments are: port_id, pin_id. See constants definitions for more details. ubyte pinToggle ubyte,ubyte
Writes the 8 bits of a DDR register. Always returns SUCCESS. The two arguments are: port_id, value. See constants definitions for more details. ubyte portDDR ubyte,ubyte
Writes the 8 bits of a PORT register. Always returns SUCCESS. The two arguments are: port_id, value. See constants definitions for more details. ubyte portWrite ubyte,ubyte
Returns the 8 bits of a PIN register. The arguments is port_id. See constants definitions for more details. ubyte portRead ubyte
Enable a Intx interrupt. Always returns SUCCESS. The arguments is the interrupt ID (0..3). ubyte intEnable ubyte
Disable a Intx interrupt. Always returns SUCCESS. The arguments is the interrupt ID (0..3). ubyte intDisable ubyte
Clear a Intx interrupt flag. Always returns SUCCESS. The arguments is the interrupt ID (0..3). ubyte intClear ubyte
Configure the interrupt mode for a Intx. Always returns SUCCESS. The arguments are the interrupt ID (0..3) and the mode - INT_LEVEL, INT_TOGGLE, INT_FALLING, or INT_RISING. ubyte intConfig ubyte, ubyte
Enable a PCIntx interrupt. Always returns SUCCESS. The arguments is the interrupt ID (0..2). ubyte pcintEnable ubyte
Disable a PCIntx interrupt. Always returns SUCCESS. The arguments is the interrupt ID (0..2). ubyte pcintDisable ubyte
Clear the PCIntx interrupt flag. Always returns SUCCESS. The arguments is the interrupt ID (0..2). ubyte pcintClear ubyte
Set a PCIntx interrupt bit-mask. Always returns SUCCESS. The arguments are the interrupt ID (0..2) and the bit-mask. ubyte pcintMask ubyte, ubyte

Constant definitions


Table 3 - TerraIno Constant definitions
Constant Value Description
SUCCESS 0 Indicates a Success operation
TRUE 1 A true value
FALSE 0 A false value
ON 1 Set LED ON
OFF 0 Set LED OFF
TOGGLE 2 Set LED with opposite value
BROADCAST 0xffff Define a broadcast message
E_DIVZERO 10 Error event - Division by zero
E_IDXOVF 11 Error event - Array index overflow
E_STKOVF 20 Error event - Stack overflow -- fatal
LOW 0 A digital low value
HIGH 1 A digital high value
INPUT 0 Defines the input mode for a digital pin
INPUT_PULLUP 2 Defines the input mode for a digital pin with the internal pull up resistor.
OUTPUT 1 Defines the output mode for a digital pin
INT0 0 Interrupt Int0 ID. PORTD, pin 0 - D21
INT1 1 Interrupt Int1 ID. PORTD, pin 1 - D20
INT2 2 Interrupt Int2 ID. PORTD, pin 2 - D19
INT3 3 Interrupt Int3 ID. PORTD, pin 3 - D18
INT_LEVEL 0 Interrupt mode: Interrupts allways on low Level
INT_TOGGLE 1 Interrupt mode: Interrupts when the input toggles its value
INT_FALLING 2 Interrupt mode: Interrupts at falling edge - 1->0
INT_RISING 3 Interrupt mode: Interrupts at rising edge - 0->1
PORTA .. PORTL 0 .. 10 Pin operations: Portx ID. PortI doens not exist!
PIN0 .. PIN7 0 .. 7 Pin operations: Pinx ID.

Table 4 - Arduino pin enumeration to Port,Pin. May be used to substitute the arguments port_id, pin_id in the Pin Functions
Arduino Pin# Terra portId,pinId ATMega2560 functions
D22 0,0 PA0 (AD0)
D23 0,1 PA1 (AD1)
D24 0,2 PA2 (AD2)
D25 0,3 PA3 (AD3)
D26 0,4 PA4 (AD4)
D27 0,5 PA5 (AD5)
D28 0,6 PA6 (AD6)
D29 0,7 PA7 (AD7)
D53 1,0 PB0 (SS)
D52 1,1 PB1 (SCK)
D51 1,2 PB2 (MOSI)
D50 1,3 PB3 (MISO)
D10 1,4 PB4 (PWM/OC2A)
D11 1,5 PB5 (PWM/OC1A)
D12 1,6 PB6 (PWM/OC1B)
D13 1,7 PB7 (PWM/OC0A/OC1C)
D37 2,0 PC0 (A8)
D36 2,1 PC1 (A9)
D35 2,2 PC2 (A10)
D34 2,3 PC3 (A11)
D33 2,4 PC4 (A12)
D32 2,5 PC5 (A13)
D31 2,6 PC6 (A14)
D30 2,7 PC7 (A15)
D21 3,0 PD0 (SCL/INT0)
D20 3,1 PD1 (SDA/INT1)
D19 3,2 PD2 (RX1/INT2)
D18 3,3 PD3 (TX1/INT3)
D38 3,7 PD7 (T0)
D0 4,0 PE0 (RXD0/PCINT8)
D1 4,1 PE1 (TXD0)
D5 4,3 PE3 (PWM/OC3A/AIN1)
D2 4,4 PE4 (PWM/OC3B) [INT4]
D3 4,5 PE5 (PWM/OC3C) [INT5]
D41 6,0 PG0 (WR)
D40 6,1 PG1 (RD)
D39 6,2 PG2 (ALE)
D4 6,5 PG5 (PWM/OC0B)
D17 7,0 PH0 (RXD2)
D16 7,1 PH1 (TXD2)
D6 7,3 PH3 (PWM/OC4A)
D7 7,4 PH4 (PWM/OC4B)
D8 7,5 PH5 (PWM/OC4C)
D9 7,6 PH6 (PWM/OC2B)
D15 8,0 PJ0 (RX3/PCINT9)
D14 8,1 PJ1 (TX3/PCINT10)
D49 10,0 PL0 (ICP4)
D48 10,1 PL1 (ICP5)
D47 10,2 PL2 (T5)
D46 10,3 PL3 (PWM/OC5A)
D45 10,4 PL4 (PWM/OC5B)
D44 10,5 PL5 (PWM/OC5C)
D43 10,6 PL6
D42 10,7 PL7
A0 5,0 PF0 (ADC0)
A1 5,1 PF1 (ADC1)
A2 5,2 PF2 (ADC2)
A3 5,3 PF3 (ADC3)
A4 5,4 PF4 (ADC4/TMK)
A5 5,5 PF5 (ADC5/TMS)
A6 5,6 PF6 (ADC6)
A7 5,7 PF7 (ADC7)
A8 9,0 PK0 (ADC8/PCINT16)
A9 9,1 PK1 (ADC9/PCINT17)
A10 9,2 PK2 (ADC10/PCINT18)
A11 9,3 PK3 (ADC11/PCINT19)
A12 9,4 PK4 (ADC12/PCINT20)
A13 9,5 PK5 (ADC13/PCINT21)
A14 9,6 PK6 (ADC14/PCINT22)
A15 9,7 PK7 (ADC15/PCINT23)

Code samples

Digital Pin - Blinking the board Led - D13/PortB.7

#include "TerraIno.defs"
pinMode(PORTB,7,OUTPUT);  // also may use: pinMode(D13,OUTPUT);
loop do
    await 500ms;          // Waits 0,5 seconds
    pinToggle(D13);       // Toggle LED value
end

DHT11 Sensor - Counting temperature value

Connect the DHT data pin to D21/PortD0/Int0.

#include "TerraIno.defs"
pinMode(PORTB,7,OUTPUT);    // also may use: pinMode(D13,OUTPUT);
var dhtData_t data;         // Data variable to store result
var ubyte count;            // Blink LED counter
loop do
    emit REQ_DHT(INT0);     // Request a DHT11 read
    data = await DHT();     // Waits the data
    count=data.temp;        // Count the Temperature or the Error code
    if data.stat != 0 then count=data.stat; end
    loop i, count do        // Loop to blink the LED
        pinWrite(D13,HIGH);
        await 150ms;
        pinWrite(D13,LOW);
        await 150ms;
    end
    await 500ms;
end

Interrupt Pin - Trigger a pin interruption

Connect a push button to GND and D20. The LED will toggle once when you press the button. Do nothing when releasing it.

#include "TerraIno.defs"
pinMode(PORTB,7,OUTPUT);    // also may use: pinMode(D13,OUTPUT);
pinMode(D20,INPUT_PULLUP);  // Int1 pin as input
intConfig(INT1,INT_FALLING);// Interrupt from 1 to 0.
intEnable(INT1);            // Enable the interrupt
intClear(INT1);             // Clear, if necessary, the interrupt flag.
loop do
    await INT_FIRED(INT1);  // Waits for an interrupt Int1
    intClear(INT1);         // Clear the IntFlag
    pinToggle(D13);         // Toggle the LED status
    await 30ms;  			// Debouncing: Waits some time before next Int1.
end