Hi everybody,
I am not sure if is it right forum for my question.
I am using XMEGA 128A4U MCU. I want use SPI. (with external Flash)
SPI initialize is ok, but when I use command spi_read_packet it is read value (wrong value) to the first cell - to the buffer_tx_servis[0]. Remain cells have value 0.
My original sub is here:
uint8_t readID_SPIFlash(void){ uint8_t result = 1; uint8_t buffer_tx_servis[4]; ioport_set_pin_low(SS_SPI_EEPROM); // Select Flash asm("NOP"); spi_write_single(pFlash_SPI, SPI_FLASH_READ_ID); // Send cmd - ask for ID spi_read_packet(pFlash_SPI, buffer_tx_servis, 4); // Read ID - it is 4 bytes ioport_set_pin_high(SS_SPI_EEPROM); // SS to high asm("NOP"); result = (buffer_tx_servis[0] == 0x1f) ? result * 1 : 0; result = (buffer_tx_servis[1] == 0x47) ? result * 1 : 0; result = (buffer_tx_servis[2] == 0x01) ? result * 1 : 0; asm("NOP"); asm("NOP"); asm("NOP"); return result; }
When I try rewrite my sub and replace spi_read_packet by 4 x spi_read_single, SW read all values and ID is OK
(new SUB)
uint8_t readID_SPIFlash(void){ uint8_t result = 1; uint8_t buffer_tx_servis[4]; ioport_set_pin_low(SS_SPI_EEPROM); // Select Flash asm("NOP"); spi_write_single(pFlash_SPI, SPI_FLASH_READ_ID); // Send cmd - ask for ID spi_read_single(pFlash_SPI, &buffer_tx_servis[0]); // Read ID - it is 4 bytes spi_read_single(pFlash_SPI, &buffer_tx_servis[1]); spi_read_single(pFlash_SPI, &buffer_tx_servis[2]); spi_read_single(pFlash_SPI, &buffer_tx_servis[3]); ioport_set_pin_high(SS_SPI_EEPROM); // SS to high asm("NOP"); result = (buffer_tx_servis[0] == 0x1f) ? result * 1 : 0; result = (buffer_tx_servis[1] == 0x47) ? result * 1 : 0; result = (buffer_tx_servis[2] == 0x01) ? result * 1 : 0; asm("NOP"); asm("NOP"); asm("NOP"); return result; }
I think there is bug in my C code, but I can not find it.
Any advice ?
Thanks