示例#1
0
int stlink2_swim_read_range(programmer_t *pgm, stm8_device_t *device, unsigned char *buffer, unsigned int start, unsigned int length) {
	stlink2_init_session(pgm);

	int i;
	for(i = 0; i < length; i += STLK_READ_BUFFER_SIZE) {
		// Determining current block start & size (relative to 0x8000)
		int block_start = start + i;
		int block_size = length - i;
		if(block_size > STLK_READ_BUFFER_SIZE) {
			block_size = STLK_READ_BUFFER_SIZE;
		}

		// Sending USB packet
		stlink2_cmd(pgm, 0xf40b, 6,
				HI(block_size), LO(block_size),
				0x00, 0x00, 
				HI(block_start), LO(block_start));
		TRY(128, (stlink2_get_status(pgm) & 0xffff) == 0);

		// Seems like we've got some bytes from stlink, downloading them
		stlink2_cmd(pgm, 0xf40c, 0);
		msg_recv(pgm, &(buffer[i]), block_size);
	}

	return(length);
}
示例#2
0
int stlink2_swim_write_range(programmer_t *pgm, stm8_device_t *device, unsigned char *buffer, unsigned int start, unsigned int length, const memtype_t memtype) {
	stlink2_init_session(pgm);

	stlink2_write_byte(pgm, 0x00, device->regs.CLK_CKDIVR);
    if(memtype == FLASH || memtype == EEPROM || memtype == OPT) {
        stlink2_write_and_read_byte(pgm, 0x00, device->regs.FLASH_IAPSR);
    }

    if(memtype == FLASH) {
        stlink2_write_byte(pgm, 0x56, device->regs.FLASH_PUKR);
        stlink2_write_byte(pgm, 0xae, device->regs.FLASH_PUKR); 
    }
    if(memtype == EEPROM || memtype == OPT) {
        stlink2_write_byte(pgm, 0xae, device->regs.FLASH_DUKR);
        stlink2_write_byte(pgm, 0x56, device->regs.FLASH_DUKR);
    }

    if(memtype == FLASH || memtype == EEPROM || memtype == OPT) {
        stlink2_write_and_read_byte(pgm, 0x56, device->regs.FLASH_IAPSR); // mov 0x56, FLASH_IAPSR
    }

	int i;
	int BLOCK_SIZE = device->flash_block_size;
	for(i = 0; i < length; i+=BLOCK_SIZE) {
        if(memtype == FLASH || memtype == EEPROM || memtype == OPT) {
            // stlink2_write_word(pgm, 0x01fe, device->regs.FLASH_CR2); // mov 0x01fe, FLASH_CR2
            stlink2_write_byte(pgm, 0x01, device->regs.FLASH_CR2); // mov 0x01fe, FLASH_CR2; 0x817e - enable write OPT bytes
            if(device->regs.FLASH_NCR2 != 0) { // Device have FLASH_NCR2 register
                stlink2_write_byte(pgm, 0xFE, device->regs.FLASH_NCR2);
            }
        }

		// The first 8 packet bytes are getting transmitted
		// with the same USB bulk transfer as the command itself
		msg_init(cmd_buf, 0xf40a);
		format_int(&(cmd_buf[2]), BLOCK_SIZE, 2, MP_BIG_ENDIAN);
		format_int(&(cmd_buf[6]), start + i, 2, MP_BIG_ENDIAN);
		memcpy(&(cmd_buf[8]), &(buffer[i]), 8);
		msg_send(pgm, cmd_buf, sizeof(cmd_buf));

		// Transmitting the rest
		msg_send(pgm, &(buffer[i + 8]), BLOCK_SIZE - 8);

		// Waiting for the transfer to process
		TRY(128, HI(stlink2_get_status(pgm)) == BLOCK_SIZE);

        if(memtype == FLASH || memtype == EEPROM || memtype == OPT) {
            stlink2_wait_until_transfer_completes(pgm, device);
        }
	}
    if(memtype == FLASH || memtype == EEPROM || memtype == OPT) {
        stlink2_write_and_read_byte(pgm, 0x56, device->regs.FLASH_IAPSR); // mov 0x56, FLASH_IAPSR
    }
	stlink2_write_byte(pgm, 0x00, 0x7f80);
	stlink2_write_byte(pgm, 0xb6, 0x7f80);
	stlink2_finish_session(pgm);
	return(length);
}