Skip to content

Commit 11bf6c0

Browse files
authored
Merge pull request #117 from rgrr/bug/113-fix-rtt
Reworking SEGGER_RTT_BUFFER access
2 parents 767b0c8 + fa7c1a4 commit 11bf6c0

File tree

2 files changed

+84
-66
lines changed

2 files changed

+84
-66
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# ATTENTION: to get the version number & git hash into the image, cmake-create-* has to be invoked.
44
#
55
VERSION_MAJOR := 1
6-
VERSION_MINOR := 20
6+
VERSION_MINOR := 21
77

88
BUILD_DIR := _build
99
PROJECT := picoprobe

src/rtt_io.c

Lines changed: 83 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@
5959

6060
typedef uint32_t (*rtt_data_to_host)(const uint8_t *buf, uint32_t cnt);
6161

62+
typedef struct {
63+
uint32_t addr; // target address of this aUp[]
64+
SEGGER_RTT_BUFFER_UP aUp; // local copy of the target aUp[]
65+
} EXT_SEGGER_RTT_BUFFER_UP;
66+
67+
typedef struct {
68+
uint32_t addr; // target address of this aDown[]
69+
SEGGER_RTT_BUFFER_DOWN aDown; // local copy of the target aDown[]
70+
} EXT_SEGGER_RTT_BUFFER_DOWN;
71+
72+
6273
#define TARGET_RAM_START g_board_info.target_cfg->ram_regions[0].start
6374
#define TARGET_RAM_END g_board_info.target_cfg->ram_regions[0].end
6475

@@ -176,41 +187,54 @@ static uint32_t search_for_rtt_cb(uint32_t prev_rtt_cb)
176187

177188

178189

179-
static bool rtt_check_channel_from_target(uint32_t rtt_cb, uint16_t channel, SEGGER_RTT_BUFFER_UP *aUp, bool *found)
190+
static bool rtt_check_channel_from_target(uint32_t rtt_cb, uint16_t channel, EXT_SEGGER_RTT_BUFFER_UP *extRttBuf, bool *found)
191+
/**
192+
* Check if there is a valid buffer from target for this channel.
193+
*/
180194
{
181195
bool ok;
182196
int32_t buff_cnt;
183197

184198
*found = (rtt_cb >= TARGET_RAM_START && rtt_cb <= TARGET_RAM_END);
185199
ok = *found && swd_read_word(rtt_cb + offsetof(SEGGER_RTT_CB, MaxNumUpBuffers), (uint32_t *)&(buff_cnt));
186200
if (ok) {
201+
extRttBuf->addr = rtt_cb + offsetof(SEGGER_RTT_CB, aUp[channel]);
187202
*found = *found && (buff_cnt > channel);
188-
*found = *found && swd_read_memory(rtt_cb + offsetof(SEGGER_RTT_CB, aUp[channel]), (uint8_t *)aUp, sizeof(SEGGER_RTT_BUFFER_UP));
189-
*found = *found && (aUp->SizeOfBuffer > 0 && aUp->SizeOfBuffer < TARGET_RAM_END - TARGET_RAM_START);
190-
*found = *found && ((uint32_t)aUp->pBuffer >= TARGET_RAM_START && (uint32_t)aUp->pBuffer + aUp->SizeOfBuffer <= TARGET_RAM_END);
203+
*found = *found && swd_read_memory(extRttBuf->addr, (uint8_t *)&(extRttBuf->aUp), sizeof(extRttBuf->aUp));
204+
*found = *found && (extRttBuf->aUp.SizeOfBuffer > 0 && extRttBuf->aUp.SizeOfBuffer < TARGET_RAM_END - TARGET_RAM_START);
205+
*found = *found && ((uint32_t)extRttBuf->aUp.pBuffer >= TARGET_RAM_START && (uint32_t)extRttBuf->aUp.pBuffer + extRttBuf->aUp.SizeOfBuffer <= TARGET_RAM_END);
191206
if (*found) {
192-
picoprobe_info(" rtt_check_channel_from_target: %u %p %5u %5u %5u\n", channel, aUp->pBuffer, aUp->SizeOfBuffer, aUp->RdOff, aUp->WrOff);
207+
picoprobe_info(" rtt_check_channel_from_target: %u %p %5u %5u %5u\n", channel, extRttBuf->aUp.pBuffer, extRttBuf->aUp.SizeOfBuffer, extRttBuf->aUp.RdOff, extRttBuf->aUp.WrOff);
193208
}
194209
}
195210
return ok;
196211
} // rtt_check_channel_from_target
197212

