void loadSensorConfig(char conf[][2], unsigned int addr, unsigned int conf_size){ unsigned int i=0 ; unsigned short buffer ; buffer = 0x4201; logi_write((unsigned char *) &buffer, 2, addr+1); buffer = 0x4200; logi_write((unsigned char *) &buffer, 2, addr+1); buffer = 0x0001 ; while( conf[i][0] != 0xFF){ printf("waiting for master not busy \n"); while((buffer & 0x01) != 0x00 && (buffer & 0x02) != 0x02 ){ logi_read((unsigned char *)&buffer, 2, addr+1); //printf("%x\n", buffer); } printf("master free \n"); //printf("0x%04x\n", buffer); if((buffer & 0x02) == 0x02){ printf("NACK error !\n"); buffer = 0x4201; logi_write((unsigned char *)&buffer, 2, addr+1); buffer = 0x4200; logi_write((unsigned char *)&buffer, 2, addr+1); buffer = 0x0001 ; i -- ; continue ; } //printf("sending config \n"); printf("0x%02x , 0x%02x \n", conf[i][0], conf[i][1]); buffer = 0x4200; logi_write((unsigned char *)&buffer, 2, addr+1); // disable master buffer = (unsigned short) conf[i][0] ; // write to fifo logi_write((unsigned char *)&buffer, 2, addr); buffer = (unsigned short) conf[i][1] ; logi_write((unsigned char *)&buffer, 2, addr); buffer = 0x4202; logi_write((unsigned char *)&buffer, 2, addr+1); //enable master buffer = 0x0001 ; if(i == 0) sleep(1); i ++ ; } }
static PyObject* logiWrite(PyObject* self, PyObject* arg) { PyObject* transferTuple; unsigned int offset, returnVal ; unsigned char type_size = 1 ; if(!PyArg_ParseTuple(arg, "lO|b", &offset, &transferTuple, &type_size)) return NULL; if(!PyTuple_Check(transferTuple)) pabort("Only accepts a single tuple as an argument\n"); if(type_size != 1 && type_size != 2 && type_size != 4) pabort("type_size argument can only be 1, 2, 4\n"); uint32_t tupleSize = PyTuple_Size(transferTuple); uint8_t tx[tupleSize*type_size]; PyObject* tempItem; uint32_t i=0; while(i < tupleSize) { tempItem = PyTuple_GetItem(transferTuple, i); // if(!PyInt_Check(tempItem)) { pabort("non-integer contained in tuple\n"); } switch(type_size){ case 1: tx[i] = (uint8_t)PyInt_AsSsize_t(tempItem); break ; case 2: ((uint16_t*)tx)[i] = (uint16_t)PyInt_AsSsize_t(tempItem); break ; case 4: ((uint32_t*)tx)[i] = (uint32_t)PyInt_AsSsize_t(tempItem); break ; default: break ; } i++; } returnVal = logi_write(tx, tupleSize*type_size, offset); return Py_BuildValue("l", returnVal) ; }
int grab_frame(void){ unsigned short cmd_buffer[8] ; unsigned short vsync1, vsync2 ; FILE * yuv_fd, * jpeg_fd ; int i,j, res, inc ; unsigned int nbFrames = 1 ; unsigned int pos = 0 ; unsigned int nb = 0 ; unsigned char * image_buffer, * start_buffer, * end_ptr; //yuv frame buffer unsigned char *y_buffer ; unsigned short fifo_state, fifo_data ; unsigned int retry_counter = 0 ; unsigned int retry_pixel = 0 ; image_buffer = (unsigned char *) malloc((IMAGE_WIDTH*IMAGE_HEIGHT*NB_CHAN*4)/8+32);// 32 are the frame start, frame end indicators y_buffer = (unsigned char *) malloc(IMAGE_WIDTH*IMAGE_HEIGHT); if(image_buffer == NULL || y_buffer == NULL){ printf("allocation error ! \n"); return -1 ; } for(inc = 0 ; inc < (nbFrames && retry_counter < 5) ; ){ sprintf(jpeg_file_name, "./grabbed_frame%04d.jpg", inc); jpeg_fd = fopen(jpeg_file_name, "wb"); if(jpeg_fd == NULL){ printf("Error opening output file \n"); exit(EXIT_FAILURE); } sprintf(yuv_file_name, "./grabbed_frame%04d.yuv", inc); yuv_fd = fopen(yuv_file_name, "wb"); if(yuv_fd == NULL){ printf("Error opening output file \n"); exit(EXIT_FAILURE); } printf("issuing reset to fifo \n"); cmd_buffer[0] = 0 ; cmd_buffer[1] = 0 ; cmd_buffer[2] = 0 ; logi_write((unsigned char *) cmd_buffer, 6, FIFO_ADDR+FIFO_CMD_OFFSET); logi_read((unsigned char *) cmd_buffer, 6, FIFO_ADDR+FIFO_CMD_OFFSET); printf("fifo size : %d, free : %d, available : %d \n", cmd_buffer[0], cmd_buffer[1], cmd_buffer[2]); // reading and printing fifo states nb = 0 ; retry_pixel = 0 ; while(nb < (((IMAGE_WIDTH*IMAGE_HEIGHT*NB_CHAN)/8)*3)){ logi_read((unsigned char *) cmd_buffer, 6, FIFO_ADDR+FIFO_CMD_OFFSET); while(cmd_buffer[2] < 512){ logi_read((unsigned char *) cmd_buffer, 6, FIFO_ADDR+FIFO_CMD_OFFSET); //printf("%u\n", cmd_buffer[2]); } logi_read(&image_buffer[nb], 1024, FIFO_ADDR); nb += 1024 ; } if(retry_pixel == 10000){ printf("no camera detected !\n"); fclose(jpeg_fd); return -1 ; } printf("nb : %u \n", nb); start_buffer = image_buffer ; end_ptr = &image_buffer[((IMAGE_WIDTH*IMAGE_HEIGHT*NB_CHAN)/8)*3]; vsync1 = *((unsigned short *) start_buffer) ; vsync2 = *((unsigned short *) &start_buffer[((IMAGE_WIDTH*IMAGE_HEIGHT*NB_CHAN)/8)+2]) ; while(vsync1 != 0x55AA && vsync2 != 0x55AA && start_buffer < end_ptr){ start_buffer+=2 ; vsync1 = *((unsigned short *) start_buffer) ; vsync2 = *((unsigned short *) &start_buffer[((IMAGE_WIDTH*IMAGE_HEIGHT*NB_CHAN)/8)+2]) ; } if(vsync1 == 0x55AA && vsync2 == 0x55AA){ inc ++ ; printf("frame found !\n"); }else{ printf("sync not found !\n"); /*fclose(rgb_fd);*/ fclose(yuv_fd); fclose(jpeg_fd); retry_counter ++ ; continue ; } start_buffer += 2 ; bin_to_gray(start_buffer, y_buffer); fwrite(y_buffer, 1, IMAGE_WIDTH*IMAGE_HEIGHT*NB_CHAN, yuv_fd); write_jpegfile(y_buffer, IMAGE_WIDTH, IMAGE_HEIGHT, 1, jpeg_fd, 100); fclose(yuv_fd); fclose(jpeg_fd); } if(retry_counter == 5){ return -1 ; } return 0 ; }
int wb_wr(unsigned int addr, unsigned short val){ logi_write( (unsigned char *) &val, (unsigned int)2, addr); printf("address:%x Write Val:%x \r\n",addr, val); return(1); }