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