|
| 1 | +/* |
| 2 | + * This file is part of the Black Magic Debug project. |
| 3 | + * |
| 4 | + * Copyright (C) 2025 1BitSquared <[email protected]> |
| 5 | + * Written by Rachel Mant <[email protected]> |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 12 | + * list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * 3. Neither the name of the copyright holder nor the names of its |
| 19 | + * contributors may be used to endorse or promote products derived from |
| 20 | + * this software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 26 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 28 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 30 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | + */ |
| 33 | + |
| 34 | +#include <assert.h> |
| 35 | +#include "bmp_remote.h" |
| 36 | +#include "protocol_v3_defs.h" |
| 37 | +#include "protocol_v3_spi.h" |
| 38 | +#include "hex_utils.h" |
| 39 | +#include "exception.h" |
| 40 | + |
| 41 | +static const char *remote_v3_fault_to_string(const char *buffer, const ssize_t length) |
| 42 | +{ |
| 43 | + if (length < 1) |
| 44 | + return "communications failure"; |
| 45 | + switch (buffer[0]) { |
| 46 | + case REMOTE_RESP_ERR: |
| 47 | + if (length != 2) |
| 48 | + return "truncated error packet"; |
| 49 | + if (buffer[1] == REMOTE_ERROR_FAULT) |
| 50 | + return "fault occured on probe"; |
| 51 | + return "unknown error occured"; |
| 52 | + case REMOTE_RESP_NOTSUP: |
| 53 | + return "not supported"; |
| 54 | + case REMOTE_RESP_PARERR: |
| 55 | + return "parameter error in request"; |
| 56 | + default: |
| 57 | + break; |
| 58 | + } |
| 59 | + return "[BUG] impossible fault state"; |
| 60 | +} |
| 61 | + |
| 62 | +bool remote_v3_spi_init(const spi_bus_e bus) |
| 63 | +{ |
| 64 | + char buffer[REMOTE_MAX_MSG_SIZE]; |
| 65 | + /* Create the request and send it to the remote */ |
| 66 | + ssize_t length = snprintf(buffer, REMOTE_MAX_MSG_SIZE, REMOTE_SPI_BEGIN_STR, bus); |
| 67 | + platform_buffer_write(buffer, length); |
| 68 | + /* Read back the answer and check for errors */ |
| 69 | + length = platform_buffer_read(buffer, REMOTE_MAX_MSG_SIZE); |
| 70 | + if (length < 1 || buffer[0] != REMOTE_RESP_OK) { |
| 71 | + DEBUG_ERROR("Remote SPI initialisation failed, %s", remote_v3_fault_to_string(buffer, length)); |
| 72 | + return false; |
| 73 | + } |
| 74 | + DEBUG_PROBE("%s: bus %u\n", __func__, bus); |
| 75 | + return true; |
| 76 | +} |
| 77 | + |
| 78 | +bool remote_v3_spi_deinit(const spi_bus_e bus) |
| 79 | +{ |
| 80 | + char buffer[REMOTE_MAX_MSG_SIZE]; |
| 81 | + /* Create the request and send it to the remote */ |
| 82 | + ssize_t length = snprintf(buffer, REMOTE_MAX_MSG_SIZE, REMOTE_SPI_END_STR, bus); |
| 83 | + platform_buffer_write(buffer, length); |
| 84 | + /* Read back the answer and check for errors */ |
| 85 | + length = platform_buffer_read(buffer, REMOTE_MAX_MSG_SIZE); |
| 86 | + if (length < 1 || buffer[0] != REMOTE_RESP_OK) { |
| 87 | + DEBUG_ERROR("Remote SPI deinitialisation failed, %s", remote_v3_fault_to_string(buffer, length)); |
| 88 | + return false; |
| 89 | + } |
| 90 | + DEBUG_PROBE("%s: bus %u\n", __func__, bus); |
| 91 | + return true; |
| 92 | +} |
| 93 | + |
| 94 | +bool remote_v3_spi_chip_select(uint8_t device_select) |
| 95 | +{ |
| 96 | + char buffer[REMOTE_MAX_MSG_SIZE]; |
| 97 | + /* Create the request and send it to the remote */ |
| 98 | + ssize_t length = snprintf(buffer, REMOTE_MAX_MSG_SIZE, REMOTE_SPI_CHIP_SELECT_STR, device_select); |
| 99 | + platform_buffer_write(buffer, length); |
| 100 | + /* Read back the answer and check for errors */ |
| 101 | + length = platform_buffer_read(buffer, REMOTE_MAX_MSG_SIZE); |
| 102 | + if (length < 1 || buffer[0] != REMOTE_RESP_OK) { |
| 103 | + DEBUG_ERROR("Remote SPI chip select failed, %s", remote_v3_fault_to_string(buffer, length)); |
| 104 | + return false; |
| 105 | + } |
| 106 | + DEBUG_PROBE("%s: %02x\n", __func__, device_select); |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | +uint8_t remote_v3_spi_xfer(spi_bus_e bus, uint8_t value) |
| 111 | +{ |
| 112 | + char buffer[REMOTE_MAX_MSG_SIZE]; |
| 113 | + /* Create the request and send it to the remote */ |
| 114 | + ssize_t length = snprintf(buffer, REMOTE_MAX_MSG_SIZE, REMOTE_SPI_TRANSFER_STR, bus, value); |
| 115 | + platform_buffer_write(buffer, length); |
| 116 | + /* Read back the answer and check for errors */ |
| 117 | + length = platform_buffer_read(buffer, REMOTE_MAX_MSG_SIZE); |
| 118 | + if (length < 1 || buffer[0] != REMOTE_RESP_OK) { |
| 119 | + DEBUG_ERROR("Remote SPI transfer failed, %s", remote_v3_fault_to_string(buffer, length)); |
| 120 | + return UINT8_MAX; |
| 121 | + } |
| 122 | + const uint8_t result_value = (uint8_t)remote_decode_response(buffer + 1U, 2U); |
| 123 | + DEBUG_PROBE("%s: bus %u => %02x -> %02x\n", __func__, bus, value, result_value); |
| 124 | + return result_value; |
| 125 | +} |
0 commit comments