-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_expander.c
113 lines (89 loc) · 3.09 KB
/
io_expander.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "io_expander.h"
#include "validate.h"
#include <stdio.h>
#include <string.h>
#include <stdint.h>
bool io_expander_init(void) {
if(gpio_enable_port(IO_EXPANDER_GPIO_BASE) == false)
{
return false;
}
// Configure SCL
if(gpio_config_digital_enable(IO_EXPANDER_GPIO_BASE, IO_EXPANDER_I2C_SCL_PIN)== false)
{
return false;
}
if(gpio_config_alternate_function(IO_EXPANDER_GPIO_BASE, IO_EXPANDER_I2C_SCL_PIN)== false)
{
return false;
}
if(gpio_config_port_control(IO_EXPANDER_GPIO_BASE, IO_EXPANDER_I2C_SCL_PCTL_M, IO_EXPANDER_I2C_SCL_PIN_PCTL)== false)
{
return false;
}
// Configure SDA
if(gpio_config_digital_enable(IO_EXPANDER_GPIO_BASE, IO_EXPANDER_I2C_SDA_PIN)== false)
{
return false;
}
if(gpio_config_open_drain(IO_EXPANDER_GPIO_BASE, IO_EXPANDER_I2C_SDA_PIN)== false)
{
return false;
}
if(gpio_config_alternate_function(IO_EXPANDER_GPIO_BASE, IO_EXPANDER_I2C_SDA_PIN)== false)
{
return false;
}
if(gpio_config_port_control(IO_EXPANDER_GPIO_BASE, IO_EXPANDER_I2C_SDA_PCTL_M, IO_EXPANDER_I2C_SDA_PIN_PCTL)== false)
{
return false;
}
// Initialize the I2C peripheral
if( initializeI2CMaster(IO_EXPANDER_I2C_BASE)!= I2C_OK)
{
return false;
}
return true;
}
void io_expander_en_interrupts() {
gpio_enable_port(IO_EXPANDER_IRQ_GPIO_BASE);
gpio_config_digital_enable(IO_EXPANDER_IRQ_GPIO_BASE, PF0);
gpio_config_enable_input(IO_EXPANDER_IRQ_GPIO_BASE, PF0);
gpio_config_falling_edge_irq(IO_EXPANDER_IRQ_GPIO_BASE, PF0);
NVIC_SetPriority(GPIOF_IRQn, 0);
NVIC_EnableIRQ(GPIOF_IRQn);
GPIOF->ICR |= PF0;
io_expander_read_reg(MCP23017_GPIOB_R);
}
void config_up_down_buttons() {
io_expander_write_reg(MCP23017_IODIRB_R, 0xFF);
io_expander_write_reg(MCP23017_GPINTENB_R, 0x0F);
io_expander_write_reg(MCP23017_DEFVALB_R, 0xFF);
io_expander_write_reg(MCP23017_INTCONB_R, 0x00);
io_expander_write_reg(MCP23017_GPPUB_R, 0xFF);
}
void io_expander_write_reg(uint8_t reg, uint8_t data) {
//set slave
i2cSetSlaveAddr(IO_EXPANDER_I2C_BASE, MCP23017_DEV_ID, I2C_WRITE);
// Before doing anything, make sure the I2C device is idle
while ( I2CMasterBusy(IO_EXPANDER_I2C_BASE)) {put_string("w");};
// Send the reg Address
i2cSendByte( IO_EXPANDER_I2C_BASE, reg, I2C_MCS_START | I2C_MCS_RUN);
// send the data
i2cSendByte( IO_EXPANDER_I2C_BASE, data, I2C_MCS_RUN | I2C_MCS_STOP);
}
uint8_t io_expander_read_reg(uint8_t addr) {
i2c_status_t status;
uint8_t data;
// Before doing anything, make sure the I2C device is idle
while ( I2CMasterBusy(IO_EXPANDER_I2C_BASE)) {put_string("w");};
// Set the I2C slave address to be the EEPROM and in Write Mode
i2cSetSlaveAddr(IO_EXPANDER_I2C_BASE, MCP23017_DEV_ID, I2C_WRITE);
// Send the address
i2cSendByte(IO_EXPANDER_I2C_BASE, addr, I2C_MCS_START | I2C_MCS_RUN);
// Set the I2C slave address to be the EEPROM and in Read Mode
i2cSetSlaveAddr(IO_EXPANDER_I2C_BASE, MCP23017_DEV_ID, I2C_READ);
// Read the data returned by the EEPROM
i2cGetByte(IO_EXPANDER_I2C_BASE, &data, I2C_MCS_START | I2C_MCS_RUN | I2C_MCS_STOP);
return data;
}