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