Okay, I'm afraid I have more problems, and this one is really driving me nuts.
My new plug-in board has serial battery-backed RAM, an RTC (DS3234) and SD card drive. I prototyped these devices with an Arduino, to get to know all the various registers etc.
The SRAM is working fine now, so I'm confident that some of my basic routines are up to snuff. But...
I'm having a weird problem with the RTC. And it seems to be to do with reading registers. To see what I mean, I've written a program that writes to a register and then reads it back. That's all. And, according to my logic analyser, it's working - except that I get the wrong value back.
I'm writing the value $13. Here's what the exchange looks like when I try to read the register.
Attachment:
SPI_read.png [ 76.27 KiB | Viewed 11485 times ]
As you can see, $13 is indeed being returned. But reading the SPI65 data register gives me $89. Weirdly, $13 and $89 are not dissimilar:
$13 = 00010011
$89 = 10001001
If you take $89 and rotate left (taking bit 7 to bit 0) you get $13. So it feels like I'm just one bit out of position.
The read function consists of putting the register number in A and then calling OSSPIEXCH twice. That function indirects to:
Code:
\ ------------------------------------------------------------------------------
\ --- SPI_EXCHANGE_BYTE
\ --- Implements: OSSPIEXCH
\ ------------------------------------------------------------------------------
\ ON ENTRY: Byte to send should be in A
\ ON EXIT : Returned byte is in A
.spi_exchange_byte ; Sends & receives a byte. Byte value should be in A
sta SPI_DATA_REG ; Write to Data Reg
.spi_wait_for_tc ; Wait for transfer
bit SPI_STAT_REG ; Run BIT on Status Reg. If TC set, N flag will be set
bpl spi_wait_for_tc ; If positive, bit 7 not set yet
lda SPI_DATA_REG ; Read incoming byte. Clears TC flag
rts
So, with $01 in A, I call that function and get $FF back. Then I immediately call the function again. This time $FF is in A because that was left over from the previous call. I get back $13 but A contains $89.
This is on a 1MHz 65C02 machine, so I'm not quite pushing the boundaries of speed here.
Any thoughts? I've tried slowing things down by putting some NOPs between the first exchange (sending the register number) and the second (getting the value back) but to no avail. I'm sure I'm doing something obviously stupid but can't see what.
[EDIT] Thought it would be useful to show the read register routine:
Code:
.rtc_read_reg
pha ; Save register number for later
lda SPI_CURR_DEV ; Contents of this location previously set to %01111111
sta SPI_DEV_SEL ; $BF02 - SPI SS register
stz SPI_DATA_REG ; $BF00 : to clear TC
pla ; Get register number back
jsr OSSPIEXCH ; Selects the reg
jsr OSSPIEXCH ; Register value returned in A
ldx #SPI_DEV_NONE ; Constant with value %11111111
stx SPI_DEV_SEL ; $BF02 - SPI SS register
rts
_________________
I like it when things smoke.
Blog –
Zolatron 64 project