Przeglądaj źródła

LPC1768: Allow I2C master channel override (#16584)

0r31 4 lat temu
rodzic
commit
451062553e

+ 7
- 40
Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.c Wyświetl plik

@@ -20,8 +20,10 @@
20 20
  *
21 21
  */
22 22
 
23
-// adapted from  I2C/master/master.c example
24
-//   https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html
23
+/**
24
+ * digipot_mcp4451_I2C_routines.c
25
+ * Adapted from https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html
26
+ */
25 27
 
26 28
 #ifdef TARGET_LPC1768
27 29
 
@@ -29,12 +31,13 @@
29 31
 
30 32
 #if MB(MKS_SBASE)
31 33
 
32
-#include "digipot_mcp4451_I2C_routines.h"
33
-
34 34
 #ifdef __cplusplus
35 35
   extern "C" {
36 36
 #endif
37 37
 
38
+#include "digipot_mcp4451_I2C_routines.h"
39
+#include "i2c_util.h"
40
+
38 41
 // These two routines are exact copies of the lpc17xx_i2c.c routines.  Couldn't link to
39 42
 // to the lpc17xx_i2c.c routines so had to copy them into this file & rename them.
40 43
 
@@ -60,7 +63,6 @@ static void _I2C_Stop(LPC_I2C_TypeDef *I2Cx) {
60 63
   I2Cx->I2CONCLR = I2C_I2CONCLR_SIC;
61 64
 }
62 65
 
63
-PINSEL_CFG_Type PinCfg;
64 66
 I2C_M_SETUP_Type transferMCfg;
65 67
 
66 68
 #define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)
@@ -89,41 +91,6 @@ uint8_t digipot_mcp4451_start(uint8_t sla) {  // send slave address and write bi
89 91
   return 1;
90 92
 }
91 93
 
92
-void digipot_mcp4451_init() {
93
-  /**
94
-   * Init I2C pin connect
95
-   */
96
-  PinCfg.OpenDrain = 0;
97
-  PinCfg.Pinmode = 0;
98
-  #if USEDI2CDEV_M == 0
99
-    PinCfg.Funcnum = 1;
100
-    PinCfg.Pinnum = 27;
101
-    PinCfg.Portnum = 0;
102
-    PINSEL_ConfigPin(&PinCfg); // SDA0 / D57  AUX-1
103
-    PinCfg.Pinnum = 28;
104
-    PINSEL_ConfigPin(&PinCfg); // SCL0 / D58  AUX-1
105
-  #elif 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
-  #elif USEDI2CDEV_M == 2
113
-    PinCfg.Funcnum = 2;
114
-    PinCfg.Pinnum = 10;
115
-    PinCfg.Portnum = 0;
116
-    PINSEL_ConfigPin(&PinCfg); // SDA2 / D38  X_ENABLE_PIN
117
-    PinCfg.Pinnum = 11;
118
-    PINSEL_ConfigPin(&PinCfg); // SCL2 / D55  X_DIR_PIN
119
-  #endif
120
-  // Initialize I2C peripheral
121
-  I2C_Init(I2CDEV_M, 400000);  // hardwired to 400KHz bit rate, 100KHz is the other option
122
-
123
-  // Enable Master I2C operation
124
-  I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
125
-}
126
-
127 94
 uint8_t digipot_mcp4451_send_byte(uint8_t data) {
128 95
   LPC_I2C1->I2DAT = data & I2C_I2DAT_BITMASK; // transmit data
129 96
   LPC_I2C1->I2CONSET = I2C_I2CONSET_AA;

+ 4
- 15
Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.h Wyświetl plik

@@ -21,20 +21,10 @@
21 21
  */
22 22
 #pragma once
23 23
 
24
-// adapted from  I2C/master/master.c example
25
-//   https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html
26
-
27
-#define USEDI2CDEV_M  1  // use I2C1 controller
28
-
29
-#if USEDI2CDEV_M == 0
30
-  #define I2CDEV_M LPC_I2C0
31
-#elif USEDI2CDEV_M == 1
32
-  #define I2CDEV_M LPC_I2C1
33
-#elif USEDI2CDEV_M == 2
34
-  #define I2CDEV_M LPC_I2C2
35
-#else
36
-  #error "Master I2C device not defined!"
37
-#endif
24
+/**
25
+ * digipot_mcp4451_I2C_routines.h
26
+ * Adapted from https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html
27
+ */
38 28
 
39 29
 #ifdef __cplusplus
40 30
   extern "C" {
@@ -45,7 +35,6 @@
45 35
 #include <lpc17xx_libcfg_default.h>
46 36
 
47 37
 uint8_t digipot_mcp4451_start(uint8_t sla);
48
-void digipot_mcp4451_init();
49 38
 uint8_t digipot_mcp4451_send_byte(uint8_t data);
50 39
 
51 40
 #ifdef __cplusplus

+ 71
- 0
Marlin/src/HAL/HAL_LPC1768/include/i2c_util.c Wyświetl plik

@@ -0,0 +1,71 @@
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
+/**
24
+ * HAL_LPC1768/include/i2c_util.c
25
+ */
26
+
27
+#ifdef TARGET_LPC1768
28
+
29
+#include "../../../inc/MarlinConfigPre.h"
30
+#include "i2c_util.h"
31
+
32
+#define U8G_I2C_OPT_FAST 16  // from u8g.h
33
+
34
+#ifdef __cplusplus
35
+  extern "C" {
36
+#endif
37
+
38
+void configure_i2c(const uint8_t clock_option) {
39
+  /**
40
+   * Init I2C pin connect
41
+   */
42
+  PINSEL_CFG_Type PinCfg;
43
+  PinCfg.OpenDrain = 0;
44
+  PinCfg.Pinmode = 0;
45
+  PinCfg.Portnum = 0;
46
+  #if USEDI2CDEV_M == 0
47
+    PinCfg.Funcnum = 1;
48
+    PinCfg.Pinnum = 27;     // SDA0 / D57  AUX-1 ... SCL0 / D58  AUX-1
49
+  #elif USEDI2CDEV_M == 1
50
+    PinCfg.Funcnum = 3;
51
+    PinCfg.Pinnum = 0;      // SDA1 / D20 SCA ... SCL1 / D21 SCL
52
+  #elif USEDI2CDEV_M == 2
53
+    PinCfg.Funcnum = 2;
54
+    PinCfg.Pinnum = 10;     // SDA2 / D38  X_ENABLE_PIN ... SCL2 / D55  X_DIR_PIN
55
+  #endif
56
+  PINSEL_ConfigPin(&PinCfg);
57
+  PinCfg.Pinnum += 1;
58
+  PINSEL_ConfigPin(&PinCfg);
59
+
60
+  // Initialize I2C peripheral
61
+  I2C_Init(I2CDEV_M, (clock_option & U8G_I2C_OPT_FAST) ? 400000: 100000);  // LCD data rates
62
+
63
+  // Enable Master I2C operation
64
+  I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
65
+}
66
+
67
+#ifdef __cplusplus
68
+  }
69
+#endif
70
+
71
+#endif // TARGET_LPC1768

+ 46
- 0
Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h Wyświetl plik

@@ -0,0 +1,46 @@
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
+#pragma once
23
+
24
+/**
25
+ * HAL_LPC1768/include/i2c_util.h
26
+ */
27
+
28
+#ifndef USEDI2CDEV_M
29
+  #define USEDI2CDEV_M  1  // By default use I2C1 controller
30
+#endif
31
+
32
+#if USEDI2CDEV_M == 0
33
+  #define I2CDEV_M LPC_I2C0
34
+#elif USEDI2CDEV_M == 1
35
+  #define I2CDEV_M LPC_I2C1
36
+#elif USEDI2CDEV_M == 2
37
+  #define I2CDEV_M LPC_I2C2
38
+#else
39
+  #error "Master I2C device not defined!"
40
+#endif
41
+
42
+#include <lpc17xx_i2c.h>
43
+#include <lpc17xx_pinsel.h>
44
+#include <lpc17xx_libcfg_default.h>
45
+
46
+void configure_i2c(const uint8_t clock_option);

+ 7
- 60
Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.cpp Wyświetl plik

@@ -31,19 +31,15 @@ extern int millis();
31 31
   extern "C" {
32 32
 #endif
33 33
 
34
-#include <lpc17xx_i2c.h>
35
-#include <lpc17xx_pinsel.h>
36
-#include <lpc17xx_libcfg_default.h>
37
-
34
+#include "../include/i2c_util.h"
38 35
 #include "../../../core/millis_t.h"
39 36
 
40
-
41 37
 //////////////////////////////////////////////////////////////////////////////////////
42 38
 
43 39
 // These two routines are exact copies of the lpc17xx_i2c.c routines.  Couldn't link to
44 40
 // to the lpc17xx_i2c.c routines so had to copy them into this file & rename them.
45 41
 
46
-static uint32_t _I2C_Start (LPC_I2C_TypeDef *I2Cx) {
42
+static uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx) {
47 43
   // Reset STA, STO, SI
48 44
   I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC;
49 45
 
@@ -67,30 +63,16 @@ static void _I2C_Stop (LPC_I2C_TypeDef *I2Cx) {
67 63
 
68 64
 //////////////////////////////////////////////////////////////////////////////////////
69 65
 
70
-#define U8G_I2C_OPT_FAST 16  // from u8g.h
71
-
72
-#define USEDI2CDEV_M            1
73
-
74 66
 #define I2CDEV_S_ADDR   0x78  // from SSD1306  //actual address is 0x3C - shift left 1 with LSB set to 0 to indicate write
75 67
 
76 68
 #define BUFFER_SIZE                     0x1  // only do single byte transfers with LCDs
77 69
 
78
-#if (USEDI2CDEV_M == 0)
79
-  #define I2CDEV_M LPC_I2C0
80
-#elif (USEDI2CDEV_M == 1)
81
-  #define I2CDEV_M LPC_I2C1
82
-#elif (USEDI2CDEV_M == 2)
83
-  #define I2CDEV_M LPC_I2C2
84
-#else
85
-  #error "Master I2C device not defined!"
86
-#endif
87
-
88
-PINSEL_CFG_Type PinCfg;
89 70
 I2C_M_SETUP_Type transferMCfg;
90 71
 
91 72
 #define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK)
92 73
 
93
-uint8_t u8g_i2c_start(uint8_t sla) {  // send slave address and write bit
74
+// Send slave address and write bit
75
+uint8_t u8g_i2c_start(const uint8_t sla) {
94 76
   // Sometimes TX data ACK or NAK status is returned.  That mean the start state didn't
95 77
   // happen which means only the value of the slave address was send.  Keep looping until
96 78
   // the slave address and write bit are actually sent.
@@ -114,44 +96,9 @@ uint8_t u8g_i2c_start(uint8_t sla) {  // send slave address and write bit
114 96
   return 1;
115 97
 }
116 98
 
117
-void u8g_i2c_init(uint8_t clock_option) {
118
-
119
-  /**
120
-   * Init I2C pin connect
121
-   */
122
-  PinCfg.OpenDrain = 0;
123
-  PinCfg.Pinmode = 0;
124
-  #if ((USEDI2CDEV_M == 0))
125
-    PinCfg.Funcnum = 1;
126
-    PinCfg.Pinnum = 27;
127
-    PinCfg.Portnum = 0;
128
-    PINSEL_ConfigPin(&PinCfg); // SDA0 / D57  AUX-1
129
-    PinCfg.Pinnum = 28;
130
-    PINSEL_ConfigPin(&PinCfg); // SCL0 / D58  AUX-1
131
-  #endif
132
-  #if ((USEDI2CDEV_M == 1))
133
-    PinCfg.Funcnum = 3;
134
-    PinCfg.Pinnum = 0;
135
-    PinCfg.Portnum = 0;
136
-    PINSEL_ConfigPin(&PinCfg);  // SDA1 / D20 SCA
137
-    PinCfg.Pinnum = 1;
138
-    PINSEL_ConfigPin(&PinCfg);  // SCL1 / D21 SCL
139
-  #endif
140
-  #if ((USEDI2CDEV_M == 2))
141
-    PinCfg.Funcnum = 2;
142
-    PinCfg.Pinnum = 10;
143
-    PinCfg.Portnum = 0;
144
-    PINSEL_ConfigPin(&PinCfg); // SDA2 / D38  X_ENABLE_PIN
145
-    PinCfg.Pinnum = 11;
146
-    PINSEL_ConfigPin(&PinCfg); // SCL2 / D55  X_DIR_PIN
147
-  #endif
148
-  // Initialize I2C peripheral
149
-  I2C_Init(I2CDEV_M, (clock_option & U8G_I2C_OPT_FAST) ? 400000: 100000);  // LCD data rates
150
-
151
-  // Enable Master I2C operation
152
-  I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE);
153
-
154
-  u8g_i2c_start(0); // send slave address and write bit
99
+void u8g_i2c_init(const uint8_t clock_option) {
100
+  configure_i2c(clock_option);
101
+  u8g_i2c_start(0); // Send slave address and write bit
155 102
 }
156 103
 
157 104
 uint8_t u8g_i2c_send_byte(uint8_t data) {

+ 2
- 2
Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.h Wyświetl plik

@@ -21,8 +21,8 @@
21 21
  */
22 22
 #pragma once
23 23
 
24
-void u8g_i2c_init(uint8_t options);
25
-uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos);
24
+void u8g_i2c_init(const uint8_t clock_options);
25
+//uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos);
26 26
 uint8_t u8g_i2c_start(uint8_t sla);
27 27
 uint8_t u8g_i2c_send_byte(uint8_t data);
28 28
 void u8g_i2c_stop();

+ 0
- 2
Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_hw_i2c.cpp Wyświetl plik

@@ -86,8 +86,6 @@
86 86
 #define I2C_CMD_MODE    0x000
87 87
 #define I2C_DATA_MODE   0x040
88 88
 
89
-//#define U8G_I2C_OPT_FAST 16
90
-
91 89
 uint8_t u8g_com_ssd_I2C_start_sequence(u8g_t *u8g) {
92 90
   /* are we requested to set the a0 state? */
93 91
   if (u8g->pin_list[U8G_PI_SET_A0] == 0) return 1;

+ 0
- 254
Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_sw_i2c.cpp under construction Wyświetl plik

@@ -1,254 +0,0 @@
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
-/**
24
- * Based on U8G2 code - u8x8_byte.c
25
- *
26
- * Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
27
- *
28
- * Copyright (c) 2016, olikraus@gmail.com
29
- * All rights reserved.
30
- *
31
- * Redistribution and use in source and binary forms, with or without modification,
32
- * are permitted provided that the following conditions are met:
33
- *
34
- *  * Redistributions of source code must retain the above copyright notice, this list
35
- *    of conditions and the following disclaimer.
36
- *
37
- *  * Redistributions in binary form must reproduce the above copyright notice, this
38
- *    list of conditions and the following disclaimer in the documentation and/or other
39
- *    materials provided with the distribution.
40
- *
41
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
42
- * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
43
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
44
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
46
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
50
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
53
- * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54
- */
55
-
56
-/**
57
- * Software i2c,
58
- * ignores ACK response (which is anyway not provided by some displays)
59
- * also does not allow reading from the device
60
- */
61
-
62
-#ifdef TARGET_LPC1768
63
-
64
-#include "../../inc/MarlinConfigPre.h"
65
-
66
-#if HAS_GRAPHICAL_LCD
67
-
68
-#include <U8glib.h>
69
-
70
-//void pinMode(int16_t pin, uint8_t mode);
71
-//void digitalWrite(int16_t pin, uint8_t pin_status);
72
-
73
-
74
-#define I2C_SLA         (0x3C*2)
75
-//#define I2C_CMD_MODE  0x080
76
-#define I2C_CMD_MODE    0x000
77
-#define I2C_DATA_MODE   0x040
78
-
79
-//static uint8_t I2C_speed; // 3 - 400KHz, 13 - 100KHz
80
-//#define SPEED_400KHz 3
81
-//#define SPEED_100KHz 13
82
-
83
-//    #define U8G_I2C_OPT_FAST 16
84
-
85
-uint8_t SCL_pin_HAL_LPC1768_sw_I2C, SCL_port_HAL_LPC1768_sw_I2C, SDA_pin_HAL_LPC1768_sw_I2C, SDA_port_HAL_LPC1768_sw_I2C;
86
-
87
-#define SPI_SPEED 2  //20: 200KHz 5:750KHz 2:3-4MHz
88
-
89
-uint8_t u8g_i2c_send_byte_sw(uint8_t data) {
90
-  for (uint8_t i = 0; i < 9; i++) {   // 1 extra bit for the ack/nak
91
-
92
-    if (val & 0x80)
93
-      for (uint8_t j = 0; j < SPI_SPEED; j++) {
94
-        LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
95
-        LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
96
-        LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
97
-      }
98
-    else
99
-      for (uint8_t j = 0; j < SPI_SPEED; j++) {
100
-        LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
101
-        LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
102
-        LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
103
-      }
104
-
105
-    for (uint8_t j = 0; j < SPI_SPEED; j++) {
106
-      LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
107
-      LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
108
-      LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
109
-      LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
110
-      LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
111
-    }
112
-
113
-    for (uint8_t j = 0; j < SPI_SPEED; j++) {
114
-      LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
115
-      LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
116
-    }
117
-    val <<= 1;
118
-  }
119
-  return 1;
120
-}
121
-
122
-
123
-uint8_t u8g_i2c_start_sw(uint8_t sla) {  // assert start condition and then send slave address with write bit
124
-  /* send the start condition, both lines go from 1 to 0 */
125
-
126
-  LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
127
-  LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
128
-  DELAY_US(2);
129
-  LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
130
-  DELAY_US(2);
131
-  LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
132
-  DELAY_US(2);
133
-  LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C);
134
-  DELAY_US(2);
135
-  LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C);
136
-
137
-  u8g_i2c_send_byte_sw(I2C_SLA);  // send slave address with write bit
138
-}
139
-
140
-
141
-void u8g_i2c_stop_sw() { }
142
-
143
-void u8g_i2c_init_sw(uint8_t clock_option) { u8g_i2c_start(0); } // send slave address and write bit
144
-
145
-uint8_t  u8g_com_ssd_I2C_start_sequence_sw(u8g_t *u8g) {
146
-  /* are we requested to set the a0 state? */
147
-  if (u8g->pin_list[U8G_PI_SET_A0] == 0) return 1;
148
-
149
-  /* setup bus, might be a repeated start */
150
-  if (u8g_i2c_start(I2C_SLA) == 0) return 0;
151
-  if (u8g->pin_list[U8G_PI_A0_STATE] == 0) {
152
-    if (u8g_i2c_send_byte(I2C_CMD_MODE) == 0) return 0;
153
-  }
154
-  else if (u8g_i2c_send_byte(I2C_DATA_MODE) == 0) return 0;
155
-
156
-  u8g->pin_list[U8G_PI_SET_A0] = 0;
157
-
158
-  return 1;
159
-}
160
-
161
-uint8_t u8g_com_HAL_LPC1768_ssd_sw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) {
162
-  switch (msg) {
163
-    case U8G_COM_MSG_INIT:
164
-
165
-      #define LPC1768_PIN_PORT(pin) ((uint8_t)((pin >> 5) & 0b111))
166
-      #define LPC1768_PIN_PIN(pin) ((uint8_t)(pin & 0b11111))
167
-      SCL_pin_HAL_LPC1768_sw_I2C = LPC1768_PIN_PIN(u8g->pin_list[U8G_PI_SCL]);
168
-      SCL_port_HAL_LPC1768_sw_I2C = LPC1768_PIN_PORT(u8g->pin_list[U8G_PI_SCL]);
169
-      SDA_pin_HAL_LPC1768_sw_I2C = LPC1768_PIN_PIN(u8g->pin_list[U8G_PI_SDA]);
170
-      SDA_port_HAL_LPC1768_sw_I2C = LPC1768_PIN_PORT(u8g->pin_list[U8G_PI_SDA]);
171
-      // As defined by Arduino INPUT(0x0), OUTPUT(0x1), INPUT_PULLUP(0x2)
172
-      #define OUTPUT 0x1
173
-      u8g_SetPIOutput(u8g, U8G_PI_SCL);
174
-      u8g_SetPIOutput(u8g, U8G_PI_SDA);
175
-      if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_CS])  u8g_SetPIOutput(u8g, U8G_PI_CS);
176
-      if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_A0])  u8g_SetPIOutput(u8g, U8G_PI_A0);
177
-      if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_RESET])  u8g_SetPIOutput(u8g, U8G_PI_RESET);
178
-
179
-      //u8g_com_arduino_digital_write(u8g, U8G_PI_SCL, HIGH);
180
-      //u8g_com_arduino_digital_write(u8g, U8G_PI_SDA, HIGH);
181
-      //u8g->pin_list[U8G_PI_A0_STATE] = 0;       /* initial RS state: unknown mode */
182
-
183
-      u8g_i2c_init_sw(u8g->pin_list[U8G_PI_I2C_OPTION]);
184
-      u8g_com_ssd_I2C_start_sequence_sw(u8g);
185
-      break;
186
-
187
-    case U8G_COM_MSG_STOP: break;
188
-
189
-    case U8G_COM_MSG_RESET: break;
190
-
191
-    case U8G_COM_MSG_CHIP_SELECT:
192
-      u8g->pin_list[U8G_PI_A0_STATE] = 0;
193
-      u8g->pin_list[U8G_PI_SET_A0] = 1;   /* force a0 to set again, also forces start condition */
194
-      if (arg_val == 0) {
195
-        /* disable chip, send stop condition */
196
-        u8g_i2c_stop_sw();
197
-      }
198
-      else {
199
-        /* enable, do nothing: any byte writing will trigger the i2c start */
200
-      }
201
-      break;
202
-
203
-    case U8G_COM_MSG_WRITE_BYTE:
204
-      //u8g->pin_list[U8G_PI_SET_A0] = 1;
205
-      //if (u8g_com_arduino_ssd_start_sequence(u8g) == 0)
206
-      //  return u8g_i2c_stop(), 0;
207
-      if (u8g_i2c_send_byte_sw(arg_val) == 0)
208
-        return u8g_i2c_stop_sw(), 0;
209
-        // u8g_i2c_stop();
210
-      break;
211
-
212
-    case U8G_COM_MSG_WRITE_SEQ: {
213
-      //u8g->pin_list[U8G_PI_SET_A0] = 1;
214
-      if (u8g_com_ssd_I2C_start_sequence_sw(u8g) == 0)
215
-        return u8g_i2c_stop_sw(), 0;
216
-
217
-        uint8_t *ptr = (uint8_t *)arg_ptr;
218
-        while (arg_val > 0) {
219
-          if (u8g_i2c_send_byte_sw(*ptr++) == 0)
220
-            return u8g_i2c_stop_sw(), 0;
221
-          arg_val--;
222
-        }
223
-      }
224
-      // u8g_i2c_stop();
225
-      break;
226
-
227
-    case U8G_COM_MSG_WRITE_SEQ_P: {
228
-      //u8g->pin_list[U8G_PI_SET_A0] = 1;
229
-      if (u8g_com_ssd_I2C_start_sequence_sw(u8g) == 0)
230
-        return u8g_i2c_stop_sw(), 0;
231
-
232
-        uint8_t *ptr = (uint8_t *)arg_ptr;
233
-        while (arg_val > 0) {
234
-          if (u8g_i2c_send_byte_sw(u8g_pgm_read(ptr)) == 0) return 0;
235
-          ptr++;
236
-          arg_val--;
237
-        }
238
-      }
239
-      // u8g_i2c_stop();
240
-      break;
241
-
242
-    case U8G_COM_MSG_ADDRESS:                     /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
243
-      u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
244
-      u8g->pin_list[U8G_PI_SET_A0] = 1;   /* force a0 to set again */
245
-      u8g_i2c_start_sw(0); // send slave address and write bit
246
-      u8g_i2c_send_byte_sw(arg_val ? 0x40 : 0x80);  // Write to ? Graphics DRAM mode : Command mode
247
-      break;
248
-  }
249
-  return 1;
250
-}
251
-
252
-#endif // HAS_GRAPHICAL_LCD
253
-
254
-#endif // TARGET_LPC1768

+ 5
- 5
Marlin/src/feature/digipot/digipot_mcp4451.cpp Wyświetl plik

@@ -47,7 +47,7 @@ static byte current_to_wiper(const float current) {
47 47
   return byte(CEIL(float((DIGIPOT_I2C_FACTOR * current))));
48 48
 }
49 49
 
50
-static void i2c_send(const byte addr, const byte a, const byte b) {
50
+static void digipot_i2c_send(const byte addr, const byte a, const byte b) {
51 51
   #if MB(MKS_SBASE)
52 52
     digipot_mcp4451_start(addr);
53 53
     digipot_mcp4451_send_byte(a);
@@ -67,17 +67,17 @@ void digipot_i2c_set_current(const uint8_t channel, const float current) {
67 67
   const byte addr = channel < 4 ? DIGIPOT_I2C_ADDRESS_A : DIGIPOT_I2C_ADDRESS_B; // channel 0-3 vs 4-7
68 68
 
69 69
   // Initial setup
70
-  i2c_send(addr, 0x40, 0xFF);
71
-  i2c_send(addr, 0xA0, 0xFF);
70
+  digipot_i2c_send(addr, 0x40, 0xFF);
71
+  digipot_i2c_send(addr, 0xA0, 0xFF);
72 72
 
73 73
   // Set actual wiper value
74 74
   byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 };
75
-  i2c_send(addr, addresses[channel & 0x3], current_to_wiper(_MIN(float(_MAX(current, 0)), DIGIPOT_I2C_MAX_CURRENT)));
75
+  digipot_i2c_send(addr, addresses[channel & 0x3], current_to_wiper(_MIN(float(_MAX(current, 0)), DIGIPOT_I2C_MAX_CURRENT)));
76 76
 }
77 77
 
78 78
 void digipot_i2c_init() {
79 79
   #if MB(MKS_SBASE)
80
-    digipot_mcp4451_init();
80
+    configure_i2c();
81 81
   #else
82 82
     Wire.begin();
83 83
   #endif

Ładowanie…
Anuluj
Zapisz