Skip to main content

Extension module

Olem has 4 extension slots, 2 of which are equipped with a connector that can be used to add new features to OLEM that require electronical components. The connectors have 4 pins :

  • P1 and P2 carry logicial signals
  • P3 is GND
  • P4 is 3V3 and can provide up to 300 mA

The pins from all slots are connected, which means that P1 of one extension slot is connected to P1 of the other extension slot (and so on for all Px).

Extension modules can be used in GPIO, PWM or I2C mode, selected with extmod_mode_set() and the EXTMOD_GPIO, EXTMOD_PWM and EXTMOD_I2C constants.

GPIO mode​

In GPIO mode, P1 and P2 can be controlled as simple GPIO in output mode. In that case, P1 is GPIO1 and P2 is GPIO2. Use extmod_gpio_set() with the EXTMOD_GPIO1, EXTMOD_GPIO2, LOW and HIGH constants.

Example​

Both pins switch between LOW and HIGH every 3 seconds.

import olem

print("Starting extension module gpio periodic test")

olem.extmod_mode_set(olem.EXTMOD_GPIO)

for i in range(0, 10):
olem.extmod_gpio_set(olem.EXTMOD_GPIO1, olem.LOW)
olem.extmod_gpio_set(olem.EXTMOD_GPIO2, olem.LOW)
olem.delay_ms(3000)
olem.extmod_gpio_set(olem.EXTMOD_GPIO1, olem.HIGH)
olem.extmod_gpio_set(olem.EXTMOD_GPIO2, olem.HIGH)
olem.delay_ms(3000)

olem.extmod_gpio_set(olem.EXTMOD_GPIO1, olem.LOW)
olem.extmod_gpio_set(olem.EXTMOD_GPIO2, olem.LOW)

print("End of extension module gpio periodic test")

PWM mode​

In PWM mode, P1 and P2 can be used to output signals with varying duty cycle and periods. Use extmod_pwm_set() with the EXTMOD_GPIO1 and EXTMOD_GPIO2 constants.

PWM signal, with its period and compare value

Example​

The duty cycle of P1 goes up while the one of P2 goes down.

import olem

period = 20

print("Start pwm")
olem.extmod_mode_set(olem.EXTMOD_PWM)

for i in range(0, period):
print("compare value " + str(i))
olem.extmod_pwm_set(olem.EXTMOD_GPIO1, period, i)
olem.extmod_pwm_set(olem.EXTMOD_GPIO2, period, period - i)
olem.delay_ms(2000)

olem.extmod_mode_set(olem.EXTMOD_GPIO)
olem.extmod_gpio_set(olem.EXTMOD_GPIO1, olem.LOW)
olem.extmod_gpio_set(olem.EXTMOD_GPIO2, olem.LOW)

print("End pwm")

I2C mode​

In I2C mode, P1 is assigned to SCL and P2 is assigned to SDA. Use extmod_i2c_read() and extmod_i2c_write().

Example​

Every second, 2 bytes are written to the device at address 0x43, then 4 bytes are read from it.

import olem

print("Starting extension module gpio i2c test")

addr = 0x43
olem.extmod_mode_set(olem.EXTMOD_I2C)
timestamp_start = olem.get_time_ms()

while ((olem.get_time_ms() - timestamp_start) < 20000):
olem.extmod_i2c_write(addr, [0x86, 0x87])
olem.delay_ms(1000)
print(olem.extmod_i2c_read(addr, 4))
olem.delay_ms(1000)

print("End of extension module gpio i2c test")

Constants and functions​

Constants​

ConstantValueDescription
LOW0Low level of a GPIO output.
HIGH1High level of a GPIO output.
EXTMOD_GPIO11Pin P1 of the extension slots.
EXTMOD_GPIO22Pin P2 of the extension slots.
EXTMOD_GPIO0GPIO mode: P1 and P2 are digital outputs.
EXTMOD_I2C1I2C mode: P1 is SCL and P2 is SDA.
EXTMOD_PWM2PWM mode: P1 and P2 output PWM signals.

extmod_mode_set(mode)​

Initialize the extension slots in the given mode. Only one mode can be used at a time.

ParameterDescription
modeEXTMOD_GPIO, EXTMOD_I2C or EXTMOD_PWM

extmod_gpio_set(gpio, value)​

Set the output level of a pin. Requires the EXTMOD_GPIO mode, see extmod_mode_set().

ParameterDescription
gpioEXTMOD_GPIO1 or EXTMOD_GPIO2
valueLOW or HIGH

extmod_pwm_set(gpio, period, compare_value)​

Set the PWM signal of a pin. Requires the EXTMOD_PWM mode, see extmod_mode_set().

ParameterDescription
gpioEXTMOD_GPIO1 or EXTMOD_GPIO2
periodperiod of the signal in ms, as an integer. With 0 the output stays LOW.
compare_valuetime the signal stays HIGH in each period in ms, as an integer

extmod_i2c_read(addr, length)​

Read bytes from a device on the I2C bus. Requires the EXTMOD_I2C mode, see extmod_mode_set().

ParameterDescription
addraddress of the device
lengthnumber of bytes to read

Returns: list of the bytes read, as integers, or an empty list if the read failed

extmod_i2c_write(addr, data)​

Write bytes to a device on the I2C bus. Requires the EXTMOD_I2C mode, see extmod_mode_set().

ParameterDescription
addraddress of the device
datalist of the bytes to write, as integers from 0 to 255

Returns: 0 on success, an error code otherwise