Test Code for OV7670 Camera module. The images are sent over ethernet link.

Dependencies:   EthernetInterface mbed-rtos mbed ov7670

You can find more information in this page: https://mbed.org/users/edodm85/notebook/ov7670-camera-and-ethernet-stream/

Files at this revision

API Documentation at this revision

Comitter:
edodm85
Date:
Sat Apr 05 14:02:07 2014 +0000
Parent:
2:d6b38a4512eb
Commit message:
Updated for "OV7670 Grabber v2.0"

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
ov7670.lib Show annotated file Show diff for this revision Revisions of this file
diff -r d6b38a4512eb -r df8de18d7fa9 main.cpp
--- a/main.cpp	Sat Feb 15 15:17:08 2014 +0000
+++ b/main.cpp	Sat Apr 05 14:02:07 2014 +0000
@@ -1,7 +1,7 @@
 /*
  * Author: Edoardo De Marchi
- * Date: 20/07/13
- * Notes: OV7670 camera with Ethernet Connection
+ * Date: 05/04/14
+ * Notes: This firmware required "OV7670 Grabber v2.0"
 */
 
 #include "main.h"
@@ -10,9 +10,6 @@
 #define QVGA    76800          //320*240
 #define QQVGA   19200          //160*120
 
-static char format = ' ';
-static int resolution = 0;
-
 
 int Init()
 {
@@ -26,41 +23,29 @@
     eth.init(ip, mask, gateway);
     //DHCP
     //eth.init();
+    
     eth.connect();
     server.bind(ECHO_SERVER_PORT);
     server.listen(1);
+    
     printf("IP Address is %s\r\n", eth.getIPAddress());
     
     //THREAD 
-    osThreadCreate(osThread(net_thread), NULL); 
-       
+    osThreadCreate(osThread(Net_Thread), NULL);
+    tencid = osThreadCreate(osThread(Grab_Thread), NULL);  
+    
     return 0;
 }
 
 
-
-int main (void) 
-{
-    Init();
-    t.start();
-    
-    while (true)
-    {
-        //mbed is alive?
-        led2 = !led2;
-        osDelay(500);
-    }     
-}
-
-
-void net_thread(void const *argument)
+void Net_Thread(void const *argument)
 {
     while (true) 
     {
         led3 = 1;
-        //printf("\r\nWait for new connection...\r\n");
         server.accept(client);
         printf("Connection from: %s\r\n", client.get_address());
+        
         while (true) 
         {
             led3 = 0;
@@ -68,10 +53,9 @@
             if (n <= 0) break;
             
             bufferRX[n]=0; // make terminater
-            parse_cmd();
+            parse_cmd(n);
         }
         client.close();
-        //printf("Connection close.\r\n");
     } 
 } 
 
@@ -83,83 +67,204 @@
 }
 
 
