-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight_sensor.c
160 lines (133 loc) · 4.1 KB
/
light_sensor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Zephyr API
#include <zephyr.h>
#include <device.h>
#include <drivers/gpio.h>
// Standard C + POSIX API
#include <stdio.h>
#include <posix/time.h>
#include <posix/unistd.h>
#include <posix/pthread.h>
// Hal API
#include <stm32f7xx_hal.h>
#include <stm32f7xx_hal_adc.h>
//Modules
#include "main.h"
#include "light_sensor.h"
#include "timespec_operations.h"
void SystemClock_Config(void)
{
__HAL_RCC_ADC1_CLK_ENABLE(); // Habilita el reloj ADC1
}
void ADC_Select_CH0(void)
{
ADC_ChannelConfTypeDef sConfig = { 0 };
/* Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_6;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_56CYCLES;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) {
Error_Handler();
}
}
/**
* @brief Configure the ADC.
* @param None
* @retval None
*/
static void ADC_Config(void)
{
/* Configure the ADC peripheral */
AdcHandle.Instance = ADC1;
AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
AdcHandle.Init.ScanConvMode =
ENABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
AdcHandle.Init.ContinuousConvMode =
ENABLE; /* Continuous mode enabled to have continuous conversion */
AdcHandle.Init.DiscontinuousConvMode =
DISABLE; /* Parameter discarded because sequencer is disabled */
AdcHandle.Init.NbrOfDiscConversion = 0;
AdcHandle.Init.ExternalTrigConvEdge =
ADC_EXTERNALTRIGCONVEDGE_NONE; /* Conversion start not trigged by an external event */
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.NbrOfConversion = 1;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
AdcHandle.Init.EOCSelection = DISABLE;
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
/* ADC initialization Error */
Error_Handler();
}
}
/**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
static void Error_Handler(void)
{
while (1) {
//Print error message
printf("Error...");
//Sleep 20 ms
//FIXME: #5 No POSIX support on Zephyr for usleep
usleep(20000);
}
}
void light_sensor(void *ptr_result)
{
extern pthread_mutex_t mutex_result;
extern pthread_cond_t cond_result;
extern bool new_result_for_supervisor;
extern bool new_result_for_publisher;
int rc;
printf("Light sensor thread started\n");
/* Variable used to get converted value */
__IO int32_t converted_light = 0;
int light_normalized = 0;
SystemClock_Config();
/*##-2- Configure the ADC peripheral*/
ADC_Config();
// Hal API
GPIO_InitTypeDef GPIO_InitStruct;
// Pin A0 is in PA6, so we need to set it up!
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//Read initial time:
struct timespec initial_ts;
clock_gettime(CLOCK_MONOTONIC, &initial_ts);
// Infinite loop
while (true) {
// Select ADC_CH0
ADC_Select_CH0();
HAL_ADC_Start(&AdcHandle);
HAL_ADC_PollForConversion(&AdcHandle, 1000);
converted_light = HAL_ADC_GetValue(&AdcHandle);
HAL_ADC_Stop(&AdcHandle);
// Normalize the light value
light_normalized = converted_light * MAX_LIGHT_VALUE / MAX_CONVERTED_VALUE;
// Print the light value
printf("Light percentage: %d \n", light_normalized);
// Send the light value to the supervisor thread
pthread_mutex_lock(&mutex_result);
((thread_result_t *)ptr_result)->type = LIGHT;
((thread_result_t *)ptr_result)->value = light_normalized;
// Notify the supervisor & publisher threads
new_result_for_supervisor = true;
new_result_for_publisher = true;
pthread_cond_broadcast(&cond_result);
pthread_mutex_unlock(&mutex_result);
//Wait for period (next activation):
incr_timespec(&initial_ts, &light_sensor_period);
//FIXME: #6 No POSIX support on Zephyr for clock_nanosleep
if ((rc = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &initial_ts, NULL)) !=
0) {
printf("Error in clock_nanosleep: %d\n", rc);
pthread_exit(NULL);
}
}
}