-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusbstk5515_i2c.c
executable file
·143 lines (118 loc) · 4.63 KB
/
usbstk5515_i2c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2010 by Spectrum Digital Incorporated.
* All rights reserved. Property of Spectrum Digital Incorporated.
*/
/*
* I2C implementation
*
*/
#include "usbstk5515_i2c.h"
#include "usbstk5515_gpio.h"
Int32 i2c_timeout = 0x0fff;
#define MDR_STT 0x2000
#define MDR_TRX 0x0200
#define MDR_MST 0x0400
#define MDR_IRS 0x0020
#define MDR_FREE 0x4000
#define STR_XRDY 0x0010
#define STR_RRDY 0x0008
#define MDR_STP 0x0800
/* ------------------------------------------------------------------------ *
* _I2C_init( ) *
* The I2C clk is set to run at 20 KHz *
* ------------------------------------------------------------------------ */
Int16 USBSTK5515_I2C_init( )
{
I2C_MDR = 0x0400; // Reset I2C
I2C_PSC = 15; // Config prescaler for 100MHz
I2C_CLKL = 25; // Config clk LOW for 100kHz
I2C_CLKH = 25; // Config clk HIGH for 100kHz
I2C_MDR = 0x0420 ; // Release from reset; Master, Transmitter, 7-bit address
return 0;
}
/* ------------------------------------------------------------------------ *
* _I2C_close( ) *
* ------------------------------------------------------------------------ */
Int16 USBSTK5515_I2C_close( )
{
I2C_MDR = 0; // Reset I2C
return 0;
}
/* ------------------------------------------------------------------------ *
* _I2C_reset( ) *
* ------------------------------------------------------------------------ */
Int16 USBSTK5515_I2C_reset( )
{
USBSTK5515_I2C_close( );
USBSTK5515_I2C_init( );
return 0;
}
/* ------------------------------------------------------------------------ *
* _I2C_write( i2c_addr, data, len ) *
* I2C write in Master mode *
* ------------------------------------------------------------------------ */
Int16 USBSTK5515_I2C_write( Uint16 i2c_addr, Uint8* data, Uint16 len )
{
Int16 timeout, i;
//I2C_IER = 0x0000;
I2C_CNT = len; // Set length
I2C_SAR = i2c_addr; // Set I2C slave address
I2C_MDR = MDR_STT // Set for Master Write
| MDR_TRX
| MDR_MST
| MDR_IRS
| MDR_FREE;
USBSTK5515_wait(100); // Short delay
for ( i = 0 ; i < len ; i++ )
{
I2C_DXR = data[i]; // Write
timeout = 0x510; // I2C_timeout = 1ms;
USBSTK5515_GPIO_setOutput( 17, 1);
do
{
if ( timeout-- < 0 )
{
USBSTK5515_GPIO_setOutput( 17, 0);
USBSTK5515_I2C_reset( );
return -1;
}
} while ( ( I2C_STR & STR_XRDY ) == 0 );// Wait for Tx Ready
}
I2C_MDR |= MDR_STP; // Generate STOP
USBSTK5515_waitusec(1000);
return 0;
}
/* ------------------------------------------------------------------------ *
* _I2C_read( i2c_addr, data, len ) *
* I2C read in Master mode *
* Returns: 0: PASS *
* -1: FAIL Timeout *
* ------------------------------------------------------------------------ */
Int16 USBSTK5515_I2C_read( Uint16 i2c_addr, Uint8* data, Uint16 len )
{
Int32 timeout, i;
I2C_CNT = len; // Set length
I2C_SAR = i2c_addr; // Set I2C slave address
I2C_MDR = MDR_STT // Set for Master Read
| MDR_MST
| MDR_IRS
| MDR_FREE;
USBSTK5515_wait( 10 ); // Short delay
for ( i = 0 ; i < len ; i++ )
{
timeout = i2c_timeout;
//Wait for Rx Ready
do
{
if ( timeout-- < 0 )
{
USBSTK5515_I2C_reset( );
return -1;
}
} while ( ( I2C_STR & STR_RRDY ) == 0 );// Wait for Rx Ready
data[i] = I2C_DRR; // Read
}
I2C_MDR |= MDR_STP; // Generate STOP
USBSTK5515_waitusec(10);
return 0;
}