Hello again,
I have a problem when communicating with a device via SPI.
I have to start the spi communication and wait for a port of the slave device to be set, but it seems, that the microcontroller reads the port as set when it is not set at this moment.
I am the SPI - master, the other device is SPI slave.
I have to send the adress byte and the length byte first. Then I have to wait, until the slave device pulls up its MISO output, then I can proceed with communication and read the data.
I use the USB Multilink interface to debug my target hardware (it runs at 3,3 V).
When I manually step the code, it seems, that it always work. When I let it run without breakpoints, then I sometimes get a wrong value (wrong value: always 0) back. I "debugged" it by hardware and I have seen, that in case of getting a wrong value, my routine does not wait until the slave device pulls its MISO output high.
I don't know, whats wrong with my code. There are no pullup or pulldown resistors activated.
#define CHECK_MISO_LINE (PTBD_PTBD4>0?0:1)
void SPIread (CS_t cs, uint8_t addr, uint8_t len, uint8_t * d)
{
uint8_t i, j;
delay_us (PREDELAY); // minimum delay, if we have subsequent calls
spi_setcs (cs); // set chipselect
delay_us (DEVICE_PREPARE); // minimum delay for device to get ready after beeing selected
j = 1;
while (j) // check the MISO line of the slave
{
j = CHECK_MISO_LINE;
}
spi_SendByte (addr); // send adress and length and wait for MISO line going high
spi_SendByte (len);
j = 1;
while (j)
{
j = CHECK_MISO_LINE;
}
for (i = 0; i < len; i++) // do the read access to the slave
{
*d = spi_TransferByte (0);
d++;
}
delay_us (DEVICE_PREPARE); // delay again...
j = 1;
while (j)
{
j = CHECK_MISO_LINE;
}
spi_resetcs (cs); // reset the chipselect to stop this access
}
The functions spi_setcs() and spi_resetcs() are setting and resetting the chipselect pins.
In case of error, I can see at the oscilloskope, that the routine spi_read() is continuing with the spi_Transferbyte() - calls, while the MISO - line is not set.
I don't know, if this is important: The SPI - clock is very slow, because the device is very slow and only allows a maximum SPI - clock of 75 kHz. I have tested it with a bus clock of about 70 and in a further step with 8 kBit. It seems, that it is independent from the clock.
If I restart the debug session with strg - shift - F5, I get sometimes an error out of the code warrior, that it is not possible to set a breakpoint at a certain adress. If I restart the Emulator window, it works agian for a while. Maybe this is a second problem?
Many thanks in advance,
Thomas