Quellcode

  1. Source-code for STM32F407VG as a keil project
  2. Source-code for Workstation as VS2013 project

 

Selected excerpts from STM32F4 code:

#include "stm32f4xx.h"
#include "stm32f4_discovery.h"
//#include "stm32f4_discovery_lcd.h"
#include "stm32f4_discovery_lis302dl.h"
#include "main.h"
//#include "bmp.h"
#include "dcmi_ov9655.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "sn8200_api.h"
#include "sn8200_core.h"
#include "delay.h"


///**
// * @brief Retargets the C library printf function to the USART.
// * @param None
// * @retval None
// */
//#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
//PUTCHAR_PROTOTYPE {
// /* Place your implementation of fputc here */
// /* e.g. write a character to the USART */
// USART_SendData(USART3, (uint8_t) ch);

// /* Loop until the end of transmission */
// while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET) {
// }

// return ch;
//}

///** @addtogroup STM32F4xx_StdPeriph_Examples
// * @{
// */

///** @addtogroup DCMI_OV9655_Camera
// * @{
// */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define DCMI_DR_ADDRESS 0x50050028
#define FSMC_LCD_ADDRESS 0x60100000

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint8_t KeyPressFlg = 0;
__IO uint32_t TimingDelay;
RCC_ClocksTypeDef RCC_Clocks;
EXTI_InitTypeDef EXTI_InitStructure;
uint8_t capture_Flag = ENABLE;



/* Private function prototypes -----------------------------------------------*/
uint8_t DCMI_OV9655Config(void);
void DCMI_Config(void);
void I2C1_Config(void);
void EXTILine0_Config(void);
void LIS302DL_Reset(void);

/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/

uint8_t seqNo = 0;
char sockConnected = -1;
char sockClosed = -1;
int8_t mysock = -1;
//frame rate control
uint8_t frameCnt = 0;
static unsigned char *frames[2];
unsigned char txReady[2] = {0,0};

#define CLIENT_IP "172.31.0.10"
#define CLIENT_PORT 0x7d0

void setupWifiForUDP(void)
{
SN8200_API_Init(921600);
WifiOn(seqNo++);
SnicInit(seqNo++);
SnicAPGetDhcp(seqNo++);
mysock = udpCreateSocket(0,0,0,seqNo++);
if (mysock != -1)
{
if (getUDPinfo() == CMD_ERROR) {
while(1);
}
}
}

void transmitFrame()
{
int i=0;
for(i=0;i<2;i++)
{
if(txReady[i] == 1)
{
txReady[i] = 0;
SendSNIC(frames[i],160*120*2); // 160*120*2
break;
}
}
//udpSendFromSock(, CLIENT_PORT, mysock, 0, frames[loc], 160*120*2, seqNo++);
}
int main(void)
{
/* SysTick end of count event each 10ms */
int i=0;
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);

//qhm allocate memory for camera frame
for(i=0;i<2;i++)
{
frames[i] = (unsigned char*)malloc(160*120*2);
memset((void*)frames[i],0,160*120*2);
}

/* SET USER Key */
/* Configure EXTI Line0 (connected to PA0 pin) in interrupt mode */
//EXTILine0_Config();

/* Initialize the LCD */
//STM32f4_Discovery_LCD_Init();
//LCD_Clear(LCD_COLOR_WHITE);
//LCD_SetTextColor(LCD_COLOR_BLUE);

//setup wifi udp
setupWifiForUDP();

DCMI_Control_IO_Init();

//LCD_DisplayStringLine(LINE(2), " Camera Init..");

/* OV9655 Camera Module configuration */
if (DCMI_OV9655Config() == 0x00)
{
//LCD_DisplayStringLine(LINE(2), " ");
//LCD_SetDisplayWindow(0, 0, 320, 240);
//LCD_WriteRAM_Prepare();



/* Start Image capture and Display on the LCD *****************************/
/* Enable DMA transfer */
DMA_Cmd(DMA2_Stream1, ENABLE);

/* Enable DCMI interface */
DCMI_Cmd(ENABLE);

/* Start Image capture */
DCMI_CaptureCmd(ENABLE);



while (1)
{
/* Insert 100ms delay */
Delay(10);
//if(frameCnt==(SKIP_FRAMES-1))
transmitFrame();
}
}
return 0;
}

