Skip to content

Commit b98a77b

Browse files
author
Clement Leger
committed
Add ihex2cyacd tools WIP
1 parent f573ff0 commit b98a77b

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

ihex2cyacd/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
BUILD_DIR := ./build
2+
SRC_DIR := ./src
3+
4+
SRC_FILES := $(wildcard $(SRC_DIR)/*.c)
5+
OBJ_FILES := $(subst $(SRC_DIR),$(BUILD_DIR),$(patsubst %.c,%.o,$(SRC_FILES)))
6+
HDR_FILES := $(wildcard $(SRC_DIR)/*.h)
7+
8+
9+
CFLAGS := -g -Wall -I$(BUILD_DIR)
10+
LFLAGS :=
11+
12+
all: ihex2cyacd
13+
14+
$(BUILD_DIR)/ihex2cyacd_cmdline.c: $(SRC_DIR)/ihex2cyacd.ggo
15+
gengetopt -i $< -F ihex2cyacd_cmdline --output-dir=$(BUILD_DIR)/ --func-name=ihex2cyacd_cmdline_parser -a ihex2cyacd_args_info
16+
17+
$(BUILD_DIR)/ihex2cyacd_cmdline.h: $(BUILD_DIR)/ihex2cyacd_cmdline.c
18+
19+
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c $(BUILD_DIR)/ihex2cyacd_cmdline.h
20+
@mkdir -p $(BUILD_DIR)
21+
$(CC) -c -o $@ $< $(CFLAGS)
22+
23+
$(BUILD_DIR)/%.o: $(BUILD_DIR)/%.c
24+
@mkdir -p $(BUILD_DIR)
25+
$(CC) -c -o $@ $^ $(CFLAGS)
26+
27+
ihex2cyacd: $(OBJ_FILES) $(BUILD_DIR)/ihex2cyacd_cmdline.o
28+
$(CC) -o $@ $^ $(CFLAGS) $(LFLAGS)
29+
30+
clean:
31+
rm -rf ihex2cyacd $(BUILD_DIR)

ihex2cyacd/src/ihex2cyacd.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <unistd.h>
5+
#include <fcntl.h>
6+
#include <inttypes.h>
7+
#include <errno.h>
8+
9+
#include <ihex2cyacd_cmdline.h>
10+
11+
#define MAX_IHEX_FILE_LENGTH 512
12+
13+
#ifdef DEBUG
14+
#define dbg_printf(fmt, args...) printf(fmt, ## args)
15+
#else
16+
#define dbg_printf(fmt, args...) /* Don't do anything in release builds */
17+
#endif
18+
19+
struct cyacd_header_info {
20+
uint32_t silicon_id;
21+
uint8_t silicon_rev;
22+
};
23+
24+
static struct cyacd_header_info header_infos[] =
25+
{
26+
[cpu_arg_CY8C41] = {0x04161193, 0x11},
27+
[cpu_arg_CY8C42] = {0x04C81193, 0x11},
28+
};
29+
30+
static struct ihex2cyacd_args_info args_info;
31+
32+
#define get_line_chars(__str, __out_str, __n) \
33+
__out_str[__n] = 0; \
34+
memcpy(__out_str, __str, __n); \
35+
__str += __n;
36+
37+
static int parse_ihex_line(const char *line, uint8_t *length, uint16_t *addr, uint8_t *type, uint8_t data[MAX_IHEX_FILE_LENGTH])
38+
{
39+
char tmp_buf[MAX_IHEX_FILE_LENGTH];
40+
int i;
41+
uint8_t crc, sum = 0;
42+
43+
get_line_chars(line, tmp_buf, 2);
44+
*length = strtol(tmp_buf, NULL, 16);
45+
sum += *length;
46+
47+
get_line_chars(line, tmp_buf, 4);
48+
*addr = strtol(tmp_buf, NULL, 16);
49+
sum += ((uint8_t) (*addr & 0xFF));
50+
sum += ((uint8_t) ((*addr) >> 8 & 0xFF));
51+
52+
get_line_chars(line, tmp_buf, 2);
53+
*type = strtol(tmp_buf, NULL, 16);
54+
sum += *type;
55+
56+
for( i = 0; i < *length; i++) {
57+
get_line_chars(line, tmp_buf, 2);
58+
data[i] = strtol(tmp_buf, NULL, 16);
59+
sum += data[i];
60+
}
61+
62+
get_line_chars(line, tmp_buf, 2);
63+
crc = strtol(tmp_buf, NULL, 16);
64+
65+
if (crc != (~sum - 1)) {
66+
printf("CRC failed\n");
67+
return 1;
68+
}
69+
70+
return 0;
71+
}
72+
73+
int main(int argc, char **argv)
74+
{
75+
uint32_t bootloader_text_rows;
76+
char line_ptr[MAX_IHEX_FILE_LENGTH];
77+
FILE *input_hex, *output_cyacd;
78+
struct cyacd_header_info *infos;
79+
size_t line_length = MAX_IHEX_FILE_LENGTH;
80+
81+
if (ihex2cyacd_cmdline_parser(argc, argv, &args_info) != 0) {
82+
return EXIT_FAILURE;
83+
}
84+
85+
input_hex = fopen(args_info.input_arg, "r");
86+
if (!input_hex) {
87+
printf("Failed to open input file %s\n", args_info.input_arg);
88+
return 1;
89+
}
90+
output_cyacd = fopen(args_info.output_arg, "w+");
91+
if (!output_cyacd) {
92+
printf("Failed to open output file %s\n", args_info.output_arg);
93+
return 1;
94+
}
95+
infos = &header_infos[args_info.cpu_arg];
96+
97+
bootloader_text_rows = args_info.bootloader_size_arg / args_info.flash_row_size_arg;
98+
99+
fprintf(output_cyacd, "%08x%02x00\r\n", infos->silicon_id, infos->silicon_rev);
100+
101+
while( getline(&line_ptr, &line_length, input_hex)) {
102+
103+
}
104+
105+
106+
return 0;
107+
}

ihex2cyacd/src/ihex2cyacd.ggo

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package "ihex2cyacd"
2+
version "0.1"
3+
purpose "Utility to create cyacd files from ihex files"
4+
usage "ihex2cyacd [options]"
5+
6+
description "ihex2cyacd is a an utility to create cyacd files"
7+
8+
option "input" i "Input ihex file" string required
9+
option "bootloader_size" b "Bootloader text size file" int required
10+
option "flash_row_size" r "Flash row size" int required
11+
option "output" o "Output cyacd file" string required
12+
option "cpu" c "CPU type" values="CY8C41","CY8C42" enum default="CY8C42" optional
13+

0 commit comments

Comments
 (0)