fw_app.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * fw_app.c
  3. *
  4. * Copyright (C) 2019 Sylvain Munaut
  5. * All rights reserved.
  6. *
  7. * LGPL v3+, see LICENSE.lgpl3
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #include <stdint.h>
  24. #include <stdbool.h>
  25. #include <string.h>
  26. #include "console.h"
  27. #include "led.h"
  28. #include "mini-printf.h"
  29. #include "spi.h"
  30. #include "usb.h"
  31. #include "utils.h"
  32. extern const struct usb_stack_descriptors app_stack_desc;
  33. static void
  34. serial_no_init()
  35. {
  36. uint8_t buf[8];
  37. char *id, *desc;
  38. int i;
  39. flash_manuf_id(buf);
  40. printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true));
  41. flash_unique_id(buf);
  42. printf("Flash Unique ID : %s\n", hexstr(buf, 8, true));
  43. /* Overwrite descriptor string */
  44. /* In theory in rodata ... but nothing is ro here */
  45. id = hexstr(buf, 8, false);
  46. desc = (char*)app_stack_desc.str[1];
  47. for (i=0; i<16; i++)
  48. desc[2 + (i << 1)] = id[i];
  49. }
  50. void main()
  51. {
  52. int cmd = 0;
  53. /* Init console IO */
  54. console_init();
  55. puts("Booting App image..\n");
  56. /* LED */
  57. led_init();
  58. led_color(48, 96, 5);
  59. led_blink(true, 200, 1000);
  60. led_breathe(true, 100, 200);
  61. led_state(true);
  62. /* SPI */
  63. spi_init();
  64. /* Enable USB directly */
  65. serial_no_init();
  66. usb_init(&app_stack_desc);
  67. /* Main loop */
  68. while (1)
  69. {
  70. /* Prompt ? */
  71. if (cmd >= 0)
  72. printf("Command> ");
  73. /* Poll for command */
  74. cmd = getchar_nowait();
  75. if (cmd >= 0) {
  76. if (cmd > 32 && cmd < 127) {
  77. putchar(cmd);
  78. putchar('\r');
  79. putchar('\n');
  80. }
  81. switch (cmd)
  82. {
  83. case 'p':
  84. usb_debug_print();
  85. break;
  86. case 'c':
  87. usb_connect();
  88. break;
  89. case 'd':
  90. usb_disconnect();
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. /* USB poll */
  97. usb_poll();
  98. }
  99. }