Test code for Grove Node BLE
Dependencies: BLE_API nRF51822
Fork of BLE_LoopbackUART by
SerialBase.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "SerialBase.h" 00017 #include "wait_api.h" 00018 00019 #if DEVICE_SERIAL 00020 00021 namespace mbed { 00022 00023 SerialBase::SerialBase(PinName tx, PinName rx) : _serial(), _baud(9600) { 00024 serial_init(&_serial, tx, rx); 00025 serial_irq_handler(&_serial, SerialBase::_irq_handler, (uint32_t)this); 00026 } 00027 00028 void SerialBase::baud(int baudrate) { 00029 serial_baud(&_serial, baudrate); 00030 _baud = baudrate; 00031 } 00032 00033 void SerialBase::format(int bits, Parity parity, int stop_bits) { 00034 serial_format(&_serial, bits, (SerialParity)parity, stop_bits); 00035 } 00036 00037 int SerialBase::readable() { 00038 return serial_readable(&_serial); 00039 } 00040 00041 00042 int SerialBase::writeable() { 00043 return serial_writable(&_serial); 00044 } 00045 00046 void SerialBase::attach(void (*fptr)(void), IrqType type) { 00047 if (fptr) { 00048 _irq[type].attach(fptr); 00049 serial_irq_set(&_serial, (SerialIrq)type, 1); 00050 } else { 00051 serial_irq_set(&_serial, (SerialIrq)type, 0); 00052 } 00053 } 00054 00055 void SerialBase::_irq_handler(uint32_t id, SerialIrq irq_type) { 00056 SerialBase *handler = (SerialBase*)id; 00057 handler->_irq[irq_type].call(); 00058 } 00059 00060 int SerialBase::_base_getc() { 00061 return serial_getc(&_serial); 00062 } 00063 00064 int SerialBase::_base_putc(int c) { 00065 serial_putc(&_serial, c); 00066 return c; 00067 } 00068 00069 void SerialBase::send_break() { 00070 // Wait for 1.5 frames before clearing the break condition 00071 // This will have different effects on our platforms, but should 00072 // ensure that we keep the break active for at least one frame. 00073 // We consider a full frame (1 start bit + 8 data bits bits + 00074 // 1 parity bit + 2 stop bits = 12 bits) for computation. 00075 // One bit time (in us) = 1000000/_baud 00076 // Twelve bits: 12000000/baud delay 00077 // 1.5 frames: 18000000/baud delay 00078 serial_break_set(&_serial); 00079 wait_us(18000000/_baud); 00080 serial_break_clear(&_serial); 00081 } 00082 00083 #if DEVICE_SERIAL_FC 00084 void SerialBase::set_flow_control(Flow type, PinName flow1, PinName flow2) { 00085 FlowControl flow_type = (FlowControl)type; 00086 switch(type) { 00087 case RTS: 00088 serial_set_flow_control(&_serial, flow_type, flow1, NC); 00089 break; 00090 00091 case CTS: 00092 serial_set_flow_control(&_serial, flow_type, NC, flow1); 00093 break; 00094 00095 case RTSCTS: 00096 case Disabled: 00097 serial_set_flow_control(&_serial, flow_type, flow1, flow2); 00098 break; 00099 00100 default: 00101 break; 00102 } 00103 } 00104 #endif 00105 00106 } // namespace mbed 00107 00108 #endif
Generated on Tue Jul 12 2022 21:43:26 by 1.7.2