fadc.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fadc.h
  15. * Date: 2022-02-10 14:53:42
  16. * LastEditTime: 2022-02-18 08:29:10
  17. * Description:  This files is for
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. */
  23. #ifndef FT_ADC_H
  24. #define FT_ADC_H
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. #include "ftypes.h"
  30. #include "fdebug.h"
  31. #include "ferror_code.h"
  32. #include "fkernel.h"
  33. #include "fparameters.h"
  34. #define FADC_SUCCESS FT_SUCCESS
  35. #define FADC_ERR_INVAL_PARM FT_MAKE_ERRCODE(ErrModBsp, ErrBspAdc, 1)
  36. #define FADC_ERR_NOT_READY FT_MAKE_ERRCODE(ErrModBsp, ErrBspAdc, 2)
  37. #define FADC_ERR_TIMEOUT FT_MAKE_ERRCODE(ErrModBsp, ErrBspAdc, 3)
  38. #define FADC_ERR_CMD_FAILED FT_MAKE_ERRCODE(ErrModBsp, ErrBspAdc, 4)
  39. #define FADC_ERR_NOT_SUPPORT FT_MAKE_ERRCODE(ErrModBsp, ErrBspAdc, 5)
  40. #define FADC_CTRL_PD_DISABLE 0
  41. #define FADC_CTRL_PD_ENABLE 1
  42. #define FADC_CONVERT_COMPLETE(x) (x=TRUE)
  43. #define FADC_CONVERT_UNCOMPLETE(x) (x=FALSE)
  44. #define FADC_READ_TIMEOUT (600)
  45. #define FADC_READ_DELAY (10)
  46. /* adc interrupt event type */
  47. typedef enum
  48. {
  49. FADC_INTR_EVENT_COVFIN = 0, /**< Handler type for convert finish interrupt */
  50. FADC_INTR_EVENT_DLIMIT = 1, /**< Handler type for low limit interrupt*/
  51. FADC_INTR_EVENT_ULIMIT = 2, /**< Handler type for high limit interrupt*/
  52. FADC_INTR_EVENT_ERROR = 3, /**< Handler type for error interrupt*/
  53. FADC_INTR_EVENT_NUM
  54. } FAdcIntrEventType;
  55. /* adc convert mode */
  56. typedef enum
  57. {
  58. FADC_CONTINUOUS_CONVERT = 0,/* continuous conversion*/
  59. FADC_SINGLE_CONVERT = 1, /* single conversion*/
  60. FADC_CONVERT_MODE_NUM
  61. } FAdcConvertMode;
  62. /* adc channel mode */
  63. typedef enum
  64. {
  65. FADC_MULTI_CHANNEL = 0, /* multi channel conversion*/
  66. FADC_FIXED_CHANNEL = 1, /* fixed channel conversion*/
  67. FADC_CHANNEL_MODE_NUM
  68. } FAdcChannelMode;
  69. /* adc base configuration */
  70. typedef struct
  71. {
  72. u32 instance_id;/* adc id */
  73. uintptr base_addr;/* adc control register base address*/
  74. u32 irq_num;/* adc interrupt number */
  75. u32 irq_prority;/* adc interrupt priority*/
  76. const char *instance_name;/* instance name */
  77. } FAdcConfig;
  78. typedef struct
  79. {
  80. u32 convert_interval; /* convert interval time */
  81. u32 clk_div; /* clock divider, must be even*/
  82. FAdcConvertMode convert_mode;/*!< convert mode */
  83. FAdcChannelMode channel_mode;/*!< channel mode */
  84. } FAdcConvertConfig;
  85. /* adc variable config */
  86. typedef struct
  87. {
  88. u16 high_threshold; /*!< Configures the ADC analog high threshold value.
  89. This parameter must be a 10-bit value. */
  90. u16 low_threshold; /*!< Configures the ADC analog low threshold value.
  91. This parameter must be a 10-bit value. */
  92. } FAdcThresholdConfig;
  93. typedef void (*FAdcIntrEventHandler)(void *param);
  94. typedef struct
  95. {
  96. FAdcConfig config;/* adc config */
  97. u32 is_ready;/* adc init ready flag */
  98. u16 value[FADC_CHANNEL_NUM]; /* adc value */
  99. boolean convert_complete[FADC_CHANNEL_NUM]; /*!< Specifies whether the conversion is complete> */
  100. FAdcIntrEventHandler event_handler[FADC_INTR_EVENT_NUM]; /* event handler for interrupt */
  101. void *event_param[FADC_INTR_EVENT_NUM]; /* parameters of event handler */
  102. } FAdcCtrl;
  103. /* get default configuration of specific adc id */
  104. const FAdcConfig *FAdcLookupConfig(FAdcInstance instance_id);
  105. /* DeInitialization function for the device instance */
  106. void FAdcDeInitialize(FAdcCtrl *pctrl);
  107. /* Initializes a specific instance such that it is ready to be used */
  108. FError FAdcCfgInitialize(FAdcCtrl *pctrl, const FAdcConfig *input_config_p);
  109. /* config adc convert parameters */
  110. FError FAdcConvertSet(FAdcCtrl *pctrl, FAdcConvertConfig *convert_config);
  111. /* Set adc channel high_threshold and low_threshold */
  112. FError FAdcChannelThresholdSet(FAdcCtrl *pctrl, FAdcChannel channel, FAdcThresholdConfig *threshold_config);
  113. /* init adc variable configuration */
  114. FError FAdcVariableConfig(FAdcCtrl *pctrl, FAdcConvertConfig *convert_config);
  115. /* enable channel interrupt */
  116. FError FAdcInterruptEnable(FAdcCtrl *pctrl, FAdcChannel channel, FAdcIntrEventType event_type);
  117. /* disable channel interrupt */
  118. FError FAdcInterruptDisable(FAdcCtrl *pctrl, FAdcChannel channel, FAdcIntrEventType event_type);
  119. void FAdcChannelEnable(FAdcCtrl *pctrl, FAdcChannel channel, boolean state);
  120. /* Start adc convert */
  121. void FAdcConvertStart(FAdcCtrl *pctrl);
  122. /* Stop adc convert */
  123. void FAdcConvertStop(FAdcCtrl *pctrl);
  124. /* read adc channel convert result value */
  125. FError FAdcReadConvertResult(FAdcCtrl *pctrl, FAdcChannel channel, u16 *val);
  126. /* read adc channel convert finish count */
  127. FError FAdcReadFinishCnt(FAdcCtrl *pctrl, FAdcChannel channel, u32 *count);
  128. /* read adc channel history limit value, include high limit and low limit */
  129. FError FAdcReadHisLimit(FAdcCtrl *pctrl, FAdcChannel channel, u16 *u_his_limit, u16 *d_his_limit);
  130. /* interrupt handler for the driver */
  131. void FAdcIntrHandler(s32 vector, void *args);
  132. /* register FAdc interrupt handler function */
  133. void FAdcRegisterInterruptHandler(FAdcCtrl *instance_p, FAdcIntrEventType event_type,
  134. FAdcIntrEventHandler handler, void *param);
  135. #ifdef __cplusplus
  136. }
  137. #endif
  138. #endif