/*
 * bsp_flash.c
 *
 *  Created on: 2025年4月8日
 *      Author: 22332
 */

#include "cola_init.h"
#include "cola_device.h"
#include "drv_flash.h"

static cola_device_t g_dev_flash;

static int bsp_flash_open(cola_device_t *dev, int oflag){
	bsp_drv_flash_init();
    return 0;
}

static int bsp_flash_close(cola_device_t *dev){
    return 0;
}


static int bsp_flash_read(cola_device_t *dev, int pos, void *buffer, int size){
	if(pos == 0)
	{
	    return bsp_drv_get_vendor_data(buffer, size);;
	}
	else
	{
		volatile uint8_t * p_address =(uint8_t *) pos;
		bsp_drv_flash_read_byte(p_address, (uint8_t *)buffer,  size);

		return 0;
	}
}

static int  bsp_flash_write(cola_device_t *dev, int pos, const void *buffer, int size){

	 bsp_drv_flash_write((uint8_t *)buffer,pos);

    return 0;
}


static struct cola_device_ops master_flash_Ops ={
    .open   = bsp_flash_open,
    .close  = bsp_flash_close,
    .read   = bsp_flash_read,
    .write  = bsp_flash_write,
};

static void bsp_flash_configuration(void){
    g_dev_flash.name = "FLASH";
    g_dev_flash.dops = &master_flash_Ops;
    cola_device_register(&g_dev_flash);
}
register_initcall(bsp_flash_configuration);

