More optimalizations and multithreading - 1FPS

main
Adam Prochazka 2 years ago
parent ae0f8749b5
commit 374a63bc0c

@ -132,39 +132,149 @@ void Cam_Init(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi)
Cam_I2C_write(hi2c, (uint16_t)0x3621, 0x10); // REGISTER FOR CORRECT MIRROR FUNCTION
Cam_I2C_write(hi2c, (uint16_t)0x3801, 0xb0); // TIMING HORIZONTAL START - ALSO FOR MIRROR
Cam_I2C_write(hi2c, (uint16_t)0x4407, 0x04); // COMPRESSION CONTROL
Cam_I2C_write(hi2c, (uint16_t)0x5000, 0xFF);
Cam_I2C_write_bulk(hi2c, ov5642_1024x768);
// Setup camera, H-sync: High, V-sync:high, Sensor_delay: no Delay, FIFO_mode:FIFO enabled, power_mode:Low_power
Cam_SPI_write(hspi, 0x03, 0x02);
Cam_SPI_write(hspi, 0x01, 0x00); // Capture Control Register - Set to capture n+1 frames
HAL_Delay(5);
/*
Cam_I2C_write(hi2c, (uint16_t)0x3008, 0x80);
Cam_I2C_write_bulk(hi2c, OV5642_QVGA_Preview);
HAL_Delay(100);
Cam_I2C_write_bulk(hi2c, OV5642_JPEG_Capture_QSXGA);
//Cam_I2C_write_bulk(hi2c, ov5642_320x240);
//Cam_I2C_write_bulk(hi2c, ov5642_640x480);
Cam_I2C_write_bulk(hi2c, ov5642_1280x960);
HAL_Delay(100);
//Cam_I2C_write_bulk(hi2c, OV5642_720P_Video_setting);
Cam_I2C_write(hi2c, (uint16_t)0x3818, 0xa8); // TIMING CONTROL - ENABLE COMPRESSION, THUMBNAIL MODE DISABLE, VERTICAL FLIP, MIRROR
Cam_I2C_write(hi2c, (uint16_t)0x3621, 0x10); // REGISTER FOR CORRECT MIRROR FUNCTION
Cam_I2C_write(hi2c, (uint16_t)0x3801, 0xb0); // TIMING HORIZONTAL START - ALSO FOR MIRROR
//Cam_I2C_write(hi2c, (uint16_t)0x4407, 0x04); // COMPRESSION CONTROL
Cam_I2C_write(hi2c, (uint16_t)0x4407, 0x04); // COMPRESSION CONTROL
//Cam_I2C_write(hi2c, (uint16_t)0x5888, 0x01);
Cam_I2C_write(hi2c, (uint16_t)0x5000, 0xFF);
// Setup camera, H-sync: High, V-sync:high, Sensor_delay: no Delay, FIFO_mode:FIFO enabled, power_mode:Low_power
Cam_SPI_write(hspi, 0x03, 0x02);
Cam_I2C_write_bulk(hi2c, ov5642_320x240);
//Cam_I2C_write_bulk(hi2c, ov5642_1024x768);
Cam_SPI_write(hspi, 0x04, 0x01);
Cam_SPI_write(hspi, 0x01, 0x00); // Capture Control Register - Set to capture n+1 frames
HAL_Delay(5);
*/
}
void Cam_Refresh(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi){
Cam_I2C_write(hi2c, (uint16_t)0x3008, 0x80);
/*Cam_I2C_write(hi2c, (uint16_t)0x3008, 0x80);
Cam_I2C_write_bulk(hi2c, OV5642_QVGA_Preview);
Cam_I2C_write_bulk(hi2c, OV5642_JPEG_Capture_QSXGA);
//Cam_I2C_write_bulk(hi2c, OV5642_720P_Video_setting);
Cam_I2C_write(hi2c, (uint16_t)0x3818, 0xa8); // TIMING CONTROL - ENABLE COMPRESSION, THUMBNAIL MODE DISABLE, VERTICAL FLIP, MIRROR
Cam_I2C_write(hi2c, (uint16_t)0x3621, 0x10); // REGISTER FOR CORRECT MIRROR FUNCTION
Cam_I2C_write(hi2c, (uint16_t)0x3801, 0xb0); // TIMING HORIZONTAL START - ALSO FOR MIRROR
Cam_I2C_write(hi2c, (uint16_t)0x4407, 0x04); // COMPRESSION CONTROL
Cam_I2C_write_bulk(hi2c, ov5642_1280x960);
// Setup camera, H-sync: High, V-sync:high, Sensor_delay: no Delay, FIFO_mode:FIFO enabled, power_mode:Low_power
Cam_SPI_write(hspi, 0x03, 0x02);
Cam_SPI_write(hspi, 0x01, 0x00); // Capture Control Register - Set to capture n+1 frames
HAL_Delay(5);*/
// SAFE TO REMOVE
//Cam_I2C_write(hi2c, (uint16_t)0x3008, 0x80);
Cam_I2C_write_bulk(hi2c, OV5642_QVGA_Preview);
Cam_I2C_write_bulk(hi2c, OV5642_JPEG_Capture_QSXGA);
Cam_I2C_write(hi2c, (uint16_t)0x3818, 0xa8); // TIMING CONTROL - ENABLE COMPRESSION, THUMBNAIL MODE DISABLE, VERTICAL FLIP, MIRROR
Cam_I2C_write(hi2c, (uint16_t)0x3621, 0x10); // REGISTER FOR CORRECT MIRROR FUNCTION
Cam_I2C_write(hi2c, (uint16_t)0x3801, 0xb0); // TIMING HORIZONTAL START - ALSO FOR MIRROR
Cam_I2C_write(hi2c, (uint16_t)0x4407, 0x04); // COMPRESSION CONTROL
Cam_I2C_write(hi2c, (uint16_t)0x5000, 0xFF);
//Cam_I2C_write_bulk(hi2c, ov5642_320x240);
//ov5642_1024x768
Cam_I2C_write_bulk(hi2c, ov5642_1024x768);
// Setup camera, H-sync: High, V-sync:high, Sensor_delay: no Delay, FIFO_mode:FIFO enabled, power_mode:Low_power
Cam_SPI_write(hspi, 0x03, 0x02);
// Brightness 4
Cam_I2C_write(hi2c, (uint16_t)0x5001, 0xff);
Cam_I2C_write(hi2c, (uint16_t)0x5589, 0x30);
Cam_I2C_write(hi2c, (uint16_t)0x5580, 0x04);
Cam_I2C_write(hi2c, (uint16_t)0x558a, 0x00);
//Cam_I2C_write(hi2c, (uint16_t)0x558a, 0x08);
//Contrast 0
//Cam_I2C_write(hi2c, (uint16_t)0x5587, 0xff);
//Cam_I2C_write(hi2c, (uint16_t)0x5588, 0xff);
/*
// Exposure _17
Cam_I2C_write(hi2c, (uint16_t)0x3a0f, 0x10);
Cam_I2C_write(hi2c, (uint16_t)0x3a10, 0x08);
Cam_I2C_write(hi2c, (uint16_t)0x3a1b, 0x10);
Cam_I2C_write(hi2c, (uint16_t)0x3a1e, 0x08);
Cam_I2C_write(hi2c, (uint16_t)0x3a11, 0x20);
Cam_I2C_write(hi2c, (uint16_t)0x3a1f, 0x10);
//Exposure 17 v2
Cam_I2C_write(hi2c, (uint16_t)0x3a0f, 0x60);
Cam_I2C_write(hi2c, (uint16_t)0x3a10, 0x58);
Cam_I2C_write(hi2c, (uint16_t)0x3a11, 0xa0);
Cam_I2C_write(hi2c, (uint16_t)0x3a1b, 0x60);
Cam_I2C_write(hi2c, (uint16_t)0x3a1e, 0x58);
Cam_I2C_write(hi2c, (uint16_t)0x3a1f, 0x20);
*/
// Exposure Manual
Cam_I2C_write(hi2c, (uint16_t)0x3503, 0x3);
Cam_I2C_write(hi2c, (uint16_t)0x350C, 0xFF);
Cam_I2C_write(hi2c, (uint16_t)0x350D, 0xFF);
Cam_I2C_write(hi2c, (uint16_t)0x3500, 0x0);
Cam_I2C_write(hi2c, (uint16_t)0x3501, 0x00);
Cam_I2C_write(hi2c, (uint16_t)0x3502, 0x0f);
//Gain MAX
Cam_I2C_write(hi2c, (uint16_t)0x350A, 0x0);
Cam_I2C_write(hi2c, (uint16_t)0x350B, 0x35);
Cam_I2C_write(hi2c, (uint16_t)0x3508, 0x0);
Cam_I2C_write(hi2c, (uint16_t)0x3509, 0x35);
// AWB
Cam_I2C_write(hi2c, (uint16_t)0x3406, 0x1);
Cam_I2C_write(hi2c, (uint16_t)0x3400, 0xf);
Cam_I2C_write(hi2c, (uint16_t)0x3401, 0xff);
Cam_I2C_write(hi2c, (uint16_t)0x3402, 0xf);
Cam_I2C_write(hi2c, (uint16_t)0x3403, 0x7f);
Cam_I2C_write(hi2c, (uint16_t)0x3404, 0xf);
Cam_I2C_write(hi2c, (uint16_t)0x3405, 0xff);
Cam_SPI_write(hspi, 0x01, 0x00); // Capture Control Register - Set to capture n+1 frames
HAL_Delay(5);
@ -200,12 +310,12 @@ void Cam_Start_Capture(SPI_HandleTypeDef *hspi)
Cam_SPI_write(hspi, 0x04, FIFO_Reg_Clear_Flags); // Reset FIFO Read Pointer
Cam_SPI_write(hspi, 0x04, FIFO_Reg_Clear_Flags);
*/
Cam_SPI_write(hspi, 0x04, 0x01); // Start capture
Cam_SPI_write(hspi, 0x04, 0x01); // Start capture
Cam_SPI_write(hspi, 0x04, 0x01);
Cam_SPI_write(hspi, 0x04, 0x01);
HAL_Delay(1);
HAL_Delay(10);
Cam_SPI_write(hspi, 0x04, 0x02); // Start capture
HAL_Delay(1);
HAL_Delay(10);
}
void Cam_Wait_Capture_Done(SPI_HandleTypeDef *hspi)
@ -229,6 +339,9 @@ void Cam_Start_Burst_Read(SPI_HandleTypeDef *hspi)
Cam_SPI_write(hspi, 0x04, FIFO_Reg_Clear_Flags);
*/
Cam_SPI_write(hspi, 0x04, 0x01);
Cam_SPI_write(hspi, 0x04, 0x01);
uint8_t BURST_FIFO_READ = 0x3c;
uint8_t empty = 0x00;

