Skip to content

Commit f14e157

Browse files
author
Clément Léger
committed
Initial commit
0 parents  commit f14e157

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
HOST_BOOTLOADER_DIR := ./host_bootloader_src
2+
BUILD_DIR := ./build
3+
SRC_DIR := ./src
4+
5+
SRC_FILES := $(wildcard $(HOST_BOOTLOADER_DIR)/*.c)
6+
OBJ_FILES := $(subst $(HOST_BOOTLOADER_DIR),$(BUILD_DIR),$(patsubst %.c,%.o,$(SRC_FILES)))
7+
HDR_FILES := $(wildcard $(BUILD_DIR)/*.h)
8+
9+
ifeq ($(SRC_FILES),)
10+
dummy := $(error Please copy the host bootloader sources into $(HOST_BOOTLOADER_DIR))
11+
endif
12+
13+
CFLAGS := -I$(HOST_BOOTLOADER_DIR) -DCALL_CON= -g
14+
LFLAGS := -lrt
15+
16+
all: cyhostboot
17+
18+
$(BUILD_DIR)/%.o: $(HOST_BOOTLOADER_DIR)/%.c
19+
@mkdir -p $(BUILD_DIR)
20+
$(CC) -c -o $@ $^ $(CFLAGS)
21+
22+
cyhostboot: $(OBJ_FILES) $(SRC_DIR)/cyhostboot.c
23+
$(CC) -o $@ $^ $(CFLAGS) $(LFLAGS)
24+
25+
clean:
26+
rm -rf cyhostboot $(BUILD_DIR)

host_bootloader_src/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This source folder should contains the cypress host bootloader sources

src/cyhostboot.c

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <unistd.h>
5+
#include <fcntl.h>
6+
#include <termios.h>
7+
#include <time.h>
8+
#include <poll.h>
9+
#include <errno.h>
10+
11+
#include <cybtldr_api.h>
12+
#include <cybtldr_api2.h>
13+
14+
static char* g_tty_device = "/dev/ttyACM2";
15+
static int g_serial_fd = -1;
16+
17+
unsigned long long timespec_milliseconds(struct timespec *a)
18+
{
19+
return a->tv_sec*1000 + a->tv_nsec/1000000;
20+
}
21+
22+
static int serial_open()
23+
{
24+
struct termios tty;
25+
26+
printf("Opening serial %s\n", g_tty_device);
27+
28+
g_serial_fd = open(g_tty_device, O_RDWR | O_NOCTTY);
29+
if (g_serial_fd < 0) {
30+
printf("Failed to open serial: %s\n", strerror(errno));
31+
return 1;
32+
}
33+
34+
if(tcgetattr(g_serial_fd, &tty) < 0) {
35+
printf( "Failed to get serial port attribute\n");
36+
return 1;
37+
}
38+
39+
cfmakeraw(&tty);
40+
/* Set Baud Rate */
41+
cfsetospeed (&tty, (speed_t)B115200);
42+
cfsetispeed (&tty, (speed_t)B115200);
43+
44+
tcflush(g_serial_fd, TCIFLUSH);
45+
tcflush(g_serial_fd, TCOFLUSH);
46+
47+
if (tcsetattr(g_serial_fd, TCSANOW, &tty) < 0) {
48+
printf( "Failed to set serial port attribute\n");
49+
return 1;
50+
}
51+
52+
return CYRET_SUCCESS;
53+
}
54+
55+
static int serial_close()
56+
{
57+
printf("Closing serial\n");
58+
close(g_serial_fd);
59+
60+
return CYRET_SUCCESS;
61+
}
62+
63+
static int serial_read(unsigned char *bytes, int size)
64+
{
65+
struct timespec tp;
66+
unsigned long long start_milli = 0, end_milli = 0;
67+
ssize_t read_bytes;
68+
struct pollfd fds[1];
69+
int poll_ret;
70+
unsigned int cur_byte = 0;
71+
72+
while(1) {
73+
fds[0].revents = 0;
74+
fds[0].events = POLLIN | POLLPRI;
75+
fds[0].fd = g_serial_fd;
76+
77+
clock_gettime(CLOCK_MONOTONIC, &tp);
78+
end_milli = timespec_milliseconds(&tp);
79+
if (start_milli && (end_milli - start_milli) > 1000)
80+
return CYRET_SUCCESS;
81+
82+
poll_ret = poll(fds, 1, 0);
83+
if (poll_ret == 0) {
84+
continue;
85+
} else if (poll_ret < 0) {
86+
printf("Poll error: %s\n", strerror(errno));
87+
return 1;
88+
}
89+
90+
clock_gettime(CLOCK_MONOTONIC, &tp);
91+
start_milli = timespec_milliseconds(&tp);
92+
93+
read_bytes = read(g_serial_fd, &bytes[cur_byte++], 1);
94+
}
95+
96+
return CYRET_SUCCESS;
97+
}
98+
99+
static int serial_write(unsigned char *bytes, int size)
100+
{
101+
int i;
102+
ssize_t write_bytes;
103+
104+
printf("Serial: writing %d bytes to bootloader\n", size);
105+
write_bytes = write(g_serial_fd, bytes, size);
106+
if (write_bytes != size) {
107+
printf("Error when writing bytes\n");
108+
return 1;
109+
}
110+
return CYRET_SUCCESS;
111+
}
112+
113+
114+
CyBtldr_CommunicationsData serial_coms = {
115+
.OpenConnection = serial_open,
116+
.CloseConnection = serial_close,
117+
.ReadData = serial_read,
118+
.WriteData = serial_write,
119+
.MaxTransferSize = 256,
120+
};
121+
122+
123+
void serial_progress_update(unsigned char arrayId, unsigned short rowNum)
124+
{
125+
printf("Flashing array_id %d, row_num %d\n", arrayId, rowNum);
126+
}
127+
128+
int main(int argc, char **argv)
129+
{
130+
int ret;
131+
g_tty_device = argv[2];
132+
133+
printf("Starting programming\n");
134+
ret = CyBtldr_RunAction(PROGRAM, argv[1], NULL, 1, &serial_coms, serial_progress_update);
135+
if (ret != CYRET_SUCCESS) {
136+
printf("Programming failed: %d\n", ret);
137+
return 1;
138+
}
139+
printf("Starting verifying\n");
140+
ret = CyBtldr_RunAction(VERIFY, argv[1], NULL, 1, &serial_coms, serial_progress_update);
141+
if (ret != CYRET_SUCCESS) {
142+
printf("Programming failed: %d\n", ret);
143+
return 1;
144+
}
145+
return 0;
146+
}

src/cyhostboot.ggo

Whitespace-only changes.

0 commit comments

Comments
 (0)