flat assembler
Message board for the users of flat assembler.

Index > Projects and Ideas > [resolved] software SPI master/

Author
Thread Post new topic Reply to topic
delta67



Joined: 18 Jul 2012
Posts: 19
delta67 29 Jul 2012, 17:59
Hi all,

I'm trying to write a software SPI master. The most important thing is the routine wich sends/receives one byte. I've written a piece of code but the result is not as expected !! What is wrong please??
When I call RD_SPI it is expected to get FFh on MOSI (C7) but I get 80h !!!
I tested this with a logic analyzer.

EDIT:
This code is for an 8088 SBC, wich have an 8255 on 00-03h. PORTC is configured like this: C0-C3 inputs, C4-C7 outputs. The SD is connected to this port (C)
MISO ---> C0
CLK ---> C4
CS ---> C5
MOSI ----> C7

Code:
;=====================
; subroutine to write/read one byte
;===========================
RD_SPI:
        mov ah,0xFF
WR_SPI:                 ; byte to send is in AH
        mov bl,8        ; 8bits to send
next:
        in al, 0x02             ; read portC 
        test ah,0x80    ; MSBit  1 or 0??
        jz A                    ; if MSBit =0  jmp to A
        or al, 0x80             ;  MSBit =1 then Set C7 (MOSI)
        jmp B
A:
        and al, 0x7F    ; Clear C7 (MOSI)
B:
        out 0x02,al             ;  write Mosi (must be on portC C7)
         
        mov al,0x09             ; CLK is connected to C4: byte to send on command port
                                        ; to set C4 high is "0000 100 1" = 09 
    out 0x03,al          ; set CLK HIGH
    dec al                      ; we decrement AL then AL = "0000 100 0" --> C4=0
    out 0x03,al          ; turn CLK LOW

        ; we go read MISO
        in al,0x02               ; MISO is connected on C0
        TEST AL, 01H    ; test C0: 0000 0001
        jz MISO_L                ; if MISO = 0 jump
        inc ah                  ; else  MISO high then set D0 of AH : AH0=1
        
MISO_L:
        shl ah,1                ; left shift 1 bit 
        dec bl          ; next bit
        jnz next        ; if not last bit we continue sending bits
        ret                     ; return, received byte is in AH
        
            


Thanks


Last edited by delta67 on 30 Jul 2012, 07:26; edited 1 time in total
Post 29 Jul 2012, 17:59
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4353
Location: Now
edfed 29 Jul 2012, 22:29
can you be more precise on the context (hardware config) of the run. cause here, the code (if PC compatible) just seems to use the io port 2 that is not really a port used as a receiver (port 002 = DMA Channel 1 address register), then, all you can get from this port is what you gave to it.

here, it seems that the initial value of port2 have the LSB still set (test 80h), and then, you send (at least) 80h to this port (or al,80h), and you get 80h from this port (in al,2).
Post 29 Jul 2012, 22:29
View user's profile Send private message Visit poster's website Reply with quote
delta67



Joined: 18 Jul 2012
Posts: 19
delta67 30 Jul 2012, 07:25
edfed wrote:
can you be more precise on the context (hardware config) of the run. cause here, the code (if PC compatible) just seems to use the io port 2 that is not really a port used as a receiver (port 002 = DMA Channel 1 address register), then, all you can get from this port is what you gave to it.

here, it seems that the initial value of port2 have the LSB still set (test 80h), and then, you send (at least) 80h to this port (or al,80h), and you get 80h from this port (in al,2).

Sorry, I should be more precise!!
This code is for an 8088 SBC, wich have an 8255 on 00-03h. PORTC is configured like this: C0-C3 inputs, C4-C7 outputs. The SD is connected to this port (C)
MISO ---> C0
CLK ---> C4
CS ---> C5
MOSI ----> C7

Thank u edfed
Post 30 Jul 2012, 07:25
View user's profile Send private message Reply with quote
delta67



Joined: 18 Jul 2012
Posts: 19
delta67 30 Jul 2012, 08:22
PROBLEM FIXED

I should shift AH befor reading MISO !!!
initialy AH = FFh, If MISO is high when I increment AH then the result AH =00
But if I do "SHL AH, 1" befor reading MISO then If MISO is high incrementing AH sets BIT0 of AH.

thus the wright code :

Code:

;=====================
; subroutine to write one byte
;===========================
RD_SPI:
    mov ah,0xFF
WR_SPI:              ; byte to send is in AH
     mov bl,08h      ; 8bits to send
next:
        in al, 0x02             ; read portC 
       test ah,0x80    ; MSBit  1 or 0??
   jz A                    ; if MSBit =0  jmp to A
     or al, 0x80             ;  MSBit =1 then Set C7 (MOSI)
      jmp B
A:
     and al, 0x7F    ; Clear C7 (MOSI)
B:
 out 0x02,al             ;  write Mosi (must be on portC C7)
  
   shl ah,1                ; left shift 1 bit = Next bit to send

   ; we go read MISO befor sending pulse on CLCK !!!
   in al,0x02               ; MISO is connected on C0
  TEST AL, 01H    ; test C0: 0000 0001
    jz MISO_L                ; if MISO = 0 jump
 inc ah                  ; else  MISO high then set D0 of AH : AH0=1
     
MISO_L:
     mov al,0x09             ; CLK is connected to C4: byte to send on command port
                                  ; to set C4 high is "0000 100 1" = 09 
    out 0x03,al      ; set CLK HIGH
    dec al                   ; we decrement AL then AL = "0000 100 0" --> C4=0
    out 0x03,al        ; turn CLK LOW

 dec bl          ; next bit
  jnz next        ; if not last bit we continue sending bits
  ret                     ; return, read byte is in AH
        

    
Post 30 Jul 2012, 08:22
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.