Edward O'Callaghan | c9a51f1 | 2020-10-03 00:36:18 +1000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2018 Miklós Márton martonmiklosqdev@gmail.com |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <ctype.h> |
| 19 | #include <inttypes.h> |
| 20 | #include <string.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <ni845x.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "flash.h" |
| 27 | #include "programmer.h" |
| 28 | #include "spi.h" |
| 29 | |
| 30 | #define NI845x_FIND_DEVICE_NO_DEVICE_FOUND -301701 |
| 31 | |
| 32 | enum USB845x_type { |
| 33 | USB8451 = 0x7166, |
| 34 | USB8452 = 0x7514, |
| 35 | Unknown_NI845X_Device |
| 36 | }; |
| 37 | |
| 38 | enum voltage_coerce_mode { |
| 39 | USE_LOWER, |
| 40 | USE_HIGHER |
| 41 | }; |
| 42 | |
Edward O'Callaghan | c9a51f1 | 2020-10-03 00:36:18 +1000 | [diff] [blame] | 43 | static unsigned char CS_number; // use chip select 0 as default |
| 44 | static enum USB845x_type device_pid = Unknown_NI845X_Device; |
| 45 | |
| 46 | static uInt32 device_handle; |
| 47 | static NiHandle configuration_handle; |
| 48 | static uint16_t io_voltage_in_mV; |
| 49 | static bool ignore_io_voltage_limits; |
| 50 | |
Edward O'Callaghan | c9a51f1 | 2020-10-03 00:36:18 +1000 | [diff] [blame] | 51 | // USB-8452 supported voltages, keep this array in ascending order! |
| 52 | static const uint8_t usb8452_io_voltages_in_100mV[5] = { |
| 53 | kNi845x12Volts, |
| 54 | kNi845x15Volts, |
| 55 | kNi845x18Volts, |
| 56 | kNi845x25Volts, |
| 57 | kNi845x33Volts |
| 58 | }; |
| 59 | |
| 60 | /* Copied from dediprog.c */ |
| 61 | /* Might be useful for other USB devices as well. static for now. */ |
| 62 | static int parse_voltage(char *voltage) |
| 63 | { |
| 64 | char *tmp = NULL; |
| 65 | int i; |
| 66 | int millivolt = 0, fraction = 0; |
| 67 | |
| 68 | if (!voltage || !strlen(voltage)) { |
| 69 | msg_perr("Empty voltage= specified.\n"); |
| 70 | return -1; |
| 71 | } |
| 72 | millivolt = (int)strtol(voltage, &tmp, 0); |
| 73 | voltage = tmp; |
| 74 | /* Handle "," and "." as decimal point. Everything after it is assumed |
| 75 | * to be in decimal notation. |
| 76 | */ |
| 77 | if ((*voltage == '.') || (*voltage == ',')) { |
| 78 | voltage++; |
| 79 | for (i = 0; i < 3; i++) { |
| 80 | fraction *= 10; |
| 81 | /* Don't advance if the current character is invalid, |
| 82 | * but continue multiplying. |
| 83 | */ |
| 84 | if ((*voltage < '0') || (*voltage > '9')) |
| 85 | continue; |
| 86 | fraction += *voltage - '0'; |
| 87 | voltage++; |
| 88 | } |
| 89 | /* Throw away remaining digits. */ |
| 90 | voltage += strspn(voltage, "0123456789"); |
| 91 | } |
| 92 | /* The remaining string must be empty or "mV" or "V". */ |
| 93 | tolower_string(voltage); |
| 94 | |
| 95 | /* No unit or "V". */ |
| 96 | if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) { |
| 97 | millivolt *= 1000; |
| 98 | millivolt += fraction; |
| 99 | } else if (!strncmp(voltage, "mv", 2) || !strncmp(voltage, "millivolt", 9)) { |
| 100 | /* No adjustment. fraction is discarded. */ |
| 101 | } else { |
| 102 | /* Garbage at the end of the string. */ |
| 103 | msg_perr("Garbage voltage= specified.\n"); |
| 104 | return -1; |
| 105 | } |
| 106 | return millivolt; |
| 107 | } |
| 108 | |
| 109 | static void ni845x_report_error(const char *const func, const int32 err) |
| 110 | { |
| 111 | static char buf[1024]; |
| 112 | |
| 113 | ni845xStatusToString(err, sizeof(buf), buf); |
| 114 | msg_perr("%s failed with: %s (%d)\n", func, buf, (int)err); |
| 115 | } |
| 116 | |
| 117 | static void ni845x_report_warning(const char *const func, const int32 err) |
| 118 | { |
| 119 | static char buf[1024]; |
| 120 | |
| 121 | ni845xStatusToString(err, sizeof(buf), buf); |
| 122 | msg_pwarn("%s failed with: %s (%d)\n", func, buf, (int)err); |
| 123 | } |
| 124 | |
| 125 | /** |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 126 | * @brief ni845x_spi_open_resource |
| 127 | * @param resource_handle the resource handle returned by the ni845xFindDevice or ni845xFindDeviceNext |
| 128 | * @param opened_handle the opened handle from the ni845xOpen |
| 129 | * @return the 0 on successful competition, negative error code on failure positive code on warning |
| 130 | */ |
| 131 | static int32 ni845x_spi_open_resource(char *resource_handle, uInt32 *opened_handle) |
| 132 | { |
| 133 | // NI-845x driver loads the FPGA bitfile at the first time |
| 134 | // which can take couple seconds |
| 135 | if (device_pid == USB8452) |
| 136 | msg_pwarn("Opening NI-8452, this might take a while for the first time\n"); |
| 137 | |
| 138 | int32 tmp = ni845xOpen(resource_handle, opened_handle); |
| 139 | |
| 140 | if (tmp < 0) |
| 141 | ni845x_report_error("ni845xOpen", tmp); |
| 142 | else if (tmp > 0) |
| 143 | ni845x_report_warning("ni845xOpen", tmp); |
| 144 | return tmp; |
| 145 | } |
| 146 | |
| 147 | /** |
Edward O'Callaghan | c9a51f1 | 2020-10-03 00:36:18 +1000 | [diff] [blame] | 148 | * @param serial a null terminated string containing the serial number of the specific device or NULL |
| 149 | * @return the 0 on successful completition, negative error code on failure |
| 150 | */ |
| 151 | static int ni845x_spi_open(const char *serial, uInt32 *return_handle) |
| 152 | { |
| 153 | char resource_name[256]; |
| 154 | NiHandle device_find_handle; |
| 155 | uInt32 found_devices_count = 0; |
| 156 | int32 tmp = 0; |
| 157 | |
| 158 | unsigned int vid, pid, usb_bus; |
| 159 | unsigned long int serial_as_number; |
| 160 | int ret = -1; |
| 161 | |
| 162 | tmp = ni845xFindDevice(resource_name, &device_find_handle, &found_devices_count); |
| 163 | if (tmp != 0) { |
| 164 | // supress warning if no device found |
| 165 | if (tmp != NI845x_FIND_DEVICE_NO_DEVICE_FOUND) |
| 166 | ni845x_report_error("ni845xFindDevice", tmp); |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | for (; found_devices_count; --found_devices_count) { |
| 171 | // Read the serial number and the PID here |
| 172 | // VISA resource name format example: |
| 173 | // USB0::0x3923::0x7514::DEADBEEF::RAW |
| 174 | // where the 0x7514 is the PID |
| 175 | // and the DEADBEEF is the serial of the device |
| 176 | if (sscanf(resource_name, |
| 177 | "USB%u::0x%04X::0x%04X::%08lX::RAW", |
| 178 | &usb_bus, &vid, &pid, &serial_as_number) != 4) { |
| 179 | // malformed resource string detected |
| 180 | msg_pwarn("Warning: Unable to parse the %s NI-845x resource string.\n", |
| 181 | resource_name); |
| 182 | msg_pwarn("Please report a bug at flashrom@flashrom.org\n"); |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | device_pid = pid; |
| 187 | |
| 188 | if (!serial || strtol(serial, NULL, 16) == serial_as_number) |
| 189 | break; |
| 190 | |
| 191 | if (found_devices_count > 1) { |
| 192 | tmp = ni845xFindDeviceNext(device_find_handle, resource_name); |
| 193 | if (tmp) { |
| 194 | ni845x_report_error("ni845xFindDeviceNext", tmp); |
| 195 | goto _close_ret; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (found_devices_count) |
| 201 | ret = ni845x_spi_open_resource(resource_name, return_handle); |
| 202 | |
| 203 | _close_ret: |
| 204 | tmp = ni845xCloseFindDeviceHandle(device_find_handle); |
| 205 | if (tmp) { |
| 206 | ni845x_report_error("ni845xCloseFindDeviceHandle", tmp); |
| 207 | return -1; |
| 208 | } |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | /** |
Edward O'Callaghan | c9a51f1 | 2020-10-03 00:36:18 +1000 | [diff] [blame] | 213 | * @brief usb8452_spi_set_io_voltage sets the IO voltage for the USB-8452 devices |
| 214 | * @param requested_io_voltage_mV the desired IO voltage in mVolts |
| 215 | * @param set_io_voltage_mV the IO voltage which was set in mVolts |
| 216 | * @param coerce_mode if set to USE_LOWER the closest supported IO voltage which is lower or equal to |
| 217 | * the requested_io_voltage_mV will be selected. Otherwise the next closest supported voltage will be choosen |
| 218 | * which is higher or equal to the requested_io_voltage_mV. |
| 219 | * @return 0 on success, negative on error, positive on warning |
| 220 | */ |
| 221 | static int usb8452_spi_set_io_voltage(uint16_t requested_io_voltage_mV, |
| 222 | uint16_t *set_io_voltage_mV, |
| 223 | enum voltage_coerce_mode coerce_mode) |
| 224 | { |
| 225 | int i = 0; |
| 226 | uint8_t selected_voltage_100mV = 0; |
| 227 | uint8_t requested_io_voltage_100mV = 0; |
| 228 | |
| 229 | if (device_pid == USB8451) { |
| 230 | io_voltage_in_mV = 3300; |
| 231 | msg_pwarn("USB-8451 does not support the changing of the SPI IO voltage\n"); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | // limit the IO voltage to 3.3V |
| 236 | if (requested_io_voltage_mV > 3300) { |
| 237 | msg_pinfo("USB-8452 maximum IO voltage is 3.3V\n"); |
| 238 | return -1; |
| 239 | } |
| 240 | requested_io_voltage_100mV = (requested_io_voltage_mV / 100.0f); |
| 241 | |
| 242 | // usb8452_io_voltages_in_100mV contains the supported voltage levels in increasing order |
| 243 | for (i = (ARRAY_SIZE(usb8452_io_voltages_in_100mV) - 1); i > 0; --i) { |
| 244 | if (requested_io_voltage_100mV >= usb8452_io_voltages_in_100mV[i]) |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | if (coerce_mode == USE_LOWER) { |
| 249 | if (requested_io_voltage_100mV < usb8452_io_voltages_in_100mV[0]) { |
| 250 | msg_perr("Unable to set the USB-8452 IO voltage below %.1fV " |
| 251 | "(the minimum supported IO voltage is %.1fV)\n", |
| 252 | (float)requested_io_voltage_100mV / 10.0f, |
| 253 | (float)usb8452_io_voltages_in_100mV[0] / 10.0f); |
| 254 | return -1; |
| 255 | } |
| 256 | selected_voltage_100mV = usb8452_io_voltages_in_100mV[i]; |
| 257 | } else { |
| 258 | if (i == ARRAY_SIZE(usb8452_io_voltages_in_100mV) - 1) |
| 259 | selected_voltage_100mV = usb8452_io_voltages_in_100mV[i]; |
| 260 | else |
| 261 | selected_voltage_100mV = usb8452_io_voltages_in_100mV[i + 1]; |
| 262 | } |
| 263 | |
| 264 | if (requested_io_voltage_100mV < usb8452_io_voltages_in_100mV[0]) { |
| 265 | /* unsupported / would have to round up */ |
| 266 | msg_pwarn("The USB-8452 does not support the %.1fV IO voltage\n", |
| 267 | requested_io_voltage_mV / 1000.0f); |
| 268 | selected_voltage_100mV = kNi845x12Volts; |
| 269 | msg_pwarn("The output voltage is set to 1.2V (this is the lowest voltage)\n"); |
| 270 | msg_pwarn("Supported IO voltages:\n"); |
| 271 | for (i = 0; i < ARRAY_SIZE(usb8452_io_voltages_in_100mV); i++) { |
| 272 | msg_pwarn("%.1fV", (float)usb8452_io_voltages_in_100mV[i] / 10.0f); |
| 273 | if (i != ARRAY_SIZE(usb8452_io_voltages_in_100mV) - 1) |
| 274 | msg_pwarn(", "); |
| 275 | } |
| 276 | msg_pwarn("\n"); |
| 277 | } else if (selected_voltage_100mV != requested_io_voltage_100mV) { |
| 278 | /* we rounded down/up */ |
| 279 | msg_pwarn("USB-8452 IO voltage forced to: %.1f V\n", |
| 280 | (float)selected_voltage_100mV / 10.0f); |
| 281 | } else { |
| 282 | /* exact match */ |
| 283 | msg_pinfo("USB-8452 IO voltage set to: %.1f V\n", |
| 284 | (float)selected_voltage_100mV / 10.0f); |
| 285 | } |
| 286 | |
| 287 | if (set_io_voltage_mV) |
| 288 | *set_io_voltage_mV = (selected_voltage_100mV * 100); |
| 289 | |
| 290 | i = ni845xSetIoVoltageLevel(device_handle, selected_voltage_100mV); |
| 291 | if (i != 0) { |
| 292 | ni845x_report_error("ni845xSetIoVoltageLevel", i); |
| 293 | return -1; |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * @brief ni845x_spi_set_speed sets the SPI SCK speed |
| 300 | * @param SCK_freq_in_KHz SCK speed in KHz |
| 301 | * @return |
| 302 | */ |
| 303 | static int ni845x_spi_set_speed(uint16_t SCK_freq_in_KHz) |
| 304 | { |
| 305 | int32 i = ni845xSpiConfigurationSetClockRate(configuration_handle, SCK_freq_in_KHz); |
| 306 | uInt16 clock_freq_read_KHz; |
| 307 | |
| 308 | if (i != 0) { |
| 309 | ni845x_report_error("ni845xSpiConfigurationSetClockRate", i); |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | // read back the clock frequency and notify the user if it is not the same as it was requested |
| 314 | i = ni845xSpiConfigurationGetClockRate(configuration_handle, &clock_freq_read_KHz); |
| 315 | if (i != 0) { |
| 316 | ni845x_report_error("ni845xSpiConfigurationGetClockRate", i); |
| 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | if (clock_freq_read_KHz != SCK_freq_in_KHz) { |
| 321 | msg_pinfo("SPI clock frequency forced to: %d KHz (requested: %d KHz)\n", |
| 322 | (int)clock_freq_read_KHz, (int)SCK_freq_in_KHz); |
| 323 | } else { |
| 324 | msg_pinfo("SPI clock frequency set to: %d KHz\n", (int)SCK_freq_in_KHz); |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * @brief ni845x_spi_print_available_devices prints a list of the available devices |
| 331 | */ |
| 332 | static void ni845x_spi_print_available_devices(void) |
| 333 | { |
| 334 | char resource_handle[256], device_type_string[16]; |
| 335 | NiHandle device_find_handle; |
| 336 | uInt32 found_devices_count = 0; |
| 337 | int32 tmp = 0; |
| 338 | unsigned int pid, vid, usb_bus; |
| 339 | unsigned long int serial_as_number; |
| 340 | |
| 341 | tmp = ni845xFindDevice(resource_handle, &device_find_handle, &found_devices_count); |
| 342 | if (tmp != 0) { |
| 343 | // supress warning if no device found |
| 344 | if (tmp != NI845x_FIND_DEVICE_NO_DEVICE_FOUND) |
| 345 | ni845x_report_error("ni845xFindDevice", tmp); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | if (found_devices_count) { |
| 350 | msg_pinfo("Available devices:\n"); |
| 351 | do { |
| 352 | tmp = sscanf(resource_handle, "USB%d::0x%04X::0x%04X::%08lX::RAW", |
| 353 | &usb_bus, &vid, &pid, &serial_as_number); |
| 354 | if (tmp == 4) { |
| 355 | switch (pid) { |
| 356 | case USB8451: |
| 357 | snprintf(device_type_string, |
| 358 | ARRAY_SIZE(device_type_string), "USB-8451"); |
| 359 | break; |
| 360 | case USB8452: |
| 361 | snprintf(device_type_string, |
| 362 | ARRAY_SIZE(device_type_string), "USB-8452"); |
| 363 | break; |
| 364 | default: |
| 365 | snprintf(device_type_string, |
| 366 | ARRAY_SIZE(device_type_string), "Unknown device"); |
| 367 | break; |
| 368 | } |
| 369 | msg_pinfo("- %lX (%s)\n", serial_as_number, device_type_string); |
| 370 | |
| 371 | found_devices_count--; |
| 372 | if (found_devices_count) { |
| 373 | tmp = ni845xFindDeviceNext(device_find_handle, resource_handle); |
| 374 | if (tmp) |
| 375 | ni845x_report_error("ni845xFindDeviceNext", tmp); |
| 376 | } |
| 377 | } |
| 378 | } while (found_devices_count); |
| 379 | } |
| 380 | |
| 381 | tmp = ni845xCloseFindDeviceHandle(device_find_handle); |
| 382 | if (tmp) |
| 383 | ni845x_report_error("ni845xCloseFindDeviceHandle", tmp); |
| 384 | } |
| 385 | |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 386 | static int ni845x_spi_shutdown(void *data) |
| 387 | { |
| 388 | int32 ret = 0; |
| 389 | |
| 390 | if (configuration_handle != 0) { |
| 391 | ret = ni845xSpiConfigurationClose(configuration_handle); |
| 392 | if (ret) |
| 393 | ni845x_report_error("ni845xSpiConfigurationClose", ret); |
| 394 | } |
| 395 | |
| 396 | if (device_handle != 0) { |
| 397 | ret = ni845xClose(device_handle); |
| 398 | if (ret) |
| 399 | ni845x_report_error("ni845xClose", ret); |
| 400 | } |
| 401 | return 0; |
| 402 | } |
| 403 | |
Edward O'Callaghan | c9a51f1 | 2020-10-03 00:36:18 +1000 | [diff] [blame] | 404 | static void ni845x_warn_over_max_voltage(const struct flashctx *flash) |
| 405 | { |
| 406 | if (device_pid == USB8451) { |
| 407 | msg_pwarn("The %s chip maximum voltage is %.1fV, while the USB-8451 " |
| 408 | "IO voltage levels are 3.3V.\n" |
| 409 | "Ignoring this because ignore_io_voltage_limits parameter is set.\n", |
| 410 | flash->chip->name, |
| 411 | flash->chip->voltage.max / 1000.0f); |
| 412 | } else if (device_pid == USB8452) { |
| 413 | msg_pwarn("The %s chip maximum voltage is %.1fV, while the USB-8452 " |
| 414 | "IO voltage is set to %.1fV.\n" |
| 415 | "Ignoring this because ignore_io_voltage_limits parameter is set.\n", |
| 416 | flash->chip->name, |
| 417 | flash->chip->voltage.max / 1000.0f, |
| 418 | io_voltage_in_mV / 1000.0f); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | static int ni845x_spi_io_voltage_check(const struct flashctx *flash) |
| 423 | { |
| 424 | static bool first_transmit = true; |
| 425 | |
| 426 | if (first_transmit && flash->chip) { |
| 427 | first_transmit = false; |
| 428 | if (io_voltage_in_mV > flash->chip->voltage.max) { |
| 429 | if (ignore_io_voltage_limits) { |
| 430 | ni845x_warn_over_max_voltage(flash); |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | if (device_pid == USB8451) { |
| 435 | msg_perr("The %s chip maximum voltage is %.1fV, while the USB-8451 " |
| 436 | "IO voltage levels are 3.3V.\nAborting operations\n", |
| 437 | flash->chip->name, |
| 438 | flash->chip->voltage.max / 1000.0f); |
| 439 | return -1; |
| 440 | } else if (device_pid == USB8452) { |
| 441 | msg_perr("Lowering IO voltage because the %s chip maximum voltage is %.1fV, " |
| 442 | "(%.1fV was set)\n", |
| 443 | flash->chip->name, |
| 444 | flash->chip->voltage.max / 1000.0f, |
| 445 | io_voltage_in_mV / 1000.0f); |
| 446 | if (usb8452_spi_set_io_voltage(flash->chip->voltage.max, |
| 447 | &io_voltage_in_mV, |
| 448 | USE_LOWER)) { |
| 449 | msg_perr("Unable to lower the IO voltage below " |
| 450 | "the chip's maximum voltage\n"); |
| 451 | return -1; |
| 452 | } |
| 453 | } |
| 454 | } else if (io_voltage_in_mV < flash->chip->voltage.min) { |
| 455 | if (device_pid == USB8451) { |
| 456 | msg_pwarn("Flash operations might be unreliable, because the %s chip's " |
| 457 | "minimum voltage is %.1fV, while the USB-8451's " |
| 458 | "IO voltage levels are 3.3V.\n", |
| 459 | flash->chip->name, |
| 460 | flash->chip->voltage.min / 1000.0f); |
| 461 | return ignore_io_voltage_limits ? 0 : -1; |
| 462 | } else if (device_pid == USB8452) { |
| 463 | msg_pwarn("Raising the IO voltage because the %s chip's " |
| 464 | "minimum voltage is %.1fV, " |
| 465 | "(%.1fV was set)\n", |
| 466 | flash->chip->name, |
| 467 | flash->chip->voltage.min / 1000.0f, |
| 468 | io_voltage_in_mV / 1000.0f); |
| 469 | if (usb8452_spi_set_io_voltage(flash->chip->voltage.min, |
| 470 | &io_voltage_in_mV, |
| 471 | USE_HIGHER)) { |
| 472 | msg_pwarn("Unable to raise the IO voltage above the chip's " |
| 473 | "minimum voltage\n" |
| 474 | "Flash operations might be unreliable.\n"); |
| 475 | return ignore_io_voltage_limits ? 0 : -1; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | static int ni845x_spi_transmit(struct flashctx *flash, |
| 484 | unsigned int write_cnt, |
| 485 | unsigned int read_cnt, |
| 486 | const unsigned char *write_arr, |
| 487 | unsigned char *read_arr) |
| 488 | { |
| 489 | uInt32 read_size = 0; |
| 490 | uInt8 *transfer_buffer = NULL; |
| 491 | int32 ret = 0; |
| 492 | |
| 493 | if (ni845x_spi_io_voltage_check(flash)) |
| 494 | return -1; |
| 495 | |
| 496 | transfer_buffer = calloc(write_cnt + read_cnt, sizeof(uInt8)); |
| 497 | if (transfer_buffer == NULL) { |
| 498 | msg_gerr("Memory allocation failed!\n"); |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | memcpy(transfer_buffer, write_arr, write_cnt); |
| 503 | |
| 504 | ret = ni845xSpiWriteRead(device_handle, |
| 505 | configuration_handle, |
| 506 | (write_cnt + read_cnt), transfer_buffer, &read_size, transfer_buffer); |
| 507 | if (ret < 0) { |
| 508 | // Negative specifies an error, meaning the function did not perform the expected behavior. |
| 509 | ni845x_report_error("ni845xSpiWriteRead", ret); |
| 510 | free(transfer_buffer); |
| 511 | return -1; |
| 512 | } else if (ret > 0) { |
| 513 | // Positive specifies a warning, meaning the function performed as expected, |
| 514 | // but a condition arose that might require attention. |
| 515 | ni845x_report_warning("ni845xSpiWriteRead", ret); |
| 516 | } |
| 517 | |
| 518 | if (read_cnt != 0 && read_arr != NULL) { |
| 519 | if ((read_cnt + write_cnt) != read_size) { |
| 520 | msg_perr("%s: expected and returned read count mismatch: %u expected, %ld recieved\n", |
| 521 | __func__, read_cnt, read_size); |
| 522 | free(transfer_buffer); |
| 523 | return -1; |
| 524 | } |
| 525 | memcpy(read_arr, &transfer_buffer[write_cnt], read_cnt); |
| 526 | } |
| 527 | free(transfer_buffer); |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static const struct spi_master spi_programmer_ni845x = { |
| 532 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 533 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
| 534 | .command = ni845x_spi_transmit, |
| 535 | .multicommand = default_spi_send_multicommand, |
| 536 | .read = default_spi_read, |
| 537 | .write_256 = default_spi_write_256, |
| 538 | .write_aai = default_spi_write_aai, |
| 539 | }; |
Anastasia Klimchuk | 54cc6b5 | 2021-03-23 16:34:07 +1100 | [diff] [blame^] | 540 | |
| 541 | int ni845x_spi_init(void) |
| 542 | { |
| 543 | char *speed_str = NULL; |
| 544 | char *CS_str = NULL; |
| 545 | char *voltage = NULL; |
| 546 | char *endptr = NULL; |
| 547 | int requested_io_voltage_mV = 1200; // default the IO voltage to 1.2V |
| 548 | int spi_speed_KHz = 1000; // selecting 1 MHz SCK is a good bet |
| 549 | char *serial_number = NULL; // by default open the first connected device |
| 550 | char *ignore_io_voltage_limits_str = NULL; |
| 551 | int32 tmp = 0; |
| 552 | |
| 553 | // read the cs parameter (which Chip select should we use) |
| 554 | CS_str = extract_programmer_param("cs"); |
| 555 | if (CS_str) { |
| 556 | CS_number = CS_str[0] - '0'; |
| 557 | free(CS_str); |
| 558 | if (strlen(CS_str) > 1 || CS_number < 0 || 7 < CS_number) { |
| 559 | msg_perr("Only CS 0-7 supported\n"); |
| 560 | return 1; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | voltage = extract_programmer_param("voltage"); |
| 565 | if (voltage != NULL) { |
| 566 | requested_io_voltage_mV = parse_voltage(voltage); |
| 567 | free(voltage); |
| 568 | if (requested_io_voltage_mV < 0) |
| 569 | return 1; |
| 570 | } |
| 571 | |
| 572 | serial_number = extract_programmer_param("serial"); |
| 573 | |
| 574 | speed_str = extract_programmer_param("spispeed"); |
| 575 | if (speed_str) { |
| 576 | spi_speed_KHz = strtoul(speed_str, &endptr, 0); |
| 577 | if (*endptr) { |
| 578 | msg_perr("The spispeed parameter passed with invalid format: %s\n", |
| 579 | speed_str); |
| 580 | msg_perr("Please pass the parameter with a simple number in kHz\n"); |
| 581 | return 1; |
| 582 | } |
| 583 | free(speed_str); |
| 584 | } |
| 585 | |
| 586 | ignore_io_voltage_limits = false; |
| 587 | ignore_io_voltage_limits_str = extract_programmer_param("ignore_io_voltage_limits"); |
| 588 | if (ignore_io_voltage_limits_str |
| 589 | && strcmp(ignore_io_voltage_limits_str, "yes") == 0) { |
| 590 | ignore_io_voltage_limits = true; |
| 591 | } |
| 592 | |
| 593 | if (ni845x_spi_open(serial_number, &device_handle)) { |
| 594 | if (serial_number) { |
| 595 | msg_pinfo("Could not find any connected NI USB-8451/8452 with serialnumber: %s!\n", |
| 596 | serial_number); |
| 597 | ni845x_spi_print_available_devices(); |
| 598 | msg_pinfo("Check the S/N field on the bottom of the device,\n" |
| 599 | "or use 'lsusb -v -d 3923:7166 | grep Serial' for USB-8451\n" |
| 600 | "or 'lsusb -v -d 3923:7514 | grep Serial' for USB-8452\n"); |
| 601 | free(serial_number); |
| 602 | } else { |
| 603 | msg_pinfo("Could not find any connected NI USB-845x device!\n"); |
| 604 | } |
| 605 | return 1; |
| 606 | } |
| 607 | free(serial_number); |
| 608 | |
| 609 | // open the SPI config handle |
| 610 | tmp = ni845xSpiConfigurationOpen(&configuration_handle); |
| 611 | if (tmp != 0) { |
| 612 | ni845x_report_error("ni845xSpiConfigurationOpen", tmp); |
| 613 | ni845x_spi_shutdown(NULL); |
| 614 | return 1; |
| 615 | } |
| 616 | |
| 617 | if (usb8452_spi_set_io_voltage(requested_io_voltage_mV, &io_voltage_in_mV, USE_LOWER) < 0) { |
| 618 | ni845x_spi_shutdown(NULL); |
| 619 | return 1; // no alert here usb8452_spi_set_io_voltage already printed that |
| 620 | } |
| 621 | |
| 622 | if (ni845x_spi_set_speed(spi_speed_KHz)) { |
| 623 | msg_perr("Unable to set SPI speed\n"); |
| 624 | ni845x_spi_shutdown(NULL); |
| 625 | return 1; |
| 626 | } |
| 627 | |
| 628 | if (register_shutdown(ni845x_spi_shutdown, NULL)) { |
| 629 | ni845x_spi_shutdown(NULL); |
| 630 | return 1; |
| 631 | } |
| 632 | |
| 633 | register_spi_master(&spi_programmer_ni845x); |
| 634 | |
| 635 | return 0; |
| 636 | } |