ESOS32
ESOSOn32-bitProcessors
esos_i2c.h
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2019 J. W. Bruce ("AUTHOR(S)")"
3  * All rights reserved.
4  * (J. W. Bruce, jwbruce_AT_tntech.edu, Tennessee Tech University)
5  *
6  * Permission to use, copy, modify, and distribute this software and its
7  * documentation for any purpose, without fee, and without written agreement is
8  * hereby granted, provided that the above copyright notice, the following
9  * two paragraphs and the authors appear in all copies of this software.
10  *
11  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
12  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
13  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
14  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15  *
16  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
19  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
20  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
21  *
22  * Please maintain this header in its entirety when copying/modifying
23  * these files.
24  *
25  *
26  */
27 
39 #ifndef ESOS_I2C_H
40 #define ESOS_I2C_H
41 
42 /* I N C L U D E S **********************************************************/
43 #include <esos.h>
44 
45 /* D E F I N E S ************************************************************/
46 #ifndef ESOS_I2C_ACK
47 #define ESOS_I2C_ACK 0
48 #endif
49 #ifndef ESOS_I2C_NAK
50 #define ESOS_I2C_NAK 1
51 #endif
52 
53 /* M A C R O S **************************************************************/
54 #define I2C_WADDR(x) (x & 0xFE) //clear R/W bit of I2C addr
55 #define I2C_RADDR(x) (x | 0x01) //set R/W bit of I2C addr
56 
67 #define ESOS_TASK_WAIT_ON_AVAILABLE_I2C() \
68  do { \
69  ESOS_TASK_WAIT_UNTIL(ESOS_IS_I2C_AVAILABLE()); \
70  __esos_SetSystemFlag(__ESOS_SYS_I2C_IN_USE); \
71  } while(0)
72 
83 #define ESOS_SIGNAL_AVAILABLE_I2C() __esos_ClearSystemFlag(__ESOS_SYS_I2C_IN_USE)
84 
95 #define ESOS_IS_I2C_AVAILABLE() (__esos_IsSystemFlagClear(__ESOS_SYS_I2C_IN_USE))
96 
103 // Must copy value to a var, send &var, just in case u8_d1 is a literal
116 #define ESOS_TASK_WAIT_ON_WRITE1I2C( u8_addr, u8_d1 ) \
117  do{ \
118  __esos_i2c_dataBytes[0] = (u8_d1); \
119  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskI2C, __esos_i2c_hw_writeN, (u8_addr), &__esos_i2c_dataBytes[0], 1 ); \
120  }while(0)
121 
122 // We needed a 2-byte array to be able to use the buffer.
123 // Otherwise, we'd need two writeN calls, which require
124 // child tasks, i.e. code size grows
125 
139 #define ESOS_TASK_WAIT_ON_WRITE2I2C( u8_addr, u8_d1, u8_d2 ) \
140  do{ \
141  __esos_i2c_dataBytes[0] = (u8_d1); \
142  __esos_i2c_dataBytes[1] = (u8_d2); \
143  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskI2C, __esos_i2c_hw_writeN, (u8_addr), &__esos_i2c_dataBytes[0], 2 ); \
144  }while(0)
145 
159 #define ESOS_TASK_WAIT_ON_WRITENI2C( u8_addr, pu8_d, u8_cnt ) \
160  do { \
161  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskI2C, __esos_i2c_hw_writeN, (u8_addr), (pu8_d), (u8_cnt) ); \
162  } while(0)
163 
177 #define ESOS_TASK_WAIT_ON_READ1I2C( u8_addr, u8_d1 ) \
178  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskI2C, __esos_i2c_hw_readN, (u8_addr), &(u8_d1), 1 )
179 
180 // While READ1 still worked fine, READ2 needs a new trick, since u8_d1 and u8_d2 are
181 // not in consecutive memory locations. Can't do two READN calls, because that (again)
182 // requires another child task. Instead, just re-use our 2-byte array as a read buffer
183 
198 #define ESOS_TASK_WAIT_ON_READ2I2C( u8_addr, u8_d1, u8_d2 ) \
199  do{ \
200  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskI2C, __esos_i2c_hw_readN, (u8_addr), &__esos_i2c_dataBytes[0], 2 ); \
201  (u8_d1) = __esos_i2c_dataBytes[0]; \
202  (u8_d2) = __esos_i2c_dataBytes[1]; \
203  }while(0)
204 
218 #define ESOS_TASK_WAIT_ON_READNI2C( u8_addr, pu8_d, u8_cnt ) \
219  do { \
220  ESOS_TASK_SPAWN_AND_WAIT( (ESOS_TASK_HANDLE)&__stChildTaskI2C, __esos_i2c_hw_readN, (u8_addr), (pu8_d), (u8_cnt) ); \
221  } while (0)
222 
223 /* S T R U C T U R E S ******************************************************/
224 
225 /* P U B L I C P R O T O T Y P E S *****************************************/
226 void __esos_i2c_config(uint32_t u32_i2cbps);
227 
228 /* P R O T O T Y P E S HARDWARE-SPECIFIC ********************************/
229 extern void __esos_i2c_hw_config(uint32_t u32_i2cbps);
230 extern ESOS_CHILD_TASK( __esos_hw_getI2C, uint8_t* pu8_x, uint8_t u8_ack2Send);
231 extern ESOS_CHILD_TASK( __esos_i2c_hw_writeN, uint8_t u8_addr, uint8_t* pu8_d, uint8_t u8_cnt);
232 extern ESOS_CHILD_TASK( __esos_i2c_hw_readN, uint8_t u8_addr, uint8_t* pu8_d, uint8_t u8_cnt);
233 // extern ESOS_CHILD_TASK( __esos_i2c_hw_writeMreadN, uint8_t u8_addr, uint8_t* pu8_wd, uint8_t u8_wcnt, uint8_t* pu8_rd, uint8_t u8_rcnt);
234 
235 #endif // ESOS_I2C_H
236 
237 
238 
__esos_i2c_config
void __esos_i2c_config(uint32_t u32_i2cbps)
Definition: esos_i2c.c:65
__esos_i2c_hw_config
void __esos_i2c_hw_config(uint32_t u32_i2cbps)
Definition: esos_hwxxx_i2c.c:74
esos.h
ESOS_CHILD_TASK
#define ESOS_CHILD_TASK(taskname,...)
Definition: esos_task.h:246