eLua Modules Reference Manual

eLua Generic Modules

A Generic eLua Module is a module that can be used by a Lua program running on any of the supported eLua platforms.
Write your code once and it is already automatically ported to the main platforms of theembedded world.









bit

Bitwise operations in eLua is implemented thru the BitLib library, from Reuben Thomas.
BitLib project is hosted at LuaForge on http://luaforge.net/projects/bitlib

Res = bit.bnot( value )

unary negation

Res = bit.band( v1, v2, ... )

bitwise "and"

Res = bit.bor( v1, v2, ... )

bitwise "or"

Res = bit.bxor( v1, v2, ... )

bitwise "exclusive or"

Res = bit.lshift( value, pos )

shift "value" left "pos" positions.

Res = bit.rshift( value, pos )

shift "value" right "pos" positions. The sign is not propagated.

Res = bit.arshift( value, pos )

shift "value" right "pos" positions. The sign is propagated ("arithmetic shift").

Res = bit.bit( bitno )

a shortcut for bit.lshift( 1, bitno )

Res1, Res2, ... = bit.set( bitno, v1, v2, ... )

set the bit at position "bitno" in v1, v2, ... to 1.

Res1, Res2, ... = bit.clear( bitno, v1, v2, ... )

set the bit at position "bitno"in v1, v2, ... to 0.

Res = bit.isset( value, bitno )

returns true if bit at position "bitno" in "value" is 1, false otherwise.

Res = bit.isclear( value, bitno )

returns true if bit at position "bitno" in "value" is 0, false otherwise.











cpu

write32( address, data ) : write the 32-bit data at the specified address

write16( address, data ) : write the 16-bit data at the specified address

write8( address, data ) : write the 8-bit data at the specified address


Data = read32( address ) : reads 32-bit data from the specified address

Data = read16( address ) : reads 16-bit data from the specified address

Data = read8( address ) : reads 8-bit data from the specified address


[cpu.disableinterrupts()] cli(): disable CPU interrupts

[cpu.enableinterrupts()] sei(): enable CPU interrupts

[cpu.clockfrequency()] Clock = clock(): returns the CPU frequency

Also, you can expose as many CPU constants (for example memory mapped registers)

as you want to this module. You might want to use this feature to access some

CPU memory areas (as defined in the CPU header files from the CPU support

package) directly from Lua. To do this, you'll need to define the

PLATFORM_CPU_CONSTANTS macro in the platform's platform_conf.h file

(src/platform/<platform name>/platform_conf.h). Include all your constants in a

_C( <constant name> ) definition, and then build your project.

For example, let's suppose that your CPU's interrupt controler has 3 memory

mapped registers: INT_REG_ENABLE, INT_REG_DISABLE and INT_REG_MASK. If you want

to access them from Lua, locate the header that defines the values of these

