firmware.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * firmware.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. static char *
  32. hexstr(void *d, int n)
  33. {
  34. static const char * const hex = "0123456789abcdef";
  35. static char buf[96];
  36. uint8_t *p = d;
  37. char *s = buf;
  38. char c;
  39. while (n--) {
  40. c = *p++;
  41. *s++ = hex[c >> 4];
  42. *s++ = hex[c & 0xf];
  43. *s++ = ' ';
  44. }
  45. s[-1] = '\0';
  46. return buf;
  47. }
  48. void main()
  49. {
  50. bool usb_active = false;
  51. uint8_t buf[8];
  52. int cmd = 0;
  53. /* Init console IO */
  54. console_init();
  55. puts("Booting..\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. flash_manuf_id(buf);
  65. puts("Flash Manuf ID : "); puts(hexstr(buf, 3)); puts("\n");
  66. flash_unique_id(buf);
  67. puts("Flash Unique ID : "); puts(hexstr(buf, 8)); puts("\n");
  68. /* Main loop */
  69. while (1)
  70. {
  71. /* Prompt ? */
  72. if (cmd >= 0)
  73. puts("\nCommand> ");
  74. /* Poll for command */
  75. cmd = getchar_nowait();
  76. if (cmd >= 0) {
  77. if (cmd > 32 && cmd < 127)
  78. putchar(cmd);
  79. switch (cmd)
  80. {
  81. case 'd':
  82. usb_debug_print();
  83. break;
  84. case 'u':
  85. usb_active = true;
  86. usb_init();
  87. break;
  88. default:
  89. break;
  90. }
  91. }
  92. /* USB poll */
  93. if (usb_active)
  94. usb_poll();
  95. }
  96. }