/**
* @brief Configures all needed resources (I2C, DCMI and DMA) to interface with
* the OV9655 camera module
* @param None
* @retval 0x00 Camera module configured correctly
* 0xFF Camera module configuration failed
*/
uint8_t DCMI_OV9655Config(void)
{
/* I2C1 will be used for OV9655 camera configuration */
I2C1_Config();

/* Reset and check the presence of the OV9655 camera module */
if (DCMI_SingleRandomWrite(OV9655_DEVICE_WRITE_ADDRESS,0x12, 0x80))
{
return (0xFF);
}

/* OV9655 Camera size setup */
//DCMI_OV9655_QVGASizeSetup(); QHM
DCMI_OV9655_QQVGASizeSetup();

/* Set the RGB565 mode */
DCMI_SingleRandomWrite(OV9655_DEVICE_WRITE_ADDRESS, OV9655_COM7, 0x63);
DCMI_SingleRandomWrite(OV9655_DEVICE_WRITE_ADDRESS, OV9655_COM15, 0x10);

/* Invert the HRef signal*/
DCMI_SingleRandomWrite(OV9655_DEVICE_WRITE_ADDRESS, OV9655_COM10, 0x08);

/* Configure the DCMI to interface with the OV9655 camera module */
DCMI_Config();

return (0x00);
}

/**
* @brief Configures the I2C1 used for OV9655 camera module configuration.
* @param None
* @retval None
*/
void I2C1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStruct;

/* I2C1 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* GPIOB clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

/* Connect I2C1 pins to AF4 ************************************************/
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);

/* Configure I2C1 GPIOs *****************************************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);

/* Configure I2C1 ***********************************************************/
/* I2C DeInit */
I2C_DeInit(I2C1);

/* Enable the I2C peripheral */
I2C_Cmd(I2C1, ENABLE);

/* Set the I2C structure parameters */
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 = 0xFE;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStruct.I2C_ClockSpeed = 30000;

/* Initialize the I2C peripheral w/ selected parameters */
I2C_Init(I2C1, &I2C_InitStruct);
}

/**
* @brief Configures the DCMI to interface with the OV9655 camera module.
* @param None
* @retval None
*/
void DCMI_Config(void)
{
DCMI_InitTypeDef DCMI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

/* Enable DCMI GPIOs clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOE |
RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOA, ENABLE);

/* Enable DCMI clock */
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);

/* Connect DCMI pins to AF13 ************************************************/
/* PCLK */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_DCMI);
/* D0-D7 */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource4, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_DCMI);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_DCMI);
/* VSYNC */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_DCMI);
/* HSYNC */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_DCMI);

/* DCMI GPIO configuration **************************************************/
/* D0 D1(PC6/7) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOC, &GPIO_InitStructure);

/* D2..D4(PE0/1/4) D6/D7(PE5/6) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1
| GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
GPIO_Init(GPIOE, &GPIO_InitStructure);

/* D5(PB6), VSYNC(PB7) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);

/* PCLK(PA6) HSYNC(PA4)*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* DCMI configuration *******************************************************/
DCMI_InitStructure.DCMI_CaptureMode = DCMI_CaptureMode_Continuous;
DCMI_InitStructure.DCMI_SynchroMode = DCMI_SynchroMode_Hardware;
DCMI_InitStructure.DCMI_PCKPolarity = DCMI_PCKPolarity_Falling;
DCMI_InitStructure.DCMI_VSPolarity = DCMI_VSPolarity_High;
DCMI_InitStructure.DCMI_HSPolarity = DCMI_HSPolarity_High;
DCMI_InitStructure.DCMI_CaptureRate = DCMI_CaptureRate_1of4_Frame;//DCMI_CaptureRate_All_Frame;
DCMI_InitStructure.DCMI_ExtendedDataMode = DCMI_ExtendedDataMode_8b;

DCMI_Init(&DCMI_InitStructure);

