Merge pull request #37 from TitanMKD/master

read_spiflash fixed (host/fw)
This commit is contained in:
Michael Ossmann
2013-03-07 17:22:09 -08:00
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_request_status_t usb_vendor_request_read_spiflash(
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage) usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage)
{ {
uint32_t i;
uint32_t addr; uint32_t addr;
uint16_t len; 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; addr = (endpoint->setup.value << 16) | endpoint->setup.index;
len = endpoint->setup.length; len = endpoint->setup.length;
if ((len > W25Q80BV_PAGE_LEN) || (addr > W25Q80BV_NUM_BYTES) if ((len > W25Q80BV_PAGE_LEN) || (addr > W25Q80BV_NUM_BYTES)
|| ((addr + len) > W25Q80BV_NUM_BYTES)) { || ((addr + len) > W25Q80BV_NUM_BYTES)) {
return USB_REQUEST_STATUS_STALL; return USB_REQUEST_STATUS_STALL;
} else { } else {
//FIXME need implementation /* TODO flush SPIFI "cache" before to read the SPIFI memory */
//usb_endpoint_schedule(endpoint->in, &endpoint->buffer, len); 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); usb_endpoint_schedule_ack(endpoint->out);
return USB_REQUEST_STATUS_OK; return USB_REQUEST_STATUS_OK;
} }
} else { } else
{
return USB_REQUEST_STATUS_OK; return USB_REQUEST_STATUS_OK;
} }
} }

View File

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