2011-06-19 10:45:38 +00:00
|
|
|
/*
|
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2012-01-21 14:29:42 +00:00
|
|
|
2011,2012 Giovanni Di Sirio.
|
2011-06-19 10:45:38 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-07-25 14:25:21 +00:00
|
|
|
* @file STM32/GPIOv2/pal_lld.c
|
2011-11-22 08:00:12 +00:00
|
|
|
* @brief STM32L1xx/STM32F2xx/STM32F4xx GPIO low level driver code.
|
2011-06-19 10:45:38 +00:00
|
|
|
*
|
|
|
|
* @addtogroup PAL
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
#if HAL_USE_PAL || defined(__DOXYGEN__)
|
|
|
|
|
2012-12-25 08:20:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local definitions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2011-07-27 12:22:07 +00:00
|
|
|
#if defined(STM32L1XX_MD)
|
2011-09-16 17:38:22 +00:00
|
|
|
#define AHB_EN_MASK (RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN | \
|
|
|
|
RCC_AHBENR_GPIOCEN | RCC_AHBENR_GPIODEN | \
|
2011-07-27 12:22:07 +00:00
|
|
|
RCC_AHBENR_GPIOEEN | RCC_AHBENR_GPIOHEN)
|
2012-05-18 15:29:20 +00:00
|
|
|
#define AHB_LPEN_MASK AHB_EN_MASK
|
|
|
|
#elif defined(STM32F0XX)
|
|
|
|
#define AHB_EN_MASK (RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN | \
|
|
|
|
RCC_AHBENR_GPIOCEN | RCC_AHBENR_GPIODEN | \
|
|
|
|
RCC_AHBENR_GPIOFEN)
|
2011-07-27 12:22:07 +00:00
|
|
|
#elif defined(STM32F2XX)
|
2011-09-16 17:38:22 +00:00
|
|
|
#define AHB1_EN_MASK (RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | \
|
|
|
|
RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIODEN | \
|
|
|
|
RCC_AHB1ENR_GPIOEEN | RCC_AHB1ENR_GPIOFEN | \
|
|
|
|
RCC_AHB1ENR_GPIOGEN | RCC_AHB1ENR_GPIOHEN | \
|
2011-11-05 14:40:48 +00:00
|
|
|
RCC_AHB1ENR_GPIOIEN)
|
|
|
|
#define AHB1_LPEN_MASK AHB1_EN_MASK
|
2012-10-24 09:46:46 +00:00
|
|
|
#elif defined(STM32F30X)
|
|
|
|
#define AHB_EN_MASK (RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN | \
|
|
|
|
RCC_AHBENR_GPIOCEN | RCC_AHBENR_GPIODEN | \
|
|
|
|
RCC_AHBENR_GPIOEEN | RCC_AHBENR_GPIOFEN)
|
2011-11-05 14:40:48 +00:00
|
|
|
#elif defined(STM32F4XX)
|
|
|
|
#define AHB1_EN_MASK (RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | \
|
|
|
|
RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIODEN | \
|
|
|
|
RCC_AHB1ENR_GPIOEEN | RCC_AHB1ENR_GPIOFEN | \
|
|
|
|
RCC_AHB1ENR_GPIOGEN | RCC_AHB1ENR_GPIOHEN | \
|
|
|
|
RCC_AHB1ENR_GPIOIEN)
|
2011-07-27 17:23:19 +00:00
|
|
|
#define AHB1_LPEN_MASK AHB1_EN_MASK
|
2011-06-19 10:45:38 +00:00
|
|
|
#else
|
2012-03-26 09:13:37 +00:00
|
|
|
#error "missing or unsupported platform for GPIOv2 PAL driver"
|
2011-06-19 10:45:38 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2011-06-19 14:41:33 +00:00
|
|
|
static void initgpio(GPIO_TypeDef *gpiop, const stm32_gpio_setup_t *config) {
|
|
|
|
|
|
|
|
gpiop->OTYPER = config->otyper;
|
|
|
|
gpiop->OSPEEDR = config->ospeedr;
|
|
|
|
gpiop->PUPDR = config->pupdr;
|
2012-09-19 16:50:34 +00:00
|
|
|
gpiop->ODR = config->odr;
|
2011-07-27 12:22:07 +00:00
|
|
|
gpiop->AFRL = config->afrl;
|
|
|
|
gpiop->AFRH = config->afrh;
|
2012-09-19 16:50:34 +00:00
|
|
|
gpiop->MODER = config->moder;
|
2011-06-19 14:41:33 +00:00
|
|
|
}
|
|
|
|
|
2011-06-19 10:45:38 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver interrupt handlers. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief STM32 I/O ports configuration.
|
2011-07-25 14:25:21 +00:00
|
|
|
* @details Ports A-D(E, F, G, H) clocks enabled.
|
2011-06-19 10:45:38 +00:00
|
|
|
*
|
|
|
|
* @param[in] config the STM32 ports configuration
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void _pal_lld_init(const PALConfig *config) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enables the GPIO related clocks.
|
|
|
|
*/
|
2011-07-27 12:22:07 +00:00
|
|
|
#if defined(STM32L1XX_MD)
|
2011-09-16 17:38:22 +00:00
|
|
|
rccEnableAHB(AHB_EN_MASK, TRUE);
|
2012-05-18 15:29:20 +00:00
|
|
|
RCC->AHBLPENR |= AHB_LPEN_MASK;
|
|
|
|
#elif defined(STM32F0XX)
|
|
|
|
rccEnableAHB(AHB_EN_MASK, TRUE);
|
2012-12-04 13:03:14 +00:00
|
|
|
#elif defined(STM32F30X)
|
|
|
|
rccEnableAHB(AHB_EN_MASK, TRUE);
|
2011-11-06 09:39:57 +00:00
|
|
|
#elif defined(STM32F2XX) || defined(STM32F4XX)
|
2011-07-27 17:23:19 +00:00
|
|
|
RCC->AHB1ENR |= AHB1_EN_MASK;
|
|
|
|
RCC->AHB1LPENR |= AHB1_LPEN_MASK;
|
2011-07-27 12:22:07 +00:00
|
|
|
#endif
|
2011-06-19 10:45:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initial GPIO setup.
|
|
|
|
*/
|
2011-06-19 14:41:33 +00:00
|
|
|
initgpio(GPIOA, &config->PAData);
|
|
|
|
initgpio(GPIOB, &config->PBData);
|
|
|
|
initgpio(GPIOC, &config->PCData);
|
|
|
|
initgpio(GPIOD, &config->PDData);
|
|
|
|
#if STM32_HAS_GPIOE
|
|
|
|
initgpio(GPIOE, &config->PEData);
|
2011-06-19 10:45:38 +00:00
|
|
|
#endif
|
2011-06-19 14:41:33 +00:00
|
|
|
#if STM32_HAS_GPIOF
|
|
|
|
initgpio(GPIOF, &config->PFData);
|
2011-06-19 10:45:38 +00:00
|
|
|
#endif
|
2011-06-19 14:41:33 +00:00
|
|
|
#if STM32_HAS_GPIOG
|
|
|
|
initgpio(GPIOG, &config->PGData);
|
|
|
|
#endif
|
|
|
|
#if STM32_HAS_GPIOH
|
|
|
|
initgpio(GPIOH, &config->PHData);
|
2011-06-19 10:45:38 +00:00
|
|
|
#endif
|
2011-07-27 12:22:07 +00:00
|
|
|
#if STM32_HAS_GPIOI
|
|
|
|
initgpio(GPIOI, &config->PIData);
|
|
|
|
#endif
|
2011-06-19 10:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Pads mode setup.
|
|
|
|
* @details This function programs a pads group belonging to the same port
|
|
|
|
* with the specified mode.
|
2011-07-25 14:25:21 +00:00
|
|
|
* @note @p PAL_MODE_UNCONNECTED is implemented as push pull at minimum
|
|
|
|
* speed.
|
2011-06-19 10:45:38 +00:00
|
|
|
*
|
|
|
|
* @param[in] port the port identifier
|
|
|
|
* @param[in] mask the group mask
|
|
|
|
* @param[in] mode the mode
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2011-07-31 17:41:47 +00:00
|
|
|
#if 1
|
2011-06-19 10:45:38 +00:00
|
|
|
void _pal_lld_setgroupmode(ioportid_t port,
|
|
|
|
ioportmask_t mask,
|
2011-07-17 13:07:16 +00:00
|
|
|
iomode_t mode) {
|
2011-07-31 17:41:47 +00:00
|
|
|
|
2011-08-01 14:33:04 +00:00
|
|
|
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;
|
|
|
|
while (TRUE) {
|
2011-07-31 17:41:47 +00:00
|
|
|
if ((mask & 1) != 0) {
|
2011-08-01 14:33:04 +00:00
|
|
|
uint32_t altrmask, m1, m2, m4;
|
|
|
|
|
|
|
|
altrmask = altr << ((bit & 7) * 4);
|
|
|
|
m4 = 15 << ((bit & 7) * 4);
|
2011-07-31 17:41:47 +00:00
|
|
|
if (bit < 8)
|
2011-08-01 14:33:04 +00:00
|
|
|
port->AFRL = (port->AFRL & ~m4) | altrmask;
|
2011-07-31 17:41:47 +00:00
|
|
|
else
|
2011-08-01 14:33:04 +00:00
|
|
|
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;
|
2011-07-31 17:41:47 +00:00
|
|
|
}
|
|
|
|
mask >>= 1;
|
2011-08-01 14:33:04 +00:00
|
|
|
if (!mask)
|
|
|
|
return;
|
|
|
|
otyper <<= 1;
|
|
|
|
ospeedr <<= 2;
|
|
|
|
pupdr <<= 2;
|
|
|
|
moder <<= 2;
|
|
|
|
bit++;
|
2011-07-31 17:41:47 +00:00
|
|
|
}
|
2011-06-19 10:45:38 +00:00
|
|
|
}
|
2011-07-31 18:14:49 +00:00
|
|
|
#else
|
|
|
|
void _pal_lld_setgroupmode(ioportid_t port,
|
|
|
|
ioportmask_t mask,
|
|
|
|
iomode_t mode) {
|
2011-08-01 14:33:04 +00:00
|
|
|
uint32_t afrm, moderm, pupdrm, otyperm, ospeedrm;
|
2011-07-31 18:14:49 +00:00
|
|
|
uint32_t m1 = (uint32_t)mask;
|
|
|
|
uint32_t m2 = 0;
|
|
|
|
uint32_t m4l = 0;
|
|
|
|
uint32_t m4h = 0;
|
|
|
|
uint32_t bit = 0;
|
2011-08-01 14:33:04 +00:00
|
|
|
do {
|
2011-07-31 18:14:49 +00:00
|
|
|
if ((mask & 1) != 0) {
|
|
|
|
m2 |= 3 << bit;
|
|
|
|
if (bit < 16)
|
|
|
|
m4l |= 15 << ((bit & 14) * 2);
|
|
|
|
else
|
|
|
|
m4h |= 15 << ((bit & 14) * 2);
|
|
|
|
}
|
|
|
|
bit += 2;
|
|
|
|
mask >>= 1;
|
2011-08-01 14:33:04 +00:00
|
|
|
} while (mask);
|
|
|
|
|
|
|
|
afrm = ((mode & PAL_STM32_ALTERNATE_MASK) >> 7) * 0x1111;
|
|
|
|
port->AFRL = (port->AFRL & ~m4l) | (afrm & m4l);
|
|
|
|
port->AFRH = (port->AFRH & ~m4h) | (afrm & m4h);
|
|
|
|
|
|
|
|
ospeedrm = ((mode & PAL_STM32_OSPEED_MASK) >> 3) * 0x5555;
|
|
|
|
port->OSPEEDR = (port->OSPEEDR & ~m2) | (ospeedrm & m2);
|
|
|
|
|
|
|
|
otyperm = ((mode & PAL_STM32_OTYPE_MASK) >> 2) * 0xffff;
|
|
|
|
port->OTYPER = (port->OTYPER & ~m1) | (otyperm & m1);
|
|
|
|
|
|
|
|
pupdrm = ((mode & PAL_STM32_PUDR_MASK) >> 5) * 0x5555;
|
|
|
|
port->PUPDR = (port->PUPDR & ~m2) | (pupdrm & m2);
|
|
|
|
|
|
|
|
moderm = ((mode & PAL_STM32_MODE_MASK) >> 0) * 0x5555;
|
|
|
|
port->MODER = (port->MODER & ~m2) | (moderm & m2);
|
2011-07-31 18:14:49 +00:00
|
|
|
}
|
2011-07-31 17:41:47 +00:00
|
|
|
#endif
|
2011-06-19 10:45:38 +00:00
|
|
|
|
|
|
|
#endif /* HAL_USE_PAL */
|
|
|
|
|
|
|
|
/** @} */
|