ipecamera.c 18 KB

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