/
measure.c
308 lines (268 loc) · 6.48 KB
/
measure.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "vmtype.h"
#include "vmsystem.h"
#include "vmthread.h"
#include "vmdcl.h"
#include "vmtimer.h"
#include "vmmemory.h"
#include "vmdcl_gpio.h"
#include "log.h"
#include "console.h"
#include "measure.h"
typedef enum
{
A128,
B32,
A64,
TERMINATE,
WAIT
} hx711_op;
typedef struct
{
hx711_op op;
int delay;
void (*callback)(void*, int);
void* callback_env;
VM_DCL_HANDLE scl_handle;
VM_DCL_HANDLE sda_handle;
} hx711_task;
typedef struct
{
vm_mutex_t mutex;
hx711_task task;
} handle_details;
handle_details* open_handles[MAX_HANDLE + 1] = {NULL};
static void new_task(handle_details* details, hx711_op op, int delay, void (*callback)(void*, int), void* callback_env)
{
vm_mutex_lock(&details->mutex);
details->task.op = op;
details->task.delay = delay;
details->task.callback = callback;
details->task.callback_env = callback_env;
vm_mutex_unlock(&details->mutex);
}
static void get_task(handle_details* details, hx711_task* task)
{
vm_mutex_lock(&details->mutex);
*task = details->task;
vm_mutex_unlock(&details->mutex);
}
static void enter_standby(hx711_task* task)
{
vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_WRITE_HIGH, NULL);
vm_thread_sleep(task->delay+1);
}
static VMBOOL wait_result(hx711_task* task)
{
vm_dcl_gpio_control_level_status_t pin_status;
int timeout_counter = 8;
vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_WRITE_LOW, NULL);
vm_dcl_control(task->sda_handle, VM_DCL_GPIO_COMMAND_READ, &pin_status);
while (timeout_counter > 0 && pin_status.level_status == VM_DCL_GPIO_IO_HIGH)
{
vm_thread_sleep(100);
vm_dcl_control(task->sda_handle, VM_DCL_GPIO_COMMAND_READ, &pin_status);
timeout_counter--;
}
if (pin_status.level_status == VM_DCL_GPIO_IO_LOW)
{
return TRUE;
}
else
{
return FALSE;
}
}
static int read_bit(hx711_task* task)
{
vm_dcl_gpio_control_level_status_t pin_status;
vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_WRITE_HIGH, NULL);
vm_dcl_control(task->sda_handle, VM_DCL_GPIO_COMMAND_READ, &pin_status);
vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_WRITE_LOW, NULL);
if (pin_status.level_status == VM_DCL_GPIO_IO_LOW)
{
return 0;
}
else
{
return 1;
}
}
static int read_result(hx711_task* task)
{
int result;
result = -read_bit(task);
for(int i=0;i<23;i++)
{
result <<= 1;
result |= read_bit(task);
}
return result;
}
static void clock_setup_pulses(hx711_task* task)
{
int setup_pulses;
switch (task->op)
{
case A128:
setup_pulses = 1;
break;
case A64:
setup_pulses = 3;
break;
case B32:
setup_pulses = 2;
break;
default:
setup_pulses = 1;
}
for (int i=0;i<setup_pulses;i++)
{
read_bit(task);
}
}
static VMINT32 measurement(VM_THREAD_HANDLE thread_handle, void* user_data)
{
VMINT measured_result;
handle_details* details;
hx711_task current_task;
write_console("thread enter\n");
details = (handle_details*) user_data;
get_task(details, ¤t_task);
enter_standby(¤t_task);
while (1)
{
get_task(details, ¤t_task);
if (current_task.op == TERMINATE)
{
break;
}
if (current_task.op == WAIT)
{
enter_standby(¤t_task);
continue;
}
if (current_task.op != A128)
{
wait_result(¤t_task);
read_result(¤t_task);
clock_setup_pulses(¤t_task);
}
wait_result(¤t_task);
measured_result = read_result(¤t_task);
current_task.callback(current_task.callback_env, measured_result);
enter_standby(¤t_task);
}
write_console("thread exit\n");
enter_standby(¤t_task);
vm_mutex_lock(&details->mutex);
vm_dcl_close(current_task.sda_handle);
vm_dcl_close(current_task.scl_handle);
current_task.callback(current_task.callback_env, 0);
vm_free(details);
return 0;
}
static void dummy_callback(void* env, int result)
{
}
static handle_details* get_handle_details(ADC_HANDLE handle)
{
if (handle < 0 && handle > MAX_HANDLE)
{
return NULL;
}
return open_handles[handle];
}
ADC_HANDLE open_hx711(int scl_pin, int sda_pin)
{
ADC_HANDLE handle = 0;
handle_details* details;
hx711_task* task;
while (open_handles[handle] != NULL)
{
handle++;
if (handle > MAX_HANDLE)
{
return ADC_HANDLE_INVALID;
}
}
details = vm_calloc(sizeof(handle_details));
if (details == NULL)
{
return ADC_HANDLE_INVALID;
}
task = &details->task;
task->scl_handle = vm_dcl_open(VM_DCL_GPIO, scl_pin);
if (task->scl_handle == VM_DCL_HANDLE_INVALID)
{
vm_free(details);
return ADC_HANDLE_INVALID;
}
vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_SET_MODE_0, NULL);
vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_OUT, NULL);
vm_dcl_control(task->scl_handle, VM_DCL_GPIO_COMMAND_WRITE_HIGH, NULL);
task->sda_handle = vm_dcl_open(VM_DCL_GPIO, sda_pin);
if (task->sda_handle == VM_DCL_HANDLE_INVALID)
{
vm_dcl_close(task->scl_handle);
vm_free(details);
return ADC_HANDLE_INVALID;
}
vm_dcl_control(task->sda_handle, VM_DCL_GPIO_COMMAND_SET_MODE_0, NULL);
vm_dcl_control(task->sda_handle, VM_DCL_GPIO_COMMAND_SET_DIRECTION_IN, NULL);
vm_mutex_init(&details->mutex);
task->op = WAIT;
task->delay = 100;
task->callback = dummy_callback;
task->callback_env = NULL;
open_handles[handle] = details;
write_console("open\n");
vm_thread_create(measurement, (void*) details, (VM_THREAD_PRIORITY) 0);
return handle;
}
void close_hx711(ADC_HANDLE handle, void (*callback)(void*, int), void* callback_env)
{
handle_details* details;
details = get_handle_details(handle);
if (details != NULL)
{
open_handles[handle] = NULL;
new_task(details, TERMINATE, 0, callback, callback_env);
write_console("close\n");
}
}
void set_hx711_a128(ADC_HANDLE handle, int delay, void (*callback)(void*, int), void* callback_env)
{
handle_details* details;
details = get_handle_details(handle);
if (details != NULL)
{
new_task(details, A128, delay, callback, callback_env);
}
}
void set_hx711_a64(ADC_HANDLE handle, int delay, void (*callback)(void*, int), void* callback_env)
{
handle_details* details;
details = get_handle_details(handle);
if (details != NULL)
{
new_task(details, A64, delay, callback, callback_env);
}
}
void set_hx711_b32(ADC_HANDLE handle, int delay, void (*callback)(void*, int), void* callback_env)
{
handle_details* details;
details = get_handle_details(handle);
if (details != NULL)
{
new_task(details, B32, delay, callback, callback_env);
}
}
void set_hx711_wait(ADC_HANDLE handle, int delay)
{
handle_details* details;
details = get_handle_details(handle);
if (details != NULL)
{
new_task(details, WAIT, delay, dummy_callback, NULL);
}
}