diff --git a/src/lpc176x/gpio.c b/src/lpc176x/gpio.c index 49363d2a..5f5f9375 100644 --- a/src/lpc176x/gpio.c +++ b/src/lpc176x/gpio.c @@ -32,6 +32,20 @@ static LPC_GPIO_TypeDef * const digital_regs[] = { * General Purpose Input Output (GPIO) pins ****************************************************************/ +// Enable a peripheral clock +void +enable_peripheral_clock(uint32_t pclk) +{ + LPC_SC->PCONP |= 1<PCLKSEL0 = (LPC_SC->PCLKSEL0 & ~(0x3<PCLKSEL1 = (LPC_SC->PCLKSEL1 & ~(0x3<PCONP & (1<<12))) { + if (!(LPC_SC->PCONP & (1<PCONP |= 1<<12; - LPC_SC->PCLKSEL0 = (LPC_SC->PCLKSEL0 & ~(0x3<<24)) | (0x1<<24); + enable_peripheral_clock(PCLK_ADC); LPC_ADC->ADCR = adcr; } diff --git a/src/lpc176x/internal.h b/src/lpc176x/internal.h index da1f169f..1ea07df4 100644 --- a/src/lpc176x/internal.h +++ b/src/lpc176x/internal.h @@ -2,6 +2,10 @@ #define __LPC176X_INTERNAL_H // Local definitions for lpc176x code +#define PCLK_TIMER0 1 +#define PCLK_UART0 3 +#define PCLK_ADC 12 +void enable_peripheral_clock(uint32_t pclk); void gpio_peripheral(int bank, int pin, int func, int pullup); #endif // internal.h diff --git a/src/lpc176x/serial.c b/src/lpc176x/serial.c index 7f2a0db9..1594a2ad 100644 --- a/src/lpc176x/serial.c +++ b/src/lpc176x/serial.c @@ -16,7 +16,7 @@ serial_init(void) { // Setup baud LPC_UART0->LCR = (1<<7); // set DLAB bit - LPC_SC->PCLKSEL0 = (LPC_SC->PCLKSEL0 & ~(0x3<<6)) | (0x1<<6); + enable_peripheral_clock(PCLK_UART0); uint32_t pclk = SystemCoreClock; uint32_t div = pclk / (CONFIG_SERIAL_BAUD * 16); LPC_UART0->DLL = div & 0xff; diff --git a/src/lpc176x/timer.c b/src/lpc176x/timer.c index 1b966d1c..318e8bea 100644 --- a/src/lpc176x/timer.c +++ b/src/lpc176x/timer.c @@ -8,6 +8,7 @@ #include "board/irq.h" // irq_disable #include "board/misc.h" // timer_read_time #include "board/timer_irq.h" // timer_dispatch_many +#include "internal.h" // enable_peripheral_clock #include "sched.h" // DECL_INIT // Set the next irq time @@ -38,7 +39,7 @@ timer_init(void) // Disable timer LPC_TIM0->TCR = 0x02; // Setup clock and prescaler (divide sys clock by 4) - LPC_SC->PCLKSEL0 = (LPC_SC->PCLKSEL0 & ~(0x3<<2)) | (0x1<<2); + enable_peripheral_clock(PCLK_TIMER0); LPC_TIM0->PR = 3; // Enable interrupts NVIC_SetPriority(TIMER0_IRQn, 2);