Update. I was finally able to get the script running with the CW1200. I’ll paste the code below. It’s probably bad Python, but since it works, might end up being useful for someone else.
Now the bad news: it’s been running for over 30 hours now. I am a measurement #62767 right now and every single line of the CSV is an “Expected” one so far…
So here’s the code.
from enum import Enum
from tqdm import tnrange
import matplotlib
import matplotlib.pyplot as plt
from collections import namedtuple
import time
import chipwhisperer as cw
import csv
import re
from datetime import datetime
def reset_target(scope):
if PLATFORM == "CW303" or PLATFORM == "CWLITEXMEGA":
scope.io.pdic = 'low'
time.sleep(0.05)
scope.io.pdic = 'high_z' #XMEGA doesn't like pdic driven high
time.sleep(0.05)
else:
scope.io.nrst = 'low'
time.sleep(0.05)
scope.io.nrst = 'high'
time.sleep(0.05)
def rejection_builder():
Range = namedtuple('Range', ['min', 'max', 'step'])
step_size = (1/512)*100
#step_size = 1
min_value = -49
max_value = min_value*-1
#change the values here:
width_range = Range(min_value, max_value, step_size)
offset_range = Range(min_value, max_value, step_size)
loop_ext_offset = 2185
scope.glitch.ext_offset = loop_ext_offset
loop_ext_max = 2186
dataReset = [[],[]]
dataSuccess = [[],[]]
dataExpected = [[],[]]
print("Starting loop: ")
#starting points
loop_offset = offset_range.min
loop_width = width_range.min
counter = 0
timestr = time.strftime("%Y-%m-%d__%H%M%S")
csv_file_name = 'rejection_builder_log' + timestr + ".csv"
f = open(csv_file_name,'w')
f.write("Time[s],EXT_Offset, Offset, Width, Result, Raw Line \n")
starting_time = datetime.utcnow()
while loop_ext_offset < loop_ext_max:
loop_offset = offset_range.min
while loop_offset < offset_range.max:
loop_width = width_range.min
while loop_width < width_range.max:
time_now = datetime.utcnow()
print("Last run at: {}".format(time_now.strftime("%H:%M:%S")))
date_time_delta = (time_now - starting_time)
seconds_elapsed = str(date_time_delta.total_seconds())
scope.glitch.width = loop_width
scope.glitch.offset = loop_offset
scope.glitch.trigger_src = "ext_single"
scope.glitch.repeat = 9
counter += 1
loop_width += width_range.step
try:
result, line = test_for_success(3)
except:
setup()
if result == Results.Success:
#if counter % 3:
dataSuccess[0].append(loop_offset)
dataSuccess[1].append(loop_width)
print("Loop width: {}, Loop offset: {} Loop ext_offset: {} Result: {} Raw Line: {}" .format(loop_width, loop_offset, loop_ext_offset, result, line))
print("success")
elif result == Results.Expected:
#elif counter % 2:
dataExpected[0].append(loop_offset)
dataExpected[1].append(loop_width)
elif result == Results.Reset:
#else:
dataReset[0].append(loop_offset)
dataReset[1].append(loop_width)
line_list = list(map(str.strip,line))
line = ''.join(line_list)
result_string = str(result).strip("Result.")
f.write("{},{},{},{},{},{}\n".format(seconds_elapsed, loop_ext_max, loop_offset, loop_width, result_string, line))
#print("Loop width: {}, Loop offset: {} Loop ext_offset: {} Result: {}" .format(loop_width, loop_offset, loop_ext_offset, result))
loop_offset += offset_range.step
loop_ext_offset += 1
f.close()
plt.scatter(dataReset[0], dataReset[1], marker='o')
plt.scatter(dataSuccess[0], dataSuccess[1], marker='x')
plt.scatter(dataExpected[0], dataExpected[1], marker='v')
#plt.scatter(dataSuccess[0], dataSuccess[1], 'r--', dataReset[0], dataReset[1], 'bs', dataExpected[0], dataExpected[1], 'g^')
plt.show()
class Results(Enum):
Reset = 0
Expected = 1
Success = 2
# Looks for a gltiched output
# If a success is detected it will break regardless of attempts left
# in order to reduce to a single data point of parameters
def test_for_success(attempts):
reset_target(scope)
target.flush()
time.sleep(1)
scope.arm()
if SCOPETYPE == "OPENADC":
scope.glitch.trigger_src = "ext_continuous"
result = Results.Expected
line = ""
for j in range(attempts):
line = ""
while "\n" not in line:
time.sleep(0.1)
line += target.read()
lines = line.split("\n")
if len(lines) > 1:
line = lines[-1]
else:
line = ""
while "\n" not in line:
time.sleep(0.1)
line += target.read()
if "hello" in line:
result = Results.Reset
nums = line.split(" ")
try:
if (int(nums[0]) != 40000 or int(nums[1]) != 200 or int(nums[2]) != 200 ) and nums[0] != '':
result = Results.Success
break
except ValueError as e:
if(result != Results.Success):
continue
else:
break
if SCOPETYPE == "OPENADC":
scope.glitch.trigger_src = "ext_single"
return result, line
fw_path = "../hardware/victims/firmware/glitch-simple/glitchsimple-{}.hex".format(PLATFORM)
prog = cw.programmers.STM32FProgrammer
scope = cw.scope()
time.sleep(0.05)
scope.default_setup()
reset_target(scope)
print(fw_path)
cw.program_target(scope, prog, fw_path)
target = cw.target(scope)
#setup()
rejection_builder()