From d921781a45a5e2f57e56bf59872e1d2aec2fb71f Mon Sep 17 00:00:00 2001 From: Konstantin Oblaukhov Date: Sun, 7 Oct 2018 18:34:01 +0700 Subject: KINETIS platform update for ChibiOS 18.2.x. --- os/hal/ports/KINETIS/K20x/hal_pwm_lld.c | 390 ------------------- os/hal/ports/KINETIS/K20x/hal_pwm_lld.h | 270 ------------- os/hal/ports/KINETIS/K20x/hal_spi_lld.c | 539 -------------------------- os/hal/ports/KINETIS/K20x/hal_spi_lld.h | 261 ------------- os/hal/ports/KINETIS/K20x/platform.mk | 48 ++- os/hal/ports/KINETIS/K60x/platform.mk | 47 ++- os/hal/ports/KINETIS/KL2x/hal_pwm_lld.c | 388 ------------------ os/hal/ports/KINETIS/KL2x/hal_pwm_lld.h | 305 --------------- os/hal/ports/KINETIS/KL2x/platform.mk | 46 ++- os/hal/ports/KINETIS/LLD/ADCv1/driver.mk | 9 + os/hal/ports/KINETIS/LLD/FTMv1/driver.mk | 9 + os/hal/ports/KINETIS/LLD/FTMv1/hal_pwm_lld.c | 390 +++++++++++++++++++ os/hal/ports/KINETIS/LLD/FTMv1/hal_pwm_lld.h | 270 +++++++++++++ os/hal/ports/KINETIS/LLD/GPIOv1/driver.mk | 9 + os/hal/ports/KINETIS/LLD/GPIOv1/hal_pal_lld.h | 5 + os/hal/ports/KINETIS/LLD/I2Cv1/driver.mk | 9 + os/hal/ports/KINETIS/LLD/I2Cv1/hal_i2c_lld.c | 6 +- os/hal/ports/KINETIS/LLD/PITv1/driver.mk | 9 + os/hal/ports/KINETIS/LLD/PORTv1/driver.mk | 9 + os/hal/ports/KINETIS/LLD/SDHCv1/driver.mk | 9 + os/hal/ports/KINETIS/LLD/SPIv1/driver.mk | 9 + os/hal/ports/KINETIS/LLD/SPIv1/hal_spi_lld.c | 539 ++++++++++++++++++++++++++ os/hal/ports/KINETIS/LLD/SPIv1/hal_spi_lld.h | 261 +++++++++++++ os/hal/ports/KINETIS/LLD/TPMv1/driver.mk | 9 + os/hal/ports/KINETIS/LLD/TPMv1/hal_pwm_lld.c | 388 ++++++++++++++++++ os/hal/ports/KINETIS/LLD/TPMv1/hal_pwm_lld.h | 305 +++++++++++++++ os/hal/ports/KINETIS/LLD/UARTv1/driver.mk | 9 + os/hal/ports/KINETIS/LLD/USBHSv1/driver.mk | 9 + os/hal/ports/KINETIS/MK66F18/hal_pwm_lld.c | 390 ------------------- os/hal/ports/KINETIS/MK66F18/hal_pwm_lld.h | 270 ------------- os/hal/ports/KINETIS/MK66F18/hal_spi_lld.c | 539 -------------------------- os/hal/ports/KINETIS/MK66F18/hal_spi_lld.h | 261 ------------- os/hal/ports/KINETIS/MK66F18/platform.mk | 50 ++- 33 files changed, 2383 insertions(+), 3684 deletions(-) delete mode 100644 os/hal/ports/KINETIS/K20x/hal_pwm_lld.c delete mode 100644 os/hal/ports/KINETIS/K20x/hal_pwm_lld.h delete mode 100644 os/hal/ports/KINETIS/K20x/hal_spi_lld.c delete mode 100644 os/hal/ports/KINETIS/K20x/hal_spi_lld.h delete mode 100644 os/hal/ports/KINETIS/KL2x/hal_pwm_lld.c delete mode 100644 os/hal/ports/KINETIS/KL2x/hal_pwm_lld.h create mode 100644 os/hal/ports/KINETIS/LLD/ADCv1/driver.mk create mode 100644 os/hal/ports/KINETIS/LLD/FTMv1/driver.mk create mode 100644 os/hal/ports/KINETIS/LLD/FTMv1/hal_pwm_lld.c create mode 100644 os/hal/ports/KINETIS/LLD/FTMv1/hal_pwm_lld.h create mode 100644 os/hal/ports/KINETIS/LLD/GPIOv1/driver.mk create mode 100644 os/hal/ports/KINETIS/LLD/I2Cv1/driver.mk create mode 100644 os/hal/ports/KINETIS/LLD/PITv1/driver.mk create mode 100644 os/hal/ports/KINETIS/LLD/PORTv1/driver.mk create mode 100644 os/hal/ports/KINETIS/LLD/SDHCv1/driver.mk create mode 100644 os/hal/ports/KINETIS/LLD/SPIv1/driver.mk create mode 100644 os/hal/ports/KINETIS/LLD/SPIv1/hal_spi_lld.c create mode 100644 os/hal/ports/KINETIS/LLD/SPIv1/hal_spi_lld.h create mode 100644 os/hal/ports/KINETIS/LLD/TPMv1/driver.mk create mode 100644 os/hal/ports/KINETIS/LLD/TPMv1/hal_pwm_lld.c create mode 100644 os/hal/ports/KINETIS/LLD/TPMv1/hal_pwm_lld.h create mode 100644 os/hal/ports/KINETIS/LLD/UARTv1/driver.mk create mode 100644 os/hal/ports/KINETIS/LLD/USBHSv1/driver.mk delete mode 100644 os/hal/ports/KINETIS/MK66F18/hal_pwm_lld.c delete mode 100644 os/hal/ports/KINETIS/MK66F18/hal_pwm_lld.h delete mode 100644 os/hal/ports/KINETIS/MK66F18/hal_spi_lld.c delete mode 100644 os/hal/ports/KINETIS/MK66F18/hal_spi_lld.h (limited to 'os/hal') diff --git a/os/hal/ports/KINETIS/K20x/hal_pwm_lld.c b/os/hal/ports/KINETIS/K20x/hal_pwm_lld.c deleted file mode 100644 index f5a8d96..0000000 --- a/os/hal/ports/KINETIS/K20x/hal_pwm_lld.c +++ /dev/null @@ -1,390 +0,0 @@ -/* - ChibiOS/HAL - Copyright (C) 2014 Adam J. Porter - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -/** - * @file K20x/pwm_lld.c - * @brief KINETIS PWM subsystem low level driver source. - * - * @addtogroup PWM - * @{ - */ - -#include "hal.h" - -#if HAL_USE_PWM || defined(__DOXYGEN__) - -/*===========================================================================*/ -/* Driver local definitions. */ -/*===========================================================================*/ - - -/*===========================================================================*/ -/* Driver exported variables. */ -/*===========================================================================*/ - -/** - * @brief PWMD1 driver identifier. - * @note The driver PWMD1 allocates the timer FTM0 when enabled. - */ -#if KINETIS_PWM_USE_FTM0 || defined(__DOXYGEN__) -PWMDriver PWMD1; -#endif - -/** - * @brief PWMD2 driver identifier. - * @note The driver PWMD2 allocates the timer FTM1 when enabled. - */ -#if KINETIS_PWM_USE_FTM1 || defined(__DOXYGEN__) -PWMDriver PWMD2; -#endif - -/** - * @brief PWMD3 driver identifier. - * @note The driver PWMD3 allocates the timer FTM2 when enabled. - */ -#if KINETIS_PWM_USE_FTM2 || defined(__DOXYGEN__) -PWMDriver PWMD3; -#endif - -/*===========================================================================*/ -/* Driver local variables and types. */ -/*===========================================================================*/ - -/*===========================================================================*/ -/* Driver local functions. */ -/*===========================================================================*/ - -static void pwm_lld_serve_interrupt(PWMDriver *pwmp) { - uint32_t sr; - - sr = pwmp->ftm->SC; - pwmp->ftm->SC = sr&(~FTM_SC_TOF); - - if (((sr & FTM_SC_TOF) != 0) && /* Timer Overflow */ - ((sr & FTM_SC_TOIE) != 0) && - (pwmp->config->callback != NULL)) { - pwmp->config->callback(pwmp); - } - - uint8_t n=0; - for(n=0;nchannels;n++) { - sr = pwmp->ftm->CHANNEL[n].CnSC; - pwmp->ftm->CHANNEL[n].CnSC = sr&(~FTM_CnSC_CHF); - if (((sr & FTM_CnSC_CHF) != 0) && - ((sr & FTM_CnSC_CHIE) != 0) && - (pwmp->config->channels[n].callback != NULL)) { - pwmp->config->channels[n].callback(pwmp); - } - } -} - -/*===========================================================================*/ -/* Driver interrupt handlers. */ -/*===========================================================================*/ - -#if KINETIS_PWM_USE_FTM0 -/** - * @brief FTM0 interrupt handler. - * - * @isr - */ -OSAL_IRQ_HANDLER(KINETIS_FTM0_IRQ_VECTOR) { - OSAL_IRQ_PROLOGUE(); - pwm_lld_serve_interrupt(&PWMD1); - OSAL_IRQ_EPILOGUE(); -} -#endif /* KINETIS_PWM_USE_FTM0 */ - -#if KINETIS_PWM_USE_FTM1 -/** - * @brief FTM1 interrupt handler. - * - * @isr - */ -OSAL_IRQ_HANDLER(KINETIS_FTM1_IRQ_VECTOR) { - - OSAL_IRQ_PROLOGUE(); - pwm_lld_serve_interrupt(&PWMD2); - OSAL_IRQ_EPILOGUE(); -} -#endif /* KINETIS_PWM_USE_FTM1 */ - -#if KINETIS_PWM_USE_FTM2 -/** - * @brief FTM2 interrupt handler. - * - * @isr - */ -OSAL_IRQ_HANDLER(KINETIS_FTM2_IRQ_VECTOR) { - - OSAL_IRQ_PROLOGUE(); - pwm_lld_serve_interrupt(&PWMD3); - OSAL_IRQ_EPILOGUE(); -} -#endif /* KINETIS_PWM_USE_FTM2 */ - -/*===========================================================================*/ -/* Driver exported functions. */ -/*===========================================================================*/ - -/** - * @brief Low level PWM driver initialization. - * - * @notapi - */ -void pwm_lld_init(void) { - -#if KINETIS_PWM_USE_FTM0 - pwmObjectInit(&PWMD1); - PWMD1.channels = KINETIS_FTM0_CHANNELS; - PWMD1.ftm = FTM0; -#endif - -#if KINETIS_PWM_USE_FTM1 - pwmObjectInit(&PWMD2); - PWMD2.channels = KINETIS_FTM1_CHANNELS; - PWMD2.ftm = FTM1; -#endif - -#if KINETIS_PWM_USE_FTM2 - pwmObjectInit(&PWMD3); - PWMD3.channels = KINETIS_FTM2_CHANNELS; - PWMD3.ftm = FTM2; -#endif -} - -/** - * @brief Configures and activates the PWM peripheral. - * @note Starting a driver that is already in the @p PWM_READY state - * disables all the active channels. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * - * @notapi - */ -void pwm_lld_start(PWMDriver *pwmp) { - uint16_t psc; - uint8_t i=0; - - if (pwmp->state == PWM_STOP) { - /* Clock activation and timer reset.*/ -#if KINETIS_PWM_USE_FTM0 - if (&PWMD1 == pwmp) { - SIM->SCGC6 |= SIM_SCGC6_FTM0; - nvicEnableVector(FTM0_IRQn, KINETIS_PWM_FTM0_PRIORITY); - } -#endif - -#if KINETIS_PWM_USE_FTM1 - if (&PWMD2 == pwmp) { - SIM->SCGC6 |= SIM_SCGC6_FTM1; - nvicEnableVector(FTM1_IRQn, KINETIS_PWM_FTM1_PRIORITY); - } -#endif - -#if KINETIS_PWM_USE_FTM2 - if (&PWMD3 == pwmp) { - SIM->SCGC3 |= SIM_SCGC3_FTM2; - nvicEnableVector(FTM2_IRQn, KINETIS_PWM_FTM2_PRIORITY); - } -#endif - } - pwmp->ftm->MODE = FTM_MODE_FTMEN_MASK|FTM_MODE_PWMSYNC_MASK; - pwmp->ftm->SYNC = FTM_SYNC_CNTMIN_MASK|FTM_SYNC_CNTMAX_MASK - |FTM_SYNC_SWSYNC_MASK; - pwmp->ftm->COMBINE = FTM_COMBINE_SYNCEN3_MASK | FTM_COMBINE_SYNCEN2_MASK - | FTM_COMBINE_SYNCEN1_MASK | FTM_COMBINE_SYNCEN0_MASK; - pwmp->ftm->SYNCONF = FTM_SYNCONF_SYNCMODE_MASK; - - pwmp->ftm->CNTIN = 0x0000; - //~ pwmp->ftm->SC = 0; /* Disable FTM counter.*/ - pwmp->ftm->CNT = 0x0000; /* Clear count register.*/ - - /* Prescaler value calculation.*/ - psc = (KINETIS_SYSCLK_FREQUENCY / pwmp->config->frequency); - //~ /* Prescaler must be power of two between 1 and 128.*/ - osalDbgAssert(psc <= 128 && !(psc & (psc - 1)), "invalid frequency"); - //~ /* Prescaler register value determination. - //~ Prescaler register value conveniently corresponds to bit position, - //~ i.e., register value for prescaler CLK/64 is 6 ((1 << 6) == 64).*/ - for (i = 0; i < 8; i++) { - if (psc == (unsigned)(1 << i)) { - break; - } - } - - /* Set prescaler and clock mode. - This also sets the following: - CPWMS up-counting mode - Timer overflow interrupt disabled - DMA disabled.*/ - pwmp->ftm->SC = FTM_SC_CLKS(1) | FTM_SC_PS(i); - /* Configure period */ - pwmp->ftm->MOD = pwmp->period-1; - pwmp->ftm->PWMLOAD = FTM_PWMLOAD_LDOK_MASK; -} - -/** - * @brief Deactivates the PWM peripheral. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * - * @notapi - */ -void pwm_lld_stop(PWMDriver *pwmp) { - - /* If in ready state then disables the PWM clock.*/ - if (pwmp->state == PWM_READY) { -#if KINETIS_PWM_USE_FTM0 - if (&PWMD1 == pwmp) { - SIM->SCGC6 &= ~SIM_SCGC6_FTM0; - nvicDisableVector(FTM0_IRQn); - } -#endif - -#if KINETIS_PWM_USE_FTM1 - if (&PWMD2 == pwmp) { - SIM->SCGC6 &= ~SIM_SCGC6_FTM1; - nvicDisableVector(FTM1_IRQn); - } -#endif - -#if KINETIS_PWM_USE_FTM2 - if (&PWMD3 == pwmp) { - SIM->SCGC3 &= ~SIM_SCGC3_FTM2; - nvicDisableVector(FTM2_IRQn); - } -#endif - /* Disable FTM counter.*/ - pwmp->ftm->SC = 0; - pwmp->ftm->MOD = 0; - } -} - -/** - * @brief Enables a PWM channel. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @post The channel is active using the specified configuration. - * @note The function has effect at the next cycle start. - * @note Channel notification is not enabled. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] channel PWM channel identifier (0...channels-1) - * @param[in] width PWM pulse width as clock pulses number - * - * @notapi - */ -void pwm_lld_enable_channel(PWMDriver *pwmp, - pwmchannel_t channel, - pwmcnt_t width) { - uint32_t mode = FTM_CnSC_MSB; /* Edge-aligned PWM mode.*/ - - switch (pwmp->config->channels[channel].mode & PWM_OUTPUT_MASK) { - case PWM_OUTPUT_ACTIVE_HIGH: - mode |= FTM_CnSC_ELSB; - break; - case PWM_OUTPUT_ACTIVE_LOW: - mode |= FTM_CnSC_ELSA; - break; - } - - if (pwmp->ftm->CHANNEL[channel].CnSC & FTM_CnSC_CHIE) - mode |= FTM_CnSC_CHIE; - - pwmp->ftm->CHANNEL[channel].CnSC = mode; - pwmp->ftm->CHANNEL[channel].CnV = width; - pwmp->ftm->PWMLOAD = FTM_PWMLOAD_LDOK_MASK; -} - -/** - * @brief Disables a PWM channel and its notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @post The channel is disabled and its output line returned to the - * idle state. - * @note The function has effect at the next cycle start. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] channel PWM channel identifier (0...channels-1) - * - * @notapi - */ -void pwm_lld_disable_channel(PWMDriver *pwmp, pwmchannel_t channel) { - - pwmp->ftm->CHANNEL[channel].CnSC = 0; - pwmp->ftm->CHANNEL[channel].CnV = 0; -} - -/** - * @brief Enables the periodic activation edge notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @note If the notification is already enabled then the call has no effect. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * - * @notapi - */ -void pwm_lld_enable_periodic_notification(PWMDriver *pwmp) { - pwmp->ftm->SC |= FTM_SC_TOIE; -} - -/** - * @brief Disables the periodic activation edge notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @note If the notification is already disabled then the call has no effect. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * - * @notapi - */ -void pwm_lld_disable_periodic_notification(PWMDriver *pwmp) { - pwmp->ftm->SC &= ~FTM_SC_TOIE; -} - -/** - * @brief Enables a channel de-activation edge notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @pre The channel must have been activated using @p pwmEnableChannel(). - * @note If the notification is already enabled then the call has no effect. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] channel PWM channel identifier (0...channels-1) - * - * @notapi - */ -void pwm_lld_enable_channel_notification(PWMDriver *pwmp, - pwmchannel_t channel) { - pwmp->ftm->CHANNEL[channel].CnSC |= FTM_CnSC_CHIE; -} - -/** - * @brief Disables a channel de-activation edge notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @pre The channel must have been activated using @p pwmEnableChannel(). - * @note If the notification is already disabled then the call has no effect. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] channel PWM channel identifier (0...channels-1) - * - * @notapi - */ -void pwm_lld_disable_channel_notification(PWMDriver *pwmp, - pwmchannel_t channel) { - pwmp->ftm->CHANNEL[channel].CnSC &= ~FTM_CnSC_CHIE; -} - -#endif /* HAL_USE_PWM */ - -/** @} */ diff --git a/os/hal/ports/KINETIS/K20x/hal_pwm_lld.h b/os/hal/ports/KINETIS/K20x/hal_pwm_lld.h deleted file mode 100644 index ccc100f..0000000 --- a/os/hal/ports/KINETIS/K20x/hal_pwm_lld.h +++ /dev/null @@ -1,270 +0,0 @@ -/* - ChibiOS/HAL - Copyright (C) 2014 Adam J. Porter - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -/** - * @file K20x7/pwm_lld.h - * @brief KINETIS PWM subsystem low level driver header. - * - * @addtogroup PWM - * @{ - */ - -#ifndef HAL_PWM_LLD_H_ -#define HAL_PWM_LLD_H_ - -#if HAL_USE_PWM || defined(__DOXYGEN__) - -/*===========================================================================*/ -/* Driver constants. */ -/*===========================================================================*/ - -/** - * @brief Number of PWM channels per PWM driver. - */ -#define PWM_CHANNELS 8 - -/*===========================================================================*/ -/* Driver pre-compile time settings. */ -/*===========================================================================*/ - -#if !defined(KINETIS_PWM_USE_FTM0) - #define KINETIS_PWM_USE_FTM0 FALSE -#endif - -#if !defined(KINETIS_PWM_USE_FTM1) - #define KINETIS_PWM_USE_FTM1 FALSE -#endif - -#if !defined(KINETIS_PWM_USE_FTM2) - #define KINETIS_PWM_USE_FTM2 FALSE -#endif - -/** - * @brief FTM0 interrupt priority level setting. - */ -#if !defined(KINETIS_PWM_FTM0_PRIORITY) || defined(__DOXYGEN__) -#define KINETIS_PWM_FTM0_PRIORITY 12 -#endif - -/** - * @brief FTM1 interrupt priority level setting. - */ -#if !defined(KINETIS_PWM_FTM1_PRIORITY) || defined(__DOXYGEN__) -#define KINETIS_PWM_FTM1_PRIORITY 12 -#endif - -/** - * @brief FTM2 interrupt priority level setting. - */ -#if !defined(KINETIS_PWM_FTM2_PRIORITY) || defined(__DOXYGEN__) -#define KINETIS_PWM_FTM2_PRIORITY 12 -#endif - -/** @} */ - -/** - * @name Configuration options - * @{ - */ -/** - * @brief If advanced timer features switch. - * @details If set to @p TRUE the advanced features for TIM1 and TIM8 are - * enabled. - * @note The default is @p TRUE. - */ -#if !defined(KINETIS_PWM_USE_ADVANCED) || defined(__DOXYGEN__) -#define KINETIS_PWM_USE_ADVANCED FALSE -#endif -/** @} */ - -/*===========================================================================*/ -/* Configuration checks. */ -/*===========================================================================*/ - -#if !KINETIS_PWM_USE_FTM0 && !KINETIS_PWM_USE_FTM1 && !KINETIS_PWM_USE_FTM2 -#error "PWM driver activated but no FTM peripheral assigned" -#endif - -/*===========================================================================*/ -/* Driver data structures and types. */ -/*===========================================================================*/ - -/** - * @brief Type of a PWM mode. - */ -typedef uint32_t pwmmode_t; - -/** - * @brief Type of a PWM channel. - */ -typedef uint8_t pwmchannel_t; - -/** - * @brief Type of a channels mask. - */ -typedef uint32_t pwmchnmsk_t; - -/** - * @brief Type of a PWM counter. - */ -typedef uint16_t pwmcnt_t; - -/** - * @brief Type of a PWM driver channel configuration structure. - */ -typedef struct { - /** - * @brief Channel active logic level. - */ - pwmmode_t mode; - - /** - * @brief Channel callback pointer. - * @note This callback is invoked on the channel compare event. If set to - * @p NULL then the callback is disabled. - */ - pwmcallback_t callback; - /* End of the mandatory fields.*/ -} PWMChannelConfig; - -/** - * @brief Type of a PWM driver configuration structure. - */ -typedef struct { - /** - * @brief Timer clock in Hz. - * @note The low level can use assertions in order to catch invalid - * frequency specifications. - */ - uint32_t frequency; - /** - * @brief PWM period in ticks. - * @note The low level can use assertions in order to catch invalid - * period specifications. - */ - pwmcnt_t period; - /** - * @brief Periodic callback pointer. - * @note This callback is invoked on PWM counter reset. If set to - * @p NULL then the callback is disabled. - */ - pwmcallback_t callback; - /** - * @brief Channels configurations. - */ - PWMChannelConfig channels[PWM_CHANNELS]; - /* End of the mandatory fields.*/ -} PWMConfig; - -/** - * @brief Structure representing a PWM driver. - */ -struct PWMDriver { - /** - * @brief Driver state. - */ - pwmstate_t state; - /** - * @brief Current driver configuration data. - */ - const PWMConfig *config; - /** - * @brief Current PWM period in ticks. - */ - pwmcnt_t period; - /** - * @brief Mask of the enabled channels. - */ - pwmchnmsk_t enabled; - /** - * @brief Number of channels in this instance. - */ - pwmchannel_t channels; -#if defined(PWM_DRIVER_EXT_FIELDS) - PWM_DRIVER_EXT_FIELDS -#endif - /* End of the mandatory fields.*/ - /** - * @brief Pointer to the FTM registers block. - */ - FTM_TypeDef *ftm; -}; - -/*===========================================================================*/ -/* Driver macros. */ -/*===========================================================================*/ - -/** - * @brief Changes the period the PWM peripheral. - * @details This function changes the period of a PWM unit that has already - * been activated using @p pwmStart(). - * @pre The PWM unit must have been activated using @p pwmStart(). - * @post The PWM unit period is changed to the new value. - * @note The function has effect at the next cycle start. - * @note If a period is specified that is shorter than the pulse width - * programmed in one of the channels then the behavior is not - * guaranteed. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] period new cycle time in ticks - * - * @notapi - */ -#define pwm_lld_change_period(pwmp, period) \ - do { \ - (pwmp)->ftm->MOD = ((period) - 1); \ - pwmp->ftm->PWMLOAD = FTM_PWMLOAD_LDOK_MASK;\ - } while(0) - -/*===========================================================================*/ -/* External declarations. */ -/*===========================================================================*/ - -#if KINETIS_PWM_USE_FTM0 || defined(__DOXYGEN__) -extern PWMDriver PWMD1; -#endif -#if KINETIS_PWM_USE_FTM1 || defined(__DOXYGEN__) -extern PWMDriver PWMD2; -#endif -#if KINETIS_PWM_USE_FTM2 || defined(__DOXYGEN__) -extern PWMDriver PWMD3; -#endif - -#ifdef __cplusplus -extern "C" { -#endif - void pwm_lld_init(void); - void pwm_lld_start(PWMDriver *pwmp); - void pwm_lld_stop(PWMDriver *pwmp); - void pwm_lld_enable_channel(PWMDriver *pwmp, - pwmchannel_t channel, - pwmcnt_t width); - void pwm_lld_disable_channel(PWMDriver *pwmp, pwmchannel_t channel); - void pwm_lld_enable_periodic_notification(PWMDriver *pwmp); - void pwm_lld_disable_periodic_notification(PWMDriver *pwmp); - void pwm_lld_enable_channel_notification(PWMDriver *pwmp, - pwmchannel_t channel); - void pwm_lld_disable_channel_notification(PWMDriver *pwmp, - pwmchannel_t channel); -#ifdef __cplusplus -} -#endif - -#endif /* HAL_USE_PWM */ - -#endif /* HAL_PWM_LLD_H_ */ - -/** @} */ diff --git a/os/hal/ports/KINETIS/K20x/hal_spi_lld.c b/os/hal/ports/KINETIS/K20x/hal_spi_lld.c deleted file mode 100644 index 29ab4e8..0000000 --- a/os/hal/ports/KINETIS/K20x/hal_spi_lld.c +++ /dev/null @@ -1,539 +0,0 @@ -/* - ChibiOS - Copyright (C) 2014-2015 Fabio Utzig - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -/** - * @file KINETIS/spi_lld.c - * @brief KINETIS SPI subsystem low level driver source. - * - * @addtogroup SPI - * @{ - */ - -#include "hal.h" - -#if HAL_USE_SPI || defined(__DOXYGEN__) - -/*===========================================================================*/ -/* Driver local definitions. */ -/*===========================================================================*/ - -#if !defined(KINETIS_SPI0_RX_DMA_IRQ_PRIORITY) -#define KINETIS_SPI0_RX_DMA_IRQ_PRIORITY 8 -#endif - -#if !defined(KINETIS_SPI0_RX_DMAMUX_CHANNEL) -#define KINETIS_SPI0_RX_DMAMUX_CHANNEL 0 -#endif - -#if !defined(KINETIS_SPI0_RX_DMA_CHANNEL) -#define KINETIS_SPI0_RX_DMA_CHANNEL 0 -#endif - -#if !defined(KINETIS_SPI0_TX_DMAMUX_CHANNEL) -#define KINETIS_SPI0_TX_DMAMUX_CHANNEL 1 -#endif - -#if !defined(KINETIS_SPI0_TX_DMA_CHANNEL) -#define KINETIS_SPI0_TX_DMA_CHANNEL 1 -#endif - -#if !defined(KINETIS_SPI1_RX_DMA_IRQ_PRIORITY) -#define KINETIS_SPI1_RX_DMA_IRQ_PRIORITY 8 -#endif - -#if !defined(KINETIS_SPI1_RX_DMAMUX_CHANNEL) -#define KINETIS_SPI1_RX_DMAMUX_CHANNEL 0 -#endif - -#if !defined(KINETIS_SPI1_RX_DMA_CHANNEL) -#define KINETIS_SPI1_RX_DMA_CHANNEL 0 -#endif - -#if !defined(KINETIS_SPI1_TX_DMAMUX_CHANNEL) -#define KINETIS_SPI1_TX_DMAMUX_CHANNEL 1 -#endif - -#if !defined(KINETIS_SPI1_TX_DMA_CHANNEL) -#define KINETIS_SPI1_TX_DMA_CHANNEL 1 -#endif - -#if KINETIS_SPI_USE_SPI0 -#define DMAMUX_SPI_RX_SOURCE 16 -#define DMAMUX_SPI_TX_SOURCE 17 -#endif - -#if KINETIS_SPI_USE_SPI1 -#define DMAMUX_SPI_RX_SOURCE 18 -#define DMAMUX_SPI_TX_SOURCE 19 -#endif - -/*===========================================================================*/ -/* Driver exported variables. */ -/*===========================================================================*/ - -/** @brief SPI0 driver identifier.*/ -#if KINETIS_SPI_USE_SPI0 || defined(__DOXYGEN__) -SPIDriver SPID1; -#endif - -/** @brief SPI1 driver identifier.*/ -#if KINETIS_SPI_USE_SPI1 || defined(__DOXYGEN__) -SPIDriver SPID2; -#endif - -/*===========================================================================*/ -/* Driver local variables and types. */ -/*===========================================================================*/ - -/* Use a dummy byte as the source/destination when a buffer is not provided */ -/* Note: The MMC driver relies on 0xFF being sent for dummy bytes. */ -static volatile uint16_t dmaRxDummy; -static uint16_t dmaTxDummy = 0xFFFF; - -/*===========================================================================*/ -/* Driver local functions. */ -/*===========================================================================*/ - -static void spi_start_xfer(SPIDriver *spip, bool polling) -{ - /* - * Enable the DSPI peripheral in master mode. - * Clear the TX and RX FIFOs. - * */ - spip->spi->MCR = SPIx_MCR_MSTR | SPIx_MCR_CLR_TXF | SPIx_MCR_CLR_RXF; - - /* If we are not polling then enable DMA */ - if (!polling) { - - /* Enable receive dma and transmit dma */ - spip->spi->RSER = SPIx_RSER_RFDF_DIRS | SPIx_RSER_RFDF_RE | - SPIx_RSER_TFFF_RE | SPIx_RSER_TFFF_DIRS; - - /* Configure RX DMA */ - if (spip->rxbuf) { - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DADDR = (uint32_t)spip->rxbuf; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DOFF = spip->word_size; - } else { - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DADDR = (uint32_t)&dmaRxDummy; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DOFF = 0; - } - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].BITER_ELINKNO = spip->count; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].CITER_ELINKNO = spip->count; - - /* Enable Request Register (ERQ) for RX by writing 0 to SERQ */ - DMA->SERQ = KINETIS_SPI0_RX_DMA_CHANNEL; - - /* Configure TX DMA */ - if (spip->txbuf) { - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SADDR = (uint32_t)spip->txbuf; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SOFF = spip->word_size; - } else { - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SADDR = (uint32_t)&dmaTxDummy; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SOFF = 0; - } - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].BITER_ELINKNO = spip->count; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].CITER_ELINKNO = spip->count; - - /* Enable Request Register (ERQ) for TX by writing 1 to SERQ */ - DMA->SERQ = KINETIS_SPI0_TX_DMA_CHANNEL; - } -} - -static void spi_stop_xfer(SPIDriver *spip) -{ - /* Halt the DSPI peripheral */ - spip->spi->MCR = SPIx_MCR_MSTR | SPIx_MCR_HALT; - - /* Clear all the flags which are currently set. */ - spip->spi->SR |= spip->spi->SR; -} - -/*===========================================================================*/ -/* Driver interrupt handlers. */ -/*===========================================================================*/ - -#if KINETIS_SPI_USE_SPI0 || defined(__DOXYGEN__) - -OSAL_IRQ_HANDLER(KINETIS_DMA0_IRQ_VECTOR) { - OSAL_IRQ_PROLOGUE(); - - /* Clear bit 0 in Interrupt Request Register (INT) by writing 0 to CINT */ - DMA->CINT = KINETIS_SPI0_RX_DMA_CHANNEL; - - spi_stop_xfer(&SPID1); - - _spi_isr_code(&SPID1); - - OSAL_IRQ_EPILOGUE(); -} - -#endif - -#if KINETIS_SPI_USE_SPI1 || defined(__DOXYGEN__) - -OSAL_IRQ_HANDLER(KINETIS_DMA0_IRQ_VECTOR) { - OSAL_IRQ_PROLOGUE(); - - /* Clear bit 0 in Interrupt Request Register (INT) by writing 0 to CINT */ - DMA->CINT = KINETIS_SPI1_RX_DMA_CHANNEL; - - spi_stop_xfer(&SPID2); - - _spi_isr_code(&SPID2); - - OSAL_IRQ_EPILOGUE(); -} - -#endif - -/*===========================================================================*/ -/* Driver exported functions. */ -/*===========================================================================*/ - -/** - * @brief Low level SPI driver initialization. - * - * @notapi - */ -void spi_lld_init(void) { -#if KINETIS_SPI_USE_SPI0 - spiObjectInit(&SPID1); -#endif -#if KINETIS_SPI_USE_SPI1 - spiObjectInit(&SPID2); -#endif -} - -/** - * @brief Configures and activates the SPI peripheral. - * - * @param[in] spip pointer to the @p SPIDriver object - * - * @notapi - */ -void spi_lld_start(SPIDriver *spip) { - - /* If in stopped state then enables the SPI and DMA clocks.*/ - if (spip->state == SPI_STOP) { - -#if KINETIS_SPI_USE_SPI0 - if (&SPID1 == spip) { - - /* Enable the clock for SPI0 */ - SIM->SCGC6 |= SIM_SCGC6_SPI0; - - SPID1.spi = SPI0; - - if (spip->config->tar0) { - spip->spi->CTAR[0] = spip->config->tar0; - } else { - spip->spi->CTAR[0] = KINETIS_SPI_TAR0_DEFAULT; - } - } -#endif - -#if KINETIS_SPI_USE_SPI1 - if (&SPID2 == spip) { - - /* Enable the clock for SPI0 */ - SIM->SCGC6 |= SIM_SCGC6_SPI1; - - SPID2.spi = SPI1; - - if (spip->config->tar0) { - spip->spi->CTAR[0] = spip->config->tar0; - } else { - spip->spi->CTAR[0] = KINETIS_SPI_TAR0_DEFAULT; - } - } -#endif - - nvicEnableVector(DMA0_IRQn, KINETIS_SPI0_RX_DMA_IRQ_PRIORITY); - - SIM->SCGC6 |= SIM_SCGC6_DMAMUX; - SIM->SCGC7 |= SIM_SCGC7_DMA; - - /* Clear DMA error flags */ - DMA->ERR = 0x0F; - -#if KINETIS_SPI_USE_SPI0 - /* Rx, select SPI Rx FIFO */ - DMAMUX->CHCFG[KINETIS_SPI0_RX_DMAMUX_CHANNEL] = DMAMUX_CHCFGn_ENBL | - DMAMUX_CHCFGn_SOURCE(DMAMUX_SPI_RX_SOURCE); - - /* Tx, select SPI Tx FIFO */ - DMAMUX->CHCFG[KINETIS_SPI0_TX_DMAMUX_CHANNEL] = DMAMUX_CHCFGn_ENBL | - DMAMUX_CHCFGn_SOURCE(DMAMUX_SPI_TX_SOURCE); - - /* Extract the frame size from the TAR */ - uint16_t frame_size = ((spip->spi->CTAR[0] >> SPIx_CTARn_FMSZ_SHIFT) & - SPIx_CTARn_FMSZ_MASK) + 1; - - /* DMA transfer size is 16 bits for a frame size > 8 bits */ - uint16_t dma_size = frame_size > 8 ? 1 : 0; - - /* DMA word size is 2 for a 16 bit frame size */ - spip->word_size = frame_size > 8 ? 2 : 1; - - /* configure DMA RX fixed values */ - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].SADDR = (uint32_t)&SPI0->POPR; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].SOFF = 0; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].SLAST = 0; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DLASTSGA = 0; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].ATTR = DMA_ATTR_SSIZE(dma_size) | - DMA_ATTR_DSIZE(dma_size); - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].NBYTES_MLNO = spip->word_size; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].CSR = DMA_CSR_DREQ_MASK | - DMA_CSR_INTMAJOR_MASK; - - /* configure DMA TX fixed values */ - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SLAST = 0; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].DADDR = (uint32_t)&SPI0->PUSHR; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].DOFF = 0; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].DLASTSGA = 0; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].ATTR = DMA_ATTR_SSIZE(dma_size) | - DMA_ATTR_DSIZE(dma_size); - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].NBYTES_MLNO = spip->word_size; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].CSR = DMA_CSR_DREQ_MASK; -#endif - -#if KINETIS_SPI_USE_SPI1 - /* Rx, select SPI Rx FIFO */ - DMAMUX->CHCFG[KINETIS_SPI1_RX_DMAMUX_CHANNEL] = DMAMUX_CHCFGn_ENBL | - DMAMUX_CHCFGn_SOURCE(DMAMUX_SPI_RX_SOURCE); - - /* Tx, select SPI Tx FIFO */ - DMAMUX->CHCFG[KINETIS_SPI1_TX_DMAMUX_CHANNEL] = DMAMUX_CHCFGn_ENBL | - DMAMUX_CHCFGn_SOURCE(DMAMUX_SPI_TX_SOURCE); - - /* Extract the frame size from the TAR */ - uint16_t frame_size = ((spip->spi->CTAR[0] >> SPIx_CTARn_FMSZ_SHIFT) & - SPIx_CTARn_FMSZ_MASK) + 1; - - /* DMA transfer size is 16 bits for a frame size > 8 bits */ - uint16_t dma_size = frame_size > 8 ? 1 : 0; - - /* DMA word size is 2 for a 16 bit frame size */ - spip->word_size = frame_size > 8 ? 2 : 1; - - /* configure DMA RX fixed values */ - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].SADDR = (uint32_t)&SPI1->POPR; - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].SOFF = 0; - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].SLAST = 0; - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].DLASTSGA = 0; - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].ATTR = DMA_ATTR_SSIZE(dma_size) | - DMA_ATTR_DSIZE(dma_size); - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].NBYTES_MLNO = spip->word_size; - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].CSR = DMA_CSR_DREQ_MASK | - DMA_CSR_INTMAJOR_MASK; - - /* configure DMA TX fixed values */ - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].SLAST = 0; - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].DADDR = (uint32_t)&SPI1->PUSHR; - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].DOFF = 0; - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].DLASTSGA = 0; - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].ATTR = DMA_ATTR_SSIZE(dma_size) | - DMA_ATTR_DSIZE(dma_size); - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].NBYTES_MLNO = spip->word_size; - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].CSR = DMA_CSR_DREQ_MASK; -#endif - } -} - -/** - * @brief Deactivates the SPI peripheral. - * - * @param[in] spip pointer to the @p SPIDriver object - * - * @notapi - */ -void spi_lld_stop(SPIDriver *spip) { - - /* If in ready state then disables the SPI clock.*/ - if (spip->state == SPI_READY) { - - nvicDisableVector(DMA0_IRQn); - - SIM->SCGC7 &= ~SIM_SCGC7_DMA; - SIM->SCGC6 &= ~SIM_SCGC6_DMAMUX; - -#if KINETIS_SPI_USE_SPI0 - if (&SPID1 == spip) { - /* SPI halt.*/ - spip->spi->MCR |= SPIx_MCR_HALT; - } - - /* Disable the clock for SPI0 */ - SIM->SCGC6 &= ~SIM_SCGC6_SPI0; -#endif - -#if KINETIS_SPI_USE_SPI1 - if (&SPID2 == spip) { - /* SPI halt.*/ - spip->spi->MCR |= SPIx_MCR_HALT; - } - - /* Disable the clock for SPI1 */ - SIM->SCGC6 &= ~SIM_SCGC6_SPI1; -#endif - } -} - -/** - * @brief Asserts the slave select signal and prepares for transfers. - * - * @param[in] spip pointer to the @p SPIDriver object - * - * @notapi - */ -void spi_lld_select(SPIDriver *spip) { - - palClearPad(spip->config->ssport, spip->config->sspad); -} - -/** - * @brief Deasserts the slave select signal. - * @details The previously selected peripheral is unselected. - * - * @param[in] spip pointer to the @p SPIDriver object - * - * @notapi - */ -void spi_lld_unselect(SPIDriver *spip) { - - palSetPad(spip->config->ssport, spip->config->sspad); -} - -/** - * @brief Ignores data on the SPI bus. - * @details This asynchronous function starts the transmission of a series of - * idle words on the SPI bus and ignores the received data. - * @post At the end of the operation the configured callback is invoked. - * - * @param[in] spip pointer to the @p SPIDriver object - * @param[in] n number of words to be ignored - * - * @notapi - */ -void spi_lld_ignore(SPIDriver *spip, size_t n) { - - spip->count = n; - spip->rxbuf = NULL; - spip->txbuf = NULL; - - spi_start_xfer(spip, false); -} - -/** - * @brief Exchanges data on the SPI bus. - * @details This asynchronous function starts a simultaneous transmit/receive - * operation. - * @post At the end of the operation the configured callback is invoked. - * @note The buffers are organized as uint8_t arrays for data sizes below or - * equal to 8 bits else it is organized as uint16_t arrays. - * - * @param[in] spip pointer to the @p SPIDriver object - * @param[in] n number of words to be exchanged - * @param[in] txbuf the pointer to the transmit buffer - * @param[out] rxbuf the pointer to the receive buffer - * - * @notapi - */ -void spi_lld_exchange(SPIDriver *spip, size_t n, - const void *txbuf, void *rxbuf) { - - spip->count = n; - spip->rxbuf = rxbuf; - spip->txbuf = txbuf; - - spi_start_xfer(spip, false); -} - -/** - * @brief Sends data over the SPI bus. - * @details This asynchronous function starts a transmit operation. - * @post At the end of the operation the configured callback is invoked. - * @note The buffers are organized as uint8_t arrays for data sizes below or - * equal to 8 bits else it is organized as uint16_t arrays. - * - * @param[in] spip pointer to the @p SPIDriver object - * @param[in] n number of words to send - * @param[in] txbuf the pointer to the transmit buffer - * - * @notapi - */ -void spi_lld_send(SPIDriver *spip, size_t n, const void *txbuf) { - - spip->count = n; - spip->rxbuf = NULL; - spip->txbuf = (void *)txbuf; - - spi_start_xfer(spip, false); -} - -/** - * @brief Receives data from the SPI bus. - * @details This asynchronous function starts a receive operation. - * @post At the end of the operation the configured callback is invoked. - * @note The buffers are organized as uint8_t arrays for data sizes below or - * equal to 8 bits else it is organized as uint16_t arrays. - * - * @param[in] spip pointer to the @p SPIDriver object - * @param[in] n number of words to receive - * @param[out] rxbuf the pointer to the receive buffer - * - * @notapi - */ -void spi_lld_receive(SPIDriver *spip, size_t n, void *rxbuf) { - - spip->count = n; - spip->rxbuf = rxbuf; - spip->txbuf = NULL; - - spi_start_xfer(spip, false); -} - -/** - * @brief Exchanges one frame using a polled wait. - * @details This synchronous function exchanges one frame using a polled - * synchronization method. This function is useful when exchanging - * small amount of data on high speed channels, usually in this - * situation is much more efficient just wait for completion using - * polling than suspending the thread waiting for an interrupt. - * - * @param[in] spip pointer to the @p SPIDriver object - * @param[in] frame the data frame to send over the SPI bus - * @return The received data frame from the SPI bus. - */ -uint16_t spi_lld_polled_exchange(SPIDriver *spip, uint16_t frame) { - - spi_start_xfer(spip, true); - - spip->spi->PUSHR = SPIx_PUSHR_TXDATA(frame); - - while ((spip->spi->SR & SPIx_SR_RFDF) == 0) - ; - - frame = spip->spi->POPR; - - spi_stop_xfer(spip); - - return frame; -} - -#endif /* HAL_USE_SPI */ - -/** @} */ diff --git a/os/hal/ports/KINETIS/K20x/hal_spi_lld.h b/os/hal/ports/KINETIS/K20x/hal_spi_lld.h deleted file mode 100644 index 0cf108e..0000000 --- a/os/hal/ports/KINETIS/K20x/hal_spi_lld.h +++ /dev/null @@ -1,261 +0,0 @@ -/* - ChibiOS - Copyright (C) 2014-2015 Fabio Utzig - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -/** - * @file KINETIS/spi_lld.h - * @brief KINETIS SPI subsystem low level driver header. - * - * @addtogroup SPI - * @{ - */ - -#ifndef HAL_SPI_LLD_H_ -#define HAL_SPI_LLD_H_ - -#if HAL_USE_SPI || defined(__DOXYGEN__) - -/*===========================================================================*/ -/* Driver constants. */ -/*===========================================================================*/ - -/*===========================================================================*/ -/* Driver pre-compile time settings. */ -/*===========================================================================*/ - -/** - * @name Configuration options - * @{ - */ -/** - * @brief SPI0 driver enable switch. - * @details If set to @p TRUE the support for SPI0 is included. - * @note The default is @p FALSE. - */ -#if !defined(KINETIS_SPI_USE_SPI0) || defined(__DOXYGEN__) -#define KINETIS_SPI_USE_SPI0 FALSE -#endif - -/** - * @brief SPI0 interrupt priority level setting. - */ -#if !defined(KINETIS_SPI_SPI0_IRQ_PRIORITY) || defined(__DOXYGEN__) -#define KINETIS_SPI_SPI0_IRQ_PRIORITY 10 -#endif - -/** - * @brief SPI1 driver enable switch. - * @details If set to @p TRUE the support for SPI0 is included. - * @note The default is @p FALSE. - */ -#if !defined(KINETIS_SPI_USE_SPI1) || defined(__DOXYGEN__) -#define KINETIS_SPI_USE_SPI1 FALSE -#endif - -/** - * @brief SPI1 interrupt priority level setting. - */ -#if !defined(KINETIS_SPI_SPI1_IRQ_PRIORITY) || defined(__DOXYGEN__) -#define KINETIS_SPI_SPI1_IRQ_PRIORITY 10 -#endif - -/*===========================================================================*/ -/* Derived constants and error checks. */ -/*===========================================================================*/ - -#if KINETIS_SPI_USE_SPI0 && !KINETIS_HAS_SPI0 -#error "SPI0 not present in the selected device" -#endif - -#if KINETIS_SPI_USE_SPI1 && !KINETIS_HAS_SPI1 -#error "SPI1 not present in the selected device" -#endif - -#if KINETIS_SPI_USE_SPI0 && KINETIS_SPI_USE_SPI1 -#error "Only one SPI peripheral can be enabled" -#endif - -#if !(KINETIS_SPI_USE_SPI0 || KINETIS_SPI_USE_SPI1) -#error "SPI driver activated but no SPI peripheral assigned" -#endif - -#if KINETIS_SPI_USE_SPI0 && \ - !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_SPI_SPI0_IRQ_PRIORITY) -#error "Invalid IRQ priority assigned to SPI0" -#endif - -#if KINETIS_SPI_USE_SPI1 && \ - !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_SPI_SPI1_IRQ_PRIORITY) -#error "Invalid IRQ priority assigned to SPI1" -#endif - -/*===========================================================================*/ -/* Driver data structures and types. */ -/*===========================================================================*/ - -/** - * @brief Type of a structure representing an SPI driver. - */ -typedef struct SPIDriver SPIDriver; - -/** - * @brief SPI notification callback type. - * - * @param[in] spip pointer to the @p SPIDriver object triggering the - * callback - */ -typedef void (*spicallback_t)(SPIDriver *spip); - -/** - * @brief Driver configuration structure. - */ -typedef struct { - /** - * @brief Operation complete callback or @p NULL. - */ - spicallback_t end_cb; - /* End of the mandatory fields.*/ - /** - * @brief The chip select line port - when not using pcs. - */ - ioportid_t ssport; - /** - * @brief The chip select line pad number - when not using pcs. - */ - uint16_t sspad; - /** - * @brief SPI initialization data. - */ - uint32_t tar0; -} SPIConfig; - -/** - * @brief Structure representing a SPI driver. - */ -struct SPIDriver { - /** - * @brief Driver state. - */ - spistate_t state; - /** - * @brief Current configuration data. - */ - const SPIConfig *config; -#if SPI_USE_WAIT || defined(__DOXYGEN__) - /** - * @brief Waiting thread. - */ - thread_reference_t thread; -#endif /* SPI_USE_WAIT */ -#if SPI_USE_MUTUAL_EXCLUSION || defined(__DOXYGEN__) - /** - * @brief Mutex protecting the bus. - */ - mutex_t mutex; -#endif /* SPI_USE_MUTUAL_EXCLUSION */ -#if defined(SPI_DRIVER_EXT_FIELDS) - SPI_DRIVER_EXT_FIELDS -#endif - /* End of the mandatory fields.*/ - /** - * @brief Pointer to the SPIx registers block. - */ - SPI_TypeDef *spi; - /** - * @brief Number of bytes/words of data to transfer. - */ - size_t count; - /** - * @brief Word size in bytes. - */ - size_t word_size; - /** - * @brief Pointer to the buffer with data to send. - */ - const uint8_t *txbuf; - /** - * @brief Pointer to the buffer to put received data. - */ - uint8_t *rxbuf; -}; - -/*===========================================================================*/ -/* Driver macros. */ -/*===========================================================================*/ - -/* TAR settings for n bits at SYSCLK / 2 */ -#define KINETIS_SPI_TAR_SYSCLK_DIV_2(n)\ - SPIx_CTARn_FMSZ((n) - 1) | \ - SPIx_CTARn_CPOL | \ - SPIx_CTARn_CPHA | \ - SPIx_CTARn_DBR | \ - SPIx_CTARn_PBR(0) | \ - SPIx_CTARn_BR(0) | \ - SPIx_CTARn_CSSCK(0) | \ - SPIx_CTARn_ASC(0) | \ - SPIx_CTARn_DT(0) - -/* TAR settings for n bits at SYSCLK / 4096 for debugging */ -#define KINETIS_SPI_TAR_SYSCLK_DIV_4096(n) \ - SPIx_CTARn_FMSZ(((n) - 1)) | \ - SPIx_CTARn_CPOL | \ - SPIx_CTARn_CPHA | \ - SPIx_CTARn_PBR(0) | \ - SPIx_CTARn_BR(0xB) | \ - SPIx_CTARn_CSSCK(0xB) | \ - SPIx_CTARn_ASC(0x7) | \ - SPIx_CTARn_DT(0xB) - -#define KINETIS_SPI_TAR_8BIT_FAST KINETIS_SPI_TAR_SYSCLK_DIV_2(8) -#define KINETIS_SPI_TAR_8BIT_SLOW KINETIS_SPI_TAR_SYSCLK_DIV_4096(8) - -#define KINETIS_SPI_TAR0_DEFAULT KINETIS_SPI_TAR_SYSCLK_DIV_2(8) -#define KINETIS_SPI_TAR1_DEFAULT KINETIS_SPI_TAR_SYSCLK_DIV_2(8) - -/*===========================================================================*/ -/* External declarations. */ -/*===========================================================================*/ - -#if KINETIS_SPI_USE_SPI0 && !defined(__DOXYGEN__) -extern SPIDriver SPID1; -#endif - -#if KINETIS_SPI_USE_SPI1 && !defined(__DOXYGEN__) -extern SPIDriver SPID2; -#endif - -#ifdef __cplusplus -extern "C" { -#endif - void spi_lld_init(void); - void spi_lld_start(SPIDriver *spip); - void spi_lld_stop(SPIDriver *spip); - void spi_lld_select(SPIDriver *spip); - void spi_lld_unselect(SPIDriver *spip); - void spi_lld_ignore(SPIDriver *spip, size_t n); - void spi_lld_exchange(SPIDriver *spip, size_t n, - const void *txbuf, void *rxbuf); - void spi_lld_send(SPIDriver *spip, size_t n, const void *txbuf); - void spi_lld_receive(SPIDriver *spip, size_t n, void *rxbuf); - uint16_t spi_lld_polled_exchange(SPIDriver *spip, uint16_t frame); -#ifdef __cplusplus -} -#endif - -#endif /* HAL_USE_SPI */ - -#endif /* HAL_SPI_LLD_H_ */ - -/** @} */ diff --git a/os/hal/ports/KINETIS/K20x/platform.mk b/os/hal/ports/KINETIS/K20x/platform.mk index beee336..537a6b6 100644 --- a/os/hal/ports/KINETIS/K20x/platform.mk +++ b/os/hal/ports/KINETIS/K20x/platform.mk @@ -1,18 +1,32 @@ -# List of all platform files. -PLATFORMSRC = ${CHIBIOS}/os/hal/ports/common/ARMCMx/nvic.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/K20x/hal_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_pal_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_serial_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/K20x/hal_spi_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_i2c_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_ext_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_adc_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_gpt_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/K20x/hal_pwm_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_st_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_usb_lld.c +PLATFORMSRC_CONTRIB := ${CHIBIOS}/os/hal/ports/common/ARMCMx/nvic.c \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/K20x/hal_lld.c \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PITv1/hal_st_lld.c + +PLATFORMINC_CONTRIB := ${CHIBIOS}/os/hal/ports/common/ARMCMx \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/K20x + +ifeq ($(USE_SMART_BUILD),yes) -# Required include directories -PLATFORMINC = ${CHIBIOS}/os/hal/ports/common/ARMCMx \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/K20x \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD +# Configuration files directory +ifeq ($(CONFDIR),) + CONFDIR = . +endif + +HALCONF := $(strip $(shell cat $(CONFDIR)/halconf.h $(CONFDIR)/halconf_community.h | egrep -e "\#define")) + +endif + +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/GPIOv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/UARTv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/SPIv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/I2Cv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PORTv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/ADCv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PITv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/FTMv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/USBHSv1/driver.mk + +# Shared variables +ALLCSRC += $(PLATFORMSRC_CONTRIB) +ALLINC += $(PLATFORMINC_CONTRIB) diff --git a/os/hal/ports/KINETIS/K60x/platform.mk b/os/hal/ports/KINETIS/K60x/platform.mk index 33ac2cc..1eb6cc5 100644 --- a/os/hal/ports/KINETIS/K60x/platform.mk +++ b/os/hal/ports/KINETIS/K60x/platform.mk @@ -1,19 +1,30 @@ -# List of all platform files. -PLATFORMSRC = ${CHIBIOS}/os/hal/ports/common/ARMCMx/nvic.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/K60x/hal_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_pal_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_serial_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_ext_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_gpt_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_st_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_sdc_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_i2c_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_adc_lld.c \ -# ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/K60x/hal_spi_lld.c \ -# ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/K60x/hal_pwm_lld.c \ -# ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_usb_lld.c +PLATFORMSRC_CONTRIB := ${CHIBIOS}/os/hal/ports/common/ARMCMx/nvic.c \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/K60x/hal_lld.c \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PITv1/hal_st_lld.c + +PLATFORMINC_CONTRIB := ${CHIBIOS}/os/hal/ports/common/ARMCMx \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/K60x + +ifeq ($(USE_SMART_BUILD),yes) -# Required include directories -PLATFORMINC = ${CHIBIOS}/os/hal/ports/common/ARMCMx \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/K60x \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD +# Configuration files directory +ifeq ($(CONFDIR),) + CONFDIR = . +endif + +HALCONF := $(strip $(shell cat $(CONFDIR)/halconf.h $(CONFDIR)/halconf_community.h | egrep -e "\#define")) + +endif + +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/GPIOv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/UARTv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PORTv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PITv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/SDHCv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/I2Cv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/ADCv1/driver.mk + +# Shared variables +ALLCSRC += $(PLATFORMSRC_CONTRIB) +ALLINC += $(PLATFORMINC_CONTRIB) diff --git a/os/hal/ports/KINETIS/KL2x/hal_pwm_lld.c b/os/hal/ports/KINETIS/KL2x/hal_pwm_lld.c deleted file mode 100644 index 2f56216..0000000 --- a/os/hal/ports/KINETIS/KL2x/hal_pwm_lld.c +++ /dev/null @@ -1,388 +0,0 @@ -/* - ChibiOS - Copyright (C) 2014 Adam J. Porter - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -/** - * @file KL2x/pwm_lld.c - * @brief KINETIS PWM subsystem low level driver source. - * - * @addtogroup PWM - * @{ - */ - -#include "hal.h" - -#if HAL_USE_PWM || defined(__DOXYGEN__) - -/*===========================================================================*/ -/* Driver exported variables. */ -/*===========================================================================*/ - -/** - * @brief PWMD1 driver identifier. - * @note The driver PWMD1 allocates the timer TPM0 when enabled. - */ -#if KINETIS_PWM_USE_TPM0 || defined(__DOXYGEN__) -PWMDriver PWMD1; -#endif - -/** - * @brief PWMD2 driver identifier. - * @note The driver PWMD2 allocates the timer TPM1 when enabled. - */ -#if KINETIS_PWM_USE_TPM1 || defined(__DOXYGEN__) -PWMDriver PWMD2; -#endif - -/** - * @brief PWMD3 driver identifier. - * @note The driver PWMD3 allocates the timer TPM2 when enabled. - */ -#if KINETIS_PWM_USE_TPM2 || defined(__DOXYGEN__) -PWMDriver PWMD3; -#endif - -/*===========================================================================*/ -/* Driver local variables and types. */ -/*===========================================================================*/ - -/*===========================================================================*/ -/* Driver local functions. */ -/*===========================================================================*/ - -static void pwm_lld_serve_interrupt(PWMDriver *pwmp) { - uint32_t sr; - - sr = pwmp->tpm->STATUS; - pwmp->tpm->STATUS = 0xFFFFFFFF; - - if (((sr & TPMx_STATUS_TOF) != 0) && - (pwmp->config->callback != NULL)) - pwmp->config->callback(pwmp); - if (((sr & TPMx_STATUS_CH0F) != 0) && - (pwmp->config->channels[0].callback != NULL)) - pwmp->config->channels[0].callback(pwmp); - if (((sr & TPMx_STATUS_CH1F) != 0) && - (pwmp->config->channels[1].callback != NULL)) - pwmp->config->channels[1].callback(pwmp); - if (((sr & TPMx_STATUS_CH2F) != 0) && - (pwmp->config->channels[2].callback != NULL)) - pwmp->config->channels[2].callback(pwmp); - if (((sr & TPMx_STATUS_CH3F) != 0) && - (pwmp->config->channels[3].callback != NULL)) - pwmp->config->channels[3].callback(pwmp); - if (((sr & TPMx_STATUS_CH4F) != 0) && - (pwmp->config->channels[4].callback != NULL)) - pwmp->config->channels[4].callback(pwmp); - if (((sr & TPMx_STATUS_CH5F) != 0) && - (pwmp->config->channels[5].callback != NULL)) - pwmp->config->channels[5].callback(pwmp); -} - -/*===========================================================================*/ -/* Driver interrupt handlers. */ -/*===========================================================================*/ - -#if KINETIS_PWM_USE_TPM0 -/** - * @brief TPM0 interrupt handler. - * - * @isr - */ -OSAL_IRQ_HANDLER(KINETIS_TPM0_IRQ_VECTOR) { - - OSAL_IRQ_PROLOGUE(); - pwm_lld_serve_interrupt(&PWMD1); - OSAL_IRQ_EPILOGUE(); -} -#endif /* KINETIS_PWM_USE_TPM0 */ - -#if KINETIS_PWM_USE_TPM1 -/** - * @brief TPM1 interrupt handler. - * - * @isr - */ -OSAL_IRQ_HANDLER(KINETIS_TPM1_IRQ_VECTOR) { - - OSAL_IRQ_PROLOGUE(); - pwm_lld_serve_interrupt(&PWMD2); - OSAL_IRQ_EPILOGUE(); -} -#endif /* KINETIS_PWM_USE_TPM1 */ - -#if KINETIS_PWM_USE_TPM2 -/** - * @brief TPM2 interrupt handler. - * - * @isr - */ -OSAL_IRQ_HANDLER(KINETIS_TPM2_IRQ_VECTOR) { - - OSAL_IRQ_PROLOGUE(); - pwm_lld_serve_interrupt(&PWMD3); - OSAL_IRQ_EPILOGUE(); -} -#endif /* KINETIS_PWM_USE_TPM2 */ - -/*===========================================================================*/ -/* Driver exported functions. */ -/*===========================================================================*/ - -/** - * @brief Low level PWM driver initialization. - * - * @notapi - */ -void pwm_lld_init(void) { - -#if KINETIS_PWM_USE_TPM0 - pwmObjectInit(&PWMD1); - PWMD1.channels = KINETIS_TPM0_CHANNELS; - PWMD1.tpm = TPM0; -#endif - -#if KINETIS_PWM_USE_TPM1 - pwmObjectInit(&PWMD2); - PWMD2.channels = KINETIS_TPM1_CHANNELS; - PWMD2.tpm = TPM1; -#endif - -#if KINETIS_PWM_USE_TPM2 - pwmObjectInit(&PWMD3); - PWMD3.channels = KINETIS_TPM2_CHANNELS; - PWMD3.tpm = TPM2; -#endif -} - -/** - * @brief Configures and activates the PWM peripheral. - * @note Starting a driver that is already in the @p PWM_READY state - * disables all the active channels. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * - * @notapi - */ -void pwm_lld_start(PWMDriver *pwmp) { - uint32_t psc; - int i; - - if (pwmp->state == PWM_STOP) { - /* Clock activation and timer reset.*/ -#if KINETIS_PWM_USE_TPM0 - if (&PWMD1 == pwmp) { - SIM->SCGC6 |= SIM_SCGC6_TPM0; - nvicEnableVector(TPM0_IRQn, KINETIS_PWM_TPM0_IRQ_PRIORITY); - } -#endif - -#if KINETIS_PWM_USE_TPM1 - if (&PWMD2 == pwmp) { - SIM->SCGC6 |= SIM_SCGC6_TPM1; - nvicEnableVector(TPM1_IRQn, KINETIS_PWM_TPM1_IRQ_PRIORITY); - } -#endif - -#if KINETIS_PWM_USE_TPM2 - if (&PWMD3 == pwmp) { - SIM->SCGC6 |= SIM_SCGC6_TPM2; - nvicEnableVector(TPM2_IRQn, KINETIS_PWM_TPM2_IRQ_PRIORITY); - } -#endif - } - - /* Disable LPTPM counter.*/ - pwmp->tpm->SC = 0; - /* Clear count register.*/ - pwmp->tpm->CNT = 0; - - /* Prescaler value calculation.*/ - psc = (KINETIS_SYSCLK_FREQUENCY / pwmp->config->frequency); - /* Prescaler must be power of two between 1 and 128.*/ - osalDbgAssert(psc <= 128 && !(psc & (psc - 1)), "invalid frequency"); - /* Prescaler register value determination. - Prescaler register value conveniently corresponds to bit position, - i.e., register value for prescaler CLK/64 is 6 ((1 << 6) == 64).*/ - for (i = 0; i < 8; i++) { - if (psc == (1UL << i)) { - break; - } - } - /* Set prescaler and clock mode. - This also sets the following: - CPWM up-counting mode - Timer overflow interrupt disabled - DMA disabled.*/ - pwmp->tpm->SC = TPMx_SC_CMOD_LPTPM_CLK | i; - /* Configure period.*/ - pwmp->tpm->MOD = pwmp->period - 1; -} - -/** - * @brief Deactivates the PWM peripheral. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * - * @notapi - */ -void pwm_lld_stop(PWMDriver *pwmp) { - - /* If in ready state then disables the PWM clock.*/ - if (pwmp->state == PWM_READY) { -#if KINETIS_PWM_USE_TPM0 - if (&PWMD1 == pwmp) { - SIM->SCGC6 &= ~SIM_SCGC6_TPM0; - nvicDisableVector(TPM0_IRQn); - } -#endif - -#if KINETIS_PWM_USE_TPM1 - if (&PWMD2 == pwmp) { - SIM->SCGC6 &= ~SIM_SCGC6_TPM1; - nvicDisableVector(TPM1_IRQn); - } -#endif - -#if KINETIS_PWM_USE_TPM2 - if (&PWMD3 == pwmp) { - SIM->SCGC6 &= ~SIM_SCGC6_TPM2; - nvicDisableVector(TPM2_IRQn); - } -#endif - /* Disable LPTPM counter.*/ - pwmp->tpm->SC = 0; - pwmp->tpm->MOD = 0; - } -} - - -/** - * @brief Enables a PWM channel. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @post The channel is active using the specified configuration. - * @note The function has effect at the next cycle start. - * @note Channel notification is not enabled. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] channel PWM channel identifier (0...channels-1) - * @param[in] width PWM pulse width as clock pulses number - * - * @notapi - */ -void pwm_lld_enable_channel(PWMDriver *pwmp, - pwmchannel_t channel, - pwmcnt_t width) { - uint32_t mode = TPMx_CnSC_MSB; /* Edge-aligned PWM mode.*/ - - switch (pwmp->config->channels[channel].mode & PWM_OUTPUT_MASK) { - case PWM_OUTPUT_ACTIVE_HIGH: - mode |= TPMx_CnSC_ELSB; - break; - case PWM_OUTPUT_ACTIVE_LOW: - mode |= TPMx_CnSC_ELSA; - break; - } - - if (pwmp->tpm->C[channel].SC & TPMx_CnSC_CHIE) - mode |= TPMx_CnSC_CHIE; - - pwmp->tpm->C[channel].SC = mode; - pwmp->tpm->C[channel].V = width; -} - -/** - * @brief Disables a PWM channel and its notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @post The channel is disabled and its output line returned to the - * idle state. - * @note The function has effect at the next cycle start. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] channel PWM channel identifier (0...channels-1) - * - * @notapi - */ -void pwm_lld_disable_channel(PWMDriver *pwmp, pwmchannel_t channel) { - - pwmp->tpm->C[channel].SC = 0; - pwmp->tpm->C[channel].V = 0; -} - -/** - * @brief Enables the periodic activation edge notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @note If the notification is already enabled then the call has no effect. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * - * @notapi - */ -void pwm_lld_enable_periodic_notification(PWMDriver *pwmp) { - - pwmp->tpm->SC |= TPMx_SC_TOIE; -} - -/** - * @brief Disables the periodic activation edge notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @note If the notification is already disabled then the call has no effect. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * - * @notapi - */ -void pwm_lld_disable_periodic_notification(PWMDriver *pwmp) { - - pwmp->tpm->SC &= ~TPMx_SC_TOIE; -} - -/** - * @brief Enables a channel de-activation edge notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @pre The channel must have been activated using @p pwmEnableChannel(). - * @note If the notification is already enabled then the call has no effect. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] channel PWM channel identifier (0...channels-1) - * - * @notapi - */ -void pwm_lld_enable_channel_notification(PWMDriver *pwmp, - pwmchannel_t channel) { - - pwmp->tpm->C[channel].SC |= TPMx_CnSC_CHIE; -} - -/** - * @brief Disables a channel de-activation edge notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @pre The channel must have been activated using @p pwmEnableChannel(). - * @note If the notification is already disabled then the call has no effect. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] channel PWM channel identifier (0...channels-1) - * - * @notapi - */ -void pwm_lld_disable_channel_notification(PWMDriver *pwmp, - pwmchannel_t channel) { - - pwmp->tpm->C[channel].SC &= ~TPMx_CnSC_CHIE; -} - -#endif /* HAL_USE_PWM */ - -/** @} */ diff --git a/os/hal/ports/KINETIS/KL2x/hal_pwm_lld.h b/os/hal/ports/KINETIS/KL2x/hal_pwm_lld.h deleted file mode 100644 index 64ff9ee..0000000 --- a/os/hal/ports/KINETIS/KL2x/hal_pwm_lld.h +++ /dev/null @@ -1,305 +0,0 @@ -/* - ChibiOS - Copyright (C) 2006..2015 Adam J. Porter - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -/** - * @file KL2x/pwm_lld.h - * @brief KINETIS PWM subsystem low level driver header. - * - * @addtogroup PWM - * @{ - */ - -#ifndef HAL_PWM_LLD_H_ -#define HAL_PWM_LLD_H_ - -#if HAL_USE_PWM || defined(__DOXYGEN__) - -/*===========================================================================*/ -/* Driver constants. */ -/*===========================================================================*/ - -#if !defined(KINETIS_PWM_USE_TPM0) -#define KINETIS_PWM_USE_TPM0 FALSE -#endif -#if !defined(KINETIS_PWM_USE_TPM1) -#define KINETIS_PWM_USE_TPM1 FALSE -#endif -#if !defined(KINETIS_PWM_USE_TPM2) -#define KINETIS_PWM_USE_TPM2 FALSE -#endif - -/** - * @brief Number of PWM channels per PWM driver. - */ -#define PWM_CHANNELS 6 - -/*===========================================================================*/ -/* Driver pre-compile time settings. */ -/*===========================================================================*/ - -/** - * @name Configuration options - * @{ - */ -/** - * @brief If advanced timer features switch. - * @details If set to @p TRUE the advanced features for TIM1 and TIM8 are - * enabled. - * @note The default is @p TRUE. - */ -#if !defined(KINETIS_PWM_USE_ADVANCED) || defined(__DOXYGEN__) -#define KINETIS_PWM_USE_ADVANCED FALSE -#endif - -/** - * @brief TPM0 interrupt priority level setting. - * @note The default is 2. - */ -#if !defined(KINETIS_PWM_TPM0_IRQ_PRIORITY)|| defined(__DOXYGEN__) -#define KINETIS_PWM_TPM0_IRQ_PRIORITY 2 -#endif - -/** - * @brief TPM1 interrupt priority level setting. - * @note The default is 2. - */ -#if !defined(KINETIS_PWM_TPM1_IRQ_PRIORITY)|| defined(__DOXYGEN__) -#define KINETIS_PWM_TPM1_IRQ_PRIORITY 2 -#endif - -/** - * @brief TPM2 interrupt priority level setting. - * @note The default is 2. - */ -#if !defined(KINETIS_PWM_TPM2_IRQ_PRIORITY)|| defined(__DOXYGEN__) -#define KINETIS_PWM_TPM2_IRQ_PRIORITY 2 -#endif - -/** @} */ - -/*===========================================================================*/ -/* Configuration checks. */ -/*===========================================================================*/ - -#if KINETIS_PWM_USE_TPM0 && !KINETIS_HAS_TPM0 -#error "TPM0 not present in the selected device" -#endif - -#if KINETIS_PWM_USE_TPM1 && !KINETIS_HAS_TPM1 -#error "TPM1 not present in the selected device" -#endif - -#if KINETIS_PWM_USE_TPM2 && !KINETIS_HAS_TPM2 -#error "TPM2 not present in the selected device" -#endif - -#if !KINETIS_PWM_USE_TPM0 && !KINETIS_PWM_USE_TPM1 && !KINETIS_PWM_USE_TPM2 -#error "PWM driver activated but no TPM peripheral assigned" -#endif - -#if KINETIS_PWM_USE_TPM0 && \ - !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_PWM_TPM0_IRQ_PRIORITY) -#error "Invalid IRQ priority assigned to KINETIS_PWM_TPM0_IRQ_PRIORITY" -#endif - -#if KINETIS_PWM_USE_TPM1 && \ - !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_PWM_TPM1_IRQ_PRIORITY) -#error "Invalid IRQ priority assigned to KINETIS_PWM_TPM1_IRQ_PRIORITY" -#endif - -#if KINETIS_PWM_USE_TPM2 && \ - !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_PWM_TPM2_IRQ_PRIORITY) -#error "Invalid IRQ priority assigned to KINETIS_PWM_TPM2_IRQ_PRIORITY" -#endif - -#if !defined(KINETIS_TPM0_IRQ_VECTOR) -#error "KINETIS_TPM0_IRQ_VECTOR not defined" -#endif - -#if !defined(KINETIS_TPM1_IRQ_VECTOR) -#error "KINETIS_TPM1_IRQ_VECTOR not defined" -#endif - -#if !defined(KINETIS_TPM2_IRQ_VECTOR) -#error "KINETIS_TPM2_IRQ_VECTOR not defined" -#endif - -/*===========================================================================*/ -/* Driver data structures and types. */ -/*===========================================================================*/ - -/** - * @brief Type of a PWM mode. - */ -typedef uint32_t pwmmode_t; - -/** - * @brief Type of a PWM channel. - */ -typedef uint8_t pwmchannel_t; - -/** - * @brief Type of a channels mask. - */ -typedef uint32_t pwmchnmsk_t; - -/** - * @brief Type of a PWM counter. - */ -typedef uint16_t pwmcnt_t; - -/** - * @brief Type of a PWM driver channel configuration structure. - */ -typedef struct { - /** - * @brief Channel active logic level. - */ - pwmmode_t mode; - /** - * @brief Channel callback pointer. - * @note This callback is invoked on the channel compare event. If set to - * @p NULL then the callback is disabled. - */ - pwmcallback_t callback; - /* End of the mandatory fields.*/ -} PWMChannelConfig; - -/** - * @brief Type of a PWM driver configuration structure. - */ -typedef struct { - /** - * @brief Timer clock in Hz. - * @note The low level can use assertions in order to catch invalid - * frequency specifications. - */ - uint32_t frequency; - /** - * @brief PWM period in ticks. - * @note The low level can use assertions in order to catch invalid - * period specifications. - */ - pwmcnt_t period; - /** - * @brief Periodic callback pointer. - * @note This callback is invoked on PWM counter reset. If set to - * @p NULL then the callback is disabled. - */ - pwmcallback_t callback; - /** - * @brief Channels configurations. - */ - PWMChannelConfig channels[PWM_CHANNELS]; - /* End of the mandatory fields.*/ -} PWMConfig; - -/** - * @brief Structure representing a PWM driver. - */ -struct PWMDriver { - /** - * @brief Driver state. - */ - pwmstate_t state; - /** - * @brief Current driver configuration data. - */ - const PWMConfig *config; - /** - * @brief Current PWM period in ticks. - */ - pwmcnt_t period; - /** - * @brief Mask of the enabled channels. - */ - pwmchnmsk_t enabled; - /** - * @brief Number of channels in this instance. - */ - pwmchannel_t channels; -#if defined(PWM_DRIVER_EXT_FIELDS) - PWM_DRIVER_EXT_FIELDS -#endif - /* End of the mandatory fields.*/ - /** - * @brief Pointer to the TPM registers block. - */ - TPM_TypeDef *tpm; -}; - -/*===========================================================================*/ -/* Driver macros. */ -/*===========================================================================*/ - -/** - * @brief Changes the period the PWM peripheral. - * @details This function changes the period of a PWM unit that has already - * been activated using @p pwmStart(). - * @pre The PWM unit must have been activated using @p pwmStart(). - * @post The PWM unit period is changed to the new value. - * @note The function has effect at the next cycle start. - * @note If a period is specified that is shorter than the pulse width - * programmed in one of the channels then the behavior is not - * guaranteed. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] period new cycle time in ticks - * - * @notapi - */ -#define pwm_lld_change_period(pwmp, period) \ - ((pwmp)->tpm->MOD = ((period) - 1)) - -/*===========================================================================*/ -/* External declarations. */ -/*===========================================================================*/ - -#if KINETIS_PWM_USE_TPM0 || defined(__DOXYGEN__) -extern PWMDriver PWMD1; -#endif -#if KINETIS_PWM_USE_TPM1 || defined(__DOXYGEN__) -extern PWMDriver PWMD2; -#endif -#if KINETIS_PWM_USE_TPM2 || defined(__DOXYGEN__) -extern PWMDriver PWMD3; -#endif - -#ifdef __cplusplus -extern "C" { -#endif - void pwm_lld_init(void); - void pwm_lld_start(PWMDriver *pwmp); - void pwm_lld_stop(PWMDriver *pwmp); - void pwm_lld_enable_channel(PWMDriver *pwmp, - pwmchannel_t channel, - pwmcnt_t width); - void pwm_lld_disable_channel(PWMDriver *pwmp, pwmchannel_t channel); - void pwm_lld_enable_periodic_notification(PWMDriver *pwmp); - void pwm_lld_disable_periodic_notification(PWMDriver *pwmp); - void pwm_lld_enable_channel_notification(PWMDriver *pwmp, - pwmchannel_t channel); - void pwm_lld_disable_channel_notification(PWMDriver *pwmp, - pwmchannel_t channel); -#ifdef __cplusplus -} -#endif - -#endif /* HAL_USE_PWM */ - -#endif /* HAL_PWM_LLD_H_ */ - -/** @} */ diff --git a/os/hal/ports/KINETIS/KL2x/platform.mk b/os/hal/ports/KINETIS/KL2x/platform.mk index dda7a6d..9185022 100644 --- a/os/hal/ports/KINETIS/KL2x/platform.mk +++ b/os/hal/ports/KINETIS/KL2x/platform.mk @@ -1,17 +1,31 @@ -# List of all platform files. -PLATFORMSRC = ${CHIBIOS}/os/hal/ports/common/ARMCMx/nvic.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/KL2x/hal_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_pal_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_serial_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_i2c_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_ext_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_adc_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_gpt_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/KL2x/hal_pwm_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_st_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_usb_lld.c +PLATFORMSRC_CONTRIB := ${CHIBIOS}/os/hal/ports/common/ARMCMx/nvic.c \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/KL2x/hal_lld.c \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PITv1/hal_st_lld.c + +PLATFORMINC_CONTRIB := ${CHIBIOS}/os/hal/ports/common/ARMCMx \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/KL2x + +ifeq ($(USE_SMART_BUILD),yes) -# Required include directories -PLATFORMINC = ${CHIBIOS}/os/hal/ports/common/ARMCMx \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/KL2x \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD +# Configuration files directory +ifeq ($(CONFDIR),) + CONFDIR = . +endif + +HALCONF := $(strip $(shell cat $(CONFDIR)/halconf.h $(CONFDIR)/halconf_community.h | egrep -e "\#define")) + +endif + +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/GPIOv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/UARTv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/I2Cv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PORTv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/ADCv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PITv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/TPMv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/USBHSv1/driver.mk + +# Shared variables +ALLCSRC += $(PLATFORMSRC_CONTRIB) +ALLINC += $(PLATFORMINC_CONTRIB) diff --git a/os/hal/ports/KINETIS/LLD/ADCv1/driver.mk b/os/hal/ports/KINETIS/LLD/ADCv1/driver.mk new file mode 100644 index 0000000..5b5bcbb --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/ADCv1/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_ADC TRUE,$(HALCONF)),) +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/ADCv1/hal_adc_lld.c +endif +else +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/ADCv1/hal_adc_lld.c +endif + +PLATFORMINC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/ADCv1 diff --git a/os/hal/ports/KINETIS/LLD/FTMv1/driver.mk b/os/hal/ports/KINETIS/LLD/FTMv1/driver.mk new file mode 100644 index 0000000..d581c2e --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/FTMv1/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_PWM TRUE,$(HALCONF)),) +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/FTMv1/hal_pwm_lld.c +endif +else +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/FTMv1/hal_pwm_lld.c +endif + +PLATFORMINC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/FTMv1 diff --git a/os/hal/ports/KINETIS/LLD/FTMv1/hal_pwm_lld.c b/os/hal/ports/KINETIS/LLD/FTMv1/hal_pwm_lld.c new file mode 100644 index 0000000..8c1be78 --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/FTMv1/hal_pwm_lld.c @@ -0,0 +1,390 @@ +/* + ChibiOS/HAL - Copyright (C) 2014 Adam J. Porter + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file K20x/pwm_lld.c + * @brief KINETIS PWM subsystem low level driver source. + * + * @addtogroup PWM + * @{ + */ + +#include "hal.h" + +#if HAL_USE_PWM || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/** + * @brief PWMD1 driver identifier. + * @note The driver PWMD1 allocates the timer FTM0 when enabled. + */ +#if KINETIS_PWM_USE_FTM0 || defined(__DOXYGEN__) +PWMDriver PWMD1; +#endif + +/** + * @brief PWMD2 driver identifier. + * @note The driver PWMD2 allocates the timer FTM1 when enabled. + */ +#if KINETIS_PWM_USE_FTM1 || defined(__DOXYGEN__) +PWMDriver PWMD2; +#endif + +/** + * @brief PWMD3 driver identifier. + * @note The driver PWMD3 allocates the timer FTM2 when enabled. + */ +#if KINETIS_PWM_USE_FTM2 || defined(__DOXYGEN__) +PWMDriver PWMD3; +#endif + +/*===========================================================================*/ +/* Driver local variables and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +static void pwm_lld_serve_interrupt(PWMDriver *pwmp) { + uint32_t sr; + + sr = pwmp->ftm->SC; + pwmp->ftm->SC = sr&(~FTM_SC_TOF); + + if (((sr & FTM_SC_TOF) != 0) && /* Timer Overflow */ + ((sr & FTM_SC_TOIE) != 0) && + (pwmp->config->callback != NULL)) { + pwmp->config->callback(pwmp); + } + + uint8_t n=0; + for(n=0;nchannels;n++) { + sr = pwmp->ftm->CHANNEL[n].CnSC; + pwmp->ftm->CHANNEL[n].CnSC = sr&(~FTM_CnSC_CHF); + if (((sr & FTM_CnSC_CHF) != 0) && + ((sr & FTM_CnSC_CHIE) != 0) && + (pwmp->config->channels[n].callback != NULL)) { + pwmp->config->channels[n].callback(pwmp); + } + } +} + +/*===========================================================================*/ +/* Driver interrupt handlers. */ +/*===========================================================================*/ + +#if KINETIS_PWM_USE_FTM0 +/** + * @brief FTM0 interrupt handler. + * + * @isr + */ +OSAL_IRQ_HANDLER(KINETIS_FTM0_IRQ_VECTOR) { + OSAL_IRQ_PROLOGUE(); + pwm_lld_serve_interrupt(&PWMD1); + OSAL_IRQ_EPILOGUE(); +} +#endif /* KINETIS_PWM_USE_FTM0 */ + +#if KINETIS_PWM_USE_FTM1 +/** + * @brief FTM1 interrupt handler. + * + * @isr + */ +OSAL_IRQ_HANDLER(KINETIS_FTM1_IRQ_VECTOR) { + + OSAL_IRQ_PROLOGUE(); + pwm_lld_serve_interrupt(&PWMD2); + OSAL_IRQ_EPILOGUE(); +} +#endif /* KINETIS_PWM_USE_FTM1 */ + +#if KINETIS_PWM_USE_FTM2 +/** + * @brief FTM2 interrupt handler. + * + * @isr + */ +OSAL_IRQ_HANDLER(KINETIS_FTM2_IRQ_VECTOR) { + + OSAL_IRQ_PROLOGUE(); + pwm_lld_serve_interrupt(&PWMD3); + OSAL_IRQ_EPILOGUE(); +} +#endif /* KINETIS_PWM_USE_FTM2 */ + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief Low level PWM driver initialization. + * + * @notapi + */ +void pwm_lld_init(void) { + +#if KINETIS_PWM_USE_FTM0 + pwmObjectInit(&PWMD1); + PWMD1.channels = KINETIS_FTM0_CHANNELS; + PWMD1.ftm = FTM0; +#endif + +#if KINETIS_PWM_USE_FTM1 + pwmObjectInit(&PWMD2); + PWMD2.channels = KINETIS_FTM1_CHANNELS; + PWMD2.ftm = FTM1; +#endif + +#if KINETIS_PWM_USE_FTM2 + pwmObjectInit(&PWMD3); + PWMD3.channels = KINETIS_FTM2_CHANNELS; + PWMD3.ftm = FTM2; +#endif +} + +/** + * @brief Configures and activates the PWM peripheral. + * @note Starting a driver that is already in the @p PWM_READY state + * disables all the active channels. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * + * @notapi + */ +void pwm_lld_start(PWMDriver *pwmp) { + uint16_t psc; + uint8_t i=0; + + if (pwmp->state == PWM_STOP) { + /* Clock activation and timer reset.*/ +#if KINETIS_PWM_USE_FTM0 + if (&PWMD1 == pwmp) { + SIM->SCGC6 |= SIM_SCGC6_FTM0; + nvicEnableVector(FTM0_IRQn, KINETIS_PWM_FTM0_PRIORITY); + } +#endif + +#if KINETIS_PWM_USE_FTM1 + if (&PWMD2 == pwmp) { + SIM->SCGC6 |= SIM_SCGC6_FTM1; + nvicEnableVector(FTM1_IRQn, KINETIS_PWM_FTM1_PRIORITY); + } +#endif + +#if KINETIS_PWM_USE_FTM2 + if (&PWMD3 == pwmp) { + SIM->SCGC3 |= SIM_SCGC3_FTM2; + nvicEnableVector(FTM2_IRQn, KINETIS_PWM_FTM2_PRIORITY); + } +#endif + } + pwmp->ftm->MODE = FTM_MODE_FTMEN_MASK|FTM_MODE_PWMSYNC_MASK; + pwmp->ftm->SYNC = FTM_SYNC_CNTMIN_MASK|FTM_SYNC_CNTMAX_MASK + |FTM_SYNC_SWSYNC_MASK; + pwmp->ftm->COMBINE = FTM_COMBINE_SYNCEN3_MASK | FTM_COMBINE_SYNCEN2_MASK + | FTM_COMBINE_SYNCEN1_MASK | FTM_COMBINE_SYNCEN0_MASK; + pwmp->ftm->SYNCONF = FTM_SYNCONF_SYNCMODE_MASK; + + pwmp->ftm->CNTIN = 0x0000; + //~ pwmp->ftm->SC = 0; /* Disable FTM counter.*/ + pwmp->ftm->CNT = 0x0000; /* Clear count register.*/ + + /* Prescaler value calculation.*/ + psc = (KINETIS_SYSCLK_FREQUENCY / pwmp->config->frequency); + //~ /* Prescaler must be power of two between 1 and 128.*/ + osalDbgAssert(psc <= 128 && !(psc & (psc - 1)), "invalid frequency"); + //~ /* Prescaler register value determination. + //~ Prescaler register value conveniently corresponds to bit position, + //~ i.e., register value for prescaler CLK/64 is 6 ((1 << 6) == 64).*/ + for (i = 0; i < 8; i++) { + if (psc == (unsigned)(1 << i)) { + break; + } + } + + /* Set prescaler and clock mode. + This also sets the following: + CPWMS up-counting mode + Timer overflow interrupt disabled + DMA disabled.*/ + pwmp->ftm->SC = FTM_SC_CLKS(1) | FTM_SC_PS(i); + /* Configure period */ + pwmp->ftm->MOD = pwmp->period-1; + pwmp->ftm->PWMLOAD = FTM_PWMLOAD_LDOK_MASK; +} + +/** + * @brief Deactivates the PWM peripheral. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * + * @notapi + */ +void pwm_lld_stop(PWMDriver *pwmp) { + + /* If in ready state then disables the PWM clock.*/ + if (pwmp->state == PWM_READY) { +#if KINETIS_PWM_USE_FTM0 + if (&PWMD1 == pwmp) { + SIM->SCGC6 &= ~SIM_SCGC6_FTM0; + nvicDisableVector(FTM0_IRQn); + } +#endif + +#if KINETIS_PWM_USE_FTM1 + if (&PWMD2 == pwmp) { + SIM->SCGC6 &= ~SIM_SCGC6_FTM1; + nvicDisableVector(FTM1_IRQn); + } +#endif + +#if KINETIS_PWM_USE_FTM2 + if (&PWMD3 == pwmp) { + SIM->SCGC3 &= ~SIM_SCGC3_FTM2; + nvicDisableVector(FTM2_IRQn); + } +#endif + /* Disable FTM counter.*/ + pwmp->ftm->SC = 0; + pwmp->ftm->MOD = 0; + } +} + +/** + * @brief Enables a PWM channel. + * @pre The PWM unit must have been activated using @p pwmStart(). + * @post The channel is active using the specified configuration. + * @note The function has effect at the next cycle start. + * @note Channel notification is not enabled. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * @param[in] channel PWM channel identifier (0...channels-1) + * @param[in] width PWM pulse width as clock pulses number + * + * @notapi + */ +void pwm_lld_enable_channel(PWMDriver *pwmp, + pwmchannel_t channel, + pwmcnt_t width) { + uint32_t mode = FTM_CnSC_MSB; /* Edge-aligned PWM mode.*/ + + switch (pwmp->config->channels[channel].mode & PWM_OUTPUT_MASK) { + case PWM_OUTPUT_ACTIVE_HIGH: + mode |= FTM_CnSC_ELSB; + break; + case PWM_OUTPUT_ACTIVE_LOW: + mode |= FTM_CnSC_ELSA; + break; + } + + if (pwmp->ftm->CHANNEL[channel].CnSC & FTM_CnSC_CHIE) + mode |= FTM_CnSC_CHIE; + + pwmp->ftm->CHANNEL[channel].CnSC = mode; + pwmp->ftm->CHANNEL[channel].CnV = width; + pwmp->ftm->PWMLOAD = FTM_PWMLOAD_LDOK_MASK; +} + +/** + * @brief Disables a PWM channel and its notification. + * @pre The PWM unit must have been activated using @p pwmStart(). + * @post The channel is disabled and its output line returned to the + * idle state. + * @note The function has effect at the next cycle start. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * @param[in] channel PWM channel identifier (0...channels-1) + * + * @notapi + */ +void pwm_lld_disable_channel(PWMDriver *pwmp, pwmchannel_t channel) { + + pwmp->ftm->CHANNEL[channel].CnSC = 0; + pwmp->ftm->CHANNEL[channel].CnV = 0; +} + +/** + * @brief Enables the periodic activation edge notification. + * @pre The PWM unit must have been activated using @p pwmStart(). + * @note If the notification is already enabled then the call has no effect. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * + * @notapi + */ +void pwm_lld_enable_periodic_notification(PWMDriver *pwmp) { + pwmp->ftm->SC |= FTM_SC_TOIE; +} + +/** + * @brief Disables the periodic activation edge notification. + * @pre The PWM unit must have been activated using @p pwmStart(). + * @note If the notification is already disabled then the call has no effect. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * + * @notapi + */ +void pwm_lld_disable_periodic_notification(PWMDriver *pwmp) { + pwmp->ftm->SC &= ~FTM_SC_TOIE; +} + +/** + * @brief Enables a channel de-activation edge notification. + * @pre The PWM unit must have been activated using @p pwmStart(). + * @pre The channel must have been activated using @p pwmEnableChannel(). + * @note If the notification is already enabled then the call has no effect. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * @param[in] channel PWM channel identifier (0...channels-1) + * + * @notapi + */ +void pwm_lld_enable_channel_notification(PWMDriver *pwmp, + pwmchannel_t channel) { + pwmp->ftm->CHANNEL[channel].CnSC |= FTM_CnSC_CHIE; +} + +/** + * @brief Disables a channel de-activation edge notification. + * @pre The PWM unit must have been activated using @p pwmStart(). + * @pre The channel must have been activated using @p pwmEnableChannel(). + * @note If the notification is already disabled then the call has no effect. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * @param[in] channel PWM channel identifier (0...channels-1) + * + * @notapi + */ +void pwm_lld_disable_channel_notification(PWMDriver *pwmp, + pwmchannel_t channel) { + pwmp->ftm->CHANNEL[channel].CnSC &= ~FTM_CnSC_CHIE; +} + +#endif /* HAL_USE_PWM */ + +/** @} */ diff --git a/os/hal/ports/KINETIS/LLD/FTMv1/hal_pwm_lld.h b/os/hal/ports/KINETIS/LLD/FTMv1/hal_pwm_lld.h new file mode 100644 index 0000000..dd3b721 --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/FTMv1/hal_pwm_lld.h @@ -0,0 +1,270 @@ +/* + ChibiOS/HAL - Copyright (C) 2014 Adam J. Porter + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file K20x7/pwm_lld.h + * @brief KINETIS PWM subsystem low level driver header. + * + * @addtogroup PWM + * @{ + */ + +#ifndef HAL_PWM_LLD_H_ +#define HAL_PWM_LLD_H_ + +#if HAL_USE_PWM || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +/** + * @brief Number of PWM channels per PWM driver. + */ +#define PWM_CHANNELS 8 + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +#if !defined(KINETIS_PWM_USE_FTM0) + #define KINETIS_PWM_USE_FTM0 FALSE +#endif + +#if !defined(KINETIS_PWM_USE_FTM1) + #define KINETIS_PWM_USE_FTM1 FALSE +#endif + +#if !defined(KINETIS_PWM_USE_FTM2) + #define KINETIS_PWM_USE_FTM2 FALSE +#endif + +/** + * @brief FTM0 interrupt priority level setting. + */ +#if !defined(KINETIS_PWM_FTM0_PRIORITY) || defined(__DOXYGEN__) +#define KINETIS_PWM_FTM0_PRIORITY 12 +#endif + +/** + * @brief FTM1 interrupt priority level setting. + */ +#if !defined(KINETIS_PWM_FTM1_PRIORITY) || defined(__DOXYGEN__) +#define KINETIS_PWM_FTM1_PRIORITY 12 +#endif + +/** + * @brief FTM2 interrupt priority level setting. + */ +#if !defined(KINETIS_PWM_FTM2_PRIORITY) || defined(__DOXYGEN__) +#define KINETIS_PWM_FTM2_PRIORITY 12 +#endif + +/** @} */ + +/** + * @name Configuration options + * @{ + */ +/** + * @brief If advanced timer features switch. + * @details If set to @p TRUE the advanced features for TIM1 and TIM8 are + * enabled. + * @note The default is @p TRUE. + */ +#if !defined(KINETIS_PWM_USE_ADVANCED) || defined(__DOXYGEN__) +#define KINETIS_PWM_USE_ADVANCED FALSE +#endif +/** @} */ + +/*===========================================================================*/ +/* Configuration checks. */ +/*===========================================================================*/ + +#if !KINETIS_PWM_USE_FTM0 && !KINETIS_PWM_USE_FTM1 && !KINETIS_PWM_USE_FTM2 +#error "PWM driver activated but no FTM peripheral assigned" +#endif + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/** + * @brief Type of a PWM mode. + */ +typedef uint32_t pwmmode_t; + +/** + * @brief Type of a PWM channel. + */ +typedef uint8_t pwmchannel_t; + +/** + * @brief Type of a channels mask. + */ +typedef uint32_t pwmchnmsk_t; + +/** + * @brief Type of a PWM counter. + */ +typedef uint16_t pwmcnt_t; + +/** + * @brief Type of a PWM driver channel configuration structure. + */ +typedef struct { + /** + * @brief Channel active logic level. + */ + pwmmode_t mode; + + /** + * @brief Channel callback pointer. + * @note This callback is invoked on the channel compare event. If set to + * @p NULL then the callback is disabled. + */ + pwmcallback_t callback; + /* End of the mandatory fields.*/ +} PWMChannelConfig; + +/** + * @brief Type of a PWM driver configuration structure. + */ +typedef struct { + /** + * @brief Timer clock in Hz. + * @note The low level can use assertions in order to catch invalid + * frequency specifications. + */ + uint32_t frequency; + /** + * @brief PWM period in ticks. + * @note The low level can use assertions in order to catch invalid + * period specifications. + */ + pwmcnt_t period; + /** + * @brief Periodic callback pointer. + * @note This callback is invoked on PWM counter reset. If set to + * @p NULL then the callback is disabled. + */ + pwmcallback_t callback; + /** + * @brief Channels configurations. + */ + PWMChannelConfig channels[PWM_CHANNELS]; + /* End of the mandatory fields.*/ +} PWMConfig; + +/** + * @brief Structure representing a PWM driver. + */ +struct PWMDriver { + /** + * @brief Driver state. + */ + pwmstate_t state; + /** + * @brief Current driver configuration data. + */ + const PWMConfig *config; + /** + * @brief Current PWM period in ticks. + */ + pwmcnt_t period; + /** + * @brief Mask of the enabled channels. + */ + pwmchnmsk_t enabled; + /** + * @brief Number of channels in this instance. + */ + pwmchannel_t channels; +#if defined(PWM_DRIVER_EXT_FIELDS) + PWM_DRIVER_EXT_FIELDS +#endif + /* End of the mandatory fields.*/ + /** + * @brief Pointer to the FTM registers block. + */ + FTM_TypeDef *ftm; +}; + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/** + * @brief Changes the period the PWM peripheral. + * @details This function changes the period of a PWM unit that has already + * been activated using @p pwmStart(). + * @pre The PWM unit must have been activated using @p pwmStart(). + * @post The PWM unit period is changed to the new value. + * @note The function has effect at the next cycle start. + * @note If a period is specified that is shorter than the pulse width + * programmed in one of the channels then the behavior is not + * guaranteed. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * @param[in] period new cycle time in ticks + * + * @notapi + */ +#define pwm_lld_change_period(pwmp, period) \ + do { \ + (pwmp)->ftm->MOD = ((period) - 1); \ + pwmp->ftm->PWMLOAD = FTM_PWMLOAD_LDOK_MASK;\ + } while(0) + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#if KINETIS_PWM_USE_FTM0 || defined(__DOXYGEN__) +extern PWMDriver PWMD1; +#endif +#if KINETIS_PWM_USE_FTM1 || defined(__DOXYGEN__) +extern PWMDriver PWMD2; +#endif +#if KINETIS_PWM_USE_FTM2 || defined(__DOXYGEN__) +extern PWMDriver PWMD3; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + void pwm_lld_init(void); + void pwm_lld_start(PWMDriver *pwmp); + void pwm_lld_stop(PWMDriver *pwmp); + void pwm_lld_enable_channel(PWMDriver *pwmp, + pwmchannel_t channel, + pwmcnt_t width); + void pwm_lld_disable_channel(PWMDriver *pwmp, pwmchannel_t channel); + void pwm_lld_enable_periodic_notification(PWMDriver *pwmp); + void pwm_lld_disable_periodic_notification(PWMDriver *pwmp); + void pwm_lld_enable_channel_notification(PWMDriver *pwmp, + pwmchannel_t channel); + void pwm_lld_disable_channel_notification(PWMDriver *pwmp, + pwmchannel_t channel); +#ifdef __cplusplus +} +#endif + +#endif /* HAL_USE_PWM */ + +#endif /* HAL_PWM_LLD_H_ */ + +/** @} */ diff --git a/os/hal/ports/KINETIS/LLD/GPIOv1/driver.mk b/os/hal/ports/KINETIS/LLD/GPIOv1/driver.mk new file mode 100644 index 0000000..1e9aca0 --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/GPIOv1/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_PAL TRUE,$(HALCONF)),) +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/GPIOv1/hal_pal_lld.c +endif +else +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/GPIOv1/hal_pal_lld.c +endif + +PLATFORMINC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/GPIOv1 diff --git a/os/hal/ports/KINETIS/LLD/GPIOv1/hal_pal_lld.h b/os/hal/ports/KINETIS/LLD/GPIOv1/hal_pal_lld.h index 17125b1..376c0a6 100644 --- a/os/hal/ports/KINETIS/LLD/GPIOv1/hal_pal_lld.h +++ b/os/hal/ports/KINETIS/LLD/GPIOv1/hal_pal_lld.h @@ -82,6 +82,11 @@ typedef uint32_t ioline_t; */ typedef GPIO_TypeDef *ioportid_t; +/** + * @brief Type of an pad identifier. + */ +typedef uint32_t iopadid_t; + /** * @brief Port Configuration. * @details This structure stores the configuration parameters of all pads diff --git a/os/hal/ports/KINETIS/LLD/I2Cv1/driver.mk b/os/hal/ports/KINETIS/LLD/I2Cv1/driver.mk new file mode 100644 index 0000000..19658e5 --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/I2Cv1/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_I2C TRUE,$(HALCONF)),) +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/I2Cv1/hal_i2c_lld.c +endif +else +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/I2Cv1/hal_i2c_lld.c +endif + +PLATFORMINC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/I2Cv1 diff --git a/os/hal/ports/KINETIS/LLD/I2Cv1/hal_i2c_lld.c b/os/hal/ports/KINETIS/LLD/I2Cv1/hal_i2c_lld.c index aa47aa3..6bb9f44 100644 --- a/os/hal/ports/KINETIS/LLD/I2Cv1/hal_i2c_lld.c +++ b/os/hal/ports/KINETIS/LLD/I2Cv1/hal_i2c_lld.c @@ -442,7 +442,9 @@ static inline msg_t _i2c_txrx_timeout(I2CDriver *i2cp, i2caddr_t addr, /* wait until the bus is released */ /* Calculating the time window for the timeout on the busy bus condition.*/ start = osalOsGetSystemTimeX(); -#if defined(OSAL_TIME_MS2I) +#if defined(OSAL_MS2I) + end = start + OSAL_MS2I(KINETIS_I2C_BUSY_TIMEOUT); +#elif defined(OSAL_TIME_MS2I) end = start + OSAL_TIME_MS2I(KINETIS_I2C_BUSY_TIMEOUT); #elif defined(OSAL_TIME_MS2ST) end = start + OSAL_TIME_MS2ST(KINETIS_I2C_BUSY_TIMEOUT); @@ -458,7 +460,7 @@ static inline msg_t _i2c_txrx_timeout(I2CDriver *i2cp, i2caddr_t addr, break; /* If the system time went outside the allowed window then a timeout condition is returned.*/ - if (!osalOsIsTimeWithinX(osalOsGetSystemTimeX(), start, end)) { + if (!osalTimeIsInRangeX(osalOsGetSystemTimeX(), start, end)) { return MSG_TIMEOUT; } osalSysUnlock(); diff --git a/os/hal/ports/KINETIS/LLD/PITv1/driver.mk b/os/hal/ports/KINETIS/LLD/PITv1/driver.mk new file mode 100644 index 0000000..065556b --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/PITv1/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_GPT TRUE,$(HALCONF)),) +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PITv1/hal_gpt_lld.c +endif +else +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PITv1/hal_gpt_lld.c +endif + +PLATFORMINC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PITv1 diff --git a/os/hal/ports/KINETIS/LLD/PORTv1/driver.mk b/os/hal/ports/KINETIS/LLD/PORTv1/driver.mk new file mode 100644 index 0000000..ca3b9df --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/PORTv1/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_EXT TRUE,$(HALCONF)),) +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PORTv1/hal_ext_lld.c +endif +else +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PORTv1/hal_ext_lld.c +endif + +PLATFORMINC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PORTv1 diff --git a/os/hal/ports/KINETIS/LLD/SDHCv1/driver.mk b/os/hal/ports/KINETIS/LLD/SDHCv1/driver.mk new file mode 100644 index 0000000..69f27db --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/SDHCv1/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_SDC TRUE,$(HALCONF)),) +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/SDHCv1/hal_sdc_lld.c +endif +else +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/SDHCv1/hal_sdc_lld.c +endif + +PLATFORMINC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/SDHCv1 diff --git a/os/hal/ports/KINETIS/LLD/SPIv1/driver.mk b/os/hal/ports/KINETIS/LLD/SPIv1/driver.mk new file mode 100644 index 0000000..394b40a --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/SPIv1/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_SPI TRUE,$(HALCONF)),) +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/SPIv1/hal_spi_lld.c +endif +else +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/SPIv1/hal_spi_lld.c +endif + +PLATFORMINC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/SPIv1 diff --git a/os/hal/ports/KINETIS/LLD/SPIv1/hal_spi_lld.c b/os/hal/ports/KINETIS/LLD/SPIv1/hal_spi_lld.c new file mode 100644 index 0000000..7e643f2 --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/SPIv1/hal_spi_lld.c @@ -0,0 +1,539 @@ +/* + ChibiOS - Copyright (C) 2014-2015 Fabio Utzig + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file SPIv1/hal_spi_lld.c + * @brief KINETIS SPI subsystem low level driver source. + * + * @addtogroup SPI + * @{ + */ + +#include "hal.h" + +#if HAL_USE_SPI || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +#if !defined(KINETIS_SPI0_RX_DMA_IRQ_PRIORITY) +#define KINETIS_SPI0_RX_DMA_IRQ_PRIORITY 8 +#endif + +#if !defined(KINETIS_SPI0_RX_DMAMUX_CHANNEL) +#define KINETIS_SPI0_RX_DMAMUX_CHANNEL 0 +#endif + +#if !defined(KINETIS_SPI0_RX_DMA_CHANNEL) +#define KINETIS_SPI0_RX_DMA_CHANNEL 0 +#endif + +#if !defined(KINETIS_SPI0_TX_DMAMUX_CHANNEL) +#define KINETIS_SPI0_TX_DMAMUX_CHANNEL 1 +#endif + +#if !defined(KINETIS_SPI0_TX_DMA_CHANNEL) +#define KINETIS_SPI0_TX_DMA_CHANNEL 1 +#endif + +#if !defined(KINETIS_SPI1_RX_DMA_IRQ_PRIORITY) +#define KINETIS_SPI1_RX_DMA_IRQ_PRIORITY 8 +#endif + +#if !defined(KINETIS_SPI1_RX_DMAMUX_CHANNEL) +#define KINETIS_SPI1_RX_DMAMUX_CHANNEL 0 +#endif + +#if !defined(KINETIS_SPI1_RX_DMA_CHANNEL) +#define KINETIS_SPI1_RX_DMA_CHANNEL 0 +#endif + +#if !defined(KINETIS_SPI1_TX_DMAMUX_CHANNEL) +#define KINETIS_SPI1_TX_DMAMUX_CHANNEL 1 +#endif + +#if !defined(KINETIS_SPI1_TX_DMA_CHANNEL) +#define KINETIS_SPI1_TX_DMA_CHANNEL 1 +#endif + +#if KINETIS_SPI_USE_SPI0 +#define DMAMUX_SPI_RX_SOURCE 16 +#define DMAMUX_SPI_TX_SOURCE 17 +#endif + +#if KINETIS_SPI_USE_SPI1 +#define DMAMUX_SPI_RX_SOURCE 18 +#define DMAMUX_SPI_TX_SOURCE 19 +#endif + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/** @brief SPI0 driver identifier.*/ +#if KINETIS_SPI_USE_SPI0 || defined(__DOXYGEN__) +SPIDriver SPID1; +#endif + +/** @brief SPI1 driver identifier.*/ +#if KINETIS_SPI_USE_SPI1 || defined(__DOXYGEN__) +SPIDriver SPID2; +#endif + +/*===========================================================================*/ +/* Driver local variables and types. */ +/*===========================================================================*/ + +/* Use a dummy byte as the source/destination when a buffer is not provided */ +/* Note: The MMC driver relies on 0xFF being sent for dummy bytes. */ +static volatile uint16_t dmaRxDummy; +static uint16_t dmaTxDummy = 0xFFFF; + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +static void spi_start_xfer(SPIDriver *spip, bool polling) +{ + /* + * Enable the DSPI peripheral in master mode. + * Clear the TX and RX FIFOs. + * */ + spip->spi->MCR = SPIx_MCR_MSTR | SPIx_MCR_CLR_TXF | SPIx_MCR_CLR_RXF; + + /* If we are not polling then enable DMA */ + if (!polling) { + + /* Enable receive dma and transmit dma */ + spip->spi->RSER = SPIx_RSER_RFDF_DIRS | SPIx_RSER_RFDF_RE | + SPIx_RSER_TFFF_RE | SPIx_RSER_TFFF_DIRS; + + /* Configure RX DMA */ + if (spip->rxbuf) { + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DADDR = (uint32_t)spip->rxbuf; + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DOFF = spip->word_size; + } else { + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DADDR = (uint32_t)&dmaRxDummy; + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DOFF = 0; + } + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].BITER_ELINKNO = spip->count; + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].CITER_ELINKNO = spip->count; + + /* Enable Request Register (ERQ) for RX by writing 0 to SERQ */ + DMA->SERQ = KINETIS_SPI0_RX_DMA_CHANNEL; + + /* Configure TX DMA */ + if (spip->txbuf) { + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SADDR = (uint32_t)spip->txbuf; + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SOFF = spip->word_size; + } else { + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SADDR = (uint32_t)&dmaTxDummy; + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SOFF = 0; + } + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].BITER_ELINKNO = spip->count; + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].CITER_ELINKNO = spip->count; + + /* Enable Request Register (ERQ) for TX by writing 1 to SERQ */ + DMA->SERQ = KINETIS_SPI0_TX_DMA_CHANNEL; + } +} + +static void spi_stop_xfer(SPIDriver *spip) +{ + /* Halt the DSPI peripheral */ + spip->spi->MCR = SPIx_MCR_MSTR | SPIx_MCR_HALT; + + /* Clear all the flags which are currently set. */ + spip->spi->SR |= spip->spi->SR; +} + +/*===========================================================================*/ +/* Driver interrupt handlers. */ +/*===========================================================================*/ + +#if KINETIS_SPI_USE_SPI0 || defined(__DOXYGEN__) + +OSAL_IRQ_HANDLER(KINETIS_DMA0_IRQ_VECTOR) { + OSAL_IRQ_PROLOGUE(); + + /* Clear bit 0 in Interrupt Request Register (INT) by writing 0 to CINT */ + DMA->CINT = KINETIS_SPI0_RX_DMA_CHANNEL; + + spi_stop_xfer(&SPID1); + + _spi_isr_code(&SPID1); + + OSAL_IRQ_EPILOGUE(); +} + +#endif + +#if KINETIS_SPI_USE_SPI1 || defined(__DOXYGEN__) + +OSAL_IRQ_HANDLER(KINETIS_DMA0_IRQ_VECTOR) { + OSAL_IRQ_PROLOGUE(); + + /* Clear bit 0 in Interrupt Request Register (INT) by writing 0 to CINT */ + DMA->CINT = KINETIS_SPI1_RX_DMA_CHANNEL; + + spi_stop_xfer(&SPID2); + + _spi_isr_code(&SPID2); + + OSAL_IRQ_EPILOGUE(); +} + +#endif + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief Low level SPI driver initialization. + * + * @notapi + */ +void spi_lld_init(void) { +#if KINETIS_SPI_USE_SPI0 + spiObjectInit(&SPID1); +#endif +#if KINETIS_SPI_USE_SPI1 + spiObjectInit(&SPID2); +#endif +} + +/** + * @brief Configures and activates the SPI peripheral. + * + * @param[in] spip pointer to the @p SPIDriver object + * + * @notapi + */ +void spi_lld_start(SPIDriver *spip) { + + /* If in stopped state then enables the SPI and DMA clocks.*/ + if (spip->state == SPI_STOP) { + +#if KINETIS_SPI_USE_SPI0 + if (&SPID1 == spip) { + + /* Enable the clock for SPI0 */ + SIM->SCGC6 |= SIM_SCGC6_SPI0; + + SPID1.spi = SPI0; + + if (spip->config->tar0) { + spip->spi->CTAR[0] = spip->config->tar0; + } else { + spip->spi->CTAR[0] = KINETIS_SPI_TAR0_DEFAULT; + } + } +#endif + +#if KINETIS_SPI_USE_SPI1 + if (&SPID2 == spip) { + + /* Enable the clock for SPI0 */ + SIM->SCGC6 |= SIM_SCGC6_SPI1; + + SPID2.spi = SPI1; + + if (spip->config->tar0) { + spip->spi->CTAR[0] = spip->config->tar0; + } else { + spip->spi->CTAR[0] = KINETIS_SPI_TAR0_DEFAULT; + } + } +#endif + + nvicEnableVector(DMA0_IRQn, KINETIS_SPI0_RX_DMA_IRQ_PRIORITY); + + SIM->SCGC6 |= SIM_SCGC6_DMAMUX; + SIM->SCGC7 |= SIM_SCGC7_DMA; + + /* Clear DMA error flags */ + DMA->ERR = 0x0F; + +#if KINETIS_SPI_USE_SPI0 + /* Rx, select SPI Rx FIFO */ + DMAMUX->CHCFG[KINETIS_SPI0_RX_DMAMUX_CHANNEL] = DMAMUX_CHCFGn_ENBL | + DMAMUX_CHCFGn_SOURCE(DMAMUX_SPI_RX_SOURCE); + + /* Tx, select SPI Tx FIFO */ + DMAMUX->CHCFG[KINETIS_SPI0_TX_DMAMUX_CHANNEL] = DMAMUX_CHCFGn_ENBL | + DMAMUX_CHCFGn_SOURCE(DMAMUX_SPI_TX_SOURCE); + + /* Extract the frame size from the TAR */ + uint16_t frame_size = ((spip->spi->CTAR[0] >> SPIx_CTARn_FMSZ_SHIFT) & + SPIx_CTARn_FMSZ_MASK) + 1; + + /* DMA transfer size is 16 bits for a frame size > 8 bits */ + uint16_t dma_size = frame_size > 8 ? 1 : 0; + + /* DMA word size is 2 for a 16 bit frame size */ + spip->word_size = frame_size > 8 ? 2 : 1; + + /* configure DMA RX fixed values */ + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].SADDR = (uint32_t)&SPI0->POPR; + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].SOFF = 0; + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].SLAST = 0; + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DLASTSGA = 0; + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].ATTR = DMA_ATTR_SSIZE(dma_size) | + DMA_ATTR_DSIZE(dma_size); + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].NBYTES_MLNO = spip->word_size; + DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].CSR = DMA_CSR_DREQ_MASK | + DMA_CSR_INTMAJOR_MASK; + + /* configure DMA TX fixed values */ + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SLAST = 0; + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].DADDR = (uint32_t)&SPI0->PUSHR; + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].DOFF = 0; + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].DLASTSGA = 0; + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].ATTR = DMA_ATTR_SSIZE(dma_size) | + DMA_ATTR_DSIZE(dma_size); + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].NBYTES_MLNO = spip->word_size; + DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].CSR = DMA_CSR_DREQ_MASK; +#endif + +#if KINETIS_SPI_USE_SPI1 + /* Rx, select SPI Rx FIFO */ + DMAMUX->CHCFG[KINETIS_SPI1_RX_DMAMUX_CHANNEL] = DMAMUX_CHCFGn_ENBL | + DMAMUX_CHCFGn_SOURCE(DMAMUX_SPI_RX_SOURCE); + + /* Tx, select SPI Tx FIFO */ + DMAMUX->CHCFG[KINETIS_SPI1_TX_DMAMUX_CHANNEL] = DMAMUX_CHCFGn_ENBL | + DMAMUX_CHCFGn_SOURCE(DMAMUX_SPI_TX_SOURCE); + + /* Extract the frame size from the TAR */ + uint16_t frame_size = ((spip->spi->CTAR[0] >> SPIx_CTARn_FMSZ_SHIFT) & + SPIx_CTARn_FMSZ_MASK) + 1; + + /* DMA transfer size is 16 bits for a frame size > 8 bits */ + uint16_t dma_size = frame_size > 8 ? 1 : 0; + + /* DMA word size is 2 for a 16 bit frame size */ + spip->word_size = frame_size > 8 ? 2 : 1; + + /* configure DMA RX fixed values */ + DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].SADDR = (uint32_t)&SPI1->POPR; + DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].SOFF = 0; + DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].SLAST = 0; + DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].DLASTSGA = 0; + DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].ATTR = DMA_ATTR_SSIZE(dma_size) | + DMA_ATTR_DSIZE(dma_size); + DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].NBYTES_MLNO = spip->word_size; + DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].CSR = DMA_CSR_DREQ_MASK | + DMA_CSR_INTMAJOR_MASK; + + /* configure DMA TX fixed values */ + DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].SLAST = 0; + DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].DADDR = (uint32_t)&SPI1->PUSHR; + DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].DOFF = 0; + DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].DLASTSGA = 0; + DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].ATTR = DMA_ATTR_SSIZE(dma_size) | + DMA_ATTR_DSIZE(dma_size); + DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].NBYTES_MLNO = spip->word_size; + DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].CSR = DMA_CSR_DREQ_MASK; +#endif + } +} + +/** + * @brief Deactivates the SPI peripheral. + * + * @param[in] spip pointer to the @p SPIDriver object + * + * @notapi + */ +void spi_lld_stop(SPIDriver *spip) { + + /* If in ready state then disables the SPI clock.*/ + if (spip->state == SPI_READY) { + + nvicDisableVector(DMA0_IRQn); + + SIM->SCGC7 &= ~SIM_SCGC7_DMA; + SIM->SCGC6 &= ~SIM_SCGC6_DMAMUX; + +#if KINETIS_SPI_USE_SPI0 + if (&SPID1 == spip) { + /* SPI halt.*/ + spip->spi->MCR |= SPIx_MCR_HALT; + } + + /* Disable the clock for SPI0 */ + SIM->SCGC6 &= ~SIM_SCGC6_SPI0; +#endif + +#if KINETIS_SPI_USE_SPI1 + if (&SPID2 == spip) { + /* SPI halt.*/ + spip->spi->MCR |= SPIx_MCR_HALT; + } + + /* Disable the clock for SPI1 */ + SIM->SCGC6 &= ~SIM_SCGC6_SPI1; +#endif + } +} + +/** + * @brief Asserts the slave select signal and prepares for transfers. + * + * @param[in] spip pointer to the @p SPIDriver object + * + * @notapi + */ +void spi_lld_select(SPIDriver *spip) { + + palClearPad(spip->config->ssport, spip->config->sspad); +} + +/** + * @brief Deasserts the slave select signal. + * @details The previously selected peripheral is unselected. + * + * @param[in] spip pointer to the @p SPIDriver object + * + * @notapi + */ +void spi_lld_unselect(SPIDriver *spip) { + + palSetPad(spip->config->ssport, spip->config->sspad); +} + +/** + * @brief Ignores data on the SPI bus. + * @details This asynchronous function starts the transmission of a series of + * idle words on the SPI bus and ignores the received data. + * @post At the end of the operation the configured callback is invoked. + * + * @param[in] spip pointer to the @p SPIDriver object + * @param[in] n number of words to be ignored + * + * @notapi + */ +void spi_lld_ignore(SPIDriver *spip, size_t n) { + + spip->count = n; + spip->rxbuf = NULL; + spip->txbuf = NULL; + + spi_start_xfer(spip, false); +} + +/** + * @brief Exchanges data on the SPI bus. + * @details This asynchronous function starts a simultaneous transmit/receive + * operation. + * @post At the end of the operation the configured callback is invoked. + * @note The buffers are organized as uint8_t arrays for data sizes below or + * equal to 8 bits else it is organized as uint16_t arrays. + * + * @param[in] spip pointer to the @p SPIDriver object + * @param[in] n number of words to be exchanged + * @param[in] txbuf the pointer to the transmit buffer + * @param[out] rxbuf the pointer to the receive buffer + * + * @notapi + */ +void spi_lld_exchange(SPIDriver *spip, size_t n, + const void *txbuf, void *rxbuf) { + + spip->count = n; + spip->rxbuf = rxbuf; + spip->txbuf = txbuf; + + spi_start_xfer(spip, false); +} + +/** + * @brief Sends data over the SPI bus. + * @details This asynchronous function starts a transmit operation. + * @post At the end of the operation the configured callback is invoked. + * @note The buffers are organized as uint8_t arrays for data sizes below or + * equal to 8 bits else it is organized as uint16_t arrays. + * + * @param[in] spip pointer to the @p SPIDriver object + * @param[in] n number of words to send + * @param[in] txbuf the pointer to the transmit buffer + * + * @notapi + */ +void spi_lld_send(SPIDriver *spip, size_t n, const void *txbuf) { + + spip->count = n; + spip->rxbuf = NULL; + spip->txbuf = (void *)txbuf; + + spi_start_xfer(spip, false); +} + +/** + * @brief Receives data from the SPI bus. + * @details This asynchronous function starts a receive operation. + * @post At the end of the operation the configured callback is invoked. + * @note The buffers are organized as uint8_t arrays for data sizes below or + * equal to 8 bits else it is organized as uint16_t arrays. + * + * @param[in] spip pointer to the @p SPIDriver object + * @param[in] n number of words to receive + * @param[out] rxbuf the pointer to the receive buffer + * + * @notapi + */ +void spi_lld_receive(SPIDriver *spip, size_t n, void *rxbuf) { + + spip->count = n; + spip->rxbuf = rxbuf; + spip->txbuf = NULL; + + spi_start_xfer(spip, false); +} + +/** + * @brief Exchanges one frame using a polled wait. + * @details This synchronous function exchanges one frame using a polled + * synchronization method. This function is useful when exchanging + * small amount of data on high speed channels, usually in this + * situation is much more efficient just wait for completion using + * polling than suspending the thread waiting for an interrupt. + * + * @param[in] spip pointer to the @p SPIDriver object + * @param[in] frame the data frame to send over the SPI bus + * @return The received data frame from the SPI bus. + */ +uint16_t spi_lld_polled_exchange(SPIDriver *spip, uint16_t frame) { + + spi_start_xfer(spip, true); + + spip->spi->PUSHR = SPIx_PUSHR_TXDATA(frame); + + while ((spip->spi->SR & SPIx_SR_RFDF) == 0) + ; + + frame = spip->spi->POPR; + + spi_stop_xfer(spip); + + return frame; +} + +#endif /* HAL_USE_SPI */ + +/** @} */ diff --git a/os/hal/ports/KINETIS/LLD/SPIv1/hal_spi_lld.h b/os/hal/ports/KINETIS/LLD/SPIv1/hal_spi_lld.h new file mode 100644 index 0000000..d25dd75 --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/SPIv1/hal_spi_lld.h @@ -0,0 +1,261 @@ +/* + ChibiOS - Copyright (C) 2014-2015 Fabio Utzig + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file SPIv1/hal_spi_lld.h + * @brief KINETIS SPI subsystem low level driver header. + * + * @addtogroup SPI + * @{ + */ + +#ifndef HAL_SPI_LLD_H_ +#define HAL_SPI_LLD_H_ + +#if HAL_USE_SPI || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @name Configuration options + * @{ + */ +/** + * @brief SPI0 driver enable switch. + * @details If set to @p TRUE the support for SPI0 is included. + * @note The default is @p FALSE. + */ +#if !defined(KINETIS_SPI_USE_SPI0) || defined(__DOXYGEN__) +#define KINETIS_SPI_USE_SPI0 FALSE +#endif + +/** + * @brief SPI0 interrupt priority level setting. + */ +#if !defined(KINETIS_SPI_SPI0_IRQ_PRIORITY) || defined(__DOXYGEN__) +#define KINETIS_SPI_SPI0_IRQ_PRIORITY 10 +#endif + +/** + * @brief SPI1 driver enable switch. + * @details If set to @p TRUE the support for SPI0 is included. + * @note The default is @p FALSE. + */ +#if !defined(KINETIS_SPI_USE_SPI1) || defined(__DOXYGEN__) +#define KINETIS_SPI_USE_SPI1 FALSE +#endif + +/** + * @brief SPI1 interrupt priority level setting. + */ +#if !defined(KINETIS_SPI_SPI1_IRQ_PRIORITY) || defined(__DOXYGEN__) +#define KINETIS_SPI_SPI1_IRQ_PRIORITY 10 +#endif + +/*===========================================================================*/ +/* Derived constants and error checks. */ +/*===========================================================================*/ + +#if KINETIS_SPI_USE_SPI0 && !KINETIS_HAS_SPI0 +#error "SPI0 not present in the selected device" +#endif + +#if KINETIS_SPI_USE_SPI1 && !KINETIS_HAS_SPI1 +#error "SPI1 not present in the selected device" +#endif + +#if KINETIS_SPI_USE_SPI0 && KINETIS_SPI_USE_SPI1 +#error "Only one SPI peripheral can be enabled" +#endif + +#if !(KINETIS_SPI_USE_SPI0 || KINETIS_SPI_USE_SPI1) +#error "SPI driver activated but no SPI peripheral assigned" +#endif + +#if KINETIS_SPI_USE_SPI0 && \ + !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_SPI_SPI0_IRQ_PRIORITY) +#error "Invalid IRQ priority assigned to SPI0" +#endif + +#if KINETIS_SPI_USE_SPI1 && \ + !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_SPI_SPI1_IRQ_PRIORITY) +#error "Invalid IRQ priority assigned to SPI1" +#endif + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/** + * @brief Type of a structure representing an SPI driver. + */ +typedef struct SPIDriver SPIDriver; + +/** + * @brief SPI notification callback type. + * + * @param[in] spip pointer to the @p SPIDriver object triggering the + * callback + */ +typedef void (*spicallback_t)(SPIDriver *spip); + +/** + * @brief Driver configuration structure. + */ +typedef struct { + /** + * @brief Operation complete callback or @p NULL. + */ + spicallback_t end_cb; + /* End of the mandatory fields.*/ + /** + * @brief The chip select line port - when not using pcs. + */ + ioportid_t ssport; + /** + * @brief The chip select line pad number - when not using pcs. + */ + uint16_t sspad; + /** + * @brief SPI initialization data. + */ + uint32_t tar0; +} SPIConfig; + +/** + * @brief Structure representing a SPI driver. + */ +struct SPIDriver { + /** + * @brief Driver state. + */ + spistate_t state; + /** + * @brief Current configuration data. + */ + const SPIConfig *config; +#if SPI_USE_WAIT || defined(__DOXYGEN__) + /** + * @brief Waiting thread. + */ + thread_reference_t thread; +#endif /* SPI_USE_WAIT */ +#if SPI_USE_MUTUAL_EXCLUSION || defined(__DOXYGEN__) + /** + * @brief Mutex protecting the bus. + */ + mutex_t mutex; +#endif /* SPI_USE_MUTUAL_EXCLUSION */ +#if defined(SPI_DRIVER_EXT_FIELDS) + SPI_DRIVER_EXT_FIELDS +#endif + /* End of the mandatory fields.*/ + /** + * @brief Pointer to the SPIx registers block. + */ + SPI_TypeDef *spi; + /** + * @brief Number of bytes/words of data to transfer. + */ + size_t count; + /** + * @brief Word size in bytes. + */ + size_t word_size; + /** + * @brief Pointer to the buffer with data to send. + */ + const uint8_t *txbuf; + /** + * @brief Pointer to the buffer to put received data. + */ + uint8_t *rxbuf; +}; + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/* TAR settings for n bits at SYSCLK / 2 */ +#define KINETIS_SPI_TAR_SYSCLK_DIV_2(n)\ + SPIx_CTARn_FMSZ((n) - 1) | \ + SPIx_CTARn_CPOL | \ + SPIx_CTARn_CPHA | \ + SPIx_CTARn_DBR | \ + SPIx_CTARn_PBR(0) | \ + SPIx_CTARn_BR(0) | \ + SPIx_CTARn_CSSCK(0) | \ + SPIx_CTARn_ASC(0) | \ + SPIx_CTARn_DT(0) + +/* TAR settings for n bits at SYSCLK / 4096 for debugging */ +#define KINETIS_SPI_TAR_SYSCLK_DIV_4096(n) \ + SPIx_CTARn_FMSZ(((n) - 1)) | \ + SPIx_CTARn_CPOL | \ + SPIx_CTARn_CPHA | \ + SPIx_CTARn_PBR(0) | \ + SPIx_CTARn_BR(0xB) | \ + SPIx_CTARn_CSSCK(0xB) | \ + SPIx_CTARn_ASC(0x7) | \ + SPIx_CTARn_DT(0xB) + +#define KINETIS_SPI_TAR_8BIT_FAST KINETIS_SPI_TAR_SYSCLK_DIV_2(8) +#define KINETIS_SPI_TAR_8BIT_SLOW KINETIS_SPI_TAR_SYSCLK_DIV_4096(8) + +#define KINETIS_SPI_TAR0_DEFAULT KINETIS_SPI_TAR_SYSCLK_DIV_2(8) +#define KINETIS_SPI_TAR1_DEFAULT KINETIS_SPI_TAR_SYSCLK_DIV_2(8) + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#if KINETIS_SPI_USE_SPI0 && !defined(__DOXYGEN__) +extern SPIDriver SPID1; +#endif + +#if KINETIS_SPI_USE_SPI1 && !defined(__DOXYGEN__) +extern SPIDriver SPID2; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + void spi_lld_init(void); + void spi_lld_start(SPIDriver *spip); + void spi_lld_stop(SPIDriver *spip); + void spi_lld_select(SPIDriver *spip); + void spi_lld_unselect(SPIDriver *spip); + void spi_lld_ignore(SPIDriver *spip, size_t n); + void spi_lld_exchange(SPIDriver *spip, size_t n, + const void *txbuf, void *rxbuf); + void spi_lld_send(SPIDriver *spip, size_t n, const void *txbuf); + void spi_lld_receive(SPIDriver *spip, size_t n, void *rxbuf); + uint16_t spi_lld_polled_exchange(SPIDriver *spip, uint16_t frame); +#ifdef __cplusplus +} +#endif + +#endif /* HAL_USE_SPI */ + +#endif /* HAL_SPI_LLD_H_ */ + +/** @} */ diff --git a/os/hal/ports/KINETIS/LLD/TPMv1/driver.mk b/os/hal/ports/KINETIS/LLD/TPMv1/driver.mk new file mode 100644 index 0000000..d4a0eef --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/TPMv1/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_PWM TRUE,$(HALCONF)),) +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/TPMv1/hal_pwm_lld.c +endif +else +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/TPMv1/hal_pwm_lld.c +endif + +PLATFORMINC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/TPMv1 diff --git a/os/hal/ports/KINETIS/LLD/TPMv1/hal_pwm_lld.c b/os/hal/ports/KINETIS/LLD/TPMv1/hal_pwm_lld.c new file mode 100644 index 0000000..a5eb46a --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/TPMv1/hal_pwm_lld.c @@ -0,0 +1,388 @@ +/* + ChibiOS - Copyright (C) 2014 Adam J. Porter + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file KL2x/pwm_lld.c + * @brief KINETIS PWM subsystem low level driver source. + * + * @addtogroup PWM + * @{ + */ + +#include "hal.h" + +#if HAL_USE_PWM || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/** + * @brief PWMD1 driver identifier. + * @note The driver PWMD1 allocates the timer TPM0 when enabled. + */ +#if KINETIS_PWM_USE_TPM0 || defined(__DOXYGEN__) +PWMDriver PWMD1; +#endif + +/** + * @brief PWMD2 driver identifier. + * @note The driver PWMD2 allocates the timer TPM1 when enabled. + */ +#if KINETIS_PWM_USE_TPM1 || defined(__DOXYGEN__) +PWMDriver PWMD2; +#endif + +/** + * @brief PWMD3 driver identifier. + * @note The driver PWMD3 allocates the timer TPM2 when enabled. + */ +#if KINETIS_PWM_USE_TPM2 || defined(__DOXYGEN__) +PWMDriver PWMD3; +#endif + +/*===========================================================================*/ +/* Driver local variables and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +static void pwm_lld_serve_interrupt(PWMDriver *pwmp) { + uint32_t sr; + + sr = pwmp->tpm->STATUS; + pwmp->tpm->STATUS = 0xFFFFFFFF; + + if (((sr & TPMx_STATUS_TOF) != 0) && + (pwmp->config->callback != NULL)) + pwmp->config->callback(pwmp); + if (((sr & TPMx_STATUS_CH0F) != 0) && + (pwmp->config->channels[0].callback != NULL)) + pwmp->config->channels[0].callback(pwmp); + if (((sr & TPMx_STATUS_CH1F) != 0) && + (pwmp->config->channels[1].callback != NULL)) + pwmp->config->channels[1].callback(pwmp); + if (((sr & TPMx_STATUS_CH2F) != 0) && + (pwmp->config->channels[2].callback != NULL)) + pwmp->config->channels[2].callback(pwmp); + if (((sr & TPMx_STATUS_CH3F) != 0) && + (pwmp->config->channels[3].callback != NULL)) + pwmp->config->channels[3].callback(pwmp); + if (((sr & TPMx_STATUS_CH4F) != 0) && + (pwmp->config->channels[4].callback != NULL)) + pwmp->config->channels[4].callback(pwmp); + if (((sr & TPMx_STATUS_CH5F) != 0) && + (pwmp->config->channels[5].callback != NULL)) + pwmp->config->channels[5].callback(pwmp); +} + +/*===========================================================================*/ +/* Driver interrupt handlers. */ +/*===========================================================================*/ + +#if KINETIS_PWM_USE_TPM0 +/** + * @brief TPM0 interrupt handler. + * + * @isr + */ +OSAL_IRQ_HANDLER(KINETIS_TPM0_IRQ_VECTOR) { + + OSAL_IRQ_PROLOGUE(); + pwm_lld_serve_interrupt(&PWMD1); + OSAL_IRQ_EPILOGUE(); +} +#endif /* KINETIS_PWM_USE_TPM0 */ + +#if KINETIS_PWM_USE_TPM1 +/** + * @brief TPM1 interrupt handler. + * + * @isr + */ +OSAL_IRQ_HANDLER(KINETIS_TPM1_IRQ_VECTOR) { + + OSAL_IRQ_PROLOGUE(); + pwm_lld_serve_interrupt(&PWMD2); + OSAL_IRQ_EPILOGUE(); +} +#endif /* KINETIS_PWM_USE_TPM1 */ + +#if KINETIS_PWM_USE_TPM2 +/** + * @brief TPM2 interrupt handler. + * + * @isr + */ +OSAL_IRQ_HANDLER(KINETIS_TPM2_IRQ_VECTOR) { + + OSAL_IRQ_PROLOGUE(); + pwm_lld_serve_interrupt(&PWMD3); + OSAL_IRQ_EPILOGUE(); +} +#endif /* KINETIS_PWM_USE_TPM2 */ + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +/** + * @brief Low level PWM driver initialization. + * + * @notapi + */ +void pwm_lld_init(void) { + +#if KINETIS_PWM_USE_TPM0 + pwmObjectInit(&PWMD1); + PWMD1.channels = KINETIS_TPM0_CHANNELS; + PWMD1.tpm = TPM0; +#endif + +#if KINETIS_PWM_USE_TPM1 + pwmObjectInit(&PWMD2); + PWMD2.channels = KINETIS_TPM1_CHANNELS; + PWMD2.tpm = TPM1; +#endif + +#if KINETIS_PWM_USE_TPM2 + pwmObjectInit(&PWMD3); + PWMD3.channels = KINETIS_TPM2_CHANNELS; + PWMD3.tpm = TPM2; +#endif +} + +/** + * @brief Configures and activates the PWM peripheral. + * @note Starting a driver that is already in the @p PWM_READY state + * disables all the active channels. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * + * @notapi + */ +void pwm_lld_start(PWMDriver *pwmp) { + uint32_t psc; + int i; + + if (pwmp->state == PWM_STOP) { + /* Clock activation and timer reset.*/ +#if KINETIS_PWM_USE_TPM0 + if (&PWMD1 == pwmp) { + SIM->SCGC6 |= SIM_SCGC6_TPM0; + nvicEnableVector(TPM0_IRQn, KINETIS_PWM_TPM0_IRQ_PRIORITY); + } +#endif + +#if KINETIS_PWM_USE_TPM1 + if (&PWMD2 == pwmp) { + SIM->SCGC6 |= SIM_SCGC6_TPM1; + nvicEnableVector(TPM1_IRQn, KINETIS_PWM_TPM1_IRQ_PRIORITY); + } +#endif + +#if KINETIS_PWM_USE_TPM2 + if (&PWMD3 == pwmp) { + SIM->SCGC6 |= SIM_SCGC6_TPM2; + nvicEnableVector(TPM2_IRQn, KINETIS_PWM_TPM2_IRQ_PRIORITY); + } +#endif + } + + /* Disable LPTPM counter.*/ + pwmp->tpm->SC = 0; + /* Clear count register.*/ + pwmp->tpm->CNT = 0; + + /* Prescaler value calculation.*/ + psc = (KINETIS_SYSCLK_FREQUENCY / pwmp->config->frequency); + /* Prescaler must be power of two between 1 and 128.*/ + osalDbgAssert(psc <= 128 && !(psc & (psc - 1)), "invalid frequency"); + /* Prescaler register value determination. + Prescaler register value conveniently corresponds to bit position, + i.e., register value for prescaler CLK/64 is 6 ((1 << 6) == 64).*/ + for (i = 0; i < 8; i++) { + if (psc == (1UL << i)) { + break; + } + } + /* Set prescaler and clock mode. + This also sets the following: + CPWM up-counting mode + Timer overflow interrupt disabled + DMA disabled.*/ + pwmp->tpm->SC = TPMx_SC_CMOD_LPTPM_CLK | i; + /* Configure period.*/ + pwmp->tpm->MOD = pwmp->period - 1; +} + +/** + * @brief Deactivates the PWM peripheral. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * + * @notapi + */ +void pwm_lld_stop(PWMDriver *pwmp) { + + /* If in ready state then disables the PWM clock.*/ + if (pwmp->state == PWM_READY) { +#if KINETIS_PWM_USE_TPM0 + if (&PWMD1 == pwmp) { + SIM->SCGC6 &= ~SIM_SCGC6_TPM0; + nvicDisableVector(TPM0_IRQn); + } +#endif + +#if KINETIS_PWM_USE_TPM1 + if (&PWMD2 == pwmp) { + SIM->SCGC6 &= ~SIM_SCGC6_TPM1; + nvicDisableVector(TPM1_IRQn); + } +#endif + +#if KINETIS_PWM_USE_TPM2 + if (&PWMD3 == pwmp) { + SIM->SCGC6 &= ~SIM_SCGC6_TPM2; + nvicDisableVector(TPM2_IRQn); + } +#endif + /* Disable LPTPM counter.*/ + pwmp->tpm->SC = 0; + pwmp->tpm->MOD = 0; + } +} + + +/** + * @brief Enables a PWM channel. + * @pre The PWM unit must have been activated using @p pwmStart(). + * @post The channel is active using the specified configuration. + * @note The function has effect at the next cycle start. + * @note Channel notification is not enabled. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * @param[in] channel PWM channel identifier (0...channels-1) + * @param[in] width PWM pulse width as clock pulses number + * + * @notapi + */ +void pwm_lld_enable_channel(PWMDriver *pwmp, + pwmchannel_t channel, + pwmcnt_t width) { + uint32_t mode = TPMx_CnSC_MSB; /* Edge-aligned PWM mode.*/ + + switch (pwmp->config->channels[channel].mode & PWM_OUTPUT_MASK) { + case PWM_OUTPUT_ACTIVE_HIGH: + mode |= TPMx_CnSC_ELSB; + break; + case PWM_OUTPUT_ACTIVE_LOW: + mode |= TPMx_CnSC_ELSA; + break; + } + + if (pwmp->tpm->C[channel].SC & TPMx_CnSC_CHIE) + mode |= TPMx_CnSC_CHIE; + + pwmp->tpm->C[channel].SC = mode; + pwmp->tpm->C[channel].V = width; +} + +/** + * @brief Disables a PWM channel and its notification. + * @pre The PWM unit must have been activated using @p pwmStart(). + * @post The channel is disabled and its output line returned to the + * idle state. + * @note The function has effect at the next cycle start. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * @param[in] channel PWM channel identifier (0...channels-1) + * + * @notapi + */ +void pwm_lld_disable_channel(PWMDriver *pwmp, pwmchannel_t channel) { + + pwmp->tpm->C[channel].SC = 0; + pwmp->tpm->C[channel].V = 0; +} + +/** + * @brief Enables the periodic activation edge notification. + * @pre The PWM unit must have been activated using @p pwmStart(). + * @note If the notification is already enabled then the call has no effect. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * + * @notapi + */ +void pwm_lld_enable_periodic_notification(PWMDriver *pwmp) { + + pwmp->tpm->SC |= TPMx_SC_TOIE; +} + +/** + * @brief Disables the periodic activation edge notification. + * @pre The PWM unit must have been activated using @p pwmStart(). + * @note If the notification is already disabled then the call has no effect. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * + * @notapi + */ +void pwm_lld_disable_periodic_notification(PWMDriver *pwmp) { + + pwmp->tpm->SC &= ~TPMx_SC_TOIE; +} + +/** + * @brief Enables a channel de-activation edge notification. + * @pre The PWM unit must have been activated using @p pwmStart(). + * @pre The channel must have been activated using @p pwmEnableChannel(). + * @note If the notification is already enabled then the call has no effect. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * @param[in] channel PWM channel identifier (0...channels-1) + * + * @notapi + */ +void pwm_lld_enable_channel_notification(PWMDriver *pwmp, + pwmchannel_t channel) { + + pwmp->tpm->C[channel].SC |= TPMx_CnSC_CHIE; +} + +/** + * @brief Disables a channel de-activation edge notification. + * @pre The PWM unit must have been activated using @p pwmStart(). + * @pre The channel must have been activated using @p pwmEnableChannel(). + * @note If the notification is already disabled then the call has no effect. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * @param[in] channel PWM channel identifier (0...channels-1) + * + * @notapi + */ +void pwm_lld_disable_channel_notification(PWMDriver *pwmp, + pwmchannel_t channel) { + + pwmp->tpm->C[channel].SC &= ~TPMx_CnSC_CHIE; +} + +#endif /* HAL_USE_PWM */ + +/** @} */ diff --git a/os/hal/ports/KINETIS/LLD/TPMv1/hal_pwm_lld.h b/os/hal/ports/KINETIS/LLD/TPMv1/hal_pwm_lld.h new file mode 100644 index 0000000..9821369 --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/TPMv1/hal_pwm_lld.h @@ -0,0 +1,305 @@ +/* + ChibiOS - Copyright (C) 2006..2015 Adam J. Porter + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file KL2x/pwm_lld.h + * @brief KINETIS PWM subsystem low level driver header. + * + * @addtogroup PWM + * @{ + */ + +#ifndef HAL_PWM_LLD_H_ +#define HAL_PWM_LLD_H_ + +#if HAL_USE_PWM || defined(__DOXYGEN__) + +/*===========================================================================*/ +/* Driver constants. */ +/*===========================================================================*/ + +#if !defined(KINETIS_PWM_USE_TPM0) +#define KINETIS_PWM_USE_TPM0 FALSE +#endif +#if !defined(KINETIS_PWM_USE_TPM1) +#define KINETIS_PWM_USE_TPM1 FALSE +#endif +#if !defined(KINETIS_PWM_USE_TPM2) +#define KINETIS_PWM_USE_TPM2 FALSE +#endif + +/** + * @brief Number of PWM channels per PWM driver. + */ +#define PWM_CHANNELS 6 + +/*===========================================================================*/ +/* Driver pre-compile time settings. */ +/*===========================================================================*/ + +/** + * @name Configuration options + * @{ + */ +/** + * @brief If advanced timer features switch. + * @details If set to @p TRUE the advanced features for TIM1 and TIM8 are + * enabled. + * @note The default is @p TRUE. + */ +#if !defined(KINETIS_PWM_USE_ADVANCED) || defined(__DOXYGEN__) +#define KINETIS_PWM_USE_ADVANCED FALSE +#endif + +/** + * @brief TPM0 interrupt priority level setting. + * @note The default is 2. + */ +#if !defined(KINETIS_PWM_TPM0_IRQ_PRIORITY)|| defined(__DOXYGEN__) +#define KINETIS_PWM_TPM0_IRQ_PRIORITY 2 +#endif + +/** + * @brief TPM1 interrupt priority level setting. + * @note The default is 2. + */ +#if !defined(KINETIS_PWM_TPM1_IRQ_PRIORITY)|| defined(__DOXYGEN__) +#define KINETIS_PWM_TPM1_IRQ_PRIORITY 2 +#endif + +/** + * @brief TPM2 interrupt priority level setting. + * @note The default is 2. + */ +#if !defined(KINETIS_PWM_TPM2_IRQ_PRIORITY)|| defined(__DOXYGEN__) +#define KINETIS_PWM_TPM2_IRQ_PRIORITY 2 +#endif + +/** @} */ + +/*===========================================================================*/ +/* Configuration checks. */ +/*===========================================================================*/ + +#if KINETIS_PWM_USE_TPM0 && !KINETIS_HAS_TPM0 +#error "TPM0 not present in the selected device" +#endif + +#if KINETIS_PWM_USE_TPM1 && !KINETIS_HAS_TPM1 +#error "TPM1 not present in the selected device" +#endif + +#if KINETIS_PWM_USE_TPM2 && !KINETIS_HAS_TPM2 +#error "TPM2 not present in the selected device" +#endif + +#if !KINETIS_PWM_USE_TPM0 && !KINETIS_PWM_USE_TPM1 && !KINETIS_PWM_USE_TPM2 +#error "PWM driver activated but no TPM peripheral assigned" +#endif + +#if KINETIS_PWM_USE_TPM0 && \ + !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_PWM_TPM0_IRQ_PRIORITY) +#error "Invalid IRQ priority assigned to KINETIS_PWM_TPM0_IRQ_PRIORITY" +#endif + +#if KINETIS_PWM_USE_TPM1 && \ + !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_PWM_TPM1_IRQ_PRIORITY) +#error "Invalid IRQ priority assigned to KINETIS_PWM_TPM1_IRQ_PRIORITY" +#endif + +#if KINETIS_PWM_USE_TPM2 && \ + !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_PWM_TPM2_IRQ_PRIORITY) +#error "Invalid IRQ priority assigned to KINETIS_PWM_TPM2_IRQ_PRIORITY" +#endif + +#if !defined(KINETIS_TPM0_IRQ_VECTOR) +#error "KINETIS_TPM0_IRQ_VECTOR not defined" +#endif + +#if !defined(KINETIS_TPM1_IRQ_VECTOR) +#error "KINETIS_TPM1_IRQ_VECTOR not defined" +#endif + +#if !defined(KINETIS_TPM2_IRQ_VECTOR) +#error "KINETIS_TPM2_IRQ_VECTOR not defined" +#endif + +/*===========================================================================*/ +/* Driver data structures and types. */ +/*===========================================================================*/ + +/** + * @brief Type of a PWM mode. + */ +typedef uint32_t pwmmode_t; + +/** + * @brief Type of a PWM channel. + */ +typedef uint8_t pwmchannel_t; + +/** + * @brief Type of a channels mask. + */ +typedef uint32_t pwmchnmsk_t; + +/** + * @brief Type of a PWM counter. + */ +typedef uint16_t pwmcnt_t; + +/** + * @brief Type of a PWM driver channel configuration structure. + */ +typedef struct { + /** + * @brief Channel active logic level. + */ + pwmmode_t mode; + /** + * @brief Channel callback pointer. + * @note This callback is invoked on the channel compare event. If set to + * @p NULL then the callback is disabled. + */ + pwmcallback_t callback; + /* End of the mandatory fields.*/ +} PWMChannelConfig; + +/** + * @brief Type of a PWM driver configuration structure. + */ +typedef struct { + /** + * @brief Timer clock in Hz. + * @note The low level can use assertions in order to catch invalid + * frequency specifications. + */ + uint32_t frequency; + /** + * @brief PWM period in ticks. + * @note The low level can use assertions in order to catch invalid + * period specifications. + */ + pwmcnt_t period; + /** + * @brief Periodic callback pointer. + * @note This callback is invoked on PWM counter reset. If set to + * @p NULL then the callback is disabled. + */ + pwmcallback_t callback; + /** + * @brief Channels configurations. + */ + PWMChannelConfig channels[PWM_CHANNELS]; + /* End of the mandatory fields.*/ +} PWMConfig; + +/** + * @brief Structure representing a PWM driver. + */ +struct PWMDriver { + /** + * @brief Driver state. + */ + pwmstate_t state; + /** + * @brief Current driver configuration data. + */ + const PWMConfig *config; + /** + * @brief Current PWM period in ticks. + */ + pwmcnt_t period; + /** + * @brief Mask of the enabled channels. + */ + pwmchnmsk_t enabled; + /** + * @brief Number of channels in this instance. + */ + pwmchannel_t channels; +#if defined(PWM_DRIVER_EXT_FIELDS) + PWM_DRIVER_EXT_FIELDS +#endif + /* End of the mandatory fields.*/ + /** + * @brief Pointer to the TPM registers block. + */ + TPM_TypeDef *tpm; +}; + +/*===========================================================================*/ +/* Driver macros. */ +/*===========================================================================*/ + +/** + * @brief Changes the period the PWM peripheral. + * @details This function changes the period of a PWM unit that has already + * been activated using @p pwmStart(). + * @pre The PWM unit must have been activated using @p pwmStart(). + * @post The PWM unit period is changed to the new value. + * @note The function has effect at the next cycle start. + * @note If a period is specified that is shorter than the pulse width + * programmed in one of the channels then the behavior is not + * guaranteed. + * + * @param[in] pwmp pointer to a @p PWMDriver object + * @param[in] period new cycle time in ticks + * + * @notapi + */ +#define pwm_lld_change_period(pwmp, period) \ + ((pwmp)->tpm->MOD = ((period) - 1)) + +/*===========================================================================*/ +/* External declarations. */ +/*===========================================================================*/ + +#if KINETIS_PWM_USE_TPM0 || defined(__DOXYGEN__) +extern PWMDriver PWMD1; +#endif +#if KINETIS_PWM_USE_TPM1 || defined(__DOXYGEN__) +extern PWMDriver PWMD2; +#endif +#if KINETIS_PWM_USE_TPM2 || defined(__DOXYGEN__) +extern PWMDriver PWMD3; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + void pwm_lld_init(void); + void pwm_lld_start(PWMDriver *pwmp); + void pwm_lld_stop(PWMDriver *pwmp); + void pwm_lld_enable_channel(PWMDriver *pwmp, + pwmchannel_t channel, + pwmcnt_t width); + void pwm_lld_disable_channel(PWMDriver *pwmp, pwmchannel_t channel); + void pwm_lld_enable_periodic_notification(PWMDriver *pwmp); + void pwm_lld_disable_periodic_notification(PWMDriver *pwmp); + void pwm_lld_enable_channel_notification(PWMDriver *pwmp, + pwmchannel_t channel); + void pwm_lld_disable_channel_notification(PWMDriver *pwmp, + pwmchannel_t channel); +#ifdef __cplusplus +} +#endif + +#endif /* HAL_USE_PWM */ + +#endif /* HAL_PWM_LLD_H_ */ + +/** @} */ diff --git a/os/hal/ports/KINETIS/LLD/UARTv1/driver.mk b/os/hal/ports/KINETIS/LLD/UARTv1/driver.mk new file mode 100644 index 0000000..e0ba660 --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/UARTv1/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_SERIAL TRUE,$(HALCONF)),) +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/UARTv1/hal_serial_lld.c +endif +else +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/UARTv1/hal_serial_lld.c +endif + +PLATFORMINC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/UARTv1 diff --git a/os/hal/ports/KINETIS/LLD/USBHSv1/driver.mk b/os/hal/ports/KINETIS/LLD/USBHSv1/driver.mk new file mode 100644 index 0000000..1c0192a --- /dev/null +++ b/os/hal/ports/KINETIS/LLD/USBHSv1/driver.mk @@ -0,0 +1,9 @@ +ifeq ($(USE_SMART_BUILD),yes) +ifneq ($(findstring HAL_USE_USB TRUE,$(HALCONF)),) +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/USBHSv1/hal_usb_lld.c +endif +else +PLATFORMSRC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/USBHSv1/hal_usb_lld.c +endif + +PLATFORMINC_CONTRIB += ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/USBHSv1 diff --git a/os/hal/ports/KINETIS/MK66F18/hal_pwm_lld.c b/os/hal/ports/KINETIS/MK66F18/hal_pwm_lld.c deleted file mode 100644 index f39823d..0000000 --- a/os/hal/ports/KINETIS/MK66F18/hal_pwm_lld.c +++ /dev/null @@ -1,390 +0,0 @@ -/* - ChibiOS/HAL - Copyright (C) 2014 Adam J. Porter - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -/** - * @file MK66F18/pwm_lld.c - * @brief KINETIS PWM subsystem low level driver source. - * - * @addtogroup PWM - * @{ - */ - -#include "hal.h" - -#if HAL_USE_PWM || defined(__DOXYGEN__) - -/*===========================================================================*/ -/* Driver local definitions. */ -/*===========================================================================*/ - - -/*===========================================================================*/ -/* Driver exported variables. */ -/*===========================================================================*/ - -/** - * @brief PWMD1 driver identifier. - * @note The driver PWMD1 allocates the timer FTM0 when enabled. - */ -#if KINETIS_PWM_USE_FTM0 || defined(__DOXYGEN__) -PWMDriver PWMD1; -#endif - -/** - * @brief PWMD2 driver identifier. - * @note The driver PWMD2 allocates the timer FTM1 when enabled. - */ -#if KINETIS_PWM_USE_FTM1 || defined(__DOXYGEN__) -PWMDriver PWMD2; -#endif - -/** - * @brief PWMD3 driver identifier. - * @note The driver PWMD3 allocates the timer FTM2 when enabled. - */ -#if KINETIS_PWM_USE_FTM2 || defined(__DOXYGEN__) -PWMDriver PWMD3; -#endif - -/*===========================================================================*/ -/* Driver local variables and types. */ -/*===========================================================================*/ - -/*===========================================================================*/ -/* Driver local functions. */ -/*===========================================================================*/ - -static void pwm_lld_serve_interrupt(PWMDriver *pwmp) { - uint32_t sr; - - sr = pwmp->ftm->SC; - pwmp->ftm->SC = sr&(~FTM_SC_TOF); - - if (((sr & FTM_SC_TOF) != 0) && /* Timer Overflow */ - ((sr & FTM_SC_TOIE) != 0) && - (pwmp->config->callback != NULL)) { - pwmp->config->callback(pwmp); - } - - uint8_t n=0; - for(n=0;nchannels;n++) { - sr = pwmp->ftm->CHANNEL[n].CnSC; - pwmp->ftm->CHANNEL[n].CnSC = sr&(~FTM_CnSC_CHF); - if (((sr & FTM_CnSC_CHF) != 0) && - ((sr & FTM_CnSC_CHIE) != 0) && - (pwmp->config->channels[n].callback != NULL)) { - pwmp->config->channels[n].callback(pwmp); - } - } -} - -/*===========================================================================*/ -/* Driver interrupt handlers. */ -/*===========================================================================*/ - -#if KINETIS_PWM_USE_FTM0 -/** - * @brief FTM0 interrupt handler. - * - * @isr - */ -OSAL_IRQ_HANDLER(KINETIS_FTM0_IRQ_VECTOR) { - OSAL_IRQ_PROLOGUE(); - pwm_lld_serve_interrupt(&PWMD1); - OSAL_IRQ_EPILOGUE(); -} -#endif /* KINETIS_PWM_USE_FTM0 */ - -#if KINETIS_PWM_USE_FTM1 -/** - * @brief FTM1 interrupt handler. - * - * @isr - */ -OSAL_IRQ_HANDLER(KINETIS_FTM1_IRQ_VECTOR) { - - OSAL_IRQ_PROLOGUE(); - pwm_lld_serve_interrupt(&PWMD2); - OSAL_IRQ_EPILOGUE(); -} -#endif /* KINETIS_PWM_USE_FTM1 */ - -#if KINETIS_PWM_USE_FTM2 -/** - * @brief FTM2 interrupt handler. - * - * @isr - */ -OSAL_IRQ_HANDLER(KINETIS_FTM2_IRQ_VECTOR) { - - OSAL_IRQ_PROLOGUE(); - pwm_lld_serve_interrupt(&PWMD3); - OSAL_IRQ_EPILOGUE(); -} -#endif /* KINETIS_PWM_USE_FTM2 */ - -/*===========================================================================*/ -/* Driver exported functions. */ -/*===========================================================================*/ - -/** - * @brief Low level PWM driver initialization. - * - * @notapi - */ -void pwm_lld_init(void) { - -#if KINETIS_PWM_USE_FTM0 - pwmObjectInit(&PWMD1); - PWMD1.channels = KINETIS_FTM0_CHANNELS; - PWMD1.ftm = FTM0; -#endif - -#if KINETIS_PWM_USE_FTM1 - pwmObjectInit(&PWMD2); - PWMD2.channels = KINETIS_FTM1_CHANNELS; - PWMD2.ftm = FTM1; -#endif - -#if KINETIS_PWM_USE_FTM2 - pwmObjectInit(&PWMD3); - PWMD3.channels = KINETIS_FTM2_CHANNELS; - PWMD3.ftm = FTM2; -#endif -} - -/** - * @brief Configures and activates the PWM peripheral. - * @note Starting a driver that is already in the @p PWM_READY state - * disables all the active channels. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * - * @notapi - */ -void pwm_lld_start(PWMDriver *pwmp) { - uint16_t psc; - uint8_t i=0; - - if (pwmp->state == PWM_STOP) { - /* Clock activation and timer reset.*/ -#if KINETIS_PWM_USE_FTM0 - if (&PWMD1 == pwmp) { - SIM->SCGC6 |= SIM_SCGC6_FTM0; - nvicEnableVector(FTM0_IRQn, KINETIS_PWM_FTM0_PRIORITY); - } -#endif - -#if KINETIS_PWM_USE_FTM1 - if (&PWMD2 == pwmp) { - SIM->SCGC6 |= SIM_SCGC6_FTM1; - nvicEnableVector(FTM1_IRQn, KINETIS_PWM_FTM1_PRIORITY); - } -#endif - -#if KINETIS_PWM_USE_FTM2 - if (&PWMD3 == pwmp) { - SIM->SCGC3 |= SIM_SCGC3_FTM2; - nvicEnableVector(FTM2_IRQn, KINETIS_PWM_FTM2_PRIORITY); - } -#endif - } - pwmp->ftm->MODE = FTM_MODE_FTMEN_MASK|FTM_MODE_PWMSYNC_MASK; - pwmp->ftm->SYNC = FTM_SYNC_CNTMIN_MASK|FTM_SYNC_CNTMAX_MASK - |FTM_SYNC_SWSYNC_MASK; - pwmp->ftm->COMBINE = FTM_COMBINE_SYNCEN3_MASK | FTM_COMBINE_SYNCEN2_MASK - | FTM_COMBINE_SYNCEN1_MASK | FTM_COMBINE_SYNCEN0_MASK; - pwmp->ftm->SYNCONF = FTM_SYNCONF_SYNCMODE_MASK; - - pwmp->ftm->CNTIN = 0x0000; - //~ pwmp->ftm->SC = 0; /* Disable FTM counter.*/ - pwmp->ftm->CNT = 0x0000; /* Clear count register.*/ - - /* Prescaler value calculation.*/ - psc = (KINETIS_SYSCLK_FREQUENCY / pwmp->config->frequency); - //~ /* Prescaler must be power of two between 1 and 128.*/ - osalDbgAssert(psc <= 128 && !(psc & (psc - 1)), "invalid frequency"); - //~ /* Prescaler register value determination. - //~ Prescaler register value conveniently corresponds to bit position, - //~ i.e., register value for prescaler CLK/64 is 6 ((1 << 6) == 64).*/ - for (i = 0; i < 8; i++) { - if (psc == (unsigned)(1 << i)) { - break; - } - } - - /* Set prescaler and clock mode. - This also sets the following: - CPWMS up-counting mode - Timer overflow interrupt disabled - DMA disabled.*/ - pwmp->ftm->SC = FTM_SC_CLKS(1) | FTM_SC_PS(i); - /* Configure period */ - pwmp->ftm->MOD = pwmp->period-1; - pwmp->ftm->PWMLOAD = FTM_PWMLOAD_LDOK_MASK; -} - -/** - * @brief Deactivates the PWM peripheral. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * - * @notapi - */ -void pwm_lld_stop(PWMDriver *pwmp) { - - /* If in ready state then disables the PWM clock.*/ - if (pwmp->state == PWM_READY) { -#if KINETIS_PWM_USE_FTM0 - if (&PWMD1 == pwmp) { - SIM->SCGC6 &= ~SIM_SCGC6_FTM0; - nvicDisableVector(FTM0_IRQn); - } -#endif - -#if KINETIS_PWM_USE_FTM1 - if (&PWMD2 == pwmp) { - SIM->SCGC6 &= ~SIM_SCGC6_FTM1; - nvicDisableVector(FTM1_IRQn); - } -#endif - -#if KINETIS_PWM_USE_FTM2 - if (&PWMD3 == pwmp) { - SIM->SCGC3 &= ~SIM_SCGC3_FTM2; - nvicDisableVector(FTM2_IRQn); - } -#endif - /* Disable FTM counter.*/ - pwmp->ftm->SC = 0; - pwmp->ftm->MOD = 0; - } -} - -/** - * @brief Enables a PWM channel. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @post The channel is active using the specified configuration. - * @note The function has effect at the next cycle start. - * @note Channel notification is not enabled. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] channel PWM channel identifier (0...channels-1) - * @param[in] width PWM pulse width as clock pulses number - * - * @notapi - */ -void pwm_lld_enable_channel(PWMDriver *pwmp, - pwmchannel_t channel, - pwmcnt_t width) { - uint32_t mode = FTM_CnSC_MSB; /* Edge-aligned PWM mode.*/ - - switch (pwmp->config->channels[channel].mode & PWM_OUTPUT_MASK) { - case PWM_OUTPUT_ACTIVE_HIGH: - mode |= FTM_CnSC_ELSB; - break; - case PWM_OUTPUT_ACTIVE_LOW: - mode |= FTM_CnSC_ELSA; - break; - } - - if (pwmp->ftm->CHANNEL[channel].CnSC & FTM_CnSC_CHIE) - mode |= FTM_CnSC_CHIE; - - pwmp->ftm->CHANNEL[channel].CnSC = mode; - pwmp->ftm->CHANNEL[channel].CnV = width; - pwmp->ftm->PWMLOAD = FTM_PWMLOAD_LDOK_MASK; -} - -/** - * @brief Disables a PWM channel and its notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @post The channel is disabled and its output line returned to the - * idle state. - * @note The function has effect at the next cycle start. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] channel PWM channel identifier (0...channels-1) - * - * @notapi - */ -void pwm_lld_disable_channel(PWMDriver *pwmp, pwmchannel_t channel) { - - pwmp->ftm->CHANNEL[channel].CnSC = 0; - pwmp->ftm->CHANNEL[channel].CnV = 0; -} - -/** - * @brief Enables the periodic activation edge notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @note If the notification is already enabled then the call has no effect. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * - * @notapi - */ -void pwm_lld_enable_periodic_notification(PWMDriver *pwmp) { - pwmp->ftm->SC |= FTM_SC_TOIE; -} - -/** - * @brief Disables the periodic activation edge notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @note If the notification is already disabled then the call has no effect. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * - * @notapi - */ -void pwm_lld_disable_periodic_notification(PWMDriver *pwmp) { - pwmp->ftm->SC &= ~FTM_SC_TOIE; -} - -/** - * @brief Enables a channel de-activation edge notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @pre The channel must have been activated using @p pwmEnableChannel(). - * @note If the notification is already enabled then the call has no effect. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] channel PWM channel identifier (0...channels-1) - * - * @notapi - */ -void pwm_lld_enable_channel_notification(PWMDriver *pwmp, - pwmchannel_t channel) { - pwmp->ftm->CHANNEL[channel].CnSC |= FTM_CnSC_CHIE; -} - -/** - * @brief Disables a channel de-activation edge notification. - * @pre The PWM unit must have been activated using @p pwmStart(). - * @pre The channel must have been activated using @p pwmEnableChannel(). - * @note If the notification is already disabled then the call has no effect. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] channel PWM channel identifier (0...channels-1) - * - * @notapi - */ -void pwm_lld_disable_channel_notification(PWMDriver *pwmp, - pwmchannel_t channel) { - pwmp->ftm->CHANNEL[channel].CnSC &= ~FTM_CnSC_CHIE; -} - -#endif /* HAL_USE_PWM */ - -/** @} */ diff --git a/os/hal/ports/KINETIS/MK66F18/hal_pwm_lld.h b/os/hal/ports/KINETIS/MK66F18/hal_pwm_lld.h deleted file mode 100644 index 9332e34..0000000 --- a/os/hal/ports/KINETIS/MK66F18/hal_pwm_lld.h +++ /dev/null @@ -1,270 +0,0 @@ -/* - ChibiOS/HAL - Copyright (C) 2014 Adam J. Porter - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -/** - * @file MK66F18/pwm_lld.h - * @brief KINETIS PWM subsystem low level driver header. - * - * @addtogroup PWM - * @{ - */ - -#ifndef HAL_PWM_LLD_H_ -#define HAL_PWM_LLD_H_ - -#if HAL_USE_PWM || defined(__DOXYGEN__) - -/*===========================================================================*/ -/* Driver constants. */ -/*===========================================================================*/ - -/** - * @brief Number of PWM channels per PWM driver. - */ -#define PWM_CHANNELS 8 - -/*===========================================================================*/ -/* Driver pre-compile time settings. */ -/*===========================================================================*/ - -#if !defined(KINETIS_PWM_USE_FTM0) - #define KINETIS_PWM_USE_FTM0 FALSE -#endif - -#if !defined(KINETIS_PWM_USE_FTM1) - #define KINETIS_PWM_USE_FTM1 FALSE -#endif - -#if !defined(KINETIS_PWM_USE_FTM2) - #define KINETIS_PWM_USE_FTM2 FALSE -#endif - -/** - * @brief FTM0 interrupt priority level setting. - */ -#if !defined(KINETIS_PWM_FTM0_PRIORITY) || defined(__DOXYGEN__) -#define KINETIS_PWM_FTM0_PRIORITY 12 -#endif - -/** - * @brief FTM1 interrupt priority level setting. - */ -#if !defined(KINETIS_PWM_FTM1_PRIORITY) || defined(__DOXYGEN__) -#define KINETIS_PWM_FTM1_PRIORITY 12 -#endif - -/** - * @brief FTM2 interrupt priority level setting. - */ -#if !defined(KINETIS_PWM_FTM2_PRIORITY) || defined(__DOXYGEN__) -#define KINETIS_PWM_FTM2_PRIORITY 12 -#endif - -/** @} */ - -/** - * @name Configuration options - * @{ - */ -/** - * @brief If advanced timer features switch. - * @details If set to @p TRUE the advanced features for TIM1 and TIM8 are - * enabled. - * @note The default is @p TRUE. - */ -#if !defined(KINETIS_PWM_USE_ADVANCED) || defined(__DOXYGEN__) -#define KINETIS_PWM_USE_ADVANCED FALSE -#endif -/** @} */ - -/*===========================================================================*/ -/* Configuration checks. */ -/*===========================================================================*/ - -#if !KINETIS_PWM_USE_FTM0 && !KINETIS_PWM_USE_FTM1 && !KINETIS_PWM_USE_FTM2 -#error "PWM driver activated but no FTM peripheral assigned" -#endif - -/*===========================================================================*/ -/* Driver data structures and types. */ -/*===========================================================================*/ - -/** - * @brief Type of a PWM mode. - */ -typedef uint32_t pwmmode_t; - -/** - * @brief Type of a PWM channel. - */ -typedef uint8_t pwmchannel_t; - -/** - * @brief Type of a channels mask. - */ -typedef uint32_t pwmchnmsk_t; - -/** - * @brief Type of a PWM counter. - */ -typedef uint16_t pwmcnt_t; - -/** - * @brief Type of a PWM driver channel configuration structure. - */ -typedef struct { - /** - * @brief Channel active logic level. - */ - pwmmode_t mode; - - /** - * @brief Channel callback pointer. - * @note This callback is invoked on the channel compare event. If set to - * @p NULL then the callback is disabled. - */ - pwmcallback_t callback; - /* End of the mandatory fields.*/ -} PWMChannelConfig; - -/** - * @brief Type of a PWM driver configuration structure. - */ -typedef struct { - /** - * @brief Timer clock in Hz. - * @note The low level can use assertions in order to catch invalid - * frequency specifications. - */ - uint32_t frequency; - /** - * @brief PWM period in ticks. - * @note The low level can use assertions in order to catch invalid - * period specifications. - */ - pwmcnt_t period; - /** - * @brief Periodic callback pointer. - * @note This callback is invoked on PWM counter reset. If set to - * @p NULL then the callback is disabled. - */ - pwmcallback_t callback; - /** - * @brief Channels configurations. - */ - PWMChannelConfig channels[PWM_CHANNELS]; - /* End of the mandatory fields.*/ -} PWMConfig; - -/** - * @brief Structure representing a PWM driver. - */ -struct PWMDriver { - /** - * @brief Driver state. - */ - pwmstate_t state; - /** - * @brief Current driver configuration data. - */ - const PWMConfig *config; - /** - * @brief Current PWM period in ticks. - */ - pwmcnt_t period; - /** - * @brief Mask of the enabled channels. - */ - pwmchnmsk_t enabled; - /** - * @brief Number of channels in this instance. - */ - pwmchannel_t channels; -#if defined(PWM_DRIVER_EXT_FIELDS) - PWM_DRIVER_EXT_FIELDS -#endif - /* End of the mandatory fields.*/ - /** - * @brief Pointer to the FTM registers block. - */ - FTM_TypeDef *ftm; -}; - -/*===========================================================================*/ -/* Driver macros. */ -/*===========================================================================*/ - -/** - * @brief Changes the period the PWM peripheral. - * @details This function changes the period of a PWM unit that has already - * been activated using @p pwmStart(). - * @pre The PWM unit must have been activated using @p pwmStart(). - * @post The PWM unit period is changed to the new value. - * @note The function has effect at the next cycle start. - * @note If a period is specified that is shorter than the pulse width - * programmed in one of the channels then the behavior is not - * guaranteed. - * - * @param[in] pwmp pointer to a @p PWMDriver object - * @param[in] period new cycle time in ticks - * - * @notapi - */ -#define pwm_lld_change_period(pwmp, period) \ - do { \ - (pwmp)->ftm->MOD = ((period) - 1); \ - pwmp->ftm->PWMLOAD = FTM_PWMLOAD_LDOK_MASK;\ - } while(0) - -/*===========================================================================*/ -/* External declarations. */ -/*===========================================================================*/ - -#if KINETIS_PWM_USE_FTM0 || defined(__DOXYGEN__) -extern PWMDriver PWMD1; -#endif -#if KINETIS_PWM_USE_FTM1 || defined(__DOXYGEN__) -extern PWMDriver PWMD2; -#endif -#if KINETIS_PWM_USE_FTM2 || defined(__DOXYGEN__) -extern PWMDriver PWMD3; -#endif - -#ifdef __cplusplus -extern "C" { -#endif - void pwm_lld_init(void); - void pwm_lld_start(PWMDriver *pwmp); - void pwm_lld_stop(PWMDriver *pwmp); - void pwm_lld_enable_channel(PWMDriver *pwmp, - pwmchannel_t channel, - pwmcnt_t width); - void pwm_lld_disable_channel(PWMDriver *pwmp, pwmchannel_t channel); - void pwm_lld_enable_periodic_notification(PWMDriver *pwmp); - void pwm_lld_disable_periodic_notification(PWMDriver *pwmp); - void pwm_lld_enable_channel_notification(PWMDriver *pwmp, - pwmchannel_t channel); - void pwm_lld_disable_channel_notification(PWMDriver *pwmp, - pwmchannel_t channel); -#ifdef __cplusplus -} -#endif - -#endif /* HAL_USE_PWM */ - -#endif /* HAL_PWM_LLD_H_ */ - -/** @} */ diff --git a/os/hal/ports/KINETIS/MK66F18/hal_spi_lld.c b/os/hal/ports/KINETIS/MK66F18/hal_spi_lld.c deleted file mode 100644 index 29ab4e8..0000000 --- a/os/hal/ports/KINETIS/MK66F18/hal_spi_lld.c +++ /dev/null @@ -1,539 +0,0 @@ -/* - ChibiOS - Copyright (C) 2014-2015 Fabio Utzig - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -/** - * @file KINETIS/spi_lld.c - * @brief KINETIS SPI subsystem low level driver source. - * - * @addtogroup SPI - * @{ - */ - -#include "hal.h" - -#if HAL_USE_SPI || defined(__DOXYGEN__) - -/*===========================================================================*/ -/* Driver local definitions. */ -/*===========================================================================*/ - -#if !defined(KINETIS_SPI0_RX_DMA_IRQ_PRIORITY) -#define KINETIS_SPI0_RX_DMA_IRQ_PRIORITY 8 -#endif - -#if !defined(KINETIS_SPI0_RX_DMAMUX_CHANNEL) -#define KINETIS_SPI0_RX_DMAMUX_CHANNEL 0 -#endif - -#if !defined(KINETIS_SPI0_RX_DMA_CHANNEL) -#define KINETIS_SPI0_RX_DMA_CHANNEL 0 -#endif - -#if !defined(KINETIS_SPI0_TX_DMAMUX_CHANNEL) -#define KINETIS_SPI0_TX_DMAMUX_CHANNEL 1 -#endif - -#if !defined(KINETIS_SPI0_TX_DMA_CHANNEL) -#define KINETIS_SPI0_TX_DMA_CHANNEL 1 -#endif - -#if !defined(KINETIS_SPI1_RX_DMA_IRQ_PRIORITY) -#define KINETIS_SPI1_RX_DMA_IRQ_PRIORITY 8 -#endif - -#if !defined(KINETIS_SPI1_RX_DMAMUX_CHANNEL) -#define KINETIS_SPI1_RX_DMAMUX_CHANNEL 0 -#endif - -#if !defined(KINETIS_SPI1_RX_DMA_CHANNEL) -#define KINETIS_SPI1_RX_DMA_CHANNEL 0 -#endif - -#if !defined(KINETIS_SPI1_TX_DMAMUX_CHANNEL) -#define KINETIS_SPI1_TX_DMAMUX_CHANNEL 1 -#endif - -#if !defined(KINETIS_SPI1_TX_DMA_CHANNEL) -#define KINETIS_SPI1_TX_DMA_CHANNEL 1 -#endif - -#if KINETIS_SPI_USE_SPI0 -#define DMAMUX_SPI_RX_SOURCE 16 -#define DMAMUX_SPI_TX_SOURCE 17 -#endif - -#if KINETIS_SPI_USE_SPI1 -#define DMAMUX_SPI_RX_SOURCE 18 -#define DMAMUX_SPI_TX_SOURCE 19 -#endif - -/*===========================================================================*/ -/* Driver exported variables. */ -/*===========================================================================*/ - -/** @brief SPI0 driver identifier.*/ -#if KINETIS_SPI_USE_SPI0 || defined(__DOXYGEN__) -SPIDriver SPID1; -#endif - -/** @brief SPI1 driver identifier.*/ -#if KINETIS_SPI_USE_SPI1 || defined(__DOXYGEN__) -SPIDriver SPID2; -#endif - -/*===========================================================================*/ -/* Driver local variables and types. */ -/*===========================================================================*/ - -/* Use a dummy byte as the source/destination when a buffer is not provided */ -/* Note: The MMC driver relies on 0xFF being sent for dummy bytes. */ -static volatile uint16_t dmaRxDummy; -static uint16_t dmaTxDummy = 0xFFFF; - -/*===========================================================================*/ -/* Driver local functions. */ -/*===========================================================================*/ - -static void spi_start_xfer(SPIDriver *spip, bool polling) -{ - /* - * Enable the DSPI peripheral in master mode. - * Clear the TX and RX FIFOs. - * */ - spip->spi->MCR = SPIx_MCR_MSTR | SPIx_MCR_CLR_TXF | SPIx_MCR_CLR_RXF; - - /* If we are not polling then enable DMA */ - if (!polling) { - - /* Enable receive dma and transmit dma */ - spip->spi->RSER = SPIx_RSER_RFDF_DIRS | SPIx_RSER_RFDF_RE | - SPIx_RSER_TFFF_RE | SPIx_RSER_TFFF_DIRS; - - /* Configure RX DMA */ - if (spip->rxbuf) { - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DADDR = (uint32_t)spip->rxbuf; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DOFF = spip->word_size; - } else { - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DADDR = (uint32_t)&dmaRxDummy; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DOFF = 0; - } - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].BITER_ELINKNO = spip->count; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].CITER_ELINKNO = spip->count; - - /* Enable Request Register (ERQ) for RX by writing 0 to SERQ */ - DMA->SERQ = KINETIS_SPI0_RX_DMA_CHANNEL; - - /* Configure TX DMA */ - if (spip->txbuf) { - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SADDR = (uint32_t)spip->txbuf; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SOFF = spip->word_size; - } else { - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SADDR = (uint32_t)&dmaTxDummy; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SOFF = 0; - } - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].BITER_ELINKNO = spip->count; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].CITER_ELINKNO = spip->count; - - /* Enable Request Register (ERQ) for TX by writing 1 to SERQ */ - DMA->SERQ = KINETIS_SPI0_TX_DMA_CHANNEL; - } -} - -static void spi_stop_xfer(SPIDriver *spip) -{ - /* Halt the DSPI peripheral */ - spip->spi->MCR = SPIx_MCR_MSTR | SPIx_MCR_HALT; - - /* Clear all the flags which are currently set. */ - spip->spi->SR |= spip->spi->SR; -} - -/*===========================================================================*/ -/* Driver interrupt handlers. */ -/*===========================================================================*/ - -#if KINETIS_SPI_USE_SPI0 || defined(__DOXYGEN__) - -OSAL_IRQ_HANDLER(KINETIS_DMA0_IRQ_VECTOR) { - OSAL_IRQ_PROLOGUE(); - - /* Clear bit 0 in Interrupt Request Register (INT) by writing 0 to CINT */ - DMA->CINT = KINETIS_SPI0_RX_DMA_CHANNEL; - - spi_stop_xfer(&SPID1); - - _spi_isr_code(&SPID1); - - OSAL_IRQ_EPILOGUE(); -} - -#endif - -#if KINETIS_SPI_USE_SPI1 || defined(__DOXYGEN__) - -OSAL_IRQ_HANDLER(KINETIS_DMA0_IRQ_VECTOR) { - OSAL_IRQ_PROLOGUE(); - - /* Clear bit 0 in Interrupt Request Register (INT) by writing 0 to CINT */ - DMA->CINT = KINETIS_SPI1_RX_DMA_CHANNEL; - - spi_stop_xfer(&SPID2); - - _spi_isr_code(&SPID2); - - OSAL_IRQ_EPILOGUE(); -} - -#endif - -/*===========================================================================*/ -/* Driver exported functions. */ -/*===========================================================================*/ - -/** - * @brief Low level SPI driver initialization. - * - * @notapi - */ -void spi_lld_init(void) { -#if KINETIS_SPI_USE_SPI0 - spiObjectInit(&SPID1); -#endif -#if KINETIS_SPI_USE_SPI1 - spiObjectInit(&SPID2); -#endif -} - -/** - * @brief Configures and activates the SPI peripheral. - * - * @param[in] spip pointer to the @p SPIDriver object - * - * @notapi - */ -void spi_lld_start(SPIDriver *spip) { - - /* If in stopped state then enables the SPI and DMA clocks.*/ - if (spip->state == SPI_STOP) { - -#if KINETIS_SPI_USE_SPI0 - if (&SPID1 == spip) { - - /* Enable the clock for SPI0 */ - SIM->SCGC6 |= SIM_SCGC6_SPI0; - - SPID1.spi = SPI0; - - if (spip->config->tar0) { - spip->spi->CTAR[0] = spip->config->tar0; - } else { - spip->spi->CTAR[0] = KINETIS_SPI_TAR0_DEFAULT; - } - } -#endif - -#if KINETIS_SPI_USE_SPI1 - if (&SPID2 == spip) { - - /* Enable the clock for SPI0 */ - SIM->SCGC6 |= SIM_SCGC6_SPI1; - - SPID2.spi = SPI1; - - if (spip->config->tar0) { - spip->spi->CTAR[0] = spip->config->tar0; - } else { - spip->spi->CTAR[0] = KINETIS_SPI_TAR0_DEFAULT; - } - } -#endif - - nvicEnableVector(DMA0_IRQn, KINETIS_SPI0_RX_DMA_IRQ_PRIORITY); - - SIM->SCGC6 |= SIM_SCGC6_DMAMUX; - SIM->SCGC7 |= SIM_SCGC7_DMA; - - /* Clear DMA error flags */ - DMA->ERR = 0x0F; - -#if KINETIS_SPI_USE_SPI0 - /* Rx, select SPI Rx FIFO */ - DMAMUX->CHCFG[KINETIS_SPI0_RX_DMAMUX_CHANNEL] = DMAMUX_CHCFGn_ENBL | - DMAMUX_CHCFGn_SOURCE(DMAMUX_SPI_RX_SOURCE); - - /* Tx, select SPI Tx FIFO */ - DMAMUX->CHCFG[KINETIS_SPI0_TX_DMAMUX_CHANNEL] = DMAMUX_CHCFGn_ENBL | - DMAMUX_CHCFGn_SOURCE(DMAMUX_SPI_TX_SOURCE); - - /* Extract the frame size from the TAR */ - uint16_t frame_size = ((spip->spi->CTAR[0] >> SPIx_CTARn_FMSZ_SHIFT) & - SPIx_CTARn_FMSZ_MASK) + 1; - - /* DMA transfer size is 16 bits for a frame size > 8 bits */ - uint16_t dma_size = frame_size > 8 ? 1 : 0; - - /* DMA word size is 2 for a 16 bit frame size */ - spip->word_size = frame_size > 8 ? 2 : 1; - - /* configure DMA RX fixed values */ - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].SADDR = (uint32_t)&SPI0->POPR; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].SOFF = 0; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].SLAST = 0; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].DLASTSGA = 0; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].ATTR = DMA_ATTR_SSIZE(dma_size) | - DMA_ATTR_DSIZE(dma_size); - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].NBYTES_MLNO = spip->word_size; - DMA->TCD[KINETIS_SPI0_RX_DMA_CHANNEL].CSR = DMA_CSR_DREQ_MASK | - DMA_CSR_INTMAJOR_MASK; - - /* configure DMA TX fixed values */ - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].SLAST = 0; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].DADDR = (uint32_t)&SPI0->PUSHR; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].DOFF = 0; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].DLASTSGA = 0; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].ATTR = DMA_ATTR_SSIZE(dma_size) | - DMA_ATTR_DSIZE(dma_size); - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].NBYTES_MLNO = spip->word_size; - DMA->TCD[KINETIS_SPI0_TX_DMA_CHANNEL].CSR = DMA_CSR_DREQ_MASK; -#endif - -#if KINETIS_SPI_USE_SPI1 - /* Rx, select SPI Rx FIFO */ - DMAMUX->CHCFG[KINETIS_SPI1_RX_DMAMUX_CHANNEL] = DMAMUX_CHCFGn_ENBL | - DMAMUX_CHCFGn_SOURCE(DMAMUX_SPI_RX_SOURCE); - - /* Tx, select SPI Tx FIFO */ - DMAMUX->CHCFG[KINETIS_SPI1_TX_DMAMUX_CHANNEL] = DMAMUX_CHCFGn_ENBL | - DMAMUX_CHCFGn_SOURCE(DMAMUX_SPI_TX_SOURCE); - - /* Extract the frame size from the TAR */ - uint16_t frame_size = ((spip->spi->CTAR[0] >> SPIx_CTARn_FMSZ_SHIFT) & - SPIx_CTARn_FMSZ_MASK) + 1; - - /* DMA transfer size is 16 bits for a frame size > 8 bits */ - uint16_t dma_size = frame_size > 8 ? 1 : 0; - - /* DMA word size is 2 for a 16 bit frame size */ - spip->word_size = frame_size > 8 ? 2 : 1; - - /* configure DMA RX fixed values */ - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].SADDR = (uint32_t)&SPI1->POPR; - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].SOFF = 0; - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].SLAST = 0; - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].DLASTSGA = 0; - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].ATTR = DMA_ATTR_SSIZE(dma_size) | - DMA_ATTR_DSIZE(dma_size); - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].NBYTES_MLNO = spip->word_size; - DMA->TCD[KINETIS_SPI1_RX_DMA_CHANNEL].CSR = DMA_CSR_DREQ_MASK | - DMA_CSR_INTMAJOR_MASK; - - /* configure DMA TX fixed values */ - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].SLAST = 0; - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].DADDR = (uint32_t)&SPI1->PUSHR; - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].DOFF = 0; - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].DLASTSGA = 0; - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].ATTR = DMA_ATTR_SSIZE(dma_size) | - DMA_ATTR_DSIZE(dma_size); - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].NBYTES_MLNO = spip->word_size; - DMA->TCD[KINETIS_SPI1_TX_DMA_CHANNEL].CSR = DMA_CSR_DREQ_MASK; -#endif - } -} - -/** - * @brief Deactivates the SPI peripheral. - * - * @param[in] spip pointer to the @p SPIDriver object - * - * @notapi - */ -void spi_lld_stop(SPIDriver *spip) { - - /* If in ready state then disables the SPI clock.*/ - if (spip->state == SPI_READY) { - - nvicDisableVector(DMA0_IRQn); - - SIM->SCGC7 &= ~SIM_SCGC7_DMA; - SIM->SCGC6 &= ~SIM_SCGC6_DMAMUX; - -#if KINETIS_SPI_USE_SPI0 - if (&SPID1 == spip) { - /* SPI halt.*/ - spip->spi->MCR |= SPIx_MCR_HALT; - } - - /* Disable the clock for SPI0 */ - SIM->SCGC6 &= ~SIM_SCGC6_SPI0; -#endif - -#if KINETIS_SPI_USE_SPI1 - if (&SPID2 == spip) { - /* SPI halt.*/ - spip->spi->MCR |= SPIx_MCR_HALT; - } - - /* Disable the clock for SPI1 */ - SIM->SCGC6 &= ~SIM_SCGC6_SPI1; -#endif - } -} - -/** - * @brief Asserts the slave select signal and prepares for transfers. - * - * @param[in] spip pointer to the @p SPIDriver object - * - * @notapi - */ -void spi_lld_select(SPIDriver *spip) { - - palClearPad(spip->config->ssport, spip->config->sspad); -} - -/** - * @brief Deasserts the slave select signal. - * @details The previously selected peripheral is unselected. - * - * @param[in] spip pointer to the @p SPIDriver object - * - * @notapi - */ -void spi_lld_unselect(SPIDriver *spip) { - - palSetPad(spip->config->ssport, spip->config->sspad); -} - -/** - * @brief Ignores data on the SPI bus. - * @details This asynchronous function starts the transmission of a series of - * idle words on the SPI bus and ignores the received data. - * @post At the end of the operation the configured callback is invoked. - * - * @param[in] spip pointer to the @p SPIDriver object - * @param[in] n number of words to be ignored - * - * @notapi - */ -void spi_lld_ignore(SPIDriver *spip, size_t n) { - - spip->count = n; - spip->rxbuf = NULL; - spip->txbuf = NULL; - - spi_start_xfer(spip, false); -} - -/** - * @brief Exchanges data on the SPI bus. - * @details This asynchronous function starts a simultaneous transmit/receive - * operation. - * @post At the end of the operation the configured callback is invoked. - * @note The buffers are organized as uint8_t arrays for data sizes below or - * equal to 8 bits else it is organized as uint16_t arrays. - * - * @param[in] spip pointer to the @p SPIDriver object - * @param[in] n number of words to be exchanged - * @param[in] txbuf the pointer to the transmit buffer - * @param[out] rxbuf the pointer to the receive buffer - * - * @notapi - */ -void spi_lld_exchange(SPIDriver *spip, size_t n, - const void *txbuf, void *rxbuf) { - - spip->count = n; - spip->rxbuf = rxbuf; - spip->txbuf = txbuf; - - spi_start_xfer(spip, false); -} - -/** - * @brief Sends data over the SPI bus. - * @details This asynchronous function starts a transmit operation. - * @post At the end of the operation the configured callback is invoked. - * @note The buffers are organized as uint8_t arrays for data sizes below or - * equal to 8 bits else it is organized as uint16_t arrays. - * - * @param[in] spip pointer to the @p SPIDriver object - * @param[in] n number of words to send - * @param[in] txbuf the pointer to the transmit buffer - * - * @notapi - */ -void spi_lld_send(SPIDriver *spip, size_t n, const void *txbuf) { - - spip->count = n; - spip->rxbuf = NULL; - spip->txbuf = (void *)txbuf; - - spi_start_xfer(spip, false); -} - -/** - * @brief Receives data from the SPI bus. - * @details This asynchronous function starts a receive operation. - * @post At the end of the operation the configured callback is invoked. - * @note The buffers are organized as uint8_t arrays for data sizes below or - * equal to 8 bits else it is organized as uint16_t arrays. - * - * @param[in] spip pointer to the @p SPIDriver object - * @param[in] n number of words to receive - * @param[out] rxbuf the pointer to the receive buffer - * - * @notapi - */ -void spi_lld_receive(SPIDriver *spip, size_t n, void *rxbuf) { - - spip->count = n; - spip->rxbuf = rxbuf; - spip->txbuf = NULL; - - spi_start_xfer(spip, false); -} - -/** - * @brief Exchanges one frame using a polled wait. - * @details This synchronous function exchanges one frame using a polled - * synchronization method. This function is useful when exchanging - * small amount of data on high speed channels, usually in this - * situation is much more efficient just wait for completion using - * polling than suspending the thread waiting for an interrupt. - * - * @param[in] spip pointer to the @p SPIDriver object - * @param[in] frame the data frame to send over the SPI bus - * @return The received data frame from the SPI bus. - */ -uint16_t spi_lld_polled_exchange(SPIDriver *spip, uint16_t frame) { - - spi_start_xfer(spip, true); - - spip->spi->PUSHR = SPIx_PUSHR_TXDATA(frame); - - while ((spip->spi->SR & SPIx_SR_RFDF) == 0) - ; - - frame = spip->spi->POPR; - - spi_stop_xfer(spip); - - return frame; -} - -#endif /* HAL_USE_SPI */ - -/** @} */ diff --git a/os/hal/ports/KINETIS/MK66F18/hal_spi_lld.h b/os/hal/ports/KINETIS/MK66F18/hal_spi_lld.h deleted file mode 100644 index 0cf108e..0000000 --- a/os/hal/ports/KINETIS/MK66F18/hal_spi_lld.h +++ /dev/null @@ -1,261 +0,0 @@ -/* - ChibiOS - Copyright (C) 2014-2015 Fabio Utzig - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -/** - * @file KINETIS/spi_lld.h - * @brief KINETIS SPI subsystem low level driver header. - * - * @addtogroup SPI - * @{ - */ - -#ifndef HAL_SPI_LLD_H_ -#define HAL_SPI_LLD_H_ - -#if HAL_USE_SPI || defined(__DOXYGEN__) - -/*===========================================================================*/ -/* Driver constants. */ -/*===========================================================================*/ - -/*===========================================================================*/ -/* Driver pre-compile time settings. */ -/*===========================================================================*/ - -/** - * @name Configuration options - * @{ - */ -/** - * @brief SPI0 driver enable switch. - * @details If set to @p TRUE the support for SPI0 is included. - * @note The default is @p FALSE. - */ -#if !defined(KINETIS_SPI_USE_SPI0) || defined(__DOXYGEN__) -#define KINETIS_SPI_USE_SPI0 FALSE -#endif - -/** - * @brief SPI0 interrupt priority level setting. - */ -#if !defined(KINETIS_SPI_SPI0_IRQ_PRIORITY) || defined(__DOXYGEN__) -#define KINETIS_SPI_SPI0_IRQ_PRIORITY 10 -#endif - -/** - * @brief SPI1 driver enable switch. - * @details If set to @p TRUE the support for SPI0 is included. - * @note The default is @p FALSE. - */ -#if !defined(KINETIS_SPI_USE_SPI1) || defined(__DOXYGEN__) -#define KINETIS_SPI_USE_SPI1 FALSE -#endif - -/** - * @brief SPI1 interrupt priority level setting. - */ -#if !defined(KINETIS_SPI_SPI1_IRQ_PRIORITY) || defined(__DOXYGEN__) -#define KINETIS_SPI_SPI1_IRQ_PRIORITY 10 -#endif - -/*===========================================================================*/ -/* Derived constants and error checks. */ -/*===========================================================================*/ - -#if KINETIS_SPI_USE_SPI0 && !KINETIS_HAS_SPI0 -#error "SPI0 not present in the selected device" -#endif - -#if KINETIS_SPI_USE_SPI1 && !KINETIS_HAS_SPI1 -#error "SPI1 not present in the selected device" -#endif - -#if KINETIS_SPI_USE_SPI0 && KINETIS_SPI_USE_SPI1 -#error "Only one SPI peripheral can be enabled" -#endif - -#if !(KINETIS_SPI_USE_SPI0 || KINETIS_SPI_USE_SPI1) -#error "SPI driver activated but no SPI peripheral assigned" -#endif - -#if KINETIS_SPI_USE_SPI0 && \ - !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_SPI_SPI0_IRQ_PRIORITY) -#error "Invalid IRQ priority assigned to SPI0" -#endif - -#if KINETIS_SPI_USE_SPI1 && \ - !OSAL_IRQ_IS_VALID_PRIORITY(KINETIS_SPI_SPI1_IRQ_PRIORITY) -#error "Invalid IRQ priority assigned to SPI1" -#endif - -/*===========================================================================*/ -/* Driver data structures and types. */ -/*===========================================================================*/ - -/** - * @brief Type of a structure representing an SPI driver. - */ -typedef struct SPIDriver SPIDriver; - -/** - * @brief SPI notification callback type. - * - * @param[in] spip pointer to the @p SPIDriver object triggering the - * callback - */ -typedef void (*spicallback_t)(SPIDriver *spip); - -/** - * @brief Driver configuration structure. - */ -typedef struct { - /** - * @brief Operation complete callback or @p NULL. - */ - spicallback_t end_cb; - /* End of the mandatory fields.*/ - /** - * @brief The chip select line port - when not using pcs. - */ - ioportid_t ssport; - /** - * @brief The chip select line pad number - when not using pcs. - */ - uint16_t sspad; - /** - * @brief SPI initialization data. - */ - uint32_t tar0; -} SPIConfig; - -/** - * @brief Structure representing a SPI driver. - */ -struct SPIDriver { - /** - * @brief Driver state. - */ - spistate_t state; - /** - * @brief Current configuration data. - */ - const SPIConfig *config; -#if SPI_USE_WAIT || defined(__DOXYGEN__) - /** - * @brief Waiting thread. - */ - thread_reference_t thread; -#endif /* SPI_USE_WAIT */ -#if SPI_USE_MUTUAL_EXCLUSION || defined(__DOXYGEN__) - /** - * @brief Mutex protecting the bus. - */ - mutex_t mutex; -#endif /* SPI_USE_MUTUAL_EXCLUSION */ -#if defined(SPI_DRIVER_EXT_FIELDS) - SPI_DRIVER_EXT_FIELDS -#endif - /* End of the mandatory fields.*/ - /** - * @brief Pointer to the SPIx registers block. - */ - SPI_TypeDef *spi; - /** - * @brief Number of bytes/words of data to transfer. - */ - size_t count; - /** - * @brief Word size in bytes. - */ - size_t word_size; - /** - * @brief Pointer to the buffer with data to send. - */ - const uint8_t *txbuf; - /** - * @brief Pointer to the buffer to put received data. - */ - uint8_t *rxbuf; -}; - -/*===========================================================================*/ -/* Driver macros. */ -/*===========================================================================*/ - -/* TAR settings for n bits at SYSCLK / 2 */ -#define KINETIS_SPI_TAR_SYSCLK_DIV_2(n)\ - SPIx_CTARn_FMSZ((n) - 1) | \ - SPIx_CTARn_CPOL | \ - SPIx_CTARn_CPHA | \ - SPIx_CTARn_DBR | \ - SPIx_CTARn_PBR(0) | \ - SPIx_CTARn_BR(0) | \ - SPIx_CTARn_CSSCK(0) | \ - SPIx_CTARn_ASC(0) | \ - SPIx_CTARn_DT(0) - -/* TAR settings for n bits at SYSCLK / 4096 for debugging */ -#define KINETIS_SPI_TAR_SYSCLK_DIV_4096(n) \ - SPIx_CTARn_FMSZ(((n) - 1)) | \ - SPIx_CTARn_CPOL | \ - SPIx_CTARn_CPHA | \ - SPIx_CTARn_PBR(0) | \ - SPIx_CTARn_BR(0xB) | \ - SPIx_CTARn_CSSCK(0xB) | \ - SPIx_CTARn_ASC(0x7) | \ - SPIx_CTARn_DT(0xB) - -#define KINETIS_SPI_TAR_8BIT_FAST KINETIS_SPI_TAR_SYSCLK_DIV_2(8) -#define KINETIS_SPI_TAR_8BIT_SLOW KINETIS_SPI_TAR_SYSCLK_DIV_4096(8) - -#define KINETIS_SPI_TAR0_DEFAULT KINETIS_SPI_TAR_SYSCLK_DIV_2(8) -#define KINETIS_SPI_TAR1_DEFAULT KINETIS_SPI_TAR_SYSCLK_DIV_2(8) - -/*===========================================================================*/ -/* External declarations. */ -/*===========================================================================*/ - -#if KINETIS_SPI_USE_SPI0 && !defined(__DOXYGEN__) -extern SPIDriver SPID1; -#endif - -#if KINETIS_SPI_USE_SPI1 && !defined(__DOXYGEN__) -extern SPIDriver SPID2; -#endif - -#ifdef __cplusplus -extern "C" { -#endif - void spi_lld_init(void); - void spi_lld_start(SPIDriver *spip); - void spi_lld_stop(SPIDriver *spip); - void spi_lld_select(SPIDriver *spip); - void spi_lld_unselect(SPIDriver *spip); - void spi_lld_ignore(SPIDriver *spip, size_t n); - void spi_lld_exchange(SPIDriver *spip, size_t n, - const void *txbuf, void *rxbuf); - void spi_lld_send(SPIDriver *spip, size_t n, const void *txbuf); - void spi_lld_receive(SPIDriver *spip, size_t n, void *rxbuf); - uint16_t spi_lld_polled_exchange(SPIDriver *spip, uint16_t frame); -#ifdef __cplusplus -} -#endif - -#endif /* HAL_USE_SPI */ - -#endif /* HAL_SPI_LLD_H_ */ - -/** @} */ diff --git a/os/hal/ports/KINETIS/MK66F18/platform.mk b/os/hal/ports/KINETIS/MK66F18/platform.mk index 0e6be12..ce16bd5 100644 --- a/os/hal/ports/KINETIS/MK66F18/platform.mk +++ b/os/hal/ports/KINETIS/MK66F18/platform.mk @@ -1,19 +1,33 @@ -# List of all platform files. -PLATFORMSRC = ${CHIBIOS}/os/hal/ports/common/ARMCMx/nvic.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/MK66F18/hal_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_pal_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_serial_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/MK66F18/hal_spi_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_i2c_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_ext_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_adc_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_gpt_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_sdc_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/MK66F18/hal_pwm_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_st_lld.c \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/hal_usb_lld.c +PLATFORMSRC_CONTRIB := ${CHIBIOS}/os/hal/ports/common/ARMCMx/nvic.c \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/MK66F18/hal_lld.c \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PITv1/hal_st_lld.c + +PLATFORMINC_CONTRIB := ${CHIBIOS}/os/hal/ports/common/ARMCMx \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD \ + ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/MK66F18 + +ifeq ($(USE_SMART_BUILD),yes) -# Required include directories -PLATFORMINC = ${CHIBIOS}/os/hal/ports/common/ARMCMx \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/MK66F18 \ - ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD +# Configuration files directory +ifeq ($(CONFDIR),) + CONFDIR = . +endif + +HALCONF := $(strip $(shell cat $(CONFDIR)/halconf.h $(CONFDIR)/halconf_community.h | egrep -e "\#define")) + +endif + +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/GPIOv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/UARTv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/SPIv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/I2Cv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PORTv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/ADCv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/PITv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/SDHCv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/FTMv1/driver.mk +include ${CHIBIOS_CONTRIB}/os/hal/ports/KINETIS/LLD/USBHSv1/driver.mk + +# Shared variables +ALLCSRC += $(PLATFORMSRC_CONTRIB) +ALLINC += $(PLATFORMINC_CONTRIB) -- cgit v1.2.3