Merge alternate function code from multiple chip series
This commit is contained in:
parent
c336f71d58
commit
f5a84196e7
|
@ -28,7 +28,7 @@ extern const stm32_port_pin_type port_pin_list[NUM_PINS];
|
|||
void stm32_gpio_clock(GPIO_TypeDef *port);
|
||||
|
||||
inline void digitalWrite(uint8_t pin, uint8_t value) {
|
||||
if (pin < 0 || pin >= sizeof(port_pin_list) / sizeof(port_pin_list[0])) {
|
||||
if (pin >= sizeof(port_pin_list) / sizeof(port_pin_list[0])) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -40,7 +40,7 @@ inline void digitalWrite(uint8_t pin, uint8_t value) {
|
|||
|
||||
|
||||
inline int digitalRead(uint8_t pin) {
|
||||
if (pin < 0 || pin >= sizeof(port_pin_list) / sizeof(port_pin_list[0])) {
|
||||
if (pin >= sizeof(port_pin_list) / sizeof(port_pin_list[0])) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
#include "stm32_gpio_af.h"
|
||||
|
||||
#include CHIP_PERIPHERAL_INCLUDE
|
||||
|
||||
GPIO_TypeDef *stm32_af_default(alternate_pin_type list[], int size, const void *instance, uint32_t *pin) {
|
||||
for(int i=0; i<size; i++) {
|
||||
if (instance == list[i].instance) {
|
||||
*pin = list[i].pin;
|
||||
return list[i].port;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void stm32_af_uart_init(const USART_TypeDef *instance, GPIO_TypeDef *rxPort, uint32_t rxPin, GPIO_TypeDef *txPort, uint32_t txPin) {
|
||||
|
||||
stm32_af_init(alternate_usart_rx, sizeof(alternate_usart_rx) / sizeof(alternate_usart_rx[0]), instance, rxPort, rxPin, GPIO_MODE_AF_PP, GPIO_PULLUP);
|
||||
stm32_af_init(alternate_usart_tx, sizeof(alternate_usart_tx) / sizeof(alternate_usart_tx[0]), instance, txPort, txPin, GPIO_MODE_AF_PP, GPIO_PULLUP);
|
||||
|
||||
}
|
|
@ -8,6 +8,29 @@
|
|||
|
||||
#include "stm32_gpio.h"
|
||||
|
||||
#ifdef STM32F1
|
||||
|
||||
typedef void (*alternate_callback)();
|
||||
|
||||
typedef struct {
|
||||
void *instance;
|
||||
GPIO_TypeDef *port;
|
||||
uint32_t pin;
|
||||
alternate_callback alternate;
|
||||
} alternate_pin_type;
|
||||
|
||||
#else
|
||||
|
||||
typedef struct {
|
||||
void *instance;
|
||||
GPIO_TypeDef *port;
|
||||
uint32_t pin;
|
||||
uint8_t alternate;
|
||||
} alternate_pin_type;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"{
|
||||
#endif
|
||||
|
@ -16,6 +39,22 @@ void stm32_af_uart_init(const USART_TypeDef *instance,
|
|||
GPIO_TypeDef *rxPort, uint32_t rxPin,
|
||||
GPIO_TypeDef *txPort, uint32_t txPin);
|
||||
|
||||
void stm32_af_spi_init(const SPI_TypeDef *instance,
|
||||
GPIO_TypeDef *mosiPort, uint32_t mosiPin,
|
||||
GPIO_TypeDef *misoPort, uint32_t misoPin,
|
||||
GPIO_TypeDef *sckPort, uint32_t sckPin);
|
||||
|
||||
/**
|
||||
* Internal: set the AF function for the selected peripheral on the selected pin
|
||||
*/
|
||||
void stm32_af_init(const alternate_pin_type list[], int size, const void *instance, GPIO_TypeDef *port, uint32_t pin, uint32_t mode, uint32_t pull);
|
||||
|
||||
/**
|
||||
* Internal: get the default pin for the given peripheral
|
||||
*/
|
||||
GPIO_TypeDef *stm32_af_default(alternate_pin_type list[], int size, const void *instance, uint32_t *pin);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -1,16 +1,7 @@
|
|||
#if defined(STM32F0) || defined(STM32F2) || defined(STM32F3) || defined(STM32F4) || \
|
||||
defined(STM32F7) || defined(STM32L0) || defined(STM32L1) || defined(STM32L4)
|
||||
|
||||
#include "stm32_gpio.h"
|
||||
|
||||
typedef struct {
|
||||
void *instance;
|
||||
GPIO_TypeDef *port;
|
||||
uint32_t pin;
|
||||
uint8_t alternate;
|
||||
} alternate_pin_type;
|
||||
|
||||
#include CHIP_PERIPHERAL_INCLUDE
|
||||
#include "stm32_gpio_af.h"
|
||||
|
||||
int8_t stm32_af_get(const alternate_pin_type list[], int size, const void *instance, const GPIO_TypeDef *port, const uint32_t pin) {
|
||||
for(int i=0; i<size; i++) {
|
||||
|
@ -24,16 +15,6 @@ int8_t stm32_af_get(const alternate_pin_type list[], int size, const void *insta
|
|||
return 0;
|
||||
}
|
||||
|
||||
GPIO_TypeDef *stm32_af_default(const alternate_pin_type list[], int size, const void *instance, uint32_t *pin) {
|
||||
for(int i=0; i<size; i++) {
|
||||
if (instance == list[i].instance) {
|
||||
*pin = list[i].pin;
|
||||
return list[i].port;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void stm32_af_init(const alternate_pin_type list[], int size, const void *instance, GPIO_TypeDef *port, uint32_t pin, uint32_t mode, uint32_t pull) {
|
||||
if (port == NULL) {
|
||||
port = stm32_af_default(list, size, instance, &pin);
|
||||
|
@ -49,11 +30,4 @@ void stm32_af_init(const alternate_pin_type list[], int size, const void *instan
|
|||
HAL_GPIO_Init(port, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
void stm32_af_uart_init(const USART_TypeDef *instance, GPIO_TypeDef *rxPort, uint32_t rxPin, GPIO_TypeDef *txPort, uint32_t txPin) {
|
||||
|
||||
stm32_af_init(alternate_usart_rx, sizeof(alternate_usart_rx) / sizeof(alternate_usart_rx[0]), instance, rxPort, rxPin, GPIO_MODE_AF_PP, GPIO_PULLUP);
|
||||
stm32_af_init(alternate_usart_tx, sizeof(alternate_usart_tx) / sizeof(alternate_usart_tx[0]), instance, txPort, txPin, GPIO_MODE_AF_PP, GPIO_PULLUP);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,19 +1,8 @@
|
|||
#if defined(STM32F1)
|
||||
|
||||
#include "stm32_gpio.h"
|
||||
#include "stm32_gpio_af.h"
|
||||
|
||||
typedef void (*alternate_callback)();
|
||||
|
||||
typedef struct {
|
||||
void *instance;
|
||||
GPIO_TypeDef *port;
|
||||
uint32_t pin;
|
||||
alternate_callback alternate;
|
||||
} alternate_pin_type;
|
||||
|
||||
#include CHIP_PERIPHERAL_INCLUDE
|
||||
|
||||
alternate_callback stm32_af_get(alternate_pin_type list[], int size, const void *instance, const GPIO_TypeDef *port, const uint32_t pin) {
|
||||
alternate_callback stm32_af_get(const alternate_pin_type list[], int size, const void *instance, const GPIO_TypeDef *port, const uint32_t pin) {
|
||||
for(int i=0; i<size; i++) {
|
||||
if (instance == list[i].instance
|
||||
&& port == list[i].port
|
||||
|
@ -25,17 +14,7 @@ alternate_callback stm32_af_get(alternate_pin_type list[], int size, const void
|
|||
return 0;
|
||||
}
|
||||
|
||||
GPIO_TypeDef *stm32_af_default(alternate_pin_type list[], int size, const void *instance, uint32_t *pin) {
|
||||
for(int i=0; i<size; i++) {
|
||||
if (instance == list[i].instance) {
|
||||
*pin = list[i].pin;
|
||||
return list[i].port;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void stm32_af_init(alternate_pin_type list[], int size, void *instance, GPIO_TypeDef *port, uint32_t *pin, uint32_t mode, uint32_t pull) {
|
||||
void stm32_af_init(const alternate_pin_type list[], int size, const void *instance, GPIO_TypeDef *port, uint32_t pin, uint32_t mode, uint32_t pull) {
|
||||
if (port == NULL) {
|
||||
port = stm32_af_default(list, size, instance, &pin);
|
||||
}
|
||||
|
@ -51,12 +30,4 @@ void stm32_af_init(alternate_pin_type list[], int size, void *instance, GPIO_Typ
|
|||
stm32_af_get(list, size, instance, port, pin)();
|
||||
}
|
||||
|
||||
void stm32_af_uart_init(const USART_TypeDef *instance, GPIO_TypeDef *rxPort, uint32_t rxPin, GPIO_TypeDef *txPort, uint32_t txPin) {
|
||||
|
||||
stm32_af_init(alternate_usart_rx, sizeof(alternate_usart_rx) / sizeof(alternate_usart_rx[0]), instance, rxPort, rxPin, GPIO_MODE_AF_PP, GPIO_PULLUP);
|
||||
stm32_af_init(alternate_usart_tx, sizeof(alternate_usart_tx) / sizeof(alternate_usart_tx[0]), instance, txPort, txPin, GPIO_MODE_AF_PP, GPIO_PULLUP);
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue