lpc176x: Introduce get_pclock_frequency()

Add get_pclock_frequency() - a standard way of obtaining the
peripheral clock frequency.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2021-03-24 19:45:43 -04:00
parent 964e3cc456
commit 6cab7bcfcb
6 changed files with 23 additions and 12 deletions

View File

@ -1,10 +1,9 @@
// Analog to digital support on lpc176x // Analog to digital support on lpc176x
// //
// Copyright (C) 2018-2019 Kevin O'Connor <kevin@koconnor.net> // Copyright (C) 2018-2021 Kevin O'Connor <kevin@koconnor.net>
// //
// This file may be distributed under the terms of the GNU GPLv3 license. // 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/armcm_boot.h" // armcm_enable_irq
#include "board/irq.h" // irq_save #include "board/irq.h" // irq_save
#include "board/misc.h" // timer_from_us #include "board/misc.h" // timer_from_us
@ -67,7 +66,8 @@ gpio_adc_setup(uint8_t pin)
if (!is_enabled_pclock(PCLK_ADC)) { if (!is_enabled_pclock(PCLK_ADC)) {
// Power up ADC // Power up ADC
enable_pclock(PCLK_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->ADCR = adc_status.adcr = (1<<21) | ((prescal & 0xff) << 8);
LPC_ADC->ADINTEN = 0xff; LPC_ADC->ADINTEN = 0xff;
adc_status.chan = ADC_DONE; adc_status.chan = ADC_DONE;
@ -102,8 +102,9 @@ gpio_adc_sample(struct gpio_adc g)
adc_status.chan = g.chan; adc_status.chan = g.chan;
LPC_ADC->ADCR = adc_status.adcr | (1 << g.chan) | (1<<16); LPC_ADC->ADCR = adc_status.adcr | (1 << g.chan) | (1<<16);
need_delay: need_delay: ;
return ((64 * DIV_ROUND_UP(CONFIG_CLOCK_FREQ, ADC_FREQ_MAX) 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)); * ARRAY_SIZE(adc_status.samples)) / 4 + timer_from_us(10));
} }

View File

@ -1,6 +1,6 @@
// I2C functions on lpc176x // I2C functions on lpc176x
// //
// Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net> // Copyright (C) 2018-2021 Kevin O'Connor <kevin@koconnor.net>
// //
// This file may be distributed under the terms of the GNU GPLv3 license. // 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 // Set 100Khz frequency
enable_pclock(info->pclk); 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->I2SCLL = pulse;
i2c->I2SCLH = pulse; i2c->I2SCLH = pulse;

View File

@ -19,6 +19,7 @@
#define PCLK_USB 31 #define PCLK_USB 31
int is_enabled_pclock(uint32_t pclk); int is_enabled_pclock(uint32_t pclk);
void enable_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); void gpio_peripheral(uint32_t gpio, int func, int pullup);
#endif // internal.h #endif // internal.h

View File

@ -1,9 +1,10 @@
// Main starting point for LPC176x boards. // Main starting point for LPC176x boards.
// //
// Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net> // Copyright (C) 2018-2021 Kevin O'Connor <kevin@koconnor.net>
// //
// This file may be distributed under the terms of the GNU GPLv3 license. // 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 "board/armcm_boot.h" // armcm_main
#include "internal.h" // enable_pclock #include "internal.h" // enable_pclock
#include "sched.h" // sched_main #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() // Main entry point - called from armcm_boot.c:ResetHandler()
void void
armcm_main(void) armcm_main(void)

View File

@ -1,6 +1,6 @@
// lpc176x serial port // lpc176x serial port
// //
// Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net> // Copyright (C) 2018-2021 Kevin O'Connor <kevin@koconnor.net>
// //
// This file may be distributed under the terms of the GNU GPLv3 license. // This file may be distributed under the terms of the GNU GPLv3 license.
@ -61,7 +61,7 @@ serial_init(void)
// Setup baud // Setup baud
LPC_UART0->LCR = (1<<7); // set DLAB bit LPC_UART0->LCR = (1<<7); // set DLAB bit
enable_pclock(PCLK_UART0); enable_pclock(PCLK_UART0);
uint32_t pclk = SystemCoreClock; uint32_t pclk = get_pclock_frequency(PCLK_UART0);
uint32_t div = pclk / (CONFIG_SERIAL_BAUD * 16); uint32_t div = pclk / (CONFIG_SERIAL_BAUD * 16);
LPC_UART0->DLL = div & 0xff; LPC_UART0->DLL = div & 0xff;
LPC_UART0->DLM = (div >> 8) & 0xff; LPC_UART0->DLM = (div >> 8) & 0xff;

View File

@ -1,6 +1,6 @@
// SPI support on lpc176x // SPI support on lpc176x
// //
// Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net> // Copyright (C) 2018-2021 Kevin O'Connor <kevin@koconnor.net>
// //
// This file may be distributed under the terms of the GNU GPLv3 license. // 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 // Setup clock rate and mode
struct spi_config res = {spi_bus[bus].spi, 0, 0}; 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; uint32_t div = DIV_ROUND_UP(pclk/2, rate) << 1;
res.cpsr = div < 2 ? 2 : (div > 254 ? 254 : div); res.cpsr = div < 2 ? 2 : (div > 254 ? 254 : div);
res.cr0 = 0x07 | ((mode & 2) << 5) | ((mode & 1) << 7); res.cr0 = 0x07 | ((mode & 2) << 5) | ((mode & 1) << 7);