1 |
6 |
sshuv2 |
|
2 |
|
|
/*******************************************************************************
|
3 |
|
|
** *
|
4 |
|
|
** Project: IIC Multiple Bus Controller (IICMB) *
|
5 |
|
|
** *
|
6 |
|
|
** File: Definitions of several basic functions for IICMB core. *
|
7 |
|
|
** Version: *
|
8 |
|
|
** 1.0, May 25, 2016 *
|
9 |
|
|
** *
|
10 |
|
|
** Author: Sergey Shuvalkin, (sshuv2@opencores.org) *
|
11 |
|
|
** *
|
12 |
|
|
********************************************************************************
|
13 |
|
|
********************************************************************************
|
14 |
|
|
** Copyright (c) 2016, Sergey Shuvalkin *
|
15 |
|
|
** All rights reserved. *
|
16 |
|
|
** *
|
17 |
|
|
** Redistribution and use in source and binary forms, with or without *
|
18 |
|
|
** modification, are permitted provided that the following conditions are met: *
|
19 |
|
|
** *
|
20 |
|
|
** 1. Redistributions of source code must retain the above copyright notice, *
|
21 |
|
|
** this list of conditions and the following disclaimer. *
|
22 |
|
|
** 2. Redistributions in binary form must reproduce the above copyright *
|
23 |
|
|
** notice, this list of conditions and the following disclaimer in the *
|
24 |
|
|
** documentation and/or other materials provided with the distribution. *
|
25 |
|
|
** *
|
26 |
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" *
|
27 |
|
|
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
|
28 |
|
|
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
|
29 |
|
|
** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE *
|
30 |
|
|
** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
|
31 |
|
|
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF *
|
32 |
|
|
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
|
33 |
|
|
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN *
|
34 |
|
|
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) *
|
35 |
|
|
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
|
36 |
|
|
** POSSIBILITY OF SUCH DAMAGE. *
|
37 |
|
|
*******************************************************************************/
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
#ifndef __IICMB_H__
|
41 |
|
|
#define __IICMB_H__
|
42 |
|
|
|
43 |
|
|
/* IICMB controller base address: */
|
44 |
|
|
#define IICMB_BASE_ADDR (0x00800000)
|
45 |
|
|
|
46 |
|
|
/* IICMB register offsets: */
|
47 |
|
|
#define IICMB_CSR (0x00)
|
48 |
|
|
#define IICMB_DPR (0x01)
|
49 |
|
|
#define IICMB_CMDR (0x02)
|
50 |
|
|
#define IICMB_FSMR (0x03)
|
51 |
|
|
|
52 |
|
|
/* Bits of CSR register */
|
53 |
|
|
#define IICMB_CSR_ENABLE (0x80)
|
54 |
|
|
#define IICMB_CSR_IRQ_ENABLE (0x40)
|
55 |
|
|
|
56 |
|
|
/* Response codes in CMDR register: */
|
57 |
|
|
#define IICMB_RSP_DONE (0x80)
|
58 |
|
|
#define IICMB_RSP_NAK (0x40)
|
59 |
|
|
#define IICMB_RSP_ARB_LOST (0x20)
|
60 |
|
|
#define IICMB_RSP_ERR (0x10)
|
61 |
|
|
#define IICMB_RSP_COMPLETED (0xF0)
|
62 |
|
|
|
63 |
|
|
/* Responses */
|
64 |
|
|
typedef enum
|
65 |
|
|
{
|
66 |
|
|
rsp_done,
|
67 |
|
|
rsp_nak,
|
68 |
|
|
rsp_arb_lost,
|
69 |
|
|
rsp_err
|
70 |
|
|
} rsp_tt;
|
71 |
|
|
|
72 |
|
|
/* Print responses */
|
73 |
|
|
void print_rsp(rsp_tt r);
|
74 |
|
|
|
75 |
|
|
/* Command codes in CMDR register: */
|
76 |
|
|
#define IICMB_CMD_WAIT (0x00)
|
77 |
|
|
#define IICMB_CMD_WRITE (0x01)
|
78 |
|
|
#define IICMB_CMD_READ_ACK (0x02)
|
79 |
|
|
#define IICMB_CMD_READ_NAK (0x03)
|
80 |
|
|
#define IICMB_CMD_START (0x04)
|
81 |
|
|
#define IICMB_CMD_STOP (0x05)
|
82 |
|
|
#define IICMB_CMD_SET_BUS (0x06)
|
83 |
|
|
|
84 |
|
|
/* Commands */
|
85 |
|
|
typedef enum
|
86 |
|
|
{
|
87 |
|
|
cmd_wait,
|
88 |
|
|
cmd_write,
|
89 |
|
|
cmd_read_ack,
|
90 |
|
|
cmd_read_nak,
|
91 |
|
|
cmd_start,
|
92 |
|
|
cmd_stop,
|
93 |
|
|
cmd_set_bus
|
94 |
|
|
} cmd_tt;
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
/* IICMB controller register read/write primitives: */
|
98 |
|
|
#define IICMB_REG_WRITE(off, val) IOWR_8DIRECT(IICMB_BASE_ADDR, (off), (val))
|
99 |
|
|
#define IICMB_REG_READ(off) IORD_8DIRECT(IICMB_BASE_ADDR, (off))
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
void iicmb_init(void); /* Initialize IICMB core */
|
103 |
|
|
void iicmb_disable(void); /* Disable IICMB core */
|
104 |
|
|
|
105 |
|
|
/* Generic Interface commands: ***********************************************/
|
106 |
|
|
rsp_tt iicmb_cmd_wait(unsigned char n); /* Wait */
|
107 |
|
|
rsp_tt iicmb_cmd_write(unsigned char n); /* Write */
|
108 |
|
|
rsp_tt iicmb_cmd_read_ack(unsigned char * n); /* Read with Ack */
|
109 |
|
|
rsp_tt iicmb_cmd_read_nak(unsigned char * n); /* Read with Nak */
|
110 |
|
|
rsp_tt iicmb_cmd_start(void); /* Start */
|
111 |
|
|
rsp_tt iicmb_cmd_stop(void); /* Stop */
|
112 |
|
|
rsp_tt iicmb_cmd_set_bus(unsigned char n); /* Set Bus */
|
113 |
|
|
|
114 |
|
|
|
115 |
|
|
/* High-level operations: ****************************************************/
|
116 |
|
|
|
117 |
|
|
/* Read a single byte
|
118 |
|
|
* Parameters:
|
119 |
|
|
* unsigned char sa -- I2C Slave address (7-bit)
|
120 |
|
|
* unsigned char a -- Byte address
|
121 |
|
|
* unsigned char * d -- Pointer to a storage for received data
|
122 |
|
|
* Returns:
|
123 |
|
|
* rsp_tt -- Response
|
124 |
|
|
*/
|
125 |
|
|
rsp_tt iicmb_read_bus(unsigned char sa, unsigned char a, unsigned char * d);
|
126 |
|
|
|
127 |
|
|
/* Read several bytes
|
128 |
|
|
* Parameters:
|
129 |
|
|
* unsigned char sa -- I2C Slave address (7-bit)
|
130 |
|
|
* unsigned char a -- Byte address
|
131 |
|
|
* unsigned char * d -- Pointer to a storage for received data
|
132 |
|
|
* int n -- Number of bytes to read
|
133 |
|
|
* Returns:
|
134 |
|
|
* rsp_tt -- Response
|
135 |
|
|
*/
|
136 |
|
|
rsp_tt iicmb_read_bus_mul(unsigned char sa, unsigned char a, unsigned char * d, int n);
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
/* Write a single byte
|
140 |
|
|
* Parameters:
|
141 |
|
|
* unsigned char sa -- I2C Slave address (7-bit)
|
142 |
|
|
* unsigned char a -- Byte address
|
143 |
|
|
* unsigned char d -- Data byte to write
|
144 |
|
|
* Returns:
|
145 |
|
|
* rsp_tt -- Response
|
146 |
|
|
*/
|
147 |
|
|
rsp_tt iicmb_write_bus(unsigned char sa, unsigned char a, unsigned char d);
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
/* Write several bytes
|
151 |
|
|
* Parameters:
|
152 |
|
|
* unsigned char sa -- I2C Slave address (7-bit)
|
153 |
|
|
* unsigned char a -- Byte address
|
154 |
|
|
* unsigned char * d -- Pointer to a storage with data to write
|
155 |
|
|
* int n -- Number of bytes to write
|
156 |
|
|
* Returns:
|
157 |
|
|
* rsp_tt -- Response
|
158 |
|
|
*/
|
159 |
|
|
rsp_tt iicmb_write_bus_mul(unsigned char sa, unsigned char a, unsigned char * d, int n);
|
160 |
|
|
|
161 |
|
|
/* Report IICMB registers */
|
162 |
|
|
void iicmb_report_registers(FILE *fp);
|
163 |
|
|
|
164 |
|
|
#endif /* __IICMB_H__ */
|
165 |
|
|
|