summaryrefslogtreecommitdiff
path: root/platform/wavelet3d/remote_jtag.hpp
blob: 0e842de8383f79940bf3569f0445f25e3336c0c7 (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
// https://github.com/pulp-platform/riscv-dbg/blob/master/tb/remote_bitbang/remote_bitbang.h
// See LICENSE.Berkeley for license details.

#ifndef REMOTE_JTAG_HPP
#define REMOTE_JTAG_HPP

#include <atomic>
#include <cstdint>
#include <condition_variable>
#include <mutex>
#include <thread>

class remote_jtag
{
	public:
		remote_jtag(std::uint16_t port);
		~remote_jtag();

		inline bool pending() noexcept
		{
			return this->readable.load(std::memory_order_relaxed);
		}

		inline bool alive() noexcept
		{
			return !this->dead.load(std::memory_order_relaxed);
		}

		template<typename F>
		void process
		(
			unsigned char *tck,
			unsigned char *tms,
        	unsigned char *tdi,
			unsigned char *trstn,
        	unsigned char *tdo,
			F            &&cycle
		);

	private:
		int                     socket_fd  = -1;
		std::atomic<bool>       readable   = false;
		std::atomic<bool>       dead       = false;
		char                    in_buffer[512];
		char                    out_buffer[512];
		ssize_t                 read_bytes = 0;
		unsigned                write_bytes = 0;
		std::mutex              buffer_lock;
		std::condition_variable processed_cond;
		std::thread             poll_thread;

		void poll_main();
};

template<typename F>
void remote_jtag::process
(
	unsigned char *tck,
	unsigned char *tms,
	unsigned char *tdi,
	unsigned char *trstn,
	unsigned char *tdo,
	F            &&cycle
)
{
	std::unique_lock<std::mutex> lock(this->buffer_lock);

	char command;

	if (this->read_bytes > 0) {
		this->write_bytes = 0;
		for (ssize_t i = 0; i < this->read_bytes; ++i) {
			char command = this->in_buffer[i];

			int dosend = 0;
			char tosend = '?';

			switch (command) {
				case 'B':
					break;
				case 'b':
					break;
				case 'r':
					*trstn = 1; //r-reset command deasserts TRST. See: openocd/blob/master/doc/manual/jtag/drivers/remote_bitbang.txt
					break; 
				case 's':
					*trstn = 1; //s-reset command deasserts TRST. See: openocd/blob/master/doc/manual/jtag/drivers/remote_bitbang.txt
					break;
				case 't':
					*trstn = 0; //t-reset command asserts TRST. See: openocd/blob/master/doc/manual/jtag/drivers/remote_bitbang.txt
					break;
				case 'u':
					*trstn = 0; //u-reset command asserts TRST. See: openocd/blob/master/doc/manual/jtag/drivers/remote_bitbang.txt
					break;
				case '0':
					*tck = 0;
					*tms = 0;
					*tdi = 0;
					break;
				case '1':
					*tck = 0;
					*tms = 0;
					*tdi = 1;
					break;
				case '2':
					*tck = 0;
					*tms = 1;
					*tdi = 0;
					break;
				case '3':
					*tck = 0;
					*tms = 1;
					*tdi = 1;
					break;
				case '4':
					*tck = 1;
					*tms = 0;
					*tdi = 0;
					break;
				case '5':
					*tck = 1;
					*tms = 0;
					*tdi = 1;
					break;
				case '6':
					*tck = 1;
					*tms = 1;
					*tdi = 0;
					break;
				case '7':
					*tck = 1;
					*tms = 1;
					*tdi = 1;
					break;
				case 'R':
					dosend = 1;
					tosend = *tdo ? '1' : '0';
					break;
				case 'Q':
					fprintf(stderr, "Remote end disconnected\n");
					this->dead.store(true, std::memory_order_relaxed);
					break;
				default:
					fprintf(stderr, "remote_bitbang got unsupported command '%c'\n", command);
					break;
			}

			if (dosend)
				this->out_buffer[this->write_bytes++] = tosend;

			cycle();
		}
	} else if (this->read_bytes < 0)
		fprintf(stderr, "jtag read() failed\n");

	this->readable.store(false, std::memory_order_relaxed);
	lock.unlock();

	this->processed_cond.notify_one();
}

#endif