This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/AFE4300EVM-PDK: AFE4300EVM-RASPBERRY PI

Part Number: AFE4300EVM-PDK

Tool/software: TI C/C++ Compiler

Good morning, I need help communicating the AFE4300EVM card with the Raspberry pùPi 4. I'm communicating with the card via the SPI protocol. I wrote my own code to read the bioimpedance in python and I'll put my script on it. I also take a picture of you with the links I made. my problem is that from the ADC_DATA_RESULT I always get zero. I kindly ask you if the connections I made are right? and at the code level where it might be the problem that I always get zero. Thank you so much in advance
import sys
import time
import math
import os
import csv
import pigpio


# GPIO settings
CLK_PIN = 4
CLK_FREQ = 100000
RDY_PIN = 16
# Init GPIO
pi = pigpio.pi()

# SPI settings
SPI_FREQ = 100000
mode = 1
spi_handle = None

# Registers
DEVICE_CONTROL1 = 0x09
DEVICE_CONTROL2 = 0x0F
ADC_DATA_RESULT = 0x00
ADC_CONTROL_REGISTER1 = 0x01
ADC_CONTROL_REGISTER2 = 0x10
ISW_MUX = 0x0A
VSENSE_MUX = 0x0B
BCM_DAC_FREQ = 0x0E
MISC_REGISTER1 = 0x02
MISC_REGISTER2 = 0x03
MISC_REGISTER3 = 0x1A
WEIGHT_SCALE_CONTROL = 0x0D
IQ_MODE_ENABLE = 0x0C


def read_device(address):
  packet = []
  packet.append(0x20 | (0x1f & address)) # the lower five bits [20:16] are the real address bits
  packet.append(0x00)
  packet.append(0x00)
  (count, data) = pi.spi_xfer(spi_handle, packet)
  print("SPI Read. Data sent: " + (','.join(format(p, "04X") for p in packet)))
  if count < 3:
    print("Error reading from SPI. Data received: " + str(count) + " bytes: " + (','.join(format(d, "#04X") for d in data)))
  print("SPI Read. Data received: " + str(count) + " bytes: " + (','.join(format(d, "#04X") for d in data)))
  return (0xff00 & (data[1]<<8)) | (0xff & packet[2])

def write_device(address, data):
  packet = []
  packet.append(0x1f & address);
  packet.append(0xff & (data >> 8));
  packet.append(0xff & data);
  # print(','.join(format(p, "#04X") for p in packet))
  # print(spi_handle)
  (count, data) = pi.spi_xfer(spi_handle, packet)
  print("SPI Write. Data sent: " + (','.join(format(p, "04X") for p in packet)))
  if count < 3:
    print("Error writing to SPI. Data received: " + str(count) + " bytes: " + (','.join(format(d, "#04X") for d in data)))
  print("SPI Write. Data received: " + str(count) + " bytes: " + (','.join(format(d, "#04X") for d in data)))

def init_device():
  print("Init device:")
  pi.hardware_clock(CLK_PIN, CLK_FREQ)
  pi.set_mode(RDY_PIN, pigpio.INPUT)
  print("Init SPI\n")
  spi_handle = pi.spi_open(0, SPI_FREQ, mode)
  print("SPI handle: " + str(spi_handle))
  print("Init registers\n")
  write_device(MISC_REGISTER1, 0x0000)
  write_device(MISC_REGISTER2, 0xFFFF);
  write_device(MISC_REGISTER3, 0x00C0);
  write_device(DEVICE_CONTROL1, 0x6006);
  write_device(DEVICE_CONTROL2, 0x0000);
  write_device(ADC_CONTROL_REGISTER1, 0xC1C0);
  write_device(ADC_CONTROL_REGISTER2, 0x0065);
  write_device(BCM_DAC_FREQ, 0x0040);
  write_device(WEIGHT_SCALE_CONTROL, 0x0000);
  write_device(IQ_MODE_ENABLE, 0x0800);
  print("Device initialized\n")

def free_device():
  pi.spi_close(spi_handle)
  pi.hardware_clock(CLK_PIN, 0)

def FWR_read_device(input_mask, frequency=32, delay=0.5):
    set_device_frequency(frequency)
    write_device(IQ_MODE_ENABLE, 0x0000)
    write_device(VSENSE_MUX, input_mask)
    write_device(ISW_MUX, input_mask)
    write_device(ADC_CONTROL_REGISTER2, 0x0063)
    time.sleep(0.5)
    return read_device(ADC_DATA_RESULT) * (1.7 / 32768.0)

