|
| 1 | +/* |
| 2 | + * This file is part of the Black Magic Debug project. |
| 3 | + * |
| 4 | + * Copyright (C) 2011 Black Sphere Technologies Ltd. |
| 5 | + * Written by Gareth McMullin <gareth@blacksphere.co.nz> |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +/* This file implements the SW-DP interface. */ |
| 22 | + |
| 23 | +#include "general.h" |
| 24 | +#include "platform.h" |
| 25 | +#include "timing.h" |
| 26 | +#include "rvswd.h" |
| 27 | +#include "maths_utils.h" |
| 28 | + |
| 29 | +// FIXME: reusing the SWD macros for now |
| 30 | +#if !defined(SWDIO_IN_PORT) |
| 31 | +#define SWDIO_IN_PORT SWDIO_PORT |
| 32 | +#endif |
| 33 | +#if !defined(SWDIO_IN_PIN) |
| 34 | +#define SWDIO_IN_PIN SWDIO_PIN |
| 35 | +#endif |
| 36 | + |
| 37 | +// typedef enum swdio_status_e { |
| 38 | +// SWDIO_STATUS_FLOAT = 0, |
| 39 | +// SWDIO_STATUS_DRIVE |
| 40 | +// } swdio_status_t; |
| 41 | + |
| 42 | +rvswd_proc_s rvswd_proc; |
| 43 | + |
| 44 | +// static void swdptap_turnaround(swdio_status_t dir) __attribute__((optimize(3))); |
| 45 | +// static uint32_t swdptap_seq_in(size_t clock_cycles) __attribute__((optimize(3))); |
| 46 | +// static bool swdptap_seq_in_parity(uint32_t *ret, size_t clock_cycles) __attribute__((optimize(3))); |
| 47 | +// static void swdptap_seq_out(uint32_t tms_states, size_t clock_cycles) __attribute__((optimize(3))); |
| 48 | +// static void swdptap_seq_out_parity(uint32_t tms_states, size_t clock_cycles) __attribute__((optimize(3))); |
| 49 | + |
| 50 | +void rvswd_init(void) |
| 51 | +{ |
| 52 | + // rvswd_proc.seq_in = swdptap_seq_in; |
| 53 | + // rvswd_proc.seq_in_parity = swdptap_seq_in_parity; |
| 54 | + // rvswd_proc.seq_out = swdptap_seq_out; |
| 55 | + // rvswd_proc.seq_out_parity = swdptap_seq_out_parity; |
| 56 | +} |
| 57 | + |
| 58 | +// static void swdptap_turnaround(const swdio_status_t dir) |
| 59 | +// { |
| 60 | +// static swdio_status_t olddir = SWDIO_STATUS_FLOAT; |
| 61 | +// /* Don't turnaround if direction not changing */ |
| 62 | +// if (dir == olddir) |
| 63 | +// return; |
| 64 | +// olddir = dir; |
| 65 | + |
| 66 | +// #ifdef DEBUG_SWD_BITS |
| 67 | +// DEBUG_INFO("%s", dir ? "\n-> " : "\n<- "); |
| 68 | +// #endif |
| 69 | + |
| 70 | +// if (dir == SWDIO_STATUS_FLOAT) { |
| 71 | +// SWDIO_MODE_FLOAT(); |
| 72 | +// } else |
| 73 | +// gpio_clear(SWCLK_PORT, SWCLK_PIN); |
| 74 | + |
| 75 | +// for (volatile uint32_t counter = target_clk_divider + 1; counter > 0; --counter) |
| 76 | +// continue; |
| 77 | + |
| 78 | +// gpio_set(SWCLK_PORT, SWCLK_PIN); |
| 79 | +// for (volatile uint32_t counter = target_clk_divider + 1; counter > 0; --counter) |
| 80 | +// continue; |
| 81 | + |
| 82 | +// if (dir == SWDIO_STATUS_DRIVE) { |
| 83 | +// gpio_clear(SWCLK_PORT, SWCLK_PIN); |
| 84 | +// SWDIO_MODE_DRIVE(); |
| 85 | +// } |
| 86 | +// } |
| 87 | + |
| 88 | +// static uint32_t swdptap_seq_in_clk_delay(size_t clock_cycles) __attribute__((optimize(3))); |
| 89 | + |
| 90 | +// static uint32_t swdptap_seq_in_clk_delay(const size_t clock_cycles) |
| 91 | +// { |
| 92 | +// uint32_t value = 0; |
| 93 | +// for (size_t cycle = 0; cycle < clock_cycles; ++cycle) { |
| 94 | +// gpio_clear(SWCLK_PORT, SWCLK_PIN); |
| 95 | +// value |= gpio_get(SWDIO_IN_PORT, SWDIO_IN_PIN) ? 1U << cycle : 0U; |
| 96 | +// for (volatile uint32_t counter = target_clk_divider; counter > 0; --counter) |
| 97 | +// continue; |
| 98 | +// gpio_set(SWCLK_PORT, SWCLK_PIN); |
| 99 | +// for (volatile uint32_t counter = target_clk_divider; counter > 0; --counter) |
| 100 | +// continue; |
| 101 | +// } |
| 102 | +// gpio_clear(SWCLK_PORT, SWCLK_PIN); |
| 103 | +// return value; |
| 104 | +// } |
| 105 | + |
| 106 | +// static uint32_t swdptap_seq_in_no_delay(size_t clock_cycles) __attribute__((optimize(3))); |
| 107 | + |
| 108 | +// static uint32_t swdptap_seq_in_no_delay(const size_t clock_cycles) |
| 109 | +// { |
| 110 | +// uint32_t value = 0; |
| 111 | +// for (size_t cycle = 0; cycle < clock_cycles; ++cycle) { |
| 112 | +// gpio_clear(SWCLK_PORT, SWCLK_PIN); |
| 113 | +// value |= gpio_get(SWDIO_IN_PORT, SWDIO_IN_PIN) ? 1U << cycle : 0U; |
| 114 | +// gpio_set(SWCLK_PORT, SWCLK_PIN); |
| 115 | +// __asm__("nop"); |
| 116 | +// } |
| 117 | +// gpio_clear(SWCLK_PORT, SWCLK_PIN); |
| 118 | +// return value; |
| 119 | +// } |
| 120 | + |
| 121 | +// static uint32_t swdptap_seq_in(size_t clock_cycles) |
| 122 | +// { |
| 123 | +// swdptap_turnaround(SWDIO_STATUS_FLOAT); |
| 124 | +// if (target_clk_divider != UINT32_MAX) |
| 125 | +// return swdptap_seq_in_clk_delay(clock_cycles); |
| 126 | +// else // NOLINT(readability-else-after-return) |
| 127 | +// return swdptap_seq_in_no_delay(clock_cycles); |
| 128 | +// } |
| 129 | + |
| 130 | +// static bool swdptap_seq_in_parity(uint32_t *ret, size_t clock_cycles) |
| 131 | +// { |
| 132 | +// const uint32_t result = swdptap_seq_in(clock_cycles); |
| 133 | +// for (volatile uint32_t counter = target_clk_divider + 1; counter > 0; --counter) |
| 134 | +// continue; |
| 135 | + |
| 136 | +// const bool parity = calculate_odd_parity(result); |
| 137 | +// const bool bit = gpio_get(SWDIO_IN_PORT, SWDIO_IN_PIN); |
| 138 | + |
| 139 | +// gpio_set(SWCLK_PORT, SWCLK_PIN); |
| 140 | +// for (volatile uint32_t counter = target_clk_divider + 1; counter > 0; --counter) |
| 141 | +// continue; |
| 142 | + |
| 143 | +// *ret = result; |
| 144 | +// /* Terminate the read cycle now */ |
| 145 | +// swdptap_turnaround(SWDIO_STATUS_DRIVE); |
| 146 | +// return parity != bit; |
| 147 | +// } |
| 148 | + |
| 149 | +// static void swdptap_seq_out_clk_delay(uint32_t tms_states, size_t clock_cycles) __attribute__((optimize(3))); |
| 150 | + |
| 151 | +// static void swdptap_seq_out_clk_delay(const uint32_t tms_states, const size_t clock_cycles) |
| 152 | +// { |
| 153 | +// for (size_t cycle = 0; cycle < clock_cycles; ++cycle) { |
| 154 | +// gpio_clear(SWCLK_PORT, SWCLK_PIN); |
| 155 | +// gpio_set_val(SWDIO_PORT, SWDIO_PIN, tms_states & (1 << cycle)); |
| 156 | +// for (volatile uint32_t counter = target_clk_divider; counter > 0; --counter) |
| 157 | +// continue; |
| 158 | +// gpio_set(SWCLK_PORT, SWCLK_PIN); |
| 159 | +// for (volatile uint32_t counter = target_clk_divider; counter > 0; --counter) |
| 160 | +// continue; |
| 161 | +// } |
| 162 | +// gpio_clear(SWCLK_PORT, SWCLK_PIN); |
| 163 | +// } |
| 164 | + |
| 165 | +// static void swdptap_seq_out_no_delay(uint32_t tms_states, size_t clock_cycles) __attribute__((optimize(3))); |
| 166 | + |
| 167 | +// static void swdptap_seq_out_no_delay(const uint32_t tms_states, const size_t clock_cycles) |
| 168 | +// { |
| 169 | +// for (size_t cycle = 0; cycle < clock_cycles; ++cycle) { |
| 170 | +// gpio_clear(SWCLK_PORT, SWCLK_PIN); |
| 171 | +// gpio_set_val(SWDIO_PORT, SWDIO_PIN, tms_states & (1 << cycle)); |
| 172 | +// gpio_set(SWCLK_PORT, SWCLK_PIN); |
| 173 | +// } |
| 174 | +// gpio_clear(SWCLK_PORT, SWCLK_PIN); |
| 175 | +// } |
| 176 | + |
| 177 | +// static void swdptap_seq_out(const uint32_t tms_states, const size_t clock_cycles) |
| 178 | +// { |
| 179 | +// swdptap_turnaround(SWDIO_STATUS_DRIVE); |
| 180 | +// if (target_clk_divider != UINT32_MAX) |
| 181 | +// swdptap_seq_out_clk_delay(tms_states, clock_cycles); |
| 182 | +// else |
| 183 | +// swdptap_seq_out_no_delay(tms_states, clock_cycles); |
| 184 | +// } |
| 185 | + |
| 186 | +// static void swdptap_seq_out_parity(const uint32_t tms_states, const size_t clock_cycles) |
| 187 | +// { |
| 188 | +// const bool parity = calculate_odd_parity(tms_states); |
| 189 | +// swdptap_seq_out(tms_states, clock_cycles); |
| 190 | +// gpio_set_val(SWDIO_PORT, SWDIO_PIN, parity); |
| 191 | +// for (volatile uint32_t counter = target_clk_divider + 1; counter > 0; --counter) |
| 192 | +// continue; |
| 193 | +// gpio_set(SWCLK_PORT, SWCLK_PIN); |
| 194 | +// for (volatile uint32_t counter = target_clk_divider + 1; counter > 0; --counter) |
| 195 | +// continue; |
| 196 | +// gpio_clear(SWCLK_PORT, SWCLK_PIN); |
| 197 | +// } |
0 commit comments