Simplified io.c

This commit is contained in:
T-moe
2016-01-30 16:30:18 +01:00
parent b6f2e470c2
commit 9acd2010dd

View File

@@ -6,13 +6,10 @@
typedef struct pin_s { typedef struct pin_s {
GPIO_TypeDef* GPIO; GPIO_TypeDef* GPIO;
uint16_t pinmask; uint16_t pinmask;
bool input;
} pin_t; } pin_t;
static void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr, bool input); static void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr);
static bool pin_get(pin_t* pin); static bool pin_get(pin_t* pin);
static void pin_set(pin_t* pin, bool status);
static void pin_toggle(pin_t* pin);
// Local variables // Local variables
static pin_t pin_t0; static pin_t pin_t0;
@@ -30,56 +27,33 @@ static volatile unsigned char* SWITCH = (volatile unsigned char*)0x6C000400;
#define SQR3_SQ_SET ((uint32_t)0x0000001F) #define SQR3_SQ_SET ((uint32_t)0x0000001F)
#define SMPR1_SMP_SET ((uint32_t)0x00000007) #define SMPR1_SMP_SET ((uint32_t)0x00000007)
void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr, bool input) { void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr) {
GPIO_InitTypeDef gi; // Create gpio init structure GPIO_InitTypeDef gi; // Create gpio init structure
GPIO_StructInit(&gi); // Fill gpio init structure with defaults GPIO_StructInit(&gi); // Fill gpio init structure with defaults
gi.GPIO_Pin = 1 << pinnr; // create bitmask out of pin number gi.GPIO_Pin = 1 << pinnr; // create bitmask out of pin number
if(input) { // If the pin is set to be an input
gi.GPIO_Mode = GPIO_Mode_IN; // Set mode to input gi.GPIO_Mode = GPIO_Mode_IN; // Set mode to input
gi.GPIO_OType = GPIO_OType_OD; // Set type to open drain gi.GPIO_OType = GPIO_OType_OD; // Set type to open drain
gi.GPIO_PuPd = GPIO_PuPd_UP; // Set a pullup gi.GPIO_PuPd = GPIO_PuPd_UP; // Set a pullup
} else {
gi.GPIO_Mode = GPIO_Mode_OUT; // Set mode to output
gi.GPIO_OType = GPIO_OType_PP; // Set type to pushpull
gi.GPIO_PuPd = GPIO_PuPd_NOPULL; // Set no pullup
}
GPIO_Init(GPIO,&gi); // Update the GPIO configuration GPIO_Init(GPIO,&gi); // Update the GPIO configuration
pin->GPIO=GPIO; // Set the gpiopin in the pin structure pin->GPIO=GPIO; // Set the gpiopin in the pin structure
pin->pinmask=0x01<<pinnr; // Insert the pinmask pin->pinmask=0x01<<pinnr; // Insert the pinmask
pin->input = input; // Store the input information
} }
bool pin_get(pin_t* pin) { bool pin_get(pin_t* pin) {
if(pin->input) { // If the pin is an input
return GPIO_ReadInputDataBit(pin->GPIO,pin->pinmask); // Read its value return GPIO_ReadInputDataBit(pin->GPIO,pin->pinmask); // Read its value
} else { // If the pin is an output
return GPIO_ReadOutputDataBit(pin->GPIO,pin->pinmask); // Read its set value
}
}
void pin_set(pin_t* pin, bool status) {
if(!pin->input) { // If the pin isn't an input
GPIO_WriteBit(pin->GPIO,pin->pinmask,status); // Set its value accordingly
}
}
void pin_toggle(pin_t* pin) {
if(!pin->input) { // If the pin isn't an input
pin_set(pin,!pin_get(pin)); // Toggle its value
}
} }
void io_init(void){ void io_init(void){
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC,ENABLE); // Enable gpio clock source RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC,ENABLE); // Enable gpio clock source
// Create player pins // Create player pins
pin_create(&pin_t0, GPIOC, 7, true); // create pin_t0 pin_create(&pin_t0, GPIOC, 7); // create pin_t0
pin_create(&pin_t1, GPIOB, 15, true); // create pin_t1 pin_create(&pin_t1, GPIOB, 15); // create pin_t1
pin_create(&pin_t2, GPIOB, 14, true); // create pin_t2 pin_create(&pin_t2, GPIOB, 14); // create pin_t2
pin_create(&pin_t3, GPIOI, 0, true); // create pin_t3 pin_create(&pin_t3, GPIOI, 0); // create pin_t3
//Enable the peripheral clock of GPIOB //Enable the peripheral clock of GPIOB
RCC->AHB1ENR |= RCC_AHB1Periph_GPIOB; RCC->AHB1ENR |= RCC_AHB1Periph_GPIOB;