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

215 строки
5.3 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief This module contains NMC1000 bus wrapper 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 <stdio.h>
  42. #include "bsp/include/nm_bsp.h"
  43. #include "common/include/nm_common.h"
  44. #include "bus_wrapper/include/nm_bus_wrapper.h"
  45. #include "define.h"
  46. #include "BoardCfg.h"
  47. #include "SPI.h"
  48. //#include "atmel_start.h"
  49. //#include "winc_init.h"
  50. #define NM_BUS_MAX_TRX_SZ 256
  51. tstrNmBusCapabilities egstrNmBusCapabilities = {NM_BUS_MAX_TRX_SZ};
  52. unsigned char mWifiSPIBaudrate;
  53. sint8 nm_spi_rw(uint8 *pu8Mosi, uint8 *pu8Miso, uint16 u16Sz)
  54. {
  55. uint8 u8Dummy = 0;
  56. uint8 u8SkipMosi = 0, u8SkipMiso = 0;
  57. if (!pu8Mosi)
  58. {
  59. pu8Mosi = &u8Dummy;
  60. u8SkipMosi = 1;
  61. }
  62. else if (!pu8Miso)
  63. {
  64. pu8Miso = &u8Dummy;
  65. u8SkipMiso = 1;
  66. }
  67. else
  68. {
  69. return M2M_ERR_BUS_FAIL;
  70. }
  71. if (!u8SkipMiso)
  72. {
  73. while(u16Sz-- != 0)
  74. {
  75. uint8 tmp;
  76. WIFI_SPI_SS_PIN = 0;
  77. tmp = SPITransaction(0x00,mWifiSPIBaudrate);
  78. WIFI_SPI_SS_PIN = 1;
  79. //tmp = SPITransaction(0xDE);
  80. *pu8Miso++ = tmp;
  81. }
  82. }
  83. if (!u8SkipMosi)
  84. {
  85. while(u16Sz-- != 0)
  86. {
  87. uint8 tmp;
  88. WIFI_SPI_SS_PIN = 0;
  89. tmp = SPITransaction(*pu8Mosi++,mWifiSPIBaudrate); //assign to tmp for debug purposes only.
  90. WIFI_SPI_SS_PIN = 1;
  91. }
  92. }
  93. WIFI_SPI_SS_PIN = 1;
  94. /*uint8 u8Dummy = 0;
  95. uint8 u8SkipMosi = 0, u8SkipMiso = 0;
  96. if (!pu8Mosi)
  97. {
  98. pu8Mosi = &u8Dummy;
  99. u8SkipMosi = 1;
  100. }
  101. else if (!pu8Miso)
  102. {
  103. pu8Miso = &u8Dummy;
  104. u8SkipMiso = 1;
  105. }
  106. else
  107. {
  108. return M2M_ERR_BUS_FAIL;
  109. }
  110. gpio_set_pin_level(CONF_WINC_PIN_CHIP_SELECT, false);
  111. if (!u8SkipMiso) {
  112. io_read(io, pu8Miso, u16Sz);
  113. }
  114. if (!u8SkipMosi) {
  115. io_write(io, pu8Mosi, u16Sz);
  116. }
  117. gpio_set_pin_level(CONF_WINC_PIN_CHIP_SELECT, true);*/
  118. return M2M_SUCCESS;
  119. }
  120. /*
  121. * @fn nm_bus_init
  122. * @brief Initialize the bus wrapper
  123. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  124. */
  125. //sint8 nm_bus_init(void *pvinit)
  126. sint8 nm_bus_init(uint8 *req_com_port, uint32 req_serial_number)
  127. {
  128. sint8 result = M2M_SUCCESS;
  129. mWifiSPIBaudrate = SPICalculateBRG(PERIPHERAL_FREQ, WIFI_MODULE_SPI_BAUDRATE);
  130. nm_bsp_reset();
  131. nm_bsp_sleep(1);
  132. return result;
  133. }
  134. //JFM The SPI module has been initialized in InitBoard()
  135. //so the two following lines are not needed.
  136. /* spi_m_sync_get_io_descriptor(spi_instance, &io);
  137. spi_m_sync_enable(spi_instance);/*
  138. nm_bsp_reset();
  139. nm_bsp_sleep(1);
  140. return result;
  141. }
  142. /*
  143. * @fn nm_bus_ioctl
  144. * @brief send/receive from the bus
  145. * @param[IN] u8Cmd
  146. * IOCTL command for the operation
  147. * @param[IN] pvParameter
  148. * Arbitrary parameter depenging on IOCTL
  149. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  150. * @note For SPI only, it's important to be able to send/receive at the same time
  151. */
  152. sint8 nm_bus_ioctl(uint8 u8Cmd, void *pvParameter)
  153. {
  154. sint8 s8Ret = 0;
  155. switch (u8Cmd)
  156. {
  157. case NM_BUS_IOCTL_RW:
  158. {
  159. tstrNmSpiRw *pstrParam = (tstrNmSpiRw *)pvParameter;
  160. s8Ret = nm_spi_rw(pstrParam->pu8InBuf, pstrParam->pu8OutBuf, pstrParam->u16Sz);
  161. break;
  162. }
  163. default:
  164. {
  165. s8Ret = -1;
  166. M2M_ERR("invalide ioclt cmd\n");
  167. break;
  168. }
  169. }
  170. return s8Ret;
  171. }
  172. /*
  173. * @fn nm_bus_deinit
  174. * @brief De-initialize the bus wrapper
  175. */
  176. sint8 nm_bus_deinit(void)
  177. {
  178. sint8 result = 0;
  179. return result;
  180. }
  181. /*
  182. * @fn nm_bus_reinit
  183. * @brief re-initialize the bus wrapper
  184. * @param [in] void *config
  185. * re-init configuration data
  186. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  187. */
  188. sint8 nm_bus_reinit(void *config)
  189. {
  190. return M2M_SUCCESS;
  191. }