Skip to content

Instantly share code, notes, and snippets.

@terryoy
Created May 15, 2014 08:28
Show Gist options
  • Save terryoy/e67de419441bc7722422 to your computer and use it in GitHub Desktop.
Save terryoy/e67de419441bc7722422 to your computer and use it in GitHub Desktop.
Python Command Wrapper of Texas Instrument Smart RF Flash Programmer on Windows
#!/bin/python
import subprocess, sys, re
PROGRAM_NAME = 'SmartRFProgConsole.exe'
FIRMWARE_NAME = 'test.hex'
# The Return Codes:
# 0 = Success
# 1 = System error
# 2 = Parameter error
# 3 = Illegal parameter combinations
# 4 = Missing parameters
# 5 = Missing image file or communication error
def run_command(cmdargs):
"""Execute an OS program with arguments(in list), and return the (stdout, stderr) result"""
p = subprocess.Popen(cmdargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = p.communicate()
return p.returncode, output[0], output[1]
def write_firmware(firmware_name):
"""Execute command to write the firmware image into flash"""
try:
code, stdout, stderr = run_command([PROGRAM_NAME, 'S', 'EPV', 'F=%s' % firmware_name])
if code != 0:
# log error
print 'program error(return_code=%d), reason: %s' % (code, stderr)
else:
print 'Firmware image burned successfully.'
except:
print 'program error:', sys.exc_info(), '\n'
def receive_mac_addr():
"""Execute command to get the mac address of the chip"""
MAC_ADDR_REGEX = r'IEEE address: ([\w\.]{17})'
try:
code, stdout, stderr = run_command([PROGRAM_NAME, 'S', 'RI(F=256, PRI)'])
if code != 0:
# log error
print 'program error(return_code=%d). reason: %s' % (code, stderr)
else:
# successfully
matches = re.findall(MAC_ADDR_REGEX, stdout)
if len(matches) == 1:
mac_addr = matches[0].replace('.', '')
print 'Receive MAC:', mac_addr
# save mac address
return mac_addr
else:
print 'None or more MAC address found:', matches
print stdout
except:
print 'program error:', sys.exc_info(), '\n'
def batch_job():
print '--- Starting the Firmware Flashing Process ---'
print '>> Getting MAC address from the device...'
receive_mac_addr()
print ' '
print '>> Flashing the firmware to the device...'
print 'image:', FIRMWARE_NAME
write_firmware(FIRMWARE_NAME)
if __name__ == '__main__':
batch_job()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment