Browse Source

SWI2C for PAT9125 simplified/optimized

Robert Pelnar 5 years ago
parent
commit
410e911364
8 changed files with 224 additions and 234 deletions
  1. 8 0
      Firmware/config.h
  2. 1 1
      Firmware/pat9125.cpp
  3. 3 6
      Firmware/pins_Einsy_1_0.h
  4. 2 6
      Firmware/pins_Rambo_1_0.h
  5. 2 6
      Firmware/pins_Rambo_1_3.h
  6. 189 0
      Firmware/swi2c.c
  7. 0 209
      Firmware/swi2c.cpp
  8. 19 6
      Firmware/swi2c.h

+ 8 - 0
Firmware/config.h

@@ -8,6 +8,14 @@
 #define ADC_OVRSAMPL      16        //oversampling multiplier
 #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
+
 //SM4 configuration
 #define SM4_DEFDELAY      500       //default step delay [us]
 

+ 1 - 1
Firmware/pat9125.cpp

@@ -80,7 +80,7 @@ int pat9125_init()
 	swspi_init();
 #endif //PAT9125_SWSPI
 #ifdef PAT9125_SWI2C
-	swi2c_init(PAT9125_SWI2C_SDA, PAT9125_SWI2C_SCL, PAT9125_SWI2C_CFG);
+	swi2c_init();
 #endif //PAT9125_SWI2C
 #ifdef PAT9125_HWI2C
 	Wire.begin();

+ 3 - 6
Firmware/pins_Einsy_1_0.h

@@ -17,15 +17,12 @@
 
 #define W25X20CL                 // external 256kB flash
 
-#define SWI2C                    // enable software i2c
-#define SWI2C_A8                 // 8bit address functions
+
+#define SWI2C_SDA      20 //SDA on P3
+#define SWI2C_SCL      21 //SCL on P3
 
 #define PAT9125_SWI2C
-#define PAT9125_SWI2C_SDA      20 //SDA on P3
-#define PAT9125_SWI2C_SCL      21 //SCL on P3
-#define PAT9125_SWI2C_CFG    0xb1 //2us clock delay, 2048 cycles timeout
 
-//#define PAT9125_HWI2C
 
 #define X_TMC2130_CS           41
 #define X_TMC2130_DIAG         64 // !!! changed from 40 (EINY03)

+ 2 - 6
Firmware/pins_Rambo_1_0.h

@@ -11,15 +11,11 @@
 
 #define PINDA_THERMISTOR
 
-#define SWI2C                    // enable software i2c
-#define SWI2C_A8                 // 8bit address functions
+#define SWI2C_SDA      20 //SDA on P3
+#define SWI2C_SCL      84 //PH2 on P3, sensor cable must be rewired
 
 #define PAT9125_SWI2C
-#define PAT9125_SWI2C_SDA      20 //SDA on P3
-#define PAT9125_SWI2C_SCL      84 //PH2 on P3, sensor cable must be rewired
-#define PAT9125_SWI2C_CFG    0xb1 //2us clock delay, 2048 cycles timeout
 
-//#define PAT9125_HWI2C
 
 #define X_STEP_PIN             37
 #define X_DIR_PIN              48

+ 2 - 6
Firmware/pins_Rambo_1_3.h

@@ -11,15 +11,11 @@
 
 #define PINDA_THERMISTOR
 
-#define SWI2C                    // enable software i2c
-#define SWI2C_A8                 // 8bit address functions
+#define SWI2C_SDA      20 //SDA on P3
+#define SWI2C_SCL      21 //SCL on P3
 
 #define PAT9125_SWI2C
-#define PAT9125_SWI2C_SDA      20 //SDA on P3
-#define PAT9125_SWI2C_SCL      21 //SCL on P3
-#define PAT9125_SWI2C_CFG    0xb1 //2us clock delay, 2048 cycles timeout
 
-//#define PAT9125_HWI2C
 
 #define X_STEP_PIN             37
 #define X_DIR_PIN              48

+ 189 - 0
Firmware/swi2c.c

