選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

259 行
6.8 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief This module contains NMC1000 I2C protocol bus APIs implementation.
  6. *
  7. * Copyright (c) 2015 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  29. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  30. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * \asf_license_stop
  39. *
  40. */
  41. #include "common/include/nm_common.h"
  42. #ifdef CONF_WINC_USE_I2C
  43. #include "nmi2c.h"
  44. #include "bus_wrapper/include/nm_bus_wrapper.h"
  45. /*
  46. * @fn nm_i2c_read_reg_with_ret
  47. * @brief Read register with error code return
  48. * @param [in] u32Addr
  49. * Register address
  50. * @param [out] pu32RetVal
  51. * Pointer to u32 variable used to return the read value
  52. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  53. * @author M. Abdelmawla
  54. * @date 11 July 2012
  55. * @version 1.0
  56. */
  57. sint8 nm_i2c_read_reg_with_ret(uint32 u32Addr, uint32 *pu32RetVal)
  58. {
  59. uint8 b[6];
  60. uint8 rsz;
  61. tstrNmI2cDefault strI2c;
  62. sint8 s8Ret = M2M_SUCCESS;
  63. if (u32Addr < 0xff) { /* clockless i2c */
  64. b[0] = 0x09;
  65. b[1] = (uint8)(u32Addr);
  66. rsz = 1;
  67. strI2c.u16Sz = 2;
  68. } else {
  69. b[0] = 0x80;
  70. b[1] = (uint8)(u32Addr >> 24);
  71. b[2] = (uint8)(u32Addr >> 16);
  72. b[3] = (uint8)(u32Addr >> 8);
  73. b[4] = (uint8)(u32Addr);
  74. b[5] = 0x04;
  75. rsz = 4;
  76. strI2c.u16Sz = 6;
  77. }
  78. strI2c.pu8Buf = b;
  79. if (M2M_SUCCESS == nm_bus_ioctl(NM_BUS_IOCTL_W, &strI2c)) {
  80. strI2c.u16Sz = rsz;
  81. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strI2c)) {
  82. // M2M_ERR("read error\n");
  83. s8Ret = M2M_ERR_BUS_FAIL;
  84. }
  85. } else {
  86. M2M_ERR("failed to send cfg bytes\n");
  87. s8Ret = M2M_ERR_BUS_FAIL;
  88. }
  89. if (rsz == 1) {
  90. *pu32RetVal = b[0];
  91. } else {
  92. *pu32RetVal = b[0] | ((uint32)b[1] << 8) | ((uint32)b[2] << 16) | ((uint32)b[3] << 24);
  93. }
  94. return s8Ret;
  95. }
  96. /*
  97. * @fn nm_i2c_read_reg
  98. * @brief Read register
  99. * @param [in] u32Addr
  100. * Register address
  101. * @return Register value
  102. * @author M. Abdelmawla
  103. * @date 11 July 2012
  104. * @version 1.0
  105. */
  106. uint32 nm_i2c_read_reg(uint32 u32Addr)
  107. {
  108. uint32 val;
  109. nm_i2c_read_reg_with_ret(u32Addr, &val);
  110. return val;
  111. }
  112. /*
  113. * @fn nm_i2c_write_reg
  114. * @brief write register
  115. * @param [in] u32Addr
  116. * Register address
  117. * @param [in] u32Val
  118. * Value to be written to the register
  119. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  120. * @author M. Abdelmawla
  121. * @date 11 July 2012
  122. * @version 1.0
  123. */
  124. sint8 nm_i2c_write_reg(uint32 u32Addr, uint32 u32Val)
  125. {
  126. tstrNmI2cDefault strI2c;
  127. uint8 b[16];
  128. sint8 s8Ret = M2M_SUCCESS;
  129. if (u32Addr < 0xff) { /* clockless i2c */
  130. b[0] = 0x19;
  131. b[1] = (uint8)(u32Addr);
  132. b[2] = (uint8)(u32Val);
  133. strI2c.u16Sz = 3;
  134. } else {
  135. b[0] = 0x90;
  136. b[1] = (uint8)(u32Addr >> 24);
  137. b[2] = (uint8)(u32Addr >> 16);
  138. b[3] = (uint8)(u32Addr >> 8);
  139. b[4] = (uint8)u32Addr;
  140. b[5] = 0x04;
  141. b[6] = (uint8)u32Val;
  142. b[7] = (uint8)(u32Val >> 8);
  143. b[8] = (uint8)(u32Val >> 16);
  144. b[9] = (uint8)(u32Val >> 24);
  145. strI2c.u16Sz = 10;
  146. }
  147. strI2c.pu8Buf = b;
  148. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strI2c)) {
  149. M2M_ERR("write error\n");
  150. s8Ret = M2M_ERR_BUS_FAIL;
  151. }
  152. return s8Ret;
  153. }
  154. /*
  155. * @fn nm_i2c_read_block
  156. * @brief Read block of data
  157. * @param [in] u32Addr
  158. * Start address
  159. * @param [out] puBuf
  160. * Pointer to a buffer used to return the read data
  161. * @param [in] u16Sz
  162. * Number of bytes to read. The buffer size must be >= u16Sz
  163. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  164. * @author M. Abdelmawla
  165. * @date 11 July 2012
  166. * @version 1.0
  167. */
  168. sint8 nm_i2c_read_block(uint32 u32Addr, uint8 *pu8Buf, uint16 u16Sz)
  169. {
  170. tstrNmI2cDefault strI2c;
  171. uint8 au8Buf[7];
  172. sint8 s8Ret = M2M_SUCCESS;
  173. au8Buf[0] = 0x02;
  174. au8Buf[1] = (uint8)(u32Addr >> 24);
  175. au8Buf[2] = (uint8)(u32Addr >> 16);
  176. au8Buf[3] = (uint8)(u32Addr >> 8);
  177. au8Buf[4] = (uint8)(u32Addr >> 0);
  178. au8Buf[5] = (uint8)(u16Sz >> 8);
  179. au8Buf[6] = (uint8)(u16Sz);
  180. strI2c.pu8Buf = au8Buf;
  181. strI2c.u16Sz = sizeof(au8Buf);
  182. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strI2c)) {
  183. M2M_ERR("write error\n");
  184. s8Ret = M2M_ERR_BUS_FAIL;
  185. } else {
  186. strI2c.pu8Buf = pu8Buf;
  187. strI2c.u16Sz = u16Sz;
  188. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strI2c)) {
  189. M2M_ERR("read error\n");
  190. s8Ret = M2M_ERR_BUS_FAIL;
  191. }
  192. }
  193. return s8Ret;
  194. }
  195. /*
  196. * @fn nm_i2c_write_block
  197. * @brief Write block of data
  198. * @param [in] u32Addr
  199. * Start address
  200. * @param [in] puBuf
  201. * Pointer to the buffer holding the data to be written
  202. * @param [in] u16Sz
  203. * Number of bytes to write. The buffer size must be >= u16Sz
  204. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  205. * @author M. Abdelmawla
  206. * @date 11 July 2012
  207. * @version 1.0
  208. */
  209. sint8 nm_i2c_write_block(uint32 u32Addr, uint8 *pu8Buf, uint16 u16Sz)
  210. {
  211. uint8 au8Buf[7];
  212. tstrNmI2cSpecial strI2c;
  213. sint8 s8Ret = M2M_SUCCESS;
  214. au8Buf[0] = 0x12;
  215. au8Buf[1] = (uint8)(u32Addr >> 24);
  216. au8Buf[2] = (uint8)(u32Addr >> 16);
  217. au8Buf[3] = (uint8)(u32Addr >> 8);
  218. au8Buf[4] = (uint8)(u32Addr);
  219. au8Buf[5] = (uint8)(u16Sz >> 8);
  220. au8Buf[6] = (uint8)(u16Sz);
  221. strI2c.pu8Buf1 = au8Buf;
  222. strI2c.pu8Buf2 = pu8Buf;
  223. strI2c.u16Sz1 = sizeof(au8Buf);
  224. strI2c.u16Sz2 = u16Sz;
  225. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W_SPECIAL, &strI2c)) {
  226. M2M_ERR("write error\n");
  227. s8Ret = M2M_ERR_BUS_FAIL;
  228. }
  229. return s8Ret;
  230. }
  231. #endif
  232. /* EOF */