diff --git a/bootloader/RAK4630/Latest/wiscore_rak4631_board_bootloader-0.4.3_s140_6.1.1.uf2.zip b/bootloader/RAK4630/Latest/wiscore_rak4631_board_bootloader-0.4.3_s140_6.1.1.uf2.zip new file mode 100644 index 00000000..1f63ebe7 Binary files /dev/null and b/bootloader/RAK4630/Latest/wiscore_rak4631_board_bootloader-0.4.3_s140_6.1.1.uf2.zip differ diff --git a/examples/RAK4630/communications/LoRa/LoRaP2P/LoRa-P2P-Simple-Ard/LoRa-P2P-Simple-Ard.ino b/examples/RAK4630/communications/LoRa/LoRaP2P/LoRa-P2P-Simple-Ard/LoRa-P2P-Simple-Ard.ino new file mode 100644 index 00000000..119e63de --- /dev/null +++ b/examples/RAK4630/communications/LoRa/LoRaP2P/LoRa-P2P-Simple-Ard/LoRa-P2P-Simple-Ard.ino @@ -0,0 +1,254 @@ +/** + * @file LoRa-P2P-Simple-Ard.ino + * @author Bernd Giesecke (bernd@giesecke.tk) + * @brief Simple LoRa P2P example with CAD + * @version 0.1 + * @date 2024-06-05 + * + * @copyright Copyright (c) 2024 + * + */ +#include +#ifdef _VARIANT_RAK4630_ +#include +#endif +#include +#include + +// Function declarations +void OnTxDone(void); +void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr); +void OnTxTimeout(void); +void OnRxTimeout(void); +void OnRxError(void); +void OnCadDone(bool cadResult); + +// Define LoRa parameters +#define RF_FREQUENCY 916100000 // Hz +#define TX_OUTPUT_POWER 22 // dBm +#define LORA_BANDWIDTH 0 // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved] +#define LORA_SPREADING_FACTOR 7 // [SF7..SF12] +#define LORA_CODINGRATE 1 // [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8] +#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx +#define LORA_SYMBOL_TIMEOUT 0 // Symbols +#define LORA_FIX_LENGTH_PAYLOAD false // No fixed payload length +#define LORA_IQ_INVERSION false // No IQ inversion +#define TX_TIMEOUT_VALUE 120000 +#define RX_TIMEOUT_VALUE 30000 +#define BUFFER_SIZE 64 // Define the payload size here + +static RadioEvents_t RadioEvents; +static uint16_t BufferSize = BUFFER_SIZE; +static uint8_t RcvBuffer[BUFFER_SIZE]; +static uint8_t TxdBuffer[BUFFER_SIZE]; + +time_t cadTime; + +/** + * @brief Arduino setup function. Called once after power-up or reset + * + */ +void setup() +{ + pinMode(LED_GREEN, OUTPUT); + digitalWrite(LED_GREEN, LOW); + pinMode(LED_BLUE, OUTPUT); + digitalWrite(LED_BLUE, LOW); + + time_t serial_timeout = millis(); + // On nRF52840 the USB serial is not available immediately + while (!Serial) + { + if ((millis() - serial_timeout) < 5000) + { + delay(100); + digitalWrite(LED_GREEN, !digitalRead(LED_GREEN)); + } + else + { + break; + } + } + digitalWrite(LED_GREEN, LOW); + + Serial.println("====================================="); + Serial.println("SX126x LoRa P2P test"); + Serial.println("====================================="); + + // Initialize the LoRa chip + Serial.println("Starting lora_hardware_init"); + +#ifdef _VARIANT_RAK4630_ + lora_rak4630_init(); +#endif +#ifdef _VARIANT_RAK11200_ + lora_rak13300_init(); +#endif +#ifdef _VARIANT_RAK11300_ + lora_rak11300_init(); +#endif + + // Initialize the Radio callbacks + RadioEvents.TxDone = OnTxDone; + RadioEvents.RxDone = OnRxDone; + RadioEvents.TxTimeout = OnTxTimeout; + RadioEvents.RxTimeout = OnRxTimeout; + RadioEvents.RxError = OnRxError; + RadioEvents.CadDone = OnCadDone; + + // Initialize the Radio + Radio.Init(&RadioEvents); + + // Set Radio channel + Radio.SetChannel(RF_FREQUENCY); + + // Set Radio TX configuration + Radio.SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, + LORA_SPREADING_FACTOR, LORA_CODINGRATE, + LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD, + true, 0, 0, LORA_IQ_INVERSION, TX_TIMEOUT_VALUE); + + // Set Radio RX configuration + Radio.SetRxConfig(MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, + LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, + LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD, + 0, true, 0, 0, LORA_IQ_INVERSION, true); + + // Start LoRa + Serial.println("Starting Radio.Rx"); + Radio.Rx(0); +} + +/** + * @brief Arduino loop function. Sleeps until semaphore is given by timer. + * Then starts sending a packet by starting CAD (channel activity detection) + * + */ +void loop() +{ + // Sleep until we are woken up by an event + delay(5000); + + /******************************************************************************/ + /* Recommended, instead of just sending, use CAD (channel activity detection) */ + /** The packet will be sent if CAD returns no activity in the CAD callback */ + /** OnCadDone */ + /******************************************************************************/ + // Check if our channel is available for sending + Radio.Standby(); + Radio.SetCadParams(LORA_CAD_08_SYMBOL, LORA_SPREADING_FACTOR + 13, 10, LORA_CAD_ONLY, 0); + cadTime = millis(); + Radio.StartCad(); + + /***************************************************************************/ + /* Optional, just send data, can lead to collisions and loss of data */ + /***************************************************************************/ + // // Send the next PING frame + // TxdBuffer[0] = 'P'; + // TxdBuffer[1] = 'I'; + // TxdBuffer[2] = 'N'; + // TxdBuffer[3] = 'G'; + // TxdBuffer[4] = 0x00; + + // Radio.Send(TxdBuffer, 5); + + digitalWrite(LED_GREEN, HIGH); +} + +/**@brief Function to be executed on Radio Tx Done event + */ +void OnTxDone(void) +{ + Serial.println("OnTxDone"); + + // Back to RX + Radio.Rx(0); +} + +/**@brief Function to be executed on Radio Rx Done event + */ +void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) +{ + Serial.println("OnRxDone"); + + delay(10); + BufferSize = size; + memcpy(RcvBuffer, payload, BufferSize); + + Serial.printf("RssiValue=%d dBm, SnrValue=%d\n", rssi, snr); + + for (int idx = 0; idx < size; idx++) + { + Serial.printf("%02X ", RcvBuffer[idx]); + } + Serial.println(""); + + digitalWrite(LED_GREEN, LOW); + + // Back to RX + Radio.Rx(0); +} + +/**@brief Function to be executed on Radio Tx Timeout event + */ +void OnTxTimeout(void) +{ + // Radio.Sleep(); + Serial.println("OnTxTimeout"); + + digitalWrite(LED_GREEN, LOW); + + // Back to RX + Radio.Rx(0); +} + +/**@brief Function to be executed on Radio Rx Timeout event + */ +void OnRxTimeout(void) +{ + Serial.println("OnRxTimeout"); + + digitalWrite(LED_GREEN, LOW); + + // Back to RX + Radio.Rx(0); +} + +/**@brief Function to be executed on Radio Rx Error event + */ +void OnRxError(void) +{ + Serial.println("OnRxError"); + + digitalWrite(LED_GREEN, LOW); + + // Back to RX + Radio.Rx(0); +} + +/**@brief Function to be executed on CAD Done event + */ +void OnCadDone(bool cadResult) +{ + time_t duration = millis() - cadTime; + if (cadResult) + { + Serial.printf("CAD returned channel busy after %ldms\n", duration); + + // Back to RX + Radio.Rx(0); + } + else + { + Serial.printf("CAD returned channel free after %ldms\n", duration); + + // Send the next PING frame + TxdBuffer[0] = 'P'; + TxdBuffer[1] = 'I'; + TxdBuffer[2] = 'N'; + TxdBuffer[3] = 'G'; + TxdBuffer[4] = 0x00; + + Radio.Send(TxdBuffer, 5); + } +} diff --git a/examples/common/sensors/RAK15006-FRAM-4MBit-MB85RS4MT/RAK15006_FRAM_Read_Write_MB85RS4MT/RAK15006_FRAM_Read_Write_MB85RS4MT.ino b/examples/common/sensors/RAK15006-FRAM-4MBit-MB85RS4MT/RAK15006_FRAM_Read_Write_MB85RS4MT/RAK15006_FRAM_Read_Write_MB85RS4MT.ino index ba447390..bf7bc05a 100644 --- a/examples/common/sensors/RAK15006-FRAM-4MBit-MB85RS4MT/RAK15006_FRAM_Read_Write_MB85RS4MT/RAK15006_FRAM_Read_Write_MB85RS4MT.ino +++ b/examples/common/sensors/RAK15006-FRAM-4MBit-MB85RS4MT/RAK15006_FRAM_Read_Write_MB85RS4MT/RAK15006_FRAM_Read_Write_MB85RS4MT.ino @@ -1,176 +1,176 @@ -/** - @file RAK15006_FRAM_Read_Write_MB85RS4MT.ino - @author rakwireless.com - @brief FRAM_4M Read And Write Test - @version 0.1 - @date 2022-06-06 - @copyright Copyright (c) 2022 -**/ - -#include -#include "Adafruit_FRAM_SPI.h" //http://librarymanager/All#Adafruit_FRAM_SPI version:2.4.1 http://librarymanager/All#Adafruit_BusIO version:1.11.6 - -#define FRAM_WP_PIN WB_IO1 //SlotA installation, please do not use it on SLOTB -//#define FRAM_WP_PIN WB_IO3 //SlotC installation. -//#define FRAM_WP_PIN WB_IO5 //SlotD installation. - -#define FRAM_4M_SIZE 0x80000 - -/* Example code for the Adafruit SPI FRAM breakout */ -uint8_t FRAM_CS = SS; - -Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(FRAM_CS, &SPI, 10000000); // use hardware SPI - -uint8_t FRAM_SCK = SCK; -uint8_t FRAM_MISO = MISO; -uint8_t FRAM_MOSI = MOSI; -//Or use software SPI, any pins! -//Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(FRAM_SCK, FRAM_MISO, FRAM_MOSI, FRAM_CS); - - -/* - Writing protect block for WRITE command is configured by the value of BP0 and BP1 in the status register - data={ - 0x00 None - 0x04 60000H to 7FFFFH (upper 1/4) - 0x08 40000H to 7FFFFH (upper 1/2) - 0x0C 00000H to 7FFFFH (all) - } -*/ -void writeBlockProtect(uint8_t data) -{ - fram.writeEnable(true); - fram.setStatusRegister(data); - fram.writeEnable(false); -} - -/* - @brief Comparing whether the read and write content is consistent. - Can be used to test the probability of FRAM read and write errors. -*/ -void readWriteTest(void) -{ - char wBuf[32] = "<<>>"; - char rBuf[32] = {0}; - uint32_t successCount = 0; - uint32_t failCount = 0; - - float progress = 0; - time_t interval = millis(); - - fram.writeEnable(true); - for (uint32_t i = 0; i < FRAM_4M_SIZE; i += sizeof(wBuf) / sizeof(char)) - { - fram.write(i, (uint8_t*)wBuf , sizeof(wBuf) / sizeof(char)); - fram.read( i, (uint8_t*)rBuf , sizeof(rBuf) / sizeof(char)); - - if (memcmp(wBuf , rBuf , sizeof(rBuf)) == 0) - { - successCount++; - } - else - { - failCount++; - } - if ((millis() - interval) > 100) - { - interval = millis(); - Serial.printf("Test progress: %5.2f%% , successCount: %ld , failCount:%ld \n", progress, successCount, failCount); - } - progress = (float)(i + sizeof(wBuf) / sizeof(char)) * 100 / FRAM_4M_SIZE; - memset(rBuf , '0' , sizeof(rBuf) / sizeof(char)); - delay(1); - } - fram.writeEnable(false); - Serial.printf("Test progress: %5.2f%% , successCount: %ld , failCount:%ld \n", progress, successCount, failCount); -} - -/* - @brief Read the contents of the entire chip. -*/ -void readEntireChip(void) -{ - char readBuf[32] = {0}; - Serial.println(); - fram.writeEnable(true); - for (uint32_t i = 0; i < FRAM_4M_SIZE; i += sizeof(readBuf)) - { - fram.read( i, (uint8_t*)readBuf , sizeof(readBuf)); - Serial.print("0x"); - Serial.print(i, HEX); - Serial.print("\t"); - for (uint32_t j = 0; j < sizeof(readBuf); j++) - { - Serial.print("0x"); - Serial.print(readBuf[j], HEX); - Serial.print(' '); - } - Serial.println(); - } - fram.writeEnable(false); -} - - -void setup(void) { - pinMode(WB_IO2, OUTPUT); - digitalWrite(WB_IO2, HIGH); // Enable power supply. - delay(300); - Serial.begin(115200); - - time_t serial_timeout = millis(); - while (!Serial) - { - if ((millis() - serial_timeout) < 5000) - { - delay(100); - } - else - { - break; - } - } - Serial.println("RAK15006 FRAM_4M Read And Write Test "); - if (fram.begin()) { - Serial.println("Found SPI FRAM"); - } else { - Serial.println("No SPI FRAM found ... check your connections\r\n"); - while (1) - { - delay(500); - } - } - writeBlockProtect(0x00); -} - -void loop(void) { - - char wData[25] = "RAK15006 FRAM_4M TEST"; - char rDate[25] = {0}; - Serial.println("reading and writing test"); - fram.writeEnable(true); - fram.write(0x0000, (uint8_t*)wData , sizeof(wData) / sizeof(char)); - delay(1); - fram.read( 0x0000, (uint8_t*)rDate , sizeof(rDate) / sizeof(char)); - Serial.println(rDate); - - delay(10); - char wDataAgain[30] = "RAK15006 FRAM_4M TEST Again"; - char rDataAgain[30] = {0}; - fram.write(0x0000, (uint8_t*)wDataAgain , sizeof(wDataAgain) / sizeof(char)); - fram.read( 0x0000, (uint8_t*)rDataAgain , sizeof(rDataAgain) / sizeof(char)); - Serial.println(rDataAgain); - fram.writeEnable(false); - Serial.println(); - - Serial.println("Comparing whether the read and write content is consistent."); - readWriteTest(); - Serial.println(); - - Serial.println("Read the contents of the entire chip."); - readEntireChip(); - Serial.println("Read complete,if you want to test again,please reset the module"); - while (1) - { - delay(500); - } -} +/** + @file RAK15006_FRAM_Read_Write_MB85RS4MT.ino + @author rakwireless.com + @brief FRAM_4M Read And Write Test + @version 0.1 + @date 2022-06-06 + @copyright Copyright (c) 2022 +**/ + +#include +#include "Adafruit_FRAM_SPI.h" //http://librarymanager/All#Adafruit_FRAM_SPI version:2.4.1 http://librarymanager/All#Adafruit_BusIO version:1.11.6 + +//#define FRAM_WP_PIN WB_IO1 //SlotA installation, please do not use it on SLOTB +//#define FRAM_WP_PIN WB_IO3 //SlotC installation. +#define FRAM_WP_PIN WB_IO5 //SlotD installation. + +#define FRAM_4M_SIZE 0x80000 + +/* Example code for the Adafruit SPI FRAM breakout */ +uint8_t FRAM_CS = SS; + +Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(FRAM_CS, &SPI, 10000000); // use hardware SPI + +uint8_t FRAM_SCK = SCK; +uint8_t FRAM_MISO = MISO; +uint8_t FRAM_MOSI = MOSI; +//Or use software SPI, any pins! +//Adafruit_FRAM_SPI fram = Adafruit_FRAM_SPI(FRAM_SCK, FRAM_MISO, FRAM_MOSI, FRAM_CS); + + +/* + Writing protect block for WRITE command is configured by the value of BP0 and BP1 in the status register + data={ + 0x00 None + 0x04 60000H to 7FFFFH (upper 1/4) + 0x08 40000H to 7FFFFH (upper 1/2) + 0x0C 00000H to 7FFFFH (all) + } +*/ +void writeBlockProtect(uint8_t data) +{ + fram.writeEnable(true); + fram.setStatusRegister(data); + fram.writeEnable(false); +} + +/* + @brief Comparing whether the read and write content is consistent. + Can be used to test the probability of FRAM read and write errors. +*/ +void readWriteTest(void) +{ + char wBuf[32] = "<<>>"; + char rBuf[32] = {0}; + uint32_t successCount = 0; + uint32_t failCount = 0; + + float progress = 0; + time_t interval = millis(); + + fram.writeEnable(true); + for (uint32_t i = 0; i < FRAM_4M_SIZE; i += sizeof(wBuf) / sizeof(char)) + { + fram.write(i, (uint8_t*)wBuf , sizeof(wBuf) / sizeof(char)); + fram.read( i, (uint8_t*)rBuf , sizeof(rBuf) / sizeof(char)); + + if (memcmp(wBuf , rBuf , sizeof(rBuf)) == 0) + { + successCount++; + } + else + { + failCount++; + } + if ((millis() - interval) > 100) + { + interval = millis(); + Serial.printf("Test progress: %5.2f%% , successCount: %ld , failCount:%ld \n", progress, successCount, failCount); + } + progress = (float)(i + sizeof(wBuf) / sizeof(char)) * 100 / FRAM_4M_SIZE; + memset(rBuf , '0' , sizeof(rBuf) / sizeof(char)); + delay(1); + } + fram.writeEnable(false); + Serial.printf("Test progress: %5.2f%% , successCount: %ld , failCount:%ld \n", progress, successCount, failCount); +} + +/* + @brief Read the contents of the entire chip. +*/ +void readEntireChip(void) +{ + char readBuf[32] = {0}; + Serial.println(); + fram.writeEnable(true); + for (uint32_t i = 0; i < FRAM_4M_SIZE; i += sizeof(readBuf)) + { + fram.read( i, (uint8_t*)readBuf , sizeof(readBuf)); + Serial.print("0x"); + Serial.print(i, HEX); + Serial.print("\t"); + for (uint32_t j = 0; j < sizeof(readBuf); j++) + { + Serial.print("0x"); + Serial.print(readBuf[j], HEX); + Serial.print(' '); + } + Serial.println(); + } + fram.writeEnable(false); +} + + +void setup(void) { + pinMode(WB_IO2, OUTPUT); + digitalWrite(WB_IO2, HIGH); // Enable power supply. + delay(300); + Serial.begin(115200); + + time_t serial_timeout = millis(); + while (!Serial) + { + if ((millis() - serial_timeout) < 5000) + { + delay(100); + } + else + { + break; + } + } + Serial.println("RAK15006 FRAM_4M Read And Write Test "); + if (fram.begin()) { + Serial.println("Found SPI FRAM"); + } else { + Serial.println("No SPI FRAM found ... check your connections\r\n"); + while (1) + { + delay(500); + } + } + writeBlockProtect(0x00); +} + +void loop(void) { + + char wData[25] = "RAK15006 FRAM_4M TEST"; + char rDate[25] = {0}; + Serial.println("reading and writing test"); + fram.writeEnable(true); + fram.write(0x0000, (uint8_t*)wData , sizeof(wData) / sizeof(char)); + delay(1); + fram.read( 0x0000, (uint8_t*)rDate , sizeof(rDate) / sizeof(char)); + Serial.println(rDate); + + delay(10); + char wDataAgain[30] = "RAK15006 FRAM_4M TEST Again"; + char rDataAgain[30] = {0}; + fram.write(0x0000, (uint8_t*)wDataAgain , sizeof(wDataAgain) / sizeof(char)); + fram.read( 0x0000, (uint8_t*)rDataAgain , sizeof(rDataAgain) / sizeof(char)); + Serial.println(rDataAgain); + fram.writeEnable(false); + Serial.println(); + + Serial.println("Comparing whether the read and write content is consistent."); + readWriteTest(); + Serial.println(); + + Serial.println("Read the contents of the entire chip."); + readEntireChip(); + Serial.println("Read complete,if you want to test again,please reset the module"); + while (1) + { + delay(500); + } +} diff --git a/examples/common/sensors/RAK15007-FRAM-8Mbit-CY15B108QN/RAK15007_FRAM_Read_Write_CY15B108QN/RAK15007_FRAM_Read_Write_CY15B108QN.ino b/examples/common/sensors/RAK15007-FRAM-8Mbit-CY15B108QN/RAK15007_FRAM_Read_Write_CY15B108QN/RAK15007_FRAM_Read_Write_CY15B108QN.ino index 41b226b8..12e230f4 100644 --- a/examples/common/sensors/RAK15007-FRAM-8Mbit-CY15B108QN/RAK15007_FRAM_Read_Write_CY15B108QN/RAK15007_FRAM_Read_Write_CY15B108QN.ino +++ b/examples/common/sensors/RAK15007-FRAM-8Mbit-CY15B108QN/RAK15007_FRAM_Read_Write_CY15B108QN/RAK15007_FRAM_Read_Write_CY15B108QN.ino @@ -1,282 +1,282 @@ -/** - @file RAK15007_FRAM_Read_Write_CY15B108QN.ino - @author rakwireless.com - @brief FRAM_8M Read And Write Test - @version 0.1 - @date 2022-06-15 - @copyright Copyright (c) 2022 -**/ -#include "RAK15007_CY15B108QN.h" //http://librarymanager/All#RAK15007_CY15B108QN - -#define FRAM_WP_PIN WB_IO1 //SlotA installation, please do not use it on SLOTB -//#define FRAM_WP_PIN WB_IO3 //SlotC installation. -//#define FRAM_WP_PIN WB_IO5 //SlotD installation. - -#define FRAM_8M_SIZE 0x100000 -uint8_t fram_cy15b108_CS = SS; -RAK_FRAM_CY15B108QN fram_cy15b108 = RAK_FRAM_CY15B108QN(fram_cy15b108_CS); - -/* - @brief Comparing whether the read and write content is consistent. - Can be used to test the probability of FRAM read and write errors. -*/ -void readWriteTest(void) -{ - char wBuf[32] = "<<>>"; - char rBuf[32] = {0}; - uint32_t successCount = 0; - uint32_t failCount = 0; - - float progress = 0; - time_t interval = millis(); - - for (uint32_t addr = 0; addr < FRAM_8M_SIZE; addr += sizeof(wBuf) / sizeof(char)) - { - fram_cy15b108.writeEnable(true); - fram_cy15b108.write(addr, (uint8_t*)wBuf , sizeof(wBuf) / sizeof(char)); - fram_cy15b108.read( addr, (uint8_t*)rBuf , sizeof(rBuf) / sizeof(char)); - fram_cy15b108.writeEnable(false); - - if (memcmp(wBuf , rBuf , sizeof(rBuf)) == 0) - { - successCount++; - } - else - { - failCount++; - } - if ((millis() - interval) > 100) - { - interval = millis(); - Serial.printf("Test progress: %5.2f%% , successCount: %ld , failCount:%ld \n", progress, successCount, failCount); - } - progress = (float)(addr + sizeof(wBuf) / sizeof(char)) * 100 / FRAM_8M_SIZE; - memset(rBuf , '0' , sizeof(rBuf) / sizeof(char)); - delay(1); - } - Serial.printf("Test progress: %5.2f%% , successCount: %ld , failCount:%ld \n", progress, successCount, failCount); -} - -/* - @brief Read the contents of the entire chip. -*/ -void readEntireChip(void) -{ - char readBuf[32] = {0}; - Serial.println(); - - for (uint32_t addr = 0; addr < FRAM_8M_SIZE; addr += sizeof(readBuf)) - { - fram_cy15b108.read( addr, (uint8_t*)readBuf , sizeof(readBuf)); - Serial.print("0x"); - Serial.print(addr, HEX); - Serial.print("\t"); - for (uint32_t bufCount = 0; bufCount < sizeof(readBuf); bufCount++) - { - Serial.print("0x"); - Serial.print(readBuf[bufCount], HEX); - Serial.print(' '); - } - Serial.println(); - } -} - - -/* - Writing protect block for WRITE command is configured by the value of BP0 and BP1 in the status register - data={ - 0x00 None - 0x04 C0000h to FFFFFh (upper 1/4) - 0x08 80000h to FFFFFh (upper 1/2) - 0x0C 00000h to FFFFFh (all) - } -*/ -void writeBlockProtect(uint8_t data) -{ - fram_cy15b108.writeEnable(true); - fram_cy15b108.setStatusRegister(data); - fram_cy15b108.writeEnable(false); -} - -/* - @brief Comparing whether the read and write special sector content is consistent. - Can be used to test the probability of FRAM read and write errors. -*/ -void specialSectorWriteAndReadTest(void) -{ - char specialSector_wBuf[32] = ""; - char specialSector_rBuf[32] = {0}; - uint32_t successCount = 0; - uint32_t failCount = 0; - - float progress = 0; - - for (uint32_t addr = 0; addr < 0xFF; addr += sizeof(specialSector_wBuf) / sizeof(char)) - { - fram_cy15b108.writeEnable(true); - fram_cy15b108.specialSectorWrite(addr, (uint8_t*)specialSector_wBuf , sizeof(specialSector_wBuf) / sizeof(char)); - fram_cy15b108.specialSectorRead( addr, (uint8_t*)specialSector_rBuf , sizeof(specialSector_rBuf) / sizeof(char)); - fram_cy15b108.writeEnable(false); - - if (memcmp(specialSector_wBuf , specialSector_rBuf , sizeof(specialSector_rBuf)) == 0) - { - successCount++; - } - else - { - failCount++; - } - Serial.printf("Special Sector Write And Read Test Progress: %5.2f%% , successCount: %ld , failCount:%ld \n", progress, successCount, failCount); - progress = (float)(addr + sizeof(specialSector_wBuf) / sizeof(char)) * 100 / 0xff; - memset(specialSector_rBuf , '0' , sizeof(specialSector_rBuf) / sizeof(char)); - delay(100); - } -} - -/** - The CY15X108QN supports a FAST READ opcode (0Bh) that is provided for opcode compatibility with serial flash devices -*/ -void fastReadOperationTest(void) -{ - char readBuf[32] = {0}; - Serial.println(); - - for (uint32_t addr = 0; addr < FRAM_8M_SIZE; addr += sizeof(readBuf)) - { - fram_cy15b108.fastReadOperation( addr, (uint8_t*)readBuf , sizeof(readBuf)); - Serial.print("0x"); - Serial.print(addr, HEX); - Serial.print("\t"); - for (uint32_t bufCount = 0; bufCount < sizeof(readBuf); bufCount++) - { - Serial.print("0x"); - Serial.print(readBuf[bufCount], HEX); - Serial.print(' '); - } - Serial.println(); - } -} - - -void setup(void) { - pinMode(WB_IO2, OUTPUT); - digitalWrite(WB_IO2, HIGH); // Enable power supply. - delay(300); - pinMode(FRAM_WP_PIN, OUTPUT); - digitalWrite(FRAM_WP_PIN, HIGH); - Serial.begin(115200); - - time_t serial_timeout = millis(); - while (!Serial) - { - if ((millis() - serial_timeout) < 5000) - { - delay(100); - } - else - { - break; - } - } - Serial.println("RAK15007 FRAM_CY15B108QN TEST"); - if (fram_cy15b108.begin()) { - Serial.println("Found FRAM_CY15B108QN"); - } else { - Serial.println("FRAM_CY15B108QN is not connected, Please check your connections\r\n"); - while (1) - { - delay(500); - } - } - writeBlockProtect(0x00); - delay(1000); - - uint32_t fram_size = fram_cy15b108.getFramSize(); - if (fram_size != 0) - { - Serial.print("FRAM_CY15B108QN address size is "); - Serial.print(fram_size); - Serial.println(" bytes"); - Serial.println("The capacity of the FRAM is"); - Serial.print(fram_size); Serial.println(" bytes"); - Serial.print(fram_size / 1024); Serial.println(" kilobytes"); - Serial.print((fram_size * 8) / 1024); Serial.println(" kilobits"); - if (fram_size >= ((1024 * 1024) / 8)) { - Serial.print((fram_size * 8) / (1024 * 1024)); - Serial.println(" megabits"); - } - } - else - { - Serial.println("Unable to identify the device,Please check your connections"); - while (1) - { - delay(100); - } - } - Serial.println(); - fram_cy15b108.writeEnable(true); - fram_cy15b108.writeByte(0x00, 0x01); - uint8_t wByte = fram_cy15b108.readByte(0x00); - if (wByte == 0x01) - { - Serial.println("write byte successful "); - } - fram_cy15b108.writeEnable(false); -} - - -void loop(void) { - - uint8_t serialNumberBuf[8] = {0x00}; - fram_cy15b108.getSerialNumber(serialNumberBuf, sizeof(serialNumberBuf) / sizeof(uint8_t)); - Serial.print("serialNumberBuf=0x"); - for (uint8_t serialCount = 0; serialCount < 8; serialCount++) - { - Serial.printf("%02x", serialNumberBuf[serialCount]); // The factory default value for the 8-byte Serial Number is ‘0000000000000000h’. - } - Serial.println(); - delay(1000); - - char wData[25] = "RAK15007 FRAM_8M TEST"; - char rDate[25] = {0}; - Serial.println("reading and writing test"); - fram_cy15b108.writeEnable(true); - fram_cy15b108.write(0x0000, (uint8_t*)wData , sizeof(wData) / sizeof(char)); - delay(1); - fram_cy15b108.read( 0x0000, (uint8_t*)rDate , sizeof(rDate) / sizeof(char)); - Serial.println(rDate); - fram_cy15b108.writeEnable(false); - delay(1); - - char wDataAgain[30] = "RAK15007 FRAM_8M TEST Again"; - char rDataAgain[30] = {0}; - fram_cy15b108.writeEnable(true); - fram_cy15b108.write(0x0000, (uint8_t*)wDataAgain , sizeof(wDataAgain) / sizeof(char)); - fram_cy15b108.read( 0x0000, (uint8_t*)rDataAgain , sizeof(rDataAgain) / sizeof(char)); - Serial.println(rDataAgain); - fram_cy15b108.writeEnable(false); - Serial.println(); - - Serial.println("Comparing whether the read and write special sector content is consistent."); - specialSectorWriteAndReadTest(); - Serial.println(); - - Serial.println("Comparing whether the read and write content is consistent."); - readWriteTest(); - Serial.println(); - - delay(2000); - Serial.println("FAST Read the contents of the entire chip."); - fastReadOperationTest(); - Serial.println(); - - delay(2000); - Serial.println("Read the contents of the entire chip."); - readEntireChip(); - Serial.println("Read complete,if you want to test again,please reset the module"); - while (1) - { - delay(500); - } -} +/** + @file RAK15007_FRAM_Read_Write_CY15B108QN.ino + @author rakwireless.com + @brief FRAM_8M Read And Write Test + @version 0.1 + @date 2022-06-15 + @copyright Copyright (c) 2022 +**/ +#include "RAK15007_CY15B108QN.h" //http://librarymanager/All#RAK15007_CY15B108QN + +//#define FRAM_WP_PIN WB_IO1 //SlotA installation, please do not use it on SLOTB +//#define FRAM_WP_PIN WB_IO3 //SlotC installation. +#define FRAM_WP_PIN WB_IO5 //SlotD installation. + +#define FRAM_8M_SIZE 0x100000 +uint8_t fram_cy15b108_CS = SS; +RAK_FRAM_CY15B108QN fram_cy15b108 = RAK_FRAM_CY15B108QN(fram_cy15b108_CS); + +/* + @brief Comparing whether the read and write content is consistent. + Can be used to test the probability of FRAM read and write errors. +*/ +void readWriteTest(void) +{ + char wBuf[32] = "<<>>"; + char rBuf[32] = {0}; + uint32_t successCount = 0; + uint32_t failCount = 0; + + float progress = 0; + time_t interval = millis(); + + for (uint32_t addr = 0; addr < FRAM_8M_SIZE; addr += sizeof(wBuf) / sizeof(char)) + { + fram_cy15b108.writeEnable(true); + fram_cy15b108.write(addr, (uint8_t*)wBuf , sizeof(wBuf) / sizeof(char)); + fram_cy15b108.read( addr, (uint8_t*)rBuf , sizeof(rBuf) / sizeof(char)); + fram_cy15b108.writeEnable(false); + + if (memcmp(wBuf , rBuf , sizeof(rBuf)) == 0) + { + successCount++; + } + else + { + failCount++; + } + if ((millis() - interval) > 100) + { + interval = millis(); + Serial.printf("Test progress: %5.2f%% , successCount: %ld , failCount:%ld \n", progress, successCount, failCount); + } + progress = (float)(addr + sizeof(wBuf) / sizeof(char)) * 100 / FRAM_8M_SIZE; + memset(rBuf , '0' , sizeof(rBuf) / sizeof(char)); + delay(1); + } + Serial.printf("Test progress: %5.2f%% , successCount: %ld , failCount:%ld \n", progress, successCount, failCount); +} + +/* + @brief Read the contents of the entire chip. +*/ +void readEntireChip(void) +{ + char readBuf[32] = {0}; + Serial.println(); + + for (uint32_t addr = 0; addr < FRAM_8M_SIZE; addr += sizeof(readBuf)) + { + fram_cy15b108.read( addr, (uint8_t*)readBuf , sizeof(readBuf)); + Serial.print("0x"); + Serial.print(addr, HEX); + Serial.print("\t"); + for (uint32_t bufCount = 0; bufCount < sizeof(readBuf); bufCount++) + { + Serial.print("0x"); + Serial.print(readBuf[bufCount], HEX); + Serial.print(' '); + } + Serial.println(); + } +} + + +/* + Writing protect block for WRITE command is configured by the value of BP0 and BP1 in the status register + data={ + 0x00 None + 0x04 C0000h to FFFFFh (upper 1/4) + 0x08 80000h to FFFFFh (upper 1/2) + 0x0C 00000h to FFFFFh (all) + } +*/ +void writeBlockProtect(uint8_t data) +{ + fram_cy15b108.writeEnable(true); + fram_cy15b108.setStatusRegister(data); + fram_cy15b108.writeEnable(false); +} + +/* + @brief Comparing whether the read and write special sector content is consistent. + Can be used to test the probability of FRAM read and write errors. +*/ +void specialSectorWriteAndReadTest(void) +{ + char specialSector_wBuf[32] = ""; + char specialSector_rBuf[32] = {0}; + uint32_t successCount = 0; + uint32_t failCount = 0; + + float progress = 0; + + for (uint32_t addr = 0; addr < 0xFF; addr += sizeof(specialSector_wBuf) / sizeof(char)) + { + fram_cy15b108.writeEnable(true); + fram_cy15b108.specialSectorWrite(addr, (uint8_t*)specialSector_wBuf , sizeof(specialSector_wBuf) / sizeof(char)); + fram_cy15b108.specialSectorRead( addr, (uint8_t*)specialSector_rBuf , sizeof(specialSector_rBuf) / sizeof(char)); + fram_cy15b108.writeEnable(false); + + if (memcmp(specialSector_wBuf , specialSector_rBuf , sizeof(specialSector_rBuf)) == 0) + { + successCount++; + } + else + { + failCount++; + } + Serial.printf("Special Sector Write And Read Test Progress: %5.2f%% , successCount: %ld , failCount:%ld \n", progress, successCount, failCount); + progress = (float)(addr + sizeof(specialSector_wBuf) / sizeof(char)) * 100 / 0xff; + memset(specialSector_rBuf , '0' , sizeof(specialSector_rBuf) / sizeof(char)); + delay(100); + } +} + +/** + The CY15X108QN supports a FAST READ opcode (0Bh) that is provided for opcode compatibility with serial flash devices +*/ +void fastReadOperationTest(void) +{ + char readBuf[32] = {0}; + Serial.println(); + + for (uint32_t addr = 0; addr < FRAM_8M_SIZE; addr += sizeof(readBuf)) + { + fram_cy15b108.fastReadOperation( addr, (uint8_t*)readBuf , sizeof(readBuf)); + Serial.print("0x"); + Serial.print(addr, HEX); + Serial.print("\t"); + for (uint32_t bufCount = 0; bufCount < sizeof(readBuf); bufCount++) + { + Serial.print("0x"); + Serial.print(readBuf[bufCount], HEX); + Serial.print(' '); + } + Serial.println(); + } +} + + +void setup(void) { + pinMode(WB_IO2, OUTPUT); + digitalWrite(WB_IO2, HIGH); // Enable power supply. + delay(300); + pinMode(FRAM_WP_PIN, OUTPUT); + digitalWrite(FRAM_WP_PIN, HIGH); + Serial.begin(115200); + + time_t serial_timeout = millis(); + while (!Serial) + { + if ((millis() - serial_timeout) < 5000) + { + delay(100); + } + else + { + break; + } + } + Serial.println("RAK15007 FRAM_CY15B108QN TEST"); + if (fram_cy15b108.begin()) { + Serial.println("Found FRAM_CY15B108QN"); + } else { + Serial.println("FRAM_CY15B108QN is not connected, Please check your connections\r\n"); + while (1) + { + delay(500); + } + } + writeBlockProtect(0x00); + delay(1000); + + uint32_t fram_size = fram_cy15b108.getFramSize(); + if (fram_size != 0) + { + Serial.print("FRAM_CY15B108QN address size is "); + Serial.print(fram_size); + Serial.println(" bytes"); + Serial.println("The capacity of the FRAM is"); + Serial.print(fram_size); Serial.println(" bytes"); + Serial.print(fram_size / 1024); Serial.println(" kilobytes"); + Serial.print((fram_size * 8) / 1024); Serial.println(" kilobits"); + if (fram_size >= ((1024 * 1024) / 8)) { + Serial.print((fram_size * 8) / (1024 * 1024)); + Serial.println(" megabits"); + } + } + else + { + Serial.println("Unable to identify the device,Please check your connections"); + while (1) + { + delay(100); + } + } + Serial.println(); + fram_cy15b108.writeEnable(true); + fram_cy15b108.writeByte(0x00, 0x01); + uint8_t wByte = fram_cy15b108.readByte(0x00); + if (wByte == 0x01) + { + Serial.println("write byte successful "); + } + fram_cy15b108.writeEnable(false); +} + + +void loop(void) { + + uint8_t serialNumberBuf[8] = {0x00}; + fram_cy15b108.getSerialNumber(serialNumberBuf, sizeof(serialNumberBuf) / sizeof(uint8_t)); + Serial.print("serialNumberBuf=0x"); + for (uint8_t serialCount = 0; serialCount < 8; serialCount++) + { + Serial.printf("%02x", serialNumberBuf[serialCount]); // The factory default value for the 8-byte Serial Number is ‘0000000000000000h’. + } + Serial.println(); + delay(1000); + + char wData[25] = "RAK15007 FRAM_8M TEST"; + char rDate[25] = {0}; + Serial.println("reading and writing test"); + fram_cy15b108.writeEnable(true); + fram_cy15b108.write(0x0000, (uint8_t*)wData , sizeof(wData) / sizeof(char)); + delay(1); + fram_cy15b108.read( 0x0000, (uint8_t*)rDate , sizeof(rDate) / sizeof(char)); + Serial.println(rDate); + fram_cy15b108.writeEnable(false); + delay(1); + + char wDataAgain[30] = "RAK15007 FRAM_8M TEST Again"; + char rDataAgain[30] = {0}; + fram_cy15b108.writeEnable(true); + fram_cy15b108.write(0x0000, (uint8_t*)wDataAgain , sizeof(wDataAgain) / sizeof(char)); + fram_cy15b108.read( 0x0000, (uint8_t*)rDataAgain , sizeof(rDataAgain) / sizeof(char)); + Serial.println(rDataAgain); + fram_cy15b108.writeEnable(false); + Serial.println(); + + Serial.println("Comparing whether the read and write special sector content is consistent."); + specialSectorWriteAndReadTest(); + Serial.println(); + + Serial.println("Comparing whether the read and write content is consistent."); + readWriteTest(); + Serial.println(); + + delay(2000); + Serial.println("FAST Read the contents of the entire chip."); + fastReadOperationTest(); + Serial.println(); + + delay(2000); + Serial.println("Read the contents of the entire chip."); + readEntireChip(); + Serial.println("Read complete,if you want to test again,please reset the module"); + while (1) + { + delay(500); + } +}