cis001v2

Revision:
8:66caa906d24c
Parent:
7:4c77f017b4b8
--- a/main.cpp	Wed Jan 06 13:35:17 2021 +0000
+++ b/main.cpp	Wed Feb 10 16:38:32 2021 +0000
@@ -2,6 +2,7 @@
 //my include
 #include "main.h"
 #include "common_task.h"
+#include "check_status.h"
 #include "register_address.h"
 
 //mbed include
@@ -19,6 +20,9 @@
 DCMI_HandleTypeDef hdcmi;
 DMA_HandleTypeDef hdma_dcmi;
 
+// DMA handle for mem2mem transfert 
+DMA_HandleTypeDef hd_m2m_dma;
+
 // Virtual USB Serial port
 Serial pc(USBTX, USBRX);
 
@@ -34,8 +38,8 @@
 char* commandbuffer;
 char* commandbuffertemp;
 
-
-int MSG_LEN=64;
+int IN_MSG_LEN=64;
+char OUT_MSG_LEN[5]="%64s";
 
 
 // SPI1
@@ -45,31 +49,45 @@
 
 //LEDs
 DigitalOut led1(LED1);  //PB_0
-DigitalOut led2(LED2);  //PB7
-DigitalOut led3(LED3);  //PB14
+DigitalOut led2(LED2);  //PB_7
+DigitalOut led3(LED3);  //PB_14
 
     
 // GPIOS
 DigitalOut RSTN(PA_0); 
 DigitalOut SLEEPN(PA_2); 
 DigitalOut CAPTURE(PA_9);
-InterruptIn button_capture(PC_13);
+DigitalIn button_capture(PC_13);
 InterruptIn irq_cis(PA_11);         //TODO
-DigitalOut CONFIG1(PA_8);           //TODO
-DigitalOut CONFIG2(PD_2);           //TODO
-//FastPWM MCLK(PA_3);         
+DigitalOut CONFIG1(PA_8);           
+DigitalOut CONFIG2(PD_2);           
+//FastPWM MCLK(PA_3);       
 
 
-//keep track fo the number of line and frame read
-int line_read=0;
-int frame_read=0;
-int imSize=frame_bsize_10b;
 
 // frame pointer and frame size
 char* FRAME_BUFFER;
-//char hidden_array[frame_bsize_10b];
+int imSize=frame_bsize_10b;
+
+
+int DEBUG=0;
+int DEBUG_DCMI=1;
+int TEST_DMA=1;
 
-int DEBUG=1;
+// DCMI callback handler
+static __IO int HAL_DCMI_ErrorCallback_value=0;
+static __IO int HAL_DCMI_IRQHandler_value=0;
+static __IO int line_read=0;
+static __IO int frame_read=0;
+static __IO int frame_ready_to_send=0;
+
+// DMA  callback handler
+static __IO int DMA1_Stream0_IRQHandler_value=0;
+static __IO int DMA1_Stream1_IRQHandler_value=0;
+static __IO int DMA2_Stream1_IRQHandler_value=0;
+static __IO int DMA_IRQHandler_value=0;
+static __IO int HAL_DMA_XferCpltCallback_value=0;
+static __IO int HAL_DMA_XferErrorCallback_value=0;
 
 
 
