Если сделать вот так: Код (C++): uint8_t data[] {0x01, 0x00, 0x05, 0x00, 0x08, 0x1C, 0x00, 0x58, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x23, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x43, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xE8, 0x01, 0x00, 0x01, 0x08, 0x20, 0x01, 0xFF, 0x68, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}; Serial.println("Writing data"); digitalWrite(chipSelectPin, LOW); SPI2.transfer(COMMAND_WRITE_DATA); SPI2.transfer(0); // First address byte SPI2.transfer(63); // Second address byte for (int i = 0; i < 64; i++) { SPI2.transfer(data[I]); } digitalWrite(chipSelectPin, HIGH); delay(15); digitalWrite(chipSelectPin, LOW); SPI2.transfer(WRDI); digitalWrite(chipSelectPin, HIGH); digitalWrite(chipSelectPin, LOW); SPI2.transfer(COMMAND_WRITE_ENABLE); digitalWrite(chipSelectPin, HIGH); digitalWrite(chipSelectPin, LOW); SPI2.transfer(COMMAND_WRITE_DATA); SPI2.transfer(64); // First address byte SPI2.transfer(127); // Second address byte for (int i = 64; i < 128; i++) { SPI2.transfer(data[I]); } digitalWrite(chipSelectPin, HIGH); delay(15); Serial.println("Reading data"); digitalWrite(chipSelectPin, LOW); SPI2.transfer(COMMAND_READ_DATA); SPI2.transfer(0); // First address byte SPI2.transfer(63); // Second address byte for (int i = 0; i < 64; i++) { Serial.println(SPI2.transfer(i), HEX); } digitalWrite(chipSelectPin, HIGH); delay(15); digitalWrite(chipSelectPin, LOW); SPI2.transfer(COMMAND_READ_DATA); SPI2.transfer(64); // First address byte SPI2.transfer(127); // Second address byte for (int i = 0; i < 64; i++) { Serial.println(SPI2.transfer(i), HEX); } digitalWrite(chipSelectPin, HIGH); delay(15); digitalWrite(WP, LOW); SPI2.endTransaction(); Код частично только записывает данные, вот что выдаёт порт: Код (C++): 1 //Откуда-то лишняя еденичка 1 0 5 0 8 1C 0 58 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 0 23 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 //Последний байт первой "порции" 1 //Опять лишняя еденичка 1 1 43 0 4 0 0 0 0 0 1 0 0 0 E8 1 0 1 8 20 1 FF 68 0 2 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 //Здесь кончается вторая "порция" 0 //Далее какие-то загадочные еденички 0 0 0 0 0 0 0 0
Адрес страницы должен быть выровнен по границе в 64 байта. Так делать не корректно Код (C++): SPI2.transfer(0); // First address byte SPI2.transfer(63); // Second address byte И перед каждой записью страницы надо делать Код (C++): digitalWrite(chipSelectPin, LOW); SPI2.transfer(COMMAND_WRITE_ENABLE); digitalWrite(chipSelectPin, HIGH);
Разве Фигура7-7 Write Timing не позволяет без передёргивания СS писать? Я понимал, что Instruction и есть COMMAND_WRITE_ENABLE
Для записи надо 2 инструкции- WREN без параметров, а потом WRITE. Write Sequence (WRITE): In order to program the AT25128B/256B, the Write Protect pin (WP) must be held high and two separate instructions must be executed. First, the device must be write enabled via the WREN instruction. Then a Write (WRITE) instruction may be executed.
Сейчас сделал так и монитор порта выдал правильные значения: Код (C++): void setup() { Serial.begin(115200); pinMode(WP, OUTPUT); pinMode(chipSelectPin, OUTPUT); SPI2.begin(); SPI2.beginTransaction(SPISettings(5000000, MSBFIRST, SPI_MODE0)); pinMode(chipSelectPin, OUTPUT); digitalWrite(chipSelectPin, HIGH); Serial.println("Write enable"); digitalWrite(chipSelectPin, LOW); digitalWrite(WP, HIGH); SPI2.transfer(COMMAND_WRITE_ENABLE); digitalWrite(chipSelectPin, HIGH); uint8_t data[] {0x01, 0x00, 0x05, 0x00, 0x08, 0x1C, 0x00, 0x58, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x23, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x43, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xE8, 0x01, 0x00, 0x01, 0x08, 0x20, 0x01, 0xFF, 0x68, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00}; Serial.println("Writing data"); digitalWrite(chipSelectPin, LOW); SPI2.transfer(COMMAND_WRITE_DATA); SPI2.transfer(0); // First address byte SPI2.transfer(64); // Second address byte for (int i = 0; i < 64; i++) { SPI2.transfer(data[i]); } digitalWrite(chipSelectPin, HIGH); delay(15); digitalWrite(chipSelectPin, LOW); SPI2.transfer(WRDI); digitalWrite(chipSelectPin, HIGH); digitalWrite(chipSelectPin, LOW); SPI2.transfer(COMMAND_WRITE_ENABLE); digitalWrite(chipSelectPin, HIGH); digitalWrite(chipSelectPin, LOW); SPI2.transfer(COMMAND_WRITE_DATA); SPI2.transfer(63); // First address byte SPI2.transfer(128); // Second address byte for (int i = 64; i < 128; i++) { SPI2.transfer(data[i]); } digitalWrite(chipSelectPin, HIGH); delay(15); Serial.println("Reading data"); digitalWrite(chipSelectPin, LOW); SPI2.transfer(COMMAND_READ_DATA); SPI2.transfer(0); // First address byte SPI2.transfer(64); // Second address byte for (int i = 0; i < 64; i++) { Serial.println(SPI2.transfer(i), HEX); } digitalWrite(chipSelectPin, HIGH); delay(15); digitalWrite(chipSelectPin, LOW); SPI2.transfer(COMMAND_READ_DATA); SPI2.transfer(63); // First address byte SPI2.transfer(128); // Second address byte for (int i = 0; i < 64; i++) { Serial.println(SPI2.transfer(i), HEX); } digitalWrite(chipSelectPin, HIGH); delay(15); digitalWrite(WP, LOW); SPI2.endTransaction(); }
Но иногда, если нажимать EN на ESP, в порт выходят немного другие данные. Бывает так: Код (C++): 1 0 5 0 8 1C 0 58 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 0 23 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 43 0 4 0 0 0 0 0 1 0 0 0 E8 1 0 1 8 20 1 FF 68 0 2 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 Так: Код (C++): 1 0 5 0 8 1C 0 58 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 0 23 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF И так: Код (C++): 1 0 0 0 0 0 0 0 0 0 0 1 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 43 0 4 0 0 0 0 0 1 0 0 0 E8 1 0 1 8 20 1 FF 68 0 2 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 Это связано с задержкой на чтение? Или проблема в другом?
Так это ресет. Ну если нажать его в процессе записи, то естественно, что может что-то не так записаться.