]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3S811_IAR/LuminaryCode/i2c.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS / Demo / CORTEX_LM3S811_IAR / LuminaryCode / i2c.c
1 //*****************************************************************************\r
2 //\r
3 // i2c.c - Driver for Inter-IC (I2C) bus block.\r
4 //\r
5 // Copyright (c) 2005,2006 Luminary Micro, Inc.  All rights reserved.\r
6 //\r
7 // Software License Agreement\r
8 //\r
9 // Luminary Micro, Inc. (LMI) is supplying this software for use solely and\r
10 // exclusively on LMI's Stellaris Family of microcontroller products.\r
11 //\r
12 // The software is owned by LMI and/or its suppliers, and is protected under\r
13 // applicable copyright laws.  All rights are reserved.  Any use in violation\r
14 // of the foregoing restrictions may subject the user to criminal sanctions\r
15 // under applicable laws, as well as to civil liability for the breach of the\r
16 // terms and conditions of this license.\r
17 //\r
18 // THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED\r
19 // OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF\r
20 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.\r
21 // LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR\r
22 // CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.\r
23 //\r
24 // This is part of revision 991 of the Stellaris Driver Library.\r
25 //\r
26 //*****************************************************************************\r
27 \r
28 //*****************************************************************************\r
29 //\r
30 //! \addtogroup i2c_api\r
31 //! @{\r
32 //\r
33 //*****************************************************************************\r
34 \r
35 #include "../hw_i2c.h"\r
36 #include "../hw_ints.h"\r
37 #include "../hw_memmap.h"\r
38 #include "../hw_types.h"\r
39 #include "debug.h"\r
40 #include "i2c.h"\r
41 #include "interrupt.h"\r
42 #include "sysctl.h"\r
43 \r
44 //*****************************************************************************\r
45 //\r
46 //! Initializes the I2C Master block.\r
47 //!\r
48 //! \param ulBase base address of the I2C Master module\r
49 //! \param bFast set up for fast data transfers\r
50 //!\r
51 //! This function initializes operation of the I2C Master block. Upon\r
52 //! successful initialization of the I2C block, this function will have\r
53 //! set the bus speed for the master, and will have enabled the I2C Master\r
54 //! block.\r
55 //!\r
56 //! If the parameter \e bFast is \b true, then the master block will be\r
57 //! set up to transfer data at 400 kbps; otherwise, it will be set up to\r
58 //! transfer data at 100 kbps.\r
59 //!\r
60 //! The I2C clocking is dependent upon the system clock rate returned by\r
61 //! SysCtlClockGet(); if it does not return the correct system clock rate then\r
62 //! the I2C clock rate will be incorrect.\r
63 //!\r
64 //! \return None.\r
65 //\r
66 //*****************************************************************************\r
67 #if defined(GROUP_masterinit) || defined(BUILD_ALL) || defined(DOXYGEN)\r
68 void\r
69 I2CMasterInit(unsigned long ulBase, tBoolean bFast)\r
70 {\r
71     unsigned long ulSysClk;\r
72     unsigned long ulSCLFreq;\r
73     unsigned long ulTPR;\r
74 \r
75     //\r
76     // Check the arguments.\r
77     //\r
78     ASSERT(ulBase == I2C_MASTER_BASE);\r
79 \r
80     //\r
81     // Must enable the device before doing anything else.\r
82     //\r
83     I2CMasterEnable(ulBase);\r
84 \r
85     //\r
86     // Get the system clock speed.\r
87     //\r
88     ulSysClk = SysCtlClockGet();\r
89 \r
90     //\r
91     // Get the desired SCL speed.\r
92     //\r
93     if(bFast == true)\r
94     {\r
95         ulSCLFreq = I2C_SCL_FAST;\r
96     }\r
97     else\r
98     {\r
99         ulSCLFreq = I2C_SCL_STANDARD;\r
100     }\r
101 \r
102     //\r
103     // Compute the clock divider that achieves the fastest speed less than or\r
104     // equal to the desired speed.  The numerator is biases to favor a larger\r
105     // clock divider so that the resulting clock is always less than or equal\r
106     // to the desired clock, never greater.\r
107     //\r
108     ulTPR = (((ulSysClk + (2 * I2C_MASTER_TPR_SCL * ulSCLFreq) - 1) /\r
109               (2 * I2C_MASTER_TPR_SCL * ulSCLFreq)) - 1);\r
110     HWREG(ulBase + I2C_MASTER_O_TPR) = ulTPR;\r
111 }\r
112 #endif\r
113 \r
114 //*****************************************************************************\r
115 //\r
116 //! Initializes the I2C Slave block.\r
117 //!\r
118 //! \param ulBase base address of the I2C Slave module\r
119 //! \param ucSlaveAddr 7-bit slave address\r
120 //!\r
121 //! This function initializes operation of the I2C Slave block. Upon\r
122 //! successful initialization of the I2C blocks, this function will have\r
123 //! set the slave address and have enabled the I2C Slave block.\r
124 //!\r
125 //! The parameter \e ucSlaveAddr is the value that will be compared\r
126 //! against the slave address sent by an I2C master.\r
127 //!\r
128 //! \return None.\r
129 //\r
130 //*****************************************************************************\r
131 #if defined(GROUP_slaveinit) || defined(BUILD_ALL) || defined(DOXYGEN)\r
132 void\r
133 I2CSlaveInit(unsigned long ulBase, unsigned char ucSlaveAddr)\r
134 {\r
135     //\r
136     // Check the arguments.\r
137     //\r
138     ASSERT(ulBase == I2C_SLAVE_BASE);\r
139     ASSERT(!(ucSlaveAddr & 0x80));\r
140 \r
141     //\r
142     // Must enable the device before doing anything else.\r
143     //\r
144     I2CSlaveEnable(ulBase);\r
145 \r
146     //\r
147     // Set up the slave address.\r
148     //\r
149     HWREG(ulBase + I2C_SLAVE_O_OAR) = ucSlaveAddr;\r
150 }\r
151 #endif\r
152 \r
153 //*****************************************************************************\r
154 //\r
155 //! Enables the I2C Master block.\r
156 //!\r
157 //! \param ulBase base address of the I2C Master module\r
158 //!\r
159 //! This will enable operation of the I2C Master block.\r
160 //!\r
161 //! \return None.\r
162 //\r
163 //*****************************************************************************\r
164 #if defined(GROUP_masterenable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
165 void\r
166 I2CMasterEnable(unsigned long ulBase)\r
167 {\r
168     //\r
169     // Check the arguments.\r
170     //\r
171     ASSERT(ulBase == I2C_MASTER_BASE);\r
172 \r
173     //\r
174     // Enable the master block.\r
175     //\r
176     HWREG(ulBase + I2C_MASTER_O_CR) |= I2C_MASTER_CR_MFE;\r
177 }\r
178 #endif\r
179 \r
180 //*****************************************************************************\r
181 //\r
182 //! Enables the I2C Slave block.\r
183 //!\r
184 //! \param ulBase base address of the I2C Slave module\r
185 //!\r
186 //! This will enable operation of the I2C Slave block.\r
187 //!\r
188 //! \return None.\r
189 //\r
190 //*****************************************************************************\r
191 #if defined(GROUP_slaveenable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
192 void\r
193 I2CSlaveEnable(unsigned long ulBase)\r
194 {\r
195     //\r
196     // Check the arguments.\r
197     //\r
198     ASSERT(ulBase == I2C_SLAVE_BASE);\r
199 \r
200     //\r
201     // Enable the clock to the slave block.\r
202     //\r
203     HWREG(ulBase - I2C_O_SLAVE + I2C_MASTER_O_CR) |= I2C_MASTER_CR_SFE;\r
204 \r
205     //\r
206     // Enable the slave.\r
207     //\r
208     HWREG(ulBase + I2C_SLAVE_O_CSR) = I2C_SLAVE_CSR_DA;\r
209 }\r
210 #endif\r
211 \r
212 //*****************************************************************************\r
213 //\r
214 //! Disables the I2C master block.\r
215 //!\r
216 //! \param ulBase base address of the I2C Master module\r
217 //!\r
218 //! This will disable operation of the I2C master block.\r
219 //!\r
220 //! \return None.\r
221 //\r
222 //*****************************************************************************\r
223 #if defined(GROUP_masterdisable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
224 void\r
225 I2CMasterDisable(unsigned long ulBase)\r
226 {\r
227     //\r
228     // Check the arguments.\r
229     //\r
230     ASSERT(ulBase == I2C_MASTER_BASE);\r
231 \r
232     //\r
233     // Disable the master block.\r
234     //\r
235     HWREG(ulBase + I2C_MASTER_O_CR) &= ~(I2C_MASTER_CR_MFE);\r
236 }\r
237 #endif\r
238 \r
239 //*****************************************************************************\r
240 //\r
241 //! Disables the I2C slave block.\r
242 //!\r
243 //! \param ulBase base address of the I2C Slave module\r
244 //!\r
245 //! This will disable operation of the I2C slave block.\r
246 //!\r
247 //! \return None.\r
248 //\r
249 //*****************************************************************************\r
250 #if defined(GROUP_slavedisable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
251 void\r
252 I2CSlaveDisable(unsigned long ulBase)\r
253 {\r
254     //\r
255     // Check the arguments.\r
256     //\r
257     ASSERT(ulBase == I2C_SLAVE_BASE);\r
258 \r
259     //\r
260     // Disable the slave.\r
261     //\r
262     HWREG(ulBase + I2C_SLAVE_O_CSR) = 0;\r
263 \r
264     //\r
265     // Disable the clock to the slave block.\r
266     //\r
267     HWREG(ulBase - I2C_O_SLAVE + I2C_MASTER_O_CR) &= ~(I2C_MASTER_CR_SFE);\r
268 }\r
269 #endif\r
270 \r
271 //*****************************************************************************\r
272 //\r
273 //! Registers an interrupt handler for the I2C module\r
274 //!\r
275 //! \param ulBase base address of the I2C module\r
276 //! \param pfnHandler is a pointer to the function to be called when the\r
277 //! synchronous serial interface interrupt occurs.\r
278 //!\r
279 //! This sets the handler to be called when an I2C interrupt occurs.  This\r
280 //! will enable the global interrupt in the interrupt controller; specific I2C\r
281 //! interrupts must be enabled via I2CMasterIntEnable() and\r
282 //! I2CSlaveIntEnable().  If necessary, it is the interrupt handler's\r
283 //! responsibility to clear the interrupt source via I2CMasterIntClear() and\r
284 //! I2CSlaveIntClear().\r
285 //!\r
286 //! \sa IntRegister() for important information about registering interrupt\r
287 //! handlers.\r
288 //!\r
289 //! \return None.\r
290 //\r
291 //*****************************************************************************\r
292 #if defined(GROUP_intregister) || defined(BUILD_ALL) || defined(DOXYGEN)\r
293 void\r
294 I2CIntRegister(unsigned long ulBase, void (*pfnHandler)(void))\r
295 {\r
296     //\r
297     // Check the arguments.\r
298     //\r
299     ASSERT(ulBase == I2C_MASTER_BASE);\r
300 \r
301     //\r
302     // Register the interrupt handler, returning an error if an error occurs.\r
303     //\r
304     IntRegister(INT_I2C, pfnHandler);\r
305 \r
306     //\r
307     // Enable the I2C interrupt.\r
308     //\r
309     IntEnable(INT_I2C);\r
310 }\r
311 #endif\r
312 \r
313 //*****************************************************************************\r
314 //\r
315 //! Unregisters an interrupt handler for the I2C module.\r
316 //!\r
317 //! \param ulBase base address of the I2C module\r
318 //!\r
319 //! This function will clear the handler to be called when an I2C\r
320 //! interrupt occurs.  This will also mask off the interrupt in the interrupt\r
321 //! controller so that the interrupt handler no longer is called.\r
322 //!\r
323 //! \sa IntRegister() for important information about registering interrupt\r
324 //! handlers.\r
325 //!\r
326 //! \return None.\r
327 //\r
328 //*****************************************************************************\r
329 #if defined(GROUP_intunregister) || defined(BUILD_ALL) || defined(DOXYGEN)\r
330 void\r
331 I2CIntUnregister(unsigned long ulBase)\r
332 {\r
333     //\r
334     // Check the arguments.\r
335     //\r
336     ASSERT(ulBase == I2C_MASTER_BASE);\r
337 \r
338     //\r
339     // Disable the interrupt.\r
340     //\r
341     IntDisable(INT_I2C);\r
342 \r
343     //\r
344     // Unregister the interrupt handler.\r
345     //\r
346     IntUnregister(INT_I2C);\r
347 }\r
348 #endif\r
349 \r
350 //*****************************************************************************\r
351 //\r
352 //! Enables the I2C Master interrupt.\r
353 //!\r
354 //! \param ulBase base address of the I2C Master module\r
355 //!\r
356 //! Enables the I2C Master interrupt source.\r
357 //!\r
358 //! \return None.\r
359 //\r
360 //*****************************************************************************\r
361 #if defined(GROUP_masterintenable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
362 void\r
363 I2CMasterIntEnable(unsigned long ulBase)\r
364 {\r
365     //\r
366     // Check the arguments.\r
367     //\r
368     ASSERT(ulBase == I2C_MASTER_BASE);\r
369 \r
370     //\r
371     // Enable the master interrupt.\r
372     //\r
373     HWREG(ulBase + I2C_MASTER_O_IMR) = 1;\r
374 }\r
375 #endif\r
376 \r
377 //*****************************************************************************\r
378 //\r
379 //! Enables the I2C Slave interrupt.\r
380 //!\r
381 //! \param ulBase base address of the I2C Slave module\r
382 //!\r
383 //! Enables the I2C Slave interrupt source.\r
384 //!\r
385 //! \return None.\r
386 //\r
387 //*****************************************************************************\r
388 #if defined(GROUP_slaveintenable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
389 void\r
390 I2CSlaveIntEnable(unsigned long ulBase)\r
391 {\r
392     //\r
393     // Check the arguments.\r
394     //\r
395     ASSERT(ulBase == I2C_SLAVE_BASE);\r
396 \r
397     //\r
398     // Enable the slave interrupt.\r
399     //\r
400     HWREG(ulBase + I2C_SLAVE_O_IM) = 1;\r
401 }\r
402 #endif\r
403 \r
404 //*****************************************************************************\r
405 //\r
406 //! Disables the I2C Master interrupt.\r
407 //!\r
408 //! \param ulBase base address of the I2C Master module\r
409 //!\r
410 //! Disables the I2C Master interrupt source.\r
411 //!\r
412 //! \return None.\r
413 //\r
414 //*****************************************************************************\r
415 #if defined(GROUP_masterintdisable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
416 void\r
417 I2CMasterIntDisable(unsigned long ulBase)\r
418 {\r
419     //\r
420     // Check the arguments.\r
421     //\r
422     ASSERT(ulBase == I2C_MASTER_BASE);\r
423 \r
424     //\r
425     // Disable the master interrupt.\r
426     //\r
427     HWREG(ulBase + I2C_MASTER_O_IMR) = 0;\r
428 }\r
429 #endif\r
430 \r
431 //*****************************************************************************\r
432 //\r
433 //! Disables the I2C Slave interrupt.\r
434 //!\r
435 //! \param ulBase base address of the I2C Slave module\r
436 //!\r
437 //! Disables the I2C Slave interrupt source.\r
438 //!\r
439 //! \return None.\r
440 //\r
441 //*****************************************************************************\r
442 #if defined(GROUP_slaveintdisable) || defined(BUILD_ALL) || defined(DOXYGEN)\r
443 void\r
444 I2CSlaveIntDisable(unsigned long ulBase)\r
445 {\r
446     //\r
447     // Check the arguments.\r
448     //\r
449     ASSERT(ulBase == I2C_SLAVE_BASE);\r
450 \r
451     //\r
452     // Disable the slave interrupt.\r
453     //\r
454     HWREG(ulBase + I2C_SLAVE_O_IM) = 0;\r
455 }\r
456 #endif\r
457 \r
458 //*****************************************************************************\r
459 //\r
460 //! Gets the current I2C Master interrupt status.\r
461 //!\r
462 //! \param ulBase base address of the I2C Master module\r
463 //! \param bMasked is false if the raw interrupt status is requested and\r
464 //! true if the masked interrupt status is requested.\r
465 //!\r
466 //! This returns the interrupt status for the I2C Master module.\r
467 //! Either the raw interrupt status or the status of interrupts that are\r
468 //! allowed to reflect to the processor can be returned.\r
469 //!\r
470 //! \return The current interrupt status, returned as \b true if active\r
471 //! or \b false if not active.\r
472 //\r
473 //*****************************************************************************\r
474 #if defined(GROUP_masterintstatus) || defined(BUILD_ALL) || defined(DOXYGEN)\r
475 tBoolean\r
476 I2CMasterIntStatus(unsigned long ulBase, tBoolean bMasked)\r
477 {\r
478     //\r
479     // Check the arguments.\r
480     //\r
481     ASSERT(ulBase == I2C_MASTER_BASE);\r
482 \r
483     //\r
484     // Return either the interrupt status or the raw interrupt status as\r
485     // requested.\r
486     //\r
487     if(bMasked)\r
488     {\r
489         return((HWREG(ulBase + I2C_MASTER_O_MIS)) ? true : false);\r
490     }\r
491     else\r
492     {\r
493         return((HWREG(ulBase + I2C_MASTER_O_RIS)) ? true : false);\r
494     }\r
495 }\r
496 #endif\r
497 \r
498 //*****************************************************************************\r
499 //\r
500 //! Gets the current I2C Slave interrupt status.\r
501 //!\r
502 //! \param ulBase base address of the I2C Slave module\r
503 //! \param bMasked is false if the raw interrupt status is requested and\r
504 //! true if the masked interrupt status is requested.\r
505 //!\r
506 //! This returns the interrupt status for the I2C Slave module.\r
507 //! Either the raw interrupt status or the status of interrupts that are\r
508 //! allowed to reflect to the processor can be returned.\r
509 //!\r
510 //! \return The current interrupt status, returned as \b true if active\r
511 //! or \b false if not active.\r
512 //\r
513 //*****************************************************************************\r
514 #if defined(GROUP_slaveintstatus) || defined(BUILD_ALL) || defined(DOXYGEN)\r
515 tBoolean\r
516 I2CSlaveIntStatus(unsigned long ulBase, tBoolean bMasked)\r
517 {\r
518     //\r
519     // Check the arguments.\r
520     //\r
521     ASSERT(ulBase == I2C_SLAVE_BASE);\r
522 \r
523     //\r
524     // Return either the interrupt status or the raw interrupt status as\r
525     // requested.\r
526     //\r
527     if(bMasked)\r
528     {\r
529         return((HWREG(ulBase + I2C_SLAVE_O_MIS)) ? true : false);\r
530     }\r
531     else\r
532     {\r
533         return((HWREG(ulBase + I2C_SLAVE_O_RIS)) ? true : false);\r
534     }\r
535 }\r
536 #endif\r
537 \r
538 //*****************************************************************************\r
539 //\r
540 //! Clears I2C Master interrupt sources.\r
541 //!\r
542 //! \param ulBase base address of the I2C Master module\r
543 //!\r
544 //! The I2C Master interrupt source is cleared, so that it no longer asserts.\r
545 //! This must be done in the interrupt handler to keep it from being called\r
546 //! again immediately upon exit.\r
547 //!\r
548 //! \return None.\r
549 //\r
550 //*****************************************************************************\r
551 #if defined(GROUP_masterintclear) || defined(BUILD_ALL) || defined(DOXYGEN)\r
552 void\r
553 I2CMasterIntClear(unsigned long ulBase)\r
554 {\r
555     //\r
556     // Check the arguments.\r
557     //\r
558     ASSERT(ulBase == I2C_MASTER_BASE);\r
559 \r
560     //\r
561     // Clear the I2C master interrupt source.\r
562     //\r
563     HWREG(ulBase + I2C_MASTER_O_MICR) = I2C_MASTER_MICR_IC;\r
564 \r
565     //\r
566     // Workaround for I2C master interrupt clear errata for rev B Stellaris\r
567     // devices.  For later devices, this write is ignored and therefore\r
568     // harmless (other than the slight performance hit).\r
569     //\r
570     HWREG(ulBase + I2C_MASTER_O_MIS) = I2C_MASTER_MICR_IC;\r
571 }\r
572 #endif\r
573 \r
574 //*****************************************************************************\r
575 //\r
576 //! Clears I2C Slave interrupt sources.\r
577 //!\r
578 //! \param ulBase base address of the I2C Slave module\r
579 //!\r
580 //! The I2C Slave interrupt source is cleared, so that it no longer asserts.\r
581 //! This must be done in the interrupt handler to keep it from being called\r
582 //! again immediately upon exit.\r
583 //!\r
584 //! \return None.\r
585 //\r
586 //*****************************************************************************\r
587 #if defined(GROUP_slaveintclear) || defined(BUILD_ALL) || defined(DOXYGEN)\r
588 void\r
589 I2CSlaveIntClear(unsigned long ulBase)\r
590 {\r
591     //\r
592     // Check the arguments.\r
593     //\r
594     ASSERT(ulBase == I2C_SLAVE_BASE);\r
595 \r
596     //\r
597     // Clear the I2C slave interrupt source.\r
598     //\r
599     HWREG(ulBase + I2C_SLAVE_O_SICR) = I2C_SLAVE_SICR_IC;\r
600 }\r
601 #endif\r
602 \r
603 //*****************************************************************************\r
604 //\r
605 //! Sets the address that the I2C Master will place on the bus.\r
606 //!\r
607 //! \param ulBase base address of the I2C Master module\r
608 //! \param ucSlaveAddr 7-bit slave address\r
609 //! \param bReceive flag indicating the type of communication with the slave\r
610 //!\r
611 //! This function will set the address that the I2C Master will place on the\r
612 //! bus when initiating a transaction. When the parameter \e bReceive is set\r
613 //! to \b true, the address will indicate that the I2C Master is initiating\r
614 //! a read from the slave; otherwise the address will indicate that the I2C\r
615 //! Master is initiating a write to the slave.\r
616 //!\r
617 //! \return None.\r
618 //\r
619 //*****************************************************************************\r
620 #if defined(GROUP_masterslaveaddrset) || defined(BUILD_ALL) || defined(DOXYGEN)\r
621 void\r
622 I2CMasterSlaveAddrSet(unsigned long ulBase, unsigned char ucSlaveAddr,\r
623                       tBoolean bReceive)\r
624 {\r
625     //\r
626     // Check the arguments.\r
627     //\r
628     ASSERT(ulBase == I2C_MASTER_BASE);\r
629     ASSERT(!(ucSlaveAddr & 0x80));\r
630 \r
631     //\r
632     // Set the address of the slave with which the master will communicate.\r
633     //\r
634     HWREG(ulBase + I2C_MASTER_O_SA) = (ucSlaveAddr << 1) | bReceive;\r
635 }\r
636 #endif\r
637 \r
638 //*****************************************************************************\r
639 //\r
640 //! Indicates whether or not the I2C Master is busy.\r
641 //!\r
642 //! \param ulBase base address of the I2C Master module\r
643 //!\r
644 //! This function returns an indication of whether or not the I2C Master is\r
645 //! busy transmitting or receiving data.\r
646 //!\r
647 //! \return Returns \b true if the I2C Master is busy; otherwise, returns\r
648 //! \b false.\r
649 //\r
650 //*****************************************************************************\r
651 #if defined(GROUP_masterbusy) || defined(BUILD_ALL) || defined(DOXYGEN)\r
652 tBoolean\r
653 I2CMasterBusy(unsigned long ulBase)\r
654 {\r
655     //\r
656     // Check the arguments.\r
657     //\r
658     ASSERT(ulBase == I2C_MASTER_BASE);\r
659 \r
660     //\r
661     // Return the busy status.\r
662     //\r
663     if(HWREG(ulBase + I2C_MASTER_O_CS) & I2C_MASTER_CS_BUSY)\r
664     {\r
665         return(true);\r
666     }\r
667     else\r
668     {\r
669         return(false);\r
670     }\r
671 }\r
672 #endif\r
673 \r
674 //*****************************************************************************\r
675 //\r
676 //! Indicates whether or not the I2C bus is busy.\r
677 //!\r
678 //! \param ulBase base address of the I2C Master module\r
679 //!\r
680 //! This function returns an indication of whether or not the I2C bus is\r
681 //! busy. This function can be used in a multi-master environment to\r
682 //! determine if another master is currently using the bus.\r
683 //!\r
684 //! \return Returns \b true if the I2C bus is busy; otherwise, returns\r
685 //! \b false.\r
686 //\r
687 //*****************************************************************************\r
688 #if defined(GROUP_masterbusbusy) || defined(BUILD_ALL) || defined(DOXYGEN)\r
689 tBoolean\r
690 I2CMasterBusBusy(unsigned long ulBase)\r
691 {\r
692     //\r
693     // Check the arguments.\r
694     //\r
695     ASSERT(ulBase == I2C_MASTER_BASE);\r
696 \r
697     //\r
698     // Return the bus busy status.\r
699     //\r
700     if(HWREG(ulBase + I2C_MASTER_O_CS) & I2C_MASTER_CS_BUS_BUSY)\r
701     {\r
702         return(true);\r
703     }\r
704     else\r
705     {\r
706         return(false);\r
707     }\r
708 }\r
709 #endif\r
710 \r
711 //*****************************************************************************\r
712 //\r
713 //! Controls the state of the I2C Master module.\r
714 //!\r
715 //! \param ulBase base address of the I2C Master module\r
716 //! \param ulCmd command to be issued to the I2C Master module\r
717 //!\r
718 //! This function is used to control the state of the Master module send and\r
719 //! receive operations. The parameter \e ucCmd can be one of the following\r
720 //! values:\r
721 //!\r
722 //! - I2C_MASTER_CMD_SINGLE_SEND\r
723 //! - I2C_MASTER_CMD_SINGLE_RECEIVE\r
724 //! - I2C_MASTER_CMD_BURST_SEND_START\r
725 //! - I2C_MASTER_CMD_BURST_SEND_CONT\r
726 //! - I2C_MASTER_CMD_BURST_SEND_FINISH\r
727 //! - I2C_MASTER_CMD_BURST_SEND_ERROR_STOP\r
728 //! - I2C_MASTER_CMD_BURST_RECEIVE_START\r
729 //! - I2C_MASTER_CMD_BURST_RECEIVE_CONT\r
730 //! - I2C_MASTER_CMD_BURST_RECEIVE_FINISH\r
731 //! - I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP\r
732 //!\r
733 //! \return None.\r
734 //\r
735 //*****************************************************************************\r
736 #if defined(GROUP_mastercontrol) || defined(BUILD_ALL) || defined(DOXYGEN)\r
737 void\r
738 I2CMasterControl(unsigned long ulBase, unsigned long ulCmd)\r
739 {\r
740     //\r
741     // Check the arguments.\r
742     //\r
743     ASSERT(ulBase == I2C_MASTER_BASE);\r
744     ASSERT((ulCmd == I2C_MASTER_CMD_SINGLE_SEND) ||\r
745            (ulCmd == I2C_MASTER_CMD_SINGLE_RECEIVE) ||\r
746            (ulCmd == I2C_MASTER_CMD_BURST_SEND_START) ||\r
747            (ulCmd == I2C_MASTER_CMD_BURST_SEND_CONT) ||\r
748            (ulCmd == I2C_MASTER_CMD_BURST_SEND_FINISH) ||\r
749            (ulCmd == I2C_MASTER_CMD_BURST_SEND_ERROR_STOP) ||\r
750            (ulCmd == I2C_MASTER_CMD_BURST_RECEIVE_START) ||\r
751            (ulCmd == I2C_MASTER_CMD_BURST_RECEIVE_CONT) ||\r
752            (ulCmd == I2C_MASTER_CMD_BURST_RECEIVE_FINISH) ||\r
753            (ulCmd == I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP));\r
754 \r
755     //\r
756     // Send the command.\r
757     //\r
758     HWREG(ulBase + I2C_MASTER_O_CS) = ulCmd;\r
759 }\r
760 #endif\r
761 \r
762 //*****************************************************************************\r
763 //\r
764 //! Gets the error status of the I2C Master module.\r
765 //!\r
766 //! \param ulBase base address of the I2C Master module\r
767 //!\r
768 //! This function is used to obtain the error status of the Master module\r
769 //! send and receive operations. It returns one of the following values:\r
770 //!\r
771 //! - I2C_MASTER_ERR_NONE\r
772 //! - I2C_MASTER_ERR_ADDR_ACK\r
773 //! - I2C_MASTER_ERR_DATA_ACK\r
774 //! - I2C_MASTER_ERR_ARB_LOST\r
775 //!\r
776 //! \return None.\r
777 //\r
778 //*****************************************************************************\r
779 #if defined(GROUP_mastererr) || defined(BUILD_ALL) || defined(DOXYGEN)\r
780 unsigned long\r
781 I2CMasterErr(unsigned long ulBase)\r
782 {\r
783     unsigned long ulErr;\r
784 \r
785     //\r
786     // Check the arguments.\r
787     //\r
788     ASSERT(ulBase == I2C_MASTER_BASE);\r
789 \r
790     //\r
791     // Get the raw error state\r
792     //\r
793     ulErr = HWREG(ulBase + I2C_MASTER_O_CS);\r
794 \r
795     //\r
796     // If the I2C master is busy, then all the other bit are invalid, and\r
797     // don't have an error to report.\r
798     //\r
799     if(ulErr & I2C_MASTER_CS_BUSY)\r
800     {\r
801         return(I2C_MASTER_ERR_NONE);\r
802     }\r
803 \r
804     //\r
805     // Check for errors.\r
806     //\r
807     if(ulErr & I2C_MASTER_CS_ERROR)\r
808     {\r
809         return(ulErr & (I2C_MASTER_CS_ERR_MASK));\r
810     }\r
811     else\r
812     {\r
813         return(I2C_MASTER_ERR_NONE);\r
814     }\r
815 }\r
816 #endif\r
817 \r
818 //*****************************************************************************\r
819 //\r
820 //! Transmits a byte from the I2C Master.\r
821 //!\r
822 //! \param ulBase base address of the I2C Master module\r
823 //! \param ucData data to be transmitted from the I2C Master\r
824 //!\r
825 //! This function will place the supplied data into I2C Master Data Register.\r
826 //!\r
827 //! \return None.\r
828 //\r
829 //*****************************************************************************\r
830 #if defined(GROUP_masterdataput) || defined(BUILD_ALL) || defined(DOXYGEN)\r
831 void\r
832 I2CMasterDataPut(unsigned long ulBase, unsigned char ucData)\r
833 {\r
834     //\r
835     // Check the arguments.\r
836     //\r
837     ASSERT(ulBase == I2C_MASTER_BASE);\r
838 \r
839     //\r
840     // Write the byte.\r
841     //\r
842     HWREG(ulBase + I2C_MASTER_O_DR) = ucData;\r
843 }\r
844 #endif\r
845 \r
846 //*****************************************************************************\r
847 //\r
848 //! Receives a byte that has been sent to the I2C Master.\r
849 //!\r
850 //! \param ulBase base address of the I2C Master module\r
851 //!\r
852 //! This function reads a byte of data from the I2C Master Data Register.\r
853 //!\r
854 //! \return Returns the byte received from by the I2C Master, cast as an\r
855 //! unsigned long.\r
856 //\r
857 //*****************************************************************************\r
858 #if defined(GROUP_masterdataget) || defined(BUILD_ALL) || defined(DOXYGEN)\r
859 unsigned long\r
860 I2CMasterDataGet(unsigned long ulBase)\r
861 {\r
862     //\r
863     // Check the arguments.\r
864     //\r
865     ASSERT(ulBase == I2C_MASTER_BASE);\r
866 \r
867     //\r
868     // Read a byte.\r
869     //\r
870     return(HWREG(ulBase + I2C_MASTER_O_DR));\r
871 }\r
872 #endif\r
873 \r
874 //*****************************************************************************\r
875 //\r
876 //! Gets the I2C Slave module status\r
877 //!\r
878 //! \param ulBase base address of the I2C Slave module\r
879 //!\r
880 //! This function will return the action requested from a master, if any. The\r
881 //! possible values returned are:\r
882 //!\r
883 //! - I2C_SLAVE_ACT_NONE\r
884 //! - I2C_SLAVE_ACT_RREQ\r
885 //! - I2C_SLAVE_ACT_TREQ\r
886 //!\r
887 //! where I2C_SLAVE_ACT_NONE means that no action has been requested of the\r
888 //! I2C Slave module, I2C_SLAVE_ACT_RREQ means that an I2C master has sent\r
889 //! data to the I2C Slave module, and I2C_SLAVE_ACT_TREQ means that an I2C\r
890 //! master has requested that the I2C Slave module send data.\r
891 //!\r
892 //! \return None.\r
893 //\r
894 //*****************************************************************************\r
895 #if defined(GROUP_slavestatus) || defined(BUILD_ALL) || defined(DOXYGEN)\r
896 unsigned long\r
897 I2CSlaveStatus(unsigned long ulBase)\r
898 {\r
899     //\r
900     // Check the arguments.\r
901     //\r
902     ASSERT(ulBase == I2C_SLAVE_BASE);\r
903 \r
904     //\r
905     // Return the slave status.\r
906     //\r
907     return(HWREG(ulBase + I2C_SLAVE_O_CSR));\r
908 }\r
909 #endif\r
910 \r
911 //*****************************************************************************\r
912 //\r
913 //! Transmits a byte from the I2C Slave.\r
914 //!\r
915 //! \param ulBase base address of the I2C Slave module\r
916 //! \param ucData data to be transmitted from the I2C Slave\r
917 //!\r
918 //! This function will place the supplied data into I2C Slave Data Register.\r
919 //!\r
920 //! \return None.\r
921 //\r
922 //*****************************************************************************\r
923 #if defined(GROUP_slavedataput) || defined(BUILD_ALL) || defined(DOXYGEN)\r
924 void\r
925 I2CSlaveDataPut(unsigned long ulBase, unsigned char ucData)\r
926 {\r
927     //\r
928     // Check the arguments.\r
929     //\r
930     ASSERT(ulBase == I2C_SLAVE_BASE);\r
931 \r
932     //\r
933     // Write the byte.\r
934     //\r
935     HWREG(ulBase + I2C_SLAVE_O_DR) = ucData;\r
936 }\r
937 #endif\r
938 \r
939 //*****************************************************************************\r
940 //\r
941 //! Receives a byte that has been sent to the I2C Slave.\r
942 //!\r
943 //! \param ulBase base address of the I2C Slave module\r
944 //!\r
945 //! This function reads a byte of data from the I2C Slave Data Register.\r
946 //!\r
947 //! \return Returns the byte received from by the I2C Slave, cast as an\r
948 //! unsigned long.\r
949 //\r
950 //*****************************************************************************\r
951 #if defined(GROUP_slavedataget) || defined(BUILD_ALL) || defined(DOXYGEN)\r
952 unsigned long\r
953 I2CSlaveDataGet(unsigned long ulBase)\r
954 {\r
955     //\r
956     // Check the arguments.\r
957     //\r
958     ASSERT(ulBase == I2C_SLAVE_BASE);\r
959 \r
960     //\r
961     // Read a byte.\r
962     //\r
963     return(HWREG(ulBase + I2C_SLAVE_O_DR));\r
964 }\r
965 #endif\r
966 \r
967 //*****************************************************************************\r
968 //\r
969 // Close the Doxygen group.\r
970 //! @}\r
971 //\r
972 //*****************************************************************************\r