-void parse_cmd(){
+void Grab_Thread(void const *argument)
+{
+    while(true)
+    {    
+        osSignalWait(0x1, osWaitForever);
+                    
+        while(bSnap_on || bGrab_on)
+        {
+            CameraSnap();
+            bSnap_on = false;
+        }
+    }
+}
+
+
+void CameraSnap()
+{
+        led4 = 1;
+        int p = 0;  
+
+        camera.CaptureNext();
+        while(camera.CaptureDone() == false);      
+                                 
+        camera.ReadStart();                             // Start reading in the image data from the camera hardware buffer  
+        
+        t1 = t.read_ms(); 
+                  
+        client.send_all(StartCondition, sizeof(StartCondition));
+                    
+        if(strcmp("BAW", CMDCamera.format) == 0 || strcmp("RAW", CMDCamera.format) == 0)                   // B&W  
+        {  
+            for(int x = 0; x<CMDCamera.resolution/sizeof(bufferTX); x++)
+            {
+                for(int q = 0; q<sizeof(bufferTX); q++)
+                {               
+                   if(CMDCamera.resolution != VGA)
+                        camera.ReadOnebyte();                   // Read in the first half of the image
+                   
+                   bufferTX[q] = camera.ReadOnebyte();          // Y only      // Read in the Second half of the image                                  
+                }  
+                Send();       
+            } 
+        }     
+        
+        if(strcmp("YUV", CMDCamera.format) == 0 || strcmp("RGB", CMDCamera.format) == 0)                    // Color
+        {
+            for(int x = 0; x<(CMDCamera.resolution/sizeof(bufferTX))*2; x++)
+            {
+                for(int q = 0; q<sizeof(bufferTX)/2; q++)
+                {
+                       p = q*2;                               
+                       bufferTX[p] = camera.ReadOnebyte();          // Read in the first half of the image                       
+                       bufferTX[p+1] = camera.ReadOnebyte();        // Read in the Second half of the image                      
+                }  
+                Send();       
+            }         
+        }       
+           
+        camera.ReadStop();               
+        t2 = t.read_ms(); 
+                                      
+        led4 = 0;
+}
+
+
+int main (void) 
+{
+    led3 = 0;
+    int iRet=Init();
+
+    if (iRet<0)
+    {
+        printf("Init Error!");
+        return iRet;
+    }
+    t.start();
+    
+    while (true)
+    {
+        //mbed is alive?
+        led2 = !led2;
+        osDelay(500);
+    }     
+}
+
+
+void parse_cmd(int sizeCMD)
+{
             new_send = false;
+            //printf("bufferRX: %s\r\n", bufferRX);
             
             if(strcmp("snap", bufferRX) == 0)              
             {
-                    CameraSnap();
+                    bSnap_on = true;
+                    osSignalSet(tencid, 0x1);
                     memset(bufferRX, 0, sizeof(bufferRX));      
             }else
-            if(strcmp("init_bw_VGA", bufferRX) == 0)                  // Set up for 640*480 pixels RAW     
-            {
-                    format = 'b';
-                    resolution = VGA;
-                    if(camera.Init('b', VGA) != 1)
-                    {
-                      printf("Init Fail\r\n");
-                    }
-                    memset(bufferRX, 0, sizeof(bufferRX));
-            }else  
-            if(strcmp("init_yuv_QVGA", bufferRX) == 0)                  // Set up for 320*240 pixels YUV422   
+            if(strncmp("grab", bufferRX, 4) == 0)               // grab start       grab stop  
             {
-                    format = 'y';
-                    resolution = QVGA;
-                    if(camera.Init('b', QVGA) != 1)
+                    if(strncmp("start", &bufferRX[5], 5) == 0)
+                    {
+                        bGrab_on = true;
+                        osSignalSet(tencid, 0x1);
+                        memset(bufferRX, 0, sizeof(bufferRX));
+                    }else
                     {
-                      printf("Init Fail\r\n");
-                    }
-                    memset(bufferRX, 0, sizeof(bufferRX));
-            }else  
-            if(strcmp("init_rgb_QVGA", bufferRX) == 0)                  // Set up for 320*240 pixels RGB565   
+                        bGrab_on = false;
+                        memset(bufferRX, 0, sizeof(bufferRX));
+                    }      
+            }else
+            if(strncmp("init", bufferRX, 4) == 0)               // init RGB 19200
             {
-                    format = 'r';
-                    resolution = QVGA;
-                    if(camera.Init('r', QVGA) != 1)
+                    char word[10];
+                    strncpy(CMDCamera.format, &bufferRX[5], 3);
+                    strncpy(word, &bufferRX[9], sizeCMD-9);                  
+                    CMDCamera.resolution = atoi(word);
+                    
+                    printf("CMDCamera.format: %s\r\n", CMDCamera.format);
+                    printf("CMDCamera.resolution: %d\r\n", CMDCamera.resolution); 
+
+                    if(camera.Init(CMDCamera.format, CMDCamera.resolution) != 1)
                     {
                       printf("Init Fail\r\n");
                     }
+                    
+                    sprintf(bufferTX, "OV Init OK\r\n");
+                    client.send_all(bufferTX, sizeof(bufferTX));
                     memset(bufferRX, 0, sizeof(bufferRX));
             }else 
-            if(strcmp("init_bw_QVGA", bufferRX) == 0)                  // Set up for 320*240 pixels YUV (Only Y)         
-            {
-                    format = 'b';
-                    resolution = QVGA;
-                    if(camera.Init('b', QVGA) != 1)
+            if(strncmp("reg", bufferRX, 3) == 0)                // reg r 018       reg w 018 004        reg 
+            {        
+                    if(!bGrab_on)
                     {
-                      printf("Init Fail\r\n");
-                    }
-                    memset(bufferRX, 0, sizeof(bufferRX));
-            }else  
-            if(strcmp("init_yuv_QQVGA", bufferRX) == 0)                 // Set up for 160*120 pixels YUV422
-            {                            
-                    format = 'y';
-                    resolution = QQVGA;
-                    if(camera.Init('b', QQVGA) != 1)
-                    {
-                      printf("Init Fail\r\n");
+                        strncpy(&Reg.type, &bufferRX[4], 1);
+                        char word[3];                    
+                    
+        
+                        if(Reg.type == 'r')
+                        {
+                            strncpy(word, &bufferRX[6], 3);
+                            Reg.addr = atoi(word);
+                            memset(word, 0, sizeof(word));
+                            sprintf(bufferTX, "reg: %x", camera.ReadReg(Reg.addr));  
+                        }else
+                        {
+                            strncpy(word, &bufferRX[6], 3);
+                            Reg.addr = atoi(word);
+                            strncpy(word, &bufferRX[10], 3);
+                            Reg.value = atoi(word);
+                            memset(word, 0, sizeof(word));
+                            camera.WriteReg(Reg.addr, Reg.value);
+                            sprintf(bufferTX, "%x", camera.ReadReg(Reg.addr));
+                        }
+                        if(sizeCMD == 3)
+                        {
+                            int n = 0;
+                            int g = 0;
+                            int c = 650;
+                            sprintf(bufferTX, "reg_tab: ");
+                            for (int i=0;i<201;i++) 
+                            {
+                                n = snprintf(9+bufferTX+g, c, "%02X", camera.ReadReg(i));
+                                g = g + n; 
+                                c = c - n - 1;                                          
+                            }                  
+                            client.send_all(bufferTX, sizeof(bufferTX));                      
+                        }
+                  
+                        client.send_all(bufferTX, sizeof(bufferTX));
                     }
-                    memset(bufferRX, 0, sizeof(bufferRX));
-            }else   
-            if(strcmp("init_rgb_QQVGA", bufferRX) == 0)                 // Set up for 160*120 pixels RGB565
-            {                            
-                    format = 'r';
-                    resolution = QQVGA;
-                    if(camera.Init('r', QQVGA) != 1)
+            }else
+            if(strncmp("w_bit", bufferRX, 5) == 0)                  //  w_bit0 018 001         w_bit1 018 001 
+            {        
+                    int type;
+                    int reg_addr = 0;                
+                    int reg_value = 0;
+                    char word[3];
+                    strncpy(word, &bufferRX[5], 1); 
+                    type = atoi(word);
+                    strncpy(word, &bufferRX[7], 3);  
+                    reg_addr = atoi(word);
+                    memset(word, 0, sizeof(word));
+                    strncpy(word, &bufferRX[11], 3); 
+                    reg_value = atoi(word); 
+                    //printf("reg read write: addr: %x - value %x\r\n", reg_addr, reg_value);
+                    int reg_value_now = camera.ReadReg(reg_addr);
+                    
+                    if(type == 0)
                     {
-                      printf("Init Fail\r\n");
+                        reg_value = reg_value_now ^ reg_value; 
+                    }else
+                    {
+                        reg_value = reg_value_now | reg_value;
                     }
-                    memset(bufferRX, 0, sizeof(bufferRX));
-            }else
-            if(strcmp("init_bw_QQVGA", bufferRX) == 0)                 // Set up for 160*120 pixels YUV (Only Y)
-            {                        
-                    format = 'b';
-                    resolution = QQVGA;
-                    if(camera.Init('b', QQVGA) != 1)
-                    {
-                      printf("Init Fail\r\n");
-                    }
-                    memset(bufferRX, 0, sizeof(bufferRX));
+                    //printf("reg value new: reg_value_now: %x - reg_value %x\r\n", reg_value_now, reg_value);                   
+                    camera.WriteReg(reg_addr, reg_value);
+                    memset(word, 0, sizeof(word));
+                    memset(bufferTX, 0, sizeof(bufferRX));
             }else
             if(strcmp("reset", bufferRX) == 0)              
             {
@@ -167,79 +272,15 @@
             }else
             if(strcmp("time", bufferRX) == 0)              
             {
-                    sprintf(bufferTX, "Tot time acq + send (mbed): %dms\r\n", t2-t1);
+                    sprintf(bufferTX, "OV Tot time acq + send (mbed): %dms\r\n", t2-t1);                  
                     client.send_all(bufferTX, sizeof(bufferTX));
                     memset(bufferTX, 0, sizeof(bufferTX));
-            }else
-            if(strcmp("reg", bufferRX) == 0)              
+            }else   
+            if(strcmp("ciao", bufferRX) == 0)              
             {
-                    int n = 0;
-                    int g = 0;
-                    int c = 650;
-                    for (int i=0;i<201;i++) 
-                    {
-                        n = snprintf(bufferTX+g, c, "%02X", camera.ReadReg(i));
-                        g = g + n; 
-                        c = c - n - 1;                                          
-                    }                    
-                    client.send_all(bufferTX, sizeof(bufferTX));                       
-                    memset(bufferTX, 0, sizeof(bufferTX));     
-            }    
+                    sprintf(bufferTX, "OV Test OK\r\n");                   
+                    client.send_all(bufferTX, sizeof(bufferTX));
+                    memset(bufferTX, 0, sizeof(bufferTX));                         
+            }            
             memset(bufferRX, 0, sizeof(bufferRX));           
 }