@ -23,12 +23,10 @@
#define __MAIN_H
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#define USE_HAL_DRIVER
#include "stm32l4xx_hal.h"
/* Private includes ----------------------------------------------------------*/
@ -88,7 +86,9 @@ extern "C"
#define LD3_GPIO_Port GPIOB
/* USER CODE BEGIN Private defines */
#define CDC_BUFF_SIZE 10048
#define CDC_FRAME_SIZE 64
#define CDC_FRAME_DELAY 1
/* USER CODE END Private defines */
#ifdef __cplusplus

@ -57,6 +57,7 @@ void PendSV_Handler(void);
void SysTick_Handler(void);
void DMA1_Channel2_IRQHandler(void);
void DMA1_Channel3_IRQHandler(void);
void SPI1_IRQHandler(void);
void USART2_IRQHandler(void);
void USB_IRQHandler(void);
/* USER CODE BEGIN EFP */

@ -18,7 +18,7 @@
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "images.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "tusb.h"
@ -47,8 +47,6 @@ DMA_HandleTypeDef hdma_spi1_tx;
UART_HandleTypeDef huart2;
PCD_HandleTypeDef hpcd_USB_FS;
/* USER CODE BEGIN PV */
@ -62,7 +60,6 @@ int SPI_Rx_Done_Flag = 0;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SPI1_Init(void);
@ -221,6 +218,18 @@ int send_CDC_Bulk(){
return 1;
}
*/
void SysTick_Init(void)
{
SysTick_Config(SystemCoreClock / 10000); // Set SysTick interrupt to occur every 100us
}
void delay_us(uint32_t us)
{
uint32_t start = SysTick->VAL;
uint32_t ticks = us * 10;
while ((start - SysTick->VAL) < ticks);
}
/* USER CODE END 0 */
@ -271,13 +280,15 @@ int main(void)
// Wait for power stabilization
//HAL_Delay(1000);
#define CDC_BUFF_SIZE 10000
tud_task();
int last_sent_idx = 0;
int buff_stop_idx = 0;
uint16_t image_size = 0;
uint8_t cdc_buff[CDC_BUFF_SIZE];
Cam_Init(&hi2c1, &hspi1);
/* USER CODE END 2 */
/* Infinite loop */
@ -288,6 +299,8 @@ int main(void)
if(buff_stop_idx >= (int)image_size){
LED_Off();
buff_stop_idx = 0;
last_sent_idx = 0;
@ -298,7 +311,7 @@ int main(void)
tud_task();
Cam_Init(&hi2c1, &hspi1);
Cam_Refresh(&hi2c1, &hspi1);
tud_task();
@ -309,6 +322,8 @@ int main(void)
image_size = Cam_FIFO_length(&hspi1);
Cam_Start_Burst_Read(&hspi1);
LED_On();
continue;
//HAL_SPI_Receive(&hspi1, cdc_buff, CDC_BUFF_SIZE, HAL_MAX_DELAY);
@ -331,16 +346,17 @@ int main(void)
int current_sending_idx = 0;
do{
tud_task();
tud_cdc_write(&cdc_buff[current_sending_idx], 50);
tud_cdc_write(&cdc_buff[current_sending_idx], CDC_FRAME_SIZE);
current_sending_idx = current_sending_idx + CDC_FRAME_SIZE;
last_sent_idx = last_sent_idx + CDC_FRAME_SIZE;
tud_cdc_write_flush();
current_sending_idx = current_sending_idx + 50;
last_sent_idx = last_sent_idx + 50;
HAL_Delay(3);
//Delay between sends
int i = 0;
for(;i<4000;i++);
}
while(last_sent_idx < buff_stop_idx);
LED_On();
/* USER CODE END WHILE */
@ -386,7 +402,8 @@ void SystemClock_Config(void)
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
@ -443,6 +460,7 @@ static void MX_I2C1_Init(void)
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**
@ -468,7 +486,7 @@ static void MX_SPI1_Init(void)
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
@ -482,6 +500,7 @@ static void MX_SPI1_Init(void)
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}
/**
@ -516,6 +535,7 @@ static void MX_USART2_UART_Init(void)
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/**
@ -548,6 +568,7 @@ static void MX_USB_PCD_Init(void)
/* USER CODE BEGIN USB_Init 2 */
/* USER CODE END USB_Init 2 */
}
/**
@ -566,6 +587,7 @@ static void MX_DMA_Init(void)
/* DMA1_Channel3_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);
}
/**
@ -601,6 +623,7 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */

