Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
hugozijlmans
Date:
Wed Dec 01 21:12:54 2010 +0000
Child:
1:2c52307d223f
Commit message:
Basic Timer0 implementation to update MC_PWM

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 01 21:12:54 2010 +0000
@@ -0,0 +1,220 @@
+#include "LPC17xx.h"
+#include "core_cm3.h"
+#include "cmsis_nvic.h"
+
+// Define the register names that haven't been defined
+#define MCLIM0 LPC_MCPWM->MCPER0
+#define MCLIM1 LPC_MCPWM->MCPER1
+#define MCLIM2 LPC_MCPWM->MCPER2
+#define PCLKSEL0 LPC_SC->PCLKSEL0
+#define PCLKSEL1 LPC_SC->PCLKSEL1
+#define T0MR0 LPC_TIM0->MR0
+#define T0PR LPC_TIM0->PR
+#define T0TCR LPC_TIM0->TCR
+#define T0CTCR LPC_TIM0->CTCR
+#define T0MCR LPC_TIM0->MCR
+#define T0IR LPC_TIM0->IR
+#define FIOSET LPC_GPIO1->FIOSET
+#define FIOCLR LPC_GPIO1->FIOCLR
+#define FIOPIN LPC_GPIO1->FIOPIN
+#define FIODIR LPC_GPIO1->FIODIR
+#define PCONP LPC_SC->PCONP
+#define CLKSRCSEL LPC_SC->CLKSRCSEL
+#define PLL0CFG LPC_SC->PLL0CFG
+#define PLL0CON LPC_SC->PLL0CON
+#define CCLKCFG LPC_SC->CCLKCFG
+#define PLL0FEED LPC_SC->PLL0FEED
+#define PLL0STAT LPC_SC->PLL0STAT
+
+// Other defines
+#define SYSCLK 96000000
+#define EXTCLK 12000000
+#define PLLCLK 288000000
+#define PLLMULT (PLLCLK/2/EXTCLK)-1
+#define TIMER0_FREQ 2
+#define TIMER0_DIV (SYSCLK/4/TIMER0_FREQ)-1
+#define LED_MASK 0x00B40000
+#define LED1 0x00040000
+#define LED2 0x00100000
+#define LED3 0x00200000
+#define LED4 0x00800000
+
+void Sys_init(void);
+void Timer0_interrupt_enable(void);
+void Timer0_interrupt_disable(void);
+void Timer0_IRQHandler(void);
+void Timer0_init(void);
+void PWM_init(void);
+void LED_init(void);
+int  main(void);
+void dispatcher(void);
+
+int Timer0_interrupt_flag=0;
+
+void Sys_init(void) {
+
+    // Set main clk as main oscillator
+    CLKSRCSEL = 1;
+
+    // Disconnect the PLL0
+    PLL0CON &= ~(1<<1); /* Disconnect the main PLL (PLL0) */
+
+    // Feed the PLL
+    PLL0FEED = 0xAA;
+    PLL0FEED = 0x55;
+
+    // Wait for main PLL (PLL0) to disconnect
+    while ((PLL0STAT & (1<<25)) != 0x00);
+
+    // Turn off the main PLL (PLL0)
+    PLL0CON &= ~(1<<0);
+
+    // Feed the PLL
+    PLL0FEED = 0xAA;
+    PLL0FEED = 0x55;
+
+    // Wait for main PLL (PLL0) to shut down
+    while ((PLL0STAT & (1<<24)) != 0x00);
+
+    // Set PLL0 multiplier
+    PLL0CFG = PLLMULT;
+
+    // Feed the PLL
+    PLL0FEED = 0xAA;
+    PLL0FEED = 0x55;
+
+    // Turn on the main PLL (PLL0)
+    PLL0CON |= 1<<0;
+
+    // Feed the PLL
+    PLL0FEED = 0xAA;
+    PLL0FEED = 0x55;
+
+    // Wait for main PLL (PLL0) to come up
+    while ((PLL0STAT & (1<<24)) == 0x00);
+
+    // Set CPU clock divider
+    CCLKCFG = 2;
+
+    // Wait for PLOCK0 to become 1
+    while ((PLL0STAT & (1<<26)) == 0x00);
+
+    // Connect to the main PLL (PLL0)
+    PLL0CON |= 1<<1;
+
+    // Feed the PLL
+    PLL0FEED = 0xAA;
+    PLL0FEED = 0x55;
+
+    // Wait for main PLL (PLL0) to connect
+    while ((PLL0STAT & (1<<25)) == 0x00);
+
+}
+
+void Timer0_interrupt_enable(void) {
+
+    // Enable Timer0 interrupt
+    NVIC_EnableIRQ(TIMER0_IRQn);
+}
+
+void Timer0_interrupt_disable(void) {
+
+    // Disable Timer0 interrupt
+    NVIC_DisableIRQ(TIMER0_IRQn);
+}
+
+void Timer0_IRQHandler(void) {
+
+    // Determine the cause of the interrupt
+    T0IR |= T0IR;
+
+    // Clear the Timer0 interrupt
+    NVIC_ClearPendingIRQ(TIMER0_IRQn);
+
+    //Set Timer0 interrupt flag
+    Timer0_interrupt_flag = 1;
+}
+
+void Timer0_init(void) {
+
+    // Set Timer0 interrupt priority
+    NVIC_SetPriority(TIMER0_IRQn, 10);
+
+    // Power the timer0
+    PCONP |= 1 << 1;
+
+    // Reset counter
+    T0TCR = 2;
+
+    // Timer0 perhiperal clock select (01 = CCLK)
+    //PCLKSEL0 &= ~0xC;
+    //PCLKSEL0 |= 0x4;
+
+    // Timer0 overflow value
+    T0MR0 = TIMER0_DIV;
+
+    // Set the prescale register 0
+    T0PR = 0;
+
+    // Timer0 is incremented when the prescale register =<
+    T0CTCR = 0x0;
+
+    // Set the timer0 reset after reaching T0MR0 and interrupt
+    // T0MCR &= ~0x7;
+    T0MCR = 0x3;
+
+    // Enable the counter
+    T0TCR = 1 << 0;
+
+    // Connect the Timer0 interrupt to the interrupt handler
+    NVIC_SetVector(TIMER0_IRQn, (uint32_t)&Timer0_IRQHandler);
+
+    Timer0_interrupt_enable();
+}
+
+void PWM_init(void) {
+
+    // Make the timer overload at 2000 (96MHz / 2000 = 48kHz)
+    // Set the value to 1999 --> hex 7CF
+    MCLIM0 = 0x000007CF;
+    MCLIM1 = 0x000007CF;
+    MCLIM2 = 0x000007CF;
+
+}
+
+void LED_init(void) {
+
+    // Set output direction for LEDs
+    FIODIR |= LED_MASK;
+
+}
+
+int main(void) {
+
+    LED_init();
+
+    Timer0_init();
+
+    Sys_init();
+
+    //PWM_init();
+
+    dispatcher();
+
+}
+
+void dispatcher(void) {
+
+    while (1) {
+
+        if (Timer0_interrupt_flag == 1) {
+            if ((FIOPIN & LED1)==LED1)
+                FIOCLR |= LED1;
+            else
+                FIOSET |= LED1;
+
+            Timer0_interrupt_flag = 0;
+        }
+    }
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Dec 01 21:12:54 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e