From 451062553e5f3a8f35647b8a445f39b6bbb80f92 Mon Sep 17 00:00:00 2001 From: 0r31 <37673727+0r31@users.noreply.github.com> Date: Mon, 20 Jan 2020 06:59:06 +0100 Subject: [PATCH] LPC1768: Allow I2C master channel override (#16584) --- .../include/digipot_mcp4451_I2C_routines.c | 47 +--- .../include/digipot_mcp4451_I2C_routines.h | 19 +- Marlin/src/HAL/HAL_LPC1768/include/i2c_util.c | 71 +++++ Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h | 46 ++++ .../HAL/HAL_LPC1768/u8g/LCD_I2C_routines.cpp | 67 +---- .../HAL/HAL_LPC1768/u8g/LCD_I2C_routines.h | 4 +- .../u8g/u8g_com_HAL_LPC1768_ssd_hw_i2c.cpp | 2 - ...LPC1768_ssd_sw_i2c.cpp under construction | 254 ------------------ .../src/feature/digipot/digipot_mcp4451.cpp | 10 +- 9 files changed, 142 insertions(+), 378 deletions(-) create mode 100644 Marlin/src/HAL/HAL_LPC1768/include/i2c_util.c create mode 100644 Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h delete mode 100644 Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_sw_i2c.cpp under construction diff --git a/Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.c b/Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.c index 3ad1aff01..8dc8c61b4 100644 --- a/Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.c +++ b/Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.c @@ -20,8 +20,10 @@ * */ -// adapted from I2C/master/master.c example -// https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html +/** + * digipot_mcp4451_I2C_routines.c + * Adapted from https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html + */ #ifdef TARGET_LPC1768 @@ -29,12 +31,13 @@ #if MB(MKS_SBASE) -#include "digipot_mcp4451_I2C_routines.h" - #ifdef __cplusplus extern "C" { #endif +#include "digipot_mcp4451_I2C_routines.h" +#include "i2c_util.h" + // These two routines are exact copies of the lpc17xx_i2c.c routines. Couldn't link to // to the lpc17xx_i2c.c routines so had to copy them into this file & rename them. @@ -60,7 +63,6 @@ static void _I2C_Stop(LPC_I2C_TypeDef *I2Cx) { I2Cx->I2CONCLR = I2C_I2CONCLR_SIC; } -PINSEL_CFG_Type PinCfg; I2C_M_SETUP_Type transferMCfg; #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 return 1; } -void digipot_mcp4451_init() { - /** - * Init I2C pin connect - */ - PinCfg.OpenDrain = 0; - PinCfg.Pinmode = 0; - #if USEDI2CDEV_M == 0 - PinCfg.Funcnum = 1; - PinCfg.Pinnum = 27; - PinCfg.Portnum = 0; - PINSEL_ConfigPin(&PinCfg); // SDA0 / D57 AUX-1 - PinCfg.Pinnum = 28; - PINSEL_ConfigPin(&PinCfg); // SCL0 / D58 AUX-1 - #elif USEDI2CDEV_M == 1 - PinCfg.Funcnum = 3; - PinCfg.Pinnum = 0; - PinCfg.Portnum = 0; - PINSEL_ConfigPin(&PinCfg); // SDA1 / D20 SCA - PinCfg.Pinnum = 1; - PINSEL_ConfigPin(&PinCfg); // SCL1 / D21 SCL - #elif USEDI2CDEV_M == 2 - PinCfg.Funcnum = 2; - PinCfg.Pinnum = 10; - PinCfg.Portnum = 0; - PINSEL_ConfigPin(&PinCfg); // SDA2 / D38 X_ENABLE_PIN - PinCfg.Pinnum = 11; - PINSEL_ConfigPin(&PinCfg); // SCL2 / D55 X_DIR_PIN - #endif - // Initialize I2C peripheral - I2C_Init(I2CDEV_M, 400000); // hardwired to 400KHz bit rate, 100KHz is the other option - - // Enable Master I2C operation - I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE); -} - uint8_t digipot_mcp4451_send_byte(uint8_t data) { LPC_I2C1->I2DAT = data & I2C_I2DAT_BITMASK; // transmit data LPC_I2C1->I2CONSET = I2C_I2CONSET_AA; diff --git a/Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.h b/Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.h index d7e4b0dc7..6d6fafa8b 100644 --- a/Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.h +++ b/Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.h @@ -21,20 +21,10 @@ */ #pragma once -// adapted from I2C/master/master.c example -// https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html - -#define USEDI2CDEV_M 1 // use I2C1 controller - -#if USEDI2CDEV_M == 0 - #define I2CDEV_M LPC_I2C0 -#elif USEDI2CDEV_M == 1 - #define I2CDEV_M LPC_I2C1 -#elif USEDI2CDEV_M == 2 - #define I2CDEV_M LPC_I2C2 -#else - #error "Master I2C device not defined!" -#endif +/** + * digipot_mcp4451_I2C_routines.h + * Adapted from https://www-users.cs.york.ac.uk/~pcc/MCP/HAPR-Course-web/CMSIS/examples/html/master_8c_source.html + */ #ifdef __cplusplus extern "C" { @@ -45,7 +35,6 @@ #include uint8_t digipot_mcp4451_start(uint8_t sla); -void digipot_mcp4451_init(); uint8_t digipot_mcp4451_send_byte(uint8_t data); #ifdef __cplusplus diff --git a/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.c b/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.c new file mode 100644 index 000000000..a451f4c43 --- /dev/null +++ b/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.c @@ -0,0 +1,71 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/** + * HAL_LPC1768/include/i2c_util.c + */ + +#ifdef TARGET_LPC1768 + +#include "../../../inc/MarlinConfigPre.h" +#include "i2c_util.h" + +#define U8G_I2C_OPT_FAST 16 // from u8g.h + +#ifdef __cplusplus + extern "C" { +#endif + +void configure_i2c(const uint8_t clock_option) { + /** + * Init I2C pin connect + */ + PINSEL_CFG_Type PinCfg; + PinCfg.OpenDrain = 0; + PinCfg.Pinmode = 0; + PinCfg.Portnum = 0; + #if USEDI2CDEV_M == 0 + PinCfg.Funcnum = 1; + PinCfg.Pinnum = 27; // SDA0 / D57 AUX-1 ... SCL0 / D58 AUX-1 + #elif USEDI2CDEV_M == 1 + PinCfg.Funcnum = 3; + PinCfg.Pinnum = 0; // SDA1 / D20 SCA ... SCL1 / D21 SCL + #elif USEDI2CDEV_M == 2 + PinCfg.Funcnum = 2; + PinCfg.Pinnum = 10; // SDA2 / D38 X_ENABLE_PIN ... SCL2 / D55 X_DIR_PIN + #endif + PINSEL_ConfigPin(&PinCfg); + PinCfg.Pinnum += 1; + PINSEL_ConfigPin(&PinCfg); + + // Initialize I2C peripheral + I2C_Init(I2CDEV_M, (clock_option & U8G_I2C_OPT_FAST) ? 400000: 100000); // LCD data rates + + // Enable Master I2C operation + I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE); +} + +#ifdef __cplusplus + } +#endif + +#endif // TARGET_LPC1768 diff --git a/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h b/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h new file mode 100644 index 000000000..c2d6c2b81 --- /dev/null +++ b/Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h @@ -0,0 +1,46 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +#pragma once + +/** + * HAL_LPC1768/include/i2c_util.h + */ + +#ifndef USEDI2CDEV_M + #define USEDI2CDEV_M 1 // By default use I2C1 controller +#endif + +#if USEDI2CDEV_M == 0 + #define I2CDEV_M LPC_I2C0 +#elif USEDI2CDEV_M == 1 + #define I2CDEV_M LPC_I2C1 +#elif USEDI2CDEV_M == 2 + #define I2CDEV_M LPC_I2C2 +#else + #error "Master I2C device not defined!" +#endif + +#include +#include +#include + +void configure_i2c(const uint8_t clock_option); diff --git a/Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.cpp b/Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.cpp index 9a95b1b66..8afb60752 100644 --- a/Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.cpp @@ -31,19 +31,15 @@ extern int millis(); extern "C" { #endif -#include -#include -#include - +#include "../include/i2c_util.h" #include "../../../core/millis_t.h" - ////////////////////////////////////////////////////////////////////////////////////// // These two routines are exact copies of the lpc17xx_i2c.c routines. Couldn't link to // to the lpc17xx_i2c.c routines so had to copy them into this file & rename them. -static uint32_t _I2C_Start (LPC_I2C_TypeDef *I2Cx) { +static uint32_t _I2C_Start(LPC_I2C_TypeDef *I2Cx) { // Reset STA, STO, SI I2Cx->I2CONCLR = I2C_I2CONCLR_SIC|I2C_I2CONCLR_STOC|I2C_I2CONCLR_STAC; @@ -67,30 +63,16 @@ static void _I2C_Stop (LPC_I2C_TypeDef *I2Cx) { ////////////////////////////////////////////////////////////////////////////////////// -#define U8G_I2C_OPT_FAST 16 // from u8g.h - -#define USEDI2CDEV_M 1 - #define I2CDEV_S_ADDR 0x78 // from SSD1306 //actual address is 0x3C - shift left 1 with LSB set to 0 to indicate write #define BUFFER_SIZE 0x1 // only do single byte transfers with LCDs -#if (USEDI2CDEV_M == 0) - #define I2CDEV_M LPC_I2C0 -#elif (USEDI2CDEV_M == 1) - #define I2CDEV_M LPC_I2C1 -#elif (USEDI2CDEV_M == 2) - #define I2CDEV_M LPC_I2C2 -#else - #error "Master I2C device not defined!" -#endif - -PINSEL_CFG_Type PinCfg; I2C_M_SETUP_Type transferMCfg; #define I2C_status (LPC_I2C1->I2STAT & I2C_STAT_CODE_BITMASK) -uint8_t u8g_i2c_start(uint8_t sla) { // send slave address and write bit +// Send slave address and write bit +uint8_t u8g_i2c_start(const uint8_t sla) { // Sometimes TX data ACK or NAK status is returned. That mean the start state didn't // happen which means only the value of the slave address was send. Keep looping until // 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 return 1; } -void u8g_i2c_init(uint8_t clock_option) { - - /** - * Init I2C pin connect - */ - PinCfg.OpenDrain = 0; - PinCfg.Pinmode = 0; - #if ((USEDI2CDEV_M == 0)) - PinCfg.Funcnum = 1; - PinCfg.Pinnum = 27; - PinCfg.Portnum = 0; - PINSEL_ConfigPin(&PinCfg); // SDA0 / D57 AUX-1 - PinCfg.Pinnum = 28; - PINSEL_ConfigPin(&PinCfg); // SCL0 / D58 AUX-1 - #endif - #if ((USEDI2CDEV_M == 1)) - PinCfg.Funcnum = 3; - PinCfg.Pinnum = 0; - PinCfg.Portnum = 0; - PINSEL_ConfigPin(&PinCfg); // SDA1 / D20 SCA - PinCfg.Pinnum = 1; - PINSEL_ConfigPin(&PinCfg); // SCL1 / D21 SCL - #endif - #if ((USEDI2CDEV_M == 2)) - PinCfg.Funcnum = 2; - PinCfg.Pinnum = 10; - PinCfg.Portnum = 0; - PINSEL_ConfigPin(&PinCfg); // SDA2 / D38 X_ENABLE_PIN - PinCfg.Pinnum = 11; - PINSEL_ConfigPin(&PinCfg); // SCL2 / D55 X_DIR_PIN - #endif - // Initialize I2C peripheral - I2C_Init(I2CDEV_M, (clock_option & U8G_I2C_OPT_FAST) ? 400000: 100000); // LCD data rates - - // Enable Master I2C operation - I2C_Cmd(I2CDEV_M, I2C_MASTER_MODE, ENABLE); - - u8g_i2c_start(0); // send slave address and write bit +void u8g_i2c_init(const uint8_t clock_option) { + configure_i2c(clock_option); + u8g_i2c_start(0); // Send slave address and write bit } uint8_t u8g_i2c_send_byte(uint8_t data) { diff --git a/Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.h b/Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.h index 9dd594cd9..85ca06744 100644 --- a/Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.h +++ b/Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.h @@ -21,8 +21,8 @@ */ #pragma once -void u8g_i2c_init(uint8_t options); -uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos); +void u8g_i2c_init(const uint8_t clock_options); +//uint8_t u8g_i2c_wait(uint8_t mask, uint8_t pos); uint8_t u8g_i2c_start(uint8_t sla); uint8_t u8g_i2c_send_byte(uint8_t data); void u8g_i2c_stop(); diff --git a/Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_hw_i2c.cpp b/Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_hw_i2c.cpp index b624b24d3..d19e66d41 100644 --- a/Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_hw_i2c.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_hw_i2c.cpp @@ -86,8 +86,6 @@ #define I2C_CMD_MODE 0x000 #define I2C_DATA_MODE 0x040 -//#define U8G_I2C_OPT_FAST 16 - uint8_t u8g_com_ssd_I2C_start_sequence(u8g_t *u8g) { /* are we requested to set the a0 state? */ if (u8g->pin_list[U8G_PI_SET_A0] == 0) return 1; diff --git a/Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_sw_i2c.cpp under construction b/Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_sw_i2c.cpp under construction deleted file mode 100644 index 0222c6228..000000000 --- a/Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_ssd_sw_i2c.cpp under construction +++ /dev/null @@ -1,254 +0,0 @@ -/** - * Marlin 3D Printer Firmware - * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] - * - * Based on Sprinter and grbl. - * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -/** - * Based on U8G2 code - u8x8_byte.c - * - * Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/) - * - * Copyright (c) 2016, olikraus@gmail.com - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, this list - * of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright notice, this - * list of conditions and the following disclaimer in the documentation and/or other - * materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * Software i2c, - * ignores ACK response (which is anyway not provided by some displays) - * also does not allow reading from the device - */ - -#ifdef TARGET_LPC1768 - -#include "../../inc/MarlinConfigPre.h" - -#if HAS_GRAPHICAL_LCD - -#include - -//void pinMode(int16_t pin, uint8_t mode); -//void digitalWrite(int16_t pin, uint8_t pin_status); - - -#define I2C_SLA (0x3C*2) -//#define I2C_CMD_MODE 0x080 -#define I2C_CMD_MODE 0x000 -#define I2C_DATA_MODE 0x040 - -//static uint8_t I2C_speed; // 3 - 400KHz, 13 - 100KHz -//#define SPEED_400KHz 3 -//#define SPEED_100KHz 13 - -// #define U8G_I2C_OPT_FAST 16 - -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; - -#define SPI_SPEED 2 //20: 200KHz 5:750KHz 2:3-4MHz - -uint8_t u8g_i2c_send_byte_sw(uint8_t data) { - for (uint8_t i = 0; i < 9; i++) { // 1 extra bit for the ack/nak - - if (val & 0x80) - for (uint8_t j = 0; j < SPI_SPEED; j++) { - LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C); - LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C); - LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C); - } - else - for (uint8_t j = 0; j < SPI_SPEED; j++) { - LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C); - LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C); - LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C); - } - - for (uint8_t j = 0; j < SPI_SPEED; j++) { - LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C); - LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C); - LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C); - LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C); - LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C); - } - - for (uint8_t j = 0; j < SPI_SPEED; j++) { - LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C); - LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C); - } - val <<= 1; - } - return 1; -} - - -uint8_t u8g_i2c_start_sw(uint8_t sla) { // assert start condition and then send slave address with write bit - /* send the start condition, both lines go from 1 to 0 */ - - LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C); - LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C); - DELAY_US(2); - LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C); - DELAY_US(2); - LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOSET = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C); - DELAY_US(2); - LPC_GPIO(SDA_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SDA_pin_HAL_LPC1768_sw_I2C); - DELAY_US(2); - LPC_GPIO(SCL_port_HAL_LPC1768_sw_I2C)->FIOCLR = LPC_PIN(SCL_pin_HAL_LPC1768_sw_I2C); - - u8g_i2c_send_byte_sw(I2C_SLA); // send slave address with write bit -} - - -void u8g_i2c_stop_sw() { } - -void u8g_i2c_init_sw(uint8_t clock_option) { u8g_i2c_start(0); } // send slave address and write bit - -uint8_t u8g_com_ssd_I2C_start_sequence_sw(u8g_t *u8g) { - /* are we requested to set the a0 state? */ - if (u8g->pin_list[U8G_PI_SET_A0] == 0) return 1; - - /* setup bus, might be a repeated start */ - if (u8g_i2c_start(I2C_SLA) == 0) return 0; - if (u8g->pin_list[U8G_PI_A0_STATE] == 0) { - if (u8g_i2c_send_byte(I2C_CMD_MODE) == 0) return 0; - } - else if (u8g_i2c_send_byte(I2C_DATA_MODE) == 0) return 0; - - u8g->pin_list[U8G_PI_SET_A0] = 0; - - return 1; -} - -uint8_t u8g_com_HAL_LPC1768_ssd_sw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { - switch (msg) { - case U8G_COM_MSG_INIT: - - #define LPC1768_PIN_PORT(pin) ((uint8_t)((pin >> 5) & 0b111)) - #define LPC1768_PIN_PIN(pin) ((uint8_t)(pin & 0b11111)) - SCL_pin_HAL_LPC1768_sw_I2C = LPC1768_PIN_PIN(u8g->pin_list[U8G_PI_SCL]); - SCL_port_HAL_LPC1768_sw_I2C = LPC1768_PIN_PORT(u8g->pin_list[U8G_PI_SCL]); - SDA_pin_HAL_LPC1768_sw_I2C = LPC1768_PIN_PIN(u8g->pin_list[U8G_PI_SDA]); - SDA_port_HAL_LPC1768_sw_I2C = LPC1768_PIN_PORT(u8g->pin_list[U8G_PI_SDA]); - // As defined by Arduino INPUT(0x0), OUTPUT(0x1), INPUT_PULLUP(0x2) - #define OUTPUT 0x1 - u8g_SetPIOutput(u8g, U8G_PI_SCL); - u8g_SetPIOutput(u8g, U8G_PI_SDA); - if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_CS]) u8g_SetPIOutput(u8g, U8G_PI_CS); - if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_A0]) u8g_SetPIOutput(u8g, U8G_PI_A0); - if (U8G_PIN_NONE != u8g->pin_list[U8G_PI_RESET]) u8g_SetPIOutput(u8g, U8G_PI_RESET); - - //u8g_com_arduino_digital_write(u8g, U8G_PI_SCL, HIGH); - //u8g_com_arduino_digital_write(u8g, U8G_PI_SDA, HIGH); - //u8g->pin_list[U8G_PI_A0_STATE] = 0; /* initial RS state: unknown mode */ - - u8g_i2c_init_sw(u8g->pin_list[U8G_PI_I2C_OPTION]); - u8g_com_ssd_I2C_start_sequence_sw(u8g); - break; - - case U8G_COM_MSG_STOP: break; - - case U8G_COM_MSG_RESET: break; - - case U8G_COM_MSG_CHIP_SELECT: - u8g->pin_list[U8G_PI_A0_STATE] = 0; - u8g->pin_list[U8G_PI_SET_A0] = 1; /* force a0 to set again, also forces start condition */ - if (arg_val == 0) { - /* disable chip, send stop condition */ - u8g_i2c_stop_sw(); - } - else { - /* enable, do nothing: any byte writing will trigger the i2c start */ - } - break; - - case U8G_COM_MSG_WRITE_BYTE: - //u8g->pin_list[U8G_PI_SET_A0] = 1; - //if (u8g_com_arduino_ssd_start_sequence(u8g) == 0) - // return u8g_i2c_stop(), 0; - if (u8g_i2c_send_byte_sw(arg_val) == 0) - return u8g_i2c_stop_sw(), 0; - // u8g_i2c_stop(); - break; - - case U8G_COM_MSG_WRITE_SEQ: { - //u8g->pin_list[U8G_PI_SET_A0] = 1; - if (u8g_com_ssd_I2C_start_sequence_sw(u8g) == 0) - return u8g_i2c_stop_sw(), 0; - - uint8_t *ptr = (uint8_t *)arg_ptr; - while (arg_val > 0) { - if (u8g_i2c_send_byte_sw(*ptr++) == 0) - return u8g_i2c_stop_sw(), 0; - arg_val--; - } - } - // u8g_i2c_stop(); - break; - - case U8G_COM_MSG_WRITE_SEQ_P: { - //u8g->pin_list[U8G_PI_SET_A0] = 1; - if (u8g_com_ssd_I2C_start_sequence_sw(u8g) == 0) - return u8g_i2c_stop_sw(), 0; - - uint8_t *ptr = (uint8_t *)arg_ptr; - while (arg_val > 0) { - if (u8g_i2c_send_byte_sw(u8g_pgm_read(ptr)) == 0) return 0; - ptr++; - arg_val--; - } - } - // u8g_i2c_stop(); - break; - - case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ - u8g->pin_list[U8G_PI_A0_STATE] = arg_val; - u8g->pin_list[U8G_PI_SET_A0] = 1; /* force a0 to set again */ - u8g_i2c_start_sw(0); // send slave address and write bit - u8g_i2c_send_byte_sw(arg_val ? 0x40 : 0x80); // Write to ? Graphics DRAM mode : Command mode - break; - } - return 1; -} - -#endif // HAS_GRAPHICAL_LCD - -#endif // TARGET_LPC1768 diff --git a/Marlin/src/feature/digipot/digipot_mcp4451.cpp b/Marlin/src/feature/digipot/digipot_mcp4451.cpp index b04406f4b..13022abad 100644 --- a/Marlin/src/feature/digipot/digipot_mcp4451.cpp +++ b/Marlin/src/feature/digipot/digipot_mcp4451.cpp @@ -47,7 +47,7 @@ static byte current_to_wiper(const float current) { return byte(CEIL(float((DIGIPOT_I2C_FACTOR * current)))); } -static void i2c_send(const byte addr, const byte a, const byte b) { +static void digipot_i2c_send(const byte addr, const byte a, const byte b) { #if MB(MKS_SBASE) digipot_mcp4451_start(addr); digipot_mcp4451_send_byte(a); @@ -67,17 +67,17 @@ void digipot_i2c_set_current(const uint8_t channel, const float current) { const byte addr = channel < 4 ? DIGIPOT_I2C_ADDRESS_A : DIGIPOT_I2C_ADDRESS_B; // channel 0-3 vs 4-7 // Initial setup - i2c_send(addr, 0x40, 0xFF); - i2c_send(addr, 0xA0, 0xFF); + digipot_i2c_send(addr, 0x40, 0xFF); + digipot_i2c_send(addr, 0xA0, 0xFF); // Set actual wiper value byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 }; - i2c_send(addr, addresses[channel & 0x3], current_to_wiper(_MIN(float(_MAX(current, 0)), DIGIPOT_I2C_MAX_CURRENT))); + digipot_i2c_send(addr, addresses[channel & 0x3], current_to_wiper(_MIN(float(_MAX(current, 0)), DIGIPOT_I2C_MAX_CURRENT))); } void digipot_i2c_init() { #if MB(MKS_SBASE) - digipot_mcp4451_init(); + configure_i2c(); #else Wire.begin(); #endif