Skip to content

Commit

Permalink
Use isr for pulling bytes off of usart to avoid blocking/hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
ps2 committed Mar 29, 2016
1 parent d2f96a0 commit d92cece
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile.spi1_alt2
@@ -1,5 +1,5 @@
SERIAL_TYPE := spi1_alt2
SERIAL_PARAMS := -DUSES_USART1_ISR
SERIAL_PARAMS := -DUSES_USART1_TX_ISR -DUSES_USART1_RX_ISR

BOARD_TYPE := RILEYLINK
BOARD_PARAMS :=
Expand Down
2 changes: 1 addition & 1 deletion Makefile.uart1_alt2
@@ -1,5 +1,5 @@
SERIAL_TYPE := uart1_alt2
SERIAL_PARAMS :=
SERIAL_PARAMS := -DUSES_USART1_RX_ISR

BOARD_TYPE := RILEYLINK
BOARD_PARAMS :=
Expand Down
6 changes: 5 additions & 1 deletion main.c
Expand Up @@ -12,14 +12,18 @@ void t1_isr(void) __interrupt T1_VECTOR;
void rftxrx_isr(void) __interrupt RFTXRX_VECTOR;
void rf_isr(void) __interrupt RF_VECTOR;

#ifdef USES_USART1_ISR
#ifdef USES_USART1_RX_ISR
void rx1_isr(void) __interrupt URX1_VECTOR;
#endif

#ifdef USES_USART1_TX_ISR
void tx1_isr(void) __interrupt UTX1_VECTOR;
#endif

#if TI_DONGLE || SRF_STICK
void usb_isr() __interrupt 6;
#endif

int main(void)
{

Expand Down
2 changes: 1 addition & 1 deletion tools/cause_hang.py
Expand Up @@ -16,7 +16,7 @@
rl.sync()
params = bytearray("00000000000033e800a968e55658e568d555d26000".decode('hex'))
rl.send_command(SerialRfSpy.CMD_SEND_AND_LISTEN, params)
params = bytearray("0000000050".decode('hex'))
params = bytearray("0000000a50".decode('hex'))
rl.send_command(SerialRfSpy.CMD_GET_PACKET, params)
resp = rl.get_response()
print "resp1 = %s" % str(resp).encode('hex')
Expand Down
44 changes: 41 additions & 3 deletions uart1_alt2/serial.c
Expand Up @@ -4,9 +4,24 @@
#include "serial.h"
#include "radio.h"

#define SERIAL_BUF_LEN 220

volatile uint8_t __xdata serial_input_buf[SERIAL_BUF_LEN];
volatile uint8_t input_size = 0;
volatile uint8_t input_head_idx = 0;
volatile uint8_t input_tail_idx = 0;

volatile uint8_t __xdata serial_output_buf[SERIAL_BUF_LEN];
volatile uint8_t output_size = 0;
volatile uint8_t output_head_idx = 0;
volatile uint8_t output_tail_idx = 0;

volatile uint8_t ready_to_send = 0;

volatile uint8_t serial_data_available;

void configure_serial()
{

// UART1 Alt. 2
// P1.4 - CT
// P1.5 - RT
Expand All @@ -32,14 +47,36 @@ void configure_serial()
// Enable receive
U1CSR |= 0x40;
URX1IF = 0;
IEN0 |= 0x88;
}

void rx1_isr(void) __interrupt URX1_VECTOR {
URX1IF = 0;
if (input_size < SERIAL_BUF_LEN) {
serial_input_buf[input_head_idx] = U1DBUF;
input_head_idx++;
if (input_head_idx >= SERIAL_BUF_LEN) {
input_head_idx = 0;
}
input_size++;
serial_data_available = 1;
} else {
// overflow
}
}

uint8_t serial_rx_byte() {
uint8_t s_data;
while(!SERIAL_DATA_AVAILABLE); // URX1IF
s_data = U1DBUF;
URX1IF = 0;
s_data = serial_input_buf[input_tail_idx];
input_tail_idx++;
if (input_tail_idx >= SERIAL_BUF_LEN) {
input_tail_idx = 0;
}
input_size--;
if (input_size == 0) {
serial_data_available = 0;
}
return s_data;
}

Expand Down Expand Up @@ -68,3 +105,4 @@ void serial_tx_str(const char *str) {
}



4 changes: 3 additions & 1 deletion uart1_alt2/serial.h
@@ -1,7 +1,9 @@
#ifndef SERIAL_H
#define SERIAL_H

#define SERIAL_DATA_AVAILABLE URX1IF
#define SERIAL_DATA_AVAILABLE serial_data_available

extern volatile uint8_t serial_data_available;

void configure_serial();
void serial_tx_byte(uint8_t);
Expand Down

0 comments on commit d92cece

Please sign in to comment.