static int write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, const u8 *data, const struct yaffs_ext_tags *tags) { unsigned int address; //printf("%s %d (data=%p tags=%p)\n", __func__, nand_chunk, data, tags); address = chunk_address(dev, nand_chunk); if(data) write_flash(data, NOR_CHUNK_DATA_SIZE, address); if(tags) { struct yaffs_packed_tags2_tags_only x; yaffs_pack_tags2_tags_only(&x, tags); write_flash(&x, NOR_CHUNK_TAGS_SIZE, address+NOR_CHUNK_DATA_SIZE); } return YAFFS_OK; }
uint8_t Init_Flash(){ read_flash(); if(!CheckMagicCode()){ reset_flash(); write_flash(); }else{ if(option.isISP == true){ option.isISP = false; write_flash(); ReinvokeISP(); } } return option.isOpposite; }
void check_sd_firmware() { int r; printf("Check SD\n"); f_mount(0, &fat); if ((r = f_open(&file, firmware_file, FA_READ)) == FR_OK) { printf("Flashing firmware...\n"); uint8_t buf[512]; unsigned int r = sizeof(buf); uint32_t address = USER_FLASH_START; while (r == sizeof(buf)) { if (f_read(&file, buf, sizeof(buf), &r) != FR_OK) { f_close(&file); return; } setleds((address - USER_FLASH_START) >> 15); printf("\t0x%lx\n", address); write_flash((void *) address, (char *)buf, sizeof(buf)); address += r; } f_close(&file); if (address > USER_FLASH_START) { printf("Complete!\n"); r = f_unlink(firmware_old); r = f_rename(firmware_file, firmware_old); } }
static void program_flash(cyg_addrword_t arg) { diag_printf("PROGRAM FLASH here!\n"); HAL_UCACHE_SYNC(); // ROM space is marked cacheable which causes problems! HAL_UCACHE_DISABLE(); // So, just disable caches. identify_FLASH(); diag_printf("About to program FLASH using data at %x..%x\n", flash_buffer, flash_buffer_end); diag_printf("*** Press RESET now to abort!\n"); cyg_thread_delay(5*100); diag_printf("\n"); diag_printf("... Erase sector\n"); if (erase_sector(ROM_address)) { diag_printf("... Programming FLASH\n"); while (flash_buffer < flash_buffer_end) { if (!write_flash(flash_buffer++, ROM_address++)) break; } } // Exit Program Mode switch (manuf_code) { case ATMEL_MANUF: FLASH[ATMEL_SEQ_ADD1] = ATMEL_START_CMD1; FLASH[ATMEL_SEQ_ADD2] = ATMEL_START_CMD2; FLASH[ATMEL_SEQ_ADD1] = ATMEL_STOP_CMD; break; case INTEL_MANUF: FLASH[0] = INTEL_STOP_CMD; break; } diag_printf("All done!\n"); cyg_test_exit(); }
void program_page(void) { char result = (char)STK_FAILED; int length = 256 * getch(); length += getch(); char memtype = getch(); // flash memory @here, (length) bytes if (memtype == 'F') { uint8_t result = write_flash(length); if (CRC_EOP == getch()) { sendCDCbyte(STK_INSYNC); sendCDCbyte(result); } else { ram.isp.error++; sendCDCbyte(STK_NOSYNC); } return; } if (memtype == 'E') { result = (char)write_eeprom(length); if (CRC_EOP == getch()) { sendCDCbyte(STK_INSYNC); sendCDCbyte(result); } else { ram.isp.error++; sendCDCbyte(STK_NOSYNC); } return; } sendCDCbyte(STK_FAILED); return; }
void DFU_transferComplete(CONTROL_TRANSFER *control) { if ((control->setup.bmRequestType & 0x7F) == 0x21) { switch(control->setup.bRequest) { case DFU_GETSTATUS: { current_state = DFU_status.bState; printf("new state is %d\n", current_state); if (current_state == dfuMANIFESTWAITRESET) { usb_disconnect(); printf("MANIFEST COMPLETE, usb disconnected\n"); } break; } case DFU_DNLOAD: { if (control->setup.wLength > 0) { printf("WRITE %p\n", flash_p); setleds(((uint32_t) (flash_p - 0x4000)) >> 15); // we must pass DFU_BLOCK_SIZE to write_flash for some reason, it does not flash if we pass a smaller length int r = write_flash((void *) flash_p, (char *) block_buffer, DFU_BLOCK_SIZE); // int r; // for (r = 0; r < control->setup.wLength; r++) // { // printf("0x%x ", flash_p[r]); // if ((r & 31) == 31) // printf("\n"); // } if (r == 0) { flash_p += control->setup.wLength; DFU_status.bState = dfuDNLOADIDLE; } else { printf("write flash error %d\n", r); DFU_status.bStatus = errPROG; DFU_status.bState = dfuERROR; } } else { current_state = dfuMANIFESTSYNC; DFU_status.bState = dfuMANIFESTWAITRESET; } break; } case DFU_UPLOAD: DFU_status.bState = dfuUPLOADIDLE; flash_p += control->setup.wLength; break; }
uint32_t write_storage(teDATASTORAGE stype, uint32_t addr, void *data, uint16_t size) { uint32_t ret_len; switch(stype) { case STORAGE_MAC: #ifndef __USE_EXT_EEPROM__ erase_storage(STORAGE_MAC); ret_len = write_flash(DEVICE_MAC_ADDR, data, 6); // internal data flash for configuration data (DAT0/1) #else //erase_storage(STORAGE_MAC); ret_len = write_eeprom(convert_eeprom_addr(DEVICE_MAC_ADDR), data, 6); // external eeprom for configuration data #ifdef _EEPROM_DEBUG_ dump_eeprom_block(convert_eeprom_addr(DEVICE_MAC_ADDR)); #endif #endif break; case STORAGE_CONFIG: #ifndef __USE_EXT_EEPROM__ // flash erase_storage(STORAGE_CONFIG); ret_len = write_flash(DEVICE_CONFIG_ADDR, data, size); // internal data flash for configuration data (DAT0/1) #else //erase_storage(STORAGE_CONFIG); ret_len = write_eeprom(convert_eeprom_addr(DEVICE_CONFIG_ADDR), data, size); // external eeprom for configuration data #ifdef _EEPROM_DEBUG_ dump_eeprom_block(convert_eeprom_addr(DEVICE_CONFIG_ADDR)); #endif #endif break; case STORAGE_APP_MAIN: ret_len = write_flash(addr, data, size); break; case STORAGE_APP_BACKUP: ret_len = write_flash(addr, data, size); break; default: break; } return ret_len; }
/** \brief This function can write the MAC address to the MACPHY * @param *dev Pointer to the PCI device of this MACPHY * @param *MACAdr Pointer to the buffer where the desired MAC address is * @return void I210_NO_ERROR or an error code */ static u32 write_mac_adr(struct device *dev, u8 *mac_adr) { u16 adr[3]; if (!dev || !mac_adr) return I210_INVALID_PARAM; /* Copy desired address into a local buffer to avoid alignment issues */ memcpy((u8*)adr, mac_adr, 6); return write_flash(dev, 0, 3, adr); }
void SetOpposite(){ if(option.isOpposite) option.isOpposite = false; else option.isOpposite = true; write_flash(); NVIC_SystemReset(); }
void save_data(uint8_t *data, uint32_t data_len, uint16_t block_number) { static uint32_t cnt = 0; int i; if(block_number == 1) { #if !defined(MULTIFLASH_ENABLE) //for(i = 0 ; i < FLASH_APP_PAGE; i++) { // erase_flash_page(APP_BASE + (FLASH_PAGE_SIZE * i)); //} for(i = 0 ; i < 24; i++) { erase_flash_block(APP_BASE + (BLOCK_SIZE * i)); } #else for(i = 0 ; i < flash.flash_app_page; i++) { erase_flash_page(flash.flash_app_base + (flash.flash_page_size * i)); } #endif } //DBG_PRINT(INFO_DBG, "#"); cnt += data_len; #if 0 if(data_len < TFTP_BLK_SIZE) { memset(data + data_len, 0xff, TFTP_BLK_SIZE - data_len); write_flash((uint32_t)g_write_point, data, TFTP_BLK_SIZE); //DBG_PRINT(INFO_DBG, "\r\nwrite flash %d bytes\r\n", cnt); #if !defined(MULTIFLASH_ENABLE) g_write_point = (uint8_t *)APP_BASE; #else g_write_point = (uint8_t *)flash.flash_app_base; #endif cnt = 0; } else #endif { write_flash((uint32_t)g_write_point, data, data_len); g_write_point += data_len; } }
void test_flash_write(unsigned long addr) { int i, j; unsigned short verify[16]; unsigned short test_pattern[] = { 0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888, 0x7777, 0x6666, 0x5555, 0x4444, 0x3333, 0x2222, 0x1111, 0x0000 }; //unsigned short test_pattern[] = { 0x11112222, 0x33334444, 0x55556666, 0x77778888, 0x99998888, 0x77776666, 0x55554444, 0x33332222}; /*print_string("Writing single word2flash..."); if (0 != single_word_program_flash(0x60, 0x1234)) print_string("failed\r\n"); else print_string("successful\r\n"); reset_flash(); print_string("Verifying write..."); unsigned short src = *( (unsigned short *) (FLASH_BASE + ALIGN(addr))); print_hex_unsigned(src); if (src != 0x1234) print_string("...failed\r\n"); else print_string("...successful\r\n"); */ //reset_flash(); int z; DELAY(20); print_string("Writing buffer2flash..."); print_hex_unsigned(addr); if (0 != write_flash(addr, test_pattern, 16)) print_string("failed\r\n"); else print_string("successful\r\n"); //reset_flash(); /*print_string("Verifying write..."); j = 0; read_flash(addr, verify, 16); for (i = 0; i < 16; i++) { if (verify[i] != test_pattern[i]) j = 1; print_hex_unsigned(verify[i]); print_string(":"); } if (j) print_string("...failed\r\n"); else print_string("...successful\r\n");*/ //reset_flash(); }
int write_IOstorage(void *data, uint16_t size) { uint32_t address; #if !defined(MULTIFLASH_ENABLE) address = IO_PAGE_ADDR; #else address = flash.io_page_addr; #endif erase_flash_page(address); return write_flash(address, data, size); }
// Copy filter chain to requested flash block // uint16_t *filters_buf => memory address to start reading the filter chain from // uint16_t *filters_count => memory address to read number of filters from // uint16_t block => block id to write to unsigned filter_chain_to_flash(uint16_t *filters_buf, uint16_t *filters_count, uint16_t block) { unsigned output; uint16_t sector; #if DEBUG==1 tty_writeln("Writing filter chain to flash"); #endif block = block*BLOCK_SIZE; sector = 11 + 2*(block > 32768 - BLOCK_SIZE); block = block % (32768 - BLOCK_SIZE); output = write_flash(filters_count, 1, sector, block, 0); if(output) return output; output = write_flash(filters_buf, (*filters_count)*8, sector, block + FILTER_COUNT_BLOCK_SIZE, 0); #if DEBUG==1 tty_writeln("Filter chain written"); #endif return output; }
int main(int argc, char **argv) { void *hnd; int ret = 1; int flags; /* Parse command line options */ flags = parse_parameters(argc, argv); /* Open the USB device */ hnd = open_ftdi_device(usb_vid, usb_pid, usb_interface, usb_serial); if (hnd == NULL) return 1; /* Trigger embedded monitor detection */ if (send_special_waveform(hnd) < 0) goto terminate; if (config_i2c(hnd) < 0) goto terminate; if (check_chipid(hnd) < 0) goto terminate; if (flags & FLAG_UNPROTECT) command_write_unprotect(hnd); if (flags & FLAG_ERASE || output_filename) command_erase(hnd, flash_size, 0); if (input_filename) { ret = read_flash(hnd, input_filename, 0, flash_size); if (ret) goto terminate; } if (output_filename) { ret = write_flash(hnd, output_filename, 0); if (ret) goto terminate; } /* Normal exit */ ret = 0; terminate: /* Close the FTDI USB handle */ ftdi_usb_close(hnd); ftdi_free(hnd); return ret; }
static unsigned long write_one_block(unsigned long *block, unsigned long length, unsigned long offset) { unsigned long block_addr = (unsigned long) block; unsigned long i = 0; static unsigned int hash; if (offset == 0) hash = 0; for (i = 0; i < length; i += 32, offset += 32, block_addr += 32) { write_flash(offset, (unsigned short *) block_addr); if (offset % 10 == 0) { print_progress(); } if (offset > hash * progress) { print_hash(); hash++; } } return offset; }
int main( void ) { // TODO: default implementation is bloated //clock_prescale_set( clock_div_1 ); // Allow user to see registers before any disruption bootLoaderInit(); initHardware(); // gives time for jumper pull-ups to stabilize while ( bootLoaderCondition() ) { // Run USB until we have some action to take and that transaction is complete uchar prevTxLen; do { prevTxLen = usbTxLen; wait_usb_interrupt(); } while ( !(prevCommand != cmd_info && usbTxLen == USBPID_NAK && prevTxLen != USBPID_NAK) ); // Stops once we have a command and we've just transmitted the final reply // back to host // Now we can ignore USB until our host program makes another request if ( prevCommand == cmd_erase ) erase_flash(); else if ( prevCommand == cmd_write ) write_flash(); else break; } leaveBootloader(); }
int main(int argc, char **argv) { int ser; struct stm32_def *chip; int ret = 1; int flags; /* Parse command line options */ flags = parse_parameters(argc, argv); if (i2c_adapter == INVALID_I2C_ADAPTER) { /* Open the serial port tty */ ser = open_serial(serial_port); } else { ser = open_i2c(i2c_adapter); } if (ser < 0) return 1; /* Trigger embedded monitor detection */ if (init_monitor(ser) < 0) goto terminate; chip = command_get_id(ser); if (!chip) goto terminate; command_get_commands(ser, chip); if (flags & FLAG_READ_UNPROTECT) command_read_unprotect(ser); if (flags & FLAG_UNPROTECT) command_write_unprotect(ser); if (flags & FLAG_ERASE || output_filename) { if (!strcmp("STM32L15", chip->name)) { /* Mass erase is not supported on STM32L15xx */ /* command_ext_erase(ser, ERASE_ALL, 0); */ int i, page_count = chip->flash_size / chip->page_size; for (i = 0; i < page_count; i += 128) { int count = MIN(128, page_count - i); ret = erase(ser, count, i); if (ret) goto terminate; } } else { ret = erase(ser, 0xFFFF, 0); if (ret) goto terminate; } } if (input_filename) { ret = read_flash(ser, chip, input_filename, 0, chip->flash_size); if (ret) goto terminate; } if (output_filename) { ret = write_flash(ser, chip, output_filename, 0); if (ret) goto terminate; } /* Run the program from flash */ if (flags & FLAG_GO) command_go(ser, chip->flash_start); /* Normal exit */ ret = 0; terminate: /* Close serial port */ close(ser); return ret; }
int write_or_verify_flash(int fd,ihex_recordset_t *ihex,int writeP) { int max=255; if (writeP) max=32; printf("max=%d\n",max); int i; int fail=0; for(i=0;i<ihex->ihrs_count;i++) if (ihex->ihrs_records[i].ihr_type==0x00) { if (fail) break; int j; // write 32 bytes at a time for(j=0;j<ihex->ihrs_records[i].ihr_length;j+=max) { // work out how big this piece is int length=max; if (j+length>ihex->ihrs_records[i].ihr_length) { // printf(" clipping read from $%02x\n",length); length=ihex->ihrs_records[i].ihr_length-j; } printf("\rRange $%04x - $%04x (len=$%02x)", ihex->ihrs_records[i].ihr_address+j, ihex->ihrs_records[i].ihr_address+j+length-1,length); fflush(stdout); if (writeP) { // Write to flash set_flash_addr(fd,ihex->ihrs_records[i].ihr_address+j); write_flash(fd,&ihex->ihrs_records[i].ihr_data[j],length); } // Read back from flash and verify. unsigned char buffer[length]; set_flash_addr(fd,ihex->ihrs_records[i].ihr_address+j); read_flash(fd,buffer,length); int k; for(k=0;k<length;k++) if (ihex->ihrs_records[i].ihr_data[j+k] !=buffer[k]) { // Verify error fprintf(stderr,"\nVerify error at $%04x" " : expected $%02x, but read $%02x\n", ihex->ihrs_records[i].ihr_address+j+k, ihex->ihrs_records[i].ihr_data[j+k],buffer[k]); fail=1; } } } printf("\n"); if (fail) { if (writeP) { write(fd,"0",1); exit(-4); } else return -1; } return 0; }
//------------------------------------------------------------------- // input: the string contain the command and its length //------------------------------------------------------------------- void process_cmd(char* cmd_str, unsigned char cmd_length) { if (strcmp(cmd_str,"get_pulse") == 0) { show_info(); } else if (strcmp(cmd_str, "cal_on")==0) { mixer_flags |= MIXER_CALIBRATING; //calibrate_on= 1; copter_config_data.mixer_calibrate = 1; tx_string("please type: save<enter> and turn off the copter!\n\r",52); // here is the procedure to do throttle calibration for brushless ESC // + Take all propellers off the copter. You don't want them to cut your fingers // + Power up the copter // + Plug in the programming dongle // + Type command: "cal_on" (without quote) and enter. Some motor may spin. // + Green LED will be slow flashing // + Type command: "save" (without quote) and enter // + Remove the programming dongle and turn off power to the copter // + Raise the throttle on the transmitter to the highest position // + Turn on copter. Green LED will be slow flashing indicating // copter is in throttle calibration mode. No motor supposes to spin. // + Wait until you hear the ESC sounds 2 beeps (exact tone depends on your ESC's) // + Move the throttle stick on TX to lowest position // + Wait until you hear 2 beeps and then a long confirm beep (depends on ESC) // + Plug in the dongle, run command "cal_off" enter, and "save" and enter (without quote) // + Re-program all gyro direction settings or any command you usually do when set up the copter // + Save the settings and then power cycle the copter. You now can arm/fly as normal. } else if (strcmp(cmd_str,"cal_off")==0) { mixer_flags &= ~MIXER_CALIBRATING; //calibrate_on= 0; copter_config_data.mixer_calibrate = 0; tx_string("please type: save<enter> and then power cycle the copter!\n\r",59); } else if (strcmp(cmd_str,"dis_input_on")==0) { mixer_flags |= MIXER_DISC_INPUT_ON; //disc_input = 1; } else if (strcmp(cmd_str,"dis_input_off")==0) { mixer_flags &= ~MIXER_DISC_INPUT_ON; //disc_input = 0; } else if (strcmp(cmd_str,"gyro_test_on")==0) { mixer_flags |= MIXER_GYRO_TEST_ON; } else if (strcmp(cmd_str,"gyro_test_off")==0) { mixer_flags &= ~MIXER_GYRO_TEST_ON; } else if (strcmp(cmd_str,"gyro_filter_on")==0) { mixer_flags |= MIXER_GYRO_FILTER_ON; } else if (strcmp(cmd_str,"gyro_filter_off")==0) { mixer_flags &= ~MIXER_GYRO_FILTER_ON; } else if (strcmp(cmd_str,"debug_on")==0) { mixer_flags |= MIXER_PRINT_DEBUG_ON; } else if (strcmp(cmd_str,"debug_off")==0) { mixer_flags &= ~MIXER_PRINT_DEBUG_ON; } else if (strstr(cmd_str,"gyro_gain")!=0) { copter_config_data.gyro_gain = parse_value(cmd_str, cmd_length); // debug printU16(copter_config_data.gyro_gain); tx_string("\n\r",2); } else if (strstr(cmd_str,"x_bias")!=0) { copter_config_data.gain_x_bias = parse_value(cmd_str, cmd_length); // debug printU16(copter_config_data.gain_x_bias); tx_string("\n\r",2); } else if (strstr(cmd_str,"y_bias")!=0) { copter_config_data.gain_y_bias = parse_value(cmd_str, cmd_length); // debug printU16(copter_config_data.gain_y_bias); tx_string("\n\r",2); } else if (strstr(cmd_str,"z_bias")!=0) { copter_config_data.gain_z_bias = parse_value(cmd_str, cmd_length); // debug printU16(copter_config_data.gain_z_bias); tx_string("\n\r",2); } else if (strstr(cmd_str,"yaw_subtrim")!=0) { copter_config_data.yaw_subtrim = parse_value(cmd_str, cmd_length); // debug printU16(copter_config_data.yaw_subtrim); tx_string("\n\r",2); } else if (strcmp(cmd_str,"flip_roll")==0) { copter_config_data.gyro_dir ^= ROLL_GYRO_DIR; } else if (strcmp(cmd_str,"flip_pitch")==0) { copter_config_data.gyro_dir ^= PITCH_GYRO_DIR; } else if (strcmp(cmd_str,"flip_yaw")==0) { copter_config_data.gyro_dir ^= YAW_GYRO_DIR; } else if (strcmp(cmd_str,"save")==0) { if (sizeof(copter_config_data)>64) /* size of flash segment */ { tx_string("config too big!\n\r",17); return; } write_flash(FLASH_CONFIG_ADDR, (char*)&copter_config_data, sizeof(copter_config_data)); } else if (strcmp(cmd_str,"read")==0) { read_config_from_flash(); } else if (strcmp(cmd_str,"clear")==0) { clear_stats(); } else if (strcmp(cmd_str,"kick_adc")==0) { start_adc(ADC_ROLL_INCH); } else { tx_string("error!\n\r",8); } }
int main (void) { uint32_t id, j; uint8_t buffer[256]; uint8_t rxbuffer[256]; // System initialization SystemInit(); // Power on inside serial flash power_on_flash(); // Reset value for (j = 0; j < 256; j++) { rxbuffer[j] = 0; buffer[j] = j; } // Read flash chip ID id = id; //avoid warning id = read_flash_id(); // Erase 1 sector flash data from 0x4000, and 1 sector size is 4KB sector_erase_flash(0x4000, 1); /* * The parameter "address" note: * * When the address range is from 0x00 to 0x1000 (NVDS area), the address must be 4 * integer times. * * When the address range is greater than or equal to 0x1000 (Code area), the * address must be 256 integer times. (Encryption request) * The parameter "size" note: * * When the address range is from 0x00 to 0x1000 (NVDS area), the size must be 4 * integer times and less than or equal to 256. * * When the address range is greater than or equal to 0x1000 (Code area), the * size must be 256 bytes. (Encryption request) */ write_flash(0x4000, (uint32_t *)buffer, 256); /* * The parameter "address" note: * * When the address range is from 0x00 to 0x1000 (NVDS area), the address must be 4 * integer times. * * When the address range is greater than or equal to 0x1000 (Code area), the * address must be 256 integer times. (Encryption request) * The parameter "size" note: * * When the address range is from 0x00 to 0x1000 (NVDS area), the size must be 4 * integer times and less than or equal to 256. * * When the address range is greater than or equal to 0x1000 (Code area), the * size must be 256 bytes integer times. (Encryption request) */ read_flash(0x4000, (uint32_t *)rxbuffer, 256); for (j = 0; j < 256; j++) { if (rxbuffer[j] != j) { // read flash data error while(1); } } while (1) /* Loop forever */ { } }
uint32_t IsConfig_Command(uint8_t *command, uint32_t size) { //Write Setting if (command[0] == 'W') { if (command[1] == 'S') { if (WS_Command(&command[2], size - 2)) SetOK(); else SetERR(); return 1; } else if (command[1] == 'C') { if (WC_Command(&command[2], size - 2)) SetOK(); else SetERR(); return 1; } else if (command[1] == 'O') { if (WO_Command(&command[2], size - 2)) SetOK(); else SetERR(); return 1; } else if (command[1] == 'H') { if (WH_Command(&command[2], size - 2)) SetOK(); else SetERR(); return 1; } } //Read Setting else if (command[0] == 'R') { if (command[1] == 'S') { RS_Command(ReadSetting, 30); return 1; } else if (command[1] == 'C') { RC_Command(ReadSetting, 30); return 1; } else if (command[1] == 'H') { RH_Command(&command[2], size - 2); return 1; } } //System else if (command[0] == 'S') { if (command[1] == 'R') { SetOK(); SendSerial(ReadSetting, 3); _delay_ms(100); NVIC_SystemReset(); } else if (command[1] == 'V') { write_flash(); SetOK(); return 1; } else if (command[1] == 'F') { reset_flash(); SetOK(); return 1; } } //Firmware else if (command[0] == 'F') { if (command[1] == 'U') { SetOK(); option.isISP = true; write_flash(); SendSerial(ReadSetting, 3); _delay_ms(100); NVIC_SystemReset(); //ReinvokeISP(); } else if (command[1] == 'V') { FV_Command(ReadSetting, 30); return 1; } } return 0; }
int main(int argc, char **argv) { // clear TODO list todo_list.write = 0; todo_list.read = 0; todo_list.verify = 0; /* First check the command line parameters and parse them. */ if(argc < 2 || argc > 4 || strncmp(argv[1], "--help", 6) == 0) { usage(); return(-1); } while(i < argc) { sscanf(argv[i], "%s ", argbuf); if(strncmp(argbuf, "-S", 2) == 0) { i++; hits = sscanf(argv[i], "%[^:]:%d", nRd_address, &nRd_port); if(hits == 2) printf("Trying to connect nRouted at %s port %d\n", nRd_address, nRd_port); bzero(argbuf, 64); } else if (strncmp(argbuf, "-w", 2) == 0) { todo_list.write = 1; i++; hexfile = fopen(argv[i], "r"); if(hexfile) { printf("Writing data from %s to FLASH\n", argv[i]); if(parse_hex()) { fclose(hexfile); } else { printf("Invalid HEX file: %s\n", argv[i]); fclose(hexfile); return (-1); } } else { printf("Failed to open file %s\n", argv[i]); return (-1); } bzero(argbuf, 64); } else if (strncmp(argbuf, "-r", 2) == 0) { if(todo_list.write) { printf("Select only one of the operations: read, write, verify\n"); return (-1); } todo_list.read = 1; i++; hexfile = fopen(argv[i], "w"); if(hexfile) { printf("Writing data from FLASH to %s\n", argv[i]); } else { printf("Failed to open file %s\n", argv[i]); return (-1); } bzero(argbuf, 64); } else if (strncmp(argbuf, "-l", 2) == 0) { printf("Loop mode.\n"); loop = 20; } else { hits = sscanf(argv[i], "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &(target[0]), &(target[1]), &(target[2]), &(target[3]), &(target[4]), &(target[5]), &(target[6]), &(target[7])); printf("Requesting SSI data from: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", target[0], target[1], target[2], target[3], target[4], target[5], target[6], target[7]); } bzero(argbuf, 64); i++; } printf("Trying to connect nRouted at %s port %d\n", nRd_address, nRd_port); /* Copying the Target address to the end of the nRouted Configuration packet, discovery packet and SSI request packet. */ for(i=0;i<8;i++) { sndbuf_conf[i+12] = target[i]; sndbuf[i+18] = target[i]; SSI_request[i+12] = target[i]; } // if((rval=libnrp_create_conf_pkt(sndbuf_conf, PROTO_UNDEFINED, target, NULL, ADDR_UNDEFINED, NULL, PORT_UNDEFINED)) < 0) if((rval=libnrp_create_conf_pkt(sndbuf_conf, PROTO_6LOWPAN, NULL, NULL, ADDR_UNDEFINED, NULL, PORT_UNDEFINED)) < 0) { printf("Failed to create an nRouted configuration packet.\n"); return(-1); } if((sockfd = libnrp_snd_conf(sndbuf_conf, rval, nRd_address, nRd_port)) < 0) { printf("Failed to send the configuration packet to nRouted.\n"); return(-1); } else { printf("nRouted configuration successful.\n"); } if(todo_list.write) { int errors; int page = 0; for(errors = 0; errors < 10; errors++) { page = write_flash(page); if(page > 0) { printf("Flash write complete\n"); reset(); break; } else { int errors2; printf("Flash write FAILED!\n"); if((rval=libnrp_create_conf_pkt(sndbuf_conf, PROTO_6LOWPAN, NULL, NULL, ADDR_UNDEFINED, NULL, PORT_UNDEFINED)) < 0) { printf("Failed to create an nRouted configuration packet.\n"); return(-1); } for(errors2 = 0; errors2 < 10; errors2++) { if((sockfd = libnrp_snd_conf(sndbuf_conf, rval, nRd_address, nRd_port)) < 0) { printf("Failed to send the configuration packet to nRouted.\n"); } else { printf("nRouted configuration successful.\n"); break; } } page *= -1; } } } else if(todo_list.read) { if(read_flash()) { printf("Flash read complete\n"); if(create_hex()) { printf("Flash data written to file\n"); reset(); return 1; } else { printf("Error writing to file"); reset(); return (-1); } } else { printf("Flash read FAILED!\n"); reset(); return (-1); } } else if(todo_list.verify) { } // Create test packet portn = 252; packet_len = libnrp_create_data_pkt_hdr(sndbuf, data, sizeof(data), PROTO_6LOWPAN, NULL, target, ADDR_IEEE_802_15_4_DEV_LONG, &portn, 253, NULL); while(loop) { loop--; /* Here we send the SSI discovery packet to the sensor device. */ write(sockfd, sndbuf, packet_len); pfds.fd = (int)(sockfd); pfds.events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL; /* We try to read the answer and then process it. */ i=0; timed_out = 0; while(!timed_out) { if((rval = poll(&pfds, nfds, 1000)) == 0) { printf("Timed out SSI discovery reply.\n"); timed_out = -1; } else { recvbytes = read(sockfd, rbuf, 512); if(recvbytes>0) { printf("Received %d bytes ", recvbytes); libnrp_get_data(rbuf, recvbytes, buffer, &bytes); buffer[bytes]='\0'; printf("\n%d data bytes: %s\n", bytes, buffer); } } } } close(sockfd); return(1); }
void translate_wr( uint32_t offset, uint8_t** buff_adr, uint32_t length) { write_flash((unsigned *)&Memory[offset],(char*)&((*buff_adr)[0]),length); }
int main() { // DeInit NVIC and SCBNVIC NVIC_DeInit(); NVIC_SCBDeInit(); /* Configure the NVIC Preemption Priority Bits: * two (2) bits of preemption priority, six (6) bits of sub-priority. * Since the Number of Bits used for Priority Levels is five (5), so the * actual bit number of sub-priority is three (3) */ NVIC_SetPriorityGrouping(0x05); NVIC_SetVTOR(0x00000000); #ifdef UARTDEBUG uart_init(); #endif BlockDevInit(); #ifdef UARTDEBUG if (1) { U32 size; BlockDevGetSize(&size); DBG("Found SD card of size %d", size); BlockDevGetBlockLength(&size); DBG("block length %d", size); } #endif if (bootloader_button_pressed() || (user_code_present() == 0)) { DBG("entering bootloader"); init_usb_msc_device(); for (;usb_msc_not_ejected();) USBHwISR(); DBG("usb ejected, rebooting"); USBHwConnect(FALSE); spi_close(); } else { if ((r = f_mount(0, &fatfs)) == FR_OK) { if ((r = f_open(&f, "/firmware.bin", FA_READ | FA_OPEN_EXISTING)) == FR_OK) { unsigned int fs = f_size(&f); DBG("found firmware.bin with %u bytes", fs); if ((fs > 0) && (fs <= USER_FLASH_SIZE)) { U8 buffer[FLASH_BUF_SIZE]; for (unsigned int i = 0; i < fs; i += FLASH_BUF_SIZE) { unsigned int j = FLASH_BUF_SIZE; if (i + j > fs) j = fs - i; DBG("writing %d-%d", i, i+j); if ((r = f_read(&f, buffer, j, &j)) == FR_OK) { // pad last block to a full sector size while (j < FLASH_BUF_SIZE) { buffer[j++] = 0xFF; } write_flash((unsigned int *) (USER_FLASH_START + i), (char *) &buffer, j); } else { DBG("read failed: %d", r); i = fs; } } r = f_close(&f); r = f_unlink("/firmware.bck"); r = f_rename("/firmware.bin", "/firmware.bck"); } } else { DBG("open \"/firmware.bin\" failed: %d", r); } #ifdef GENERATE_FIRMWARE_CUR if (f_open(&f, "/firmware.bck", FA_READ | FA_OPEN_EXISTING)) { f_close(&f); } else { // no firmware.bck, generate one! if (f_open(&f, "/firmware.bck", FA_WRITE | FA_CREATE_NEW) == FR_OK) { U8 *flash = (U8 *) USER_FLASH_START; f_close(&f); } } #endif // elm-chan's fatfs doesn't have an unmount function // f_umount(&fatfs); } else { DBG("mount failed: %d", r); } spi_close(); if (user_code_present()) { DBG("starting user code..."); execute_user_code(); } else { DBG("user code invalid, rebooting"); } } NVIC_SystemReset(); }
int main (void) { // Short circuit test /* DDRB |= (1 << 6) | (1 << 7); // LEDS DDRD = (1<<7) | (1<<6); // ROMSEL, R/W PORTD = (1<<7) | (1<<6); // ROMSEL, R/W while(1) { int i; for (i = 0; i < 8; i++) { DDRA = 0; PORTA = 0xFF; DDRF = 0; PORTF = 0xFF; PORTA &= ~(1<<i); DDRA |= 1<<i; LED_RED_OFF; LED_GREEN_OFF; _delay_ms(500); if ((PINA != PORTA) || (PINF != PORTF)) { LED_RED_ON; } else { LED_GREEN_ON; } _delay_ms(500); } for (i = 0; i < 8; i++) { DDRA = 0; PORTA = 0xFF; DDRF = 0; PORTF = 0xFF; PORTF &= ~(1<<i); DDRF |= 1<<i; LED_RED_OFF; LED_GREEN_OFF; _delay_ms(500); if ((PINA != PORTA) || (PINF != PORTF)) { LED_RED_ON; } else { LED_GREEN_ON; } _delay_ms(500); } } */ /* DDRB |= (1 << 6) | (1 << 7); // LEDS DDRD = (1<<7) | (1<<6); // ROMSEL, R/W PORTD = (1<<7) | (1<<6); // ROMSEL, R/W while(1) { int i; for (i = 0; i < 8; i++) { DDRC = 0; PORTC = 0xFF; PORTC &= ~(1<<i); DDRC |= 1<<i; LED_RED_OFF; LED_GREEN_OFF; _delay_ms(500); if ((PINC != PORTC)) { LED_RED_ON; } else { LED_GREEN_ON; } _delay_ms(500); } } */ sei(); USART_init(); USART_init2(); #ifdef UPDATE_CHR_MODULE unsigned int bd = (F_CPU / (16UL * 19200UL)) - 1; UBRR0L = bd & 0xFF; UBRR0H = bd >> 8; UBRR1L = bd & 0xFF; UBRR1H = bd >> 8; LED_RED_ON; LED_GREEN_ON; while(1) { } #endif init_ports(); LED_RED_OFF; LED_GREEN_OFF; // MMC1 test /* set_address(0x8000); PHI2_LOW; MODE_WRITE; PORTC = 0; while (1) { LED_GREEN_OFF; ROMSEL_HI; _delay_ms(200); LED_GREEN_ON; ROMSEL_LOW; _delay_ms(200); } */ /* write_prg_byte(0x8000, 0x80); write_prg_byte(0x8000, 0); write_prg_byte(0x8000, 0); write_prg_byte(0x8000, 1); write_prg_byte(0x8000, 1); write_prg_byte(0x8000, 0); write_prg_byte(0xe000, 0); write_prg_byte(0xe000, 0); write_prg_byte(0xe000, 0); write_prg_byte(0xe000, 0); write_prg_byte(0xe000, 0); _delay_ms(500); MODE_READ; //set_address(0x8000); PORTA = 0; while(1); */ comm_init(); comm_start(COMMAND_PRG_STARTED, 0); uint16_t address; uint16_t length; unsigned long int t = 0; char led_down = 0; int led_bright = 0; while (1) { TCCR1A |= (1<<COM1C1) | (1<<COM1B1) | (1<<WGM10); TCCR1B |= (1<<CS10); if (t++ >= 10000) { if (!led_down) { led_bright++; if (led_bright >= 110) led_down = 1; } else { led_bright--; if (!led_bright) led_down = 0; } if (led_bright >= 100) OCR1B = led_bright - 100; if (led_down) { int led_bright2 = 110-led_bright; if (led_bright2 <= 20) { if (led_bright2 > 10) led_bright2 = 20 - led_bright2; OCR1C = led_bright2*2; } } t = 0; } if (comm_recv_done) { t = led_down = led_bright = 0; TCCR1A = OCR1B = OCR1C = 0; switch (comm_recv_command) { case COMMAND_PRG_INIT: comm_start(COMMAND_PRG_STARTED, 0); break; case COMMAND_PRG_READ_REQUEST: address = recv_buffer[0] | ((uint16_t)recv_buffer[1]<<8); length = recv_buffer[2] | ((uint16_t)recv_buffer[3]<<8); read_prg_send(address, length); break; case COMMAND_PRG_WRITE_REQUEST: address = recv_buffer[0] | ((uint16_t)recv_buffer[1]<<8); length = recv_buffer[2] | ((uint16_t)recv_buffer[3]<<8); write_prg(address, length, (uint8_t*)&recv_buffer[4]); comm_start(COMMAND_PRG_WRITE_DONE, 0); break; case COMMAND_PHI2_INIT: phi2_init(); comm_start(COMMAND_PHI2_INIT_DONE, 0); break; case COMMAND_RESET: reset_phi2(); comm_start(COMMAND_RESET_ACK, 0); break; case COMMAND_EPROM_PREPARE: write_eprom_prepare(); break; case COMMAND_PRG_EPROM_WRITE_REQUEST: address = recv_buffer[0] | ((uint16_t)recv_buffer[1]<<8); length = recv_buffer[2] | ((uint16_t)recv_buffer[3]<<8); write_eprom(address, length, (uint8_t*)&recv_buffer[4]); comm_start(COMMAND_PRG_WRITE_DONE, 0); break; case COMMAND_PRG_FLASH_ERASE_REQUEST: if (erase_flash()) comm_start(COMMAND_PRG_WRITE_DONE, 0); break; case COMMAND_PRG_FLASH_WRITE_REQUEST: address = recv_buffer[0] | ((uint16_t)recv_buffer[1]<<8); length = recv_buffer[2] | ((uint16_t)recv_buffer[3]<<8); if (write_flash(address, length, (uint8_t*)&recv_buffer[4])) comm_start(COMMAND_PRG_WRITE_DONE, 0); break; case COMMAND_COOLGIRL_ERASE_SECTOR_REQUEST: if (erase_coolgirl_sector()) comm_start(COMMAND_PRG_WRITE_DONE, 0); break; case COMMAND_COOLGIRL_WRITE_REQUEST: address = recv_buffer[0] | ((uint16_t)recv_buffer[1]<<8); length = recv_buffer[2] | ((uint16_t)recv_buffer[3]<<8); if (write_coolgirl(address, length, (uint8_t*)&recv_buffer[4])) comm_start(COMMAND_PRG_WRITE_DONE, 0); break; } comm_recv_done = 0; } } }