drv_nor_flash.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2006-2018, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-31 ZYH first version
  9. * 2018-12-10 Zohar_Lee format file
  10. */
  11. #include <rtdevice.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <SWM320.h>
  16. #define BLOCK_SIZE (64 * 1024)
  17. #define FLASH_SIZE (BSP_NOR_FLASH_SIZE)
  18. #define BLOCK_COUNTER (FLASH_SIZE / BLOCK_SIZE)
  19. static struct rt_mutex flash_lock;
  20. /* RT-Thread MTD device interface */
  21. static long swm320_read_id(struct rt_mtd_nor_device *device)
  22. {
  23. return 0xdeadbeef;
  24. }
  25. static rt_size_t swm320_read(struct rt_mtd_nor_device *device,
  26. rt_off_t position,
  27. rt_uint8_t *data,
  28. rt_size_t size)
  29. {
  30. rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
  31. memcpy(data, ((const void *)(NORFLM_BASE + position)), size);
  32. rt_mutex_release(&flash_lock);
  33. return size;
  34. }
  35. static rt_size_t swm320_write(struct rt_mtd_nor_device *device,
  36. rt_off_t position,
  37. const rt_uint8_t *data,
  38. rt_size_t size)
  39. {
  40. rt_size_t i;
  41. const rt_uint16_t *hwdata = (const rt_uint16_t *)data;
  42. rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
  43. for (i = 0; i < size / 2; i++)
  44. {
  45. NORFL_Write(position, hwdata[i]);
  46. position += 2;
  47. }
  48. rt_mutex_release(&flash_lock);
  49. return size;
  50. }
  51. static rt_err_t swm320_erase_block(struct rt_mtd_nor_device *device,
  52. rt_off_t offset,
  53. rt_uint32_t length)
  54. {
  55. rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
  56. NORFL_SectorErase(offset);
  57. rt_mutex_release(&flash_lock);
  58. return RT_EOK;
  59. }
  60. const static struct rt_mtd_nor_driver_ops mtd_ops =
  61. {
  62. swm320_read_id,
  63. swm320_read,
  64. swm320_write,
  65. swm320_erase_block
  66. };
  67. static rt_err_t hw_init()
  68. {
  69. NORFL_InitStructure NORFL_InitStruct;
  70. PORT->PORTP_SEL0 = 0xAAAAAAAA; //PP0-23 => ADDR0-23
  71. PORT->PORTP_SEL1 = 0xAAAA;
  72. PORT->PORTM_SEL0 = 0xAAAAAAAA; //PM0-15 => DATA15-0
  73. PORT->PORTM_INEN = 0xFFFF;
  74. PORT->PORTM_SEL1 = 0x2AA; //PM16 => OEN, PM17 => WEN, PM18 => NORFL_CSN,PM19 => SDRAM_CSN, PM20 => SRAM_CSN, PM21 => SDRAM_CKE
  75. NORFL_InitStruct.DataWidth = 16;
  76. NORFL_InitStruct.WELowPulseTime = 5;
  77. NORFL_InitStruct.OEPreValidTime = 12;
  78. NORFL_InitStruct.OperFinishIEn = 0;
  79. NORFL_InitStruct.OperTimeoutIEn = 0;
  80. NORFL_Init(&NORFL_InitStruct);
  81. return RT_EOK;
  82. }
  83. static struct rt_mtd_nor_device mtd;
  84. int rt_hw_norflash_init(void)
  85. {
  86. hw_init();
  87. /* set page size and block size */
  88. mtd.block_size = BLOCK_SIZE; /* 64kByte */
  89. mtd.ops = &mtd_ops;
  90. /* initialize mutex */
  91. if (rt_mutex_init(&flash_lock, "nor", RT_IPC_FLAG_FIFO) != RT_EOK)
  92. {
  93. rt_kprintf("init sd lock mutex failed\n");
  94. return -RT_ERROR;
  95. }
  96. mtd.block_start = 0;
  97. mtd.block_end = BLOCK_COUNTER;
  98. /* register MTD device */
  99. rt_mtd_nor_register_device("nor", &mtd);
  100. return RT_EOK;
  101. }
  102. INIT_DEVICE_EXPORT(rt_hw_norflash_init);
  103. #ifdef RT_USING_FINSH
  104. #include <finsh.h>
  105. void nor_erase(void)
  106. {
  107. NORFL_ChipErase();
  108. }
  109. MSH_CMD_EXPORT(nor_erase, erase all block in SPI flash);
  110. #endif