Skip to content

GPIO enhancement #368

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cores/arduino/pins_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ uint32_t pinNametoDigitalPin(PinName p);
#define digitalPinToPort(p) (get_GPIO_Port(STM_PORT(digitalPinToPinName(p))))
#define digitalPinToBitMask(p) (STM_GPIO_PIN(digitalPinToPinName(p)))

#define analogInPinToBit(p) (STM_PIN(digitalPinToPinName(p)))
#define analogInPinToBit(p) (STM_GPIO_PIN(digitalPinToPinName(p)))
#define portOutputRegister(P) (&(P->ODR))
#define portInputRegister(P) (&(P->IDR))

Expand Down
46 changes: 0 additions & 46 deletions cores/arduino/stm32/PinConfigured.c

This file was deleted.

10 changes: 6 additions & 4 deletions cores/arduino/stm32/PinConfigured.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ extern "C" {

#define PINCONF_VAL(X, Y) ((Y >> PINCONF_SHIFT(X)) & PINCONF_MASK)


bool is_pin_configured(PinName pin, uint32_t* map);
void set_pin_configured(PinName pin, uint32_t* map);
void reset_pin_configured(PinName pin, uint32_t* map);
#define is_pin_configured(pin, map) \
(PINCONF_VAL(pin, map[PINCONF_INDEX(pin)]))
#define set_pin_configured(pin, map) \
(map[PINCONF_INDEX(pin)] = map[PINCONF_INDEX(pin)] | PINCONF_BIT(pin))
#define reset_pin_configured(pin, map) \
(map[PINCONF_INDEX(pin)] = map[PINCONF_INDEX(pin)] & (~PINCONF_BIT(pin)))

#ifdef __cplusplus
}
Expand Down
62 changes: 14 additions & 48 deletions cores/arduino/stm32/PortNames.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,73 +28,40 @@
*******************************************************************************
*/
#include "PortNames.h"
#include "stm32_def.h"

// Return GPIO base address
GPIO_TypeDef *get_GPIO_Port(uint32_t port_idx) {
GPIO_TypeDef* gpioPort = 0;
switch (port_idx) {
case PortA:
gpioPort = (GPIO_TypeDef *)GPIOA_BASE;
break;
case PortB:
gpioPort = (GPIO_TypeDef *)GPIOB_BASE;
break;
GPIO_TypeDef * GPIOPort[MAX_NB_PORT] = {
(GPIO_TypeDef *)GPIOA_BASE,
(GPIO_TypeDef *)GPIOB_BASE
#if defined GPIOC_BASE
case PortC:
gpioPort = (GPIO_TypeDef *)GPIOC_BASE;
break;
,(GPIO_TypeDef *)GPIOC_BASE
#endif
#if defined GPIOD_BASE
case PortD:
gpioPort = (GPIO_TypeDef *)GPIOD_BASE;
break;
,(GPIO_TypeDef *)GPIOD_BASE
#endif
#if defined GPIOE_BASE
case PortE:
gpioPort = (GPIO_TypeDef *)GPIOE_BASE;
break;
,(GPIO_TypeDef *)GPIOE_BASE
#endif
#if defined GPIOF_BASE
case PortF:
gpioPort = (GPIO_TypeDef *)GPIOF_BASE;
break;
,(GPIO_TypeDef *)GPIOF_BASE
#endif
#if defined GPIOG_BASE
case PortG:
gpioPort = (GPIO_TypeDef *)GPIOG_BASE;
break;
,(GPIO_TypeDef *)GPIOG_BASE
#endif
#if defined GPIOH_BASE
case PortH:
gpioPort = (GPIO_TypeDef *)GPIOH_BASE;
break;
,(GPIO_TypeDef *)GPIOH_BASE
#endif
#if defined GPIOI_BASE
case PortI:
gpioPort = (GPIO_TypeDef *)GPIOI_BASE;
break;
,(GPIO_TypeDef *)GPIOI_BASE
#endif
#if defined GPIOJ_BASE
case PortJ:
gpioPort = (GPIO_TypeDef *)GPIOJ_BASE;
break;
,(GPIO_TypeDef *)GPIOJ_BASE
#endif
#if defined GPIOK_BASE
case PortK:
gpioPort = (GPIO_TypeDef *)GPIOK_BASE;
break;
,(GPIO_TypeDef *)GPIOK_BASE
#endif
default:
// wrong port number
//TBD: error management
gpioPort = 0;
break;
}
return gpioPort;
}
};

// Enable GPIO clock and return GPIO base address
/* Enable GPIO clock and return GPIO base address */
GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx) {
GPIO_TypeDef* gpioPort = 0;
switch (port_idx) {
Expand Down Expand Up @@ -173,4 +140,3 @@ GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx) {
}
return gpioPort;
}

