From 393c41b8dd08ca47ba39c944ab490e7b0c82cb41 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Mon, 22 Jun 2020 12:41:18 -0700 Subject: [PATCH] Added configs to allow switching to 328P variant boards #featureadd --- .../Pyr0_Piezo_Sensor_v2.x.x/platformio.ini | 4 +-- .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.cpp | 34 ++++++++++++++++++- .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_pins.h | 26 ++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/platformio.ini b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/platformio.ini index a77335c..e0c0ca3 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/platformio.ini +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/platformio.ini @@ -26,9 +26,9 @@ board_hardware.uart = uart0 board_hardware.bod = 1.8v board_hardware.eesave = yes -[env:ATmega88P] +[env:ATmega328P] platform = atmelavr -board = ATmega88P +board = ATmega328P framework = arduino lib_deps = Arduino diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.cpp index f41c200..69d5e2a 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_i2c.cpp @@ -2,7 +2,12 @@ #include "pP_cmd.h" #include "pP_i2c_config.h" #include "pP_volatile.h" -#include +#ifdef __AVR_ATmega328P__ + #include +#endif +#ifdef __AVR_ATmega328PB__ + #include +#endif uint8_t command; uint32_t value; @@ -36,7 +41,12 @@ void i2cReportConfig() { i2cWrite(buffer, 16, VCCSW); i2cWrite(buffer, 18, voltMeterConstant); memcpy(buffer + 22, PP_VERSION, length - 22); + #ifdef __AVR_ATmega328P__ + Wire.write(buffer, length); + #endif + #ifdef __AVR_ATmega328PB__ Wire1.write(buffer, length); + #endif } void i2cReportState() { @@ -47,7 +57,12 @@ void i2cReportState() { i2cWrite(buffer, 4, (int)((long)VFol * Vin / 1023)); i2cWrite(buffer, 6, ERR_STATE); i2cWrite(buffer, 8, PZ_STATE); + #ifdef __AVR_ATmega328P__ + Wire.write(buffer, length); + #endif + #ifdef __AVR_ATmega328PB__ Wire1.write(buffer, length); + #endif } void i2cReply() { @@ -68,9 +83,19 @@ void i2cInput(int bytesReceived) { for (int a = 0; a < bytesReceived; a++) { // Check length of message, drops anything longer than [longBytes] if (a == 0) { + #ifdef __AVR_ATmega328P__ + command = Wire.read(); + #endif + #ifdef __AVR_ATmega328PB__ command = Wire1.read(); + #endif } else if (a == 1) { + #ifdef __AVR_ATmega328P__ + value = Wire.read(); + #endif + #ifdef __AVR_ATmega328PB__ value = Wire1.read(); + #endif } else { value = value << 8 | Wire1.read(); } @@ -124,7 +149,14 @@ void i2cInput(int bytesReceived) { } void i2cInit() { + #ifdef __AVR_ATmega328P__ + Wire.begin(pP_i2c_address); + Wire.onRequest(i2cReply); + Wire.onReceive(i2cInput); + #endif + #ifdef __AVR_ATmega328PB__ Wire1.begin(pP_i2c_address); Wire1.onRequest(i2cReply); Wire1.onReceive(i2cInput); + #endif } diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_pins.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_pins.h index bbafdfc..57ca5c6 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_pins.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_pins.h @@ -15,6 +15,8 @@ Default pins (based on Rev.2.x.xPCB layout) * PD3 OC2B (Voltage Follower VRef PWM Out 'D3') */ +#ifdef __AVR_ATmega328PB__ // Pin assignments for 328PB variant + // Analog Pin Assignments #define V_FOLLOW_PIN A0 // Sense pin to check Voltage Follower stage #define VCOMP_SENSE_PIN A1 // Sense pin to check comparator stage voltage @@ -32,3 +34,27 @@ Default pins (based on Rev.2.x.xPCB layout) #define VCOMP_PWM 9 // PWM analog output pin for comparator adjustment #define PZDET_PIN 16 // Digital input pin for detecting piezo connection #define VCCSW_PIN 8 // VCC variable regulator switch pin + +#endif //__AVR_ATmega328PB__ + +#ifdef __AVR_ATmega328P__ // Pin assignments for 328P variant + +// Analog Pin Assignments +#define V_FOLLOW_PIN A0 // PC0 Sense pin to check Voltage Follower stage +#define VCOMP_SENSE_PIN A1 // PC1 Sense pin to check comparator stage voltage + +// Digital Pin Assignments +#define TRG_OUT 7 // LED and Z-Min trigger output connected to digital pin 7 +//#define TRG_OUT 13 // For testing on Atmega328/2560, Output is moved to onboard LED pin +#define Z_TRG 2 // the piezo is connected to INT0 / digital pin 2 +#define ERR_LED 4 // LED will blink if optimal voltage range cannot be achieved +#define GADJ_R0 20 // PB6 Auto-adjust ladder pin assignments +#define GADJ_R1 21 // PB7 " +#define GADJ_R2 5 // PD5 " +#define GADJ_R3 6 // PD6 " +#define V_FOL_PWM 3 // PD3 PWM analog output pin for voltage follower adjustment +#define VCOMP_PWM 9 // PB1 PWM analog output pin for comparator adjustment +#define PZDET_PIN 16 // PC2 Digital input pin for detecting piezo connection +#define VCCSW_PIN 8 // PB0 VCC variable regulator switch pin + +#endif //__AVR_ATmega328P__ \ No newline at end of file