ADC까지, 신호 확인 필요(반사 신호 약함 or 뒤쪽을 찍고 있음)
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
/*******************************************************************************
|
||||
* @file echo_adc.c
|
||||
* @brief ADC121S051 echo capture driver using nrfx SPIM3
|
||||
******************************************************************************/
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <nrfx_spim.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "echo_adc.h"
|
||||
#include "debug_print.h"
|
||||
|
||||
#define ECHO_SCLK_NODE DT_NODELABEL(echo_sclk)
|
||||
#define ECHO_MISO_NODE DT_NODELABEL(echo_miso)
|
||||
#define ECHO_CS_NODE DT_NODELABEL(echo_cs)
|
||||
|
||||
static const struct gpio_dt_spec echo_sclk = GPIO_DT_SPEC_GET(ECHO_SCLK_NODE, gpios);
|
||||
static const struct gpio_dt_spec echo_miso = GPIO_DT_SPEC_GET(ECHO_MISO_NODE, gpios);
|
||||
static const struct gpio_dt_spec echo_cs = GPIO_DT_SPEC_GET(ECHO_CS_NODE, gpios);
|
||||
|
||||
/*
|
||||
* NRFX_SPIM_INSTANCE() takes the peripheral register symbol (for example
|
||||
* NRF_SPIM3), not a numeric instance index. Passing 3 makes p_reg invalid
|
||||
* and causes a fault inside nrfx_spim_init().
|
||||
*/
|
||||
static nrfx_spim_t echo_spim = NRFX_SPIM_INSTANCE(NRF_SPIM3);
|
||||
static bool echo_adc_initialized;
|
||||
static uint8_t raw_capture[ECHO_ADC_MAX_SAMPLES * 2];
|
||||
|
||||
static uint32_t gpio_abs_pin(const struct gpio_dt_spec *spec)
|
||||
{
|
||||
uint32_t port = (spec->port == DEVICE_DT_GET(DT_NODELABEL(gpio1))) ? 1U : 0U;
|
||||
return NRF_GPIO_PIN_MAP(port, spec->pin);
|
||||
}
|
||||
|
||||
static int echo_adc_transfer_rx(uint8_t *buf, size_t len)
|
||||
{
|
||||
nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_XFER_RX(buf, len);
|
||||
int err = nrfx_spim_xfer(&echo_spim, &xfer, 0);
|
||||
|
||||
if (err != 0)
|
||||
{
|
||||
DBG_PRINTF("[ECHO] xfer fail err=%d\r\n", err);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int echo_adc_init(void)
|
||||
{
|
||||
if (echo_adc_initialized || nrfx_spim_init_check(&echo_spim))
|
||||
{
|
||||
echo_adc_initialized = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct gpio_dt_spec *pins[] = { &echo_sclk, &echo_miso, &echo_cs };
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(pins); i++)
|
||||
{
|
||||
if (!device_is_ready(pins[i]->port))
|
||||
{
|
||||
DBG_PRINTF("[ECHO] GPIO device not ready (%d)\r\n", (int)i);
|
||||
return -ENODEV;
|
||||
}
|
||||
}
|
||||
|
||||
nrfx_spim_config_t config = NRFX_SPIM_DEFAULT_CONFIG(
|
||||
gpio_abs_pin(&echo_sclk),
|
||||
NRF_SPIM_PIN_NOT_CONNECTED,
|
||||
gpio_abs_pin(&echo_miso),
|
||||
gpio_abs_pin(&echo_cs));
|
||||
|
||||
config.frequency = NRFX_MHZ_TO_HZ(16);
|
||||
config.mode = NRF_SPIM_MODE_3;
|
||||
config.bit_order = NRF_SPIM_BIT_ORDER_MSB_FIRST;
|
||||
config.orc = 0x00;
|
||||
config.miso_pull = NRF_GPIO_PIN_NOPULL;
|
||||
config.ss_active_high = false;
|
||||
|
||||
nrfx_err_t err = nrfx_spim_init(&echo_spim, &config, NULL, NULL);
|
||||
if (err != NRFX_SUCCESS)
|
||||
{
|
||||
DBG_PRINTF("[ECHO] init fail err=%d\r\n", err);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
echo_adc_initialized = true;
|
||||
DBG_PRINTF("[ECHO] SPIM3 init OK\r\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int echo_adc_wake(void)
|
||||
{
|
||||
int err = echo_adc_init();
|
||||
if (err)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
uint8_t discard[2];
|
||||
return echo_adc_transfer_rx(discard, sizeof(discard));
|
||||
}
|
||||
|
||||
int echo_adc_capture(uint16_t *samples, uint16_t num_samples)
|
||||
{
|
||||
if (samples == NULL)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if ((num_samples == 0U) || (num_samples > ECHO_ADC_MAX_SAMPLES))
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
int err = echo_adc_init();
|
||||
if (err)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
for (uint16_t i = 0; i < num_samples; i++)
|
||||
{
|
||||
/*
|
||||
* Keep the framing that matched the old firmware behavior:
|
||||
* one 16-clock / 2-byte SPI frame per sample.
|
||||
*/
|
||||
err = echo_adc_transfer_rx(&raw_capture[i * 2U], 2U);
|
||||
if (err)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
|
||||
uint16_t raw16 = ((uint16_t)raw_capture[i * 2U] << 8) |
|
||||
(uint16_t)raw_capture[i * 2U + 1U];
|
||||
samples[i] = (raw16 >> 1) & 0x0FFF;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user