LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
Groups
Pages
i2c_18xx_43xx.c
Go to the documentation of this file.
1
/*
2
* @brief LPC18xx/43xx I2C driver
3
*
4
* @note
5
* Copyright(C) NXP Semiconductors, 2012
6
* All rights reserved.
7
*
8
* @par
9
* Software that is described herein is for illustrative purposes only
10
* which provides customers with programming information regarding the
11
* LPC products. This software is supplied "AS IS" without any warranties of
12
* any kind, and NXP Semiconductors and its licensor disclaim any and
13
* all warranties, express or implied, including all implied warranties of
14
* merchantability, fitness for a particular purpose and non-infringement of
15
* intellectual property rights. NXP Semiconductors assumes no responsibility
16
* or liability for the use of the software, conveys no license or rights under any
17
* patent, copyright, mask work right, or any other intellectual property rights in
18
* or to any products. NXP Semiconductors reserves the right to make changes
19
* in the software without notification. NXP Semiconductors also makes no
20
* representation or warranty that such application will be suitable for the
21
* specified use without further testing or modification.
22
*
23
* @par
24
* Permission to use, copy, modify, and distribute this software and its
25
* documentation is hereby granted, under NXP Semiconductors' and its
26
* licensor's relevant copyrights in the software, without fee, provided that it
27
* is used in conjunction with NXP Semiconductors microcontrollers. This
28
* copyright, permission, and disclaimer notice must appear in all copies of
29
* this code.
30
*/
31
32
#include "
i2c_18xx_43xx.h
"
33
#include "
scu_18xx_43xx.h
"
34
35
/*****************************************************************************
36
* Private types/enumerations/variables
37
****************************************************************************/
38
39
#define MAX_TX_BUFFER_SIZE 10
/* Maximum transmit buffer size in Chip_I2C_MasterWriteReg() function */
40
41
static
uint32_t
i2cClockrate
[2];
42
static
I2C_M_SETUP_Type
TransferMCfg
;
43
static
uint8_t
p_regAddr
;
44
static
uint8_t
tx_buffer
[
MAX_TX_BUFFER_SIZE
];
45
46
/*****************************************************************************
47
* Public types/enumerations/variables
48
****************************************************************************/
49
50
/*****************************************************************************
51
* Private functions
52
****************************************************************************/
53
54
/* Determine clock for uart BASED ON SELECTED uart */
55
static
CCU_CLK_T
Chip_I2C_DetermineClk
(
LPC_I2C_Type
*I2Cx) {
56
57
CCU_CLK_T
i2cclk;
58
59
/* Pick clock for uart BASED ON SELECTED uart */
60
if
(I2Cx ==
LPC_I2C0
) {
61
i2cclk =
CLK_APB1_I2C0
;
62
}
63
else
{
64
i2cclk =
CLK_APB3_I2C1
;
65
}
66
67
return
i2cclk;
68
}
69
70
/* Get UART bus number BASED ON SELECTED uart */
71
static
I2C_ID_Type
Chip_I2C_Get_BusNum
(
LPC_I2C_Type
*I2Cx)
72
{
73
if
(I2Cx ==
LPC_I2C1
) {
74
return
I2C1;
75
}
76
77
return
I2C0
;
78
}
79
80
/*****************************************************************************
81
* Public functions
82
****************************************************************************/
83
84
/* Initializes the LPC_I2C peripheral with specified parameter */
85
void
Chip_I2C_Init
(
LPC_I2C_Type
*I2Cx)
86
{
87
/* Enable I2C Clocking */
88
// Chip_Clock_EnableOpts(Chip_I2C_DetermineClk(I2Cx), false, false, 1);
89
Chip_Clock_Enable
(
Chip_I2C_DetermineClk
(I2Cx));
90
91
IP_I2C_Init
(I2Cx);
92
}
93
94
/* De-initializes the I2C peripheral registers to their default reset values */
95
void
Chip_I2C_DeInit
(
LPC_I2C_Type
*I2Cx)
96
{
97
IP_I2C_DeInit
(I2Cx);
98
99
/* Disable UART clocking */
100
Chip_Clock_Disable
(
Chip_I2C_DetermineClk
(I2Cx));
101
}
102
103
/* Set up clock rate for LPC_I2C peripheral */
104
void
Chip_I2C_SetClockRate
(
LPC_I2C_Type
*I2Cx,
uint32_t
clockrate)
105
{
106
if
(I2Cx ==
LPC_I2C0
) {
107
i2cClockrate
[0] = clockrate;
108
/* Select weather standard, fast, fast plus mode*/
109
if
(clockrate >= 1000000) {
/* Fast mode plus: 1MHz, high speed 3.4MHz */
110
Chip_SCU_I2C0PinConfig
(
I2C0_FAST_MODE_PLUS
);
111
}
112
else
{
/* standard 100KHz, fast 400KHz */
113
Chip_SCU_I2C0PinConfig
(
I2C0_STANDARD_FAST_MODE
);
114
}
115
}
116
else
if
(I2Cx ==
LPC_I2C1
) {
117
i2cClockrate
[1] = clockrate;
118
/* Check if I2C1 run fast mode*/
119
if
(clockrate > 400000) {
120
return
;
121
}
122
}
123
else
{
return
; }
124
125
/* Set clock rate */
126
if
(clockrate < 1000) {
/* make sure SCLH,SCLL not exceed its 16bit value */
127
return
;
128
}
129
130
IP_I2C_SetClockRate
(I2Cx, (
Chip_Clock_GetRate
(
Chip_I2C_DetermineClk
(I2Cx)) / clockrate));
131
}
132
133
/* Get current clock rate for LPC_I2C peripheral */
134
uint32_t
Chip_I2C_GetClockRate
(
LPC_I2C_Type
*I2Cx)
135
{
136
if
(I2Cx ==
LPC_I2C0
) {
137
return
i2cClockrate
[0];
138
}
139
else
if
(I2Cx ==
LPC_I2C1
) {
140
return
i2cClockrate
[1];
141
}
142
return
0;
143
}
144
145
/* Transmit and Receive data in master mode */
146
Status
Chip_I2C_MasterTransferData
(
LPC_I2C_Type
*I2Cx,
I2C_M_SETUP_Type
*TransferCfg,
I2C_TRANSFER_OPT_Type
Opt)
147
{
148
I2C_ID_Type
I2C_Num =
Chip_I2C_Get_BusNum
(I2Cx);
149
150
TransferCfg->
retransmissions_max
= 3;
151
TransferCfg->
tx_count
= 0;
152
TransferCfg->
rx_count
= 0;
153
TransferCfg->
retransmissions_count
= 0;
154
155
return
IP_I2C_MasterTransferData
(I2Cx, I2C_Num, TransferCfg, Opt);
156
}
157
158
/* Transmit an array of bytes in Master mode */
159
Status
Chip_I2C_MasterTransmitData
(
LPC_I2C_Type
*I2Cx,
I2C_M_SETUP_Type
*TransferCfg,
I2C_TRANSFER_OPT_Type
Opt)
160
{
161
I2C_ID_Type
I2C_Num =
Chip_I2C_Get_BusNum
(I2Cx);
162
163
TransferCfg->
rx_data
=
NULL
;
164
TransferCfg->
rx_length
= 0;
165
TransferCfg->
retransmissions_max
= 3;
166
TransferCfg->
tx_count
= 0;
167
TransferCfg->
rx_count
= 0;
168
TransferCfg->
retransmissions_count
= 0;
169
170
return
IP_I2C_MasterTransferData
(I2Cx, I2C_Num, TransferCfg, Opt);
171
}
172
173
/* Receive an array of bytes in Master mode */
174
Status
Chip_I2C_MasterReceiveData
(
LPC_I2C_Type
*I2Cx,
I2C_M_SETUP_Type
*TransferCfg,
I2C_TRANSFER_OPT_Type
Opt)
175
{
176
I2C_ID_Type
I2C_Num =
Chip_I2C_Get_BusNum
(I2Cx);
177
178
TransferCfg->
tx_data
=
NULL
;
179
TransferCfg->
tx_length
= 0;
180
TransferCfg->
retransmissions_max
= 3;
181
TransferCfg->
tx_count
= 0;
182
TransferCfg->
rx_count
= 0;
183
TransferCfg->
retransmissions_count
= 0;
184
185
return
IP_I2C_MasterTransferData
(I2Cx, I2C_Num, TransferCfg, Opt);
186
}
187
188
/* Transmit one byte and continue to send an array of bytes
189
* after a repeated start condition is generated in Master mode
190
*/
191
uint32_t
Chip_I2C_MasterWriteReg
(
LPC_I2C_Type
*I2Cx,
192
uint32_t
SlaveAddr,
193
uint8_t regAddr,
194
uint8_t *buffer,
195
uint8_t buffer_len)
196
{
197
uint8_t i = 0;
198
I2C_ID_Type
I2C_Num =
Chip_I2C_Get_BusNum
(I2Cx);
199
200
tx_buffer
[0] = regAddr;
201
202
for
(i = 0; i < buffer_len; i++)
203
tx_buffer
[i + 1] = *(buffer + i);
204
205
TransferMCfg.
sl_addr7bit
= SlaveAddr;
206
TransferMCfg.
tx_data
=
tx_buffer
;
207
TransferMCfg.
tx_length
= buffer_len + 1;
208
TransferMCfg.
rx_data
=
NULL
;
209
TransferMCfg.
rx_length
= 0;
210
TransferMCfg.
retransmissions_max
= 3;
211
TransferMCfg.
tx_count
= 0;
212
TransferMCfg.
rx_count
= 0;
213
TransferMCfg.
retransmissions_count
= 0;
214
IP_I2C_MasterTransferData
(I2Cx, I2C_Num, &TransferMCfg,
I2C_TRANSFER_POLLING
);
215
216
return
TransferMCfg.
tx_count
;
217
}
218
219
/* Transmit one byte and receive an array of bytes after a repeated start condition is generated in Master mode.
220
* This function is useful for communicating with the I2C slave registers
221
*/
222
uint32_t
Chip_I2C_MasterReadReg
(
LPC_I2C_Type
*I2Cx,
223
uint32_t
SlaveAddr,
224
uint8_t regAddr,
225
uint8_t *buffer,
226
uint8_t buffer_len)
227
{
228
I2C_ID_Type
I2C_Num =
Chip_I2C_Get_BusNum
(I2Cx);
229
230
p_regAddr
= regAddr;
231
232
TransferMCfg.
sl_addr7bit
= SlaveAddr;
233
TransferMCfg.
tx_data
= &
p_regAddr
;
234
TransferMCfg.
tx_length
= 1;
235
TransferMCfg.
rx_data
= buffer;
236
TransferMCfg.
rx_length
= buffer_len;
237
TransferMCfg.
retransmissions_max
= 3;
238
TransferMCfg.
tx_count
= 0;
239
TransferMCfg.
rx_count
= 0;
240
TransferMCfg.
retransmissions_count
= 0;
241
IP_I2C_MasterTransferData
(I2Cx, I2C_Num, &TransferMCfg,
I2C_TRANSFER_POLLING
);
242
243
return
TransferMCfg.
rx_count
;
244
}
245
246
/* General Master Interrupt handler for I2C peripheral */
247
void
Chip_I2C_Interrupt_MasterHandler
(
LPC_I2C_Type
*I2Cx)
248
{
249
I2C_ID_Type
I2C_Num =
Chip_I2C_Get_BusNum
(I2Cx);
250
251
IP_I2C_Interrupt_MasterHandler
(I2Cx, I2C_Num);
252
}
253
254
/* Get status of Master Transfer */
255
bool
Chip_I2C_Interrupt_MasterTransferComplete
(
LPC_I2C_Type
*I2Cx)
256
{
257
I2C_ID_Type
I2C_Num =
Chip_I2C_Get_BusNum
(I2Cx);
258
259
return
IP_I2C_Interrupt_MasterTransferComplete
(I2C_Num);
260
}
261
262
/* Receive and Transmit data in slave mode */
263
Status
Chip_I2C_SlaveTransferData
(
LPC_I2C_Type
*I2Cx,
I2C_S_SETUP_Type
*TransferCfg,
I2C_TRANSFER_OPT_Type
Opt)
264
{
265
I2C_ID_Type
I2C_Num =
Chip_I2C_Get_BusNum
(I2Cx);
266
267
TransferCfg->
tx_count
= 0;
268
TransferCfg->
rx_count
= 0;
269
270
return
IP_I2C_SlaveTransferData
(I2Cx, I2C_Num, TransferCfg, Opt);
271
}
272
273
/* Transmit an array of bytes in Slave mode */
274
Status
Chip_I2C_SlaveTransmitData
(
LPC_I2C_Type
*I2Cx,
I2C_S_SETUP_Type
*TransferCfg,
I2C_TRANSFER_OPT_Type
Opt)
275
{
276
I2C_ID_Type
I2C_Num =
Chip_I2C_Get_BusNum
(I2Cx);
277
278
TransferCfg->
tx_count
= 0;
279
TransferCfg->
rx_data
=
NULL
;
280
TransferCfg->
rx_length
= 0;
281
TransferCfg->
rx_count
= 0;
282
283
return
IP_I2C_SlaveTransferData
(I2Cx, I2C_Num, TransferCfg, Opt);
284
}
285
286
/* Receive an array of bytes in Slave mode */
287
Status
Chip_I2C_SlaveReceiveData
(
LPC_I2C_Type
*I2Cx,
I2C_S_SETUP_Type
*TransferCfg,
I2C_TRANSFER_OPT_Type
Opt)
288
{
289
I2C_ID_Type
I2C_Num =
Chip_I2C_Get_BusNum
(I2Cx);
290
291
TransferCfg->
tx_data
=
NULL
;
292
TransferCfg->
tx_length
= 0;
293
TransferCfg->
tx_count
= 0;
294
TransferCfg->
rx_count
= 0;
295
296
return
IP_I2C_SlaveTransferData
(I2Cx, I2C_Num, TransferCfg, Opt);
297
}
298
299
/* General Slave Interrupt handler for I2C peripheral */
300
void
Chip_I2C_Interrupt_SlaveHandler
(
LPC_I2C_Type
*I2Cx)
301
{
302
I2C_ID_Type
I2C_Num =
Chip_I2C_Get_BusNum
(I2Cx);
303
304
IP_I2C_Interrupt_SlaveHandler
(I2Cx, I2C_Num);
305
}
306
307
/* Get status of Slave Transfer */
308
bool
Chip_I2C_Interrupt_SlaveTransferComplete
(
LPC_I2C_Type
*I2Cx)
309
{
310
I2C_ID_Type
I2C_Num =
Chip_I2C_Get_BusNum
(I2Cx);
311
312
return
IP_I2C_Interrupt_SlaveTransferComplete
(I2C_Num);
313
}
software
lpc_core
lpc_chip
chip_18xx_43xx
i2c_18xx_43xx.c
Generated on Fri Nov 16 2012 13:36:41 for LPCOpen Platform by
1.8.2