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.
 
 
 
 

680 linhas
18 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief This module contains M2M host interface APIs implementation.
  6. *
  7. * Copyright (c) 2015 - 2018 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. #include "driver/source/nmbus.h"
  43. #include "bsp/include/nm_bsp.h"
  44. #include "m2m_hif.h"
  45. #include "driver/include/m2m_types.h"
  46. #include "driver/source/nmasic.h"
  47. #include "driver/include/m2m_periph.h"
  48. #if (defined NM_EDGE_INTERRUPT) && (defined NM_LEVEL_INTERRUPT)
  49. #error "only one type of interrupt NM_EDGE_INTERRUPT,NM_LEVEL_INTERRUPT"
  50. #endif
  51. #if !((defined NM_EDGE_INTERRUPT) || (defined NM_LEVEL_INTERRUPT))
  52. #error "define interrupt type NM_EDGE_INTERRUPT,NM_LEVEL_INTERRUPT"
  53. #endif
  54. #ifndef CORTUS_APP
  55. #define NMI_AHB_DATA_MEM_BASE 0x30000
  56. #define NMI_AHB_SHARE_MEM_BASE 0xd0000
  57. #define WIFI_HOST_RCV_CTRL_0 (0x1070)
  58. #define WIFI_HOST_RCV_CTRL_1 (0x1084)
  59. #define WIFI_HOST_RCV_CTRL_2 (0x1078)
  60. #define WIFI_HOST_RCV_CTRL_3 (0x106c)
  61. #define WIFI_HOST_RCV_CTRL_4 (0x150400)
  62. #define WIFI_HOST_RCV_CTRL_5 (0x1088)
  63. typedef struct {
  64. uint8 u8ChipMode;
  65. uint8 u8ChipSleep;
  66. uint8 u8HifRXDone;
  67. uint8 u8Interrupt;
  68. uint8 u8Yield;
  69. uint32 u32RxAddr;
  70. uint32 u32RxSize;
  71. tpfHifCallBack pfWifiCb;
  72. tpfHifCallBack pfIpCb;
  73. tpfHifCallBack pfOtaCb;
  74. tpfHifCallBack pfSigmaCb;
  75. tpfHifCallBack pfHifCb;
  76. tpfHifCallBack pfCryptoCb;
  77. tpfHifCallBack pfSslCb;
  78. } tstrHifContext;
  79. volatile tstrHifContext gstrHifCxt;
  80. #ifdef ETH_MODE
  81. extern void os_hook_isr(void);
  82. #endif
  83. static void isr(void)
  84. {
  85. gstrHifCxt.u8Interrupt++;
  86. #ifdef NM_LEVEL_INTERRUPT
  87. nm_bsp_interrupt_ctrl(0);
  88. #endif
  89. #ifdef ETH_MODE
  90. os_hook_isr();
  91. #endif
  92. }
  93. static sint8 hif_set_rx_done(void)
  94. {
  95. uint32 reg;
  96. sint8 ret = M2M_SUCCESS;
  97. gstrHifCxt.u8HifRXDone = 0;
  98. #ifdef NM_EDGE_INTERRUPT
  99. nm_bsp_interrupt_ctrl(1);
  100. #endif
  101. ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_0, &reg);
  102. if (ret != M2M_SUCCESS)
  103. goto ERR1;
  104. /* Set RX Done */
  105. reg |= NBIT1;
  106. ret = nm_write_reg(WIFI_HOST_RCV_CTRL_0, reg);
  107. if (ret != M2M_SUCCESS)
  108. goto ERR1;
  109. #ifdef NM_LEVEL_INTERRUPT
  110. nm_bsp_interrupt_ctrl(1);
  111. #endif
  112. ERR1:
  113. return ret;
  114. }
  115. /**
  116. * @fn static void m2m_hif_cb(uint8 u8OpCode, uint16 u16DataSize, uint32 u32Addr)
  117. * @brief WiFi call back function
  118. * @param [in] u8OpCode
  119. * HIF Opcode type.
  120. * @param [in] u16DataSize
  121. * HIF data length.
  122. * @param [in] u32Addr
  123. * HIF address.
  124. * @param [in] grp
  125. * HIF group type.
  126. * @author
  127. * @date
  128. * @version 1.0
  129. */
  130. static void m2m_hif_cb(uint8 u8OpCode, uint16 u16DataSize, uint32 u32Addr)
  131. {
  132. }
  133. /**
  134. * @fn NMI_API sint8 hif_chip_wake(void);
  135. * @brief To Wakeup the chip.
  136. * @return The function shall return ZERO for successful operation and a negative value otherwise.
  137. */
  138. sint8 hif_chip_wake(void)
  139. {
  140. sint8 ret = M2M_SUCCESS;
  141. if (gstrHifCxt.u8HifRXDone) {
  142. /*chip already wake for the rx not done no need to send wake request*/
  143. return ret;
  144. }
  145. if (gstrHifCxt.u8ChipSleep == 0) {
  146. if (gstrHifCxt.u8ChipMode != M2M_NO_PS) {
  147. ret = chip_wake();
  148. if (ret != M2M_SUCCESS)
  149. goto ERR1;
  150. } else {
  151. }
  152. }
  153. gstrHifCxt.u8ChipSleep++;
  154. ERR1:
  155. return ret;
  156. }
  157. /*!
  158. @fn \
  159. NMI_API void hif_set_sleep_mode(uint8 u8Pstype);
  160. @brief
  161. Set the sleep mode of the HIF layer.
  162. @param [in] u8Pstype
  163. Sleep mode.
  164. @return
  165. The function SHALL return 0 for success and a negative value otherwise.
  166. */
  167. void hif_set_sleep_mode(uint8 u8Pstype)
  168. {
  169. gstrHifCxt.u8ChipMode = u8Pstype;
  170. }
  171. /*!
  172. @fn \
  173. NMI_API uint8 hif_get_sleep_mode(void);
  174. @brief
  175. Get the sleep mode of the HIF layer.
  176. @return
  177. The function SHALL return the sleep mode of the HIF layer.
  178. */
  179. uint8 hif_get_sleep_mode(void)
  180. {
  181. return gstrHifCxt.u8ChipMode;
  182. }
  183. /**
  184. * @fn NMI_API sint8 hif_chip_sleep_sc(void);
  185. * @brief To clear the chip sleep but keep the chip sleep
  186. * @return The function shall return ZERO for successful operation and a negative value otherwise.
  187. */
  188. sint8 hif_chip_sleep_sc(void)
  189. {
  190. if (gstrHifCxt.u8ChipSleep >= 1) {
  191. gstrHifCxt.u8ChipSleep--;
  192. }
  193. return M2M_SUCCESS;
  194. }
  195. /**
  196. * @fn NMI_API sint8 hif_chip_sleep(void);
  197. * @brief To make the chip sleep.
  198. * @return The function shall return ZERO for successful operation and a negative value otherwise.
  199. */
  200. sint8 hif_chip_sleep(void)
  201. {
  202. sint8 ret = M2M_SUCCESS;
  203. if (gstrHifCxt.u8ChipSleep >= 1) {
  204. gstrHifCxt.u8ChipSleep--;
  205. }
  206. if (gstrHifCxt.u8ChipSleep == 0) {
  207. if (gstrHifCxt.u8ChipMode != M2M_NO_PS) {
  208. ret = chip_sleep();
  209. if (ret != M2M_SUCCESS)
  210. goto ERR1;
  211. } else {
  212. }
  213. }
  214. ERR1:
  215. return ret;
  216. }
  217. /**
  218. * @fn NMI_API sint8 hif_init(void * arg);
  219. * @brief To initialize HIF layer.
  220. * @param [in] arg
  221. * Pointer to the arguments.
  222. * @return The function shall return ZERO for successful operation and a negative value otherwise.
  223. */
  224. sint8 hif_init(void *arg)
  225. {
  226. m2m_memset((uint8 *)&gstrHifCxt, 0, sizeof(tstrHifContext));
  227. nm_bsp_register_isr(isr);
  228. hif_register_cb(M2M_REQ_GROUP_HIF, m2m_hif_cb);
  229. return M2M_SUCCESS;
  230. }
  231. /**
  232. * @fn NMI_API sint8 hif_deinit(void * arg);
  233. * @brief To De-initialize HIF layer.
  234. * @param [in] arg
  235. * Pointer to the arguments.
  236. * @return The function shall return ZERO for successful operation and a negative value otherwise.
  237. */
  238. sint8 hif_deinit(void *arg)
  239. {
  240. sint8 ret = M2M_SUCCESS;
  241. ret = hif_chip_wake();
  242. m2m_memset((uint8 *)&gstrHifCxt, 0, sizeof(tstrHifContext));
  243. return ret;
  244. }
  245. /**
  246. * @fn NMI_API sint8 hif_send(uint8 u8Gid,uint8 u8Opcode,uint8 *pu8CtrlBuf,uint16 u16CtrlBufSize,
  247. uint8 *pu8DataBuf,uint16 u16DataSize, uint16 u16DataOffset)
  248. * @brief Send packet using host interface.
  249. * @param [in] u8Gid
  250. * Group ID.
  251. * @param [in] u8Opcode
  252. * Operation ID.
  253. * @param [in] pu8CtrlBuf
  254. * Pointer to the Control buffer.
  255. * @param [in] u16CtrlBufSize
  256. Control buffer size.
  257. * @param [in] u16DataOffset
  258. Packet Data offset.
  259. * @param [in] pu8DataBuf
  260. * Packet buffer Allocated by the caller.
  261. * @param [in] u16DataSize
  262. Packet buffer size (including the HIF header).
  263. * @return The function shall return ZERO for successful operation and a negative value otherwise.
  264. */
  265. sint8 hif_send(uint8 u8Gid, uint8 u8Opcode, uint8 *pu8CtrlBuf, uint16 u16CtrlBufSize, uint8 *pu8DataBuf,
  266. uint16 u16DataSize, uint16 u16DataOffset)
  267. {
  268. sint8 ret = M2M_ERR_SEND;
  269. volatile tstrHifHdr strHif;
  270. strHif.u8Opcode = u8Opcode & (~NBIT7);
  271. strHif.u8Gid = u8Gid;
  272. strHif.u16Length = M2M_HIF_HDR_OFFSET;
  273. if (pu8DataBuf != NULL) {
  274. strHif.u16Length += u16DataOffset + u16DataSize;
  275. } else {
  276. strHif.u16Length += u16CtrlBufSize;
  277. }
  278. ret = hif_chip_wake();
  279. if (ret == M2M_SUCCESS) {
  280. volatile uint32 reg, dma_addr = 0;
  281. volatile uint16 cnt = 0;
  282. //#define OPTIMIZE_BUS
  283. /*please define in firmware also*/
  284. #ifndef OPTIMIZE_BUS
  285. reg = 0UL;
  286. reg |= (uint32)u8Gid;
  287. reg |= ((uint32)u8Opcode << 8);
  288. reg |= ((uint32)strHif.u16Length << 16);
  289. ret = nm_write_reg(NMI_STATE_REG, reg);
  290. if (M2M_SUCCESS != ret)
  291. goto ERR1;
  292. reg = 0UL;
  293. reg |= NBIT1;
  294. ret = nm_write_reg(WIFI_HOST_RCV_CTRL_2, reg);
  295. if (M2M_SUCCESS != ret)
  296. goto ERR1;
  297. #else
  298. reg = 0UL;
  299. reg |= NBIT1;
  300. reg |= ((u8Opcode & NBIT7) ? (NBIT2) : (0)); /*Data = 1 or config*/
  301. reg |= (u8Gid == M2M_REQ_GROUP_IP) ? (NBIT3) : (0); /*IP = 1 or non IP*/
  302. reg |= ((uint32)strHif.u16Length << 4); /*length of pkt max = 4096*/
  303. ret = nm_write_reg(WIFI_HOST_RCV_CTRL_2, reg);
  304. if (M2M_SUCCESS != ret)
  305. goto ERR1;
  306. #endif
  307. dma_addr = 0;
  308. for (cnt = 0; cnt < 1000; cnt++) {
  309. ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_2, (uint32 *)&reg);
  310. if (ret != M2M_SUCCESS)
  311. break;
  312. /*
  313. * If it takes too long to get a response, the slow down to
  314. * avoid back-to-back register read operations.
  315. */
  316. if (cnt >= 500) {
  317. if (cnt < 501) {
  318. M2M_INFO("Slowing down...\n");
  319. }
  320. nm_bsp_sleep(1);
  321. }
  322. if (!(reg & NBIT1)) {
  323. ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_4, (uint32 *)&dma_addr);
  324. if (ret != M2M_SUCCESS) {
  325. /*in case of read error clear the DMA address and return error*/
  326. dma_addr = 0;
  327. goto ERR1;
  328. }
  329. /*in case of success break */
  330. break;
  331. }
  332. }
  333. if (dma_addr != 0) {
  334. volatile uint32 u32CurrAddr;
  335. u32CurrAddr = dma_addr;
  336. strHif.u16Length = NM_BSP_B_L_16(strHif.u16Length);
  337. ret = nm_write_block(u32CurrAddr, (uint8 *)&strHif, M2M_HIF_HDR_OFFSET);
  338. if (M2M_SUCCESS != ret)
  339. goto ERR1;
  340. u32CurrAddr += M2M_HIF_HDR_OFFSET;
  341. if (pu8CtrlBuf != NULL) {
  342. ret = nm_write_block(u32CurrAddr, pu8CtrlBuf, u16CtrlBufSize);
  343. if (M2M_SUCCESS != ret)
  344. goto ERR1;
  345. u32CurrAddr += u16CtrlBufSize;
  346. }
  347. if (pu8DataBuf != NULL) {
  348. u32CurrAddr += (u16DataOffset - u16CtrlBufSize);
  349. ret = nm_write_block(u32CurrAddr, pu8DataBuf, u16DataSize);
  350. if (M2M_SUCCESS != ret)
  351. goto ERR1;
  352. u32CurrAddr += u16DataSize;
  353. }
  354. reg = dma_addr << 2;
  355. reg |= NBIT1;
  356. ret = nm_write_reg(WIFI_HOST_RCV_CTRL_3, reg);
  357. if (M2M_SUCCESS != ret)
  358. goto ERR1;
  359. } else {
  360. ret = hif_chip_sleep();
  361. M2M_DBG("Failed to alloc rx size %d\r", ret);
  362. ret = M2M_ERR_MEM_ALLOC;
  363. goto ERR2;
  364. }
  365. } else {
  366. M2M_ERR("(HIF)Fail to wakup the chip\n");
  367. goto ERR2;
  368. }
  369. /*actual sleep ret = M2M_SUCCESS*/
  370. ret = hif_chip_sleep();
  371. return ret;
  372. ERR1:
  373. /*reset the count but no actual sleep as it already bus error*/
  374. hif_chip_sleep_sc();
  375. ERR2:
  376. /*logical error*/
  377. return ret;
  378. }
  379. /**
  380. * @fn hif_isr
  381. * @brief Host interface interrupt service routine
  382. * @author M. Abdelmawla
  383. * @date 15 July 2012
  384. * @return 1 in case of interrupt received else 0 will be returned
  385. * @version 1.0
  386. */
  387. static sint8 hif_isr(void)
  388. {
  389. sint8 ret = M2M_SUCCESS;
  390. uint32 reg;
  391. volatile tstrHifHdr strHif;
  392. ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_0, &reg);
  393. if (M2M_SUCCESS == ret) {
  394. if (reg & 0x1) /* New interrupt has been received */
  395. {
  396. uint16 size;
  397. nm_bsp_interrupt_ctrl(0);
  398. /*Clearing RX interrupt*/
  399. reg &= ~NBIT0;
  400. ret = nm_write_reg(WIFI_HOST_RCV_CTRL_0, reg);
  401. if (ret != M2M_SUCCESS)
  402. goto ERR1;
  403. gstrHifCxt.u8HifRXDone = 1;
  404. size = (uint16)((reg >> 2) & 0xfff);
  405. if (size > 0) {
  406. uint32 address = 0;
  407. /**
  408. start bus transfer
  409. **/
  410. ret = nm_read_reg_with_ret(WIFI_HOST_RCV_CTRL_1, &address);
  411. if (M2M_SUCCESS != ret) {
  412. M2M_ERR("(hif) WIFI_HOST_RCV_CTRL_1 bus fail\n");
  413. nm_bsp_interrupt_ctrl(1);
  414. goto ERR1;
  415. }
  416. gstrHifCxt.u32RxAddr = address;
  417. gstrHifCxt.u32RxSize = size;
  418. ret = nm_read_block(address, (uint8 *)&strHif, sizeof(tstrHifHdr));
  419. strHif.u16Length = NM_BSP_B_L_16(strHif.u16Length);
  420. if (M2M_SUCCESS != ret) {
  421. M2M_ERR("(hif) address bus fail\n");
  422. nm_bsp_interrupt_ctrl(1);
  423. goto ERR1;
  424. }
  425. if (strHif.u16Length != size) {
  426. if ((size - strHif.u16Length) > 4) {
  427. M2M_ERR("(hif) Corrupted packet Size = %u <L = %u, G = %u, OP = %02X>\n",
  428. size,
  429. strHif.u16Length,
  430. strHif.u8Gid,
  431. strHif.u8Opcode);
  432. nm_bsp_interrupt_ctrl(1);
  433. ret = M2M_ERR_BUS_FAIL;
  434. goto ERR1;
  435. }
  436. }
  437. if (M2M_REQ_GROUP_WIFI == strHif.u8Gid) {
  438. if (gstrHifCxt.pfWifiCb)
  439. gstrHifCxt.pfWifiCb(
  440. strHif.u8Opcode, strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET);
  441. else
  442. M2M_ERR("WIFI callback is not registered\n");
  443. } else if (M2M_REQ_GROUP_IP == strHif.u8Gid) {
  444. if (gstrHifCxt.pfIpCb)
  445. gstrHifCxt.pfIpCb(
  446. strHif.u8Opcode, strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET);
  447. else
  448. M2M_ERR("Scoket callback is not registered\n");
  449. } else if (M2M_REQ_GROUP_OTA == strHif.u8Gid) {
  450. if (gstrHifCxt.pfOtaCb)
  451. gstrHifCxt.pfOtaCb(
  452. strHif.u8Opcode, strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET);
  453. else
  454. M2M_ERR("Ota callback is not registered\n");
  455. } else if (M2M_REQ_GROUP_CRYPTO == strHif.u8Gid) {
  456. if (gstrHifCxt.pfCryptoCb)
  457. gstrHifCxt.pfCryptoCb(
  458. strHif.u8Opcode, strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET);
  459. else
  460. M2M_ERR("Crypto callback is not registered\n");
  461. } else if (M2M_REQ_GROUP_SIGMA == strHif.u8Gid) {
  462. if (gstrHifCxt.pfSigmaCb)
  463. gstrHifCxt.pfSigmaCb(
  464. strHif.u8Opcode, strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET);
  465. else
  466. M2M_ERR("Sigma callback is not registered\n");
  467. } else if (M2M_REQ_GROUP_SSL == strHif.u8Gid) {
  468. if (gstrHifCxt.pfSslCb)
  469. gstrHifCxt.pfSslCb(
  470. strHif.u8Opcode, strHif.u16Length - M2M_HIF_HDR_OFFSET, address + M2M_HIF_HDR_OFFSET);
  471. } else {
  472. M2M_ERR("(hif) invalid group ID\n");
  473. ret = M2M_ERR_BUS_FAIL;
  474. goto ERR1;
  475. }
  476. if (gstrHifCxt.u8HifRXDone) {
  477. M2M_ERR("(hif) host app didn't set RX Done <%u><%X>\n", strHif.u8Gid, strHif.u8Opcode);
  478. ret = hif_set_rx_done();
  479. if (ret != M2M_SUCCESS)
  480. goto ERR1;
  481. }
  482. } else {
  483. M2M_ERR("(hif) Wrong Size\n");
  484. ret = M2M_ERR_RCV;
  485. goto ERR1;
  486. }
  487. } else {
  488. #ifndef WIN32
  489. M2M_ERR("(hif) False interrupt %lx", reg);
  490. ret = M2M_ERR_FAIL;
  491. goto ERR1;
  492. #else
  493. #endif
  494. }
  495. } else {
  496. M2M_ERR("(hif) Fail to Read interrupt reg\n");
  497. goto ERR1;
  498. }
  499. ERR1:
  500. return ret;
  501. }
  502. /**
  503. * @fn hif_yield(void)
  504. * @brief
  505. Yields control from interrupt event handler.
  506. */
  507. void hif_yield(void)
  508. {
  509. gstrHifCxt.u8Yield = 1;
  510. }
  511. /**
  512. * @fn hif_handle_isr(void)
  513. * @brief Handle interrupt received from NMC1500 firmware.
  514. * @return The function SHALL return 0 for success and a negative value otherwise.
  515. */
  516. sint8 hif_handle_isr(void)
  517. {
  518. sint8 ret = M2M_SUCCESS;
  519. gstrHifCxt.u8Yield = 0;
  520. while (gstrHifCxt.u8Interrupt && !gstrHifCxt.u8Yield) {
  521. /*must be at that place because of the race of interrupt increment and that decrement*/
  522. /*when the interrupt enabled*/
  523. gstrHifCxt.u8Interrupt--;
  524. while (1) {
  525. ret = hif_isr();
  526. if (ret == M2M_SUCCESS) {
  527. /*we will try forever untill we get that interrupt*/
  528. /*Fail return errors here due to bus errors (reading expected values)*/
  529. break;
  530. } else {
  531. M2M_ERR("(HIF) Fail to handle interrupt %d try Again..\n", ret);
  532. }
  533. }
  534. }
  535. return ret;
  536. }
  537. /*
  538. * @fn hif_receive
  539. * @brief Host interface interrupt serviece routine
  540. * @param [in] u32Addr
  541. * Receive start address
  542. * @param [out] pu8Buf
  543. * Pointer to receive buffer. Allocated by the caller
  544. * @param [in] u16Sz
  545. * Receive buffer size
  546. * @param [in] isDone
  547. * If you don't need any more packets send True otherwise send false
  548. * @return The function shall return ZERO for successful operation and a negative value otherwise.
  549. */
  550. sint8 hif_receive(uint32 u32Addr, uint8 *pu8Buf, uint16 u16Sz, uint8 isDone)
  551. {
  552. sint8 ret = M2M_SUCCESS;
  553. if ((u32Addr == 0) || (pu8Buf == NULL) || (u16Sz == 0)) {
  554. if (isDone) {
  555. /* set RX done */
  556. ret = hif_set_rx_done();
  557. } else {
  558. ret = M2M_ERR_FAIL;
  559. M2M_ERR(" hif_receive: Invalid argument\n");
  560. }
  561. goto ERR1;
  562. }
  563. if (u16Sz > gstrHifCxt.u32RxSize) {
  564. ret = M2M_ERR_FAIL;
  565. M2M_ERR("APP Requested Size is larger than the recived buffer size <%u><%lu>\n", u16Sz, gstrHifCxt.u32RxSize);
  566. goto ERR1;
  567. }
  568. if ((u32Addr < gstrHifCxt.u32RxAddr) || ((u32Addr + u16Sz) > (gstrHifCxt.u32RxAddr + gstrHifCxt.u32RxSize))) {
  569. ret = M2M_ERR_FAIL;
  570. M2M_ERR("APP Requested Address beyond the recived buffer address and length\n");
  571. goto ERR1;
  572. }
  573. /* Receive the payload */
  574. ret = nm_read_block(u32Addr, pu8Buf, u16Sz);
  575. if (ret != M2M_SUCCESS)
  576. goto ERR1;
  577. /* check if this is the last packet */
  578. if ((((gstrHifCxt.u32RxAddr + gstrHifCxt.u32RxSize) - (u32Addr + u16Sz)) <= 0) || isDone) {
  579. /* set RX done */
  580. ret = hif_set_rx_done();
  581. }
  582. ERR1:
  583. return ret;
  584. }
  585. /**
  586. * @fn hif_register_cb
  587. * @brief To set Callback function for every compantent Component
  588. * @param [in] u8Grp
  589. * Group to which the Callback function should be set.
  590. * @param [in] fn
  591. * function to be set
  592. * @return The function shall return ZERO for successful operation and a negative value otherwise.
  593. */
  594. sint8 hif_register_cb(uint8 u8Grp, tpfHifCallBack fn)
  595. {
  596. sint8 ret = M2M_SUCCESS;
  597. switch (u8Grp) {
  598. case M2M_REQ_GROUP_IP:
  599. gstrHifCxt.pfIpCb = fn;
  600. break;
  601. case M2M_REQ_GROUP_WIFI:
  602. gstrHifCxt.pfWifiCb = fn;
  603. break;
  604. case M2M_REQ_GROUP_OTA:
  605. gstrHifCxt.pfOtaCb = fn;
  606. break;
  607. case M2M_REQ_GROUP_HIF:
  608. gstrHifCxt.pfHifCb = fn;
  609. break;
  610. case M2M_REQ_GROUP_CRYPTO:
  611. gstrHifCxt.pfCryptoCb = fn;
  612. break;
  613. case M2M_REQ_GROUP_SIGMA:
  614. gstrHifCxt.pfSigmaCb = fn;
  615. break;
  616. case M2M_REQ_GROUP_SSL:
  617. gstrHifCxt.pfSslCb = fn;
  618. break;
  619. default:
  620. M2M_ERR("GRp ? %d\n", u8Grp);
  621. ret = M2M_ERR_FAIL;
  622. break;
  623. }
  624. return ret;
  625. }
  626. #endif