Alcor6L/eLua/pio

From Wikibooks, open books for an open world
Jump to: navigation, search


Overview[edit]

This module contains functions for accessing the CPU's PIO (Programmable Input Output) pins. It contains two set of functions with identical names and behaviour. One set groups the functions used to access individual pins from ports, the other groups the functions used to access full ports.

With the pio module, you specifiy names of ports as they appear in your eLua's CPU datasheet. For example, if your CPU's ports are named PA, PB and PC, you can refer to them using pio.PA, pio.PB and pio.PC, respectively. If your CPU uses P0, P1, P2 instead of PA, PB and PC, you can simply use pio.P0, pio.P1 and pio.P2 instead.

You can also refer to individual pins instead of ports. With the same notation as above, pio.PA_0 refers to the first pin of port PA, pio.P0_15 refers to the 16th pin of port P0 and so on.

Functions[edit]

pio.pin.setdir[edit]

Set pin(s) direction

pio.pin.setdir( direction, pin1, pin2, ..., pinn )
  • pio.pin.setdir( direction, pin1, pin2, ..., pinn )
  • direction - the pin direction, can be either pio.INPUT or pio.OUTPUT
  • pin1 - the first pin
  • pin2 (optional) - the second pin
  • pinn (optional) - the n-th pin

Returns: nothing.

pio.pin.setpull[edit]

Enable/disable pullups/pulldowns on the specified pin(s)

pio.pin.setpull( type, pin1, pin2, ..., pinn )
  • type - 'pull' type, can be either pio.PULLUP to enable pullups, pio.PULLDOWN to enable pulldowns, or pio.NOPULL to disable both pullups and pulldowns
  • pin1 - the first pin
  • pin2 (optional) - the second pin
  • pinn (optional) - the n-th pin

Returns: nothing.

pio.pin.setval[edit]

Set pin(s) value

pio.pin.setval( value, pin1, pin2, ..., pinn )
  • value - pin value, can be either 0 or 1
  • pin1 - the first pin
  • pin2 (optional) - the second pin
  • pinn (optional) - the n-th pin

Returns: nothing.

pio.pin.getval[edit]

Get value of pin(s)

val1, val2, ..., valn = pio.pin.getval( pin1, pin2, ..., pinn )
  • pin1 - the first pin
  • pin2 (optional) - the second pin
  • pinn (optional) - the n-th pin

Returns: The value(s) of the pin(s), either 0 or 1

pio.pin.sethigh[edit]

Set pin(s) to 1 (high)

pio.pin.sethigh( pin1, pin2, ..., pinn )
  • pin1 - the first pin
  • pin2 (optional) - the second pin
  • pinn (optional) - the n-th pin

Returns: nothing.

pio.pin.setlow[edit]

Set pin(s) to 0 (low)

pio.pin.setlow( pin1, pin2, ..., pinn )
  • pin1 - the first pin
  • pin2 (optional) - the second pin
  • pinn (optional) - the n-th pin

Returns: nothing.

pio.port.setdir[edit]

Set port(s) direction

pio.port.setdir( direction, port1, port2, ..., portn )
  • direction - the port direction, can be either pio.INPUT or pio.OUTPUT
  • port1 - the first port
  • port2 (optional) - the second port
  • portn (optional) - the n-th port

Returns: nothing.

pio.port.setpull[edit]

Enable/disable pullups/pulldowns on the specified port(s)

pio.port.setpull( type, port1, port2, ..., portn )
  • type - 'pull' type, can be either pio.PULLUP to enable pullups, pio.PULLDOWN to enable pulldowns, or pio.NOPULL to disable both pullups and pulldowns
  • port1 - the first port
  • port2 (optional) - the second port
  • portn (optional) - the n-th port

Returns: nothing.

pio.port.setval[edit]

Set port(s) value

pio.port.setval( value, port1, port2, ..., portn )
  • value - port value
  • port1 - the first port
  • port2 (optional) - the second port
  • portn (optional) - the n-th port

Returns: nothing.

pio.port.getval[edit]

Get value of port(s)

val1, val2, ..., valn = pio.port.getval( port1, port2, ..., portn )
  • port1 - the first port
  • port2 (optional) - the second port
  • portn (optional) - the n-th port

Returns: The value(s) of the port(s)

pio.port.sethigh[edit]

Set port(s) to all 1 (high)

pio.port.sethigh( port1, port2, ..., portn )
  • port1 - the first port
  • port2 (optional) - the second port
  • portn (optional) - the n-th port

Returns: nothing.

pio.port.setlow[edit]

Set port(s) to all 0 (low)

pio.port.setlow( port1, port2, ..., portn )
  • port1 - the first port
  • port2 (optional) - the second port
  • portn (optional) - the n-th port

Returns: nothing.

pio.decode[edit]

Convert a PIO resource number to the corresponding port and pin. This is most commonly used in GPIO edge interrupt routines to convert the Lua interrupt routine's argument to the port and pin that caused the interrupt but it can also be used on the values returned by the pin names pio.PA_0, pio.P2_15 and so on.

port, pin = pio.decode( resnum )
  • resnum - the resource number of the pin

Returns:

  • port - the index of the port, starting from 0 (so port A is 0, port B is 1 and so on)
  • pin - the pin number, usually from 0 to 31