logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

st7565.c (15029B)


  1. /*
  2. Copyright 2021
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "st7565.h"
  15. #include <string.h>
  16. #include "compiler_support.h"
  17. #include "keyboard.h"
  18. #include "progmem.h"
  19. #include "timer.h"
  20. #include "wait.h"
  21. #include ST7565_FONT_H
  22. // Fundamental Commands
  23. #define CONTRAST 0x81
  24. #define DISPLAY_ALL_ON 0xA5
  25. #define DISPLAY_ALL_ON_RESUME 0xA4
  26. #define NORMAL_DISPLAY 0xA6
  27. #define INVERT_DISPLAY 0xA7
  28. #define DISPLAY_ON 0xAF
  29. #define DISPLAY_OFF 0xAE
  30. #define NOP 0xE3
  31. // Addressing Setting Commands
  32. #define PAM_SETCOLUMN_LSB 0x00
  33. #define PAM_SETCOLUMN_MSB 0x10
  34. #define PAM_PAGE_ADDR 0xB0 // 0xb0 -- 0xb7
  35. // Hardware Configuration Commands
  36. #define DISPLAY_START_LINE 0x40
  37. #define SEGMENT_REMAP 0xA0
  38. #define SEGMENT_REMAP_INV 0xA1
  39. #define COM_SCAN_INC 0xC0
  40. #define COM_SCAN_DEC 0xC8
  41. #define LCD_BIAS_7 0xA3
  42. #define LCD_BIAS_9 0xA2
  43. #define RESISTOR_RATIO 0x20
  44. #define POWER_CONTROL 0x28
  45. // Misc defines
  46. #ifndef ST7565_BLOCK_COUNT
  47. # define ST7565_BLOCK_COUNT (sizeof(ST7565_BLOCK_TYPE) * 8)
  48. #endif
  49. #ifndef ST7565_BLOCK_SIZE
  50. # define ST7565_BLOCK_SIZE (ST7565_MATRIX_SIZE / ST7565_BLOCK_COUNT)
  51. #endif
  52. #define ST7565_ALL_BLOCKS_MASK (((((ST7565_BLOCK_TYPE)1 << (ST7565_BLOCK_COUNT - 1)) - 1) << 1) | 1)
  53. #define HAS_FLAGS(bits, flags) ((bits & flags) == flags)
  54. // Display buffer's is the same as the display memory layout
  55. // this is so we don't end up with rounding errors with
  56. // parts of the display unusable or don't get cleared correctly
  57. // and also allows for drawing & inverting
  58. uint8_t st7565_buffer[ST7565_MATRIX_SIZE];
  59. uint8_t * st7565_cursor;
  60. ST7565_BLOCK_TYPE st7565_dirty = 0;
  61. bool st7565_initialized = false;
  62. bool st7565_active = false;
  63. bool st7565_inverted = false;
  64. display_rotation_t st7565_rotation = DISPLAY_ROTATION_0;
  65. #if ST7565_TIMEOUT > 0
  66. uint32_t st7565_timeout;
  67. #endif
  68. #if ST7565_UPDATE_INTERVAL > 0
  69. uint16_t st7565_update_timeout;
  70. #endif
  71. // Flips the rendering bits for a character at the current cursor position
  72. static void InvertCharacter(uint8_t *cursor) {
  73. const uint8_t *end = cursor + ST7565_FONT_WIDTH;
  74. while (cursor < end) {
  75. *cursor = ~(*cursor);
  76. cursor++;
  77. }
  78. }
  79. bool st7565_init(display_rotation_t rotation) {
  80. gpio_set_pin_output(ST7565_A0_PIN);
  81. gpio_write_pin_high(ST7565_A0_PIN);
  82. gpio_set_pin_output(ST7565_RST_PIN);
  83. gpio_write_pin_high(ST7565_RST_PIN);
  84. st7565_rotation = st7565_init_user(rotation);
  85. spi_init();
  86. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  87. st7565_reset();
  88. st7565_send_cmd(LCD_BIAS_7);
  89. if (!HAS_FLAGS(st7565_rotation, DISPLAY_ROTATION_180)) {
  90. st7565_send_cmd(SEGMENT_REMAP);
  91. st7565_send_cmd(COM_SCAN_DEC);
  92. } else {
  93. st7565_send_cmd(SEGMENT_REMAP_INV);
  94. st7565_send_cmd(COM_SCAN_INC);
  95. }
  96. st7565_send_cmd(DISPLAY_START_LINE | 0x00);
  97. st7565_send_cmd(CONTRAST);
  98. st7565_send_cmd(ST7565_CONTRAST);
  99. st7565_send_cmd(RESISTOR_RATIO | 0x01);
  100. st7565_send_cmd(POWER_CONTROL | 0x04);
  101. wait_ms(50);
  102. st7565_send_cmd(POWER_CONTROL | 0x06);
  103. wait_ms(50);
  104. st7565_send_cmd(POWER_CONTROL | 0x07);
  105. wait_ms(10);
  106. st7565_send_cmd(DISPLAY_ON);
  107. st7565_send_cmd(DISPLAY_ALL_ON_RESUME);
  108. st7565_send_cmd(NORMAL_DISPLAY);
  109. spi_stop();
  110. #if ST7565_TIMEOUT > 0
  111. st7565_timeout = timer_read32() + ST7565_TIMEOUT;
  112. #endif
  113. st7565_clear();
  114. st7565_initialized = true;
  115. st7565_active = true;
  116. return true;
  117. }
  118. __attribute__((weak)) display_rotation_t st7565_init_user(display_rotation_t rotation) {
  119. return rotation;
  120. }
  121. void st7565_clear(void) {
  122. memset(st7565_buffer, 0, sizeof(st7565_buffer));
  123. st7565_cursor = &st7565_buffer[0];
  124. st7565_dirty = ST7565_ALL_BLOCKS_MASK;
  125. }
  126. uint8_t crot(uint8_t a, int8_t n) {
  127. const uint8_t mask = 0x7;
  128. n &= mask;
  129. return a << n | a >> (-n & mask);
  130. }
  131. void st7565_render(void) {
  132. if (!st7565_initialized) {
  133. return;
  134. }
  135. // Do we have work to do?
  136. st7565_dirty &= ST7565_ALL_BLOCKS_MASK;
  137. if (!st7565_dirty) {
  138. return;
  139. }
  140. // Find first dirty block
  141. uint8_t update_start = 0;
  142. while (!(st7565_dirty & ((ST7565_BLOCK_TYPE)1 << update_start))) {
  143. ++update_start;
  144. }
  145. // Calculate commands to set memory addressing bounds.
  146. uint8_t start_page = ST7565_BLOCK_SIZE * update_start / ST7565_DISPLAY_WIDTH;
  147. uint8_t start_column = ST7565_BLOCK_SIZE * update_start % ST7565_DISPLAY_WIDTH;
  148. // IC has 132 segment drivers, for panels with less width we need to offset the starting column
  149. if (HAS_FLAGS(st7565_rotation, DISPLAY_ROTATION_180)) {
  150. start_column += (132 - ST7565_DISPLAY_WIDTH);
  151. }
  152. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  153. st7565_send_cmd(PAM_PAGE_ADDR | start_page);
  154. st7565_send_cmd(PAM_SETCOLUMN_LSB | ((ST7565_COLUMN_OFFSET + start_column) & 0x0f));
  155. st7565_send_cmd(PAM_SETCOLUMN_MSB | ((ST7565_COLUMN_OFFSET + start_column) >> 4 & 0x0f));
  156. st7565_send_data(&st7565_buffer[ST7565_BLOCK_SIZE * update_start], ST7565_BLOCK_SIZE);
  157. spi_stop();
  158. // Turn on display if it is off
  159. st7565_on();
  160. // Clear dirty flag
  161. st7565_dirty &= ~((ST7565_BLOCK_TYPE)1 << update_start);
  162. }
  163. void st7565_set_cursor(uint8_t col, uint8_t line) {
  164. uint16_t index = line * ST7565_DISPLAY_WIDTH + col * ST7565_FONT_WIDTH;
  165. // Out of bounds?
  166. if (index >= ST7565_MATRIX_SIZE) {
  167. index = 0;
  168. }
  169. st7565_cursor = &st7565_buffer[index];
  170. }
  171. void st7565_advance_page(bool clearPageRemainder) {
  172. uint16_t index = st7565_cursor - &st7565_buffer[0];
  173. uint8_t remaining = ST7565_DISPLAY_WIDTH - (index % ST7565_DISPLAY_WIDTH);
  174. if (clearPageRemainder) {
  175. // Remaining Char count
  176. remaining = remaining / ST7565_FONT_WIDTH;
  177. // Write empty character until next line
  178. while (remaining--)
  179. st7565_write_char(' ', false);
  180. } else {
  181. // Next page index out of bounds?
  182. if (index + remaining >= ST7565_MATRIX_SIZE) {
  183. index = 0;
  184. remaining = 0;
  185. }
  186. st7565_cursor = &st7565_buffer[index + remaining];
  187. }
  188. }
  189. void st7565_advance_char(void) {
  190. uint16_t nextIndex = st7565_cursor - &st7565_buffer[0] + ST7565_FONT_WIDTH;
  191. uint8_t remainingSpace = ST7565_DISPLAY_WIDTH - (nextIndex % ST7565_DISPLAY_WIDTH);
  192. // Do we have enough space on the current line for the next character
  193. if (remainingSpace < ST7565_FONT_WIDTH) {
  194. nextIndex += remainingSpace;
  195. }
  196. // Did we go out of bounds
  197. if (nextIndex >= ST7565_MATRIX_SIZE) {
  198. nextIndex = 0;
  199. }
  200. // Update cursor position
  201. st7565_cursor = &st7565_buffer[nextIndex];
  202. }
  203. // Main handler that writes character data to the display buffer
  204. void st7565_write_char(const char data, bool invert) {
  205. // Advance to the next line if newline
  206. if (data == '\n') {
  207. // Old source wrote ' ' until end of line...
  208. st7565_advance_page(true);
  209. return;
  210. }
  211. if (data == '\r') {
  212. st7565_advance_page(false);
  213. return;
  214. }
  215. // copy the current render buffer to check for dirty after
  216. static uint8_t st7565_temp_buffer[ST7565_FONT_WIDTH];
  217. memcpy(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH);
  218. STATIC_ASSERT(sizeof(font) >= ((ST7565_FONT_END + 1 - ST7565_FONT_START) * ST7565_FONT_WIDTH), "ST7565_FONT_END references outside array");
  219. // set the reder buffer data
  220. uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
  221. if (cast_data < ST7565_FONT_START || cast_data > ST7565_FONT_END) {
  222. memset(st7565_cursor, 0x00, ST7565_FONT_WIDTH);
  223. } else {
  224. const uint8_t *glyph = &font[(cast_data - ST7565_FONT_START) * ST7565_FONT_WIDTH];
  225. memcpy_P(st7565_cursor, glyph, ST7565_FONT_WIDTH);
  226. }
  227. // Invert if needed
  228. if (invert) {
  229. InvertCharacter(st7565_cursor);
  230. }
  231. // Dirty check
  232. if (memcmp(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH)) {
  233. uint16_t index = st7565_cursor - &st7565_buffer[0];
  234. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
  235. // Edgecase check if the written data spans the 2 chunks
  236. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << ((index + ST7565_FONT_WIDTH - 1) / ST7565_BLOCK_SIZE));
  237. }
  238. // Finally move to the next char
  239. st7565_advance_char();
  240. }
  241. void st7565_write(const char *data, bool invert) {
  242. const char *end = data + strlen(data);
  243. while (data < end) {
  244. st7565_write_char(*data, invert);
  245. data++;
  246. }
  247. }
  248. void st7565_write_ln(const char *data, bool invert) {
  249. st7565_write(data, invert);
  250. st7565_advance_page(true);
  251. }
  252. void st7565_pan(bool left) {
  253. uint16_t i = 0;
  254. for (uint16_t y = 0; y < ST7565_DISPLAY_HEIGHT / 8; y++) {
  255. if (left) {
  256. for (uint16_t x = 0; x < ST7565_DISPLAY_WIDTH - 1; x++) {
  257. i = y * ST7565_DISPLAY_WIDTH + x;
  258. st7565_buffer[i] = st7565_buffer[i + 1];
  259. }
  260. } else {
  261. for (uint16_t x = ST7565_DISPLAY_WIDTH - 1; x > 0; x--) {
  262. i = y * ST7565_DISPLAY_WIDTH + x;
  263. st7565_buffer[i] = st7565_buffer[i - 1];
  264. }
  265. }
  266. }
  267. st7565_dirty = ST7565_ALL_BLOCKS_MASK;
  268. }
  269. display_buffer_reader_t st7565_read_raw(uint16_t start_index) {
  270. if (start_index > ST7565_MATRIX_SIZE) start_index = ST7565_MATRIX_SIZE;
  271. display_buffer_reader_t ret_reader;
  272. ret_reader.current_element = &st7565_buffer[start_index];
  273. ret_reader.remaining_element_count = ST7565_MATRIX_SIZE - start_index;
  274. return ret_reader;
  275. }
  276. void st7565_write_raw_byte(const char data, uint16_t index) {
  277. if (index > ST7565_MATRIX_SIZE) index = ST7565_MATRIX_SIZE;
  278. if (st7565_buffer[index] == data) return;
  279. st7565_buffer[index] = data;
  280. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
  281. }
  282. void st7565_write_raw(const char *data, uint16_t size) {
  283. uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
  284. if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
  285. for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
  286. uint8_t c = *data++;
  287. if (st7565_buffer[i] == c) continue;
  288. st7565_buffer[i] = c;
  289. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
  290. }
  291. }
  292. void st7565_write_pixel(uint8_t x, uint8_t y, bool on) {
  293. if (x >= ST7565_DISPLAY_WIDTH) {
  294. return;
  295. }
  296. uint16_t index = x + (y / 8) * ST7565_DISPLAY_WIDTH;
  297. if (index >= ST7565_MATRIX_SIZE) {
  298. return;
  299. }
  300. uint8_t data = st7565_buffer[index];
  301. if (on) {
  302. data |= (1 << (y % 8));
  303. } else {
  304. data &= ~(1 << (y % 8));
  305. }
  306. if (st7565_buffer[index] != data) {
  307. st7565_buffer[index] = data;
  308. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
  309. }
  310. }
  311. #if defined(__AVR__)
  312. void st7565_write_P(const char *data, bool invert) {
  313. uint8_t c = pgm_read_byte(data);
  314. while (c != 0) {
  315. st7565_write_char(c, invert);
  316. c = pgm_read_byte(++data);
  317. }
  318. }
  319. void st7565_write_ln_P(const char *data, bool invert) {
  320. st7565_write_P(data, invert);
  321. st7565_advance_page(true);
  322. }
  323. void st7565_write_raw_P(const char *data, uint16_t size) {
  324. uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
  325. if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
  326. for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
  327. uint8_t c = pgm_read_byte(data++);
  328. if (st7565_buffer[i] == c) continue;
  329. st7565_buffer[i] = c;
  330. st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
  331. }
  332. }
  333. #endif // defined(__AVR__)
  334. bool st7565_on(void) {
  335. if (!st7565_initialized) {
  336. return st7565_active;
  337. }
  338. #if ST7565_TIMEOUT > 0
  339. st7565_timeout = timer_read32() + ST7565_TIMEOUT;
  340. #endif
  341. if (!st7565_active) {
  342. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  343. st7565_send_cmd(DISPLAY_ON);
  344. spi_stop();
  345. st7565_active = true;
  346. st7565_on_user();
  347. }
  348. return st7565_active;
  349. }
  350. __attribute__((weak)) void st7565_on_user(void) {}
  351. bool st7565_off(void) {
  352. if (!st7565_initialized) {
  353. return !st7565_active;
  354. }
  355. if (st7565_active) {
  356. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  357. st7565_send_cmd(DISPLAY_OFF);
  358. spi_stop();
  359. st7565_active = false;
  360. st7565_off_user();
  361. }
  362. return !st7565_active;
  363. }
  364. __attribute__((weak)) void st7565_off_user(void) {}
  365. bool st7565_is_on(void) {
  366. return st7565_active;
  367. }
  368. bool st7565_invert(bool invert) {
  369. if (!st7565_initialized) {
  370. return st7565_inverted;
  371. }
  372. if (invert != st7565_inverted) {
  373. spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
  374. st7565_send_cmd(invert ? INVERT_DISPLAY : NORMAL_DISPLAY);
  375. spi_stop();
  376. st7565_inverted = invert;
  377. }
  378. return st7565_inverted;
  379. }
  380. uint8_t st7565_max_chars(void) {
  381. return ST7565_DISPLAY_WIDTH / ST7565_FONT_WIDTH;
  382. }
  383. uint8_t st7565_max_lines(void) {
  384. return ST7565_DISPLAY_HEIGHT / ST7565_FONT_HEIGHT;
  385. }
  386. void st7565_task(void) {
  387. if (!st7565_initialized) {
  388. return;
  389. }
  390. #if ST7565_UPDATE_INTERVAL > 0
  391. if (timer_elapsed(st7565_update_timeout) >= ST7565_UPDATE_INTERVAL) {
  392. st7565_update_timeout = timer_read();
  393. st7565_set_cursor(0, 0);
  394. st7565_task_user();
  395. }
  396. #else
  397. st7565_set_cursor(0, 0);
  398. st7565_task_user();
  399. #endif
  400. // Smart render system, no need to check for dirty
  401. st7565_render();
  402. // Display timeout check
  403. #if ST7565_TIMEOUT > 0
  404. if (st7565_active && timer_expired32(timer_read32(), st7565_timeout)) {
  405. st7565_off();
  406. }
  407. #endif
  408. }
  409. __attribute__((weak)) void st7565_task_user(void) {}
  410. void st7565_reset(void) {
  411. gpio_write_pin_low(ST7565_RST_PIN);
  412. wait_ms(20);
  413. gpio_write_pin_high(ST7565_RST_PIN);
  414. wait_ms(20);
  415. }
  416. spi_status_t st7565_send_cmd(uint8_t cmd) {
  417. gpio_write_pin_low(ST7565_A0_PIN);
  418. return spi_write(cmd);
  419. }
  420. spi_status_t st7565_send_data(uint8_t *data, uint16_t length) {
  421. gpio_write_pin_high(ST7565_A0_PIN);
  422. return spi_transmit(data, length);
  423. }