Overview

greenpak is Python package that provides access and programming of Renesas GreenPak SPLD’s via I2C bus adapters. This is done via a simple to use API that is abstracted at the GreenPak model level.

The package supports all USB to I2C adapters that are supported by the following python drivers and support for new drivers can be easily added.


Wiring

Important:

Do not exceed the max voltages of the I2C adapter and the GreenPak device(s). If necessary, use I2C level shifter to adapt the SDA/SCL signal level.

I2C Adapter pin

GreenPak pin

Comments

GND

GND

Common ground

VCC

VDD, VDD2

Optional, if circuit is not self powered.

SDA

SDA

I2C data line. Should have a pullup resistor.

SCL

SCL

I2C clock line. Should have a pullup resistor.


Examples

Programming a device

Programming a GreenPak device in circuit. Upon reset, the GreenPak device loads the new configuration from its NVM memory.

 1from greenpak import driver, i2c, utils
 2
 3print("Connecting.")
 4i2c_driver = i2c.GreenPakI2cAdapter(port = "/dev/tty.usbmodem1101")
 5gp_driver = gp.GreenpakDriver(i2c_driver, device_type="SLG46826", device_control_code=0b0001)
 6
 7print("Loading configuration from file.")
 8data = utils.read_bits_config_file("test_data/slg46826_blinky_slow.txt")
 9utils.hex_dump(data)
10
11print("Programming the NVM.")
12gp_driver.program_nvm_pages(0, data)
13
14print("Resetting the device.")
15gp_driver.reset_device()

Reading device configuration

Loading the configuration from the NVM space of a GreenPak device and writing it to a file. This requires the device to be non locked.

 1from greenpak import driver, i2c, utils
 2
 3print("Connecting.")
 4i2c_driver = i2c.GreenPakI2cAdapter(port = "/dev/tty.usbmodem1101")
 5gp_driver = driver.GreenpakDriver(i2c_driver, device_type="SLG46826", device_control_code=0b0001)
 6
 7print ("Reading the configuration from device's NVM.")
 8data = gp_driver.read_nvm_bytes(0, 256)
 9utils.hex_dump(data)
10
11print ("Writing configuration to a file.")
12utils.write_hex_config_file("_output_file.hex", data)

Scanning the I2C bus

Scanning for I2C devices vs scanning for GreenPak devices. Each GreenPak devices appear at four consecutive I2C addresses.

 1from greenpak import driver, i2c
 2
 3i2c_driver = i2c.GreenPakI2cAdapter(port = "/dev/tty.usbmodem1101")
 4gp_driver = driver.GreenpakDriver(i2c_driver, device_type="SLG46826", device_control_code=0b0001)
 5
 6# Scan for I2C devices, printing their addresses.
 7print("Scanning for I2C addresses:")
 8for addr in range(0, 128):
 9    if i2c_driver.write(addr, bytearray(), silent=True):
10        print(f"* I2C device at address 0x{addr:02x}")
11
12# Scan for GreenPak devices, printing their control codes. Each GreenPak devices
13# occupies 4 consecutive I2C addresses, one for each of its memory spaces.
14print("Scanning for GreenPak control codes:")
15for control_code in gp_driver.scan_greenpak_devices():
16    print(f"* Potential GreenPak device at control code 0x{control_code:02x}")

Assigning initial addresses

Initial in-circuit programming of three factory reset GreenPak devices, one of type SLG47004 and two of type SLG46826. This is an experimental example that may requires some tweaks.

 1from greenpak import driver, i2c
 2
 3def scan():
 4  for control_code in gp_driver.scan_greenpak_devices():
 5    print(f"* Found control code {control_code}")
 6  for addr in range(0, 128):
 7    if i2c_driver.write(addr, bytearray(), silent=True):
 8        print(f"* Found I2C address 0x{addr:02x}")
 9
10# Initially all three devices respond to control code 1.
11i2c_driver = i2c.GreenPakI2cAdapter(port = "/dev/tty.usbmodem1101")
12gp_driver = driver.GreenpakDriver(i2c_driver, device_type="SLG46826", device_control_code=0b0001)
13
14# At this points, all the devices are at the Renesas's default control code 1.
15print("Scan before:")
16scan()
17
18# Move the devices to their permanent address. In this example we assume that each device
19# has two control code input pins that are hard wired to a unique combination.
20for device_type in ["SLG47004", "SLG46826"]:
21    if gp_driver.scan_greenpak_device(0b0001):
22        gp_driver.set_device_type(device_type)
23        gp_driver.program_control_code("01XX")
24        gp_driver.reset_device()
25
26# Here the devices are at their designated addresses and can be programmed individually.
27print("Scan after:")
28scan()
29assert not gp_driver.scan_greenpak_device(0b0001)

Installation

The Python API package is available from PyPi at https://pypi.org/project/greenpak and can be installed on your computer using pip:

pip install greenpak

API Reference

Utilities to handles GreenPak configurations.

greenpak.utils.read_hex_config_file(file_name: str) bytes

Load GreenPak configuration from a hex file.

Parameters:

file_name (str) – Path to the input intelhex file. The file is expected to have exactly 256 byte values for the address range [0, 255].

Returns:

The 256 configuration bytes.

Return type:

bytearray.

greenpak.utils.write_hex_config_file(file_name: str, data: bytearray | bytes) bytes

Read GreenPak config from a hex file.

Parameters:
  • file_name (str) – Path to the output intelhex file.

  • data (bytearray or bytes) – The config bytes to write. Should have exactly 256 bytes.

Returns:

None

greenpak.utils.read_bits_config_file(file_path: str) bytearray

Read a GreenPak SPLD config file.

Reads the config file, converts to configuration bytes, and asserts that there were no errors. Config files are text files with the values of the GreenPak configuration bits. These are the output files of the Renesas GreenPAK Designer.

Parameters:

file_path (str) – Path to the file to read.

Returns:

The configuration bits as a bytearray of 256 bytes, in the same representation as the GreenPak’s NVM memory.

Return type:

bytearray

greenpak.utils.write_bits_config_file(file_name: str, data: bytearray | bytes) None

Write a GreenPak SPLD config file.

Writes the given configuration bytes, in the same representation as the GreenPak NVM memory, as a GreenPak config file. Config files are text files with the values of the GreenPak configuration bits. These are th output files of the Renesas GreenPAK Designer.

Parameters:
  • file_path (str) – Path to the output file.

  • data (bytearray or bytes) – The configuration bytes to write. len(data) is asserted to be 256.

greenpak.utils.hex_dump(data: bytearray | bytes, start_addr: int = 0) None

Print bytes in hex format.

This utility help function is useful to print binary data such as GreenPak configuration bytes.

Parameters:
  • data (bytearray or bytes) – The bytes to dump.

  • start_addr (int) – Allows to assign an index other than zero to the first byte.

.


Contact

Bug reports and contributions are welcome. You can contact the team and fellow users at the gibhub repository at https://github.com/zapta/greenpak.