Skip to content

Commit

Permalink
add read_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
dehanj committed Feb 8, 2024
1 parent d0aca27 commit 90a884e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/tkey/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ void writebyte(uint8_t b);
void write(const uint8_t *buf, size_t nbytes);
uint8_t readbyte();
int read(uint8_t *buf, size_t bufsize, size_t nbytes);
size_t read_timeout(uint8_t *buf, size_t nbytes, uint32_t timeout, uint32_t prescaler);
#endif
44 changes: 40 additions & 4 deletions libcommon/proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
#include <tkey/tk1_mem.h>

// clang-format off
static volatile uint32_t* const can_rx = (volatile uint32_t *)TK1_MMIO_UART_RX_STATUS;
static volatile uint32_t* const rx = (volatile uint32_t *)TK1_MMIO_UART_RX_DATA;
static volatile uint32_t* const can_tx = (volatile uint32_t *)TK1_MMIO_UART_TX_STATUS;
static volatile uint32_t* const tx = (volatile uint32_t *)TK1_MMIO_UART_TX_DATA;
static volatile uint32_t* const can_rx = (volatile uint32_t *)TK1_MMIO_UART_RX_STATUS;
static volatile uint32_t* const rx = (volatile uint32_t *)TK1_MMIO_UART_RX_DATA;
static volatile uint32_t* const can_tx = (volatile uint32_t *)TK1_MMIO_UART_TX_STATUS;
static volatile uint32_t* const tx = (volatile uint32_t *)TK1_MMIO_UART_TX_DATA;
static volatile uint32_t* const timer = (volatile uint32_t *)TK1_MMIO_TIMER_TIMER;
static volatile uint32_t* const timer_prescaler = (volatile uint32_t *)TK1_MMIO_TIMER_PRESCALER;
static volatile uint32_t* const timer_status = (volatile uint32_t *)TK1_MMIO_TIMER_STATUS;
static volatile uint32_t* const timer_ctrl = (volatile uint32_t *)TK1_MMIO_TIMER_CTRL;
// clang-format on

uint8_t genhdr(uint8_t id, uint8_t endpoint, uint8_t status, enum cmdlen len)
Expand Down Expand Up @@ -94,3 +98,35 @@ int read(uint8_t *buf, size_t bufsize, size_t nbytes)

return 0;
}

size_t read_timeout(uint8_t *buf, size_t nbytes, uint32_t timeout, uint32_t prescaler)
{
int n;

*timer = timeout;
*timer_prescaler = prescaler;

// Start timer
*timer_ctrl |= (1 << TK1_MMIO_TIMER_CTRL_START_BIT);
for (n = 0; n < nbytes; n++) {

for (;;) {
if (*can_rx) {
buf[n] = *rx;
break;
}

if (*timer_status == 1) {
// Timer expired
return n;
}
}
}

// Stop and reset timer
if (*timer_status != 1) {
*timer_ctrl |= (1 << TK1_MMIO_TIMER_CTRL_START_BIT);
}

return n;
}

0 comments on commit 90a884e

Please sign in to comment.