-
-
-void CameraSnap(){
-        led4 = 1;
-        t1 = t.read_ms();
-        
-                // Kick things off by capturing an image
-        camera.CaptureNext();
-        while(camera.CaptureDone() == false);      
-                // Start reading in the image data from the camera hardware buffer                   
-        camera.ReadStart();  
-
-     
-        int p = 0;         
-        if(format == 'b')                       // B&W  
-        {  
-            for(int x = 0; x<resolution/sizeof(bufferTX); x++)
-            {
-                for(int q = 0; q<sizeof(bufferTX); q++)
-                { 
-                       // Read in the first half of the image
-                       if(resolution != VGA)
-                            camera.ReadOnebyte();  
-                       // Read in the Second half of the image
-                       bufferTX[q] = camera.ReadOnebyte();      // Y only                                        
-                }  
-                Send();       
-            } 
-        }     
-        
-        if(format == 'y' || format == 'r')          // Color
-        {
-            for(int x = 0; x<(resolution/sizeof(bufferTX))*2; x++)
-            {
-                for(int q = 0; q<sizeof(bufferTX)/2; q++)
-                {
-                       p = q*2;         
-                       // Read in the first half of the image
-                       bufferTX[p] = camera.ReadOnebyte();   
-                       // Read in the Second half of the image
-                       bufferTX[p+1] = camera.ReadOnebyte();                           
-                }  
-                Send();       
-            }         
-        }   
-        camera.ReadStop();               
-        t2 = t.read_ms(); 
-                        
-                 // Immediately request the next image to be captured (takes around 45ms)
-        camera.CaptureNext();                             
-                // Now wait for the image to finish being captured
-        while (camera.CaptureDone() == false);
-        
-        //printf("Snap_done\r\n");   
-        led4 = 0;
-}
\ No newline at end of file
diff -r d6b38a4512eb -r df8de18d7fa9 main.h
--- a/main.h	Sat Feb 15 15:17:08 2014 +0000
+++ b/main.h	Sat Apr 05 14:02:07 2014 +0000
@@ -15,15 +15,17 @@
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
 
+
 //Camera
 OV7670 camera
 (
-    p28,p27,            // SDA,SCL(I2C / SCCB)
-    p23,p24,p25,        // VSYNC,HREF,WEN(FIFO)  
-    Port0,0x07878000,   // PortIn data        p18(P0.26),p17(P0.25),p16(P0.24),p15(P0.23),p11(P0.18),p12(P0.17),p14(P0.16),p13(P0.15)
-    p26,p29,p30         // RRST,OE,RCLK
+    p28,p27,                // SDA,SCL(I2C / SCCB)
+    p23,NC,p25,             // VSYNC,HREF,WEN(FIFO)  
+    Port0,0x07878000,       // PortIn data        p18(P0.26),p17(P0.25),p16(P0.24),p15(P0.23),p11(P0.18),p12(P0.17),p14(P0.16),p13(P0.15)
+    p26,p29,p30             // RRST,OE,RCLK
 ); 
 
+
 //ETHERNET
 char* ip = "192.168.153.153";             // ip
 char* mask = "255.255.255.0";           // mask
@@ -32,22 +34,50 @@
 TCPSocketConnection client;
 TCPSocketServer server; 
 
+
 //ETHERNET BUFFER
 char bufferRX[20];
 char bufferTX[4800];
 
+char *StartCondition = "sRt";        // START
+bool bGrab_on = false;
+bool bSnap_on = false;
+
+
 //RESET
 extern "C" void mbed_reset();
 
+
 //TIMER
 int t1 = 0; 
 int t2 = 0;
 
+
 //THREAD
-void net_thread(void const *argument);
-osThreadDef(net_thread, osPriorityNormal, DEFAULT_STACK_SIZE); 
+void Net_Thread(void const *argument);
+void Grab_Thread(void const *argument);
+osThreadId tencid;
+osThreadDef(Net_Thread, osPriorityNormal, DEFAULT_STACK_SIZE); 
+osThreadDef(Grab_Thread, osPriorityNormal, DEFAULT_STACK_SIZE);
+
 
 //FUNCTION
 void Send();
-void parse_cmd();
+void parse_cmd(int sizeCMD);
 void CameraSnap();
+void CameraGrab();
+
+
+struct CameraSet
+{
+    char format[3];
+    int resolution;
+}CMDCamera;
+
+
+struct Register
+{
+    char type;
+    int addr;
+    int value;
+}Reg;
diff -r d6b38a4512eb -r df8de18d7fa9 ov7670.lib
--- a/ov7670.lib	Sat Feb 15 15:17:08 2014 +0000
+++ b/ov7670.lib	Sat Apr 05 14:02:07 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/edodm85/code/ov7670/#b40a945dd672
+http://mbed.org/users/edodm85/code/ov7670/#119b4c04a4ed