fw_uploader.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. /*
  14. uploader for IOMCU partly based on px4io_uploader.cpp from px4
  15. */
  16. /****************************************************************************
  17. *
  18. * Copyright (c) 2012-2015 PX4 Development Team. All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * 1. Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * 2. Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * 3. Neither the name PX4 nor the names of its contributors may be
  31. * used to endorse or promote products derived from this software
  32. * without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  37. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  38. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  39. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  40. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  41. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  42. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  43. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  44. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  45. * POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ****************************************************************************/
  48. #include <AP_HAL/AP_HAL.h>
  49. #if HAL_WITH_IO_MCU
  50. #include "AP_IOMCU.h"
  51. #include <AP_ROMFS/AP_ROMFS.h>
  52. #include <AP_Math/crc.h>
  53. #define debug(fmt, args ...) do { hal.console->printf("IOMCU: " fmt "\n", ## args); } while(0)
  54. extern const AP_HAL::HAL &hal;
  55. /*
  56. upload a firmware to the IOMCU
  57. */
  58. bool AP_IOMCU::upload_fw(void)
  59. {
  60. // set baudrate for bootloader
  61. uart.begin(115200, 256, 256);
  62. bool ret = false;
  63. /* look for the bootloader for 150 ms */
  64. for (uint8_t i = 0; i < 15; i++) {
  65. ret = sync();
  66. if (ret) {
  67. break;
  68. }
  69. hal.scheduler->delay(10);
  70. }
  71. if (!ret) {
  72. debug("IO update failed sync");
  73. return false;
  74. }
  75. uint32_t bl_rev;
  76. ret = get_info(INFO_BL_REV, bl_rev);
  77. if (!ret) {
  78. debug("Err: failed to contact bootloader");
  79. return false;
  80. }
  81. if (bl_rev > BL_REV) {
  82. debug("Err: unsupported bootloader revision %u", unsigned(bl_rev));
  83. return false;
  84. }
  85. debug("found bootloader revision: %u", unsigned(bl_rev));
  86. ret = erase();
  87. if (!ret) {
  88. debug("erase failed");
  89. return false;
  90. }
  91. ret = program(fw_size);
  92. if (!ret) {
  93. debug("program failed");
  94. return false;
  95. }
  96. if (bl_rev <= 2) {
  97. ret = verify_rev2(fw_size);
  98. } else {
  99. ret = verify_rev3(fw_size);
  100. }
  101. if (!ret) {
  102. debug("verify failed");
  103. return false;
  104. }
  105. ret = reboot();
  106. if (!ret) {
  107. debug("reboot failed");
  108. return false;
  109. }
  110. debug("update complete");
  111. // sleep for enough time for the IO chip to boot
  112. hal.scheduler->delay(100);
  113. return true;
  114. }
  115. /*
  116. receive a byte from the IO bootloader
  117. */
  118. bool AP_IOMCU::recv_byte_with_timeout(uint8_t *c, uint32_t timeout_ms)
  119. {
  120. uint32_t start = AP_HAL::millis();
  121. do {
  122. int16_t v = uart.read();
  123. if (v >= 0) {
  124. *c = uint8_t(v);
  125. return true;
  126. }
  127. hal.scheduler->delay_microseconds(50);
  128. } while (AP_HAL::millis() - start < timeout_ms);
  129. return false;
  130. }
  131. /*
  132. receive multiple bytes from the bootloader
  133. */
  134. bool AP_IOMCU::recv_bytes(uint8_t *p, uint32_t count)
  135. {
  136. bool ret = true;
  137. while (count--) {
  138. ret = recv_byte_with_timeout(p++, 5000);
  139. if (!ret) {
  140. break;
  141. }
  142. }
  143. return ret;
  144. }
  145. /*
  146. discard any pending bytes
  147. */
  148. void AP_IOMCU::drain(void)
  149. {
  150. uint8_t c;
  151. bool ret;
  152. do {
  153. ret = recv_byte_with_timeout(&c, 40);
  154. } while (ret);
  155. }
  156. /*
  157. send a byte to the bootloader
  158. */
  159. bool AP_IOMCU::send(uint8_t c)
  160. {
  161. if (uart.write(c) != 1) {
  162. return false;
  163. }
  164. return true;
  165. }
  166. /*
  167. send a buffer to the bootloader
  168. */
  169. bool AP_IOMCU::send(const uint8_t *p, uint32_t count)
  170. {
  171. bool ret = true;
  172. while (count--) {
  173. ret = send(*p++);
  174. if (!ret) {
  175. break;
  176. }
  177. }
  178. return ret;
  179. }
  180. /*
  181. wait for bootloader protocol sync
  182. */
  183. bool AP_IOMCU::get_sync(uint32_t timeout_ms)
  184. {
  185. uint8_t c[2];
  186. bool ret;
  187. ret = recv_byte_with_timeout(c, timeout_ms);
  188. if (!ret) {
  189. return false;
  190. }
  191. ret = recv_byte_with_timeout(c + 1, timeout_ms);
  192. if (!ret) {
  193. return ret;
  194. }
  195. if ((c[0] != PROTO_INSYNC) || (c[1] != PROTO_OK)) {
  196. debug("bad sync 0x%02x,0x%02x", c[0], c[1]);
  197. return false;
  198. }
  199. return true;
  200. }
  201. /*
  202. drain then get sync with bootloader
  203. */
  204. bool AP_IOMCU::sync()
  205. {
  206. drain();
  207. /* complete any pending program operation */
  208. for (uint32_t i = 0; i < (PROG_MULTI_MAX + 6); i++) {
  209. send(0);
  210. }
  211. send(PROTO_GET_SYNC);
  212. send(PROTO_EOC);
  213. return get_sync();
  214. }
  215. /*
  216. get bootloader version
  217. */
  218. bool AP_IOMCU::get_info(uint8_t param, uint32_t &val)
  219. {
  220. bool ret;
  221. send(PROTO_GET_DEVICE);
  222. send(param);
  223. send(PROTO_EOC);
  224. ret = recv_bytes((uint8_t *)&val, sizeof(val));
  225. if (!ret) {
  226. return ret;
  227. }
  228. return get_sync();
  229. }
  230. /*
  231. erase IO firmware
  232. */
  233. bool AP_IOMCU::erase()
  234. {
  235. debug("erase...");
  236. send(PROTO_CHIP_ERASE);
  237. send(PROTO_EOC);
  238. return get_sync(10000);
  239. }
  240. /*
  241. send new firmware to bootloader
  242. */
  243. bool AP_IOMCU::program(uint32_t size)
  244. {
  245. bool ret = false;
  246. uint32_t sent = 0;
  247. if (size & 3) {
  248. return false;
  249. }
  250. debug("programming %u bytes...", (unsigned)size);
  251. while (sent < size) {
  252. /* get more bytes to program */
  253. uint32_t n = size - sent;
  254. if (n > PROG_MULTI_MAX) {
  255. n = PROG_MULTI_MAX;
  256. }
  257. send(PROTO_PROG_MULTI);
  258. send(n);
  259. send(&fw[sent], n);
  260. send(PROTO_EOC);
  261. ret = get_sync(1000);
  262. if (!ret) {
  263. debug("Failed at %u", (unsigned)sent);
  264. return false;
  265. }
  266. sent += n;
  267. }
  268. debug("upload OK");
  269. return true;
  270. }
  271. /*
  272. verify firmware for a rev2 bootloader
  273. */
  274. bool AP_IOMCU::verify_rev2(uint32_t size)
  275. {
  276. bool ret;
  277. size_t sent = 0;
  278. debug("verify...");
  279. send(PROTO_CHIP_VERIFY);
  280. send(PROTO_EOC);
  281. ret = get_sync();
  282. if (!ret) {
  283. return ret;
  284. }
  285. while (sent < size) {
  286. /* get more bytes to verify */
  287. uint32_t n = size - sent;
  288. if (n > 4) {
  289. n = 4;
  290. }
  291. send(PROTO_READ_MULTI);
  292. send(n);
  293. send(PROTO_EOC);
  294. for (uint8_t i = 0; i<n; i++) {
  295. uint8_t c;
  296. ret = recv_byte_with_timeout(&c, 5000);
  297. if (!ret) {
  298. debug("%d: got %d waiting for bytes", sent + i, ret);
  299. return ret;
  300. }
  301. if (c != fw[sent+i]) {
  302. debug("%d: got 0x%02x expected 0x%02x", sent + i, c, fw[sent+i]);
  303. return false;
  304. }
  305. }
  306. sent += n;
  307. ret = get_sync();
  308. if (!ret) {
  309. debug("timeout waiting for post-verify sync");
  310. return ret;
  311. }
  312. }
  313. return true;
  314. }
  315. /*
  316. verify firmware for a rev3 bootloader
  317. */
  318. bool AP_IOMCU::verify_rev3(uint32_t fw_size_local)
  319. {
  320. bool ret;
  321. uint32_t sum = 0;
  322. uint32_t crc = 0;
  323. uint32_t fw_size_remote;
  324. const uint8_t fill_blank = 0xff;
  325. debug("verify...");
  326. ret = get_info(INFO_FLASH_SIZE, fw_size_remote);
  327. send(PROTO_EOC);
  328. if (!ret) {
  329. debug("could not read firmware size");
  330. return ret;
  331. }
  332. sum = crc_crc32(0, fw, fw_size_local);
  333. /* fill the rest of CRC with 0xff */
  334. for (uint32_t i=0; i<fw_size_remote - fw_size_local; i++) {
  335. sum = crc_crc32(sum, &fill_blank, 1);
  336. }
  337. /* request CRC from IO */
  338. send(PROTO_GET_CRC);
  339. send(PROTO_EOC);
  340. ret = recv_bytes((uint8_t *)(&crc), sizeof(crc));
  341. if (!ret) {
  342. debug("did not receive CRC checksum");
  343. return ret;
  344. }
  345. /* compare the CRC sum from the IO with the one calculated */
  346. if (sum != crc) {
  347. debug("CRC wrong: received: 0x%x, expected: 0x%x", (unsigned)crc, (unsigned)sum);
  348. return false;
  349. }
  350. crc_is_ok = true;
  351. return true;
  352. }
  353. /*
  354. reboot IO MCU
  355. */
  356. bool AP_IOMCU::reboot()
  357. {
  358. send(PROTO_REBOOT);
  359. hal.scheduler->delay(200);
  360. send(PROTO_EOC);
  361. hal.scheduler->delay(200);
  362. return true;
  363. }
  364. #endif // HAL_WITH_IO_MCU