HS-SerialRx
PUBlic Spin MethodsCall this method before first use of object to initialize pins and baud rate. Tested up to 3_000_000 baud, probably okay for 4M as well. Returns adress of read-buffer (@Cog-Image+4 used here so 256 bytes buffer). The BYTE before the buffer-adress (aka adress-1) is the current Write-Pointer into the buffer (0-255). The 3 Bytes before that are not used by the driver and can be used as Read-Pointer and scrap-area. start(rxdpin, baudrate)SOURCE CODE... PUB start(rxdpin, baudrate) long[@rxpin] := |<rxdpin long[@bitticks] := (clkfreq / baudrate) result := @HSSerialRx+4 ' Use cog image for serial buffer (skip 4 for control) cognew(@HSSerialRx, @HSSerialRx) Assembly CogSOURCE CODE... org HSSerialRx add rxwr, par add rxbuf, par mov Y0,rxbuf sub Y0,#4 mov X0,#0 wrlong X0,Y0 mov stticks,bitticks shr stticks,#1 sub stticks,#8 ' compensate timing receive mov rxdata,#0 mov rxcnt,stticks waitpne rxpin,rxpin ' ' START BIT DETECTED ' 'time sample for middle of start bit add rxcnt,cnt ' uses special start bit timing waitcnt rxcnt,bitticks 'sample middle of start bit test rxpin,ina wz 'sample middle of start bit rxcond2 if_nz jmp #receive ' restart if false start ' ' START bit validated ' Read in data bits ' No point in looping as we have plenty of code to play with ' and inlining can lead to higher receive speeds ' waitcnt rxcnt,bitticks test rxpin,ina wc if_c or rxdata,#01 waitcnt rxcnt,bitticks test rxpin,ina wc if_c or rxdata,#02 waitcnt rxcnt,bitticks test rxpin,ina wc if_c or rxdata,#04 waitcnt rxcnt,bitticks test rxpin,ina wc if_c or rxdata,#08 waitcnt rxcnt,bitticks test rxpin,ina wc if_c or rxdata,#$10 waitcnt rxcnt,bitticks test rxpin,ina wc if_c or rxdata,#$20 waitcnt rxcnt,bitticks test rxpin,ina wc if_c or rxdata,#$40 waitcnt rxcnt,bitticks test rxpin,ina wc if_c or rxdata,#$80 waitcnt rxcnt,bitticks ' check stop bit test rxpin,ina wc if_nc jmp #abreak ' discard if framing error (no stop bits) wrbuf rdbyte Y0,rxwr ' rxwr points to byte index in hub mov X0,rxbuf add X0,Y0 ' X points to buffer location to store wrbyte rxdata,X0 add Y0,#1 wrbyte Y0,rxwr mov breakcnt,#3 'reset any break detection in progress jmp #receive 'byte done, receive next byte abreak djnz breakcnt,#receive mov Y0,#$0FF hubop Y0,#0 jmp $ 'takes a while to reset, meanwhile.. long 0[16] ' make sure this code image is more than 256+4 bytes rxpin long 0 'mask of rx pin rxwr long 3 ' byte address of rxwr in hub rxbuf long 4 ' pointer to rxbuf in hub memory bitticks long 0 stticks long 0 breakcnt long 40 rxcnt res 1 rxdata res 1 'assembled character X0 res 1 Y0 res 1 |