soc: arm: st_stm32: Cleanup gpio function code.
All series STM32 have mostly the same GPIO architecture and can share the same code for GPIO manipulation. Functions of the external interrupt line control are also the same. This patch extracts common code from them and put them into the 'common' folder. Functions of control GPIO of these series scattered in soc/arm/st_stm32/stm32xx/ folders contain these functions: stm32_gpio_flags_to_conf(), stm32_gpio_configure(), stm32_gpio_set(), stm32_gpio_get, stm32_gpio_enable_int(). This patch merges them into the gpio_stm32.c file. Signed-off-by: Song Qiang <songqiang1304521@gmail.com>
This commit is contained in:
parent
d4ef80ec57
commit
9612f9d840
36 changed files with 376 additions and 1478 deletions
18
soc/arm/st_stm32/common/soc_syscfg_common.h
Normal file
18
soc/arm/st_stm32/common/soc_syscfg_common.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Song Qiang <songqiang1304521@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef __SOC_SYSCFG_COMMON_H
|
||||
#define __SOC_SYSCFG_COMMON_H
|
||||
|
||||
union syscfg_exticr {
|
||||
u32_t val;
|
||||
struct {
|
||||
u16_t exti;
|
||||
u16_t rsvd__16_31;
|
||||
} bit;
|
||||
};
|
||||
|
||||
#endif /* __STM32_SYSCFG_COMMON_H */
|
|
@ -1,3 +1,2 @@
|
|||
zephyr_include_directories(${ZEPHYR_BASE}/drivers)
|
||||
zephyr_sources(soc.c)
|
||||
zephyr_sources_ifdef(CONFIG_GPIO soc_gpio.c)
|
||||
|
|
|
@ -1,133 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2017 RnDity Sp. z o.o.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* Based on reference manual:
|
||||
* STM32F030x4/x6/x8/xC,
|
||||
* STM32F070x6/xB advanced ARM ® -based MCUs
|
||||
*
|
||||
* Chapter 8: General-purpose I/Os (GPIO)
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <device.h>
|
||||
#include "soc.h"
|
||||
#include "soc_registers.h"
|
||||
#include <gpio.h>
|
||||
#include <gpio/gpio_stm32.h>
|
||||
|
||||
int stm32_gpio_flags_to_conf(int flags, int *pincfg)
|
||||
{
|
||||
int direction = flags & GPIO_DIR_MASK;
|
||||
int pud = flags & GPIO_PUD_MASK;
|
||||
|
||||
if (pincfg == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (direction == GPIO_DIR_OUT) {
|
||||
*pincfg = STM32_MODER_OUTPUT_MODE;
|
||||
} else {
|
||||
/* pull-{up,down} maybe? */
|
||||
*pincfg = STM32_MODER_INPUT_MODE;
|
||||
if (pud == GPIO_PUD_PULL_UP) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_UP;
|
||||
} else if (pud == GPIO_PUD_PULL_DOWN) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_DOWN;
|
||||
} else {
|
||||
/* floating */
|
||||
*pincfg = *pincfg | STM32_PUPDR_NO_PULL;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_configure(u32_t *base_addr, int pin, int conf, int altf)
|
||||
{
|
||||
volatile struct stm32f0x_gpio *gpio =
|
||||
(struct stm32f0x_gpio *)(base_addr);
|
||||
unsigned int mode, otype, ospeed, pupd;
|
||||
unsigned int pin_shift = pin << 1;
|
||||
unsigned int afr_bank = pin / 8;
|
||||
unsigned int afr_shift = (pin % 8) << 2;
|
||||
u32_t scratch;
|
||||
|
||||
mode = (conf >> STM32_MODER_SHIFT) & STM32_MODER_MASK;
|
||||
otype = (conf >> STM32_OTYPER_SHIFT) & STM32_OTYPER_MASK;
|
||||
ospeed = (conf >> STM32_OSPEEDR_SHIFT) & STM32_OSPEEDR_MASK;
|
||||
pupd = (conf >> STM32_PUPDR_SHIFT) & STM32_PUPDR_MASK;
|
||||
|
||||
scratch = gpio->moder & ~(STM32_MODER_MASK << pin_shift);
|
||||
gpio->moder = scratch | (mode << pin_shift);
|
||||
|
||||
scratch = gpio->ospeedr & ~(STM32_OSPEEDR_MASK << pin_shift);
|
||||
gpio->ospeedr = scratch | (ospeed << pin_shift);
|
||||
|
||||
scratch = gpio->otyper & ~(STM32_OTYPER_MASK << pin);
|
||||
gpio->otyper = scratch | (otype << pin);
|
||||
|
||||
scratch = gpio->pupdr & ~(STM32_PUPDR_MASK << pin_shift);
|
||||
gpio->pupdr = scratch | (pupd << pin_shift);
|
||||
|
||||
scratch = gpio->afr[afr_bank] & ~(STM32_AFR_MASK << afr_shift);
|
||||
gpio->afr[afr_bank] = scratch | (altf << afr_shift);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_set(u32_t *base, int pin, int value)
|
||||
{
|
||||
struct stm32f0x_gpio *gpio = (struct stm32f0x_gpio *)base;
|
||||
|
||||
int pval = 1 << (pin & 0xf);
|
||||
|
||||
if (value != 0) {
|
||||
gpio->odr |= pval;
|
||||
} else {
|
||||
gpio->odr &= ~pval;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_get(u32_t *base, int pin)
|
||||
{
|
||||
struct stm32f0x_gpio *gpio = (struct stm32f0x_gpio *)base;
|
||||
|
||||
return (gpio->idr >> pin) & 0x1;
|
||||
}
|
||||
|
||||
int stm32_gpio_enable_int(int port, int pin)
|
||||
{
|
||||
volatile struct stm32f0x_syscfg *syscfg =
|
||||
(struct stm32f0x_syscfg *)SYSCFG_BASE;
|
||||
volatile union syscfg__exticr *exticr;
|
||||
|
||||
int shift = 0;
|
||||
|
||||
if (pin <= 3) {
|
||||
exticr = &syscfg->exticr1;
|
||||
} else if (pin <= 7) {
|
||||
exticr = &syscfg->exticr2;
|
||||
} else if (pin <= 11) {
|
||||
exticr = &syscfg->exticr3;
|
||||
} else if (pin <= 15) {
|
||||
exticr = &syscfg->exticr4;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
shift = 4 * (pin % 4);
|
||||
|
||||
exticr->val &= ~(0xf << shift);
|
||||
exticr->val |= port << shift;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -9,6 +9,6 @@
|
|||
|
||||
/* include register mapping headers */
|
||||
#include "flash_registers.h"
|
||||
#include "gpio_registers.h"
|
||||
#include "syscfg_registers.h"
|
||||
|
||||
#endif /* _STM32F0X_SOC_REGISTERS_H_ */
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _STM32F0X_GPIO_REGISTERS_H_
|
||||
#define _STM32F0X_GPIO_REGISTERS_H_
|
||||
#ifndef _STM32_SYSCFG_REGISTERS_H_
|
||||
#define _STM32_SYSCFG_REGISTERS_H_
|
||||
|
||||
/**
|
||||
* @brief
|
||||
|
@ -14,22 +14,10 @@
|
|||
* STM32F030x4/x6/x8/xC,
|
||||
* STM32F070x6/xB advanced ARM ® -based MCUs
|
||||
*
|
||||
* Chapter 8: General-purpose I/Os (GPIO)
|
||||
* Chapter 9: System configuration controller (SYSCFG)
|
||||
*/
|
||||
|
||||
struct stm32f0x_gpio {
|
||||
u32_t moder;
|
||||
u32_t otyper;
|
||||
u32_t ospeedr;
|
||||
u32_t pupdr;
|
||||
u32_t idr;
|
||||
u32_t odr;
|
||||
u32_t bsrr;
|
||||
u32_t lckr;
|
||||
u32_t afr[2];
|
||||
u32_t brr;
|
||||
};
|
||||
#include "../common/soc_syscfg_common.h"
|
||||
|
||||
union syscfg_cfgr1 {
|
||||
u32_t val;
|
||||
|
@ -56,22 +44,14 @@ union syscfg_cfgr1 {
|
|||
} bit;
|
||||
};
|
||||
|
||||
union syscfg__exticr {
|
||||
u32_t val;
|
||||
struct {
|
||||
u16_t exti;
|
||||
u16_t rsvd__16_31;
|
||||
} bit;
|
||||
};
|
||||
|
||||
struct stm32f0x_syscfg {
|
||||
struct stm32_syscfg {
|
||||
union syscfg_cfgr1 cfgr1;
|
||||
u32_t rsvd;
|
||||
union syscfg__exticr exticr1;
|
||||
union syscfg__exticr exticr2;
|
||||
union syscfg__exticr exticr3;
|
||||
union syscfg__exticr exticr4;
|
||||
union syscfg_exticr exticr1;
|
||||
union syscfg_exticr exticr2;
|
||||
union syscfg_exticr exticr3;
|
||||
union syscfg_exticr exticr4;
|
||||
u32_t cfgr2;
|
||||
};
|
||||
|
||||
#endif /* _STM32F0X_GPIO_REGISTERS_H_ */
|
||||
#endif /* _STM32_SYSCFG_REGISTERS_H_ */
|
|
@ -2,4 +2,3 @@ zephyr_include_directories(${ZEPHYR_BASE}/drivers)
|
|||
zephyr_sources(
|
||||
soc.c
|
||||
)
|
||||
zephyr_sources_ifdef(CONFIG_GPIO soc_gpio.c)
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _STM32F10X_GPIO_REGISTERS_H_
|
||||
#define _STM32F10X_GPIO_REGISTERS_H_
|
||||
#ifndef _STM32_AFIO_REGISTERS_H_
|
||||
#define _STM32_AFIO_REGISTERS_H_
|
||||
|
||||
/**
|
||||
* @brief
|
||||
|
@ -18,16 +18,7 @@
|
|||
* (GPIOs and AFIOs)
|
||||
*/
|
||||
|
||||
/* 9.2 GPIO registers - each GPIO port controls 16 pins */
|
||||
struct stm32f10x_gpio {
|
||||
u32_t crl;
|
||||
u32_t crh;
|
||||
u32_t idr;
|
||||
u32_t odr;
|
||||
u32_t bsrr;
|
||||
u32_t brr;
|
||||
u32_t lckr;
|
||||
};
|
||||
#include "../common/soc_syscfg_common.h"
|
||||
|
||||
/* 9.4.1 AFIO_EVCR */
|
||||
union __afio_evcr {
|
||||
|
@ -67,15 +58,6 @@ union __afio_mapr {
|
|||
} bit;
|
||||
};
|
||||
|
||||
/* 9.4.{3,4,5,6} AFIO_EXTICRx */
|
||||
union __afio_exticr {
|
||||
u32_t val;
|
||||
struct {
|
||||
u16_t rsvd__16_31;
|
||||
u16_t exti;
|
||||
} bit;
|
||||
};
|
||||
|
||||
/* 9.4.7 AFIO_MAPR2 */
|
||||
union __afio_mapr2 {
|
||||
u32_t val;
|
||||
|
@ -92,14 +74,14 @@ union __afio_mapr2 {
|
|||
};
|
||||
|
||||
/* 9.4 AFIO registers */
|
||||
struct stm32f10x_afio {
|
||||
struct stm32_afio {
|
||||
union __afio_evcr evcr;
|
||||
union __afio_mapr mapr;
|
||||
union __afio_exticr exticr1;
|
||||
union __afio_exticr exticr2;
|
||||
union __afio_exticr exticr3;
|
||||
union __afio_exticr exticr4;
|
||||
union syscfg_exticr exticr1;
|
||||
union syscfg_exticr exticr2;
|
||||
union syscfg_exticr exticr3;
|
||||
union syscfg_exticr exticr4;
|
||||
union __afio_mapr2 mapr2;
|
||||
};
|
||||
|
||||
#endif /* _STM32F10X_GPIO_REGISTERS_H_ */
|
||||
#endif /* _STM32_AFIO_REGISTERS_H_ */
|
|
@ -1,184 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Open-RnD Sp. z o.o.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* Based on reference manual:
|
||||
* STM32F101xx, STM32F102xx, STM32F103xx, STM32F105xx and STM32F107xx
|
||||
* advanced ARM ® -based 32-bit MCUs
|
||||
*
|
||||
* Chapter 9: General-purpose and alternate-function I/Os
|
||||
* (GPIOs and AFIOs)
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <device.h>
|
||||
#include "soc.h"
|
||||
#include "soc_registers.h"
|
||||
#include <gpio.h>
|
||||
#include <gpio/gpio_stm32.h>
|
||||
#include <pinmux/stm32/pinmux_stm32.h>
|
||||
|
||||
|
||||
int stm32_gpio_flags_to_conf(int flags, int *pincfg)
|
||||
{
|
||||
int direction = flags & GPIO_DIR_MASK;
|
||||
|
||||
if (pincfg == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (direction == GPIO_DIR_OUT) {
|
||||
/* Pin is configured as an output */
|
||||
*pincfg = (STM32_MODE_OUTPUT | STM32_CNF_GP_OUTPUT |
|
||||
STM32_CNF_PUSH_PULL);
|
||||
} else {
|
||||
/* Pin is configured as an input */
|
||||
int pud = flags & GPIO_PUD_MASK;
|
||||
|
||||
/* pull-{up,down} maybe? */
|
||||
if (pud == GPIO_PUD_PULL_UP) {
|
||||
*pincfg = (STM32_MODE_INPUT | STM32_CNF_IN_PUPD |
|
||||
STM32_PUPD_PULL_UP);
|
||||
} else if (pud == GPIO_PUD_PULL_DOWN) {
|
||||
*pincfg = (STM32_MODE_INPUT | STM32_CNF_IN_PUPD |
|
||||
STM32_PUPD_PULL_DOWN);
|
||||
} else {
|
||||
/* floating */
|
||||
*pincfg = (STM32_MODE_INPUT | STM32_CNF_IN_FLOAT |
|
||||
STM32_PUPD_NO_PULL);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_configure(u32_t *base_addr, int pin, int conf, int altf)
|
||||
{
|
||||
volatile struct stm32f10x_gpio *gpio =
|
||||
(struct stm32f10x_gpio *)(base_addr);
|
||||
int cnf, mode, mode_io;
|
||||
int crpin = pin;
|
||||
|
||||
/* pins are configured in CRL (0-7) and CRH (8-15)
|
||||
* registers
|
||||
*/
|
||||
volatile u32_t *reg = &gpio->crl;
|
||||
|
||||
ARG_UNUSED(altf);
|
||||
|
||||
if (crpin > 7) {
|
||||
reg = &gpio->crh;
|
||||
crpin -= 8;
|
||||
}
|
||||
|
||||
/* each port is configured by 2 registers:
|
||||
* CNFy[1:0]: Port x configuration bits
|
||||
* MODEy[1:0]: Port x mode bits
|
||||
*
|
||||
* memory layout is repeated for every port:
|
||||
* | CNF | MODE |
|
||||
* | [0:1] | [0:1] |
|
||||
*/
|
||||
|
||||
mode_io = (conf >> STM32_MODE_INOUT_SHIFT) & STM32_MODE_INOUT_MASK;
|
||||
|
||||
if (mode_io == STM32_MODE_INPUT) {
|
||||
int in_pudpd = conf & (STM32_PUPD_MASK << STM32_PUPD_SHIFT);
|
||||
|
||||
/* Pin configured in input mode */
|
||||
/* Mode: 00 */
|
||||
mode = mode_io;
|
||||
/* Configuration values: */
|
||||
/* 00: Analog mode */
|
||||
/* 01: Floating input */
|
||||
/* 10: Pull-up/Pull-Down */
|
||||
cnf = (conf >> STM32_CNF_IN_SHIFT) & STM32_CNF_IN_MASK;
|
||||
|
||||
if (in_pudpd == STM32_PUPD_PULL_UP) {
|
||||
/* enable pull up */
|
||||
gpio->odr |= 1 << pin;
|
||||
} else if (in_pudpd == STM32_PUPD_PULL_DOWN) {
|
||||
/* or pull down */
|
||||
gpio->odr &= ~(1 << pin);
|
||||
}
|
||||
} else {
|
||||
/* Pin configured in output mode */
|
||||
int mode_speed = ((conf >> STM32_MODE_OSPEED_SHIFT) & \
|
||||
STM32_MODE_OSPEED_MASK);
|
||||
/* Mode output possible values */
|
||||
/* 01: Max speed 10MHz (default value) */
|
||||
/* 10: Max speed 2MHz */
|
||||
/* 11: Max speed 50MHz */
|
||||
mode = mode_speed + mode_io;
|
||||
/* Configuration possible values */
|
||||
/* x0: Push-pull */
|
||||
/* x1: Open-drain */
|
||||
/* 0x: General Purpose Output */
|
||||
/* 1x: Alternate Function Output */
|
||||
cnf = ((conf >> STM32_CNF_OUT_0_SHIFT) & STM32_CNF_OUT_0_MASK) |
|
||||
(((conf >> STM32_CNF_OUT_1_SHIFT) & STM32_CNF_OUT_1_MASK)
|
||||
<< 1);
|
||||
}
|
||||
|
||||
/* clear bits */
|
||||
*reg &= ~(0xf << (crpin * 4));
|
||||
/* set bits */
|
||||
*reg |= (cnf << (crpin * 4 + 2) | mode << (crpin * 4));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_set(u32_t *base, int pin, int value)
|
||||
{
|
||||
struct stm32f10x_gpio *gpio = (struct stm32f10x_gpio *)base;
|
||||
|
||||
int pval = 1 << (pin & 0xf);
|
||||
|
||||
if (value != 0) {
|
||||
gpio->odr |= pval;
|
||||
} else {
|
||||
gpio->odr &= ~pval;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_get(u32_t *base, int pin)
|
||||
{
|
||||
struct stm32f10x_gpio *gpio = (struct stm32f10x_gpio *)base;
|
||||
|
||||
return (gpio->idr >> pin) & 0x1;
|
||||
}
|
||||
|
||||
int stm32_gpio_enable_int(int port, int pin)
|
||||
{
|
||||
volatile struct stm32f10x_afio *afio =
|
||||
(struct stm32f10x_afio *)AFIO_BASE;
|
||||
volatile union __afio_exticr *exticr;
|
||||
int shift = 0;
|
||||
|
||||
if (pin <= 3) {
|
||||
exticr = &afio->exticr1;
|
||||
} else if (pin <= 7) {
|
||||
exticr = &afio->exticr2;
|
||||
} else if (pin <= 11) {
|
||||
exticr = &afio->exticr3;
|
||||
} else if (pin <= 15) {
|
||||
exticr = &afio->exticr4;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
shift = 4 * (pin % 4);
|
||||
|
||||
exticr->val &= ~(0xf << shift);
|
||||
exticr->val |= port << shift;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -8,7 +8,7 @@
|
|||
#define _STM32F10X_SOC_REGISTERS_H_
|
||||
|
||||
/* include register mapping headers */
|
||||
#include "gpio_registers.h"
|
||||
#include "afio_registers.h"
|
||||
#include "flash_registers.h"
|
||||
|
||||
#endif /* _STM32F10X_SOC_REGISTERS_H_ */
|
||||
|
|
|
@ -2,4 +2,3 @@ zephyr_include_directories(${ZEPHYR_BASE}/drivers)
|
|||
zephyr_sources(
|
||||
soc.c
|
||||
)
|
||||
zephyr_sources_ifdef(CONFIG_GPIO soc_gpio.c)
|
|
@ -1,144 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 qianfan Zhao <qianfanguijin@163.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* Based on reference manual:
|
||||
* STM32F205xx, STM32F207xx, STM32F215xx and STM32F217xx
|
||||
* advanced ARM-based 32-bit MCUs
|
||||
*
|
||||
* Chapter 6: General-purpose I/Os (GPIO)
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <device.h>
|
||||
#include "soc.h"
|
||||
#include "soc_registers.h"
|
||||
#include <gpio.h>
|
||||
#include <gpio/gpio_stm32.h>
|
||||
|
||||
int stm32_gpio_flags_to_conf(int flags, int *pincfg)
|
||||
{
|
||||
int direction = flags & GPIO_DIR_MASK;
|
||||
int pud = flags & GPIO_PUD_MASK;
|
||||
|
||||
if (pincfg == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (direction == GPIO_DIR_OUT) {
|
||||
*pincfg = STM32_MODER_OUTPUT_MODE;
|
||||
} else {
|
||||
/* pull-{up,down} maybe? */
|
||||
*pincfg = STM32_MODER_INPUT_MODE;
|
||||
if (pud == GPIO_PUD_PULL_UP) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_UP;
|
||||
} else if (pud == GPIO_PUD_PULL_DOWN) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_DOWN;
|
||||
} else {
|
||||
/* floating */
|
||||
*pincfg = *pincfg | STM32_PUPDR_NO_PULL;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_configure(u32_t *base_addr, int pin, int conf, int altf)
|
||||
{
|
||||
volatile struct stm32f2x_gpio *gpio =
|
||||
(struct stm32f2x_gpio *)(base_addr);
|
||||
unsigned int mode, otype, ospeed, pupd;
|
||||
unsigned int pin_shift = pin << 1;
|
||||
unsigned int afr_bank = pin / 8;
|
||||
unsigned int afr_shift = (pin % 8) << 2;
|
||||
u32_t scratch;
|
||||
|
||||
mode = (conf >> STM32_MODER_SHIFT) & STM32_MODER_MASK;
|
||||
otype = (conf >> STM32_OTYPER_SHIFT) & STM32_OTYPER_MASK;
|
||||
ospeed = (conf >> STM32_OSPEEDR_SHIFT) & STM32_OSPEEDR_MASK;
|
||||
pupd = (conf >> STM32_PUPDR_SHIFT) & STM32_PUPDR_MASK;
|
||||
|
||||
scratch = gpio->moder & ~(STM32_MODER_MASK << pin_shift);
|
||||
gpio->moder = scratch | (mode << pin_shift);
|
||||
|
||||
scratch = gpio->ospeedr & ~(STM32_OSPEEDR_MASK << pin_shift);
|
||||
gpio->ospeedr = scratch | (ospeed << pin_shift);
|
||||
|
||||
scratch = gpio->otyper & ~(STM32_OTYPER_MASK << pin);
|
||||
gpio->otyper = scratch | (otype << pin);
|
||||
|
||||
scratch = gpio->pupdr & ~(STM32_PUPDR_MASK << pin_shift);
|
||||
gpio->pupdr = scratch | (pupd << pin_shift);
|
||||
|
||||
scratch = gpio->afr[afr_bank] & ~(STM32_AFR_MASK << afr_shift);
|
||||
gpio->afr[afr_bank] = scratch | (altf << afr_shift);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_set(u32_t *base, int pin, int value)
|
||||
{
|
||||
struct stm32f2x_gpio *gpio = (struct stm32f2x_gpio *)base;
|
||||
|
||||
int pval = 1 << (pin & 0xf);
|
||||
|
||||
if (value != 0) {
|
||||
gpio->odr |= pval;
|
||||
} else {
|
||||
gpio->odr &= ~pval;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_get(u32_t *base, int pin)
|
||||
{
|
||||
struct stm32f2x_gpio *gpio = (struct stm32f2x_gpio *)base;
|
||||
|
||||
return (gpio->idr >> pin) & 0x1;
|
||||
}
|
||||
|
||||
int stm32_gpio_enable_int(int port, int pin)
|
||||
{
|
||||
volatile struct stm32f2x_syscfg *syscfg =
|
||||
(struct stm32f2x_syscfg *)SYSCFG_BASE;
|
||||
volatile union syscfg_exticr *exticr;
|
||||
|
||||
/* Enable System Configuration Controller clock. */
|
||||
struct device *clk =
|
||||
device_get_binding(STM32_CLOCK_CONTROL_NAME);
|
||||
|
||||
struct stm32_pclken pclken = {
|
||||
.bus = STM32_CLOCK_BUS_APB2,
|
||||
.enr = LL_APB2_GRP1_PERIPH_SYSCFG
|
||||
};
|
||||
|
||||
clock_control_on(clk, (clock_control_subsys_t *) &pclken);
|
||||
|
||||
int shift = 0;
|
||||
|
||||
if (pin <= 3) {
|
||||
exticr = &syscfg->exticr1;
|
||||
} else if (pin <= 7) {
|
||||
exticr = &syscfg->exticr2;
|
||||
} else if (pin <= 11) {
|
||||
exticr = &syscfg->exticr3;
|
||||
} else if (pin <= 15) {
|
||||
exticr = &syscfg->exticr4;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
shift = 4 * (pin % 4);
|
||||
|
||||
exticr->val &= ~(0xf << shift);
|
||||
exticr->val |= port << shift;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -8,6 +8,6 @@
|
|||
#define _STM32F2X_SOC_REGISTERS_H_
|
||||
|
||||
/* include register mapping headers */
|
||||
#include "gpio_registers.h"
|
||||
#include "syscfg_registers.h"
|
||||
|
||||
#endif /* _STM32F2X_SOC_REGISTERS_H_ */
|
||||
|
|
|
@ -4,42 +4,22 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _STM32F2X_GPIO_REGISTERS_H_
|
||||
#define _STM32F2X_GPIO_REGISTERS_H_
|
||||
#ifndef _STM32_SYSCFG_REGISTERS_H_
|
||||
#define _STM32_SYSCFG_REGISTERS_H_
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* Based on reference manual:
|
||||
* stm32f2X advanced ARM ® -based 32-bit MCUs
|
||||
|
||||
*
|
||||
* Chapter 6: General-purpose I/Os (GPIO)
|
||||
* Chapter 7: System configuration controller (SYSCFG)
|
||||
*/
|
||||
|
||||
struct stm32f2x_gpio {
|
||||
u32_t moder;
|
||||
u32_t otyper;
|
||||
u32_t ospeedr;
|
||||
u32_t pupdr;
|
||||
u32_t idr;
|
||||
u32_t odr;
|
||||
u32_t bsrr;
|
||||
u32_t lckr;
|
||||
u32_t afr[2];
|
||||
};
|
||||
|
||||
union syscfg_exticr {
|
||||
u32_t val;
|
||||
struct {
|
||||
u16_t rsvd__16_31;
|
||||
u16_t exti;
|
||||
} bit;
|
||||
};
|
||||
#include "../common/soc_syscfg_common.h"
|
||||
|
||||
/* 7.2 SYSCFG registers */
|
||||
struct stm32f2x_syscfg {
|
||||
struct stm32_syscfg {
|
||||
u32_t memrmp;
|
||||
u32_t pmc;
|
||||
union syscfg_exticr exticr1;
|
||||
|
@ -49,4 +29,4 @@ struct stm32f2x_syscfg {
|
|||
u32_t cmpcr;
|
||||
};
|
||||
|
||||
#endif /* _STM32F2X_GPIO_REGISTERS_H_ */
|
||||
#endif /* _STM32_SYSCFG_REGISTERS_H_ */
|
|
@ -2,4 +2,3 @@ zephyr_include_directories(${ZEPHYR_BASE}/drivers)
|
|||
zephyr_sources(
|
||||
soc.c
|
||||
)
|
||||
zephyr_sources_ifdef(CONFIG_GPIO soc_gpio.c)
|
||||
|
|
|
@ -1,146 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 RnDity Sp. z o.o.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* Based on reference manual:
|
||||
* STM32F303xB/C/D/E, STM32F303x6/8, STM32F328x8, STM32F358xC,
|
||||
* STM32F398xE advanced ARM ® -based MCUs
|
||||
*
|
||||
* Chapter 11: General-purpose I/Os (GPIO)
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <device.h>
|
||||
#include "soc.h"
|
||||
#include "soc_registers.h"
|
||||
#include <gpio.h>
|
||||
#include <gpio/gpio_stm32.h>
|
||||
|
||||
|
||||
int stm32_gpio_flags_to_conf(int flags, int *pincfg)
|
||||
{
|
||||
int direction = flags & GPIO_DIR_MASK;
|
||||
int pud = flags & GPIO_PUD_MASK;
|
||||
|
||||
if (pincfg == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (direction == GPIO_DIR_OUT) {
|
||||
*pincfg = STM32_MODER_OUTPUT_MODE;
|
||||
} else {
|
||||
/* pull-{up,down} maybe? */
|
||||
*pincfg = STM32_MODER_INPUT_MODE;
|
||||
|
||||
if (pud == GPIO_PUD_PULL_UP) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_UP;
|
||||
} else if (pud == GPIO_PUD_PULL_DOWN) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_DOWN;
|
||||
} else {
|
||||
/* floating */
|
||||
*pincfg = *pincfg | STM32_PUPDR_NO_PULL;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_configure(u32_t *base_addr, int pin, int conf, int altf)
|
||||
{
|
||||
volatile struct stm32f3x_gpio *gpio =
|
||||
(struct stm32f3x_gpio *)(base_addr);
|
||||
unsigned int mode, otype, ospeed, pupd;
|
||||
unsigned int pin_shift = pin << 1;
|
||||
unsigned int afr_bank = pin / 8;
|
||||
unsigned int afr_shift = (pin % 8) << 2;
|
||||
u32_t scratch;
|
||||
|
||||
mode = (conf >> STM32_MODER_SHIFT) & STM32_MODER_MASK;
|
||||
otype = (conf >> STM32_OTYPER_SHIFT) & STM32_OTYPER_MASK;
|
||||
ospeed = (conf >> STM32_OSPEEDR_SHIFT) & STM32_OSPEEDR_MASK;
|
||||
pupd = (conf >> STM32_PUPDR_SHIFT) & STM32_PUPDR_MASK;
|
||||
|
||||
scratch = gpio->moder & ~(STM32_MODER_MASK << pin_shift);
|
||||
gpio->moder = scratch | (mode << pin_shift);
|
||||
|
||||
scratch = gpio->ospeedr & ~(STM32_OSPEEDR_MASK << pin_shift);
|
||||
gpio->ospeedr = scratch | (ospeed << pin_shift);
|
||||
|
||||
scratch = gpio->otyper & ~(STM32_OTYPER_MASK << pin);
|
||||
gpio->otyper = scratch | (otype << pin);
|
||||
|
||||
scratch = gpio->pupdr & ~(STM32_PUPDR_MASK << pin_shift);
|
||||
gpio->pupdr = scratch | (pupd << pin_shift);
|
||||
|
||||
scratch = gpio->afr[afr_bank] & ~(STM32_AFR_MASK << afr_shift);
|
||||
gpio->afr[afr_bank] = scratch | (altf << afr_shift);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_set(u32_t *base, int pin, int value)
|
||||
{
|
||||
struct stm32f3x_gpio *gpio = (struct stm32f3x_gpio *)base;
|
||||
|
||||
int pval = 1 << (pin & 0xf);
|
||||
|
||||
if (value != 0) {
|
||||
gpio->odr |= pval;
|
||||
} else {
|
||||
gpio->odr &= ~pval;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_get(u32_t *base, int pin)
|
||||
{
|
||||
struct stm32f3x_gpio *gpio = (struct stm32f3x_gpio *)base;
|
||||
|
||||
return (gpio->idr >> pin) & 0x1;
|
||||
}
|
||||
|
||||
int stm32_gpio_enable_int(int port, int pin)
|
||||
{
|
||||
volatile struct stm32f3x_syscfg *syscfg =
|
||||
(struct stm32f3x_syscfg *)SYSCFG_BASE;
|
||||
volatile union syscfg__exticr *exticr;
|
||||
|
||||
/* Enable System Configuration Controller clock. */
|
||||
struct device *clk =
|
||||
device_get_binding(STM32_CLOCK_CONTROL_NAME);
|
||||
|
||||
struct stm32_pclken pclken = {
|
||||
.bus = STM32_CLOCK_BUS_APB2,
|
||||
.enr = LL_APB2_GRP1_PERIPH_SYSCFG
|
||||
};
|
||||
|
||||
clock_control_on(clk, (clock_control_subsys_t *) &pclken);
|
||||
|
||||
int shift = 0;
|
||||
|
||||
if (pin <= 3) {
|
||||
exticr = &syscfg->exticr1;
|
||||
} else if (pin <= 7) {
|
||||
exticr = &syscfg->exticr2;
|
||||
} else if (pin <= 11) {
|
||||
exticr = &syscfg->exticr3;
|
||||
} else if (pin <= 15) {
|
||||
exticr = &syscfg->exticr4;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
shift = 4 * (pin % 4);
|
||||
|
||||
exticr->val &= ~(0xf << shift);
|
||||
exticr->val |= port << shift;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -9,6 +9,6 @@
|
|||
|
||||
/* include register mapping headers */
|
||||
#include "flash_registers.h"
|
||||
#include "gpio_registers.h"
|
||||
#include "syscfg_registers.h"
|
||||
|
||||
#endif /* _STM32F3X_SOC_REGISTERS_H_ */
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _STM32F3X_GPIO_REGISTERS_H_
|
||||
#define _STM32F3X_GPIO_REGISTERS_H_
|
||||
#ifndef _STM32_SYSCFG_REGISTERS_H_
|
||||
#define _STM32_SYSCFG_REGISTERS_H_
|
||||
|
||||
/**
|
||||
* @brief
|
||||
|
@ -13,22 +13,9 @@
|
|||
* Based on reference manual:
|
||||
* STM32F303xB/C/D/E, STM32F303x6/8, STM32F328x8, STM32F358xC,
|
||||
* STM32F398xE advanced ARM(r)-based MCUs
|
||||
*
|
||||
* Chapter 11: General-purpose I/Os
|
||||
*/
|
||||
|
||||
struct stm32f3x_gpio {
|
||||
u32_t moder;
|
||||
u32_t otyper;
|
||||
u32_t ospeedr;
|
||||
u32_t pupdr;
|
||||
u32_t idr;
|
||||
u32_t odr;
|
||||
u32_t bsrr;
|
||||
u32_t lckr;
|
||||
u32_t afr[2];
|
||||
u32_t brr;
|
||||
};
|
||||
#include "../common/soc_syscfg_common.h"
|
||||
|
||||
union syscfg_cfgr1 {
|
||||
u32_t val;
|
||||
|
@ -66,21 +53,13 @@ union syscfg_rcr {
|
|||
} bit;
|
||||
};
|
||||
|
||||
union syscfg__exticr {
|
||||
u32_t val;
|
||||
struct {
|
||||
u16_t exti;
|
||||
u16_t rsvd__16_31;
|
||||
} bit;
|
||||
};
|
||||
|
||||
struct stm32f3x_syscfg {
|
||||
struct stm32_syscfg {
|
||||
union syscfg_cfgr1 cfgr1;
|
||||
union syscfg_rcr rcr;
|
||||
union syscfg__exticr exticr1;
|
||||
union syscfg__exticr exticr2;
|
||||
union syscfg__exticr exticr3;
|
||||
union syscfg__exticr exticr4;
|
||||
union syscfg_exticr exticr1;
|
||||
union syscfg_exticr exticr2;
|
||||
union syscfg_exticr exticr3;
|
||||
union syscfg_exticr exticr4;
|
||||
u32_t cfgr2;
|
||||
u32_t rsvd_0x1C;
|
||||
u32_t rsvd_0x20;
|
||||
|
@ -98,4 +77,4 @@ struct stm32f3x_syscfg {
|
|||
u32_t cfgr3;
|
||||
};
|
||||
|
||||
#endif /* _STM32F3X_GPIO_REGISTERS_H_ */
|
||||
#endif /* _STM32_GPIO_REGISTERS_H_ */
|
|
@ -2,4 +2,3 @@ zephyr_include_directories(${ZEPHYR_BASE}/drivers)
|
|||
zephyr_sources(
|
||||
soc.c
|
||||
)
|
||||
zephyr_sources_ifdef(CONFIG_GPIO soc_gpio.c)
|
||||
|
|
|
@ -1,142 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) Linaro Limited.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* Based on reference manual:
|
||||
* RM0368 Reference manual STM32F401xB/C and STM32F401xD/E
|
||||
* advanced ARM ® -based 32-bit MCUs
|
||||
*
|
||||
* Chapter 8: General-purpose I/Os (GPIOs)
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <device.h>
|
||||
#include "soc.h"
|
||||
#include "soc_registers.h"
|
||||
#include <gpio.h>
|
||||
#include <gpio/gpio_stm32.h>
|
||||
|
||||
|
||||
int stm32_gpio_flags_to_conf(int flags, int *pincfg)
|
||||
{
|
||||
int direction = flags & GPIO_DIR_MASK;
|
||||
int pud = flags & GPIO_PUD_MASK;
|
||||
|
||||
if (pincfg == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (direction == GPIO_DIR_OUT) {
|
||||
*pincfg = STM32_MODER_OUTPUT_MODE;
|
||||
} else {
|
||||
/* pull-{up,down} maybe? */
|
||||
*pincfg = STM32_MODER_INPUT_MODE;
|
||||
|
||||
if (pud == GPIO_PUD_PULL_UP) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_UP;
|
||||
} else if (pud == GPIO_PUD_PULL_DOWN) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_DOWN;
|
||||
} else {
|
||||
/* floating */
|
||||
*pincfg = *pincfg | STM32_PUPDR_NO_PULL;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_configure(u32_t *base_addr, int pin, int conf, int altf)
|
||||
{
|
||||
volatile struct stm32f4x_gpio *gpio =
|
||||
(struct stm32f4x_gpio *)(base_addr);
|
||||
unsigned int mode, otype, ospeed, pupd;
|
||||
unsigned int pin_shift = pin << 1;
|
||||
unsigned int afr_bank = pin / 8;
|
||||
unsigned int afr_shift = (pin % 8) << 2;
|
||||
u32_t scratch;
|
||||
|
||||
mode = (conf >> STM32_MODER_SHIFT) & STM32_MODER_MASK;
|
||||
otype = (conf >> STM32_OTYPER_SHIFT) & STM32_OTYPER_MASK;
|
||||
ospeed = (conf >> STM32_OSPEEDR_SHIFT) & STM32_OSPEEDR_MASK;
|
||||
pupd = (conf >> STM32_PUPDR_SHIFT) & STM32_PUPDR_MASK;
|
||||
|
||||
scratch = gpio->mode & ~(STM32_MODER_MASK << pin_shift);
|
||||
gpio->mode = scratch | (mode << pin_shift);
|
||||
|
||||
scratch = gpio->ospeed & ~(STM32_OSPEEDR_MASK << pin_shift);
|
||||
gpio->ospeed = scratch | (ospeed << pin_shift);
|
||||
|
||||
scratch = gpio->otype & ~(STM32_OTYPER_MASK << pin);
|
||||
gpio->otype = scratch | (otype << pin);
|
||||
|
||||
scratch = gpio->pupdr & ~(STM32_PUPDR_MASK << pin_shift);
|
||||
gpio->pupdr = scratch | (pupd << pin_shift);
|
||||
|
||||
scratch = gpio->afr[afr_bank] & ~(STM32_AFR_MASK << afr_shift);
|
||||
gpio->afr[afr_bank] = scratch | (altf << afr_shift);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_set(u32_t *base, int pin, int value)
|
||||
{
|
||||
struct stm32f4x_gpio *gpio = (struct stm32f4x_gpio *)base;
|
||||
|
||||
if (value != 0) {
|
||||
/* atomic set */
|
||||
gpio->bsr = (1 << (pin & 0x0f));
|
||||
} else {
|
||||
/* atomic reset */
|
||||
gpio->bsr = (1 << ((pin & 0x0f) + 0x10));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_get(u32_t *base, int pin)
|
||||
{
|
||||
struct stm32f4x_gpio *gpio = (struct stm32f4x_gpio *)base;
|
||||
|
||||
return (gpio->idr >> pin) & 0x1;
|
||||
}
|
||||
|
||||
int stm32_gpio_enable_int(int port, int pin)
|
||||
{
|
||||
volatile struct stm32f4x_syscfg *syscfg =
|
||||
(struct stm32f4x_syscfg *)SYSCFG_BASE;
|
||||
volatile union syscfg_exticr *exticr;
|
||||
struct device *clk = device_get_binding(STM32_CLOCK_CONTROL_NAME);
|
||||
struct stm32_pclken pclken = {
|
||||
.bus = STM32_CLOCK_BUS_APB2,
|
||||
.enr = LL_APB2_GRP1_PERIPH_SYSCFG
|
||||
};
|
||||
int shift = 0;
|
||||
|
||||
/* Enable SYSCFG clock */
|
||||
clock_control_on(clk, (clock_control_subsys_t *) &pclken);
|
||||
|
||||
if (pin <= 3) {
|
||||
exticr = &syscfg->exticr1;
|
||||
} else if (pin <= 7) {
|
||||
exticr = &syscfg->exticr2;
|
||||
} else if (pin <= 11) {
|
||||
exticr = &syscfg->exticr3;
|
||||
} else if (pin <= 15) {
|
||||
exticr = &syscfg->exticr4;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
shift = 4 * (pin % 4);
|
||||
|
||||
exticr->val &= ~(0xf << shift);
|
||||
exticr->val |= port << shift;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -9,6 +9,6 @@
|
|||
|
||||
/* include register mapping headers */
|
||||
#include "flash_registers.h"
|
||||
#include "gpio_registers.h"
|
||||
#include "syscfg_registers.h"
|
||||
|
||||
#endif /* _STM32F4_SOC_REGISTERS_H_ */
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _STM32F4X_GPIO_REGISTERS_H_
|
||||
#define _STM32F4X_GPIO_REGISTERS_H_
|
||||
#ifndef _STM32_SYSCFG_REGISTERS_H_
|
||||
#define _STM32_SYSCFG_REGISTERS_H_
|
||||
|
||||
/**
|
||||
* @brief Driver for GPIO of STM32F4X family processor.
|
||||
|
@ -13,33 +13,12 @@
|
|||
* Based on reference manual:
|
||||
* RM0368 Reference manual STM32F401xB/C and STM32F401xD/E
|
||||
* advanced ARM(r)-based 32-bit MCUs
|
||||
*
|
||||
* Chapter 8: General-purpose I/Os (GPIOs)
|
||||
*/
|
||||
|
||||
/* 8.4 GPIO registers - each GPIO port controls 16 pins */
|
||||
struct stm32f4x_gpio {
|
||||
u32_t mode;
|
||||
u32_t otype;
|
||||
u32_t ospeed;
|
||||
u32_t pupdr;
|
||||
u32_t idr;
|
||||
u32_t odr;
|
||||
u32_t bsr;
|
||||
u32_t lck;
|
||||
u32_t afr[2];
|
||||
};
|
||||
|
||||
union syscfg_exticr {
|
||||
u32_t val;
|
||||
struct {
|
||||
u16_t rsvd__16_31;
|
||||
u16_t exti;
|
||||
} bit;
|
||||
};
|
||||
#include "../common/soc_syscfg_common.h"
|
||||
|
||||
/* 7.2 SYSCFG registers */
|
||||
struct stm32f4x_syscfg {
|
||||
struct stm32_syscfg {
|
||||
u32_t memrmp;
|
||||
u32_t pmc;
|
||||
union syscfg_exticr exticr1;
|
||||
|
@ -49,4 +28,4 @@ struct stm32f4x_syscfg {
|
|||
u32_t cmpcr;
|
||||
};
|
||||
|
||||
#endif /* _STM32F4X_GPIO_REGISTERS_H_ */
|
||||
#endif /* _STM32_SYSCFG_REGISTERS_H_ */
|
|
@ -2,4 +2,3 @@ zephyr_include_directories(${ZEPHYR_BASE}/drivers)
|
|||
zephyr_sources(
|
||||
soc.c
|
||||
)
|
||||
zephyr_sources_ifdef(CONFIG_GPIO soc_gpio.c)
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Yurii Hamann
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _STM32F7X_GPIO_REGISTERS_H_
|
||||
#define _STM32F7X_GPIO_REGISTERS_H_
|
||||
|
||||
/**
|
||||
* @brief Driver for GPIO of STM32F7X family processor.
|
||||
*
|
||||
* Based on reference manual:
|
||||
* RM0385 Reference manual STM32F75xxx and STM32F74xxx
|
||||
* advanced ARM(r)-based 32-bit MCUs
|
||||
*
|
||||
* Chapter 6: General-purpose I/Os (GPIOs)
|
||||
*/
|
||||
|
||||
/* 6.4 GPIO registers - each GPIO port controls 16 pins */
|
||||
struct stm32f7x_gpio {
|
||||
u32_t moder;
|
||||
u32_t otyper;
|
||||
u32_t ospeedr;
|
||||
u32_t pupdr;
|
||||
u32_t idr;
|
||||
u32_t odr;
|
||||
u32_t bsrr;
|
||||
u32_t lckr;
|
||||
u32_t afr[2];
|
||||
u32_t brr;
|
||||
};
|
||||
|
||||
union syscfg_exticr {
|
||||
u32_t val;
|
||||
struct {
|
||||
u16_t rsvd__16_31;
|
||||
u16_t exti;
|
||||
} bit;
|
||||
};
|
||||
|
||||
/* 7.2 SYSCFG registers */
|
||||
struct stm32f7x_syscfg {
|
||||
u32_t memrmp;
|
||||
u32_t pmc;
|
||||
union syscfg_exticr exticr1;
|
||||
union syscfg_exticr exticr2;
|
||||
union syscfg_exticr exticr3;
|
||||
union syscfg_exticr exticr4;
|
||||
u32_t cmpcr;
|
||||
};
|
||||
|
||||
#endif /* _STM32F7X_GPIO_REGISTERS_H_ */
|
|
@ -1,142 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Yurii Hamann
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* Based on reference manual:
|
||||
* RM0385 Reference manual STM32F75xxx and STM32F74xxx
|
||||
* advanced ARM ® -based 32-bit MCUs
|
||||
*
|
||||
* Chapter 6: General-purpose I/Os (GPIOs)
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <device.h>
|
||||
#include "soc.h"
|
||||
#include "soc_registers.h"
|
||||
#include <gpio.h>
|
||||
#include <gpio/gpio_stm32.h>
|
||||
|
||||
|
||||
int stm32_gpio_flags_to_conf(int flags, int *pincfg)
|
||||
{
|
||||
int direction = flags & GPIO_DIR_MASK;
|
||||
int pud = flags & GPIO_PUD_MASK;
|
||||
|
||||
if (pincfg == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (direction == GPIO_DIR_OUT) {
|
||||
*pincfg = STM32_MODER_OUTPUT_MODE;
|
||||
} else {
|
||||
/* pull-{up,down} maybe? */
|
||||
*pincfg = STM32_MODER_INPUT_MODE;
|
||||
|
||||
if (pud == GPIO_PUD_PULL_UP) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_UP;
|
||||
} else if (pud == GPIO_PUD_PULL_DOWN) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_DOWN;
|
||||
} else {
|
||||
/* floating */
|
||||
*pincfg = *pincfg | STM32_PUPDR_NO_PULL;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_configure(u32_t *base_addr, int pin, int conf, int altf)
|
||||
{
|
||||
volatile struct stm32f7x_gpio *gpio =
|
||||
(struct stm32f7x_gpio *)(base_addr);
|
||||
unsigned int mode, otype, ospeed, pupd;
|
||||
unsigned int pin_shift = pin << 1;
|
||||
unsigned int afr_bank = pin / 8;
|
||||
unsigned int afr_shift = (pin % 8) << 2;
|
||||
u32_t scratch;
|
||||
|
||||
mode = (conf >> STM32_MODER_SHIFT) & STM32_MODER_MASK;
|
||||
otype = (conf >> STM32_OTYPER_SHIFT) & STM32_OTYPER_MASK;
|
||||
ospeed = (conf >> STM32_OSPEEDR_SHIFT) & STM32_OSPEEDR_MASK;
|
||||
pupd = (conf >> STM32_PUPDR_SHIFT) & STM32_PUPDR_MASK;
|
||||
|
||||
scratch = gpio->moder & ~(STM32_MODER_MASK << pin_shift);
|
||||
gpio->moder = scratch | (mode << pin_shift);
|
||||
|
||||
scratch = gpio->ospeedr & ~(STM32_OSPEEDR_MASK << pin_shift);
|
||||
gpio->ospeedr = scratch | (ospeed << pin_shift);
|
||||
|
||||
scratch = gpio->otyper & ~(STM32_OTYPER_MASK << pin);
|
||||
gpio->otyper = scratch | (otype << pin);
|
||||
|
||||
scratch = gpio->pupdr & ~(STM32_PUPDR_MASK << pin_shift);
|
||||
gpio->pupdr = scratch | (pupd << pin_shift);
|
||||
|
||||
scratch = gpio->afr[afr_bank] & ~(STM32_AFR_MASK << afr_shift);
|
||||
gpio->afr[afr_bank] = scratch | (altf << afr_shift);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_set(u32_t *base, int pin, int value)
|
||||
{
|
||||
struct stm32f7x_gpio *gpio = (struct stm32f7x_gpio *)base;
|
||||
|
||||
if (value != 0) {
|
||||
/* atomic set */
|
||||
gpio->bsrr = (1 << (pin & 0x0f));
|
||||
} else {
|
||||
/* atomic reset */
|
||||
gpio->bsrr = (1 << ((pin & 0x0f) + 0x10));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_get(u32_t *base, int pin)
|
||||
{
|
||||
struct stm32f7x_gpio *gpio = (struct stm32f7x_gpio *)base;
|
||||
|
||||
return (gpio->idr >> pin) & 0x1;
|
||||
}
|
||||
|
||||
int stm32_gpio_enable_int(int port, int pin)
|
||||
{
|
||||
volatile struct stm32f7x_syscfg *syscfg =
|
||||
(struct stm32f7x_syscfg *)SYSCFG_BASE;
|
||||
volatile union syscfg_exticr *exticr;
|
||||
struct device *clk = device_get_binding(STM32_CLOCK_CONTROL_NAME);
|
||||
struct stm32_pclken pclken = {
|
||||
.bus = STM32_CLOCK_BUS_APB2,
|
||||
.enr = LL_APB2_GRP1_PERIPH_SYSCFG
|
||||
};
|
||||
int shift = 0;
|
||||
|
||||
/* Enable SYSCFG clock */
|
||||
clock_control_on(clk, (clock_control_subsys_t *) &pclken);
|
||||
|
||||
if (pin <= 3) {
|
||||
exticr = &syscfg->exticr1;
|
||||
} else if (pin <= 7) {
|
||||
exticr = &syscfg->exticr2;
|
||||
} else if (pin <= 11) {
|
||||
exticr = &syscfg->exticr3;
|
||||
} else if (pin <= 15) {
|
||||
exticr = &syscfg->exticr4;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
shift = 4 * (pin % 4);
|
||||
|
||||
exticr->val &= ~(0xf << shift);
|
||||
exticr->val |= port << shift;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -9,6 +9,6 @@
|
|||
|
||||
/* include register mapping headers */
|
||||
#include "flash_registers.h"
|
||||
#include "gpio_registers.h"
|
||||
#include "syscfg_registers.h"
|
||||
|
||||
#endif /* _STM32F7_SOC_REGISTERS_H_ */
|
||||
|
|
31
soc/arm/st_stm32/stm32f7/syscfg_registers.h
Normal file
31
soc/arm/st_stm32/stm32f7/syscfg_registers.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Yurii Hamann
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _STM32_SYSCFG_REGISTERS_H_
|
||||
#define _STM32_SYSCFG_REGISTERS_H_
|
||||
|
||||
/**
|
||||
* @brief Driver for GPIO of STM32F7X family processor.
|
||||
*
|
||||
* Based on reference manual:
|
||||
* RM0385 Reference manual STM32F75xxx and STM32F74xxx
|
||||
* advanced ARM(r)-based 32-bit MCUs
|
||||
*/
|
||||
|
||||
#include "../common/soc_syscfg_common.h"
|
||||
|
||||
/* 7.2 SYSCFG registers */
|
||||
struct stm32_syscfg {
|
||||
u32_t memrmp;
|
||||
u32_t pmc;
|
||||
union syscfg_exticr exticr1;
|
||||
union syscfg_exticr exticr2;
|
||||
union syscfg_exticr exticr3;
|
||||
union syscfg_exticr exticr4;
|
||||
u32_t cmpcr;
|
||||
};
|
||||
|
||||
#endif /* _STM32_SYSCFG_REGISTERS_H_ */
|
|
@ -2,4 +2,3 @@ zephyr_include_directories(${ZEPHYR_BASE}/drivers)
|
|||
zephyr_sources(
|
||||
soc.c
|
||||
)
|
||||
zephyr_sources_ifdef(CONFIG_GPIO soc_gpio.c)
|
||||
|
|
|
@ -1,142 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018 Endre Karlson <endre.karlson@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* Based on reference manual:
|
||||
* STM32F030x4/x6/x8/xC,
|
||||
* STM32F070x6/xB advanced ARM ® -based MCUs
|
||||
*
|
||||
* Chapter 9: General-purpose I/Os (GPIO)
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <device.h>
|
||||
#include "soc.h"
|
||||
#include "soc_registers.h"
|
||||
#include <gpio.h>
|
||||
#include <gpio/gpio_stm32.h>
|
||||
|
||||
int stm32_gpio_flags_to_conf(int flags, int *pincfg)
|
||||
{
|
||||
int direction = flags & GPIO_DIR_MASK;
|
||||
int pud = flags & GPIO_PUD_MASK;
|
||||
|
||||
if (pincfg == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (direction == GPIO_DIR_OUT) {
|
||||
*pincfg = STM32_MODER_OUTPUT_MODE;
|
||||
} else {
|
||||
/* pull-{up,down} maybe? */
|
||||
*pincfg = STM32_MODER_INPUT_MODE;
|
||||
if (pud == GPIO_PUD_PULL_UP) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_UP;
|
||||
} else if (pud == GPIO_PUD_PULL_DOWN) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_DOWN;
|
||||
} else {
|
||||
/* floating */
|
||||
*pincfg = *pincfg | STM32_PUPDR_NO_PULL;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_configure(u32_t *base_addr, int pin, int conf, int altf)
|
||||
{
|
||||
volatile struct stm32l0x_gpio *gpio =
|
||||
(struct stm32l0x_gpio *)(base_addr);
|
||||
unsigned int mode, otype, ospeed, pupd;
|
||||
unsigned int pin_shift = pin << 1;
|
||||
unsigned int afr_bank = pin / 8;
|
||||
unsigned int afr_shift = (pin % 8) << 2;
|
||||
u32_t scratch;
|
||||
|
||||
mode = (conf >> STM32_MODER_SHIFT) & STM32_MODER_MASK;
|
||||
otype = (conf >> STM32_OTYPER_SHIFT) & STM32_OTYPER_MASK;
|
||||
ospeed = (conf >> STM32_OSPEEDR_SHIFT) & STM32_OSPEEDR_MASK;
|
||||
pupd = (conf >> STM32_PUPDR_SHIFT) & STM32_PUPDR_MASK;
|
||||
|
||||
scratch = gpio->moder & ~(STM32_MODER_MASK << pin_shift);
|
||||
gpio->moder = scratch | (mode << pin_shift);
|
||||
|
||||
scratch = gpio->ospeedr & ~(STM32_OSPEEDR_MASK << pin_shift);
|
||||
gpio->ospeedr = scratch | (ospeed << pin_shift);
|
||||
|
||||
scratch = gpio->otyper & ~(STM32_OTYPER_MASK << pin);
|
||||
gpio->otyper = scratch | (otype << pin);
|
||||
|
||||
scratch = gpio->pupdr & ~(STM32_PUPDR_MASK << pin_shift);
|
||||
gpio->pupdr = scratch | (pupd << pin_shift);
|
||||
|
||||
scratch = gpio->afr[afr_bank] & ~(STM32_AFR_MASK << afr_shift);
|
||||
gpio->afr[afr_bank] = scratch | (altf << afr_shift);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_set(u32_t *base, int pin, int value)
|
||||
{
|
||||
struct stm32l0x_gpio *gpio = (struct stm32l0x_gpio *)base;
|
||||
|
||||
int pval = 1 << (pin & 0xf);
|
||||
|
||||
if (value != 0) {
|
||||
gpio->odr |= pval;
|
||||
} else {
|
||||
gpio->odr &= ~pval;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_get(u32_t *base, int pin)
|
||||
{
|
||||
struct stm32l0x_gpio *gpio = (struct stm32l0x_gpio *)base;
|
||||
|
||||
return (gpio->idr >> pin) & 0x1;
|
||||
}
|
||||
|
||||
int stm32_gpio_enable_int(int port, int pin)
|
||||
{
|
||||
volatile struct stm32l0x_syscfg *syscfg =
|
||||
(struct stm32l0x_syscfg *)SYSCFG_BASE;
|
||||
volatile union syscfg_exticr *exticr;
|
||||
|
||||
int shift = 0;
|
||||
|
||||
if (pin <= 3) {
|
||||
exticr = &syscfg->exticr1;
|
||||
} else if (pin <= 7) {
|
||||
exticr = &syscfg->exticr2;
|
||||
} else if (pin <= 11) {
|
||||
exticr = &syscfg->exticr3;
|
||||
} else if (pin <= 15) {
|
||||
exticr = &syscfg->exticr4;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Ports F and G are not present on some STM32L0 parts, so
|
||||
* for these parts port H external interrupt should be enabled
|
||||
* by writing value 0x5 instead of 0x7.
|
||||
*/
|
||||
if (port == STM32_PORTH) {
|
||||
port = LL_SYSCFG_EXTI_PORTH;
|
||||
}
|
||||
|
||||
shift = 4 * (pin % 4);
|
||||
|
||||
exticr->val &= ~(0xf << shift);
|
||||
exticr->val |= port << shift;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -8,6 +8,7 @@
|
|||
#define _STM32L0X_SOC_REGISTERS_H_
|
||||
|
||||
/* include register mapping headers */
|
||||
#include "gpio_registers.h"
|
||||
#include "syscfg_registers.h"
|
||||
|
||||
|
||||
#endif /* _STM32L0X_SOC_REGISTERS_H_ */
|
||||
|
|
|
@ -4,42 +4,21 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _STM32L0X_GPIO_REGISTERS_H_
|
||||
#define _STM32L0X_GPIO_REGISTERS_H_
|
||||
#ifndef _STM32_SYSCFG_REGISTERS_H_
|
||||
#define _STM32_SYSCFG_REGISTERS_H_
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* Based on reference manual:
|
||||
* STM32L0X advanced ARM ® -based 32-bit MCUs
|
||||
|
||||
*
|
||||
* Chapter 9: General-purpose I/Os (GPIO)
|
||||
* Chapter 10: System configuration controller (SYSCFG)
|
||||
*/
|
||||
|
||||
struct stm32l0x_gpio {
|
||||
u32_t moder;
|
||||
u32_t otyper;
|
||||
u32_t ospeedr;
|
||||
u32_t pupdr;
|
||||
u32_t idr;
|
||||
u32_t odr;
|
||||
u32_t bsrr;
|
||||
u32_t lckr;
|
||||
u32_t afr[2];
|
||||
u32_t brr;
|
||||
};
|
||||
#include "../common/soc_syscfg_common.h"
|
||||
|
||||
union syscfg_exticr {
|
||||
u32_t val;
|
||||
struct {
|
||||
u16_t exti;
|
||||
u16_t rsvd__16_31;
|
||||
} bit;
|
||||
};
|
||||
|
||||
struct stm32l0x_syscfg {
|
||||
struct stm32_syscfg {
|
||||
u32_t cfgr1;
|
||||
u32_t cfgr2;
|
||||
union syscfg_exticr exticr1;
|
||||
|
@ -51,4 +30,4 @@ struct stm32l0x_syscfg {
|
|||
u32_t cfgr3;
|
||||
};
|
||||
|
||||
#endif /* _STM32L0X_GPIO_REGISTERS_H_ */
|
||||
#endif /* _STM32_SYSCFG_REGISTERS_H_ */
|
|
@ -2,4 +2,3 @@ zephyr_include_directories(${ZEPHYR_BASE}/drivers)
|
|||
zephyr_sources(
|
||||
soc.c
|
||||
)
|
||||
zephyr_sources_ifdef(CONFIG_GPIO soc_gpio.c)
|
||||
|
|
|
@ -1,163 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Open-RnD Sp. z o.o.
|
||||
* Copyright (c) 2016 BayLibre, SAS
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* Based on reference manual:
|
||||
* STM32L4x1, STM32L4x2, STM32L431xx STM32L443xx STM32L433xx, STM32L4x5,
|
||||
* STM32l4x6 advanced ARM ® -based 32-bit MCUs
|
||||
*
|
||||
* General-purpose I/Os (GPIOs)
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <device.h>
|
||||
#include "soc.h"
|
||||
#include "soc_registers.h"
|
||||
#include <gpio.h>
|
||||
#include <gpio/gpio_stm32.h>
|
||||
|
||||
|
||||
enum {
|
||||
STM32L4X_PIN3 = 3,
|
||||
STM32L4X_PIN7 = 7,
|
||||
STM32L4X_PIN11 = 11,
|
||||
STM32L4X_PIN15 = 15,
|
||||
};
|
||||
|
||||
#define STM32L4X_IDR_PIN_MASK 0x1
|
||||
#define STM32L4X_AFR_MASK 0xf
|
||||
|
||||
/* GPIO registers - each GPIO port controls 16 pins */
|
||||
struct stm32l4x_gpio {
|
||||
u32_t moder;
|
||||
u32_t otyper;
|
||||
u32_t ospeedr;
|
||||
u32_t pupdr;
|
||||
u32_t idr;
|
||||
u32_t odr;
|
||||
u32_t bsrr;
|
||||
u32_t lckr;
|
||||
u32_t afr[2];
|
||||
u32_t brr;
|
||||
u32_t ascr; /* Only present on STM32L4x1, STM32L4x5, STM32L4x6 */
|
||||
};
|
||||
|
||||
int stm32_gpio_flags_to_conf(int flags, int *pincfg)
|
||||
{
|
||||
int direction = flags & GPIO_DIR_MASK;
|
||||
int pud = flags & GPIO_PUD_MASK;
|
||||
|
||||
if (pincfg == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (direction == GPIO_DIR_OUT) {
|
||||
*pincfg = STM32_MODER_OUTPUT_MODE;
|
||||
} else {
|
||||
/* pull-{up,down} maybe? */
|
||||
*pincfg = STM32_MODER_INPUT_MODE;
|
||||
|
||||
if (pud == GPIO_PUD_PULL_UP) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_UP;
|
||||
} else if (pud == GPIO_PUD_PULL_DOWN) {
|
||||
*pincfg = *pincfg | STM32_PUPDR_PULL_DOWN;
|
||||
} else {
|
||||
/* floating */
|
||||
*pincfg = *pincfg | STM32_PUPDR_NO_PULL;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_configure(u32_t *base_addr, int pin, int pinconf, int afnum)
|
||||
{
|
||||
volatile struct stm32l4x_gpio *gpio =
|
||||
(struct stm32l4x_gpio *)(base_addr);
|
||||
unsigned int mode, otype, ospeed, pupd;
|
||||
unsigned int pin_shift = pin << 1;
|
||||
unsigned int afr_bank = pin / 8;
|
||||
unsigned int afr_shift = (pin % 8) << 2;
|
||||
u32_t scratch;
|
||||
|
||||
mode = (pinconf >> STM32_MODER_SHIFT) & STM32_MODER_MASK;
|
||||
otype = (pinconf >> STM32_OTYPER_SHIFT) & STM32_OTYPER_MASK;
|
||||
ospeed = (pinconf >> STM32_OSPEEDR_SHIFT) & STM32_OSPEEDR_MASK;
|
||||
pupd = (pinconf >> STM32_PUPDR_SHIFT) & STM32_PUPDR_MASK;
|
||||
|
||||
scratch = gpio->moder & ~(STM32_MODER_MASK << pin_shift);
|
||||
gpio->moder = scratch | (mode << pin_shift);
|
||||
|
||||
scratch = gpio->ospeedr & ~(STM32_OSPEEDR_MASK << pin_shift);
|
||||
gpio->ospeedr = scratch | (ospeed << pin_shift);
|
||||
|
||||
scratch = gpio->otyper & ~(STM32_OTYPER_MASK << pin);
|
||||
gpio->otyper = scratch | (otype << pin);
|
||||
|
||||
scratch = gpio->pupdr & ~(STM32_PUPDR_MASK << pin_shift);
|
||||
gpio->pupdr = scratch | (pupd << pin_shift);
|
||||
|
||||
scratch = gpio->afr[afr_bank] & ~(STM32_AFR_MASK << afr_shift);
|
||||
gpio->afr[afr_bank] = scratch | (afnum << afr_shift);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_set(u32_t *base, int pin, int value)
|
||||
{
|
||||
struct stm32l4x_gpio *gpio = (struct stm32l4x_gpio *)base;
|
||||
int pval = 1 << (pin & 0xf);
|
||||
|
||||
if (value != 0) {
|
||||
gpio->odr |= pval;
|
||||
} else {
|
||||
gpio->odr &= ~pval;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_gpio_get(u32_t *base, int pin)
|
||||
{
|
||||
struct stm32l4x_gpio *gpio = (struct stm32l4x_gpio *)base;
|
||||
|
||||
return (gpio->idr >> pin) & STM32L4X_IDR_PIN_MASK;
|
||||
}
|
||||
|
||||
int stm32_gpio_enable_int(int port, int pin)
|
||||
{
|
||||
struct stm32l4x_syscfg *syscfg = (struct stm32l4x_syscfg *)SYSCFG_BASE;
|
||||
struct device *clk = device_get_binding(STM32_CLOCK_CONTROL_NAME);
|
||||
u32_t *reg;
|
||||
|
||||
/* Enable SYSCFG clock */
|
||||
struct stm32_pclken pclken = {
|
||||
.bus = STM32_CLOCK_BUS_APB2,
|
||||
.enr = LL_APB2_GRP1_PERIPH_SYSCFG
|
||||
};
|
||||
clock_control_on(clk, (clock_control_subsys_t *) &pclken);
|
||||
|
||||
if (pin <= STM32L4X_PIN3) {
|
||||
reg = &syscfg->exticr1;
|
||||
} else if (pin <= STM32L4X_PIN7) {
|
||||
reg = &syscfg->exticr2;
|
||||
} else if (pin <= STM32L4X_PIN11) {
|
||||
reg = &syscfg->exticr3;
|
||||
} else if (pin <= STM32L4X_PIN15) {
|
||||
reg = &syscfg->exticr4;
|
||||
} else {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
*reg &= ~(STM32L4X_SYSCFG_EXTICR_PIN_MASK << ((pin % 4) * 4));
|
||||
*reg |= port << ((pin % 4) * 4);
|
||||
|
||||
return 0; /* Nothing to do here for STM32L4s */
|
||||
}
|
|
@ -4,23 +4,23 @@
|
|||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef _STM32L4X_SYSCFG_REGISTERS_H_
|
||||
#define _STM32L4X_SYSCFG_REGISTERS_H_
|
||||
#ifndef _STM32_SYSCFG_REGISTERS_H_
|
||||
#define _STM32_SYSCFG_REGISTERS_H_
|
||||
|
||||
#define STM32L4X_SYSCFG_EXTICR_PIN_MASK 7
|
||||
#include "../common/soc_syscfg_common.h"
|
||||
|
||||
/* SYSCFG registers */
|
||||
struct stm32l4x_syscfg {
|
||||
struct stm32_syscfg {
|
||||
u32_t memrmp;
|
||||
u32_t cfgr1;
|
||||
u32_t exticr1;
|
||||
u32_t exticr2;
|
||||
u32_t exticr3;
|
||||
u32_t exticr4;
|
||||
union syscfg_exticr exticr1;
|
||||
union syscfg_exticr exticr2;
|
||||
union syscfg_exticr exticr3;
|
||||
union syscfg_exticr exticr4;
|
||||
u32_t scsr;
|
||||
u32_t cfgr2;
|
||||
u32_t swpr;
|
||||
u32_t skr;
|
||||
};
|
||||
|
||||
#endif /* _STM32L4X_SYSCFG_REGISTERS_H_ */
|
||||
#endif /* _STM32_SYSCFG_REGISTERS_H_ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue