Revision 2:f034e862af1f, committed 2010-12-02
- Comitter:
- hugozijlmans
- Date:
- Thu Dec 02 20:55:45 2010 +0000
- Parent:
- 1:2c52307d223f
- Commit message:
- Added UART0 support over USB mBed1768
Changed in this revision
diff -r 2c52307d223f -r f034e862af1f LPC1768.h
--- a/LPC1768.h Thu Dec 02 12:32:18 2010 +0000
+++ b/LPC1768.h Thu Dec 02 20:55:45 2010 +0000
@@ -10,8 +10,16 @@
// Perhiperal clock select
#define PCLKSEL0 LPC_SC->PCLKSEL0
+#define PCLK_UART1_1 1 << 9
+#define PCLK_UART1_0 1 << 8
+#define PCLK_UART0_1 1 << 7
+#define PCLK_UART0_0 1 << 6
+#define PCLK_TIMER1_1 1 << 5
+#define PCLK_TIMER1_0 1 << 4
#define PCLK_TIMER0_1 1 << 3
#define PCLK_TIMER0_0 1 << 2
+#define PCLK_WDT_1 1 << 1
+#define PCLK_WDT_0 1 << 0
#define PCLKSEL1 LPC_SC->PCLKSEL1
#define PCLK_RIT_1 1 << 27
#define PCLK_RIT_0 1 << 26
@@ -20,12 +28,17 @@
#define PCONP LPC_SC->PCONP
#define PCTIM0 1 << 1
#define PCTIM1 1 << 2
+#define PCUART0 1 << 3
+#define PCUART1 1 << 4
#define PCRIT 1 << 16
// CLKOUT
#define CLKSRCSEL LPC_SC->CLKSRCSEL
#define CCLKCFG LPC_SC->CCLKCFG
+// Pin output/input function selection
+#define PINSEL0 LPC_PINCON->PINSEL0
+
// FI0
#define FIOSET LPC_GPIO1->FIOSET
#define FIOCLR LPC_GPIO1->FIOCLR
@@ -52,8 +65,18 @@
#define T0IR_CR0 1 << 4
#define T0IR_CR1 1 << 5
+// UART0
+#define U0LCR LPC_UART0->LCR
+#define U0DLL LPC_UART0->DLL
+#define U0DLM LPC_UART0->DLM
+#define U0FDR LPC_UART0->FDR
+#define U0FCR LPC_UART0->FCR
+#define U0THR LPC_UART0->THR
+#define U0RBR LPC_UART0->RBR
+#define U0IER LPC_UART0->IER
+#define U0IIR LPC_UART0->IIR
+
// RIT
-
#define RICOMPVAL LPC_RIT->RICOMPVAL
#define RICTRL LPC_RIT->RICTRL
#define RITINT 1 << 0
diff -r 2c52307d223f -r f034e862af1f MCPWM.c
--- a/MCPWM.c Thu Dec 02 12:32:18 2010 +0000
+++ b/MCPWM.c Thu Dec 02 20:55:45 2010 +0000
@@ -3,13 +3,14 @@
#include "cmsis_nvic.h"
#include "MCPWM.h"
#include "LPC1768.h"
+#include "main.h"
void MCPWM_init(void) {
// Make the timer overload at 2000 (96MHz / 2000 = 48kHz)
// Set the value to 1999 --> hex 7CF
- MCLIM0 = 0x000007CF;
- MCLIM1 = 0x000007CF;
- MCLIM2 = 0x000007CF;
+ MCLIM0 = MCPWM_DIV;
+ MCLIM1 = MCPWM_DIV;
+ MCLIM2 = MCPWM_DIV;
}
\ No newline at end of file
diff -r 2c52307d223f -r f034e862af1f UART0.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/UART0.c Thu Dec 02 20:55:45 2010 +0000
@@ -0,0 +1,143 @@
+#include "LPC17xx.h"
+#include "core_cm3.h"
+#include "cmsis_nvic.h"
+#include "UART0.h"
+#include "main.h"
+#include "LPC1768.h"
+
+void UART0_interrupt_enable(void) {
+
+ // Enable UART0 interrupt
+ NVIC_EnableIRQ(UART0_IRQn);
+}
+
+void UART0_interrupt_disable(void) {
+
+ // Disable UART0 interrupt
+ NVIC_DisableIRQ(UART0_IRQn);
+}
+
+void UART0_IRQHandler(void) {
+
+ // Determine the cause of the interrupt
+
+ // If THRE then buffer is empty
+ if ((U0IIR & 0x2) == 0x2)
+ i_flags.UART0_tx_idle = 1;
+
+ // If receive buffer contains a byte
+ if ((U0IIR & 0x8) == 0x8) {
+ i_flags.UART0_rx_idle = 0;
+ UART0_rx_interrupt_disable();
+ }
+
+ // Clear the UART0 interrupt
+ NVIC_ClearPendingIRQ(UART0_IRQn);
+
+}
+
+void UART0_init(void) {
+
+ // Set UART0 interrupt priority
+ NVIC_SetPriority(UART0_IRQn, 12);
+
+ // Set RXD0/TXD0 to function UART0
+ PINSEL0 &= ~(0xF0);
+ PINSEL0 |= 0x50;
+
+ // Select the UART0 clk
+ UART0_select_clk();
+
+ // Power the UART0
+ PCONP |= PCUART0;
+
+ // Set the UART0 Line Control Register
+ U0LCR &= 0;
+ U0LCR |= (1 << 1) | (1 << 0) // 8 bit
+ | (1 << 2) // 1 stop bit
+ | (0 << 3) // disable parity
+ | (1 << 5) | (0 << 4) // force 1 stick parity
+ | (0 << 6) // disable break control
+ | (1 << 7); // divisor access bit enable
+
+ // Set Baudrate 115200
+ // DIVADDVAL 3
+ // MULVAL 10
+ // DLL 40
+ // DLM 0
+ // Error 0.1603%
+
+ // Set Fractional Divider Register
+ U0FDR = 0xA3;
+
+ // Set Baudrate divisor
+ U0DLL = 40;
+ U0DLM = 0;
+
+ // Set divisor access bit 0
+ U0LCR &= ~(1 << 7);
+
+ // Set Fifo control register
+ U0FCR &= 0;
+ U0FCR |= (1 << 7) | (1 << 6) // RX Fifo DMA @ 14 bytes
+ | (1 << 3) // 1 stop bit
+ | (1 << 2) // disable parity
+ | (1 << 1) // disable break control
+ | (1 << 0); // divisor access bit enable
+
+ // Set interrupt enable sources THRE & RBR
+ U0IER |= 3;
+
+ // Set that the TX is idle
+ i_flags.UART0_tx_idle = 1;
+ // Set that the RX is idle
+ i_flags.UART0_rx_idle = 1;
+
+ // Connect the UART0 interrupt to the interrupt handler
+ NVIC_SetVector(UART0_IRQn, (uint32_t)&UART0_IRQHandler);
+
+ UART0_interrupt_enable();
+
+}
+
+void UART0_select_clk(void) {
+
+ // Including work-around described in errata.lpc1768.pdf R04
+
+ PLL0_disconnect();
+
+ PLL0_disable();
+
+ // Timer0 perhiperal clock select (01 = CCLK)
+ PCLKSEL0 &= ~(PCLK_UART0_1 | PCLK_UART0_0);
+ PCLKSEL0 |= PCLK_UART0_0;
+
+ PLL0_enable();
+
+ PLL0_connect();
+
+}
+
+void UART0_write(uint8_t byte) {
+
+ // Send byte to writebuffer
+ U0THR = byte;
+}
+
+uint8_t UART0_read(void) {
+
+ // Read the RX FIFO
+ uint8_t byte = U0RBR;
+
+ UART0_rx_interrupt_enable();
+
+ return byte;
+}
+
+void UART0_rx_interrupt_disable(void) {
+ U0IER &= ~(1);
+}
+
+void UART0_rx_interrupt_enable(void) {
+ U0IER |= 1;
+}
\ No newline at end of file
diff -r 2c52307d223f -r f034e862af1f UART0.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/UART0.h Thu Dec 02 20:55:45 2010 +0000
@@ -0,0 +1,14 @@
+#ifndef __UART0_h
+#define __UART0_h
+
+extern void UART0_interrupt_enable(void);
+extern void UART0_interrupt_disable(void);
+extern void UART0_IRQHandler(void);
+extern void UART0_init(void);
+extern void UART0_select_clk(void);
+extern void UART0_write(uint8_t byte);
+extern uint8_t UART0_read(void);
+extern void UART0_rx_interrupt_disable(void);
+extern void UART0_rx_interrupt_enable(void);
+
+#endif
\ No newline at end of file
diff -r 2c52307d223f -r f034e862af1f main.c
--- a/main.c Thu Dec 02 12:32:18 2010 +0000
+++ b/main.c Thu Dec 02 20:55:45 2010 +0000
@@ -6,6 +6,7 @@
#include "LPC1768.h"
#include "MCPWM.h"
#include "RIT.h"
+#include "UART0.h"
// BSP defines see main.h
@@ -24,13 +25,16 @@
// Set the timer0, enable it and connect to ISR
Timer0_init();
-
+
// Set the RIT, enable it and connect to ISR
RIT_init();
// Initialize MCPWM
//MCPWM_init();
+ // Initialize UART0
+ UART0_init();
+
// Start the dispatcher
dispatcher();
@@ -38,27 +42,35 @@
void dispatcher(void) {
+ int byte;
+
while (1) {
// When the timer0 interrupt occurred
if (i_flags.Timer0_int == 1) {
- if ((FIOPIN & LED1)==LED1)
- FIOCLR |= LED1;
- else
- FIOSET |= LED1;
+ i_flags.Timer0_int = 0;
+ }
- i_flags.Timer0_int = 0;
+ // When the RIT interrupt occurred
+ if (i_flags.RIT_int == 1) {
+ i_flags.RIT_int = 0;
+ }
+
+ // When UART0 TX is idle
+ if (i_flags.UART0_tx_idle == 1) {
+ i_flags.UART0_tx_idle = 0;
}
- // When the RIT interrupt occurred
- if (i_flags.RIT_int == 1) {
- if ((FIOPIN & LED2)==LED2)
- FIOCLR |= LED2;
+ // When UART0 RX isn't idle
+ if (i_flags.UART0_rx_idle == 0) {
+ i_flags.UART0_rx_idle = 1;
+ byte = UART0_read();
+ if(byte == 'a')
+ FIOSET |= LED1;
else
- FIOSET |= LED2;
-
- i_flags.RIT_int = 0;
- }
+ FIOCLR |= LED1;
+ UART0_write(byte);
+ }
}
}
\ No newline at end of file
diff -r 2c52307d223f -r f034e862af1f main.h
--- a/main.h Thu Dec 02 12:32:18 2010 +0000
+++ b/main.h Thu Dec 02 20:55:45 2010 +0000
@@ -8,8 +8,10 @@
#define PLLMULT (PLLCLK/2/EXTCLK)-1
#define TIMER0_FREQ 2
#define TIMER0_DIV (SYSCLK/TIMER0_FREQ)-1
-#define RIT_FREQ 2
+#define RIT_FREQ 10
#define RIT_DIV (SYSCLK/RIT_FREQ)-1
+#define MCPWM_FREQ 48000
+#define MCPWM_DIV (SYSCLK/MCPWM_FREQ)-1
#define LED_MASK 0x00B40000
#define LED1 0x00040000
#define LED2 0x00100000
@@ -22,6 +24,8 @@
{
unsigned Timer0_int:1;
unsigned RIT_int:1;
+ unsigned UART0_tx_idle:1;
+ unsigned UART0_rx_idle:1;
};
unsigned char All_Flags;
} Tinterrupt_flags;