6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Nov 24, 2024 10:11 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Wed May 08, 2019 4:13 am 
Offline

Joined: Sat May 26, 2018 1:00 am
Posts: 26
Location: Riverton, UT
I put together some routines to bit bang SPI for my SBC. These routines are based on code written by Garth Wilson. I have added support for SPI mode 0-3 and full-duplex transfers. It has been successfully used to talk to a MAX3100 UART in mode 0, and a DS3234 RTC in mode 1. Mode 2 and 3 are not tested but should work.

The code can be found here https://github.com/bradleystach/65C02-SPI for anyone who might be interested.

Now keep in mind this was put together by someone who hasn't done much 6502 coding since I was 14 years old back in 1984.
No guarantees on it being the fastest or most compact implementation. If anyone has any suggestions for improvements, I am completely open to them.

Also, if anyone has any questions, feel free to post them here.

Hope they help someone out.


Top
 Profile  
Reply with quote  
PostPosted: Tue May 14, 2019 11:19 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8546
Location: Southern California
I'm surprised no one has responded yet. I can't take time to analyze the function, but I'll make some comments to help efficiency.

bradleystach wrote:
I put together some routines to bit bang SPI for my SBC. These routines are based on code written by Garth Wilson. I have added support for SPI mode 0-3 and full-duplex transfers. It has been successfully used to talk to a MAX3100 UART in mode 0, and a DS3234 RTC in mode 1. Mode 2 and 3 are not tested but should work.

The code can be found here https://github.com/bradleystach/65C02-SPI for anyone who might be interested.

Now keep in mind this was put together by someone who hasn't done much 6502 coding since I was 14 years old back in 1984.
No guarantees on it being the fastest or most compact implementation. If anyone has any suggestions for improvements, I am completely open to them.

Also, if anyone has any questions, feel free to post them here.

Hope they help someone out.


At README.md, you wrote,
Quote:
NOTE: All SPI routines should be called with Interrupts disabled to prevent disruption of data transfer

One beauty of synchronous serial (like SPI) however is that you can interrupt it, as long as the interrupt does not change the I/O port bits being used for the communication.

In SPI.s65, you have,
Code:
        asl
        cmp  #0

and later I saw
Code:
        ldx  SPIMODE    ; Get the current SPI communication mode
        cpx  #0         ; Is it mode 0?

Note that the comparison to zero is an automatic, implied part of every load, pull, arithmetic, and logical instruction; so it's not necessary to put it here. It's redundant.

Where you have
Code:
        cmp  #1         ; Mode 0/1 or mode 2/3

you can do the comparison with one byte less (although it will be the same speed) with DEA if you don't need the original value anymore (as is the case here). DEA (or DEC A) will take the 1 down to 0 and set the Z flag. Every other value will result in Z being cleared. Taking it further, it you want to test for a sequence of numbers, you can DEA, BNE/BEQ, DEA, BNE/BEQ, etc..

You put PHA...PLA as bookends in all the subroutines. Are you sure you need to do that? Subroutines are typically called where, if A matters, it's being used to pass a parameter one or both ways.

I have a lot of programming tips for not-so-experienced 65xx programmers, at http://wilsonminesco.com/6502primer/PgmTips.html .

In any case, I'm always glad for resources being published to promote the use of SPI and similar with 65xx processors.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: