flash.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * File : flash.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-12-04 Haley the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include "am_mcu_apollo.h"
  27. #include "flash.h"
  28. /* RT-Thread device interface */
  29. static rt_err_t rt_flash_init(rt_device_t dev)
  30. {
  31. return RT_EOK;
  32. }
  33. static rt_err_t rt_flash_open(rt_device_t dev, rt_uint16_t oflag)
  34. {
  35. return RT_EOK;
  36. }
  37. static rt_err_t rt_flash_close(rt_device_t dev)
  38. {
  39. return RT_EOK;
  40. }
  41. static rt_err_t rt_flash_control(rt_device_t dev, int cmd, void *args)
  42. {
  43. uint32_t ui32Critical, i;
  44. uint32_t ui32CurrentPage, ui32CurrentBlock;
  45. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  46. {
  47. struct rt_device_blk_geometry *geometry;
  48. geometry = (struct rt_device_blk_geometry *)args;
  49. if (geometry == RT_NULL)
  50. return -RT_ERROR;
  51. geometry->bytes_per_sector = 8192;
  52. geometry->sector_count = 8192;
  53. geometry->block_size = 8192;
  54. }
  55. else if(cmd == RT_DEVICE_CTRL_BLK_ERASE)
  56. {
  57. struct rom_control_erase *erase;
  58. erase = (struct rom_control_erase *)args;
  59. // Start a critical section.
  60. ui32Critical = am_hal_interrupt_master_disable();
  61. if (erase->type == 0x01)
  62. {
  63. for(i = 0; i < erase->pagenums; i++)
  64. {
  65. // Figure out what page and block we're working on.
  66. ui32CurrentPage = AM_HAL_FLASH_ADDR2PAGE(erase->addrstart);
  67. ui32CurrentBlock = AM_HAL_FLASH_ADDR2INST(erase->addrstart);
  68. am_hal_flash_page_erase(AM_HAL_FLASH_PROGRAM_KEY, ui32CurrentBlock, ui32CurrentPage); //µ¥ÉÈÇø²Á³ýÃüÁî
  69. erase->addrstart += 8192;
  70. }
  71. }
  72. // Exit the critical section.
  73. am_hal_interrupt_master_set(ui32Critical);
  74. }
  75. return RT_EOK;
  76. }
  77. static rt_size_t rt_flash_read(rt_device_t dev,
  78. rt_off_t pos,
  79. void* buffer,
  80. rt_size_t size)
  81. {
  82. rt_memcpy((uint8_t *)buffer, (uint8_t *)pos, size);
  83. return size;
  84. }
  85. static rt_size_t rt_flash_write(rt_device_t dev,
  86. rt_off_t pos,
  87. const void* buffer,
  88. rt_size_t size)
  89. {
  90. uint32_t ui32Critical;
  91. uint32_t ui32WordsInBuffer;
  92. ui32WordsInBuffer = (size + 3)/ 4;
  93. // Start a critical section.
  94. ui32Critical = am_hal_interrupt_master_disable();
  95. // Program the flash page with the data we just received.
  96. am_hal_flash_program_main(AM_HAL_FLASH_PROGRAM_KEY, (uint32_t *)buffer, (uint32_t *)pos, ui32WordsInBuffer);
  97. // Exit the critical section.
  98. am_hal_interrupt_master_set(ui32Critical);
  99. return size;
  100. }
  101. int rt_hw_rom_init(void)
  102. {
  103. static struct rt_device device;
  104. /* register device */
  105. device.type = RT_Device_Class_Block;
  106. device.init = rt_flash_init;
  107. device.open = rt_flash_open;
  108. device.close = rt_flash_close;
  109. device.read = rt_flash_read;
  110. device.write = rt_flash_write;
  111. device.control = rt_flash_control;
  112. /* no private */
  113. device.user_data = RT_NULL;
  114. /* register the device */
  115. rt_device_register(&device, "rom", RT_DEVICE_FLAG_RDWR);
  116. rt_kprintf("register device rom!\r\n");
  117. return 0;
  118. }
  119. #ifdef RT_USING_COMPONENTS_INIT
  120. INIT_DEVICE_EXPORT(rt_hw_rom_init);
  121. #endif
  122. /*@}*/