@ -221,6 +221,9 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
__HAL_LINKDMA(hspi,hdmatx,hdma_spi1_tx);
/* SPI1 interrupt Init */
HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(SPI1_IRQn);
/* USER CODE BEGIN SPI1_MspInit 1 */
/* USER CODE END SPI1_MspInit 1 */
@ -254,6 +257,9 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
/* SPI1 DMA DeInit */
HAL_DMA_DeInit(hspi->hdmarx);
HAL_DMA_DeInit(hspi->hdmatx);
/* SPI1 interrupt DeInit */
HAL_NVIC_DisableIRQ(SPI1_IRQn);
/* USER CODE BEGIN SPI1_MspDeInit 1 */
/* USER CODE END SPI1_MspDeInit 1 */

@ -57,6 +57,7 @@
/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_spi1_rx;
extern DMA_HandleTypeDef hdma_spi1_tx;
extern SPI_HandleTypeDef hspi1;
extern UART_HandleTypeDef huart2;
extern PCD_HandleTypeDef hpcd_USB_FS;
/* USER CODE BEGIN EV */
@ -229,6 +230,20 @@ void DMA1_Channel3_IRQHandler(void)
/* USER CODE END DMA1_Channel3_IRQn 1 */
}
/**
* @brief This function handles SPI1 global interrupt.
*/
void SPI1_IRQHandler(void)
{
/* USER CODE BEGIN SPI1_IRQn 0 */
/* USER CODE END SPI1_IRQn 0 */
HAL_SPI_IRQHandler(&hspi1);
/* USER CODE BEGIN SPI1_IRQn 1 */
/* USER CODE END SPI1_IRQn 1 */
}
/**
* @brief This function handles USART2 global interrupt.
*/

