static PyObject* logiRead(PyObject* self, PyObject* arg) { PyObject* transferTuple; unsigned int offset, size, i ; unsigned char type_size = 1 ; uint8_t * rx ; if(!PyArg_ParseTuple(arg, "ll|b", &offset, &size, &type_size)) return NULL; switch(type_size){ case 1: rx = (uint8_t *) malloc(size*type_size); logi_read(rx, size, offset); transferTuple = PyTuple_New(size); for(i=0;i<size;i++) PyTuple_SetItem(transferTuple, i, Py_BuildValue("i",((uint8_t *) rx)[i])); free(rx); break ; case 2: rx = (uint8_t *) malloc(size*type_size); logi_read(rx, size*2, offset); transferTuple = PyTuple_New(size); for(i=0;i<size;i++) PyTuple_SetItem(transferTuple, i, Py_BuildValue("i",((uint16_t *) rx)[i])); free(rx); break ; case 4: rx = (uint8_t *) malloc(size*type_size); logi_read(rx, size*4, offset); transferTuple = PyTuple_New(size); for(i=0;i<size;i++) PyTuple_SetItem(transferTuple, i, Py_BuildValue("i",((uint32_t *) rx)[i])); free(rx); break ; default : pabort("type_size argument can only be 1, 2, 4\n"); break; } return transferTuple ; }
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 ++ ; } }
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_rd(unsigned int addr){ unsigned short read_val; logi_read((unsigned char *) &read_val, (unsigned int)2, addr); printf("address: 0x%x Read Value: 0x%x \r\n", addr, read_val); return(1); }