2013-08-04 13:38:53 +00:00
|
|
|
/*
|
2015-01-11 13:56:55 +00:00
|
|
|
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
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 STM32/GPIOv2/pal_lld.c
|
2015-11-12 16:28:17 +00:00
|
|
|
* @brief STM32 PAL low level driver code.
|
2013-08-04 13:38:53 +00:00
|
|
|
*
|
|
|
|
* @addtogroup PAL
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
#if HAL_USE_PAL || defined(__DOXYGEN__)
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local definitions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2015-07-26 06:09:53 +00:00
|
|
|
#if defined(STM32L0XX) || defined(STM32L1XX)
|
2014-10-02 12:06:56 +00:00
|
|
|
#define AHB_EN_MASK STM32_GPIO_EN_MASK
|
2015-07-26 06:09:53 +00:00
|
|
|
#define AHB_LPEN_MASK AHB_EN_MASK
|
2014-01-02 15:11:59 +00:00
|
|
|
|
2015-07-26 06:09:53 +00:00
|
|
|
#elif defined(STM32F0XX) || defined(STM32F3XX) || defined(STM32F37X)
|
2014-09-28 08:06:08 +00:00
|
|
|
#define AHB_EN_MASK STM32_GPIO_EN_MASK
|
2015-07-26 06:09:53 +00:00
|
|
|
#define AHB_LPEN_MASK 0
|
2014-01-02 15:11:59 +00:00
|
|
|
|
2015-08-01 17:25:29 +00:00
|
|
|
#elif defined(STM32F2XX) || defined(STM32F4XX) || defined(STM32F7XX)
|
2014-10-02 12:00:19 +00:00
|
|
|
#define AHB1_EN_MASK STM32_GPIO_EN_MASK
|
2013-08-04 13:38:53 +00:00
|
|
|
#define AHB1_LPEN_MASK AHB1_EN_MASK
|
2014-01-02 15:11:59 +00:00
|
|
|
|
2013-08-04 13:38:53 +00:00
|
|
|
#else
|
|
|
|
#error "missing or unsupported platform for GPIOv2 PAL driver"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local variables and types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2013-08-12 15:42:54 +00:00
|
|
|
static void initgpio(stm32_gpio_t *gpiop, const stm32_gpio_setup_t *config) {
|
2013-08-04 13:38:53 +00:00
|
|
|
|
|
|
|
gpiop->OTYPER = config->otyper;
|
|
|
|
gpiop->OSPEEDR = config->ospeedr;
|
|
|
|
gpiop->PUPDR = config->pupdr;
|
|
|
|
gpiop->ODR = config->odr;
|
|
|
|
gpiop->AFRL = config->afrl;
|
|
|
|
gpiop->AFRH = config->afrh;
|
|
|
|
gpiop->MODER = config->moder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver interrupt handlers. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief STM32 I/O ports configuration.
|
|
|
|
* @details Ports A-D(E, F, G, H) clocks enabled.
|
|
|
|
*
|
|
|
|
* @param[in] config the STM32 ports configuration
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void _pal_lld_init(const PALConfig *config) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enables the GPIO related clocks.
|
|
|
|
*/
|
2015-07-26 06:09:53 +00:00
|
|
|
#if defined(STM32L0XX)
|
2015-07-26 06:17:10 +00:00
|
|
|
RCC->IOPENR |= AHB_EN_MASK;
|
2015-07-26 06:09:53 +00:00
|
|
|
RCC->IOPSMENR |= AHB_LPEN_MASK;
|
|
|
|
#elif defined(STM32L1XX)
|
2013-08-04 13:38:53 +00:00
|
|
|
rccEnableAHB(AHB_EN_MASK, TRUE);
|
|
|
|
RCC->AHBLPENR |= AHB_LPEN_MASK;
|
|
|
|
#elif defined(STM32F0XX)
|
|
|
|
rccEnableAHB(AHB_EN_MASK, TRUE);
|
2014-10-18 09:23:06 +00:00
|
|
|
#elif defined(STM32F3XX) || defined(STM32F37X)
|
2013-08-04 13:38:53 +00:00
|
|
|
rccEnableAHB(AHB_EN_MASK, TRUE);
|
2015-08-03 13:11:07 +00:00
|
|
|
#elif defined(STM32F2XX) || defined(STM32F4XX) || defined(STM32F7XX)
|
2013-08-04 13:38:53 +00:00
|
|
|
RCC->AHB1ENR |= AHB1_EN_MASK;
|
|
|
|
RCC->AHB1LPENR |= AHB1_LPEN_MASK;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initial GPIO setup.
|
|
|
|
*/
|
2014-01-02 15:11:59 +00:00
|
|
|
#if STM32_HAS_GPIOA
|
2013-08-04 13:38:53 +00:00
|
|
|
initgpio(GPIOA, &config->PAData);
|
2014-01-02 15:11:59 +00:00
|
|
|
#endif
|
|
|
|
#if STM32_HAS_GPIOB
|
2013-08-04 13:38:53 +00:00
|
|
|
initgpio(GPIOB, &config->PBData);
|
2014-01-02 15:11:59 +00:00
|
|
|
#endif
|
|
|
|
#if STM32_HAS_GPIOC
|
2013-08-04 13:38:53 +00:00
|
|
|
initgpio(GPIOC, &config->PCData);
|
2014-01-02 15:11:59 +00:00
|
|
|
#endif
|
|
|
|
#if STM32_HAS_GPIOD
|
2013-08-04 13:38:53 +00:00
|
|
|
initgpio(GPIOD, &config->PDData);
|
2014-01-02 15:11:59 +00:00
|
|
|
#endif
|
2013-08-04 13:38:53 +00:00
|
|
|
#if STM32_HAS_GPIOE
|
|
|
|
initgpio(GPIOE, &config->PEData);
|
|
|
|
#endif
|
|
|
|
#if STM32_HAS_GPIOF
|
|
|
|
initgpio(GPIOF, &config->PFData);
|
|
|
|
#endif
|
|
|
|
#if STM32_HAS_GPIOG
|
|
|
|
initgpio(GPIOG, &config->PGData);
|
|
|
|
#endif
|
|
|
|
#if STM32_HAS_GPIOH
|
|
|
|
initgpio(GPIOH, &config->PHData);
|
|
|
|
#endif
|
|
|
|
#if STM32_HAS_GPIOI
|
|
|
|
initgpio(GPIOI, &config->PIData);
|
|
|
|
#endif
|
2015-08-03 13:11:07 +00:00
|
|
|
#if STM32_HAS_GPIOJ
|
|
|
|
initgpio(GPIOJ, &config->PJData);
|
|
|
|
#endif
|
|
|
|
#if STM32_HAS_GPIOK
|
|
|
|
initgpio(GPIOK, &config->PKData);
|
|
|
|
#endif
|
2013-08-04 13:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Pads mode setup.
|
|
|
|
* @details This function programs a pads group belonging to the same port
|
|
|
|
* with the specified mode.
|
|
|
|
* @note @p PAL_MODE_UNCONNECTED is implemented as push pull at minimum
|
|
|
|
* speed.
|
|
|
|
*
|
|
|
|
* @param[in] port the port identifier
|
|
|
|
* @param[in] mask the group mask
|
|
|
|
* @param[in] mode the mode
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void _pal_lld_setgroupmode(ioportid_t port,
|
|
|
|
ioportmask_t mask,
|
|
|
|
iomode_t mode) {
|
|
|
|
|
|
|
|
uint32_t moder = (mode & PAL_STM32_MODE_MASK) >> 0;
|
|
|
|
uint32_t otyper = (mode & PAL_STM32_OTYPE_MASK) >> 2;
|
|
|
|
uint32_t ospeedr = (mode & PAL_STM32_OSPEED_MASK) >> 3;
|
|
|
|
uint32_t pupdr = (mode & PAL_STM32_PUDR_MASK) >> 5;
|
|
|
|
uint32_t altr = (mode & PAL_STM32_ALTERNATE_MASK) >> 7;
|
|
|
|
uint32_t bit = 0;
|
2015-11-13 09:52:26 +00:00
|
|
|
while (true) {
|
2013-08-04 13:38:53 +00:00
|
|
|
if ((mask & 1) != 0) {
|
|
|
|
uint32_t altrmask, m1, m2, m4;
|
|
|
|
|
|
|
|
altrmask = altr << ((bit & 7) * 4);
|
|
|
|
m4 = 15 << ((bit & 7) * 4);
|
|
|
|
if (bit < 8)
|
|
|
|
port->AFRL = (port->AFRL & ~m4) | altrmask;
|
|
|
|
else
|
|
|
|
port->AFRH = (port->AFRH & ~m4) | altrmask;
|
|
|
|
m1 = 1 << bit;
|
|
|
|
port->OTYPER = (port->OTYPER & ~m1) | otyper;
|
|
|
|
m2 = 3 << (bit * 2);
|
|
|
|
port->OSPEEDR = (port->OSPEEDR & ~m2) | ospeedr;
|
|
|
|
port->PUPDR = (port->PUPDR & ~m2) | pupdr;
|
|
|
|
port->MODER = (port->MODER & ~m2) | moder;
|
|
|
|
}
|
|
|
|
mask >>= 1;
|
|
|
|
if (!mask)
|
|
|
|
return;
|
|
|
|
otyper <<= 1;
|
|
|
|
ospeedr <<= 2;
|
|
|
|
pupdr <<= 2;
|
|
|
|
moder <<= 2;
|
|
|
|
bit++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAL_USE_PAL */
|
|
|
|
|
|
|
|
/** @} */
|