Modified MODSERIAL library
MODSERIAL.cpp@0:ef4645a79fca, 2012-09-09 (annotated)
- Committer:
- mpanetta
- Date:
- Sun Sep 09 22:28:07 2012 +0000
- Revision:
- 0:ef4645a79fca
[mbed] converted /BeaconAvoid/MODSERIAL
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mpanetta | 0:ef4645a79fca | 1 | /* |
mpanetta | 0:ef4645a79fca | 2 | Copyright (c) 2010 Andy Kirkham |
mpanetta | 0:ef4645a79fca | 3 | |
mpanetta | 0:ef4645a79fca | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
mpanetta | 0:ef4645a79fca | 5 | of this software and associated documentation files (the "Software"), to deal |
mpanetta | 0:ef4645a79fca | 6 | in the Software without restriction, including without limitation the rights |
mpanetta | 0:ef4645a79fca | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mpanetta | 0:ef4645a79fca | 8 | copies of the Software, and to permit persons to whom the Software is |
mpanetta | 0:ef4645a79fca | 9 | furnished to do so, subject to the following conditions: |
mpanetta | 0:ef4645a79fca | 10 | |
mpanetta | 0:ef4645a79fca | 11 | The above copyright notice and this permission notice shall be included in |
mpanetta | 0:ef4645a79fca | 12 | all copies or substantial portions of the Software. |
mpanetta | 0:ef4645a79fca | 13 | |
mpanetta | 0:ef4645a79fca | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mpanetta | 0:ef4645a79fca | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mpanetta | 0:ef4645a79fca | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mpanetta | 0:ef4645a79fca | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mpanetta | 0:ef4645a79fca | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mpanetta | 0:ef4645a79fca | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
mpanetta | 0:ef4645a79fca | 20 | THE SOFTWARE. |
mpanetta | 0:ef4645a79fca | 21 | |
mpanetta | 0:ef4645a79fca | 22 | @file MODSERIAL.h |
mpanetta | 0:ef4645a79fca | 23 | @purpose Extends Serial to provide fully buffered IO |
mpanetta | 0:ef4645a79fca | 24 | @version 1.6 |
mpanetta | 0:ef4645a79fca | 25 | @date Nov 2010 |
mpanetta | 0:ef4645a79fca | 26 | @author Andy Kirkham |
mpanetta | 0:ef4645a79fca | 27 | */ |
mpanetta | 0:ef4645a79fca | 28 | |
mpanetta | 0:ef4645a79fca | 29 | #include "MODSERIAL.h" |
mpanetta | 0:ef4645a79fca | 30 | #include "MACROS.h" |
mpanetta | 0:ef4645a79fca | 31 | |
mpanetta | 0:ef4645a79fca | 32 | namespace AjK { |
mpanetta | 0:ef4645a79fca | 33 | |
mpanetta | 0:ef4645a79fca | 34 | MODSERIAL::MODSERIAL(PinName tx, PinName rx, const char *name) : Serial(tx, rx, name) |
mpanetta | 0:ef4645a79fca | 35 | { |
mpanetta | 0:ef4645a79fca | 36 | init(MODSERIAL_DEFAULT_TX_BUFFER_SIZE, MODSERIAL_DEFAULT_RX_BUFFER_SIZE); |
mpanetta | 0:ef4645a79fca | 37 | } |
mpanetta | 0:ef4645a79fca | 38 | |
mpanetta | 0:ef4645a79fca | 39 | MODSERIAL::MODSERIAL(PinName tx, PinName rx, int bufferSize, const char *name) : Serial(tx, rx, name) |
mpanetta | 0:ef4645a79fca | 40 | { |
mpanetta | 0:ef4645a79fca | 41 | init(bufferSize, bufferSize); |
mpanetta | 0:ef4645a79fca | 42 | } |
mpanetta | 0:ef4645a79fca | 43 | |
mpanetta | 0:ef4645a79fca | 44 | MODSERIAL::MODSERIAL(PinName tx, PinName rx, int txSize, int rxSize, const char *name) : Serial(tx, rx, name) |
mpanetta | 0:ef4645a79fca | 45 | { |
mpanetta | 0:ef4645a79fca | 46 | init(txSize, rxSize); |
mpanetta | 0:ef4645a79fca | 47 | } |
mpanetta | 0:ef4645a79fca | 48 | |
mpanetta | 0:ef4645a79fca | 49 | MODSERIAL::~MODSERIAL() |
mpanetta | 0:ef4645a79fca | 50 | { |
mpanetta | 0:ef4645a79fca | 51 | disableIrq(); |
mpanetta | 0:ef4645a79fca | 52 | if (buffer[0] != NULL) free((char *)buffer[0]); |
mpanetta | 0:ef4645a79fca | 53 | if (buffer[1] != NULL) free((char *)buffer[1]); |
mpanetta | 0:ef4645a79fca | 54 | } |
mpanetta | 0:ef4645a79fca | 55 | |
mpanetta | 0:ef4645a79fca | 56 | bool |
mpanetta | 0:ef4645a79fca | 57 | MODSERIAL::txBufferFull(void) |
mpanetta | 0:ef4645a79fca | 58 | { |
mpanetta | 0:ef4645a79fca | 59 | return MODSERIAL_TX_BUFFER_FULL; |
mpanetta | 0:ef4645a79fca | 60 | } |
mpanetta | 0:ef4645a79fca | 61 | |
mpanetta | 0:ef4645a79fca | 62 | bool |
mpanetta | 0:ef4645a79fca | 63 | MODSERIAL::rxBufferFull(void) |
mpanetta | 0:ef4645a79fca | 64 | { |
mpanetta | 0:ef4645a79fca | 65 | return MODSERIAL_RX_BUFFER_FULL; |
mpanetta | 0:ef4645a79fca | 66 | } |
mpanetta | 0:ef4645a79fca | 67 | |
mpanetta | 0:ef4645a79fca | 68 | bool |
mpanetta | 0:ef4645a79fca | 69 | MODSERIAL::txBufferEmpty(void) |
mpanetta | 0:ef4645a79fca | 70 | { |
mpanetta | 0:ef4645a79fca | 71 | return MODSERIAL_TX_BUFFER_EMPTY; |
mpanetta | 0:ef4645a79fca | 72 | } |
mpanetta | 0:ef4645a79fca | 73 | |
mpanetta | 0:ef4645a79fca | 74 | bool |
mpanetta | 0:ef4645a79fca | 75 | MODSERIAL::rxBufferEmpty(void) |
mpanetta | 0:ef4645a79fca | 76 | { |
mpanetta | 0:ef4645a79fca | 77 | return MODSERIAL_RX_BUFFER_EMPTY; |
mpanetta | 0:ef4645a79fca | 78 | } |
mpanetta | 0:ef4645a79fca | 79 | |
mpanetta | 0:ef4645a79fca | 80 | bool |
mpanetta | 0:ef4645a79fca | 81 | MODSERIAL::txIsBusy(void) |
mpanetta | 0:ef4645a79fca | 82 | { |
mpanetta | 0:ef4645a79fca | 83 | return (_LSR & (3UL << 5) == 0) ? true : false; |
mpanetta | 0:ef4645a79fca | 84 | } |
mpanetta | 0:ef4645a79fca | 85 | |
mpanetta | 0:ef4645a79fca | 86 | void |
mpanetta | 0:ef4645a79fca | 87 | MODSERIAL::disableIrq(void) |
mpanetta | 0:ef4645a79fca | 88 | { |
mpanetta | 0:ef4645a79fca | 89 | switch(_uidx) { |
mpanetta | 0:ef4645a79fca | 90 | case 0: NVIC_DisableIRQ(UART0_IRQn); break; |
mpanetta | 0:ef4645a79fca | 91 | case 1: NVIC_DisableIRQ(UART1_IRQn); break; |
mpanetta | 0:ef4645a79fca | 92 | case 2: NVIC_DisableIRQ(UART2_IRQn); break; |
mpanetta | 0:ef4645a79fca | 93 | case 3: NVIC_DisableIRQ(UART3_IRQn); break; |
mpanetta | 0:ef4645a79fca | 94 | } |
mpanetta | 0:ef4645a79fca | 95 | } |
mpanetta | 0:ef4645a79fca | 96 | |
mpanetta | 0:ef4645a79fca | 97 | void |
mpanetta | 0:ef4645a79fca | 98 | MODSERIAL::enableIrq(void) |
mpanetta | 0:ef4645a79fca | 99 | { |
mpanetta | 0:ef4645a79fca | 100 | switch(_uidx) { |
mpanetta | 0:ef4645a79fca | 101 | case 0: NVIC_EnableIRQ(UART0_IRQn); break; |
mpanetta | 0:ef4645a79fca | 102 | case 1: NVIC_EnableIRQ(UART1_IRQn); break; |
mpanetta | 0:ef4645a79fca | 103 | case 2: NVIC_EnableIRQ(UART2_IRQn); break; |
mpanetta | 0:ef4645a79fca | 104 | case 3: NVIC_EnableIRQ(UART3_IRQn); break; |
mpanetta | 0:ef4645a79fca | 105 | } |
mpanetta | 0:ef4645a79fca | 106 | } |
mpanetta | 0:ef4645a79fca | 107 | |
mpanetta | 0:ef4645a79fca | 108 | int |
mpanetta | 0:ef4645a79fca | 109 | MODSERIAL::rxDiscardLastChar(void) |
mpanetta | 0:ef4645a79fca | 110 | { |
mpanetta | 0:ef4645a79fca | 111 | // This function can only be called indirectly from |
mpanetta | 0:ef4645a79fca | 112 | // an rxCallback function. Therefore, we know we |
mpanetta | 0:ef4645a79fca | 113 | // just placed a char into the buffer. |
mpanetta | 0:ef4645a79fca | 114 | char c = buffer[RxIrq][buffer_in[RxIrq]]; |
mpanetta | 0:ef4645a79fca | 115 | |
mpanetta | 0:ef4645a79fca | 116 | if (buffer_count[RxIrq]) { |
mpanetta | 0:ef4645a79fca | 117 | buffer_count[RxIrq]--; |
mpanetta | 0:ef4645a79fca | 118 | buffer_in[RxIrq]--; |
mpanetta | 0:ef4645a79fca | 119 | if (buffer_in[RxIrq] < 0) { |
mpanetta | 0:ef4645a79fca | 120 | buffer_in[RxIrq] = buffer_size[RxIrq] - 1; |
mpanetta | 0:ef4645a79fca | 121 | } |
mpanetta | 0:ef4645a79fca | 122 | } |
mpanetta | 0:ef4645a79fca | 123 | |
mpanetta | 0:ef4645a79fca | 124 | return (int)c; |
mpanetta | 0:ef4645a79fca | 125 | } |
mpanetta | 0:ef4645a79fca | 126 | |
mpanetta | 0:ef4645a79fca | 127 | |
mpanetta | 0:ef4645a79fca | 128 | }; // namespace AjK ends |