1 /******************************************************************************
2 * Filename: i2c.h
3 *
4 * Description: Prototypes and defines for the I2C API.
5 *
6 * Copyright (c) 2022 Texas Instruments Incorporated
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * 2) Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * 3) Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ******************************************************************************/
35
36 #ifndef __I2C_H__
37 #define __I2C_H__
38
39 //*****************************************************************************
40 //
41 //! \addtogroup peripheral_group
42 //! @{
43 //! \addtogroup i2c_api
44 //! @{
45 //
46 //*****************************************************************************
47
48 //*****************************************************************************
49 //
50 // If building with a C++ compiler, make all of the definitions in this header
51 // have a C binding.
52 //
53 //*****************************************************************************
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57
58 #include <stdbool.h>
59 #include <stdint.h>
60 #include "../inc/hw_types.h"
61 #include "../inc/hw_ints.h"
62 #include "../inc/hw_memmap.h"
63 #include "../inc/hw_i2c.h"
64 #include "debug.h"
65 #include "interrupt.h"
66 #include "cpu.h"
67
68 //*****************************************************************************
69 //
70 // I2C Controller commands
71 //
72 //*****************************************************************************
73 #define I2C_CONTROLLER_CMD_SINGLE_SEND 0x00000007
74 #define I2C_CONTROLLER_CMD_SINGLE_RECEIVE 0x00000007
75 #define I2C_CONTROLLER_CMD_BURST_SEND_START 0x00000003
76 #define I2C_CONTROLLER_CMD_BURST_SEND_CONT 0x00000001
77 #define I2C_CONTROLLER_CMD_BURST_SEND_FINISH 0x00000005
78 #define I2C_CONTROLLER_CMD_BURST_SEND_ERROR_STOP 0x00000004
79 #define I2C_CONTROLLER_CMD_BURST_RECEIVE_START 0x0000000b
80 #define I2C_CONTROLLER_CMD_BURST_RECEIVE_CONT 0x00000009
81 #define I2C_CONTROLLER_CMD_BURST_RECEIVE_FINISH 0x00000005
82 #define I2C_CONTROLLER_CMD_BURST_RECEIVE_ERROR_STOP 0x00000004
83
84 //*****************************************************************************
85 //
86 // I2C Controller error status
87 //
88 //*****************************************************************************
89 #define I2C_CONTROLLER_ERR_NONE 0
90 #define I2C_CONTROLLER_ERR_ADDR_ACK 0x00000004
91 #define I2C_CONTROLLER_ERR_DATA_ACK 0x00000008
92 #define I2C_CONTROLLER_ERR_ARB_LOST 0x00000010
93
94 //*****************************************************************************
95 //
96 // I2C Target action requests
97 //
98 //*****************************************************************************
99 #define I2C_TARGET_ACT_NONE 0
100 #define I2C_TARGET_ACT_RREQ 0x00000001 // Controller has sent data
101 #define I2C_TARGET_ACT_TREQ 0x00000002 // Controller has requested data
102 #define I2C_TARGET_ACT_RREQ_FBR 0x00000005 // Controller has sent first byte
103
104 //*****************************************************************************
105 //
106 // I2C Target interrupts
107 //
108 //*****************************************************************************
109 #define I2C_TARGET_INT_STOP 0x00000004 // Stop Condition Interrupt.
110 #define I2C_TARGET_INT_START 0x00000002 // Start Condition Interrupt.
111 #define I2C_TARGET_INT_DATA 0x00000001 // Data Interrupt.
112
113 //*****************************************************************************
114 //
115 // I2C module clock frequency
116 //
117 //*****************************************************************************
118 #define I2C_CLK_FREQ 48000000 // Clock supplied to I2C periph in Hz
119
120 //*****************************************************************************
121 //
122 // API Functions and prototypes
123 //
124 //*****************************************************************************
125
126 #ifdef DRIVERLIB_DEBUG
127 //*****************************************************************************
128 //
129 //! \internal
130 //!
131 //! \brief Checks an I2C base address.
132 //!
133 //! This function determines if a I2C port base address is valid.
134 //!
135 //! \param base is the base address of the I2C port.
136 //!
137 //! \return Returns \c true if the base address is valid and \c false
138 //! otherwise
139 //
140 //*****************************************************************************
I2CBaseValid(uint32_t base)141 static bool I2CBaseValid(uint32_t base)
142 {
143 return (base == I2C0_BASE);
144 }
145 #endif
146
147 //*****************************************************************************
148 //
149 //! \brief Initializes the \I2C Controller module.
150 //!
151 //! This function initializes operation of the \I2C Controller module. Upon
152 //! successful initialization of the \I2C module, this function will have set the
153 //! bus speed for the controller, and will have enabled the \I2C Controller module.
154 //!
155 //! If the parameter \c fast is \c true, then the controller module will be set up
156 //! to transfer data at 400 kbps; otherwise, it will be set up to transfer data
157 //! at 100 kbps.
158 //!
159 //! \param base is the base address of the \I2C module.
160 //! \param fast set up for fast data transfers.
161 //!
162 //! \return None
163 //
164 //*****************************************************************************
165 extern void I2CControllerInitExpClk(uint32_t base, bool fast);
166
167 //*****************************************************************************
168 //
169 //! \brief Controls the state of the \I2C Controller module.
170 //!
171 //! This function is used to control the state of the Controller module send and
172 //! receive operations.
173 //!
174 //! \param base is the base address of the \I2C module.
175 //! \param cmd is the command to be issued by the \I2C Controller module
176 //! The parameter can be one of the following values:
177 //! - \ref I2C_CONTROLLER_CMD_SINGLE_SEND
178 //! - \ref I2C_CONTROLLER_CMD_SINGLE_RECEIVE
179 //! - \ref I2C_CONTROLLER_CMD_BURST_SEND_START
180 //! - \ref I2C_CONTROLLER_CMD_BURST_SEND_CONT
181 //! - \ref I2C_CONTROLLER_CMD_BURST_SEND_FINISH
182 //! - \ref I2C_CONTROLLER_CMD_BURST_SEND_ERROR_STOP
183 //! - \ref I2C_CONTROLLER_CMD_BURST_RECEIVE_START
184 //! - \ref I2C_CONTROLLER_CMD_BURST_RECEIVE_CONT
185 //! - \ref I2C_CONTROLLER_CMD_BURST_RECEIVE_FINISH
186 //! - \ref I2C_CONTROLLER_CMD_BURST_RECEIVE_ERROR_STOP
187 //!
188 //! \return None
189 //
190 //*****************************************************************************
I2CControllerCommand(uint32_t base,uint32_t cmd)191 __STATIC_INLINE void I2CControllerCommand(uint32_t base, uint32_t cmd)
192 {
193 // Check the arguments.
194 ASSERT(I2CBaseValid(base));
195 ASSERT((cmd == I2C_CONTROLLER_CMD_SINGLE_SEND) ||
196 // (cmd == I2C_CONTROLLER_CMD_SINGLE_RECEIVE) || -> Equal to SINGLE_SEND
197 (cmd == I2C_CONTROLLER_CMD_BURST_SEND_START) || (cmd == I2C_CONTROLLER_CMD_BURST_SEND_CONT) ||
198 (cmd == I2C_CONTROLLER_CMD_BURST_SEND_FINISH) || (cmd == I2C_CONTROLLER_CMD_BURST_SEND_ERROR_STOP) ||
199 (cmd == I2C_CONTROLLER_CMD_BURST_RECEIVE_START) || (cmd == I2C_CONTROLLER_CMD_BURST_RECEIVE_CONT) ||
200 (cmd == I2C_CONTROLLER_CMD_BURST_RECEIVE_FINISH) || (cmd == I2C_CONTROLLER_CMD_BURST_RECEIVE_ERROR_STOP));
201
202 // Send the command.
203 HWREG(base + I2C_O_CCTL) = cmd;
204
205 // Delay minimum four cycles in order to ensure that the I2C_O_CCTL
206 // register has been correctly updated before function exit
207 CPUDelay(2);
208 }
209
210 //*****************************************************************************
211 //
212 //! \brief Sets the address that the \I2C Controller will place on the bus.
213 //!
214 //! This function will set the address that the \I2C Controller will place on the
215 //! bus when initiating a transaction. When the \c receive parameter is set
216 //! to \b true, the address will indicate that the \I2C Controller is initiating a
217 //! read from the target; otherwise the address will indicate that the \I2C
218 //! Controller is initiating a write to the target.
219 //!
220 //! \param base is the base address of the \I2C module.
221 //! \param targetAddr is a 7-bit target address
222 //! \param receive flag indicates the type of communication with the target.
223 //! - \c true : \I2C Controller is initiating a read from the target.
224 //! - \c false : \I2C Controller is initiating a write to the target.
225 //!
226 //! \return None
227 //
228 //*****************************************************************************
I2CControllerSetTargetAddr(uint32_t base,uint8_t targetAddr,bool receive)229 __STATIC_INLINE void I2CControllerSetTargetAddr(uint32_t base, uint8_t targetAddr, bool receive)
230 {
231 // Check the arguments.
232 ASSERT(I2CBaseValid(base));
233 ASSERT(!(targetAddr & 0x80));
234
235 // Set the address of the target with which the controller will communicate.
236 HWREG(base + I2C_O_CTA) = (targetAddr << 1) | receive;
237 }
238
239 //*****************************************************************************
240 //
241 //! \brief Enables the \I2C Controller module.
242 //!
243 //! This will enable operation of the \I2C Controller module.
244 //!
245 //! \param base is the base address of the \I2C module.
246 //!
247 //! \return None
248 //
249 //*****************************************************************************
I2CControllerEnable(uint32_t base)250 __STATIC_INLINE void I2CControllerEnable(uint32_t base)
251 {
252 // Check the arguments.
253 ASSERT(I2CBaseValid(base));
254
255 // Enable the clock for the controller.
256 HWREG(base + I2C_O_CCR) |= I2C_CCR_CFE_M;
257
258 // Enable the controller module.
259 HWREG(base + I2C_O_CCTL) = I2C_CCTL_RUN_EN;
260 }
261
262 //*****************************************************************************
263 //
264 //! \brief Disables the \I2C controller module.
265 //!
266 //! This will disable operation of the \I2C controller module.
267 //!
268 //! \param base is the base address of the \I2C module.
269 //!
270 //! \return None
271 //
272 //*****************************************************************************
I2CControllerDisable(uint32_t base)273 __STATIC_INLINE void I2CControllerDisable(uint32_t base)
274 {
275 // Check the arguments.
276 ASSERT(I2CBaseValid(base));
277
278 // Disable the controller module.
279 HWREG(base + I2C_O_CCTL) = 0;
280
281 // Disable the clock for the controller.
282 HWREG(base + I2C_O_CCR) &= ~I2C_CCR_CFE_M;
283 }
284
285 //*****************************************************************************
286 //
287 //! \brief Indicates whether or not the \I2C Controller is busy.
288 //!
289 //! This function returns an indication of whether or not the \I2C Controller is
290 //! busy transmitting or receiving data.
291 //!
292 //! \param base is the base address of the \I2C module.
293 //!
294 //! \return Returns status of \I2C Controller:
295 //! - \c true : \I2C Controller is busy.
296 //! - \c false : \I2C Controller is not busy.
297 //
298 //*****************************************************************************
I2CControllerBusy(uint32_t base)299 __STATIC_INLINE bool I2CControllerBusy(uint32_t base)
300 {
301 // Check the arguments.
302 ASSERT(I2CBaseValid(base));
303
304 // Return the busy status.
305 if (HWREG(base + I2C_O_CSTA) & I2C_CSTA_BUSY_M)
306 {
307 return (true);
308 }
309 else
310 {
311 return (false);
312 }
313 }
314
315 //*****************************************************************************
316 //
317 //! \brief Indicates whether or not the \I2C bus is busy.
318 //!
319 //! This function returns an indication of whether or not the \I2C bus is busy.
320 //! This function can be used in a multi-controller environment to determine if
321 //! another controller is currently using the bus.
322 //!
323 //! \param base is the base address of the \I2C module.
324 //!
325 //! \return Returns status of the \I2C bus:
326 //! - \c true : \I2C bus is busy.
327 //! - \c false : \I2C bus is not busy.
328 //
329 //*****************************************************************************
I2CControllerBusBusy(uint32_t base)330 __STATIC_INLINE bool I2CControllerBusBusy(uint32_t base)
331 {
332 // Check the arguments.
333 ASSERT(I2CBaseValid(base));
334
335 // Return the bus busy status.
336 if (HWREG(base + I2C_O_CSTA) & I2C_CSTA_BUSBSY_M)
337 {
338 return (true);
339 }
340 else
341 {
342 return (false);
343 }
344 }
345
346 //*****************************************************************************
347 //
348 //! \brief Receives a byte that has been sent to the \I2C Controller.
349 //!
350 //! This function reads a byte of data from the \I2C Controller Data Register.
351 //!
352 //! \param base is the base address of the \I2C module.
353 //!
354 //! \return Returns the byte received from by the \I2C Controller, cast as an
355 //! uint32_t.
356 //
357 //*****************************************************************************
I2CControllerGetData(uint32_t base)358 __STATIC_INLINE uint32_t I2CControllerGetData(uint32_t base)
359 {
360 // Check the arguments.
361 ASSERT(I2CBaseValid(base));
362
363 // Read a byte.
364 return (HWREG(base + I2C_O_CDR));
365 }
366
367 //*****************************************************************************
368 //
369 //! \brief Transmits a byte from the \I2C Controller.
370 //!
371 //! This function will place the supplied data into \I2C Controller Data Register.
372 //!
373 //! \param base is the base address of the \I2C module.
374 //! \param data is the data to be transmitted by the \I2C Controller
375 //!
376 //! \return None
377 //
378 //*****************************************************************************
I2CControllerPutData(uint32_t base,uint8_t data)379 __STATIC_INLINE void I2CControllerPutData(uint32_t base, uint8_t data)
380 {
381 // Check the arguments.
382 ASSERT(I2CBaseValid(base));
383
384 // Write the byte.
385 HWREG(base + I2C_O_CDR) = data;
386 }
387
388 //*****************************************************************************
389 //
390 //! \brief Gets the error status of the \I2C Controller module.
391 //!
392 //! This function is used to obtain the error status of the Controller module send
393 //! and receive operations.
394 //!
395 //! \param base is the base address of the \I2C module.
396 //!
397 //! \return Returns the error status of the Controller module:
398 //! - \ref I2C_CONTROLLER_ERR_NONE
399 //! - \ref I2C_CONTROLLER_ERR_ADDR_ACK
400 //! - \ref I2C_CONTROLLER_ERR_DATA_ACK
401 //! - \ref I2C_CONTROLLER_ERR_ARB_LOST
402 //
403 //*****************************************************************************
404 extern uint32_t I2CControllerError(uint32_t base);
405
406 //*****************************************************************************
407 //
408 //! \brief Enables the \I2C Controller interrupt.
409 //!
410 //! Enables the \I2C Controller interrupt source.
411 //!
412 //! \param base is the base address of the \I2C module.
413 //!
414 //! \return None
415 //
416 //*****************************************************************************
I2CControllerEnableInt(uint32_t base)417 __STATIC_INLINE void I2CControllerEnableInt(uint32_t base)
418 {
419 // Check the arguments.
420 ASSERT(I2CBaseValid(base));
421
422 // Enable the controller interrupt.
423 HWREG(base + I2C_O_CIMR) = I2C_CIMR_IM;
424 }
425
426 //*****************************************************************************
427 //
428 //! \brief Disables the \I2C Controller interrupt.
429 //!
430 //! Disables the \I2C Controller interrupt source.
431 //!
432 //! \param base is the base address of the \I2C module.
433 //!
434 //! \return None
435 //
436 //*****************************************************************************
I2CControllerDisableInt(uint32_t base)437 __STATIC_INLINE void I2CControllerDisableInt(uint32_t base)
438 {
439 // Check the arguments.
440 ASSERT(I2CBaseValid(base));
441
442 // Disable the controller interrupt.
443 HWREG(base + I2C_O_CIMR) = 0;
444 }
445
446 //*****************************************************************************
447 //
448 //! \brief Clears \I2C Controller interrupt sources.
449 //!
450 //! The \I2C Controller interrupt source is cleared, so that it no longer asserts.
451 //! This must be done in the interrupt handler to keep it from being called
452 //! again immediately upon exit.
453 //!
454 //! \note Due to write buffers and synchronizers in the system it may take several
455 //! clock cycles from a register write clearing an event in a module and until the
456 //! event is actually cleared in the NVIC of the system CPU. It is recommended to
457 //! clear the event source early in the interrupt service routine (ISR) to allow
458 //! the event clear to propagate to the NVIC before returning from the ISR.
459 //! At the same time, an early event clear allows new events of the same type to be
460 //! pended instead of ignored if the event is cleared later in the ISR.
461 //! It is the responsibility of the programmer to make sure that enough time has passed
462 //! before returning from the ISR to avoid false re-triggering of the cleared event.
463 //! A simple, although not necessarily optimal, way of clearing an event before
464 //! returning from the ISR is:
465 //! -# Write to clear event (interrupt source). (buffered write)
466 //! -# Dummy read from the event source module. (making sure the write has propagated)
467 //! -# Wait two system CPU clock cycles (user code or two NOPs). (allowing cleared event to propagate through any
468 //! synchronizers)
469 //!
470 //! \param base is the base address of the \I2C module.
471 //!
472 //! \return None
473 //
474 //*****************************************************************************
I2CControllerClearInt(uint32_t base)475 __STATIC_INLINE void I2CControllerClearInt(uint32_t base)
476 {
477 // Check the arguments.
478 ASSERT(I2CBaseValid(base));
479
480 // Clear the I2C controller interrupt source.
481 HWREG(base + I2C_O_CICR) = I2C_CICR_IC;
482 }
483
484 //*****************************************************************************
485 //
486 //! \brief Gets the current \I2C Controller interrupt status.
487 //!
488 //! This returns the interrupt status for the \I2C Controller module. Either the
489 //! raw interrupt status or the status of interrupts that are allowed to
490 //! reflect to the processor can be returned.
491 //!
492 //! \param base is the base address of the \I2C Controller module.
493 //! \param masked selects either raw or masked interrupt status.
494 //! - \c false : Raw interrupt status is requested.
495 //! - \c true : Masked interrupt status is requested.
496 //!
497 //! \return Returns the current interrupt status.
498 //! - \c true : Active.
499 //! - \c false : Not active.
500 //
501 //*****************************************************************************
I2CControllerIntStatus(uint32_t base,bool masked)502 __STATIC_INLINE bool I2CControllerIntStatus(uint32_t base, bool masked)
503 {
504 // Check the arguments.
505 ASSERT(I2CBaseValid(base));
506
507 // Return either the interrupt status or the raw interrupt status as
508 // requested.
509 if (masked)
510 {
511 return ((HWREG(base + I2C_O_CMIS)) ? true : false);
512 }
513 else
514 {
515 return ((HWREG(base + I2C_O_CRIS)) ? true : false);
516 }
517 }
518
519 //*****************************************************************************
520 //
521 //! \brief Enables the \I2C Target module.
522 //!
523 //! This will enable operation of the \I2C Target module.
524 //!
525 //! \param base is the base address of the \I2C Target module.
526 //!
527 //! \return None
528 //
529 //*****************************************************************************
I2CTargetEnable(uint32_t base)530 __STATIC_INLINE void I2CTargetEnable(uint32_t base)
531 {
532 // Check the arguments.
533 ASSERT(I2CBaseValid(base));
534
535 // Enable the clock to the target module.
536 HWREG(base + I2C_O_CCR) |= I2C_CCR_TFE_M;
537
538 // Enable the target.
539 HWREG(base + I2C_O_TCTL) = I2C_TCTL_DA_EN;
540 }
541
542 //*****************************************************************************
543 //
544 //! \brief Initializes the \I2C Target module.
545 //!
546 //! This function initializes operation of the \I2C Target module. Upon
547 //! successful initialization of the \I2C module, this function will have set
548 //! the target address and have enabled the \I2C Target module.
549 //!
550 //! The parameter \c targetAddr is the value that will be compared against the
551 //! target address sent by an \I2C controller.
552 //!
553 //! \param base is the base address of the \I2C Target module.
554 //! \param targetAddr is the 7-bit target address.
555 //!
556 //! \return None
557 //
558 //*****************************************************************************
I2CTargetInit(uint32_t base,uint8_t targetAddr)559 __STATIC_INLINE void I2CTargetInit(uint32_t base, uint8_t targetAddr)
560 {
561 // Check the arguments.
562 ASSERT(I2CBaseValid(base));
563 ASSERT(!(targetAddr & 0x80));
564
565 // Must enable the device before doing anything else.
566 I2CTargetEnable(base);
567
568 // Set up the target address.
569 HWREG(base + I2C_O_TOAR) = targetAddr;
570 }
571
572 //*****************************************************************************
573 //
574 //! \brief Sets the \I2C target address.
575 //!
576 //! This function writes the specified target address.
577 //!
578 //! \param base is the base address of the \I2C Target module.
579 //! \param targetAddr is the 7-bit target address
580 //!
581 //! \return None.
582 //
583 //*****************************************************************************
I2CTargetSetAddress(uint32_t base,uint8_t targetAddr)584 __STATIC_INLINE void I2CTargetSetAddress(uint32_t base, uint8_t targetAddr)
585 {
586 // Check the arguments.
587 ASSERT(I2CBaseValid(base));
588 ASSERT(!(targetAddr & 0x80));
589
590 // Set up the primary target address.
591 HWREG(base + I2C_O_TOAR) = targetAddr;
592 }
593
594 //*****************************************************************************
595 //
596 //! \brief Disables the \I2C target module.
597 //!
598 //! This will disable operation of the \I2C target module.
599 //!
600 //! \param base is the base address of the \I2C Target module.
601 //!
602 //! \return None
603 //
604 //*****************************************************************************
I2CTargetDisable(uint32_t base)605 __STATIC_INLINE void I2CTargetDisable(uint32_t base)
606 {
607 // Check the arguments.
608 ASSERT(I2CBaseValid(base));
609
610 // Disable the target.
611 HWREG(base + I2C_O_TCTL) = 0x0;
612
613 // Disable the clock to the target module.
614 HWREG(base + I2C_O_CCR) &= ~I2C_CCR_TFE_M;
615 }
616
617 //*****************************************************************************
618 //
619 //! \brief Gets the \I2C Target module status.
620 //!
621 //! This function will return the action requested from a controller, if any.
622 //!
623 //! \param base is the base address of the \I2C Target module.
624 //!
625 //! \return Returns the status of the \I2C Target module:
626 //! - \ref I2C_TARGET_ACT_NONE : No action has been requested of the \I2C Target module.
627 //! - \ref I2C_TARGET_ACT_RREQ : An \I2C controller has sent data to the \I2C Target module.
628 //! - \ref I2C_TARGET_ACT_TREQ : An \I2C controller has requested that the \I2C Target module send data.
629 //! - \ref I2C_TARGET_ACT_RREQ_FBR : An \I2C controller has sent data to the \I2C target
630 //! and the first byte following the target's own address has been received.
631 //
632 //*****************************************************************************
I2CTargetStatus(uint32_t base)633 __STATIC_INLINE uint32_t I2CTargetStatus(uint32_t base)
634 {
635 // Check the arguments.
636 ASSERT(I2CBaseValid(base));
637
638 // Return the target status.
639 return (HWREG(base + I2C_O_TSTA));
640 }
641
642 //*****************************************************************************
643 //
644 //! \brief Receives a byte that has been sent to the \I2C Target.
645 //!
646 //! This function reads a byte of data from the \I2C Target Data Register.
647 //!
648 //! \param base is the base address of the \I2C Target module.
649 //!
650 //! \return Returns the byte received from by the \I2C Target, cast as an
651 //! uint32_t.
652 //
653 //*****************************************************************************
I2CTargetGetData(uint32_t base)654 __STATIC_INLINE uint32_t I2CTargetGetData(uint32_t base)
655 {
656 // Check the arguments.
657 ASSERT(I2CBaseValid(base));
658
659 // Read a byte.
660 return (HWREG(base + I2C_O_TDR));
661 }
662
663 //*****************************************************************************
664 //
665 //! \brief Transmits a byte from the \I2C Target.
666 //!
667 //! This function will place the supplied data into \I2C Target Data Register.
668 //!
669 //! \param base is the base address of the \I2C Target module.
670 //! \param data data to be transmitted from the \I2C Target.
671 //!
672 //! \return None
673 //
674 //*****************************************************************************
I2CTargetPutData(uint32_t base,uint8_t data)675 __STATIC_INLINE void I2CTargetPutData(uint32_t base, uint8_t data)
676 {
677 // Check the arguments.
678 ASSERT(I2CBaseValid(base));
679
680 // Write the byte.
681 HWREG(base + I2C_O_TDR) = data;
682 }
683
684 //*****************************************************************************
685 //
686 //! \brief Enables individual \I2C Target interrupt sources.
687 //!
688 //! Enables the indicated \I2C Target interrupt sources. Only the sources that
689 //! are enabled can be reflected to the processor interrupt; disabled sources
690 //! have no effect on the processor.
691 //!
692 //! \param base is the base address of the \I2C module.
693 //! \param intFlags is the bit mask of the target interrupt sources to be enabled.
694 //! The parameter is the bitwise OR of any of the following:
695 //! - \ref I2C_TARGET_INT_STOP
696 //! - \ref I2C_TARGET_INT_START
697 //! - \ref I2C_TARGET_INT_DATA
698 //!
699 //! \return None
700 //
701 //*****************************************************************************
I2CTargetEnableInt(uint32_t base,uint32_t intFlags)702 __STATIC_INLINE void I2CTargetEnableInt(uint32_t base, uint32_t intFlags)
703 {
704 uint32_t val;
705
706 // Check the arguments.
707 ASSERT(I2CBaseValid(base));
708 ASSERT(intFlags & (I2C_TARGET_INT_STOP | I2C_TARGET_INT_START | I2C_TARGET_INT_DATA));
709
710 // Enable the target interrupt.
711 val = HWREG(base + I2C_O_TIMR);
712 val |= intFlags;
713 HWREG(base + I2C_O_TIMR) = val;
714 }
715
716 //*****************************************************************************
717 //
718 //! \brief Disables individual \I2C Target interrupt sources.
719 //!
720 //! Disables the indicated \I2C Target interrupt sources. Only the sources that
721 //! are enabled can be reflected to the processor interrupt; disabled sources
722 //! have no effect on the processor.
723 //!
724 //! \param base is the base address of the \I2C Target module.
725 //! \param intFlags is the bit mask of the interrupt sources to be disabled.
726 //! The parameter is the bitwise OR of any of the following:
727 //! - \ref I2C_TARGET_INT_STOP
728 //! - \ref I2C_TARGET_INT_START
729 //! - \ref I2C_TARGET_INT_DATA
730 //!
731 //! \return None
732 //
733 //*****************************************************************************
I2CTargetDisableInt(uint32_t base,uint32_t intFlags)734 __STATIC_INLINE void I2CTargetDisableInt(uint32_t base, uint32_t intFlags)
735 {
736 uint32_t val;
737
738 // Check the arguments.
739 ASSERT(I2CBaseValid(base));
740 ASSERT(intFlags & (I2C_TARGET_INT_STOP | I2C_TARGET_INT_START | I2C_TARGET_INT_DATA));
741
742 // Disable the target interrupt.
743 val = HWREG(base + I2C_O_TIMR);
744 val &= ~intFlags;
745 HWREG(base + I2C_O_TIMR) = val;
746 }
747
748 //*****************************************************************************
749 //
750 //! \brief Clears \I2C Target interrupt sources.
751 //!
752 //! The specified \I2C Target interrupt sources are cleared, so that they no
753 //! longer assert. This must be done in the interrupt handler to keep it from
754 //! being called again immediately upon exit.
755 //!
756 //! \note Due to write buffers and synchronizers in the system it may take several
757 //! clock cycles from a register write clearing an event in a module and until the
758 //! event is actually cleared in the NVIC of the system CPU. It is recommended to
759 //! clear the event source early in the interrupt service routine (ISR) to allow
760 //! the event clear to propagate to the NVIC before returning from the ISR.
761 //! At the same time, an early event clear allows new events of the same type to be
762 //! pended instead of ignored if the event is cleared later in the ISR.
763 //! It is the responsibility of the programmer to make sure that enough time has passed
764 //! before returning from the ISR to avoid false re-triggering of the cleared event.
765 //! A simple, although not necessarily optimal, way of clearing an event before
766 //! returning from the ISR is:
767 //! -# Write to clear event (interrupt source). (buffered write)
768 //! -# Dummy read from the event source module. (making sure the write has propagated)
769 //! -# Wait two system CPU clock cycles (user code or two NOPs). (allowing cleared event to propagate through any
770 //! synchronizers)
771 //!
772 //! \param base is the base address of the \I2C module.
773 //! \param intFlags is a bit mask of the interrupt sources to be cleared.
774 //! The parameter is the bitwise OR of any of the following:
775 //! - \ref I2C_TARGET_INT_STOP
776 //! - \ref I2C_TARGET_INT_START
777 //! - \ref I2C_TARGET_INT_DATA
778 //!
779 //! \return None
780 //
781 //*****************************************************************************
I2CTargetClearInt(uint32_t base,uint32_t intFlags)782 __STATIC_INLINE void I2CTargetClearInt(uint32_t base, uint32_t intFlags)
783 {
784 // Check the arguments.
785 ASSERT(I2CBaseValid(base));
786
787 // Clear the I2C target interrupt source.
788 HWREG(base + I2C_O_TICR) = intFlags;
789 }
790
791 //*****************************************************************************
792 //
793 //! \brief Gets the current \I2C Target interrupt status.
794 //!
795 //! This returns the interrupt status for the \I2C Target module. Either the raw
796 //! interrupt status or the status of interrupts that are allowed to reflect to
797 //! the processor can be returned.
798 //!
799 //! \param base is the base address of the \I2C Target module.
800 //! \param masked selects either raw or masked interrupt status.
801 //! - \c false : Raw interrupt status is requested.
802 //! - \c true : Masked interrupt status is requested.
803 //!
804 //! \return Returns the current interrupt status as an OR'ed combination of:
805 //! - \ref I2C_TARGET_INT_STOP
806 //! - \ref I2C_TARGET_INT_START
807 //! - \ref I2C_TARGET_INT_DATA
808 //
809 //*****************************************************************************
I2CTargetIntStatus(uint32_t base,bool masked)810 __STATIC_INLINE uint32_t I2CTargetIntStatus(uint32_t base, bool masked)
811 {
812 // Check the arguments.
813 ASSERT(I2CBaseValid(base));
814
815 // Return either the interrupt status or the raw interrupt status as
816 // requested.
817 if (masked)
818 {
819 return (HWREG(base + I2C_O_TMIS));
820 }
821 else
822 {
823 return (HWREG(base + I2C_O_TRIS));
824 }
825 }
826
827 //*****************************************************************************
828 //
829 // Mark the end of the C bindings section for C++ compilers.
830 //
831 //*****************************************************************************
832 #ifdef __cplusplus
833 }
834 #endif
835
836 //*****************************************************************************
837 //
838 //! Close the Doxygen group.
839 //! @}
840 //! @}
841 //
842 //*****************************************************************************
843
844 #endif // __I2C_H__
845