[NUCLEO_F103RB] Cleanup code

Add STM_PIN and STM_PORT macros for GPIOs.
pull/150/head
bcostm 2014-01-22 07:48:10 +01:00
parent 0af4419c74
commit e3e5180256
4 changed files with 15 additions and 14 deletions

View File

@ -42,15 +42,17 @@ extern "C" {
#define STM_PIN_MODE(X) ((X) >> 8)
#define STM_PIN_AFNUM(X) ((X) & 0xFF)
// High nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, 6=G, 7=H)
// Low nibble = pin number
#define STM_PORT(X) (((uint32_t)(X) >> 4) & 0xF)
#define STM_PIN(X) ((uint32_t)(X) & 0xF)
typedef enum {
PIN_INPUT,
PIN_OUTPUT
} PinDirection;
typedef enum {
// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F)
// low nibble = pin number
PA_0 = 0x00,
PA_1 = 0x01,
PA_2 = 0x02,

View File

@ -41,9 +41,9 @@ uint32_t gpio_set(PinName pin) {
void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
if (pin == NC) return;
uint32_t port_index = STM_PORT(pin);
// Get GPIO structure base address
uint32_t pin_number = (uint32_t)pin;
uint32_t port_index = (pin_number >> 4);
GPIO_TypeDef *gpio = (GPIO_TypeDef *)(GPIOA_BASE + (port_index << 10));
// Fill GPIO object structure for future use

View File

@ -87,9 +87,8 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
if (pin == NC) return -1;
uint32_t pin_number = (uint32_t)pin;
uint32_t pin_index = (pin_number & 0xF);
uint32_t port_index = (pin_number >> 4);
uint32_t port_index = STM_PORT(pin);
uint32_t pin_index = STM_PIN(pin);
// Select irq number and vector
switch (pin_index) {

View File

@ -53,10 +53,10 @@ void pin_function(PinName pin, int data) {
uint32_t mode = STM_PIN_MODE(data);
uint32_t afnum = STM_PIN_AFNUM(data);
uint32_t port_index = STM_PORT(pin);
uint32_t pin_index = STM_PIN(pin);
// Get GPIO structure base address
uint32_t pin_number = (uint32_t)pin;
uint32_t pin_index = (pin_number & 0xF);
uint32_t port_index = (pin_number >> 4);
GPIO_TypeDef *gpio = ((GPIO_TypeDef *)(GPIOA_BASE + (port_index << 10)));
// Enable GPIO and AFIO clocks
@ -92,11 +92,11 @@ void pin_mode(PinName pin, PinMode mode) {
if (pin == NC) return;
GPIO_InitTypeDef GPIO_InitStructure;
uint32_t port_index = STM_PORT(pin);
uint32_t pin_index = STM_PIN(pin);
// Get GPIO structure base address
uint32_t pin_number = (uint32_t)pin;
uint32_t pin_index = (pin_number & 0xF);
uint32_t port_index = (pin_number >> 4);
GPIO_TypeDef *gpio = ((GPIO_TypeDef *)(GPIOA_BASE + (port_index << 10)));
// Enable GPIO clock