My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

LCD_I2C_routines.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. // adapted from I2C/master/master.c example
  23. // https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html
  24. #ifdef TARGET_LPC1768
  25. extern int millis();
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include "../include/i2c_util.h"
  30. #include "../../../core/millis_t.h"
  31. //////////////////////////////////////////////////////////////////////////////////////
  32. // These two routines are exact copies of the lpc17xx_i2c.c routines. Couldn't link to
  33. // to the lpc17xx_i2c.c routines so had to copy them into this file & rename them.
  34. static uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx) {
  35. // Reset STA, STO, SI
  36. I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;
  37. // Enter to Master Transmitter mode
  38. I2Cx->I2CONSET = I2C_I2CONSET_STA;
  39. // Wait for complete
  40. while (!(I2Cx->I2CONSET & I2C_I2CONSET_SI));
  41. I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
  42. return (I2Cx->I2STAT & I2C_STAT_CODE_BITMASK);
  43. }
  44. static void _I2C_Stop (LPC_I2C_TypeDef *I2Cx) {
  45. /* Make sure start bit is not active */
  46. if (I2Cx->I2CONSET & I2C_I2CONSET_STA)
  47. I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
  48. I2Cx->I2CONSET = I2C_I2CONSET_STO|I2C_I2CONSET_AA;
  49. I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;
  50. }
  51. //////////////////////////////////////////////////////////////////////////////////////
  52. #define I2CDEV_S_ADDR 0x78 // from SSD1306 //actual address is 0x3C - shift left 1 with LSB set to 0 to indicate write
  53. #define BUFFER_SIZE 0x1 // only do single byte transfers with LCDs
  54. I2C_M_SETUP_Type transferMCfg;
  55. #define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)
  56. // Send slave address and write bit
  57. uint8_t u8g_i2c_start(const uint8_t sla) {
  58. // Sometimes TX data ACK or NAK status is returned. That mean the start state didn't
  59. // happen which means only the value of the slave address was send. Keep looping until
  60. // the slave address and write bit are actually sent.
  61. do{
  62. _I2C_Stop(I2CDEV_M); // output stop state on I2C bus
  63. _I2C_Start(I2CDEV_M); // output start state on I2C bus
  64. while ((I2C_status != I2C_I2STAT_M_TX_START)
  65. && (I2C_status != I2C_I2STAT_M_TX_RESTART)
  66. && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK)
  67. && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)); //wait for start to be asserted
  68. LPC_I2C1->I2CONCLR = I2C_I2CONCLR_STAC; // clear start state before tansmitting slave address
  69. LPC_I2C1->I2DAT = I2CDEV_S_ADDR & I2C_I2DAT_BITMASK; // transmit slave address & write bit
  70. LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;
  71. LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC;
  72. while ((I2C_status != I2C_I2STAT_M_TX_SLAW_ACK)
  73. && (I2C_status != I2C_I2STAT_M_TX_SLAW_NACK)
  74. && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK)
  75. && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)); //wait for slaw to finish
  76. }while ( (I2C_status == I2C_I2STAT_M_TX_DAT_ACK) || (I2C_status == I2C_I2STAT_M_TX_DAT_NACK));
  77. return 1;
  78. }
  79. void u8g_i2c_init(const uint8_t clock_option) {
  80. configure_i2c(clock_option);
  81. u8g_i2c_start(0); // Send slave address and write bit
  82. }
  83. uint8_t u8g_i2c_send_byte(uint8_t data) {
  84. #define I2C_TIMEOUT 3
  85. LPC_I2C1->I2DAT = data & I2C_I2DAT_BITMASK; // transmit data
  86. LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;
  87. LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC;
  88. const millis_t timeout = millis() + I2C_TIMEOUT;
  89. while ((I2C_status != I2C_I2STAT_M_TX_DAT_ACK) && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK) && PENDING(millis(), timeout)); // wait for xmit to finish
  90. // had hangs with SH1106 so added time out - have seen temporary screen corruption when this happens
  91. return 1;
  92. }
  93. void u8g_i2c_stop() {
  94. }
  95. #ifdef __cplusplus
  96. }
  97. #endif
  98. #endif // TARGET_LPC1768