Add nonblock flag to UART FILE*

This commit is contained in:
Aadi Desai 2021-06-10 00:19:35 +01:00
parent 7ab9de2cc7
commit 108f4d1495

View file

@ -1,5 +1,3 @@
#include <stdio.h> #include <stdio.h>
#include "I2C_core.h" #include "I2C_core.h"
#include "terasic_includes.h" #include "terasic_includes.h"
@ -12,7 +10,7 @@
#include <unistd.h> #include <unistd.h>
//EEE_IMGPROC defines //EEE_IMGPROC defines
#define EEE_IMGPROC_MSG_START ('R'<<16 | 'B'<<8 | 'B') #define EEE_IMGPROC_MSG_START ('R' << 16 | 'B' << 8 | 'B')
//offsets //offsets
#define EEE_IMGPROC_STATUS 0 #define EEE_IMGPROC_STATUS 0
@ -39,23 +37,25 @@
#define MIPI_REG_FrmErrCnt 0x0080 #define MIPI_REG_FrmErrCnt 0x0080
#define MIPI_REG_MDLErrCnt 0x0090 #define MIPI_REG_MDLErrCnt 0x0090
void mipi_clear_error(void){ void mipi_clear_error(void)
MipiBridgeRegWrite(MIPI_REG_CSIStatus,0x01FF); // clear error {
MipiBridgeRegWrite(MIPI_REG_MDLSynErr,0x0000); // clear error MipiBridgeRegWrite(MIPI_REG_CSIStatus, 0x01FF); // clear error
MipiBridgeRegWrite(MIPI_REG_FrmErrCnt,0x0000); // clear error MipiBridgeRegWrite(MIPI_REG_MDLSynErr, 0x0000); // clear error
MipiBridgeRegWrite(MIPI_REG_FrmErrCnt, 0x0000); // clear error
MipiBridgeRegWrite(MIPI_REG_MDLErrCnt, 0x0000); // clear error MipiBridgeRegWrite(MIPI_REG_MDLErrCnt, 0x0000); // clear error
MipiBridgeRegWrite(0x0082,0x00); MipiBridgeRegWrite(0x0082, 0x00);
MipiBridgeRegWrite(0x0084,0x00); MipiBridgeRegWrite(0x0084, 0x00);
MipiBridgeRegWrite(0x0086,0x00); MipiBridgeRegWrite(0x0086, 0x00);
MipiBridgeRegWrite(0x0088,0x00); MipiBridgeRegWrite(0x0088, 0x00);
MipiBridgeRegWrite(0x008A,0x00); MipiBridgeRegWrite(0x008A, 0x00);
MipiBridgeRegWrite(0x008C,0x00); MipiBridgeRegWrite(0x008C, 0x00);
MipiBridgeRegWrite(0x008E,0x00); MipiBridgeRegWrite(0x008E, 0x00);
MipiBridgeRegWrite(0x0090,0x00); MipiBridgeRegWrite(0x0090, 0x00);
} }
void mipi_show_error_info(void){ void mipi_show_error_info(void)
{
alt_u16 PHY_status, SCI_status, MDLSynErr, FrmErrCnt, MDLErrCnt; alt_u16 PHY_status, SCI_status, MDLSynErr, FrmErrCnt, MDLErrCnt;
@ -64,64 +64,50 @@ void mipi_show_error_info(void){
MDLSynErr = MipiBridgeRegRead(MIPI_REG_MDLSynErr); MDLSynErr = MipiBridgeRegRead(MIPI_REG_MDLSynErr);
FrmErrCnt = MipiBridgeRegRead(MIPI_REG_FrmErrCnt); FrmErrCnt = MipiBridgeRegRead(MIPI_REG_FrmErrCnt);
MDLErrCnt = MipiBridgeRegRead(MIPI_REG_MDLErrCnt); MDLErrCnt = MipiBridgeRegRead(MIPI_REG_MDLErrCnt);
printf("PHY_status=%xh, CSI_status=%xh, MDLSynErr=%xh, FrmErrCnt=%xh, MDLErrCnt=%xh\r\n", PHY_status, SCI_status, MDLSynErr,FrmErrCnt, MDLErrCnt); printf("PHY_status=%xh, CSI_status=%xh, MDLSynErr=%xh, FrmErrCnt=%xh, MDLErrCnt=%xh\r\n", PHY_status, SCI_status, MDLSynErr, FrmErrCnt, MDLErrCnt);
} }
void mipi_show_error_info_more(void){ void mipi_show_error_info_more(void)
printf("FrmErrCnt = %d\n",MipiBridgeRegRead(0x0080)); {
printf("CRCErrCnt = %d\n",MipiBridgeRegRead(0x0082)); printf("FrmErrCnt = %d\n", MipiBridgeRegRead(0x0080));
printf("CorErrCnt = %d\n",MipiBridgeRegRead(0x0084)); printf("CRCErrCnt = %d\n", MipiBridgeRegRead(0x0082));
printf("HdrErrCnt = %d\n",MipiBridgeRegRead(0x0086)); printf("CorErrCnt = %d\n", MipiBridgeRegRead(0x0084));
printf("EIDErrCnt = %d\n",MipiBridgeRegRead(0x0088)); printf("HdrErrCnt = %d\n", MipiBridgeRegRead(0x0086));
printf("CtlErrCnt = %d\n",MipiBridgeRegRead(0x008A)); printf("EIDErrCnt = %d\n", MipiBridgeRegRead(0x0088));
printf("SoTErrCnt = %d\n",MipiBridgeRegRead(0x008C)); printf("CtlErrCnt = %d\n", MipiBridgeRegRead(0x008A));
printf("SynErrCnt = %d\n",MipiBridgeRegRead(0x008E)); printf("SoTErrCnt = %d\n", MipiBridgeRegRead(0x008C));
printf("MDLErrCnt = %d\n",MipiBridgeRegRead(0x0090)); printf("SynErrCnt = %d\n", MipiBridgeRegRead(0x008E));
printf("FIFOSTATUS = %d\n",MipiBridgeRegRead(0x00F8)); printf("MDLErrCnt = %d\n", MipiBridgeRegRead(0x0090));
printf("DataType = 0x%04x\n",MipiBridgeRegRead(0x006A)); printf("FIFOSTATUS = %d\n", MipiBridgeRegRead(0x00F8));
printf("CSIPktLen = %d\n",MipiBridgeRegRead(0x006E)); printf("DataType = 0x%04x\n", MipiBridgeRegRead(0x006A));
printf("CSIPktLen = %d\n", MipiBridgeRegRead(0x006E));
} }
bool MIPI_Init(void)
{
bool MIPI_Init(void){
bool bSuccess; bool bSuccess;
bSuccess = oc_i2c_init_ex(I2C_OPENCORES_MIPI_BASE, 50 * 1000 * 1000, 400 * 1000); //I2C: 400K
bSuccess = oc_i2c_init_ex(I2C_OPENCORES_MIPI_BASE, 50*1000*1000,400*1000); //I2C: 400K
if (!bSuccess) if (!bSuccess)
{
printf("failed to init MIPI- Bridge i2c\r\n"); printf("failed to init MIPI- Bridge i2c\r\n");
}
usleep(50*1000); usleep(50 * 1000);
MipiBridgeInit(); MipiBridgeInit();
usleep(500*1000); usleep(500 * 1000);
// bSuccess = oc_i2c_init_ex(I2C_OPENCORES_CAMERA_BASE, 50*1000*1000,400*1000); //I2C: 400K
// if (!bSuccess)
// printf("failed to init MIPI- Camera i2c\r\n");
MipiCameraInit(); MipiCameraInit();
MIPI_BIN_LEVEL(DEFAULT_LEVEL); MIPI_BIN_LEVEL(DEFAULT_LEVEL);
// OV8865_FOCUS_Move_to(340);
// oc_i2c_uninit(I2C_OPENCORES_CAMERA_BASE); // Release I2C bus , due to two I2C master shared!
usleep(1000); usleep(1000);
// oc_i2c_uninit(I2C_OPENCORES_MIPI_BASE);
return bSuccess; return bSuccess;
} }
int main() int main()
{ {
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
printf("DE10-LITE D8M VGA Demo\n"); printf("DE10-LITE D8M VGA Demo\n");
@ -134,52 +120,30 @@ int main()
usleep(2000); usleep(2000);
IOWR(MIPI_RESET_N_BASE, 0x00, 0xFF); IOWR(MIPI_RESET_N_BASE, 0x00, 0xFF);
printf("Image Processor ID: %x\n",IORD(0x42000,EEE_IMGPROC_ID)); printf("Image Processor ID: %x\n", IORD(0x42000, EEE_IMGPROC_ID));
//printf("Image Processor ID: %x\n",IORD(EEE_IMGPROC_0_BASE,EEE_IMGPROC_ID)); //Don't know why this doesn't work - definition is in system.h in BSP //printf("Image Processor ID: %x\n",IORD(EEE_IMGPROC_0_BASE,EEE_IMGPROC_ID)); //Don't know why this doesn't work - definition is in system.h in BSP
usleep(2000); usleep(2000);
// MIPI Init // MIPI Init
if (!MIPI_Init()){ if (!MIPI_Init())
{
printf("MIPI_Init Init failed!\r\n"); printf("MIPI_Init Init failed!\r\n");
}else{ }
else
{
printf("MIPI_Init Init successfully!\r\n"); printf("MIPI_Init Init successfully!\r\n");
} }
// while(1){ // while(1){
mipi_clear_error(); mipi_clear_error();
usleep(50*1000); usleep(50 * 1000);
mipi_clear_error(); mipi_clear_error();
usleep(1000*1000); usleep(1000 * 1000);
mipi_show_error_info(); mipi_show_error_info();
// mipi_show_error_info_more(); // mipi_show_error_info_more();
printf("\n"); printf("\n");
// } // }
#if 0 // focus sweep
printf("\nFocus sweep\n");
alt_u16 ii= 350;
alt_u8 dir = 0;
while(1){
if(ii< 50) dir = 1;
else if (ii> 1000) dir =0;
if(dir) ii += 20;
else ii -= 20;
printf("%d\n",ii);
OV8865_FOCUS_Move_to(ii);
usleep(50*1000);
}
#endif
////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////
alt_u16 bin_level = DEFAULT_LEVEL; alt_u16 bin_level = DEFAULT_LEVEL;
@ -193,73 +157,50 @@ int main()
OV8865SetGain(gain); OV8865SetGain(gain);
Focus_Init(); Focus_Init();
FILE* ser = fopen("/dev/uart_0", "rb+"); FILE *ser = fopen("/dev/uart_0", "rb+");
if(ser){ fcntl(ser, F_SETFL, O_NONBLOCK);
if (ser)
{
printf("Opened UART\n"); printf("Opened UART\n");
} else { }
else
{
printf("Failed to open UART\n"); printf("Failed to open UART\n");
while (1); while (1)
{
}
} }
while(1){ while (1)
{
// touch KEY0 to trigger Auto focus // touch KEY0 to trigger Auto focus
if((IORD(KEY_BASE,0)&0x03) == 0x02){ if ((IORD(KEY_BASE, 0) & 0x03) == 0x02)
{
current_focus = Focus_Window(320,240); current_focus = Focus_Window(320, 240);
} }
// touch KEY1 to ZOOM // touch KEY1 to ZOOM
if((IORD(KEY_BASE,0)&0x03) == 0x01){ if ((IORD(KEY_BASE, 0) & 0x03) == 0x01)
if(bin_level == 3 )bin_level = 1; {
else bin_level ++; if (bin_level == 3)
printf("set bin level to %d\n",bin_level); bin_level = 1;
else
bin_level++;
printf("set bin level to %d\n", bin_level);
MIPI_BIN_LEVEL(bin_level); MIPI_BIN_LEVEL(bin_level);
usleep(500000); usleep(500000);
} }
#if 0
if((IORD(KEY_BASE,0)&0x0F) == 0x0E){
current_focus = Focus_Window(320,240);
}
// touch KEY1 to trigger Manual focus - step
if((IORD(KEY_BASE,0)&0x0F) == 0x0D){
if(current_focus > manual_focus_step) current_focus -= manual_focus_step;
else current_focus = 0;
OV8865_FOCUS_Move_to(current_focus);
}
// touch KEY2 to trigger Manual focus + step
if((IORD(KEY_BASE,0)&0x0F) == 0x0B){
current_focus += manual_focus_step;
if(current_focus >1023) current_focus = 1023;
OV8865_FOCUS_Move_to(current_focus);
}
// touch KEY3 to ZOOM
if((IORD(KEY_BASE,0)&0x0F) == 0x07){
if(bin_level == 3 )bin_level = 1;
else bin_level ++;
printf("set bin level to %d\n",bin_level);
MIPI_BIN_LEVEL(bin_level);
usleep(500000);
}
#endif
//Read messages from the image processor and print them on the terminal //Read messages from the image processor and print them on the terminal
while ((IORD(0x42000,EEE_IMGPROC_STATUS)>>8) & 0xff) { //Find out if there are words to read while ((IORD(0x42000, EEE_IMGPROC_STATUS) >> 8) & 0xff)
int word = IORD(0x42000,EEE_IMGPROC_MSG); //Get next word from message buffer { //Find out if there are words to read
int word = IORD(0x42000, EEE_IMGPROC_MSG); //Get next word from message buffer
if (fwrite(&word, 4, 1, ser) != 1) if (fwrite(&word, 4, 1, ser) != 1)
printf("Error writing to UART"); printf("Error writing to UART");
if (word == EEE_IMGPROC_MSG_START) //Newline on message identifier if (word == EEE_IMGPROC_MSG_START) //Newline on message identifier
printf("\n"); printf("\n");
printf("%08x ",word); printf("%08x ", word);
} }
//Update the bounding box colour //Update the bounding box colour
@ -268,44 +209,57 @@ int main()
//Process input commands //Process input commands
int in = getchar(); int in = getchar();
switch (in) { switch (in)
case 'e': { {
case 'e':
{
exposureTime += EXPOSURE_STEP; exposureTime += EXPOSURE_STEP;
OV8865SetExposure(exposureTime); OV8865SetExposure(exposureTime);
printf("\nExposure = %x ", exposureTime); printf("\nExposure = %x ", exposureTime);
break;} break;
case 'd': { }
case 'd':
{
exposureTime -= EXPOSURE_STEP; exposureTime -= EXPOSURE_STEP;
OV8865SetExposure(exposureTime); OV8865SetExposure(exposureTime);
printf("\nExposure = %x ", exposureTime); printf("\nExposure = %x ", exposureTime);
break;} break;
case 't': { }
case 't':
{
gain += GAIN_STEP; gain += GAIN_STEP;
OV8865SetGain(gain); OV8865SetGain(gain);
printf("\nGain = %x ", gain); printf("\nGain = %x ", gain);
break;} break;
case 'g': { }
case 'g':
{
gain -= GAIN_STEP; gain -= GAIN_STEP;
OV8865SetGain(gain); OV8865SetGain(gain);
printf("\nGain = %x ", gain); printf("\nGain = %x ", gain);
break;} break;
case 'r': { }
current_focus += manual_focus_step; case 'r':
if(current_focus >1023) current_focus = 1023; {
OV8865_FOCUS_Move_to(current_focus); current_focus += manual_focus_step;
printf("\nFocus = %x ",current_focus); if (current_focus > 1023)
break;} current_focus = 1023;
case 'f': { OV8865_FOCUS_Move_to(current_focus);
if(current_focus > manual_focus_step) current_focus -= manual_focus_step; printf("\nFocus = %x ", current_focus);
OV8865_FOCUS_Move_to(current_focus); break;
printf("\nFocus = %x ",current_focus); }
break;} case 'f':
{
if (current_focus > manual_focus_step)
current_focus -= manual_focus_step;
OV8865_FOCUS_Move_to(current_focus);
printf("\nFocus = %x ", current_focus);
break;
}
} }
//Main loop delay //Main loop delay
usleep(10000); usleep(10000);
}; };
return 0; return 0;
} }