Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UnbufferedSerial.cpp Source File

UnbufferedSerial.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "drivers/UnbufferedSerial.h"
00019 
00020 #if DEVICE_SERIAL
00021 
00022 #include "platform/mbed_critical.h"
00023 
00024 namespace mbed {
00025 
00026 UnbufferedSerial::UnbufferedSerial(
00027     PinName tx,
00028     PinName rx,
00029     int baud
00030 ) : SerialBase(tx, rx, baud)
00031 {
00032     // No lock needed in the constructor
00033 }
00034 
00035 UnbufferedSerial::UnbufferedSerial(
00036     const serial_pinmap_t &static_pinmap, int baud
00037 ) : SerialBase(static_pinmap, baud)
00038 {
00039     // No lock needed in the constructor
00040 }
00041 
00042 ssize_t UnbufferedSerial::write(const void *buffer, size_t size)
00043 {
00044     const unsigned char *buf = static_cast<const unsigned char *>(buffer);
00045 
00046     if (size == 0) {
00047         return 0;
00048     }
00049 
00050     bool lock_api = !core_util_in_critical_section();
00051 
00052     if (lock_api) {
00053         lock();
00054     }
00055 
00056     for (size_t i = 0; i < size; i++) {
00057         _base_putc(buf[i]);
00058     }
00059 
00060     if (lock_api) {
00061         unlock();
00062     }
00063 
00064     return size;
00065 }
00066 
00067 ssize_t UnbufferedSerial::read(void *buffer, size_t size)
00068 {
00069     unsigned char *buf = static_cast<unsigned char *>(buffer);
00070 
00071     if (size == 0) {
00072         return 0;
00073     }
00074 
00075     lock();
00076 
00077     buf[0] = _base_getc();
00078 
00079     unlock();
00080 
00081     return 1;
00082 }
00083 
00084 short UnbufferedSerial::poll(short events) const
00085 {
00086     short revents = 0;
00087     if (
00088         (events & POLLIN)
00089         && (const_cast <UnbufferedSerial *>(this))->SerialBase::readable()
00090     ) {
00091         revents |= POLLIN;
00092     }
00093     if (
00094         (events & POLLOUT)
00095         && (const_cast <UnbufferedSerial *>(this))->SerialBase::writeable()
00096     ) {
00097         revents |= POLLOUT;
00098     }
00099     return revents;
00100 }
00101 
00102 #if DEVICE_SERIAL_FC
00103 void UnbufferedSerial::set_flow_control(Flow type, PinName flow1, PinName flow2)
00104 {
00105     lock();
00106     SerialBase::set_flow_control(type, flow1, flow2);
00107     unlock();
00108 }
00109 #endif // DEVICE_SERIAL_FC
00110 
00111 } // namespace mbed
00112 
00113 #endif // #if DEVICE_SERIAL