My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

digipot_mcp4451_I2C_routines.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /**
  23. * digipot_mcp4451_I2C_routines.c
  24. * Adapted from https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html
  25. */
  26. #ifdef TARGET_LPC1768
  27. #include "../../../inc/MarlinConfigPre.h"
  28. #if MB(MKS_SBASE)
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #include "digipot_mcp4451_I2C_routines.h"
  33. // These two routines are exact copies of the lpc17xx_i2c.c routines. Couldn't link to
  34. // to the lpc17xx_i2c.c routines so had to copy them into this file & rename them.
  35. static uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx) {
  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. // Make sure start bit is not active
  47. if (I2Cx->I2CONSET & I2C_I2CONSET_STA)
  48. I2Cx->I2CONCLR = I2C_I2CONCLR_STAC;
  49. I2Cx->I2CONSET = I2C_I2CONSET_STO|I2C_I2CONSET_AA;
  50. I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;
  51. }
  52. I2C_M_SETUP_Type transferMCfg;
  53. #define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)
  54. uint8_t digipot_mcp4451_start(uint8_t sla) { // send slave address and write bit
  55. // Sometimes TX data ACK or NAK status is returned. That mean the start state didn't
  56. // happen which means only the value of the slave address was send. Keep looping until
  57. // the slave address and write bit are actually sent.
  58. do {
  59. _I2C_Stop(I2CDEV_M); // output stop state on I2C bus
  60. _I2C_Start(I2CDEV_M); // output start state on I2C bus
  61. while ((I2C_status != I2C_I2STAT_M_TX_START)
  62. && (I2C_status != I2C_I2STAT_M_TX_RESTART)
  63. && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK)
  64. && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)); //wait for start to be asserted
  65. LPC_I2C1->I2CONCLR = I2C_I2CONCLR_STAC; // clear start state before tansmitting slave address
  66. LPC_I2C1->I2DAT = (sla << 1) & I2C_I2DAT_BITMASK; // transmit slave address & write bit
  67. LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;
  68. LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC;
  69. while ((I2C_status != I2C_I2STAT_M_TX_SLAW_ACK)
  70. && (I2C_status != I2C_I2STAT_M_TX_SLAW_NACK)
  71. && (I2C_status != I2C_I2STAT_M_TX_DAT_ACK)
  72. && (I2C_status != I2C_I2STAT_M_TX_DAT_NACK)) { /* wait for slaw to finish */ }
  73. } while ( (I2C_status == I2C_I2STAT_M_TX_DAT_ACK) || (I2C_status == I2C_I2STAT_M_TX_DAT_NACK));
  74. return 1;
  75. }
  76. uint8_t digipot_mcp4451_send_byte(uint8_t data) {
  77. LPC_I2C1->I2DAT = data & I2C_I2DAT_BITMASK; // transmit data
  78. LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;
  79. LPC_I2C1->I2CONCLR = I2C_I2CONCLR_SIC;
  80. while (I2C_status != I2C_I2STAT_M_TX_DAT_ACK && I2C_status != I2C_I2STAT_M_TX_DAT_NACK); // wait for xmit to finish
  81. return 1;
  82. }
  83. #ifdef __cplusplus
  84. }
  85. #endif
  86. #endif // MB(MKS_SBASE)
  87. #endif // TARGET_LPC1768