1 /******************************************************************************
2 * Filename: i2c.h
3 * Revised: 2017-05-23 12:08:52 +0200 (Tue, 23 May 2017)
4 * Revision: 49048
5 *
6 * Description: Defines and prototypes for the I2C.
7 *
8 * Copyright (c) 2015 - 2017, 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 //*****************************************************************************
40 //
41 //! \addtogroup peripheral_group
42 //! @{
43 //! \addtogroup i2c_api
44 //! @{
45 //
46 //*****************************************************************************
47
48 #ifndef __I2C_H__
49 #define __I2C_H__
50
51 //*****************************************************************************
52 //
53 // If building with a C++ compiler, make all of the definitions in this header
54 // have a C binding.
55 //
56 //*****************************************************************************
57 #ifdef __cplusplus
58 extern "C"
59 {
60 #endif
61
62 #include <stdbool.h>
63 #include <stdint.h>
64 #include "../inc/hw_types.h"
65 #include "../inc/hw_ints.h"
66 #include "../inc/hw_memmap.h"
67 #include "../inc/hw_i2c.h"
68 #include "../inc/hw_sysctl.h"
69 #include "debug.h"
70 #include "interrupt.h"
71 #include "cpu.h"
72
73 //*****************************************************************************
74 //
75 // Support for DriverLib in ROM:
76 // This section renames all functions that are not "static inline", so that
77 // calling these functions will default to implementation in flash. At the end
78 // of this file a second renaming will change the defaults to implementation in
79 // ROM for available functions.
80 //
81 // To force use of the implementation in flash, e.g. for debugging:
82 // - Globally: Define DRIVERLIB_NOROM at project level
83 // - Per function: Use prefix "NOROM_" when calling the function
84 //
85 //*****************************************************************************
86 #if !defined(DOXYGEN)
87 #define I2CMasterInitExpClk NOROM_I2CMasterInitExpClk
88 #define I2CMasterErr NOROM_I2CMasterErr
89 #define I2CIntRegister NOROM_I2CIntRegister
90 #define I2CIntUnregister NOROM_I2CIntUnregister
91 #endif
92
93 //*****************************************************************************
94 //
95 // I2C Master commands
96 //
97 //*****************************************************************************
98 #define I2C_MASTER_CMD_SINGLE_SEND \
99 0x00000007
100 #define I2C_MASTER_CMD_SINGLE_RECEIVE \
101 0x00000007
102 #define I2C_MASTER_CMD_BURST_SEND_START \
103 0x00000003
104 #define I2C_MASTER_CMD_BURST_SEND_CONT \
105 0x00000001
106 #define I2C_MASTER_CMD_BURST_SEND_FINISH \
107 0x00000005
108 #define I2C_MASTER_CMD_BURST_SEND_ERROR_STOP \
109 0x00000004
110 #define I2C_MASTER_CMD_BURST_RECEIVE_START \
111 0x0000000b
112 #define I2C_MASTER_CMD_BURST_RECEIVE_CONT \
113 0x00000009
114 #define I2C_MASTER_CMD_BURST_RECEIVE_FINISH \
115 0x00000005
116 #define I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP \
117 0x00000004
118
119 //*****************************************************************************
120 //
121 // I2C Master error status
122 //
123 //*****************************************************************************
124 #define I2C_MASTER_ERR_NONE 0
125 #define I2C_MASTER_ERR_ADDR_ACK 0x00000004
126 #define I2C_MASTER_ERR_DATA_ACK 0x00000008
127 #define I2C_MASTER_ERR_ARB_LOST 0x00000010
128
129 //*****************************************************************************
130 //
131 // I2C Slave action requests
132 //
133 //*****************************************************************************
134 #define I2C_SLAVE_ACT_NONE 0
135 #define I2C_SLAVE_ACT_RREQ 0x00000001 // Master has sent data
136 #define I2C_SLAVE_ACT_TREQ 0x00000002 // Master has requested data
137 #define I2C_SLAVE_ACT_RREQ_FBR 0x00000005 // Master has sent first byte
138
139 //*****************************************************************************
140 //
141 // I2C Slave interrupts
142 //
143 //*****************************************************************************
144 #define I2C_SLAVE_INT_STOP 0x00000004 // Stop Condition Interrupt.
145 #define I2C_SLAVE_INT_START 0x00000002 // Start Condition Interrupt.
146 #define I2C_SLAVE_INT_DATA 0x00000001 // Data Interrupt.
147
148 //*****************************************************************************
149 //
150 // API Functions and prototypes
151 //
152 //*****************************************************************************
153
154 #ifdef DRIVERLIB_DEBUG
155 //*****************************************************************************
156 //
157 //! \internal
158 //!
159 //! \brief Checks an I2C base address.
160 //!
161 //! This function determines if a I2C port base address is valid.
162 //!
163 //! \param ui32Base is the base address of the I2C port.
164 //!
165 //! \return Returns \c true if the base address is valid and \c false
166 //! otherwise
167 //
168 //*****************************************************************************
169 static bool
I2CBaseValid(uint32_t ui32Base)170 I2CBaseValid(uint32_t ui32Base)
171 {
172 return(ui32Base == I2C0_BASE);
173 }
174 #endif
175
176 //*****************************************************************************
177 //
178 //! \brief Initializes the I2C Master block.
179 //!
180 //! This function initializes operation of the I2C Master block. Upon
181 //! successful initialization of the I2C block, this function will have set the
182 //! bus speed for the master, and will have enabled the I2C Master block.
183 //!
184 //! If the parameter \c bFast is \c true, then the master block will be set up
185 //! to transfer data at 400 kbps; otherwise, it will be set up to transfer data
186 //! at 100 kbps.
187 //!
188 //!
189 //! \param ui32Base is the base address of the I2C module.
190 //! \param ui32I2CClk is the rate of the clock supplied to the I2C module.
191 //! \param bFast set up for fast data transfers.
192 //!
193 //! \return None
194 //
195 //*****************************************************************************
196 extern void I2CMasterInitExpClk(uint32_t ui32Base, uint32_t ui32I2CClk,
197 bool bFast);
198
199 //*****************************************************************************
200 //
201 //! \brief Controls the state of the I2C Master module.
202 //!
203 //! This function is used to control the state of the Master module send and
204 //! receive operations.
205 //!
206 //! \param ui32Base is the base address of the I2C module.
207 //! \param ui32Cmd is the command to be issued by the I2C Master module
208 //! The parameter can be one of the following values:
209 //! - \ref I2C_MASTER_CMD_SINGLE_SEND
210 //! - \ref I2C_MASTER_CMD_SINGLE_RECEIVE
211 //! - \ref I2C_MASTER_CMD_BURST_SEND_START
212 //! - \ref I2C_MASTER_CMD_BURST_SEND_CONT
213 //! - \ref I2C_MASTER_CMD_BURST_SEND_FINISH
214 //! - \ref I2C_MASTER_CMD_BURST_SEND_ERROR_STOP
215 //! - \ref I2C_MASTER_CMD_BURST_RECEIVE_START
216 //! - \ref I2C_MASTER_CMD_BURST_RECEIVE_CONT
217 //! - \ref I2C_MASTER_CMD_BURST_RECEIVE_FINISH
218 //! - \ref I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP
219 //!
220 //! \return None
221 //
222 //*****************************************************************************
223 __STATIC_INLINE void
I2CMasterControl(uint32_t ui32Base,uint32_t ui32Cmd)224 I2CMasterControl(uint32_t ui32Base, uint32_t ui32Cmd)
225 {
226 // Check the arguments.
227 ASSERT(I2CBaseValid(ui32Base));
228 ASSERT((ui32Cmd == I2C_MASTER_CMD_SINGLE_SEND) ||
229 // (ui32Cmd == I2C_MASTER_CMD_SINGLE_RECEIVE) || -> Equal to SINGLE_SEND
230 (ui32Cmd == I2C_MASTER_CMD_BURST_SEND_START) ||
231 (ui32Cmd == I2C_MASTER_CMD_BURST_SEND_CONT) ||
232 (ui32Cmd == I2C_MASTER_CMD_BURST_SEND_FINISH) ||
233 (ui32Cmd == I2C_MASTER_CMD_BURST_SEND_ERROR_STOP) ||
234 (ui32Cmd == I2C_MASTER_CMD_BURST_RECEIVE_START) ||
235 (ui32Cmd == I2C_MASTER_CMD_BURST_RECEIVE_CONT) ||
236 (ui32Cmd == I2C_MASTER_CMD_BURST_RECEIVE_FINISH) ||
237 (ui32Cmd == I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP));
238
239 // Send the command.
240 HWREG(I2C0_BASE + I2C_O_MCTRL) = ui32Cmd;
241
242 // Delay minimum four cycles in order to ensure that the I2C_O_MSTAT
243 // register has been correctly updated before function exit
244 CPUdelay(2);
245 }
246
247 //*****************************************************************************
248 //
249 //! \brief Sets the address that the I2C Master will place on the bus.
250 //!
251 //! This function will set the address that the I2C Master will place on the
252 //! bus when initiating a transaction. When the \e bReceive parameter is set
253 //! to \b true, the address will indicate that the I2C Master is initiating a
254 //! read from the slave; otherwise the address will indicate that the I2C
255 //! Master is initiating a write to the slave.
256 //!
257 //! \param ui32Base is the base address of the I2C module.
258 //! \param ui8SlaveAddr is a 7-bit slave address
259 //! \param bReceive flag indicates the type of communication with the slave.
260 //! - \c true : I2C Master is initiating a read from the slave.
261 //! - \c false : I2C Master is initiating a write to the slave.
262 //!
263 //! \return None
264 //
265 //*****************************************************************************
266 __STATIC_INLINE void
I2CMasterSlaveAddrSet(uint32_t ui32Base,uint8_t ui8SlaveAddr,bool bReceive)267 I2CMasterSlaveAddrSet(uint32_t ui32Base, uint8_t ui8SlaveAddr,
268 bool bReceive)
269 {
270 // Check the arguments.
271 ASSERT(I2CBaseValid(ui32Base));
272 ASSERT(!(ui8SlaveAddr & 0x80));
273
274 // Set the address of the slave with which the master will communicate.
275 HWREG(I2C0_BASE + I2C_O_MSA) = (ui8SlaveAddr << 1) | bReceive;
276 }
277
278 //*****************************************************************************
279 //
280 //! \brief Enables the I2C Master block.
281 //!
282 //! This will enable operation of the I2C Master block.
283 //!
284 //! \param ui32Base is the base address of the I2C module.
285 //!
286 //! \return None
287 //
288 //*****************************************************************************
289 __STATIC_INLINE void
I2CMasterEnable(uint32_t ui32Base)290 I2CMasterEnable(uint32_t ui32Base)
291 {
292 // Check the arguments.
293 ASSERT(I2CBaseValid(ui32Base));
294
295 // Enable the clock for the master.
296 HWREGBITW(I2C0_BASE + I2C_O_MCR, I2C_MCR_MFE_BITN) = 1;
297
298 // Enable the master block.
299 HWREG(I2C0_BASE + I2C_O_MCTRL) = I2C_MCTRL_RUN;
300 }
301
302 //*****************************************************************************
303 //
304 //! \brief Disables the I2C master block.
305 //!
306 //! This will disable operation of the I2C master block.
307 //!
308 //! \param ui32Base is the base address of the I2C module.
309 //!
310 //! \return None
311 //
312 //*****************************************************************************
313 __STATIC_INLINE void
I2CMasterDisable(uint32_t ui32Base)314 I2CMasterDisable(uint32_t ui32Base)
315 {
316 // Check the arguments.
317 ASSERT(I2CBaseValid(ui32Base));
318
319 // Disable the master block.
320 HWREG(I2C0_BASE + I2C_O_MCTRL) = 0;
321
322 // Disable the clock for the master.
323 HWREGBITW(I2C0_BASE + I2C_O_MCR, I2C_MCR_MFE_BITN) = 0;
324 }
325
326 //*****************************************************************************
327 //
328 //! \brief Indicates whether or not the I2C Master is busy.
329 //!
330 //! This function returns an indication of whether or not the I2C Master is
331 //! busy transmitting or receiving data.
332 //!
333 //! \param ui32Base is the base address of the I2C module.
334 //!
335 //! \return Returns status of I2C Master:
336 //! - \c true : I2C Master is busy.
337 //! - \c false : I2C Master is not busy.
338 //
339 //*****************************************************************************
340 __STATIC_INLINE bool
I2CMasterBusy(uint32_t ui32Base)341 I2CMasterBusy(uint32_t ui32Base)
342 {
343 // Check the arguments.
344 ASSERT(I2CBaseValid(ui32Base));
345
346 // Return the busy status.
347 if(HWREG(I2C0_BASE + I2C_O_MSTAT) & I2C_MSTAT_BUSY)
348 {
349 return(true);
350 }
351 else
352 {
353 return(false);
354 }
355 }
356
357 //*****************************************************************************
358 //
359 //! \brief Indicates whether or not the I2C bus is busy.
360 //!
361 //! This function returns an indication of whether or not the I2C bus is busy.
362 //! This function can be used in a multi-master environment to determine if
363 //! another master is currently using the bus.
364 //!
365 //! \param ui32Base is the base address of the I2C module.
366 //!
367 //! \return Returns status of the I2C bus:
368 //! - \c true : I2C bus is busy.
369 //! - \c false : I2C bus is not busy.
370 //
371 //*****************************************************************************
372 __STATIC_INLINE bool
I2CMasterBusBusy(uint32_t ui32Base)373 I2CMasterBusBusy(uint32_t ui32Base)
374 {
375 // Check the arguments.
376 ASSERT(I2CBaseValid(ui32Base));
377
378 // Return the bus busy status.
379 if(HWREG(I2C0_BASE + I2C_O_MSTAT) & I2C_MSTAT_BUSBSY)
380 {
381 return(true);
382 }
383 else
384 {
385 return(false);
386 }
387 }
388
389 //*****************************************************************************
390 //
391 //! \brief Receives a byte that has been sent to the I2C Master.
392 //!
393 //! This function reads a byte of data from the I2C Master Data Register.
394 //!
395 //! \param ui32Base is the base address of the I2C module.
396 //!
397 //! \return Returns the byte received from by the I2C Master, cast as an
398 //! uint32_t.
399 //
400 //*****************************************************************************
401 __STATIC_INLINE uint32_t
I2CMasterDataGet(uint32_t ui32Base)402 I2CMasterDataGet(uint32_t ui32Base)
403 {
404 // Check the arguments.
405 ASSERT(I2CBaseValid(ui32Base));
406
407 // Read a byte.
408 return(HWREG(I2C0_BASE + I2C_O_MDR));
409 }
410
411 //*****************************************************************************
412 //
413 //! \brief Transmits a byte from the I2C Master.
414 //!
415 //! This function will place the supplied data into I2C Master Data Register.
416 //!
417 //! \param ui32Base is the base address of the I2C module.
418 //! \param ui8Data is the data to be transmitted by the I2C Master
419 //!
420 //! \return None
421 //
422 //*****************************************************************************
423 __STATIC_INLINE void
I2CMasterDataPut(uint32_t ui32Base,uint8_t ui8Data)424 I2CMasterDataPut(uint32_t ui32Base, uint8_t ui8Data)
425 {
426 // Check the arguments.
427 ASSERT(I2CBaseValid(ui32Base));
428
429 // Write the byte.
430 HWREG(I2C0_BASE + I2C_O_MDR) = ui8Data;
431 }
432
433 //*****************************************************************************
434 //
435 //! \brief Gets the error status of the I2C Master module.
436 //!
437 //! This function is used to obtain the error status of the Master module send
438 //! and receive operations.
439 //!
440 //! \param ui32Base is the base address of the I2C module.
441 //!
442 //! \return Returns the error status of the Master module:
443 //! - \ref I2C_MASTER_ERR_NONE
444 //! - \ref I2C_MASTER_ERR_ADDR_ACK
445 //! - \ref I2C_MASTER_ERR_DATA_ACK
446 //! - \ref I2C_MASTER_ERR_ARB_LOST
447 //
448 //*****************************************************************************
449 extern uint32_t I2CMasterErr(uint32_t ui32Base);
450
451 //*****************************************************************************
452 //
453 //! \brief Enables the I2C Master interrupt.
454 //!
455 //! Enables the I2C Master interrupt source.
456 //!
457 //! \param ui32Base is the base address of the I2C module.
458 //!
459 //! \return None
460 //
461 //*****************************************************************************
462 __STATIC_INLINE void
I2CMasterIntEnable(uint32_t ui32Base)463 I2CMasterIntEnable(uint32_t ui32Base)
464 {
465 // Check the arguments.
466 ASSERT(I2CBaseValid(ui32Base));
467
468 // Enable the master interrupt.
469 HWREG(I2C0_BASE + I2C_O_MIMR) = I2C_MIMR_IM;
470 }
471
472 //*****************************************************************************
473 //
474 //! \brief Disables the I2C Master interrupt.
475 //!
476 //! Disables the I2C Master interrupt source.
477 //!
478 //! \param ui32Base is the base address of the I2C module.
479 //!
480 //! \return None
481 //
482 //*****************************************************************************
483 __STATIC_INLINE void
I2CMasterIntDisable(uint32_t ui32Base)484 I2CMasterIntDisable(uint32_t ui32Base)
485 {
486 // Check the arguments.
487 ASSERT(I2CBaseValid(ui32Base));
488
489 // Disable the master interrupt.
490 HWREG(I2C0_BASE + I2C_O_MIMR) = 0;
491 }
492
493 //*****************************************************************************
494 //
495 //! \brief Clears I2C Master interrupt sources.
496 //!
497 //! The I2C Master interrupt source is cleared, so that it no longer asserts.
498 //! This must be done in the interrupt handler to keep it from being called
499 //! again immediately upon exit.
500 //!
501 //! \note Due to write buffers and synchronizers in the system it may take several
502 //! clock cycles from a register write clearing an event in a module and until the
503 //! event is actually cleared in the NVIC of the system CPU. It is recommended to
504 //! clear the event source early in the interrupt service routine (ISR) to allow
505 //! the event clear to propagate to the NVIC before returning from the ISR.
506 //! At the same time, an early event clear allows new events of the same type to be
507 //! pended instead of ignored if the event is cleared later in the ISR.
508 //! It is the responsibility of the programmer to make sure that enough time has passed
509 //! before returning from the ISR to avoid false re-triggering of the cleared event.
510 //! A simple, although not necessarily optimal, way of clearing an event before
511 //! returning from the ISR is:
512 //! -# Write to clear event (interrupt source). (buffered write)
513 //! -# Dummy read from the event source module. (making sure the write has propagated)
514 //! -# Wait two system CPU clock cycles (user code or two NOPs). (allowing cleared event to propagate through any synchronizers)
515 //!
516 //! \param ui32Base is the base address of the I2C module.
517 //!
518 //! \return None
519 //
520 //*****************************************************************************
521 __STATIC_INLINE void
I2CMasterIntClear(uint32_t ui32Base)522 I2CMasterIntClear(uint32_t ui32Base)
523 {
524 // Check the arguments.
525 ASSERT(I2CBaseValid(ui32Base));
526
527 // Clear the I2C master interrupt source.
528 HWREG(I2C0_BASE + I2C_O_MICR) = I2C_MICR_IC;
529 }
530
531 //*****************************************************************************
532 //
533 //! \brief Gets the current I2C Master interrupt status.
534 //!
535 //! This returns the interrupt status for the I2C Master module. Either the
536 //! raw interrupt status or the status of interrupts that are allowed to
537 //! reflect to the processor can be returned.
538 //!
539 //! \param ui32Base is the base address of the I2C Master module.
540 //! \param bMasked selects either raw or masked interrupt status.
541 //! - \c false : Raw interrupt status is requested.
542 //! - \c true : Masked interrupt status is requested.
543 //!
544 //! \return Returns the current interrupt status.
545 //! - \c true : Active.
546 //! - \c false : Not active.
547 //
548 //*****************************************************************************
549 __STATIC_INLINE bool
I2CMasterIntStatus(uint32_t ui32Base,bool bMasked)550 I2CMasterIntStatus(uint32_t ui32Base, bool bMasked)
551 {
552 // Check the arguments.
553 ASSERT(I2CBaseValid(ui32Base));
554
555 // Return either the interrupt status or the raw interrupt status as
556 // requested.
557 if(bMasked)
558 {
559 return((HWREG(I2C0_BASE + I2C_O_MMIS)) ? true : false);
560 }
561 else
562 {
563 return((HWREG(I2C0_BASE + I2C_O_MRIS)) ? true : false);
564 }
565 }
566
567 //*****************************************************************************
568 //
569 //! \brief Enables the I2C Slave block.
570 //!
571 //! This will enable operation of the I2C Slave block.
572 //!
573 //! \param ui32Base is the base address of the I2C Slave module.
574 //!
575 //! \return None
576 //
577 //*****************************************************************************
578 __STATIC_INLINE void
I2CSlaveEnable(uint32_t ui32Base)579 I2CSlaveEnable(uint32_t ui32Base)
580 {
581 // Check the arguments.
582 ASSERT(I2CBaseValid(ui32Base));
583
584 // Enable the clock to the slave block.
585 HWREGBITW(I2C0_BASE + I2C_O_MCR, I2C_MCR_SFE_BITN) = 1;
586
587 // Enable the slave.
588 HWREG(I2C0_BASE + I2C_O_SCTL) = I2C_SCTL_DA;
589 }
590
591 //*****************************************************************************
592 //
593 //! \brief Initializes the I2C Slave block.
594 //!
595 //! This function initializes operation of the I2C Slave block. Upon
596 //! successful initialization of the I2C blocks, this function will have set
597 //! the slave address and have enabled the I2C Slave block.
598 //!
599 //! The parameter \c ui8SlaveAddr is the value that will be compared against the
600 //! slave address sent by an I2C master.
601 //!
602 //! \param ui32Base is the base address of the I2C Slave module.
603 //! \param ui8SlaveAddr is the 7-bit slave address.
604 //!
605 //! \return None
606 //
607 //*****************************************************************************
608 __STATIC_INLINE void
I2CSlaveInit(uint32_t ui32Base,uint8_t ui8SlaveAddr)609 I2CSlaveInit(uint32_t ui32Base, uint8_t ui8SlaveAddr)
610 {
611 // Check the arguments.
612 ASSERT(I2CBaseValid(ui32Base));
613 ASSERT(!(ui8SlaveAddr & 0x80));
614
615 // Must enable the device before doing anything else.
616 I2CSlaveEnable(I2C0_BASE);
617
618 // Set up the slave address.
619 HWREG(I2C0_BASE + I2C_O_SOAR) = ui8SlaveAddr;
620 }
621
622 //*****************************************************************************
623 //
624 //! \brief Sets the I2C slave address.
625 //!
626 //! This function writes the specified slave address.
627 //!
628 //! \param ui32Base is the base address of the I2C Slave module.
629 //! \param ui8SlaveAddr is the 7-bit slave address
630 //!
631 //! \return None.
632 //
633 //*****************************************************************************
634 __STATIC_INLINE void
I2CSlaveAddressSet(uint32_t ui32Base,uint8_t ui8SlaveAddr)635 I2CSlaveAddressSet(uint32_t ui32Base, uint8_t ui8SlaveAddr)
636 {
637 // Check the arguments.
638 ASSERT(I2CBaseValid(ui32Base));
639 ASSERT(!(ui8SlaveAddr & 0x80));
640
641 // Set up the primary slave address.
642 HWREG(I2C0_BASE + I2C_O_SOAR) = ui8SlaveAddr;
643 }
644
645 //*****************************************************************************
646 //
647 //! \brief Disables the I2C slave block.
648 //!
649 //! This will disable operation of the I2C slave block.
650 //!
651 //! \param ui32Base is the base address of the I2C Slave module.
652 //!
653 //! \return None
654 //
655 //*****************************************************************************
656 __STATIC_INLINE void
I2CSlaveDisable(uint32_t ui32Base)657 I2CSlaveDisable(uint32_t ui32Base)
658 {
659 // Check the arguments.
660 ASSERT(I2CBaseValid(ui32Base));
661
662 // Disable the slave.
663 HWREG(I2C0_BASE + I2C_O_SCTL) = 0x0;
664
665 // Disable the clock to the slave block.
666 HWREGBITW(I2C0_BASE + I2C_O_MCR, I2C_MCR_SFE_BITN) = 0;
667 }
668
669 //*****************************************************************************
670 //
671 //! \brief Gets the I2C Slave module status.
672 //!
673 //! This function will return the action requested from a master, if any.
674 //!
675 //! \param ui32Base is the base address of the I2C Slave module.
676 //!
677 //! \return Returns the status of the I2C Slave module:
678 //! - \ref I2C_SLAVE_ACT_NONE : No action has been requested of the I2C Slave module.
679 //! - \ref I2C_SLAVE_ACT_RREQ : An I2C master has sent data to the I2C Slave module.
680 //! - \ref I2C_SLAVE_ACT_TREQ : An I2C master has requested that the I2C Slave module send data.
681 //! - \ref I2C_SLAVE_ACT_RREQ_FBR : An I2C master has sent data to the I2C slave
682 //! and the first byte following the slave's own address has been received.
683 //
684 //*****************************************************************************
685 __STATIC_INLINE uint32_t
I2CSlaveStatus(uint32_t ui32Base)686 I2CSlaveStatus(uint32_t ui32Base)
687 {
688 // Check the arguments.
689 ASSERT(I2CBaseValid(ui32Base));
690
691 // Return the slave status.
692 return(HWREG(I2C0_BASE + I2C_O_SSTAT));
693 }
694
695 //*****************************************************************************
696 //
697 //! \brief Receives a byte that has been sent to the I2C Slave.
698 //!
699 //! This function reads a byte of data from the I2C Slave Data Register.
700 //!
701 //! \param ui32Base is the base address of the I2C Slave module.
702 //!
703 //! \return Returns the byte received from by the I2C Slave, cast as an
704 //! uint32_t.
705 //
706 //*****************************************************************************
707 __STATIC_INLINE uint32_t
I2CSlaveDataGet(uint32_t ui32Base)708 I2CSlaveDataGet(uint32_t ui32Base)
709 {
710 // Check the arguments.
711 ASSERT(I2CBaseValid(ui32Base));
712
713 // Read a byte.
714 return(HWREG(I2C0_BASE + I2C_O_SDR));
715 }
716
717 //*****************************************************************************
718 //
719 //! \brief Transmits a byte from the I2C Slave.
720 //!
721 //! This function will place the supplied data into I2C Slave Data Register.
722 //!
723 //! \param ui32Base is the base address of the I2C Slave module.
724 //! \param ui8Data data to be transmitted from the I2C Slave.
725 //!
726 //! \return None
727 //
728 //*****************************************************************************
729 __STATIC_INLINE void
I2CSlaveDataPut(uint32_t ui32Base,uint8_t ui8Data)730 I2CSlaveDataPut(uint32_t ui32Base, uint8_t ui8Data)
731 {
732 // Check the arguments.
733 ASSERT(I2CBaseValid(ui32Base));
734
735 // Write the byte.
736 HWREG(I2C0_BASE + I2C_O_SDR) = ui8Data;
737 }
738
739 //*****************************************************************************
740 //
741 //! \brief Enables individual I2C Slave interrupt sources.
742 //!
743 //! Enables the indicated I2C Slave interrupt sources. Only the sources that
744 //! are enabled can be reflected to the processor interrupt; disabled sources
745 //! have no effect on the processor.
746 //!
747 //! \param ui32Base is the base address of the I2C module.
748 //! \param ui32IntFlags is the bit mask of the slave interrupt sources to be enabled.
749 //! The parameter is the bitwise OR of any of the following:
750 //! - \ref I2C_SLAVE_INT_STOP
751 //! - \ref I2C_SLAVE_INT_START
752 //! - \ref I2C_SLAVE_INT_DATA
753 //!
754 //! \return None
755 //
756 //*****************************************************************************
757 __STATIC_INLINE void
I2CSlaveIntEnable(uint32_t ui32Base,uint32_t ui32IntFlags)758 I2CSlaveIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
759 {
760 uint32_t ui32Val;
761
762 // Check the arguments.
763 ASSERT(I2CBaseValid(ui32Base));
764 ASSERT(ui32IntFlags & (I2C_SLAVE_INT_STOP | I2C_SLAVE_INT_START |
765 I2C_SLAVE_INT_DATA));
766
767 // Enable the slave interrupt.
768 ui32Val = HWREG(I2C0_BASE + I2C_O_SIMR);
769 ui32Val |= ui32IntFlags;
770 HWREG(I2C0_BASE + I2C_O_SIMR) = ui32Val;
771 }
772
773 //*****************************************************************************
774 //
775 //! \brief Disables individual I2C Slave interrupt sources.
776 //!
777 //! Disables the indicated I2C Slave interrupt sources. Only the sources that
778 //! are enabled can be reflected to the processor interrupt; disabled sources
779 //! have no effect on the processor.
780 //!
781 //! \param ui32Base is the base address of the I2C Slave module.
782 //! \param ui32IntFlags is the bit mask of the interrupt sources to be disabled.
783 //! The parameter is the bitwise OR of any of the following:
784 //! - \ref I2C_SLAVE_INT_STOP
785 //! - \ref I2C_SLAVE_INT_START
786 //! - \ref I2C_SLAVE_INT_DATA
787 //!
788 //! \return None
789 //
790 //*****************************************************************************
791 __STATIC_INLINE void
I2CSlaveIntDisable(uint32_t ui32Base,uint32_t ui32IntFlags)792 I2CSlaveIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
793 {
794 uint32_t ui32Val;
795
796 // Check the arguments.
797 ASSERT(I2CBaseValid(ui32Base));
798 ASSERT(ui32IntFlags & (I2C_SLAVE_INT_STOP | I2C_SLAVE_INT_START |
799 I2C_SLAVE_INT_DATA));
800
801 // Disable the slave interrupt.
802 ui32Val = HWREG(I2C0_BASE + I2C_O_SIMR);
803 ui32Val &= ~ui32IntFlags;
804 HWREG(I2C0_BASE + I2C_O_SIMR) = ui32Val;
805 }
806
807 //*****************************************************************************
808 //
809 //! \brief Clears I2C Slave interrupt sources.
810 //!
811 //! The specified I2C Slave interrupt sources are cleared, so that they no
812 //! longer assert. This must be done in the interrupt handler to keep it from
813 //! being called again immediately upon exit.
814 //!
815 //! \note Due to write buffers and synchronizers in the system it may take several
816 //! clock cycles from a register write clearing an event in a module and until the
817 //! event is actually cleared in the NVIC of the system CPU. It is recommended to
818 //! clear the event source early in the interrupt service routine (ISR) to allow
819 //! the event clear to propagate to the NVIC before returning from the ISR.
820 //! At the same time, an early event clear allows new events of the same type to be
821 //! pended instead of ignored if the event is cleared later in the ISR.
822 //! It is the responsibility of the programmer to make sure that enough time has passed
823 //! before returning from the ISR to avoid false re-triggering of the cleared event.
824 //! A simple, although not necessarily optimal, way of clearing an event before
825 //! returning from the ISR is:
826 //! -# Write to clear event (interrupt source). (buffered write)
827 //! -# Dummy read from the event source module. (making sure the write has propagated)
828 //! -# Wait two system CPU clock cycles (user code or two NOPs). (allowing cleared event to propagate through any synchronizers)
829 //!
830 //! \param ui32Base is the base address of the I2C module.
831 //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
832 //! The parameter is the bitwise OR of any of the following:
833 //! - \ref I2C_SLAVE_INT_STOP
834 //! - \ref I2C_SLAVE_INT_START
835 //! - \ref I2C_SLAVE_INT_DATA
836 //!
837 //! \return None
838 //
839 //*****************************************************************************
840 __STATIC_INLINE void
I2CSlaveIntClear(uint32_t ui32Base,uint32_t ui32IntFlags)841 I2CSlaveIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
842 {
843 // Check the arguments.
844 ASSERT(I2CBaseValid(ui32Base));
845
846 // Clear the I2C slave interrupt source.
847 HWREG(I2C0_BASE + I2C_O_SICR) = ui32IntFlags;
848 }
849
850 //*****************************************************************************
851 //
852 //! \brief Gets the current I2C Slave interrupt status.
853 //!
854 //! This returns the interrupt status for the I2C Slave module. Either the raw
855 //! interrupt status or the status of interrupts that are allowed to reflect to
856 //! the processor can be returned.
857 //!
858 //! \param ui32Base is the base address of the I2C Slave module.
859 //! \param bMasked selects either raw or masked interrupt status.
860 //! - \c false : Raw interrupt status is requested.
861 //! - \c true : Masked interrupt status is requested.
862 //!
863 //! \return Returns the current interrupt status as an OR'ed combination of:
864 //! - \ref I2C_SLAVE_INT_STOP
865 //! - \ref I2C_SLAVE_INT_START
866 //! - \ref I2C_SLAVE_INT_DATA
867 //
868 //*****************************************************************************
869 __STATIC_INLINE uint32_t
I2CSlaveIntStatus(uint32_t ui32Base,bool bMasked)870 I2CSlaveIntStatus(uint32_t ui32Base, bool bMasked)
871 {
872 // Check the arguments.
873 ASSERT(I2CBaseValid(ui32Base));
874
875 // Return either the interrupt status or the raw interrupt status as
876 // requested.
877 if(bMasked)
878 {
879 return(HWREG(I2C0_BASE + I2C_O_SMIS));
880 }
881 else
882 {
883 return(HWREG(I2C0_BASE + I2C_O_SRIS));
884 }
885 }
886
887 //*****************************************************************************
888 //
889 //! \brief Registers an interrupt handler for the I2C module in the dynamic interrupt table.
890 //!
891 //! \note Only use this function if you want to use the dynamic vector table (in SRAM)!
892 //!
893 //! This function registers a function as the interrupt handler for a specific
894 //! interrupt and enables the corresponding interrupt in the interrupt controller.
895 //!
896 //! Specific I2C interrupts must be enabled via \ref I2CMasterIntEnable() and
897 //! \ref I2CSlaveIntEnable(). If necessary, it is the interrupt handler's
898 //! responsibility to clear the interrupt source via \ref I2CMasterIntClear() and
899 //! \ref I2CSlaveIntClear().
900 //!
901 //! \param ui32Base is the base address of the I2C Master module.
902 //! \param pfnHandler is a pointer to the function to be called when the
903 //! I2C interrupt occurs.
904 //!
905 //! \return None
906 //!
907 //! \sa \ref IntRegister() for important information about registering interrupt
908 //! handlers.
909 //
910 //*****************************************************************************
911 extern void I2CIntRegister(uint32_t ui32Base, void (*pfnHandler)(void));
912
913 //*****************************************************************************
914 //
915 //! \brief Unregisters an interrupt handler for the I2C module in the dynamic interrupt table.
916 //!
917 //! This function will clear the handler to be called when an I2C interrupt
918 //! occurs. This will also mask off the interrupt in the interrupt controller
919 //! so that the interrupt handler no longer is called.
920 //!
921 //! \param ui32Base is the base address of the I2C Master module.
922 //!
923 //! \return None
924 //!
925 //! \sa \brief IntRegister() for important information about registering interrupt
926 //! handlers.
927 //
928 //*****************************************************************************
929 extern void I2CIntUnregister(uint32_t ui32Base);
930
931 //*****************************************************************************
932 //
933 // Support for DriverLib in ROM:
934 // Redirect to implementation in ROM when available.
935 //
936 //*****************************************************************************
937 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
938 #include "../driverlib/rom.h"
939 #ifdef ROM_I2CMasterInitExpClk
940 #undef I2CMasterInitExpClk
941 #define I2CMasterInitExpClk ROM_I2CMasterInitExpClk
942 #endif
943 #ifdef ROM_I2CMasterErr
944 #undef I2CMasterErr
945 #define I2CMasterErr ROM_I2CMasterErr
946 #endif
947 #ifdef ROM_I2CIntRegister
948 #undef I2CIntRegister
949 #define I2CIntRegister ROM_I2CIntRegister
950 #endif
951 #ifdef ROM_I2CIntUnregister
952 #undef I2CIntUnregister
953 #define I2CIntUnregister ROM_I2CIntUnregister
954 #endif
955 #endif
956
957 //*****************************************************************************
958 //
959 // Mark the end of the C bindings section for C++ compilers.
960 //
961 //*****************************************************************************
962 #ifdef __cplusplus
963 }
964 #endif
965
966 #endif // __I2C_H__
967
968 //*****************************************************************************
969 //
970 //! Close the Doxygen group.
971 //! @}
972 //! @}
973 //
974 //*****************************************************************************
975