annotate Small_CPU/Src/adc.c @ 409:2e2d34c1cc99 Improment_NVM

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