]> rtime.felk.cvut.cz Git - lincan.git/blob - embedded/libs4c/i2c/i2c_drv.h
Update of system-less architecture and board support code to actual uLAN.sf.net version.
[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 typedef int (i2c_stroke_fnc_t)(struct i2c_drv *drv);
77
78 #define I2C_DRV_ON      1 /* flag indicating that driver is ready to operate */
79 #define I2C_DRV_MS_INPR 2 /* master request in in progress */
80 #define I2C_DRV_NA      4 /* driver is not active for some period */
81 #define I2C_DRV_SL_CEXP 8 /* slave expect receive of the first byte */
82 #define I2C_DRV_SL_INRX 0x10 /* slave in mode */
83
84 #define I2C_DRV_MAGIC 0x12345432
85
86 typedef struct i2c_drv {
87     int magic;          /* magic number */
88     int irq;            /* irq number */
89     long port;          /* base port number */
90     uint8_t flags;
91     uint16_t self_addr;
92     i2c_msg_head_t *master_queue;
93     i2c_msg_head_t *slave_queue;
94     i2c_msg_head_t *proc_queue;
95     i2c_msg_head_t *msg_act;
96     i2c_sfnc_t *sfnc_act;
97     void *failed;
98     i2c_ctrl_fnc_t *ctrl_fnc;
99     int (*poll_fnc)(struct i2c_drv *drv);
100     i2c_stroke_fnc_t *stroke_fnc;
101     uint8_t sl_last_cmd; /* last received slave command */
102   } i2c_drv_t;
103
104 #define I2C_CTRL_MS_RQ 1
105
106 #ifdef CONFIG_OC_I2C_CHIP_C552
107 int c552_init_start(struct i2c_drv *drv, int port, int irq, int bitrate, int sladr);
108 #endif /* CONFIG_OC_I2C_CHIP_C552 */
109
110 void i2c_drv_queue_msg(i2c_msg_head_t **queue, i2c_msg_head_t *msg);
111 int  i2c_drv_init(i2c_drv_t *drv, int port, int irq, int bitrate,int sladr);
112 int  i2c_drv_master_msg_ins(i2c_drv_t *drv, i2c_msg_head_t *msg);
113 int  i2c_drv_master_msg_rem(i2c_drv_t *drv, i2c_msg_head_t *msg);
114 int  i2c_drv_flush_all(i2c_drv_t *drv);
115 int  i2c_drv_master_transfer(i2c_drv_t *drv, int addr, int tx_rq, int rx_rq,
116                    void *tx_buf, void *rx_buf, int *ptx_len, int *prx_len);
117
118 #ifdef I2C_LOG_ENABLE
119   /* todo */
120 #else
121   #define I2C_PRINTF(x,args...) 
122 #endif
123
124 #endif /* _I2C_DRV_H_ */