#include "extgpio.h"
#include "pcf8574.h"
#include "74hc595.h"
#define FREE_CONTROL 0
const IIC_PIN pcf8574[3] =
{
{
.SCL_PORT = IIC_SCL_GPIO_Port,
.SCL_PIN = IIC_SCL_Pin,
.SDA_PORT = IIC_SDA_GPIO_Port,
.SDA_PIN = IIC_SDA_Pin,
.DelayTick = 10,
.ADDR = 0x42
},
{
.SCL_PORT = IIC_SCL_GPIO_Port,
.SCL_PIN = IIC_SCL_Pin,
.SDA_PORT = IIC_SDA_GPIO_Port,
.SDA_PIN = IIC_SDA_Pin,
.DelayTick = 10,
.ADDR = 0x44
},
{
.SCL_PORT = IIC_SCL_GPIO_Port,
.SCL_PIN = IIC_SCL_Pin,
.SDA_PORT = IIC_SDA_GPIO_Port,
.SDA_PIN = IIC_SDA_Pin,
.DelayTick = 10,
.ADDR = 0x46
}
};
const T_GPIO_TYPE INPUT_PINS[8] =
{
{.port = GPIOB, .pin = GPIO_PIN_12},
{.port = GPIOB, .pin = GPIO_PIN_13},
{.port = GPIOB, .pin = GPIO_PIN_14},
{.port = GPIOB, .pin = GPIO_PIN_15},
{.port = GPIOC, .pin = GPIO_PIN_6},
{.port = GPIOC, .pin = GPIO_PIN_7},
{.port = GPIOC, .pin = GPIO_PIN_8},
{.port = GPIOA, .pin = GPIO_PIN_11}
};
const T_GPIO_TYPE OUTPUT_PINS[6] =
{
{.port = GPIOC, .pin = GPIO_PIN_5},
{.port = GPIOC, .pin = GPIO_PIN_4},
{.port = GPIOA, .pin = GPIO_PIN_7},
{.port = GPIOA, .pin = GPIO_PIN_6},
{.port = GPIOA, .pin = GPIO_PIN_5},
{.port = GPIOA, .pin = GPIO_PIN_4}
};
const T_GPIO_TYPE id_pin[4] =
{
{.port = GPIOA, .pin = GPIO_PIN_12},
{.port = GPIOB, .pin = GPIO_PIN_4},
{.port = GPIOB, .pin = GPIO_PIN_8},
{.port = GPIOB, .pin = GPIO_PIN_9}
};
U_INPUT IN;
U_OUTPUT OUT;
void init_ext_gpio(void)
{
hc595Init(&hc595);
}
void get_gpio_pins(U_INPUT *pin)
{
uint8_t i, XIN = 0;
for(i = 0; i < 3; i++)
{
PCF8574ReadByte(&pcf8574[i], &pin->pin.EX[i]);
}
for(i = 0; i < 8; i++)
{
XIN >>= 1;
if(HAL_GPIO_ReadPin(INPUT_PINS[i].port, INPUT_PINS[i].pin) == GPIO_PIN_RESET)
{
XIN &= ~0x80;
}
else
{
XIN |= 0x80;
}
}
pin->pin.X = XIN;
}
void set_gpio_pins(U_OUTPUT *pout)
{
uint8_t i, XOUT;
hc595WriteStr(&hc595, &pout->pin.EY[0], 3);
XOUT = pout->pin.Y;
for(i = 0; i < 6; i++)
{
HAL_GPIO_WritePin(OUTPUT_PINS[i].port, OUTPUT_PINS[i].pin, XOUT & 0x01 ? GPIO_PIN_SET : GPIO_PIN_RESET);
XOUT >>= 1;
}
}
uint8_t read_board_id(void)
{
uint8_t id, i;
id = 0;
for(i = 0; i < 4; i++)
{
id >>= 1;
if(HAL_GPIO_ReadPin(id_pin[i].port, id_pin[i].pin) == GPIO_PIN_RESET)
{
id |= 0x08;
}
}
return id;
}
uint8_t get_led_state(const U_OUTPUT *pout, uint8_t *pled)
{
uint8_t i;
for(i = 0; i < 6; i++)
{
*pled++ = pout->pin.EY[i];
}
return 0;
}
#ifndef __EXT_GPIO_HEADER__
#define __EXT_GPIO_HEADER__
#include "main.h"
typedef struct
{
GPIO_TypeDef* port;
uint16_t pin;
} T_GPIO_TYPE;
typedef union
{
uint8_t input[4];
struct
{
uint8_t EX[3];
uint8_t X;
} pin;
uint32_t data;
} U_INPUT;
typedef union
{
uint8_t output[4];
struct
{
uint8_t EY[3];
uint8_t Y;
} pin;
uint32_t data;
} U_OUTPUT;
extern U_INPUT IN;
extern U_OUTPUT OUT;
void init_ext_gpio(void);
void get_gpio_pins(U_INPUT *pin);
void set_gpio_pins(U_OUTPUT *pout);
uint8_t read_board_id(void);
uint8_t get_led_state(const U_OUTPUT *pout, uint8_t *pled);
#endif