Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

249 строки
7.1 KiB

  1. /*******************************************************************************
  2. This module contains WINC3400 bus APIs implementation.
  3. File Name:
  4. nmbus.c
  5. Summary:
  6. This module contains WINC3400 bus APIs implementation.
  7. Description:
  8. This module contains WINC3400 bus APIs implementation.
  9. *******************************************************************************/
  10. //DOM-IGNORE-BEGIN
  11. /*******************************************************************************
  12. * Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries.
  13. *
  14. * Subject to your compliance with these terms, you may use Microchip software
  15. * and any derivatives exclusively with Microchip products. It is your
  16. * responsibility to comply with third party license terms applicable to your
  17. * use of third party software (including open source software) that may
  18. * accompany Microchip software.
  19. *
  20. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
  21. * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
  22. * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
  23. * PARTICULAR PURPOSE.
  24. *
  25. * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
  26. * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
  27. * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
  28. * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
  29. * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
  30. * ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  31. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  32. *******************************************************************************/
  33. #include "nmbus.h"
  34. #include "nmspi.h"
  35. #define MAX_TRX_CFG_SZ 8
  36. #define NM_BUS_MAX_TRX_SZ 256
  37. /**
  38. * @struct tstrNmBusCapabilities
  39. * @brief Structure holding bus capabilities information
  40. * @sa NM_BUS_TYPE_I2C, NM_BUS_TYPE_SPI
  41. */
  42. typedef struct
  43. {
  44. uint16_t u16MaxTrxSz; /*!< Maximum transfer size. Must be >= 16 bytes*/
  45. } tstrNmBusCapabilities;
  46. tstrNmBusCapabilities egstrNmBusCapabilities =
  47. {
  48. NM_BUS_MAX_TRX_SZ
  49. };
  50. /*
  51. * @fn nm_bus_init
  52. * @brief Initialize the bus wrapper
  53. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  54. */
  55. static int8_t nm_bus_init(void *pvinit)
  56. {
  57. nm_reset();
  58. nm_sleep(1);
  59. return M2M_SUCCESS;
  60. }
  61. /*
  62. * @fn nm_bus_deinit
  63. * @brief De-initialize the bus wrapper
  64. */
  65. static int8_t nm_bus_deinit(void)
  66. {
  67. return M2M_SUCCESS;
  68. }
  69. /**
  70. * @fn nm_bus_iface_init
  71. * @brief Initialize bus interface
  72. * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  73. */
  74. int8_t nm_bus_iface_init(void *pvInitVal)
  75. {
  76. int8_t ret = M2M_SUCCESS;
  77. ret = nm_bus_init(pvInitVal);
  78. return ret;
  79. }
  80. /**
  81. * @fn nm_bus_iface_deinit
  82. * @brief Deinitialize bus interface
  83. * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  84. */
  85. int8_t nm_bus_iface_deinit(void)
  86. {
  87. int8_t ret = M2M_SUCCESS;
  88. ret = nm_bus_deinit();
  89. return ret;
  90. }
  91. /**
  92. * @fn nm_bus_reset
  93. * @brief reset bus interface
  94. * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  95. * @version 1.0
  96. */
  97. int8_t nm_bus_reset(void)
  98. {
  99. return nm_spi_reset();
  100. }
  101. /**
  102. * @fn nm_bus_iface_reconfigure
  103. * @brief reconfigure bus interface
  104. * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  105. */
  106. int8_t nm_bus_iface_reconfigure(void *ptr)
  107. {
  108. int8_t ret = M2M_SUCCESS;
  109. return ret;
  110. }
  111. /*
  112. * @fn nm_read_reg
  113. * @brief Read register
  114. * @param[in] u32Addr
  115. * Register address
  116. * @return Register value
  117. */
  118. uint32_t nm_read_reg(uint32_t u32Addr)
  119. {
  120. return nm_spi_read_reg(u32Addr);
  121. }
  122. /*
  123. * @fn nm_read_reg_with_ret
  124. * @brief Read register with error code return
  125. * @param[in] u32Addr
  126. * Register address
  127. * @param[out] pu32RetVal
  128. * Pointer to u32 variable used to return the read value
  129. * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  130. */
  131. int8_t nm_read_reg_with_ret(uint32_t u32Addr, uint32_t* pu32RetVal)
  132. {
  133. return nm_spi_read_reg_with_ret(u32Addr, pu32RetVal);
  134. }
  135. /*
  136. * @fn nm_write_reg
  137. * @brief write register
  138. * @param[in] u32Addr
  139. * Register address
  140. * @param[in] u32Val
  141. * Value to be written to the register
  142. * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  143. */
  144. int8_t nm_write_reg(uint32_t u32Addr, uint32_t u32Val)
  145. {
  146. return nm_spi_write_reg(u32Addr, u32Val);
  147. }
  148. static inline int8_t p_nm_read_block(uint32_t u32Addr, uint8_t *puBuf, uint16_t u16Sz)
  149. {
  150. return nm_spi_read_block(u32Addr, puBuf, u16Sz);
  151. }
  152. /*
  153. * @fn nm_read_block
  154. * @brief Read block of data
  155. * @param[in] u32Addr
  156. * Start address
  157. * @param[out] puBuf
  158. * Pointer to a buffer used to return the read data
  159. * @param[in] u32Sz
  160. * Number of bytes to read. The buffer size must be >= u32Sz
  161. * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  162. */
  163. int8_t nm_read_block(uint32_t u32Addr, uint8_t *puBuf, uint32_t u32Sz)
  164. {
  165. uint16_t u16MaxTrxSz = egstrNmBusCapabilities.u16MaxTrxSz - MAX_TRX_CFG_SZ;
  166. uint32_t off = 0;
  167. int8_t s8Ret = M2M_SUCCESS;
  168. for(;;)
  169. {
  170. if(u32Sz <= u16MaxTrxSz)
  171. {
  172. s8Ret += p_nm_read_block(u32Addr, &puBuf[off], (uint16_t)u32Sz);
  173. break;
  174. }
  175. else
  176. {
  177. s8Ret += p_nm_read_block(u32Addr, &puBuf[off], u16MaxTrxSz);
  178. if(M2M_SUCCESS != s8Ret) break;
  179. u32Sz -= u16MaxTrxSz;
  180. off += u16MaxTrxSz;
  181. u32Addr += u16MaxTrxSz;
  182. }
  183. }
  184. return s8Ret;
  185. }
  186. static inline int8_t p_nm_write_block(uint32_t u32Addr, uint8_t *puBuf, uint16_t u16Sz)
  187. {
  188. return nm_spi_write_block(u32Addr, puBuf, u16Sz);
  189. }
  190. /**
  191. * @fn nm_write_block
  192. * @brief Write block of data
  193. * @param[in] u32Addr
  194. * Start address
  195. * @param[in] puBuf
  196. * Pointer to the buffer holding the data to be written
  197. * @param[in] u32Sz
  198. * Number of bytes to write. The buffer size must be >= u32Sz
  199. * @return @ref M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  200. */
  201. int8_t nm_write_block(uint32_t u32Addr, uint8_t *puBuf, uint32_t u32Sz)
  202. {
  203. uint16_t u16MaxTrxSz = egstrNmBusCapabilities.u16MaxTrxSz - MAX_TRX_CFG_SZ;
  204. uint32_t off = 0;
  205. int8_t s8Ret = M2M_SUCCESS;
  206. for(;;)
  207. {
  208. if(u32Sz <= u16MaxTrxSz)
  209. {
  210. s8Ret += p_nm_write_block(u32Addr, &puBuf[off], (uint16_t)u32Sz);
  211. break;
  212. }
  213. else
  214. {
  215. s8Ret += p_nm_write_block(u32Addr, &puBuf[off], u16MaxTrxSz);
  216. if(M2M_SUCCESS != s8Ret) break;
  217. u32Sz -= u16MaxTrxSz;
  218. off += u16MaxTrxSz;
  219. u32Addr += u16MaxTrxSz;
  220. }
  221. }
  222. return s8Ret;
  223. }
  224. //DOM-IGNORE-END