Mbed SDK for XRange SX1272 LoRa module
Dependents: XRangePingPong XRange-LoRaWAN-lmic-app lora-transceiver
SX1272 LoRa RF module
https://www.netblocks.eu/xrange-sx1272-lora-datasheet/
common/SerialBase.cpp@339:ac6f3fd999f3, 2016-01-07 (annotated)
- Committer:
- netblocks
- Date:
- Thu Jan 07 13:01:25 2016 +0000
- Revision:
- 339:ac6f3fd999f3
- Parent:
- 336:1e18a06a987b
HSE_VALUE set for XTAL 16Mhz
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudmuck | 336:1e18a06a987b | 1 | /* mbed Microcontroller Library |
dudmuck | 336:1e18a06a987b | 2 | * Copyright (c) 2006-2013 ARM Limited |
dudmuck | 336:1e18a06a987b | 3 | * |
dudmuck | 336:1e18a06a987b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dudmuck | 336:1e18a06a987b | 5 | * you may not use this file except in compliance with the License. |
dudmuck | 336:1e18a06a987b | 6 | * You may obtain a copy of the License at |
dudmuck | 336:1e18a06a987b | 7 | * |
dudmuck | 336:1e18a06a987b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
dudmuck | 336:1e18a06a987b | 9 | * |
dudmuck | 336:1e18a06a987b | 10 | * Unless required by applicable law or agreed to in writing, software |
dudmuck | 336:1e18a06a987b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
dudmuck | 336:1e18a06a987b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dudmuck | 336:1e18a06a987b | 13 | * See the License for the specific language governing permissions and |
dudmuck | 336:1e18a06a987b | 14 | * limitations under the License. |
dudmuck | 336:1e18a06a987b | 15 | */ |
dudmuck | 336:1e18a06a987b | 16 | #include "SerialBase.h" |
dudmuck | 336:1e18a06a987b | 17 | #include "wait_api.h" |
dudmuck | 336:1e18a06a987b | 18 | |
dudmuck | 336:1e18a06a987b | 19 | #if DEVICE_SERIAL |
dudmuck | 336:1e18a06a987b | 20 | |
dudmuck | 336:1e18a06a987b | 21 | namespace mbed { |
dudmuck | 336:1e18a06a987b | 22 | |
dudmuck | 336:1e18a06a987b | 23 | SerialBase::SerialBase(PinName tx, PinName rx) : _serial(), _baud(9600) { |
dudmuck | 336:1e18a06a987b | 24 | serial_init(&_serial, tx, rx); |
dudmuck | 336:1e18a06a987b | 25 | serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this); |
dudmuck | 336:1e18a06a987b | 26 | } |
dudmuck | 336:1e18a06a987b | 27 | |
dudmuck | 336:1e18a06a987b | 28 | void SerialBase::baud(int baudrate) { |
dudmuck | 336:1e18a06a987b | 29 | serial_baud(&_serial, baudrate); |
dudmuck | 336:1e18a06a987b | 30 | _baud = baudrate; |
dudmuck | 336:1e18a06a987b | 31 | } |
dudmuck | 336:1e18a06a987b | 32 | |
dudmuck | 336:1e18a06a987b | 33 | void SerialBase::format(int bits, Parity parity, int stop_bits) { |
dudmuck | 336:1e18a06a987b | 34 | serial_format(&_serial, bits, (SerialParity)parity, stop_bits); |
dudmuck | 336:1e18a06a987b | 35 | } |
dudmuck | 336:1e18a06a987b | 36 | |
dudmuck | 336:1e18a06a987b | 37 | int SerialBase::readable() { |
dudmuck | 336:1e18a06a987b | 38 | return serial_readable(&_serial); |
dudmuck | 336:1e18a06a987b | 39 | } |
dudmuck | 336:1e18a06a987b | 40 | |
dudmuck | 336:1e18a06a987b | 41 | |
dudmuck | 336:1e18a06a987b | 42 | int SerialBase::writeable() { |
dudmuck | 336:1e18a06a987b | 43 | return serial_writable(&_serial); |
dudmuck | 336:1e18a06a987b | 44 | } |
dudmuck | 336:1e18a06a987b | 45 | |
dudmuck | 336:1e18a06a987b | 46 | void SerialBase::attach(void (*fptr)(void), IrqType type) { |
dudmuck | 336:1e18a06a987b | 47 | if (fptr) { |
dudmuck | 336:1e18a06a987b | 48 | _irq[type].attach(fptr); |
dudmuck | 336:1e18a06a987b | 49 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
dudmuck | 336:1e18a06a987b | 50 | } else { |
dudmuck | 336:1e18a06a987b | 51 | serial_irq_set(&_serial, (SerialIrq)type, 0); |
dudmuck | 336:1e18a06a987b | 52 | } |
dudmuck | 336:1e18a06a987b | 53 | } |
dudmuck | 336:1e18a06a987b | 54 | |
dudmuck | 336:1e18a06a987b | 55 | void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) { |
dudmuck | 336:1e18a06a987b | 56 | SerialBase *handler = (SerialBase*)id; |
dudmuck | 336:1e18a06a987b | 57 | handler->_irq[irq_type].call(); |
dudmuck | 336:1e18a06a987b | 58 | } |
dudmuck | 336:1e18a06a987b | 59 | |
dudmuck | 336:1e18a06a987b | 60 | int SerialBase::_base_getc() { |
dudmuck | 336:1e18a06a987b | 61 | return serial_getc(&_serial); |
dudmuck | 336:1e18a06a987b | 62 | } |
dudmuck | 336:1e18a06a987b | 63 | |
dudmuck | 336:1e18a06a987b | 64 | int SerialBase::_base_putc(int c) { |
dudmuck | 336:1e18a06a987b | 65 | serial_putc(&_serial, c); |
dudmuck | 336:1e18a06a987b | 66 | return c; |
dudmuck | 336:1e18a06a987b | 67 | } |
dudmuck | 336:1e18a06a987b | 68 | |
dudmuck | 336:1e18a06a987b | 69 | void SerialBase::send_break() { |
dudmuck | 336:1e18a06a987b | 70 | // Wait for 1.5 frames before clearing the break condition |
dudmuck | 336:1e18a06a987b | 71 | // This will have different effects on our platforms, but should |
dudmuck | 336:1e18a06a987b | 72 | // ensure that we keep the break active for at least one frame. |
dudmuck | 336:1e18a06a987b | 73 | // We consider a full frame (1 start bit + 8 data bits bits + |
dudmuck | 336:1e18a06a987b | 74 | // 1 parity bit + 2 stop bits = 12 bits) for computation. |
dudmuck | 336:1e18a06a987b | 75 | // One bit time (in us) = 1000000/_baud |
dudmuck | 336:1e18a06a987b | 76 | // Twelve bits: 12000000/baud delay |
dudmuck | 336:1e18a06a987b | 77 | // 1.5 frames: 18000000/baud delay |
dudmuck | 336:1e18a06a987b | 78 | serial_break_set(&_serial); |
dudmuck | 336:1e18a06a987b | 79 | wait_us(18000000/_baud); |
dudmuck | 336:1e18a06a987b | 80 | serial_break_clear(&_serial); |
dudmuck | 336:1e18a06a987b | 81 | } |
dudmuck | 336:1e18a06a987b | 82 | |
dudmuck | 336:1e18a06a987b | 83 | #if DEVICE_SERIAL_FC |
dudmuck | 336:1e18a06a987b | 84 | void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) { |
dudmuck | 336:1e18a06a987b | 85 | FlowControl flow_type = (FlowControl)type; |
dudmuck | 336:1e18a06a987b | 86 | switch(type) { |
dudmuck | 336:1e18a06a987b | 87 | case RTS: |
dudmuck | 336:1e18a06a987b | 88 | serial_set_flow_control(&_serial, flow_type, flow1, NC); |
dudmuck | 336:1e18a06a987b | 89 | break; |
dudmuck | 336:1e18a06a987b | 90 | |
dudmuck | 336:1e18a06a987b | 91 | case CTS: |
dudmuck | 336:1e18a06a987b | 92 | serial_set_flow_control(&_serial, flow_type, NC, flow1); |
dudmuck | 336:1e18a06a987b | 93 | break; |
dudmuck | 336:1e18a06a987b | 94 | |
dudmuck | 336:1e18a06a987b | 95 | case RTSCTS: |
dudmuck | 336:1e18a06a987b | 96 | case Disabled: |
dudmuck | 336:1e18a06a987b | 97 | serial_set_flow_control(&_serial, flow_type, flow1, flow2); |
dudmuck | 336:1e18a06a987b | 98 | break; |
dudmuck | 336:1e18a06a987b | 99 | |
dudmuck | 336:1e18a06a987b | 100 | default: |
dudmuck | 336:1e18a06a987b | 101 | break; |
dudmuck | 336:1e18a06a987b | 102 | } |
dudmuck | 336:1e18a06a987b | 103 | } |
dudmuck | 336:1e18a06a987b | 104 | #endif |
dudmuck | 336:1e18a06a987b | 105 | |
dudmuck | 336:1e18a06a987b | 106 | } // namespace mbed |
dudmuck | 336:1e18a06a987b | 107 | |
dudmuck | 336:1e18a06a987b | 108 | #endif |