Lancaster University's (short term!) clone of mbed-src for micro:bit. This is a copy of the github branch https://github.com/lancaster-university/mbed-classic

Fork of mbed-src by mbed official

Committer:
bogdanm
Date:
Wed Aug 07 16:43:59 2013 +0300
Revision:
15:4892fe388435
Parent:
13:0645d8841f51
Child:
36:ab3ee77451e7
Added LPC4088 target and interrupt chaining code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 13:0645d8841f51 1 /* mbed Microcontroller Library
bogdanm 13:0645d8841f51 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 13:0645d8841f51 3 *
bogdanm 13:0645d8841f51 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 13:0645d8841f51 5 * you may not use this file except in compliance with the License.
bogdanm 13:0645d8841f51 6 * You may obtain a copy of the License at
bogdanm 13:0645d8841f51 7 *
bogdanm 13:0645d8841f51 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 13:0645d8841f51 9 *
bogdanm 13:0645d8841f51 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 13:0645d8841f51 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 13:0645d8841f51 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 13:0645d8841f51 13 * See the License for the specific language governing permissions and
bogdanm 13:0645d8841f51 14 * limitations under the License.
bogdanm 13:0645d8841f51 15 */
bogdanm 13:0645d8841f51 16 #include "Serial.h"
bogdanm 13:0645d8841f51 17 #include "wait_api.h"
bogdanm 13:0645d8841f51 18
bogdanm 13:0645d8841f51 19 #if DEVICE_SERIAL
bogdanm 13:0645d8841f51 20
bogdanm 13:0645d8841f51 21 namespace mbed {
bogdanm 13:0645d8841f51 22
bogdanm 13:0645d8841f51 23 Serial::Serial(PinName tx, PinName rx, const char *name) : Stream(name) {
bogdanm 13:0645d8841f51 24 serial_init(&_serial, tx, rx);
bogdanm 13:0645d8841f51 25 _baud = 9600;
bogdanm 13:0645d8841f51 26 serial_irq_handler(&_serial, Serial::_irq_handler, (uint32_t)this);
bogdanm 13:0645d8841f51 27 }
bogdanm 13:0645d8841f51 28
bogdanm 13:0645d8841f51 29 void Serial::baud(int baudrate) {
bogdanm 13:0645d8841f51 30 serial_baud(&_serial, baudrate);
bogdanm 13:0645d8841f51 31 _baud = baudrate;
bogdanm 13:0645d8841f51 32 }
bogdanm 13:0645d8841f51 33
bogdanm 13:0645d8841f51 34 void Serial::format(int bits, Parity parity, int stop_bits) {
bogdanm 13:0645d8841f51 35 serial_format(&_serial, bits, (SerialParity)parity, stop_bits);
bogdanm 13:0645d8841f51 36 }
bogdanm 13:0645d8841f51 37
bogdanm 13:0645d8841f51 38 int Serial::readable() {
bogdanm 13:0645d8841f51 39 return serial_readable(&_serial);
bogdanm 13:0645d8841f51 40 }
bogdanm 13:0645d8841f51 41
bogdanm 13:0645d8841f51 42
bogdanm 13:0645d8841f51 43 int Serial::writeable() {
bogdanm 13:0645d8841f51 44 return serial_writable(&_serial);
bogdanm 13:0645d8841f51 45 }
bogdanm 13:0645d8841f51 46
bogdanm 15:4892fe388435 47 pFunctionPointer_t Serial::attach(void (*fptr)(void), IrqType type) {
bogdanm 15:4892fe388435 48 pFunctionPointer_t pf = NULL;
bogdanm 15:4892fe388435 49 _irq[type].clear();
bogdanm 13:0645d8841f51 50 if (fptr) {
bogdanm 15:4892fe388435 51 pf = _irq[type].add(fptr);
bogdanm 13:0645d8841f51 52 serial_irq_set(&_serial, (SerialIrq)type, 1);
bogdanm 13:0645d8841f51 53 } else {
bogdanm 13:0645d8841f51 54 serial_irq_set(&_serial, (SerialIrq)type, 0);
bogdanm 13:0645d8841f51 55 }
bogdanm 15:4892fe388435 56 return pf;
bogdanm 13:0645d8841f51 57 }
bogdanm 13:0645d8841f51 58
bogdanm 15:4892fe388435 59 pFunctionPointer_t Serial::add_handler_helper(void (*fptr)(void), IrqType type, bool front) {
bogdanm 15:4892fe388435 60 if (NULL == fptr)
bogdanm 15:4892fe388435 61 return NULL;
bogdanm 15:4892fe388435 62 pFunctionPointer_t pf = front ? _irq[type].add_front(fptr) : _irq[type].add(fptr);
bogdanm 15:4892fe388435 63 serial_irq_set(&_serial, (SerialIrq)type, 1);
bogdanm 15:4892fe388435 64 return pf;
bogdanm 15:4892fe388435 65 }
bogdanm 15:4892fe388435 66
bogdanm 15:4892fe388435 67 bool Serial::remove_handler(pFunctionPointer_t pf, IrqType type) {
bogdanm 15:4892fe388435 68 bool res = _irq[type].remove(pf);
bogdanm 15:4892fe388435 69 if (res && _irq[type].size() == 0)
bogdanm 15:4892fe388435 70 serial_irq_set(&_serial, (SerialIrq)type, 0);
bogdanm 15:4892fe388435 71 return res;
bogdanm 15:4892fe388435 72 }
bogdanm 13:0645d8841f51 73
bogdanm 13:0645d8841f51 74 void Serial::_irq_handler(uint32_t id, SerialIrq irq_type) {
bogdanm 13:0645d8841f51 75 Serial *handler = (Serial*)id;
bogdanm 13:0645d8841f51 76 handler->_irq[irq_type].call();
bogdanm 13:0645d8841f51 77 }
bogdanm 13:0645d8841f51 78
bogdanm 13:0645d8841f51 79 int Serial::_getc() {
bogdanm 13:0645d8841f51 80 return serial_getc(&_serial);
bogdanm 13:0645d8841f51 81 }
bogdanm 13:0645d8841f51 82
bogdanm 13:0645d8841f51 83 int Serial::_putc(int c) {
bogdanm 13:0645d8841f51 84 serial_putc(&_serial, c);
bogdanm 13:0645d8841f51 85 return c;
bogdanm 13:0645d8841f51 86 }
bogdanm 13:0645d8841f51 87
bogdanm 13:0645d8841f51 88 void Serial::send_break() {
bogdanm 13:0645d8841f51 89 // Wait for 1.5 frames before clearing the break condition
bogdanm 13:0645d8841f51 90 // This will have different effects on our platforms, but should
bogdanm 13:0645d8841f51 91 // ensure that we keep the break active for at least one frame.
bogdanm 13:0645d8841f51 92 // We consider a full frame (1 start bit + 8 data bits bits +
bogdanm 13:0645d8841f51 93 // 1 parity bit + 2 stop bits = 12 bits) for computation.
bogdanm 13:0645d8841f51 94 // One bit time (in us) = 1000000/_baud
bogdanm 13:0645d8841f51 95 // Twelve bits: 12000000/baud delay
bogdanm 13:0645d8841f51 96 // 1.5 frames: 18000000/baud delay
bogdanm 13:0645d8841f51 97 serial_break_set(&_serial);
bogdanm 13:0645d8841f51 98 wait_us(18000000/_baud);
bogdanm 13:0645d8841f51 99 serial_break_clear(&_serial);
bogdanm 13:0645d8841f51 100 }
bogdanm 13:0645d8841f51 101
bogdanm 13:0645d8841f51 102 } // namespace mbed
bogdanm 13:0645d8841f51 103
bogdanm 13:0645d8841f51 104 #endif