yu.dong | 638a4a9 | 2023-10-18 00:14:06 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <sc_bsp.h> |
| 5 | #include <errno.h> |
| 6 | #include "lynq-adc.h" |
| 7 | #include "liblog/lynq_deflog.h" |
| 8 | |
| 9 | #ifdef __cplusplus |
| 10 | extern "C" { |
| 11 | #endif |
| 12 | |
| 13 | #define ADC0_NODES "/sys/kernel/debug/pmu_zx29/adc0" |
| 14 | #define ADC1_NODES "/sys/kernel/debug/pmu_zx29/adc1" |
| 15 | #define ADC2_NODES "/sys/kernel/debug/pmu_zx29/adc2" |
| 16 | |
| 17 | /******************************************************************** |
| 18 | * @brief: qser_adc_show, function to read ADC value from specific channel |
| 19 | * @param qadc [IN]: ADC_CHANNEL_E, the ADC channel to read from |
| 20 | * @return : int, the read ADC value, or error code if failed |
| 21 | * @todo: NA |
| 22 | * @see: NA |
| 23 | * @warning: NA |
| 24 | *********************************************************************/ |
| 25 | int qser_adc_show(ADC_CHANNEL_E qadc) |
| 26 | { |
| 27 | int adc_value; |
| 28 | char cmd[1024]; |
| 29 | char cmd_ret[1024]; |
| 30 | FILE *fp; |
| 31 | switch(qadc) |
| 32 | { |
| 33 | case QADC_NONE: |
| 34 | { |
| 35 | LYERRLOG("function %s line %d\n", __FUNCTION__, __LINE__); |
| 36 | return 0; |
| 37 | } |
| 38 | case ADC0: |
| 39 | { |
| 40 | LYDBGLOG("function %s line %d\n", __FUNCTION__, __LINE__); |
| 41 | sprintf(cmd, "cat %s", ADC0_NODES); |
| 42 | break; |
| 43 | } |
| 44 | case ADC1: |
| 45 | { |
| 46 | LYDBGLOG("function %s line %d\n", __FUNCTION__, __LINE__); |
| 47 | sprintf(cmd, "cat %s", ADC1_NODES); |
| 48 | break; |
| 49 | } |
| 50 | case ADC2: |
| 51 | { |
| 52 | LYDBGLOG("function %s line %d\n", __FUNCTION__, __LINE__); |
| 53 | sprintf(cmd, "cat %s", ADC2_NODES); |
| 54 | break; |
| 55 | } |
| 56 | default: |
| 57 | { |
| 58 | LYERRLOG("input error\n"); |
| 59 | return -1; |
| 60 | } |
| 61 | } |
| 62 | if((fp = popen(cmd,"r")) == NULL) |
| 63 | { |
| 64 | LYERRLOG("popen error: %s", strerror(errno)); |
| 65 | return -1; |
| 66 | } |
| 67 | if((fread(cmd_ret,sizeof(cmd_ret),1,fp))<0) |
| 68 | { |
| 69 | LYERRLOG("fread fail: %s", strerror(errno)); |
| 70 | fclose(fp); |
| 71 | return -1; |
| 72 | } |
| 73 | fclose(fp); |
| 74 | adc_value = atoi(cmd_ret); |
| 75 | if (adc_value < 0 || adc_value > 12000) |
| 76 | { |
| 77 | LYERRLOG("bad adc value %s!", cmd_ret); |
| 78 | return -1; |
| 79 | } |
| 80 | return adc_value; |
| 81 | } |
| 82 | |
| 83 | DEFINE_LYNQ_LIB_LOG(LYNQ_ADC) |
| 84 | |
| 85 | #ifdef __cplusplus |
| 86 | } |
| 87 | #endif |
| 88 | |