diff --git a/sw/silta/stm32f407.py b/sw/silta/stm32f407.py index 09fea9b..adf81ea 100644 --- a/sw/silta/stm32f407.py +++ b/sw/silta/stm32f407.py @@ -87,6 +87,11 @@ class bridge(Silta): DEBUG = False + # Hardcoding until we can read values back from device + __CMD_MAX_STR_LEN = 4095 + __SPI_MAX_BYTES = 1024 + __I2C_MAX_BYTES = 1024 + def __init__(self, serial_device, baud_rate=None): ''' Initialize Silta STM32F407 Bridge @@ -143,6 +148,10 @@ def close(self): # Send terminal command and wait for response def __send_cmd(self, cmd): + + if (len(cmd) + 1) > self.__CMD_MAX_STR_LEN: + raise RuntimeException('Command string too long') + self.stream.write(cmd + '\n') if self.DEBUG is True: print 'CMD : ' + cmd @@ -183,6 +192,12 @@ def i2c(self, addr, rlen, wbytes = []): List with read bytes (or empty list if write-only command) ''' + if len(wbytes) > self.__I2C_MAX_BYTES: + raise ValueError('wbytes too long. Max:', self.__I2C_MAX_BYTES) + + if rlen > self.__I2C_MAX_BYTES: + raise ValueError('rlen too long. Max:', self.__I2C_MAX_BYTES) + rbytes = [] cmd = 'i2c ' + format(addr, '02X') + ' ' + str(rlen) @@ -242,6 +257,9 @@ def spi(self, cspin, wbytes = []): or List of read bytes ''' + if len(wbytes) > self.__SPI_MAX_BYTES: + raise ValueError('wbytes too long. Max:', self.__SPI_MAX_BYTES) + rbytes = [] # Make sure the CS pin is selected @@ -254,7 +272,6 @@ def spi(self, cspin, wbytes = []): line = self.__send_cmd(cmd) - result = line.strip().split(' ') if result[0] == 'OK':