blob: f65cabda5c82a3e3aa6469269e0eebd214e85558 [file] [log] [blame]
Miklós Márton2d20d6d2018-01-30 20:20:15 +01001/*
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
32enum USB845x_type {
33 USB8451 = 0x7166,
34 USB8452 = 0x7514,
35 Unknown_NI845X_Device
36};
37
38enum voltage_coerce_mode {
39 USE_LOWER,
40 USE_HIGHER
41};
42
43static const struct spi_master spi_programmer_ni845x;
44
Miklós Márton2d20d6d2018-01-30 20:20:15 +010045static enum USB845x_type device_pid = Unknown_NI845X_Device;
46
47static uInt32 device_handle;
48static NiHandle configuration_handle;
49static uint16_t io_voltage_in_mV;
50static bool ignore_io_voltage_limits;
51
52static int ni845x_spi_shutdown(void *data);
53static int32 ni845x_spi_open_resource(char *resource_handle, uInt32 *opened_handle);
54static void ni845x_spi_print_available_devices(void);
55
56// USB-8452 supported voltages, keep this array in ascending order!
57static const uint8_t usb8452_io_voltages_in_100mV[5] = {
58 kNi845x12Volts,
59 kNi845x15Volts,
60 kNi845x18Volts,
61 kNi845x25Volts,
62 kNi845x33Volts
63};
64
65/* Copied from dediprog.c */
66/* Might be useful for other USB devices as well. static for now. */
67static int parse_voltage(char *voltage)
68{
69 char *tmp = NULL;
70 int i;
71 int millivolt = 0, fraction = 0;
72
73 if (!voltage || !strlen(voltage)) {
74 msg_perr("Empty voltage= specified.\n");
75 return -1;
76 }
77 millivolt = (int)strtol(voltage, &tmp, 0);
78 voltage = tmp;
79 /* Handle "," and "." as decimal point. Everything after it is assumed
80 * to be in decimal notation.
81 */
82 if ((*voltage == '.') || (*voltage == ',')) {
83 voltage++;
84 for (i = 0; i < 3; i++) {
85 fraction *= 10;
86 /* Don't advance if the current character is invalid,
87 * but continue multiplying.
88 */
89 if ((*voltage < '0') || (*voltage > '9'))
90 continue;
91 fraction += *voltage - '0';
92 voltage++;
93 }
94 /* Throw away remaining digits. */
95 voltage += strspn(voltage, "0123456789");
96 }
97 /* The remaining string must be empty or "mV" or "V". */
98 tolower_string(voltage);
99
100 /* No unit or "V". */
101 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
102 millivolt *= 1000;
103 millivolt += fraction;
104 } else if (!strncmp(voltage, "mv", 2) || !strncmp(voltage, "millivolt", 9)) {
105 /* No adjustment. fraction is discarded. */
106 } else {
107 /* Garbage at the end of the string. */
108 msg_perr("Garbage voltage= specified.\n");
109 return -1;
110 }
111 return millivolt;
112}
113
114static void ni845x_report_error(const char *const func, const int32 err)
115{
116 static char buf[1024];
117
118 ni845xStatusToString(err, sizeof(buf), buf);
119 msg_perr("%s failed with: %s (%d)\n", func, buf, (int)err);
120}
121
122static void ni845x_report_warning(const char *const func, const int32 err)
123{
124 static char buf[1024];
125
126 ni845xStatusToString(err, sizeof(buf), buf);
127 msg_pwarn("%s failed with: %s (%d)\n", func, buf, (int)err);
128}
129
130/**
131 * @param serial a null terminated string containing the serial number of the specific device or NULL
Martin Rothf6c1cb12022-03-15 10:55:25 -0600132 * @return the 0 on successful completion, negative error code on failure
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100133 */
134static int ni845x_spi_open(const char *serial, uInt32 *return_handle)
135{
136 char resource_name[256];
137 NiHandle device_find_handle;
138 uInt32 found_devices_count = 0;
139 int32 tmp = 0;
140
141 unsigned int vid, pid, usb_bus;
142 unsigned long int serial_as_number;
143 int ret = -1;
144
145 tmp = ni845xFindDevice(resource_name, &device_find_handle, &found_devices_count);
146 if (tmp != 0) {
Martin Rothf6c1cb12022-03-15 10:55:25 -0600147 // suppress warning if no device found
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100148 if (tmp != NI845x_FIND_DEVICE_NO_DEVICE_FOUND)
149 ni845x_report_error("ni845xFindDevice", tmp);
150 return -1;
151 }
152
153 for (; found_devices_count; --found_devices_count) {
154 // Read the serial number and the PID here
155 // VISA resource name format example:
156 // USB0::0x3923::0x7514::DEADBEEF::RAW
157 // where the 0x7514 is the PID
158 // and the DEADBEEF is the serial of the device
159 if (sscanf(resource_name,
160 "USB%u::0x%04X::0x%04X::%08lX::RAW",
161 &usb_bus, &vid, &pid, &serial_as_number) != 4) {
162 // malformed resource string detected
163 msg_pwarn("Warning: Unable to parse the %s NI-845x resource string.\n",
164 resource_name);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200165 msg_pwarn("Please report a bug at flashprog@flashprog.org\n");
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100166 continue;
167 }
168
169 device_pid = pid;
170
Nico Huber70fe55f2026-02-14 21:43:19 +0100171 if (!serial || strtoul(serial, NULL, 16) == serial_as_number)
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100172 break;
173
174 if (found_devices_count > 1) {
175 tmp = ni845xFindDeviceNext(device_find_handle, resource_name);
176 if (tmp) {
177 ni845x_report_error("ni845xFindDeviceNext", tmp);
178 goto _close_ret;
179 }
180 }
181 }
182
183 if (found_devices_count)
184 ret = ni845x_spi_open_resource(resource_name, return_handle);
185
186_close_ret:
187 tmp = ni845xCloseFindDeviceHandle(device_find_handle);
188 if (tmp) {
189 ni845x_report_error("ni845xCloseFindDeviceHandle", tmp);
190 return -1;
191 }
192 return ret;
193}
194
195/**
196 * @brief ni845x_spi_open_resource
197 * @param resource_handle the resource handle returned by the ni845xFindDevice or ni845xFindDeviceNext
198 * @param opened_handle the opened handle from the ni845xOpen
199 * @return the 0 on successful competition, negative error code on failure positive code on warning
200 */
201static int32 ni845x_spi_open_resource(char *resource_handle, uInt32 *opened_handle)
202{
203 // NI-845x driver loads the FPGA bitfile at the first time
204 // which can take couple seconds
205 if (device_pid == USB8452)
206 msg_pwarn("Opening NI-8452, this might take a while for the first time\n");
207
208 int32 tmp = ni845xOpen(resource_handle, opened_handle);
209
210 if (tmp < 0)
211 ni845x_report_error("ni845xOpen", tmp);
212 else if (tmp > 0)
213 ni845x_report_warning("ni845xOpen", tmp);
214 return tmp;
215}
216
217/**
218 * @brief usb8452_spi_set_io_voltage sets the IO voltage for the USB-8452 devices
219 * @param requested_io_voltage_mV the desired IO voltage in mVolts
220 * @param set_io_voltage_mV the IO voltage which was set in mVolts
221 * @param coerce_mode if set to USE_LOWER the closest supported IO voltage which is lower or equal to
Martin Rothf6c1cb12022-03-15 10:55:25 -0600222 * the requested_io_voltage_mV will be selected. Otherwise the next closest supported voltage will be chosen
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100223 * which is higher or equal to the requested_io_voltage_mV.
224 * @return 0 on success, negative on error, positive on warning
225 */
226static int usb8452_spi_set_io_voltage(uint16_t requested_io_voltage_mV,
227 uint16_t *set_io_voltage_mV,
228 enum voltage_coerce_mode coerce_mode)
229{
Nico Huber1e610f22026-02-14 21:56:44 +0100230 int32 ret;
231 unsigned int i;
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100232 uint8_t selected_voltage_100mV = 0;
233 uint8_t requested_io_voltage_100mV = 0;
234
235 if (device_pid == USB8451) {
236 io_voltage_in_mV = 3300;
237 msg_pwarn("USB-8451 does not support the changing of the SPI IO voltage\n");
238 return 0;
239 }
240
241 // limit the IO voltage to 3.3V
242 if (requested_io_voltage_mV > 3300) {
243 msg_pinfo("USB-8452 maximum IO voltage is 3.3V\n");
244 return -1;
245 }
246 requested_io_voltage_100mV = (requested_io_voltage_mV / 100.0f);
247
248 // usb8452_io_voltages_in_100mV contains the supported voltage levels in increasing order
249 for (i = (ARRAY_SIZE(usb8452_io_voltages_in_100mV) - 1); i > 0; --i) {
250 if (requested_io_voltage_100mV >= usb8452_io_voltages_in_100mV[i])
251 break;
252 }
253
254 if (coerce_mode == USE_LOWER) {
255 if (requested_io_voltage_100mV < usb8452_io_voltages_in_100mV[0]) {
256 msg_perr("Unable to set the USB-8452 IO voltage below %.1fV "
257 "(the minimum supported IO voltage is %.1fV)\n",
258 (float)requested_io_voltage_100mV / 10.0f,
259 (float)usb8452_io_voltages_in_100mV[0] / 10.0f);
260 return -1;
261 }
262 selected_voltage_100mV = usb8452_io_voltages_in_100mV[i];
263 } else {
264 if (i == ARRAY_SIZE(usb8452_io_voltages_in_100mV) - 1)
265 selected_voltage_100mV = usb8452_io_voltages_in_100mV[i];
266 else
267 selected_voltage_100mV = usb8452_io_voltages_in_100mV[i + 1];
268 }
269
270 if (requested_io_voltage_100mV < usb8452_io_voltages_in_100mV[0]) {
271 /* unsupported / would have to round up */
272 msg_pwarn("The USB-8452 does not support the %.1fV IO voltage\n",
273 requested_io_voltage_mV / 1000.0f);
274 selected_voltage_100mV = kNi845x12Volts;
275 msg_pwarn("The output voltage is set to 1.2V (this is the lowest voltage)\n");
276 msg_pwarn("Supported IO voltages:\n");
277 for (i = 0; i < ARRAY_SIZE(usb8452_io_voltages_in_100mV); i++) {
278 msg_pwarn("%.1fV", (float)usb8452_io_voltages_in_100mV[i] / 10.0f);
279 if (i != ARRAY_SIZE(usb8452_io_voltages_in_100mV) - 1)
280 msg_pwarn(", ");
281 }
282 msg_pwarn("\n");
283 } else if (selected_voltage_100mV != requested_io_voltage_100mV) {
284 /* we rounded down/up */
285 msg_pwarn("USB-8452 IO voltage forced to: %.1f V\n",
286 (float)selected_voltage_100mV / 10.0f);
287 } else {
288 /* exact match */
289 msg_pinfo("USB-8452 IO voltage set to: %.1f V\n",
290 (float)selected_voltage_100mV / 10.0f);
291 }
292
293 if (set_io_voltage_mV)
294 *set_io_voltage_mV = (selected_voltage_100mV * 100);
295
Nico Huber1e610f22026-02-14 21:56:44 +0100296 ret = ni845xSetIoVoltageLevel(device_handle, selected_voltage_100mV);
297 if (ret != 0) {
298 ni845x_report_error("ni845xSetIoVoltageLevel", ret);
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100299 return -1;
300 }
301 return 0;
302}
303
304/**
305 * @brief ni845x_spi_set_speed sets the SPI SCK speed
306 * @param SCK_freq_in_KHz SCK speed in KHz
307 * @return
308 */
309static int ni845x_spi_set_speed(uint16_t SCK_freq_in_KHz)
310{
311 int32 i = ni845xSpiConfigurationSetClockRate(configuration_handle, SCK_freq_in_KHz);
312 uInt16 clock_freq_read_KHz;
313
314 if (i != 0) {
315 ni845x_report_error("ni845xSpiConfigurationSetClockRate", i);
316 return -1;
317 }
318
319 // read back the clock frequency and notify the user if it is not the same as it was requested
320 i = ni845xSpiConfigurationGetClockRate(configuration_handle, &clock_freq_read_KHz);
321 if (i != 0) {
322 ni845x_report_error("ni845xSpiConfigurationGetClockRate", i);
323 return -1;
324 }
325
326 if (clock_freq_read_KHz != SCK_freq_in_KHz) {
327 msg_pinfo("SPI clock frequency forced to: %d KHz (requested: %d KHz)\n",
328 (int)clock_freq_read_KHz, (int)SCK_freq_in_KHz);
329 } else {
330 msg_pinfo("SPI clock frequency set to: %d KHz\n", (int)SCK_freq_in_KHz);
331 }
332 return 0;
333}
334
335/**
336 * @brief ni845x_spi_print_available_devices prints a list of the available devices
337 */
338static void ni845x_spi_print_available_devices(void)
339{
340 char resource_handle[256], device_type_string[16];
341 NiHandle device_find_handle;
342 uInt32 found_devices_count = 0;
343 int32 tmp = 0;
344 unsigned int pid, vid, usb_bus;
345 unsigned long int serial_as_number;
346
347 tmp = ni845xFindDevice(resource_handle, &device_find_handle, &found_devices_count);
348 if (tmp != 0) {
Martin Rothf6c1cb12022-03-15 10:55:25 -0600349 // suppress warning if no device found
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100350 if (tmp != NI845x_FIND_DEVICE_NO_DEVICE_FOUND)
351 ni845x_report_error("ni845xFindDevice", tmp);
352 return;
353 }
354
355 if (found_devices_count) {
356 msg_pinfo("Available devices:\n");
357 do {
358 tmp = sscanf(resource_handle, "USB%d::0x%04X::0x%04X::%08lX::RAW",
359 &usb_bus, &vid, &pid, &serial_as_number);
360 if (tmp == 4) {
361 switch (pid) {
362 case USB8451:
363 snprintf(device_type_string,
364 ARRAY_SIZE(device_type_string), "USB-8451");
365 break;
366 case USB8452:
367 snprintf(device_type_string,
368 ARRAY_SIZE(device_type_string), "USB-8452");
369 break;
370 default:
371 snprintf(device_type_string,
372 ARRAY_SIZE(device_type_string), "Unknown device");
373 break;
374 }
375 msg_pinfo("- %lX (%s)\n", serial_as_number, device_type_string);
376
377 found_devices_count--;
378 if (found_devices_count) {
379 tmp = ni845xFindDeviceNext(device_find_handle, resource_handle);
380 if (tmp)
381 ni845x_report_error("ni845xFindDeviceNext", tmp);
382 }
383 }
384 } while (found_devices_count);
385 }
386
387 tmp = ni845xCloseFindDeviceHandle(device_find_handle);
388 if (tmp)
389 ni845x_report_error("ni845xCloseFindDeviceHandle", tmp);
390}
391
Nico Huberfbe27662026-02-14 22:00:16 +0100392static int ni845x_spi_init(struct flashprog_programmer *const prog)
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100393{
394 char *speed_str = NULL;
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100395 char *voltage = NULL;
396 char *endptr = NULL;
397 int requested_io_voltage_mV = 1200; // default the IO voltage to 1.2V
398 int spi_speed_KHz = 1000; // selecting 1 MHz SCK is a good bet
399 char *serial_number = NULL; // by default open the first connected device
400 char *ignore_io_voltage_limits_str = NULL;
401 int32 tmp = 0;
402
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100403 voltage = extract_programmer_param("voltage");
404 if (voltage != NULL) {
405 requested_io_voltage_mV = parse_voltage(voltage);
406 free(voltage);
407 if (requested_io_voltage_mV < 0)
408 return 1;
409 }
410
411 serial_number = extract_programmer_param("serial");
412
413 speed_str = extract_programmer_param("spispeed");
414 if (speed_str) {
415 spi_speed_KHz = strtoul(speed_str, &endptr, 0);
416 if (*endptr) {
417 msg_perr("The spispeed parameter passed with invalid format: %s\n",
418 speed_str);
419 msg_perr("Please pass the parameter with a simple number in kHz\n");
420 return 1;
421 }
422 free(speed_str);
423 }
424
425 ignore_io_voltage_limits = false;
426 ignore_io_voltage_limits_str = extract_programmer_param("ignore_io_voltage_limits");
427 if (ignore_io_voltage_limits_str
428 && strcmp(ignore_io_voltage_limits_str, "yes") == 0) {
429 ignore_io_voltage_limits = true;
430 }
431
432 if (ni845x_spi_open(serial_number, &device_handle)) {
433 if (serial_number) {
434 msg_pinfo("Could not find any connected NI USB-8451/8452 with serialnumber: %s!\n",
435 serial_number);
436 ni845x_spi_print_available_devices();
437 msg_pinfo("Check the S/N field on the bottom of the device,\n"
438 "or use 'lsusb -v -d 3923:7166 | grep Serial' for USB-8451\n"
439 "or 'lsusb -v -d 3923:7514 | grep Serial' for USB-8452\n");
440 free(serial_number);
441 } else {
442 msg_pinfo("Could not find any connected NI USB-845x device!\n");
443 }
444 return 1;
445 }
446 free(serial_number);
447
448 // open the SPI config handle
449 tmp = ni845xSpiConfigurationOpen(&configuration_handle);
450 if (tmp != 0) {
451 ni845x_report_error("ni845xSpiConfigurationOpen", tmp);
452 ni845x_spi_shutdown(NULL);
453 return 1;
454 }
455
456 if (usb8452_spi_set_io_voltage(requested_io_voltage_mV, &io_voltage_in_mV, USE_LOWER) < 0) {
457 ni845x_spi_shutdown(NULL);
458 return 1; // no alert here usb8452_spi_set_io_voltage already printed that
459 }
460
461 if (ni845x_spi_set_speed(spi_speed_KHz)) {
462 msg_perr("Unable to set SPI speed\n");
463 ni845x_spi_shutdown(NULL);
464 return 1;
465 }
466
Nico Huber89569d62023-01-12 23:31:40 +0100467 return register_spi_master(&spi_programmer_ni845x, 0, NULL);
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100468}
469
470static int ni845x_spi_shutdown(void *data)
471{
472 int32 ret = 0;
473
474 if (configuration_handle != 0) {
475 ret = ni845xSpiConfigurationClose(configuration_handle);
476 if (ret)
477 ni845x_report_error("ni845xSpiConfigurationClose", ret);
478 }
479
480 if (device_handle != 0) {
481 ret = ni845xClose(device_handle);
482 if (ret)
483 ni845x_report_error("ni845xClose", ret);
484 }
485 return 0;
486}
487
Nico Huberb95fe9b2026-02-12 23:03:45 +0100488static void ni845x_warn_over_max_voltage(const unsigned int max_mv)
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100489{
490 if (device_pid == USB8451) {
Nico Huberb95fe9b2026-02-12 23:03:45 +0100491 msg_pwarn("The chip maximum voltage is %.1fV, while the USB-8451 "
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100492 "IO voltage levels are 3.3V.\n"
493 "Ignoring this because ignore_io_voltage_limits parameter is set.\n",
Nico Huberb95fe9b2026-02-12 23:03:45 +0100494 max_mv / 1000.0f);
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100495 } else if (device_pid == USB8452) {
Nico Huberb95fe9b2026-02-12 23:03:45 +0100496 msg_pwarn("The chip maximum voltage is %.1fV, while the USB-8452 "
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100497 "IO voltage is set to %.1fV.\n"
498 "Ignoring this because ignore_io_voltage_limits parameter is set.\n",
Nico Huberb95fe9b2026-02-12 23:03:45 +0100499 max_mv / 1000.0f,
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100500 io_voltage_in_mV / 1000.0f);
501 }
502}
503
Nico Huberb95fe9b2026-02-12 23:03:45 +0100504static int ni845x_adapt_voltage(const struct master_common *const mst,
505 const unsigned int min_mv, const unsigned int max_mv)
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100506{
Nico Huberb95fe9b2026-02-12 23:03:45 +0100507 if (io_voltage_in_mV > max_mv) {
508 if (ignore_io_voltage_limits) {
509 ni845x_warn_over_max_voltage(max_mv);
510 return 0;
511 }
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100512
Nico Huberb95fe9b2026-02-12 23:03:45 +0100513 if (device_pid == USB8451) {
514 msg_perr("The chip maximum voltage is %.1fV, while the USB-8451 "
515 "IO voltage levels are 3.3V.\nAborting operations\n",
516 max_mv / 1000.0f);
517 return -1;
518 } else if (device_pid == USB8452) {
519 msg_perr("Lowering IO voltage because the chip maximum voltage is %.1fV, "
520 "(%.1fV was set)\n",
521 max_mv / 1000.0f,
522 io_voltage_in_mV / 1000.0f);
523 if (usb8452_spi_set_io_voltage(max_mv,
524 &io_voltage_in_mV,
525 USE_LOWER)) {
526 msg_perr("Unable to lower the IO voltage below "
527 "the chip's maximum voltage\n");
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100528 return -1;
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100529 }
Nico Huberb95fe9b2026-02-12 23:03:45 +0100530 }
531 } else if (io_voltage_in_mV < min_mv) {
532 if (device_pid == USB8451) {
533 msg_pwarn("Flash operations might be unreliable, because the chip's "
534 "minimum voltage is %.1fV, while the USB-8451's "
535 "IO voltage levels are 3.3V.\n",
536 min_mv / 1000.0f);
537 return ignore_io_voltage_limits ? 0 : -1;
538 } else if (device_pid == USB8452) {
539 msg_pwarn("Raising the IO voltage because the chip's "
540 "minimum voltage is %.1fV, "
541 "(%.1fV was set)\n",
542 min_mv / 1000.0f,
543 io_voltage_in_mV / 1000.0f);
544 if (usb8452_spi_set_io_voltage(min_mv,
545 &io_voltage_in_mV,
546 USE_HIGHER)) {
547 msg_pwarn("Unable to raise the IO voltage above the chip's "
548 "minimum voltage\n"
549 "Flash operations might be unreliable.\n");
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100550 return ignore_io_voltage_limits ? 0 : -1;
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100551 }
552 }
553 }
Nico Huberb95fe9b2026-02-12 23:03:45 +0100554
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100555 return 0;
556}
557
Nico Huber610c1aa2023-02-15 02:56:05 +0100558static int ni845x_spi_transmit(const struct spi_master *mst,
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100559 unsigned int write_cnt,
560 unsigned int read_cnt,
561 const unsigned char *write_arr,
562 unsigned char *read_arr)
563{
564 uInt32 read_size = 0;
565 uInt8 *transfer_buffer = NULL;
566 int32 ret = 0;
567
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100568 transfer_buffer = calloc(write_cnt + read_cnt, sizeof(uInt8));
569 if (transfer_buffer == NULL) {
570 msg_gerr("Memory allocation failed!\n");
571 return -1;
572 }
573
574 memcpy(transfer_buffer, write_arr, write_cnt);
575
576 ret = ni845xSpiWriteRead(device_handle,
577 configuration_handle,
578 (write_cnt + read_cnt), transfer_buffer, &read_size, transfer_buffer);
579 if (ret < 0) {
580 // Negative specifies an error, meaning the function did not perform the expected behavior.
581 ni845x_report_error("ni845xSpiWriteRead", ret);
582 free(transfer_buffer);
583 return -1;
584 } else if (ret > 0) {
585 // Positive specifies a warning, meaning the function performed as expected,
586 // but a condition arose that might require attention.
587 ni845x_report_warning("ni845xSpiWriteRead", ret);
588 }
589
590 if (read_cnt != 0 && read_arr != NULL) {
591 if ((read_cnt + write_cnt) != read_size) {
Nico Hubercb35a8d2026-02-14 22:17:55 +0100592 msg_perr("%s: expected and returned read count mismatch: %u expected, %lu received\n",
593 __func__, read_cnt, (unsigned long)read_size);
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100594 free(transfer_buffer);
595 return -1;
596 }
597 memcpy(read_arr, &transfer_buffer[write_cnt], read_cnt);
598 }
599 free(transfer_buffer);
600 return 0;
601}
602
603static const struct spi_master spi_programmer_ni845x = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200604 .max_data_read = MAX_DATA_READ_UNLIMITED,
605 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
606 .command = ni845x_spi_transmit,
607 .multicommand = default_spi_send_multicommand,
608 .read = default_spi_read,
609 .write_256 = default_spi_write_256,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +1000610 .shutdown = ni845x_spi_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +0530611 .probe_opcode = default_spi_probe_opcode,
Nico Huberb95fe9b2026-02-12 23:03:45 +0100612
613 .common.adapt_voltage = ni845x_adapt_voltage,
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100614};
Thomas Heijligencc853d82021-05-04 15:32:17 +0200615
616const struct programmer_entry programmer_ni845x_spi = {
617 .name = "ni845x_spi",
618 .type = OTHER, // choose other because NI-845x uses own USB implementation
619 .devs.note = "National Instruments USB-845x\n",
620 .init = ni845x_spi_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +0200621};