def IQ_read_device(input_mask, frequency=64, delay=0.5, quiet=False):
    set_device_frequency(frequency)
    write_device(IQ_MODE_ENABLE, 0x0800)
    write_device(VSENSE_MUX, input_mask)
    write_device(ISW_MUX, input_mask)        

    # read from I channel
    write_device(ADC_CONTROL_REGISTER2, 0x0063)
    wait_for_stability(stability_thresh=0.002, quiet=quiet)
    result_I = read_device(ADC_DATA_RESULT)  # *(1.7 / 32768.0)
    print("I result: " + str(result_I))
    if result_I >= 32768:
        result_I -= 65536
    
    # read from Q channel
    write_device(ADC_CONTROL_REGISTER2, 0x0065)
    wait_for_stability(stability_thresh=0.002, quiet=quiet)
    result_Q = read_device(ADC_DATA_RESULT)  # *(1.7 / 32768.0)
    print("Q result: " + str(result_Q))
    if result_Q >= 32768:
        result_Q -= 65536
    
    # impedance calculation
    mag = math.sqrt(result_I ** 2 + result_Q ** 2) * (1.7 / 32768.0)
    phase = math.atan(result_Q / result_I)
    print("Mag: " + str(mag) + "; Phase: " + str(phase) + ";")
    return mag, phase

def read_device_last_measure():
    return (read_device(ADC_DATA_RESULT) * (1.7 / 32768.0))

def wait_for_stability(avglen=10, stability_thresh=0.001, quiet=False):
    hist = []
    last = read_device_last_measure()
    avg = 1.0
    while avg > stability_thresh:
        curr = read_device_last_measure()
        hist.append(abs(last - curr))
        if len(hist) > avglen:
            hist.pop(0)
        sum = 0
        for item in hist:
            sum += item
        last = curr
        avg = sum / len(hist)
        if not quiet:
            sys.stdout.write("Diff: %.6f\r" % (avg))
    if not quiet:
        print("")

def set_device_frequency(frequency_khz, set_IQ_demod=True):
    frequency_khz = int(frequency_khz)
    if frequency_khz == 8:
        write_device(BCM_DAC_FREQ, 0x0008)  # DAC to 8 kHz
        if set_IQ_demod:
            write_device(DEVICE_CONTROL2, 0x2800)  # set IQ Demod clock accordingly
    elif frequency_khz == 16:
        write_device(BCM_DAC_FREQ, 0x0010)  # DAC to 16 kHz
        if set_IQ_demod:
            write_device(DEVICE_CONTROL2, 0x2000)  # set IQ Demod clock accordingly
    elif frequency_khz == 32:
        write_device(BCM_DAC_FREQ, 0x0020)  # DAC to 32 kHz
        if set_IQ_demod:
            write_device(DEVICE_CONTROL2, 0x1800)  # set IQ Demod clock accordingly
    elif frequency_khz == 64:
        write_device(BCM_DAC_FREQ, 0x0040)  # DAC to 64 kHz
        if set_IQ_demod:
            write_device(DEVICE_CONTROL2, 0x1000)  # set IQ Demod clock accordingly
    elif frequency_khz == 128:
        write_device(BCM_DAC_FREQ, 0x0080)  # DAC to 128 kHz
        if set_IQ_demod:
            write_device(DEVICE_CONTROL2, 0x0800)  # set IQ Demod clock accordingly
    else:
        if set_IQ_demod:
            raise ValueError("Supported IQ frequencies are 8,16,32,64 and 128 kHz")
        write_device(BCM_DAC_FREQ, frequency_khz)  # DAC


def main():
  init_device()
  # filename = time.strftime("bioimpedance_%d%m%y_%H%M%S.csv", time.gmtime())
  # f = open(filename, 'w')
  # f.write("time,amp_8k,amp16k,amp32k,amp64k,amp128k,phase8k,phase16k,phase32k,phase64k,phase128k\n")
  # f.flush()

  # sample_num = 5
  # for i in range(0, sample_num):
    # print("Measurement " + str(i))
    # print("Waiting ...")
    # sys.stdout.write("Reading 8kHz ")
    # IQ_8I, IQ_8P = IQ_read_device(0x0408, frequency=8, quiet=False)
    # sys.stdout.write("Reading 16kHz ")
    # IQ_16I, IQ_16P = IQ_read_device(0x0408, frequency=16, quiet=False)
    # sys.stdout.write("Reading 32kHz ")
    # IQ_32I, IQ_32P = IQ_read_device(0x0408, frequency=32, quiet=False)
    # sys.stdout.write("Reading 64kHz ")
  IQ_64I, IQ_64P = IQ_read_device(0x0408, frequency=64, quiet=False)
    # sys.stdout.write("Reading 128kHz")
    # IQ_128I, IQ_128P = IQ_read_device(0x0408, frequency=128, quiet=False)
    # sys.stdout.write("\r\n")
    # f.write("%.3f,%.6f,%.6f,%.6f,%.6f,%.6f,%.3f,%.3f,%.3f,%.3f,%.3f\n" % (
    #     time.time(), IQ_8I, IQ_16I, IQ_32I, IQ_64I, IQ_128I, IQ_8P, IQ_16P, IQ_32P, IQ_64P, IQ_128P))
    # f.flush()
    # print("Magnitude:   %.3f %.3f %.3f %.3f %.3f     Phase %+01.3f %+01.3f %+01.3f %+01.3f %+01.3f " % (
    #     IQ_8I, IQ_16I, IQ_32I, IQ_64I, IQ_128I, IQ_8P, IQ_16P, IQ_32P, IQ_64P, IQ_128P))

  # f.close()
  free_device()

main()
..