From b222af943c405df509eb9df084d203da4fbd324e Mon Sep 17 00:00:00 2001
From: "Suren A. Chilingaryan" <csa@suren.me>
Date: Tue, 10 Mar 2015 21:47:38 +0100
Subject: ipecamera hack

---
 dma/ipe.c             |  2 +-
 ipecamera/data.c      |  6 ++++++
 ipecamera/ipecamera.c |  3 ++-
 ipecamera/model.h     |  9 +++++----
 ipecamera/private.h   | 10 +++++-----
 ipecamera/reader.c    | 17 ++++++++++-------
 pci.c                 |  2 +-
 pci.h                 |  2 +-
 8 files changed, 31 insertions(+), 20 deletions(-)

diff --git a/dma/ipe.c b/dma/ipe.c
index 147bf38..b4dcc8c 100644
--- a/dma/ipe.c
+++ b/dma/ipe.c
@@ -206,7 +206,7 @@ int dma_ipe_start(pcilib_dma_context_t *vctx, pcilib_dma_engine_t dma, pcilib_dm
 	}
 	
 	    // Enable DMA
-//	WR(IPEDMA_REG_CONTROL, 0x1);
+	WR(IPEDMA_REG_CONTROL, 0x1);
 	
 	ctx->last_read = IPEDMA_DMA_PAGES - 1;
 
