|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import signal |
| 5 | +import sys |
| 6 | +import time |
| 7 | + |
| 8 | +from aardvark_py import * |
| 9 | + |
| 10 | +done = False |
| 11 | +contents = None |
| 12 | + |
| 13 | + |
| 14 | +def parse_args(): |
| 15 | + global args |
| 16 | + parser = argparse.ArgumentParser(description='Aardvark I2C "Storage Device"') |
| 17 | + parser.add_argument("-p", "--port", default=0, help="Aardvark port number") |
| 18 | + parser.add_argument("device_addr", type=str, help="I2C device address") |
| 19 | + parser.add_argument("file", type=str, help="File to provide") |
| 20 | + parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output") |
| 21 | + args = parser.parse_args() |
| 22 | + |
| 23 | + |
| 24 | +def signal_handler(sig, frame): |
| 25 | + global done |
| 26 | + global contents |
| 27 | + |
| 28 | + if sig == signal.SIGINT: |
| 29 | + done = True |
| 30 | + elif sig == signal.SIGQUIT: |
| 31 | + print("Reloading file") |
| 32 | + with open(args.file, "rb") as f: |
| 33 | + contents = f.read() |
| 34 | + |
| 35 | + |
| 36 | +def watch(handle): |
| 37 | + print("Watching I2C data (Use ^\\ to reload file) ...") |
| 38 | + |
| 39 | + # Loop until aa_async_poll times out |
| 40 | + while True: |
| 41 | + result = aa_async_poll(handle, 100) |
| 42 | + # Read the I2C message. |
| 43 | + if (result & AA_ASYNC_I2C_READ) == AA_ASYNC_I2C_READ: |
| 44 | + # Get data written by master |
| 45 | + (num_bytes, addr, data_in) = aa_i2c_slave_read(handle, 6) |
| 46 | + |
| 47 | + if num_bytes < 0: |
| 48 | + print("slave_read error: %s" % aa_status_string(num_bytes)) |
| 49 | + return |
| 50 | + |
| 51 | + if num_bytes == 0: |
| 52 | + continue |
| 53 | + |
| 54 | + if data_in[0] == 0x1: |
| 55 | + if args.verbose: |
| 56 | + print( |
| 57 | + f"Got 0x1, asking size of file. Responding with {len(contents)}" |
| 58 | + ) |
| 59 | + ret = aa_i2c_slave_set_response( |
| 60 | + handle, |
| 61 | + array( |
| 62 | + "B", |
| 63 | + [ |
| 64 | + (len(contents) >> 24) & 0xFF, |
| 65 | + (len(contents) >> 16) & 0xFF, |
| 66 | + (len(contents) >> 8) & 0xFF, |
| 67 | + len(contents) & 0xFF, |
| 68 | + ], |
| 69 | + ), |
| 70 | + ) |
| 71 | + elif data_in[0] == 0x2: |
| 72 | + addr = ( |
| 73 | + data_in[1] << 24 | data_in[2] << 16 | data_in[3] << 8 | data_in[4] |
| 74 | + ) |
| 75 | + size = data_in[5] |
| 76 | + |
| 77 | + if args.verbose: |
| 78 | + print(f"Got 0x2, asking for data, at {addr} with size {size}.") |
| 79 | + |
| 80 | + if addr < 0 or addr + size > len(contents): |
| 81 | + print("Requested data is out of bounds, responding with 0x0") |
| 82 | + ret = aa_i2c_slave_set_response(handle, array("B", [0x0])) |
| 83 | + else: |
| 84 | + ret = aa_i2c_slave_set_response( |
| 85 | + handle, array("B", contents[addr : addr + size]) |
| 86 | + ) |
| 87 | + else: |
| 88 | + print("Got unknown data, responding with 0x0") |
| 89 | + ret = aa_i2c_slave_set_response(handle, array("B", [0x0])) |
| 90 | + |
| 91 | + elif (result & AA_ASYNC_I2C_WRITE) == AA_ASYNC_I2C_WRITE: |
| 92 | + # Get number of bytes written to master |
| 93 | + num_bytes = aa_i2c_slave_write_stats(handle) |
| 94 | + |
| 95 | + if num_bytes < 0: |
| 96 | + print("slave_write_stats error: %s" % aa_status_string(num_bytes)) |
| 97 | + return |
| 98 | + |
| 99 | + # Print status information to the screen |
| 100 | + if args.verbose: |
| 101 | + print(f"Number of bytes written to master: {num_bytes:04}\n") |
| 102 | + |
| 103 | + elif result == AA_ASYNC_NO_DATA: |
| 104 | + if done: |
| 105 | + break |
| 106 | + else: |
| 107 | + print("error: non-I2C asynchronous message is pending", result) |
| 108 | + return |
| 109 | + |
| 110 | + |
| 111 | +def main(): |
| 112 | + global contents |
| 113 | + |
| 114 | + parse_args() |
| 115 | + |
| 116 | + signal.signal(signal.SIGINT, signal_handler) |
| 117 | + signal.signal(signal.SIGQUIT, signal_handler) |
| 118 | + |
| 119 | + port = args.port |
| 120 | + addr = int(args.device_addr, 0) |
| 121 | + |
| 122 | + # Open the device |
| 123 | + handle = aa_open(port) |
| 124 | + if handle <= 0: |
| 125 | + print("Unable to open Aardvark device on port %d" % port) |
| 126 | + print("Error code = %d" % handle) |
| 127 | + sys.exit() |
| 128 | + |
| 129 | + # Ensure that the I2C subsystem is enabled |
| 130 | + aa_configure(handle, AA_CONFIG_SPI_I2C) |
| 131 | + |
| 132 | + # Disable the I2C bus pullup resistors (2.2k resistors). |
| 133 | + # This command is only effective on v2.0 hardware or greater. |
| 134 | + # The pullup resistors on the v1.02 hardware are enabled by default. |
| 135 | + aa_i2c_pullup(handle, AA_I2C_PULLUP_NONE) |
| 136 | + |
| 137 | + # Power the EEPROM using the Aardvark adapter's power supply. |
| 138 | + # This command is only effective on v2.0 hardware or greater. |
| 139 | + # The power pins on the v1.02 hardware are not enabled by default. |
| 140 | + aa_target_power(handle, AA_TARGET_POWER_BOTH) |
| 141 | + |
| 142 | + # Set default response |
| 143 | + aa_i2c_slave_set_response(handle, array("B", [0])) |
| 144 | + |
| 145 | + # Enabled the device |
| 146 | + aa_i2c_slave_enable(handle, addr, 64, 6) |
| 147 | + |
| 148 | + # Read the file |
| 149 | + with open(args.file, "rb") as f: |
| 150 | + contents = f.read() |
| 151 | + |
| 152 | + # Watch the I2C port |
| 153 | + watch(handle) |
| 154 | + |
| 155 | + # Disable and close the device |
| 156 | + aa_i2c_slave_disable(handle) |
| 157 | + aa_close(handle) |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == "__main__": |
| 161 | + main() |
0 commit comments