@ -74,6 +74,7 @@ NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4
NVIC.SPI1_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.SysTick_IRQn=true\:0\:0\:true\:false\:true\:true\:true\:false
NVIC.USART2_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
@ -160,6 +161,7 @@ ProjectManager.PreviousToolchain=
ProjectManager.ProjectBuild=false
ProjectManager.ProjectFileName=Probe.ioc
ProjectManager.ProjectName=Probe
ProjectManager.ProjectStructure=
ProjectManager.RegisterCallBack=
ProjectManager.StackSize=0x400
ProjectManager.TargetToolchain=Makefile
@ -225,8 +227,8 @@ RCC.VCOInputFreq_Value=16000000
RCC.VCOOutputFreq_Value=160000000
RCC.VCOSAI1OutputFreq_Value=128000000
RCC.WatchDogFreq_Value=32000
SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_128
SPI1.CalculateBaudRate=625.0 KBits/s
SPI1.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_32
SPI1.CalculateBaudRate=2.5 MBits/s
SPI1.DataSize=SPI_DATASIZE_8BIT
SPI1.Direction=SPI_DIRECTION_2LINES
SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate,DataSize,BaudRatePrescaler,NSSPMode

@ -1,5 +1,5 @@
CC = g++
CFLAGS = -pthread -std=c++11 -Wall -Wextra -pedantic
CFLAGS = -pthread -std=c++11 -Wall -Wextra -pedantic -O3
LDLIBS = -lSDL2 -lSDL2_image
SRCS = $(wildcard *.cpp)

