mini-jtag: code style clean up

This commit is contained in:
Xiangfu 2012-09-25 22:28:26 +08:00
parent aeddee61ff
commit ee6066e225

View File

@ -5,107 +5,98 @@
// For details see the UNLICENSE file at the root of the source tree. // For details see the UNLICENSE file at the root of the source tree.
// //
#include <ftdi.h>
#include <usb.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "load-bits.h" #include "load-bits.h"
int read_section (FILE *bit_file, char *id, uint8_t **data, uint32_t *len) int read_section(FILE *bit_file, char *id, uint8_t **data, uint32_t *len)
{ {
uint8_t buf[4]; uint8_t buf[4];
int lenbytes; int lenbytes;
/* first read 1 bytes, the section key */ /* first read 1 bytes, the section key */
if (fread (buf, 1, 1, bit_file) != 1) if (fread(buf, 1, 1, bit_file) != 1)
return -1; return -1;
*id = buf[0]; *id = buf[0];
/* section 'e' has 4 bytes indicating section length */ /* section 'e' has 4 bytes indicating section length */
if (*id == 'e') if (*id == 'e')
lenbytes = 4; lenbytes = 4;
else else
lenbytes = 2; lenbytes = 2;
/* first read 1 bytes */ /* first read 1 bytes */
if (fread (buf, 1, lenbytes, bit_file) != lenbytes) if (fread(buf, 1, lenbytes, bit_file) != lenbytes)
return -1; return -1;
/* second and third is section length */ /* second and third is section length */
if (*id != 'e') if (*id != 'e')
*len = buf[0] << 8 | buf[1]; *len = buf[0] << 8 | buf[1];
else else
*len = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3]; *len = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
/* now allocate memory for data */ /* now allocate memory for data */
*data = malloc (*len); *data = malloc(*len);
if (fread (*data, 1, *len, bit_file) != *len) if (fread(*data, 1, *len, bit_file) != *len)
return -1; return -1;
return 0; return 0;
} }
int load_bits(FILE *bit_file, struct load_bits *bs) int load_bits(FILE *bit_file, struct load_bits *bs)
{ {
char sid = 0; char sid = 0;
uint8_t *sdata; uint8_t *sdata;
uint32_t slen; uint32_t slen;
uint8_t buf[128]; uint8_t buf[128];
uint8_t header[] = { uint8_t header[] = {
0x00, 0x09, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x00, 0x09, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0,
0x0f, 0xf0, 0x00, 0x00, 0x01, 0x0f, 0xf0, 0x00, 0x00, 0x01,
}; };
if (fread (buf, 1, sizeof (header), bit_file) != sizeof (header)) if (fread(buf, 1, sizeof (header), bit_file) != sizeof (header))
return -1;
if (memcmp (buf, header, sizeof (header)) != 0)
return -1;
/* printf("Valid bitfile header found.\n"); */
while (sid != 'e')
{
if (read_section (bit_file, &sid, &sdata, &slen) != 0)
return -1; return -1;
/* printf("Read section id=%c len=%d.\n", sid, slen); */ if (memcmp(buf, header, sizeof (header)) != 0)
return -1;
/* make sure that strings are terminated */ while (sid != 'e') {
if (sid != 'e') if (read_section(bit_file, &sid, &sdata, &slen) != 0)
sdata[slen-1] = '\0'; return -1;
switch (sid) /* make sure that strings are terminated */
{ if (sid != 'e')
case 'a': bs->design = (char *) sdata; break; sdata[slen-1] = '\0';
case 'b': bs->part_name = (char *) sdata; break;
case 'c': bs->date = (char *) sdata; break;
case 'd': bs->time = (char *) sdata; break;
case 'e': bs->data = sdata; bs->length = slen; break;
}
}
return 0; switch (sid) {
case 'a': bs->design = (char *)sdata; break;
case 'b': bs->part_name = (char *)sdata; break;
case 'c': bs->date = (char *)sdata; break;
case 'd': bs->time = (char *)sdata; break;
case 'e': bs->data = sdata; bs->length = slen; break;
}
}
return 0;
} }
void bits_free(struct load_bits *bs) void bits_free(struct load_bits *bs)
{ {
if (bs->design) if (bs->design)
free(bs->design); free(bs->design);
if (bs->part_name) if (bs->part_name)
free(bs->part_name); free(bs->part_name);
if (bs->date) if (bs->date)
free(bs->date); free(bs->date);
if (bs->time) if (bs->time)
free(bs->time); free(bs->time);
if (bs->data) if (bs->data)
free(bs->data); free(bs->data);
free (bs); free (bs);
} }