|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 ARM Limited. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Licensed under the Apache License, Version 2.0 (the License); you may |
| 5 | + * not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include <stdio.h> |
| 17 | +#include <stdarg.h> |
| 18 | +#include "mbed.h" |
| 19 | +#include "mbed-client-cli/ns_cmdline.h" |
| 20 | +#include "mbed_config.h" |
| 21 | +#include "spi_test_common.h" |
| 22 | +#include "spi_master.h" |
| 23 | +#include "spi_slave.h" |
| 24 | +/** |
| 25 | + * Macros for setting console flow control. |
| 26 | + */ |
| 27 | +#define CONSOLE_FLOWCONTROL_RTS 1 |
| 28 | +#define CONSOLE_FLOWCONTROL_CTS 2 |
| 29 | +#define CONSOLE_FLOWCONTROL_RTSCTS 3 |
| 30 | +#define mbed_console_concat_(x) CONSOLE_FLOWCONTROL_##x |
| 31 | +#define mbed_console_concat(x) mbed_console_concat_(x) |
| 32 | +#define CONSOLE_FLOWCONTROL mbed_console_concat(MBED_CONF_TARGET_CONSOLE_UART_FLOW_CONTROL) |
| 33 | + |
| 34 | +#define SERIAL_CONSOLE_BAUD_RATE 115200 |
| 35 | + |
| 36 | +static config_test_case_t tc_config; |
| 37 | +static spi_t spi_master = { 0 }; |
| 38 | +static spi_t spi_slave = { 0 }; |
| 39 | +static DigitalOut *ss = NULL; |
| 40 | +static target_t spi_target; |
| 41 | + |
| 42 | +void cmd_ready_cb(int retcode) |
| 43 | +{ |
| 44 | + cmd_next(retcode); |
| 45 | +} |
| 46 | + |
| 47 | +void wrap_printf(const char *f, va_list a) |
| 48 | +{ |
| 49 | + vprintf(f, a); |
| 50 | +} |
| 51 | + |
| 52 | +int validate_config_callback(int argc, char *argv[]) |
| 53 | +{ |
| 54 | + int32_t duplex_buf; |
| 55 | + int32_t mode_buf; |
| 56 | + int32_t sync; |
| 57 | + int32_t buffers; |
| 58 | + int32_t target; |
| 59 | + int result; |
| 60 | + |
| 61 | + sym_count = DEFAULT_TEST_SYM_CNT; |
| 62 | + |
| 63 | + cmd_parameter_int(argc, argv, "target", &target); |
| 64 | + cmd_parameter_int(argc, argv, "symbol_size", (int32_t *) &tc_config.symbol_size); |
| 65 | + cmd_parameter_int(argc, argv, "mode", &mode_buf); |
| 66 | + cmd_parameter_int(argc, argv, "bit_ordering", (int32_t *) &tc_config.bit_ordering); |
| 67 | + cmd_parameter_int(argc, argv, "freq_hz", (int32_t *) &tc_config.freq_hz); |
| 68 | + cmd_parameter_int(argc, argv, "buffers", &buffers); |
| 69 | + cmd_parameter_bool(argc, argv, "master_tx_defined", &tc_config.master_tx_defined); |
| 70 | + cmd_parameter_bool(argc, argv, "master_rx_defined", &tc_config.master_rx_defined); |
| 71 | + cmd_parameter_bool(argc, argv, "slave_tx_defined", &tc_config.slave_tx_defined); |
| 72 | + cmd_parameter_bool(argc, argv, "slave_rx_defined", &tc_config.slave_rx_defined); |
| 73 | + cmd_parameter_bool(argc, argv, "auto_ss", &tc_config.auto_ss); |
| 74 | + cmd_parameter_int(argc, argv, "duplex", &duplex_buf); |
| 75 | + cmd_parameter_int(argc, argv, "sync", &sync); |
| 76 | + tc_config.duplex = (duplex_t) duplex_buf; |
| 77 | + tc_config.mode = (_spi_mode_t) mode_buf; |
| 78 | + tc_config.master_sync = (bool)(sync & MASTER_SYNC_BIT_MASK); |
| 79 | + tc_config.slave_sync = (bool)(sync & SLAVE_SYNC_BIT_MASK); |
| 80 | + tc_config.master_tx_cnt = (buffers == SPI_BUFFERS_MASTER_TX_LT_RX ? SHORTER_TEST_SYM_CNT : DEFAULT_TEST_SYM_CNT); |
| 81 | + tc_config.master_rx_cnt = (buffers == SPI_BUFFERS_MASTER_TX_GT_RX ? SHORTER_TEST_SYM_CNT : DEFAULT_TEST_SYM_CNT); |
| 82 | + tc_config.slave_tx_cnt = (buffers == SPI_BUFFERS_SLAVE_TX_LT_RX ? SHORTER_TEST_SYM_CNT : DEFAULT_TEST_SYM_CNT); |
| 83 | + tc_config.slave_rx_cnt = (buffers == SPI_BUFFERS_SLAVE_TX_GT_RX ? SHORTER_TEST_SYM_CNT : DEFAULT_TEST_SYM_CNT); |
| 84 | + |
| 85 | + spi_target = (target_t)target; |
| 86 | + |
| 87 | + if (buffers == SPI_BUFFERS_SHORTEST) { |
| 88 | + sym_count = SHORTEST_TEST_SYM_CNT; |
| 89 | + tc_config.master_tx_cnt = SHORTEST_TEST_SYM_CNT; |
| 90 | + tc_config.master_rx_cnt = SHORTEST_TEST_SYM_CNT; |
| 91 | + tc_config.slave_tx_cnt = SHORTEST_TEST_SYM_CNT; |
| 92 | + tc_config.slave_rx_cnt = SHORTEST_TEST_SYM_CNT; |
| 93 | + } |
| 94 | + |
| 95 | + if (buffers == SPI_BUFFERS_LONG) { |
| 96 | + sym_count = LONG_TEST_SYM_CNT; |
| 97 | + tc_config.master_tx_cnt = LONG_TEST_SYM_CNT; |
| 98 | + tc_config.master_rx_cnt = LONG_TEST_SYM_CNT; |
| 99 | + tc_config.slave_tx_cnt = LONG_TEST_SYM_CNT; |
| 100 | + tc_config.slave_rx_cnt = LONG_TEST_SYM_CNT; |
| 101 | + } |
| 102 | + |
| 103 | + if (spi_target == MASTER) { |
| 104 | + result = check_capabilities(tc_config.symbol_size, false, tc_config.duplex, tc_config.master_sync); |
| 105 | + } else { |
| 106 | + result = check_capabilities(tc_config.symbol_size, true, tc_config.duplex, tc_config.slave_sync); |
| 107 | + } |
| 108 | + |
| 109 | + return result; |
| 110 | +} |
| 111 | + |
| 112 | +int init_test_callback(int argc, char *argv[]) |
| 113 | +{ |
| 114 | + int result; |
| 115 | + |
| 116 | + if (spi_target == MASTER) { |
| 117 | + result = test_init_master(&spi_master, &tc_config, &ss); |
| 118 | + } else { |
| 119 | + result = test_init_slave(&spi_slave, &tc_config); |
| 120 | + } |
| 121 | + |
| 122 | + return result; |
| 123 | +} |
| 124 | + |
| 125 | +int exec_test_callback(int argc, char *argv[]) |
| 126 | +{ |
| 127 | + int result; |
| 128 | + |
| 129 | + if (spi_target == MASTER) { |
| 130 | + result = test_transfer_master(&spi_master, &tc_config, ss); |
| 131 | + } else { |
| 132 | + result = test_transfer_slave(&spi_slave, &tc_config); |
| 133 | + } |
| 134 | + |
| 135 | + return result; |
| 136 | +} |
| 137 | + |
| 138 | +int finish_test_callback(int argc, char *argv[]) |
| 139 | +{ |
| 140 | + int result; |
| 141 | + |
| 142 | + if (spi_target == MASTER) { |
| 143 | + result = test_finish_master(&spi_master, &tc_config); |
| 144 | + } else { |
| 145 | + result = test_finish_slave(&spi_slave, &tc_config); |
| 146 | + } |
| 147 | + |
| 148 | + return result; |
| 149 | +} |
| 150 | + |
| 151 | +int main() |
| 152 | +{ |
| 153 | + cmd_init(&wrap_printf); |
| 154 | + cmd_add("validate_config", validate_config_callback, "Validate if given SPI configuration can be handled by this device.", 0); |
| 155 | + cmd_add("init_test", init_test_callback, "Initialise the SPI interface.", 0); |
| 156 | + cmd_add("exec_test", exec_test_callback, "Execute SPI communication test.", 0); |
| 157 | + cmd_add("finish_test", finish_test_callback, "Deinitialise the SPI interface.", 0); |
| 158 | + |
| 159 | + int c; |
| 160 | + while ((c = getchar()) != EOF) { |
| 161 | + cmd_char_input(c); |
| 162 | + } |
| 163 | + return 0; |
| 164 | +} |
| 165 | + |
| 166 | +FileHandle *mbed::mbed_override_console(int) |
| 167 | +{ |
| 168 | + static UARTSerial console(STDIO_UART_TX, STDIO_UART_RX, SERIAL_CONSOLE_BAUD_RATE); |
| 169 | +#if CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_RTS |
| 170 | + console.set_flow_control(SerialBase::RTS, STDIO_UART_RTS, NC); |
| 171 | +#elif CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_CTS |
| 172 | + console.set_flow_control(SerialBase::CTS, NC, STDIO_UART_CTS); |
| 173 | +#elif CONSOLE_FLOWCONTROL == CONSOLE_FLOWCONTROL_RTSCTS |
| 174 | + console.set_flow_control(SerialBase::RTSCTS, STDIO_UART_RTS, STDIO_UART_CTS); |
| 175 | +#endif |
| 176 | + return &console; |
| 177 | +} |
| 178 | + |
0 commit comments