|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// Copyright (c) 2022, Alex Taradov <alex@taradov.com>. All rights reserved. |
| 3 | + |
| 4 | +#ifndef _DAP_CONFIG_H_ |
| 5 | +#define _DAP_CONFIG_H_ |
| 6 | + |
| 7 | +/*- Includes ----------------------------------------------------------------*/ |
| 8 | +#include <furi_hal_gpio.h> |
| 9 | + |
| 10 | +/*- Definitions -------------------------------------------------------------*/ |
| 11 | +#define DAP_CONFIG_ENABLE_JTAG |
| 12 | + |
| 13 | +#define DAP_CONFIG_DEFAULT_PORT DAP_PORT_SWD |
| 14 | +#define DAP_CONFIG_DEFAULT_CLOCK 4200000 // Hz |
| 15 | + |
| 16 | +#define DAP_CONFIG_PACKET_SIZE 64 |
| 17 | +#define DAP_CONFIG_PACKET_COUNT 1 |
| 18 | + |
| 19 | +#define DAP_CONFIG_JTAG_DEV_COUNT 8 |
| 20 | + |
| 21 | +// DAP_CONFIG_PRODUCT_STR must contain "CMSIS-DAP" to be compatible with the standard |
| 22 | +#define DAP_CONFIG_VENDOR_STR "Flipper Zero" |
| 23 | +#define DAP_CONFIG_PRODUCT_STR "Generic CMSIS-DAP Adapter" |
| 24 | +#define DAP_CONFIG_SER_NUM_STR usb_serial_number |
| 25 | +#define DAP_CONFIG_CMSIS_DAP_VER_STR "2.0.0" |
| 26 | + |
| 27 | +#define DAP_CONFIG_RESET_TARGET_FN dap_app_target_reset |
| 28 | +#define DAP_CONFIG_VENDOR_FN dap_app_vendor_cmd |
| 29 | + |
| 30 | +// Attribute to use for performance-critical functions |
| 31 | +#define DAP_CONFIG_PERFORMANCE_ATTR |
| 32 | + |
| 33 | +// A value at which dap_clock_test() produces 1 kHz output on the SWCLK pin |
| 34 | +// #define DAP_CONFIG_DELAY_CONSTANT 19000 |
| 35 | +#define DAP_CONFIG_DELAY_CONSTANT 6290 |
| 36 | + |
| 37 | +// A threshold for switching to fast clock (no added delays) |
| 38 | +// This is the frequency produced by dap_clock_test(1) on the SWCLK pin |
| 39 | +#define DAP_CONFIG_FAST_CLOCK 2400000 // Hz |
| 40 | + |
| 41 | +/*- Prototypes --------------------------------------------------------------*/ |
| 42 | +extern char usb_serial_number[16]; |
| 43 | + |
| 44 | +/*- Implementations ---------------------------------------------------------*/ |
| 45 | +extern GpioPin flipper_dap_swclk_pin; |
| 46 | +extern GpioPin flipper_dap_swdio_pin; |
| 47 | +extern GpioPin flipper_dap_reset_pin; |
| 48 | +extern GpioPin flipper_dap_tdo_pin; |
| 49 | +extern GpioPin flipper_dap_tdi_pin; |
| 50 | + |
| 51 | +extern void dap_app_vendor_cmd(uint8_t cmd); |
| 52 | +extern void dap_app_target_reset(); |
| 53 | +extern void dap_app_disconnect(); |
| 54 | +extern void dap_app_connect_swd(); |
| 55 | +extern void dap_app_connect_jtag(); |
| 56 | + |
| 57 | +//----------------------------------------------------------------------------- |
| 58 | +static inline void DAP_CONFIG_SWCLK_TCK_write(int value) { |
| 59 | + furi_hal_gpio_write(&flipper_dap_swclk_pin, value); |
| 60 | +} |
| 61 | + |
| 62 | +//----------------------------------------------------------------------------- |
| 63 | +static inline void DAP_CONFIG_SWDIO_TMS_write(int value) { |
| 64 | + furi_hal_gpio_write(&flipper_dap_swdio_pin, value); |
| 65 | +} |
| 66 | + |
| 67 | +//----------------------------------------------------------------------------- |
| 68 | +static inline void DAP_CONFIG_TDI_write(int value) { |
| 69 | +#ifdef DAP_CONFIG_ENABLE_JTAG |
| 70 | + furi_hal_gpio_write(&flipper_dap_tdi_pin, value); |
| 71 | +#else |
| 72 | + (void)value; |
| 73 | +#endif |
| 74 | +} |
| 75 | + |
| 76 | +//----------------------------------------------------------------------------- |
| 77 | +static inline void DAP_CONFIG_TDO_write(int value) { |
| 78 | +#ifdef DAP_CONFIG_ENABLE_JTAG |
| 79 | + furi_hal_gpio_write(&flipper_dap_tdo_pin, value); |
| 80 | +#else |
| 81 | + (void)value; |
| 82 | +#endif |
| 83 | +} |
| 84 | + |
| 85 | +//----------------------------------------------------------------------------- |
| 86 | +static inline void DAP_CONFIG_nTRST_write(int value) { |
| 87 | + (void)value; |
| 88 | +} |
| 89 | + |
| 90 | +//----------------------------------------------------------------------------- |
| 91 | +static inline void DAP_CONFIG_nRESET_write(int value) { |
| 92 | + furi_hal_gpio_write(&flipper_dap_reset_pin, value); |
| 93 | +} |
| 94 | + |
| 95 | +//----------------------------------------------------------------------------- |
| 96 | +static inline int DAP_CONFIG_SWCLK_TCK_read(void) { |
| 97 | + return furi_hal_gpio_read(&flipper_dap_swclk_pin); |
| 98 | +} |
| 99 | + |
| 100 | +//----------------------------------------------------------------------------- |
| 101 | +static inline int DAP_CONFIG_SWDIO_TMS_read(void) { |
| 102 | + return furi_hal_gpio_read(&flipper_dap_swdio_pin); |
| 103 | +} |
| 104 | + |
| 105 | +//----------------------------------------------------------------------------- |
| 106 | +static inline int DAP_CONFIG_TDO_read(void) { |
| 107 | +#ifdef DAP_CONFIG_ENABLE_JTAG |
| 108 | + return furi_hal_gpio_read(&flipper_dap_tdo_pin); |
| 109 | +#else |
| 110 | + return 0; |
| 111 | +#endif |
| 112 | +} |
| 113 | + |
| 114 | +//----------------------------------------------------------------------------- |
| 115 | +static inline int DAP_CONFIG_TDI_read(void) { |
| 116 | +#ifdef DAP_CONFIG_ENABLE_JTAG |
| 117 | + return furi_hal_gpio_read(&flipper_dap_tdi_pin); |
| 118 | +#else |
| 119 | + return 0; |
| 120 | +#endif |
| 121 | +} |
| 122 | + |
| 123 | +//----------------------------------------------------------------------------- |
| 124 | +static inline int DAP_CONFIG_nTRST_read(void) { |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +//----------------------------------------------------------------------------- |
| 129 | +static inline int DAP_CONFIG_nRESET_read(void) { |
| 130 | + return furi_hal_gpio_read(&flipper_dap_reset_pin); |
| 131 | +} |
| 132 | + |
| 133 | +//----------------------------------------------------------------------------- |
| 134 | +static inline void DAP_CONFIG_SWCLK_TCK_set(void) { |
| 135 | + LL_GPIO_SetOutputPin(flipper_dap_swclk_pin.port, flipper_dap_swclk_pin.pin); |
| 136 | +} |
| 137 | + |
| 138 | +//----------------------------------------------------------------------------- |
| 139 | +static inline void DAP_CONFIG_SWCLK_TCK_clr(void) { |
| 140 | + LL_GPIO_ResetOutputPin(flipper_dap_swclk_pin.port, flipper_dap_swclk_pin.pin); |
| 141 | +} |
| 142 | + |
| 143 | +//----------------------------------------------------------------------------- |
| 144 | +static inline void DAP_CONFIG_SWDIO_TMS_in(void) { |
| 145 | + LL_GPIO_SetPinMode(flipper_dap_swdio_pin.port, flipper_dap_swdio_pin.pin, LL_GPIO_MODE_INPUT); |
| 146 | +} |
| 147 | + |
| 148 | +//----------------------------------------------------------------------------- |
| 149 | +static inline void DAP_CONFIG_SWDIO_TMS_out(void) { |
| 150 | + LL_GPIO_SetPinMode(flipper_dap_swdio_pin.port, flipper_dap_swdio_pin.pin, LL_GPIO_MODE_OUTPUT); |
| 151 | +} |
| 152 | + |
| 153 | +//----------------------------------------------------------------------------- |
| 154 | +static inline void DAP_CONFIG_SETUP(void) { |
| 155 | + furi_hal_gpio_init(&flipper_dap_swdio_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 156 | + furi_hal_gpio_init(&flipper_dap_swclk_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 157 | + furi_hal_gpio_init(&flipper_dap_reset_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 158 | +#ifdef DAP_CONFIG_ENABLE_JTAG |
| 159 | + furi_hal_gpio_init(&flipper_dap_tdo_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 160 | + furi_hal_gpio_init(&flipper_dap_tdi_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 161 | +#endif |
| 162 | +} |
| 163 | + |
| 164 | +//----------------------------------------------------------------------------- |
| 165 | +static inline void DAP_CONFIG_DISCONNECT(void) { |
| 166 | + furi_hal_gpio_init(&flipper_dap_swdio_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 167 | + furi_hal_gpio_init(&flipper_dap_swclk_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 168 | + furi_hal_gpio_init(&flipper_dap_reset_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 169 | +#ifdef DAP_CONFIG_ENABLE_JTAG |
| 170 | + furi_hal_gpio_init(&flipper_dap_tdo_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 171 | + furi_hal_gpio_init(&flipper_dap_tdi_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 172 | +#endif |
| 173 | + dap_app_disconnect(); |
| 174 | +} |
| 175 | + |
| 176 | +//----------------------------------------------------------------------------- |
| 177 | +static inline void DAP_CONFIG_CONNECT_SWD(void) { |
| 178 | + furi_hal_gpio_init( |
| 179 | + &flipper_dap_swdio_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); |
| 180 | + furi_hal_gpio_write(&flipper_dap_swdio_pin, true); |
| 181 | + |
| 182 | + furi_hal_gpio_init( |
| 183 | + &flipper_dap_swclk_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); |
| 184 | + furi_hal_gpio_write(&flipper_dap_swclk_pin, true); |
| 185 | + |
| 186 | + furi_hal_gpio_init( |
| 187 | + &flipper_dap_reset_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); |
| 188 | + furi_hal_gpio_write(&flipper_dap_reset_pin, true); |
| 189 | + |
| 190 | +#ifdef DAP_CONFIG_ENABLE_JTAG |
| 191 | + furi_hal_gpio_init(&flipper_dap_tdo_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 192 | + furi_hal_gpio_init(&flipper_dap_tdi_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 193 | +#endif |
| 194 | + dap_app_connect_swd(); |
| 195 | +} |
| 196 | + |
| 197 | +//----------------------------------------------------------------------------- |
| 198 | +static inline void DAP_CONFIG_CONNECT_JTAG(void) { |
| 199 | + furi_hal_gpio_init( |
| 200 | + &flipper_dap_swdio_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); |
| 201 | + furi_hal_gpio_write(&flipper_dap_swdio_pin, true); |
| 202 | + |
| 203 | + furi_hal_gpio_init( |
| 204 | + &flipper_dap_swclk_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); |
| 205 | + furi_hal_gpio_write(&flipper_dap_swclk_pin, true); |
| 206 | + |
| 207 | + furi_hal_gpio_init( |
| 208 | + &flipper_dap_reset_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); |
| 209 | + furi_hal_gpio_write(&flipper_dap_reset_pin, true); |
| 210 | + |
| 211 | +#ifdef DAP_CONFIG_ENABLE_JTAG |
| 212 | + furi_hal_gpio_init(&flipper_dap_tdo_pin, GpioModeInput, GpioPullNo, GpioSpeedVeryHigh); |
| 213 | + |
| 214 | + furi_hal_gpio_init( |
| 215 | + &flipper_dap_tdi_pin, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); |
| 216 | + furi_hal_gpio_write(&flipper_dap_tdi_pin, true); |
| 217 | +#endif |
| 218 | + dap_app_connect_jtag(); |
| 219 | +} |
| 220 | + |
| 221 | +//----------------------------------------------------------------------------- |
| 222 | +static inline void DAP_CONFIG_LED(int index, int state) { |
| 223 | + (void)index; |
| 224 | + (void)state; |
| 225 | +} |
| 226 | + |
| 227 | +//----------------------------------------------------------------------------- |
| 228 | +__attribute__((always_inline)) static inline void DAP_CONFIG_DELAY(uint32_t cycles) { |
| 229 | + asm volatile("1: subs %[cycles], %[cycles], #1 \n" |
| 230 | + " bne 1b \n" |
| 231 | + : [cycles] "+l"(cycles)); |
| 232 | +} |
| 233 | + |
| 234 | +#endif // _DAP_CONFIG_H_ |
0 commit comments