spi_console: use print/scanf instead of ncurses, hack 64 bit.
[avr_work.git] / common / i2c.h
blob9f613fae20a8524a085a38af74586898396eb661
2 #ifndef _TWI_I2C_H_
3 #define _TWI_I2C_H_
5 #include <util/twi.h>
7 /** I2C Clock Generation **/
8 #define F_SCL 100000
9 #define TWI_PS_MSK 0
10 #define TWI_BR_VAL -(16*F_SCL-F_CPU)/(2*F_SCL)
12 /** Slave Mode **/
13 // Slave Address(s) for device 0b7654321
14 #define I2C_SLAVE_ADDR 0b0011100
15 #define I2C_SLAVE_ADDR_MSK 0b0000000
16 // Respond to General Call in Slave mode?
17 #define I2C_GENERAL_CALL_EN 0
19 /** State Control **/
20 typedef enum { I2C_IDLE = 0,
21 I2C_BUSY = 1,
22 I2C_MT = 2,
23 I2C_MR = 3,
24 I2C_ST = 4,
25 I2C_SR = 5
26 } i2c_mode_t;
27 i2c_mode_t i2c_mode;
30 /** TWCR Control Values **/
31 // Enabe TWI, Clear INT flag, Enable Ack, Enable Interupt
32 #define TWCR_NACK ( (1<<TWEN)|(1<<TWINT)|(1<<TWIE) )
33 #define TWCR_BASE ( TWCR_NACK|(1<<TWEA) )
34 #define TWCR_START ( TWCR_BASE|(1<<TWSTA) )
35 #define TWCR_STOP ( TWCR_BASE|(1<<TWSTO) )
36 #define TWCR_RESET ( TWCR_BASE|(1<<TWSTO)|(1<<TWSTA) )
39 /** Transfer Control & Queue **/
41 typedef struct {
42 uint8_t addr;
43 uint8_t w_len;
44 uint8_t r_len;
45 uint8_t * w_buf;
46 uint8_t * r_buf;
47 uint8_t w_pos;
48 uint8_t r_pos;
49 uint8_t (*callback)(void);
50 } i2c_msg_t;
52 uint8_t i2c_msg_curr;
53 uint8_t i2c_buf_loc;
55 #define I2C_MSG_BUF_LEN 5
56 i2c_msg_t i2c_msg_buffer[I2C_MSG_BUF_LEN];
58 void i2c_next_msg();
60 int i2c_add_msg(uint8_t addr,
61 uint8_t w_len,
62 uint8_t r_len,
63 uint8_t * w_buf,
64 uint8_t * r_buf,
65 uint8_t (*callback)(void);
66 int i2c_rem_msg(int msg_num);
69 /** Static Bus Operation **/
70 uint8_t dev_w_addr;
71 uint8_t dev_r_addr;
73 uint8_t w_data_buf_len;
74 uint8_t * w_data_buf;
75 volatile uint8_t w_data_buf_pos;
77 uint8_t r_data_buf_len;
78 volatile uint8_t * r_data_buf;
79 volatile uint8_t r_data_buf_pos;
81 uint8_t (*xfer_complete_cb)(void);
84 /** Functions **/
85 void twi_init(void);
86 int i2c_reset_xfer(void);
87 int i2c_start_xfer(void);
91 #endif