38
|
1 /**
|
|
2 ******************************************************************************
|
|
3 * @file adc.c
|
|
4 * @author heinrichs weikamp gmbh
|
|
5 * @version V0.0.1
|
|
6 * @date 11-Dec-2014
|
|
7 * @brief ADC for ambient light sensor
|
|
8 *
|
|
9 @verbatim
|
|
10 ==============================================================================
|
|
11 ##### How to use #####
|
|
12 ==============================================================================
|
|
13 @endverbatim
|
|
14 ******************************************************************************
|
|
15 * @attention
|
|
16 *
|
|
17 * <h2><center>© COPYRIGHT(c) 2014 heinrichs weikamp</center></h2>
|
|
18 *
|
|
19 ******************************************************************************
|
|
20 */
|
|
21
|
|
22 /* Includes ------------------------------------------------------------------*/
|
|
23
|
|
24 #include "stm32f4xx_hal.h"
|
|
25 #include "adc.h"
|
|
26
|
|
27 /* Exported variables --------------------------------------------------------*/
|
|
28 /* Private variables ---------------------------------------------------------*/
|
|
29 ADC_HandleTypeDef AdcHandle;
|
|
30 __IO uint16_t uhADCxConvertedValue = 0;
|
|
31
|
|
32 /* Private types -------------------------------------------------------------*/
|
|
33 #define ADCx ADC1
|
|
34 #define ADCx_CLK_ENABLE() __ADC1_CLK_ENABLE();
|
|
35 #define ADCx_CHANNEL_GPIO_CLK_ENABLE() __GPIOF_CLK_ENABLE()
|
|
36
|
|
37 #define ADCx_FORCE_RESET() __ADC_FORCE_RESET()
|
|
38 #define ADCx_RELEASE_RESET() __ADC_RELEASE_RESET()
|
|
39
|
|
40 /* Definition for ADCx Channel Pin */
|
|
41 #define ADCx_GPIO_PIN GPIO_PIN_0
|
|
42 #define ADCx_GPIO_PORT GPIOB
|
|
43 #define ADCx_GPIO_CLK_ENABLE() __GPIOB_CLK_ENABLE()
|
|
44 #define ADCx_GPIO_CLK_DISABLE() __GPIOB_CLK_DISABLE()
|
|
45
|
|
46 /* Definition for ADCx's Channel */
|
|
47 #define ADCx_CHANNEL ADC_CHANNEL_8
|
|
48
|
|
49 /* Definition for ADCx's NVIC */
|
|
50 //#define ADCx_IRQn ADC_IRQn
|
|
51
|
|
52 /* Private function prototypes -----------------------------------------------*/
|
|
53 void ADC_Start_single_IT_Conversion(void);
|
|
54
|
|
55 /* Exported functions --------------------------------------------------------*/
|
|
56
|
|
57 uint16_t get_ambient_light_level(void)
|
|
58 {
|
|
59 #ifdef OSTC_ON_DISCOVERY_HARDWARE
|
|
60 return 800;
|
|
61 #endif
|
|
62
|
|
63 return uhADCxConvertedValue;
|
|
64 }
|
|
65
|
|
66
|
|
67 static void ADCx_MspInit(ADC_HandleTypeDef *hadc)
|
|
68 {
|
|
69 #ifdef OSTC_ON_DISCOVERY_HARDWARE
|
|
70 return;
|
|
71 #endif
|
|
72
|
|
73 GPIO_InitTypeDef GPIO_InitStruct;
|
|
74
|
|
75 ADCx_GPIO_CLK_ENABLE();
|
|
76
|
|
77 GPIO_InitStruct.Pin = ADCx_GPIO_PIN ;
|
|
78 GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
|
79 GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
80 HAL_GPIO_Init(ADCx_GPIO_PORT, &GPIO_InitStruct);
|
|
81
|
|
82 ADCx_CLK_ENABLE();
|
|
83
|
|
84 // HAL_NVIC_SetPriority(ADCx_IRQn, 2, 0);
|
|
85 // HAL_NVIC_EnableIRQ(ADCx_IRQn);
|
|
86 }
|
|
87
|
|
88
|
|
89 void ADCx_DeInit(void)
|
|
90 {
|
|
91 // HAL_ADC_Stop_IT(&AdcHandle);
|
|
92 HAL_ADC_DeInit(&AdcHandle);
|
|
93 }
|
|
94
|
|
95
|
|
96 void ADCx_Init(void)
|
|
97 {
|
|
98 #ifdef OSTC_ON_DISCOVERY_HARDWARE
|
|
99 return;
|
|
100 #endif
|
|
101
|
|
102 ADC_ChannelConfTypeDef sConfig;
|
|
103
|
|
104 AdcHandle.Instance = ADCx;
|
|
105
|
|
106 AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
|
|
107 AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
|
|
108 AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
|
109 AdcHandle.Init.ContinuousConvMode = DISABLE;
|
|
110 AdcHandle.Init.DiscontinuousConvMode = DISABLE;
|
|
111 AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
|
112 AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV;
|
|
113 AdcHandle.Init.NbrOfConversion = 1;
|
|
114 AdcHandle.Init.DMAContinuousRequests = DISABLE;
|
|
115
|
|
116 ADCx_MspInit(&AdcHandle);
|
|
117 HAL_ADC_Init(&AdcHandle);
|
|
118
|
|
119 sConfig.Channel = ADCx_CHANNEL;
|
|
120 sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
|
|
121 sConfig.Rank = 1;
|
|
122 sConfig.Offset = 0;
|
|
123
|
|
124 HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
|
|
125
|
|
126 // ADC_Start_single_IT_Conversion();
|
|
127 }
|
|
128
|
|
129
|
|
130 uint32_t adc_debug_status = 0;
|
|
131
|
|
132 void adc_ambient_light_sensor_get_data(void)
|
|
133 {
|
|
134 #ifdef OSTC_ON_DISCOVERY_HARDWARE
|
|
135 return;
|
|
136 #endif
|
|
137
|
|
138 HAL_ADC_Start(&AdcHandle);
|
|
139 HAL_ADC_PollForConversion(&AdcHandle, 10);
|
|
140 adc_debug_status = HAL_ADC_GetState(&AdcHandle);
|
|
141 if(adc_debug_status == HAL_ADC_STATE_EOC_REG + HAL_ADC_STATE_READY) // new HAL_ADC_STATE_READY 160613
|
|
142 // if(HAL_ADC_GetState(&AdcHandle) == HAL_ADC_STATE_EOC_REG)
|
|
143 uhADCxConvertedValue = HAL_ADC_GetValue(&AdcHandle);
|
|
144 HAL_ADC_Stop(&AdcHandle);
|
|
145 }
|
|
146
|
|
147
|
|
148 /* Private functions ---------------------------------------------------------*/
|
|
149 /*
|
|
150 void ADC_Start_single_IT_Conversion(void)
|
|
151 {
|
|
152 HAL_ADC_Start_IT(&AdcHandle);
|
|
153 }
|
|
154
|
|
155
|
|
156 void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
|
|
157 {
|
|
158 extern void copyAmbientLightData(void);
|
|
159
|
|
160 uhADCxConvertedValue = HAL_ADC_GetValue(AdcHandle);
|
|
161 copyAmbientLightData();
|
|
162 ADC_Start_single_IT_Conversion();
|
|
163 }
|
|
164 */
|
|
165
|
|
166 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/
|