Prof Greg Egan / Mbed 2 deprecated UAVXArm-GKE

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sb_uart0.cpp Source File

sb_uart0.cpp

00001 /*
00002     Copyright (c) 2010 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021 */
00022 
00023 #include "../SerialBuffered.h"
00024 #include "../sb_globals.h"
00025 
00026 extern "C" void SerialBuffered_ISR(unsigned long, int);
00027 
00028 extern "C" static void SerialBuffered_ISR_0(void) __irq { SerialBuffered_ISR(LPC_UART0_BASE, 0); }
00029 
00030 void SerialBuffered::set_uart0(PinName tx, PinName rx) {
00031     unsigned ier = 0;
00032     
00033     if (tx == NC && rx == NC) return; // Eh, why would one?
00034     
00035     LPC_SC->PCONP       |=  (1UL << 3);
00036     LPC_SC->PCLKSEL0    &= ~(3UL << 6);
00037     LPC_SC->PCLKSEL0    |=  (1UL << 6);
00038     
00039     if (tx == USBTX) { 
00040         set_tx_buffer_size(SERIALBUFFERED_BUFFER_SIZE); 
00041         LPC_PINCON->PINSEL0 &= ~(1UL << 4); 
00042         LPC_PINCON->PINSEL0 |=  (1UL << 4); 
00043         ier |= 1;
00044     }
00045     else { reset_uart_tx(0); }
00046     
00047     if (rx == USBRX) { 
00048         set_rx_buffer_size(SERIALBUFFERED_BUFFER_SIZE); 
00049         LPC_PINCON->PINSEL0 &= ~(1UL << 6); 
00050         LPC_PINCON->PINSEL0 |=  (1UL << 6); 
00051         ier |= 2;
00052     }
00053     else { reset_uart_rx(0); }
00054     
00055     if (ier) {
00056         baud(9600);
00057         format(SerialBuffered::WordLength8, SerialBuffered::NoParity, SerialBuffered::StopBit1);
00058         LPC_UART0->FCR = 0x7;
00059         NVIC_SetVector(UART0_IRQn, (uint32_t)SerialBuffered_ISR_0);
00060         NVIC_EnableIRQ(UART0_IRQn);
00061         LPC_UART0->IER = ier;
00062     }
00063 }