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; |
xf.li | f8024dc | 2024-09-26 03:10:16 -0700 | [diff] [blame^] | 28 | char cmd_ret[1024]={0}; |
| 29 | FILE *fd; |
yu.dong | 638a4a9 | 2023-10-18 00:14:06 -0700 | [diff] [blame] | 30 | switch(qadc) |
| 31 | { |
xf.li | f8024dc | 2024-09-26 03:10:16 -0700 | [diff] [blame^] | 32 | case QADC_NONE: |
| 33 | { |
| 34 | LYERRLOG("function %s line %d\n", __FUNCTION__, __LINE__); |
| 35 | return 0; |
| 36 | } |
| 37 | case ADC0: |
| 38 | { |
| 39 | LYDBGLOG("function %s line %d\n", __FUNCTION__, __LINE__); |
| 40 | fd = fopen(ADC0_NODES, "r"); |
| 41 | if(fd == NULL) |
| 42 | { |
| 43 | LYERRLOG("fopen error: %s", strerror(errno)); |
| 44 | return -1; |
| 45 | } |
| 46 | if(fgets(cmd_ret, sizeof(cmd_ret), fd) == NULL) |
| 47 | { |
| 48 | LYERRLOG("fgets error!!!\n"); |
| 49 | fclose(fd); |
| 50 | return -1; |
| 51 | } |
| 52 | fclose(fd); |
| 53 | break; |
| 54 | } |
| 55 | case ADC1: |
| 56 | { |
| 57 | LYDBGLOG("function %s line %d\n", __FUNCTION__, __LINE__); |
| 58 | fd = fopen(ADC1_NODES, "r"); |
| 59 | if(fd == NULL) |
| 60 | { |
| 61 | LYERRLOG("fopen error: %s", strerror(errno)); |
| 62 | return -1; |
| 63 | } |
| 64 | if(fgets(cmd_ret, sizeof(cmd_ret), fd) == NULL) |
| 65 | { |
| 66 | LYERRLOG("fgets error!!!\n"); |
| 67 | fclose(fd); |
| 68 | return -1; |
| 69 | } |
| 70 | fclose(fd); |
| 71 | break; |
| 72 | } |
| 73 | case ADC2: |
| 74 | { |
| 75 | LYDBGLOG("function %s line %d\n", __FUNCTION__, __LINE__); |
| 76 | fd = fopen(ADC2_NODES, "r"); |
| 77 | if(fd == NULL) |
| 78 | { |
| 79 | LYERRLOG("fopen error: %s", strerror(errno)); |
| 80 | return -1; |
| 81 | } |
| 82 | if(fgets(cmd_ret, sizeof(cmd_ret), fd) == NULL) |
| 83 | { |
| 84 | LYERRLOG("fgets error!!!\n"); |
| 85 | fclose(fd); |
| 86 | return -1; |
| 87 | } |
| 88 | fclose(fd); |
| 89 | break; |
| 90 | } |
| 91 | default: |
| 92 | { |
| 93 | LYERRLOG("input error\n"); |
| 94 | return -1; |
| 95 | } |
yu.dong | 638a4a9 | 2023-10-18 00:14:06 -0700 | [diff] [blame] | 96 | } |
xf.li | f8024dc | 2024-09-26 03:10:16 -0700 | [diff] [blame^] | 97 | |
yu.dong | 638a4a9 | 2023-10-18 00:14:06 -0700 | [diff] [blame] | 98 | adc_value = atoi(cmd_ret); |
| 99 | if (adc_value < 0 || adc_value > 12000) |
| 100 | { |
| 101 | LYERRLOG("bad adc value %s!", cmd_ret); |
| 102 | return -1; |
| 103 | } |
| 104 | return adc_value; |
| 105 | } |
| 106 | |
| 107 | DEFINE_LYNQ_LIB_LOG(LYNQ_ADC) |
| 108 | |
| 109 | #ifdef __cplusplus |
| 110 | } |
| 111 | #endif |
| 112 | |