registers (I'll assume its name is "cpu.h") and add these lines to the

platform_conf.h:

#include "cpu.h"

#define PLATFORM_CPU_CONSTANTS\

_C( INT_REG_ENABLE ),\

_C( INT_REG_DISABLE ),\

_C( INT_REG_MASK )

After this you'll be able to access the regs directly from Lua, like this:

data = cpu.r32( cpu.INT_REG_ENABLE )

cpu.w32( cpu.INT_REG_ENABLE, data )

For a "real-life" example, see the src/platform/lm3s/platform_conf.h file.

[uart.sendstring] uart.sendstr( id, str1, str2, ... ): this is similar to "uart.send", but its parameters are string.

pio

pio

Programable Input Output Module

Some notes on PIO:


[pio.setpinvalue] pio.setpin( value, Pin1, Pin2 ... ): set the value to all the pins in the list

to "value" (0 or 1).

[pio.setpinhigh] pio.set( Pin1, Pin2, ... ): set the value of all the pins in the list to 1.

[pio.getpinvalue] Val1, Val2, ... = pio.get( Pin1, Pin2, ... ): reads one or more pins and returns

their values (0 or 1).

[pio.setpinlow] pio.clear( Pin1, Pin2, ... ): set the value of all the pins in the list to 0.

[pio.configpin(pio.DIR, pio.DIR_INPUT)] pio.input( Pin1, Pin2, ... ): set the specified pin(s) as input(s).

[pio.configpin(pio.DIR, pio.DIR_OUTPUT)] pio.output( Pin1, Pin2, ... ): set the specified pin(s) as output(s).

[pio.setportvalue] pio.setport( value, Port1, Port2, ... ): set the value of all the ports in the

list to "value".

[pio.getportvalue] Val1, Val2, ... = pio.getport( Port1, Port2, ... ): reads one or more ports and

returns their values.

[pio.getportname] Port = pio.port( code ): return the physical port number associated with the given code. For example, "pio.port( pio.P0_20 )" will return 0.

[pio.getpinnumber] Pin = pio.pin( code ): return the physical pin number associated with the

given code. For example, "pio.pin( pio.P0_20 )" will return 20.


[pio.togglepin([Pin1], [Pin2], ...)]

[pio.toggleport([Port1], [Port2], ...)]

Another idea (can be added to the above ?)
[pio.configport(pio.[FUNCTION], pio.MASK, [MASK])]
Ex:
pio.configpin(pio.DIR, pio.DIR_INPUT) (.DIR_OUTPUT)
pio.configpin(pio.PULL, pio.PULL_UP) (.PULL_DOWN, PULL_NO)

[pio.configport(pio.DIR, pio.DIR_INPUT, [Port1], [Port2], ...)] pio.port_input( Port1, Port2, ... ): set the specified port(s) as input(s).

[pio.configport(pio.DIR, pio.DIR_OUTPUT, [Port1], [Port2], ...)] pio.port_output( Port1, Port2, ... ): set the specified port(s) as output(s).

[pio.configpin(pio.PULL, pio.PULL_UP, [Pin1], [Pin2], ...)] pio.pullup( Pin1, Pin2, ... ): enable internal pullups on the specified pins.Note that some CPUs might not provide this feature.

[pio.configpin(pio.PULL, pio.PULL_DOWN, [Pin1], [Pin2], ...)] pio.pulldown( Pin1, Pin2, ... ): enable internal pulldowns on the specified pins. Note that some CPUs might not provide this feature.

[pio.configpin(pio.PULL, pio.PULL_NO, [Pin1], [Pin2], ...)] pio.nopull( Pin1, Pin2, ... ): disable the pullups/pulldowns on the specifiedpins. Note that some CPUs might not provide this feature.


net


pwm


It allows Lua to use the PWM blocks on the target CPU.

[pwm.setup](pwm.setup( id, frequency, Active Cycle ) Data = pwm.setup( id, frequency, duty ): sets the PWM block 'id' to generate the specified frequency with the specified duty cycle (duty is an integer number from 0 to 100, specifying the duty cycle in percents). It returns the actual frequency set on the PWM block.

Here there is a bigger change on the proposal.

The Timer Clock and the PWM "frame" frequency would be set up in the same function (.setup)

The normal control function would only set the active cicle (.setcycle)

The original .setup function would then be replaced by:

[pwm.setup( id, tmrclock, pwm_frequency ) ]

[pwm.setcycle( id, active_cycle )]

[pwm.start()] pwm.start( id ): start the PWM block 'id'.

[pwm.stop()] pwm.stop( id ): stop the PWM block 'id'.


Data = pwm.setclock( id, clock ): set the base clock of the PWM block 'id' to

the given clock. In returns the actual clock set on the PWM block.

[pwm.getclock] Data = pwm.getclock( id ): returns the base clock of the PWM block 'id'.

spi


Actual_clock = spi.setup( id, spi.MASTER | spi.SLAVE, clock, cpol, cpha,
databits): set the SPI interface with the given parameters, returns the clock
that was set for the interface.

spi.select( id ): sets the selected spi as active (sets the SS line of the given interface).

spi.unselect( id ): clears the SS line of the given interface.

spi.send( id, Data1, Data2, ... ): sends all the data to the specified SPI
interface.

[spi.sendrecv(id, Out1, Out2, ...)] In1, In2, ... = spi.send_recv( id, Out1, Out2, ... ): sends all the "out" bytes to the specified SPI interface and returts the data read after each sent byte.

Returning several values in this blocking way would not complicate some queued send implementations ? (ok, this could be another function :)

Sending multiple data/chars in a single call and not in a table argument does not allow the data to be built in run time (without some string massage, of course :)



sys


[sys.platform()] pd.platform(): returns the platform name (f.e. LM3S)

[sys.mcu()] pd.cpu(): returns the CPU name (f.e. LM3S8962)

[sys.cpu()] would return ARM Cortex M3 in this case.....

[sys.board()] pd.board(): returns the CPU board (f.e. EK-LM3S8962)

term

Terminal support

[term.clear] term.clrscr(): clear the screen


[term.cleareol] term.clreol(): clear from the current cursor position to the end of the line

[term.moveto] term.gotoxy( x, y ): position the cursor at the given coordinates


[term.moveup] term.up( delta ): move the cursor up "delta" lines

[term.movedown] term.down( delta ): move the cursor down "delta" lines

[term.moveleft] term.left( delta ): move the cursor left "delta" lines


[term.moveright] term.right( delta ): move the cursor right "delta" lines

[term.getlinecount] Lines = term.lines(): returns the number of lines

[term.getcolcount] Cols = term.cols(): returns the number of columns


[term.printstr] term.putstr( s1, s2, ... ): writes the specified string(s) to the terminal

[term.printchar] term.put( c1, c2, ... ): writes the specified character(s) to the terminal

[term.getx] Cx = term.cursorx(): return the cursor X position

[term.gety] Cy = term.cursory(): return the cursor Y position

[term.inputchar] c = term.getch( term.WAIT | term.NOWAIT ): returns a char read from the

terminal.


tmr


It allows Lua to execute timer specific operations (delay, read timer value,

start timer, get time difference).

Some notes on timers:

tmr.delay( id, delay ): uses timer 'id' to wait for 'delay' us.

Data = tmr.read( id ): reads the value of timer 'id'. The returned value is

platform dependent.

Data = tmr.start( id ): start the timer 'id', and also returns its value at

the moment of start. The returned value is platform dependent.

diff = tmr.diff( id, end, start ): returns the time difference (in us) between

the timer values 'end' and 'start' (obtained from calling tmr.start or

tmr.read). The order of end/start is irrelevant.

Data = tmr.mindelay( id ): returns the minimum delay (in us ) that can be

achieved by calling the tmr.delay function. If the return value is 0, the

platform layer is capable of executing sub-microsecond delays.

Data = tmr.maxdelay( id ): returns the maximum delay (in us) that can be

achieved by calling the tmr.delay function.

Data = tmr.setclock( id, clock ): sets the clock of the given timer. Returns the

actual clock set for the timer.

Data = tmr.getclock( id ): return the clock of the given timer.






uart

uart.setup( id, baud, databits,
uart.PARITY_EVEN | uart.PARITY_ODD | uart.PARITY_NONE,
uart.STOPBITS_1 | uart.STOPBITS_1_5 | uart.STOPBITS_2 )

Set the UART interface with the given parameters.

Returns the actual baud rate that was set for the UART.

uart.send( id, Data1, Data2, ... )

Send all the data to the specified UART interface.

uart.recv( id, uart.TIMEOUT_NO | uart.TIMEOUT_INFINITE | timeout ) Data = uart.recv( id, uart.NO_TIMEOUT | uart.INF_TIMEOUT | timeout )

Reads a byte from the specified UART interface.

eLua Platform Dependent Modules

A Platform Dependent eLua Module is a module that runs only on one or on a few supported eLua platforms.
These modules make use of specifical devices and features offered by some kits and allow eLua aplications to make the best use of the external hardware on your platforms.

adc - Analog to Digital Conversion Module

Currently runs on: LM3Sxxxx

The ADC module handles the Analog to Digital Conversion Peripherals.

adc.sample(channel_id | {channel_id1, channel_id2, ...}, count)
Request that count samples be converted from channel_id. count must be greater than zero and a power of 2

adc.flush(channel_id)
Empty sample and smoothing buffers.

adc.getsample(channel_id)
Request a single sample from the buffer.

adc.getsamples(channel_id, [count])
Request count samples from the buffer, in a table. If count is either zero or omitted, all available samples are returned.

adc.maxval(channel_id)
Returns the largest integer one can expect fromr this channel on a given platform (based on bit depth).

adc.setclock(channel_id, frequency, [timer_id])
Sets the frequency and clock source for sample collection. If frequency is zero (timer_id not needed), samples on channel_id are collected as fast as possible.  If frequency is non-zero, timer_id is configured to trigger sampling on channel_id at frequency.

adc.isdone(channel_id)
Returns 1 if samples are still being collected on channel_id, 0 if channel is inactive.

adc.setblocking(channel_id, mode)
mode 1 sets blocking mode (default). adc.getsample(s) will wait for requested samples to be captured before returning. mode 0 sets non-blocking mode

adc.setsmoothing(channel_id, length)
Set the length of the smoothing filter on channel_id.  When greater than 1, and samples are requested, smoothing filter will fill to length with samples, and then put the requested number of samples into the adc buffer.
length must be a power of 2 (maximum = 64)

disp

Currently runs on: LM3Sxxxx

The disp module handles the RIT OLED display usage on Luminary Micro Cortex-M3 boards

## Following functions may change to merge init/on/enable and off/disable

disp.init( freq )

freq specifies the SSI Clock Frequency to be used.

This function initializes the SSI interface to the OLED display and configures the SSD1329 controller on the panel.


disp.enable()

Enable the SSI component of the OLED display driver.

freq specifies the SSI Clock Frequency to be used.
This function initializes the SSI interface to the OLED display.

disp.disable()

Disable the SSI component of the OLED display driver and frees the SPI channel for other uses.

disp.on()

Turns on the OLED display.
This function will turn on the OLED display, causing it to display the contents of its internal frame buffer.

disp.off()

Turns off the OLED display
This function will turn off the OLED display. This will stop the scanning of the panel and turn off the on-chip DC-DC converter, preventing damage to the panel due to burn-in (it has similar characters to a CRT in this respect).

disp.clear()

Clears the OLED display.
This function will clear the display RAM. All pixels in the display will be turned off.


disp.print( str, x, y, gray )

Writes a string on the OLED display.

Calling Arguments:
str is a string to be displayed.
x is the horizontal position to display the string, specified in columns from the left edge of the display.
y is the vertical position to display the string, specified in rows from the top edge of the display.
gray is the 4-bit gray scale (intensity) value to be used for displayed text.

This function will draw a string on the display. Only the ASCII characters between 32 (space) and 126 (tilde) are supported; other characters will result in random data being draw on the display (based on whatever appears before/after the font in memory). The font is mono-spaced, so characters such as ``i'' and ``l'' have more white space around them than characters such as ``m'' or ``w''.
If the drawing of the string reaches the right edge of the display, no more characters will be drawn. Therefore, special care is not required to avoid supplying a string that is ``too long'' to display.

Because the OLED display packs 2 pixels of data in a single byte, the
parameter x must be an even column number (for example, 0, 2, 4, and
so on).


disp.draw( img, x, y, width, height, gray )

Displays an image on the OLED display.

img a pointer to the string data representing a rit format image to display.
x is the horizontal position to display the string, specified in columns from the left edge of the display.
y is the vertical position to display the string, specified in rows from the top edge of the display.
width is the width of the image, specified in columns.
height is the height of the image, specified in rows.

This function will display a bitmap graphic on the display. Because of the format of the display RAM, the starting column x and the number of columns y must be an integer multiple of two.
The image data is organized with the first row of image data appearing left to right, followed immediately by the second row of image data. Each byte contains the data for two columns in the current row, with the leftmost column being contained in bits 7:4 and the rightmost column being contained in bits 3:0.
For example, an image six columns wide and seven scan lines tall would be arranged as follows (showing how the twenty one bytes of the image would appear on the display):

Because the OLED display packs 2 pixels of data in a single byte, the parameter x must be an even column number (for example, 0, 2, 4, and so on).

+-------------------+-------------------+-------------------+
| Byte 0 | Byte 1 | Byte 2 |
+---------+---------+---------+---------+---------+---------+
| 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 |
+---------+---------+---------+---------+---------+---------+
| Byte 3 | Byte 4 | Byte 5 |
+---------+---------+---------+---------+---------+---------+
| 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 |
+---------+---------+---------+---------+---------+---------+
| Byte 6 | Byte 7 | Byte 8 |
+---------+---------+---------+---------+---------+---------+
| 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 |
+---------+---------+---------+---------+---------+---------+
| Byte 9 | Byte 10 | Byte 11 |
+---------+---------+---------+---------+---------+---------+
| 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 |
+---------+---------+---------+---------+---------+---------+
| Byte 12 | Byte 13 | Byte 14 |
+---------+---------+---------+---------+---------+---------+
| 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 |
+---------+---------+---------+---------+---------+---------+
| Byte 15 | Byte 16 | Byte 17 |
+---------+---------+---------+---------+---------+---------+
| 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 |
+---------+---------+---------+---------+---------+---------+
| Byte 18 | Byte 19 | Byte 20 |
+---------+---------+---------+---------+---------+---------+
| 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 | 7 6 5 4 | 3 2 1 0 |
+---------+---------+---------+---------+---------+---------+