diff options
Diffstat (limited to 'src/wiring/comm')
-rw-r--r-- | src/wiring/comm/HardwareSerial.cpp | 92 | ||||
-rw-r--r-- | src/wiring/comm/HardwareSerial.h | 48 | ||||
-rw-r--r-- | src/wiring/comm/Serial.cpp | 67 | ||||
-rw-r--r-- | src/wiring/comm/Serial.h | 20 |
4 files changed, 140 insertions, 87 deletions
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 - |