aboutsummaryrefslogtreecommitdiff
path: root/src/platforms/dev2/platform.c
blob: a62c49eb69af0022ea85613b2ee5c0c8b9be2343 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * This file is part of the Black Magic Debug project.
 *
 * Copyright (C) 2011  Black Sphere Technologies Ltd.
 * Written by Gareth McMullin <gareth@blacksphere.co.nz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "general.h"
#include "gdb_if.h"
#include "version.h"

#include <stdarg.h>
#include <assert.h>
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>

static int serial_fd = -1;
static int debug;

#define BUF_SIZE 4096
static uint8_t outbuf[BUF_SIZE];
static uint16_t bufptr = 0;

int serial_open(const char *name)
{
	int fd;
	/* Open. */
	fd = open(name, O_RDWR | O_NOCTTY);
	if (fd == -1)
		return -1;
	/* Setup raw. */
	struct termios tc;
	if(tcgetattr(fd, &tc) == -1) {
		close(fd);
		return -1;
	}
	tc.c_iflag &= ~(IGNPAR | PARMRK | ISTRIP | IGNBRK | BRKINT | IGNCR |
			ICRNL | INLCR | IXON | IXOFF | IXANY | IMAXBEL);
	tc.c_oflag &= ~(OPOST);
	tc.c_cflag &= ~(HUPCL | CSTOPB | PARENB | PARODD | CSIZE);
	tc.c_cflag |= CS8 | CLOCAL | CREAD;
	tc.c_lflag &= ~(ICANON | ECHO | ISIG | IEXTEN | NOFLSH | TOSTOP);
	tc.c_cc[VTIME] = 0;
	tc.c_cc[VMIN] = 1;
	tcflush(fd, TCIFLUSH);
	if(tcsetattr(fd, TCSANOW, &tc) == -1) {
		close(fd);
		return -1;
	}
	return fd;
}

void platform_init(int argc, char **argv)
{
	int c;
	char *serial = NULL;

	while((c = getopt(argc, argv, "ds:")) != -1) {
		switch(c) {
		case 'd':
			debug = 1;
			break;
		case 's':
			serial = optarg;
			break;
		}
	}
	if(optind != argc) {
		fprintf(stderr, "too many arguments\n");
		abort();
	}

	serial_fd = serial_open(serial);
	if(serial_fd == -1) {
		fprintf(stderr, "unable to open serial device: %s\n",
			strerror(errno));
		abort();
	}

	uint8_t init_str[] = {
		DEV2_OP_RESET_SYNC, DEV2_OP_RESET_SYNC, DEV2_OP_RESET_SYNC,
		DEV2_OP_RESET_SYNC, DEV2_OP_RESET_SYNC, DEV2_OP_RESET_SYNC,
		DEV2_OP_RESET_SYNC, DEV2_OP_RESET_SYNC, DEV2_OP_RESET_SYNC,
		DEV2_OP_RESET_SYNC, DEV2_OP_RESET_SYNC, DEV2_OP_RESET_SYNC,
		DEV2_OP_SETUP, 8, 0, 0, 0, 0,
		DEV2_OP_DIR, TMS_PIN | TCK_PIN | TDI_PIN
	};
	platform_buffer_write (init_str, sizeof (init_str));

	printf("\nBlack Magic Probe (" FIRMWARE_VERSION ")\n");
	printf("Copyright (C) 2015  Black Sphere Technologies Ltd.\n");
	printf("License GPLv3+: GNU GPL version 3 or later "
	       "<http://gnu.org/licenses/gpl.html>\n\n");

	assert(gdb_if_init() == 0);
}

void platform_buffer_flush(void)
{
	if(bufptr) {
		assert(write(serial_fd, outbuf, bufptr) == bufptr);
		bufptr = 0;
	}
}

int platform_buffer_write(const uint8_t *data, int size)
{
	if((bufptr + size) / BUF_SIZE > 0) platform_buffer_flush();
	memcpy(outbuf + bufptr, data, size);
	bufptr += size;
	return size;
}

int platform_buffer_read(uint8_t *data, int size)
{
	int index = 0;
	platform_buffer_flush();
	while((index += read(serial_fd, data + index, size - index)) != size);
	return size;
}

#ifdef WIN32
#warning "This vasprintf() is dubious!"
int vasprintf(char **strp, const char *fmt, va_list ap)
{
	int size = 128, ret = 0;

	*strp = malloc(size);
	while(*strp && ((ret = vsnprintf(*strp, size, fmt, ap)) == size)) 
		*strp = realloc(*strp, size <<= 1);
	
	return ret;
}
#endif

const char *platform_target_voltage(void)
{
	return "not supported";
}

void platform_delay(uint32_t delay)
{
	usleep(delay * 100000);
}

void platform_debug(const char *fmt, ...)
{
	if(debug) {
		va_list ap;
		va_start(ap, fmt);
		vprintf(fmt, ap);
		va_end(ap);
	}
}

static uint32_t timeout_time;
static uint32_t time_ms(void)
{
	struct timeval tv;
	gettimeofday(&tv, NULL);
	return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}

void platform_timeout_set(uint32_t ms)
{
	timeout_time = time_ms() + ms;
}

bool platform_timeout_is_expired(void)
{
	return time_ms() > timeout_time;
}