198213

199214

200-
static bool rtt_check_channel_to_target(uint32_t rtt_cb, uint16_t channel, SEGGER_RTT_BUFFER_DOWN *aDown, bool *found)
215+
static bool rtt_check_channel_to_target(uint32_t rtt_cb, uint16_t channel, EXT_SEGGER_RTT_BUFFER_DOWN *extRttBuf, bool *found)
216+
/**
217+
* Check if there is a valid buffer to target for this channel.
218+
*
219+
* \note
220+
* Order of \a SEGGER_RTT_CB must be up buffer first, then down buffers.
221+
*/
201222
{
202223
bool ok;
203224
int32_t buff_cnt;
225+
int32_t buff_cnt_up;
204226

205227
*found = (rtt_cb >= TARGET_RAM_START && rtt_cb <= TARGET_RAM_END);
206-
ok = *found && swd_read_word(rtt_cb + offsetof(SEGGER_RTT_CB, MaxNumDownBuffers), (uint32_t *)&(buff_cnt));
228+
ok = *found && swd_read_word(rtt_cb + offsetof(SEGGER_RTT_CB, MaxNumDownBuffers), (uint32_t *)&(buff_cnt))
229+
&& swd_read_word(rtt_cb + offsetof(SEGGER_RTT_CB, MaxNumUpBuffers), (uint32_t *)&(buff_cnt_up));
207230
if (ok) {
231+
extRttBuf->addr = rtt_cb + offsetof(SEGGER_RTT_CB, aUp[buff_cnt_up]) + channel * sizeof(SEGGER_RTT_BUFFER_DOWN);
208232
*found = *found && (buff_cnt > channel);
209-
*found = *found && swd_read_memory(rtt_cb + offsetof(SEGGER_RTT_CB, aDown[channel]), (uint8_t *)aDown, sizeof(SEGGER_RTT_BUFFER_DOWN));
210-
*found = *found && (aDown->SizeOfBuffer > 0 && aDown->SizeOfBuffer < TARGET_RAM_END - TARGET_RAM_START);
211-
*found = *found && ((uint32_t)aDown->pBuffer >= TARGET_RAM_START && (uint32_t)aDown->pBuffer + aDown->SizeOfBuffer <= TARGET_RAM_END);
233+
*found = *found && swd_read_memory(extRttBuf->addr, (uint8_t *)&(extRttBuf->aDown), sizeof(extRttBuf->aDown));
234+
*found = *found && (extRttBuf->aDown.SizeOfBuffer > 0 && extRttBuf->aDown.SizeOfBuffer < TARGET_RAM_END - TARGET_RAM_START);
235+
*found = *found && ((uint32_t)extRttBuf->aDown.pBuffer >= TARGET_RAM_START && (uint32_t)extRttBuf->aDown.pBuffer + extRttBuf->aDown.SizeOfBuffer <= TARGET_RAM_END);
212236
if (*found) {
213-
picoprobe_info(" rtt_check_channel_to_target : %u %p %5u %5u %5u\n", channel, aDown->pBuffer, aDown->SizeOfBuffer, aDown->RdOff, aDown->WrOff);
237+
picoprobe_info(" rtt_check_channel_to_target : %u %p %5u %5u %5u\n", channel, extRttBuf->aDown.pBuffer, extRttBuf->aDown.SizeOfBuffer, extRttBuf->aDown.RdOff, extRttBuf->aDown.WrOff);
214238
}
215239
}
216240
return ok;
@@ -240,9 +264,8 @@ static unsigned rtt_get_write_space(SEGGER_RTT_BUFFER_DOWN *pRing)
240264

241265

242266

243-
static SEGGER_RTT_BUFFER_UP *ft_aUp;
244-
static uint16_t ft_channel;
245-
static uint32_t ft_rtt_cb;
267+
// ft = from target
268+
static EXT_SEGGER_RTT_BUFFER_UP *ft_extRttBuf;
246269
static uint8_t ft_buf[256];
247270
static uint32_t ft_cnt;
248271
static bool ft_ok;
@@ -263,24 +286,24 @@ static void rtt_from_target_thread(void *p)
263286
continue;
264287
}
265288

266-
ft_ok = swd_read_word(ft_rtt_cb + offsetof(SEGGER_RTT_CB, aUp[ft_channel].WrOff), (uint32_t *)&(ft_aUp->WrOff));
289+
ft_ok = swd_read_word(ft_extRttBuf->addr + offsetof(SEGGER_RTT_BUFFER_UP, WrOff), (uint32_t *)&(ft_extRttBuf->aUp.WrOff));
267290

268-
if (ft_ok && ft_aUp->WrOff != ft_aUp->RdOff) {
291+
if (ft_ok && ft_extRttBuf->aUp.WrOff != ft_extRttBuf->aUp.RdOff) {
269292
//
270293
// fetch data from target
271294
//
272-
if (ft_aUp->WrOff > ft_aUp->RdOff) {
273-
ft_cnt = MIN(ft_cnt, ft_aUp->WrOff - ft_aUp->RdOff);
295+
if (ft_extRttBuf->aUp.WrOff > ft_extRttBuf->aUp.RdOff) {
296+
ft_cnt = MIN(ft_cnt, ft_extRttBuf->aUp.WrOff - ft_extRttBuf->aUp.RdOff);
274297
}
275298
else {
276-
ft_cnt = MIN(ft_cnt, ft_aUp->SizeOfBuffer - ft_aUp->RdOff);
299+
ft_cnt = MIN(ft_cnt, ft_extRttBuf->aUp.SizeOfBuffer - ft_extRttBuf->aUp.RdOff);
277300
}
278301
ft_cnt = MIN(ft_cnt, sizeof(ft_buf));
279302

280303
memset(ft_buf, 0, sizeof(ft_buf));
281-
ft_ok = ft_ok && swd_read_memory((uint32_t)ft_aUp->pBuffer + ft_aUp->RdOff, ft_buf, ft_cnt);
282-
ft_aUp->RdOff = (ft_aUp->RdOff + ft_cnt) % ft_aUp->SizeOfBuffer;
283-
ft_ok = ft_ok && swd_write_word(ft_rtt_cb + offsetof(SEGGER_RTT_CB, aUp[ft_channel].RdOff), ft_aUp->RdOff);
304+
ft_ok = ft_ok && swd_read_memory((uint32_t)ft_extRttBuf->aUp.pBuffer + ft_extRttBuf->aUp.RdOff, ft_buf, ft_cnt);
305+
ft_extRttBuf->aUp.RdOff = (ft_extRttBuf->aUp.RdOff + ft_cnt) % ft_extRttBuf->aUp.SizeOfBuffer;
306+
ft_ok = ft_ok && swd_write_word(ft_extRttBuf->addr + offsetof(SEGGER_RTT_BUFFER_UP, RdOff), ft_extRttBuf->aUp.RdOff);
284307

285308
rtt_cb_alive = true;
286309
}
@@ -295,22 +318,20 @@ static void rtt_from_target_thread(void *p)
295318

296319

297320
#if INCLUDE_SYSVIEW
298-
static void rtt_from_target_reset(uint32_t rtt_cb, uint16_t channel, SEGGER_RTT_BUFFER_UP *aUp)
321+
static void rtt_from_target_reset(EXT_SEGGER_RTT_BUFFER_UP *extRttBuf)
299322
/**
300323
* Reset an upstream buffer.
301324
*/
302325
{
303-
// printf("rtt_from_target_reset(%lx,%d,%p)\n", rtt_cb, channel, aUp);
304-
305-
swd_read_word(rtt_cb + offsetof(SEGGER_RTT_CB, aUp[channel].WrOff), (uint32_t *)&(aUp->WrOff));
306-
aUp->RdOff = aUp->WrOff;
307-
swd_write_word(rtt_cb + offsetof(SEGGER_RTT_CB, aUp[channel].RdOff), aUp->RdOff);
326+
swd_read_word(extRttBuf->addr + offsetof(SEGGER_RTT_BUFFER_UP, WrOff), (uint32_t *)&(extRttBuf->aUp.WrOff));
327+
extRttBuf->aUp.RdOff = extRttBuf->aUp.WrOff;
328+
swd_write_word(extRttBuf->addr + offsetof(SEGGER_RTT_BUFFER_UP, RdOff), extRttBuf->aUp.RdOff);
308329
} // rtt_from_target_reset
309330
#endif
310331

311332

312333

313-
static bool rtt_from_target(uint32_t rtt_cb, uint16_t channel, SEGGER_RTT_BUFFER_UP *aUp,
334+
static bool rtt_from_target(EXT_SEGGER_RTT_BUFFER_UP *extRttBuf,
314335
rtt_data_to_host data_to_host, bool check_host_buffer, bool *worked)
315336
/**
316337
* Fetch data via RTT from target.
@@ -338,9 +359,7 @@ static bool rtt_from_target(uint32_t rtt_cb, uint16_t channel, SEGGER_RTT_BUFFER
338359
}
339360

340361
if (send_data_to_host) {
341-
ft_aUp = aUp;
342-
ft_channel = channel;
343-
ft_rtt_cb = rtt_cb;
362+
ft_extRttBuf = extRttBuf;
344363

345364
xEventGroupSetBits(events, EV_RTT_FROM_TARGET_STRT);
346365
xEventGroupWaitBits(events, EV_RTT_FROM_TARGET_END, pdTRUE, pdFALSE, portMAX_DELAY);
@@ -358,8 +377,7 @@ static bool rtt_from_target(uint32_t rtt_cb, uint16_t channel, SEGGER_RTT_BUFFER
358377

359378

360379

361-
static bool rtt_to_target(uint32_t rtt_cb, StreamBufferHandle_t stream, uint16_t channel,
362-
SEGGER_RTT_BUFFER_DOWN *aDown, bool *worked)
380+
static bool rtt_to_target(EXT_SEGGER_RTT_BUFFER_DOWN *extRttBuf, StreamBufferHandle_t stream, bool *worked)
363381
{
364382
bool ok = true;
365383
uint8_t buf[16];
@@ -369,9 +387,9 @@ static bool rtt_to_target(uint32_t rtt_cb, StreamBufferHandle_t stream, uint16_t
369387
//
370388
// send data to target
371389
//
372-
ok = ok && swd_read_word(rtt_cb + offsetof(SEGGER_RTT_CB, aDown[channel].RdOff), (uint32_t *)&(aDown->RdOff));
390+
ok = ok && swd_read_word(extRttBuf->addr + offsetof(SEGGER_RTT_BUFFER_DOWN, RdOff), (uint32_t *)&(extRttBuf->aDown.RdOff));
373391

374-
num_bytes = rtt_get_write_space(aDown);
392+
num_bytes = rtt_get_write_space( &(extRttBuf->aDown));
375393
if (num_bytes > 0) {
376394
//printf("a cnt: %u -> ", num_bytes);
377395

@@ -383,17 +401,17 @@ static bool rtt_to_target(uint32_t rtt_cb, StreamBufferHandle_t stream, uint16_t
383401
unsigned wr_off;
384402
unsigned remaining;
385403

386-
wr_off = aDown->WrOff;
387-
remaining = aDown->SizeOfBuffer - wr_off;
404+
wr_off = extRttBuf->aDown.WrOff;
405+
remaining = extRttBuf->aDown.SizeOfBuffer - wr_off;
388406

389407
//printf("%u %u %u %u", channel, aDown->WrOff, num_bytes, remaining);
390408

391409
if (remaining > num_bytes) {
392410
//
393411
// All data fits before wrap around
394412
//
395-
ok = ok && swd_write_memory((uint32_t)aDown->pBuffer + wr_off, buf, num_bytes);
396-
aDown->WrOff = wr_off + num_bytes;
413+
ok = ok && swd_write_memory((uint32_t)extRttBuf->aDown.pBuffer + wr_off, buf, num_bytes);
414+
extRttBuf->aDown.WrOff = wr_off + num_bytes;
397415
}
398416
else {
399417
//
@@ -402,13 +420,13 @@ static bool rtt_to_target(uint32_t rtt_cb, StreamBufferHandle_t stream, uint16_t
402420
unsigned num_bytes_at_once;
403421

404422
num_bytes_at_once = remaining;
405-
ok = ok && swd_write_memory((uint32_t)aDown->pBuffer + wr_off, buf, num_bytes_at_once);
423+
ok = ok && swd_write_memory((uint32_t)extRttBuf->aDown.pBuffer + wr_off, buf, num_bytes_at_once);
406424
num_bytes_at_once = num_bytes - remaining;
407-
ok = ok && swd_write_memory((uint32_t)aDown->pBuffer, buf + remaining, num_bytes_at_once);
408-
aDown->WrOff = num_bytes_at_once;
425+
ok = ok && swd_write_memory((uint32_t)extRttBuf->aDown.pBuffer, buf + remaining, num_bytes_at_once);
426+
extRttBuf->aDown.WrOff = num_bytes_at_once;
409427
}
410428

411-
ok = ok && swd_write_word(rtt_cb + offsetof(SEGGER_RTT_CB, aDown[channel].WrOff), aDown->WrOff);
429+
ok = ok && swd_write_word(extRttBuf->addr + offsetof(SEGGER_RTT_BUFFER_DOWN, WrOff), extRttBuf->aDown.WrOff);
412430

413431
//printf(" -> %u\n", aDown->WrOff);
414432
}
@@ -423,14 +441,14 @@ static bool rtt_to_target(uint32_t rtt_cb, StreamBufferHandle_t stream, uint16_t
423441
static void do_rtt_io(uint32_t rtt_cb, bool with_alive_check)
424442
{
425443
#if OPT_TARGET_UART
426-
SEGGER_RTT_BUFFER_UP aUpConsole; // Up buffer, transferring information up from target via debug probe to host
427-
SEGGER_RTT_BUFFER_DOWN aDownConsole; // Down buffer, transferring information from host via debug probe to target
444+
EXT_SEGGER_RTT_BUFFER_UP aUpConsole; // Up buffer, transferring information up from target via debug probe to host
445+
EXT_SEGGER_RTT_BUFFER_DOWN aDownConsole; // Down buffer, transferring information from host via debug probe to target
428446
ok_console_from_target = false;
429447
ok_console_to_target = false;
430448
#endif
431449
#if INCLUDE_SYSVIEW
432-
SEGGER_RTT_BUFFER_UP aUpSysView; // Up buffer, transferring information up from target via debug probe to host
433-
SEGGER_RTT_BUFFER_DOWN aDownSysView; // Down buffer, transferring information from host via debug probe to target
450+
EXT_SEGGER_RTT_BUFFER_UP aUpSysView; // Up buffer, transferring information up from target via debug probe to host
451+
EXT_SEGGER_RTT_BUFFER_DOWN aDownSysView; // Down buffer, transferring information from host via debug probe to target
434452
bool ok_sysview_from_target = false;
435453
bool ok_sysview_to_target = false;
436454
bool net_sysview_was_connected = false;
@@ -468,10 +486,10 @@ static void do_rtt_io(uint32_t rtt_cb, bool with_alive_check)
468486
working_uart = false;
469487

470488
if (ok_console_from_target)
471-
ok = ok && rtt_from_target(rtt_cb, RTT_CHANNEL_CONSOLE, &aUpConsole, cdc_uart_write, false, &working_uart);
489+
ok = ok && rtt_from_target(&aUpConsole, cdc_uart_write, false, &working_uart);
472490

473491
if (ok_console_to_target)
474-
ok = ok && rtt_to_target(rtt_cb, stream_rtt_console_to_target, RTT_CHANNEL_CONSOLE, &aDownConsole, &working_uart);
492+
ok = ok && rtt_to_target(&aDownConsole, stream_rtt_console_to_target, &working_uart);
475493

476494
probe_rtt_cb = probe_rtt_cb && !working_uart;
477495

@@ -486,13 +504,13 @@ static void do_rtt_io(uint32_t rtt_cb, bool with_alive_check)
486504

487505
if ( !net_sysview_was_connected) {
488506
net_sysview_was_connected = true;
489-
rtt_from_target_reset(rtt_cb, RTT_CHANNEL_SYSVIEW, &aUpSysView);
507+
rtt_from_target_reset(&aUpSysView);
490508
}
491509
if (ok_sysview_from_target)
492-
ok = ok && rtt_from_target(rtt_cb, RTT_CHANNEL_SYSVIEW, &aUpSysView, net_sysview_send, true, &working_sysview);
510+
ok = ok && rtt_from_target(&aUpSysView, net_sysview_send, true, &working_sysview);
493511

494512
if (ok_sysview_to_target)
495-
ok = ok && rtt_to_target(rtt_cb, stream_rtt_sysview_to_target, RTT_CHANNEL_SYSVIEW, &aDownSysView, &working_sysview);
513+
ok = ok && rtt_to_target(&aDownSysView, stream_rtt_sysview_to_target, &working_sysview);
496514

497515
probe_rtt_cb = probe_rtt_cb && !working_sysview;
498516
}
@@ -504,18 +522,18 @@ static void do_rtt_io(uint32_t rtt_cb, bool with_alive_check)
504522
//printf("%d %d\n", ok, probe_rtt_cb);
505523
if (ok && probe_rtt_cb) {
506524
// did nothing -> check if RTT channels appeared
507-
#if OPT_TARGET_UART
508-
if ( !ok_console_from_target)
509-
ok = ok && rtt_check_channel_from_target(rtt_cb, RTT_CHANNEL_CONSOLE, &aUpConsole, &ok_console_from_target);
510-
if ( !ok_console_to_target)
511-
ok = ok && rtt_check_channel_to_target(rtt_cb, RTT_CHANNEL_CONSOLE, &aDownConsole, &ok_console_to_target);
512-
#endif
513-
#if INCLUDE_SYSVIEW
514-
if ( !ok_sysview_from_target)
515-
ok = ok && rtt_check_channel_from_target(rtt_cb, RTT_CHANNEL_SYSVIEW, &aUpSysView, &ok_sysview_from_target);
516-
if ( !ok_sysview_to_target)
517-
ok = ok && rtt_check_channel_to_target(rtt_cb, RTT_CHANNEL_SYSVIEW, &aDownSysView, &ok_sysview_to_target);
518-
#endif
525+
#if OPT_TARGET_UART
526+
if ( !ok_console_from_target)
527+
ok = ok && rtt_check_channel_from_target(rtt_cb, RTT_CHANNEL_CONSOLE, &aUpConsole, &ok_console_from_target);
528+
if ( !ok_console_to_target)
529+
ok = ok && rtt_check_channel_to_target(rtt_cb, RTT_CHANNEL_CONSOLE, &aDownConsole, &ok_console_to_target);
530+
#endif
531+
#if INCLUDE_SYSVIEW
532+
if ( !ok_sysview_from_target)
533+
ok = ok && rtt_check_channel_from_target(rtt_cb, RTT_CHANNEL_SYSVIEW, &aUpSysView, &ok_sysview_from_target);
534+
if ( !ok_sysview_to_target)
535+
ok = ok && rtt_check_channel_to_target(rtt_cb, RTT_CHANNEL_SYSVIEW, &aDownSysView, &ok_sysview_to_target);
536+
#endif
519537

520538
// -> delay
521539
xEventGroupWaitBits(events, EV_RTT_TO_TARGET, pdTRUE, pdFALSE, pdMS_TO_TICKS(RTT_POLL_INT_MS));

0 commit comments

Comments
 (0)