rt-thread-official/bsp/swm320-lq100/drivers/drv_nor_flash.c

114 lines
3.2 KiB
C
Raw Normal View History

2018-12-24 17:17:27 +08:00
/*
* Copyright (c) 2006-2018, Synwit Technology Co.,Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-05-31 ZYH first version
* 2018-12-10 Zohar_Lee format file
2021-09-30 17:55:16 +08:00
* 2020-07-10 lik rewrite
2018-12-24 17:17:27 +08:00
*/
2021-09-30 17:55:16 +08:00
#include "drv_nor_flash.h"
2018-12-24 17:17:27 +08:00
2021-09-30 17:55:16 +08:00
#ifdef BSP_USING_NOR_FLASH
#define DRV_DEBUG
#define LOG_TAG "drv.norflash"
#include <drv_log.h>
2018-12-24 17:17:27 +08:00
static struct rt_mutex flash_lock;
/* RT-Thread MTD device interface */
2021-09-30 17:55:16 +08:00
static long swm_norflash_read_id(struct rt_mtd_nor_device *device)
2018-12-24 17:17:27 +08:00
{
return 0xdeadbeef;
}
2021-09-30 17:55:16 +08:00
static rt_size_t swm_norflash_read(struct rt_mtd_nor_device *device,
rt_off_t position,
rt_uint8_t *data,
rt_uint32_t size)
2018-12-24 17:17:27 +08:00
{
2021-09-30 17:55:16 +08:00
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
2018-12-24 17:17:27 +08:00
memcpy(data, ((const void *)(NORFLM_BASE + position)), size);
rt_mutex_release(&flash_lock);
return size;
}
2021-09-30 17:55:16 +08:00
static rt_size_t swm_norflash_write(struct rt_mtd_nor_device *device,
rt_off_t position,
const rt_uint8_t *data,
rt_uint32_t size)
2018-12-24 17:17:27 +08:00
{
rt_size_t i;
const rt_uint16_t *hwdata = (const rt_uint16_t *)data;
2021-09-30 17:55:16 +08:00
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
2018-12-24 17:17:27 +08:00
for (i = 0; i < size / 2; i++)
{
NORFL_Write(position, hwdata[i]);
position += 2;
}
rt_mutex_release(&flash_lock);
return size;
}
2021-09-30 17:55:16 +08:00
static rt_err_t swm_norflash_erase_block(struct rt_mtd_nor_device *device,
rt_off_t offset,
rt_uint32_t length)
2018-12-24 17:17:27 +08:00
{
2021-09-30 17:55:16 +08:00
rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
2018-12-24 17:17:27 +08:00
NORFL_SectorErase(offset);
rt_mutex_release(&flash_lock);
return RT_EOK;
}
const static struct rt_mtd_nor_driver_ops mtd_ops =
2021-09-30 17:55:16 +08:00
{
swm_norflash_read_id,
swm_norflash_read,
swm_norflash_write,
swm_norflash_erase_block};
2018-12-24 17:17:27 +08:00
2021-09-30 17:55:16 +08:00
static struct rt_mtd_nor_device mtd;
int rt_hw_norflash_init(void)
2018-12-24 17:17:27 +08:00
{
NORFL_InitStructure NORFL_InitStruct;
2021-09-30 17:55:16 +08:00
2018-12-24 17:17:27 +08:00
PORT->PORTP_SEL0 = 0xAAAAAAAA; //PP0-23 => ADDR0-23
PORT->PORTP_SEL1 = 0xAAAA;
PORT->PORTM_SEL0 = 0xAAAAAAAA; //PM0-15 => DATA15-0
PORT->PORTM_INEN = 0xFFFF;
2021-09-30 17:55:16 +08:00
PORT->PORTM_SEL1 = 0xAAA; //PM16 => OEN、PM17 => WEN、PM18 => NORFL_CSN、PM19 => SDRAM_CSN、PM20 => SRAM_CSN、PM21 => SDRAM_CKE
2018-12-24 17:17:27 +08:00
NORFL_InitStruct.DataWidth = 16;
NORFL_InitStruct.WELowPulseTime = 5;
NORFL_InitStruct.OEPreValidTime = 12;
NORFL_InitStruct.OperFinishIEn = 0;
NORFL_InitStruct.OperTimeoutIEn = 0;
NORFL_Init(&NORFL_InitStruct);
2021-09-30 17:55:16 +08:00
2018-12-24 17:17:27 +08:00
/* set page size and block size */
mtd.block_size = BLOCK_SIZE; /* 64kByte */
mtd.ops = &mtd_ops;
/* initialize mutex */
if (rt_mutex_init(&flash_lock, "nor", RT_IPC_FLAG_PRIO) != RT_EOK)
2018-12-24 17:17:27 +08:00
{
rt_kprintf("init sd lock mutex failed\n");
return -RT_ERROR;
}
mtd.block_start = 0;
mtd.block_end = BLOCK_COUNTER;
/* register MTD device */
rt_mtd_nor_register_device("nor", &mtd);
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_norflash_init);
2021-09-30 17:55:16 +08:00
#endif /* BSP_USING_NOR_FLASH */