62 lines
2.1 KiB
C++
62 lines
2.1 KiB
C++
/*
|
|
* Copyright (C) OpenTX
|
|
*
|
|
* Based on code named
|
|
* th9x - http://code.google.com/p/th9x
|
|
* er9x - http://code.google.com/p/er9x
|
|
* gruvin9x - http://code.google.com/p/gruvin9x
|
|
*
|
|
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*
|
|
* This program 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.
|
|
*/
|
|
|
|
#include "../../opentx.h"
|
|
|
|
void configure_pins( uint32_t pins, uint16_t config )
|
|
{
|
|
uint32_t address ;
|
|
GPIO_TypeDef *pgpio ;
|
|
uint32_t thispin ;
|
|
uint32_t pos ;
|
|
|
|
address = ( config & PIN_PORT_MASK ) >> 8 ;
|
|
address *= (GPIOB_BASE-GPIOA_BASE) ;
|
|
address += GPIOA_BASE ;
|
|
pgpio = (GPIO_TypeDef* ) address ;
|
|
|
|
/* -------------------------Configure the port pins---------------- */
|
|
/*-- GPIO Mode Configuration --*/
|
|
for (thispin = 0x0001, pos = 0; thispin < 0x10000; thispin <<= 1, pos +=1 )
|
|
{
|
|
if ( pins & thispin)
|
|
{
|
|
pgpio->MODER &= ~(GPIO_MODER_MODER0 << (pos * 2)) ;
|
|
pgpio->MODER |= (config & PIN_MODE_MASK) << (pos * 2) ;
|
|
|
|
if ( ( (config & PIN_MODE_MASK ) == PIN_OUTPUT) || ( (config & PIN_MODE_MASK) == PIN_PERIPHERAL) )
|
|
{
|
|
/* Speed mode configuration */
|
|
pgpio->OSPEEDR &= ~(GPIO_OSPEEDER_OSPEEDR0 << (pos * 2)) ;
|
|
pgpio->OSPEEDR |= ((config & PIN_SPEED_MASK) >> 13 ) << (pos * 2) ;
|
|
|
|
/* Output mode configuration*/
|
|
pgpio->OTYPER &= ~((GPIO_OTYPER_OT_0) << ((uint16_t)pos)) ;
|
|
}
|
|
/* Pull-up Pull down resistor configuration*/
|
|
pgpio->PUPDR &= ~(GPIO_PUPDR_PUPDR0 << ((uint16_t)pos * 2));
|
|
pgpio->PUPDR |= ((config & PIN_PULL_MASK) >> 2) << (pos * 2) ;
|
|
|
|
pgpio->AFR[pos >> 3] &= ~(0x000F << ((pos & 7)*4)) ;
|
|
pgpio->AFR[pos >> 3] |= ((config & PIN_PERI_MASK) >> 4) << ((pos & 7)*4) ;
|
|
}
|
|
}
|
|
}
|