Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_FlashIface/examples/jedec_test_bl/jedec_test.cpp
Views: 1799
#include <AP_HAL/AP_HAL.h>1#include <AP_FlashIface/AP_FlashIface.h>2#include <stdio.h>3#include <support.h>4#include <hal.h>56AP_FlashIface_JEDEC jedec_dev;78static UNUSED_FUNCTION void test_page_program()9{10uint8_t *data = new uint8_t[jedec_dev.get_page_size()];11if (data == nullptr) {12uprintf("Failed to allocate data for program");13}14uint8_t *rdata = new uint8_t[jedec_dev.get_page_size()];15if (rdata == nullptr) {16uprintf("Failed to allocate data for read");17}1819// fill program data with its own address20for (uint32_t i = 0; i < jedec_dev.get_page_size(); i++) {21data[i] = i;22}23uprintf("Writing Page #1\n");24uint32_t delay_us, timeout_us;25uint64_t start_time_us = AP_HAL::micros64();26if (!jedec_dev.start_program_page(0, data, delay_us, timeout_us)) {27uprintf("Page write command failed\n");28return;29}30while (true) {31chThdSleep(chTimeUS2I(delay_us));32if (AP_HAL::micros64() > (start_time_us+delay_us)) {33if (!jedec_dev.is_device_busy()) {34uprintf("Page Program Successful, elapsed %ld us\n", uint32_t(AP_HAL::micros64() - start_time_us));35break;36} else {37uprintf("Typical page program time reached, Still Busy?!\n");38}39}40if (AP_HAL::micros64() > (start_time_us+timeout_us)) {41uprintf("Page Program Timed out, elapsed %lld us\n", AP_HAL::micros64() - start_time_us);42return;43}44}45if (!jedec_dev.read(0, rdata, jedec_dev.get_page_size())) {46uprintf("Failed to read Flash page\n");47} else {48if (memcmp(data, rdata, jedec_dev.get_page_size()) != 0) {49uprintf("Program Data Mismatch!\n");50} else {51uprintf("Program Data Verified Good!\n");52}53}5455// Now test XIP mode here as well56uint8_t *chip_data = nullptr;57if (!jedec_dev.start_xip_mode((void**)&chip_data)) {58uprintf("Failed to setup XIP mode\n");59}60if (chip_data == nullptr) {61uprintf("Invalid address!\n");62}63// Here comes the future!64if (memcmp(data, chip_data, jedec_dev.get_page_size()) != 0) {65uprintf("Program Data Mismatch in XIP mode!\n");66} else {67uprintf("Program Data Verified Good in XIP mode!\n");68}69jedec_dev.stop_xip_mode();70}7172static UNUSED_FUNCTION void test_sector_erase()73{74uint32_t delay_ms, timeout_ms;75if (!jedec_dev.start_sector_erase(0, delay_ms, timeout_ms)) { // erase first sector76uprintf("Sector erase command failed\n");77return;78}79uint32_t erase_start = AP_HAL::millis();80uint32_t next_check_ms = 0;81uprintf("Erasing Sector #1 ");82while (true) {83if (AP_HAL::millis() > next_check_ms) {84uprintf("\n");85if (!jedec_dev.is_device_busy()) {86if (next_check_ms == 0) {87uprintf("Sector Erase happened too fast\n");88return;89}90uprintf("Sector Erase Successful, elapsed %ld ms\n", AP_HAL::millis() - erase_start);91break;92} else {93uprintf("Still busy erasing, elapsed %ld ms\n", AP_HAL::millis() - erase_start);94}95if ((AP_HAL::millis() - erase_start) > timeout_ms) {96uprintf("Sector Erase Timed Out, elapsed %ld ms\n", AP_HAL::millis() - erase_start);97return;98}99next_check_ms = erase_start+delay_ms;100}101chThdSleep(chTimeMS2I((delay_ms/100) + 10));102uprintf("*");103}104if (!jedec_dev.verify_sector_erase(0)) {105uprintf("Erase Verification Failed!\n");106} else {107uprintf("Erase Verification Successful!\n");108}109}110111static UNUSED_FUNCTION void test_mass_erase()112{113uint32_t delay_ms, timeout_ms;114if (!jedec_dev.start_mass_erase(delay_ms, timeout_ms)) {115uprintf("Mass erase command failed\n");116return;117}118uint32_t erase_start = AP_HAL::millis();119uint32_t next_check_ms = 0;120uprintf("Mass Erasing ");121while (true) {122if (AP_HAL::millis() > next_check_ms) {123uprintf("\n");124if (!jedec_dev.is_device_busy()) {125if (next_check_ms == 0) {126uprintf("Sector Erase happened too fast\n");127return;128}129uprintf("Mass Erase Successful, elapsed %ld ms\n", AP_HAL::millis() - erase_start);130return;131} else {132uprintf("Still busy erasing, elapsed %ld ms\n", AP_HAL::millis() - erase_start);133}134if ((AP_HAL::millis() - erase_start) > timeout_ms) {135uprintf("Mass Erase Timed Out, elapsed %ld ms\n", AP_HAL::millis() - erase_start);136return;137}138next_check_ms = erase_start+delay_ms;139}140chThdSleep(chTimeMS2I(delay_ms/100));141uprintf("*");142}143}144145int main()146{147init_uarts();148149while (true) {150// Start on user input151while (cin(0) < 0) {}152uprintf("\n\n******************Starting Test********************\n");153jedec_dev.init();154test_sector_erase();155test_page_program();156// test_mass_erase();157chThdSleep(chTimeMS2I(1000));158}159}160161162