summaryrefslogtreecommitdiffstats
path: root/ulp_soil.s
blob: 8c77c55243ae8f822b350d31267c5c9911c27237 (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* ULP Soil Sensor

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.

   This file contains assembly code which runs on the ULP.
*/

/* ULP assembly files are passed through C preprocessor first, so include directives
   and C macros may be used in these files 
 */
#include "soc/rtc_cntl_reg.h"
#include "soc/rtc_io_reg.h"
#include "soc/soc_ulp.h"

  /* Configure the number of ADC samples to average on each measurement.
     For convenience, make it a power of 2. */
  .set adc_oversampling_factor_log, 4
  .set adc_oversampling_factor, (1 << adc_oversampling_factor_log)

  .set soil_threshold, 8

  .data   /* .data section */

  /* Define variables, which go into .bss section (zero-initialized data) */
  .bss

  /* Stores maximum difference of last sent value vs. last measured value of all soil sensors */
  .global max_diff
max_diff:
  .long 0

  /* soilx value stores last sent value */
  .global soil0
soil0:
  .long 0

  /* soilx_shadow stores last measured value */
  .global soil0_shadow
soil0_shadow:
  .long 0

  .global soil1
soil1:
  .long 0

  .global soil1_shadow
soil1_shadow:
  .long 0

  .global soil2
soil2:
  .long 0

  .global soil2_shadow
soil2_shadow:
  .long 0

   .global soil3
soil3:
  .long 0

  .global soil3_shadow
soil3_shadow:
  .long 0

   .global soil4
soil4:
  .long 0

  .global soil4_shadow
soil4_shadow:
  .long 0

   .global soil5
soil5:
  .long 0

  .global soil5_shadow
soil5_shadow:
  .long 0

  .global reboots
reboots:
  .long 0

  .global runs
runs:
  .long 0

  /* Entry point into code. */
  .text
  .global entry
entry:

  /* increment ulp_runs counter by one */
  move r0, runs
  ld r1, r0, 0
  add r1, r1, 1
  st r1, r0, 0

  /* set GPIO25/RTC_GPIO06 low to enable moisture sensors */
enable_sensors:
  WRITE_RTC_REG(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_HOLD_S, 1, 0)
  WRITE_RTC_REG(RTC_GPIO_OUT_W1TC_REG, RTC_GPIO_OUT_DATA_W1TC_S + 6, 1, 1)
  WRITE_RTC_REG(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_HOLD_S, 1, 1)

  /* wait about 125ms for SENSOR values to stabilize */
  wait 40000 /* 5 ms */
  wait 40000
  wait 40000
  wait 40000

  wait 40000
  wait 40000
  wait 40000
  wait 40000

  wait 40000
  wait 40000
  wait 40000
  wait 40000

  wait 40000
  wait 40000
  wait 40000
  wait 40000

  wait 40000
  wait 40000
  wait 40000
  wait 40000

  wait 40000
  wait 40000
  wait 40000
  wait 40000

  wait 40000


  /* r2: adc_soilx maps soil sensor to adc channel */
  /* r3: after measurement return to store soilx value to its shadow value */
measure_soil0:
  move r2, adc_soil0
  move r3, save_soil0
  jump start_measurement

  .global save_soil0
save_soil0:
  /* stores current measurement as shadow value */
  move r2, soil0_shadow
  st r0, r2, 0

measure_soil1:
  move r2, adc_soil1
  move r3, save_soil1
  jump start_measurement

  .global save_soil1
save_soil1:
  move r2, soil1_shadow
  st r0, r2, 0

measure_soil2:
  move r2, adc_soil2
  move r3, save_soil2
  jump start_measurement

  .global save_soil2
save_soil2:
  move r2, soil2_shadow
  st r0, r2, 0

measure_soil3:
  move r2, adc_soil3
  move r3, save_soil3
  jump start_measurement

  .global save_soil3
save_soil3:
  move r2, soil3_shadow
  st r0, r2, 0

measure_soil4:
  move r2, adc_soil4
  move r3, save_soil4
  jump start_measurement

  .global save_soil4
save_soil4:
  move r2, soil4_shadow
  st r0, r2, 0

measure_soil5:
  move r2, adc_soil5
  move r3, save_soil5
  jump start_measurement

  .global save_soil5
save_soil5:
  move r2, soil5_shadow
  st r0, r2, 0

  /* set GPIO25/RTC_GPIO06 high to disable moisture sensors */
disable_sensors:
  WRITE_RTC_REG(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_HOLD_S, 1, 0)
  WRITE_RTC_REG(RTC_GPIO_OUT_W1TS_REG, RTC_GPIO_OUT_DATA_W1TS_S + 6, 1, 1)
  WRITE_RTC_REG(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_HOLD_S, 1, 1)
  /* get maximum change in soil value compared to last sent */
calc_max_diff:
  /* reset max_diff, r0, r1 to zero */
  move r0, max_diff
  move r1, 0
  st r1, r0, 0
  move r0, 0

soil0_diff:
  move r1, soil0
  move r2, soil0_shadow
  move r3, soil0_compare_diff

  /* load last sent value and shadow value into r1, r2 */
  ld r1, r1, 0
  ld r2, r2, 0

  /* calculate absolute difference between sent and shadow value to r0 */
  sub r0, r1, r2
  /* in case r2 > r1, absolute_diff calcs r0 = r2 - r1 and returnso to r3 */
  jump absolute_diff, OV

soil0_compare_diff:
  /* load current max_diff to r1 */
  move r2, max_diff
  ld r1, r2, 0

  /* in case r1 (max_diff) > r0 (soil0_diff) continue to next soil diff */
  sub r3, r0, r1
  jump soil1_diff, OV

  /* in case r0 (soil0_diff) > r1 (max_diff) store soil0_diff to max_value */
  st r0, r2, 0

soil1_diff:
  move r1, soil1
  move r2, soil1_shadow
  move r3, soil1_compare_diff

  ld r1, r1, 0
  ld r2, r2, 0

  sub r0, r1, r2
  jump absolute_diff, OV

soil1_compare_diff:
  move r2, max_diff
  ld r1, r2, 0

  sub r3, r0, r1
  jump soil2_diff, OV

  st r0, r2, 0

soil2_diff:
  move r1, soil2
  move r2, soil2_shadow
  move r3, soil2_compare_diff

  ld r1, r1, 0
  ld r2, r2, 0

  sub r0, r1, r2
  jump absolute_diff, OV

soil2_compare_diff:
  move r2, max_diff
  ld r1, r2, 0

  sub r3, r0, r1
  jump soil3_diff, OV

  st r0, r2, 0

soil3_diff:
  move r1, soil3
  move r2, soil3_shadow
  move r3, soil3_compare_diff

  ld r1, r1, 0
  ld r2, r2, 0

  sub r0, r1, r2
  jump absolute_diff, OV

soil3_compare_diff:
  move r2, max_diff
  ld r1, r2, 0
  
  sub r3, r0, r1
  jump soil4_diff, OV

  st r0, r2, 0

soil4_diff:
  move r1, soil4
  move r2, soil4_shadow
  move r3, soil4_compare_diff

  ld r1, r1, 0
  ld r2, r2, 0

  sub r0, r1, r2
  jump absolute_diff, OV

soil4_compare_diff:
  move r2, max_diff
  ld r1, r2, 0
  
  sub r3, r0, r1
  jump soil5_diff, OV

  st r0, r2, 0

soil5_diff:
  move r1, soil5
  move r2, soil5_shadow
  move r3, soil5_compare_diff

  ld r1, r1, 0
  ld r2, r2, 0

  sub r0, r1, r2
  jump absolute_diff, OV

soil5_compare_diff:
  move r2, max_diff
  ld r1, r2, 0

  sub r3, r0, r1
  jump try_wake, OV

  st r0, r2, 0

/* wake in case max_diff >= soil_threshold, copy shadow values to output values else halt */
try_wake:
  move r0, max_diff
  ld r0, r0, 0
  
  jumpr copy_and_wake, soil_threshold, GE
  
  .global exit
exit:
  halt

/* reset accumulator r0 and stage counter */
start_measurement:
  move r0, 0
  stage_rst

measure:
  /* measure adc value to r1, according to soil -> adc channel mapping */
  jump r2

accumulate_measurement:
  /* accumulate adc measurements in r0 */
  add r0, r0, r1

  /* increment loop counter and check exit condition */
  stage_inc 1
  jumps measure, adc_oversampling_factor, lt

  /* divide accumulator by adc_oversampling_factor.
     Since it is chosen as a power of two, use right shift */
  rsh r0, r0, adc_oversampling_factor_log

  /* averaged value is now in r0
     jump back to store value */
  jump r3

absolute_diff:
  sub r0, r2, r1
  jump r3

adc_soil0:
  adc r1, 0, 8  
  jump accumulate_measurement

adc_soil1:
  adc r1, 0, 5
  jump accumulate_measurement

adc_soil2:
  adc r1, 0, 6
  jump accumulate_measurement

adc_soil3:
  adc r1, 0, 7
  jump accumulate_measurement

adc_soil4:
  adc r1, 0, 4
  jump accumulate_measurement

adc_soil5:
  adc r1, 0, 1
  jump accumulate_measurement

copy_and_wake:
  /* copy shadow values into sensor value location */
  move r0, soil0_shadow
  move r1, soil0
  ld r0, r0, 0
  st r0, r1, 0
  move r0, soil1_shadow
  move r1, soil1
  ld r0, r0, 0
  st r0, r1, 0
  move r0, soil2_shadow
  move r1, soil2
  ld r0, r0, 0
  st r0, r1, 0
  move r0, soil3_shadow
  move r1, soil3
  ld r0, r0, 0
  st r0, r1, 0
  move r0, soil4_shadow
  move r1, soil4
  ld r0, r0, 0
  st r0, r1, 0
  move r0, soil5_shadow
  move r1, soil5
  ld r0, r0, 0
  st r0, r1, 0

  /* wake main processor */
  .global wake_up
wake_up:
  /* Check if the system can be woken up */
  READ_RTC_FIELD(RTC_CNTL_LOW_POWER_ST_REG, RTC_CNTL_RDY_FOR_WAKEUP)

  and r0, r0, 1
  jump exit, eq

  /* Wake up the SoC, end program */
  wake
  WRITE_RTC_FIELD(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN, 0)
  halt