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.

digipot_mcp4451_I2C_routines.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include "../../../inc/MarlinConfigPre.h"
  26. #if MB(MKS_SBASE)
  27. #include "digipot_mcp4451_I2C_routines.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. // These two routines are exact copies of the lpc17xx_i2c.c routines. Couldn't link to
  32. // to the lpc17xx_i2c.c routines so had to copy them into this file & rename them.
  33. static uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx) {
  34. // Reset STA, STO, SI
  35. I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;
  36. // Enter to Master Transmitter mode
  37. I2Cx->I2CONSET = I2C_I2CONSET_STA;
  38. // Wait for complete
  39. while (!(I2Cx->I2CONSET & I2C_I2CONSET_SI));
  40. I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
  41. return (I2Cx->I2STAT & I2C_STAT_CODE_BITMASK);
  42. }
  43. static void _I2C_Stop(LPC_I2C_TypeDef *I2Cx) {
  44. // Make sure start bit is not active
  45. if (I2Cx->I2CONSET & I2C_I2CONSET_STA)
  46. I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
  47. I2Cx->I2CONSET = I2C_I2CONSET_STO|I2C_I2CONSET_AA;
  48. I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;
  49. }
  50. PINSEL_CFG_Type PinCfg;
  51. I2C_M_SETUP_Type transferMCfg;
  52. #define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)
  53. uint8_t digipot_mcp4451_start(uint8_t sla) { // send slave address and write bit
  54. // Sometimes TX data ACK or NAK status is returned. That mean the start state didn't
  55. // happen which means only the value of the slave address was send. Keep looping until
  56. // the slave address and write bit are actually sent.
  57. do {
  58. _I2C_Stop(I2CDEV_M); // output stop state on I2C bus
  59. _I2C_Start(I2CDEV_M); // output start state on I2C bus
  60. while ((I2C_status != I2C_I2STAT_M_TX_START)
  61. && (I2C_status != I2C_I2STAT_M_TX_RESTART)
  62. && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK)
  63. && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)); //wait for start to be asserted
  64. LPC_I2C1->I2CONCLR = I2C_I2CONCLR_STAC; // clear start state before tansmitting slave address
  65. LPC_I2C1->I2DAT = (sla << 1) & I2C_I2DAT_BITMASK; // transmit slave address & write bit
  66. LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;
  67. LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC;
  68. while ((I2C_status != I2C_I2STAT_M_TX_SLAW_ACK)
  69. && (I2C_status != I2C_I2STAT_M_TX_SLAW_NACK)
  70. && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK)
  71. && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)) { /* wait for slaw to finish */ }
  72. } while ( (I2C_status == I2C_I2STAT_M_TX_DAT_ACK) || (I2C_status == I2C_I2STAT_M_TX_DAT_NACK));
  73. return 1;
  74. }
  75. void digipot_mcp4451_init(void) {
  76. /**
  77. * Init I2C pin connect
  78. */
  79. PinCfg.OpenDrain = 0;
  80. PinCfg.Pinmode = 0;
  81. #if USEDI2CDEV_M == 0
  82. PinCfg.Funcnum = 1;
  83. PinCfg.Pinnum = 27;
  84. PinCfg.Portnum = 0;
  85. PINSEL_ConfigPin(&PinCfg); // SDA0 / D57 AUX-1
  86. PinCfg.Pinnum = 28;
  87. PINSEL_ConfigPin(&PinCfg); // SCL0 / D58 AUX-1
  88. #elif USEDI2CDEV_M == 1
  89. PinCfg.Funcnum = 3;
  90. PinCfg.Pinnum = 0;
  91. PinCfg.Portnum = 0;
  92. PINSEL_ConfigPin(&PinCfg); // SDA1 / D20 SCA
  93. PinCfg.Pinnum = 1;
  94. PINSEL_ConfigPin(&PinCfg); // SCL1 / D21 SCL
  95. #elif USEDI2CDEV_M == 2
  96. PinCfg.Funcnum = 2;
  97. PinCfg.Pinnum = 10;
  98. PinCfg.Portnum = 0;
  99. PINSEL_ConfigPin(&PinCfg); // SDA2 / D38 X_ENABLE_PIN
  100. PinCfg.Pinnum = 11;
  101. PINSEL_ConfigPin(&PinCfg); // SCL2 / D55 X_DIR_PIN
  102. #endif
  103. // Initialize I2C peripheral
  104. I2C_Init(I2CDEV_M, 400000); // hardwired to 400KHz bit rate, 100KHz is the other option
  105. // Enable Master I2C operation
  106. I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
  107. }
  108. uint8_t digipot_mcp4451_send_byte(uint8_t data) {
  109. LPC_I2C1->I2DAT = data & I2C_I2DAT_BITMASK; // transmit data
  110. LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;
  111. LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC;
  112. while (I2C_status != I2C_I2STAT_M_TX_DAT_ACK && I2C_status != I2C_I2STAT_M_TX_DAT_NACK); // wait for xmit to finish
  113. return 1;
  114. }
  115. #ifdef __cplusplus
  116. }
  117. #endif
  118. #endif // MB(MKS_SBASE)
  119. #endif // TARGET_LPC1768