/* Enable DMA2 clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);

/* DMA2 Stream1 Configuration */
DMA_DeInit(DMA2_Stream1);

DMA_InitStructure.DMA_Channel = DMA_Channel_1;
DMA_InitStructure.DMA_PeripheralBaseAddr = DCMI_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)frames[0];//FSMC_LCD_ADDRESS; QHM store in system heap memory
//DMA_InitStructure. = (uint32_t)frame0;//FSMC_LCD_ADDRESS; QHM store in system heap memory
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 160 * 60;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

DMA_Init(DMA2_Stream1, &DMA_InitStructure);

DMA_DoubleBufferModeConfig(DMA2_Stream1,(uint32_t)frames[1],DMA_Memory_0);
DMA_DoubleBufferModeCmd(DMA2_Stream1,ENABLE);

DMA_ITConfig(DMA2_Stream1,DMA_IT_TC,ENABLE);

NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x09;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

}




/**
* @brief Configures EXTI Line0 (connected to PA0 pin) in interrupt mode
* @param None
* @retval None
*/
void EXTILine0_Config(void)
{

GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

/* Enable GPIOA clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

/* Configure PA0 pin as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Connect EXTI Line0 to PA0 pin */
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);

/* Configure EXTI Line0 */
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);

/* Enable and set EXTI Line0 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

/**
* @brief Inserts a delay time.
* @param nTime: specifies the delay time length, in milliseconds
* @retval None
*/
void Delay(uint32_t nTime)
{
TimingDelay = nTime;

while (TimingDelay != 0);

}

/**

* @brief Decrements the TimingDelay variable.
* @param None
* @retval None
*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}




#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* Infinite loop */
while (1)
{
}
}
#endif

Selected excerpts from Receiver Application on Workstation:
#include<stdio.h>
#include<winsock2.h>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>

#pragma comment(lib,"ws2_32.lib") //Winsock Library
#ifdef _DEBUG
#pragma comment(lib,"opencv_core249d.lib")
#pragma comment(lib,"opencv_imgproc249d.lib")
#pragma comment(lib,"opencv_highgui249d.lib")
#else
#pragma comment(lib,"opencv_core249.lib")
#pragma comment(lib,"opencv_imgproc249.lib")
#pragma comment(lib,"opencv_highgui249.lib")
#endif

#define BUFLEN 2000 //Max length of buffer
#define PORT 2015 //The port on which to listen for incoming data

using namespace cv;

int main()
{
SOCKET s;
struct sockaddr_in server, si_other;
int slen, recv_len;
char buf[BUFLEN];
WSADATA wsa;



slen = sizeof(si_other);

//Initialise winsock
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");

//Create a socket
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
}
printf("Socket created.\n");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);

//Bind
if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
puts("Bind done");




//while (1)
//{
// //clear the buffer by filling null, it might have previously received data
// memset(buf, '\0', BUFLEN);

// //try to receive some data, this is a blocking call
// if ((recv_len = recvfrom(s, buf, 1001, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
// {
// printf("recvfrom() failed with error code : %d", WSAGetLastError());
// exit(EXIT_FAILURE);
// }

// //look for first packet
// if (buf[0] == 0)
// break;

//}

while (1)
{
Mat rimg(120, 160, CV_8UC2, Scalar(0, 0));
int rlen = 0;
for (int i = 0; i < 38;i++)
{
memset(buf, '\0', BUFLEN);
if ((recv_len = recvfrom(s,buf , 1001, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
{
//printf("recvfrom() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
memcpy((char*)(rimg.data + rlen), buf + 1, recv_len - 1);
rlen += recv_len - 1;
}

memset(buf, '\0', BUFLEN);
if ((recv_len = recvfrom(s, buf, 401, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
{
//printf("recvfrom() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
memcpy((char*)(rimg.data + rlen), buf + 1, recv_len - 1);
rlen += recv_len - 1;

cvtColor(rimg, rimg, CV_BGR5652BGR);
resize(rimg, rimg, Size(), 4, 4);
imshow("test", rimg);
waitKey(1);
}
closesocket(s);
WSACleanup();

return 0;
}