Page 1 of 1

[Software] Joyport SPI Master

Posted: Sun Apr 24, 2016 8:15 pm
by MooZ
I'm putting this here as I don't if I'll ever find time to implement it.
SPI is a rather simple bus. It only uses 4 pins, SCK, MISO, MOSI, CS. Long story short, joyport SEL and CLR can be used for SCK and MISO, any of the D0-D3 lines can be used for MOSI, and CS can be directly wired to GND. The code may look like this:

Code: Select all

  .zp
spi_polarity .ds 1
spi_phase    .ds 1
  .code
;;
;; Function: spi_mode
;; Set SPI phase and polarity.
;;
;; Parameters:
;;   A - mode
;;
spi_mode:
    pha
    and    #$02
    sta    <spi_polarity
    pla
    and    #$01
    sta    <spi_phase
    rts

Code: Select all

;;
;; Function: spi_write
;;
;; Parameters:
;;   _al - byte to send
;;
;; Return:
;;   _ah - byte read
;;
spi_write:
    stz    <_ah
    ldx    #$08
@loop:
    lda    <_al
    and    #$01
    sta    joyport
    
    lda    <spi_phase
    bne    @l0
        lda    joyport
        lsr    A
        rol    <_ah
@l0:
    lda    <spi_polarity
    eor    #$02
    sta    joyport
    
    ; todo : delay
    
    lda    <spi_phase
    beq    @l1
        lda    joyport
        lsr    A
        rol    <_ah
@l1:
    
    lda    <spi_polarity
    sta    joyport
    
    ; todo : delay
    
    dex
    bne    @loop
    
    rts


Re: [Software] Joyport SPI Master

Posted: Mon Apr 25, 2016 12:04 pm
by MooZ
I forgot the CS line is used to synchronize packets/byte between master and slave.
A direct implementation is impossible as there are only 2 output PINs on the joyport.

Crap...

Re: [Software] Joyport SPI Master

Posted: Sun Nov 27, 2016 11:27 pm
by MooZ
That leaves us with the good old rx/tx or i2c...