adc.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @file adc.c
  3. * @author chipsea
  4. * @brief Contains all functions support for adc driver
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. #ifndef __ADC__H__
  11. #define __ADC__H__
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #include "types.h"
  16. #include "gpio.h"
  17. #define MAX_ADC_SAMPLE_SIZE 32
  18. #define ADC_CH_BASE (0x40050400UL)
  19. #define CLEAR_ADC_INT(n) AP_ADC->intr_clear |= BIT(n)
  20. #define IS_CLAER_ADC_INT_VOICE (AP_ADC->intr_clear & BIT(8))
  21. #define IS_CLAER_ADC_INT(n) (AP_ADC->intr_clear & BIT(n))
  22. #define GET_IRQ_STATUS (AP_ADCC->intr_status & 0x3ff)
  23. #define ENABLE_ADC (AP_PCRM->ANA_CTL |= BIT(3))
  24. #define DISABLE_ADC (AP_PCRM->ANA_CTL &= ~BIT(3))
  25. #define ADC_CLOCK_ENABLE (AP_PCRM->CLKHF_CTL1 |= BIT(13))
  26. #define ADC_CLOCK_DISABLE (AP_PCRM->CLKHF_CTL1 &= ~BIT(13))
  27. #define ADC_CH0_POINTER (AP_ADCC->int_pointer_ch0_ch3 & 0x0000003f)
  28. #define ADC_CH1_POINTER (AP_ADCC->int_pointer_ch0_ch3 & (0x0000003f << 6))>>6
  29. #define ADC_CH2_POINTER (AP_ADCC->int_pointer_ch0_ch3 & (0x0000003f << 12))>>12
  30. #define ADC_CH3_POINTER (AP_ADCC->int_pointer_ch0_ch3 & (0x0000003f << 18))>>18
  31. #define ADC_CH4_POINTER (AP_ADCC->int_pointer_ch4_ch7 & 0x0000003f)
  32. #define ADC_CH5_POINTER (AP_ADCC->int_pointer_ch4_ch7 & (0x0000003f << 6))>>6
  33. #define ADC_CH6_POINTER (AP_ADCC->int_pointer_ch4_ch7 & (0x0000003f << 12))>>12
  34. #define ADC_CH7_POINTER (AP_ADCC->int_pointer_ch4_ch7 & (0x0000003f << 18))>>18
  35. #define SPIF_RSVD_AREA_1 (0x1008)
  36. #define pSPIF_RSVD1_ADC_CALIBRATE ((volatile uint32_t*)(SPIF_BASE_ADDR + SPIF_RSVD_AREA_1))
  37. #define SPIF_RSVD1_ADC_CALIBRATE (SPIF_BASE_ADDR + SPIF_RSVD_AREA_1)
  38. typedef enum {
  39. ADC_CH0DIFF = 1,/*p18(positive),p25(negative),only works in diff*/
  40. ADC_CH0 = 2,ADC_CH1N_P11 = 2,MIN_ADC_CH = 2,
  41. ADC_CH1 = 3,ADC_CH1P_P23 = 3,/*P23 and P11*/
  42. ADC_CH2 = 4,ADC_CH2N_P24 = 4,
  43. ADC_CH3 = 5,ADC_CH2P_P14 = 5,/*P14 and P24*/
  44. ADC_CH4 = 6,ADC_CH3N_P15 = 6,
  45. ADC_CH9 = 7,ADC_CH3P_P20 = 7,MAX_ADC_CH = 7,/*P20 and P15*/
  46. ADC_CH_VOICE = 8,
  47. ADC_CH_NUM =9,
  48. }AdcChannel_t;
  49. #define ADC_BIT(ch) (1<<ch)
  50. enum{
  51. HAL_ADC_EVT_DATA = 1,
  52. HAL_ADC_EVT_FAIL = 0xff
  53. };
  54. typedef enum {
  55. HAL_ADC_CLOCK_80K = 0,
  56. HAL_ADC_CLOCK_160K = 1,
  57. HAL_ADC_CLOCK_320K = 2,
  58. }AdcClockSel_t;
  59. typedef struct _adc_Cfg_t{
  60. uint8_t channel_flag;
  61. bool is_continue_mode;
  62. uint8_t is_high_resolution;
  63. }AdcCfg_t;
  64. typedef struct _adc_Evt_t{
  65. int type;
  66. AdcChannel_t ch;
  67. uint16_t* data;
  68. uint8_t size; //word size
  69. }AdcEvt_t;
  70. typedef void (*adc_Hdl_t)(AdcEvt_t* pev);
  71. extern GpioPin_t s_pinmap[ADC_CH_NUM];
  72. void HalAdcInit(void);
  73. int HalAdcChannelConfig(AdcCfg_t cfg, adc_Hdl_t evt_handler);
  74. int HalAdcClockConfig(AdcClockSel_t clk);
  75. int HalAdcStart(void);
  76. int HalAdcStop(void);
  77. void __attribute__((weak)) HalAdcIRQHandler(void);
  78. int32_t HalAdcValueCal(AdcChannel_t ch,uint16_t* buf, uint32_t size, uint8_t high_resol);
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. #endif