dma_st7789.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2025, Satoshi Yoneda (GADGET LTD.)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #ifndef __DMA_ST7789
  21. #define __DMA_ST7789
  22. #include <Arduino.h>
  23. #include <Adafruit_GFX.h>
  24. #include <Adafruit_ST7735.h>
  25. #include <Adafruit_ST7789.h>
  26. #include <SPI.h>
  27. #include "hardware/dma.h"
  28. #include "hardware/spi.h"
  29. #include "pico/stdlib.h"
  30. class dma_st7789 : public Adafruit_ST7789 {
  31. private:
  32. volatile bool data_transfer_flag;
  33. int dma_channel;
  34. dma_channel_config c;
  35. static dma_st7789 *instance_ptr; // インスタンス
  36. // 割り込みハンドラ(動的)
  37. void dma_interrupt_handler() {
  38. // 割り込みフラグをクリア
  39. dma_hw->ints0 = 1u << dma_channel;
  40. // SPI転送の終了処理
  41. this->endWrite();
  42. // 転送中フラグをリセット
  43. data_transfer_flag = false;
  44. };
  45. public:
  46. // コンストラクタ
  47. dma_st7789(int8_t cs, int8_t dc, int8_t rst) : Adafruit_ST7789(cs, dc, rst) {
  48. data_transfer_flag = false;
  49. instance_ptr = this; // インスタンスを保存
  50. // DMAの設定
  51. dma_channel = dma_claim_unused_channel(true);
  52. c = dma_channel_get_default_config(dma_channel);
  53. channel_config_set_transfer_data_size(&c, DMA_SIZE_8);
  54. channel_config_set_read_increment(&c, true);
  55. channel_config_set_write_increment(&c, false);
  56. channel_config_set_dreq(&c, DREQ_SPI0_TX);
  57. dma_channel_set_irq0_enabled(dma_channel, true);
  58. // 割り込みハンドラとして静的関数を登録
  59. irq_set_exclusive_handler(DMA_IRQ_0, dma_irq_handler_static);
  60. irq_set_enabled(DMA_IRQ_0, true);
  61. };
  62. // 転送開始
  63. void transfer(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t *buffer) {
  64. // 1つ前(があればそれ)のDMAの転送完了を待つ
  65. (void)wait_for_transfer();
  66. uint size = width * height * sizeof(uint16_t);
  67. this->setAddrWindow(x, y, width, height);
  68. this->startWrite();
  69. data_transfer_flag = true;
  70. // 転送
  71. dma_channel_configure(
  72. dma_channel,
  73. &c,
  74. &spi_get_hw(spi0)->dr,
  75. buffer,
  76. size,
  77. true
  78. );
  79. };
  80. // 転送完了待機(タイムアウト付き)
  81. bool wait_for_transfer(int timeout_ms = 1000) {
  82. bool retval = true;
  83. ulong start_time = millis();
  84. while( data_transfer_flag ) {
  85. if( (millis() - start_time) > timeout_ms)
  86. break;
  87. }
  88. if(data_transfer_flag) retval = false;
  89. return retval;
  90. };
  91. // 割り込みハンドラ(静的)
  92. static void dma_irq_handler_static(void) {
  93. if(instance_ptr) {
  94. instance_ptr->dma_interrupt_handler();
  95. }
  96. };
  97. };
  98. // インスタンスを初期化
  99. dma_st7789 *dma_st7789::instance_ptr = nullptr;
  100. #endif