0: AD5941 EIS project init — driver, ESP32-S3 port, chip ID read
This commit is contained in:
commit
397e7efacb
|
|
@ -0,0 +1,6 @@
|
|||
.cache/
|
||||
build/
|
||||
sdkconfig.old
|
||||
.espport
|
||||
managed_components/
|
||||
dependencies.lock
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
# The following five lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(eis4)
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
IDF_PATH ?= $(HOME)/esp/esp-idf
|
||||
PORT := $(or $(ESP_PORT),$(shell cat .espport 2>/dev/null),/dev/cu.usbmodem1101)
|
||||
|
||||
IDF = . $(IDF_PATH)/export.sh > /dev/null 2>&1 && idf.py
|
||||
|
||||
.PHONY: all flash monitor clean menuconfig size erase select
|
||||
|
||||
all:
|
||||
$(IDF) build
|
||||
|
||||
flash:
|
||||
$(IDF) -p $(PORT) flash
|
||||
|
||||
monitor:
|
||||
$(IDF) -p $(PORT) monitor
|
||||
|
||||
clean:
|
||||
$(IDF) fullclean
|
||||
|
||||
menuconfig:
|
||||
$(IDF) menuconfig
|
||||
|
||||
size:
|
||||
$(IDF) size
|
||||
|
||||
erase:
|
||||
$(IDF) -p $(PORT) erase-flash
|
||||
|
||||
select:
|
||||
@devs=($$(ls /dev/cu.usb* 2>/dev/null)); \
|
||||
if [ $${#devs[@]} -eq 0 ]; then \
|
||||
echo "No USB devices found."; exit 1; \
|
||||
fi; \
|
||||
echo ""; \
|
||||
i=1; for d in $${devs[@]}; do echo " $$i) $$d"; i=$$((i+1)); done; \
|
||||
echo ""; \
|
||||
printf "Select device [1-$${#devs[@]}]: "; read n; \
|
||||
if [ "$$n" -ge 1 ] 2>/dev/null && [ "$$n" -le $${#devs[@]} ] 2>/dev/null; then \
|
||||
sel=$${devs[$$((n-1))]}; \
|
||||
echo "$$sel" > .espport; \
|
||||
echo "=> $$sel"; \
|
||||
else \
|
||||
echo "Invalid selection."; exit 1; \
|
||||
fi
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
idf_component_register(SRCS "ad5940.c"
|
||||
INCLUDE_DIRS ".")
|
||||
|
||||
# Vendor library — suppress warnings we can't fix
|
||||
target_compile_options(${COMPONENT_LIB} PRIVATE
|
||||
-Wno-maybe-uninitialized
|
||||
-Wno-unused-variable
|
||||
-Wno-unused-but-set-variable)
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,3 @@
|
|||
idf_component_register(SRCS "ad5941_port.c" "ad5941_bridge.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES ad5941 driver esp_rom)
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* AD5940 library ↔ ESP32-S3 bridge.
|
||||
* Implements the platform functions the ad5940 library expects to find at link time.
|
||||
*/
|
||||
#include "ad5940.h"
|
||||
#include "ad5941_port.h"
|
||||
#include "esp_rom_sys.h"
|
||||
|
||||
static volatile uint8_t int_flag;
|
||||
|
||||
static void IRAM_ATTR gp0_isr(void *arg)
|
||||
{
|
||||
int_flag = 1;
|
||||
}
|
||||
|
||||
/* ---- SPI ---- */
|
||||
|
||||
void AD5940_ReadWriteNBytes(unsigned char *pSendBuffer, unsigned char *pRecvBuff, unsigned long length)
|
||||
{
|
||||
ad5941_port_spi_rw(pSendBuffer, pRecvBuff, length);
|
||||
}
|
||||
|
||||
/* ---- CS ---- */
|
||||
|
||||
void AD5940_CsClr(void) { gpio_set_level(AD5940_CS_PIN, 0); }
|
||||
void AD5940_CsSet(void) { gpio_set_level(AD5940_CS_PIN, 1); }
|
||||
|
||||
/* ---- Reset ---- */
|
||||
|
||||
void AD5940_RstSet(void) { gpio_set_level(AD5940_RST_PIN, 1); }
|
||||
void AD5940_RstClr(void) { gpio_set_level(AD5940_RST_PIN, 0); }
|
||||
|
||||
/* ---- Timing ---- */
|
||||
|
||||
void AD5940_Delay10us(uint32_t time)
|
||||
{
|
||||
if (time < 100)
|
||||
esp_rom_delay_us(time * 10);
|
||||
else
|
||||
vTaskDelay(pdMS_TO_TICKS(time / 100));
|
||||
}
|
||||
|
||||
/* ---- Interrupt ---- */
|
||||
|
||||
uint32_t AD5940_GetMCUIntFlag(void) { return int_flag; }
|
||||
|
||||
uint32_t AD5940_ClrMCUIntFlag(void)
|
||||
{
|
||||
int_flag = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ---- Resource init (called by AD5940_MCUResourceInit) ---- */
|
||||
|
||||
uint32_t AD5940_MCUResourceInit(void *pCfg)
|
||||
{
|
||||
ad5941_port_init_spi();
|
||||
ad5941_port_init_gpio();
|
||||
|
||||
AD5940_CsSet();
|
||||
AD5940_RstSet();
|
||||
|
||||
gpio_install_isr_service(0);
|
||||
gpio_isr_handler_add(AD5940_GP0INT_PIN, gp0_isr, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
#include "ad5941_port.h"
|
||||
|
||||
static const char *TAG = "ad5941-port";
|
||||
static spi_device_handle_t spi;
|
||||
|
||||
void ad5941_port_init_spi(void)
|
||||
{
|
||||
if (spi) return;
|
||||
|
||||
spi_bus_config_t bus = {
|
||||
.mosi_io_num = PIN_NUM_MOSI,
|
||||
.miso_io_num = PIN_NUM_MISO,
|
||||
.sclk_io_num = PIN_NUM_CLK,
|
||||
.quadwp_io_num = -1,
|
||||
.quadhd_io_num = -1,
|
||||
.max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE,
|
||||
};
|
||||
|
||||
spi_device_interface_config_t dev = {
|
||||
.clock_source = SPI_CLK_SRC_DEFAULT,
|
||||
.queue_size = 1,
|
||||
.mode = 0,
|
||||
.clock_speed_hz = SPI_MASTER_FREQ_16M,
|
||||
.spics_io_num = -1, /* CS managed manually */
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(spi_bus_initialize(AD594x_HOST, &bus, SPI_DMA_CH_AUTO));
|
||||
ESP_ERROR_CHECK(spi_bus_add_device(AD594x_HOST, &dev, &spi));
|
||||
ESP_LOGI(TAG, "SPI%d initialised", AD594x_HOST + 1);
|
||||
}
|
||||
|
||||
void ad5941_port_init_gpio(void)
|
||||
{
|
||||
gpio_reset_pin(AD5940_CS_PIN);
|
||||
gpio_set_direction(AD5940_CS_PIN, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(AD5940_CS_PIN, 1);
|
||||
|
||||
gpio_reset_pin(AD5940_RST_PIN);
|
||||
gpio_set_direction(AD5940_RST_PIN, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(AD5940_RST_PIN, 1);
|
||||
|
||||
gpio_reset_pin(AD5940_GP0INT_PIN);
|
||||
gpio_set_direction(AD5940_GP0INT_PIN, GPIO_MODE_INPUT);
|
||||
gpio_set_pull_mode(AD5940_GP0INT_PIN, GPIO_PULLUP_ONLY);
|
||||
gpio_set_intr_type(AD5940_GP0INT_PIN, GPIO_INTR_NEGEDGE);
|
||||
}
|
||||
|
||||
void ad5941_port_spi_rw(uint8_t *tx, uint8_t *rx, unsigned long len)
|
||||
{
|
||||
if (!len) return;
|
||||
|
||||
spi_transaction_t t = {
|
||||
.length = len * 8,
|
||||
.tx_buffer = tx,
|
||||
.rx_buffer = rx,
|
||||
};
|
||||
ESP_ERROR_CHECK(spi_device_polling_transmit(spi, &t));
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef AD5941_PORT_H
|
||||
#define AD5941_PORT_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "driver/spi_master.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
/* SPI host */
|
||||
#define AD594x_HOST SPI2_HOST
|
||||
|
||||
/* SPI pins — match PCB net EIS_IC */
|
||||
#define PIN_NUM_MISO 13
|
||||
#define PIN_NUM_MOSI 11
|
||||
#define PIN_NUM_CLK 12
|
||||
#define PIN_NUM_CS 10
|
||||
|
||||
/* AD5941 control pins */
|
||||
#define AD5940_CS_PIN PIN_NUM_CS
|
||||
#define AD5940_RST_PIN 5 /* ESP32-S3 GPIO5 → AD5941 ~RESET */
|
||||
#define AD5940_GP0INT_PIN 4 /* ESP32-S3 GPIO4 → AD5941 GPIO0 */
|
||||
|
||||
void ad5941_port_init_spi(void);
|
||||
void ad5941_port_init_gpio(void);
|
||||
void ad5941_port_spi_rw(uint8_t *tx, uint8_t *rx, unsigned long len);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
idf_component_register(SRCS "eis4.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES ad5941 ad5941_port)
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
#include <stdio.h>
|
||||
#include "ad5940.h"
|
||||
#include "ad5941_port.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#define AD5941_EXPECTED_ADIID 0x4144
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
printf("EIS4: AD5941 bring-up\n");
|
||||
|
||||
AD5940_MCUResourceInit(NULL);
|
||||
AD5940_HWReset();
|
||||
AD5940_Initialize();
|
||||
|
||||
uint32_t adiid = AD5940_ReadReg(REG_AFECON_ADIID);
|
||||
uint32_t chipid = AD5940_ReadReg(REG_AFECON_CHIPID);
|
||||
|
||||
printf("ADIID : 0x%04lX %s\n", adiid,
|
||||
adiid == AD5941_EXPECTED_ADIID ? "(OK)" : "(UNEXPECTED)");
|
||||
printf("CHIPID: 0x%04lX\n", chipid);
|
||||
|
||||
if (adiid != AD5941_EXPECTED_ADIID)
|
||||
printf("FAIL: cannot communicate with AD5941 — check SPI wiring\n");
|
||||
else
|
||||
printf("AD5941 alive and responding\n");
|
||||
}
|
||||
Loading…
Reference in New Issue