You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.7 KiB

  1. /*
  2. Copyright 2016 Fred Sundvik <fsundvik@gmail.com>
  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. #ifndef _GDISP_LLD_BOARD_H
  15. #define _GDISP_LLD_BOARD_H
  16. static const I2CConfig i2ccfg = {
  17. 400000 // clock speed (Hz); 400kHz max for IS31
  18. };
  19. static const uint8_t led_mask[] = {
  20. 0xFF, 0x00, /* C1-1 -> C1-16 */
  21. 0xFF, 0x00, /* C2-1 -> C2-16 */
  22. 0xFF, 0x00, /* C3-1 -> C3-16 */
  23. 0xFF, 0x00, /* C4-1 -> C4-16 */
  24. 0x3F, 0x00, /* C5-1 -> C5-16 */
  25. 0x00, 0x00, /* C6-1 -> C6-16 */
  26. 0x00, 0x00, /* C7-1 -> C7-16 */
  27. 0x00, 0x00, /* C8-1 -> C8-16 */
  28. 0x00, 0x00, /* C9-1 -> C9-16 */
  29. };
  30. // The address of the LED
  31. #define LA(c, r) (c + r * 16 )
  32. // Need to be an address that is not mapped, but inside the range of the controller matrix
  33. #define NA LA(8, 8)
  34. // The numbers in the comments are the led numbers DXX on the PCB
  35. // The mapping is taken from the schematic of left hand side
  36. static const uint8_t led_mapping[GDISP_SCREEN_HEIGHT][GDISP_SCREEN_WIDTH] = {
  37. // 45 44 43 42 41 40 39
  38. { LA(1, 1), LA(1, 0), LA(0, 4), LA(0, 3), LA(0, 2), LA(0, 1), LA(0, 0)},
  39. // 52 51 50 49 48 47 46
  40. { LA(2, 3), LA(2, 2), LA(2, 1), LA(2, 0), LA(1, 4), LA(1, 3), LA(1, 2) },
  41. // 58 57 56 55 54 53 N/A
  42. { LA(3, 4), LA(3, 3), LA(3, 2), LA(3, 1), LA(3, 0), LA(2, 4), NA },
  43. // 67 66 65 64 63 62 61
  44. { LA(5, 3), LA(5, 2), LA(5, 1), LA(5, 0), LA(4, 4), LA(4, 3), LA(4, 2) },
  45. // 76 75 74 73 72 60 59
  46. { LA(7, 3), LA(7, 2), LA(7, 1), LA(7, 0), LA(6, 3), LA(4, 1), LA(4, 0) },
  47. // N/A N/A N/A N/A N/A N/A 68
  48. { NA, NA, NA, NA, NA, NA, LA(5, 4) },
  49. // N/A N/A N/A N/A 71 70 69
  50. { NA, NA, NA, NA, LA(6, 2), LA(6, 1), LA(6, 0) },
  51. };
  52. #define IS31_ADDR_DEFAULT 0x74 // AD connected to GND
  53. #define IS31_TIMEOUT 5000
  54. static GFXINLINE void init_board(GDisplay *g) {
  55. (void) g;
  56. /* I2C pins */
  57. palSetPadMode(GPIOB, 0, PAL_MODE_ALTERNATIVE_2); // PTB0/I2C0/SCL
  58. palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATIVE_2); // PTB1/I2C0/SDA
  59. palSetPadMode(GPIOB, 16, PAL_MODE_OUTPUT_PUSHPULL);
  60. palClearPad(GPIOB, 16);
  61. /* start I2C */
  62. i2cStart(&I2CD1, &i2ccfg);
  63. // try high drive (from kiibohd)
  64. I2CD1.i2c->C2 |= I2Cx_C2_HDRS;
  65. // try glitch fixing (from kiibohd)
  66. I2CD1.i2c->FLT = 4;
  67. }
  68. static GFXINLINE void post_init_board(GDisplay *g) {
  69. (void) g;
  70. }
  71. static GFXINLINE const uint8_t* get_led_mask(GDisplay* g) {
  72. (void) g;
  73. return led_mask;
  74. }
  75. static GFXINLINE uint8_t get_led_address(GDisplay* g, uint16_t x, uint16_t y)
  76. {
  77. (void) g;
  78. return led_mapping[y][x];
  79. }
  80. static GFXINLINE void set_hardware_shutdown(GDisplay* g, bool shutdown) {
  81. (void) g;
  82. if(!shutdown) {
  83. palSetPad(GPIOB, 16);
  84. }
  85. else {
  86. palClearPad(GPIOB, 16);
  87. }
  88. }
  89. static GFXINLINE void write_data(GDisplay *g, uint8_t* data, uint16_t length) {
  90. (void) g;
  91. i2cMasterTransmitTimeout(&I2CD1, IS31_ADDR_DEFAULT, data, length, 0, 0, TIME_US2I(IS31_TIMEOUT));
  92. }
  93. #endif /* _GDISP_LLD_BOARD_H */