summaryrefslogtreecommitdiff
path: root/rs422lib/rsbus.c
diff options
context:
space:
mode:
authorJoe Zhao <ztuowen@gmail.com>2014-05-16 16:41:30 +0800
committerJoe Zhao <ztuowen@gmail.com>2014-05-16 16:41:30 +0800
commitb038c3b87564e9f4acac0019b18fd6d20692ac38 (patch)
tree4e795aeb88fbb67f6ecdfcdc0d75fdf2e15150bb /rs422lib/rsbus.c
parentd78e451d6bb81823c77d421e327a738bc54dc943 (diff)
downloadrsbus-b038c3b87564e9f4acac0019b18fd6d20692ac38.tar.gz
rsbus-b038c3b87564e9f4acac0019b18fd6d20692ac38.tar.bz2
rsbus-b038c3b87564e9f4acac0019b18fd6d20692ac38.zip
Tangential change
Diffstat (limited to 'rs422lib/rsbus.c')
-rw-r--r--rs422lib/rsbus.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/rs422lib/rsbus.c b/rs422lib/rsbus.c
index 4c0fcb2..2b548a6 100644
--- a/rs422lib/rsbus.c
+++ b/rs422lib/rsbus.c
@@ -10,6 +10,11 @@
#define SYSRET (0xF0)
#define INSREP (0x0F)
+// Maximum size of receive buffer
+#define RSMAX (20)
+
+#define CODE_LEN (6)
+
unsigned char rs_rxbuf[RSMAX];
unsigned char len;
unsigned char pch;
@@ -17,7 +22,7 @@ rshdlr rxhdlr;
enum rs_stat{REST,ST,ADDR,READ,DISCARD,ED,DED};
enum rs_stat rs_st=REST;
-unsigned char rs_tx[20]={STBIT1,0,RSADDR,MACTYPE};
+unsigned char rs_tx[CODE_LEN+RS_MAX]={STBIT1,0,RSADDR,MACTYPE};
volatile unsigned char tx_len;
volatile unsigned char tx_pos=255;
@@ -36,6 +41,7 @@ __interrupt void USCI0TX_ISR(void)
UCA0TXBUF = rs_tx[tx_pos++]; // TX next character
}
+// Inline send for ISR
#pragma FUNC_ALWAYS_INLINE(rsbus_w_irq)
static __inline void rsbus_w_irq(unsigned char* buf,unsigned int len)
{
@@ -46,8 +52,6 @@ static __inline void rsbus_w_irq(unsigned char* buf,unsigned int len)
tx_len=len+7;
unsigned int parity=MACTYPE^RSADDR;
rs_tx[1]=0; // Useless addr spec
- rs_tx[2]=RSADDR;
- rs_tx[3]=MACTYPE;
for (i=0;i<len;++i)
parity^=(rs_tx[4+i]=buf[i]);
rs_tx[4+len]=parity;
@@ -132,19 +136,19 @@ __interrupt void rsbus_rx(void)
}
}
+// Send something to the rs422 bus
+// Length must not exceeds RS_MAX
void rsbus_w(int addr,unsigned char* buf,unsigned int len)
{
while (tx_pos < tx_len) // This shouldn't happen
__delay_cycles(100); // Race condition -> wait
unsigned int i;
- if (len>14)
+ if (len>RS_MAX)
return;
RSOUT |= RSPIN;
tx_len=len+7;
unsigned int parity=addr^MACTYPE^RSADDR;
rs_tx[1]=addr; // Useless addr spec
- rs_tx[2]=RSADDR;
- rs_tx[3]=MACTYPE;
for (i=0;i<len;++i)
parity^=(rs_tx[4+i]=buf[i]);
rs_tx[4+len]=parity;