firmware_tools/utilities/sine_seq_gen.py
2018-04-03 09:24:25 +01:00

127 lines
4.5 KiB
Python

# -----------------------------------------------------------------------------
# Title : Waveform sequencer sinusoid program generator
# Project : Generic FW tools
# -----------------------------------------------------------------------------
# File : sie_seq_gen.py
# Author : Simon Southwell
# Company : Global Inkjet Systems
# Created : 2018-03-14
# Last update: 2018-03-14
# Platform :
# Standard : Python 3.x
# -----------------------------------------------------------------------------
# Description:
# Generates the instructions for a waveform sequencer to generate a sinusoid
# with a specification set from command line arguments
# -----------------------------------------------------------------------------
# Copyright (c) 2018 Global Inkjet Systems
# -----------------------------------------------------------------------------
import os
import sys
import argparse
import math
# --------------------------------------------------------------
# Parse the command line arguments
#
def processCmdLine():
# Create a parser object
parser = argparse.ArgumentParser(description='Process command line options.')
parser.add_argument('-p', '--points', dest='points', default='8', action='store', help='Number of points')
parser.add_argument('-w', '--wait_cycles', dest='waitCycles', default='10', action='store', help='Number of wait cycles')
parser.add_argument('-a', '--amplitude', dest='amplitude', default='2048', action='store', help='Amplitude')
parser.add_argument('-o', '--offset', dest='offset', default='split', action='store', help='offset')
parser.add_argument('-v', '--vhdl_fmt', dest='vhdlFmt', default=False, action='store_true', help='VHDL format')
return parser.parse_args()
# --------------------------------------------------------------
def genCmdStr(cmdPrefixStr, addrInt, opInt, vhdlFmt):
cmdStr = ''
if vhdlFmt:
cmdStr += ' ' + str(int(addrInt / 4)) + ' => 32x"'
else:
cmdStr += '0x'
cmdStr += cmdPrefixStr + format(opInt, '03x')
if vhdlFmt:
cmdStr += '",'
return cmdStr
# ###############################################################
# Only run if not imported (to test)
#
if __name__ == '__main__':
# Process the command line options
cmdArgs = processCmdLine()
MAX_VAL = 16383
MAX_WAITS = 4095
offset = 0
if not cmdArgs.waitCycles.isdigit() or int(cmdArgs.waitCycles) < 0 or int(cmdArgs.waitCycles) > MAX_WAITS :
print('***Error: wait cycles value out of range (valid 0 to ' + str(MAX_WAITS) + ')\n')
sys.exit(1)
if not cmdArgs.points.isdigit() or int(cmdArgs.points) < 4 or int(cmdArgs.points) > 64 :
print('***Error: Number of points value out of range (valid 4 to 64)\n')
sys.exit(1)
if not cmdArgs.amplitude.isdigit() or int(cmdArgs.amplitude) < 2 or int(cmdArgs.amplitude) > MAX_VAL :
print('***Error: amplitude value out of range (valid 2 to 4095)\n')
sys.exit(1)
if not cmdArgs.offset.isdigit() == 'split' :
offset = -1
elif int(cmdArgs.offset) < 2 or int(cmdArgs.offset) > MAX_VAL :
print('***Error: offset value out of range (valid 2 to ' + str(MAX_VAL) + ')\n')
sys.exit(1)
addr = 0
numPoints = int(cmdArgs.points)
ampDiv2 = float(int(cmdArgs.amplitude)/2)
if offset == -1 :
offset = ampDiv2
else :
offset = int(cmdArgs.offset)
phase = math.radians(270.0)
phaseStep = 2*math.pi / float(numPoints)
if cmdArgs.vhdlFmt :
print('constant SEQ_PATTERN : mem_t := (')
cmdStr = genCmdStr('10000', addr, 1, cmdArgs.vhdlFmt)
print(cmdStr)
addr += 4
for idx in range(numPoints) :
sinFloat = offset + ampDiv2 * math.sin(phase)
if sinFloat < 0 :
sinFloat = 0
elif sinFloat > MAX_VAL :
sinFloat = MAX_VAL
cmdStr = genCmdStr('30000', addr, int(round(sinFloat)), cmdArgs.vhdlFmt)
print(cmdStr)
phase += phaseStep
addr += 4
if int(cmdArgs.waitCycles) != 0 or idx != numPoints - 1 :
cmdStr = genCmdStr('20000', addr, int(cmdArgs.waitCycles), cmdArgs.vhdlFmt)
print(cmdStr)
addr += 4
cmdStr = genCmdStr('c0000', addr, 1, cmdArgs.vhdlFmt)
print(cmdStr)
if cmdArgs.vhdlFmt:
print(' others => 32x"0");')