air105_adc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #ifndef __AIR105_ADC_H__
  22. #define __AIR105_ADC_H__
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /* Include ------------------------------------------------------------------*/
  27. #include "air105.h"
  28. #define IS_ADC_PERIPH(PERIPH) ((PERIPH) == ADC0)
  29. #define ADC_CR1_CHANNEL_MASK ((uint32_t)0x07)
  30. #define ADC_CR1_SAMPLE_RATE_Pos 3
  31. #define ADC_CR1_SAMPLE_RATE_MASK (0x3 << ADC_CR1_SAMPLE_RATE_Pos)
  32. #define ADC_CR1_SAMP_ENABLE BIT(6)
  33. #define ADC_CR1_POWER_DOWN BIT(8)
  34. #define ADC_CR1_IRQ_ENABLE BIT(5)
  35. #define ADC_SR_DONE_FLAG BIT(0)
  36. #define ADC_SR_FIFO_OV_FLAG BIT(1)
  37. #define ADC_FIFO_OV_INT_ENABLE BIT(2)
  38. #define ADC_FIFO_RESET BIT(1)
  39. #define ADC_FIFO_ENABLE BIT(0)
  40. #define ADC_CR2_BUFF_ENABLE BIT(14)
  41. #define ADC_DIV_RESISTOR_EN_BIT BIT(13)
  42. typedef enum
  43. {
  44. ADC_Overflow = 0,
  45. ADC_NoOverflow = 1,
  46. }ADC_OverflowTypeDef;
  47. /* ADC Channel select */
  48. typedef enum
  49. {
  50. ADC_CHANNEL_CHARGE_VBAT =0,
  51. ADC_CHANNEL_1,
  52. ADC_CHANNEL_2,
  53. ADC_CHANNEL_3,
  54. ADC_CHANNEL_4,
  55. ADC_CHANNEL_5,
  56. ADC_CHANNEL_6,
  57. }ADC_ChxTypeDef;
  58. #define IS_ADC_CHANNEL(CHANNEL_NUM) (((CHANNEL_NUM) == ADC_CHANNEL_CHARGE_VBAT) || \
  59. ((CHANNEL_NUM) == ADC_CHANNEL_1) || \
  60. ((CHANNEL_NUM) == ADC_CHANNEL_2) || \
  61. ((CHANNEL_NUM) == ADC_CHANNEL_3) || \
  62. ((CHANNEL_NUM) == ADC_CHANNEL_4) || \
  63. ((CHANNEL_NUM) == ADC_CHANNEL_5) || \
  64. ((CHANNEL_NUM) == ADC_CHANNEL_6))
  65. /* ADC Samp Select */
  66. typedef enum
  67. {
  68. ADC_SpeedPrescaler_None = 0,
  69. ADC_SpeedPrescaler_2,
  70. ADC_SpeedPrescaler_4,
  71. ADC_SpeedPrescaler_8,
  72. }ADC_SampTypeDef;
  73. #define IS_ADC_SAMP(SAMP) (((SAMP) == ADC_SpeedPrescaler_None) || \
  74. ((SAMP) == ADC_SpeedPrescaler_2) || \
  75. ((SAMP) == ADC_SpeedPrescaler_4) || \
  76. ((SAMP) == ADC_SpeedPrescaler_8))
  77. typedef struct _ADC_InitTypeDef
  78. {
  79. ADC_ChxTypeDef ADC_Channel; /* ADC Channel select */
  80. ADC_SampTypeDef ADC_SampSpeed; /* ADC sampspeed select */
  81. FunctionalState ADC_IRQ_EN; /* ADC IRQ/Polling Select */
  82. FunctionalState ADC_FIFO_EN; /* ADC FIFO Enable Select */
  83. } ADC_InitTypeDef;
  84. /* Exported constants -------------------------------------------------------*/
  85. /* Exported macro -----------------------------------------------------------*/
  86. /* Exported functions -------------------------------------------------------*/
  87. void ADC_Init(ADC_InitTypeDef *ADC_InitStruct);
  88. void ADC_StartCmd(FunctionalState NewState);
  89. void ADC_FIFODeepth(uint32_t FIFO_Deepth);
  90. void ADC_FIFOReset(void);
  91. void ADC_ITCmd(FunctionalState NewState);
  92. void ADC_FIFOOverflowITcmd(FunctionalState NewState);
  93. void ADC_BuffCmd(FunctionalState NewState);
  94. void ADC_DivResistorCmd(FunctionalState NewState);
  95. int32_t ADC_GetFIFOCount(void);
  96. int32_t ADC_GetResult(void);
  97. int32_t ADC_GetFIFOResult(uint16_t *ADCdata, uint32_t len);
  98. uint32_t ADC_CalVoltage(uint32_t u32ADC_Value, uint32_t u32ADC_Ref_Value);
  99. ADC_ChxTypeDef ADC_GetChannel(void);
  100. FunctionalState ADC_IsDivResistorEnable(void);
  101. ADC_OverflowTypeDef ADC_IsFIFOOverflow(void);
  102. void ADC_ChannelSwitch(ADC_ChxTypeDef Channelx);
  103. /* Exported variables -------------------------------------------------------*/
  104. /* Exported types -----------------------------------------------------------*/
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. #endif /* __AIR105_ADC_H__ */