UAVX Multicopter Flight Controller.

Dependencies:   mbed

Committer:
gke
Date:
Tue Apr 26 12:12:29 2011 +0000
Revision:
2:90292f8bd179
Parent:
0:62a1c91a859a
Not flightworthy. Posted for others to make use of the I2C SW code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gke 2:90292f8bd179 1 /*
gke 2:90292f8bd179 2 Copyright (c) 2010 Andy Kirkham
gke 2:90292f8bd179 3
gke 2:90292f8bd179 4 Permission is hereby granted, free of charge, to any person obtaining a copy
gke 2:90292f8bd179 5 of this software and associated documentation files (the "Software"), to deal
gke 2:90292f8bd179 6 in the Software without restriction, including without limitation the rights
gke 2:90292f8bd179 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
gke 2:90292f8bd179 8 copies of the Software, and to permit persons to whom the Software is
gke 2:90292f8bd179 9 furnished to do so, subject to the following conditions:
gke 2:90292f8bd179 10
gke 2:90292f8bd179 11 The above copyright notice and this permission notice shall be included in
gke 2:90292f8bd179 12 all copies or substantial portions of the Software.
gke 2:90292f8bd179 13
gke 2:90292f8bd179 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
gke 2:90292f8bd179 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
gke 2:90292f8bd179 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
gke 2:90292f8bd179 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
gke 2:90292f8bd179 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gke 2:90292f8bd179 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
gke 2:90292f8bd179 20 THE SOFTWARE.
gke 2:90292f8bd179 21 */
gke 2:90292f8bd179 22
gke 2:90292f8bd179 23 #ifndef SERIALBUFFERED_H
gke 2:90292f8bd179 24 #define SERIALBUFFERED_H
gke 2:90292f8bd179 25
gke 2:90292f8bd179 26 #include "mbed.h"
gke 2:90292f8bd179 27
gke 2:90292f8bd179 28
gke 2:90292f8bd179 29
gke 2:90292f8bd179 30 /** SerialBuffered based on Serial but fully buffered IO
gke 2:90292f8bd179 31 *
gke 2:90292f8bd179 32 * Example:
gke 2:90292f8bd179 33 * @code
gke 2:90292f8bd179 34 * #include "mbed.h"
gke 2:90292f8bd179 35 * #include "SerialBuffered.h"
gke 2:90292f8bd179 36 *
gke 2:90292f8bd179 37 * SerialBuffered serial1 (p13, p14);
gke 2:90292f8bd179 38 * SerialBuffered serial2 (p28, p27);
gke 2:90292f8bd179 39 *
gke 2:90292f8bd179 40 * int main() {
gke 2:90292f8bd179 41 * while(1) {
gke 2:90292f8bd179 42 * if (serial1.readable()) {
gke 2:90292f8bd179 43 * while (!serial2.writeable());
gke 2:90292f8bd179 44 * serial2.putc(serial1.getch());
gke 2:90292f8bd179 45 * }
gke 2:90292f8bd179 46 * if (serial2.readable()) {
gke 2:90292f8bd179 47 * while (!serial1.writeable());
gke 2:90292f8bd179 48 * serial1.putc(serial2.getc());
gke 2:90292f8bd179 49 * }
gke 2:90292f8bd179 50 * }
gke 2:90292f8bd179 51 * }
gke 2:90292f8bd179 52 * @endcode
gke 2:90292f8bd179 53 *
gke 2:90292f8bd179 54 * <b>Note</b>, because this system "traps" the interrupts for the UART
gke 2:90292f8bd179 55 * being used <b>do not</b> use the .attach() method, otherwise the buffers
gke 2:90292f8bd179 56 * will cease functioning. Or worse, behaviour becomes unpredictable.
gke 2:90292f8bd179 57 */
gke 2:90292f8bd179 58
gke 2:90292f8bd179 59 class SerialBuffered : public Serial {
gke 2:90292f8bd179 60
gke 2:90292f8bd179 61 public:
gke 2:90292f8bd179 62 enum { None = 0, One = 1, Two = 2 };
gke 2:90292f8bd179 63 enum { WordLength5 = 0, WordLength6 = 1, WordLength7 = 2, WordLength8 = 3 };
gke 2:90292f8bd179 64 enum { NoParity = 0, OddParity = (1UL << 3), EvenParity = (3UL << 3), Forced1 = (5UL << 3), Forced0 = (7UL << 3) };
gke 2:90292f8bd179 65 enum { StopBit1 = (0UL << 2), StopBit2 = (1UL << 2) };
gke 2:90292f8bd179 66
gke 2:90292f8bd179 67 /** Create a SerialBuffered object connected to the specified pins
gke 2:90292f8bd179 68 *
gke 2:90292f8bd179 69 * @param PinName tx The Mbed TX pin for the uart port.
gke 2:90292f8bd179 70 * @param PinName rx The Mbed RX pin for the uart port.
gke 2:90292f8bd179 71 */
gke 2:90292f8bd179 72 SerialBuffered(PinName tx, PinName rx);
gke 2:90292f8bd179 73
gke 2:90292f8bd179 74 virtual ~SerialBuffered();
gke 2:90292f8bd179 75
gke 2:90292f8bd179 76 /** Get a character from the serial stream.
gke 2:90292f8bd179 77 *
gke 2:90292f8bd179 78 * @return char A char value of the character read.
gke 2:90292f8bd179 79 */
gke 2:90292f8bd179 80 char getc(void);
gke 2:90292f8bd179 81
gke 2:90292f8bd179 82 /** Gets a character from the serial stream with optional blocking.
gke 2:90292f8bd179 83 *
gke 2:90292f8bd179 84 * This method allows for getting a character from the serial stream
gke 2:90292f8bd179 85 * if one is available. If <b>blocking</b> is true, the method will
gke 2:90292f8bd179 86 * wait for serial input if the RX buffer is empty. If <b>blocking</b>
gke 2:90292f8bd179 87 * is false, the method will return immediately if the RX buffer is empty.
gke 2:90292f8bd179 88 * On return, if not blocking and the buffer was empty, -1 is returned.
gke 2:90292f8bd179 89 *
gke 2:90292f8bd179 90 * @param blocking true or false, when true will block.
gke 2:90292f8bd179 91 * @return int An int representation of the 8bit char or -1 on buffer empty.
gke 2:90292f8bd179 92 */
gke 2:90292f8bd179 93 int getc(bool blocking);
gke 2:90292f8bd179 94
gke 2:90292f8bd179 95 /** Puts a characher to the serial stream.
gke 2:90292f8bd179 96 *
gke 2:90292f8bd179 97 * This sends a character out of the uart port or, if no room in the
gke 2:90292f8bd179 98 * TX FIFO, will place the character into the TX buffer. <b>Note</b>, if the
gke 2:90292f8bd179 99 * TX buffer is also full, this method will <b>block</b> (wait) until
gke 2:90292f8bd179 100 * there is room in the buffer.
gke 2:90292f8bd179 101 *
gke 2:90292f8bd179 102 * @param int An int representation of the 8bit character to send.
gke 2:90292f8bd179 103 * @return int Always returns zero.
gke 2:90292f8bd179 104 */
gke 2:90292f8bd179 105 int putc(int c);
gke 2:90292f8bd179 106
gke 2:90292f8bd179 107 /** Puts a characher to the serial stream.
gke 2:90292f8bd179 108 *
gke 2:90292f8bd179 109 * As with putc(int c) this function allows for a character to be sent
gke 2:90292f8bd179 110 * to the uart TX port. However, an extra parameter is added to allow
gke 2:90292f8bd179 111 * the caller to decide if the method should block or not. If blocking
gke 2:90292f8bd179 112 * is disabled then this method returns -1 to signal there was no room
gke 2:90292f8bd179 113 * in the TX FIFO or the TX buffer. The character c passed is has not
gke 2:90292f8bd179 114 * therefore been sent.
gke 2:90292f8bd179 115 *
gke 2:90292f8bd179 116 * @param int An int representation of the 8bit character to send.
gke 2:90292f8bd179 117 * @param bool true if blocking required, false to disable blocking.
gke 2:90292f8bd179 118 * @return int Zero on success, -1 if no room in TX FIFO or TX buffer.
gke 2:90292f8bd179 119 */
gke 2:90292f8bd179 120 int putc(int c, bool blocking);
gke 2:90292f8bd179 121
gke 2:90292f8bd179 122 /** Are there characters in the RX buffer we can read?
gke 2:90292f8bd179 123 *
gke 2:90292f8bd179 124 * @return int 1 if characters are available, 0 otherwise.
gke 2:90292f8bd179 125 */
gke 2:90292f8bd179 126 int readable(void);
gke 2:90292f8bd179 127
gke 2:90292f8bd179 128 /** Is there room in the TX buffer to send a character?
gke 2:90292f8bd179 129 *
gke 2:90292f8bd179 130 * @return int 1 if room available, 0 otherwise.
gke 2:90292f8bd179 131 */
gke 2:90292f8bd179 132 int writeable(void);
gke 2:90292f8bd179 133
gke 2:90292f8bd179 134 /** Set's the UART baud rate.
gke 2:90292f8bd179 135 *
gke 2:90292f8bd179 136 * Any allowed baudrate may be passed. However, you should
gke 2:90292f8bd179 137 * ensure it matches the far end of the serial link.
gke 2:90292f8bd179 138 *
gke 2:90292f8bd179 139 * @param int The baudrate to set.
gke 2:90292f8bd179 140 */
gke 2:90292f8bd179 141 void baud(int baudrate);
gke 2:90292f8bd179 142
gke 2:90292f8bd179 143 /** Sets the serial format.
gke 2:90292f8bd179 144 *
gke 2:90292f8bd179 145 * Valid serial bit lengths are:-
gke 2:90292f8bd179 146 * <ul>
gke 2:90292f8bd179 147 * <li>SerialBuffered::WordLength5</li>
gke 2:90292f8bd179 148 * <li>SerialBuffered::WordLength6</li>
gke 2:90292f8bd179 149 * <li>SerialBuffered::WordLength7</li>
gke 2:90292f8bd179 150 * <li>SerialBuffered::WordLength8</li>
gke 2:90292f8bd179 151 * </ul>
gke 2:90292f8bd179 152 *
gke 2:90292f8bd179 153 * Valid serial parity are:-
gke 2:90292f8bd179 154 * <ul>
gke 2:90292f8bd179 155 * <li>SerialBuffered::NoParity</li>
gke 2:90292f8bd179 156 * <li>SerialBuffered::OddParity</li>
gke 2:90292f8bd179 157 * <li>SerialBuffered::EvenParity</li>
gke 2:90292f8bd179 158 * <li>SerialBuffered::Forced1</li>
gke 2:90292f8bd179 159 * <li>SerialBuffered::Forced0</li>
gke 2:90292f8bd179 160 * </ul>
gke 2:90292f8bd179 161 *
gke 2:90292f8bd179 162 * Valid stop bits are:-
gke 2:90292f8bd179 163 * <ul>
gke 2:90292f8bd179 164 * <li>SerialBuffered::None</li>
gke 2:90292f8bd179 165 * <li>SerialBuffered::One</li>
gke 2:90292f8bd179 166 * <li>SerialBuffered::Two</li>
gke 2:90292f8bd179 167 * </ul>
gke 2:90292f8bd179 168 *
gke 2:90292f8bd179 169 * @param int bits
gke 2:90292f8bd179 170 * @param int parity
gke 2:90292f8bd179 171 * @param int stopbits
gke 2:90292f8bd179 172 */
gke 2:90292f8bd179 173 void format(int bits, int parity, int stopbits);
gke 2:90292f8bd179 174
gke 2:90292f8bd179 175 /** Change the TX buffer size
gke 2:90292f8bd179 176 *
gke 2:90292f8bd179 177 * By default, when the SerialBuffer object is created, a default
gke 2:90292f8bd179 178 * TX buffer of 256 bytes in size is created. If you need a bigger
gke 2:90292f8bd179 179 * (or smaller) buffer then use this function to change the TX buffer
gke 2:90292f8bd179 180 * size.
gke 2:90292f8bd179 181 *
gke 2:90292f8bd179 182 * <b>Note</b>, when a buffer is resized, any previous buffer
gke 2:90292f8bd179 183 * in operation is discarded (destroyed and lost).
gke 2:90292f8bd179 184 *
gke 2:90292f8bd179 185 * @param int The size of the TX buffer required.
gke 2:90292f8bd179 186 */
gke 2:90292f8bd179 187 void set_tx_buffer_size(int buffer_size);
gke 2:90292f8bd179 188
gke 2:90292f8bd179 189 /** Change the TX buffer size and provide your own allocation.
gke 2:90292f8bd179 190 *
gke 2:90292f8bd179 191 * This methos allows for the buffer size to be changed and for the
gke 2:90292f8bd179 192 * caller to pass a pointer to an area of RAM they have already set
gke 2:90292f8bd179 193 * aside to hold the buffer. The standard method is to malloc space
gke 2:90292f8bd179 194 * from the heap. This method allows that to be overriden and use a
gke 2:90292f8bd179 195 * user supplied buffer.
gke 2:90292f8bd179 196 *
gke 2:90292f8bd179 197 * <b>Note</b>, the buffer you create must be of the size you specify!
gke 2:90292f8bd179 198 * <b>Note</b>, when a buffer is resized, any previous buffer
gke 2:90292f8bd179 199 * in operation is discarded (destroyed and lost).
gke 2:90292f8bd179 200 *
gke 2:90292f8bd179 201 * @param int The size of the TX buffer required.
gke 2:90292f8bd179 202 * @param char* A pointer to a buffer area you previously allocated.
gke 2:90292f8bd179 203 */
gke 2:90292f8bd179 204 void set_tx_buffer_size(int buffer_size, char *buffer);
gke 2:90292f8bd179 205
gke 2:90292f8bd179 206 /** Change the RX buffer size
gke 2:90292f8bd179 207 *
gke 2:90292f8bd179 208 * By default, when the SerialBuffer object is created, a default
gke 2:90292f8bd179 209 * RX buffer of 256 bytes in size is created. If you need a bigger
gke 2:90292f8bd179 210 * (or smaller) buffer then use this function to change the RX buffer
gke 2:90292f8bd179 211 * size.
gke 2:90292f8bd179 212 *
gke 2:90292f8bd179 213 * <b>Note</b>, when a buffer is resized, any previous buffer
gke 2:90292f8bd179 214 * in operation is discarded (destroyed and lost).
gke 2:90292f8bd179 215 *
gke 2:90292f8bd179 216 * @param int The size of the RX buffer required.
gke 2:90292f8bd179 217 */
gke 2:90292f8bd179 218 void set_rx_buffer_size(int buffer_size);
gke 2:90292f8bd179 219
gke 2:90292f8bd179 220 /** Change the RX buffer size and provide your own allocation.
gke 2:90292f8bd179 221 *
gke 2:90292f8bd179 222 * This methos allows for the buffer size to be changed and for the
gke 2:90292f8bd179 223 * caller to pass a pointer to an area of RAM they have already set
gke 2:90292f8bd179 224 * aside to hold the buffer. The standard method is to malloc space
gke 2:90292f8bd179 225 * from the heap. This method allows that to be overriden and use a
gke 2:90292f8bd179 226 * user supplied buffer.
gke 2:90292f8bd179 227 *
gke 2:90292f8bd179 228 * <b>Note</b>, the buffer you create must be of the size you specify!
gke 2:90292f8bd179 229 * <b>Note</b>, when a buffer is resized, any previous buffer
gke 2:90292f8bd179 230 * in operation is discarded (destroyed and lost).
gke 2:90292f8bd179 231 *
gke 2:90292f8bd179 232 * @param int The size of the RX buffer required.
gke 2:90292f8bd179 233 * @param char* A pointer to a buffer area you previously allocated.
gke 2:90292f8bd179 234 */
gke 2:90292f8bd179 235 void set_rx_buffer_size(int buffer_size, char *buffer);
gke 2:90292f8bd179 236
gke 2:90292f8bd179 237 protected:
gke 2:90292f8bd179 238 /** Calculate the divisors for a UART's baud
gke 2:90292f8bd179 239 *
gke 2:90292f8bd179 240 * @param int The desired baud rate
gke 2:90292f8bd179 241 * @param int The UART in use to calculate for
gke 2:90292f8bd179 242 */
gke 2:90292f8bd179 243 uint16_t calculate_baud(int baud, int uart);
gke 2:90292f8bd179 244
gke 2:90292f8bd179 245 private:
gke 2:90292f8bd179 246 /** set_uart0
gke 2:90292f8bd179 247 *
gke 2:90292f8bd179 248 * Sets up the hardware and interrupts for the UART.
gke 2:90292f8bd179 249 *
gke 2:90292f8bd179 250 * @param PinName tx
gke 2:90292f8bd179 251 * @param PinName rx
gke 2:90292f8bd179 252 */
gke 2:90292f8bd179 253 void set_uart0(PinName tx, PinName rx);
gke 2:90292f8bd179 254
gke 2:90292f8bd179 255 /** set_uart1
gke 2:90292f8bd179 256 *
gke 2:90292f8bd179 257 * Sets up the hardware and interrupts for the UART.
gke 2:90292f8bd179 258 *
gke 2:90292f8bd179 259 * @param PinName tx
gke 2:90292f8bd179 260 * @param PinName rx
gke 2:90292f8bd179 261 */
gke 2:90292f8bd179 262 void set_uart1(PinName tx, PinName rx);
gke 2:90292f8bd179 263
gke 2:90292f8bd179 264 /** set_uart2
gke 2:90292f8bd179 265 *
gke 2:90292f8bd179 266 * Sets up the hardware and interrupts for the UART.
gke 2:90292f8bd179 267 *
gke 2:90292f8bd179 268 * @param PinName tx
gke 2:90292f8bd179 269 * @param PinName rx
gke 2:90292f8bd179 270 */
gke 2:90292f8bd179 271 void set_uart2(PinName tx, PinName rx);
gke 2:90292f8bd179 272
gke 2:90292f8bd179 273 /** set_uart3
gke 2:90292f8bd179 274 *
gke 2:90292f8bd179 275 * Sets up the hardware and interrupts for the UART.
gke 2:90292f8bd179 276 *
gke 2:90292f8bd179 277 * @param PinName tx
gke 2:90292f8bd179 278 * @param PinName rx
gke 2:90292f8bd179 279 */
gke 2:90292f8bd179 280 void set_uart3(PinName tx, PinName rx);
gke 2:90292f8bd179 281
gke 2:90292f8bd179 282 /** Reset the TX Buffer
gke 2:90292f8bd179 283 *
gke 2:90292f8bd179 284 * @param int The UART buffer to reset.
gke 2:90292f8bd179 285 */
gke 2:90292f8bd179 286 void reset_uart_tx(int uart_number);
gke 2:90292f8bd179 287
gke 2:90292f8bd179 288 /** Reset the RX Buffer
gke 2:90292f8bd179 289 *
gke 2:90292f8bd179 290 * @param int The UART buffer to reset.
gke 2:90292f8bd179 291 */
gke 2:90292f8bd179 292 void reset_uart_rx(int uart_number);
gke 2:90292f8bd179 293
gke 2:90292f8bd179 294 /** LPC1768 UART peripheral base address for UART in use.
gke 2:90292f8bd179 295 */
gke 2:90292f8bd179 296 unsigned long uart_base;
gke 2:90292f8bd179 297
gke 2:90292f8bd179 298 /** LPC1768 UART number for UART in use.
gke 2:90292f8bd179 299 */
gke 2:90292f8bd179 300 int uart_number;
gke 2:90292f8bd179 301 };
gke 2:90292f8bd179 302
gke 2:90292f8bd179 303 #endif