aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lpc176x/adc.c4
-rw-r--r--src/lpc176x/gpio.c14
-rw-r--r--src/lpc176x/i2c.c2
-rw-r--r--src/lpc176x/internal.h4
-rw-r--r--src/lpc176x/main.c21
-rw-r--r--src/lpc176x/serial.c2
-rw-r--r--src/lpc176x/spi.c2
-rw-r--r--src/lpc176x/timer.c4
-rw-r--r--src/lpc176x/usbserial.c2
9 files changed, 32 insertions, 23 deletions
diff --git a/src/lpc176x/adc.c b/src/lpc176x/adc.c
index d7071896..c9d2ccae 100644
--- a/src/lpc176x/adc.c
+++ b/src/lpc176x/adc.c
@@ -38,9 +38,9 @@ gpio_adc_setup(uint8_t pin)
uint32_t prescal = DIV_ROUND_UP(CONFIG_CLOCK_FREQ*4, ADC_FREQ_MAX) - 1;
uint32_t adcr = (1<<21) | ((prescal & 0xff) << 8);
- if (!(LPC_SC->PCONP & (1<<PCLK_ADC))) {
+ if (!is_enabled_pclock(PCLK_ADC)) {
// Power up ADC
- enable_peripheral_clock(PCLK_ADC);
+ enable_pclock(PCLK_ADC);
LPC_ADC->ADCR = adcr;
}
diff --git a/src/lpc176x/gpio.c b/src/lpc176x/gpio.c
index e48feb01..66b657d8 100644
--- a/src/lpc176x/gpio.c
+++ b/src/lpc176x/gpio.c
@@ -26,20 +26,6 @@ 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<<pclk;
- if (pclk < 16) {
- uint32_t shift = pclk * 2;
- LPC_SC->PCLKSEL0 = (LPC_SC->PCLKSEL0 & ~(0x3<<shift)) | (0x1<<shift);
- } else {
- uint32_t shift = (pclk - 16) * 2;
- LPC_SC->PCLKSEL1 = (LPC_SC->PCLKSEL1 & ~(0x3<<shift)) | (0x1<<shift);
- }
-}
-
// Set the mode and extended function of a pin
void
gpio_peripheral(int bank, int pin, int func, int pullup)
diff --git a/src/lpc176x/i2c.c b/src/lpc176x/i2c.c
index 58761aba..cc45ad14 100644
--- a/src/lpc176x/i2c.c
+++ b/src/lpc176x/i2c.c
@@ -29,7 +29,7 @@ i2c_init(void)
gpio_peripheral(0, 1, 3, 0);
// Set 100Khz frequency
- enable_peripheral_clock(PCLK_I2C1);
+ enable_pclock(PCLK_I2C1);
uint32_t pclk = SystemCoreClock, pulse = pclk / (100000 * 2);
LPC_I2C1->I2SCLL = pulse;
LPC_I2C1->I2SCLH = pulse;
diff --git a/src/lpc176x/internal.h b/src/lpc176x/internal.h
index 5638eed3..3e458c18 100644
--- a/src/lpc176x/internal.h
+++ b/src/lpc176x/internal.h
@@ -11,7 +11,9 @@
#define PCLK_ADC 12
#define PCLK_I2C1 19
#define PCLK_SSP0 21
-void enable_peripheral_clock(uint32_t pclk);
+#define PCLK_USB 31
+int is_enabled_pclock(uint32_t pclk);
+void enable_pclock(uint32_t pclk);
void gpio_peripheral(int bank, int pin, int func, int pullup);
#endif // internal.h
diff --git a/src/lpc176x/main.c b/src/lpc176x/main.c
index fc06bc24..82777bc1 100644
--- a/src/lpc176x/main.c
+++ b/src/lpc176x/main.c
@@ -38,6 +38,27 @@ DECL_INIT(watchdog_init);
* misc functions
****************************************************************/
+// Check if a peripheral clock has been enabled
+int
+is_enabled_pclock(uint32_t pclk)
+{
+ return !!(LPC_SC->PCONP & (1<<pclk));
+}
+
+// Enable a peripheral clock
+void
+enable_pclock(uint32_t pclk)
+{
+ LPC_SC->PCONP |= 1<<pclk;
+ if (pclk < 16) {
+ uint32_t shift = pclk * 2;
+ LPC_SC->PCLKSEL0 = (LPC_SC->PCLKSEL0 & ~(0x3<<shift)) | (0x1<<shift);
+ } else {
+ uint32_t shift = (pclk - 16) * 2;
+ LPC_SC->PCLKSEL1 = (LPC_SC->PCLKSEL1 & ~(0x3<<shift)) | (0x1<<shift);
+ }
+}
+
void
command_reset(uint32_t *args)
{
diff --git a/src/lpc176x/serial.c b/src/lpc176x/serial.c
index 1594a2ad..29a535a3 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
- enable_peripheral_clock(PCLK_UART0);
+ enable_pclock(PCLK_UART0);
uint32_t pclk = SystemCoreClock;
uint32_t div = pclk / (CONFIG_SERIAL_BAUD * 16);
LPC_UART0->DLL = div & 0xff;
diff --git a/src/lpc176x/spi.c b/src/lpc176x/spi.c
index e85f348c..726ffce3 100644
--- a/src/lpc176x/spi.c
+++ b/src/lpc176x/spi.c
@@ -24,7 +24,7 @@ spi_init(void)
gpio_peripheral(0, 18, 2, 0);
// Setup clock
- enable_peripheral_clock(PCLK_SSP0);
+ enable_pclock(PCLK_SSP0);
// Set initial registers
LPC_SSP0->CR0 = 0x07;
diff --git a/src/lpc176x/timer.c b/src/lpc176x/timer.c
index 318e8bea..69add9c3 100644
--- a/src/lpc176x/timer.c
+++ b/src/lpc176x/timer.c
@@ -8,7 +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 "internal.h" // enable_pclock
#include "sched.h" // DECL_INIT
// Set the next irq time
@@ -39,7 +39,7 @@ timer_init(void)
// Disable timer
LPC_TIM0->TCR = 0x02;
// Setup clock and prescaler (divide sys clock by 4)
- enable_peripheral_clock(PCLK_TIMER0);
+ enable_pclock(PCLK_TIMER0);
LPC_TIM0->PR = 3;
// Enable interrupts
NVIC_SetPriority(TIMER0_IRQn, 2);
diff --git a/src/lpc176x/usbserial.c b/src/lpc176x/usbserial.c
index a9a0e7eb..041c2da4 100644
--- a/src/lpc176x/usbserial.c
+++ b/src/lpc176x/usbserial.c
@@ -251,7 +251,7 @@ usbserial_init(void)
{
usb_irq_disable();
// enable power
- LPC_SC->PCONP |= (1<<31);
+ enable_pclock(PCLK_USB);
// enable clock
LPC_USB->USBClkCtrl = 0x12;
while (LPC_USB->USBClkSt != 0x12)