aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorPerry Hung <iperry@alum.mit.edu>2010-03-30 22:03:00 -0400
committerPerry Hung <iperry@alum.mit.edu>2010-03-30 22:03:00 -0400
commit2c33d55bae8f9e0e009634072ab05302fc734a65 (patch)
tree63285f96e795fc423cd5bf573340d12f6c8d5513 /core
parent23149e9706ff0a6a338e13804456dff4c655e34b (diff)
parent1d3861ef93f8423176c6010ab606abdab00a7cbd (diff)
downloadlibrambutan-2c33d55bae8f9e0e009634072ab05302fc734a65.tar.gz
librambutan-2c33d55bae8f9e0e009634072ab05302fc734a65.zip
Merge branch 'master' into rcc-dev
Conflicts: Makefile
Diffstat (limited to 'core')
-rw-r--r--core/Print.cpp215
-rw-r--r--core/Print.h62
-rw-r--r--core/WProgram.h8
-rw-r--r--core/binary.h515
-rw-r--r--core/bits.h35
-rw-r--r--core/comm/HardwareSerial.cpp97
-rw-r--r--core/comm/HardwareSerial.h48
-rw-r--r--core/comm/HardwareUsb.cpp96
-rw-r--r--core/comm/HardwareUsb.h48
-rw-r--r--core/ext_interrupts.c104
-rw-r--r--core/ext_interrupts.h56
-rw-r--r--core/io.h141
-rw-r--r--core/main.cxx28
-rw-r--r--core/pwm.c48
-rw-r--r--core/pwm.h41
-rw-r--r--core/time.c72
-rw-r--r--core/time.h64
-rw-r--r--core/wiring.c60
-rw-r--r--core/wiring.h60
-rw-r--r--core/wiring_analog.c39
-rw-r--r--core/wiring_digital.c140
-rw-r--r--core/wiring_math.cpp38
-rw-r--r--core/wiring_math.h32
-rw-r--r--core/wiring_shift.c40
24 files changed, 2087 insertions, 0 deletions
diff --git a/core/Print.cpp b/core/Print.cpp
new file mode 100644
index 0000000..efe3eef
--- /dev/null
+++ b/core/Print.cpp
@@ -0,0 +1,215 @@
+/*
+ Print.cpp - Base class that provides print() and println()
+ Copyright (c) 2008 David A. Mellis. All right reserved.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+ Modified 23 November 2006 by David A. Mellis
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+#include "wiring.h"
+
+#include "Print.h"
+
+// Public Methods //////////////////////////////////////////////////////////////
+
+/* default implementation: may be overridden */
+void Print::write(const char *str)
+{
+ while (*str)
+ write(*str++);
+}
+
+/* default implementation: may be overridden */
+void Print::write(const uint8_t *buffer, size_t size)
+{
+ while (size--)
+ write(*buffer++);
+}
+
+void Print::print(uint8_t b)
+{
+ this->write(b);
+}
+
+void Print::print(char c)
+{
+ print((byte) c);
+}
+
+void Print::print(const char str[])
+{
+ write(str);
+}
+
+void Print::print(int n)
+{
+ print((long) n);
+}
+
+void Print::print(unsigned int n)
+{
+ print((unsigned long) n);
+}
+
+void Print::print(long n)
+{
+ if (n < 0) {
+ print('-');
+ n = -n;
+ }
+ printNumber(n, 10);
+}
+
+void Print::print(unsigned long n)
+{
+ printNumber(n, 10);
+}
+
+void Print::print(long n, int base)
+{
+ if (base == 0)
+ print((char) n);
+ else if (base == 10)
+ print(n);
+ else
+ printNumber(n, base);
+}
+
+void Print::print(double n)
+{
+ printFloat(n, 2);
+}
+
+void Print::println(void)
+{
+ print('\r');
+ print('\n');
+}
+
+void Print::println(char c)
+{
+ print(c);
+ println();
+}
+
+void Print::println(const char c[])
+{
+ print(c);
+ println();
+}
+
+void Print::println(uint8_t b)
+{
+ print(b);
+ println();
+}
+
+void Print::println(int n)
+{
+ print(n);
+ println();
+}
+
+void Print::println(unsigned int n)
+{
+ print(n);
+ println();
+}
+
+void Print::println(long n)
+{
+ print(n);
+ println();
+}
+
+void Print::println(unsigned long n)
+{
+ print(n);
+ println();
+}
+
+void Print::println(long n, int base)
+{
+ print(n, base);
+ println();
+}
+
+void Print::println(double n)
+{
+ print(n);
+ println();
+}
+
+// Private Methods /////////////////////////////////////////////////////////////
+
+void Print::printNumber(unsigned long n, uint8_t base)
+{
+ unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
+ unsigned long i = 0;
+
+ if (n == 0) {
+ print('0');
+ return;
+ }
+
+ while (n > 0) {
+ buf[i++] = n % base;
+ n /= base;
+ }
+
+ for (; i > 0; i--)
+ print((char) (buf[i - 1] < 10 ?
+ '0' + buf[i - 1] :
+ 'A' + buf[i - 1] - 10));
+}
+
+void Print::printFloat(double number, uint8_t digits)
+{
+ // Handle negative numbers
+ if (number < 0.0)
+ {
+ print('-');
+ number = -number;
+ }
+
+ // Round correctly so that print(1.999, 2) prints as "2.00"
+ double rounding = 0.5;
+ for (uint8_t i=0; i<digits; ++i)
+ rounding /= 10.0;
+
+ number += rounding;
+
+ // Extract the integer part of the number and print it
+ unsigned long int_part = (unsigned long)number;
+ double remainder = number - (double)int_part;
+ print(int_part);
+
+ // Print the decimal point, but only if there are digits beyond
+ if (digits > 0)
+ print(".");
+
+ // Extract digits from the remainder one at a time
+ while (digits-- > 0)
+ {
+ remainder *= 10.0;
+ int toPrint = int(remainder);
+ print(toPrint);
+ remainder -= toPrint;
+ }
+}
diff --git a/core/Print.h b/core/Print.h
new file mode 100644
index 0000000..a69e85d
--- /dev/null
+++ b/core/Print.h
@@ -0,0 +1,62 @@
+/*
+ Print.h - Base class that provides print() and println()
+ Copyright (c) 2008 David A. Mellis. All right reserved.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef Print_h
+#define Print_h
+
+#include <inttypes.h>
+#include <stdio.h> // for size_t
+
+#define DEC 10
+#define HEX 16
+#define OCT 8
+#define BIN 2
+#define BYTE 0
+
+class Print
+{
+ private:
+ void printNumber(unsigned long, uint8_t);
+ void printFloat(double, uint8_t);
+ public:
+ virtual void write(uint8_t) = 0;
+ virtual void write(const char *str);
+ virtual void write(const uint8_t *buffer, size_t size);
+ void print(char);
+ void print(const char[]);
+ void print(uint8_t);
+ void print(int);
+ void print(unsigned int);
+ void print(long);
+ void print(unsigned long);
+ void print(long, int);
+ void print(double);
+ void println(void);
+ void println(char);
+ void println(const char[]);
+ void println(uint8_t);
+ void println(int);
+ void println(unsigned int);
+ void println(long);
+ void println(unsigned long);
+ void println(long, int);
+ void println(double);
+};
+
+#endif
diff --git a/core/WProgram.h b/core/WProgram.h
new file mode 100644
index 0000000..1d38776
--- /dev/null
+++ b/core/WProgram.h
@@ -0,0 +1,8 @@
+#include "wiring.h"
+#include "HardwareSerial.h"
+#include "HardwareUsb.h"
+#include "math.h"
+#include "usb.h"
+
+void setup();
+void loop();
diff --git a/core/binary.h b/core/binary.h
new file mode 100644
index 0000000..af14980
--- /dev/null
+++ b/core/binary.h
@@ -0,0 +1,515 @@
+#ifndef Binary_h
+#define Binary_h
+
+#define B0 0
+#define B00 0
+#define B000 0
+#define B0000 0
+#define B00000 0
+#define B000000 0
+#define B0000000 0
+#define B00000000 0
+#define B1 1
+#define B01 1
+#define B001 1
+#define B0001 1
+#define B00001 1
+#define B000001 1
+#define B0000001 1
+#define B00000001 1
+#define B10 2
+#define B010 2
+#define B0010 2
+#define B00010 2
+#define B000010 2
+#define B0000010 2
+#define B00000010 2
+#define B11 3
+#define B011 3
+#define B0011 3
+#define B00011 3
+#define B000011 3
+#define B0000011 3
+#define B00000011 3
+#define B100 4
+#define B0100 4
+#define B00100 4
+#define B000100 4
+#define B0000100 4
+#define B00000100 4
+#define B101 5
+#define B0101 5
+#define B00101 5
+#define B000101 5
+#define B0000101 5
+#define B00000101 5
+#define B110 6
+#define B0110 6
+#define B00110 6
+#define B000110 6
+#define B0000110 6
+#define B00000110 6
+#define B111 7
+#define B0111 7
+#define B00111 7
+#define B000111 7
+#define B0000111 7
+#define B00000111 7
+#define B1000 8
+#define B01000 8
+#define B001000 8
+#define B0001000 8
+#define B00001000 8
+#define B1001 9
+#define B01001 9
+#define B001001 9
+#define B0001001 9
+#define B00001001 9
+#define B1010 10
+#define B01010 10
+#define B001010 10
+#define B0001010 10
+#define B00001010 10
+#define B1011 11
+#define B01011 11
+#define B001011 11
+#define B0001011 11
+#define B00001011 11
+#define B1100 12
+#define B01100 12
+#define B001100 12
+#define B0001100 12
+#define B00001100 12
+#define B1101 13
+#define B01101 13
+#define B001101 13
+#define B0001101 13
+#define B00001101 13
+#define B1110 14
+#define B01110 14
+#define B001110 14
+#define B0001110 14
+#define B00001110 14
+#define B1111 15
+#define B01111 15
+#define B001111 15
+#define B0001111 15
+#define B00001111 15
+#define B10000 16
+#define B010000 16
+#define B0010000 16
+#define B00010000 16
+#define B10001 17
+#define B010001 17
+#define B0010001 17
+#define B00010001 17
+#define B10010 18
+#define B010010 18
+#define B0010010 18
+#define B00010010 18
+#define B10011 19
+#define B010011 19
+#define B0010011 19
+#define B00010011 19
+#define B10100 20
+#define B010100 20
+#define B0010100 20
+#define B00010100 20
+#define B10101 21
+#define B010101 21
+#define B0010101 21
+#define B00010101 21
+#define B10110 22
+#define B010110 22
+#define B0010110 22
+#define B00010110 22
+#define B10111 23
+#define B010111 23
+#define B0010111 23
+#define B00010111 23
+#define B11000 24
+#define B011000 24
+#define B0011000 24
+#define B00011000 24
+#define B11001 25
+#define B011001 25
+#define B0011001 25
+#define B00011001 25
+#define B11010 26
+#define B011010 26
+#define B0011010 26
+#define B00011010 26
+#define B11011 27
+#define B011011 27
+#define B0011011 27
+#define B00011011 27
+#define B11100 28
+#define B011100 28
+#define B0011100 28
+#define B00011100 28
+#define B11101 29
+#define B011101 29
+#define B0011101 29
+#define B00011101 29
+#define B11110 30
+#define B011110 30
+#define B0011110 30
+#define B00011110 30
+#define B11111 31
+#define B011111 31
+#define B0011111 31
+#define B00011111 31
+#define B100000 32
+#define B0100000 32
+#define B00100000 32
+#define B100001 33
+#define B0100001 33
+#define B00100001 33
+#define B100010 34
+#define B0100010 34
+#define B00100010 34
+#define B100011 35
+#define B0100011 35
+#define B00100011 35
+#define B100100 36
+#define B0100100 36
+#define B00100100 36
+#define B100101 37
+#define B0100101 37
+#define B00100101 37
+#define B100110 38
+#define B0100110 38
+#define B00100110 38
+#define B100111 39
+#define B0100111 39
+#define B00100111 39
+#define B101000 40
+#define B0101000 40
+#define B00101000 40
+#define B101001 41
+#define B0101001 41
+#define B00101001 41
+#define B101010 42
+#define B0101010 42
+#define B00101010 42
+#define B101011 43
+#define B0101011 43
+#define B00101011 43
+#define B101100 44
+#define B0101100 44
+#define B00101100 44
+#define B101101 45
+#define B0101101 45
+#define B00101101 45
+#define B101110 46
+#define B0101110 46
+#define B00101110 46
+#define B101111 47
+#define B0101111 47
+#define B00101111 47
+#define B110000 48
+#define B0110000 48
+#define B00110000 48
+#define B110001 49
+#define B0110001 49
+#define B00110001 49
+#define B110010 50
+#define B0110010 50
+#define B00110010 50
+#define B110011 51
+#define B0110011 51
+#define B00110011 51
+#define B110100 52
+#define B0110100 52
+#define B00110100 52
+#define B110101 53
+#define B0110101 53
+#define B00110101 53
+#define B110110 54
+#define B0110110 54
+#define B00110110 54
+#define B110111 55
+#define B0110111 55
+#define B00110111 55
+#define B111000 56
+#define B0111000 56
+#define B00111000 56
+#define B111001 57
+#define B0111001 57
+#define B00111001 57
+#define B111010 58
+#define B0111010 58
+#define B00111010 58
+#define B111011 59
+#define B0111011 59
+#define B00111011 59
+#define B111100 60
+#define B0111100 60
+#define B00111100 60
+#define B111101 61
+#define B0111101 61
+#define B00111101 61
+#define B111110 62
+#define B0111110 62
+#define B00111110 62
+#define B111111 63
+#define B0111111 63
+#define B00111111 63
+#define B1000000 64
+#define B01000000 64
+#define B1000001 65
+#define B01000001 65
+#define B1000010 66
+#define B01000010 66
+#define B1000011 67
+#define B01000011 67
+#define B1000100 68
+#define B01000100 68
+#define B1000101 69
+#define B01000101 69
+#define B1000110 70
+#define B01000110 70
+#define B1000111 71
+#define B01000111 71
+#define B1001000 72
+#define B01001000 72
+#define B1001001 73
+#define B01001001 73
+#define B1001010 74
+#define B01001010 74
+#define B1001011 75
+#define B01001011 75
+#define B1001100 76
+#define B01001100 76
+#define B1001101 77
+#define B01001101 77
+#define B1001110 78
+#define B01001110 78
+#define B1001111 79
+#define B01001111 79
+#define B1010000 80
+#define B01010000 80
+#define B1010001 81
+#define B01010001 81
+#define B1010010 82
+#define B01010010 82
+#define B1010011 83
+#define B01010011 83
+#define B1010100 84
+#define B01010100 84
+#define B1010101 85
+#define B01010101 85
+#define B1010110 86
+#define B01010110 86
+#define B1010111 87
+#define B01010111 87
+#define B1011000 88
+#define B01011000 88
+#define B1011001 89
+#define B01011001 89
+#define B1011010 90
+#define B01011010 90
+#define B1011011 91
+#define B01011011 91
+#define B1011100 92
+#define B01011100 92
+#define B1011101 93
+#define B01011101 93
+#define B1011110 94
+#define B01011110 94
+#define B1011111 95
+#define B01011111 95
+#define B1100000 96
+#define B01100000 96
+#define B1100001 97
+#define B01100001 97
+#define B1100010 98
+#define B01100010 98
+#define B1100011 99
+#define B01100011 99
+#define B1100100 100
+#define B01100100 100
+#define B1100101 101
+#define B01100101 101
+#define B1100110 102
+#define B01100110 102
+#define B1100111 103
+#define B01100111 103
+#define B1101000 104
+#define B01101000 104
+#define B1101001 105
+#define B01101001 105
+#define B1101010 106
+#define B01101010 106
+#define B1101011 107
+#define B01101011 107
+#define B1101100 108
+#define B01101100 108
+#define B1101101 109
+#define B01101101 109
+#define B1101110 110
+#define B01101110 110
+#define B1101111 111
+#define B01101111 111
+#define B1110000 112
+#define B01110000 112
+#define B1110001 113
+#define B01110001 113
+#define B1110010 114
+#define B01110010 114
+#define B1110011 115
+#define B01110011 115
+#define B1110100 116
+#define B01110100 116
+#define B1110101 117
+#define B01110101 117
+#define B1110110 118
+#define B01110110 118
+#define B1110111 119
+#define B01110111 119
+#define B1111000 120
+#define B01111000 120
+#define B1111001 121
+#define B01111001 121
+#define B1111010 122
+#define B01111010 122
+#define B1111011 123
+#define B01111011 123
+#define B1111100 124
+#define B01111100 124
+#define B1111101 125
+#define B01111101 125
+#define B1111110 126
+#define B01111110 126
+#define B1111111 127
+#define B01111111 127
+#define B10000000 128
+#define B10000001 129
+#define B10000010 130
+#define B10000011 131
+#define B10000100 132
+#define B10000101 133
+#define B10000110 134
+#define B10000111 135
+#define B10001000 136
+#define B10001001 137
+#define B10001010 138
+#define B10001011 139
+#define B10001100 140
+#define B10001101 141
+#define B10001110 142
+#define B10001111 143
+#define B10010000 144
+#define B10010001 145
+#define B10010010 146
+#define B10010011 147
+#define B10010100 148
+#define B10010101 149
+#define B10010110 150
+#define B10010111 151
+#define B10011000 152
+#define B10011001 153
+#define B10011010 154
+#define B10011011 155
+#define B10011100 156
+#define B10011101 157
+#define B10011110 158
+#define B10011111 159
+#define B10100000 160
+#define B10100001 161
+#define B10100010 162
+#define B10100011 163
+#define B10100100 164
+#define B10100101 165
+#define B10100110 166
+#define B10100111 167
+#define B10101000 168
+#define B10101001 169
+#define B10101010 170
+#define B10101011 171
+#define B10101100 172
+#define B10101101 173
+#define B10101110 174
+#define B10101111 175
+#define B10110000 176
+#define B10110001 177
+#define B10110010 178
+#define B10110011 179
+#define B10110100 180
+#define B10110101 181
+#define B10110110 182
+#define B10110111 183
+#define B10111000 184
+#define B10111001 185
+#define B10111010 186
+#define B10111011 187
+#define B10111100 188
+#define B10111101 189
+#define B10111110 190
+#define B10111111 191
+#define B11000000 192
+#define B11000001 193
+#define B11000010 194
+#define B11000011 195
+#define B11000100 196
+#define B11000101 197
+#define B11000110 198
+#define B11000111 199
+#define B11001000 200
+#define B11001001 201
+#define B11001010 202
+#define B11001011 203
+#define B11001100 204
+#define B11001101 205
+#define B11001110 206
+#define B11001111 207
+#define B11010000 208
+#define B11010001 209
+#define B11010010 210
+#define B11010011 211
+#define B11010100 212
+#define B11010101 213
+#define B11010110 214
+#define B11010111 215
+#define B11011000 216
+#define B11011001 217
+#define B11011010 218
+#define B11011011 219
+#define B11011100 220
+#define B11011101 221
+#define B11011110 222
+#define B11011111 223
+#define B11100000 224
+#define B11100001 225
+#define B11100010 226
+#define B11100011 227
+#define B11100100 228
+#define B11100101 229
+#define B11100110 230
+#define B11100111 231
+#define B11101000 232
+#define B11101001 233
+#define B11101010 234
+#define B11101011 235
+#define B11101100 236
+#define B11101101 237
+#define B11101110 238
+#define B11101111 239
+#define B11110000 240
+#define B11110001 241
+#define B11110010 242
+#define B11110011 243
+#define B11110100 244
+#define B11110101 245
+#define B11110110 246
+#define B11110111 247
+#define B11111000 248
+#define B11111001 249
+#define B11111010 250
+#define B11111011 251
+#define B11111100 252
+#define B11111101 253
+#define B11111110 254
+#define B11111111 255
+
+#endif
diff --git a/core/bits.h b/core/bits.h
new file mode 100644
index 0000000..e614094
--- /dev/null
+++ b/core/bits.h
@@ -0,0 +1,35 @@
+// BIT DEFINITION
+
+#define BIT0 0x00000001
+#define BIT1 0x00000002
+#define BIT2 0x00000004
+#define BIT3 0x00000008
+#define BIT4 0x00000010
+#define BIT5 0x00000020
+#define BIT6 0x00000040
+#define BIT7 0x00000080
+#define BIT8 0x00000100
+#define BIT9 0x00000200
+#define BIT10 0x00000400
+#define BIT11 0x00000800
+#define BIT12 0x00001000
+#define BIT13 0x00002000
+#define BIT14 0x00004000
+#define BIT15 0x00008000
+#define BIT16 0x00010000
+#define BIT17 0x00020000
+#define BIT18 0x00040000
+#define BIT19 0x00080000
+#define BIT20 0x00100000
+#define BIT21 0x00200000
+#define BIT22 0x00400000
+#define BIT23 0x00800000
+#define BIT24 0x01000000
+#define BIT25 0x02000000
+#define BIT26 0x04000000
+#define BIT27 0x08000000
+#define BIT28 0x10000000
+#define BIT29 0x20000000
+#define BIT30 0x40000000
+#define BIT31 0x80000000
+
diff --git a/core/comm/HardwareSerial.cpp b/core/comm/HardwareSerial.cpp
new file mode 100644
index 0000000..04d8d18
--- /dev/null
+++ b/core/comm/HardwareSerial.cpp
@@ -0,0 +1,97 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Created: 12/19/09 05:15:24 EST
+ * Copyright (c) 2009 Perry L. Hung. All rights reserved.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file HardwareSerial.cpp
+ *
+ * @brief Wiring-like serial api
+ */
+
+#include "wiring.h"
+#include "HardwareSerial.h"
+#include "usart.h"
+#include "gpio.h"
+#include "timers.h"
+
+#define USART1_TX_PORT GPIOA_BASE
+#define USART1_TX_PIN 9
+#define USART1_RX_PORT GPIOA_BASE
+#define USART1_RX_PIN 10
+
+#define USART2_TX_PORT GPIOA_BASE
+#define USART2_TX_PIN 2
+#define USART2_RX_PORT GPIOA_BASE
+#define USART2_RX_PIN 3
+
+#define USART3_TX_PORT GPIOB_BASE
+#define USART3_TX_PIN 10
+#define USART3_RX_PORT GPIOB_BASE
+#define USART3_RX_PIN 11
+
+HardwareSerial::HardwareSerial(uint8_t usartNum) {
+ ASSERT(usartNum == 1 ||
+ usartNum == 2 ||
+ usartNum == 3);
+ this->usartNum = usartNum;
+}
+
+uint8_t HardwareSerial::read(void) {
+ return usart_getc(usartNum);
+}
+
+uint32 HardwareSerial::available(void) {
+
+ return usart_data_available(usartNum);
+}
+
+void HardwareSerial::write(unsigned char ch) {
+ usart_putc(usartNum, ch);
+}
+
+void HardwareSerial::begin(uint32_t baud) {
+ ASSERT(!(baud > USART_MAX_BAUD));
+
+ /* Set appropriate pin modes */
+ switch (usartNum) {
+ case 1:
+ gpio_set_mode(USART1_TX_PORT, USART1_TX_PIN, GPIO_MODE_AF_OUTPUT_PP);
+ gpio_set_mode(USART1_RX_PORT, USART1_RX_PIN, GPIO_MODE_INPUT_FLOATING);
+ /* Turn off any pwm */
+ timers_disable_channel(1, 2);
+ break;
+ case 2:
+ gpio_set_mode(USART2_TX_PORT, USART2_TX_PIN, GPIO_MODE_AF_OUTPUT_PP);
+ gpio_set_mode(USART2_RX_PORT, USART2_RX_PIN, GPIO_MODE_INPUT_FLOATING);
+ /* Turn off any pwm */
+ timers_disable_channel(2, 3);
+ break;
+ case 3:
+ gpio_set_mode(USART3_TX_PORT, USART3_TX_PIN, GPIO_MODE_AF_OUTPUT_PP);
+ gpio_set_mode(USART3_RX_PORT, USART3_RX_PIN, GPIO_MODE_INPUT_FLOATING);
+ break;
+ default:
+ ASSERT(0);
+ }
+
+ usart_init(usartNum, baud);
+}
+
+HardwareSerial Serial1(1);
+HardwareSerial Serial2(2);
+HardwareSerial Serial3(3);
diff --git a/core/comm/HardwareSerial.h b/core/comm/HardwareSerial.h
new file mode 100644
index 0000000..a3913f9
--- /dev/null
+++ b/core/comm/HardwareSerial.h
@@ -0,0 +1,48 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Created: 12/19/09 05:45:37 EST
+ * Copyright (c) 2009 Perry L. Hung. All rights reserved.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file HardwareSerial.h
+ *
+ * @brief
+ */
+
+#ifndef _HARDWARESERIAL_H_
+#define _HARDWARESERIAL_H_
+
+#include "Print.h"
+
+class HardwareSerial : public Print {
+ private:
+ uint8 usartNum;
+ public:
+ HardwareSerial(uint8_t);
+ void begin(uint32_t);
+ uint32 available(void);
+ uint8_t read(void);
+ void flush(void);
+ virtual void write(unsigned char);
+ using Print::write;
+};
+
+extern HardwareSerial Serial1;
+extern HardwareSerial Serial2;
+extern HardwareSerial Serial3;
+#endif
+
diff --git a/core/comm/HardwareUsb.cpp b/core/comm/HardwareUsb.cpp
new file mode 100644
index 0000000..269a68f
--- /dev/null
+++ b/core/comm/HardwareUsb.cpp
@@ -0,0 +1,96 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file HardwareUsb.cpp
+ *
+ * @brief Wiring like serial api to USB virtual COM
+ */
+
+#include "wiring.h"
+#include "HardwareUsb.h"
+#include "bootVect.h"
+#include "usb.h"
+
+HardwareUsb::HardwareUsb(void) {
+ mapleVectTable = (BootVectTable*)(BOOTLOADER_VECT_TABLE);
+ mapleVectTable->serial_tx_cb = usb_tx_cb;
+ mapleVectTable->serial_rx_cb = usb_rx_cb;
+ mapleVectTable->usb_local_obj_ptr = this;
+ rx_buffer_offset_in = 0;
+ rx_buffer_offset_out = 0;
+}
+
+uint8_t HardwareUsb::read(void) {
+ uint8_t outVal = rx_buffer[rx_buffer_offset_out++];
+
+#if 1
+ if (rx_buffer_offset_out == rx_buffer_offset_in) {
+ flush();
+ }
+#endif
+
+ return outVal;
+}
+
+uint8_t HardwareUsb::available(void) {
+ ASSERT(rx_buffer_offset_out >= 0);
+ // return rx_buffer_offset+1;
+ // return usb_serialGetRecvLen();
+ return rx_buffer_offset_in - rx_buffer_offset_out;
+}
+
+void HardwareUsb::flush(void) {
+ rx_buffer_offset_in = 0;
+ rx_buffer_offset_out = 0;
+}
+
+void HardwareUsb::write(unsigned char ch) {
+ usb_serialWriteChar(ch);
+}
+
+void HardwareUsb::begin(void) {
+ /* placeholder for usb<->uart linking */
+}
+
+void HardwareUsb::usb_rx_cb(void) {
+ BootVectTable *vectTable = (BootVectTable*)(BOOTLOADER_VECT_TABLE);
+ HardwareUsb *thisPtr = (HardwareUsb*) vectTable->usb_local_obj_ptr;
+
+ uint8_t numBytes = usb_serialGetRecvLen();
+
+#if 0
+ /* ONE-SHOT-TO-READ Version (buffer cleared on next recv interrupt */
+ usb_copyRecvBuffer(thisPtr->rx_buffer,numBytes);
+ thisPtr->rx_buffer_offset_in = numBytes;
+ thisPtr->rx_buffer_offset_out = 0;
+#else
+ /* FIFO DISCARD OVERFLOW VERSION */
+ if ((thisPtr->rx_buffer_offset_in + numBytes) > (USB_SERIAL_BUF_SIZE)) {
+ numBytes = USB_SERIAL_BUF_SIZE - thisPtr->rx_buffer_offset_in;
+ }
+
+ if (numBytes > 0) {
+ ASSERT(thisPtr->rx_buffer_offset_in <= USB_SERIAL_BUF_SIZE);
+ usb_copyRecvBuffer(&(thisPtr->rx_buffer[thisPtr->rx_buffer_offset_in]),numBytes);
+ thisPtr->rx_buffer_offset_in += numBytes;
+ }
+#endif
+}
+
+void HardwareUsb::usb_tx_cb(void) {
+ /* placeholder for when serial dumps exceed buflen */
+}
diff --git a/core/comm/HardwareUsb.h b/core/comm/HardwareUsb.h
new file mode 100644
index 0000000..b8733c1
--- /dev/null
+++ b/core/comm/HardwareUsb.h
@@ -0,0 +1,48 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file HardwareUsb.h
+ *
+ * @brief Wiring like serial api to USB virtual COM
+ */
+
+#ifndef _HARDWAREUSB_H_
+#define _HARDWAREUSB_H_
+
+#include "Print.h"
+#include "bootVect.h"
+#include "usb.h"
+
+class HardwareUsb : public Print {
+ private:
+ BootVectTable* mapleVectTable;
+ static void usb_rx_cb(void);
+ static void usb_tx_cb(void);
+ unsigned char rx_buffer[USB_SERIAL_BUF_SIZE];
+ int8_t rx_buffer_offset_out;
+ int8_t rx_buffer_offset_in;
+ public:
+ HardwareUsb(void);
+ void begin();
+ uint8_t available(void);
+ uint8_t read(void);
+ void flush(void);
+ virtual void write(unsigned char);
+ using Print::write;
+};
+
+#endif //_HARDWAREUSB_H
diff --git a/core/ext_interrupts.c b/core/ext_interrupts.c
new file mode 100644
index 0000000..20ec11f
--- /dev/null
+++ b/core/ext_interrupts.c
@@ -0,0 +1,104 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Created: 12/18/09 02:39:48
+ * Copyright (c) 2009 Perry L. Hung. All rights reserved.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file ext_interrupts.c
+ *
+ * @brief Wiring-like interface for external interrupts
+ */
+
+#include "wiring.h"
+#include "exti.h"
+#include "ext_interrupts.h"
+
+typedef struct ExtiInfo {
+ uint8_t channel;
+ uint8_t port;
+} ExtiInfo;
+
+static ExtiInfo PIN_TO_EXTI_CHANNEL[NR_MAPLE_PINS] = {
+ {EXTI3, EXTI_CONFIG_PORTA}, // D0/PA3
+ {EXTI2, EXTI_CONFIG_PORTA}, // D1/PA2
+ {EXTI0, EXTI_CONFIG_PORTA}, // D2/PA0
+ {EXTI1, EXTI_CONFIG_PORTA}, // D3/PA1
+ {EXTI5, EXTI_CONFIG_PORTB}, // D4/PB5
+ {EXTI6, EXTI_CONFIG_PORTB}, // D5/PB6
+ {EXTI8, EXTI_CONFIG_PORTA}, // D6/PA8
+ {EXTI9, EXTI_CONFIG_PORTA}, // D7/PA9
+ {EXTI10, EXTI_CONFIG_PORTA}, // D8/PA10
+ {EXTI7, EXTI_CONFIG_PORTB}, // D9/PB7
+ {EXTI4, EXTI_CONFIG_PORTA}, // D10/PA4
+ {EXTI7, EXTI_CONFIG_PORTA}, // D11/PA7
+ {EXTI6, EXTI_CONFIG_PORTA}, // D12/PA6
+ {EXTI5, EXTI_CONFIG_PORTA}, // D13/PA5
+};
+
+
+/**
+ * @brief Attach an interrupt handler to be triggered on a given
+ * transition on the pin. Runs in interrupt context
+ *
+ * @param[in] pin Maple pin number
+ * @param[in] handler Function to run upon external interrupt trigger.
+ * @param[in] mode Type of transition to trigger on, eg falling, rising, etc.
+ *
+ * @sideeffect Registers a handler
+ */
+int attachInterrupt(uint8_t pin, voidFuncPtr handler, ExtInterruptTriggerMode mode) {
+ uint8_t outMode;
+ /* Parameter checking */
+ if (pin >= NR_MAPLE_PINS) {
+ return EXT_INTERRUPT_INVALID_PIN;
+ }
+
+ if (!handler) {
+ ASSERT(0);
+ return EXT_INTERRUPT_INVALID_FUNCTION;
+ }
+
+ switch (mode) {
+ case RISING:
+ outMode = EXTI_RISING;
+ break;
+ case FALLING:
+ outMode = EXTI_FALLING;
+ break;
+ case CHANGE:
+ outMode = EXTI_RISING_FALLING;
+ break;
+ default:
+ ASSERT(0);
+ return EXT_INTERRUPT_INVALID_MODE;;
+ }
+
+ exti_attach_interrupt(PIN_TO_EXTI_CHANNEL[pin].channel,
+ PIN_TO_EXTI_CHANNEL[pin].port,
+ handler, mode);
+
+ return 0;
+}
+
+int detachInterrupt(uint8_t pin) {
+ if (!(pin < NR_MAPLE_PINS)) {
+ return EXT_INTERRUPT_INVALID_PIN;
+ }
+
+ exti_detach_interrupt(PIN_TO_EXTI_CHANNEL[pin].channel);
+}
+
diff --git a/core/ext_interrupts.h b/core/ext_interrupts.h
new file mode 100644
index 0000000..d0e6365
--- /dev/null
+++ b/core/ext_interrupts.h
@@ -0,0 +1,56 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Created: 12/18/09 02:40:09
+ * Copyright (c) 2009 Perry L. Hung. All rights reserved.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file ext_interrupts.h
+ *
+ * @brief External interrupt wiring prototypes and types
+ */
+
+#ifndef _EXT_INTERRUPTS_H_
+#define _EXT_INTERRUPTS_H_
+
+typedef enum ExtInterruptTriggerMode {
+ RISING,
+ FALLING,
+ CHANGE
+} ExtInterruptTriggerMode;
+
+
+enum ExtInterruptError {
+ EXT_INTERRUPT_INVALID_PIN = (-1),
+ EXT_INTERRUPT_INVALID_FUNCTION = (-2),
+ EXT_INTERRUPT_INVALID_MODE = (-3),
+
+};
+
+#ifdef __cplusplus
+extern "C"{
+#endif
+
+int attachInterrupt(uint8_t pin, voidFuncPtr, ExtInterruptTriggerMode mode);
+int detachInterrupt(uint8_t pin);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif
+
diff --git a/core/io.h b/core/io.h
new file mode 100644
index 0000000..9f6aa4a
--- /dev/null
+++ b/core/io.h
@@ -0,0 +1,141 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Created: 12/18/09 02:40:56
+ * Copyright (c) 2009 Perry L. Hung. All rights reserved.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file io.h
+ *
+ * @brief
+ */
+
+#ifndef _IO_H
+#define _IO_H
+
+#include <inttypes.h>
+#include "gpio.h"
+#include "adc.h"
+
+#ifdef __cplusplus
+extern "C"{
+#endif
+
+/* stash these here for now */
+#define D0 0
+#define D1 1
+#define D2 2
+#define D3 3
+#define D4 4
+#define D5 5
+#define D6 6
+#define D7 7
+#define D8 8
+#define D9 9
+#define D10 10
+#define D11 11
+#define D12 12
+#define D13 13
+#define D14 14
+#define D15 15
+#define D16 16
+#define D16 16
+#define D17 17
+#define D18 18
+#define D19 19
+#define D20 20
+#define D21 21
+#define D22 22
+#define D23 23
+#define D24 24
+#define D25 25
+#define D26 26
+#define D27 27
+#define D28 28
+#define D29 29
+#define D30 30
+#define D31 31
+#define D32 32
+#define D33 33
+#define D34 34
+#define D35 35
+#define D36 36
+#define D37 37
+#define D38 38
+#define D39 39
+
+typedef enum WiringPinMode {
+ OUTPUT,
+ OUTPUT_OPEN_DRAIN,
+ INPUT,
+ INPUT_ANALOG,
+ INPUT_PULLUP,
+ INPUT_PULLDOWN,
+ INPUT_FLOATING,
+ PWM
+} WiringPinMode;
+
+typedef struct PinMapping {
+ GPIO_Port *port;
+ uint32 pin;
+ uint32 adc;
+ TimerCCR timer_channel;
+} PinMapping;
+
+#define ADC_INVALID 0xFFFFFFFF
+#define TIMER_INVALID (TimerCCR)0xFFFFFFFF
+
+/* Set pin to mode
+ * pinMode(pin, mode):
+ * pin -> {0-38, D0-D39, A0-16}
+ * mode -> {
+ * INPUT/INPUT_DIGITAL
+ * INPUT_PULLUP
+ * INPUT_PULLDOWN
+ * INPUT_ANALOG
+ * OUTPUT/OUTPUT_PP
+ * OUTPUT_OPEN_DRAIN
+ * }
+ */
+void pinMode(uint8_t, uint8_t);
+
+/*
+ * Writes VALUE to digital pin[0-38]
+ * digitalWrite(pin, value):
+ * pin -> {0-38, D0-D39, A0-16}
+ * value -> LOW, HIGH;
+*/
+void digitalWrite(uint8_t, uint8_t);
+
+/* Read a digital value from pin, the pin mode must be set to
+ * {INPUT, INPUT_PULLUP, INPUT_PULLDOWN}
+ * digitalRead(pin)
+ * pin -> {0-38, D0-D39, A0-16}
+ */
+uint32_t digitalRead(uint8_t);
+
+/* Read an analog value from pin, the pin mode must be set
+ * to INPUT_ANALOG
+ * analogRead(pin)
+ * pin -> {A0-A16}
+ * */
+uint32_t analogRead(uint8_t);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+#endif
+
diff --git a/core/main.cxx b/core/main.cxx
new file mode 100644
index 0000000..24b6483
--- /dev/null
+++ b/core/main.cxx
@@ -0,0 +1,28 @@
+int main(void)
+{
+ init();
+ setup();
+
+ while (1) {
+ loop();
+ }
+ return 0;
+}
+
+/* Required for C++ hackery */
+/* TODO: This really shouldn't go here... move it later
+ * */
+extern "C" void __cxa_pure_virtual(void) {
+ while(1)
+ ;
+}
+
+/* Implemented:
+ * void pinMode(pin, mode)
+ * void digitalWrite(pin, value)
+ * uint32_t digitalRead(pin)
+ * uint32_t analogRead(pin)
+ * void randomSeed(seed)
+ * long random(max)
+ * long random(min, max)
+ * */
diff --git a/core/pwm.c b/core/pwm.c
new file mode 100644
index 0000000..2cc815c
--- /dev/null
+++ b/core/pwm.c
@@ -0,0 +1,48 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Created: 12/18/09 02:41:24
+ * Copyright (c) 2009 Perry L. Hung. All rights reserved.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file pwm.c
+ *
+ * @brief
+ */
+
+#include "wiring.h"
+#include "timers.h"
+#include "io.h"
+#include "pwm.h"
+
+extern const PinMapping PIN_MAP[NR_MAPLE_PINS];
+
+void pwmWrite(uint8_t pin, uint16_t duty_cycle) {
+ TimerCCR ccr;
+
+ if (pin >= NR_MAPLE_PINS) {
+ return;
+ }
+
+ ccr = PIN_MAP[pin].timer_channel;
+
+ if (ccr == TIMER_INVALID)
+ return;
+
+ timer_pwm_write_ccr(ccr, duty_cycle);
+}
+
+
diff --git a/core/pwm.h b/core/pwm.h
new file mode 100644
index 0000000..60d805d
--- /dev/null
+++ b/core/pwm.h
@@ -0,0 +1,41 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Created: 12/18/09 02:41:27
+ * Copyright (c) 2009 Perry L. Hung. All rights reserved.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file pwm.h
+ *
+ * @brief
+ */
+
+#ifndef _PWM_H
+#define _PWM_H
+
+#ifdef __cplusplus
+extern "C"{
+#endif
+
+void pwmWrite(uint8_t, uint16_t);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif
+
diff --git a/core/time.c b/core/time.c
new file mode 100644
index 0000000..164d978
--- /dev/null
+++ b/core/time.c
@@ -0,0 +1,72 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Created: 12/18/09 02:41:36
+ * Copyright (c) 2009 Perry L. Hung. All rights reserved.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file time.c
+ *
+ * @brief
+ */
+
+#include "libmaple.h"
+#include "systick.h"
+#include "time.h"
+
+#define CYCLES_PER_MICROSECOND 72
+#define FUDGE 42
+
+extern volatile uint32_t systick_timer_millis;
+
+unsigned long millis() {
+ unsigned long m;
+ m = systick_timer_millis;
+ return m;
+}
+
+void delay(unsigned long ms)
+{
+ unsigned long start = millis();
+
+ while (millis() - start <= ms)
+ ;
+}
+
+
+
+#if 1
+void delayMicroseconds(uint32_t us) {
+ uint32_t target;
+ uint32_t last, cur, count;
+ /* fudge factor hacky hack hack for function overhead */
+ target = us * CYCLES_PER_MICROSECOND - FUDGE;
+
+ /* Get current count */
+ last = systick_get_count();
+ cur = systick_get_count();
+ count = last;
+ while ((count-cur) <= target) {
+ cur = systick_get_count();
+
+ /* check for overflow */
+ if (cur > last) {
+ count += MAPLE_RELOAD_VAL;
+ }
+ last = cur;
+ }
+}
+#endif
diff --git a/core/time.h b/core/time.h
new file mode 100644
index 0000000..68b3901
--- /dev/null
+++ b/core/time.h
@@ -0,0 +1,64 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Created: 12/18/09 02:41:40
+ * Copyright (c) 2009 Perry L. Hung. All rights reserved.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file time.h
+ *
+ * @brief
+ */
+
+#ifndef _TIME_H
+#define _TIME_H
+
+#ifdef __cplusplus
+extern "C"{
+#endif
+/* Returns time since boot in milliseconds */
+uint32_t millis(void);
+
+/* Time in microseconds since boot */
+uint32_t micros(void);
+
+/* Delay for ms milliseconds */
+void delay(unsigned long ms);
+
+/* Delay for us microseconds */
+void delayMicroseconds(uint32_t us);
+
+#if 0
+static inline void delay_us(uint32 us) {
+ us *= 12;
+ asm volatile("mov r0, %[us] \n\t"
+ "subs r0, #2 \n\t"
+"1: \n\t"
+ "subs r0, r0, #1 \n\t"
+ "bne 1b"
+ :
+ : [us] "r" (us)
+ : "r0", "cc");
+
+}
+#endif
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+
+#endif
+
diff --git a/core/wiring.c b/core/wiring.c
new file mode 100644
index 0000000..488d15c
--- /dev/null
+++ b/core/wiring.c
@@ -0,0 +1,60 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Created: 12/18/09 02:41:47
+ * Copyright (c) 2009 Perry L. Hung. All rights reserved.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file wiring.c
+ *
+ * @brief
+ */
+
+#include "wiring.h"
+#include "stm32f10x_flash.h"
+#include "stm32f10x_map.h"
+#include "stm32f10x_nvic.h"
+#include "rcc.h"
+#include "systick.h"
+#include "gpio.h"
+
+void NVIC_Configuration(void);
+
+void init(void) {
+ rcc_init();
+ NVIC_Configuration();
+ systick_init();
+ gpio_init();
+ adc_init();
+ timer_init(1, 1);
+ timer_init(2, 1);
+ timer_init(3, 1);
+ timer_init(4, 1);
+}
+
+void NVIC_Configuration(void) {
+#ifdef VECT_TAB_ROM
+ NVIC_SetVectorTable(USER_ADDR_ROM, 0x0);
+#warning writing to ROM
+#elif defined VECT_TAB_RAM
+ NVIC_SetVectorTable(USER_ADDR_RAM, 0x0);
+#warning writing to RAM
+#else // VECT_TAB_BASE
+ /* Set the Vector Table base location at 0x08000000 */
+ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
+#endif
+}
+
diff --git a/core/wiring.h b/core/wiring.h
new file mode 100644
index 0000000..bcf6eda
--- /dev/null
+++ b/core/wiring.h
@@ -0,0 +1,60 @@
+#ifndef _WIRING_H_
+#define _WIRING_H_
+
+#include "libmaple.h"
+#include "timers.h"
+#include "io.h"
+#include "binary.h"
+#include "bits.h"
+#include "time.h"
+#include "pwm.h"
+#include "ext_interrupts.h"
+
+#ifdef __cplusplus
+extern "C"{
+#endif
+
+#define MAPLE 1
+#define NR_MAPLE_PINS 39 // temporary
+
+#define HIGH 0x1
+#define LOW 0x0
+
+#define true 0x1
+#define false 0x0
+
+#define SERIAL 0x0
+#define DISPLAY 0x1
+
+#define LSBFIRST 0
+#define MSBFIRST 1
+
+#define USER_ADDR_ROM 0x08005000
+#define USER_ADDR_RAM 0x20000C00
+
+#define lowByte(w) ((w) & 0xff)
+#define highByte(w) ((w) >> 8)
+#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
+#define bitSet(value, bit) ((value) |= (1UL << (bit)))
+#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
+#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
+#define bit(b) (1UL << (b))
+
+
+typedef uint8 boolean;
+typedef uint8 byte;
+
+void init(void);
+unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
+void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte val);
+
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+
+
+
+#endif
+
diff --git a/core/wiring_analog.c b/core/wiring_analog.c
new file mode 100644
index 0000000..d7d7150
--- /dev/null
+++ b/core/wiring_analog.c
@@ -0,0 +1,39 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Created: 12/18/09 02:41:55
+ * Copyright (c) 2009 Perry L. Hung. All rights reserved.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file wiring_analog.c
+ *
+ * @brief
+ */
+
+#include "libmaple.h"
+#include "wiring.h"
+#include "io.h"
+
+extern const PinMapping PIN_MAP[NR_MAPLE_PINS];
+
+/* Assumes that the ADC has been initialized and
+ * that the pin is set to ANALOG_INPUT */
+uint32_t analogRead(uint8_t pin) {
+ if (pin >= NR_ANALOG_PINS)
+ return 0;
+
+ return adc_read(PIN_MAP[pin].adc);
+}
diff --git a/core/wiring_digital.c b/core/wiring_digital.c
new file mode 100644
index 0000000..71dae57
--- /dev/null
+++ b/core/wiring_digital.c
@@ -0,0 +1,140 @@
+/* *****************************************************************************
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Created: 12/18/09 02:42:00
+ * Copyright (c) 2009 Perry L. Hung. All rights reserved.
+ *
+ * ****************************************************************************/
+
+/**
+ * @file wiring_digital.c
+ *
+ * @brief
+ */
+
+#include "wiring.h"
+#include "io.h"
+
+#define ADC0 0
+#define ADC1 1
+#define ADC2 2
+#define ADC3 3
+#define ADC4 4
+#define ADC5 5
+#define ADC6 6
+#define ADC7 7
+#define ADC8 8
+#define ADC9 9
+#define ADC10 10
+#define ADC11 11
+#define ADC12 12
+#define ADC13 13
+#define ADC14 14
+#define ADC15 15
+#define ADC16 16
+
+const PinMapping PIN_MAP[NR_MAPLE_PINS] = {
+ {GPIOA_BASE, 3, ADC3, TIMER2_CH4_CCR}, // D0/PA3
+ {GPIOA_BASE, 2, ADC2, TIMER2_CH3_CCR}, // D1/PA2
+ {GPIOA_BASE, 0, ADC0, TIMER2_CH1_CCR}, // D2/PA0
+ {GPIOA_BASE, 1, ADC1, TIMER2_CH2_CCR}, // D3/PA1
+ {GPIOB_BASE, 5, ADC_INVALID, TIMER_INVALID}, // D4/PB5
+ {GPIOB_BASE, 6, ADC_INVALID, TIMER4_CH1_CCR}, // D5/PB6
+ {GPIOA_BASE, 8, ADC_INVALID, TIMER1_CH1_CCR}, // D6/PA8
+ {GPIOA_BASE, 9, ADC_INVALID, TIMER1_CH2_CCR}, // D7/PA9
+ {GPIOA_BASE, 10, ADC_INVALID, TIMER1_CH3_CCR}, // D8/PA10
+ {GPIOB_BASE, 7, ADC_INVALID, TIMER4_CH2_CCR}, // D9/PB7
+ {GPIOA_BASE, 4, ADC4, TIMER_INVALID}, // D10/PA4
+ {GPIOA_BASE, 7, ADC7, TIMER3_CH2_CCR}, // D11/PA7
+ {GPIOA_BASE, 6, ADC6, TIMER3_CH1_CCR}, // D12/PA6
+ {GPIOA_BASE, 5, ADC5, TIMER_INVALID}, // D13/PA5
+ {GPIOB_BASE, 8, ADC_INVALID, TIMER4_CH3_CCR}, // D14/PB8
+ /* Little header */
+ {GPIOC_BASE, 0, ADC10, TIMER_INVALID}, // D15/PC0
+ {GPIOC_BASE, 1, ADC11, TIMER_INVALID}, // D16/PC1
+ {GPIOC_BASE, 2, ADC12, TIMER_INVALID}, // D17/PC2
+ {GPIOC_BASE, 3, ADC13, TIMER_INVALID}, // D18/PC3
+ {GPIOC_BASE, 4, ADC14, TIMER_INVALID}, // D19/PC4
+ {GPIOC_BASE, 5, ADC15, TIMER_INVALID}, // D20/PC5
+ /* External header */
+ {GPIOC_BASE, 13, ADC_INVALID, TIMER_INVALID}, // D21/PC13
+ {GPIOC_BASE, 14, ADC_INVALID, TIMER_INVALID}, // D22/PC14
+ {GPIOC_BASE, 15, ADC_INVALID, TIMER_INVALID}, // D23/PC15
+ {GPIOB_BASE, 9, ADC_INVALID, TIMER4_CH4_CCR}, // D24/PB9
+ {GPIOD_BASE, 2, ADC_INVALID, TIMER_INVALID}, // D25/PD2
+ {GPIOC_BASE, 10, ADC_INVALID, TIMER_INVALID}, // D26/PC10
+ {GPIOB_BASE, 0, ADC8, TIMER3_CH3_CCR}, // D27/PB0
+ {GPIOB_BASE, 1, ADC9, TIMER3_CH4_CCR}, // D28/PB1
+ {GPIOB_BASE, 10, ADC_INVALID, TIMER_INVALID}, // D29/PB10
+ {GPIOB_BASE, 11, ADC_INVALID, TIMER_INVALID}, // D30/PB11
+ {GPIOB_BASE, 12, ADC_INVALID, TIMER_INVALID}, // D31/PB12
+ {GPIOB_BASE, 13, ADC_INVALID, TIMER_INVALID}, // D32/PB13
+ {GPIOB_BASE, 14, ADC_INVALID, TIMER_INVALID}, // D33/PB14
+ {GPIOB_BASE, 15, ADC_INVALID, TIMER_INVALID}, // D34/PB15
+ {GPIOC_BASE, 6, ADC_INVALID, TIMER_INVALID}, // D35/PC6
+ {GPIOC_BASE, 7, ADC_INVALID, TIMER_INVALID}, // D36/PC7
+ {GPIOC_BASE, 8, ADC_INVALID, TIMER_INVALID}, // D37/PC8
+ {GPIOC_BASE, 9, ADC_INVALID, TIMER_INVALID} // D38/PC9
+};
+
+void pinMode(uint8_t pin, WiringPinMode mode) {
+ uint8 outputMode;
+
+ if (pin >= NR_MAPLE_PINS)
+ return;
+
+ switch(mode) {
+ case OUTPUT:
+ outputMode = GPIO_MODE_OUTPUT_PP;
+ break;
+ case OUTPUT_OPEN_DRAIN:
+ outputMode = GPIO_MODE_OUTPUT_OD;
+ break;
+ case INPUT:
+ case INPUT_FLOATING:
+ outputMode = GPIO_MODE_INPUT_FLOATING;
+ break;
+ case INPUT_ANALOG:
+ outputMode = GPIO_MODE_INPUT_ANALOG;
+ case INPUT_PULLUP:
+ outputMode = GPIO_MODE_INPUT_PU;
+ break;
+ case INPUT_PULLDOWN:
+ outputMode = GPIO_MODE_INPUT_PD;
+ break;
+ case PWM:
+ outputMode = GPIO_MODE_AF_OUTPUT_PP;
+ break;
+ default:
+ ASSERT(0);
+ return;
+ }
+
+ gpio_set_mode(PIN_MAP[pin].port, PIN_MAP[pin].pin, outputMode);
+}
+
+
+uint32_t digitalRead(uint8_t pin) {
+ if (pin >= NR_MAPLE_PINS)
+ return 0;
+
+ return (PIN_MAP[pin].port->IDR & BIT(PIN_MAP[pin].pin)) ? 1 : 0;
+}
+
+void digitalWrite(uint8_t pin, uint8_t val) {
+ if (pin >= NR_MAPLE_PINS)
+ return;
+
+ gpio_write_bit(PIN_MAP[pin].port, PIN_MAP[pin].pin, val);
+}
diff --git a/core/wiring_math.cpp b/core/wiring_math.cpp
new file mode 100644
index 0000000..9be7dc3
--- /dev/null
+++ b/core/wiring_math.cpp
@@ -0,0 +1,38 @@
+#include <stdlib.h>
+#include "math.h"
+
+/* from newlib:
+ *
+ * rand returns the next pseudo-random integer in sequence; it is a number
+ * between 0 and RAND_MAX (inclusive).
+ *
+ * srand does not return a result. */
+
+
+/* The rest copied from WMath.cpp */
+void randomSeed(unsigned int seed) {
+ if (seed != 0) {
+ srand(seed);
+ }
+}
+
+long random(long howbig) {
+ if (howbig == 0) {
+ return 0;
+ }
+ return rand() % howbig;
+}
+
+long random(long howsmall, long howbig) {
+ if (howsmall >= howbig) {
+ return howsmall;
+ }
+ long diff = howbig - howsmall;
+ return random(diff) + howsmall;
+}
+
+long map(long x, long in_min, long in_max, long out_min, long out_max) {
+ return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+
+
diff --git a/core/wiring_math.h b/core/wiring_math.h
new file mode 100644
index 0000000..f000bbd
--- /dev/null
+++ b/core/wiring_math.h
@@ -0,0 +1,32 @@
+#ifndef _WIRING_MATH_H_
+#define _WIRING_MATH_H_
+
+#include <math.h>
+
+void randomSeed(unsigned int);
+long random(long);
+long random(long, long);
+long map(long, long, long, long, long);
+
+#define PI 3.1415926535897932384626433832795
+#define HALF_PI 1.5707963267948966192313216916398
+#define TWO_PI 6.283185307179586476925286766559
+#define DEG_TO_RAD 0.017453292519943295769236907684886
+#define RAD_TO_DEG 57.295779513082320876798154814105
+
+#define min(a,b) ((a)<(b)?(a):(b))
+#define max(a,b) ((a)>(b)?(a):(b))
+#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
+#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
+#define radians(deg) ((deg)*DEG_TO_RAD)
+#define degrees(rad) ((rad)*RAD_TO_DEG)
+#define sq(x) ((x)*(x))
+
+/* undefine stdlib's abs if encountered */
+#ifdef abs
+#undef abs
+#endif
+#define abs(x) (((x) > 0) ? (x) : -(unsigned)(x))
+
+#endif
+
diff --git a/core/wiring_shift.c b/core/wiring_shift.c
new file mode 100644
index 0000000..28c6dfc
--- /dev/null
+++ b/core/wiring_shift.c
@@ -0,0 +1,40 @@
+/*
+ wiring_shift.c - shiftOut() function
+ Part of Arduino - http://www.arduino.cc/
+
+ Copyright (c) 2005-2006 David A. Mellis
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
+*/
+
+#include "wiring.h"
+
+void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte val)
+{
+ int i;
+
+ for (i = 0; i < 8; i++) {
+ if (bitOrder == LSBFIRST)
+ digitalWrite(dataPin, !!(val & (1 << i)));
+ else
+ digitalWrite(dataPin, !!(val & (1 << (7 - i))));
+
+ digitalWrite(clockPin, HIGH);
+ digitalWrite(clockPin, LOW);
+ }
+}