|
| 1 | +/****************************************************************************** |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2010 Perry Hung. |
| 5 | + * Copyright (c) 2011, 2012 LeafLabs, LLC. |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person |
| 8 | + * obtaining a copy of this software and associated documentation |
| 9 | + * files (the "Software"), to deal in the Software without |
| 10 | + * restriction, including without limitation the rights to use, copy, |
| 11 | + * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 12 | + * of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be |
| 16 | + * included in all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 22 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 23 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | + * SOFTWARE. |
| 26 | + *****************************************************************************/ |
| 27 | + |
| 28 | +/** |
| 29 | + * @file wirish/HardwareSerial.cpp |
| 30 | + * @brief Wirish serial port implementation. |
| 31 | + */ |
| 32 | + |
| 33 | +#include "HardwareSerial.h" |
| 34 | + |
| 35 | +#include <libmaple/libmaple.h> |
| 36 | +#include <libmaple/gpio.h> |
| 37 | +#include <libmaple/timer.h> |
| 38 | +#include <libmaple/usart.h> |
| 39 | + |
| 40 | +#if 0 |
| 41 | + |
| 42 | + #define DEFINE_HWSERIAL(name, n) \ |
| 43 | + HardwareSerial name(USART##n, \ |
| 44 | + BOARD_USART##n##_TX_PIN, \ |
| 45 | + BOARD_USART##n##_RX_PIN) |
| 46 | + |
| 47 | + #define DEFINE_HWSERIAL_UART(name, n) \ |
| 48 | + HardwareSerial name(UART##n, \ |
| 49 | + BOARD_USART##n##_TX_PIN, \ |
| 50 | + BOARD_USART##n##_RX_PIN) |
| 51 | + |
| 52 | + #ifdef SERIAL_USB |
| 53 | + #if BOARD_HAVE_USART1 |
| 54 | + DEFINE_HWSERIAL(Serial1, 1); |
| 55 | + #endif |
| 56 | + #if BOARD_HAVE_USART2 |
| 57 | + DEFINE_HWSERIAL(Serial2, 2); |
| 58 | + #endif |
| 59 | + #if BOARD_HAVE_USART3 |
| 60 | + DEFINE_HWSERIAL(Serial3, 3); |
| 61 | + #endif |
| 62 | + #if BOARD_HAVE_UART4 |
| 63 | + DEFINE_HWSERIAL_UART(Serial4, 4); |
| 64 | + #endif |
| 65 | + #if BOARD_HAVE_UART5 |
| 66 | + DEFINE_HWSERIAL_UART(Serial5, 5); |
| 67 | + #endif |
| 68 | + #if BOARD_HAVE_USART6 |
| 69 | + DEFINE_HWSERIAL_UART(Serial6, 6); |
| 70 | + #endif |
| 71 | + #else |
| 72 | + #if BOARD_HAVE_USART1 |
| 73 | + DEFINE_HWSERIAL(Serial, 1); |
| 74 | + #endif |
| 75 | + #if BOARD_HAVE_USART2 |
| 76 | + DEFINE_HWSERIAL(Serial1, 2); |
| 77 | + #endif |
| 78 | + #if BOARD_HAVE_USART3 |
| 79 | + DEFINE_HWSERIAL(Serial2, 3); |
| 80 | + #endif |
| 81 | + #if BOARD_HAVE_UART4 |
| 82 | + DEFINE_HWSERIAL_UART(Serial3, 4); |
| 83 | + #endif |
| 84 | + #if BOARD_HAVE_UART5 |
| 85 | + DEFINE_HWSERIAL_UART(Serial4, 5); |
| 86 | + #endif |
| 87 | + #if BOARD_HAVE_USART6 |
| 88 | + DEFINE_HWSERIAL_UART(Serial5, 6); |
| 89 | + #endif |
| 90 | + #endif |
| 91 | + |
| 92 | +#endif |
| 93 | +HardwareSerial::HardwareSerial(usart_dev *usart_device, |
| 94 | + uint8 tx_pin, |
| 95 | + uint8 rx_pin) { |
| 96 | + this->usart_device = usart_device; |
| 97 | + this->tx_pin = tx_pin; |
| 98 | + this->rx_pin = rx_pin; |
| 99 | +} |
| 100 | + |
| 101 | +/* |
| 102 | + * Set up/tear down |
| 103 | + */ |
| 104 | + |
| 105 | +#if STM32_MCU_SERIES == STM32_SERIES_F1 |
| 106 | +/* F1 MCUs have no GPIO_AFR[HL], so turn off PWM if there's a conflict |
| 107 | + * on this GPIO bit. */ |
| 108 | +static void disable_timer_if_necessary(timer_dev *dev, uint8 ch) { |
| 109 | + if (dev != NULL) { |
| 110 | + timer_set_mode(dev, ch, TIMER_DISABLED); |
| 111 | + } |
| 112 | +} |
| 113 | +#elif (STM32_MCU_SERIES == STM32_SERIES_F2) || \ |
| 114 | + (STM32_MCU_SERIES == STM32_SERIES_F4) |
| 115 | +#define disable_timer_if_necessary(dev, ch) ((void)0) |
| 116 | +#else |
| 117 | +#warning "Unsupported STM32 series; timer conflicts are possible" |
| 118 | +#endif |
| 119 | + |
| 120 | +void HardwareSerial::begin(uint32 baud) |
| 121 | +{ |
| 122 | + begin(baud,SERIAL_8N1); |
| 123 | +} |
| 124 | +/* |
| 125 | + * Roger Clark. |
| 126 | + * Note. The config parameter is not currently used. This is a work in progress. |
| 127 | + * Code needs to be written to set the config of the hardware serial control register in question. |
| 128 | + * |
| 129 | +*/ |
| 130 | + |
| 131 | +void HardwareSerial::begin(uint32 baud, uint8_t config) |
| 132 | +{ |
| 133 | + // ASSERT(baud <= this->usart_device->max_baud);// Roger Clark. Assert doesn't do anything useful, we may as well save the space in flash and ram etc |
| 134 | + |
| 135 | + if (baud > this->usart_device->max_baud) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + const stm32_pin_info *txi = &PIN_MAP[this->tx_pin]; |
| 140 | + const stm32_pin_info *rxi = &PIN_MAP[this->rx_pin]; |
| 141 | + |
| 142 | + disable_timer_if_necessary(txi->timer_device, txi->timer_channel); |
| 143 | + |
| 144 | + usart_config_gpios_async(this->usart_device, |
| 145 | + rxi->gpio_device, rxi->gpio_bit, |
| 146 | + txi->gpio_device, txi->gpio_bit, |
| 147 | + config); |
| 148 | + usart_init(this->usart_device); |
| 149 | + usart_set_baud_rate(this->usart_device, USART_USE_PCLK, baud); |
| 150 | + usart_enable(this->usart_device); |
| 151 | +} |
| 152 | + |
| 153 | +void HardwareSerial::end(void) { |
| 154 | + usart_disable(this->usart_device); |
| 155 | +} |
| 156 | + |
| 157 | +/* |
| 158 | + * I/O |
| 159 | + */ |
| 160 | + |
| 161 | +int HardwareSerial::read(void) { |
| 162 | + // Block until a byte becomes available, to save user confusion. |
| 163 | + while (!this->available()) |
| 164 | + ; |
| 165 | + return usart_getc(this->usart_device); |
| 166 | +} |
| 167 | + |
| 168 | +int HardwareSerial::available(void) { |
| 169 | + return usart_data_available(this->usart_device); |
| 170 | +} |
| 171 | + |
| 172 | +/* Roger Clark. Added function missing from LibMaple code */ |
| 173 | + |
| 174 | +int HardwareSerial::peek(void) |
| 175 | +{ |
| 176 | + return usart_peek(this->usart_device); |
| 177 | +} |
| 178 | + |
| 179 | +int HardwareSerial::availableForWrite(void) |
| 180 | +{ |
| 181 | +/* Roger Clark. |
| 182 | + * Currently there isn't an output ring buffer, chars are sent straight to the hardware. |
| 183 | + * so just return 1, meaning that 1 char can be written |
| 184 | + * This will be slower than a ring buffer implementation, but it should at least work ! |
| 185 | + */ |
| 186 | + return 1; |
| 187 | +} |
| 188 | + |
| 189 | +size_t HardwareSerial::write(unsigned char ch) { |
| 190 | + |
| 191 | + usart_putc(this->usart_device, ch); |
| 192 | + return 1; |
| 193 | +} |
| 194 | + |
| 195 | +void HardwareSerial::flush(void) { |
| 196 | + usart_reset_rx(this->usart_device); |
| 197 | +} |
0 commit comments