Port of TI's CC3100 Websock camera demo. Using FreeRTOS, mbedTLS, also parts of Arducam for cams ov5642 and 0v2640. Can also use MT9D111. Work in progress. Be warned some parts maybe a bit flacky. This is for Seeed Arch max only, for an M3, see the demo for CM3 using the 0v5642 aducam mini.

Dependencies:   mbed

Revision:
12:7d75ba5deed1
Parent:
11:404bd8bb7fa6
Child:
13:bf45f592c2b6
--- a/camera_app/camera_app.cpp	Tue Jul 07 22:56:31 2015 +0000
+++ b/camera_app/camera_app.cpp	Sun Jul 12 09:13:56 2015 +0000
@@ -79,7 +79,6 @@
 extern int g_uiIpObtained = 0;
 extern int g_uiSimplelinkRole = ROLE_INVALID;
 unsigned int g_uiIpAddress = 0;
-static unsigned int *p_buffer = NULL;
 static unsigned char g_dma_txn_done;
 volatile static unsigned char g_frame_end;
 static unsigned int g_total_dma_intrpts;
@@ -94,16 +93,13 @@
     int FRAME_SIZE_IN_BYTES = (240 * 256 * 2);
 #endif    
 
-//#ifdef ENABLE_JPEG
-unsigned int g_header_length;
-//#endif 
-
 struct ImageBuffer
 {
-//#ifdef ENABLE_JPEG
+#ifdef ENABLE_JPEG
 	char g_header[SMTP_BUF_LEN] /*= {'\0'}*/;
-//#endif
-	uint32_t g_image_buffer[NUM_OF_4B_CHUNKS];//60Kb 15360, 50Kb 12800
+#endif
+//    uint32_t g_image_buffer[NUM_OF_4B_CHUNKS];//60Kb 15360, 50Kb 12800
+	uint32_t g_image_buffer[((IMAGE_BUF_SIZE)/(sizeof(unsigned int)))];//51Kb
 };
 
 ImageBuffer g_image;
@@ -326,14 +322,16 @@
 //!                               
 //
 //*****************************************************************************
