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 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://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. #include "../include/i2c_util.h"
  26. #include "../../../core/millis_t.h"
  27. extern int millis();
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. //////////////////////////////////////////////////////////////////////////////////////
  32. #define I2CDEV_S_ADDR 0x78 // From SSD1306 (actual address is 0x3C - shift left 1 with LSB set to 0 to indicate write)
  33. // Send slave address and write bit
  34. uint8_t u8g_i2c_start(const uint8_t sla) {
  35. // Sometimes TX data ACK or NAK status is returned. That mean the start state didn't
  36. // happen which means only the value of the slave address was send. Keep looping until
  37. // the slave address and write bit are actually sent.
  38. do{
  39. _I2C_Stop(I2CDEV_M); // output stop state on I2C bus
  40. _I2C_Start(I2CDEV_M); // output start state on I2C bus
  41. while ((I2C_status != I2C_I2STAT_M_TX_START)
  42. && (I2C_status != I2C_I2STAT_M_TX_RESTART)
  43. && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK)
  44. && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)); //wait for start to be asserted
  45. LPC_I2C1->I2CONCLR = I2C_I2CONCLR_STAC; // clear start state before tansmitting slave address
  46. LPC_I2C1->I2DAT = I2CDEV_S_ADDR & I2C_I2DAT_BITMASK; // transmit slave address & write bit
  47. LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;
  48. LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC;
  49. while ((I2C_status != I2C_I2STAT_M_TX_SLAW_ACK)
  50. && (I2C_status != I2C_I2STAT_M_TX_SLAW_NACK)
  51. && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK)
  52. && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)); //wait for slaw to finish
  53. }while ( (I2C_status == I2C_I2STAT_M_TX_DAT_ACK) || (I2C_status == I2C_I2STAT_M_TX_DAT_NACK));
  54. return 1;
  55. }
  56. void u8g_i2c_init(const uint8_t clock_option) {
  57. configure_i2c(clock_option);
  58. u8g_i2c_start(0); // Send slave address and write bit
  59. }
  60. uint8_t u8g_i2c_send_byte(uint8_t data) {
  61. #define I2C_TIMEOUT 3
  62. LPC_I2C1->I2DAT = data & I2C_I2DAT_BITMASK; // transmit data
  63. LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;
  64. LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC;
  65. const millis_t timeout = millis() + I2C_TIMEOUT;
  66. 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
  67. // had hangs with SH1106 so added time out - have seen temporary screen corruption when this happens
  68. return 1;
  69. }
  70. void u8g_i2c_stop() {
  71. }
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75. #endif // TARGET_LPC1768