2018-08-11 02:44 AM
I am using SPI3 of STM32F446ZE MCU to communicate with 25AA02UID EEPROM. I am writing 8 bytes and reading 12 bytes (4 bytes + last written 8 bytes).
I am using HAL Library for this operation. For writing I am using:
HAL_SPI_Transmit()
And for reading I am using:
HAL_SPI_Receive()
I believe the writing is done properly as it is normal SPI communication, but while reading its responding with random data! The above receive function transmits dummy and then receive and I guess that it might be reason that its responding with some garbage data.
I have seen the read sequence given in datasheet of it, And after sending the instruction and address from where to read, the datasheet suggests that Master clock should be on after above sequence but how this can be done? How can I keep SPI clock ON without sending data?
Notes:
Link to datasheet:
2018-08-11 06:08 AM
Typically you can send/receive data with HAL_SPI_TransmitReceive(), and you stuff dummy bytes onto the channel to generate clocks to recover the data sent back
2018-08-12 01:55 PM
I use this code for a similar chip.
void ReadManufacturernDeviceID(void){
char buf[8];
readFlashCommandBytes(ReadFlashManDevID, 3, &buf[0]);
int length = terminalLF(length, &string[0]);
length += sprintf(string + length, "Manufacturer and Device IDs %02x %02x %02x ", buf[0], buf[1], buf[2]);
length = terminalLF(length, &string[0]);
puts1(string);
}
void readFlashCommandBytes(char flashCommand, char byteCount, char*ptr){
HAL_GPIO_WritePin(AT25DN256_nSS_GPIO_Port, AT25DN256_nSS_Pin, GPIO_PIN_RESET);
transfer(flashCommand);
char dummyByte = 0;
do
{
*ptr++ = transfer_receive(dummyByte);
} while (byteCount--);
//putc1(RxSPI);
HAL_GPIO_WritePin(AT25DN256_nSS_GPIO_Port, AT25DN256_nSS_Pin, GPIO_PIN_SET);
}
void transfer(unsigned short data) {
char RxSPI;
while (!(hspi1.Instance->SR & SPI_FLAG_TXE)) ;
*((__IO uint8_t *)&hspi1.Instance->DR) = data;
RxSPI = hspi1.Instance->DR;// + readSPI_SR; // read Rx byte and discard, only clearing the buffer
}
char transfer_receive(unsigned short data) { // send a dummy byte to receive a byte
char RxSPI;
while(!(hspi1.Instance->SR & SPI_FLAG_TXE))
;
while ((hspi1.Instance->SR & SPI_FLAG_RXNE)) // load all the bytes received, only keep the last one
RxSPI = hspi1.Instance->DR; //empty fifo
*((__IO uint8_t *)&hspi1.Instance->DR) = data; // force the SPI to transceive 8 bit
while(!(hspi1.Instance->SR & SPI_FLAG_TXE)) // wait to transmitter double buffer to shift into transmitter
;
while ((hspi1.Instance->SR & SPI_FLAG_BSY)) ; // wait for data to shift out of the processor pins
while((hspi1.Instance->SR & SPI_FLAG_RXNE)) // load all the bytes received, only keep the last one
RxSPI = hspi1.Instance->DR; // we only want the last byte
return RxSPI;
}