@@ -0,0 +1,189 @@
+//swi2c.c
+#include "swi2c.h"
+#include <avr/io.h>
+#include <avr/delay.h>
+#include <avr/pgmspace.h>
+#include "Configuration_prusa.h"
+#include "pins.h"
+#include "io_atmega2560.h"
+
+
+#define SWI2C_RMSK   0x01 //read mask (bit0 = 1)
+#define SWI2C_WMSK   0x00 //write mask (bit0 = 0)
+#define SWI2C_ASHF   0x01 //address shift (<< 1)
+#define SWI2C_DMSK   0x7f //device address mask
+
+
+void __delay(void)
+{
+	_delay_us(1.5);
+}
+
+void swi2c_init(void)
+{
+	PIN_OUT(SWI2C_SDA);
+	PIN_OUT(SWI2C_SCL);
+	PIN_SET(SWI2C_SDA);
+	PIN_SET(SWI2C_SCL);
+	uint8_t i; for (i = 0; i < 100; i++)
+		__delay();
+}
+
+void swi2c_start(void)
+{
+	PIN_CLR(SWI2C_SDA);
+	__delay();
+	PIN_CLR(SWI2C_SCL);
+	__delay();
+}
+
+void swi2c_stop(void)
+{
+	PIN_SET(SWI2C_SCL);
+	__delay();
+	PIN_SET(SWI2C_SDA);
+	__delay();
+}
+
+void swi2c_ack(void)
+{
+	PIN_CLR(SWI2C_SDA);
+	__delay();
+	PIN_SET(SWI2C_SCL);
+	__delay();
+	PIN_CLR(SWI2C_SCL);
+	__delay();
+}
+
+uint8_t swi2c_wait_ack()
+{
+	PIN_INP(SWI2C_SDA);
+	__delay();
+//	PIN_SET(SWI2C_SDA);
+	__delay();
+	PIN_SET(SWI2C_SCL);
+//	__delay();
+	uint8_t ack = 0;
+	uint16_t ackto = SWI2C_TMO;
+	while (!(ack = (PIN_GET(SWI2C_SDA)?0:1)) && ackto--) __delay();
+	PIN_CLR(SWI2C_SCL);
+	__delay();
+	PIN_OUT(SWI2C_SDA);
+	__delay();
+	PIN_CLR(SWI2C_SDA);
+	__delay();
+	return ack;
+}
+
+uint8_t swi2c_read(void)
+{
+	PIN_SET(SWI2C_SDA);
+	__delay();
+	PIN_INP(SWI2C_SDA);
+	uint8_t data = 0;
+	int8_t bit; for (bit = 7; bit >= 0; bit--)
+	{
+		PIN_SET(SWI2C_SCL);
+		__delay();
+		data |= (PIN_GET(SWI2C_SDA)?1:0) << bit;
+		PIN_CLR(SWI2C_SCL);
+		__delay();
+	}
+	PIN_OUT(SWI2C_SDA);
+	return data;
+}
+
+void swi2c_write(uint8_t data)
+{
+	int8_t bit; for (bit = 7; bit >= 0; bit--)
+	{
+		if (data & (1 << bit)) PIN_SET(SWI2C_SDA);
+		else PIN_CLR(SWI2C_SDA);
+		__delay();
+		PIN_SET(SWI2C_SCL);
+		__delay();
+		PIN_CLR(SWI2C_SCL);
+		__delay();
+	}
+}
+
+uint8_t swi2c_check(uint8_t dev_addr)
+{
+	swi2c_start();
+	swi2c_write((dev_addr & SWI2C_DMSK) << SWI2C_ASHF);
+	if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
+	swi2c_stop();
+	return 1;
+}
+
+#ifdef SWI2C_A8 //8bit address
+
+uint8_t swi2c_readByte_A8(uint8_t dev_addr, uint8_t addr, uint8_t* pbyte)
+{
+	swi2c_start();
+	swi2c_write(SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
+	if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
+	swi2c_write(addr & 0xff);
+	if (!swi2c_wait_ack()) return 0;
+	swi2c_stop();
+	swi2c_start();
+	swi2c_write(SWI2C_RMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
+	if (!swi2c_wait_ack()) return 0;
+	uint8_t byte = swi2c_read();
+	swi2c_stop();
+	if (pbyte) *pbyte = byte;
+	return 1;
+}
+
+uint8_t swi2c_writeByte_A8(uint8_t dev_addr, uint8_t addr, uint8_t* pbyte)
+{
+	swi2c_start();
+	swi2c_write(SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
+	if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
+	swi2c_write(addr & 0xff);
+	if (!swi2c_wait_ack()) return 0;
+	swi2c_write(*pbyte);
+	if (!swi2c_wait_ack()) return 0;
+	swi2c_stop();
+	return 1;
+}
+
+#endif //SWI2C_A8
+
+#ifdef SWI2C_A16 //16bit address
+
+uint8_t swi2c_readByte_A16(uint8_t dev_addr, unsigned short addr, uint8_t* pbyte)
+{
+	swi2c_start();
+	swi2c_write(SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
+	if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
+	swi2c_write(addr >> 8);
+	if (!swi2c_wait_ack()) return 0;
+	swi2c_write(addr & 0xff);
+	if (!swi2c_wait_ack()) return 0;
+	swi2c_stop();
+	swi2c_start();
+	swi2c_write(SWI2C_RMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
+	if (!swi2c_wait_ack()) return 0;
+	uint8_t byte = swi2c_read();
+	swi2c_stop();
+	if (pbyte) *pbyte = byte;
+	return 1;
+}
+
+uint8_t swi2c_writeByte_A16(uint8_t dev_addr, unsigned short addr, uint8_t* pbyte)
+{
+	swi2c_start();
+	swi2c_write(SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
+	if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
+	swi2c_write(addr >> 8);
+	if (!swi2c_wait_ack()) return 0;
+	swi2c_write(addr & 0xff);
+	if (!swi2c_wait_ack()) return 0;
+	swi2c_write(*pbyte);
+	if (!swi2c_wait_ack()) return 0;
+	swi2c_stop();
+	return 1;
+}
+
+#endif //SWI2C_A16

+ 0 - 209
Firmware/swi2c.cpp

@@ -1,209 +0,0 @@
-#include "uni_avr_rpi.h"
-
-#ifdef SWI2C
-#include "swi2c.h"
-
-#ifdef __AVR
-unsigned char swi2c_sda = 20; // SDA pin
-unsigned char swi2c_scl = 21; // SCL pin
-#endif //__AVR
-
-#ifdef __RPI
-unsigned char swi2c_sda = 2; // SDA pin
-unsigned char swi2c_scl = 3; // SCL pin
-#endif //__RPI
-
-unsigned char swi2c_cfg = 0xb1; // config
-//  bit0..3 = clock delay factor = 1 << 1 = 2 [us]
-//  bit4..7 = ack timeout factor = 1 << 11 = 2048 [cycles]
-
-#define SWI2C_SDA    swi2c_sda
-#define SWI2C_SCL    swi2c_scl
-#define SWI2C_RMSK   0x01 //read mask (bit0 = 1)
-#define SWI2C_WMSK   0x00 //write mask (bit0 = 0)
-#define SWI2C_ASHF   0x01 //address shift (<< 1)
-#define SWI2C_DMSK   0x7f //device address mask
-
-
-void swi2c_init(unsigned char sda, unsigned char scl, unsigned char cfg)
-{
-	swi2c_sda = sda;
-	swi2c_scl = scl;
-	swi2c_cfg = cfg;
-	GPIO_OUT(SWI2C_SDA);
-	GPIO_OUT(SWI2C_SCL);
-	GPIO_SET(SWI2C_SDA);
-	GPIO_SET(SWI2C_SCL);
-	DELAY(1000);
-}
-
-void swi2c_start(int delay)
-{
-	GPIO_CLR(SWI2C_SDA);
-	DELAY(delay);
-	GPIO_CLR(SWI2C_SCL);
-	DELAY(delay);
-}
-
-void swi2c_stop(int delay)
-{
-	GPIO_SET(SWI2C_SCL);
-	DELAY(delay);
-	GPIO_SET(SWI2C_SDA);
-	DELAY(delay);
-}
-
-void swi2c_ack(int delay)
-{
-	GPIO_CLR(SWI2C_SDA);
-	DELAY(delay);
-	GPIO_SET(SWI2C_SCL);
-	DELAY(delay);
-	GPIO_CLR(SWI2C_SCL);
-	DELAY(delay);
-}
-
-int swi2c_wait_ack(int delay, int ackto)
-{
-	GPIO_INP(SWI2C_SDA);
-	DELAY(delay);
-//	GPIO_SET(SWI2C_SDA);
-	DELAY(delay);
-	GPIO_SET(SWI2C_SCL);
-//	DELAY(delay);
-	int ack = 0;
-	while (!(ack = !GPIO_GET(SWI2C_SDA)) && ackto--) DELAY(delay);
-	GPIO_CLR(SWI2C_SCL);
-	DELAY(delay);
-	GPIO_OUT(SWI2C_SDA);
-	DELAY(delay);
-	GPIO_CLR(SWI2C_SDA);
-	DELAY(delay);
-	return ack;
-}
-
-unsigned char swi2c_read(int delay)
-{
-	GPIO_SET(SWI2C_SDA);
-	DELAY(delay);
-	GPIO_INP(SWI2C_SDA);
-	unsigned char data = 0;
-	int bit; for (bit = 7; bit >= 0; bit--)
-	{
-		GPIO_SET(SWI2C_SCL);
-		DELAY(delay);
-		data |= GPIO_GET(SWI2C_SDA) << bit;
-		GPIO_CLR(SWI2C_SCL);
-		DELAY(delay);
-	}
-	GPIO_OUT(SWI2C_SDA);
-	return data;
-}
-
-void swi2c_write(int delay, unsigned char data)
-{
-	int bit; for (bit = 7; bit >= 0; bit--)
-	{
-		if (data & (1 << bit)) GPIO_SET(SWI2C_SDA);
-		else GPIO_CLR(SWI2C_SDA);
-		DELAY(delay);
-		GPIO_SET(SWI2C_SCL);
-		DELAY(delay);
-		GPIO_CLR(SWI2C_SCL);
-		DELAY(delay);
-	}
-}
-
-int swi2c_check(unsigned char dev_addr)
-{
-	int delay = 1 << (swi2c_cfg & 0xf);
-	int tmout = 1 << (swi2c_cfg >> 4);
-	swi2c_start(delay);
-	swi2c_write(delay, (dev_addr & SWI2C_DMSK) << SWI2C_ASHF);
-	if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
-	swi2c_stop(delay);
-	return 1;
-}
-
-#ifdef SWI2C_A8 //8bit address
-
-int swi2c_readByte_A8(unsigned char dev_addr, unsigned char addr, unsigned char* pbyte)
-{
-	int delay = 1 << (swi2c_cfg & 0xf);
-	int tmout = 1 << (swi2c_cfg >> 4);
-	swi2c_start(delay);
-	swi2c_write(delay, SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
-	if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
-	swi2c_write(delay, addr & 0xff);
-	if (!swi2c_wait_ack(delay, tmout)) return 0;
-	swi2c_stop(delay);
-	swi2c_start(delay);
-	swi2c_write(delay, SWI2C_RMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
-	if (!swi2c_wait_ack(delay, tmout)) return 0;
-	unsigned char byte = swi2c_read(delay);
-	swi2c_stop(delay);
-	if (pbyte) *pbyte = byte;
-	return 1;
-}
-
-int swi2c_writeByte_A8(unsigned char dev_addr, unsigned char addr, unsigned char* pbyte)
-{
-	int delay = 1 << (swi2c_cfg & 0xf);
-	int tmout = 1 << (swi2c_cfg >> 4);
-	swi2c_start(delay);
-	swi2c_write(delay, SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
-	if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
-	swi2c_write(delay, addr & 0xff);
-	if (!swi2c_wait_ack(delay, tmout)) return 0;
-	swi2c_write(delay, *pbyte);
-	if (!swi2c_wait_ack(delay, tmout)) return 0;
-	swi2c_stop(delay);
-	return 1;
-}
-
-#endif //SWI2C_A8
-
-#ifdef SWI2C_A16 //16bit address
-
-int swi2c_readByte_A16(unsigned char dev_addr, unsigned short addr, unsigned char* pbyte)
-{
-	int delay = 1 << (swi2c_cfg & 0xf);
-	int tmout = 1 << (swi2c_cfg >> 4);
-	swi2c_start(delay);
-	swi2c_write(delay, SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
-	if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
-	swi2c_write(delay, addr >> 8);
-	if (!swi2c_wait_ack(delay, tmout)) return 0;
-	swi2c_write(delay, addr & 0xff);
-	if (!swi2c_wait_ack(delay, tmout)) return 0;
-	swi2c_stop(delay);
-	swi2c_start(delay);
-	swi2c_write(delay, SWI2C_RMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
-	if (!swi2c_wait_ack(delay, tmout)) return 0;
-	unsigned char byte = swi2c_read(delay);
-	swi2c_stop(delay);
-	if (pbyte) *pbyte = byte;
-	return 1;
-}
-
-int swi2c_writeByte_A16(unsigned char dev_addr, unsigned short addr, unsigned char* pbyte)
-{
-	int delay = 1 << (swi2c_cfg & 0xf);
-	int tmout = 1 << (swi2c_cfg >> 4);
-	swi2c_start(delay);
-	swi2c_write(delay, SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
-	if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
-	swi2c_write(delay, addr >> 8);
-	if (!swi2c_wait_ack(delay, tmout)) return 0;
-	swi2c_write(delay, addr & 0xff);
-	if (!swi2c_wait_ack(delay, tmout)) return 0;
-	swi2c_write(delay, *pbyte);
-	if (!swi2c_wait_ack(delay, tmout)) return 0;
-	swi2c_stop(delay);
-	return 1;
-}
-
-#endif //SWI2C_A16
-
-
-#endif //SWI2C

+ 19 - 6
Firmware/swi2c.h

@@ -1,22 +1,35 @@
+//swi2c.h
 #ifndef SWI2C_H
 #define SWI2C_H
 
+#include <inttypes.h>
+#include "config.h"
+
+
+#if defined(__cplusplus)
+extern "C" {
+#endif //defined(__cplusplus)
+
 //initialize
-extern void swi2c_init(unsigned char sda, unsigned char scl, unsigned char cfg);
+extern void swi2c_init(void);
 
 //check device address acknowledge
-extern int swi2c_check(unsigned char dev_addr);
+extern uint8_t swi2c_check(uint8_t dev_addr);
 
 //read write functions - 8bit address (most i2c chips)
 #ifdef SWI2C_A8
-extern int swi2c_readByte_A8(unsigned char dev_addr, unsigned char addr, unsigned char* pbyte);
-extern int swi2c_writeByte_A8(unsigned char dev_addr, unsigned char addr, unsigned char* pbyte);
+extern uint8_t swi2c_readByte_A8(uint8_t dev_addr, uint8_t addr, uint8_t* pbyte);
+extern uint8_t swi2c_writeByte_A8(uint8_t dev_addr, uint8_t addr, uint8_t* pbyte);
 #endif //SWI2C_A8
 
 //read write functions - 16bit address (e.g. serial eeprom AT24C256)
 #ifdef SWI2C_A16
-extern int swi2c_readByte_A16(unsigned char dev_addr, unsigned short addr, unsigned char* pbyte);
-extern int swi2c_writeByte_A16(unsigned char dev_addr, unsigned short addr, unsigned char* pbyte);
+extern uint8_t swi2c_readByte_A16(uint8_t dev_addr, uint16_t addr, uint8_t* pbyte);
+extern uint8_t swi2c_writeByte_A16(uint8_t dev_addr, uint16_t addr, uint8_t* pbyte);
 #endif //SWI2C_A16
 
+#if defined(__cplusplus)
+}
+#endif //defined(__cplusplus)
+
 #endif //SWI2C_H