aboutsummaryrefslogtreecommitdiffstats
path: root/wirish/comm
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2010-08-31 17:39:46 -0400
committerbnewbold <bnewbold@robocracy.org>2010-08-31 17:39:46 -0400
commit02d7b08f0497096f21e41922e0efb54c4ef33bab (patch)
treea7e04293efcba70f37cffcd03c0fcc4c0be7858a /wirish/comm
parentb2dd49c3141d8a21a4e7c7ef51dee7329f847c30 (diff)
parente03d58f4dab4176514924baa3a1ff430bf5819b8 (diff)
downloadlibrambutan-02d7b08f0497096f21e41922e0efb54c4ef33bab.tar.gz
librambutan-02d7b08f0497096f21e41922e0efb54c4ef33bab.zip
Merge maple-native changes into portable
This compiles for both maple and maple_native but is untested.
Diffstat (limited to 'wirish/comm')
-rw-r--r--wirish/comm/HardwareSerial.cpp89
-rw-r--r--wirish/comm/HardwareSerial.h18
2 files changed, 51 insertions, 56 deletions
diff --git a/wirish/comm/HardwareSerial.cpp b/wirish/comm/HardwareSerial.cpp
index 7157e74..6399ad5 100644
--- a/wirish/comm/HardwareSerial.cpp
+++ b/wirish/comm/HardwareSerial.cpp
@@ -34,77 +34,60 @@
#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 usartNum) {
- ASSERT(usartNum == 1 ||
- usartNum == 2 ||
- usartNum == 3);
- this->usartNum = usartNum;
+HardwareSerial Serial1(USART1, 4500000UL, GPIOA_BASE, 9, 10, 1, 2);
+HardwareSerial Serial2(USART2, 2250000UL, GPIOA_BASE, 2, 3, 2, 3);
+HardwareSerial Serial3(USART3, 2250000UL, GPIOB_BASE, 10, 11, 0, 0);
+// TODO: High density device ports
+
+HardwareSerial::HardwareSerial(uint8 usart_num,
+ uint32 max_baud,
+ GPIO_Port *gpio_port,
+ uint8 tx_pin,
+ uint8 rx_pin,
+ uint8 timer_num,
+ uint8 compare_num) {
+ this->usart_num = usart_num;
+ this->max_baud = max_baud;
+ this->gpio_port = gpio_port;
+ this->tx_pin = tx_pin;
+ this->rx_pin = rx_pin;
+ this->timer_num = timer_num;
+ this->compare_num = compare_num;
}
uint8 HardwareSerial::read(void) {
- return usart_getc(usartNum);
+ return usart_getc(usart_num);
}
uint32 HardwareSerial::available(void) {
-
- return usart_data_available(usartNum);
+ return usart_data_available(usart_num);
}
void HardwareSerial::write(unsigned char ch) {
- usart_putc(usartNum, ch);
+ usart_putc(usart_num, ch);
}
void HardwareSerial::begin(uint32 baud) {
- ASSERT(!(baud > USART_MAX_BAUD));
+ if (baud > max_baud) {
+ return;
+ }
+
+ gpio_set_mode(gpio_port, tx_pin, GPIO_MODE_AF_OUTPUT_PP);
+ gpio_set_mode(gpio_port, rx_pin, GPIO_MODE_INPUT_FLOATING);
- /* 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 */
- timer_set_mode(1, 2, TIMER_DISABLED);
- 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 */
- timer_set_mode(2, 3, TIMER_DISABLED);
- 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);
- }
+ if ((usart_num == USART1) ||
+ (usart_num == USART2)) {
+ /* turn off any pwm if there's a conflict on this usart */
+ timer_set_mode(timer_num, compare_num, TIMER_DISABLED);
+ }
- usart_init(usartNum, baud);
+ usart_init(usart_num, baud);
}
void HardwareSerial::end(void) {
- usart_disable(usartNum);
+ usart_disable(usart_num);
}
void HardwareSerial::flush(void) {
- usart_clear_buffer(usartNum);
+ usart_reset_rx(usart_num);
}
-
-HardwareSerial Serial1(1);
-HardwareSerial Serial2(2);
-HardwareSerial Serial3(3);
diff --git a/wirish/comm/HardwareSerial.h b/wirish/comm/HardwareSerial.h
index 5ed81cd..df8d7bf 100644
--- a/wirish/comm/HardwareSerial.h
+++ b/wirish/comm/HardwareSerial.h
@@ -35,9 +35,21 @@
class HardwareSerial : public Print {
private:
- uint8 usartNum;
+ uint8 usart_num;
+ uint32 max_baud;
+ GPIO_Port *gpio_port;
+ uint8 tx_pin;
+ uint8 rx_pin;
+ uint8 timer_num;
+ uint8 compare_num;
public:
- HardwareSerial(uint8);
+ HardwareSerial(uint8 usart_num,
+ uint32 max_baud,
+ GPIO_Port *gpio_port,
+ uint8 tx_pin,
+ uint8 rx_pin,
+ uint8 timer_num,
+ uint8 compare_num);
void begin(uint32);
void end(void);
uint32 available(void);
@@ -46,9 +58,9 @@ class HardwareSerial : public Print {
virtual void write(unsigned char);
using Print::write;
};
-
extern HardwareSerial Serial1;
extern HardwareSerial Serial2;
extern HardwareSerial Serial3;
+// TODO: high density device ports
#endif