From 6cab7bcfcb12003a538435f351a73978f5fbb954 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 24 Mar 2021 19:45:43 -0400 Subject: [PATCH] lpc176x: Introduce get_pclock_frequency() Add get_pclock_frequency() - a standard way of obtaining the peripheral clock frequency. Signed-off-by: Kevin O'Connor --- src/lpc176x/adc.c | 11 ++++++----- src/lpc176x/i2c.c | 5 +++-- src/lpc176x/internal.h | 1 + src/lpc176x/main.c | 10 +++++++++- src/lpc176x/serial.c | 4 ++-- src/lpc176x/spi.c | 4 ++-- 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/lpc176x/adc.c b/src/lpc176x/adc.c index 97e24c16..9eff6e53 100644 --- a/src/lpc176x/adc.c +++ b/src/lpc176x/adc.c @@ -1,10 +1,9 @@ // Analog to digital support on lpc176x // -// Copyright (C) 2018-2019 Kevin O'Connor +// Copyright (C) 2018-2021 Kevin O'Connor // // This file may be distributed under the terms of the GNU GPLv3 license. -#include "autoconf.h" // CONFIG_CLOCK_FREQ #include "board/armcm_boot.h" // armcm_enable_irq #include "board/irq.h" // irq_save #include "board/misc.h" // timer_from_us @@ -67,7 +66,8 @@ gpio_adc_setup(uint8_t pin) if (!is_enabled_pclock(PCLK_ADC)) { // Power up ADC enable_pclock(PCLK_ADC); - uint32_t prescal = DIV_ROUND_UP(CONFIG_CLOCK_FREQ, ADC_FREQ_MAX) - 1; + uint32_t pclk = get_pclock_frequency(PCLK_ADC); + uint32_t prescal = DIV_ROUND_UP(pclk, ADC_FREQ_MAX) - 1; LPC_ADC->ADCR = adc_status.adcr = (1<<21) | ((prescal & 0xff) << 8); LPC_ADC->ADINTEN = 0xff; adc_status.chan = ADC_DONE; @@ -102,8 +102,9 @@ gpio_adc_sample(struct gpio_adc g) adc_status.chan = g.chan; LPC_ADC->ADCR = adc_status.adcr | (1 << g.chan) | (1<<16); -need_delay: - return ((64 * DIV_ROUND_UP(CONFIG_CLOCK_FREQ, ADC_FREQ_MAX) +need_delay: ; + uint32_t pclk = get_pclock_frequency(PCLK_ADC); + return ((64 * DIV_ROUND_UP(pclk, ADC_FREQ_MAX) * ARRAY_SIZE(adc_status.samples)) / 4 + timer_from_us(10)); } diff --git a/src/lpc176x/i2c.c b/src/lpc176x/i2c.c index 10b45ede..4d8dc361 100644 --- a/src/lpc176x/i2c.c +++ b/src/lpc176x/i2c.c @@ -1,6 +1,6 @@ // I2C functions on lpc176x // -// Copyright (C) 2018 Kevin O'Connor +// Copyright (C) 2018-2021 Kevin O'Connor // // This file may be distributed under the terms of the GNU GPLv3 license. @@ -54,7 +54,8 @@ i2c_setup(uint32_t bus, uint32_t rate, uint8_t addr) // Set 100Khz frequency enable_pclock(info->pclk); - uint32_t pclk = SystemCoreClock, pulse = pclk / (100000 * 2); + uint32_t pclk = get_pclock_frequency(info->pclk); + uint32_t pulse = pclk / (100000 * 2); i2c->I2SCLL = pulse; i2c->I2SCLH = pulse; diff --git a/src/lpc176x/internal.h b/src/lpc176x/internal.h index a8253138..84b7ac84 100644 --- a/src/lpc176x/internal.h +++ b/src/lpc176x/internal.h @@ -19,6 +19,7 @@ #define PCLK_USB 31 int is_enabled_pclock(uint32_t pclk); void enable_pclock(uint32_t pclk); +uint32_t get_pclock_frequency(uint32_t pclk); void gpio_peripheral(uint32_t gpio, int func, int pullup); #endif // internal.h diff --git a/src/lpc176x/main.c b/src/lpc176x/main.c index 7f19f8c0..b35e31ee 100644 --- a/src/lpc176x/main.c +++ b/src/lpc176x/main.c @@ -1,9 +1,10 @@ // Main starting point for LPC176x boards. // -// Copyright (C) 2018 Kevin O'Connor +// Copyright (C) 2018-2021 Kevin O'Connor // // This file may be distributed under the terms of the GNU GPLv3 license. +#include "autoconf.h" // CONFIG_CLOCK_FREQ #include "board/armcm_boot.h" // armcm_main #include "internal.h" // enable_pclock #include "sched.h" // sched_main @@ -57,6 +58,13 @@ enable_pclock(uint32_t pclk) } } +// Return the frequency of the given peripheral clock +uint32_t +get_pclock_frequency(uint32_t pclk) +{ + return CONFIG_CLOCK_FREQ; +} + // Main entry point - called from armcm_boot.c:ResetHandler() void armcm_main(void) diff --git a/src/lpc176x/serial.c b/src/lpc176x/serial.c index 3d0432ba..63a2bd7d 100644 --- a/src/lpc176x/serial.c +++ b/src/lpc176x/serial.c @@ -1,6 +1,6 @@ // lpc176x serial port // -// Copyright (C) 2018 Kevin O'Connor +// Copyright (C) 2018-2021 Kevin O'Connor // // This file may be distributed under the terms of the GNU GPLv3 license. @@ -61,7 +61,7 @@ serial_init(void) // Setup baud LPC_UART0->LCR = (1<<7); // set DLAB bit enable_pclock(PCLK_UART0); - uint32_t pclk = SystemCoreClock; + uint32_t pclk = get_pclock_frequency(PCLK_UART0); uint32_t div = pclk / (CONFIG_SERIAL_BAUD * 16); LPC_UART0->DLL = div & 0xff; LPC_UART0->DLM = (div >> 8) & 0xff; diff --git a/src/lpc176x/spi.c b/src/lpc176x/spi.c index 2e644a1e..582fa77e 100644 --- a/src/lpc176x/spi.c +++ b/src/lpc176x/spi.c @@ -1,6 +1,6 @@ // SPI support on lpc176x // -// Copyright (C) 2018 Kevin O'Connor +// Copyright (C) 2018-2021 Kevin O'Connor // // This file may be distributed under the terms of the GNU GPLv3 license. @@ -58,7 +58,7 @@ spi_setup(uint32_t bus, uint8_t mode, uint32_t rate) // Setup clock rate and mode struct spi_config res = {spi_bus[bus].spi, 0, 0}; - uint32_t pclk = SystemCoreClock; + uint32_t pclk = get_pclock_frequency(spi_bus[bus].pclk); uint32_t div = DIV_ROUND_UP(pclk/2, rate) << 1; res.cpsr = div < 2 ? 2 : (div > 254 ? 254 : div); res.cr0 = 0x07 | ((mode & 2) << 5) | ((mode & 1) << 7);