spibdev.c 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017-2018 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "py/obj.h"
  27. #include "py/mperrno.h"
  28. #include "irq.h"
  29. #include "led.h"
  30. #include "storage.h"
  31. #if MICROPY_HW_ENABLE_STORAGE
  32. int32_t spi_bdev_ioctl(spi_bdev_t *bdev, uint32_t op, uint32_t arg) {
  33. switch (op) {
  34. case BDEV_IOCTL_INIT:
  35. bdev->spiflash.config = (const mp_spiflash_config_t*)arg;
  36. mp_spiflash_init(&bdev->spiflash);
  37. bdev->flash_tick_counter_last_write = 0;
  38. return 0;
  39. case BDEV_IOCTL_IRQ_HANDLER:
  40. if ((bdev->spiflash.flags & 1) && HAL_GetTick() - bdev->flash_tick_counter_last_write >= 1000) {
  41. mp_spiflash_cache_flush(&bdev->spiflash);
  42. led_state(PYB_LED_RED, 0); // indicate a clean cache with LED off
  43. }
  44. return 0;
  45. case BDEV_IOCTL_SYNC:
  46. if (bdev->spiflash.flags & 1) {
  47. // we must disable USB irqs to prevent MSC contention with SPI flash
  48. uint32_t basepri = raise_irq_pri(IRQ_PRI_OTG_FS);
  49. mp_spiflash_cache_flush(&bdev->spiflash);
  50. led_state(PYB_LED_RED, 0); // indicate a clean cache with LED off
  51. restore_irq_pri(basepri);
  52. }
  53. return 0;
  54. }
  55. return -MP_EINVAL;
  56. }
  57. int spi_bdev_readblocks(spi_bdev_t *bdev, uint8_t *dest, uint32_t block_num, uint32_t num_blocks) {
  58. // we must disable USB irqs to prevent MSC contention with SPI flash
  59. uint32_t basepri = raise_irq_pri(IRQ_PRI_OTG_FS);
  60. mp_spiflash_cached_read(&bdev->spiflash, block_num * FLASH_BLOCK_SIZE, num_blocks * FLASH_BLOCK_SIZE, dest);
  61. restore_irq_pri(basepri);
  62. return 0;
  63. }
  64. int spi_bdev_writeblocks(spi_bdev_t *bdev, const uint8_t *src, uint32_t block_num, uint32_t num_blocks) {
  65. // we must disable USB irqs to prevent MSC contention with SPI flash
  66. uint32_t basepri = raise_irq_pri(IRQ_PRI_OTG_FS);
  67. int ret = mp_spiflash_cached_write(&bdev->spiflash, block_num * FLASH_BLOCK_SIZE, num_blocks * FLASH_BLOCK_SIZE, src);
  68. if (bdev->spiflash.flags & 1) {
  69. led_state(PYB_LED_RED, 1); // indicate a dirty cache with LED on
  70. bdev->flash_tick_counter_last_write = HAL_GetTick();
  71. }
  72. restore_irq_pri(basepri);
  73. return ret;
  74. }
  75. #endif