PIC24 Support Libraries
esos_pic24_i2c.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 
31 /**
32  * \addtogroup ESOS_I2C_Service
33  * @{
34  */
35 
36 /*** I N C L U D E S *************************************************/
37 #include "esos_pic24_i2c.h"
38 
39 /*** G L O B A L S *************************************************/
40 struct stTask __stChildTaskI2C, __stGrandChildTaskI2C;
41 uint8_t __esos_i2c_dataBytes[2];
42 
43 /*** T H E C O D E *************************************************/
44 
45 /*********************************************************
46  * Public functions intended to be called by other files *
47  *********************************************************/
48 
49 // Documentation for this file. If the \file tag is not present,
50 // this file will not be documented.
51 // Note: place this comment below the #if NUM_I2C_MODS so Doxygen
52 // will only see it once.
53 /** \file
54  * I2C support functions. \see pic24_i2c.h for details.
55  */
56 
57 
58 /**
59 Configure and enable the I2C1 module for operation at \em u16_fKHz KHz clock speed.
60 \param u16_fKHz specifies clock speed in KHz
61 \sa ESOS_TASK_WAIT_ON_WRITENI2C1
62 \sa ESOS_TASK_WAIT_ON_READNI2C1
63 \hideinitializer
64  */
65 void esos_pic24_configI2C1(uint16_t u16_fKHz) {
66  uint32_t u32_temp;
67 
68  u32_temp = (FCY/1000L)/((uint32_t) u16_fKHz);
69  u32_temp = u32_temp - FCY/10000000L - 1;
70  I2C1BRG = u32_temp;
71  I2C1CONbits.I2CEN = 1;
72 }
73 
74 ESOS_CHILD_TASK( __esos_pic24_getI2C1, uint8_t* pu8_x, uint8_t u8_ack2Send) {
75  static uint8_t u8_tempAck;
76 
78  u8_tempAck = u8_ack2Send;
79 
80  ESOS_TASK_WAIT_WHILE(I2C1CON & 0x1F); //wait for idle condition
81  I2C1CONbits.RCEN = 1; //enable receive
82  ESOS_TASK_WAIT_UNTIL(I2C1STATbits.RBF); //wait for receive byte
83  *pu8_x = I2C1RCV; //read byte;
84  //wait for idle condition before attempting ACK
85  ESOS_TASK_WAIT_WHILE(I2C1CON & 0x1F); //lower 5 bits must be 0
86  I2C1CONbits.ACKDT = u8_tempAck; //ACK bit to send back on receive
87  I2C1CONbits.ACKEN = 1; //enable ACKbit transmittion
88  ESOS_TASK_WAIT_WHILE(I2C1CONbits.ACKEN); //wait for completion
89  ESOS_TASK_END();
90 } // end __esos_pic24_getI2C1
91 
92 /**
93 Transaction: Write \em u16_cnt bytes stored in buffer \em *pu8_d to I2C slave at address \em u8_addr.
94 \param u8_addr Slave I2C address
95 \param pu8_d Pointer to buffer containing bytes to send
96 \param u16_cnt Number of bytes to send
97  */
98 ESOS_CHILD_TASK( __esos_pic24_writeNI2C1, uint8_t u8_addr, uint8_t* pu8_d, uint16_t u16_cnt) {
99  static uint8_t u8_tempAddr;
100  static uint8_t* pu8_tempPtr;
101  static uint16_t u16_tempCnt, u16_i;
102 
103  ESOS_TASK_BEGIN();
104  u8_tempAddr=u8_addr;
105  pu8_tempPtr=pu8_d;
106  u16_tempCnt=u16_cnt;
107  __PIC24_I2C1_START();
108  __PIC24_I2C1_PUT(I2C_WADDR(u8_tempAddr));
109  for (u16_i=0; u16_i < u16_tempCnt; u16_i++) {
110  __PIC24_I2C1_PUT(*pu8_tempPtr);
111  pu8_tempPtr++;
112  }
113  __PIC24_I2C1_STOP();
114  ESOS_TASK_END();
115 } // end __esos_pic24_writeNI2C1
116 
117 /**
118 Transaction: Read \em u16_cnt bytes from I2C slave at address \em u8_addr, save to buffer \em *pu8_d.
119 As per the I2C standard, a NAK is returned for the last byte read from the slave, ACKs are returned for the other bytes.
120 \param u8_addr Slave I2C address
121 \param pu8_d Pointer to buffer for storing bytes read from slave
122 \param u16_cnt Number of bytes read from slave.
123  */
124 ESOS_CHILD_TASK( __esos_pic24_readNI2C1, uint8_t u8_addr, uint8_t* pu8_d, uint16_t u16_cnt) {
125  static uint8_t u8_tempAddr;
126  static uint8_t* pu8_tempD;
127  static uint16_t u16_tempCnt, u16_i;
128 
129  ESOS_TASK_BEGIN();
130  u8_tempAddr=u8_addr;
131  pu8_tempD=pu8_d;
132  u16_tempCnt=u16_cnt;
133  __PIC24_I2C1_START();
134  __PIC24_I2C1_PUT(I2C_RADDR(u8_tempAddr));
135  for (u16_i=0; u16_i < u16_tempCnt-1; u16_i++) {
136  ESOS_TASK_WAIT_ON_GETI2C1(pu8_tempD, I2C_ACK);
137  pu8_tempD++;
138  }
139  ESOS_TASK_WAIT_ON_GETI2C1(pu8_tempD, I2C_NAK);
140  __PIC24_I2C1_STOP();
141  ESOS_TASK_END();
142  /** @} */
143 } // end __esos_pic24_readNI2C1
144 
void esos_pic24_configI2C1(uint16_t u16_FkHZ)
This file contains routines which configure and use I2C on the Microchip PIC24 MCUs.
#define ESOS_CHILD_TASK(taskname,...)
Definition: esos_task.h:247
#define ESOS_TASK_END()
Definition: esos_task.h:272
#define ESOS_TASK_WAIT_WHILE(cond)
Definition: esos_task.h:364
#define ESOS_TASK_WAIT_UNTIL(condition)
Definition: esos_task.h:336
#define ESOS_TASK_BEGIN()
Definition: esos_task.h:260
#define FCY
unsigned char uint8_t
An abbreviation for an 8-bit unsigned integer.
Definition: dataXferImpl.h:194