Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

289 linhas
7.5 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief This module contains NMC1000 bus APIs implementation.
  6. *
  7. * Copyright (c) 2015 - 2017 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. #ifndef CORTUS_APP
  42. #include "nmbus.h"
  43. #include "nmi2c.h"
  44. #include "nmspi.h"
  45. #include "nmuart.h"
  46. #define MAX_TRX_CFG_SZ 8
  47. /**
  48. * @fn nm_bus_iface_init
  49. * @brief Initialize bus interface
  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_bus_iface_init(void *pvInitVal)
  56. {
  57. sint8 ret = M2M_SUCCESS;
  58. ret = nm_bus_init(pvInitVal);
  59. return ret;
  60. }
  61. /**
  62. * @fn nm_bus_iface_deinit
  63. * @brief Deinitialize bus interface
  64. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  65. * @author Samer Sarhan
  66. * @date 07 April 2014
  67. * @version 1.0
  68. */
  69. sint8 nm_bus_iface_deinit(void)
  70. {
  71. sint8 ret = M2M_SUCCESS;
  72. ret = nm_bus_deinit();
  73. return ret;
  74. }
  75. /**
  76. * @fn nm_bus_reset
  77. * @brief reset bus interface
  78. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  79. * @version 1.0
  80. */
  81. sint8 nm_bus_reset(void)
  82. {
  83. sint8 ret = M2M_SUCCESS;
  84. #ifdef CONF_WINC_USE_UART
  85. #elif defined(CONF_WINC_USE_SPI)
  86. return nm_spi_reset();
  87. #elif defined(CONF_WINC_USE_I2C)
  88. #else
  89. #error "Plesae define bus usage"
  90. #endif
  91. return ret;
  92. }
  93. /**
  94. * @fn nm_bus_iface_reconfigure
  95. * @brief reconfigure bus interface
  96. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  97. * @author Viswanathan Murugesan
  98. * @date 22 Oct 2014
  99. * @version 1.0
  100. */
  101. sint8 nm_bus_iface_reconfigure(void *ptr)
  102. {
  103. sint8 ret = M2M_SUCCESS;
  104. #ifdef CONF_WINC_USE_UART
  105. ret = nm_uart_reconfigure(ptr);
  106. #endif
  107. return ret;
  108. }
  109. /*
  110. * @fn nm_read_reg
  111. * @brief Read register
  112. * @param [in] u32Addr
  113. * Register address
  114. * @return Register value
  115. * @author M. Abdelmawla
  116. * @date 11 July 2012
  117. * @version 1.0
  118. */
  119. uint32 nm_read_reg(uint32 u32Addr)
  120. {
  121. #ifdef CONF_WINC_USE_UART
  122. return nm_uart_read_reg(u32Addr);
  123. #elif defined(CONF_WINC_USE_SPI)
  124. return nm_spi_read_reg(u32Addr);
  125. #elif defined(CONF_WINC_USE_I2C)
  126. return nm_i2c_read_reg(u32Addr);
  127. #else
  128. #error "Plesae define bus usage"
  129. #endif
  130. }
  131. /*
  132. * @fn nm_read_reg_with_ret
  133. * @brief Read register with error code return
  134. * @param [in] u32Addr
  135. * Register address
  136. * @param [out] pu32RetVal
  137. * Pointer to u32 variable used to return the read value
  138. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  139. * @author M. Abdelmawla
  140. * @date 11 July 2012
  141. * @version 1.0
  142. */
  143. sint8 nm_read_reg_with_ret(uint32 u32Addr, uint32 *pu32RetVal)
  144. {
  145. #ifdef CONF_WINC_USE_UART
  146. return nm_uart_read_reg_with_ret(u32Addr, pu32RetVal);
  147. #elif defined(CONF_WINC_USE_SPI)
  148. return nm_spi_read_reg_with_ret(u32Addr, pu32RetVal);
  149. #elif defined(CONF_WINC_USE_I2C)
  150. return nm_i2c_read_reg_with_ret(u32Addr, pu32RetVal);
  151. #else
  152. #error "Plesae define bus usage"
  153. #endif
  154. }
  155. /*
  156. * @fn nm_write_reg
  157. * @brief write register
  158. * @param [in] u32Addr
  159. * Register address
  160. * @param [in] u32Val
  161. * Value to be written to the register
  162. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  163. * @author M. Abdelmawla
  164. * @date 11 July 2012
  165. * @version 1.0
  166. */
  167. sint8 nm_write_reg(uint32 u32Addr, uint32 u32Val)
  168. {
  169. #ifdef CONF_WINC_USE_UART
  170. return nm_uart_write_reg(u32Addr, u32Val);
  171. #elif defined(CONF_WINC_USE_SPI)
  172. return nm_spi_write_reg(u32Addr, u32Val);
  173. #elif defined(CONF_WINC_USE_I2C)
  174. return nm_i2c_write_reg(u32Addr, u32Val);
  175. #else
  176. #error "Plesae define bus usage"
  177. #endif
  178. }
  179. static sint8 p_nm_read_block(uint32 u32Addr, uint8 *puBuf, uint16 u16Sz)
  180. {
  181. #ifdef CONF_WINC_USE_UART
  182. return nm_uart_read_block(u32Addr, puBuf, u16Sz);
  183. #elif defined(CONF_WINC_USE_SPI)
  184. return nm_spi_read_block(u32Addr, puBuf, u16Sz);
  185. #elif defined(CONF_WINC_USE_I2C)
  186. return nm_i2c_read_block(u32Addr, puBuf, u16Sz);
  187. #else
  188. #error "Plesae define bus usage"
  189. #endif
  190. }
  191. /*
  192. * @fn nm_read_block
  193. * @brief Read block of data
  194. * @param [in] u32Addr
  195. * Start address
  196. * @param [out] puBuf
  197. * Pointer to a buffer used to return the read data
  198. * @param [in] u32Sz
  199. * Number of bytes to read. The buffer size must be >= u32Sz
  200. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  201. * @author M. Abdelmawla
  202. * @date 11 July 2012
  203. * @version 1.0
  204. */
  205. sint8 nm_read_block(uint32 u32Addr, uint8 *puBuf, uint32 u32Sz)
  206. {
  207. uint16 u16MaxTrxSz = egstrNmBusCapabilities.u16MaxTrxSz - MAX_TRX_CFG_SZ;
  208. uint32 off = 0;
  209. sint8 s8Ret = M2M_SUCCESS;
  210. for (;;) {
  211. if (u32Sz <= u16MaxTrxSz) {
  212. s8Ret += p_nm_read_block(u32Addr, &puBuf[off], (uint16)u32Sz);
  213. break;
  214. } else {
  215. s8Ret += p_nm_read_block(u32Addr, &puBuf[off], u16MaxTrxSz);
  216. if (M2M_SUCCESS != s8Ret)
  217. break;
  218. u32Sz -= u16MaxTrxSz;
  219. off += u16MaxTrxSz;
  220. u32Addr += u16MaxTrxSz;
  221. }
  222. }
  223. return s8Ret;
  224. }
  225. static sint8 p_nm_write_block(uint32 u32Addr, uint8 *puBuf, uint16 u16Sz)
  226. {
  227. #ifdef CONF_WINC_USE_UART
  228. return nm_uart_write_block(u32Addr, puBuf, u16Sz);
  229. #elif defined(CONF_WINC_USE_SPI)
  230. return nm_spi_write_block(u32Addr, puBuf, u16Sz);
  231. #elif defined(CONF_WINC_USE_I2C)
  232. return nm_i2c_write_block(u32Addr, puBuf, u16Sz);
  233. #else
  234. #error "Plesae define bus usage"
  235. #endif
  236. }
  237. /**
  238. * @fn nm_write_block
  239. * @brief Write block of data
  240. * @param [in] u32Addr
  241. * Start address
  242. * @param [in] puBuf
  243. * Pointer to the buffer holding the data to be written
  244. * @param [in] u32Sz
  245. * Number of bytes to write. The buffer size must be >= u32Sz
  246. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  247. * @author M. Abdelmawla
  248. * @date 11 July 2012
  249. * @version 1.0
  250. */
  251. sint8 nm_write_block(uint32 u32Addr, uint8 *puBuf, uint32 u32Sz)
  252. {
  253. uint16 u16MaxTrxSz = egstrNmBusCapabilities.u16MaxTrxSz - MAX_TRX_CFG_SZ;
  254. uint32 off = 0;
  255. sint8 s8Ret = M2M_SUCCESS;
  256. for (;;) {
  257. if (u32Sz <= u16MaxTrxSz) {
  258. s8Ret += p_nm_write_block(u32Addr, &puBuf[off], (uint16)u32Sz);
  259. break;
  260. } else {
  261. s8Ret += p_nm_write_block(u32Addr, &puBuf[off], u16MaxTrxSz);
  262. if (M2M_SUCCESS != s8Ret)
  263. break;
  264. u32Sz -= u16MaxTrxSz;
  265. off += u16MaxTrxSz;
  266. u32Addr += u16MaxTrxSz;
  267. }
  268. }
  269. return s8Ret;
  270. }
  271. #endif