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