-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPIO_Platform.ino
141 lines (116 loc) · 3.16 KB
/
GPIO_Platform.ino
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
/*
* Copyright (C) 2014 Lars Marowsky-Bree <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <DueTimer.h>
#include <Wire.h>
#include <ADS1115.h>
#include <Arduino.h>
#include "GPIO_Platform.h"
#include "Sources.h"
#include "Outputs.h"
#include "SerialMonitor.h"
#include "Lowlevel.h"
/////////////////////////////////////////////////////////////////////////////
// Set to 1 for having all functions log what they do
// Set to 2 for much more verbose logging
int debug = 1;
tMaster Master;
///////////////////////////////////////////////////////////////////////
static int gcd(int a, int b) {
int t;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
return a;
}
// To be called while interrupts are disabled
void master_period(void) {
int i;
int new_period;
// We default to ticking the timer at least every 10s
new_period = 10000000;
for (i = 0; i < Sources.entries; i++)
new_period = gcd(new_period, Sources.s[i].period);
for (i = 0; i < Outputs.entries; i++)
new_period = gcd(new_period, Outputs.out[i].period);
if (new_period != Master.period) {
Timer1.stop();
Master.period = new_period;
Timer1.setPeriod(Master.period);
if (debug) {
SerialUSB.print("DEBUG Timer period adjusted to ");
SerialUSB.println(Master.period);
}
if (new_period < 50) {
SerialUSB.print("WARN Timer period very short: ");
SerialUSB.println(Master.period);
}
}
if (Master.started)
Timer1.start();
}
static void master_handler() {
sources_poll();
outputs_push();
}
/****************************************************************************
Main code
****************************************************************************/
void setup(){
SerialMonitor_setup();
if (debug > 1) {
delay(20000);
SerialUSB.println("Joining I²C bus at 400 kHz");
}
Wire.begin();
Wire.setClock(400000);
if (debug > 1) {
delay(20000);
SerialUSB.println("setting up sources");
}
sources_setup();
if (debug > 1) {
delay(1000);
SerialUSB.println("Setting up outputs");
}
outputs_setup();
if (debug > 1) {
delay(1000);
SerialUSB.println("Setting up ringbuffer");
}
if (debug > 1) {
delay(1000);
SerialUSB.println("Setting resolution");
}
analogReadResolution(12);
analogWriteResolution(12);
if (debug > 1) {
delay(1000);
SerialUSB.println("Attaching timer");
}
Timer1.attachInterrupt(master_handler);
if (debug > 1) {
delay(1000);
SerialUSB.println("At your service (hopefully).");
}
}
void loop(){
SerialMonitor_poll();
sources_process();
}