ipecamera.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. #define _IPECAMERA_IMAGE_C
  2. #define _BSD_SOURCE
  3. #define _GNU_SOURCE
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <sys/time.h>
  9. #include <pthread.h>
  10. #include <assert.h>
  11. #include <ufodecode.h>
  12. #include "../tools.h"
  13. #include "../error.h"
  14. #include "../event.h"
  15. #include "pcilib.h"
  16. #include "private.h"
  17. #include "model.h"
  18. #include "reader.h"
  19. #include "events.h"
  20. #include "data.h"
  21. #include "dma/nwl_dma.h"
  22. #ifdef IPECAMERA_DEBUG
  23. #include "dma/nwl.h"
  24. #endif /* IPECAMERA_DEBUG */
  25. #define FIND_REG(var, bank, name) \
  26. ctx->var = pcilib_find_register(pcilib, bank, name); \
  27. if (ctx->var == PCILIB_REGISTER_INVALID) { \
  28. err = PCILIB_ERROR_NOTFOUND; \
  29. pcilib_error("Unable to find a %s register", name); \
  30. }
  31. #define GET_REG(reg, var) \
  32. if (!err) { \
  33. err = pcilib_read_register_by_id(pcilib, ctx->reg, &var); \
  34. if (err) { \
  35. pcilib_error("Error reading %s register", ipecamera_registers[ctx->reg].name); \
  36. } \
  37. }
  38. #define SET_REG(reg, val) \
  39. if (!err) { \
  40. err = pcilib_write_register_by_id(pcilib, ctx->reg, val); \
  41. if (err) { \
  42. pcilib_error("Error writting %s register", ipecamera_registers[ctx->reg].name); \
  43. } \
  44. }
  45. #define CHECK_REG(reg, check) \
  46. if (!err) { \
  47. err = pcilib_read_register_by_id(pcilib, ctx->reg, &value); \
  48. if (err) { \
  49. pcilib_error("Error reading %s register", ipecamera_registers[ctx->reg].name); \
  50. } \
  51. if (value != check) { \
  52. pcilib_error("Unexpected value (0x%lx) of register %s", value, ipecamera_registers[ctx->reg].name); \
  53. err = PCILIB_ERROR_INVALID_DATA; \
  54. } \
  55. }
  56. #define IPECAMERA_GET_EXPECTED_STATUS(ctx) ((ctx->firmware == 4)?IPECAMERA_EXPECTED_STATUS_4:IPECAMERA_EXPECTED_STATUS)
  57. #define CHECK_STATUS_REG() CHECK_REG(status_reg, IPECAMERA_GET_EXPECTED_STATUS(ctx))
  58. #define CHECK_VALUE(value, val) \
  59. if ((!err)&&(value != val)) { \
  60. pcilib_error("Unexpected value (0x%x) in data stream (0x%x is expected)", value, val); \
  61. err = PCILIB_ERROR_INVALID_DATA; \
  62. }
  63. #define CHECK_FLAG(flag, check, ...) \
  64. if ((!err)&&(!(check))) { \
  65. pcilib_error("Unexpected value (0x%x) of " flag, __VA_ARGS__); \
  66. err = PCILIB_ERROR_INVALID_DATA; \
  67. }
  68. pcilib_context_t *ipecamera_init(pcilib_t *pcilib) {
  69. int err = 0;
  70. ipecamera_t *ctx = malloc(sizeof(ipecamera_t));
  71. if (ctx) {
  72. pcilib_register_value_t value;
  73. memset(ctx, 0, sizeof(ipecamera_t));
  74. ctx->buffer_size = IPECAMERA_DEFAULT_BUFFER_SIZE;
  75. ctx->dim.bpp = sizeof(ipecamera_pixel_t) * 8;
  76. // We need DMA engine initialized to resolve DMA registers
  77. // FIND_REG(packet_len_reg, "fpga", "xrawdata_packet_length");
  78. FIND_REG(status_reg, "fpga", "status");
  79. FIND_REG(control_reg, "fpga", "control");
  80. FIND_REG(status3_reg, "fpga", "status3");
  81. FIND_REG(n_lines_reg, "cmosis", "cmosis_number_lines");
  82. FIND_REG(line_reg, "cmosis", "cmosis_start1");
  83. FIND_REG(exposure_reg, "cmosis", "cmosis_exp_time");
  84. FIND_REG(flip_reg, "cmosis", "cmosis_image_flipping");
  85. FIND_REG(firmware_version_reg, "fpga", "firmware_version");
  86. FIND_REG(adc_resolution_reg, "fpga", "adc_resolution");
  87. FIND_REG(output_mode_reg, "fpga", "output_mode");
  88. FIND_REG(max_frames_reg, "fpga", "ddr_max_frames");
  89. FIND_REG(num_frames_reg, "fpga", "ddr_num_frames");
  90. GET_REG(firmware_version_reg, value);
  91. switch (value) {
  92. case 4:
  93. case 5:
  94. ctx->firmware = value;
  95. break;
  96. default:
  97. pcilib_error("Unsupported version of firmware (%lu)", value);
  98. }
  99. #ifdef IPECAMERA_BUG_POSTPONED_READ
  100. GET_REG(max_frames_reg, value);
  101. if ((value + IPECAMERA_RESERVE_BUFFERS + 3) > ctx->buffer_size) {
  102. ctx->buffer_size = (value + 1) + IPECAMERA_RESERVE_BUFFERS + 2;
  103. }
  104. #endif /* IPECAMERA_BUG_POSTPONED_READ */
  105. ctx->rdma = PCILIB_DMA_ENGINE_INVALID;
  106. ctx->wdma = PCILIB_DMA_ENGINE_INVALID;
  107. if (err) {
  108. free(ctx);
  109. return NULL;
  110. }
  111. }
  112. return (pcilib_context_t*)ctx;
  113. }
  114. void ipecamera_free(pcilib_context_t *vctx) {
  115. if (vctx) {
  116. ipecamera_t *ctx = (ipecamera_t*)vctx;
  117. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  118. free(ctx);
  119. }
  120. }
  121. pcilib_dma_context_t *ipecamera_init_dma(pcilib_context_t *vctx) {
  122. // ipecamera_t *ctx = (ipecamera_t*)vctx;
  123. pcilib_model_description_t *model_info = pcilib_get_model_description(vctx->pcilib);
  124. if ((!model_info->dma_api)||(!model_info->dma_api->init)) {
  125. pcilib_error("The DMA engine is not configured in model");
  126. return NULL;
  127. }
  128. #ifdef IPECAMERA_DMA_R3
  129. return model_info->dma_api->init(vctx->pcilib, PCILIB_NWL_MODIFICATION_IPECAMERA, NULL);
  130. #else
  131. return model_info->dma_api->init(vctx->pcilib, PCILIB_DMA_MODIFICATION_DEFAULT, NULL);
  132. #endif
  133. }
  134. int ipecamera_set_buffer_size(ipecamera_t *ctx, int size) {
  135. if (ctx->started) {
  136. pcilib_error("Can't change buffer size while grabbing");
  137. return PCILIB_ERROR_INVALID_REQUEST;
  138. }
  139. if (size < 2) {
  140. pcilib_error("The buffer size is too small");
  141. return PCILIB_ERROR_INVALID_REQUEST;
  142. }
  143. if (((size^(size-1)) < size) < size) {
  144. pcilib_error("The buffer size is not power of 2");
  145. }
  146. ctx->buffer_size = size;
  147. return 0;
  148. }
  149. int ipecamera_reset(pcilib_context_t *vctx) {
  150. int err = 0;
  151. ipecamera_t *ctx = (ipecamera_t*)vctx;
  152. pcilib_t *pcilib = vctx->pcilib;
  153. pcilib_register_t control, status;
  154. pcilib_register_value_t value;
  155. if (!ctx) {
  156. pcilib_error("IPECamera imaging is not initialized");
  157. return PCILIB_ERROR_NOTINITIALIZED;
  158. }
  159. pcilib = vctx->pcilib;
  160. control = ctx->control_reg;
  161. status = ctx->status_reg;
  162. // Set Reset bit to CMOSIS
  163. err = pcilib_write_register_by_id(pcilib, control, 0x1e4);
  164. if (err) {
  165. pcilib_error("Error setting FPGA reset bit");
  166. return err;
  167. }
  168. usleep(IPECAMERA_SLEEP_TIME);
  169. // Remove Reset bit to CMOSIS
  170. err = pcilib_write_register_by_id(pcilib, control, 0x1e1);
  171. if (err) {
  172. pcilib_error("Error reseting FPGA reset bit");
  173. return err;
  174. }
  175. usleep(IPECAMERA_SLEEP_TIME);
  176. // Special settings for CMOSIS v.2
  177. value = 01; err = pcilib_write_register_space(pcilib, "cmosis", 115, 1, &value);
  178. if (err) {
  179. pcilib_error("Error setting CMOSIS configuration");
  180. return err;
  181. }
  182. usleep(IPECAMERA_SLEEP_TIME);
  183. value = 07; err = pcilib_write_register_space(pcilib, "cmosis", 82, 1, &value);
  184. if (err) {
  185. pcilib_error("Error setting CMOSIS configuration");
  186. return err;
  187. }
  188. usleep(IPECAMERA_SLEEP_TIME);
  189. // Set default parameters
  190. err = pcilib_write_register_by_id(pcilib, control, IPECAMERA_IDLE);
  191. if (err) {
  192. pcilib_error("Error bringing FPGA in default mode");
  193. return err;
  194. }
  195. usleep(10000);
  196. err = pcilib_read_register_by_id(pcilib, status, &value);
  197. if (err) {
  198. pcilib_error("Error reading status register");
  199. return err;
  200. }
  201. if (value != IPECAMERA_GET_EXPECTED_STATUS(ctx)) {
  202. pcilib_error("Unexpected value (%lx) of status register, expected %lx", value, IPECAMERA_GET_EXPECTED_STATUS(ctx));
  203. return PCILIB_ERROR_VERIFY;
  204. }
  205. return 0;
  206. }
  207. int ipecamera_start(pcilib_context_t *vctx, pcilib_event_t event_mask, pcilib_event_flags_t flags) {
  208. int i;
  209. int err = 0;
  210. ipecamera_t *ctx = (ipecamera_t*)vctx;
  211. pcilib_t *pcilib = vctx->pcilib;
  212. pcilib_register_value_t value;
  213. pthread_attr_t attr;
  214. struct sched_param sched;
  215. if (!ctx) {
  216. pcilib_error("IPECamera imaging is not initialized");
  217. return PCILIB_ERROR_NOTINITIALIZED;
  218. }
  219. if (ctx->started) {
  220. pcilib_error("IPECamera grabbing is already started");
  221. return PCILIB_ERROR_INVALID_REQUEST;
  222. }
  223. // Allow readout and clean the FRAME_REQUEST mode if set for some reason
  224. GET_REG(control_reg, value);
  225. SET_REG(control_reg, value|IPECAMERA_READOUT_FLAG);
  226. usleep(IPECAMERA_SLEEP_TIME);
  227. if (value&0x1000) ctx->fr_mode = 1;
  228. else {
  229. ctx->fr_mode = 0;
  230. CHECK_STATUS_REG();
  231. if (err) return err;
  232. }
  233. ctx->event_id = 0;
  234. ctx->preproc_id = 0;
  235. ctx->reported_id = 0;
  236. ctx->buffer_pos = 0;
  237. ctx->parse_data = (flags&PCILIB_EVENT_FLAG_RAW_DATA_ONLY)?0:1;
  238. ctx->cur_size = 0;
  239. ctx->dim.width = IPECAMERA_WIDTH;
  240. ctx->dim.height = IPECAMERA_MAX_LINES;
  241. // GET_REG(n_lines_reg, ctx->dim.height);
  242. GET_REG(output_mode_reg, value);
  243. switch (value) {
  244. case IPECAMERA_MODE_16_CHAN_IO:
  245. ctx->cmosis_outputs = 16;
  246. break;
  247. case IPECAMERA_MODE_4_CHAN_IO:
  248. ctx->cmosis_outputs = 4;
  249. break;
  250. default:
  251. pcilib_error("IPECamera reporting invalid output_mode 0x%lx", value);
  252. return PCILIB_ERROR_INVALID_STATE;
  253. }
  254. ipecamera_compute_buffer_size(ctx, ctx->dim.height);
  255. ctx->raw_size = ctx->cur_raw_size;
  256. ctx->full_size = ctx->cur_full_size;
  257. ctx->padded_size = ctx->cur_padded_size;
  258. ctx->image_size = ctx->dim.width * ctx->dim.height;
  259. GET_REG(max_frames_reg, value);
  260. ctx->max_frames = value;
  261. ctx->buffer = malloc(ctx->padded_size * ctx->buffer_size);
  262. if (!ctx->buffer) {
  263. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  264. pcilib_error("Unable to allocate ring buffer (%lu bytes)", ctx->padded_size * ctx->buffer_size);
  265. return PCILIB_ERROR_MEMORY;
  266. }
  267. ctx->image = (ipecamera_pixel_t*)malloc(ctx->image_size * ctx->buffer_size * sizeof(ipecamera_pixel_t));
  268. if (!ctx->image) {
  269. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  270. pcilib_error("Unable to allocate image buffer (%lu bytes)", ctx->image_size * ctx->buffer_size * sizeof(ipecamera_pixel_t));
  271. return PCILIB_ERROR_MEMORY;
  272. }
  273. ctx->cmask = malloc(ctx->dim.height * ctx->buffer_size * sizeof(ipecamera_change_mask_t));
  274. if (!ctx->cmask) {
  275. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  276. pcilib_error("Unable to allocate change-mask buffer");
  277. return PCILIB_ERROR_MEMORY;
  278. }
  279. ctx->frame = (ipecamera_frame_t*)malloc(ctx->buffer_size * sizeof(ipecamera_frame_t));
  280. if (!ctx->frame) {
  281. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  282. pcilib_error("Unable to allocate frame-info buffer");
  283. return PCILIB_ERROR_MEMORY;
  284. }
  285. memset(ctx->frame, 0, ctx->buffer_size * sizeof(ipecamera_frame_t));
  286. for (i = 0; i < ctx->buffer_size; i++) {
  287. err = pthread_rwlock_init(&ctx->frame[i].mutex, NULL);
  288. if (err) break;
  289. }
  290. ctx->frame_mutex_destroy = i;
  291. if (err) {
  292. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  293. pcilib_error("Initialization of rwlock mutexes for frame synchronization has failed");
  294. return PCILIB_ERROR_FAILED;
  295. }
  296. ctx->ipedec = ufo_decoder_new(ctx->dim.height, ctx->dim.width, NULL, 0);
  297. if (!ctx->ipedec) {
  298. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  299. pcilib_error("Unable to initialize IPECamera decoder library");
  300. return PCILIB_ERROR_FAILED;
  301. }
  302. if (!err) {
  303. ctx->rdma = pcilib_find_dma_by_addr(vctx->pcilib, PCILIB_DMA_FROM_DEVICE, IPECAMERA_DMA_ADDRESS);
  304. if (ctx->rdma == PCILIB_DMA_ENGINE_INVALID) {
  305. err = PCILIB_ERROR_NOTFOUND;
  306. pcilib_error("The C2S channel of IPECamera DMA Engine (%u) is not found", IPECAMERA_DMA_ADDRESS);
  307. } else {
  308. err = pcilib_start_dma(vctx->pcilib, ctx->rdma, PCILIB_DMA_FLAGS_DEFAULT);
  309. if (err) {
  310. ctx->rdma = PCILIB_DMA_ENGINE_INVALID;
  311. pcilib_error("Failed to initialize C2S channel of IPECamera DMA Engine (%u)", IPECAMERA_DMA_ADDRESS);
  312. }
  313. }
  314. }
  315. /*
  316. if (!err) {
  317. ctx->wdma = pcilib_find_dma_by_addr(vctx->pcilib, PCILIB_DMA_TO_DEVICE, IPECAMERA_DMA_ADDRESS);
  318. if (ctx->wdma == PCILIB_DMA_ENGINE_INVALID) {
  319. err = PCILIB_ERROR_NOTFOUND;
  320. pcilib_error("The S2C channel of IPECamera DMA Engine (%u) is not found", IPECAMERA_DMA_ADDRESS);
  321. } else {
  322. err = pcilib_start_dma(vctx->pcilib, ctx->wdma, PCILIB_DMA_FLAGS_DEFAULT);
  323. if (err) {
  324. ctx->wdma = PCILIB_DMA_ENGINE_INVALID;
  325. pcilib_error("Failed to initialize S2C channel of IPECamera DMA Engine (%u)", IPECAMERA_DMA_ADDRESS);
  326. }
  327. }
  328. }
  329. */
  330. /*
  331. SET_REG(packet_len_reg, IPECAMERA_DMA_PACKET_LENGTH);
  332. */
  333. if (err) {
  334. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  335. return err;
  336. }
  337. // Clean DMA
  338. #ifndef IPECAMERA_BUG_POSTPONED_READ
  339. err = pcilib_skip_dma(vctx->pcilib, ctx->rdma);
  340. if (err) {
  341. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  342. pcilib_error("Can't start grabbing, device continuously writes unexpected data using DMA engine");
  343. return err;
  344. }
  345. #endif /* ! IPECAMERA_BUG_POSTPONED_READ */
  346. if (vctx->params.autostop.duration) {
  347. gettimeofday(&ctx->autostop.timestamp, NULL);
  348. ctx->autostop.timestamp.tv_usec += vctx->params.autostop.duration % 1000000;
  349. if (ctx->autostop.timestamp.tv_usec > 999999) {
  350. ctx->autostop.timestamp.tv_sec += 1 + vctx->params.autostop.duration / 1000000;
  351. ctx->autostop.timestamp.tv_usec -= 1000000;
  352. } else {
  353. ctx->autostop.timestamp.tv_sec += vctx->params.autostop.duration / 1000000;
  354. }
  355. }
  356. if (vctx->params.autostop.max_events) {
  357. ctx->autostop.evid = vctx->params.autostop.max_events;
  358. }
  359. if ((ctx->parse_data)&&(flags&PCILIB_EVENT_FLAG_PREPROCESS)) {
  360. ctx->n_preproc = pcilib_get_cpu_count();
  361. // it would be greate to detect hyperthreading cores and ban them
  362. switch (ctx->n_preproc) {
  363. case 1: break;
  364. case 2 ... 3: ctx->n_preproc -= 1; break;
  365. default: ctx->n_preproc -= 2; break;
  366. }
  367. if ((vctx->params.parallel.max_threads)&&(vctx->params.parallel.max_threads < ctx->n_preproc))
  368. ctx->n_preproc = vctx->params.parallel.max_threads;
  369. ctx->preproc = (ipecamera_preprocessor_t*)malloc(ctx->n_preproc * sizeof(ipecamera_preprocessor_t));
  370. if (!ctx->preproc) {
  371. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  372. pcilib_error("Unable to allocate memory for preprocessor contexts");
  373. return PCILIB_ERROR_MEMORY;
  374. }
  375. memset(ctx->preproc, 0, ctx->n_preproc * sizeof(ipecamera_preprocessor_t));
  376. err = pthread_mutex_init(&ctx->preproc_mutex, NULL);
  377. if (err) {
  378. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  379. pcilib_error("Failed to initialize event mutex");
  380. return PCILIB_ERROR_FAILED;
  381. }
  382. ctx->preproc_mutex_destroy = 1;
  383. ctx->run_preprocessors = 1;
  384. for (i = 0; i < ctx->n_preproc; i++) {
  385. ctx->preproc[i].i = i;
  386. ctx->preproc[i].ipecamera = ctx;
  387. err = pthread_create(&ctx->preproc[i].thread, NULL, ipecamera_preproc_thread, ctx->preproc + i);
  388. if (err) {
  389. err = PCILIB_ERROR_FAILED;
  390. break;
  391. } else {
  392. ctx->preproc[i].started = 1;
  393. }
  394. }
  395. if (err) {
  396. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  397. pcilib_error("Failed to schedule some of the preprocessor threads");
  398. return err;
  399. }
  400. } else {
  401. ctx->n_preproc = 0;
  402. }
  403. ctx->started = 1;
  404. ctx->run_reader = 1;
  405. pthread_attr_init(&attr);
  406. if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO)) {
  407. pcilib_warning("Can't schedule a real-time thread, you may consider running as root");
  408. } else {
  409. sched.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1; // Let 1 priority for something really critcial
  410. pthread_attr_setschedparam(&attr, &sched);
  411. }
  412. if (pthread_create(&ctx->rthread, &attr, &ipecamera_reader_thread, (void*)ctx)) {
  413. ctx->started = 0;
  414. ipecamera_stop(vctx, PCILIB_EVENT_FLAGS_DEFAULT);
  415. err = PCILIB_ERROR_FAILED;
  416. }
  417. pthread_attr_destroy(&attr);
  418. return err;
  419. }
  420. int ipecamera_stop(pcilib_context_t *vctx, pcilib_event_flags_t flags) {
  421. int i;
  422. int err;
  423. void *retcode;
  424. ipecamera_t *ctx = (ipecamera_t*)vctx;
  425. if (!ctx) {
  426. pcilib_error("IPECamera imaging is not initialized");
  427. return PCILIB_ERROR_NOTINITIALIZED;
  428. }
  429. if (flags&PCILIB_EVENT_FLAG_STOP_ONLY) {
  430. ctx->run_reader = 0;
  431. return 0;
  432. }
  433. if (ctx->started) {
  434. ctx->run_reader = 0;
  435. err = pthread_join(ctx->rthread, &retcode);
  436. if (err) pcilib_error("Error joining the reader thread");
  437. }
  438. if (ctx->preproc) {
  439. ctx->run_preprocessors = 0;
  440. for (i = 0; i < ctx->n_preproc; i++) {
  441. if (ctx->preproc[i].started) {
  442. pthread_join(ctx->preproc[i].thread, &retcode);
  443. ctx->preproc[i].started = 0;
  444. }
  445. }
  446. if (ctx->preproc_mutex_destroy) {
  447. pthread_mutex_destroy(&ctx->preproc_mutex);
  448. ctx->preproc_mutex_destroy = 0;
  449. }
  450. free(ctx->preproc);
  451. ctx->preproc = NULL;
  452. }
  453. if (ctx->frame_mutex_destroy) {
  454. for (i = 0; i < ctx->frame_mutex_destroy; i++) {
  455. pthread_rwlock_destroy(&ctx->frame[i].mutex);
  456. }
  457. ctx->frame_mutex_destroy = 0;
  458. }
  459. if (ctx->wdma != PCILIB_DMA_ENGINE_INVALID) {
  460. pcilib_stop_dma(vctx->pcilib, ctx->wdma, PCILIB_DMA_FLAGS_DEFAULT);
  461. ctx->wdma = PCILIB_DMA_ENGINE_INVALID;
  462. }
  463. if (ctx->rdma != PCILIB_DMA_ENGINE_INVALID) {
  464. pcilib_stop_dma(vctx->pcilib, ctx->rdma, PCILIB_DMA_FLAGS_DEFAULT);
  465. ctx->rdma = PCILIB_DMA_ENGINE_INVALID;
  466. }
  467. while (ctx->streaming) {
  468. usleep(IPECAMERA_NOFRAME_SLEEP);
  469. }
  470. if (ctx->ipedec) {
  471. ufo_decoder_free(ctx->ipedec);
  472. ctx->ipedec = NULL;
  473. }
  474. if (ctx->frame) {
  475. free(ctx->frame);
  476. ctx->frame = NULL;
  477. }
  478. if (ctx->cmask) {
  479. free(ctx->cmask);
  480. ctx->cmask = NULL;
  481. }
  482. if (ctx->image) {
  483. free(ctx->image);
  484. ctx->image = NULL;
  485. }
  486. if (ctx->buffer) {
  487. free(ctx->buffer);
  488. ctx->buffer = NULL;
  489. }
  490. memset(&ctx->autostop, 0, sizeof(ipecamera_autostop_t));
  491. ctx->event_id = 0;
  492. ctx->reported_id = 0;
  493. ctx->buffer_pos = 0;
  494. ctx->started = 0;
  495. return 0;
  496. }
  497. int ipecamera_trigger(pcilib_context_t *vctx, pcilib_event_t event, size_t trigger_size, void *trigger_data) {
  498. int err = 0;
  499. pcilib_register_value_t value;
  500. ipecamera_t *ctx = (ipecamera_t*)vctx;
  501. pcilib_t *pcilib = vctx->pcilib;
  502. if (!ctx) {
  503. pcilib_error("IPECamera imaging is not initialized");
  504. return PCILIB_ERROR_NOTINITIALIZED;
  505. }
  506. pcilib_sleep_until_deadline(&ctx->next_trigger);
  507. GET_REG(num_frames_reg, value);
  508. if (value == ctx->max_frames) {
  509. return PCILIB_ERROR_BUSY;
  510. }
  511. /*
  512. do {
  513. usleep(10);
  514. GET_REG(status3_reg, value);
  515. } while (value&0x20000000);
  516. */
  517. SET_REG(control_reg, IPECAMERA_FRAME_REQUEST|IPECAMERA_READOUT_FLAG);
  518. usleep(IPECAMERA_WAIT_FRAME_RCVD_TIME);
  519. //CHECK_REG(status_reg, IPECAMERA_EXPECTED_STATUS);
  520. SET_REG(control_reg, IPECAMERA_IDLE|IPECAMERA_READOUT_FLAG);
  521. pcilib_calc_deadline(&ctx->next_trigger, IPECAMERA_NEXT_FRAME_DELAY);
  522. return 0;
  523. }