ssd1306_image.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <memory.h>
  4. #include "pico/stdlib.h"
  5. #include "hardware/i2c.h"
  6. #include "hardware/dma.h"
  7. #define I2C_SCL PICO_DEFAULT_I2C_SCL_PIN
  8. #define I2C_SDA PICO_DEFAULT_I2C_SDA_PIN
  9. #define SSD1306_ADDR 0x3C // SSD1306のデフォルトのI2Cアドレス
  10. #define SSD1306_HEIGHT 32
  11. #define SSD1306_WIDTH 128
  12. // SSD1306コマンド定義
  13. #define SSD1306_SET_MEM_MODE _u(0x20)
  14. #define SSD1306_SET_COL_ADDR _u(0x21)
  15. #define SSD1306_SET_PAGE_ADDR _u(0x22)
  16. #define SSD1306_SET_HORIZ_SCROLL _u(0x26)
  17. #define SSD1306_SET_SCROLL _u(0x2E)
  18. #define SSD1306_SET_DISP_START_LINE _u(0x40)
  19. #define SSD1306_SET_CONTRAST _u(0x81)
  20. #define SSD1306_SET_CHARGE_PUMP _u(0x8D)
  21. #define SSD1306_SET_SEG_REMAP _u(0xA0)
  22. #define SSD1306_SET_ENTIRE_ON _u(0xA4)
  23. #define SSD1306_SET_ALL_ON _u(0xA5)
  24. #define SSD1306_SET_NORM_DISP _u(0xA6)
  25. #define SSD1306_SET_INV_DISP _u(0xA7)
  26. #define SSD1306_SET_MUX_RATIO _u(0xA8)
  27. #define SSD1306_SET_DISP _u(0xAE)
  28. #define SSD1306_SET_COM_OUT_DIR _u(0xC0)
  29. #define SSD1306_SET_COM_OUT_DIR_FLIP _u(0xC0)
  30. #define SSD1306_SET_DISP_OFFSET _u(0xD3)
  31. #define SSD1306_SET_DISP_CLK_DIV _u(0xD5)
  32. #define SSD1306_SET_PRECHARGE _u(0xD9)
  33. #define SSD1306_SET_COM_PIN_CFG _u(0xDA)
  34. #define SSD1306_SET_VCOM_DESEL _u(0xDB)
  35. #define SSD1306_PAGE_HEIGHT _u(8)
  36. #define SSD1306_NUM_PAGES (SSD1306_HEIGHT / SSD1306_PAGE_HEIGHT)
  37. #define SSD1306_BUF_LEN (SSD1306_NUM_PAGES * SSD1306_WIDTH)
  38. #define SSD1306_WRITE_MODE _u(0xFE)
  39. #define SSD1306_READ_MODE _u(0xFF)
  40. #define SSD1306_CONTROL_CMD _u(0x80)
  41. #define SSD1306_CONTROL_DATA _u(0x40)
  42. // SSD1306フレームバッファの領域指定
  43. typedef struct {
  44. uint8_t start_col;
  45. uint8_t end_col;
  46. uint8_t start_page;
  47. uint8_t end_page;
  48. } ssd1306_rect_t;
  49. inline int ssd1306_buf_len(ssd1306_rect_t *r)
  50. {
  51. return (r->end_col-r->start_col+1)*(r->end_page-r->start_page+1);
  52. }
  53. // コマンド発行
  54. void ssd1306_send_cmd(uint8_t cmd)
  55. {
  56. uint8_t buf[2] = {SSD1306_CONTROL_CMD, cmd}; // 0x80 = コマンド
  57. i2c_write_blocking(i2c0, SSD1306_ADDR, buf, 2, false);
  58. }
  59. // SSD1306を初期化
  60. void ssd1306_init()
  61. {
  62. /* memory mapping */
  63. ssd1306_send_cmd(SSD1306_SET_MEM_MODE); // set memory address mode
  64. ssd1306_send_cmd(0x00); // horizontal addressing mode
  65. /* resolution and layout */
  66. ssd1306_send_cmd(SSD1306_SET_DISP_START_LINE); // set display start line to 0
  67. ssd1306_send_cmd(SSD1306_SET_SEG_REMAP | 0x01); // set segment re-map
  68. // column address 127 is mapped to SEG0
  69. ssd1306_send_cmd(SSD1306_SET_MUX_RATIO); // set multiplex ratio
  70. ssd1306_send_cmd(SSD1306_HEIGHT - 1); // 32 pixels high
  71. ssd1306_send_cmd(SSD1306_SET_COM_OUT_DIR | 0x08); // set COM (common) output scan direction
  72. // scan from bottom up, COM[N-1] to COM0
  73. ssd1306_send_cmd(SSD1306_SET_DISP_OFFSET); // set display offset
  74. ssd1306_send_cmd(0x00); // no offset
  75. ssd1306_send_cmd(SSD1306_SET_COM_PIN_CFG); // set COM (common) pins hardware configuration
  76. ssd1306_send_cmd(0x02); // manufacturer magic number
  77. /* timing and driving scheme */
  78. ssd1306_send_cmd(SSD1306_SET_DISP_CLK_DIV); // set display clock divide ratio
  79. ssd1306_send_cmd(0x80); // div ratio of 1, standard freq
  80. ssd1306_send_cmd(SSD1306_SET_PRECHARGE); // set pre-charge period
  81. ssd1306_send_cmd(0xF1); // Vcc internally generated on our board
  82. ssd1306_send_cmd(SSD1306_SET_VCOM_DESEL); // set VCOMH deselect level
  83. ssd1306_send_cmd(0x30); // 0.83xVcc
  84. /* display */
  85. ssd1306_send_cmd(SSD1306_SET_CONTRAST); // set contrast control
  86. ssd1306_send_cmd(0xFF);
  87. ssd1306_send_cmd(SSD1306_SET_ENTIRE_ON); // set entire display on to follow RAM content
  88. ssd1306_send_cmd(SSD1306_SET_NORM_DISP); // set normal (not inverted) display
  89. ssd1306_send_cmd(SSD1306_SET_CHARGE_PUMP); // set charge pump
  90. ssd1306_send_cmd(0x14); // Vcc internally generated on our board
  91. ssd1306_send_cmd(SSD1306_SET_SCROLL | 0x00); // deactivate horizontal scrolling if set
  92. // this is necessary as memory writes will corrupt if scrolling was enabled
  93. ssd1306_send_cmd(SSD1306_SET_DISP | 0x01); // turn display on
  94. }
  95. // データ送信
  96. void ssd1306_send_data(uint8_t *buf, uint len)
  97. {
  98. uint8_t *temp_buf = malloc(len+1);
  99. for (int i = 1; i < len + 1; i++) {
  100. temp_buf[i] = buf[i - 1];
  101. }
  102. temp_buf[0] = SSD1306_CONTROL_DATA; // データ
  103. i2c_write_blocking(i2c0, SSD1306_ADDR, temp_buf, len+1, false);
  104. free(temp_buf);
  105. }
  106. // OLEDエリア書き込み
  107. void ssd1306_write_area(uint8_t *buf, ssd1306_rect_t *rect)
  108. {
  109. uint8_t cmd[6];
  110. ssd1306_send_cmd(SSD1306_SET_COL_ADDR);
  111. ssd1306_send_cmd(rect->start_col);
  112. ssd1306_send_cmd(rect->end_col);
  113. ssd1306_send_cmd(SSD1306_SET_PAGE_ADDR);
  114. ssd1306_send_cmd(rect->start_page);
  115. ssd1306_send_cmd(rect->end_page);
  116. ssd1306_send_data(buf, ssd1306_buf_len(rect));
  117. }
  118. void ssd1306_clear(void)
  119. {
  120. size_t buf_size = SSD1306_WIDTH * SSD1306_NUM_PAGES;
  121. uint8_t *buf =(uint8_t *)calloc(buf_size, sizeof(uint8_t));
  122. ssd1306_rect_t rect;
  123. rect.start_col = 0;
  124. rect.end_col = SSD1306_WIDTH - 1;
  125. rect.start_page = 0;
  126. rect.end_page = SSD1306_NUM_PAGES - 1;
  127. ssd1306_write_area(buf, &rect);
  128. free(buf);
  129. }
  130. // 画像ファイルの埋め込み
  131. __asm(\
  132. ".section \".rodata\" \n"
  133. ".balign 4\n"
  134. ".global _image_data\n"
  135. ".global _image_data_len\n"
  136. "_image_data:\n"
  137. ".incbin \"ssd1306img.bin\"\n"
  138. ".set _image_data_len, . - _image_data\n"
  139. ".section \".text\"\n"
  140. );
  141. extern const uint8_t _image_data[]; // 画像データ先頭アドレス
  142. extern uint32_t _image_data_len[]; // 画像データ長
  143. int dma_ch;
  144. dma_channel_config dmac_config;
  145. static uint16_t *dma_buffer;
  146. void dma_irq_handler()
  147. {
  148. // 割り込みフラグクリア
  149. dma_channel_acknowledge_irq0(dma_ch);
  150. while(i2c_get_write_available(i2c0) != 16);
  151. i2c0_hw->data_cmd = 0x200; // STOP Condition = bit 9
  152. free(dma_buffer);
  153. }
  154. void ssd1306_write_dma_area(uint8_t *buf, ssd1306_rect_t *rect)
  155. {
  156. uint8_t cmd[1] = {SSD1306_CONTROL_DATA};
  157. size_t len = ssd1306_buf_len(rect);
  158. dma_buffer = malloc(len * sizeof(uint16_t));
  159. for(int i = 0; i < len; i++) {
  160. dma_buffer[i] = buf[i];
  161. }
  162. dma_ch = dma_claim_unused_channel(true);
  163. dmac_config = dma_channel_get_default_config(dma_ch);
  164. channel_config_set_transfer_data_size(&dmac_config, DMA_SIZE_16);
  165. channel_config_set_read_increment(&dmac_config, true);
  166. channel_config_set_write_increment(&dmac_config, false);
  167. // channel_config_set_bswap(&dmac_config, true);
  168. uint dreq = i2c_get_dreq(i2c0, true);
  169. channel_config_set_dreq(&dmac_config, dreq);
  170. // DMAチャンネルの設定
  171. dma_channel_configure(
  172. dma_ch, // DMAチャンネル
  173. &dmac_config, // 構成情報
  174. &i2c0_hw->data_cmd, // 書き込みアドレス
  175. dma_buffer, // 転送元レジスタアドレス
  176. len, // 転送データサイズ
  177. false // すぐに開始ならtrue
  178. );
  179. dma_channel_set_irq0_enabled(dma_ch, true);
  180. irq_set_exclusive_handler(DMA_IRQ_0, dma_irq_handler);
  181. irq_set_enabled(DMA_IRQ_0, true);
  182. // エリア設定
  183. ssd1306_send_cmd(SSD1306_SET_COL_ADDR);
  184. ssd1306_send_cmd(rect->start_col);
  185. ssd1306_send_cmd(rect->end_col);
  186. ssd1306_send_cmd(SSD1306_SET_PAGE_ADDR);
  187. ssd1306_send_cmd(rect->start_page);
  188. ssd1306_send_cmd(rect->end_page);
  189. // SSD1306_CONTROL_DATAをバースト書き込み
  190. i2c_write_burst_blocking(i2c0, SSD1306_ADDR, cmd, 1);
  191. // DMA転送開始
  192. dma_channel_start(dma_ch);
  193. }
  194. int main()
  195. {
  196. stdio_init_all();
  197. // I2C0を400kbpsで初期化
  198. i2c_init(i2c0, 400*1000);
  199. // ピンをI2Cに割り当て
  200. gpio_set_function(I2C_SDA, GPIO_FUNC_I2C);
  201. gpio_set_function(I2C_SCL, GPIO_FUNC_I2C);
  202. gpio_disable_pulls(I2C_SDA);
  203. gpio_disable_pulls(I2C_SCL);
  204. // 外付けでプルアップした場合次の2行をコメントアウト
  205. gpio_pull_up(I2C_SDA);
  206. gpio_pull_up(I2C_SCL);
  207. ssd1306_init();
  208. ssd1306_clear();
  209. ssd1306_rect_t rect;
  210. rect.start_col = 0;
  211. rect.end_col = SSD1306_WIDTH - 1;
  212. rect.start_page = 0;
  213. rect.end_page = SSD1306_NUM_PAGES;
  214. ssd1306_write_dma_area((uint8_t *)_image_data, &rect );
  215. while (true) {
  216. tight_loop_contents();
  217. }
  218. }