Vincent Belanger
/
APP
APP 1 S5.
UART.cpp@13:08ef55cd14c6, 2016-01-12 (annotated)
- Committer:
- vinbel93
- Date:
- Tue Jan 12 15:17:21 2016 +0000
- Revision:
- 13:08ef55cd14c6
UART without mbed.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vinbel93 | 13:08ef55cd14c6 | 1 | // UART.cpp |
vinbel93 | 13:08ef55cd14c6 | 2 | // Vincent Belanger - belv1802 |
vinbel93 | 13:08ef55cd14c6 | 3 | // Jeremy Pare - parj2713 |
vinbel93 | 13:08ef55cd14c6 | 4 | |
vinbel93 | 13:08ef55cd14c6 | 5 | #include <stdint.h> |
vinbel93 | 13:08ef55cd14c6 | 6 | |
vinbel93 | 13:08ef55cd14c6 | 7 | #include "UART.h" |
vinbel93 | 13:08ef55cd14c6 | 8 | #include "APP.h" |
vinbel93 | 13:08ef55cd14c6 | 9 | #include "LPC17xx.h" |
vinbel93 | 13:08ef55cd14c6 | 10 | |
vinbel93 | 13:08ef55cd14c6 | 11 | |
vinbel93 | 13:08ef55cd14c6 | 12 | void UARTInit(uint32_t baudrate) |
vinbel93 | 13:08ef55cd14c6 | 13 | { |
vinbel93 | 13:08ef55cd14c6 | 14 | uint32_t Fdiv; |
vinbel93 | 13:08ef55cd14c6 | 15 | uint32_t pclkdiv, pclk; |
vinbel93 | 13:08ef55cd14c6 | 16 | |
vinbel93 | 13:08ef55cd14c6 | 17 | // Select UART3 |
vinbel93 | 13:08ef55cd14c6 | 18 | LPC_PINCON->PINSEL0 &= ~0x00000003; |
vinbel93 | 13:08ef55cd14c6 | 19 | LPC_PINCON->PINSEL0 |= 0x00000002; |
vinbel93 | 13:08ef55cd14c6 | 20 | |
vinbel93 | 13:08ef55cd14c6 | 21 | LPC_SC->PCONP |= 1 << 25; // Activate UART3 in PCONP register. |
vinbel93 | 13:08ef55cd14c6 | 22 | pclkdiv = (LPC_SC->PCLKSEL1 >> 18) & 0x03; // Bits 18~19 are for UART3 |
vinbel93 | 13:08ef55cd14c6 | 23 | |
vinbel93 | 13:08ef55cd14c6 | 24 | switch (pclkdiv) |
vinbel93 | 13:08ef55cd14c6 | 25 | { |
vinbel93 | 13:08ef55cd14c6 | 26 | case 0x00: |
vinbel93 | 13:08ef55cd14c6 | 27 | default: |
vinbel93 | 13:08ef55cd14c6 | 28 | pclk = SystemCoreClock/4; |
vinbel93 | 13:08ef55cd14c6 | 29 | break; |
vinbel93 | 13:08ef55cd14c6 | 30 | case 0x01: |
vinbel93 | 13:08ef55cd14c6 | 31 | pclk = SystemCoreClock; |
vinbel93 | 13:08ef55cd14c6 | 32 | break; |
vinbel93 | 13:08ef55cd14c6 | 33 | case 0x02: |
vinbel93 | 13:08ef55cd14c6 | 34 | pclk = SystemCoreClock/2; |
vinbel93 | 13:08ef55cd14c6 | 35 | break; |
vinbel93 | 13:08ef55cd14c6 | 36 | case 0x03: |
vinbel93 | 13:08ef55cd14c6 | 37 | pclk = SystemCoreClock/8; |
vinbel93 | 13:08ef55cd14c6 | 38 | break; |
vinbel93 | 13:08ef55cd14c6 | 39 | } |
vinbel93 | 13:08ef55cd14c6 | 40 | |
vinbel93 | 13:08ef55cd14c6 | 41 | LPC_UART3->LCR = 0x83; // 8 bits, no Parity, 1 Stop bit, DLAB = 1 |
vinbel93 | 13:08ef55cd14c6 | 42 | Fdiv = (pclk / 16) / baudrate; // baud rate |
vinbel93 | 13:08ef55cd14c6 | 43 | LPC_UART3->DLM = Fdiv / 256; // MSB |
vinbel93 | 13:08ef55cd14c6 | 44 | LPC_UART3->DLL = Fdiv % 256; // LSB |
vinbel93 | 13:08ef55cd14c6 | 45 | LPC_UART3->LCR = 0x03; // DLAB = 0 |
vinbel93 | 13:08ef55cd14c6 | 46 | } |
vinbel93 | 13:08ef55cd14c6 | 47 | |
vinbel93 | 13:08ef55cd14c6 | 48 | void UARTSend(uint8_t byte) |
vinbel93 | 13:08ef55cd14c6 | 49 | { |
vinbel93 | 13:08ef55cd14c6 | 50 | LPC_UART3->THR = byte; |
vinbel93 | 13:08ef55cd14c6 | 51 | } |
vinbel93 | 13:08ef55cd14c6 | 52 | |
vinbel93 | 13:08ef55cd14c6 | 53 | void UARTSend(uint8_t *buffer, uint32_t length) |
vinbel93 | 13:08ef55cd14c6 | 54 | { |
vinbel93 | 13:08ef55cd14c6 | 55 | while (length != 0) |
vinbel93 | 13:08ef55cd14c6 | 56 | { |
vinbel93 | 13:08ef55cd14c6 | 57 | LPC_UART3->THR = *buffer; |
vinbel93 | 13:08ef55cd14c6 | 58 | buffer++; |
vinbel93 | 13:08ef55cd14c6 | 59 | length--; |
vinbel93 | 13:08ef55cd14c6 | 60 | } |
vinbel93 | 13:08ef55cd14c6 | 61 | } |