xref: /nrf52832-nimble/rt-thread/components/drivers/include/drivers/mtd_nor.h (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * COPYRIGHT (C) 2018, Real-Thread Information Technology Ltd
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2012-5-30     Bernard      the first version
9  */
10 
11 #ifndef __MTD_NOR_H__
12 #define __MTD_NOR_H__
13 
14 #include <rtdevice.h>
15 
16 struct rt_mtd_nor_driver_ops;
17 #define RT_MTD_NOR_DEVICE(device)	((struct rt_mtd_nor_device*)(device))
18 
19 struct rt_mtd_nor_device
20 {
21 	struct rt_device parent;
22 
23 	rt_uint32_t block_size;			/* The Block size in the flash */
24 	rt_uint32_t block_start;		/* The start of available block*/
25 	rt_uint32_t block_end;			/* The end of available block */
26 
27 	/* operations interface */
28 	const struct rt_mtd_nor_driver_ops* ops;
29 };
30 
31 struct rt_mtd_nor_driver_ops
32 {
33 	rt_err_t (*read_id) (struct rt_mtd_nor_device* device);
34 
35 	rt_size_t (*read)    (struct rt_mtd_nor_device* device, rt_off_t offset, rt_uint8_t* data, rt_uint32_t length);
36 	rt_size_t (*write)   (struct rt_mtd_nor_device* device, rt_off_t offset, const rt_uint8_t* data, rt_uint32_t length);
37 
38 	rt_err_t (*erase_block)(struct rt_mtd_nor_device* device, rt_off_t offset, rt_uint32_t length);
39 };
40 
41 rt_err_t rt_mtd_nor_register_device(const char* name, struct rt_mtd_nor_device* device);
42 
rt_mtd_nor_read_id(struct rt_mtd_nor_device * device)43 rt_inline rt_uint32_t rt_mtd_nor_read_id(struct rt_mtd_nor_device* device)
44 {
45 	return device->ops->read_id(device);
46 }
47 
rt_mtd_nor_read(struct rt_mtd_nor_device * device,rt_off_t offset,rt_uint8_t * data,rt_uint32_t length)48 rt_inline rt_size_t rt_mtd_nor_read(
49 	struct rt_mtd_nor_device* device,
50 	rt_off_t offset, rt_uint8_t* data, rt_uint32_t length)
51 {
52 	return device->ops->read(device, offset, data, length);
53 }
54 
rt_mtd_nor_write(struct rt_mtd_nor_device * device,rt_off_t offset,const rt_uint8_t * data,rt_uint32_t length)55 rt_inline rt_size_t rt_mtd_nor_write(
56 	struct rt_mtd_nor_device* device,
57 	rt_off_t offset, const rt_uint8_t* data, rt_uint32_t length)
58 {
59 	return device->ops->write(device, offset, data, length);
60 }
61 
rt_mtd_nor_erase_block(struct rt_mtd_nor_device * device,rt_off_t offset,rt_size_t length)62 rt_inline rt_err_t rt_mtd_nor_erase_block(struct rt_mtd_nor_device* device, rt_off_t offset, rt_size_t length)
63 {
64 	return device->ops->erase_block(device, offset, length);
65 }
66 
67 #endif
68