void task_fs(){ #ifdef DEBUG_FS printl("in task_fs\n"); #endif init_fs(); MESSAGE message; memset(&message,0,sizeof(message)); while(TRUE){ send_receive(RECEIVE,ANY,&message); int source_pid=message.source_pid; int fd; switch(message.type){ case INFO_FS_CREATE: message.res_bool=do_create(&message); break; case INFO_FS_UNLINK: message.res_bool=do_unlink(&message); break; case INFO_FS_LS: message.res_int=do_ls(&message); break; case INFO_FS_OPEN: fd=do_open(&message); message.fd=fd; break; case INFO_FS_READ: do_read(&message); break; case INFO_FS_WRITE: do_write(&message); break; case INFO_FS_SEEK: do_seek(&message); break; case INFO_FS_CLOSE: message.res_int=do_close(&message); break; default: printl("\n\n\nunknown message type:%d\n",message.type); assert(FALSE,"unknown message type!"); } if(message.type!=INFO_SUSPEND_PROCESS){ send_receive(SEND,source_pid,&message); }else{ printl("inof_suspend_process\n"); } } #ifndef _FS_H_ #define _FS_H_ #endif /* _FS_H_ */ while(1) ; spin("never here"); }
/** * Read a byte from the compass' EEPROM * @param address EEPROM address to read * @param data Pointer to received data * @return True if transaction successful, otherwise false */ bool compass_read_eeprom(uint8_t address, uint8_t *data) { uint8_t tx_data[] = {COMPASS_READ_EEPROM, address}; bool send, receive; send = send_receive(0, sizeof(tx_data), NULL, tx_data); receive = send_receive(1, 0, data, NULL); return send && receive; }
/* * Задача опрашивает кнопки и генерит транзакции по SPI. */ void task_console (void *data) { unsigned up_pressed = 0, left_pressed = 0, select_pressed = 0; unsigned right_pressed = 0, down_pressed = 0; for (;;) { mdelay (20); if (! joystick_up ()) up_pressed = 0; else if (! up_pressed) { up_pressed = 1; /* Up */ send_receive ('U'); } if (! joystick_down ()) down_pressed = 0; else if (! down_pressed) { down_pressed = 1; /* Down */ send_receive ('D'); } if (! joystick_left ()) left_pressed = 0; else if (! left_pressed) { left_pressed = 1; /* Left */ send_receive ('L'); } if (! joystick_right ()) right_pressed = 0; else if (! right_pressed) { right_pressed = 1; /* Right. */ send_receive ('R'); } if (! joystick_select ()) select_pressed = 0; else if (! select_pressed) { select_pressed = 1; /* Select. */ //send_receive ('S'); spi_output_block (&spi, (unsigned short *)data_block, sizeof (data_block) / sizeof (unsigned) ); } } }
int parent_process(char *mqname, int read_pipe, int write_pipe, int child_pid) { mqd_t mqdes; char reply; int rval; mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0); if (mqdes == (mqd_t) - 1) { perror(ERROR_PREFIX "mq_open"); return PTS_UNRESOLVED; } // Tell child a message queue has been opened. rval = send_receive(read_pipe, write_pipe, 'a', &reply); if (rval) { return rval; } if (reply != 'b') { printf(ERROR_PREFIX "send_receive: " "expected a 'b'\n"); return PTS_UNRESOLVED; } if (mq_unlink(mqname) == 0) { rval = send_receive(read_pipe, write_pipe, 'c', &reply); if (rval) { return rval; } if (reply != 'd') { printf(ERROR_PREFIX "send_receive: " "expected a 'd'\n"); return PTS_UNRESOLVED; } if (mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0) != -1) { if (mq_unlink(mqname) != 0) { perror(ERROR_PREFIX "mq_unlink(2)"); return PTS_UNRESOLVED; } printf("Test PASSED\n"); return PTS_PASS; } else { printf ("mq_open may fail until the message queue is actually removed \n"); printf("Test PASSED\n"); return PTS_PASS; } } printf(ERROR_PREFIX "mq_unlink\n"); return PTS_UNRESOLVED; }
int reset_configuration_flash_address() { DEBUG("Reset flash address\n"); if (dev_fd == -1) return 1; // Debug mode return send_receive(LSC_INIT_ADDRESS, 0, DIRECTION_RECEIVE, 0, 0); }
int erase_user_flash() { DEBUG(fprintf(stderr, "Erase user flash\n")); if (dev_fd == -1) return 1; // Debug mode return send_receive(LSC_ERASE_TAG, 0, DIRECTION_RECEIVE, 0, 0); }
int enable_offline_configuration() { DEBUG(fprintf(stderr, "Enable offline configuration\n")); if (dev_fd == -1) return 1; // Debug mode return send_receive(ISC_ENABLE, 0x080000, DIRECTION_RECEIVE, 0, 0); /* TODO: special command for i2c */ }
int program_configuration_flash(uint8_t *data, int data_len) { DEBUG(fprintf(stderr, "Program flash\n")); if (dev_fd == -1) return 1; // Debug mode return send_receive(LSC_PROG_INCR_NV, 1, DIRECTION_SEND, data, data_len); }
int program_feature_bits(uint8_t *feature_bits) { DEBUG(fprintf(stderr, "Program feature bits\n")); if (dev_fd == -1) return 1; // Debug mode return send_receive(LSC_PROG_FEABITS, 0, DIRECTION_SEND, feature_bits, 2); }
int program_done() { DEBUG(fprintf(stderr, "Program DONE\n")); if (dev_fd == -1) return 1; // Debug mode return send_receive(ISC_PROGRAM_DONE, 0, DIRECTION_RECEIVE, 0, 0); }
/** * Clock out two bytes to read data from compass in continuous and query modes * @param data Pointer to received heading data * @return True if transaction successful, otherwise false */ bool compass_read(uint16_t *data) { uint8_t rx_data[2]; bool result = send_receive(2, 0, rx_data, NULL); *data = rx_data[1] | (rx_data[0] << 8); return result; }
int refresh() { DEBUG(fprintf(stderr, "Refresh device\n")); if (dev_fd == -1) return 1; // Debug mode return send_receive(LSC_REFRESH, 0, DIRECTION_RECEIVE, 0, 0); }
int program_feature_row(uint8_t *feature_row) { DEBUG(fprintf(stderr, "Program feature row\n")); if (dev_fd == -1) return 1; // Debug mode return send_receive(LSC_PROG_FEATURE, 0, DIRECTION_SEND, feature_row, 8); }
static void init_fs(){ /* u8 fsbuf[SECTOR_SIZE]; */ MESSAGE message; message.type=INFO_FS_DEVICE; message.device=ROOT_DEVICE; assert(dd_map[DRIVER(ROOT_DEVICE)].driver_pid!=PID_INVALID,""); send_receive(BOTH,dd_map[DRIVER(ROOT_DEVICE)].driver_pid,&message); //如果系统已经是要求的系统,就不需要在格式化系统了 get_super_block(ROOT_DEVICE,&super_block); /* if(super_block.magic!=MAGIC_V1) */{ mkfs(); get_super_block(ROOT_DEVICE,&super_block); } init_inode_table(); init_file_descriptor_table(); #ifdef DEBUG_FS //write test /* memset(fsbuf,0x23,SECTOR_SIZE); */ /* WRITE_SECTOR(ROOT_DEVICE,fsbuf,1); */ //read test u8 fsbuf[SECTOR_SIZE]; READ_SECTOR(ROOT_DEVICE,fsbuf,1); printl("read test:\nfsbuf[0]=%x fsbuf[1]=%x fsbuf[2]=%x fsbuf[3]=%x\n",fsbuf[0],fsbuf[1],fsbuf[2],fsbuf[3]); #endif }
/** * Get data from compass by sending an 'A' command. * @param data Pointer to received heading data * @return True if transaction successful, otherwise false */ bool compass_get_data(uint16_t *data) { uint8_t rx_data[2]; uint8_t tx_data[] = {COMPASS_GET_DATA}; bool result = send_receive(2, sizeof(tx_data), rx_data, tx_data); *data = rx_data[1] | (rx_data[0] << 8); return result; }
int program_user_code(uint32_t user_code) { uint8_t buffer[4]; DEBUG(fprintf(stderr, "Program user code\n")); if (dev_fd == -1) return 1; // Debug mode to_be_4bytes(user_code, buffer); return send_receive(ISC_PROGRAM_USERCODE, 0, DIRECTION_SEND, buffer, 4); }
int verify_configuration_flash(uint8_t *expected_data, int data_len) { uint8_t *data; int read_len; int status; uint32_t op; int read_idx, data_idx; DEBUG(fprintf(stderr, "Verify flash\n")); if (dev_fd == -1) return 1; // Debug mode if (data_len > MACHXO2_PAGE_SIZE) { if (mode == MODE_SPI) { read_len = data_len + MACHXO2_PAGE_SIZE; // One extra page read_idx = MACHXO2_PAGE_SIZE; } else { read_len = 2*MACHXO2_PAGE_SIZE + (data_len / MACHXO2_PAGE_SIZE) * 4 + data_len; read_idx = 2*MACHXO2_PAGE_SIZE; } op = data_len / MACHXO2_PAGE_SIZE + 1; } else { read_len = data_len; op = 1; read_idx = 0; } if (mode != MODE_I2C) op |= 0x100000; data = (uint8_t*)malloc(read_len); if (data == 0) { fprintf(stderr, "Malloc failed\n"); return 0; } status = send_receive(LSC_READ_INCR_NV, op, DIRECTION_RECEIVE, data, read_len); if (status != 1) return status; data_idx = 0; while (data_idx < data_len) { if (data[read_idx] != expected_data[data_idx]) { fprintf(stderr, "Verify failed at offset %d (%d) : found = %02x expected = %02x\n", data_idx, read_idx, data[read_idx], expected_data[data_idx]); return 0; // differs } data_idx++; read_idx++; if (((data_idx % MACHXO2_PAGE_SIZE) == 0) && (mode == MODE_I2C)) read_idx += 4; } return 1; }
static void mkfs(){ MESSAGE message; HD_PART_INFO part_info; message.type=INFO_FS_IOCTL; message.subtype=DIOCTL_GET_PART_INFO; message.device=ROOT_DEVICE;//-------------需要指定到具体分区-------------------- message.arg_pointer=(void*)&part_info; message.source_pid=TASK_FS; assert(dd_map[DRIVER(message.device)].driver_pid!=PID_INVALID,"driver not exist!"); send_receive(BOTH,dd_map[DRIVER(message.device)].driver_pid,&message); #ifdef DEBUG_FS printl("device=%d base=%d size=%d (in sector)\n",DEVICE(message.device),part_info.base,part_info.size); #endif SUPER_BLOCK sb; //super block int super_block_first_index=get_super_block_first_index(); init_super_block(&sb,super_block_first_index,part_info.size); #ifdef DEBUG_FS printl("init_super_block ok(start_sector=%d)\n",super_block_first_index); #endif //imap /* int imap_first_index=super_block_first_index+SUPER_SECTORS_LENGTH; */ int imap_first_index=get_imap_first_index(sb); int imap_sectors_length=get_imap_length(sb)/* sb.imap_sectors_length */; init_imap(imap_first_index,imap_sectors_length); #ifdef DEBUG_FS printl("init_imap ok(start_sector=%d)\n",imap_first_index); #endif //smap /* int smap_first_index=imap_first_index+imap_sectors_length; */ int smap_first_index=get_smap_first_index(sb); int smap_sectors_length=get_smap_length(sb)/* sb.smap_sectors_length */; init_smap(smap_first_index,smap_sectors_length); #ifdef DEBUG_FS printl("init_smap ok(start_sector=%d)\n",smap_first_index); #endif //inode /* int inode_first_index=smap_first_index+smap_sectors_length; */ int inode_first_index=get_inode_first_index(sb); /* int inode_sectors_length=get_inode_length(sb)/\* sb.inodes_sectors_length *\/; */ int root_dir_start_index=get_data_block_first_index(sb)/* sb.data_first_sector_index */; init_inode(inode_first_index,root_dir_start_index); #ifdef DEBUG_FS printl("init_inode ok(start_sector=%d)\n",inode_first_index); #endif //data /* int data_block_first_index=inode_first_index+inode_sectors_length; */ int data_block_first_index=get_data_block_first_index(sb); init_data_block(data_block_first_index); #ifdef DEBUG_FS printl("init_data_block ok(start_sector=%d)\n",data_block_first_index); #endif }
/* 功能: 等待硬盘中断 参数: (无) 返回值: (无) */ static void interrupt_wait(){ MESSAGE msg; msg.type=INFO_INT_HD; #ifdef DEBUG_HD printl("interrupt_wait mst.type=%d\n",msg.type); #endif send_receive(RECEIVE,INTERRUPT,&msg); }
int erase_flash() { int status; int i; DEBUG(fprintf(stderr, "Erase flash\n")); if (dev_fd == -1) return 1; // Debug mode status = send_receive(ISC_ERASE, ERASE_FEATURE_ROW | ERASE_CONFIGURATION | ERASE_USER_FLASH, DIRECTION_RECEIVE, 0, 0); return status; }
/* 功能: 处理硬盘相关请求的任务进程。初始化后,永久运行接收有关硬盘请求的信息 参数: (无) 返回值: (无) */ void task_hd(){ MESSAGE message; #ifdef DEBUG_HD printl("in task_hd\n"); #endif //初始化硬盘 init_hd(); while(1){ reset_message(&message); send_receive(RECEIVE,ANY,&message); int src=message.source_pid; switch(message.type){ case INFO_FS_OPEN: hd_open(&message); break; case INFO_FS_CLOSE: hd_close(&message); break; case INFO_FS_READ: case INFO_FS_WRITE: hd_rdwt(&message); break; case INFO_FS_IOCTL: hd_ioctl(&message); break; case INFO_FS_DEVICE: hd_device_info(&message); break; default: //panic("unkonw message type(%d) in task_hd",src); break; } #ifdef DEBUG_HD printl("in task_hd\n"); #endif /* message.source_pid=process2pid(p_process_ready); */ send_receive(SEND,src,&message); } }
int verify_feature_bits(uint8_t *expected_feature_bits) { uint8_t buffer[2]; int status; DEBUG(fprintf(stderr, "Verify feature bits\n")); if (dev_fd == -1) return 1; // Debug mode status = send_receive(LSC_READ_FEABITS, 0, DIRECTION_RECEIVE, buffer, 2); if (status != 1) return status; return buffer[0] == expected_feature_bits[0] && buffer[1] == expected_feature_bits[1]; }
int set_configuration_flash_address(uint16_t page_address, int is_user_flash) { uint8_t buffer[4]; uint32_t address = page_address; DEBUG(fprintf(stderr, "Set configuration flash address\n")); if (dev_fd == -1) return 1; // Debug mode if (is_user_flash) address |= 0x40000000; to_be_4bytes(address, buffer); return send_receive(LSC_WRITE_ADDRESS, 0, DIRECTION_SEND, buffer, 4); }
int check_device_id(uint32_t expected_id) { uint8_t buffer[4]; int status; DEBUG(fprintf(stderr, "Check device ID\n")); if (dev_fd == -1) return 1; // Debug mode status = send_receive(IDCODE_PUB, 0, DIRECTION_RECEIVE, buffer, 4); if (status != 1) return status; return be_4bytes(buffer) == expected_id; }
int read_busy_status() { uint8_t buffer[1]; int status; DEBUG(fprintf(stderr, "Read busy status\n")); if (dev_fd == -1) return 1; // Debug mode status = send_receive(LSC_CHECK_BUSY, 0, DIRECTION_RECEIVE, buffer, 1); if (status != 1) return 0; DEBUG(fprintf(stderr, "Status: %02x\n", buffer[0])); return buffer[0] != 0; }
int read_status_register() { uint8_t buffer[4]; uint32_t read_status; int status; // DEBUG(fprintf(stderr, "Read status register\n")); if (dev_fd == -1) return 1; // Debug mode status = send_receive(LSC_READ_STATUS, 0, DIRECTION_RECEIVE, buffer, 4); read_status = be_4bytes(buffer); DEBUG(fprintf(stderr, "Status: %04x %02x\n", read_status, buffer[2])); return READ_STATUS_BUSY(read_status) | READ_STATUS_FAIL(read_status); }
/* 注意:读取的起始位置是扇区的整数倍 */ int rw_sector(int type,int device,u64 position,int length,int pid,void *buffer){ MESSAGE message; message.type=type; message.device=device; message.position=position; message.length=length; message.arg_pointer=buffer; message.source_pid=pid; assert(dd_map[DRIVER(device)].driver_pid!=PID_INVALID,""); send_receive(BOTH,dd_map[DRIVER(device)].driver_pid,&message); return 0; }
/* * Main program entry point. */ int main (void) { /* Unlock CFGCON register. */ SYSKEY = 0; SYSKEY = 0xAA996655; SYSKEY = 0x556699AA; CFGCON &= (1 << 13); // clear IOLOCK /* Disable JTAG ports, to make more pins available. */ CFGCON &= (1 << 3); // clear JTAGEN /* Use all ports as digital. */ ANSELA = 0; ANSELB = 0; ANSELC = 0; // Activate all of the LED pins. LATBSET = MASKB_LED1; TRISBCLR = MASKB_LED1; LATASET = MASKA_LED2; TRISACLR = MASKA_LED2; USBDeviceInit(); //usb_device.c. Initializes USB module SFRs and firmware //variables to known states. PMCON = 0; for (;;) { // Check bus status and service USB interrupts. USBDeviceTasks(); // Interrupt or polling method. If using polling, must call // this function periodically. This function will take care // of processing and responding to SETUP transactions // (such as during the enumeration process when you first // plug in). USB hosts require that USB devices should accept // and process SETUP packets in a timely fashion. Therefore, // when using polling, this function should be called // frequently (such as once about every 100 microseconds) at any // time that a SETUP packet might reasonably be expected to // be sent by the host to your device. In most cases, the // USBDeviceTasks() function does not take very long to // execute (~50 instruction cycles) before it returns. // Application-specific tasks. // Blink the LEDs according to the USB device status blink_status(); // User Application USB tasks if (USBDeviceState >= CONFIGURED_STATE && ! (U1PWRC & PIC32_U1PWRC_USUSPEND)) { send_receive(); } } }
int child_process(char *mqname, int read_pipe, int write_pipe) { mqd_t mqdes; int rval; char reply; rval = send_receive(read_pipe, write_pipe, 0, &reply); if (rval) { return rval; } if (reply != 'a') { printf(ERROR_PREFIX "send_receive: " "expected an 'a'\n"); return PTS_UNRESOLVED; } mqdes = mq_open(mqname, O_RDWR, 0, 0); if (mqdes == (mqd_t)-1) { perror(ERROR_PREFIX "mq_open"); return PTS_UNRESOLVED; } rval = send_receive(read_pipe, write_pipe, 'b', NULL); return 0; }
int main(int argc, char *argv[]) { int optical_eye_fd = setup_optical_eye(DEVICE, BAUDRATE); int mode; char *explanation = "data readout"; // Set up stdin to enable manuel control of the communication: // Will take next step when a key has been pressed on the // keyboard. fcntl(F_SETFL, O_NONBLOCK); if (argc == 1) mode = MODE_DATA_READOUT; else { mode = (char)atoi(argv[1]); if (mode == MODE_DATA_READOUT) /* do nothing */; else if (mode == MODE_PROGRAMMING) explanation = "programming"; else if (mode == MODE_MANUFACTURER_SPECIFIC) explanation = "manufacturer specific"; else explanation = "unknown"; } printf("Setting mode to %d (%s)\n", (int)mode, explanation); send_receive(optical_eye_fd, "/?!\r\n"); char message[] = "\00605 \r\n"; switch (mode) { case MODE_DATA_READOUT: message[3] = '0'; break; case MODE_PROGRAMMING: message[3] = '1'; break; case MODE_MANUFACTURER_SPECIFIC: message[3] = '6'; break; default: message[3] = (char)(mode + '0'); } send_receive(optical_eye_fd, message); return 0; }