주석 추가
This commit is contained in:
@@ -18,30 +18,32 @@
|
||||
#define ECHO_MISO_NODE DT_NODELABEL(echo_miso)
|
||||
#define ECHO_CS_NODE DT_NODELABEL(echo_cs)
|
||||
|
||||
/* ADC SPI 핀 */
|
||||
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().
|
||||
*/
|
||||
/* SPIM3 레지스터 인스턴스 */
|
||||
static nrfx_spim_t echo_spim = NRFX_SPIM_INSTANCE(NRF_SPIM3);
|
||||
static bool echo_adc_initialized;
|
||||
|
||||
/* ADC 원본 수신 버퍼 */
|
||||
static uint8_t raw_capture[ECHO_ADC_MAX_SAMPLES * 2];
|
||||
|
||||
/* GPIO 포트 레지스터 선택 */
|
||||
static NRF_GPIO_Type *gpio_reg_for_spec(const struct gpio_dt_spec *spec)
|
||||
{
|
||||
return (spec->port == DEVICE_DT_GET(DT_NODELABEL(gpio1))) ? NRF_P1 : NRF_P0;
|
||||
}
|
||||
|
||||
/* 포트 포함 절대 핀 번호 */
|
||||
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);
|
||||
}
|
||||
|
||||
/* SPIM RX 전송 */
|
||||
static int echo_adc_transfer_rx(uint8_t *buf, size_t len)
|
||||
{
|
||||
nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_XFER_RX(buf, len);
|
||||
@@ -56,6 +58,7 @@ static int echo_adc_transfer_rx(uint8_t *buf, size_t len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 12비트 샘플 추출 */
|
||||
static void echo_adc_unpack_samples(uint16_t *samples, uint16_t num_samples)
|
||||
{
|
||||
for (uint16_t i = 0; i < num_samples; i++)
|
||||
@@ -66,6 +69,7 @@ static void echo_adc_unpack_samples(uint16_t *samples, uint16_t num_samples)
|
||||
}
|
||||
}
|
||||
|
||||
/* 무응답 샘플 확인 */
|
||||
static bool echo_adc_samples_all_zero(const uint16_t *samples, uint16_t num_samples)
|
||||
{
|
||||
for (uint16_t i = 0; i < num_samples; i++)
|
||||
@@ -79,6 +83,7 @@ static bool echo_adc_samples_all_zero(const uint16_t *samples, uint16_t num_samp
|
||||
return true;
|
||||
}
|
||||
|
||||
/* 안정 우선 캡처 */
|
||||
static int echo_adc_capture_slow(uint16_t *samples, uint16_t num_samples)
|
||||
{
|
||||
for (uint16_t i = 0; i < num_samples; i++)
|
||||
@@ -94,28 +99,33 @@ static int echo_adc_capture_slow(uint16_t *samples, uint16_t num_samples)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 속도 우선 캡처 */
|
||||
static int echo_adc_capture_fast(uint16_t *samples, uint16_t num_samples)
|
||||
{
|
||||
NRF_SPIM_Type *spim = echo_spim.p_reg;
|
||||
NRF_GPIO_Type *cs_port = gpio_reg_for_spec(&echo_cs);
|
||||
uint32_t cs_mask = BIT(echo_cs.pin);
|
||||
|
||||
/* SPIM 직접 제어 */
|
||||
nrf_spim_enable(spim);
|
||||
spim->RXD.MAXCNT = 2U;
|
||||
|
||||
for (uint16_t i = 0; i < num_samples; i++)
|
||||
{
|
||||
/* CS 하강 후 2바이트 수신 */
|
||||
cs_port->OUTCLR = cs_mask;
|
||||
spim->RXD.PTR = (uint32_t)&raw_capture[i * 2U];
|
||||
spim->EVENTS_END = 0;
|
||||
spim->TASKS_START = 1;
|
||||
|
||||
/* 전송 완료 대기 */
|
||||
uint32_t timeout = 10000U;
|
||||
while (!spim->EVENTS_END && (timeout > 0U))
|
||||
{
|
||||
timeout--;
|
||||
}
|
||||
|
||||
/* CS 복귀 */
|
||||
cs_port->OUTSET = cs_mask;
|
||||
|
||||
if (timeout == 0U)
|
||||
@@ -131,12 +141,14 @@ static int echo_adc_capture_fast(uint16_t *samples, uint16_t num_samples)
|
||||
|
||||
int echo_adc_init(void)
|
||||
{
|
||||
/* 중복 초기화 방지 */
|
||||
if (echo_adc_initialized || nrfx_spim_init_check(&echo_spim))
|
||||
{
|
||||
echo_adc_initialized = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* GPIO 준비 확인 */
|
||||
const struct gpio_dt_spec *pins[] = { &echo_sclk, &echo_miso, &echo_cs };
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(pins); i++)
|
||||
@@ -148,6 +160,7 @@ int echo_adc_init(void)
|
||||
}
|
||||
}
|
||||
|
||||
/* ADC SPI 설정 */
|
||||
nrfx_spim_config_t config = NRFX_SPIM_DEFAULT_CONFIG(
|
||||
gpio_abs_pin(&echo_sclk),
|
||||
NRF_SPIM_PIN_NOT_CONNECTED,
|
||||
@@ -162,7 +175,7 @@ int echo_adc_init(void)
|
||||
config.ss_active_high = false;
|
||||
|
||||
nrfx_err_t err = nrfx_spim_init(&echo_spim, &config, NULL, NULL);
|
||||
if (err != NRFX_SUCCESS)
|
||||
if (err != 0)
|
||||
{
|
||||
DBG_PRINTF("[ECHO] init fail err=%d\r\n", err);
|
||||
return -EIO;
|
||||
@@ -175,6 +188,7 @@ int echo_adc_init(void)
|
||||
|
||||
int echo_adc_wake(void)
|
||||
{
|
||||
/* 더미 읽기 웨이크업 */
|
||||
int err = echo_adc_init();
|
||||
if (err)
|
||||
{
|
||||
@@ -187,6 +201,7 @@ int echo_adc_wake(void)
|
||||
|
||||
int echo_adc_capture(uint16_t *samples, uint16_t num_samples)
|
||||
{
|
||||
/* 입력값 검사 */
|
||||
if (samples == NULL)
|
||||
{
|
||||
return -EINVAL;
|
||||
@@ -203,12 +218,14 @@ int echo_adc_capture(uint16_t *samples, uint16_t num_samples)
|
||||
return err;
|
||||
}
|
||||
|
||||
/* 빠른 캡처 우선 */
|
||||
err = echo_adc_capture_fast(samples, num_samples);
|
||||
if ((err == 0) && !echo_adc_samples_all_zero(samples, num_samples))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 실패 시 안정 캡처 */
|
||||
DBG_PRINTF("[ECHO] fast capture fallback err=%d\r\n", err);
|
||||
return echo_adc_capture_slow(samples, num_samples);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user