Files
luban-lite/bsp/examples/test-psadc/test_psadc.c

210 lines
5.5 KiB
C
Raw Normal View History

2023-08-30 16:21:18 +08:00
/*
2025-10-21 13:59:50 +08:00
* Copyright (c) 2022-2025, ArtInChip Technology Co., Ltd
2023-08-30 16:21:18 +08:00
*
* SPDX-License-Identifier: Apache-2.0
*
* Authors: Li Siyao <siyao.li@artinchip.com>
*/
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/time.h>
#include <rtthread.h>
#include "rtdevice.h"
#include "aic_core.h"
#include "aic_log.h"
2024-04-03 16:40:57 +08:00
#include "rtdevice.h"
#include "hal_psadc.h"
2024-09-03 11:16:08 +08:00
#ifdef AIC_SYSCFG_DRV
#include "hal_syscfg.h"
#endif
2023-08-30 16:21:18 +08:00
/* Global macro and variables */
#define AIC_PSADC_NAME "psadc"
#define AIC_PSADC_ADC_MAX_VAL 0xFFF
#define AIC_PSADC_DEFAULT_VOLTAGE 3
2024-04-03 16:40:57 +08:00
#define AIC_PSADC_QC_MODE 0
2024-09-03 11:16:08 +08:00
#define AIC_PSADC_VOLTAGE_ACCURACY 10000
2023-08-30 16:21:18 +08:00
static rt_adc_device_t psadc_dev;
2025-10-21 13:59:50 +08:00
static const char sopts[] = "t:n:w:sh";
2023-08-30 16:21:18 +08:00
static const struct option lopts[] = {
{"voltage", required_argument, NULL, 't'},
2024-06-04 19:00:30 +08:00
{"number", required_argument, NULL, 'n'},
2025-10-21 13:59:50 +08:00
{"window", required_argument, NULL, 'w'},
2024-04-03 16:40:57 +08:00
{"status", no_argument, NULL, 's'},
2023-08-30 16:21:18 +08:00
{"help", no_argument, NULL, 'h'},
{0, 0, 0, 0}
};
/* Functions */
static void cmd_psadc_usage(char *program)
{
printf("Usage: %s [options]\n", program);
2024-09-03 11:16:08 +08:00
printf("\t -t, --voltage\t\tSet default voltage\n");
2024-04-03 16:40:57 +08:00
printf("\t -s, --status\t\tShow more hardware information\n");
2024-06-04 19:00:30 +08:00
printf("\t -n, --number\t\tSet the number of samples, default is 10\n");
2025-10-21 13:59:50 +08:00
printf("\t -w, --window\t\tOnly show sample rate within the window\n");
2023-08-30 16:21:18 +08:00
printf("\t -h, --help \n");
printf("\n");
2025-10-21 13:59:50 +08:00
printf("Example: %s -t 3 -n 10\n", program);
2023-08-30 16:21:18 +08:00
}
2025-10-21 13:59:50 +08:00
static int test_psadc_adc2voltage(float ref_voltage, int adc_value)
2023-08-30 16:21:18 +08:00
{
2025-10-21 13:59:50 +08:00
return (adc_value * (ref_voltage / 100)) / AIC_PSADC_ADC_MAX_VAL;
}
2024-04-03 16:40:57 +08:00
2025-10-21 13:59:50 +08:00
static void show_sample_rate(u32 cnt, u32 window, u64 start_us)
{
u64 elapse = aic_get_time_us() - start_us;
2024-09-03 11:16:08 +08:00
2025-10-21 13:59:50 +08:00
printf("Cnt %d, Sample rate: %ld Hz\n", cnt,
(long)(((u64)window * 1000000ULL) / elapse));
2024-04-03 16:40:57 +08:00
}
2025-10-21 13:59:50 +08:00
int psadc_get_adc(float def_voltage, int sample_num, u32 window)
2024-04-03 16:40:57 +08:00
{
int ret = 0;
u32 adc_values[AIC_PSADC_CH_NUM];
2025-10-21 13:59:50 +08:00
int cnt = 0, i, voltage = 0;
2024-04-03 16:40:57 +08:00
int chan_cnt = 0;
2025-10-21 13:59:50 +08:00
u64 start_us = 0, end_us = 0;
2024-09-03 11:16:08 +08:00
int ref_voltage = 0;
2023-08-30 16:21:18 +08:00
psadc_dev = (rt_adc_device_t)rt_device_find(AIC_PSADC_NAME);
if (!psadc_dev) {
rt_kprintf("Failed to open %s device\n", AIC_PSADC_NAME);
return -RT_ERROR;
}
2024-09-03 11:16:08 +08:00
#ifdef AIC_SYSCFG_DRV
2025-01-08 19:12:06 +08:00
ref_voltage = hal_syscfg_read_ldo_cfg();
2024-09-03 11:16:08 +08:00
#endif
if (!ref_voltage) {
rt_kprintf("Failed to obtain reference voltage through eFuse\n");
ref_voltage = (int)(def_voltage) * AIC_PSADC_VOLTAGE_ACCURACY;
}
2025-10-21 13:59:50 +08:00
rt_kprintf("Reference voltage: %d.%04d V\n",
2024-09-03 11:16:08 +08:00
ref_voltage / AIC_PSADC_VOLTAGE_ACCURACY,
ref_voltage % AIC_PSADC_VOLTAGE_ACCURACY);
2024-04-03 16:40:57 +08:00
rt_adc_enable(psadc_dev, AIC_PSADC_QC_MODE);
chan_cnt = rt_adc_control(psadc_dev, RT_ADC_CMD_GET_CHAN_COUNT, NULL);
2025-10-21 13:59:50 +08:00
printf("Will get %d data from %d channels in %s mode\n\n", sample_num,
chan_cnt,
#ifdef AIC_PSADC_DRV_POLL
"poll"
#else
"IRQ"
#endif
);
if (!window) {
printf("Cnt ");
for (i = 0; i < chan_cnt; i++)
printf("Ch%d_ADC Ch%d_Vol ", i, i);
printf("Time(us)\n");
}
if (window)
start_us = aic_get_time_us();
2024-04-03 16:40:57 +08:00
2024-06-04 19:00:30 +08:00
while (cnt < sample_num) {
2024-04-03 16:40:57 +08:00
cnt++;
2025-10-21 13:59:50 +08:00
if (!window)
start_us = aic_get_time_us();
#ifdef AIC_PSADC_DRV_POLL
2024-04-03 16:40:57 +08:00
ret = rt_adc_control(psadc_dev, RT_ADC_CMD_GET_VALUES_POLL,
(void *)adc_values);
2025-10-21 13:59:50 +08:00
#else
ret = rt_adc_control(psadc_dev, RT_ADC_CMD_GET_VALUES,
(void *)adc_values);
#endif
if (window) {
if (cnt && cnt % window == 0) {
show_sample_rate(cnt, window, start_us);
start_us = aic_get_time_us();
}
} else {
end_us = aic_get_time_us();
printf("%3d ", cnt);
for (i = 0; i < chan_cnt; i++) {
voltage = test_psadc_adc2voltage(ref_voltage, adc_values[i]);
printf("%7d %2d.%02d V ", adc_values[i],
voltage / 100, voltage % 100);
}
if (start_us)
printf("%8d\n", abs(end_us - start_us));
2024-04-03 16:40:57 +08:00
}
2025-10-21 13:59:50 +08:00
if (ret < 0) {
printf("Read timeout! return %d\n", ret);
return -RT_ERROR;
2024-04-03 16:40:57 +08:00
}
2025-10-21 13:59:50 +08:00
#ifdef AIC_PSADC_TRIG_BY_SOFT
aicos_msleep(500);
#endif
2023-08-30 16:21:18 +08:00
}
2024-04-03 16:40:57 +08:00
rt_adc_disable(psadc_dev, AIC_PSADC_QC_MODE);
2023-08-30 16:21:18 +08:00
return -RT_ERROR;
}
static void cmd_test_psadc(int argc, char **argv)
{
2024-09-03 11:16:08 +08:00
float def_voltage = AIC_PSADC_DEFAULT_VOLTAGE;
2024-04-03 16:40:57 +08:00
bool show_status = false;
2025-10-21 13:59:50 +08:00
int sample_num = 10;
u32 window = 0;
int c;
2023-08-30 16:21:18 +08:00
optind = 0;
while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
switch (c) {
case 't':
2024-09-03 11:16:08 +08:00
def_voltage = atof(optarg);
2024-04-03 16:40:57 +08:00
break;
case 's':
show_status = true;
2023-08-30 16:21:18 +08:00
break;
2024-06-04 19:00:30 +08:00
case 'n':
sample_num = atoi(optarg);
break;
2025-10-21 13:59:50 +08:00
case 'w':
window = atoi(optarg);
break;
2023-08-30 16:21:18 +08:00
case 'h':
default:
cmd_psadc_usage(argv[0]);
return;
}
}
2024-04-03 16:40:57 +08:00
if (show_status) {
aich_psadc_status_show();
aicos_msleep(10);
2023-08-30 16:21:18 +08:00
return;
}
2024-04-03 16:40:57 +08:00
2024-09-03 11:16:08 +08:00
if (def_voltage < 0) {
rt_kprintf("Please set valid default voltage\n");
2023-08-30 16:21:18 +08:00
return;
}
2024-06-04 19:00:30 +08:00
if (sample_num < 0) {
rt_kprintf("Please set vaild sample count\n");
return;
}
2025-10-21 13:59:50 +08:00
psadc_get_adc(def_voltage, sample_num, window);
2024-06-04 19:00:30 +08:00
2023-08-30 16:21:18 +08:00
}
MSH_CMD_EXPORT_ALIAS(cmd_test_psadc, test_psadc, psadc device sample);