read_spiflash fixed. (Probably need to flush SPIFI "internal cache" before to read to be sure to read real SPIFI data).

This commit is contained in:
TitanMKD
2013-03-08 02:10:51 +01:00
parent 6dc82ae966
commit b1821ae8da
2 changed files with 53 additions and 19 deletions

View File

@ -541,22 +541,44 @@ usb_request_status_t usb_vendor_request_write_spiflash(
usb_request_status_t usb_vendor_request_read_spiflash(
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage)
{
uint32_t i;
uint32_t addr;
uint16_t len;
uint8_t* u8_addr_pt;
if (stage == USB_TRANSFER_STAGE_SETUP) {
if (stage == USB_TRANSFER_STAGE_SETUP)
{
addr = (endpoint->setup.value << 16) | endpoint->setup.index;
len = endpoint->setup.length;
if ((len > W25Q80BV_PAGE_LEN) || (addr > W25Q80BV_NUM_BYTES)
|| ((addr + len) > W25Q80BV_NUM_BYTES)) {
return USB_REQUEST_STATUS_STALL;
} else {
//FIXME need implementation
//usb_endpoint_schedule(endpoint->in, &endpoint->buffer, len);
/* TODO flush SPIFI "cache" before to read the SPIFI memory */
u8_addr_pt = (uint8_t*)addr;
for(i=0; i<len; i++)
{
spiflash_buffer[i] = u8_addr_pt[i];
}
usb_endpoint_schedule(endpoint->in, &spiflash_buffer[0], len);
return USB_REQUEST_STATUS_OK;
}
} else if (stage == USB_TRANSFER_STAGE_DATA)
{
addr = (endpoint->setup.value << 16) | endpoint->setup.index;
len = endpoint->setup.length;
/* This check is redundant but makes me feel better. */
if ((len > W25Q80BV_PAGE_LEN) || (addr > W25Q80BV_NUM_BYTES)
|| ((addr + len) > W25Q80BV_NUM_BYTES))
{
return USB_REQUEST_STATUS_STALL;
} else
{
usb_endpoint_schedule_ack(endpoint->out);
return USB_REQUEST_STATUS_OK;
}
} else {
} else
{
return USB_REQUEST_STATUS_OK;
}
}

View File

@ -41,7 +41,7 @@ static struct option long_options[] = {
{ 0, 0, 0, 0 },
};
int parse_int(char* s, uint32_t* const value)
int parse_u32(char* s, uint32_t* const value)
{
uint_fast8_t base = 10;
if (strlen(s) > 2) {
@ -57,9 +57,9 @@ int parse_int(char* s, uint32_t* const value)
}
char* s_end = s;
const long long_value = strtol(s, &s_end, base);
const uint32_t u32_value = strtoul(s, &s_end, base);
if ((s != s_end) && (*s_end == 0)) {
*value = long_value;
*value = u32_value;
return HACKRF_SUCCESS;
} else {
return HACKRF_ERROR_INVALID_PARAM;
@ -80,12 +80,13 @@ int main(int argc, char** argv)
int opt;
uint32_t address = 0;
uint32_t length = 0;
uint32_t tmp_length;
uint16_t xfer_len = 0;
const char* path = NULL;
hackrf_device* device = NULL;
int result = HACKRF_SUCCESS;
int option_index = 0;
uint8_t data[MAX_LENGTH];
static uint8_t data[MAX_LENGTH];
uint8_t* pdata = &data[0];
FILE* fd = NULL;
bool read = false;
@ -95,11 +96,11 @@ int main(int argc, char** argv)
&option_index)) != EOF) {
switch (opt) {
case 'a':
result = parse_int(optarg, &address);
result = parse_u32(optarg, &address);
break;
case 'l':
result = parse_int(optarg, &length);
result = parse_u32(optarg, &length);
break;
case 'r':
@ -113,6 +114,7 @@ int main(int argc, char** argv)
break;
default:
fprintf(stderr, "opt error: %d\n", opt);
usage();
return EXIT_FAILURE;
}
@ -179,8 +181,14 @@ int main(int argc, char** argv)
return EXIT_FAILURE;
}
if (read) {
result = hackrf_spiflash_read(device, address, length, data);
if (read)
{
tmp_length = length;
while (tmp_length)
{
xfer_len = (tmp_length > 256) ? 256 : tmp_length;
printf("Reading %d bytes from 0x%06x.\n", xfer_len, address);
result = hackrf_spiflash_read(device, address, xfer_len, pdata);
if (result != HACKRF_SUCCESS) {
fprintf(stderr, "hackrf_spiflash_read() failed: %s (%d)\n",
hackrf_error_name(result), result);
@ -188,6 +196,10 @@ int main(int argc, char** argv)
fd = NULL;
return EXIT_FAILURE;
}
address += xfer_len;
pdata += xfer_len;
tmp_length -= xfer_len;
}
const ssize_t bytes_written = fwrite(data, 1, length, fd);
if (bytes_written != length) {
fprintf(stderr, "Failed write to file (wrote %d bytes).\n",