6 changes: 5 additions & 1 deletion cores/arduino/stm32/PortNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
extern "C" {
#endif

extern GPIO_TypeDef *GPIOPort[];

typedef enum {
FirstPort = 0x00,
PortA = FirstPort,
Expand Down Expand Up @@ -73,7 +75,9 @@ typedef enum {

#define MAX_NB_PORT (LastPort-FirstPort+1)

GPIO_TypeDef *get_GPIO_Port(uint32_t port_idx);
/* Return GPIO base address */
#define get_GPIO_Port(p) ((p < MAX_NB_PORT) ? GPIOPort[p] : (GPIO_TypeDef *)NULL)
/* Enable GPIO clock and return GPIO base address */
GPIO_TypeDef *set_GPIO_Port_Clock(uint32_t port_idx);

#ifdef __cplusplus
Expand Down
39 changes: 5 additions & 34 deletions cores/arduino/stm32/digital_io.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/**
******************************************************************************
* @file digital_io.c
* @author WI6LABS
* @version V1.0.0
* @date 01-August-2016
* @brief Provide an interface to configure hw ios
*
******************************************************************************
Expand Down Expand Up @@ -36,12 +33,10 @@
******************************************************************************
*/
#include "digital_io.h"
#include "stm32_def.h"
#include "hw_config.h"
#include "PinAF_STM32F1.h"

#ifdef __cplusplus
extern "C" {
extern "C" {
#endif

/**
Expand All @@ -57,7 +52,11 @@ void digital_io_init(PinName pin, uint32_t mode, uint32_t pull)
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_TypeDef *port = set_GPIO_Port_Clock(STM_PORT(pin));
GPIO_InitStructure.Pin = STM_GPIO_PIN(pin);
#ifdef GPIO_SPEED_FREQ_VERY_HIGH
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
#else
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
#endif
GPIO_InitStructure.Mode = mode;
GPIO_InitStructure.Pull = pull;
#ifdef STM32F1xx
Expand All @@ -66,34 +65,6 @@ void digital_io_init(PinName pin, uint32_t mode, uint32_t pull)
HAL_GPIO_Init(port, &GPIO_InitStructure);
}

/**
* @brief This function set a value to an IO
* @param port : one of the gpio port
* @param pin : one of the gpio pin
* @param val : 0 to set to low, any other value to set to high
* @retval None
*/
void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val)
{
if(val) {
HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET);
} else {
HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET);
}
}


/**
* @brief This function set a value to an IO
* @param port : one of the gpio port
* @param pin : one of the gpio pin
* @retval The pin state (LOW or HIGH)
*/
uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin)
{
return (uint32_t)HAL_GPIO_ReadPin(port, pin);
}

#ifdef __cplusplus
}
#endif
Expand Down
88 changes: 77 additions & 11 deletions cores/arduino/stm32/digital_io.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/**
******************************************************************************
* @file digital_io.h
* @author WI6LABS
* @version V1.0.0
* @date 01-August-2016
* @brief Header for digital_io module
******************************************************************************
* @attention
Expand Down Expand Up @@ -40,20 +37,89 @@
#define __DIGITAL_IO_H

/* Includes ------------------------------------------------------------------*/
#include "stm32_def.h"
#include "PeripheralPins.h"
#include "wiring_constants.h"
#include "PinNames.h"
#include "pinmap.h"
#include "stm32yyxx_ll_gpio.h"

#ifdef __cplusplus
extern "C" {
extern "C" {
#endif

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void digital_io_init(PinName pin, uint32_t mode, uint32_t pull);
void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val);
uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin);

/**
* @brief This function set a value to an IO
* @param port : one of the gpio port
* @param pin : one of the gpio pin
* @param val : 0 to set to low, any other value to set to high
* @retval None
*/
static inline void digital_io_write(GPIO_TypeDef *port, uint32_t pin, uint32_t val)
{
if (val) {
LL_GPIO_SetOutputPin(port, pin);
} else {
LL_GPIO_ResetOutputPin(port, pin);
}
}

/**
* @brief This function read the value of an IO
* @param port : one of the gpio port
* @param pin : one of the gpio pin
* @retval The pin state (LOW or HIGH)
*/
static inline uint32_t digital_io_read(GPIO_TypeDef *port, uint32_t pin)
{
return LL_GPIO_IsInputPinSet(port, pin);
}

/**
* @brief This function toggle value of an IO
* @param port : one of the gpio port
* @param pin : one of the gpio pin
* @retval None
*/
static inline void digital_io_toggle(GPIO_TypeDef *port, uint32_t pin)
{
LL_GPIO_TogglePin(port, pin);
}

/**
* @brief This function set a value to an IO
* @param pn : Pin name
* @param val : 0 to set to low, any other value to set to high
* @retval None
*/
static inline void digitalWriteFast(PinName pn, uint32_t ulVal)
{
digital_io_write(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn), ulVal);
}

/**
* @brief This function read the value of an IO
* @param pn : Pin name
* @retval The pin state (LOW or HIGH)
*/
static inline int digitalReadFast(PinName pn)
{
uint8_t level = 0;
level = digital_io_read(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn));
return (level) ? HIGH : LOW;
}

/**
* @brief This function toggle value of an IO
* @param port : one of the gpio port
* @param pin : one of the gpio pin
* @retval None
*/
static inline void digitalToggleFast(PinName pn)
{
digital_io_toggle(get_GPIO_Port(STM_PORT(pn)), STM_LL_GPIO_PIN(pn));
}

#ifdef __cplusplus
}
Expand Down
21 changes: 21 additions & 0 deletions cores/arduino/stm32/pinmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@
//Based on mbed-os/hal/mbed_pinmap_common.c

#include "pinmap.h"
#include "stm32yyxx_ll_gpio.h"

/* Map STM_PIN to LL */
const uint32_t pin_map_ll[16] = {
LL_GPIO_PIN_0,
LL_GPIO_PIN_1,
LL_GPIO_PIN_2,
LL_GPIO_PIN_3,
LL_GPIO_PIN_4,
LL_GPIO_PIN_5,
LL_GPIO_PIN_6,
LL_GPIO_PIN_7,
LL_GPIO_PIN_8,
LL_GPIO_PIN_9,
LL_GPIO_PIN_10,
LL_GPIO_PIN_11,
LL_GPIO_PIN_12,
LL_GPIO_PIN_13,
LL_GPIO_PIN_14,
LL_GPIO_PIN_15
};

void* pinmap_find_peripheral(PinName pin, const PinMap* map) {
while (map->pin != NC) {
Expand Down
Loading