drv_nor_flash.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * 2020-07-10 lik rewrite
  11. */
  12. #include "drv_nor_flash.h"
  13. #ifdef BSP_USING_NOR_FLASH
  14. #define DRV_DEBUG
  15. #define LOG_TAG "drv.norflash"
  16. #include <drv_log.h>
  17. static struct rt_mutex flash_lock;
  18. /* RT-Thread MTD device interface */
  19. static long swm_norflash_read_id(struct rt_mtd_nor_device *device)
  20. {
  21. return 0xdeadbeef;
  22. }
  23. static rt_size_t swm_norflash_read(struct rt_mtd_nor_device *device,
  24. rt_off_t position,
  25. rt_uint8_t *data,
  26. rt_uint32_t size)
  27. {
  28. rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
  29. memcpy(data, ((const void *)(NORFLM_BASE + position)), size);
  30. rt_mutex_release(&flash_lock);
  31. return size;
  32. }
  33. static rt_size_t swm_norflash_write(struct rt_mtd_nor_device *device,
  34. rt_off_t position,
  35. const rt_uint8_t *data,
  36. rt_uint32_t size)
  37. {
  38. rt_size_t i;
  39. const rt_uint16_t *hwdata = (const rt_uint16_t *)data;
  40. rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
  41. for (i = 0; i < size / 2; i++)
  42. {
  43. NORFL_Write(position, hwdata[i]);
  44. position += 2;
  45. }
  46. rt_mutex_release(&flash_lock);
  47. return size;
  48. }
  49. static rt_err_t swm_norflash_erase_block(struct rt_mtd_nor_device *device,
  50. rt_off_t offset,
  51. rt_uint32_t length)
  52. {
  53. rt_mutex_take(&flash_lock, RT_WAITING_FOREVER);
  54. NORFL_SectorErase(offset);
  55. rt_mutex_release(&flash_lock);
  56. return RT_EOK;
  57. }
  58. const static struct rt_mtd_nor_driver_ops mtd_ops =
  59. {
  60. swm_norflash_read_id,
  61. swm_norflash_read,
  62. swm_norflash_write,
  63. swm_norflash_erase_block};
  64. static struct rt_mtd_nor_device mtd;
  65. int rt_hw_norflash_init(void)
  66. {
  67. NORFL_InitStructure NORFL_InitStruct;
  68. PORT->PORTP_SEL0 = 0xAAAAAAAA; //PP0-23 => ADDR0-23
  69. PORT->PORTP_SEL1 = 0xAAAA;
  70. PORT->PORTM_SEL0 = 0xAAAAAAAA; //PM0-15 => DATA15-0
  71. PORT->PORTM_INEN = 0xFFFF;
  72. PORT->PORTM_SEL1 = 0xAAA; //PM16 => OEN、PM17 => WEN、PM18 => NORFL_CSN、PM19 => SDRAM_CSN、PM20 => SRAM_CSN、PM21 => SDRAM_CKE
  73. NORFL_InitStruct.DataWidth = 16;
  74. NORFL_InitStruct.WELowPulseTime = 5;
  75. NORFL_InitStruct.OEPreValidTime = 12;
  76. NORFL_InitStruct.OperFinishIEn = 0;
  77. NORFL_InitStruct.OperTimeoutIEn = 0;
  78. NORFL_Init(&NORFL_InitStruct);
  79. /* set page size and block size */
  80. mtd.block_size = BLOCK_SIZE; /* 64kByte */
  81. mtd.ops = &mtd_ops;
  82. /* initialize mutex */
  83. if (rt_mutex_init(&flash_lock, "nor", RT_IPC_FLAG_PRIO) != RT_EOK)
  84. {
  85. rt_kprintf("init sd lock mutex failed\n");
  86. return -RT_ERROR;
  87. }
  88. mtd.block_start = 0;
  89. mtd.block_end = BLOCK_COUNTER;
  90. /* register MTD device */
  91. rt_mtd_nor_register_device("nor", &mtd);
  92. return RT_EOK;
  93. }
  94. INIT_DEVICE_EXPORT(rt_hw_norflash_init);
  95. #endif /* BSP_USING_NOR_FLASH */