aboutsummaryrefslogtreecommitdiffstats
path: root/src/wiring
diff options
context:
space:
mode:
Diffstat (limited to 'src/wiring')
-rw-r--r--src/wiring/Print.cpp26
-rw-r--r--src/wiring/comm/HardwareSerial.cpp92
-rw-r--r--src/wiring/comm/HardwareSerial.h48
-rw-r--r--src/wiring/comm/Serial.cpp67
-rw-r--r--src/wiring/comm/Serial.h20
-rw-r--r--src/wiring/ext_interrupts.c49
-rw-r--r--src/wiring/ext_interrupts.h39
-rw-r--r--src/wiring/io.h56
-rw-r--r--src/wiring/math.h4
-rw-r--r--src/wiring/pwm.c25
-rw-r--r--src/wiring/pwm.h27
-rw-r--r--src/wiring/time.c26
-rw-r--r--src/wiring/time.h27
-rw-r--r--src/wiring/wiring.c30
-rw-r--r--src/wiring/wiring.h12
-rw-r--r--src/wiring/wiring_analog.c30
-rw-r--r--src/wiring/wiring_digital.c112
-rw-r--r--src/wiring/wiring_shift.c2
18 files changed, 533 insertions, 159 deletions
diff --git a/src/wiring/Print.cpp b/src/wiring/Print.cpp
index 20f5e40..c7e0cc6 100644
--- a/src/wiring/Print.cpp
+++ b/src/wiring/Print.cpp
@@ -98,14 +98,14 @@ void Print::print(double n)
void Print::println(void)
{
- print('\r');
- print('\n');
+// print('\r');
+ print('\n');
}
void Print::println(char c)
{
print(c);
- println();
+ println();
}
void Print::println(const char c[])
@@ -135,13 +135,13 @@ void Print::println(unsigned int n)
void Print::println(long n)
{
print(n);
- println();
+ println();
}
void Print::println(unsigned long n)
{
print(n);
- println();
+ println();
}
void Print::println(long n, int base)
@@ -160,13 +160,13 @@ void Print::println(double n)
void Print::printNumber(unsigned long n, uint8_t base)
{
- unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
+ 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;
@@ -179,8 +179,8 @@ void Print::printNumber(unsigned long n, uint8_t base)
'A' + buf[i - 1] - 10));
}
-void Print::printFloat(double number, uint8_t digits)
-{
+void Print::printFloat(double number, uint8_t digits)
+{
// Handle negative numbers
if (number < 0.0)
{
@@ -192,7 +192,7 @@ void Print::printFloat(double number, uint8_t digits)
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
@@ -202,7 +202,7 @@ void Print::printFloat(double number, uint8_t digits)
// Print the decimal point, but only if there are digits beyond
if (digits > 0)
- print(".");
+ print(".");
// Extract digits from the remainder one at a time
while (digits-- > 0)
@@ -210,6 +210,6 @@ void Print::printFloat(double number, uint8_t digits)
remainder *= 10.0;
int toPrint = int(remainder);
print(toPrint);
- remainder -= toPrint;
- }
+ remainder -= toPrint;
+ }
}
diff --git a/src/wiring/comm/HardwareSerial.cpp b/src/wiring/comm/HardwareSerial.cpp
new file mode 100644
index 0000000..84eb4c7
--- /dev/null
+++ b/src/wiring/comm/HardwareSerial.cpp
@@ -0,0 +1,92 @@
+/* *****************************************************************************
+ * 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"
+
+#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);
+ 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);
+ 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/src/wiring/comm/HardwareSerial.h b/src/wiring/comm/HardwareSerial.h
new file mode 100644
index 0000000..a3913f9
--- /dev/null
+++ b/src/wiring/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/src/wiring/comm/Serial.cpp b/src/wiring/comm/Serial.cpp
deleted file mode 100644
index 3c43436..0000000
--- a/src/wiring/comm/Serial.cpp
+++ /dev/null
@@ -1,67 +0,0 @@
-//#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
deleted file mode 100644
index 0e7a126..0000000
--- a/src/wiring/comm/Serial.h
+++ /dev/null
@@ -1,20 +0,0 @@
-#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
index 0a35472..20ec11f 100644
--- a/src/wiring/ext_interrupts.c
+++ b/src/wiring/ext_interrupts.c
@@ -1,3 +1,28 @@
+/* *****************************************************************************
+ * 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"
@@ -25,7 +50,6 @@ static ExtiInfo PIN_TO_EXTI_CHANNEL[NR_MAPLE_PINS] = {
};
-
/**
* @brief Attach an interrupt handler to be triggered on a given
* transition on the pin. Runs in interrupt context
@@ -36,23 +60,38 @@ static ExtiInfo PIN_TO_EXTI_CHANNEL[NR_MAPLE_PINS] = {
*
* @sideeffect Registers a handler
*/
-int attachInterrupt(uint8_t pin, void (*handler)(void), uint8_t mode) {
+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;
}
- if (!(mode < NR_EXTI_MODES)) {
- return EXT_INTERRUPT_INVALID_MODE;
+ 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,
+ exti_attach_interrupt(PIN_TO_EXTI_CHANNEL[pin].channel,
PIN_TO_EXTI_CHANNEL[pin].port,
handler, mode);
+
+ return 0;
}
int detachInterrupt(uint8_t pin) {
diff --git a/src/wiring/ext_interrupts.h b/src/wiring/ext_interrupts.h
index d6fb23f..d0e6365 100644
--- a/src/wiring/ext_interrupts.h
+++ b/src/wiring/ext_interrupts.h
@@ -1,11 +1,36 @@
+/* *****************************************************************************
+ * 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_
-#include "exti.h"
-
-#define RISING EXTI_RISING
-#define FALLING EXTI_FALLING
-#define CHANGE EXTI_RISING_FALLING
+typedef enum ExtInterruptTriggerMode {
+ RISING,
+ FALLING,
+ CHANGE
+} ExtInterruptTriggerMode;
enum ExtInterruptError {
@@ -19,8 +44,8 @@ enum ExtInterruptError {
extern "C"{
#endif
-int attachInterrupt(uint8_t, void ((*)(void)), uint8_t);
-int detachInterrupt(uint8_t);
+int attachInterrupt(uint8_t pin, voidFuncPtr, ExtInterruptTriggerMode mode);
+int detachInterrupt(uint8_t pin);
#ifdef __cplusplus
}
diff --git a/src/wiring/io.h b/src/wiring/io.h
index 387326a..9c226c5 100644
--- a/src/wiring/io.h
+++ b/src/wiring/io.h
@@ -1,3 +1,28 @@
+/* *****************************************************************************
+ * 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
@@ -67,6 +92,31 @@ extern "C"{
#define A14 D26
#define A15 D11
+typedef enum WiringPinMode {
+ OUTPUT,
+ INPUT,
+ INPUT_PULLUP,
+ INPUT_PULLDOWN,
+ INPUT_FLOATING,
+ PWM
+} WiringPinMode;
+
+#if 0
+typedef enum PinMode {
+ INPUT_FLOATING,
+ INPUT_ANALOG,
+ INPUT_DIGITAL,
+ INPUT_PULLDOWN,
+ INPUT_PULLUP,
+ INPUT,
+ OUTPUT,
+ PWM,
+ SERIAL,
+ SPI,
+ I2C,
+} PinMode;
+#endif
+
/* Set pin to mode
* pinMode(pin, mode):
* pin -> {0-38, D0-D39, A0-16}
@@ -81,7 +131,7 @@ extern "C"{
*/
void pinMode(uint8_t, uint8_t);
-/*
+/*
* Writes VALUE to digital pin[0-38]
* digitalWrite(pin, value):
* pin -> {0-38, D0-D39, A0-16}
@@ -91,13 +141,13 @@ 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)
+ * 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
+ * to INPUT_ANALOG
* analogRead(pin)
* pin -> {A0-A16}
* */
diff --git a/src/wiring/math.h b/src/wiring/math.h
index ee269e9..8f30396 100644
--- a/src/wiring/math.h
+++ b/src/wiring/math.h
@@ -1,5 +1,5 @@
-#ifndef _MAPLE_MATH_H_
-#define _MAPLE_MATH_H_
+#ifndef _MATH_H_
+#define _MATH_H_
void randomSeed(unsigned int);
long random(long);
diff --git a/src/wiring/pwm.c b/src/wiring/pwm.c
index 0c33c3a..e6e100f 100644
--- a/src/wiring/pwm.c
+++ b/src/wiring/pwm.c
@@ -1,3 +1,28 @@
+/* *****************************************************************************
+ * 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 "gpio.h"
diff --git a/src/wiring/pwm.h b/src/wiring/pwm.h
index 3ff440b..60d805d 100644
--- a/src/wiring/pwm.h
+++ b/src/wiring/pwm.h
@@ -1,3 +1,28 @@
+/* *****************************************************************************
+ * 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
@@ -5,8 +30,6 @@
extern "C"{
#endif
-#define PWM GPIO_MODE_AF_OUTPUT_PP
-
void pwmWrite(uint8_t, uint16_t);
#ifdef __cplusplus
diff --git a/src/wiring/time.c b/src/wiring/time.c
index bf6a28b..1a91b5e 100644
--- a/src/wiring/time.c
+++ b/src/wiring/time.c
@@ -1,3 +1,29 @@
+/* *****************************************************************************
+ * 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"
diff --git a/src/wiring/time.h b/src/wiring/time.h
index 694545b..a97310a 100644
--- a/src/wiring/time.h
+++ b/src/wiring/time.h
@@ -1,8 +1,31 @@
+/* *****************************************************************************
+ * 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
-#include <inttypes.h>
-
#ifdef __cplusplus
extern "C"{
#endif
diff --git a/src/wiring/wiring.c b/src/wiring/wiring.c
index 7fcd5fd..9d77a5b 100644
--- a/src/wiring/wiring.c
+++ b/src/wiring/wiring.c
@@ -1,9 +1,33 @@
-#include <inttypes.h>
+/* *****************************************************************************
+ * 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_rcc.h"
#include "stm32f10x_map.h"
#include "stm32f10x_nvic.h"
-#include "wiring.h"
#include "systick.h"
#include "gpio.h"
@@ -26,7 +50,7 @@ void init(void) {
/* off for debug */
// adc_init();
-// timer_init(1, 1);
+ timer_init(1, 1);
// timer_init(2, 1);
// timer_init(3, 1);
// timer_init(4, 1);
diff --git a/src/wiring/wiring.h b/src/wiring/wiring.h
index 4bf93bc..2a33b75 100644
--- a/src/wiring/wiring.h
+++ b/src/wiring/wiring.h
@@ -1,16 +1,20 @@
#ifndef _WIRING_H_
#define _WIRING_H_
-#include <inttypes.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 14 // temporary
@@ -38,12 +42,10 @@ extern "C"{
#define bit(b) (1UL << (b))
-typedef uint8_t boolean;
-typedef uint8_t byte;
+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);
diff --git a/src/wiring/wiring_analog.c b/src/wiring/wiring_analog.c
index abcf7c6..0426f85 100644
--- a/src/wiring/wiring_analog.c
+++ b/src/wiring/wiring_analog.c
@@ -1,7 +1,31 @@
-#include "adc.h"
-#include "gpio.h"
+/* *****************************************************************************
+ * 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 <stdio.h>
+#include "adc.h"
/* Indexed by pins A[0-15] */
uint32_t PIN_TO_ADC[NR_ANALOG_PINS] = {
diff --git a/src/wiring/wiring_digital.c b/src/wiring/wiring_digital.c
index 1ece41a..456f5da 100644
--- a/src/wiring/wiring_digital.c
+++ b/src/wiring/wiring_digital.c
@@ -1,42 +1,104 @@
-#include "gpio.h"
+/* *****************************************************************************
+ * 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 "util.h"
+#include "io.h"
+#include "gpio.h"
+
+typedef enum AFMode{
+ AF_NONE,
+ AF_PWM,
+ AF_SERIAL,
+ AF_I2C, // unused for now
+ AF_SPI, // unusued for now
+} AFMode;
+
typedef struct PinGPIOMapping {
GPIO_Port *port;
- uint32_t pin;
+ uint32 pin;
} PinGPIOMapping;
+
+/* Reset state is input floating */
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
+ {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
+/* for later */
#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
+ {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) {
+void pinMode(uint8_t pin, WiringPinMode mode) {
+ uint8 outputMode;
+
if (pin >= NR_MAPLE_PINS)
return;
- gpio_set_mode(PIN_TO_GPIO[pin].port, PIN_TO_GPIO[pin].pin, mode);
+ switch(mode) {
+ case OUTPUT:
+ outputMode = GPIO_MODE_OUTPUT_PP;
+ break;
+ case INPUT:
+ case INPUT_FLOATING:
+ outputMode = GPIO_MODE_INPUT_FLOATING;
+ break;
+ 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_TO_GPIO[pin].port, PIN_TO_GPIO[pin].pin, outputMode);
}
diff --git a/src/wiring/wiring_shift.c b/src/wiring/wiring_shift.c
index c138b56..28c6dfc 100644
--- a/src/wiring/wiring_shift.c
+++ b/src/wiring/wiring_shift.c
@@ -1,5 +1,3 @@
-/* copied straight from arduino/wiring_shift.c */
-
/*
wiring_shift.c - shiftOut() function
Part of Arduino - http://www.arduino.cc/