Subversion Repositories group.electronics

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
123 pfowler 1
#include <avr/io.h>
2
#include <stdlib.h>
3
#include "wire.h"
4
#include "avrutil.h"
5
#include <util/delay.h>
6
 
7
#include "mio.h"
8
 
9
#define IODIR	0x00	// I/O Direction, 1 = Input, 0 = Output
10
#define IPOL	0x01	// Polarity, 1 = Invert
11
#define	GPINTEN	0x02	// Interrupt on change, 1 = Enable
12
#define	DEFVAL	0x03	// Default compare value
13
#define INTCON	0x04
14
#define	IOCON	0x05	// Config
15
#define	GPPU	0x06	// Pullups for input, 1 = Enable
16
#define	INTF	0x07	// Interrupt flag (pin mask)
17
#define	INTCAP	0x08	// GPIO value at interrupt (RO)
18
#define	GPIO	0x09	// Read value of pins
19
#define OLAT	0x0a	// Latch pins (write)
20
 
21
void mio_init() {
22
  mio.i2c_addr = 0x20;
23
}
24
 
25
void mio_init_addr(uint8_t i2c_addr) {
26
  mio.i2c_addr = i2c_addr;
27
}
28
 
29
inline uint8_t mio_readPin(uint8_t pin) {
30
    return rbi(mio_read(), pin);
31
}
32
 
33
inline uint8_t mio_read() {
34
  return mio_readReg(GPIO);
35
}
36
 
37
inline void mio_latch(uint8_t data) {
38
  mio_writeReg(OLAT, data);
39
}
40
 
41
void mio_latchPin(uint8_t pin, uint8_t value) {
42
  uint8_t val = mio_read();
43
  if (value)
44
    sbi(val, pin);
45
  else
46
    cbi(val, pin);
47
  mio_latch(val);
48
}
49
 
50
inline void mio_iodir(uint8_t dir) {
51
  mio_writeReg(IODIR, dir);
52
}
53
 
54
inline void mio_pullups(uint8_t pu) {
55
  mio_writeReg(GPPU, pu);
56
}
57
 
58
 
59
 
60
 
61
uint8_t mio_readReg(uint8_t address) {
62
  if (!mio.i2c_addr) return 0xFF;
63
 
64
  uint8_t reg = 0xFF;
65
 
66
  i2c_beginTransmission(mio.i2c_addr);
67
  i2c_writeByte((int) address);
68
  i2c_endTransmission(0);
69
 
70
  i2c_requestFrom(mio.i2c_addr, 1, 1);
71
  if (i2c_available()) reg = i2c_read();  
72
 
73
  return reg;
74
}
75
 
76
void mio_writeReg(uint8_t address, uint8_t reg) {
77
    if (!mio.i2c_addr) return;
78
    i2c_beginTransmission(mio.i2c_addr);
79
    i2c_writeByte((int) address);
80
    i2c_writeByte(reg);
81
    i2c_endTransmission(1);
82
}
83