Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * fw_app.c |
| 3 | * |
| 4 | * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com> |
| 5 | * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | #include <stdbool.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include <no2usb/usb.h> |
| 13 | #include <no2usb/usb_dfu_rt.h> |
| 14 | |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 15 | #include "console.h" |
| 16 | #include "e1.h" |
| 17 | #include "led.h" |
| 18 | #include "misc.h" |
| 19 | #include "mini-printf.h" |
| 20 | #include "spi.h" |
Sylvain Munaut | 632a300 | 2020-10-29 13:25:45 +0100 | [diff] [blame] | 21 | #include "usb_e1.h" |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 22 | #include "utils.h" |
| 23 | |
| 24 | |
| 25 | extern const struct usb_stack_descriptors app_stack_desc; |
| 26 | |
| 27 | static void |
| 28 | serial_no_init() |
| 29 | { |
| 30 | uint8_t buf[8]; |
| 31 | char *id, *desc; |
| 32 | int i; |
| 33 | |
| 34 | flash_manuf_id(buf); |
| 35 | printf("Flash Manufacturer : %s\n", hexstr(buf, 3, true)); |
| 36 | |
| 37 | flash_unique_id(buf); |
| 38 | printf("Flash Unique ID : %s\n", hexstr(buf, 8, true)); |
| 39 | |
| 40 | /* Overwrite descriptor string */ |
| 41 | /* In theory in rodata ... but nothing is ro here */ |
| 42 | id = hexstr(buf, 8, false); |
| 43 | desc = (char*)app_stack_desc.str[1]; |
| 44 | for (i=0; i<16; i++) |
| 45 | desc[2 + (i << 1)] = id[i]; |
| 46 | } |
| 47 | |
| 48 | static void |
| 49 | boot_dfu(void) |
| 50 | { |
| 51 | /* Force re-enumeration */ |
| 52 | usb_disconnect(); |
| 53 | |
| 54 | /* Boot firmware */ |
Sylvain Munaut | 46d6b41 | 2020-10-29 13:19:05 +0100 | [diff] [blame] | 55 | reboot(1); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void |
| 59 | usb_dfu_rt_cb_reboot(void) |
| 60 | { |
| 61 | boot_dfu(); |
| 62 | } |
| 63 | |
| 64 | |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 65 | void main() |
| 66 | { |
| 67 | bool e1_active = false; |
| 68 | int cmd = 0; |
| 69 | |
| 70 | /* Init console IO */ |
| 71 | console_init(); |
| 72 | puts("Booting App image..\n"); |
| 73 | |
| 74 | /* LED */ |
| 75 | led_init(); |
| 76 | |
| 77 | /* SPI */ |
| 78 | spi_init(); |
Sylvain Munaut | 5e86047 | 2020-09-15 22:20:21 +0200 | [diff] [blame] | 79 | serial_no_init(); |
| 80 | |
| 81 | /* Enable LED now that we're done with SPI */ |
| 82 | e1_led_set(true, 0x00); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 83 | |
| 84 | /* Setup E1 Vref */ |
| 85 | int d = 25; |
Sylvain Munaut | 5e86047 | 2020-09-15 22:20:21 +0200 | [diff] [blame] | 86 | #if defined(BOARD_ICE1USB_PROTO_ICEBREAKER) || defined(BOARD_ICE1USB_PROTO_BITSY) |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 87 | pdm_set(PDM_E1_CT, true, 128, false); |
| 88 | pdm_set(PDM_E1_P, true, 128 - d, false); |
| 89 | pdm_set(PDM_E1_N, true, 128 + d, false); |
Sylvain Munaut | 5e86047 | 2020-09-15 22:20:21 +0200 | [diff] [blame] | 90 | #else |
| 91 | pdm_set(PDM_E1_RX0, true, 128 + d, false); |
| 92 | pdm_set(PDM_E1_RX1, true, 128 + d, false); |
| 93 | #endif |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 94 | |
| 95 | /* Setup clock tuning */ |
| 96 | pdm_set(PDM_CLK_HI, true, 2048, false); |
| 97 | pdm_set(PDM_CLK_LO, false, 0, false); |
| 98 | |
| 99 | /* Enable USB directly */ |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 100 | usb_init(&app_stack_desc); |
| 101 | usb_dfu_rt_init(); |
| 102 | usb_e1_init(); |
| 103 | |
| 104 | /* Start */ |
Harald Welte | 9469e04 | 2020-12-15 23:09:40 +0100 | [diff] [blame] | 105 | e1_init(0, 0); |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 106 | e1_active = true; |
| 107 | led_state(true); |
| 108 | usb_connect(); |
| 109 | |
| 110 | /* Main loop */ |
| 111 | while (1) |
| 112 | { |
| 113 | /* Prompt ? */ |
| 114 | if (cmd >= 0) |
| 115 | printf("Command> "); |
| 116 | |
| 117 | /* Poll for command */ |
| 118 | cmd = getchar_nowait(); |
| 119 | |
| 120 | if (cmd >= 0) { |
| 121 | if (cmd > 32 && cmd < 127) { |
| 122 | putchar(cmd); |
| 123 | putchar('\r'); |
| 124 | putchar('\n'); |
| 125 | } |
| 126 | |
| 127 | switch (cmd) |
| 128 | { |
| 129 | case 'p': |
| 130 | usb_debug_print(); |
| 131 | break; |
| 132 | case 'b': |
| 133 | boot_dfu(); |
| 134 | break; |
| 135 | case 'o': |
| 136 | e1_debug_print(false); |
| 137 | break; |
| 138 | case 'O': |
| 139 | e1_debug_print(true); |
| 140 | break; |
Sylvain Munaut | bc9f5c4 | 2020-09-14 10:22:29 +0200 | [diff] [blame] | 141 | case 'c': |
| 142 | usb_connect(); |
| 143 | break; |
| 144 | case 'd': |
| 145 | usb_disconnect(); |
| 146 | break; |
| 147 | default: |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /* USB poll */ |
| 153 | usb_poll(); |
| 154 | |
| 155 | /* E1 poll */ |
| 156 | if (e1_active) { |
| 157 | e1_poll(); |
| 158 | usb_e1_run(); |
| 159 | } |
| 160 | } |
| 161 | } |