nmi2c.c
Go to the documentation of this file.
1 
36 
37 #ifdef CONF_WINC_USE_I2C
38 
39 #include "nmi2c.h"
41 
42 
43 /*
44 * @fn nm_i2c_read_reg_with_ret
45 * @brief Read register with error code return
46 * @param [in] u32Addr
47 * Register address
48 * @param [out] pu32RetVal
49 * Pointer to u32 variable used to return the read value
50 * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
51 * @author M. Abdelmawla
52 * @date 11 July 2012
53 * @version 1.0
54 */
55  sint8 nm_i2c_read_reg_with_ret(uint32 u32Addr, uint32* pu32RetVal)
56 {
57  uint8 b[6];
58  uint8 rsz;
59  tstrNmI2cDefault strI2c;
60  sint8 s8Ret = M2M_SUCCESS;
61 
62  if(u32Addr < 0xff) { /* clockless i2c */
63  b[0] = 0x09;
64  b[1] = (uint8)(u32Addr);
65  rsz = 1;
66  strI2c.u16Sz = 2;
67  } else {
68  b[0] = 0x80;
69  b[1] = (uint8)(u32Addr >> 24);
70  b[2] = (uint8)(u32Addr >> 16);
71  b[3] = (uint8)(u32Addr >> 8);
72  b[4] = (uint8)(u32Addr);
73  b[5] = 0x04;
74  rsz = 4;
75  strI2c.u16Sz = 6;
76  }
77 
78  strI2c.pu8Buf = b;
79 
80  if(M2M_SUCCESS == nm_bus_ioctl(NM_BUS_IOCTL_W, &strI2c))
81  {
82  strI2c.u16Sz = rsz;
83  if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strI2c))
84  {
85  //M2M_ERR("read error\n");
86  s8Ret = M2M_ERR_BUS_FAIL;
87  }
88  }
89  else
90  {
91  M2M_ERR("failed to send cfg bytes\n");
92  s8Ret = M2M_ERR_BUS_FAIL;
93  }
94 
95  if (rsz == 1) {
96  *pu32RetVal = b[0];
97  } else {
98  *pu32RetVal = b[0] | ((uint32)b[1] << 8) | ((uint32)b[2] << 16) | ((uint32)b[3] << 24);
99  }
100  return s8Ret;
101 }
102 
103 /*
104 * @fn nm_i2c_read_reg
105 * @brief Read register
106 * @param [in] u32Addr
107 * Register address
108 * @return Register value
109 * @author M. Abdelmawla
110 * @date 11 July 2012
111 * @version 1.0
112 */
114 {
115  uint32 val;
116  nm_i2c_read_reg_with_ret(u32Addr, &val);
117  return val;
118 }
119 
120 /*
121 * @fn nm_i2c_write_reg
122 * @brief write register
123 * @param [in] u32Addr
124 * Register address
125 * @param [in] u32Val
126 * Value to be written to the register
127 * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
128 * @author M. Abdelmawla
129 * @date 11 July 2012
130 * @version 1.0
131 */
132 sint8 nm_i2c_write_reg(uint32 u32Addr, uint32 u32Val)
133 {
134  tstrNmI2cDefault strI2c;
135  uint8 b[16];
136  sint8 s8Ret = M2M_SUCCESS;
137 
138  if(u32Addr < 0xff) { /* clockless i2c */
139  b[0] = 0x19;
140  b[1] = (uint8)(u32Addr);
141  b[2] = (uint8)(u32Val);
142  strI2c.u16Sz = 3;
143  } else {
144  b[0] = 0x90;
145  b[1] = (uint8)(u32Addr >> 24);
146  b[2] = (uint8)(u32Addr >> 16);
147  b[3] = (uint8)(u32Addr >> 8);
148  b[4] = (uint8)u32Addr;
149  b[5] = 0x04;
150  b[6] = (uint8)u32Val;
151  b[7] = (uint8)(u32Val >> 8);
152  b[8] = (uint8)(u32Val >> 16);
153  b[9] = (uint8)(u32Val >> 24);
154  strI2c.u16Sz = 10;
155  }
156 
157  strI2c.pu8Buf = b;
158 
159  if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strI2c))
160  {
161  M2M_ERR("write error\n");
162  s8Ret = M2M_ERR_BUS_FAIL;
163  }
164 
165  return s8Ret;
166 }
167 
168 /*
169 * @fn nm_i2c_read_block
170 * @brief Read block of data
171 * @param [in] u32Addr
172 * Start address
173 * @param [out] puBuf
174 * Pointer to a buffer used to return the read data
175 * @param [in] u16Sz
176 * Number of bytes to read. The buffer size must be >= u16Sz
177 * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
178 * @author M. Abdelmawla
179 * @date 11 July 2012
180 * @version 1.0
181 */
182 sint8 nm_i2c_read_block(uint32 u32Addr, uint8 *pu8Buf, uint16 u16Sz)
183 {
184  tstrNmI2cDefault strI2c;
185  uint8 au8Buf[7];
186  sint8 s8Ret = M2M_SUCCESS;
187 
188  au8Buf[0] = 0x02;
189  au8Buf[1] = (uint8)(u32Addr >> 24);
190  au8Buf[2] = (uint8)(u32Addr >> 16);
191  au8Buf[3] = (uint8)(u32Addr >> 8);
192  au8Buf[4] = (uint8)(u32Addr >> 0);
193  au8Buf[5] = (uint8)(u16Sz >> 8);
194  au8Buf[6] = (uint8)(u16Sz);
195 
196  strI2c.pu8Buf = au8Buf;
197  strI2c.u16Sz = sizeof(au8Buf);
198 
199  if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strI2c))
200  {
201  M2M_ERR("write error\n");
202  s8Ret = M2M_ERR_BUS_FAIL;
203  }
204  else
205  {
206  strI2c.pu8Buf = pu8Buf;
207  strI2c.u16Sz = u16Sz;
208 
209  if(M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strI2c))
210  {
211  M2M_ERR("read error\n");
212  s8Ret = M2M_ERR_BUS_FAIL;
213  }
214  }
215 
216  return s8Ret;
217 }
218 
219 /*
220 * @fn nm_i2c_write_block
221 * @brief Write block of data
222 * @param [in] u32Addr
223 * Start address
224 * @param [in] puBuf
225 * Pointer to the buffer holding the data to be written
226 * @param [in] u16Sz
227 * Number of bytes to write. The buffer size must be >= u16Sz
228 * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
229 * @author M. Abdelmawla
230 * @date 11 July 2012
231 * @version 1.0
232 */
233 sint8 nm_i2c_write_block(uint32 u32Addr, uint8 *pu8Buf, uint16 u16Sz)
234 {
235  uint8 au8Buf[7];
236  tstrNmI2cSpecial strI2c;
237  sint8 s8Ret = M2M_SUCCESS;
238 
239  au8Buf[0] = 0x12;
240  au8Buf[1] = (uint8)(u32Addr >> 24);
241  au8Buf[2] = (uint8)(u32Addr >> 16);
242  au8Buf[3] = (uint8)(u32Addr >> 8);
243  au8Buf[4] = (uint8)(u32Addr);
244  au8Buf[5] = (uint8)(u16Sz >> 8);
245  au8Buf[6] = (uint8)(u16Sz);
246 
247  strI2c.pu8Buf1 = au8Buf;
248  strI2c.pu8Buf2 = pu8Buf;
249  strI2c.u16Sz1 = sizeof(au8Buf);
250  strI2c.u16Sz2 = u16Sz;
251 
253  {
254  M2M_ERR("write error\n");
255  s8Ret = M2M_ERR_BUS_FAIL;
256  }
257 
258  return s8Ret;
259 }
260 
261 #endif
262 /* EOF */
Structure holding I2C special operation parameters.
#define NM_BUS_IOCTL_R
This module contains common APIs declarations.
signed char sint8
Range of values between -128 to 127.
Definition: nm_bsp.h:111
sint8 nm_i2c_read_block(uint32 u32Addr, uint8 *puBuf, uint16 u16Sz)
sint8 nm_i2c_write_block(uint32 u32Addr, uint8 *puBuf, uint16 u16Sz)
#define M2M_SUCCESS
Definition: nm_common.h:51
This module contains WINC3400 bus wrapper APIs declarations.
unsigned short uint16
Range of values between 0 to 65535.
Definition: nm_bsp.h:96
sint8 nm_i2c_write_reg(uint32 u32Addr, uint32 u32Val)
#define M2M_ERR(...)
Definition: nm_debug.h:80
#define NM_BUS_IOCTL_W_SPECIAL
This module contains WINC3400 I2C protocol bus APIs implementation.
unsigned long uint32
Range of values between 0 to 4294967295.
Definition: nm_bsp.h:103
sint8 nm_i2c_read_reg_with_ret(uint32 u32Addr, uint32 *pu32RetVal)
unsigned char uint8
Range of values between 0 to 255.
Definition: nm_bsp.h:89
sint8 nm_bus_ioctl(uint8 u8Cmd, void *pvParameter)
uint32 nm_i2c_read_reg(uint32 u32Addr)
Structure holding I2C default operation parameters.
#define NM_BUS_IOCTL_W
#define M2M_ERR_BUS_FAIL
Definition: nm_common.h:57


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:04