GR712RC unit tests 1.0
Bare-metal C++ hardware tests for the PLATO DPU (IAA-CSIC, 2019)
Loading...
Searching...
No Matches
mcal_spw.h
Go to the documentation of this file.
1
7
8#ifndef MCAL_GRSPW_GR718RC_10_18_H_
9#define MCAL_GRSPW_GR718RC_10_18_H_
10
11#include <mcal_reg_access.h>
12
13namespace mcal
14{
15namespace spw
16{
17
18typedef void config_type;
19
20void init(const config_type*);
21
22template <typename port_type,// std::uint32_t
23 typename bval_type,// std::uint8_t
24 port_type port>
26{
27 public:
28 typedef std::uint32_t addr_type;
29 typedef std::uint32_t reg_type;
30 /*
31 typedef array<std::uint32_t,2> recv_descriptor_type;
32 typedef array<std::uint32_t,4> send_descriptor_type;
33
34 typedef array<std::uint32_t,16> header_buffer_type;
35 typedef array<std::uint32_t,256> data_buffer_type;
36 typedef array<std::uint32_t,4> packet_buffer_type;
37 */
38 typedef std::uint32_t *recv_descriptor_type;
39 typedef std::uint32_t *send_descriptor_type;
40
41 typedef std::uint32_t *header_buffer_type;
42 typedef std::uint32_t *data_buffer_type;
43 typedef std::uint32_t *packet_buffer_type;
44
45
49 spw_communication() : send_is_active(false)
50 {
51
52
53 // Set Packet Maximun Length. 16 MB = 16777216 = 0x1000000
54 // 1 KB = 1024 = 0x400
57 dma_maxlen_register,
58 static_cast<std::uint32_t>(UINT32_C(0x00000400))>::reg_set();
59
60 // Set the base addr of the Spacewire Recv Descriptor Table.
61 configure_recv_descriptors( number_of_recv_desciptor);
64 dma_rx_descriptor_register,
65 recv_descriptor_table_base>::reg_set();
66
67 // Set the base addr of the Spacewire Transmit Descriptor Table.
68 configure_send_descriptors( number_of_send_desciptor);
71 dma_tx_descriptor_register,
72 send_descriptor_table_base>::reg_set();
73
74 // Set Node Address to
77 node_addr_register,
78 spw_node_addr>::reg_set();
79
80 // Set Node Address to
83 baudrate_register,
84 0x0909>::reg_set();
85
88 ctrl_register,
89 0x0022>::reg_set();
90
91
92 }
93
95 {
96 return ( (get_link_status() >> 21) & 0x7 );
97 }
98
99 std::uint32_t get_link_status()
100 {
101 const std::uint32_t link_status = mcal::reg::access<addr_type,
102 reg_type,
103 status_register>::reg_get();
104
105 return link_status;
106 }
107
111 bool send(const bval_type byte_to_send)
112 {
113
114 if ( send_is_active )
115 {
116 return false;
117 }
118 else{
119 send_is_active = true;
120
121 // Get the descriptor in use.
122 const std::uint32_t send_descriptor_address = mcal::reg::access<addr_type,
123 reg_type,
124 dma_tx_descriptor_register>::reg_get();
125
126 const std::uint32_t desc_number = (send_descriptor_address & 0x3FF) >> 4;
127
128 // Fill the header and data.
129 send_descriptor_type desc_ptr = send_descriptor_table + 4*desc_number;
130
131 std::uint32_t * hbuff = reinterpret_cast<std::uint32_t *>(* (desc_ptr+1)); // Stores the pointer to the header_buffer.
132 std::uint32_t * dbuff = reinterpret_cast<std::uint32_t *>(* (desc_ptr+3));
133 std::uint32_t dbuff_size = (* (desc_ptr+2)); //0x400;Size of the data max.
134 std::uint32_t hbuff_size = 10;
135
136 for (std::uint32_t it = 0; it< 4; ++it)
137 {
138 *hbuff = 0xABACAD00 | it;
139 hbuff ++;
140 }
141
142 *dbuff = byte_to_send;
143
144 if (desc_number < (number_of_send_desciptor-1) )
145 (* desc_ptr ) = 0x0031000 | hbuff_size;
146 else
147 (* desc_ptr ) = 0x0033000 | hbuff_size;
148 /*
149 mcal::reg::dynamic_access<addr_type,
150 reg_type>::reg_set(data_buffer, static_cast<bval_type>(byte_to_send));
151*/
152
153 // enable the transmision of the actual descriptor.
155 reg_type,
156 dma_ctrl_register,
157 static_cast<std::uint32_t>(UINT32_C(0x00000001))>::reg_or();
158
159 send_is_active = false;
160 }
161 return true;
162 }
163
164 template<typename send_iterator_type>
165 bool send_n(send_iterator_type first,
166 send_iterator_type last)
167 {
168 bool send_result = true;
169
170 typedef typename
171 std::iterator_traits<send_iterator_type>::value_type
172 send_value_type;
173
174 const send_value_type value(*first);
175 std::uint32_t dbuff_size = sizeof(send_value_type)*(last-first);
176
177 send_is_active = true;
178
179 // Get the descriptor in use.
180 const std::uint32_t send_descriptor_address = mcal::reg::access<addr_type,
181 reg_type,
182 dma_tx_descriptor_register>::reg_get();
183
184 const std::uint32_t desc_number = (send_descriptor_address & 0x3FF) >> 4;
185
186 // Fill the descriptor data, control, header_ptr and data_ptr.
187 send_descriptor_type desc_ptr = send_descriptor_table + 4*desc_number;
188
189 std::uint32_t hbuff_size = 8;
190 (* (desc_ptr+1)) = reinterpret_cast<std::uint32_t>(first); //Header is the first 16Bytes of the buffer.
191 (* (desc_ptr+2)) = dbuff_size; //0x400;Size of the data max.
192 (* (desc_ptr+3)) = reinterpret_cast<std::uint32_t>(first)+4; //Data is the rest of the packet.
193
194 if (desc_number < (number_of_send_desciptor-1) )
195 (* desc_ptr ) = 0x0031000 | hbuff_size;
196 else
197 (* desc_ptr ) = 0x0033000 | hbuff_size;
198
199 send_is_active = true;
200
201 return send_result;
202 }
203
204 bool recv(bval_type &byte_to_recv)
205 {
206 /*
207 byte_to_recv = mcal::reg::access<addr_type,
208 reg_type,
209 input_data_register>::reg_get();
210
211*/
212 for (int i=0; i < 1000; ++i)
213 for (int j=0; j< 100; ++j)
214 {
216 }
217
218
219 return true;
220 }
221
222 //Checks if there is data to receive.
224 {
225 reg_type recv_desc_status = 0x123; /*mcal::reg::access<addr_type,
226 reg_type,
227 uart_status_register>::reg_get();*/
228 return (recv_desc_status & (0x1 << 0));
229 }
230
231
235 bool configure_send_descriptors(std::uint32_t number_descriptors)
236 {
237 std::uint32_t desc_number;
238 send_descriptor_type desc_it = send_descriptor_table;
239
240 desc_number = 0;
241 while( desc_number < (number_descriptors-1))
242 {
243 (* desc_it ) = 0x003000F;
244 (* (desc_it+1)) = header_buffer_base_addr + desc_number*header_max_length;
245 (* (desc_it+2)) = 0x4; //0x400;
246 (* (desc_it+3)) = data_buffer_base_addr + desc_number*data_max_length;
247
248 desc_it += 4;
249 desc_number ++;
250 }
251
252 (* desc_it ) = 0x003200F;
253 (* (desc_it+1)) = header_buffer_base_addr + desc_number*header_max_length;
254 (* (desc_it+2)) = 0x4; //0x400;
255 (* (desc_it+3)) = data_buffer_base_addr + desc_number*data_max_length;
256
257
258 return true;
259 }
260
261 // Esta funcion deberia hacer su trabajo mediante, iteradores.
262 // It should return the address of the recv_descritor table, after build it.
263 bool configure_recv_descriptors(std::uint32_t number_descriptors)
264 {
265
266 std::uint32_t it = 0;
267 std::uint32_t desc_number;
268 recv_descriptor_type desc_it = recv_descriptor_table;
269
270 desc_number = 0;
271 while( desc_number < (number_descriptors-1))
272 {
273 (* desc_it ) = 0x02000000;
274 (* (desc_it+1)) = packet_buffer_base_addr + desc_number*packet_max_length;
275
276 desc_it += 2;
277 desc_number ++;
278 }
279
280 (* desc_it ) = 0x003300F;
281 (* (desc_it+1)) = packet_buffer_base_addr + desc_number*packet_max_length;
282
283 return true;
284 }
285
286 private:
287
288 bool send_is_active;
289
290 static constexpr std::uint32_t packet_max_length = 0x400UL; // 1024 Bytes.
291 static constexpr std::uint32_t header_max_length = 0x10UL; // 16 Bytes.
292 static constexpr std::uint32_t data_max_length = 0x400UL; // 1024 Bytes.
293
294 static constexpr std::uint32_t recv_descriptor_table_base = 0x40001000; // Max Descriptor : 128*8 = 1024
295 static constexpr std::uint32_t send_descriptor_table_base = 0x40002000;
296 static constexpr std::uint32_t header_buffer_base_addr = 0x40010000;
297 static constexpr std::uint32_t data_buffer_base_addr = 0x40020000;
298 static constexpr std::uint32_t packet_buffer_base_addr = 0x40030000;
299
300 static constexpr std::uint32_t number_of_recv_desciptor = 5;
301 static constexpr std::uint32_t number_of_send_desciptor = 5;
302
303 static constexpr std::uint32_t spw_node_addr = 0xFE;
304
305 //Each of this should be build in base a fill using only the aligned part.
306 recv_descriptor_type recv_descriptor_table = reinterpret_cast<recv_descriptor_type>( recv_descriptor_table_base );
307 send_descriptor_type send_descriptor_table = reinterpret_cast<send_descriptor_type>( send_descriptor_table_base );
308 header_buffer_type header_buffer = reinterpret_cast<header_buffer_type>( header_buffer_base_addr );
309 data_buffer_type data_buffer = reinterpret_cast<data_buffer_type>( data_buffer_base_addr );
310 packet_buffer_type packet_buffer = reinterpret_cast<packet_buffer_type>( packet_buffer_base_addr );
311
312
313 static constexpr addr_type ctrl_register = addr_type(port + 0x00UL);
314 static constexpr addr_type status_register = addr_type(port + 0x04UL);
315
316 static constexpr addr_type node_addr_register = addr_type(port + 0x08UL);
317 static constexpr addr_type baudrate_register = addr_type(port + 0x0CUL);
318 static constexpr addr_type dest_key_register = addr_type(port + 0x10UL);
319 static constexpr addr_type time_register = addr_type(port + 0x14UL);
320
321 static constexpr addr_type dma_ctrl_register = addr_type(port + 0x20UL);
322 static constexpr addr_type dma_maxlen_register = addr_type(port + 0x24UL);
323 static constexpr addr_type dma_tx_descriptor_register = addr_type(port + 0x28UL);
324 static constexpr addr_type dma_rx_descriptor_register = addr_type(port + 0x2CUL);
325 static constexpr addr_type dma_addr_register = addr_type(port + 0x30UL);
326
327};
328
329/*
330extern spw_communication<std::uint32_t,
331 std::uint32_t,
332 mcal::reg::grspw0_base_address> the_spw0;
333*/
334}
335}
336
337#endif // MCAL_GRSPW_GR718RC_10_18_H_
bool configure_recv_descriptors(std::uint32_t number_descriptors)
Definition mcal_spw.h:263
spw_communication()
Init the Spacewire IP Core.
Definition mcal_spw.h:49
std::uint32_t * send_descriptor_type
Definition mcal_spw.h:39
bool send_n(send_iterator_type first, send_iterator_type last)
Definition mcal_spw.h:165
bool recv(bval_type &byte_to_recv)
Definition mcal_spw.h:204
std::uint32_t * data_buffer_type
Definition mcal_spw.h:42
bool configure_send_descriptors(std::uint32_t number_descriptors)
Definition mcal_spw.h:235
bool send(const bval_type byte_to_send)
Add data to the actual descriptor and enable the transfer.
Definition mcal_spw.h:111
std::uint32_t * recv_descriptor_type
Definition mcal_spw.h:38
std::uint32_t * packet_buffer_type
Definition mcal_spw.h:43
std::uint32_t get_link_status()
Definition mcal_spw.h:99
std::uint32_t * header_buffer_type
Definition mcal_spw.h:41
void nop()
Definition mcal_cpu.h:24
void config_type
Definition mcal_spw.h:18
void init(const config_type *)