Skip to content

Instantly share code, notes, and snippets.

@strobo
Created September 15, 2019 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strobo/eb3ce0c63c4976eb8de8ff87aecf2588 to your computer and use it in GitHub Desktop.
Save strobo/eb3ce0c63c4976eb8de8ff87aecf2588 to your computer and use it in GitHub Desktop.
Zephyr Native POSIX Flash driver read and write sample
#include <zephyr.h>
#include <sys/printk.h>
#include <device.h>
#include <drivers/flash.h>
// How to build.
// cd ~/zephyrproject
// mkdir app
// cd app
// cp -r ../../zephyr/samples/hello_world/ .
// cd hello_world
// west build -b native_posix_64
// west build -t menuconfig
// Enable Device Drivers -> Flash hardware support -> Native POSIX Flash driver
// Save configuration and Quit.
// Edit src/main.c
// west build -t run
// A file named "build/flash.bin" will generate after run.
// cat build/flash.bin #=> Z
void main(void)
{
struct device *flash;
off_t offset = 0;
uint8_t data;
int ret;
printk("Hello World! %s\n", CONFIG_BOARD);
flash = device_get_binding(DT_FLASH_DEV_NAME);
if (flash == NULL) {
printk("device not found. Aborting.\n");
return;
}
ret = flash_read(flash, offset, &data, 1);
if(ret) {
printk("error %d\n", ret);
return;
}
printk("read data is : %d(%c)\n", data, data);
data = 'Z';
ret = flash_write(flash, offset, &data, 1);
if(ret) {
printk("error %d\n", ret);
return;
}
printk("write data is : %d(%c)\n", data, data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment