Skip to content

Commit

Permalink
rtt: support channels other than 0
Browse files Browse the repository at this point in the history
Add support for targets that use channels other than 0. Currently the
channel number is ignored, which duplicates existing functionality,
however furure work may be done to connect other channels to e.g.
different USB endpoints.

Signed-off-by: Sean Cross <[email protected]>
  • Loading branch information
xobs authored and dragonmux committed Sep 25, 2024
1 parent a85b601 commit 3c79062
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/include/rtt_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ int rtt_if_init(void);
/* hosted teardown */
int rtt_if_exit(void);

/* target to host: write len bytes from the buffer starting at buf. return number bytes written */
uint32_t rtt_write(const char *buf, uint32_t len);
/* host to target: read one character, non-blocking. return character, -1 if no character */
int32_t rtt_getchar(void);
/* host to target: true if no characters available for reading */
bool rtt_nodata(void);
/* target to host: write len bytes from the buffer on the channel starting at buf. return number bytes written */
uint32_t rtt_write(const uint32_t channel, const char *buf, uint32_t len);
/* host to target: read one character from the channel, non-blocking. return character, -1 if no character */
int32_t rtt_getchar(const uint32_t channel);
/* host to target: true if no characters available for reading in the selected channel */
bool rtt_nodata(const uint32_t channel);

#endif /* INCLUDE_RTT_IF_H */
13 changes: 10 additions & 3 deletions src/platforms/common/stm32/rtt_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ void rtt_serial_receive_callback(usbd_device *dev, uint8_t ep)
}

/* rtt host to target: read one character */
int32_t rtt_getchar()
int32_t rtt_getchar(const uint32_t channel)
{
int retval;
(void)channel;

if (recv_head == recv_tail)
return -1;
Expand All @@ -110,14 +111,20 @@ int32_t rtt_getchar()
}

/* rtt host to target: true if no characters available for reading */
bool rtt_nodata()
bool rtt_nodata(const uint32_t channel)
{
/* only support reading from down channel 0 */
if (channel != 0U)
return true;
return recv_head == recv_tail;
}

/* rtt target to host: write string */
uint32_t rtt_write(const char *buf, uint32_t len)
uint32_t rtt_write(const uint32_t channel, const char *buf, uint32_t len)
{
/* only support writing to up channel 0 */
if (channel != 0U)
return len;
if (len != 0 && usbdev && usb_get_config() && gdb_serial_get_dtr()) {
for (uint32_t p = 0; p < len; p += CDCACM_PACKET_SIZE) {
uint32_t plen = MIN(CDCACM_PACKET_SIZE, len - p);
Expand Down
22 changes: 16 additions & 6 deletions src/platforms/hosted/rtt_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,23 @@ int rtt_if_exit()

/* write buffer to terminal */

uint32_t rtt_write(const char *buf, uint32_t len)
uint32_t rtt_write(const uint32_t channel, const char *buf, uint32_t len)
{
/* only support writing to up channel 0 */
if (channel != 0U)
return len;
int unused = write(1, buf, len);
(void)unused;
return len;
}

/* read character from terminal */

int32_t rtt_getchar()
int32_t rtt_getchar(const uint32_t channel)
{
char ch;
int len;
(void)channel;
len = read(0, &ch, 1);
if (len == 1)
return ch;
Expand All @@ -92,8 +96,9 @@ int32_t rtt_getchar()

/* true if no characters available */

bool rtt_nodata()
bool rtt_nodata(const uint32_t channel)
{
(void)channel;
return false;
}

Expand All @@ -113,23 +118,28 @@ int rtt_if_exit()

/* write buffer to terminal */

uint32_t rtt_write(const char *buf, uint32_t len)
uint32_t rtt_write(const uint32_t channel, const char *buf, uint32_t len)
{
/* only support writing to up channel 0 */
if (channel != 0U)
return len;
write(1, buf, len);
return len;
}

/* read character from terminal */

int32_t rtt_getchar()
int32_t rtt_getchar(const uint32_t channel)
{
(void)channel;
return -1;
}

/* true if no characters available */

bool rtt_nodata()
bool rtt_nodata(const uint32_t channel)
{
(void)channel;
return false;
}

Expand Down
13 changes: 10 additions & 3 deletions src/platforms/launchpad-icdi/rtt_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ void rtt_serial_receive_callback(usbd_device *dev, uint8_t ep)
}

/* rtt host to target: read one character */
int32_t rtt_getchar()
int32_t rtt_getchar(const uint32_t channel)
{
int retval;
(void)channel;

if (recv_head == recv_tail)
return -1;
Expand All @@ -110,14 +111,20 @@ int32_t rtt_getchar()
}

/* rtt host to target: true if no characters available for reading */
bool rtt_nodata()
bool rtt_nodata(const uint32_t channel)
{
/* only support reading from down channel 0 */
if (channel != 0U)
return true;
return recv_head == recv_tail;
}

/* rtt target to host: write string */
uint32_t rtt_write(const char *buf, uint32_t len)
uint32_t rtt_write(const uint32_t channel, const char *buf, uint32_t len)
{
/* only support writing to up channel 0 */
if (channel != 0U)
return len;
if (len != 0 && usbdev && usb_get_config() && gdb_serial_get_dtr()) {
for (uint32_t p = 0; p < len; p += CDCACM_PACKET_SIZE) {
uint32_t plen = MIN(CDCACM_PACKET_SIZE, len - p);
Expand Down
11 changes: 8 additions & 3 deletions src/rtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,13 @@ static void find_rtt(target_s *const cur_target)
/* poll if host has new data for target */
static rtt_retval_e read_rtt(target_s *const cur_target, const uint32_t i)
{
/* down buffers are located in the rtt_channel array after the
* up buffers. subtract the index from the total number of
* up buffers to get the down buffer number. */
uint32_t channel = i - rtt_num_up_chan;

/* copy data from recv_buf to target rtt 'down' buffer */
if (rtt_nodata())
if (rtt_nodata(channel))
return RTT_IDLE;

if (cur_target == NULL || rtt_channel[i].buf_addr == 0 || rtt_channel[i].buf_size == 0)
Expand All @@ -237,7 +242,7 @@ static rtt_retval_e read_rtt(target_s *const cur_target, const uint32_t i)
const uint32_t next_head = (rtt_channel[i].head + 1U) % rtt_channel[i].buf_size;
if (rtt_channel[i].tail == next_head)
break;
const int ch = rtt_getchar();
const int ch = rtt_getchar(channel);
if (ch == -1)
break;
if (target_mem32_write(cur_target, rtt_channel[i].buf_addr + rtt_channel[i].head, &ch, 1))
Expand Down Expand Up @@ -320,7 +325,7 @@ static rtt_retval_e print_rtt(target_s *const cur_target, const uint32_t i)
return RTT_ERR;

/* write buffer to usb */
rtt_write(xmit_buf, bytes_read);
rtt_write(i, xmit_buf, bytes_read);

return RTT_OK;
}
Expand Down

0 comments on commit 3c79062

Please sign in to comment.