CC13xx Driver Library
i2c.c
Go to the documentation of this file.
1 /******************************************************************************
2 * Filename: i2c.c
3 * Revised: 2015-05-18 13:52:35 +0200 (Mon, 18 May 2015)
4 * Revision: 43512
5 *
6 * Description: Driver for the I2C module
7 *
8 * Copyright (c) 2015, Texas Instruments Incorporated
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * 1) Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 *
17 * 2) Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 *
21 * 3) Neither the name of the ORGANIZATION nor the names of its contributors may
22 * be used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 ******************************************************************************/
38 
39 #include <driverlib/i2c.h>
40 
41 //*****************************************************************************
42 //
43 // Handle support for DriverLib in ROM:
44 // This section will undo prototype renaming made in the header file
45 //
46 //*****************************************************************************
47 #if !defined(DOXYGEN)
48  #undef I2CMasterInitExpClk
49  #define I2CMasterInitExpClk NOROM_I2CMasterInitExpClk
50  #undef I2CMasterErr
51  #define I2CMasterErr NOROM_I2CMasterErr
52  #undef I2CIntRegister
53  #define I2CIntRegister NOROM_I2CIntRegister
54  #undef I2CIntUnregister
55  #define I2CIntUnregister NOROM_I2CIntUnregister
56 #endif
57 
58 //*****************************************************************************
59 //
61 //
62 //*****************************************************************************
63 
64 void
65 I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk,
66  bool bFast)
67 {
68  uint32_t ui32SCLFreq;
69  uint32_t ui32TPR;
70 
71  //
72  // Check the arguments.
73  //
74  ASSERT(I2CBaseValid(ui32Base));
75 
76  //
77  // Must enable the device before doing anything else.
78  //
80 
81  //
82  // Get the desired SCL speed.
83  //
84  if(bFast == true)
85  {
86  ui32SCLFreq = 400000;
87  }
88  else
89  {
90  ui32SCLFreq = 100000;
91  }
92 
93  //
94  // Compute the clock divider that achieves the fastest speed less than or
95  // equal to the desired speed. The numerator is biased to favor a larger
96  // clock divider so that the resulting clock is always less than or equal
97  // to the desired clock, never greater.
98  //
99  ui32TPR = ((ui32I2CClk + (2 * 10 * ui32SCLFreq) - 1) / (2 * 10 * ui32SCLFreq)) - 1;
100  HWREG(I2C0_BASE + I2C_O_MTPR) = ui32TPR;
101 }
102 
103 //*****************************************************************************
104 //
106 //
107 //*****************************************************************************
108 
109 uint32_t
110 I2CMasterErr(uint32_t ui32Base)
111 {
112  uint32_t ui32Err;
113 
114  //
115  // Check the arguments.
116  //
117  ASSERT(I2CBaseValid(ui32Base));
118 
119  //
120  // Get the raw error state.
121  //
122  ui32Err = HWREG(I2C0_BASE + I2C_O_MSTAT);
123 
124  //
125  // If the I2C master is busy, then all the other status bits are invalid,
126  // and there is no error to report.
127  //
128  if(ui32Err & I2C_MSTAT_BUSY)
129  {
130  return(I2C_MASTER_ERR_NONE);
131  }
132 
133  //
134  // Check for errors.
135  //
136  if(ui32Err & (I2C_MSTAT_ERR | I2C_MSTAT_ARBLST))
137  {
139  }
140  else
141  {
142  return(I2C_MASTER_ERR_NONE);
143  }
144 }
145 
146 //*****************************************************************************
147 //
149 //
150 //*****************************************************************************
151 void
152 I2CIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
153 {
154  uint32_t ui32Int;
155 
156  //
157  // Check the arguments.
158  //
159  ASSERT(I2CBaseValid(ui32Base));
160 
161  //
162  // Get the interrupt number.
163  //
164  ui32Int = INT_I2C;
165 
166  //
167  // Register the interrupt handler, returning an error if an error occurs.
168  //
169  IntRegister(ui32Int, pfnHandler);
170 
171  //
172  // Enable the I2C interrupt.
173  //
174  IntEnable(ui32Int);
175 }
176 
177 //*****************************************************************************
178 //
180 //
181 //*****************************************************************************
182 void
183 I2CIntUnregister(uint32_t ui32Base)
184 {
185  uint32_t ui32Int;
186 
187  //
188  // Check the arguments.
189  //
190  ASSERT(I2CBaseValid(ui32Base));
191 
192  //
193  // Get the interrupt number.
194  //
195  ui32Int = INT_I2C;
196 
197  //
198  // Disable the interrupt.
199  //
200  IntDisable(ui32Int);
201 
202  //
203  // Unregister the interrupt handler.
204  //
205  IntUnregister(ui32Int);
206 }
static void I2CMasterEnable(uint32_t ui32Base)
Enables the I2C Master block.
Definition: i2c.h:300
#define I2C_MASTER_ERR_NONE
Definition: i2c.h:124
void I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk, bool bFast)
Initializes the I2C Master block.
Definition: i2c.c:65
#define ASSERT(expr)
Definition: debug.h:74
uint32_t I2CMasterErr(uint32_t ui32Base)
Gets the error status of the I2C Master module.
Definition: i2c.c:110
void IntUnregister(uint32_t ui32Interrupt)
Unregisters the function to be called when an interrupt occurs.
Definition: interrupt.c:200
void I2CIntRegister(uint32_t ui32Base, void(*pfnHandler)(void))
Registers an interrupt handler for the I2C module.
Definition: i2c.c:152
void IntDisable(uint32_t ui32Interrupt)
Disables an interrupt.
Definition: interrupt.c:378
void IntRegister(uint32_t ui32Interrupt, void(*pfnHandler)(void))
Registers a function to be called when an interrupt occurs.
Definition: interrupt.c:152
void I2CIntUnregister(uint32_t ui32Base)
Unregisters an interrupt handler for the I2C module.
Definition: i2c.c:183
void IntEnable(uint32_t ui32Interrupt)
Enables an interrupt.
Definition: interrupt.c:318