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

186 lines
4.8 KiB
C
Raw Normal View History

2023-08-30 16:21:18 +08:00
/*
2024-09-03 11:16:08 +08:00
* Copyright (c) 2022-2024, 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;
2024-06-04 19:00:30 +08:00
static const char sopts[] = "t:n:rsh";
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'},
{"read", no_argument, NULL, 'r'},
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("Compile time: %s %s\n", __DATE__, __TIME__);
printf("Usage: %s [options]\n", program);
2024-04-03 16:40:57 +08:00
printf("\t -r, --read\t\tRead the adc value\n");
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");
2023-08-30 16:21:18 +08:00
printf("\t -h, --help \n");
printf("\n");
2024-06-04 19:00:30 +08:00
printf("Example: %s -r -t 3 -n 10\n", program);
2023-08-30 16:21:18 +08:00
}
2024-09-03 11:16:08 +08:00
static void test_psadc_adc2voltage(float ref_voltage, int adc_value)
2023-08-30 16:21:18 +08:00
{
2024-04-03 16:40:57 +08:00
int voltage;
2024-09-03 11:16:08 +08:00
voltage = (adc_value * (ref_voltage / 100)) / AIC_PSADC_ADC_MAX_VAL;
rt_kprintf(" %d.%02d V", voltage / 100, voltage % 100);
2024-04-03 16:40:57 +08:00
return;
}
2024-09-03 11:16:08 +08:00
int psadc_get_adc(float def_voltage, int sample_num)
2024-04-03 16:40:57 +08:00
{
int ret = 0;
u32 adc_values[AIC_PSADC_CH_NUM];
int cnt = 0;
int chan_cnt = 0;
u64 start_us, end_us;
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
ref_voltage = syscfg_read_ldo_cfg();
#endif
if (!ref_voltage) {
rt_kprintf("Failed to obtain reference voltage through eFuse\n");
ref_voltage = (int)(def_voltage) * AIC_PSADC_VOLTAGE_ACCURACY;
}
rt_kprintf("The reference voltage is %d.%02d V\n",
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);
2024-06-04 19:00:30 +08:00
rt_kprintf("Start samplng %d samples for %d channels\n", sample_num,
chan_cnt);
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++;
start_us = aic_get_time_us();
ret = rt_adc_control(psadc_dev, RT_ADC_CMD_GET_VALUES_POLL,
(void *)adc_values);
end_us = aic_get_time_us();
rt_kprintf("Sample time: %d us\n", abs(end_us - start_us));
if (ret < 0) {
rt_kprintf("Read timeout!\n");
return -RT_ERROR;
}
// aic_udelay(10);
2024-06-04 19:00:30 +08:00
rt_kprintf("[%d] PSADC:", cnt);
2024-04-03 16:40:57 +08:00
for (int i = 0; i < chan_cnt; i++) {
rt_kprintf(" %d", adc_values[i]);
}
2024-06-04 19:00:30 +08:00
rt_kprintf("\nvoltage:");
2024-04-03 16:40:57 +08:00
for (int i = 0; i < chan_cnt; i++) {
2024-09-03 11:16:08 +08:00
test_psadc_adc2voltage(ref_voltage, adc_values[i]);
2024-04-03 16:40:57 +08:00
}
rt_kprintf("\n");
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)
{
int c;
2024-06-04 19:00:30 +08:00
int sample_num = 10;
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;
2024-06-04 19:00:30 +08:00
int read_enable = 0;
2023-08-30 16:21:18 +08:00
if (argc < 2) {
cmd_psadc_usage(argv[0]);
return;
}
optind = 0;
while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
switch (c) {
2024-04-03 16:40:57 +08:00
case 'r':
2024-06-04 19:00:30 +08:00
read_enable = 1;
2023-08-30 16:21:18 +08:00
break;
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;
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;
}
if (read_enable)
2024-09-03 11:16:08 +08:00
psadc_get_adc(def_voltage, sample_num);
2024-06-04 19:00:30 +08:00
2023-08-30 16:21:18 +08:00
return;
}
MSH_CMD_EXPORT_ALIAS(cmd_test_psadc, test_psadc, psadc device sample);