-static unsigned short CaptureImage(char** WriteBuffer)
+uint16_t CaptureImage(char** WriteBuffer)
 {
+//    Uart_Write((uint8_t*)"CaptureImage \n\r");
 	DCMI_HandleTypeDef phdcmi;
     DMA_HandleTypeDef hdma_dcmi;
-//    Uart_Write((uint8_t*)"CaptureImage \n\r");
-    memset(g_image.g_image_buffer,0xF80F,sizeof(g_image.g_image_buffer));
-//    memset(g_image.g_image_buffer,0x0F,sizeof(g_image.g_image_buffer));
-    p_buffer = &(g_image.g_image_buffer[0]);
+
+    uint32_t g_header_length = 0;
+//    memset(g_image.g_image_buffer,0xF80F,sizeof(g_image.g_image_buffer));
+    memset(g_image.g_image_buffer,0x0F,sizeof(g_image.g_image_buffer));
+    uint32_t *p_buffer = &(g_image.g_image_buffer[0]);
     
     DMAConfig();
 
@@ -341,18 +339,18 @@
     // Perform Image Capture 
     //
 //    wait_ms(500);
+
 #ifdef ENABLE_JPEG
     HAL_DCMI_Start_DMA(&phdcmi, DCMI_MODE_SNAPSHOT, (uint32_t)p_buffer, NUM_OF_4B_CHUNKS);
 #else
     HAL_DCMI_Start_DMA(&phdcmi, DCMI_MODE_CONTINUOUS, (uint32_t)p_buffer, NUM_OF_4B_CHUNKS);
-#endif
-    HttpDebug("CaptureImage Complete\n\r");    
+#endif   
     g_frame_end = 1;
  
-    /* Read the number of data items transferred (4 x uint = 4 bytes) */
+    /* Read the number of data items transferred in bytes ((4x) uint = 4 bytes) */
     g_frame_size_in_bytes = 4*(NUM_OF_4B_CHUNKS - hdma_dcmi.Instance->NDTR);
-    if(g_frame_size_in_bytes <= 0){
-    	HttpDebug("\r\nFailed to capture any data, check camera connections!\r\n");
+    if(g_frame_size_in_bytes <= 0 || g_frame_size_in_bytes > (NUM_OF_4B_CHUNKS *4)){
+    	HttpDebug("\r\nFailed to capture or data over-run, check camera connections!\r\n");
     	HAL_DCMI_MspDeInit(&phdcmi);
     	while(1){
     		wait(0.5);
@@ -361,14 +359,14 @@
     g_dma_txn_done = 1;
     g_total_dma_intrpts = 1;
     
-    HttpDebug("g_frame_size_in_bytes = 0x%x \r\n",g_frame_size_in_bytes);
+//    HttpDebug("frame size = 0x%x \r\n",g_frame_size_in_bytes);
     
     uint8_t* Image = reinterpret_cast<uint8_t*>(&g_image.g_image_buffer);
-//#if 0    
+#if 0    
     for(int i =0;i< 10;i++){
     	HttpDebug("0x%x ",Image[i]);
     }
-//#endif//if 0    
+#endif//if 0    
     //
     // Create JPEG Header
     //
@@ -376,10 +374,12 @@
     memset(g_image.g_header, '\0', sizeof(g_image.g_header));
     g_header_length = CreateJpegHeader((char *)&(g_image.g_header[0]), PIXELS_IN_X_AXIS,
                                        PIXELS_IN_Y_AXIS, 0, 0x0020, 9);
-
+//    HttpDebug("g_header_length = 0x%x \r\n",g_header_length);
     // This pushes the header to the start of the array so that the entire picture can be contiguous in memory
-    memcpy(Image + g_header_length, Image, g_frame_size_in_bytes);
-    memcpy(Image, g_image.g_header, g_header_length);
+//    memcpy(Image + g_header_length, Image, g_frame_size_in_bytes);
+//    memcpy(Image, g_image.g_header, g_header_length);
+    // This pushes the header to the end of the array so that the entire picture can be contiguous in memory
+    memcpy(Image + g_frame_size_in_bytes, g_image.g_header, g_header_length);
 #if 0    
     for(int i =624;i< 725;i++){
     	HttpDebug("0x%x ",Image[i]);
@@ -401,7 +401,7 @@
 //!     \return                     None                  
 //
 //*****************************************************************************
-static void DMAConfig()
+void DMAConfig()
 {
 	DCMI_HandleTypeDef phdcmi;
     DMA_HandleTypeDef hdma_dcmi;
@@ -415,8 +415,8 @@
     hdma_dcmi.Init.Direction           = DMA_PERIPH_TO_MEMORY;
     hdma_dcmi.Init.PeriphInc           = DMA_PINC_DISABLE;
     hdma_dcmi.Init.MemInc              = DMA_MINC_ENABLE;
-    hdma_dcmi.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;//Cam is 1 byte wide data (8 bits) should this be word?????
-    hdma_dcmi.Init.MemDataAlignment    = DMA_PDATAALIGN_WORD;//Memory is defined as uint (1 word)
+    hdma_dcmi.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;//Cam is 1 byte wide data (8 bits) should this be word?????
+    hdma_dcmi.Init.MemDataAlignment    = DMA_PDATAALIGN_WORD;//Memory has been defined as uint (1 word)
     hdma_dcmi.Init.Mode                = DMA_CIRCULAR;
     hdma_dcmi.Init.Priority            = DMA_PRIORITY_HIGH;
     hdma_dcmi.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;
@@ -428,7 +428,7 @@
     __HAL_LINKDMA(&phdcmi, DMA_Handle, hdma_dcmi);
     
      /* DMA interrupt init */
-    HAL_NVIC_SetPriority(DMA2_Stream1_IRQn, 5, 0);
+    HAL_NVIC_SetPriority(DMA2_Stream1_IRQn, 0, 0);
     HAL_NVIC_EnableIRQ(DMA2_Stream1_IRQn);
     
     /* Peripheral DCMI init*/
@@ -437,9 +437,12 @@
     __DCMI_CLK_ENABLE();    
  
     phdcmi.Init.SynchroMode      = DCMI_SYNCHRO_HARDWARE;
-    phdcmi.Init.PCKPolarity      = DCMI_PCKPOLARITY_RISING;
-    phdcmi.Init.VSPolarity       = DCMI_VSPOLARITY_LOW;
-    phdcmi.Init.HSPolarity       = DCMI_HSPOLARITY_LOW;
+    phdcmi.Init.PCKPolarity      = DCMI_PCKPOLARITY_FALLING;
+//    phdcmi.Init.PCKPolarity      = DCMI_PCKPOLARITY_RISING;
+    phdcmi.Init.VSPolarity       = DCMI_VSPOLARITY_HIGH;
+//    phdcmi.Init.VSPolarity       = DCMI_VSPOLARITY_LOW;
+    phdcmi.Init.HSPolarity       = DCMI_HSPOLARITY_HIGH;
+//    phdcmi.Init.HSPolarity       = DCMI_HSPOLARITY_LOW;
     phdcmi.Init.CaptureRate      = DCMI_CR_ALL_FRAME;
     phdcmi.Init.ExtendedDataMode = DCMI_EXTEND_DATA_8B;    
 #ifdef ENABLE_JPEG  
@@ -474,7 +477,7 @@
     PA6     ------> DCMI_PIXCK
     PA4     ------> DCMI_HSYNC
     PB7     ------> DCMI_VSYNC
-    PC9     ------> MCLK 
+    
     */
     /* D3 D4 D6 D7 */
     GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;
@@ -500,22 +503,22 @@
     GPIO_InitStruct.Alternate = GPIO_AF13_DCMI;
     HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
     
-    /*Configure GPIO pin : GPIO_AF0_MCO PC9 */
+    /*Configure GPIO pin : GPIO_AF0_MCO PC9 
     GPIO_InitStruct.Pin = GPIO_PIN_9;
     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
     GPIO_InitStruct.Pull = GPIO_PULLUP;
     GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
     GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
     HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
-    
-    /*Configure GPIO pin : PA8 */
+*/    
+    /*Configure GPIO pin : GPIO_AF0_MCO PA8 
     GPIO_InitStruct.Pin = GPIO_PIN_8;
     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
     GPIO_InitStruct.Pull = GPIO_PULLUP;
     GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
     GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-    
+*/    
     /* D5 VSYNC */
     GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
@@ -525,7 +528,7 @@
     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
   /* System interrupt init*/
-    HAL_NVIC_SetPriority(DCMI_IRQn, 5, 0);
+    HAL_NVIC_SetPriority(DCMI_IRQn, 0, 0);
     HAL_NVIC_EnableIRQ(DCMI_IRQn);
 
     HAL_DMA_Init(&hdma_dcmi);