You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.1 KiB

/*
* Copyright (c) Hisilicon Technologies Co., Ltd. 2012-2018. All rights reserved.
* Description: sample_lasdc.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include "uapi_lsadc.h"
#include "uapi_system.h"
static td_u32 g_channel = 0;
static int g_thread_should_stop = 0;
static td_u32 g_cur_value;
#define MAX_CHANNEL 15
static int check_params(void)
{
uapi_sys_version version = {0};
td_s32 ret;
int maxChn = 4; /* defult the max channe is 4 */
ret = uapi_sys_get_version(&version);
if (ret != TD_SUCCESS) {
printf("get chip version fail\n");
return -1;
}
if (version.chip_name_id == UAPI_CHIP_NAME_RESERVED9 || version.chip_name_id == UAPI_CHIP_NAME_RESERVED13) {
maxChn = MAX_CHANNEL;
}
if (g_channel > maxChn) {
printf("channel error; Only Support 0 ~ %d channel\n", maxChn);
return -1;
}
return 0;
}
/*lint -save -e715 */
td_void *lsadc_sample_thread(td_void *arg)
{
td_u32 value = 0;
if (arg != NULL) {
printf("start lsadc_sample_thread with args:%p\n", arg);
}
while (!g_thread_should_stop) {
uapi_lsadc_get_value(g_channel, &value);
if (g_cur_value != value) {
printf("Channel %d Value 0x%08x\n", g_channel, value);
g_cur_value = value;
}
}
return 0;
}
td_void sample_lsadc_set_config(td_void)
{
td_s32 ret;
td_u32 boardid = 0;
uapi_lsadc_config config;
uapi_sys_version version = {0};
td_u32 mask = 0;
td_u32 channel_mask = 0;
ret = uapi_sys_get_version(&version);
if (ret != TD_SUCCESS) {
printf("get chip version fail\n");
return;
}
ret = uapi_sys_get_board_id(&boardid);
if (ret != TD_SUCCESS) {
printf("get board id fail\n");
return;
}
uapi_lsadc_get_config(&config);
mask = (config.channel_mask | (0x1 << (g_channel % 4))); /* 4 groups */
channel_mask = g_channel << 16 | mask; /* Support 16 channels */
boardid &= 0x2000;
if ((version.chip_name_id == UAPI_CHIP_NAME_RESERVED9 || version.chip_name_id == UAPI_CHIP_NAME_RESERVED8 ||
version.chip_name_id == UAPI_CHIP_NAME_RESERVED13 || version.chip_name_id == UAPI_CHIP_NAME_RESERVED6) &&
boardid != 0x2000) {
config.channel_mask = channel_mask;
} else {
config.channel_mask = channel_mask;
config.active_bit = 0xfc;
config.data_delta = 0x8;
config.deglitch_bypass = 0x1;
config.glitch_sample = 0;
config.lsadc_reset = 0;
config.lsadc_zero = 0xff;
config.model_sel = 0x1;
config.power_down_mod = 0x0;
config.time_scan = 600; /* lsacd chn scan time gap 600 us */
}
ret = uapi_lsadc_set_config(&config);
if (ret != TD_SUCCESS) {
printf("set config fail\n");
}
}
td_s32 main(td_s32 argc, td_char *argv[])
{
int ret;
pthread_t thread;
int c;
/* Get param */
if (argc == 2 && argv != NULL && argv[1] != NULL) { /* input 2 param */
g_channel = (td_u32)strtol(argv[1], NULL, 0);
} else if (argv != NULL && argv[0] != NULL) {
printf("Usage: %s [Channel]\n", argv[0]);
printf("Example: %s 0\n", argv[0]);
return TD_FAILURE;
} else {
return TD_FAILURE;
}
uapi_sys_init();
/* Chech param */
if (check_params() < 0) {
return TD_FAILURE;
}
/* Open LSADC */
ret = uapi_lsadc_init();
if (ret != TD_SUCCESS) {
printf("uapi_lsadc_init err, 0x%08x !\n", ret);
return ret;
}
sample_lsadc_set_config();
printf("Create lsadc sample thread, press q to exit!\n");
ret = pthread_create(&thread, NULL, lsadc_sample_thread, NULL);
if (ret < 0) {
printf("Failt to create thread!");
uapi_lsadc_deinit();
uapi_sys_deinit();
return TD_FAILURE;
}
while ((c = getchar()) != 'q') {
sleep(3); /* sleep 3s */
}
g_thread_should_stop = 1;
pthread_join(thread, NULL);
printf("LSADC sample thread exit! Bye!\n");
uapi_lsadc_deinit();
uapi_sys_deinit();
return TD_SUCCESS;
}