Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

472 lignes
12 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief This module contains NMC1000 UART 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_UART
  43. #include "driver/source/nmuart.h"
  44. #include "bus_wrapper/include/nm_bus_wrapper.h"
  45. #define HDR_SZ 12
  46. static uint8 get_cs(uint8 *b, uint8 sz)
  47. {
  48. int i;
  49. uint8 cs = 0;
  50. for (i = 0; i < sz; i++)
  51. cs ^= b[i];
  52. return cs;
  53. }
  54. /*
  55. * @fn nm_uart_sync_cmd
  56. * @brief Check COM Port
  57. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  58. * @author Dina El Sissy
  59. * @date 13 AUG 2012
  60. * @version 1.0
  61. */
  62. sint8 nm_uart_sync_cmd(void)
  63. {
  64. tstrNmUartDefault strUart;
  65. sint8 s8Ret = -1;
  66. uint8 b[HDR_SZ + 1];
  67. uint8 rsz;
  68. uint8 onchip = 0;
  69. /*read reg*/
  70. b[0] = 0x12;
  71. rsz = 1;
  72. strUart.pu8Buf = b;
  73. strUart.u16Sz = 1;
  74. if (M2M_SUCCESS == nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) {
  75. strUart.u16Sz = rsz;
  76. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) {
  77. s8Ret = M2M_ERR_BUS_FAIL;
  78. }
  79. } else {
  80. M2M_ERR("failed to send cfg bytes\n");
  81. s8Ret = M2M_ERR_BUS_FAIL;
  82. }
  83. if (b[0] == 0x5a) {
  84. s8Ret = 0;
  85. onchip = 1;
  86. M2M_INFO("Built-in WINC1500 UART Found\n");
  87. } else if (b[0] == 0x5b) {
  88. s8Ret = 0;
  89. onchip = 0;
  90. M2M_INFO("WINC1500 Serial Bridge Found\n");
  91. }
  92. /*TODO: this should be the way we read the register since the cortus is little endian*/
  93. /**pu32RetVal = b[0] | ((uint32)b[1] << 8) | ((uint32)b[2] << 16) | ((uint32)b[3] << 24);*/
  94. if (s8Ret == M2M_SUCCESS)
  95. s8Ret = (sint8)onchip;
  96. return s8Ret;
  97. }
  98. sint8 nm_uart_read_reg_with_ret(uint32 u32Addr, uint32 *pu32RetVal)
  99. {
  100. tstrNmUartDefault strUart;
  101. sint8 s8Ret = M2M_SUCCESS;
  102. uint8 b[HDR_SZ + 1];
  103. uint8 rsz;
  104. /*read reg*/
  105. b[0] = 0xa5;
  106. b[1] = 0;
  107. b[2] = 0;
  108. b[3] = 0;
  109. b[4] = 0;
  110. b[5] = (uint8)(u32Addr & 0x000000ff);
  111. b[6] = (uint8)((u32Addr & 0x0000ff00) >> 8);
  112. b[7] = (uint8)((u32Addr & 0x00ff0000) >> 16);
  113. b[8] = (uint8)((u32Addr & 0xff000000) >> 24);
  114. b[9] = 0;
  115. b[10] = 0;
  116. b[11] = 0;
  117. b[12] = 0;
  118. b[2] = get_cs(&b[1], HDR_SZ);
  119. rsz = 4;
  120. strUart.pu8Buf = b;
  121. strUart.u16Sz = sizeof(b);
  122. if (M2M_SUCCESS == nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) {
  123. if (!nm_bus_get_chip_type()) {
  124. strUart.u16Sz = 1;
  125. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) {
  126. s8Ret = M2M_ERR_BUS_FAIL;
  127. }
  128. if (b[0] == 0xAC) {
  129. M2M_DBG("Successfully sent the command\n");
  130. strUart.u16Sz = rsz;
  131. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) {
  132. s8Ret = M2M_ERR_BUS_FAIL;
  133. }
  134. } else {
  135. s8Ret = M2M_ERR_BUS_FAIL;
  136. }
  137. } else {
  138. strUart.u16Sz = rsz;
  139. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) {
  140. s8Ret = M2M_ERR_BUS_FAIL;
  141. }
  142. }
  143. } else {
  144. M2M_ERR("failed to send cfg bytes\n");
  145. s8Ret = M2M_ERR_BUS_FAIL;
  146. }
  147. /*TODO: this should be the way we read the register since the cortus is little endian*/
  148. /**pu32RetVal = b[0] | ((uint32)b[1] << 8) | ((uint32)b[2] << 16) | ((uint32)b[3] << 24);*/
  149. *pu32RetVal = ((uint32)b[0] << 24) | ((uint32)b[1] << 16) | ((uint32)b[2] << 8) | b[3];
  150. return s8Ret;
  151. }
  152. /*
  153. * @fn nm_uart_read_reg
  154. * @brief Read register
  155. * @param [in] u32Addr
  156. * Register address
  157. * @return Register value
  158. * @author Dina El Sissy
  159. * @date 13 AUG 2012
  160. * @version 1.0
  161. */
  162. uint32 nm_uart_read_reg(uint32 u32Addr)
  163. {
  164. uint32 val;
  165. nm_uart_read_reg_with_ret(u32Addr, &val);
  166. return val;
  167. }
  168. /*
  169. * @fn nm_uart_write_reg
  170. * @brief write register
  171. * @param [in] u32Addr
  172. * Register address
  173. * @param [in] u32Val
  174. * Value to be written to the register
  175. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  176. * @author Dina El Sissy
  177. * @date 13 AUG 2012
  178. * @version 1.0
  179. */
  180. sint8 nm_uart_write_reg(uint32 u32Addr, uint32 u32Val)
  181. {
  182. tstrNmUartDefault strUart;
  183. sint8 s8Ret = M2M_SUCCESS;
  184. uint8 b[HDR_SZ + 1];
  185. /*write reg*/
  186. b[0] = 0xa5;
  187. b[1] = 1;
  188. b[2] = 0;
  189. b[3] = 0;
  190. b[4] = 0;
  191. b[5] = (uint8)(u32Addr & 0x000000ff);
  192. b[6] = (uint8)((u32Addr & 0x0000ff00) >> 8);
  193. b[7] = (uint8)((u32Addr & 0x00ff0000) >> 16);
  194. b[8] = (uint8)((u32Addr & 0xff000000) >> 24);
  195. b[9] = (uint8)(u32Val & 0x000000ff);
  196. b[10] = (uint8)((u32Val & 0x0000ff00) >> 8);
  197. b[11] = (uint8)((u32Val & 0x00ff0000) >> 16);
  198. b[12] = (uint8)((u32Val & 0xff000000) >> 24);
  199. b[2] = get_cs(&b[1], HDR_SZ);
  200. get_cs(&b[1], HDR_SZ);
  201. strUart.pu8Buf = b;
  202. strUart.u16Sz = sizeof(b);
  203. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) {
  204. M2M_ERR("write error\n");
  205. s8Ret = M2M_ERR_BUS_FAIL;
  206. } else {
  207. if (!nm_bus_get_chip_type()) {
  208. // check for the ack from the SAMD21 for the packet reception.
  209. strUart.u16Sz = 1;
  210. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) {
  211. s8Ret = M2M_ERR_BUS_FAIL;
  212. }
  213. if (b[0] == 0xAC) {
  214. M2M_DBG("Successfully sent the reg write command\n");
  215. } else {
  216. M2M_ERR("write error\n");
  217. s8Ret = M2M_ERR_BUS_FAIL;
  218. }
  219. }
  220. }
  221. return s8Ret;
  222. }
  223. /**
  224. * @fn nm_uart_read_block
  225. * @brief Read block of data
  226. * @param [in] u32Addr
  227. * Start address
  228. * @param [out] puBuf
  229. * Pointer to a buffer used to return the read data
  230. * @param [in] u16Sz
  231. * Number of bytes to read. The buffer size must be >= u16Sz
  232. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  233. * @author Dina El Sissy
  234. * @date 13 AUG 2012
  235. * @version 1.0
  236. */
  237. sint8 nm_uart_read_block(uint32 u32Addr, uint8 *pu8Buf, uint16 u16Sz)
  238. {
  239. tstrNmUartDefault strUart;
  240. sint8 s8Ret = M2M_SUCCESS;
  241. uint8 au8Buf[HDR_SZ + 1];
  242. au8Buf[0] = 0xa5;
  243. au8Buf[1] = 2;
  244. au8Buf[2] = 0;
  245. au8Buf[3] = (uint8)(u16Sz & 0x00ff);
  246. au8Buf[4] = (uint8)((u16Sz & 0xff00) >> 8);
  247. au8Buf[5] = (uint8)(u32Addr & 0x000000ff);
  248. au8Buf[6] = (uint8)((u32Addr & 0x0000ff00) >> 8);
  249. au8Buf[7] = (uint8)((u32Addr & 0x00ff0000) >> 16);
  250. au8Buf[8] = (uint8)((u32Addr & 0xff000000) >> 24);
  251. au8Buf[9] = 0;
  252. au8Buf[10] = 0;
  253. au8Buf[11] = 0;
  254. au8Buf[12] = 0;
  255. au8Buf[2] = get_cs(&au8Buf[1], HDR_SZ);
  256. strUart.pu8Buf = au8Buf;
  257. strUart.u16Sz = sizeof(au8Buf);
  258. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) {
  259. M2M_ERR("write error\n");
  260. s8Ret = M2M_ERR_BUS_FAIL;
  261. } else {
  262. if (!nm_bus_get_chip_type()) {
  263. // check for the ack from the SAMD21 for the packet reception.
  264. strUart.u16Sz = 1;
  265. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) {
  266. s8Ret = M2M_ERR_BUS_FAIL;
  267. }
  268. if (au8Buf[0] == 0xAC) {
  269. M2M_DBG("Successfully sent the block read command\n");
  270. strUart.pu8Buf = pu8Buf;
  271. strUart.u16Sz = u16Sz;
  272. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) {
  273. M2M_ERR("read error\n");
  274. s8Ret = M2M_ERR_BUS_FAIL;
  275. }
  276. } else {
  277. M2M_ERR("write error (Error sending the block read command)\n");
  278. s8Ret = M2M_ERR_BUS_FAIL;
  279. }
  280. } else {
  281. strUart.pu8Buf = pu8Buf;
  282. strUart.u16Sz = u16Sz;
  283. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) {
  284. M2M_ERR("read error\n");
  285. s8Ret = M2M_ERR_BUS_FAIL;
  286. }
  287. }
  288. }
  289. return s8Ret;
  290. }
  291. /**
  292. * @fn nm_uart_write_block
  293. * @brief Write block of data
  294. * @param [in] u32Addr
  295. * Start address
  296. * @param [in] puBuf
  297. * Pointer to the buffer holding the data to be written
  298. * @param [in] u16Sz
  299. * Number of bytes to write. The buffer size must be >= u16Sz
  300. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  301. * @author Dina El Sissy
  302. * @date 13 AUG 2012
  303. * @version 1.0
  304. */
  305. sint8 nm_uart_write_block(uint32 u32Addr, uint8 *puBuf, uint16 u16Sz)
  306. {
  307. tstrNmUartDefault strUart;
  308. sint8 s8Ret = M2M_SUCCESS;
  309. static uint8 au8Buf[HDR_SZ + 1];
  310. au8Buf[0] = 0xa5;
  311. au8Buf[1] = 3;
  312. au8Buf[2] = 0;
  313. au8Buf[3] = (uint8)(u16Sz & 0x00ff);
  314. au8Buf[4] = (uint8)((u16Sz & 0xff00) >> 8);
  315. au8Buf[5] = (uint8)(u32Addr & 0x000000ff);
  316. au8Buf[6] = (uint8)((u32Addr & 0x0000ff00) >> 8);
  317. au8Buf[7] = (uint8)((u32Addr & 0x00ff0000) >> 16);
  318. au8Buf[8] = (uint8)((u32Addr & 0xff000000) >> 24);
  319. au8Buf[9] = 0;
  320. au8Buf[10] = 0;
  321. au8Buf[11] = 0;
  322. au8Buf[12] = 0;
  323. au8Buf[2] = get_cs(&au8Buf[1], HDR_SZ);
  324. strUart.pu8Buf = au8Buf;
  325. strUart.u16Sz = sizeof(au8Buf);
  326. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) {
  327. M2M_ERR("write error\n");
  328. s8Ret = M2M_ERR_BUS_FAIL;
  329. } else {
  330. if (!nm_bus_get_chip_type()) {
  331. // check for the ack from the SAMD21 for the packet reception.
  332. strUart.u16Sz = 1;
  333. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) {
  334. s8Ret = M2M_ERR_BUS_FAIL;
  335. }
  336. if (au8Buf[0] == 0xAC) {
  337. M2M_DBG("Successfully sent the block Write command\n");
  338. strUart.pu8Buf = puBuf;
  339. strUart.u16Sz = u16Sz;
  340. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) {
  341. M2M_ERR("write error\n");
  342. s8Ret = M2M_ERR_BUS_FAIL;
  343. } else {
  344. // check for the ack from the SAMD21 for the payload reception.
  345. strUart.pu8Buf = au8Buf;
  346. strUart.u16Sz = 1;
  347. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) {
  348. s8Ret = M2M_ERR_BUS_FAIL;
  349. }
  350. if (au8Buf[0] == 0xAC) {
  351. M2M_DBG("Successfully sent the data payload\n");
  352. } else {
  353. M2M_ERR("write error\n");
  354. s8Ret = M2M_ERR_BUS_FAIL;
  355. }
  356. }
  357. } else {
  358. M2M_ERR("write error (Error sending the block write command)\n");
  359. s8Ret = M2M_ERR_BUS_FAIL;
  360. }
  361. } else {
  362. strUart.pu8Buf = puBuf;
  363. strUart.u16Sz = u16Sz;
  364. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) {
  365. M2M_ERR("write error\n");
  366. s8Ret = M2M_ERR_BUS_FAIL;
  367. }
  368. }
  369. }
  370. return s8Ret;
  371. }
  372. /**
  373. * @fn nm_uart_reconfigure
  374. * @brief Reconfigures the UART interface
  375. * @param [in] ptr
  376. * Pointer to a DWORD containing baudrate at this moment.
  377. * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure
  378. * @author Viswanathan Murugesan
  379. * @date 22 OCT 2014
  380. * @version 1.0
  381. */
  382. sint8 nm_uart_reconfigure(void *ptr)
  383. {
  384. tstrNmUartDefault strUart;
  385. sint8 s8Ret = M2M_SUCCESS;
  386. uint8 b[HDR_SZ + 1];
  387. /*write reg*/
  388. b[0] = 0xa5;
  389. b[1] = 5;
  390. b[2] = 0;
  391. b[3] = 0;
  392. b[4] = 0;
  393. b[5] = 0;
  394. b[6] = 0;
  395. b[7] = 0;
  396. b[8] = 0;
  397. b[9] = (uint8)((*(unsigned long *)ptr) & 0x000000ff);
  398. b[10] = (uint8)(((*(unsigned long *)ptr) & 0x0000ff00) >> 8);
  399. b[11] = (uint8)(((*(unsigned long *)ptr) & 0x00ff0000) >> 16);
  400. b[12] = (uint8)(((*(unsigned long *)ptr) & 0xff000000) >> 24);
  401. b[2] = get_cs(&b[1], HDR_SZ);
  402. get_cs(&b[1], HDR_SZ);
  403. strUart.pu8Buf = b;
  404. strUart.u16Sz = sizeof(b);
  405. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_W, &strUart)) {
  406. M2M_ERR("write error\n");
  407. s8Ret = M2M_ERR_BUS_FAIL;
  408. } else {
  409. if (!nm_bus_get_chip_type()) {
  410. // check for the ack from the SAMD21 for the packet reception.
  411. strUart.u16Sz = 1;
  412. if (M2M_SUCCESS != nm_bus_ioctl(NM_BUS_IOCTL_R, &strUart)) {
  413. s8Ret = M2M_ERR_BUS_FAIL;
  414. }
  415. if (b[0] == 0xAC) {
  416. M2M_DBG("Successfully sent the UART reconfigure command\n");
  417. } else {
  418. M2M_ERR("write error\n");
  419. s8Ret = M2M_ERR_BUS_FAIL;
  420. }
  421. }
  422. }
  423. return s8Ret;
  424. }
  425. #endif
  426. /* EOF */