@@ -92,11 +110,11 @@
     for (i=0; i<1 ; i++){
         for (k=0; k<20; k++){
             for (j=0; j<32; j++){
-                //pc.printf("%3i ",(uint8_t) *(FRAME_BUFFER+i*640+k*32+j));
+                pc.printf("%3i ",(uint8_t) *(FRAME_BUFFER+i*640+k*32+j));
                 sprintf(commandbuffer, "%3i ",(uint8_t) *(FRAME_BUFFER+i*640+k*32+j));
                 byte_sent=cs.send(commandbuffer, strlen(commandbuffer));
             }
-            //pc.printf("\b\r\n");
+            pc.printf("\b\r\n");
             sprintf(commandbuffer, "\b\r\n");
             byte_sent=cs.send(commandbuffer, strlen(commandbuffer));
             
@@ -149,39 +167,43 @@
 *
 ************************************************************************************************/
 
-
-
-
 //https://github.com/tomvdb/stm32f401-nucleo-basic-template/blob/master/Drivers/BSP/STM324x9I_EVAL/stm324x9i_eval_camera.c
 /**
 @brief start dcmi and start frame capture
 @param none
 @return  void
 */
-void start_capture_10b(){
-        
+void start_capture(){
         frame_read=0;
         line_read=0;
         
         //memset(FRAME_BUFFER,0,(size_t) frame_bsize_10b);
-        //HAL_DCMI_Stop(&hdcmi);
-        //pc.printf("starting snapshot 10b\r\n");
+        HAL_DCMI_Stop(&hdcmi);
+        if(DEBUG_DCMI==1)
+            pc.printf("starting snapshot\r\n");
         
         // disable crop features
-        //HAL_DCMI_DisableCrop(&hdcmi);
+        HAL_DCMI_DisableCrop(&hdcmi);
         
         //enable DCMI
         ///HAL_DCMI_Start_DMA(&hdcmi, DCMI_MODE_SNAPSHOT, (uint32_t) FRAME_BUFFER, Im_size);
-        HAL_DCMI_Start_DMA (&hdcmi, DCMI_MODE_SNAPSHOT,(uint32_t) FRAME_BUFFER, (uint32_t) imSize);    
-            
-        //start capture
-        TASK_SPI_WRITE(capture_add,1);
-        wait_us(1000);             
-        TASK_SPI_WRITE(capture_add,0);
+        //HAL_StatusTypeDef status = HAL_DCMI_Start_DMA (&hdcmi, DCMI_MODE_SNAPSHOT,(uint32_t) FRAME_BUFFER, (uint32_t) frame_wsize_QVGA_10b);  
+        HAL_StatusTypeDef status = HAL_DCMI_Start_DMA (&hdcmi, DCMI_MODE_SNAPSHOT,(uint32_t) 0x30000000, (uint32_t) frame_wsize_QVGA_10b);  
+        wait_us(100);
+        
+        if(DEBUG_DCMI==1)  {
+            print_HAL_status_code(status);
+            print_DCMI_state_code(HAL_DCMI_GetState(&hdcmi));
+            print_DCMI_error_code(HAL_DCMI_GetError(&hdcmi));
+            pc.printf("%i XferCount\r\n",hdcmi.XferCount);
+            pc.printf("%i XferSize\r\n",hdcmi.XferSize);
+            pc.printf("%i XferTransferNumber\r\n",hdcmi.XferTransferNumber);
+            pc.printf("%x pBuffPtr\r\n",hdcmi.pBuffPtr);
+            pc.printf("%i ErrorCode\r\n",hdcmi.ErrorCode);
+        }
 
 }
 
-
 /**
 @brief suspend DCMI
 @param none
@@ -189,7 +211,10 @@
 */
 void suspend_capture(void){
     // Suspend the Camera Capture //
-    HAL_DCMI_Suspend(&hdcmi);     
+    HAL_DCMI_Suspend(&hdcmi); 
+    
+    HAL_DCMI_StateTypeDef DCMI_state = HAL_DCMI_GetState(&hdcmi);
+    print_DCMI_state_code(DCMI_state);    
 }
 
 /**
@@ -208,25 +233,29 @@
 @return  void
 */
 void stop_capture(void) {
-  if(HAL_DCMI_Stop(&hdcmi) == HAL_OK){
+    if(HAL_DCMI_Stop(&hdcmi) == HAL_OK){
      pc.printf("Error stopping camera\n\r");
-  }
-  else{
+    }else{
       pc.printf("Camera stopped\n\r"); 
     }
+    
+    HAL_DCMI_StateTypeDef DCMI_state = HAL_DCMI_GetState(&hdcmi);
+    print_DCMI_state_code(DCMI_state);
 }
 
 
-
+/**********************************
+*
+* override DCMI callbacks
+*
+**********************************/
 /**
 @brief override interrupt DCMI: on frame callback; send frame
 @param none
 @return  void
 */
 void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi){
-    pc.printf("frame event callback: read %i lines \r\n",line_read);    
-    line_read=0;
-    send_frame_ethernet_nohandshake_while();
+    frame_ready_to_send=1;
 }
 
 /**
@@ -235,19 +264,45 @@
 @return  void
 */
 void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi){
-    frame_read++;      
+    frame_read++;       
 }
 
 /**
-@brief override interrupt DCMI: on hsync callback; inrement line count
+@brief override interrupt DCMI: on hsync callback; increment line count
 @param none
 @return  void
 */
 void HAL_DCMI_LineEventCallback (DCMI_HandleTypeDef *hdcmi){  
-    line_read++;     
+    line_read++;  
+}
+
+/**
+@brief override interrupt DCMI: on error callback; 
+@param none
+@return  void
+*/
+void HAL_DCMI_ErrorCallback (DCMI_HandleTypeDef *hdcmi){    
+    HAL_DCMI_ErrorCallback_value=HAL_DCMI_GetError(hdcmi);
+    pc.printf("error callback\r\n");
 }
 
 /**
+@brief override interrupt DCMI: on IRQ handler; 
+@param none
+@return  void
+*/
+/*void HAL_DCMI_IRQHandler (DCMI_HandleTypeDef * hdcmi){
+    HAL_DCMI_IRQHandler_value=1;
+    pc.printf("DCMI IRQ handler\n\r");
+}*/
+
+/**********************************
+*
+* override DCMI callbacks
+*
+**********************************/
+
+/**
 @brief debug: set frmae buffer as a known pattern
 @param none
 @return  void
@@ -268,12 +323,6 @@
 
 }
 
-/**********************************************************************************************
-*
-*       debug
-*
-************************************************************************************************/
-
 void print_memory_info() {
     // allocate enough room for every thread's stack statistics
     int cnt = osThreadGetCount();
@@ -281,15 +330,55 @@
  
     cnt = mbed_stats_stack_get_each(stats, cnt);
     for (int i = 0; i < cnt; i++) {
-        printf("Thread: 0x%lX, Stack size: %lu / %lu\r\n", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size);
+        printf("Thread: 0x%iX, Stack size: %iu / %iu\r\n", stats[i].thread_id, stats[i].max_size, stats[i].reserved_size);
     }
     free(stats);
  
     // Grab the heap statistics
     mbed_stats_heap_t heap_stats;
     mbed_stats_heap_get(&heap_stats);
-    printf("Heap size: %lu / %lu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
+    printf("Heap size: %iu / %iu bytes\r\n", heap_stats.current_size, heap_stats.reserved_size);
+}
+
+
+/******************************************************************************/
+/* STM32H7xx Peripheral Interrupt Handlers                                    */
+/* Add here the Interrupt Handlers for the used peripherals.                  */
+/* For the available peripheral interrupt handler names,                      */
+/* please refer to the startup file (startup_stm32h7xx.s).                    */
+/******************************************************************************/
+/**
+  * @brief  This function is executed in case of complition.
+  * @param  MDMA_HandleTypeDef * hmdma
+  * @retval None
+  */
+void HAL_DMA_XferCpltCallback (DMA_HandleTypeDef * hddma){
+    HAL_DMA_XferCpltCallback_value=1;
 }
+/**
+  * @brief  This function is executed in case of error.
+  * @param  MDMA_HandleTypeDef * hmdma
+  * @retval None
+  */
+void HAL_DMA_XferErrorCallback (DMA_HandleTypeDef * hddma){
+    HAL_DMA_XferErrorCallback_value=1;
+}
+
+/**
+  * @brief This function handles DMA1 stream0 global interrupt.
+  */
+void DMA2_Stream1_IRQHandler(void){
+    HAL_DMA_IRQHandler(&hd_m2m_dma);
+    DMA2_Stream1_IRQHandler_value=1;
+}
+/*void DMA1_Stream0_IRQHandler(void){
+    HAL_DMA_IRQHandler(&hd_m2m_dma);
+    DMA1_Stream0_IRQHandler_value=1;
+}*/
+
+/*void DMA_IRQHandler(DMA_HandleTypeDef* hdma){
+    DMA_IRQHandler_value=1;
+}*/
 
 
 /**********************************************************************************************
@@ -310,56 +399,163 @@
     uint8_t OEN_flag=0;
     int status=0;
     int byte_sent;
-    char host_cmd[64];
+    char host_cmd[IN_MSG_LEN+1];
     int loopcount=0;
     int button_pushed=0;
     
     //ethernet 
-    char rbuffer[MSG_LEN];
+    char rbuffer[IN_MSG_LEN+1];
     char * pch;
     int rcount;
     int data=0;
     int add=0;
     int readvalue=0;
     int i=0;
-    int sizetosent=64;
+    int sizetosent=IN_MSG_LEN;
 
     
-    //***********************
+    //***********************************************
     /* Initialize all configured peripherals - First thing to do  */ 
-    //***********************
+    //***********************************************
+    HAL_Init();
+    // general purpose IO
     MX_GPIO_Init();
-    MX_DMA_Init();
-    MX_DCMI_Init_10b();
+    // digital camera interface
     
-    //************************
+    //************************************************
     // config serial print
-    //************************
+    //************************************************
     pc.baud(115200);//Set baudrate here. 
-    pc.printf("*****************\r\n CIS001  \r\n*****************\r\n");
-  
-    //************************
-    // init pointer
-    //************************
-    //
-    //!!!!!! attention : malloc is in num of words and not byte !!!!!!!! idem memset
-    //
-    //initialize frame buffer for 10b photo (malloc size en byte)
+    pc.printf("**********************************\r\n CIS001  \r\n**********************************\r\n");
+    pc.printf("Nucleo CPU SystemCoreClock is %d MHz\r\n", SystemCoreClock/1000000);
+    
+    MX_DMA_Init();
+    // flexible memory controller
+    MX_DCMI_Init_10b();
+    // direct memory access
+    
+    
+    //***********************************************
+    /* Disable/Enable the CPU Cache */ 
+    //***********************************************
     
+    // must clean Dcache before disabling;
+    SCB_CleanDCache();
+    SCB_DisableICache();
+    SCB_DisableDCache();
+    
+    //SCB_EnableICache();
+    //SCB_EnableDCache();
+        
+    
+    //************************************************
+    // reset leds
+    //************************************************
+    led1=0;
+    led2=0;
+    led3=0;
+    
+    //************************************************
+    // init pointers
+    //************************************************
+    pc.printf("**********************************\r\n");
+    pc.printf("Initialising pointer with malloc...\r\n");     
     print_memory_info();
     int random_memset= (rand()%3) ;
-    FRAME_BUFFER=(char *) malloc( frame_bsize_10b/2*sizeof(char));
-    memset((void *)FRAME_BUFFER,random_memset,(size_t) frame_bsize_10b/2); 
+    FRAME_BUFFER=(char *) malloc( frame_bsize_QVGA_10b*sizeof(char));
+    memset((void *)FRAME_BUFFER,random_memset,(size_t) frame_bsize_QVGA_10b); 
     print_memory_info();
 
+    //initialize commandbuffer pointer
+    commandbuffer=(char *) malloc((IN_MSG_LEN+1)*sizeof(char));
+    commandbuffertemp=(char *) malloc((IN_MSG_LEN+1)*sizeof(char));
+    pc.printf("done\r\n");
     
-    //initialize commandbuffer pointer
-    commandbuffer=(char *) malloc((MSG_LEN+1)*sizeof(char));
-    commandbuffertemp=(char *) malloc((MSG_LEN+1)*sizeof(char));
     
-    //************************
+    //************************************************
+    // test  DMA
+    //************************************************
+    if(TEST_DMA){
+        pc.printf("**********************************\r\n");
+        pc.printf("testing Mem2Mem DMA : writing from secondary SRAM to secondary SRAM\r\n");
+        
+        int debug_dma=0;
+        const int buff_size=16384; // word size
+        uint32_t* DMATestPatternAddresss= (uint32_t *) (0x30000000);
+        uint32_t* DMAReadAddress= (uint32_t *) (0x30000000+buff_size*4);
+        int nb_error_dma=0;
+        char pattern[4]={0x00,0x55,0xaa,0xFF};
+        uint32_t errordma;
+        HAL_DMA_StateTypeDef state_dma;
+        HAL_StatusTypeDef res_dma;
+        
+        for (int i=0;i<4;i++){
+            //set pattern & clean dest buffer
+            memset((void *) DMATestPatternAddresss, (int) pattern[i], (size_t) (buff_size*sizeof(uint32_t)/sizeof(char)) );
+            memset((void *) DMAReadAddress, (int) 0x01, (size_t) (buff_size*sizeof(uint32_t)/sizeof(char)) );
+            
+            if(debug_dma){
+                errordma = HAL_DMA_GetError (&hd_m2m_dma);
+                state_dma =  HAL_DMA_GetState (&hd_m2m_dma);
+                pc.printf("\n\r");
+                print_HAL_DMA_error_code(errordma);
+                print_HAL_DMA_status_code(state_dma);
+            }
+            
+            //reset interrupt variable
+            DMA2_Stream1_IRQHandler_value=0;
+            HAL_DMA_XferErrorCallback_value=0;
+            HAL_DMA_XferCpltCallback_value=0;
+            
+            res_dma= HAL_DMA_Start_IT(&hd_m2m_dma, (uint32_t) DMATestPatternAddresss, (uint32_t) DMAReadAddress, (uint32_t) buff_size);
+            if(debug_dma){
+                errordma = HAL_DMA_GetError (&hd_m2m_dma);
+                state_dma =  HAL_DMA_GetState (&hd_m2m_dma);
+                pc.printf("\n\r");
+                print_HAL_status_code(res_dma);
+                print_HAL_DMA_error_code(errordma);
+                print_HAL_DMA_status_code(state_dma);
+            }
+            
+            while (  !(DMA2_Stream1_IRQHandler_value|| HAL_DMA_XferCpltCallback_value || HAL_DMA_XferErrorCallback_value)){
+                 if(debug_dma){
+                     pc.printf(" %i %i %i\r\n",DMA2_Stream1_IRQHandler_value,HAL_DMA_XferCpltCallback_value,HAL_DMA_XferErrorCallback_value);
+                    wait_us(100000);
+                }
+            }
+            if(HAL_DMA_XferErrorCallback_value){
+                    pc.printf("DMA error interrupt\n\r"); 
+            }  
+            if(debug_dma){
+                pc.printf("dma exit: dma2s1-%i CmpltCB-%i ErrorCB-%i\r\n",DMA2_Stream1_IRQHandler_value,HAL_DMA_XferCpltCallback_value,HAL_DMA_XferErrorCallback_value);
+                errordma = HAL_DMA_GetError (&hd_m2m_dma);
+                state_dma =  HAL_DMA_GetState (&hd_m2m_dma);
+                print_HAL_DMA_error_code(errordma);
+                print_HAL_DMA_status_code(state_dma);
+                pc.printf("\n\r");
+            }
+    
+            //check result
+            for (int j=0;j<buff_size;j++){
+                if(debug_dma && j<10){
+                    pc.printf("%u == %u\r\n",*(DMATestPatternAddresss+j),*(DMAReadAddress+j));
+                }
+                if(*(DMATestPatternAddresss+j)!= *(DMAReadAddress+j)){
+                    nb_error_dma++;  
+                }   
+            }
+            pc.printf("%i/%i error(s) in DMA test %i\r\n",nb_error_dma,buff_size,i);
+            nb_error_dma=0;
+            //wait_us(10000);
+        }
+    }
+        
+    
+    //************************************************
     // init spi
-    //************************  
+    //************************************************
+    pc.printf("**********************************\r\n");
+    pc.printf("Confirguring the SPI...");   
     // Setup the spi for 16 bit data (2x8), high steady state clock,second edge capture, with a 5MHz clock rate
     /*  mode | POL PHA
         -----+--------
@@ -370,30 +566,38 @@
     spi.format(16,0);
     spi.frequency(100000);
     
-    
     //start generating MCLK
     uint32_t half_periode=4;
     //MCLK.prescaler(1);
     //MCLK.period_ticks(9);
     //MCLK.pulsewidth_ticks(half_periode);
     //mclk=1;
+    pc.printf("done\r\n");
+    pc.printf("Testing the SPI...\r\n"); 
+    a=TASK_TEST_SPI();                     
+    if (a==0){
+        pc.printf("Test spi OK :) \r\n");
+    }else{
+        pc.printf("Test spi KO :( \r\n");    
+    }
+
+    
     
     
-    //************************
-    // reset leds
-    //************************
-    led1=0;
-    led2=0;
-    led3=0;
+    //************************************************
+    //reset & init CIS001
+    //************************************************
+    pc.printf("**********************************\r\n");
+    pc.printf("reseting image sensor...");
     
-    //************************
-    //init CIS001
-    //************************
-    TASK_INIT();
+    TASK_INIT_SENSOR();
+    
+    pc.printf("done\r\n");
  
-    //************************
+    //************************************************
     // Ethernet & socket stuff
-    //************************
+    //************************************************
+    pc.printf("**********************************\r\n");
     pc.printf("Connecting Ethernet cable....\r\n");
     eth.connect();
     SocketAddress sockaddr;
@@ -401,13 +605,11 @@
     const char* ip = eth.get_ip_address();
     pc.printf("MAC Address is %s\r\n", mac ? mac : "No Mac");
     pc.printf("IP address is: %s\r\n", ip ? ip : "No IP");
-    pc.printf("*****************\r\n");
-    pc.printf("Nucleo CPU SystemCoreClock is %d MHz\r\n", SystemCoreClock/1000000);
-    pc.printf("*****************\r\n");
+    pc.printf("**********************************\r\n");
     
-    //************************
+    //************************************************
     //open a new socket for printing information & debug
-    //************************
+    //************************************************
     status=cs.open(&eth);
     if(status != 0){
         pc.printf("error open com interface\r\n");
@@ -415,34 +617,21 @@
         return 1;
     }
     else{
-        pc.printf("com interface open\r\n");
+        pc.printf("comunication socket open");
     }
     
     status=cs.connect(ServerIP, portnumber_com);
     if(status !=0){
-        pc.printf("error open com socket\r\n");
+        pc.printf("\r\nerror open com socket\r\n");
         cs.close();
         return 1;
     }else{
-        pc.printf("socket com open\r\n");
+        pc.printf(" and connected\r\n");
     }
     
-    // send 10x test2 to the pc 
-    //sprintf(commandbuffer,"%64s","test2");
-    //pc.printf("[%s]_%d\r\n",commandbuffer,strlen(commandbuffer));
-    //byte_sent=0;
-    //for(i=0; i<10;i++){
-    //    byte_sent+=cs.send(commandbuffer, strlen(commandbuffer));
-    //}
-    //sprintf(commandbuffer,"%64s","******************\nHello from Nucleo :)\n******************\n");
-    //cs.send(commandbuffer, strlen(commandbuffer));
-    
-    
-    
-    
-    //************************
+    //************************************************
     //open a new socket for data and command
-    //************************
+    //************************************************
     status=socket.open(&eth);
     if(status != 0){
         //myprint("*error open interface\r\n");
@@ -450,58 +639,96 @@
         socket.close();
         return 1;
     }else{
-        pc.printf("interface open\r\n");
+        pc.printf("data socket open ");
     }
     
     status=socket.connect(ServerIP, portnumber);
     if(status !=0){
-        pc.printf("failed to connect to server\n\r");
+        pc.printf("\r\nfailed to connect to server\n\r");
         socket.close();
         return 1;
     }else{
-        pc.printf("socket open\r\n");
+        pc.printf(" and connected\r\n");
     }
     
+    //************************************************
+    // set socking as non blocking and set timer.
+    // allows to do something else than waiting for messages
+    //************************************************
+    socket.set_blocking(false); // non blocking 
+    socket.set_timeout(1);       // Timeout after (1)ms
+    pc.printf("**********************************\r\n");
     
     
-    //socket.set_blocking(false); // non blocking 
-    //socket.set_timeout(1);       // Timeout after (10)ms
-  
-
-
-    //************************
+    //************************************************
     // Infinite loop
-    //************************
+    //************************************************
     pc.printf("main loop\r\n");
     while (1){
+                
         led2= !led2;
         loopcount++;
-
-        //************************
+        //************************************************
+        // check if DCMI git an error
+        //************************************************
+        if ((HAL_DCMI_ErrorCallback_value != 0) || (HAL_DCMI_IRQHandler_value != 0) || (DMA1_Stream0_IRQHandler_value==1)){
+            //reset flags
+            HAL_DCMI_ErrorCallback_value=0;
+            HAL_DCMI_IRQHandler_value=0;
+            DMA1_Stream0_IRQHandler_value=0;
+            
+            
+            // deals with the error 
+            print_DCMI_state_code(HAL_DCMI_GetState(&hdcmi));
+            print_DCMI_error_code(HAL_DCMI_GetError(&hdcmi));
+            pc.printf(" %i line %i frame\r\n",line_read,frame_read);
+            pc.printf("%i XferCount\r\n",hdcmi.XferCount);
+            pc.printf("%i XferSize\r\n",hdcmi.XferSize);
+            pc.printf("%i XferTransferNumber\r\n",hdcmi.XferTransferNumber);
+            pc.printf("%x pBuffPtr\r\n",hdcmi.pBuffPtr);
+            pc.printf("%i ErrorCode\r\n",hdcmi.ErrorCode);
+        }
+        
+        //************************************************
+        // check if there is a frame ready to be sent
+        //************************************************
+        if(line_read != 0){
+            pc.printf("%i line read\r\n",line_read);    
+        }
+        if(frame_ready_to_send==1){
+            frame_ready_to_send=0;
+            pc.printf("ready to send frame: %i \r\n",line_read);
+            line_read=line_read;
+            send_frame_ethernet_nohandshake_while();
+            
+        }
+        //************************************************
         // blink led every 1024 loop in the main loop
-        //************************
+        //************************************************
         if(loopcount==1024){    // more or less evey second
             led1=!led1;
             loopcount=0;   
         }
         
-        
         //************************************************
-        // do soemthing when button is pressed
+        // do soemthing when button is pressed (pressed = active low)
         //************************************************
-        if(button_capture==1 && button_pushed==0){
+        if(button_capture==0 && button_pushed==0){
             pc.printf("bc\n\r");
             button_pushed=1;
             
             // do something here
+            CAPTURE=1;
+            wait_us(500);
+            CAPTURE=0;
             
-        }else if(button_capture==0 && button_pushed==1){
+        }else if(button_capture==1 && button_pushed==1){
             button_pushed=0;
         }
         
 
         //************************************************
-        // listening for message (non bloacking)
+        // check if we receiveid a message (non bloacking, timeout=1ms)
         //************************************************
         rcount = socket.recv(&rbuffer, sizeof rbuffer);
         if(rcount>0){
@@ -524,7 +751,7 @@
                     TASK_SPI_WRITE(add,data);
                     
                     sprintf(commandbuffertemp, "done writing %d at %d", data, add);
-                    sprintf(commandbuffer,"%64s",commandbuffertemp);
+                    sprintf(commandbuffer,OUT_MSG_LEN,commandbuffertemp);
                     byte_sent=cs.send(commandbuffer,strlen(commandbuffer));
                     //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent);
                     
@@ -535,38 +762,50 @@
                     readvalue = TASK_SPI_READ(add);
                     
                     sprintf(commandbuffertemp, "%d/%d", readvalue,add);
-                    sprintf(commandbuffer,"%64s",commandbuffertemp);
+                    sprintf(commandbuffer,OUT_MSG_LEN,commandbuffertemp);
                     byte_sent=socket.send(commandbuffer,strlen(commandbuffer));
                     //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent);
                     
                     sprintf(commandbuffertemp, "done reading %d at %d", readvalue, add);
-                    sprintf(commandbuffer,"%64s",commandbuffertemp);
+                    sprintf(commandbuffer,OUT_MSG_LEN,commandbuffertemp);
                     byte_sent=cs.send(commandbuffer,strlen(commandbuffer));
                     //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent);
                     
                 }else if (strcmp (pch,"capture")==0 ||strcmp (pch,"CAPTURE")==0){
+                    // check size of image to be received
                     pch = strtok (NULL, " ");
                     pc.printf("[%s]\n\r",pch);
-                    imSize=atoi(pch);
+                    // PAS OP !
+                    //imSize=atoi(pch);
+                    imSize=frame_bsize_10b;
                     sprintf(commandbuffertemp, "trying to capture image of %d bytes...", imSize);
-                    sprintf(commandbuffer,"%64s",commandbuffertemp);
-                    //byte_sent=cs.send(commandbuffer,strlen(commandbuffer));
+                    sprintf(commandbuffer,OUT_MSG_LEN,commandbuffertemp);
+                    byte_sent=cs.send(commandbuffer,strlen(commandbuffer));
                     
-                    start_capture_10b();
+                    //start DCMI
+                    start_capture();
+                    //start capture
+                    wait_us(100);
+                    CAPTURE=1;
+                    wait_us(100);
+                    CAPTURE=0;
+                    wait_us(100);
+
+                    // if we want to output a dum frame
                     if(DEBUG==1){
                         format_frame(imSize);
                         byte_sent=send_frame_ethernet_nohandshake_while();
-                        sprintf(commandbuffertemp, "sent image of %d bytes/%d bytes", byte_sent,imSize);
-                        sprintf(commandbuffer,"%64s",commandbuffertemp);
+                        //sprintf(commandbuffertemp, "sent image of %d bytes/%d bytes", byte_sent,imSize);
+                        //sprintf(commandbuffer,OUT_MSG_LEN,commandbuffertemp);
                         //byte_sent=cs.send(commandbuffer,strlen(commandbuffer));
                         //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent);
                     }                                   
                     
                 }else if (strcmp (pch,"reset")==0 ||strcmp (pch,"RESET")==0){
-                    TASK_RSTN();  
+                    TASK_RSTN_SENSOR();  
                                 
                     sprintf(commandbuffertemp, "done reseting chip");
-                    sprintf(commandbuffer,"%64s",commandbuffertemp);
+                    sprintf(commandbuffer,OUT_MSG_LEN,commandbuffertemp);
                     byte_sent=cs.send(commandbuffer,strlen(commandbuffer));
                     //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent);
                     
@@ -578,23 +817,23 @@
                     }else{
                         sprintf(commandbuffertemp, "test spi KO");    
                     }
-                    sprintf(commandbuffer,"%64s",commandbuffertemp);
+                    sprintf(commandbuffer,OUT_MSG_LEN,commandbuffertemp);
                     byte_sent=cs.send(commandbuffer,strlen(commandbuffer));
                     
                 }else if (strcmp (pch,"exit")==0 ||strcmp (pch,"EXIT")==0){             
-                    sprintf(commandbuffer, "%64s","close com and data sockets");
+                    sprintf(commandbuffer, OUT_MSG_LEN,"close com and data sockets");
                     byte_sent=cs.send(commandbuffer,strlen(commandbuffer));
                     //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent);
                     status=socket.close();
                     status=cs.close();
     
                 }else{
-                    sprintf(commandbuffer, "%64s","command not recoginzed");
+                    sprintf(commandbuffer, OUT_MSG_LEN,"command not recoginzed");
                     byte_sent=cs.send(commandbuffer,strlen(commandbuffer));
                     //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent);
                 }
             } else{ //tok is null
-                    sprintf(commandbuffer,  "%64s","empty tok");
+                    sprintf(commandbuffer,  OUT_MSG_LEN,"empty tok");
                     byte_sent=cs.send(commandbuffer,strlen(commandbuffer));
                     //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent);    
             }  
@@ -622,29 +861,26 @@
                     byte_sent=cs.send(commandbuffer,strlen(commandbuffer));
                     pc.printf("[%s]%d\n\r",commandbuffer,byte_sent);
                     
+                }else if (strcmp (host_cmd,"pix")==0 || strcmp (host_cmd,"PIX")==0){
+                    sprintf(commandbuffer, "line : %i\n\r frame: %i",line_read,frame_read );
+                    byte_sent=cs.send(commandbuffer,strlen(commandbuffer));
+                    pc.printf("[%s]%d\n\r",commandbuffer,byte_sent);
+                    
+                }else if (strcmp (host_cmd,"print")==0 || strcmp (host_cmd,"PRINT")==0){
+                    print_start_frame();
+                }else{
+                    pc.printf("command not reconginzed\n\r");
                 }
             }
             
-            
         }  //end non blocking timed-out
     
-    
-    
-    
     } //end while(1) loop
     
-    
 } // end main
 
 
 
-
-
-
-
-
-
-
 /***********************************************************************************************
 *
 *       configuration function
@@ -662,11 +898,9 @@
 
     hdcmi.Instance = DCMI;
     hdcmi.Init.SynchroMode        = DCMI_SYNCHRO_HARDWARE;
-    hdcmi.Init.PCKPolarity        = DCMI_PCKPOLARITY_RISING;
-    //hdcmi.Init.PCKPolarity        = DCMI_PCKPOLARITY_FALLING;
-    //the data is not valid in the parallel interface, when VSYNC or HSYNC is at that level (high or low)
-    hdcmi.Init.VSPolarity         = DCMI_VSPOLARITY_LOW;
-    hdcmi.Init.HSPolarity         = DCMI_HSPOLARITY_LOW;
+    hdcmi.Init.PCKPolarity        = DCMI_PCKPOLARITY_FALLING;
+    hdcmi.Init.VSPolarity         = DCMI_VSPOLARITY_HIGH;
+    hdcmi.Init.HSPolarity         = DCMI_HSPOLARITY_HIGH;
     
     hdcmi.Init.CaptureRate        = DCMI_CR_ALL_FRAME;
     hdcmi.Init.ExtendedDataMode   = DCMI_EXTEND_DATA_8B;
@@ -677,17 +911,15 @@
     hdcmi.Init.LineSelectStart    = DCMI_OELS_ODD;
     
     if (HAL_DCMI_Init(&hdcmi) != HAL_OK)
-    {
+  {
     Error_Handler();
-    }
+  }
   
   
+  /* USER CODE BEGIN DCMI_Init 2 */
     //check status
-    /*HAL_DCMI_StateTypeDef status;
-    status=HAL_DCMI_GetState(&hdcmi);
-    if(status != HAL_OK){
-        pc.printf("DCMI_init: %i\n\r",status);
-    }*/
+    print_DCMI_state_code(HAL_DCMI_GetState(&hdcmi));
+  /* USER CODE END DCMI_Init 2 */
 
 }
 
@@ -700,32 +932,40 @@
 static void MX_DCMI_Init_10b(void)
 {
   
+    /* in CIS doc:
+    PCLK: pixel clock. Values of the pixels are changed on posedge of this clock and must be sampled on the negedge.
+    VSYNC: This signal goes up at the beginning of the frame, stays up during all transmission and goes down at the end of the frame.
+    HSYNC: This signal goes up at the beginning of a row of pixel and goes down at the end of the row.
+    */
+  
+  
   hdcmi.Instance = DCMI;
-  hdcmi.Init.SynchroMode = DCMI_SYNCHRO_HARDWARE;
-  hdcmi.Init.PCKPolarity = DCMI_PCKPOLARITY_FALLING;
-  hdcmi.Init.VSPolarity = DCMI_VSPOLARITY_LOW;
-  hdcmi.Init.HSPolarity = DCMI_HSPOLARITY_LOW;
-  hdcmi.Init.CaptureRate = DCMI_CR_ALL_FRAME;
-  hdcmi.Init.ExtendedDataMode = DCMI_EXTEND_DATA_10B;
-  hdcmi.Init.JPEGMode = DCMI_JPEG_DISABLE;
-  hdcmi.Init.ByteSelectMode = DCMI_BSM_ALL;
-  hdcmi.Init.ByteSelectStart = DCMI_OEBS_ODD;
-  hdcmi.Init.LineSelectMode = DCMI_LSM_ALL;
-  hdcmi.Init.LineSelectStart = DCMI_OELS_ODD;
+  hdcmi.Init.SynchroMode    = DCMI_SYNCHRO_HARDWARE;
+  hdcmi.Init.PCKPolarity    = DCMI_PCKPOLARITY_FALLING;    //  Values of the pixels are changed on posedge of this clock and must be sampled on the negedge.
+  hdcmi.Init.VSPolarity     = DCMI_VSPOLARITY_HIGH;         // Vertical synchronization active High (active low in mdcam & camel)
+  hdcmi.Init.HSPolarity     = DCMI_HSPOLARITY_HIGH;         // Horizontal synchronization active High (active low in mdcam & camel)
+  
+  hdcmi.Init.CaptureRate            = DCMI_CR_ALL_FRAME;
+  hdcmi.Init.ExtendedDataMode       = DCMI_EXTEND_DATA_10B;
+  hdcmi.Init.JPEGMode               = DCMI_JPEG_DISABLE;
+  hdcmi.Init.ByteSelectMode         = DCMI_BSM_ALL;
+  hdcmi.Init.ByteSelectStart        = DCMI_OEBS_ODD;
+  hdcmi.Init.LineSelectMode         = DCMI_LSM_ALL;
+  hdcmi.Init.LineSelectStart        = DCMI_OELS_ODD;
   
   if (HAL_DCMI_Init(&hdcmi) != HAL_OK)
   {
     Error_Handler();
   }
   
-  
-  /* USER CODE BEGIN DCMI_Init 2 */
+    /* USER CODE BEGIN DCMI_Init 2 */
     //check status
-    /*HAL_DCMI_StateTypeDef status;
-    status=HAL_DCMI_GetState(&hdcmi);
-    if(status != HAL_OK){
-        pc.printf("DCMI_init: %i\n\r",status);
-    }*/
+    pc.printf("%i XferCount\r\n",hdcmi.XferCount);
+    pc.printf("%i XferSize\r\n",hdcmi.XferSize);
+    pc.printf("%i XferTransferNumber\r\n",hdcmi.XferTransferNumber);
+    pc.printf("%i pBuffPtr\r\n",hdcmi.pBuffPtr);
+    pc.printf("%i ErrorCode\r\n",hdcmi.ErrorCode);
+    print_DCMI_state_code(HAL_DCMI_GetState(&hdcmi));
   /* USER CODE END DCMI_Init 2 */
 
 }
@@ -733,7 +973,6 @@
 
 
 
-
 /**
   * @brief Enable DMA controller clock
   * @param None
@@ -741,14 +980,51 @@
   */
 static void MX_DMA_Init(void) 
 {
-
-  /* DMA controller clock enable */
-  __HAL_RCC_DMA1_CLK_ENABLE();
+  
+    /* DMA controller clock enable */
+    __HAL_RCC_DMA1_CLK_ENABLE();
+    __HAL_RCC_DMA2_CLK_ENABLE();
+    
+    if(TEST_DMA==1){
+        /* Configure DMA request hd_m2m_dma on DMA2_Stream1 */
+        hd_m2m_dma.Init.Request               = DMA_REQUEST_MEM2MEM;
+        hd_m2m_dma.Init.Direction             = DMA_MEMORY_TO_MEMORY;
+        hd_m2m_dma.Init.PeriphInc             = DMA_PINC_ENABLE;
+        hd_m2m_dma.Init.MemInc                = DMA_MINC_ENABLE;
+        hd_m2m_dma.Init.PeriphDataAlignment   = DMA_PDATAALIGN_WORD;
+        hd_m2m_dma.Init.MemDataAlignment      = DMA_MDATAALIGN_WORD;
+        hd_m2m_dma.Init.Mode                  = DMA_NORMAL;
+        hd_m2m_dma.Init.Priority              = DMA_PRIORITY_HIGH;
+        hd_m2m_dma.Init.FIFOMode              = DMA_FIFOMODE_ENABLE;
+        hd_m2m_dma.Init.FIFOThreshold         = DMA_FIFO_THRESHOLD_FULL;
+        hd_m2m_dma.Init.MemBurst              = DMA_MBURST_INC4;
+        hd_m2m_dma.Init.PeriphBurst           = DMA_MBURST_INC4;
+        hd_m2m_dma.Instance = DMA2_Stream1;
+        if (HAL_DMA_Init(&hd_m2m_dma) != HAL_OK) {
+        Error_Handler( );
+        }
+      
+        //thom : register callback
+        HAL_DMA_RegisterCallback (&hd_m2m_dma,HAL_DMA_XFER_CPLT_CB_ID, HAL_DMA_XferCpltCallback);
+        HAL_DMA_RegisterCallback (&hd_m2m_dma,HAL_DMA_XFER_ERROR_CB_ID, HAL_DMA_XferErrorCallback);
+    }
 
-  /* DMA interrupt init */
-  /* DMA1_Stream0_IRQn interrupt configuration */
-  HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0);
-  HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
+
+    /* DMA interrupt init */
+    
+    //DCMI
+    /* DMA1_Stream0_IRQn interrupt configuration */
+    HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0x00, 0);
+    HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);  
+  
+    
+    if(TEST_DMA==1){
+        //MEM2MEM DMA
+        /* NVIC configuration for DMA transfer complete interrupt*/
+        HAL_NVIC_SetPriority(DMA2_Stream1_IRQn, 0x0F, 0);
+        HAL_NVIC_EnableIRQ(DMA2_Stream1_IRQn); 
+    }
+  
 
 }
 
@@ -776,7 +1052,6 @@
 
 
 
-
 /**
   * @brief  This function is executed in case of error occurrence.
   * @retval None