diff options
Diffstat (limited to 'src/wiring')
-rw-r--r-- | src/wiring/Print.cpp | 215 | ||||
-rw-r--r-- | src/wiring/Print.h | 62 | ||||
-rw-r--r-- | src/wiring/binary.h | 515 | ||||
-rw-r--r-- | src/wiring/bits.h | 35 | ||||
-rw-r--r-- | src/wiring/comm/Serial.cpp | 67 | ||||
-rw-r--r-- | src/wiring/comm/Serial.h | 20 | ||||
-rw-r--r-- | src/wiring/ext_interrupts.c | 65 | ||||
-rw-r--r-- | src/wiring/ext_interrupts.h | 31 | ||||
-rw-r--r-- | src/wiring/io.h | 110 | ||||
-rw-r--r-- | src/wiring/math.cpp | 38 | ||||
-rw-r--r-- | src/wiring/math.h | 30 | ||||
-rw-r--r-- | src/wiring/pwm.c | 40 | ||||
-rw-r--r-- | src/wiring/pwm.h | 18 | ||||
-rw-r--r-- | src/wiring/time.c | 59 | ||||
-rw-r--r-- | src/wiring/time.h | 27 | ||||
-rw-r--r-- | src/wiring/wiring.c | 85 | ||||
-rw-r--r-- | src/wiring/wiring.h | 56 | ||||
-rw-r--r-- | src/wiring/wiring_analog.c | 33 | ||||
-rw-r--r-- | src/wiring/wiring_digital.c | 55 | ||||
-rw-r--r-- | src/wiring/wiring_shift.c | 42 |
20 files changed, 1603 insertions, 0 deletions
diff --git a/src/wiring/Print.cpp b/src/wiring/Print.cpp new file mode 100644 index 0000000..20f5e40 --- /dev/null +++ b/src/wiring/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/src/wiring/Print.h b/src/wiring/Print.h new file mode 100644 index 0000000..a69e85d --- /dev/null +++ b/src/wiring/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/src/wiring/binary.h b/src/wiring/binary.h new file mode 100644 index 0000000..af14980 --- /dev/null +++ b/src/wiring/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/src/wiring/bits.h b/src/wiring/bits.h new file mode 100644 index 0000000..e614094 --- /dev/null +++ b/src/wiring/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/src/wiring/comm/Serial.cpp b/src/wiring/comm/Serial.cpp new file mode 100644 index 0000000..3c43436 --- /dev/null +++ b/src/wiring/comm/Serial.cpp @@ -0,0 +1,67 @@ +//#include "stm32f10x_usart.h" +#include "stm32f10x_gpio.h" +#include "stm32f10x_rcc.h" +#include "gpio.h" +#include "Serial.h" +#include "wiring.h" + +int SendChar (int ch) { + /* Write character to Serial Port */ +// USART_SendData(USART2, (unsigned char) ch); +// while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) +// ; +// +// // while (!(USART2->SR & USART_FLAG_TXE)); +// return (ch); +} + +void uart_send(const char* str) { + while (*str != '\0') { + SendChar(*str); + str++; + } +} + + +Serial::Serial() { +} + +void Serial::write(uint8_t c) { + SendChar(c); +} + +void Serial::begin(uint32_t baud) { + // USART_InitTypeDef USART_InitStructure; + + /* Turn on the clock */ + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); + +#if 0 + /* Configure USART2 Tx as alternate function push-pull */ + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_Init(GPIOA, &GPIO_InitStructure); + + /* Configure USART2 Rx as input floating */ + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; + GPIO_Init(GPIOA, &GPIO_InitStructure); +#endif + + pinMode(1, GPIO_MODE_AF_OUTPUT_PP); + pinMode(0, GPIO_MODE_INPUT_FLOATING); + + /* Enable USART2 */ +// USART_InitStructure.USART_BaudRate = baud; +// USART_InitStructure.USART_WordLength = USART_WordLength_8b; +// USART_InitStructure.USART_StopBits = USART_StopBits_1; +// USART_InitStructure.USART_Parity = USART_Parity_No ; +// USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; +// USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; +// USART_Init(USART2, &USART_InitStructure); +// USART_Cmd(USART2, ENABLE); +} + +Serial Serial1; diff --git a/src/wiring/comm/Serial.h b/src/wiring/comm/Serial.h new file mode 100644 index 0000000..0e7a126 --- /dev/null +++ b/src/wiring/comm/Serial.h @@ -0,0 +1,20 @@ +#ifndef _SERIAL_H_ +#define _SERIAL_H_ + +#include <inttypes.h> +#include <Print.h> + +class Serial : public Print { + public: + Serial(); + void begin(uint32_t); + uint8_t available(void); + int read(void); + void flush(void); + virtual void write(uint8_t); + using Print::write; +}; + +extern Serial Serial1; +#endif + diff --git a/src/wiring/ext_interrupts.c b/src/wiring/ext_interrupts.c new file mode 100644 index 0000000..0a35472 --- /dev/null +++ b/src/wiring/ext_interrupts.c @@ -0,0 +1,65 @@ +#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, void (*handler)(void), uint8_t mode) { + /* Parameter checking */ + if (pin >= NR_MAPLE_PINS) { + return EXT_INTERRUPT_INVALID_PIN; + } + + if (!handler) { + return EXT_INTERRUPT_INVALID_FUNCTION; + } + + if (!(mode < NR_EXTI_MODES)) { + return EXT_INTERRUPT_INVALID_MODE; + } + + exti_attach_interrupt(PIN_TO_EXTI_CHANNEL[pin].channel, + PIN_TO_EXTI_CHANNEL[pin].port, + handler, mode); +} + +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/src/wiring/ext_interrupts.h b/src/wiring/ext_interrupts.h new file mode 100644 index 0000000..d6fb23f --- /dev/null +++ b/src/wiring/ext_interrupts.h @@ -0,0 +1,31 @@ +#ifndef _EXT_INTERRUPTS_H_ +#define _EXT_INTERRUPTS_H_ + +#include "exti.h" + +#define RISING EXTI_RISING +#define FALLING EXTI_FALLING +#define CHANGE EXTI_RISING_FALLING + + +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, void ((*)(void)), uint8_t); +int detachInterrupt(uint8_t); + +#ifdef __cplusplus +} +#endif + + +#endif + diff --git a/src/wiring/io.h b/src/wiring/io.h new file mode 100644 index 0000000..387326a --- /dev/null +++ b/src/wiring/io.h @@ -0,0 +1,110 @@ +#ifndef _IO_H +#define _IO_H + +#include <inttypes.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 + +#define A0 D14 +#define A1 D15 +#define A2 D16 +#define A3 D17 +#define A4 D18 +#define A5 D19 +#define A6 D0 +#define A7 D1 +#define A8 D2 +#define A9 D3 +#define A10 D10 +#define A11 D11 +#define A12 D12 +#define A13 D13 +#define A14 D26 +#define A15 D11 + +/* 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/src/wiring/math.cpp b/src/wiring/math.cpp new file mode 100644 index 0000000..9be7dc3 --- /dev/null +++ b/src/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/src/wiring/math.h b/src/wiring/math.h new file mode 100644 index 0000000..ee269e9 --- /dev/null +++ b/src/wiring/math.h @@ -0,0 +1,30 @@ +#ifndef _MAPLE_MATH_H_ +#define _MAPLE_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/src/wiring/pwm.c b/src/wiring/pwm.c new file mode 100644 index 0000000..0c33c3a --- /dev/null +++ b/src/wiring/pwm.c @@ -0,0 +1,40 @@ +#include "wiring.h" +#include "timers.h" +#include "gpio.h" +#include "pwm.h" + +#define NOT_A_TIMER 0 + +static const TimerCCR PIN_TO_TIMER[NR_MAPLE_PINS] = { + TIMER2_CH4_CCR, // D0/A6 + TIMER2_CH3_CCR, // D1/A7 + TIMER2_CH1_CCR, // D2/A8 + TIMER2_CH2_CCR, // D3/A9 + NOT_A_TIMER, // D4 + TIMER4_CH1_CCR, // D5 + TIMER1_CH1_CCR, // D6 + TIMER1_CH2_CCR, // D7 + TIMER1_CH3_CCR, // D8 + TIMER4_CH2_CCR, // D9 + NOT_A_TIMER, // D10/A10 + TIMER3_CH2_CCR, // D11/A11 + TIMER3_CH1_CCR, // D12/A12 + NOT_A_TIMER, // D13/A13 +}; + +void pwmWrite(uint8_t pin, uint16_t duty_cycle) { + TimerCCR ccr; + + if (pin >= NR_MAPLE_PINS) { + return; + } + + ccr = PIN_TO_TIMER[pin]; + + if (ccr == NOT_A_TIMER) + return; + + timer_pwm_write_ccr(ccr, duty_cycle); +} + + diff --git a/src/wiring/pwm.h b/src/wiring/pwm.h new file mode 100644 index 0000000..3ff440b --- /dev/null +++ b/src/wiring/pwm.h @@ -0,0 +1,18 @@ +#ifndef _PWM_H +#define _PWM_H + +#ifdef __cplusplus +extern "C"{ +#endif + +#define PWM GPIO_MODE_AF_OUTPUT_PP + +void pwmWrite(uint8_t, uint16_t); + +#ifdef __cplusplus +} +#endif + + +#endif + diff --git a/src/wiring/time.c b/src/wiring/time.c new file mode 100644 index 0000000..bf6a28b --- /dev/null +++ b/src/wiring/time.c @@ -0,0 +1,59 @@ +#include "systick.h" +#include "time.h" + +#define CYCLES_PER_MICROSECOND 72 +#define THE_SECRET_TO_LIFE_THE_UNIVERSE_AND_EVERYTHING 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) + ; +} + + +#define MAGIC 4096 +/* HZ = 1000 + * n HZ*/ +void delayMicroseconds(uint32_t us) { + uint32_t target; + uint32_t last, cur, count; + +#if 0 + asm volatile("mov r0, %[count] \n\t" +"1: \n\t" + "subs r0, r0, #1 \n\t" + "bne 1b" + : + : [count] "r" (count) + : "r0", "cc"); +#endif + +#if 1 + /* fudge factor hacky hack hack for function overhead */ + target = us * CYCLES_PER_MICROSECOND - THE_SECRET_TO_LIFE_THE_UNIVERSE_AND_EVERYTHING; + + /* 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/src/wiring/time.h b/src/wiring/time.h new file mode 100644 index 0000000..694545b --- /dev/null +++ b/src/wiring/time.h @@ -0,0 +1,27 @@ +#ifndef _TIME_H +#define _TIME_H + +#include <inttypes.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); + +#ifdef __cplusplus +} // extern "C" +#endif + + +#endif + diff --git a/src/wiring/wiring.c b/src/wiring/wiring.c new file mode 100644 index 0000000..83679c7 --- /dev/null +++ b/src/wiring/wiring.c @@ -0,0 +1,85 @@ +#include <inttypes.h> +#include "stm32f10x_flash.h" +#include "stm32f10x_rcc.h" +#include "stm32f10x_map.h" +#include "stm32f10x_nvic.h" +#include "wiring.h" +#include "systick.h" +#include "gpio.h" + +void RCC_Configuration(void); +void NVIC_Configuration(void); + +void init(void) { + RCC_Configuration(); + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | + RCC_APB2Periph_GPIOB | + RCC_APB2Periph_GPIOC | + RCC_APB2Periph_AFIO + , ENABLE); + NVIC_Configuration(); + + systick_init(); + + gpio_init(); + + /* off for debug */ +// adc_init(); + +// timer_init(1, 1); +// timer_init(2, 1); +// timer_init(3, 1); +// timer_init(4, 1); +} + +void NVIC_Configuration(void) { + /* Set the Vector Table base location at 0x08000000 */ + NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); +} + + +void RCC_Configuration(void) { + ErrorStatus HSEStartUpStatus; + /* RCC system reset(for debug purpose) */ + RCC_DeInit(); + + /* Enable HSE */ + RCC_HSEConfig(RCC_HSE_ON); + + /* Wait till HSE is ready */ + HSEStartUpStatus = RCC_WaitForHSEStartUp(); + + if(HSEStartUpStatus == SUCCESS) { + /* Enable Prefetch Buffer */ + FLASH_PrefetchBufferCmd( (u32)FLASH_PrefetchBuffer_Enable); + + /* Flash 2 wait state */ + FLASH_SetLatency(FLASH_Latency_2); + + /* HCLK = SYSCLK */ + RCC_HCLKConfig(RCC_SYSCLK_Div1); + + /* PCLK2 = HCLK APB2 Periphs, no prescaler 72MHz */ + RCC_PCLK2Config(RCC_HCLK_Div1); + + /* PCLK1 = HCLK/2 APB1 periphs = 36MHZ*/ + RCC_PCLK1Config(RCC_HCLK_Div2); + + /* PLLCLK = 8MHz * 9 = 72 MHz */ + RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); + + /* Enable PLL */ + RCC_PLLCmd(ENABLE); + + /* Wait till PLL is ready */ + while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) + ; + + /* Select PLL as system clock source */ + RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); + + /* Wait till PLL is used as system clock source */ + while(RCC_GetSYSCLKSource() != 0x08) + ; + } +} diff --git a/src/wiring/wiring.h b/src/wiring/wiring.h new file mode 100644 index 0000000..789e697 --- /dev/null +++ b/src/wiring/wiring.h @@ -0,0 +1,56 @@ +#ifndef _WIRING_H_ +#define _WIRING_H_ + +#include <inttypes.h> +#include "timers.h" +#include "io.h" +#include "binary.h" +#include "time.h" + +#ifdef __cplusplus +extern "C"{ +#endif + +#define MAPLE 1 +#define NR_MAPLE_PINS 14 // 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 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_t boolean; +typedef uint8_t 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/src/wiring/wiring_analog.c b/src/wiring/wiring_analog.c new file mode 100644 index 0000000..abcf7c6 --- /dev/null +++ b/src/wiring/wiring_analog.c @@ -0,0 +1,33 @@ +#include "adc.h" +#include "gpio.h" +#include "wiring.h" +#include <stdio.h> + +/* Indexed by pins A[0-15] */ +uint32_t PIN_TO_ADC[NR_ANALOG_PINS] = { + 10, // A0/D14 ADC10 + 11, // A1/D15 ADC11 + 12, // A2/D16 ADC12 + 13, // A3/D17 ADC13 + 14, // A4/D18 ADC14 + 15, // A5/D19 ADC15 + 3, // A6/D0 ADC3 + 2, // A7/D1 ADC2 + 0, // A8/D2 ADC0 + 1, // A9/D3 ADC1 + 4, // A10/D10 ADC4 + 7, // A11/D11 ADC7 + 6, // A12/D12 ADC6 + 5, // A13/D13 ADC5 + 8, // A14/D26 ADC8 + 9, // A15/D11 ADC9 +}; + +/* 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_TO_ADC[pin]); +} diff --git a/src/wiring/wiring_digital.c b/src/wiring/wiring_digital.c new file mode 100644 index 0000000..1ece41a --- /dev/null +++ b/src/wiring/wiring_digital.c @@ -0,0 +1,55 @@ +#include "gpio.h" +#include "wiring.h" +#include "util.h" + +typedef struct PinGPIOMapping { + GPIO_Port *port; + uint32_t pin; +} PinGPIOMapping; + +static const PinGPIOMapping PIN_TO_GPIO[NR_MAPLE_PINS] = { + {_GPIOA_BASE, 3}, // D0/PA3 + {_GPIOA_BASE, 2}, // D1/PA2 + {_GPIOA_BASE, 0}, // D2/PA0 + {_GPIOA_BASE, 1}, // D3/PA1 + {_GPIOB_BASE, 5}, // D4/PB5 + {_GPIOB_BASE, 6}, // D5/PB6 + {_GPIOA_BASE, 8}, // D6/PA8 + {_GPIOA_BASE, 9}, // D7/PA9 + {_GPIOA_BASE, 10}, // D8/PA10 + {_GPIOB_BASE, 7}, // D9/PB7 + {_GPIOA_BASE, 4}, // D10/PA4 + {_GPIOA_BASE, 7}, // D11/PA7 + {_GPIOA_BASE, 6}, // D12/PA6 + {_GPIOA_BASE, 5}, // D13/PA5 +#if 0 + {_GPIOC_BASE, 0}, // D14/A0/PC0 + {_GPIOC_BASE, 1}, // D15/A1/PC1 + {_GPIOC_BASE, 2}, // D16/A2/PC2 + {_GPIOC_BASE, 3}, // D17/A3/PC3 + {_GPIOC_BASE, 4}, // D18/A4/PC4 + {_GPIOC_BASE, 5}, // D19/A5/PC5 +#endif +}; + +void pinMode(uint8_t pin, uint8_t mode) { + if (pin >= NR_MAPLE_PINS) + return; + + gpio_set_mode(PIN_TO_GPIO[pin].port, PIN_TO_GPIO[pin].pin, mode); +} + + +uint32_t digitalRead(uint8_t pin) { + if (pin >= NR_MAPLE_PINS) + return 0; + + return (PIN_TO_GPIO[pin].port->IDR & BIT(PIN_TO_GPIO[pin].pin)) ? 1 : 0; +} + +void digitalWrite(uint8_t pin, uint8_t val) { + if (pin >= NR_MAPLE_PINS) + return; + + gpio_write_bit(PIN_TO_GPIO[pin].port, PIN_TO_GPIO[pin].pin, val); +} diff --git a/src/wiring/wiring_shift.c b/src/wiring/wiring_shift.c new file mode 100644 index 0000000..c138b56 --- /dev/null +++ b/src/wiring/wiring_shift.c @@ -0,0 +1,42 @@ +/* copied straight from arduino/wiring_shift.c */ + +/* + 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); + } +} |