Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hardware TWI for the PAT9125 (optical) filament sensor #2814

Merged
merged 15 commits into from
Feb 2, 2021
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
15 changes: 4 additions & 11 deletions Firmware/Marlin_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,13 @@
#include "la10compat.h"
#endif

#ifdef SWSPI
#include "swspi.h"
#endif //SWSPI

#include "spi.h"

#ifdef SWI2C
#include "swi2c.h"
#endif //SWI2C

#ifdef FILAMENT_SENSOR
#include "fsensor.h"
#ifdef IR_SENSOR
#include "pat9125.h" // for pat9125_probe
#endif
#endif //FILAMENT_SENSOR

#ifdef TMC2130
Expand Down Expand Up @@ -921,9 +916,7 @@ static void check_if_fw_is_on_right_printer(){
#ifdef FILAMENT_SENSOR
if((PRINTER_TYPE == PRINTER_MK3) || (PRINTER_TYPE == PRINTER_MK3S)){
#ifdef IR_SENSOR
swi2c_init();
const uint8_t pat9125_detected = swi2c_readByte_A8(PAT9125_I2C_ADDR,0x00,NULL);
if (pat9125_detected){
if (pat9125_probe()){
lcd_show_fullscreen_message_and_wait_P(_i("MK3S firmware detected on MK3 printer"));}////c=20 r=3
#endif //IR_SENSOR

Expand Down
6 changes: 1 addition & 5 deletions Firmware/Sd2PinMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ struct pin_map_t {
|| defined(__AVR_ATmega2560__)
// Mega

// Two Wire (aka I2C) ports
uint8_t const SDA_PIN = 20; // D1
uint8_t const SCL_PIN = 21; // D0

#undef MOSI_PIN
#undef MISO_PIN
// SPI port
Expand Down Expand Up @@ -365,4 +361,4 @@ static inline __attribute__((always_inline))
#endif // Sd2PinMap_h


#endif
#endif
9 changes: 7 additions & 2 deletions Firmware/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@
#define ADC_CALLBACK adc_ready //callback function ()

//SWI2C configuration
#define SWI2C
//#define SWI2C_SDA 20 //SDA on P3
//#define SWI2C_SCL 21 //SCL on P3
#define SWI2C_A8
#define SWI2C_DEL 20 //2us clock delay
#define SWI2C_TMO 2048 //2048 cycles timeout

//PAT9125 configuration
#define PAT9125_SWI2C
//#define PAT9125_SWSPI // software SPI mode (incomplete)
#ifdef SWI2C_SCL
#define PAT9125_SWI2C // software I2C mode
#else
#define PAT9125_I2C // hardware I2C mode
#endif

#define PAT9125_I2C_ADDR 0x75 //ID=LO
//#define PAT9125_I2C_ADDR 0x79 //ID=HI
//#define PAT9125_I2C_ADDR 0x73 //ID=NC
Expand Down
1 change: 1 addition & 0 deletions Firmware/fastio.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <avr/io.h>
#include "macros.h"


/*
magic I/O routines
now you can simply SET_OUTPUT(STEP); WRITE(STEP, 1); WRITE(STEP, 0);
Expand Down
83 changes: 55 additions & 28 deletions Firmware/pat9125.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
#define PAT9125_BANK_SELECTION 0x7f


#ifdef PAT9125_SWSPI
#if defined(PAT9125_SWSPI)
#include "swspi.h"
#endif //PAT9125_SWSPI
#ifdef PAT9125_SWI2C
#elif defined(PAT9125_SWI2C)
#include "swi2c.h"
#endif //PAT9125_SWI2C
#elif defined(PAT9125_I2C)
#include "twi.h"
#else
#error unknown PAT9125 communication method
#endif


uint8_t pat9125_PID1 = 0;
Expand Down Expand Up @@ -103,14 +106,31 @@ extern FILE _uartout;
#define uartout (&_uartout)


uint8_t pat9125_probe()
{
#if defined(PAT9125_SWSPI)
swspi_init();
#error not implemented
#elif defined(PAT9125_SWI2C)
swi2c_init();
return swi2c_readByte_A8(PAT9125_I2C_ADDR,0x00,NULL);
#elif defined(PAT9125_I2C)
twi_init();
#ifdef IR_SENSOR
// NOTE: this is called from the MK3S variant, so it should be kept minimal
uint8_t data;
return (twi_r8(PAT9125_I2C_ADDR,PAT9125_PID1,&data) == 0);
#else
return (pat9125_rd_reg(PAT9125_PID1) != 0);
#endif
#endif
}

uint8_t pat9125_init(void)
{
#ifdef PAT9125_SWSPI
swspi_init();
#endif //PAT9125_SWSPI
#ifdef PAT9125_SWI2C
swi2c_init();
#endif //PAT9125_SWI2C
if (!pat9125_probe())
return 0;

// Verify that the sensor responds with its correct product ID.
pat9125_PID1 = pat9125_rd_reg(PAT9125_PID1);
pat9125_PID2 = pat9125_rd_reg(PAT9125_PID2);
Expand Down Expand Up @@ -234,39 +254,46 @@ uint8_t pat9125_update_bs(void)
uint8_t pat9125_rd_reg(uint8_t addr)
{
uint8_t data = 0;
#ifdef PAT9125_SWSPI
#if defined(PAT9125_SWSPI)
swspi_start();
swspi_tx(addr & 0x7f);
data = swspi_rx();
swspi_stop();
#endif //PAT9125_SWSPI
#ifdef PAT9125_SWI2C
#elif defined(PAT9125_SWI2C)
if (!swi2c_readByte_A8(PAT9125_I2C_ADDR, addr, &data)) //NO ACK error
{
pat9125_PID1 = 0xff;
pat9125_PID2 = 0xff;
return 0;
}
#endif //PAT9125_SWI2C
goto error;
#elif defined(PAT9125_I2C)
if (twi_r8(PAT9125_I2C_ADDR,addr,&data))
goto error;
#endif
return data;

error:
pat9125_PID1 = 0xff;
pat9125_PID2 = 0xff;
return 0;
}

void pat9125_wr_reg(uint8_t addr, uint8_t data)
{
#ifdef PAT9125_SWSPI
#if defined(PAT9125_SWSPI)
swspi_start();
swspi_tx(addr | 0x80);
swspi_tx(data);
swspi_stop();
#endif //PAT9125_SWSPI
#ifdef PAT9125_SWI2C
#elif defined(PAT9125_SWI2C)
if (!swi2c_writeByte_A8(PAT9125_I2C_ADDR, addr, &data)) //NO ACK error
{
pat9125_PID1 = 0xff;
pat9125_PID2 = 0xff;
return;
}
#endif //PAT9125_SWI2C
goto error;
#elif defined(PAT9125_I2C)
if (twi_w8(PAT9125_I2C_ADDR,addr,data))
goto error;
#endif
return;

error:
pat9125_PID1 = 0xff;
pat9125_PID2 = 0xff;
return;
}

uint8_t pat9125_wr_reg_verify(uint8_t addr, uint8_t data)
Expand Down
1 change: 1 addition & 0 deletions Firmware/pat9125.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern int16_t pat9125_y;
extern uint8_t pat9125_b;
extern uint8_t pat9125_s;

extern uint8_t pat9125_probe(void); // Return non-zero if PAT9125 can be trivially detected
extern uint8_t pat9125_init(void);
extern uint8_t pat9125_update(void); // update all sensor data
extern uint8_t pat9125_update_y(void); // update _y only
Expand Down
5 changes: 5 additions & 0 deletions Firmware/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
#error Unknown MOTHERBOARD value in configuration.h
#endif

#if !defined(SDA_PIN) && (defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__))
#define SDA_PIN 20
#define SCL_PIN 21
#endif

//List of pins which to ignore when asked to change by gcode, 0 and 1 are RX and TX, do not mess with those!
#define _E0_PINS E0_STEP_PIN, E0_DIR_PIN, E0_ENABLE_PIN, HEATER_0_PIN,
#if EXTRUDERS > 1
Expand Down
6 changes: 0 additions & 6 deletions Firmware/pins_Einsy_1_0.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
#define W25X20CL // external 256kB flash
#define BOOTAPP // bootloader support


#define SWI2C_SDA 20 //SDA on P3
#define SWI2C_SCL 21 //SCL on P3



#define X_TMC2130_CS 41
#define X_TMC2130_DIAG 64 // !!! changed from 40 (EINY03)
#define X_STEP_PIN 37
Expand Down
3 changes: 0 additions & 3 deletions Firmware/pins_Rambo_1_3.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

#define PINDA_THERMISTOR

#define SWI2C_SDA 20 //SDA on P3
#define SWI2C_SCL 21 //SCL on P3

#ifdef MICROMETER_LOGGING
#define D_DATACLOCK 24 //Y_MAX (green)
#define D_DATA 30 //X_MAX (blue)
Expand Down
3 changes: 3 additions & 0 deletions Firmware/swi2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "pins.h"
#include "fastio.h"

#ifdef SWI2C_SCL

#define SWI2C_RMSK 0x01 //read mask (bit0 = 1)
#define SWI2C_WMSK 0x00 //write mask (bit0 = 0)
Expand Down Expand Up @@ -187,3 +188,5 @@ uint8_t swi2c_writeByte_A16(uint8_t dev_addr, unsigned short addr, uint8_t* pbyt
}

#endif //SWI2C_A16

#endif //SWI2C_SCL
2 changes: 0 additions & 2 deletions Firmware/tone04.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#ifdef SYSTEM_TIMER_2

#include <avr/io.h>
#include <avr/interrupt.h>
#include "pins.h"
#include "fastio.h"
#include "macros.h"
Expand Down
Loading