본문 바로가기

하드웨어/Arduino

아두이노 Leonardo 로 spiflash 시험, chatGPT 활용

반응형

 

chatGPT에 물어 보았다 (  chat.openai.com )

 

arduino로 spiflash 128MB 전영역을 쓰고 읽어서 비교하는 프로그램 작성

 

spiFlash.ino 

#include <SPI.h>

const int chipSelect = 10;
const int bufferSize = 256;
byte buffer[bufferSize];

void setup() {
  Serial.begin(115200);
  SPI.begin();
  pinMode(chipSelect, OUTPUT);

  // Fill the buffer with some test data
  for (int i = 0; i < bufferSize; i++) {
    buffer[i] = i;
  }

  // Write the buffer to the flash memory
  for (int address = 0; address < 128 * 1024 * 1024; address += bufferSize) {
    writeFlash(address, buffer, bufferSize);
  }
}

void loop() {
  // Read the flash memory and compare it to the buffer
  for (int address = 0; address < 128 * 1024 * 1024; address += bufferSize) {
    readFlash(address, buffer, bufferSize);
    for (int i = 0; i < bufferSize; i++) {
      if (buffer[i] != (address + i) % 256) {
        Serial.println("Mismatch at address " + String(address + i, HEX) + ": expected " + String(buffer[i], HEX) + " but got " + String((address + i) % 256, HEX));
        while (true);
      }
    }
  }

  Serial.println("Verification complete");
}

void writeFlash(int address, byte* data, int length) {
  digitalWrite(chipSelect, LOW);

  SPI.transfer(0x06); // Write enable
  digitalWrite(chipSelect, HIGH);

  digitalWrite(chipSelect, LOW);
  SPI.transfer(0x02); // Write instruction
  SPI.transfer((address >> 16) & 0xff);
  SPI.transfer((address >> 8) & 0xff);
  SPI.transfer(address & 0xff);
  for (int i = 0; i < length; i++) {
    SPI.transfer(data[i]);
  }
  digitalWrite(chipSelect, HIGH);

  // Wait for the write to complete
  digitalWrite(chipSelect, LOW);
  SPI.transfer(0x05);
  while ((SPI.transfer(0xff) & 0x01) != 0) {
  }
  digitalWrite(chipSelect, HIGH);
}

void readFlash(int address, byte* data, int length) {
  digitalWrite(chipSelect, LOW);
  SPI.transfer(0x03); // Read instruction
  SPI.transfer((address >> 16) & 0xff);
  SPI.transfer((address >> 8) & 0xff);
  SPI.transfer(address & 0xff);
  for (int i = 0; i < length; i++) {
    data[i] = SPI.transfer(0xff);
  }
  digitalWrite(chipSelect, HIGH);
}

 

 

프로그램은 만들어 졌다.

아누이노로 돌려 보면 에러는 없다

 

하드웨어 구성을 위해 다시 질문 했다 

 

 

연결하는 핀번호를 알아야 한다. 또 질문 

헉!!!   아두이노 UNO 보드가 PC와 연결이 안된다.( 아두이노 UNO 보드 고장 !!)

Leonardo 보드로 교체...

spiFlash의 핀번호도 알아야 한다

그래서 또 질문

핀번호가 아닌 신호이름만 알려 준다

할수 없이 데이터 시트 확인

 

 

Arduino Leonardo   W25N01GVxxIG/IT
/CS 10   1 /CS
DO 12   2 DO
GND GND   4 GND
MOSI 11   5 DI
SCK 13   6 CLK
5V VCC   8 VCC   

 

SPI Flash와 Arduino Leonardo 연결
W25N01GVxxIG/IT&nbsp; SPI Flash 연결 (바닥면)
아두이노 Leonardo 연결

https://youtu.be/IPrk7nD4plQ

 

코드 약간 수정

//
// SPI Flash test program
// 
// arduino로 spiflash 128MB 전영역을 쓰고 읽어서 비교하는 프로그램
//
// Board : Arduino UNO
// Device : SPI Flash    WinBoand - W25N01GVZEIG    8-pad WSON 8x6-mm
// 
//  PAD NO.       PAD NAME    I/O FUNCTION
//  1 /CS         I           Chip Select Input
//  2  DO (IO1)   I/O         Data Output (Data Input Output 1)(1)
//  3 /WP (IO2)   I/O         Write Protect Input ( Data Input Output 2)(2)
//  4  GND                    Ground
//  5  DI         (IO0)       I/O Data Input (Data Input Output 0)(1)
//  6  CLK        I           Serial Clock Input
//  7 /HOLD       (IO3)       I/O Hold Input (Data Input Output 3)(2)
//  8  VCC        Power       Supply 
//
//
//  Standard SPI    : CLK, /CS, DI, DO, /WP, /Hold
//
//  Dual SPI        : Dual SPI: CLK, /CS, IO0, IO1, /WP, /Hold 
//
//  Quad SPI        : CLK, /CS, IO0, IO1, IO2, IO3
//
//

#include <SPI.h>

const int chipSelect = 10;
const int bufferSize = 256;
byte buffer[bufferSize];
  unsigned long k=0;				//  추가


void setup() {
  Serial.begin(115200);
  SPI.begin();
  pinMode(chipSelect, OUTPUT);

}

void loop() {



  // Fill the buffer with some test data
  for (int i = 0; i < bufferSize; i++) {
    buffer[i] = i;
  }

  // Write the buffer to the flash memory
  for (int address = 0; address < 128 * 1024 * 1024; address += bufferSize) {
    writeFlash(address, buffer, bufferSize);
  }

  // Read the flash memory and compare it to the buffer
  for (int address = 0; address < 128 * 1024 * 1024; address += bufferSize) {
    readFlash(address, buffer, bufferSize);
    for (int i = 0; i < bufferSize; i++) {
      if (buffer[i] != (address + i) % 256) {
        Serial.println("Mismatch at address " + String(address + i, HEX) + ": expected " + String(buffer[i], HEX) + " but got " + String((address + i) % 256, HEX));
        while (true);
      }
    }
  }

  Serial.print(k++);                             //   추가
  Serial.println("  Verification complete"); 
}

void writeFlash(int address, byte* data, int length) {
  digitalWrite(chipSelect, LOW);

  SPI.transfer(0x06); // Write enable
  digitalWrite(chipSelect, HIGH);

  digitalWrite(chipSelect, LOW);
  SPI.transfer(0x02); // Write instruction
  SPI.transfer((address >> 16) & 0xff);
  SPI.transfer((address >> 8) & 0xff);
  SPI.transfer(address & 0xff);
  for (int i = 0; i < length; i++) {
    SPI.transfer(data[i]);
  }
  digitalWrite(chipSelect, HIGH);

  // Wait for the write to complete
  digitalWrite(chipSelect, LOW);
  SPI.transfer(0x05);
  while ((SPI.transfer(0xff) & 0x01) != 0) {
  }
  digitalWrite(chipSelect, HIGH);
}

void readFlash(int address, byte* data, int length) {
  digitalWrite(chipSelect, LOW);
  SPI.transfer(0x03); // Read instruction
  SPI.transfer((address >> 16) & 0xff);
  SPI.transfer((address >> 8) & 0xff);
  SPI.transfer(address & 0xff);
  for (int i = 0; i < length; i++) {
    data[i] = SPI.transfer(0xff);
  }
  digitalWrite(chipSelect, HIGH);
}

 

https://youtu.be/IPrk7nD4plQ

 

반응형