Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kinetis: Set LPUART clock source during uart_lpuart_init #8724

Merged
merged 2 commits into from
Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions cpu/kinetis/include/cpu_conf_kinetis.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ extern "C"
* @name ARM Cortex-M specific CPU configuration
* @{
*/
#define CPU_DEFAULT_IRQ_PRIO (1U)
#define CPU_IRQ_NUMOF (NUMBER_OF_INT_VECTORS)
#define CPU_FLASH_BASE (0x00000000)
#define CPU_DEFAULT_IRQ_PRIO (1U)
#define CPU_IRQ_NUMOF (NUMBER_OF_INT_VECTORS)
#define CPU_FLASH_BASE (0x00000000)
/** @} */

/**
* @name GPIO pin mux function numbers
* @{
*/
#define PIN_MUX_FUNCTION_ANALOG 0
#define PIN_MUX_FUNCTION_GPIO 1
#define PIN_MUX_FUNCTION_GPIO 1
/** @} */

/**
* @name GPIO interrupt flank settings
* @{
*/
#define PIN_INTERRUPT_RISING 0b1001
#define PIN_INTERRUPT_FALLING 0b1010
#define PIN_INTERRUPT_EDGE 0b1011
#define PIN_INTERRUPT_RISING 0b1001
#define PIN_INTERRUPT_FALLING 0b1010
#define PIN_INTERRUPT_EDGE 0b1011
/** @} */

/**
Expand All @@ -61,22 +61,27 @@ extern "C"
* matters for the RIOT driver implementations.
*/
#if !defined(MCG_C2_RANGE0) && defined(MCG_C2_RANGE)
#define MCG_C2_RANGE0(x) MCG_C2_RANGE(x)
#define MCG_C2_RANGE0(x) MCG_C2_RANGE(x)
#endif
#if !defined(MCG_C2_RANGE0_MASK) && defined(MCG_C2_RANGE_MASK)
#define MCG_C2_RANGE0_MASK MCG_C2_RANGE_MASK
#define MCG_C2_RANGE0_MASK MCG_C2_RANGE_MASK
#endif
#if !defined(MCG_C7_OSCSEL) && defined(MCG_C7_OSCSEL_SHIFT)
#define MCG_C7_OSCSEL(x) (((uint32_t)(x) << MCG_C7_OSCSEL_SHIFT) & MCG_C7_OSCSEL_MASK)
#define MCG_C7_OSCSEL(x) (((uint32_t)(x) << MCG_C7_OSCSEL_SHIFT) & MCG_C7_OSCSEL_MASK)
#endif
#if !defined(OSC0) && defined(OSC)
#define OSC0 OSC
#endif
#if !defined(SIM_SOPT2_LPUART0SRC_MASK) && defined(SIM_SOPT2_LPUARTSRC_MASK)
#define SIM_SOPT2_LPUART0SRC_MASK SIM_SOPT2_LPUARTSRC_MASK
#define SIM_SOPT2_LPUART0SRC_SHIFT SIM_SOPT2_LPUARTSRC_SHIFT
#define SIM_SOPT2_LPUART0SRC SIM_SOPT2_LPUARTSRC
#endif
#if !defined(SIM_SCGC5_LPTMR_SHIFT) && defined(SIM_SCGC5_LPTIMER_SHIFT)
#define SIM_SCGC5_LPTMR_SHIFT SIM_SCGC5_LPTIMER_SHIFT
#define SIM_SCGC5_LPTMR_SHIFT SIM_SCGC5_LPTIMER_SHIFT
#endif
#if !defined(SIM_SCGC5_LPTMR_MASK) && defined(SIM_SCGC5_LPTIMER_MASK)
#define SIM_SCGC5_LPTMR_MASK SIM_SCGC5_LPTIMER_MASK
#define SIM_SCGC5_LPTMR_MASK SIM_SCGC5_LPTIMER_MASK
#endif
#if !defined(GPIOA_BASE) && defined(PTA_BASE)
#define GPIOA_BASE PTA_BASE
Expand Down
23 changes: 22 additions & 1 deletion cpu/kinetis/periph/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
#define LPUART_OVERSAMPLING_RATE (16)
#endif

/* Default LPUART clock setting to avoid compilation failures, define this in
* periph_conf.h to set board specific configuration if using the LPUART. */
#ifndef LPUART_0_SRC
#define LPUART_0_SRC 0
#endif
#ifndef LPUART_1_SRC
#define LPUART_1_SRC 0
#endif

/**
* @brief Runtime configuration space, holds pointers to callback functions for RX
*/
Expand Down Expand Up @@ -296,7 +305,19 @@ static inline void uart_init_lpuart(uart_t uart, uint32_t baudrate)
LPUART_Type *dev = uart_config[uart].dev;
uint32_t clk = uart_config[uart].freq;

/* Remember to select a module clock in board_init! (SIM->SOPT2[LPUART0SRC]) */
/* Set LPUART clock source */
#ifdef SIM_SOPT2_LPUART0SRC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value of this definition doesn't matter? It only needs to be defined to execute the code below? It's just because above there is:

#define SIM_SOPT2_LPUART0SRC SIM_SOPT2_LPUARTSRC

thus the value might be needed or evaluated.

Copy link
Member Author

@jnohlgard jnohlgard Mar 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are defined by the vendor headers for the CPUs which have the appropriate hardware module. The definition in cpu_conf_kinetis.h is only a compatibility definition for some CPU headers which have named the bit field LPUARTSRC instead of LPUART0SRC. See the other definitions in the same area in cpu_conf_kinetis.h.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I got it. Thanks!

if (dev == LPUART0) {
SIM->SOPT2 = (SIM->SOPT2 & ~SIM_SOPT2_LPUART0SRC_MASK) |
SIM_SOPT2_LPUART0SRC(LPUART_0_SRC);
}
#endif
#ifdef SIM_SOPT2_LPUART1SRC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I see a define for SIM_SOPT2_LPUART0SRC, I can't find for SIM_SOPT2_LPUART1SRC. Is this intentional? Same for SIM_SOPT2_LPUART1SRC_MASK used below, since there is a definition for SIM_SOPT2_LPUART0SRC_MASK.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LPUART1SRC only exist for CPUs which have two or more lpuart modules (LPUART0, LPUART1). Therefore we need the preprocessor conditional to avoid compilation errors on the CPUs which don't have these modules.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see, that's right for me.

if (dev == LPUART1) {
SIM->SOPT2 = (SIM->SOPT2 & ~SIM_SOPT2_LPUART1SRC_MASK) |
SIM_SOPT2_LPUART1SRC(LPUART_1_SRC);
}
#endif

/* Select mode */
/* transmitter and receiver disabled */
Expand Down