asix-sigma.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. /*
  2. * This file is part of the libsigrok project.
  3. *
  4. * Copyright (C) 2010-2012 Håvard Espeland <gus@ping.uio.no>,
  5. * Copyright (C) 2010 Martin Stensgård <mastensg@ping.uio.no>
  6. * Copyright (C) 2010 Carl Henrik Lunde <chlunde@ping.uio.no>
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /*
  22. * ASIX SIGMA/SIGMA2 logic analyzer driver
  23. */
  24. #include <glib.h>
  25. #include <glib/gstdio.h>
  26. #include <ftdi.h>
  27. #include <string.h>
  28. #include "libsigrok.h"
  29. #include "libsigrok-internal.h"
  30. #include "asix-sigma.h"
  31. #define USB_VENDOR 0xa600
  32. #define USB_PRODUCT 0xa000
  33. #define USB_DESCRIPTION "ASIX SIGMA"
  34. #define USB_VENDOR_NAME "ASIX"
  35. #define USB_MODEL_NAME "SIGMA"
  36. #define TRIGGER_TYPE "rf10"
  37. SR_PRIV struct sr_dev_driver asix_sigma_driver_info;
  38. static struct sr_dev_driver *di = &asix_sigma_driver_info;
  39. static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
  40. /*
  41. * The ASIX Sigma supports arbitrary integer frequency divider in
  42. * the 50MHz mode. The divider is in range 1...256 , allowing for
  43. * very precise sampling rate selection. This driver supports only
  44. * a subset of the sampling rates.
  45. */
  46. static const uint64_t samplerates[] = {
  47. SR_KHZ(200), /* div=250 */
  48. SR_KHZ(250), /* div=200 */
  49. SR_KHZ(500), /* div=100 */
  50. SR_MHZ(1), /* div=50 */
  51. SR_MHZ(5), /* div=10 */
  52. SR_MHZ(10), /* div=5 */
  53. SR_MHZ(25), /* div=2 */
  54. SR_MHZ(50), /* div=1 */
  55. SR_MHZ(100), /* Special FW needed */
  56. SR_MHZ(200), /* Special FW needed */
  57. };
  58. /*
  59. * Channel numbers seem to go from 1-16, according to this image:
  60. * http://tools.asix.net/img/sigma_sigmacab_pins_720.jpg
  61. * (the cable has two additional GND pins, and a TI and TO pin)
  62. */
  63. static const char *channel_names[] = {
  64. "1", "2", "3", "4", "5", "6", "7", "8",
  65. "9", "10", "11", "12", "13", "14", "15", "16",
  66. };
  67. static const int32_t hwcaps[] = {
  68. SR_CONF_LOGIC_ANALYZER,
  69. SR_CONF_SAMPLERATE,
  70. SR_CONF_TRIGGER_TYPE,
  71. SR_CONF_CAPTURE_RATIO,
  72. SR_CONF_LIMIT_MSEC,
  73. };
  74. static const char *sigma_firmware_files[] = {
  75. /* 50 MHz, supports 8 bit fractions */
  76. FIRMWARE_DIR "/asix-sigma-50.fw",
  77. /* 100 MHz */
  78. FIRMWARE_DIR "/asix-sigma-100.fw",
  79. /* 200 MHz */
  80. FIRMWARE_DIR "/asix-sigma-200.fw",
  81. /* Synchronous clock from pin */
  82. FIRMWARE_DIR "/asix-sigma-50sync.fw",
  83. /* Frequency counter */
  84. FIRMWARE_DIR "/asix-sigma-phasor.fw",
  85. };
  86. static int sigma_read(void *buf, size_t size, struct dev_context *devc)
  87. {
  88. int ret;
  89. ret = ftdi_read_data(&devc->ftdic, (unsigned char *)buf, size);
  90. if (ret < 0) {
  91. sr_err("ftdi_read_data failed: %s",
  92. ftdi_get_error_string(&devc->ftdic));
  93. }
  94. return ret;
  95. }
  96. static int sigma_write(void *buf, size_t size, struct dev_context *devc)
  97. {
  98. int ret;
  99. ret = ftdi_write_data(&devc->ftdic, (unsigned char *)buf, size);
  100. if (ret < 0) {
  101. sr_err("ftdi_write_data failed: %s",
  102. ftdi_get_error_string(&devc->ftdic));
  103. } else if ((size_t) ret != size) {
  104. sr_err("ftdi_write_data did not complete write.");
  105. }
  106. return ret;
  107. }
  108. static int sigma_write_register(uint8_t reg, uint8_t *data, size_t len,
  109. struct dev_context *devc)
  110. {
  111. size_t i;
  112. uint8_t buf[len + 2];
  113. int idx = 0;
  114. buf[idx++] = REG_ADDR_LOW | (reg & 0xf);
  115. buf[idx++] = REG_ADDR_HIGH | (reg >> 4);
  116. for (i = 0; i < len; ++i) {
  117. buf[idx++] = REG_DATA_LOW | (data[i] & 0xf);
  118. buf[idx++] = REG_DATA_HIGH_WRITE | (data[i] >> 4);
  119. }
  120. return sigma_write(buf, idx, devc);
  121. }
  122. static int sigma_set_register(uint8_t reg, uint8_t value, struct dev_context *devc)
  123. {
  124. return sigma_write_register(reg, &value, 1, devc);
  125. }
  126. static int sigma_read_register(uint8_t reg, uint8_t *data, size_t len,
  127. struct dev_context *devc)
  128. {
  129. uint8_t buf[3];
  130. buf[0] = REG_ADDR_LOW | (reg & 0xf);
  131. buf[1] = REG_ADDR_HIGH | (reg >> 4);
  132. buf[2] = REG_READ_ADDR;
  133. sigma_write(buf, sizeof(buf), devc);
  134. return sigma_read(data, len, devc);
  135. }
  136. static uint8_t sigma_get_register(uint8_t reg, struct dev_context *devc)
  137. {
  138. uint8_t value;
  139. if (1 != sigma_read_register(reg, &value, 1, devc)) {
  140. sr_err("sigma_get_register: 1 byte expected");
  141. return 0;
  142. }
  143. return value;
  144. }
  145. static int sigma_read_pos(uint32_t *stoppos, uint32_t *triggerpos,
  146. struct dev_context *devc)
  147. {
  148. uint8_t buf[] = {
  149. REG_ADDR_LOW | READ_TRIGGER_POS_LOW,
  150. REG_READ_ADDR | NEXT_REG,
  151. REG_READ_ADDR | NEXT_REG,
  152. REG_READ_ADDR | NEXT_REG,
  153. REG_READ_ADDR | NEXT_REG,
  154. REG_READ_ADDR | NEXT_REG,
  155. REG_READ_ADDR | NEXT_REG,
  156. };
  157. uint8_t result[6];
  158. sigma_write(buf, sizeof(buf), devc);
  159. sigma_read(result, sizeof(result), devc);
  160. *triggerpos = result[0] | (result[1] << 8) | (result[2] << 16);
  161. *stoppos = result[3] | (result[4] << 8) | (result[5] << 16);
  162. /* Not really sure why this must be done, but according to spec. */
  163. if ((--*stoppos & 0x1ff) == 0x1ff)
  164. stoppos -= 64;
  165. if ((*--triggerpos & 0x1ff) == 0x1ff)
  166. triggerpos -= 64;
  167. return 1;
  168. }
  169. static int sigma_read_dram(uint16_t startchunk, size_t numchunks,
  170. uint8_t *data, struct dev_context *devc)
  171. {
  172. size_t i;
  173. uint8_t buf[4096];
  174. int idx = 0;
  175. /* Send the startchunk. Index start with 1. */
  176. buf[0] = startchunk >> 8;
  177. buf[1] = startchunk & 0xff;
  178. sigma_write_register(WRITE_MEMROW, buf, 2, devc);
  179. /* Read the DRAM. */
  180. buf[idx++] = REG_DRAM_BLOCK;
  181. buf[idx++] = REG_DRAM_WAIT_ACK;
  182. for (i = 0; i < numchunks; ++i) {
  183. /* Alternate bit to copy from DRAM to cache. */
  184. if (i != (numchunks - 1))
  185. buf[idx++] = REG_DRAM_BLOCK | (((i + 1) % 2) << 4);
  186. buf[idx++] = REG_DRAM_BLOCK_DATA | ((i % 2) << 4);
  187. if (i != (numchunks - 1))
  188. buf[idx++] = REG_DRAM_WAIT_ACK;
  189. }
  190. sigma_write(buf, idx, devc);
  191. return sigma_read(data, numchunks * CHUNK_SIZE, devc);
  192. }
  193. /* Upload trigger look-up tables to Sigma. */
  194. static int sigma_write_trigger_lut(struct triggerlut *lut, struct dev_context *devc)
  195. {
  196. int i;
  197. uint8_t tmp[2];
  198. uint16_t bit;
  199. /* Transpose the table and send to Sigma. */
  200. for (i = 0; i < 16; ++i) {
  201. bit = 1 << i;
  202. tmp[0] = tmp[1] = 0;
  203. if (lut->m2d[0] & bit)
  204. tmp[0] |= 0x01;
  205. if (lut->m2d[1] & bit)
  206. tmp[0] |= 0x02;
  207. if (lut->m2d[2] & bit)
  208. tmp[0] |= 0x04;
  209. if (lut->m2d[3] & bit)
  210. tmp[0] |= 0x08;
  211. if (lut->m3 & bit)
  212. tmp[0] |= 0x10;
  213. if (lut->m3s & bit)
  214. tmp[0] |= 0x20;
  215. if (lut->m4 & bit)
  216. tmp[0] |= 0x40;
  217. if (lut->m0d[0] & bit)
  218. tmp[1] |= 0x01;
  219. if (lut->m0d[1] & bit)
  220. tmp[1] |= 0x02;
  221. if (lut->m0d[2] & bit)
  222. tmp[1] |= 0x04;
  223. if (lut->m0d[3] & bit)
  224. tmp[1] |= 0x08;
  225. if (lut->m1d[0] & bit)
  226. tmp[1] |= 0x10;
  227. if (lut->m1d[1] & bit)
  228. tmp[1] |= 0x20;
  229. if (lut->m1d[2] & bit)
  230. tmp[1] |= 0x40;
  231. if (lut->m1d[3] & bit)
  232. tmp[1] |= 0x80;
  233. sigma_write_register(WRITE_TRIGGER_SELECT0, tmp, sizeof(tmp),
  234. devc);
  235. sigma_set_register(WRITE_TRIGGER_SELECT1, 0x30 | i, devc);
  236. }
  237. /* Send the parameters */
  238. sigma_write_register(WRITE_TRIGGER_SELECT0, (uint8_t *) &lut->params,
  239. sizeof(lut->params), devc);
  240. return SR_OK;
  241. }
  242. static void clear_helper(void *priv)
  243. {
  244. struct dev_context *devc;
  245. devc = priv;
  246. ftdi_deinit(&devc->ftdic);
  247. }
  248. static int dev_clear(void)
  249. {
  250. return std_dev_clear(di, clear_helper);
  251. }
  252. static int init(struct sr_context *sr_ctx)
  253. {
  254. return std_init(sr_ctx, di, LOG_PREFIX);
  255. }
  256. static GSList *scan(GSList *options)
  257. {
  258. struct sr_dev_inst *sdi;
  259. struct sr_channel *ch;
  260. struct drv_context *drvc;
  261. struct dev_context *devc;
  262. GSList *devices;
  263. struct ftdi_device_list *devlist;
  264. char serial_txt[10];
  265. uint32_t serial;
  266. int ret;
  267. unsigned int i;
  268. (void)options;
  269. drvc = di->priv;
  270. devices = NULL;
  271. if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
  272. sr_err("%s: devc malloc failed", __func__);
  273. return NULL;
  274. }
  275. ftdi_init(&devc->ftdic);
  276. /* Look for SIGMAs. */
  277. if ((ret = ftdi_usb_find_all(&devc->ftdic, &devlist,
  278. USB_VENDOR, USB_PRODUCT)) <= 0) {
  279. if (ret < 0)
  280. sr_err("ftdi_usb_find_all(): %d", ret);
  281. goto free;
  282. }
  283. /* Make sure it's a version 1 or 2 SIGMA. */
  284. ftdi_usb_get_strings(&devc->ftdic, devlist->dev, NULL, 0, NULL, 0,
  285. serial_txt, sizeof(serial_txt));
  286. sscanf(serial_txt, "%x", &serial);
  287. if (serial < 0xa6010000 || serial > 0xa602ffff) {
  288. sr_err("Only SIGMA and SIGMA2 are supported "
  289. "in this version of libsigrok.");
  290. goto free;
  291. }
  292. sr_info("Found ASIX SIGMA - Serial: %s", serial_txt);
  293. devc->cur_samplerate = samplerates[0];
  294. devc->period_ps = 0;
  295. devc->limit_msec = 0;
  296. devc->cur_firmware = -1;
  297. devc->num_channels = 0;
  298. devc->samples_per_event = 0;
  299. devc->capture_ratio = 50;
  300. devc->use_triggers = 0;
  301. /* Register SIGMA device. */
  302. if (!(sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING, USB_VENDOR_NAME,
  303. USB_MODEL_NAME, NULL))) {
  304. sr_err("%s: sdi was NULL", __func__);
  305. goto free;
  306. }
  307. sdi->driver = di;
  308. for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
  309. ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
  310. channel_names[i]);
  311. if (!ch)
  312. return NULL;
  313. sdi->channels = g_slist_append(sdi->channels, ch);
  314. }
  315. devices = g_slist_append(devices, sdi);
  316. drvc->instances = g_slist_append(drvc->instances, sdi);
  317. sdi->priv = devc;
  318. /* We will open the device again when we need it. */
  319. ftdi_list_free(&devlist);
  320. return devices;
  321. free:
  322. ftdi_deinit(&devc->ftdic);
  323. g_free(devc);
  324. return NULL;
  325. }
  326. static GSList *dev_list(void)
  327. {
  328. return ((struct drv_context *)(di->priv))->instances;
  329. }
  330. /*
  331. * Configure the FPGA for bitbang mode.
  332. * This sequence is documented in section 2. of the ASIX Sigma programming
  333. * manual. This sequence is necessary to configure the FPGA in the Sigma
  334. * into Bitbang mode, in which it can be programmed with the firmware.
  335. */
  336. static int sigma_fpga_init_bitbang(struct dev_context *devc)
  337. {
  338. uint8_t suicide[] = {
  339. 0x84, 0x84, 0x88, 0x84, 0x88, 0x84, 0x88, 0x84,
  340. };
  341. uint8_t init_array[] = {
  342. 0x01, 0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01,
  343. 0x01, 0x01,
  344. };
  345. int i, ret, timeout = 10000;
  346. uint8_t data;
  347. /* Section 2. part 1), do the FPGA suicide. */
  348. sigma_write(suicide, sizeof(suicide), devc);
  349. sigma_write(suicide, sizeof(suicide), devc);
  350. sigma_write(suicide, sizeof(suicide), devc);
  351. sigma_write(suicide, sizeof(suicide), devc);
  352. /* Section 2. part 2), do pulse on D1. */
  353. sigma_write(init_array, sizeof(init_array), devc);
  354. ftdi_usb_purge_buffers(&devc->ftdic);
  355. /* Wait until the FPGA asserts D6/INIT_B. */
  356. for (i = 0; i < timeout; i++) {
  357. ret = sigma_read(&data, 1, devc);
  358. if (ret < 0)
  359. return ret;
  360. /* Test if pin D6 got asserted. */
  361. if (data & (1 << 5))
  362. return 0;
  363. /* The D6 was not asserted yet, wait a bit. */
  364. usleep(10000);
  365. }
  366. return SR_ERR_TIMEOUT;
  367. }
  368. /*
  369. * Configure the FPGA for logic-analyzer mode.
  370. */
  371. static int sigma_fpga_init_la(struct dev_context *devc)
  372. {
  373. /* Initialize the logic analyzer mode. */
  374. uint8_t logic_mode_start[] = {
  375. REG_ADDR_LOW | (READ_ID & 0xf),
  376. REG_ADDR_HIGH | (READ_ID >> 8),
  377. REG_READ_ADDR, /* Read ID register. */
  378. REG_ADDR_LOW | (WRITE_TEST & 0xf),
  379. REG_DATA_LOW | 0x5,
  380. REG_DATA_HIGH_WRITE | 0x5,
  381. REG_READ_ADDR, /* Read scratch register. */
  382. REG_DATA_LOW | 0xa,
  383. REG_DATA_HIGH_WRITE | 0xa,
  384. REG_READ_ADDR, /* Read scratch register. */
  385. REG_ADDR_LOW | (WRITE_MODE & 0xf),
  386. REG_DATA_LOW | 0x0,
  387. REG_DATA_HIGH_WRITE | 0x8,
  388. };
  389. uint8_t result[3];
  390. int ret;
  391. /* Initialize the logic analyzer mode. */
  392. sigma_write(logic_mode_start, sizeof(logic_mode_start), devc);
  393. /* Expect a 3 byte reply since we issued three READ requests. */
  394. ret = sigma_read(result, 3, devc);
  395. if (ret != 3)
  396. goto err;
  397. if (result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa)
  398. goto err;
  399. return SR_OK;
  400. err:
  401. sr_err("Configuration failed. Invalid reply received.");
  402. return SR_ERR;
  403. }
  404. /*
  405. * Read the firmware from a file and transform it into a series of bitbang
  406. * pulses used to program the FPGA. Note that the *bb_cmd must be free()'d
  407. * by the caller of this function.
  408. */
  409. static int sigma_fw_2_bitbang(const char *filename,
  410. uint8_t **bb_cmd, gsize *bb_cmd_size)
  411. {
  412. GMappedFile *file;
  413. GError *error;
  414. gsize i, file_size, bb_size;
  415. gchar *firmware;
  416. uint8_t *bb_stream, *bbs;
  417. uint32_t imm;
  418. int bit, v;
  419. int ret = SR_OK;
  420. /*
  421. * Map the file and make the mapped buffer writable.
  422. * NOTE: Using writable=TRUE does _NOT_ mean that file that is mapped
  423. * will be modified. It will not be modified until someone uses
  424. * g_file_set_contents() on it.
  425. */
  426. error = NULL;
  427. file = g_mapped_file_new(filename, TRUE, &error);
  428. g_assert_no_error(error);
  429. file_size = g_mapped_file_get_length(file);
  430. firmware = g_mapped_file_get_contents(file);
  431. g_assert(firmware);
  432. /* Weird magic transformation below, I have no idea what it does. */
  433. imm = 0x3f6df2ab;
  434. for (i = 0; i < file_size; i++) {
  435. imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
  436. firmware[i] ^= imm & 0xff;
  437. }
  438. /*
  439. * Now that the firmware is "transformed", we will transcribe the
  440. * firmware blob into a sequence of toggles of the Dx wires. This
  441. * sequence will be fed directly into the Sigma, which must be in
  442. * the FPGA bitbang programming mode.
  443. */
  444. /* Each bit of firmware is transcribed as two toggles of Dx wires. */
  445. bb_size = file_size * 8 * 2;
  446. bb_stream = (uint8_t *)g_try_malloc(bb_size);
  447. if (!bb_stream) {
  448. sr_err("%s: Failed to allocate bitbang stream", __func__);
  449. ret = SR_ERR_MALLOC;
  450. goto exit;
  451. }
  452. bbs = bb_stream;
  453. for (i = 0; i < file_size; i++) {
  454. for (bit = 7; bit >= 0; bit--) {
  455. v = (firmware[i] & (1 << bit)) ? 0x40 : 0x00;
  456. *bbs++ = v | 0x01;
  457. *bbs++ = v;
  458. }
  459. }
  460. /* The transformation completed successfully, return the result. */
  461. *bb_cmd = bb_stream;
  462. *bb_cmd_size = bb_size;
  463. exit:
  464. g_mapped_file_unref(file);
  465. return ret;
  466. }
  467. static int upload_firmware(int firmware_idx, struct dev_context *devc)
  468. {
  469. int ret;
  470. unsigned char *buf;
  471. unsigned char pins;
  472. size_t buf_size;
  473. const char *firmware = sigma_firmware_files[firmware_idx];
  474. struct ftdi_context *ftdic = &devc->ftdic;
  475. /* Make sure it's an ASIX SIGMA. */
  476. ret = ftdi_usb_open_desc(ftdic, USB_VENDOR, USB_PRODUCT,
  477. USB_DESCRIPTION, NULL);
  478. if (ret < 0) {
  479. sr_err("ftdi_usb_open failed: %s",
  480. ftdi_get_error_string(ftdic));
  481. return 0;
  482. }
  483. ret = ftdi_set_bitmode(ftdic, 0xdf, BITMODE_BITBANG);
  484. if (ret < 0) {
  485. sr_err("ftdi_set_bitmode failed: %s",
  486. ftdi_get_error_string(ftdic));
  487. return 0;
  488. }
  489. /* Four times the speed of sigmalogan - Works well. */
  490. ret = ftdi_set_baudrate(ftdic, 750000);
  491. if (ret < 0) {
  492. sr_err("ftdi_set_baudrate failed: %s",
  493. ftdi_get_error_string(ftdic));
  494. return 0;
  495. }
  496. /* Initialize the FPGA for firmware upload. */
  497. ret = sigma_fpga_init_bitbang(devc);
  498. if (ret)
  499. return ret;
  500. /* Prepare firmware. */
  501. ret = sigma_fw_2_bitbang(firmware, &buf, &buf_size);
  502. if (ret != SR_OK) {
  503. sr_err("An error occured while reading the firmware: %s",
  504. firmware);
  505. return ret;
  506. }
  507. /* Upload firmare. */
  508. sr_info("Uploading firmware file '%s'.", firmware);
  509. sigma_write(buf, buf_size, devc);
  510. g_free(buf);
  511. ret = ftdi_set_bitmode(ftdic, 0x00, BITMODE_RESET);
  512. if (ret < 0) {
  513. sr_err("ftdi_set_bitmode failed: %s",
  514. ftdi_get_error_string(ftdic));
  515. return SR_ERR;
  516. }
  517. ftdi_usb_purge_buffers(ftdic);
  518. /* Discard garbage. */
  519. while (sigma_read(&pins, 1, devc) == 1)
  520. ;
  521. /* Initialize the FPGA for logic-analyzer mode. */
  522. ret = sigma_fpga_init_la(devc);
  523. if (ret != SR_OK)
  524. return ret;
  525. devc->cur_firmware = firmware_idx;
  526. sr_info("Firmware uploaded.");
  527. return SR_OK;
  528. }
  529. static int dev_open(struct sr_dev_inst *sdi)
  530. {
  531. struct dev_context *devc;
  532. int ret;
  533. devc = sdi->priv;
  534. /* Make sure it's an ASIX SIGMA. */
  535. if ((ret = ftdi_usb_open_desc(&devc->ftdic,
  536. USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
  537. sr_err("ftdi_usb_open failed: %s",
  538. ftdi_get_error_string(&devc->ftdic));
  539. return 0;
  540. }
  541. sdi->status = SR_ST_ACTIVE;
  542. return SR_OK;
  543. }
  544. static int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
  545. {
  546. struct dev_context *devc;
  547. unsigned int i;
  548. int ret;
  549. devc = sdi->priv;
  550. ret = SR_OK;
  551. for (i = 0; i < ARRAY_SIZE(samplerates); i++) {
  552. if (samplerates[i] == samplerate)
  553. break;
  554. }
  555. if (samplerates[i] == 0)
  556. return SR_ERR_SAMPLERATE;
  557. if (samplerate <= SR_MHZ(50)) {
  558. ret = upload_firmware(0, devc);
  559. devc->num_channels = 16;
  560. } else if (samplerate == SR_MHZ(100)) {
  561. ret = upload_firmware(1, devc);
  562. devc->num_channels = 8;
  563. } else if (samplerate == SR_MHZ(200)) {
  564. ret = upload_firmware(2, devc);
  565. devc->num_channels = 4;
  566. }
  567. if (ret == SR_OK) {
  568. devc->cur_samplerate = samplerate;
  569. devc->period_ps = 1000000000000ULL / samplerate;
  570. devc->samples_per_event = 16 / devc->num_channels;
  571. devc->state.state = SIGMA_IDLE;
  572. }
  573. return ret;
  574. }
  575. /*
  576. * In 100 and 200 MHz mode, only a single pin rising/falling can be
  577. * set as trigger. In other modes, two rising/falling triggers can be set,
  578. * in addition to value/mask trigger for any number of channels.
  579. *
  580. * The Sigma supports complex triggers using boolean expressions, but this
  581. * has not been implemented yet.
  582. */
  583. static int configure_channels(const struct sr_dev_inst *sdi)
  584. {
  585. struct dev_context *devc = sdi->priv;
  586. const struct sr_channel *ch;
  587. const GSList *l;
  588. int trigger_set = 0;
  589. int channelbit;
  590. memset(&devc->trigger, 0, sizeof(struct sigma_trigger));
  591. for (l = sdi->channels; l; l = l->next) {
  592. ch = (struct sr_channel *)l->data;
  593. channelbit = 1 << (ch->index);
  594. if (!ch->enabled || !ch->trigger)
  595. continue;
  596. if (devc->cur_samplerate >= SR_MHZ(100)) {
  597. /* Fast trigger support. */
  598. if (trigger_set) {
  599. sr_err("Only a single pin trigger in 100 and "
  600. "200MHz mode is supported.");
  601. return SR_ERR;
  602. }
  603. if (ch->trigger[0] == 'f')
  604. devc->trigger.fallingmask |= channelbit;
  605. else if (ch->trigger[0] == 'r')
  606. devc->trigger.risingmask |= channelbit;
  607. else {
  608. sr_err("Only rising/falling trigger in 100 "
  609. "and 200MHz mode is supported.");
  610. return SR_ERR;
  611. }
  612. ++trigger_set;
  613. } else {
  614. /* Simple trigger support (event). */
  615. if (ch->trigger[0] == '1') {
  616. devc->trigger.simplevalue |= channelbit;
  617. devc->trigger.simplemask |= channelbit;
  618. }
  619. else if (ch->trigger[0] == '0') {
  620. devc->trigger.simplevalue &= ~channelbit;
  621. devc->trigger.simplemask |= channelbit;
  622. }
  623. else if (ch->trigger[0] == 'f') {
  624. devc->trigger.fallingmask |= channelbit;
  625. ++trigger_set;
  626. }
  627. else if (ch->trigger[0] == 'r') {
  628. devc->trigger.risingmask |= channelbit;
  629. ++trigger_set;
  630. }
  631. /*
  632. * Actually, Sigma supports 2 rising/falling triggers,
  633. * but they are ORed and the current trigger syntax
  634. * does not permit ORed triggers.
  635. */
  636. if (trigger_set > 1) {
  637. sr_err("Only 1 rising/falling trigger "
  638. "is supported.");
  639. return SR_ERR;
  640. }
  641. }
  642. if (trigger_set)
  643. devc->use_triggers = 1;
  644. }
  645. return SR_OK;
  646. }
  647. static int dev_close(struct sr_dev_inst *sdi)
  648. {
  649. struct dev_context *devc;
  650. devc = sdi->priv;
  651. /* TODO */
  652. if (sdi->status == SR_ST_ACTIVE)
  653. ftdi_usb_close(&devc->ftdic);
  654. sdi->status = SR_ST_INACTIVE;
  655. return SR_OK;
  656. }
  657. static int cleanup(void)
  658. {
  659. return dev_clear();
  660. }
  661. static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
  662. const struct sr_channel_group *cg)
  663. {
  664. struct dev_context *devc;
  665. (void)cg;
  666. if (!sdi)
  667. return SR_ERR;
  668. devc = sdi->priv;
  669. switch (id) {
  670. case SR_CONF_SAMPLERATE:
  671. *data = g_variant_new_uint64(devc->cur_samplerate);
  672. break;
  673. case SR_CONF_LIMIT_MSEC:
  674. *data = g_variant_new_uint64(devc->limit_msec);
  675. break;
  676. case SR_CONF_CAPTURE_RATIO:
  677. *data = g_variant_new_uint64(devc->capture_ratio);
  678. break;
  679. default:
  680. return SR_ERR_NA;
  681. }
  682. return SR_OK;
  683. }
  684. static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
  685. const struct sr_channel_group *cg)
  686. {
  687. struct dev_context *devc;
  688. uint64_t tmp;
  689. int ret;
  690. (void)cg;
  691. if (sdi->status != SR_ST_ACTIVE)
  692. return SR_ERR_DEV_CLOSED;
  693. devc = sdi->priv;
  694. ret = SR_OK;
  695. switch (id) {
  696. case SR_CONF_SAMPLERATE:
  697. ret = set_samplerate(sdi, g_variant_get_uint64(data));
  698. break;
  699. case SR_CONF_LIMIT_MSEC:
  700. tmp = g_variant_get_uint64(data);
  701. if (tmp > 0)
  702. devc->limit_msec = g_variant_get_uint64(data);
  703. else
  704. ret = SR_ERR;
  705. break;
  706. case SR_CONF_LIMIT_SAMPLES:
  707. tmp = g_variant_get_uint64(data);
  708. devc->limit_msec = tmp * 1000 / devc->cur_samplerate;
  709. break;
  710. case SR_CONF_CAPTURE_RATIO:
  711. tmp = g_variant_get_uint64(data);
  712. if (tmp <= 100)
  713. devc->capture_ratio = tmp;
  714. else
  715. ret = SR_ERR;
  716. break;
  717. default:
  718. ret = SR_ERR_NA;
  719. }
  720. return ret;
  721. }
  722. static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
  723. const struct sr_channel_group *cg)
  724. {
  725. GVariant *gvar;
  726. GVariantBuilder gvb;
  727. (void)sdi;
  728. (void)cg;
  729. switch (key) {
  730. case SR_CONF_DEVICE_OPTIONS:
  731. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  732. hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
  733. break;
  734. case SR_CONF_SAMPLERATE:
  735. g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
  736. gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
  737. ARRAY_SIZE(samplerates), sizeof(uint64_t));
  738. g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
  739. *data = g_variant_builder_end(&gvb);
  740. break;
  741. case SR_CONF_TRIGGER_TYPE:
  742. *data = g_variant_new_string(TRIGGER_TYPE);
  743. break;
  744. default:
  745. return SR_ERR_NA;
  746. }
  747. return SR_OK;
  748. }
  749. /* Software trigger to determine exact trigger position. */
  750. static int get_trigger_offset(uint8_t *samples, uint16_t last_sample,
  751. struct sigma_trigger *t)
  752. {
  753. int i;
  754. uint16_t sample = 0;
  755. for (i = 0; i < 8; ++i) {
  756. if (i > 0)
  757. last_sample = sample;
  758. sample = samples[2 * i] | (samples[2 * i + 1] << 8);
  759. /* Simple triggers. */
  760. if ((sample & t->simplemask) != t->simplevalue)
  761. continue;
  762. /* Rising edge. */
  763. if (((last_sample & t->risingmask) != 0) ||
  764. ((sample & t->risingmask) != t->risingmask))
  765. continue;
  766. /* Falling edge. */
  767. if ((last_sample & t->fallingmask) != t->fallingmask ||
  768. (sample & t->fallingmask) != 0)
  769. continue;
  770. break;
  771. }
  772. /* If we did not match, return original trigger pos. */
  773. return i & 0x7;
  774. }
  775. /*
  776. * Return the timestamp of "DRAM cluster".
  777. */
  778. static uint16_t sigma_dram_cluster_ts(struct sigma_dram_cluster *cluster)
  779. {
  780. return (cluster->timestamp_hi << 8) | cluster->timestamp_lo;
  781. }
  782. static void sigma_decode_dram_cluster(struct sigma_dram_cluster *dram_cluster,
  783. unsigned int events_in_cluster,
  784. unsigned int triggered,
  785. struct sr_dev_inst *sdi)
  786. {
  787. struct dev_context *devc = sdi->priv;
  788. struct sigma_state *ss = &devc->state;
  789. struct sr_datafeed_packet packet;
  790. struct sr_datafeed_logic logic;
  791. uint16_t tsdiff, ts;
  792. uint8_t samples[2048];
  793. unsigned int i;
  794. ts = sigma_dram_cluster_ts(dram_cluster);
  795. tsdiff = ts - ss->lastts;
  796. ss->lastts = ts;
  797. packet.type = SR_DF_LOGIC;
  798. packet.payload = &logic;
  799. logic.unitsize = 2;
  800. logic.data = samples;
  801. /*
  802. * First of all, send Sigrok a copy of the last sample from
  803. * previous cluster as many times as needed to make up for
  804. * the differential characteristics of data we get from the
  805. * Sigma. Sigrok needs one sample of data per period.
  806. *
  807. * One DRAM cluster contains a timestamp and seven samples,
  808. * the units of timestamp are "devc->period_ps" , the first
  809. * sample in the cluster happens at the time of the timestamp
  810. * and the remaining samples happen at timestamp +1...+6 .
  811. */
  812. for (ts = 0; ts < tsdiff - (EVENTS_PER_CLUSTER - 1); ts++) {
  813. i = ts % 1024;
  814. samples[2 * i + 0] = ss->lastsample & 0xff;
  815. samples[2 * i + 1] = ss->lastsample >> 8;
  816. /*
  817. * If we have 1024 samples ready or we're at the
  818. * end of submitting the padding samples, submit
  819. * the packet to Sigrok.
  820. */
  821. if ((i == 1023) || (ts == (tsdiff - EVENTS_PER_CLUSTER))) {
  822. logic.length = (i + 1) * logic.unitsize;
  823. sr_session_send(devc->cb_data, &packet);
  824. }
  825. }
  826. /*
  827. * Parse the samples in current cluster and prepare them
  828. * to be submitted to Sigrok.
  829. */
  830. for (i = 0; i < events_in_cluster; i++) {
  831. samples[2 * i + 1] = dram_cluster->samples[i].sample_lo;
  832. samples[2 * i + 0] = dram_cluster->samples[i].sample_hi;
  833. }
  834. /* Send data up to trigger point (if triggered). */
  835. int trigger_offset = 0;
  836. if (triggered) {
  837. /*
  838. * Trigger is not always accurate to sample because of
  839. * pipeline delay. However, it always triggers before
  840. * the actual event. We therefore look at the next
  841. * samples to pinpoint the exact position of the trigger.
  842. */
  843. trigger_offset = get_trigger_offset(samples,
  844. ss->lastsample, &devc->trigger);
  845. if (trigger_offset > 0) {
  846. packet.type = SR_DF_LOGIC;
  847. logic.length = trigger_offset * logic.unitsize;
  848. sr_session_send(devc->cb_data, &packet);
  849. events_in_cluster -= trigger_offset;
  850. }
  851. /* Only send trigger if explicitly enabled. */
  852. if (devc->use_triggers) {
  853. packet.type = SR_DF_TRIGGER;
  854. sr_session_send(devc->cb_data, &packet);
  855. }
  856. }
  857. if (events_in_cluster > 0) {
  858. packet.type = SR_DF_LOGIC;
  859. logic.length = events_in_cluster * logic.unitsize;
  860. logic.data = samples + (trigger_offset * logic.unitsize);
  861. sr_session_send(devc->cb_data, &packet);
  862. }
  863. ss->lastsample =
  864. samples[2 * (events_in_cluster - 1) + 0] |
  865. (samples[2 * (events_in_cluster - 1) + 1] << 8);
  866. }
  867. /*
  868. * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
  869. * Each event is 20ns apart, and can contain multiple samples.
  870. *
  871. * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart.
  872. * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart.
  873. * For 50 MHz and below, events contain one sample for each channel,
  874. * spread 20 ns apart.
  875. */
  876. static int decode_chunk_ts(struct sigma_dram_line *dram_line,
  877. uint16_t events_in_line,
  878. uint32_t trigger_event,
  879. void *cb_data)
  880. {
  881. struct sigma_dram_cluster *dram_cluster;
  882. struct sr_dev_inst *sdi = cb_data;
  883. struct dev_context *devc = sdi->priv;
  884. unsigned int clusters_in_line =
  885. (events_in_line + (EVENTS_PER_CLUSTER - 1)) / EVENTS_PER_CLUSTER;
  886. unsigned int events_in_cluster;
  887. unsigned int i;
  888. uint32_t trigger_cluster = ~0, triggered = 0;
  889. /* Check if trigger is in this chunk. */
  890. if (trigger_event < (64 * 7)) {
  891. if (devc->cur_samplerate <= SR_MHZ(50)) {
  892. trigger_event -= MIN(EVENTS_PER_CLUSTER - 1,
  893. trigger_event);
  894. }
  895. /* Find in which cluster the trigger occured. */
  896. trigger_cluster = trigger_event / EVENTS_PER_CLUSTER;
  897. }
  898. /* For each full DRAM cluster. */
  899. for (i = 0; i < clusters_in_line; i++) {
  900. dram_cluster = &dram_line->cluster[i];
  901. /* The last cluster might not be full. */
  902. if ((i == clusters_in_line - 1) &&
  903. (events_in_line % EVENTS_PER_CLUSTER)) {
  904. events_in_cluster = events_in_line % EVENTS_PER_CLUSTER;
  905. } else {
  906. events_in_cluster = EVENTS_PER_CLUSTER;
  907. }
  908. triggered = (i == trigger_cluster);
  909. sigma_decode_dram_cluster(dram_cluster, events_in_cluster,
  910. triggered, sdi);
  911. }
  912. return SR_OK;
  913. }
  914. static int download_capture(struct sr_dev_inst *sdi)
  915. {
  916. struct dev_context *devc = sdi->priv;
  917. const int chunks_per_read = 32;
  918. struct sigma_dram_line *dram_line;
  919. int bufsz;
  920. uint32_t stoppos, triggerpos;
  921. struct sr_datafeed_packet packet;
  922. uint8_t modestatus;
  923. uint32_t i;
  924. uint32_t dl_lines_total, dl_lines_curr, dl_lines_done;
  925. uint32_t dl_events_in_line = 64 * 7;
  926. uint32_t trg_line = ~0, trg_event = ~0;
  927. dram_line = g_try_malloc0(chunks_per_read * sizeof(*dram_line));
  928. if (!dram_line)
  929. return FALSE;
  930. sr_info("Downloading sample data.");
  931. /* Stop acquisition. */
  932. sigma_set_register(WRITE_MODE, 0x11, devc);
  933. /* Set SDRAM Read Enable. */
  934. sigma_set_register(WRITE_MODE, 0x02, devc);
  935. /* Get the current position. */
  936. sigma_read_pos(&stoppos, &triggerpos, devc);
  937. /* Check if trigger has fired. */
  938. modestatus = sigma_get_register(READ_MODE, devc);
  939. if (modestatus & 0x20) {
  940. trg_line = triggerpos >> 9;
  941. trg_event = triggerpos & 0x1ff;
  942. }
  943. /*
  944. * Determine how many 1024b "DRAM lines" do we need to read from the
  945. * Sigma so we have a complete set of samples. Note that the last
  946. * line can be only partial, containing less than 64 clusters.
  947. */
  948. dl_lines_total = (stoppos >> 9) + 1;
  949. dl_lines_done = 0;
  950. while (dl_lines_total > dl_lines_done) {
  951. /* We can download only up-to 32 DRAM lines in one go! */
  952. dl_lines_curr = MIN(chunks_per_read, dl_lines_total);
  953. bufsz = sigma_read_dram(dl_lines_done, dl_lines_curr,
  954. (uint8_t *)dram_line, devc);
  955. /* TODO: Check bufsz. For now, just avoid compiler warnings. */
  956. (void)bufsz;
  957. /* This is the first DRAM line, so find the initial timestamp. */
  958. if (dl_lines_done == 0) {
  959. devc->state.lastts =
  960. sigma_dram_cluster_ts(&dram_line[0].cluster[0]);
  961. devc->state.lastsample = 0;
  962. }
  963. for (i = 0; i < dl_lines_curr; i++) {
  964. uint32_t trigger_event = ~0;
  965. /* The last "DRAM line" can be only partially full. */
  966. if (dl_lines_done + i == dl_lines_total - 1)
  967. dl_events_in_line = stoppos & 0x1ff;
  968. /* Test if the trigger happened on this line. */
  969. if (dl_lines_done + i == trg_line)
  970. trigger_event = trg_event;
  971. decode_chunk_ts(dram_line + i, dl_events_in_line,
  972. trigger_event, sdi);
  973. }
  974. dl_lines_done += dl_lines_curr;
  975. }
  976. /* All done. */
  977. packet.type = SR_DF_END;
  978. sr_session_send(sdi, &packet);
  979. dev_acquisition_stop(sdi, sdi);
  980. g_free(dram_line);
  981. return TRUE;
  982. }
  983. /*
  984. * Handle the Sigma when in CAPTURE mode. This function checks:
  985. * - Sampling time ended
  986. * - DRAM capacity overflow
  987. * This function triggers download of the samples from Sigma
  988. * in case either of the above conditions is true.
  989. */
  990. static int sigma_capture_mode(struct sr_dev_inst *sdi)
  991. {
  992. struct dev_context *devc = sdi->priv;
  993. uint64_t running_msec;
  994. struct timeval tv;
  995. uint32_t stoppos, triggerpos;
  996. /* Check if the selected sampling duration passed. */
  997. gettimeofday(&tv, 0);
  998. running_msec = (tv.tv_sec - devc->start_tv.tv_sec) * 1000 +
  999. (tv.tv_usec - devc->start_tv.tv_usec) / 1000;
  1000. if (running_msec >= devc->limit_msec)
  1001. return download_capture(sdi);
  1002. /* Get the position in DRAM to which the FPGA is writing now. */
  1003. sigma_read_pos(&stoppos, &triggerpos, devc);
  1004. /* Test if DRAM is full and if so, download the data. */
  1005. if ((stoppos >> 9) == 32767)
  1006. return download_capture(sdi);
  1007. return TRUE;
  1008. }
  1009. static int receive_data(int fd, int revents, void *cb_data)
  1010. {
  1011. struct sr_dev_inst *sdi;
  1012. struct dev_context *devc;
  1013. (void)fd;
  1014. (void)revents;
  1015. sdi = cb_data;
  1016. devc = sdi->priv;
  1017. if (devc->state.state == SIGMA_IDLE)
  1018. return TRUE;
  1019. if (devc->state.state == SIGMA_CAPTURE)
  1020. return sigma_capture_mode(sdi);
  1021. return TRUE;
  1022. }
  1023. /* Build a LUT entry used by the trigger functions. */
  1024. static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
  1025. {
  1026. int i, j, k, bit;
  1027. /* For each quad channel. */
  1028. for (i = 0; i < 4; ++i) {
  1029. entry[i] = 0xffff;
  1030. /* For each bit in LUT. */
  1031. for (j = 0; j < 16; ++j)
  1032. /* For each channel in quad. */
  1033. for (k = 0; k < 4; ++k) {
  1034. bit = 1 << (i * 4 + k);
  1035. /* Set bit in entry */
  1036. if ((mask & bit) &&
  1037. ((!(value & bit)) !=
  1038. (!(j & (1 << k)))))
  1039. entry[i] &= ~(1 << j);
  1040. }
  1041. }
  1042. }
  1043. /* Add a logical function to LUT mask. */
  1044. static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
  1045. int index, int neg, uint16_t *mask)
  1046. {
  1047. int i, j;
  1048. int x[2][2], tmp, a, b, aset, bset, rset;
  1049. memset(x, 0, 4 * sizeof(int));
  1050. /* Trigger detect condition. */
  1051. switch (oper) {
  1052. case OP_LEVEL:
  1053. x[0][1] = 1;
  1054. x[1][1] = 1;
  1055. break;
  1056. case OP_NOT:
  1057. x[0][0] = 1;
  1058. x[1][0] = 1;
  1059. break;
  1060. case OP_RISE:
  1061. x[0][1] = 1;
  1062. break;
  1063. case OP_FALL:
  1064. x[1][0] = 1;
  1065. break;
  1066. case OP_RISEFALL:
  1067. x[0][1] = 1;
  1068. x[1][0] = 1;
  1069. break;
  1070. case OP_NOTRISE:
  1071. x[1][1] = 1;
  1072. x[0][0] = 1;
  1073. x[1][0] = 1;
  1074. break;
  1075. case OP_NOTFALL:
  1076. x[1][1] = 1;
  1077. x[0][0] = 1;
  1078. x[0][1] = 1;
  1079. break;
  1080. case OP_NOTRISEFALL:
  1081. x[1][1] = 1;
  1082. x[0][0] = 1;
  1083. break;
  1084. }
  1085. /* Transpose if neg is set. */
  1086. if (neg) {
  1087. for (i = 0; i < 2; ++i) {
  1088. for (j = 0; j < 2; ++j) {
  1089. tmp = x[i][j];
  1090. x[i][j] = x[1-i][1-j];
  1091. x[1-i][1-j] = tmp;
  1092. }
  1093. }
  1094. }
  1095. /* Update mask with function. */
  1096. for (i = 0; i < 16; ++i) {
  1097. a = (i >> (2 * index + 0)) & 1;
  1098. b = (i >> (2 * index + 1)) & 1;
  1099. aset = (*mask >> i) & 1;
  1100. bset = x[b][a];
  1101. if (func == FUNC_AND || func == FUNC_NAND)
  1102. rset = aset & bset;
  1103. else if (func == FUNC_OR || func == FUNC_NOR)
  1104. rset = aset | bset;
  1105. else if (func == FUNC_XOR || func == FUNC_NXOR)
  1106. rset = aset ^ bset;
  1107. if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR)
  1108. rset = !rset;
  1109. *mask &= ~(1 << i);
  1110. if (rset)
  1111. *mask |= 1 << i;
  1112. }
  1113. }
  1114. /*
  1115. * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
  1116. * simple pin change and state triggers. Only two transitions (rise/fall) can be
  1117. * set at any time, but a full mask and value can be set (0/1).
  1118. */
  1119. static int build_basic_trigger(struct triggerlut *lut, struct dev_context *devc)
  1120. {
  1121. int i,j;
  1122. uint16_t masks[2] = { 0, 0 };
  1123. memset(lut, 0, sizeof(struct triggerlut));
  1124. /* Contant for simple triggers. */
  1125. lut->m4 = 0xa000;
  1126. /* Value/mask trigger support. */
  1127. build_lut_entry(devc->trigger.simplevalue, devc->trigger.simplemask,
  1128. lut->m2d);
  1129. /* Rise/fall trigger support. */
  1130. for (i = 0, j = 0; i < 16; ++i) {
  1131. if (devc->trigger.risingmask & (1 << i) ||
  1132. devc->trigger.fallingmask & (1 << i))
  1133. masks[j++] = 1 << i;
  1134. }
  1135. build_lut_entry(masks[0], masks[0], lut->m0d);
  1136. build_lut_entry(masks[1], masks[1], lut->m1d);
  1137. /* Add glue logic */
  1138. if (masks[0] || masks[1]) {
  1139. /* Transition trigger. */
  1140. if (masks[0] & devc->trigger.risingmask)
  1141. add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3);
  1142. if (masks[0] & devc->trigger.fallingmask)
  1143. add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3);
  1144. if (masks[1] & devc->trigger.risingmask)
  1145. add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3);
  1146. if (masks[1] & devc->trigger.fallingmask)
  1147. add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3);
  1148. } else {
  1149. /* Only value/mask trigger. */
  1150. lut->m3 = 0xffff;
  1151. }
  1152. /* Triggertype: event. */
  1153. lut->params.selres = 3;
  1154. return SR_OK;
  1155. }
  1156. static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
  1157. {
  1158. struct dev_context *devc;
  1159. struct clockselect_50 clockselect;
  1160. int frac, triggerpin, ret;
  1161. uint8_t triggerselect = 0;
  1162. struct triggerinout triggerinout_conf;
  1163. struct triggerlut lut;
  1164. if (sdi->status != SR_ST_ACTIVE)
  1165. return SR_ERR_DEV_CLOSED;
  1166. devc = sdi->priv;
  1167. if (configure_channels(sdi) != SR_OK) {
  1168. sr_err("Failed to configure channels.");
  1169. return SR_ERR;
  1170. }
  1171. /* If the samplerate has not been set, default to 200 kHz. */
  1172. if (devc->cur_firmware == -1) {
  1173. if ((ret = set_samplerate(sdi, SR_KHZ(200))) != SR_OK)
  1174. return ret;
  1175. }
  1176. /* Enter trigger programming mode. */
  1177. sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20, devc);
  1178. /* 100 and 200 MHz mode. */
  1179. if (devc->cur_samplerate >= SR_MHZ(100)) {
  1180. sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81, devc);
  1181. /* Find which pin to trigger on from mask. */
  1182. for (triggerpin = 0; triggerpin < 8; ++triggerpin)
  1183. if ((devc->trigger.risingmask | devc->trigger.fallingmask) &
  1184. (1 << triggerpin))
  1185. break;
  1186. /* Set trigger pin and light LED on trigger. */
  1187. triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7);
  1188. /* Default rising edge. */
  1189. if (devc->trigger.fallingmask)
  1190. triggerselect |= 1 << 3;
  1191. /* All other modes. */
  1192. } else if (devc->cur_samplerate <= SR_MHZ(50)) {
  1193. build_basic_trigger(&lut, devc);
  1194. sigma_write_trigger_lut(&lut, devc);
  1195. triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0);
  1196. }
  1197. /* Setup trigger in and out pins to default values. */
  1198. memset(&triggerinout_conf, 0, sizeof(struct triggerinout));
  1199. triggerinout_conf.trgout_bytrigger = 1;
  1200. triggerinout_conf.trgout_enable = 1;
  1201. sigma_write_register(WRITE_TRIGGER_OPTION,
  1202. (uint8_t *) &triggerinout_conf,
  1203. sizeof(struct triggerinout), devc);
  1204. /* Go back to normal mode. */
  1205. sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect, devc);
  1206. /* Set clock select register. */
  1207. if (devc->cur_samplerate == SR_MHZ(200))
  1208. /* Enable 4 channels. */
  1209. sigma_set_register(WRITE_CLOCK_SELECT, 0xf0, devc);
  1210. else if (devc->cur_samplerate == SR_MHZ(100))
  1211. /* Enable 8 channels. */
  1212. sigma_set_register(WRITE_CLOCK_SELECT, 0x00, devc);
  1213. else {
  1214. /*
  1215. * 50 MHz mode (or fraction thereof). Any fraction down to
  1216. * 50 MHz / 256 can be used, but is not supported by sigrok API.
  1217. */
  1218. frac = SR_MHZ(50) / devc->cur_samplerate - 1;
  1219. clockselect.async = 0;
  1220. clockselect.fraction = frac;
  1221. clockselect.disabled_channels = 0;
  1222. sigma_write_register(WRITE_CLOCK_SELECT,
  1223. (uint8_t *) &clockselect,
  1224. sizeof(clockselect), devc);
  1225. }
  1226. /* Setup maximum post trigger time. */
  1227. sigma_set_register(WRITE_POST_TRIGGER,
  1228. (devc->capture_ratio * 255) / 100, devc);
  1229. /* Start acqusition. */
  1230. gettimeofday(&devc->start_tv, 0);
  1231. sigma_set_register(WRITE_MODE, 0x0d, devc);
  1232. devc->cb_data = cb_data;
  1233. /* Send header packet to the session bus. */
  1234. std_session_send_df_header(cb_data, LOG_PREFIX);
  1235. /* Add capture source. */
  1236. sr_source_add(0, G_IO_IN, 10, receive_data, (void *)sdi);
  1237. devc->state.state = SIGMA_CAPTURE;
  1238. return SR_OK;
  1239. }
  1240. static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
  1241. {
  1242. struct dev_context *devc;
  1243. (void)cb_data;
  1244. devc = sdi->priv;
  1245. devc->state.state = SIGMA_IDLE;
  1246. sr_source_remove(0);
  1247. return SR_OK;
  1248. }
  1249. SR_PRIV struct sr_dev_driver asix_sigma_driver_info = {
  1250. .name = "asix-sigma",
  1251. .longname = "ASIX SIGMA/SIGMA2",
  1252. .api_version = 1,
  1253. .init = init,
  1254. .cleanup = cleanup,
  1255. .scan = scan,
  1256. .dev_list = dev_list,
  1257. .dev_clear = dev_clear,
  1258. .config_get = config_get,
  1259. .config_set = config_set,
  1260. .config_list = config_list,
  1261. .dev_open = dev_open,
  1262. .dev_close = dev_close,
  1263. .dev_acquisition_start = dev_acquisition_start,
  1264. .dev_acquisition_stop = dev_acquisition_stop,
  1265. .priv = NULL,
  1266. };