@ -64,7 +64,6 @@ int Receiver::readCdcData(uint8_t (*character)[CDC_FRAME_SIZE]) {
std::cerr << "Error in read" << std::endl;
return -1;
}
#if 0 // PRINT WHAT IS RECEIVED
for(int i = 0; i<CDC_FRAME_SIZE; i++)
@ -77,7 +76,6 @@ int Receiver::readCdcData(uint8_t (*character)[CDC_FRAME_SIZE]) {
#endif
return 0;
}
@ -124,8 +122,6 @@ int Receiver::fillBuffer(){
unsigned char character[CDC_FRAME_SIZE];
std::vector<uint8_t> tempVec{};
Receiver::readCdcData(&character);
int i = -1;
do {
Receiver::readCdcData(&character);
@ -186,15 +182,17 @@ void Receiver::bufferToDisplay(){
currentBufferIndexMutex.lock();
int buffIdx = currentBufferIndex;
currentBufferIndexMutex.unlock();
std::cout << buffIdx << std::endl;
switch(buffIdx){
case 0:
std::cout << buffer1.size() << std::endl;
dis->vectorToTexture(&buffer1);
break;
case 1:
std::cout << buffer2.size() << std::endl;
dis->vectorToTexture(&buffer2);
break;
case 2:
std::cout << buffer3.size() << std::endl;
dis->vectorToTexture(&buffer3);
break;
default:

@ -32,11 +32,18 @@ void windowFlipThread(Displayer * displayerPtr){
displayerPtr->flipThroughTextures();
}
void WrapperBufferToDisplay(Receiver ** receiverPtr){
(*receiverPtr)->bufferToDisplay();
}
void receiverLoop(Receiver ** receiverPtr){
std::vector<std::thread> displayThreads;
while(1){
(*receiverPtr)->fillBuffer();
(*receiverPtr)->bufferToDisplay();
std::this_thread::sleep_for(std::chrono::milliseconds(3));
std::thread t2(WrapperBufferToDisplay, receiverPtr);
t2.detach();
}
}

@ -15,4 +15,5 @@
#include <mutex>
#include <fstream>
#define CDC_FRAME_SIZE 50
#define CDC_FRAME_SIZE 64
#define CDC_FRAME_DELAY 1

Loading…
Cancel
Save