1 /******************************************************************************
2 *  Filename:       i2c.c
3 *  Revised:        2015-11-16 19:41:47 +0100 (Mon, 16 Nov 2015)
4 *  Revision:       45094
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 //
60 //! Initializes the I2C Master block
61 //
62 //*****************************************************************************
63 void
I2CMasterInitExpClk(uint32_t ui32Base,uint32_t ui32I2CClk,bool bFast)64 I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk,
65                     bool bFast)
66 {
67     uint32_t ui32SCLFreq;
68     uint32_t ui32TPR;
69 
70     //
71     // Check the arguments.
72     //
73     ASSERT(I2CBaseValid(ui32Base));
74 
75     //
76     // Must enable the device before doing anything else.
77     //
78     I2CMasterEnable(I2C0_BASE);
79 
80     //
81     // Get the desired SCL speed.
82     //
83     if(bFast == true)
84     {
85         ui32SCLFreq = 400000;
86     }
87     else
88     {
89         ui32SCLFreq = 100000;
90     }
91 
92     //
93     // Compute the clock divider that achieves the fastest speed less than or
94     // equal to the desired speed. The numerator is biased to favor a larger
95     // clock divider so that the resulting clock is always less than or equal
96     // to the desired clock, never greater.
97     //
98     ui32TPR = ((ui32I2CClk + (2 * 10 * ui32SCLFreq) - 1) / (2 * 10 * ui32SCLFreq)) - 1;
99     HWREG(I2C0_BASE + I2C_O_MTPR) = ui32TPR;
100 }
101 
102 //*****************************************************************************
103 //
104 //! Gets the error status of the I2C Master module
105 //
106 //*****************************************************************************
107 uint32_t
I2CMasterErr(uint32_t ui32Base)108 I2CMasterErr(uint32_t ui32Base)
109 {
110     uint32_t ui32Err;
111 
112     //
113     // Check the arguments.
114     //
115     ASSERT(I2CBaseValid(ui32Base));
116 
117     //
118     // Get the raw error state.
119     //
120     ui32Err = HWREG(I2C0_BASE + I2C_O_MSTAT);
121 
122     //
123     // If the I2C master is busy, then all the other status bits are invalid,
124     // and there is no error to report.
125     //
126     if(ui32Err & I2C_MSTAT_BUSY)
127     {
128         return(I2C_MASTER_ERR_NONE);
129     }
130 
131     //
132     // Check for errors.
133     //
134     if(ui32Err & (I2C_MSTAT_ERR | I2C_MSTAT_ARBLST))
135     {
136         return(ui32Err & (I2C_MSTAT_ARBLST | I2C_MSTAT_DATACK_N | I2C_MSTAT_ADRACK_N));
137     }
138     else
139     {
140         return(I2C_MASTER_ERR_NONE);
141     }
142 }
143 
144 //*****************************************************************************
145 //
146 //! Registers an interrupt handler for the I2C module
147 //
148 //*****************************************************************************
149 void
I2CIntRegister(uint32_t ui32Base,void (* pfnHandler)(void))150 I2CIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
151 {
152     uint32_t ui32Int;
153 
154     //
155     // Check the arguments.
156     //
157     ASSERT(I2CBaseValid(ui32Base));
158 
159     //
160     // Get the interrupt number.
161     //
162     ui32Int = INT_I2C_IRQ;
163 
164     //
165     // Register the interrupt handler, returning an error if an error occurs.
166     //
167     IntRegister(ui32Int, pfnHandler);
168 
169     //
170     // Enable the I2C interrupt.
171     //
172     IntEnable(ui32Int);
173 }
174 
175 //*****************************************************************************
176 //
177 //! Unregisters an interrupt handler for the I2C module
178 //
179 //*****************************************************************************
180 void
I2CIntUnregister(uint32_t ui32Base)181 I2CIntUnregister(uint32_t ui32Base)
182 {
183     uint32_t ui32Int;
184 
185     //
186     // Check the arguments.
187     //
188     ASSERT(I2CBaseValid(ui32Base));
189 
190     //
191     // Get the interrupt number.
192     //
193     ui32Int = INT_I2C_IRQ;
194 
195     //
196     // Disable the interrupt.
197     //
198     IntDisable(ui32Int);
199 
200     //
201     // Unregister the interrupt handler.
202     //
203     IntUnregister(ui32Int);
204 }
205