Analog Devices / Mbed OS EVAL-AD7689

Dependencies:   sdp_k1_sdram

Committer:
Mahesh Phalke
Date:
Thu Jul 21 16:45:24 2022 +0530
Revision:
2:007533849deb
Initial firmware commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mahesh Phalke 2:007533849deb 1 import numpy as np
Mahesh Phalke 2:007533849deb 2 from time import sleep
Mahesh Phalke 2:007533849deb 3 from pynput import keyboard
Mahesh Phalke 2:007533849deb 4 import sys
Mahesh Phalke 2:007533849deb 5 import select
Mahesh Phalke 2:007533849deb 6 import os
Mahesh Phalke 2:007533849deb 7 import csv
Mahesh Phalke 2:007533849deb 8 import math
Mahesh Phalke 2:007533849deb 9 from adi import ad7689
Mahesh Phalke 2:007533849deb 10
Mahesh Phalke 2:007533849deb 11 # Global variables
Mahesh Phalke 2:007533849deb 12 line = 0
Mahesh Phalke 2:007533849deb 13 writer = 0
Mahesh Phalke 2:007533849deb 14 run_continuous = False
Mahesh Phalke 2:007533849deb 15 iterations = 0
Mahesh Phalke 2:007533849deb 16 data_capture_abort = False
Mahesh Phalke 2:007533849deb 17 chn_count = 0
Mahesh Phalke 2:007533849deb 18 data_list = []
Mahesh Phalke 2:007533849deb 19
Mahesh Phalke 2:007533849deb 20 def key_press_event(key):
Mahesh Phalke 2:007533849deb 21 global data_capture_abort
Mahesh Phalke 2:007533849deb 22 data_capture_abort = True
Mahesh Phalke 2:007533849deb 23
Mahesh Phalke 2:007533849deb 24 def init_data_capture():
Mahesh Phalke 2:007533849deb 25 global device
Mahesh Phalke 2:007533849deb 26 global data_list
Mahesh Phalke 2:007533849deb 27 global chn_count
Mahesh Phalke 2:007533849deb 28 global listener
Mahesh Phalke 2:007533849deb 29 global samples_block
Mahesh Phalke 2:007533849deb 30
Mahesh Phalke 2:007533849deb 31 ######## User configuration ##########
Mahesh Phalke 2:007533849deb 32 # Configure the backend for PC to IIOD interface
Mahesh Phalke 2:007533849deb 33 uri = "serial:COM11,230400" # For UART, baud rate must be same as set in the FW. COM port is physical Or VCOM.
Mahesh Phalke 2:007533849deb 34 device_name = "ad7689" # Name of the device must be same as set in the FW.
Mahesh Phalke 2:007533849deb 35 ######################################
Mahesh Phalke 2:007533849deb 36
Mahesh Phalke 2:007533849deb 37 # Create an IIO device context
Mahesh Phalke 2:007533849deb 38 device = ad7689(uri, device_name)
Mahesh Phalke 2:007533849deb 39 device._ctx.set_timeout(100000)
Mahesh Phalke 2:007533849deb 40
Mahesh Phalke 2:007533849deb 41 ######## User configuration ##########
Mahesh Phalke 2:007533849deb 42 # Channels to be captured
Mahesh Phalke 2:007533849deb 43 device.rx_enabled_channels = ["voltage0", "voltage1", "voltage2", "voltage3", "voltage4", "voltage5", "voltage6", "voltage7", "temp8"]
Mahesh Phalke 2:007533849deb 44
Mahesh Phalke 2:007533849deb 45 # The block of samples to be captured. Total samples are received in multiple iterations or blocks
Mahesh Phalke 2:007533849deb 46 samples_block = 400 # The samples needs to be captured in smaller blocks due to limitations
Mahesh Phalke 2:007533849deb 47 # of buffer size (RAM) in the firmware and IIO client timeout factor.
Mahesh Phalke 2:007533849deb 48 # Request to capture samples more than buffer size, will be ignored by firmware.
Mahesh Phalke 2:007533849deb 49 # Large time taken to read the samples from device, may cause timeout on IIO client.
Mahesh Phalke 2:007533849deb 50 ######################################
Mahesh Phalke 2:007533849deb 51
Mahesh Phalke 2:007533849deb 52 # Get the channels count from user
Mahesh Phalke 2:007533849deb 53 chn_count = len(device.rx_enabled_channels)
Mahesh Phalke 2:007533849deb 54
Mahesh Phalke 2:007533849deb 55 # Store the rx buffer size and rx data type based on input channels
Mahesh Phalke 2:007533849deb 56 device.rx_buffer_size = samples_block # Size of the IIO buffer (buffer is submitted during call to rx() method)
Mahesh Phalke 2:007533849deb 57 device._rx_data_type = np.uint16 # size of ADC sample
Mahesh Phalke 2:007533849deb 58
Mahesh Phalke 2:007533849deb 59 listener = keyboard.Listener(on_press=key_press_event)
Mahesh Phalke 2:007533849deb 60 listener.start()
Mahesh Phalke 2:007533849deb 61
Mahesh Phalke 2:007533849deb 62 def read_user_inputs():
Mahesh Phalke 2:007533849deb 63 global iterations
Mahesh Phalke 2:007533849deb 64 global run_continuous
Mahesh Phalke 2:007533849deb 65 global device
Mahesh Phalke 2:007533849deb 66 global samples_block
Mahesh Phalke 2:007533849deb 67
Mahesh Phalke 2:007533849deb 68 samples_count = int(input("Enter the number of samples to be captured \n\
Mahesh Phalke 2:007533849deb 69 0: Unlimited \n\
Mahesh Phalke 2:007533849deb 70 <50-1000000>: "))
Mahesh Phalke 2:007533849deb 71
Mahesh Phalke 2:007533849deb 72 if (samples_count == 0):
Mahesh Phalke 2:007533849deb 73 run_continuous = True
Mahesh Phalke 2:007533849deb 74 else:
Mahesh Phalke 2:007533849deb 75 run_continuous = False
Mahesh Phalke 2:007533849deb 76 if (samples_count <= samples_block):
Mahesh Phalke 2:007533849deb 77 device.rx_buffer_size = samples_count
Mahesh Phalke 2:007533849deb 78 iterations = 1
Mahesh Phalke 2:007533849deb 79 else:
Mahesh Phalke 2:007533849deb 80 iterations = math.ceil(samples_count / samples_block)
Mahesh Phalke 2:007533849deb 81
Mahesh Phalke 2:007533849deb 82 def init_data_logger():
Mahesh Phalke 2:007533849deb 83 global writer
Mahesh Phalke 2:007533849deb 84 global chn_count
Mahesh Phalke 2:007533849deb 85
Mahesh Phalke 2:007533849deb 86 file_name = "adc_data_capture.csv"
Mahesh Phalke 2:007533849deb 87 current_dir = os.getcwd()
Mahesh Phalke 2:007533849deb 88 output_file = os.path.join(current_dir, file_name)
Mahesh Phalke 2:007533849deb 89 result_file = open(output_file, 'w', newline="")
Mahesh Phalke 2:007533849deb 90 writer = csv.writer(result_file)
Mahesh Phalke 2:007533849deb 91 row = []
Mahesh Phalke 2:007533849deb 92 # Write the channels list header
Mahesh Phalke 2:007533849deb 93 for chn in range(0,chn_count):
Mahesh Phalke 2:007533849deb 94 item = "Ch {}".format(chn)
Mahesh Phalke 2:007533849deb 95 row.insert(chn, item)
Mahesh Phalke 2:007533849deb 96 writer.writerow(row)
Mahesh Phalke 2:007533849deb 97
Mahesh Phalke 2:007533849deb 98 def read_buffered_data():
Mahesh Phalke 2:007533849deb 99 global line
Mahesh Phalke 2:007533849deb 100 global writer
Mahesh Phalke 2:007533849deb 101 global device
Mahesh Phalke 2:007533849deb 102
Mahesh Phalke 2:007533849deb 103 # Receive data from device
Mahesh Phalke 2:007533849deb 104 data = device.rx()
Mahesh Phalke 2:007533849deb 105 if (line == 0):
Mahesh Phalke 2:007533849deb 106 print("Data capture started >>")
Mahesh Phalke 2:007533849deb 107 print("."*line, end="\r")
Mahesh Phalke 2:007533849deb 108
Mahesh Phalke 2:007533849deb 109 if (chn_count == 1):
Mahesh Phalke 2:007533849deb 110 # Convert 1-D array to 2-D array
Mahesh Phalke 2:007533849deb 111 data_arr = np.reshape(data, (-1,1))
Mahesh Phalke 2:007533849deb 112 else:
Mahesh Phalke 2:007533849deb 113 # Transpose data from N-D data array
Mahesh Phalke 2:007533849deb 114 data_arr = np.transpose(data)
Mahesh Phalke 2:007533849deb 115
Mahesh Phalke 2:007533849deb 116 writer.writerows(data_arr)
Mahesh Phalke 2:007533849deb 117
Mahesh Phalke 2:007533849deb 118 line = line+1
Mahesh Phalke 2:007533849deb 119 if (line == 100):
Mahesh Phalke 2:007533849deb 120 line = 1
Mahesh Phalke 2:007533849deb 121 print("\n", end="\r")
Mahesh Phalke 2:007533849deb 122
Mahesh Phalke 2:007533849deb 123 def do_data_capture():
Mahesh Phalke 2:007533849deb 124 global data_capture_abort
Mahesh Phalke 2:007533849deb 125 done = False
Mahesh Phalke 2:007533849deb 126
Mahesh Phalke 2:007533849deb 127 if (run_continuous == True):
Mahesh Phalke 2:007533849deb 128 print("Press any key to stop data capture..")
Mahesh Phalke 2:007533849deb 129 sleep(2)
Mahesh Phalke 2:007533849deb 130 data_capture_abort = False
Mahesh Phalke 2:007533849deb 131 while not data_capture_abort:
Mahesh Phalke 2:007533849deb 132 read_buffered_data()
Mahesh Phalke 2:007533849deb 133 else:
Mahesh Phalke 2:007533849deb 134 for val in range(0,iterations):
Mahesh Phalke 2:007533849deb 135 read_buffered_data()
Mahesh Phalke 2:007533849deb 136
Mahesh Phalke 2:007533849deb 137 print("\r\nData capture finished\r\n")
Mahesh Phalke 2:007533849deb 138
Mahesh Phalke 2:007533849deb 139 def exit():
Mahesh Phalke 2:007533849deb 140 global listener
Mahesh Phalke 2:007533849deb 141 global writer
Mahesh Phalke 2:007533849deb 142 global device
Mahesh Phalke 2:007533849deb 143
Mahesh Phalke 2:007533849deb 144 # Delete the objects
Mahesh Phalke 2:007533849deb 145 del listener
Mahesh Phalke 2:007533849deb 146 del writer
Mahesh Phalke 2:007533849deb 147 del device
Mahesh Phalke 2:007533849deb 148
Mahesh Phalke 2:007533849deb 149 def main():
Mahesh Phalke 2:007533849deb 150 init_data_capture()
Mahesh Phalke 2:007533849deb 151 init_data_logger()
Mahesh Phalke 2:007533849deb 152 read_user_inputs()
Mahesh Phalke 2:007533849deb 153 do_data_capture()
Mahesh Phalke 2:007533849deb 154 exit()
Mahesh Phalke 2:007533849deb 155
Mahesh Phalke 2:007533849deb 156 if __name__ == "__main__":
Mahesh Phalke 2:007533849deb 157 main()