My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

digipot_mcp4451_I2C_routines.c 5.4KB

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