Files
luban-lite/bsp/examples_bare/test-gpai/test_gpai.c

144 lines
3.6 KiB
C
Raw Normal View History

2023-08-30 16:21:18 +08:00
/*
2024-04-03 16:40:57 +08:00
* Copyright (c) 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 <stdint.h>
#include <string.h>
#include <console.h>
#include <getopt.h>
#include "hal_adcim.h"
#include "hal_gpai.h"
#include "mpp_fb.h"
2024-04-03 16:40:57 +08:00
#include "test_gpai.h"
2023-08-30 16:21:18 +08:00
2024-04-03 16:40:57 +08:00
/* The default voltages are set to D21x->3.0V, D31x、D12x->2.5V */
2023-11-09 20:19:51 +08:00
#define AIC_GPAI_DEFAULT_VOLTAGE 3
#define AIC_GPAI_ADC_MAX_VAL 0xFFF
2024-01-27 08:47:24 +08:00
#define AIC_GPAI_VOLTAGE_ACCURACY 10000
2024-04-03 16:40:57 +08:00
#define AIC_GPAI_DEFAULT_SAMPLES_NUM 100
2024-01-27 08:47:24 +08:00
static float g_def_voltage = AIC_GPAI_DEFAULT_VOLTAGE;
2024-04-03 16:40:57 +08:00
static int g_sample_num = AIC_GPAI_DEFAULT_SAMPLES_NUM;
2023-08-30 16:21:18 +08:00
2024-01-27 08:47:24 +08:00
static void cmd_gpai_usage(void)
2023-08-30 16:21:18 +08:00
{
2023-11-09 20:19:51 +08:00
printf("Compile time: %s %s\n", __DATE__, __TIME__);
2024-01-27 08:47:24 +08:00
printf("Usage: test_gpai [options]\n");
2024-04-03 16:40:57 +08:00
printf("test_gpai read <channel_id> : Select one channel in [0, %d], default is 0\n", AIC_GPAI_CH_NUM - 1);
printf("test_gpai modify <default_voltage> : Modify default voltage\n");
printf("test_gpai set <samples_number> : Set the number of samples,default is 100\n");
printf("test_gpai help : Get this help\n");
2023-11-09 20:19:51 +08:00
printf("\n");
2024-01-27 08:47:24 +08:00
printf("Example: test_gpai read 4\n");
2023-08-30 16:21:18 +08:00
}
static int test_gpai_init(int ch)
{
static int inited = 0;
struct aic_gpai_ch *chan;
if (!inited) {
hal_adcim_probe();
hal_gpai_clk_init();
inited = 1;
}
hal_gpai_set_ch_num(AIC_GPAI_CH_NUM);
chan = hal_gpai_ch_is_valid(ch);
if (!chan)
return -1;
aich_gpai_enable(1);
hal_gpai_clk_get(chan);
aich_gpai_ch_init(chan, chan->pclk_rate);
return 0;
}
2024-01-27 08:47:24 +08:00
static void cmd_gpai_set(int argc, char **argv)
{
g_def_voltage = strtod(argv[1], NULL);
if (g_def_voltage < 0) {
printf("Please input valid default voltage\n");
return;
}
2024-04-03 16:40:57 +08:00
printf("Successfully set the default voltage\n");
2024-01-27 08:47:24 +08:00
}
static int cmd_gpai_read(int argc, char **argv)
{
2024-06-04 19:00:30 +08:00
u16 value;
u32 ch;
2024-04-03 16:40:57 +08:00
u32 cal_param;
int cnt = 0;
int voltage = 0;
2023-08-30 16:21:18 +08:00
struct aic_gpai_ch *chan;
2024-04-03 16:40:57 +08:00
int scale = AIC_GPAI_VOLTAGE_ACCURACY;
int sample_cnt = g_sample_num;
2024-01-27 08:47:24 +08:00
ch = strtod(argv[1], NULL);
if ((ch < 0) || (ch >= AIC_GPAI_CH_NUM)) {
printf("Invalid channel No.%d\n", ch);
return -1;
}
2024-04-03 16:40:57 +08:00
cal_param = hal_adcim_auto_calibration();
2024-01-27 08:47:24 +08:00
test_gpai_init(ch);
2023-08-30 16:21:18 +08:00
chan = hal_gpai_ch_is_valid(ch);
chan->complete = aicos_sem_create(0);
aicos_request_irq(GPAI_IRQn, aich_gpai_isr, 0, NULL, NULL);
2024-04-03 16:40:57 +08:00
while(sample_cnt) {
aich_gpai_read(chan, &value, AIC_GPAI_TIMEOUT);
printf("[%d] ch %d: %d\n", cnt, ch, value);
cnt++;
if (value) {
voltage = hal_adcim_adc2voltage(value, cal_param,
AIC_GPAI_VOLTAGE_ACCURACY,
g_def_voltage);
printf("voltage : %d.%04d v\n", voltage / scale, voltage % scale);
}
sample_cnt--;
}
2023-08-30 16:21:18 +08:00
2024-01-27 08:47:24 +08:00
return 0;
2023-08-30 16:21:18 +08:00
}
static int cmd_test_gpai(int argc, char *argv[])
{
if (argc < 3) {
2024-01-27 08:47:24 +08:00
cmd_gpai_usage();
2023-08-30 16:21:18 +08:00
return 0;
}
2024-01-27 08:47:24 +08:00
if (!strcmp(argv[1], "read")) {
cmd_gpai_read(argc - 1, &argv[1]);
2023-08-30 16:21:18 +08:00
return 0;
}
2024-01-27 08:47:24 +08:00
if (!strcmp(argv[1], "set")) {
2024-04-03 16:40:57 +08:00
g_sample_num = atoi(argv[2]);
if (g_sample_num <= 0)
printf("Please set the number of samples\n");
return 0;
}
if (!strcmp(argv[1], "modify")) {
2024-01-27 08:47:24 +08:00
cmd_gpai_set(argc - 1, &argv[1]);
2023-08-30 16:21:18 +08:00
return 0;
}
2024-01-27 08:47:24 +08:00
cmd_gpai_usage();
2023-08-30 16:21:18 +08:00
return 0;
}
CONSOLE_CMD(test_gpai, cmd_test_gpai, "GPAI test example");