aboutsummaryrefslogtreecommitdiffstats
path: root/src/atsam/spi.c
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2021-11-20 13:47:17 -0500
committerKevin O'Connor <kevin@koconnor.net>2021-11-20 13:52:16 -0500
commitbb08dc7ae91488a6e9e1e88de28bd43be9fa5651 (patch)
tree0f8255ecf3f5d4bd4a9b66db302795071533ba64 /src/atsam/spi.c
parent92ca1119868abbd859c8f7a3f88143cf53561eb3 (diff)
downloadkutter-bb08dc7ae91488a6e9e1e88de28bd43be9fa5651.tar.gz
kutter-bb08dc7ae91488a6e9e1e88de28bd43be9fa5651.tar.xz
kutter-bb08dc7ae91488a6e9e1e88de28bd43be9fa5651.zip
atsam: Add get_pclock_frequency() helper function
Add get_pclock_frequency() and use it to calculate peripheral clocks. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src/atsam/spi.c')
-rw-r--r--src/atsam/spi.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/atsam/spi.c b/src/atsam/spi.c
index d64f0df7..e32085df 100644
--- a/src/atsam/spi.c
+++ b/src/atsam/spi.c
@@ -123,19 +123,18 @@ spihw_setup(uint32_t bus, uint8_t mode, uint32_t rate)
// Make sure bus is enabled
spihw_init(bus);
- uint32_t config = 0;
- uint32_t clockDiv;
- if (rate < (CHIP_FREQ_CPU_MAX / 255)) {
+ uint32_t clockDiv, pclk = get_pclock_frequency(ID_USART0);
+ if (rate < pclk / 255) {
clockDiv = 255;
- } else if (rate >= (CHIP_FREQ_CPU_MAX / 2)) {
+ } else if (rate >= pclk / 2) {
clockDiv = 2;
} else {
- clockDiv = (CHIP_FREQ_CPU_MAX / (rate + 1)) + 1;
+ clockDiv = pclk / (rate + 1) + 1;
}
/****** Will be written to SPI_CSRx register ******/
// CSAAT : Chip Select Active After Transfer
- config = SPI_CSR_CSAAT;
+ uint32_t config = SPI_CSR_CSAAT;
config |= SPI_CSR_BITS_8_BIT; // TODO: support for SPI_CSR_BITS_16_BIT
// NOTE: NCPHA is inverted, CPHA normal!!
switch(mode) {
@@ -208,7 +207,8 @@ usart_setup(uint32_t bus, uint8_t mode, uint32_t rate)
p_usart->US_CR = US_CR_RSTTX | US_CR_RSTRX | US_CR_TXDIS | US_CR_RXDIS;
- uint32_t br = DIV_ROUND_UP(CHIP_FREQ_CPU_MAX, rate);
+ uint32_t pclk = get_pclock_frequency(ID_USART0);
+ uint32_t br = DIV_ROUND_UP(pclk, rate);
p_usart->US_BRGR = br << US_BRGR_CD_Pos;
uint32_t reg = US_MR_CHRL_8_BIT |