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 0:62a1c91a859a 1 /*
gke 0:62a1c91a859a 2 Copyright (c) 2010 Andy Kirkham
gke 0:62a1c91a859a 3
gke 0:62a1c91a859a 4 Permission is hereby granted, free of charge, to any person obtaining a copy
gke 0:62a1c91a859a 5 of this software and associated documentation files (the "Software"), to deal
gke 0:62a1c91a859a 6 in the Software without restriction, including without limitation the rights
gke 0:62a1c91a859a 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
gke 0:62a1c91a859a 8 copies of the Software, and to permit persons to whom the Software is
gke 0:62a1c91a859a 9 furnished to do so, subject to the following conditions:
gke 0:62a1c91a859a 10
gke 0:62a1c91a859a 11 The above copyright notice and this permission notice shall be included in
gke 0:62a1c91a859a 12 all copies or substantial portions of the Software.
gke 0:62a1c91a859a 13
gke 0:62a1c91a859a 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
gke 0:62a1c91a859a 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
gke 0:62a1c91a859a 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
gke 0:62a1c91a859a 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
gke 0:62a1c91a859a 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gke 0:62a1c91a859a 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
gke 0:62a1c91a859a 20 THE SOFTWARE.
gke 0:62a1c91a859a 21 */
gke 0:62a1c91a859a 22
gke 0:62a1c91a859a 23 #include "../SerialBuffered.h"
gke 0:62a1c91a859a 24 #include "../sb_globals.h"
gke 0:62a1c91a859a 25
gke 0:62a1c91a859a 26 void SerialBuffered::format(int bits, int parity, int stopbits) {
gke 0:62a1c91a859a 27 SET_REGISTER(SERIALBUFFERED_LCR, (bits | parity | stopbits) & 0x7F);
gke 0:62a1c91a859a 28 }
gke 0:62a1c91a859a 29
gke 0:62a1c91a859a 30 void SerialBuffered::baud(int baud) {
gke 0:62a1c91a859a 31 uint16_t rate = calculate_baud(baud, uart_number);
gke 0:62a1c91a859a 32 SET_REGISTER(SERIALBUFFERED_LCR, GET_REGISTER(SERIALBUFFERED_LCR) | 0x80);
gke 0:62a1c91a859a 33 SET_REGISTER(SERIALBUFFERED_DML, (rate >> 8) & 0xFF);
gke 0:62a1c91a859a 34 SET_REGISTER(SERIALBUFFERED_DLL, (rate &0xFF));
gke 0:62a1c91a859a 35 SET_REGISTER(SERIALBUFFERED_LCR, GET_REGISTER(SERIALBUFFERED_LCR) & 0x7F);
gke 0:62a1c91a859a 36 }
gke 0:62a1c91a859a 37
gke 0:62a1c91a859a 38 uint16_t SerialBuffered::calculate_baud(int baud, int uart) {
gke 0:62a1c91a859a 39 static int multipliers[4] = { 4, 1, 2, 8 };
gke 0:62a1c91a859a 40 int clock = 0;
gke 0:62a1c91a859a 41
gke 0:62a1c91a859a 42 switch (uart) {
gke 0:62a1c91a859a 43 case 0: clock = (LPC_SC->PCLKSEL0 >> 6) & 0x3; break;
gke 0:62a1c91a859a 44 case 1: clock = (LPC_SC->PCLKSEL0 >> 8) & 0x3; break;
gke 0:62a1c91a859a 45 case 2: clock = (LPC_SC->PCLKSEL1 >> 16) & 0x3; break;
gke 0:62a1c91a859a 46 case 3: clock = (LPC_SC->PCLKSEL1 >> 18) & 0x3; break;
gke 0:62a1c91a859a 47 }
gke 0:62a1c91a859a 48
gke 0:62a1c91a859a 49 return (uint16_t)(((SystemCoreClock / 16 / baud) * multipliers[clock]) & 0xFFFF);
gke 0:62a1c91a859a 50 }
gke 0:62a1c91a859a 51
gke 0:62a1c91a859a 52 void SerialBuffered::set_tx_buffer_size(int buffer_size, char *buffer) {
gke 0:62a1c91a859a 53 SET_REGISTER(SERIALBUFFERED_IER, GET_REGISTER(SERIALBUFFERED_IER) & (1UL << 2));
gke 0:62a1c91a859a 54 SET_REGISTER(SERIALBUFFERED_FCR, (1UL << 2) | 1);
gke 0:62a1c91a859a 55 _tx_buffer_size[uart_number] = buffer_size;
gke 0:62a1c91a859a 56 if (_tx_buffer_used_malloc[uart_number] && _tx_buffer[uart_number]) {
gke 0:62a1c91a859a 57 free(_tx_buffer[uart_number]);
gke 0:62a1c91a859a 58 _tx_buffer_used_malloc[uart_number] = false;
gke 0:62a1c91a859a 59 }
gke 0:62a1c91a859a 60 if (buffer == (char *)NULL) {
gke 0:62a1c91a859a 61 _tx_buffer[uart_number] = (char *)malloc(buffer_size);
gke 0:62a1c91a859a 62 _tx_buffer_used_malloc[uart_number] = true;
gke 0:62a1c91a859a 63 }
gke 0:62a1c91a859a 64 else {
gke 0:62a1c91a859a 65 _tx_buffer[uart_number] = buffer;
gke 0:62a1c91a859a 66 _tx_buffer_used_malloc[uart_number] = false;
gke 0:62a1c91a859a 67 }
gke 0:62a1c91a859a 68 _tx_buffer_in[uart_number] = _tx_buffer_out[uart_number] = 0;
gke 0:62a1c91a859a 69 _tx_buffer_full[uart_number] = false;
gke 0:62a1c91a859a 70 SET_REGISTER(SERIALBUFFERED_IER, GET_REGISTER(SERIALBUFFERED_IER) | 0x2);
gke 0:62a1c91a859a 71 }
gke 0:62a1c91a859a 72
gke 0:62a1c91a859a 73 void SerialBuffered::set_tx_buffer_size(int buffer_size) {
gke 0:62a1c91a859a 74 set_tx_buffer_size(buffer_size, (char *)NULL);
gke 0:62a1c91a859a 75 }
gke 0:62a1c91a859a 76
gke 0:62a1c91a859a 77 /**
gke 0:62a1c91a859a 78 */
gke 0:62a1c91a859a 79 void SerialBuffered::set_rx_buffer_size(int buffer_size, char *buffer) {
gke 0:62a1c91a859a 80 SET_REGISTER(SERIALBUFFERED_IER, GET_REGISTER(SERIALBUFFERED_IER) & (1UL << 1));
gke 0:62a1c91a859a 81 SET_REGISTER(SERIALBUFFERED_FCR, (1UL << 1) | 1);
gke 0:62a1c91a859a 82 _rx_buffer_size[uart_number] = buffer_size;
gke 0:62a1c91a859a 83 if (_rx_buffer_used_malloc[uart_number] && _rx_buffer[uart_number]) {
gke 0:62a1c91a859a 84 free(_rx_buffer[uart_number]);
gke 0:62a1c91a859a 85 _rx_buffer_used_malloc[uart_number] = false;
gke 0:62a1c91a859a 86 }
gke 0:62a1c91a859a 87 if (buffer == (char *)NULL) {
gke 0:62a1c91a859a 88 _rx_buffer[uart_number] = (char *)malloc(buffer_size);
gke 0:62a1c91a859a 89 _rx_buffer_used_malloc[uart_number] = true;
gke 0:62a1c91a859a 90 }
gke 0:62a1c91a859a 91 else {
gke 0:62a1c91a859a 92 _rx_buffer[uart_number] = buffer;
gke 0:62a1c91a859a 93 _rx_buffer_used_malloc[uart_number] = false;
gke 0:62a1c91a859a 94 }
gke 0:62a1c91a859a 95 _rx_buffer_in[uart_number] = _rx_buffer_out[uart_number] = 0;
gke 0:62a1c91a859a 96 _rx_buffer_full[uart_number] = false;
gke 0:62a1c91a859a 97 SET_REGISTER(SERIALBUFFERED_IER, GET_REGISTER(SERIALBUFFERED_IER) | 0x1);
gke 0:62a1c91a859a 98 }
gke 0:62a1c91a859a 99
gke 0:62a1c91a859a 100 void SerialBuffered::set_rx_buffer_size(int buffer_size) {
gke 0:62a1c91a859a 101 set_rx_buffer_size(buffer_size, (char *)NULL);
gke 0:62a1c91a859a 102 }
gke 0:62a1c91a859a 103
gke 0:62a1c91a859a 104
gke 0:62a1c91a859a 105 void SerialBuffered::reset_uart_tx(int uart_number) {
gke 0:62a1c91a859a 106 SET_REGISTER(SERIALBUFFERED_IER, GET_REGISTER(SERIALBUFFERED_IER) & (1UL << 1));
gke 0:62a1c91a859a 107 SET_REGISTER(SERIALBUFFERED_FCR, (1UL << 2) | 1);
gke 0:62a1c91a859a 108 if (_tx_buffer_used_malloc[uart_number] && _tx_buffer[uart_number]) {
gke 0:62a1c91a859a 109 free(_tx_buffer[uart_number]);
gke 0:62a1c91a859a 110 _tx_buffer[uart_number] = (char *)NULL;
gke 0:62a1c91a859a 111 _tx_buffer_used_malloc[uart_number] = false;
gke 0:62a1c91a859a 112 }
gke 0:62a1c91a859a 113 _tx_buffer_in[uart_number] = _tx_buffer_out[uart_number] = 0;
gke 0:62a1c91a859a 114 _tx_buffer_full[uart_number] = false;
gke 0:62a1c91a859a 115 }
gke 0:62a1c91a859a 116
gke 0:62a1c91a859a 117 void SerialBuffered::reset_uart_rx(int uart_number) {
gke 0:62a1c91a859a 118 SET_REGISTER(SERIALBUFFERED_IER, GET_REGISTER(SERIALBUFFERED_IER) & (1UL << 0));
gke 0:62a1c91a859a 119 SET_REGISTER(SERIALBUFFERED_FCR, (1UL << 1) | 1);
gke 0:62a1c91a859a 120 if (_rx_buffer_used_malloc[uart_number] && _rx_buffer[uart_number]) {
gke 0:62a1c91a859a 121 free(_rx_buffer[uart_number]);
gke 0:62a1c91a859a 122 _rx_buffer[uart_number] = (char *)NULL;
gke 0:62a1c91a859a 123 _rx_buffer_used_malloc[uart_number] = false;
gke 0:62a1c91a859a 124 }
gke 0:62a1c91a859a 125 _rx_buffer_in[uart_number] = _rx_buffer_out[uart_number] = 0;
gke 0:62a1c91a859a 126 _rx_buffer_full[uart_number] = false;
gke 0:62a1c91a859a 127 }