]> rtime.felk.cvut.cz Git - lincan.git/blob - embedded/libs4c/i2c/i2c_drv.h
Merge branch 'master' into can-usb1
[lincan.git] / embedded / libs4c / i2c / i2c_drv.h
1 /*******************************************************************
2   Components for embedded applications builded for
3   laboratory and medical instruments firmware  
4  
5   i2c_drv.h - I2C communication automat interface 
6  
7   Copyright holders and project originators
8     (C) 2001-2008 by Pavel Pisa pisa@cmp.felk.cvut.cz
9     (C) 2002-2008 by PiKRON Ltd. http://www.pikron.com
10     (C) 2007-2008 by Petr Smolik
11
12  The COLAMI components can be used and copied under next licenses
13    - MPL - Mozilla Public License
14    - GPL - GNU Public License
15    - LGPL - Lesser GNU Public License
16    - and other licenses added by project originators
17  Code can be modified and re-distributed under any combination
18  of the above listed licenses. If contributor does not agree with
19  some of the licenses, he can delete appropriate line.
20  Warning, if you delete all lines, you are not allowed to
21  distribute code or build project.
22  *******************************************************************/
23
24 #ifndef _I2C_DRV_H_
25 #define _I2C_DRV_H_
26
27 #include <types.h>
28 #include <cpu_def.h>
29
30 #if defined(CONFIG_OC_I2C_DRV_SYSLESS)
31   #define I2C_IRQ_LOCK_FINI unsigned long i2c_irq_lock_flags=0;
32   #define I2C_IRQ_LOCK \
33     {save_flags(i2c_irq_lock_flags);cli();}
34   #define I2C_IRQ_UNLOCK \
35     {restore_flags(i2c_irq_lock_flags);}
36   #define I2C_MB()       {asm volatile ("":::"memory");}
37 #endif
38
39 struct i2c_drv;
40
41 #define I2C_MSG_TX       0x001
42 #define I2C_MSG_RX       0x002
43 #define I2C_MSG_MS_TX    I2C_MSG_TX
44 #define I2C_MSG_MS_RX    I2C_MSG_RX
45 #define I2C_MSG_SL_TX    I2C_MSG_TX
46 #define I2C_MSG_SL_RX    I2C_MSG_RX
47 #define I2C_MSG_SLAVE    0x004
48 #define I2C_MSG_FAIL     0x008
49 #define I2C_MSG_REPEAT   0x010
50 #define I2C_MSG_NOPROC   0x020
51 #define I2C_MSG_FINISHED 0x040
52 #define I2C_MSG_CB_START 0x100
53 #define I2C_MSG_CB_END   0x200
54 #define I2C_MSG_CB_PROC  0x400
55
56 typedef struct i2c_msg_head {
57     unsigned long flags;/* message flags */
58     uint8_t  sl_cmd;    /* command for slave queue lookup */
59     uint8_t  sl_msk;    /* sl_cmd match mask */
60     uint16_t addr;      /* message destination address */
61     uint16_t tx_rq;     /* requested TX transfer length */
62     uint16_t rx_rq;     /* requested RX transfer length */
63     uint16_t tx_len;    /* finished TX transfer length */
64     uint16_t rx_len;    /* finished RX transfer length */
65     uint8_t *tx_buf;    /* pointer to TX data */
66     uint8_t *rx_buf;    /* pointer to RX data */
67     struct i2c_msg_head *prev;
68     struct i2c_msg_head *next;
69     struct i2c_msg_head **on_queue;
70     int (*callback)(struct i2c_drv *ifc, int code, struct i2c_msg_head *msg);
71     unsigned long private;
72   } i2c_msg_head_t;
73
74 typedef int (i2c_sfnc_t)(struct i2c_drv *drv, int code);
75 typedef int (i2c_ctrl_fnc_t)(struct i2c_drv *drv, int ctrl, void *p);
76
77 #define I2C_DRV_ON      1 /* flag indicating that driver is ready to operate */
78 #define I2C_DRV_MS_INPR 2 /* master request in in progress */
79 #define I2C_DRV_NA      4 /* driver is not active for some period */
80 #define I2C_DRV_SL_CEXP 8 /* slave expect receive of the first byte */
81 #define I2C_DRV_SL_INRX 0x10 /* slave in mode */
82
83 #define I2C_DRV_MAGIC 0x12345432
84
85 typedef struct i2c_drv {
86     int magic;          /* magic number */
87     int irq;            /* irq number */
88     long port;          /* base port number */
89     uint8_t flags;
90     uint16_t self_addr;
91     i2c_msg_head_t *master_queue;
92     i2c_msg_head_t *slave_queue;
93     i2c_msg_head_t *proc_queue;
94     i2c_msg_head_t *msg_act;
95     i2c_sfnc_t *sfnc_act;
96     void *failed;
97     i2c_ctrl_fnc_t *ctrl_fnc;
98     int (*poll_fnc)(struct i2c_drv *drv);
99     uint8_t sl_last_cmd; /* last received slave command */
100   } i2c_drv_t;
101
102 #define I2C_CTRL_MS_RQ 1
103
104 #ifdef CONFIG_OC_I2C_CHIP_C552
105 int c552_init_start(struct i2c_drv *drv, int port, int irq, int bitrate, int sladr);
106 #endif /* CONFIG_OC_I2C_CHIP_C552 */
107
108 void i2c_drv_queue_msg(i2c_msg_head_t **queue, i2c_msg_head_t *msg);
109 int  i2c_drv_init(i2c_drv_t *drv, int port, int irq, int bitrate,int sladr);
110 int  i2c_drv_master_msg_ins(i2c_drv_t *drv, i2c_msg_head_t *msg);
111 int  i2c_drv_master_msg_rem(i2c_drv_t *drv, i2c_msg_head_t *msg);
112 int  i2c_drv_flush_all(i2c_drv_t *drv);
113 int  i2c_drv_master_transfer(i2c_drv_t *drv, int addr, int tx_rq, int rx_rq,
114                    void *tx_buf, void *rx_buf, int *ptx_len, int *prx_len);
115
116 #ifdef I2C_LOG_ENABLE
117   /* todo */
118 #else
119   #define I2C_PRINTF(x,args...) 
120 #endif
121
122 #endif /* _I2C_DRV_H_ */