initial
This commit is contained in:
commit
2be95189f4
106
gpio.c
Normal file
106
gpio.c
Normal file
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* gpio.c
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
|
||||
#include <msp430g2553.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define GPIO_C
|
||||
#include "gpio.h"
|
||||
#undef GPIO_C
|
||||
|
||||
|
||||
|
||||
extern tPinCfg pinCfg[];
|
||||
|
||||
|
||||
void gpioInitPins() {
|
||||
P1OUT = 0;
|
||||
P1DIR = 0;
|
||||
P1REN = 0;
|
||||
P2OUT = 0;
|
||||
P2DIR = 0;
|
||||
P2REN = 0;
|
||||
|
||||
for (tPin p = PINS_FIRST; p < PINS_END; p += 1) {
|
||||
tPinCfg pin = pinCfg[p];
|
||||
if (pin.portId == PORT1) {
|
||||
if (pin.direction == PIN_OUT) {
|
||||
P1DIR |= pin.bit;
|
||||
P1SEL &= ~pin.bit;
|
||||
P1SEL2 &= ~pin.bit;
|
||||
gpioSetPin(p, pin.defaultOut);
|
||||
} else if (pin.direction == PIN_IN) {
|
||||
P1DIR &= ~pin.bit;
|
||||
P1SEL &= ~pin.bit;
|
||||
P1SEL2 &= ~pin.bit;
|
||||
P1REN &= ~pin.bit;
|
||||
} else if (pin.direction == PIN_IN_PULLUP) {
|
||||
P1DIR &= ~pin.bit;
|
||||
P1SEL &= ~pin.bit;
|
||||
P1SEL2 &= ~pin.bit;
|
||||
P1REN |= pin.bit;
|
||||
P1OUT |= pin.bit;
|
||||
}
|
||||
} else if (pin.portId == PORT2) {
|
||||
if (pin.direction == PIN_OUT) {
|
||||
P2DIR |= pin.bit;
|
||||
P2SEL &= ~pin.bit;
|
||||
P2SEL2 &= ~pin.bit;
|
||||
gpioSetPin(p, pin.defaultOut);
|
||||
} else if (pin.direction == PIN_IN) {
|
||||
P2DIR &= ~pin.bit;
|
||||
P2SEL &= ~pin.bit;
|
||||
P2SEL2 &= ~pin.bit;
|
||||
P2REN &= ~pin.bit;
|
||||
} else if (pin.direction == PIN_IN_PULLUP) {
|
||||
P2DIR &= ~pin.bit;
|
||||
P2SEL &= ~pin.bit;
|
||||
P2SEL2 &= ~pin.bit;
|
||||
P2REN |= pin.bit;
|
||||
P2OUT |= pin.bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void gpioSetPin(tPin p, tPinState v) {
|
||||
tPinCfg pin = pinCfg[p];
|
||||
if (v == HIGH) {
|
||||
if (pin.portId == PORT1) {
|
||||
P1OUT |= pin.bit;
|
||||
} else if (pin.portId == PORT2) {
|
||||
P2OUT |= pin.bit;
|
||||
}
|
||||
} else {
|
||||
if (pin.portId == PORT1) {
|
||||
P1OUT &= ~pin.bit;
|
||||
} else if (pin.portId == PORT2) {
|
||||
P2OUT &= ~pin.bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void gpioTogglePin(tPin p) {
|
||||
tPinCfg pin = pinCfg[p];
|
||||
if (pin.portId == PORT1) {
|
||||
P1OUT ^= pin.bit;
|
||||
} else if (pin.portId == PORT2) {
|
||||
P2OUT ^= pin.bit;
|
||||
}
|
||||
}
|
||||
|
||||
tPinState gpioGetPin(tPin p) {
|
||||
tPinCfg pin = pinCfg[p];
|
||||
uint16_t pinValue = 0;
|
||||
if (pin.portId == PORT1) {
|
||||
pinValue = P1IN;
|
||||
} else if (pin.portId == PORT2) {
|
||||
pinValue = P2IN;
|
||||
}
|
||||
return (pinValue & pin.bit) ? HIGH : LOW;
|
||||
}
|
54
gpio.h
Normal file
54
gpio.h
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* gpio.h
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef GPIO_H_
|
||||
#define GPIO_H_
|
||||
|
||||
|
||||
#include <msp430g2553.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
||||
typedef enum {
|
||||
PORT1,
|
||||
PORT2
|
||||
} tPort;
|
||||
|
||||
typedef enum {
|
||||
LOW,
|
||||
HIGH,
|
||||
} tPinState;
|
||||
|
||||
typedef enum {
|
||||
PIN_OUT,
|
||||
PIN_IN,
|
||||
PIN_IN_PULLUP
|
||||
} tPinDirection;
|
||||
|
||||
typedef struct {
|
||||
tPort portId;
|
||||
uint16_t bit;
|
||||
tPinDirection direction;
|
||||
tPinState defaultOut;
|
||||
} tPinCfg;
|
||||
|
||||
|
||||
|
||||
#include "gpioCfg.h"
|
||||
|
||||
|
||||
void gpioInitPins();
|
||||
void gpioSetPin(tPin p, tPinState v);
|
||||
void gpioTogglePin(tPin p);
|
||||
tPinState gpioGetPin(tPin p);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* GPIO_H_ */
|
24
gpioCfg.c-example
Normal file
24
gpioCfg.c-example
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* gpioCfg.c
|
||||
*
|
||||
* Created on: 29.08.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#include <msp430g2553.h>
|
||||
#include "gpio.h"
|
||||
|
||||
tPinCfg pinCfg[PINS_END] = {
|
||||
{PORT1, BIT7, PIN_OUT, LOW}, //A
|
||||
{PORT1, BIT6, PIN_OUT, LOW}, //B
|
||||
{PORT2, BIT1, PIN_OUT, LOW}, //C
|
||||
{PORT2, BIT3, PIN_OUT, LOW}, //D
|
||||
{PORT2, BIT4, PIN_OUT, LOW}, //E
|
||||
{PORT2, BIT5, PIN_OUT, LOW}, //F
|
||||
{PORT2, BIT2, PIN_OUT, LOW}, //G
|
||||
{PORT2, BIT0, PIN_OUT, HIGH}, //0
|
||||
{PORT1, BIT5, PIN_OUT, HIGH}, //1
|
||||
{PORT1, BIT2, PIN_OUT, HIGH}, //2
|
||||
{PORT1, BIT0, PIN_IN_PULLUP, LOW}, // BUTTON_1
|
||||
{PORT1, BIT1, PIN_OUT, LOW}, // LED_1
|
||||
};
|
31
gpioCfg.h-example
Normal file
31
gpioCfg.h-example
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* gpioCfg.h
|
||||
*
|
||||
* Created on: 18.09.2016
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#ifndef GPIOCFG_H_
|
||||
#define GPIOCFG_H_
|
||||
|
||||
#include "gpio.h"
|
||||
|
||||
typedef enum {
|
||||
PINS_FIRST,
|
||||
SEG_A = PINS_FIRST,
|
||||
SEG_B,
|
||||
SEG_C,
|
||||
SEG_D,
|
||||
SEG_E,
|
||||
SEG_F,
|
||||
SEG_G,
|
||||
DIGIT_0,
|
||||
DIGIT_1,
|
||||
DIGIT_2,
|
||||
BUTTON_1,
|
||||
LED_1,
|
||||
PINS_END
|
||||
} tPin;
|
||||
|
||||
|
||||
#endif /* GPIOCFG_H_ */
|
Loading…
x
Reference in New Issue
Block a user