diff --git a/ipecamera/data.c b/ipecamera/data.c
index 115d12e..fb29018 100644
--- a/ipecamera/data.c
+++ b/ipecamera/data.c
@@ -53,7 +53,13 @@ inline static int ipecamera_decode_frame(ipecamera_t *ctx, pcilib_event_id_t eve
 		
     pixels = ctx->image + buf_ptr * ctx->image_size;
     memset(ctx->cmask + ctx->buffer_pos * ctx->dim.height, 0, ctx->dim.height * sizeof(ipecamera_change_mask_t));
+/*
+    printf("decoding %lx...\n", ctx->raw_size);
+    FILE *f = fopen("/mnt/frame.xxx", "w");
+    fwrite(ctx->buffer + buf_ptr * ctx->padded_size, 1, ctx->raw_size, f);
+    fclose(f);*/
     res = ufo_decoder_decode_frame(ctx->ipedec, ctx->buffer + buf_ptr * ctx->padded_size, ctx->raw_size, pixels, &ctx->frame[buf_ptr].event.meta);
+//    puts("done\n");
     if (!res) {
         err = PCILIB_ERROR_FAILED;
         ctx->frame[buf_ptr].event.image_broken = err;
diff --git a/ipecamera/ipecamera.c b/ipecamera/ipecamera.c
index fe66948..1b3d305 100644
--- a/ipecamera/ipecamera.c
+++ b/ipecamera/ipecamera.c
@@ -653,11 +653,12 @@ int ipecamera_trigger(pcilib_context_t *vctx, pcilib_event_t event, size_t trigg
     }
 
     pcilib_sleep_until_deadline(&ctx->next_trigger);
-
+/*
     GET_REG(num_frames_reg, value);
     if (value == ctx->max_frames) {
 	return PCILIB_ERROR_BUSY;
     }
+*/
 /*
     do {
 	usleep(10);
diff --git a/ipecamera/model.h b/ipecamera/model.h
index cc47891..60226f0 100644
--- a/ipecamera/model.h
+++ b/ipecamera/model.h
@@ -9,7 +9,7 @@
 //#define IPECAMERA_DEBUG
 
 #define IPECAMERA_DMA_R3
-#define IPECAMERA_DMA_ADDRESS 1
+#define IPECAMERA_DMA_ADDRESS 0
 #define IPECAMERA_DMA_PACKET_LENGTH 4096
 
 //#define IPECAMERA_REGISTER_SPACE 0xfeaffc00
@@ -21,7 +21,8 @@
 pcilib_register_bank_description_t ipecamera_register_banks[] = {
     { PCILIB_REGISTER_BANK0,    PCILIB_BAR0,    128, IPECAMERA_REGISTER_PROTOCOL, IPECAMERA_REGISTER_READ , IPECAMERA_REGISTER_WRITE, PCILIB_LITTLE_ENDIAN, 8 , PCILIB_LITTLE_ENDIAN, "%lu"  , "cmosis", "CMOSIS CMV2000 Registers" },
     { PCILIB_REGISTER_BANK1,    PCILIB_BAR0, 0x0200, PCILIB_DEFAULT_PROTOCOL    , IPECAMERA_REGISTER_SPACE, IPECAMERA_REGISTER_SPACE, PCILIB_LITTLE_ENDIAN, 32, PCILIB_LITTLE_ENDIAN, "0x%lx", "fpga", "IPECamera Registers" },
-    { PCILIB_REGISTER_BANK_DMA, PCILIB_BAR0, 0xA000, PCILIB_DEFAULT_PROTOCOL    , 0,                        0,                        PCILIB_LITTLE_ENDIAN, 32, PCILIB_LITTLE_ENDIAN, "0x%lx", "dma", "DMA Registers"},
+//    { PCILIB_REGISTER_BANK_DMA, PCILIB_BAR0, 0xA000, PCILIB_DEFAULT_PROTOCOL    , 0,                        0,                        PCILIB_LITTLE_ENDIAN, 32, PCILIB_LITTLE_ENDIAN, "0x%lx", "dma", "DMA Registers"},
+    { PCILIB_REGISTER_BANK_DMA, PCILIB_BAR0, 0x0200, PCILIB_DEFAULT_PROTOCOL    , 0,                        0,                    PCILIB_LITTLE_ENDIAN, 32, PCILIB_LITTLE_ENDIAN, "0x%lx", "dma", "DMA Registers"},
     { 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL }
 };
 
@@ -125,8 +126,8 @@ pcilib_register_description_t ipecamera_registers[] = {
 };
 
 pcilib_register_range_t ipecamera_register_ranges[] = {
-    {0, 	128, 	PCILIB_REGISTER_BANK0, 0},
-    {0x9000,	0x9FFF,	PCILIB_REGISTER_BANK1, -0x9000},
+//    {0xF000, 	0xF000 + 128, 	PCILIB_REGISTER_BANK0, 0},
+//    {0x9000,	0x9FFF,	PCILIB_REGISTER_BANK1, -0x9000},
     {0, 0, 0, 0}
 };
 
diff --git a/ipecamera/private.h b/ipecamera/private.h
index 47c80a9..b6fd0f7 100644
--- a/ipecamera/private.h
+++ b/ipecamera/private.h
@@ -6,8 +6,8 @@
 #define IPECAMERA_BUG_EXTRA_DATA
 #define IPECAMERA_BUG_MULTIFRAME_PACKETS
 //#define IPECAMERA_BUG_INCOMPLETE_PACKETS
-//#define IPECAMERA_BUG_POSTPONED_READ
-//#define IPECAMERA_DEBUG_RAW_PACKETS		"/mnt/fast/frames"
+#define IPECAMERA_BUG_POSTPONED_READ
+//#define IPECAMERA_DEBUG_RAW_PACKETS		"/mnt/frames"
 
 //#define IPECAMERA_ANNOUNCE_READY		//**< announce new event only after the reconstruction is done */
 
@@ -30,10 +30,10 @@
 #define IPECAMERA_PIXELS_PER_CHANNEL 128
 #define IPECAMERA_WIDTH (IPECAMERA_MAX_CHANNELS * IPECAMERA_PIXELS_PER_CHANNEL)
 
-#define IPECAMERA_FRAME_REQUEST 		0x1E9
+#define IPECAMERA_FRAME_REQUEST 		0x80000209 // 0x1E9
 #define IPECAMERA_READOUT_FLAG			0x200
-#define IPECAMERA_READOUT			0x3E1
-#define IPECAMERA_IDLE 				0x1E1
+#define IPECAMERA_READOUT			0x80000201 // 0x3E1
+#define IPECAMERA_IDLE 				0x80000001 // 0x1E1
 #define IPECAMERA_START_INTERNAL_STIMULI 	0x1F1
 
 #define IPECAMERA_MODE_16_CHAN_IO		0
diff --git a/ipecamera/reader.c b/ipecamera/reader.c
index b9b7193..1f054ae 100644
--- a/ipecamera/reader.c
+++ b/ipecamera/reader.c
@@ -24,7 +24,7 @@
 
 int ipecamera_compute_buffer_size(ipecamera_t *ctx, size_t lines) {
     const size_t header_size = 8 * sizeof(ipecamera_payload_t);
-    const size_t footer_size = 8 * sizeof(ipecamera_payload_t);
+    const size_t footer_size = 16 * sizeof(ipecamera_payload_t);
 
     size_t line_size, raw_size, padded_blocks;
 
@@ -51,6 +51,7 @@ int ipecamera_compute_buffer_size(ipecamera_t *ctx, size_t lines) {
 #endif /* IPECAMERA_BUG_EXTRA_DATA */
 
     ctx->cur_padded_size = padded_blocks * IPECAMERA_DMA_PACKET_LENGTH;
+//    printf("%lu %lu %lu\n", ctx->cur_raw_size, ctx->cur_full_size, ctx->cur_padded_size);
 
     return 0;
 }
@@ -129,7 +130,7 @@ static int ipecamera_data_callback(void *user, pcilib_dma_flags_t flags, size_t
 #endif /* IPECAMERA_DEBUG_RAW_PACKETS */
 	    
 	    if (invalid_frame_id != ctx->event_id) {
-		pcilib_warning("No frame magic in DMA packet of %u bytes, current event %lu", bufsize, ctx->event_id);
+//		pcilib_warning("No frame magic in DMA packet of %u bytes, current event %lu (got %lu)", bufsize, ctx->event_id, invalid_frame_id);
 		invalid_frame_id = ctx->event_id;
 	    }
 
@@ -211,7 +212,9 @@ static int ipecamera_data_callback(void *user, pcilib_dma_flags_t flags, size_t
     ctx->cur_size += bufsize;
 //    printf("%i: %i %i\n", ctx->buffer_pos, ctx->cur_size, bufsize);
 
-    if (ctx->cur_size >= ctx->full_size) eof = 1;
+    if (ctx->cur_size >= ctx->full_size) {
+	eof = 1;
+    }
 
     if (ctx->event.params.rawdata.callback) {
 	res = ctx->event.params.rawdata.callback(ctx->event_id, (pcilib_event_info_t*)(ctx->frame + ctx->buffer_pos), (eof?PCILIB_EVENT_FLAG_EOF:PCILIB_EVENT_FLAGS_DEFAULT), bufsize, buf, ctx->event.params.rawdata.user);
@@ -241,14 +244,14 @@ void *ipecamera_reader_thread(void *user) {
     ipecamera_t *ctx = (ipecamera_t*)user;
     
     while (ctx->run_reader) {
-	err = pcilib_stream_dma(ctx->event.pcilib, ctx->rdma, 0, 0, PCILIB_DMA_FLAG_MULTIPACKET, PCILIB_DMA_TIMEOUT, &ipecamera_data_callback, user);
+	err = pcilib_stream_dma(ctx->event.pcilib, ctx->rdma, 0, 0, PCILIB_DMA_FLAG_MULTIPACKET, 10 * PCILIB_DMA_TIMEOUT, &ipecamera_data_callback, user);
 	if (err) {
 	    if (err == PCILIB_ERROR_TIMEOUT) {
 		if (ctx->cur_size >= ctx->cur_raw_size) ipecamera_new_frame(ctx);
 #ifdef IPECAMERA_BUG_INCOMPLETE_PACKETS
 		else if (ctx->cur_size > 0) ipecamera_new_frame(ctx);
 #endif /* IPECAMERA_BUG_INCOMPLETE_PACKETS */
-		if (pcilib_check_deadline(&ctx->autostop.timestamp, PCILIB_DMA_TIMEOUT)) {
+		if (pcilib_check_deadline(&ctx->autostop.timestamp, 10 * PCILIB_DMA_TIMEOUT)) {
 		    ctx->run_reader = 0;
 		    break;
 		}
@@ -261,8 +264,8 @@ void *ipecamera_reader_thread(void *user) {
     
     ctx->run_streamer = 0;
     
-    if (ctx->cur_size)
-	pcilib_error("partialy read frame after stop signal, %zu bytes in the buffer", ctx->cur_size);
+//    if (ctx->cur_size)
+//	pcilib_error("partialy read frame after stop signal, %zu bytes in the buffer", ctx->cur_size);
 
     return NULL;
 }
diff --git a/pci.c b/pci.c
index 474e315..ca858de 100644
--- a/pci.c
+++ b/pci.c
@@ -90,7 +90,7 @@ pcilib_model_t pcilib_get_model(pcilib_t *ctx) {
 	if ((board_info->vendor_id == PCIE_XILINX_VENDOR_ID)&&(board_info->device_id == PCIE_IPECAMERA_DEVICE_ID))
 	    ctx->model = PCILIB_MODEL_IPECAMERA;
 	else if ((board_info->vendor_id == PCIE_XILINX_VENDOR_ID)&&(board_info->device_id == PCIE_KAPTURE_DEVICE_ID))
-	    ctx->model = PCILIB_MODEL_KAPTURE;
+	    ctx->model = PCILIB_MODEL_IPECAMERA;
 	else
 	    ctx->model = PCILIB_MODEL_PCI;
     }
diff --git a/pci.h b/pci.h
index 4f3b9a1..8f8359c 100644
--- a/pci.h
+++ b/pci.h
@@ -60,7 +60,7 @@ struct pcilib_s {
 pcilib_model_description_t pcilib_model[4] = {
     { 4, PCILIB_HOST_ENDIAN, 	NULL, NULL, NULL, NULL, NULL, NULL },
     { 4, PCILIB_HOST_ENDIAN, 	NULL, NULL, NULL, NULL, NULL, NULL },
-    { 4, PCILIB_LITTLE_ENDIAN,	ipecamera_registers, ipecamera_register_banks, ipecamera_register_ranges, ipecamera_events, ipecamera_data_types, &nwl_dma_api, &ipecamera_image_api },
+    { 4, PCILIB_LITTLE_ENDIAN,	ipecamera_registers, ipecamera_register_banks, ipecamera_register_ranges, ipecamera_events, ipecamera_data_types, &ipe_dma_api, &ipecamera_image_api },
     { 4, PCILIB_LITTLE_ENDIAN,	kapture_registers, kapture_register_banks, kapture_register_ranges, kapture_events, kapture_data_types, &ipe_dma_api, &kapture_api },
 };
 
-- 
cgit v1.2.3