Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 7 additions & 40 deletions Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,24 @@
*
*/

// 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

#include "../../../inc/MarlinConfigPre.h"

#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.

Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 4 additions & 15 deletions Marlin/src/HAL/HAL_LPC1768/include/digipot_mcp4451_I2C_routines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -45,7 +35,6 @@
#include <lpc17xx_libcfg_default.h>

uint8_t digipot_mcp4451_start(uint8_t sla);
void digipot_mcp4451_init();
uint8_t digipot_mcp4451_send_byte(uint8_t data);

#ifdef __cplusplus
Expand Down
71 changes: 71 additions & 0 deletions Marlin/src/HAL/HAL_LPC1768/include/i2c_util.c
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/

/**
* 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
46 changes: 46 additions & 0 deletions Marlin/src/HAL/HAL_LPC1768/include/i2c_util.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/
#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 <lpc17xx_i2c.h>
#include <lpc17xx_pinsel.h>
#include <lpc17xx_libcfg_default.h>

void configure_i2c(const uint8_t clock_option);
67 changes: 7 additions & 60 deletions Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,15 @@ extern int millis();
extern "C" {
#endif

#include <lpc17xx_i2c.h>
#include <lpc17xx_pinsel.h>
#include <lpc17xx_libcfg_default.h>

#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;

Expand All @@ -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.
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/HAL/HAL_LPC1768/u8g/LCD_I2C_routines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading