adc.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file adc.h
  30. * @brief Contains all functions support for adc driver
  31. * @version 0.0
  32. * @date 18. Oct. 2017
  33. * @author qing.han
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #ifndef __ADC__H__
  40. #define __ADC__H__
  41. #include "types.h"
  42. #include "common.h"
  43. #include "gpio.h"
  44. #define MAX_ADC_SAMPLE_SIZE 32
  45. #define ADC_CH_BASE (0x40050400UL)
  46. #define ENABLE_ADC_INT *(volatile unsigned int *)0x40050034 |= 0x000001ff
  47. #define MASK_ADC_INT *(volatile unsigned int *)0x40050034 &= 0xfffffe00
  48. #define ADC_IRQ_ENABLE *(volatile unsigned int *) 0xe000e100 = BIT(29)
  49. #define ADC_IRQ_DISABLE *(volatile unsigned int *) 0xe000e100 = BIT(29)
  50. #define CLEAR_ADC_INT_CH0 *(volatile unsigned int *)0x40050038 |= BIT(0)
  51. #define CLEAR_ADC_INT_CH1 *(volatile unsigned int *)0x40050038 |= BIT(1)
  52. #define CLEAR_ADC_INT_CH2 *(volatile unsigned int *)0x40050038 |= BIT(2)
  53. #define CLEAR_ADC_INT_CH3 *(volatile unsigned int *)0x40050038 |= BIT(3)
  54. #define CLEAR_ADC_INT_CH4 *(volatile unsigned int *)0x40050038 |= BIT(4)
  55. #define CLEAR_ADC_INT_CH5 *(volatile unsigned int *)0x40050038 |= BIT(5)
  56. #define CLEAR_ADC_INT_CH6 *(volatile unsigned int *)0x40050038 |= BIT(6)
  57. #define CLEAR_ADC_INT_CH7 *(volatile unsigned int *)0x40050038 |= BIT(7)
  58. #define CLEAR_ADC_INT_VOICE *(volatile unsigned int *)0x40050038 |= BIT(8)
  59. #define CLEAR_ADC_INT(n) *(volatile unsigned int *)0x40050038 |= BIT(n)
  60. #define IS_CLAER_ADC_INT_CH0 (*(volatile unsigned int *)0x4005003c) & BIT(0)
  61. #define IS_CLAER_ADC_INT_CH1 (*(volatile unsigned int *)0x4005003c) & BIT(1)
  62. #define IS_CLAER_ADC_INT_CH2 (*(volatile unsigned int *)0x4005003c) & BIT(2)
  63. #define IS_CLAER_ADC_INT_CH3 (*(volatile unsigned int *)0x4005003c) & BIT(3)
  64. #define IS_CLAER_ADC_INT_CH4 (*(volatile unsigned int *)0x4005003c) & BIT(4)
  65. #define IS_CLAER_ADC_INT_CH5 (*(volatile unsigned int *)0x4005003c) & BIT(5)
  66. #define IS_CLAER_ADC_INT_CH6 (*(volatile unsigned int *)0x4005003c) & BIT(6)
  67. #define IS_CLAER_ADC_INT_CH7 (*(volatile unsigned int *)0x4005003c) & BIT(7)
  68. #define IS_CLAER_ADC_INT_VOICE (*(volatile unsigned int *)0x4005003c) & BIT(8)
  69. #define IS_CLAER_ADC_INT(n) (*(volatile unsigned int *)0x4005003c) & BIT(n)
  70. #define GET_IRQ_STATUS ((*(volatile unsigned int *) 0x4005003c) & 0x3ff)
  71. #define ENABLE_ADC (*(volatile unsigned int *)0x4000f048 |= BIT(3))
  72. #define DISABLE_ADC (*(volatile unsigned int *)0x4000f048 &= ~BIT(3))
  73. #define ADC_CLOCK_ENABLE (*(volatile unsigned int *)0x4000f044 |= BIT(13))
  74. #define ADC_CLOCK_DISABLE (*(volatile unsigned int *)0x4000f044 &= ~BIT(13))
  75. #define ADC_DBLE_CLOCK_DISABLE (*(volatile unsigned int *)0x4000f044 &= ~BIT(21))
  76. #define POWER_DOWN_ADC (*(volatile unsigned int *)0x4000f048 &= ~BIT(3))
  77. #define POWER_UP_TEMPSENSOR (*(volatile unsigned int *)0x4000f048 |= BIT(29))
  78. #define REG_IO_CONTROL ((volatile unsigned int *)0x4000f020)
  79. #define ADCC_REG_BASE (0x4000F000)
  80. #define ADC_USE_TIMEOUT 1
  81. #define ADC_OP_TIMEOUT 100
  82. #if(ADC_USE_TIMEOUT == 1)
  83. #define ADC_INIT_TOUT(to) int to = hal_systick()
  84. #define ADC_CHECK_TOUT(to, timeout, loginfo) {if(hal_ms_intv(to) > timeout){LOG(loginfo);return PPlus_ERR_TIMEOUT;}}
  85. #else
  86. #define ADC_INIT_TOUT(to)
  87. #define ADC_CHECK_TOUT(to, timeout, loginfo)
  88. #endif
  89. /**************************************************************************************
  90. * @fn hal_get_adc_int_source
  91. *
  92. * @brief This function process for get adc interrupt source,such as adc channel NO
  93. *
  94. * input parameters
  95. *
  96. * @param None.
  97. *
  98. * output parameters
  99. *
  100. * @param None.
  101. *
  102. * @return adc interrupt source bit loaction(uint8_t)
  103. **************************************************************************************/
  104. typedef enum {
  105. ADC_CH0 = 0,
  106. ADC_CH1 = 1,
  107. ADC_CH2 = 2,ADC_CH1N = 2, ADC_CH1N_P11 = 2,
  108. ADC_CH3 = 3,ADC_CH1P = 3, ADC_CH1P_P12 = 3,ADC_CH1DIFF = 3,
  109. ADC_CH4 = 4,ADC_CH2N = 4, ADC_CH2N_P13 = 4,
  110. ADC_CH5 = 5,ADC_CH2P = 5, ADC_CH2P_P14 = 5,ADC_CH2DIFF = 5,
  111. ADC_CH6 = 6,ADC_CH3N = 6, ADC_CH3N_P15 = 6,
  112. ADC_CH7 = 7,ADC_CH3P = 7, ADC_CH3P_P20 = 7,ADC_CH3DIFF = 7,
  113. ADC_CH_VOICE =8,
  114. ADC_CH_NUM =9,
  115. }adc_CH_t;
  116. #define ADC_BIT(ch) (1<<ch)
  117. enum{
  118. HAL_ADC_EVT_DATA = 1,
  119. HAL_ADC_EVT_FAIL = 0xff
  120. };
  121. typedef enum {
  122. HAL_ADC_CLOCK_80K = 0,
  123. HAL_ADC_CLOCK_160K = 1,
  124. HAL_ADC_CLOCK_320K = 2,
  125. }adc_CLOCK_SEL_t;
  126. typedef struct _adc_Cfg_t{
  127. uint8_t channel;
  128. bool is_continue_mode;
  129. uint8_t is_differential_mode;
  130. uint8_t is_high_resolution;
  131. }adc_Cfg_t;
  132. typedef struct _adc_Evt_t{
  133. int type;
  134. adc_CH_t ch;
  135. uint16_t* data;
  136. uint8_t size; //word size
  137. }adc_Evt_t;
  138. typedef void (*adc_Hdl_t)(adc_Evt_t* pev);
  139. typedef struct _adc_Contex_t{
  140. bool enable;
  141. uint8_t all_channel;
  142. bool continue_mode;
  143. adc_Hdl_t evt_handler[ADC_CH_NUM];
  144. }adc_Ctx_t;
  145. extern GPIO_Pin_e s_pinmap[ADC_CH_NUM];
  146. /**************************************************************************************
  147. * @fn hal_adc_init
  148. *
  149. * @brief This function process for adc initial
  150. *
  151. * input parameters
  152. *
  153. * @param ADC_CH_e adc_pin: adc pin select;ADC_CH0~ADC_CH7 and ADC_CH_VOICE
  154. * ADC_SEMODE_e semode: single-end mode and diff mode select; 1:SINGLE_END(single-end mode) 0:DIFF(Diff mode)
  155. * IO_CONTROL_e amplitude: input signal amplitude, 0:BELOW_1V,1:UP_1V
  156. *
  157. * output parameters
  158. *
  159. * @param None.
  160. *
  161. * @return None.
  162. **************************************************************************************/
  163. void hal_adc_init(void);
  164. int hal_adc_config_channel(adc_Cfg_t cfg, adc_Hdl_t evt_handler);
  165. int hal_adc_clock_config(adc_CLOCK_SEL_t clk);
  166. int hal_adc_start(void);
  167. int hal_adc_stop(void);
  168. void __attribute__((weak)) hal_ADC_IRQHandler(void);
  169. float hal_adc_value_cal(adc_CH_t ch,uint16_t* buf, uint32_t size, uint8_t high_resol, uint8_t diff_mode);
  170. extern void pad_ds_control(GPIO_Pin_e pin, BitAction_e value);
  171. #endif