Skip to content

Commit

Permalink
Merge pull request #60 from DCC-EX:test-bluepill-again
Browse files Browse the repository at this point in the history
Test-bluepill-again
  • Loading branch information
peteGSX authored Jul 3, 2023
2 parents d26f138 + 84ad10a commit 6ea6ff6
Show file tree
Hide file tree
Showing 19 changed files with 155 additions and 47 deletions.
8 changes: 6 additions & 2 deletions EX-IOExpander.ino
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
#include "arduino_nucleo_f412zg.h"
#elif defined(ARDUINO_ARCH_SAMD)
#include "arduino_arch_samd.h"
// #elif defined(ARDUINO_GENERIC_F103C8TX)
// #include "arduino_generic_f103c8tx"
#elif defined(ARDUINO_BLUEPILL_F103C8)
#include "arduino_bluepill_f103c8.h"
#endif

/*
Expand All @@ -88,6 +88,9 @@ bool outputTestState = LOW; // Flag to set outputs high or low for testing
* Main setup function here.
*/
void setup() {
#if defined(ARDUINO_BLUEPILL_F103C8)
disableJTAG();
#endif
Serial.begin(115200);
USB_SERIAL.print(F("DCC-EX EX-IOExpander v"));
USB_SERIAL.println(VERSION);
Expand Down Expand Up @@ -117,6 +120,7 @@ void setup() {
digitalWrite(I2C_SDA, LOW);
digitalWrite(I2C_SCL, LOW);
#endif
startupDisplay();
// Need to intialise every pin in INPUT mode (no pull ups) for safe start
initialisePins();
USB_SERIAL.println(F("Initialised all pins as input only"));
Expand Down
2 changes: 0 additions & 2 deletions arduino_arch_samd.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#include <Arduino.h>
#include "globals.h"

#define BOARD_TYPE F("Arduino Zero or Clone")

pinDefinition pinMap[TOTAL_PINS] = {
{2,DIOP},{3,DIOP},{4,DIOP},{5,DIOP},{6,DIOP},{7,DIOP},{8,DIOP},{9,DIOP},{10,DIOP},{11,DIOP},
{12,DIOP},{13,DIOP},{14,DIO},{15,DIO},{16,DIO},{17,DIO},{18,DIO},{19,DIO},{22,DIO},{23,DIO},
Expand Down
1 change: 0 additions & 1 deletion arduino_avr_mega.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <Arduino.h>
#include "globals.h"

#define BOARD_TYPE F("Mega")
pinDefinition pinMap[TOTAL_PINS] = {
{2,DIOP},{3,DIOP},{4,DIOP},{5,DIOP},{6,DIOP},{7,DIOP},{8,DIOP},{9,DIOP},{10,DIOP},{11,DIOP},
{12,DIOP},{13,DIOP},{14,DIO},{15,DIO},{16,DIO},{17,DIO},{18,DIO},{19,DIO},{22,DIO},{23,DIO},
Expand Down
5 changes: 0 additions & 5 deletions arduino_avr_nano.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/

#if defined(ARDUINO_AVR_NANO)
#define BOARD_TYPE F("Nano")
#elif defined(ARDUINO_AVR_PRO)
#define BOARD_TYPE F("Pro Mini")
#endif
pinDefinition pinMap[TOTAL_PINS] = {
{2,DIO},{3,DIOP},{4,DIO},{5,DIOP},{6,DIOP},{7,DIO},
{8,DIO},{9,DIOP},{10,DIOP},{11,DIOP},{12,DIO},{13,DIO},
Expand Down
1 change: 0 additions & 1 deletion arduino_avr_uno.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <Arduino.h>
#include "globals.h"

#define BOARD_TYPE F("Uno")
pinDefinition pinMap[TOTAL_PINS] = {
{2,DIO},{3,DIOP},{4,DIO},{5,DIOP},{6,DIOP},{7,DIO},
{8,DIO},{9,DIOP},{10,DIOP},{11,DIOP},{12,DIO},{13,DIO},
Expand Down
23 changes: 15 additions & 8 deletions arduino_generic_f103c8tx → arduino_bluepill_f103c8.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,32 @@
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef ARDUINO_GENERIC_F103C8TX_H
#define ARDUINO_GENERIC_F103C8TX_H
#ifndef ARDUINO_BLUEPILL_F103C8_H
#define ARDUINO_BLUEPILL_F103C8_H

#include <Arduino.h>
#include "globals.h"

#define BOARD_TYPE F("STM32F103C8Tx")
pinDefinition pinMap[TOTAL_PINS] = {
{PB11,DIOP},{PB10,DIOP},{PB1,AIDIOP},{PB0,AIDIOP},{PA7,AIDIOP},{PA6,AIDIOP},{PA5,AIDIO},{PA4,AIDIO},{PA3,AIDIOP},
{PA2,AIDIOP},{PA1,AIDIOP},{PA0,AIDIO},{PC15,DIO},{PC14,DIO},{PC13,DIO},{PB7,DIO},{PB6,DIO},{PB5,DIOP},{PB4,DIOP},
{PB3,DIOP},{PA15,DIOP},{PA12,DIO},{PA11,DIOP},{PA10,DIOP},{PA9,DIOP},{PA8,DIOP},{PB15,DIOP},{PB14,DIOP},
{PB13,DIOP},{PB12,DIO},
{PC13,DIO},{PC14,DIO},{PC15,DIO},{PA0,AIDIO},{PA1,AIDIOP},{PA2,AIDIOP},{PA3,AIDIOP},{PA4,AIDIO},
{PA5,AIDIO},{PA6,AIDIOP},{PA7,AIDIOP},{PB0,AIDIOP},{PB1,AIDIOP},{PB10,DIOP},{PB11,DIOP},
{PB9,DIO},{PB8,DIO},{PB5,DIOP},{PB4,DIOP},{PB3,DIOP},{PA15,DIOP},
{PA10,DIOP},{PA9,DIOP},{PA8,DIOP},{PB15,DIOP},{PB14,DIOP},{PB13,DIOP},{PB12,DIO},
};
#define I2C_SDA PB9
#define I2C_SCL PB8

pinName pinNameMap[TOTAL_PINS] = {
{PC13,"PC13"},{PC14,"PC14"},{PC15,"PC15"},{PA0,"PA0"},{PA1,"PA1"},{PA2,"PA2"},{PA3,"PA3"},{PA4,"PA4"},
{PA5,"PA5"},{PA6,"PA6"},{PA7,"PA7"},{PB0,"PB0"},{PB1,"PB1"},{PB10,"PB10"},{PB11,"PB11"},
{PB9,"PB9"},{PB8,"PB8"},{PB5,"PB5"},{PB4,"PB4"},{PB3,"PB3"},{PA15,"PA15"},
{PA10,"PA10"},{PA9,"PA9"},{PA8,"PA8"},{PB15,"PB15"},{PB14,"PB14"},{PB13,"PB13"},{PB12,"PB12"},
};

/*
Pins not available for use:
PB9|PB8 (I2C)
PB7|PB6 (I2C)
PA11|PA12 (USB)
Buttons/LEDs:
Note that PC13 has an LED
Expand Down
2 changes: 0 additions & 2 deletions arduino_nucleo_f411re.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#include <Arduino.h>
#include "globals.h"

#define BOARD_TYPE F("NUCLEO-F411RE")

pinDefinition pinMap[TOTAL_PINS] = {
{PC10,DIO},{PC11,DIO},{PC12,DIO},{PD2,DIO},{PA15,DIOP},{PB7,DIOP},{PC15,DIO}, // CN7
{PA0,AIDIOP},{PA1,AIDIOP},{PA4,AIDIO},{PB0,AIDIOP},{PC2,AIDIO},{PC1,AIDIO},{PC3,AIDIO},{PC0,AIDIO}, // CN7
Expand Down
2 changes: 0 additions & 2 deletions arduino_nucleo_f412zg.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#include <Arduino.h>
#include "globals.h"

#define BOARD_TYPE F("NUCLEO-F412ZG")

pinDefinition pinMap[TOTAL_PINS] = {
{PC10,DIO},{PC11,DIO},{PC12,DIO},{PD2,DIO},{PF6,DIOP},{PF7,DIOP},{PA15,DIOP},{PB7,DIOP},{PC13,DIO}, // CN11
{PA0,AIDIOP},{PA1,AIDIOP},{PA4,AIDIO},{PB0,AIDIOP},{PC2,AIDIO},{PC1,AIDIO},{PC3,AIDIO},{PC0,AIDIO}, // CN11
Expand Down
17 changes: 14 additions & 3 deletions defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,42 @@
//
// Arduino Nano or Pro Mini
#if defined(ARDUINO_AVR_NANO) || defined(ARDUINO_AVR_PRO)
#if defined(ARDUINO_AVR_NANO)
#define BOARD_TYPE F("Nano")
#elif defined(ARDUINO_AVR_PRO)
#define BOARD_TYPE F("Pro Mini")
#endif
#define TOTAL_PINS 18
#define NUM_PWM_PINS 6
#define HAS_EEPROM
// Arduino Uno
#elif defined(ARDUINO_AVR_UNO)
#define BOARD_TYPE F("Uno")
#define TOTAL_PINS 16
#define NUM_PWM_PINS 6
#define HAS_EEPROM
// Arduino Mega2560
#elif defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_MEGA)
#define BOARD_TYPE F("Mega")
#define TOTAL_PINS 62
#define NUM_PWM_PINS 12
#define HAS_EEPROM
#elif defined(ARDUINO_NUCLEO_F411RE)
#define BOARD_TYPE F("Nucleo-F411RE")
#define TOTAL_PINS 40
#define NUM_PWM_PINS 25
#elif defined(ARDUINO_NUCLEO_F412ZG)
#define BOARD_TYPE F("Nucleo-F412ZG")
#define TOTAL_PINS 97
#define NUM_PWM_PINS 40
#elif defined(ARDUINO_ARCH_SAMD)
#define BOARD_TYPE F("Arduino Zero or Clone")
#define TOTAL_PINS 27
#define NUM_PWM_PINS 12
#elif defined(ARDUINO_GENERIC_F103C8TX)
#define TOTAL_PINS 30
#define NUM_PWM_PINS 20
#elif defined(ARDUINO_BLUEPILL_F103C8)
#define BOARD_TYPE F("BLUEPILL-STM32F103C8")
#define TOTAL_PINS 28
#define NUM_PWM_PINS 19
#endif

/////////////////////////////////////////////////////////////////////////////////////
Expand Down
15 changes: 14 additions & 1 deletion device_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,17 @@ void reset() {
NVIC_SystemReset();
while(true) {};
#endif
}
}

/*
Disabling JTAG is required to avoid pin conflicts on Bluepill
*/
#if defined(ARDUINO_BLUEPILL_F103C8)
void disableJTAG() {
// Disable JTAG and enable SWD by clearing the SWJ_CFG bits
// Assuming the register is named AFIO_MAPR or AFIO_MAPR2
AFIO->MAPR &= ~(AFIO_MAPR_SWJ_CFG);
// or
// AFIO->MAPR2 &= ~(AFIO_MAPR2_SWJ_CFG);
}
#endif
3 changes: 3 additions & 0 deletions device_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ uint8_t getI2CAddress();
void writeI2CAddress(int16_t eepromAddress);
void eraseI2CAddress();
void reset();
#if defined(ARDUINO_BLUEPILL_F103C8)
void disableJTAG();
#endif

#endif
48 changes: 47 additions & 1 deletion display_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ void displayPins() {
USB_SERIAL.println("Current pin states:");
for (uint8_t pin = 0; pin < numPins; pin++) {
String pinLabel = pinNameMap[pin].pinLabel;
if (pinLabel.length() == 1) {
pinLabel += " ";
} else if (pinLabel.length() == 2) {
pinLabel += " ";
} else if (pinLabel.length() == 3) {
pinLabel += " ";
}
switch(exioPins[pin].mode) {
case MODE_UNUSED: {
USB_SERIAL.print(F("Pin "));
Expand Down Expand Up @@ -183,4 +190,43 @@ void processDisplayOutput() {
default:
break;
}
}
}

/*
* Function to display startup info
*/
void startupDisplay() {
USB_SERIAL.print(F("DCC-EX EX-IOExpander v"));
USB_SERIAL.println(VERSION);
USB_SERIAL.print(F("Detected device: "));
USB_SERIAL.println(BOARD_TYPE);
if (i2cAddress < 0x08 || i2cAddress > 0x77) {
USB_SERIAL.print(F("ERROR: Invalid I2C address configured: 0x"));
USB_SERIAL.print(i2cAddress, HEX);
USB_SERIAL.println(F(", using myConfig.h instead"));
i2cAddress = I2C_ADDRESS;
}
USB_SERIAL.print(F("Available at I2C address 0x"));
USB_SERIAL.println(i2cAddress, HEX);
#if defined(HAS_SERVO_LIB)
USB_SERIAL.print(F("Servo library support for up to "));
USB_SERIAL.print(MAX_SERVOS);
USB_SERIAL.println(F(" servos"));
#endif
#if defined(HAS_DIMMER_LIB)
USB_SERIAL.print(F("Dimmer library support for up to "));
USB_SERIAL.print(MAX_DIMMERS);
USB_SERIAL.println(F(" LEDs"));
#endif
#if !defined(HAS_SERVO_LIB)
USB_SERIAL.print(F("Use hardware PWM pins for up to "));
USB_SERIAL.print(numPWMPins);
USB_SERIAL.println(F(" servos/LEDs"));
#endif
#if defined(DISABLE_I2C_PULLUPS) && defined(I2C_SDA) && defined(I2C_SCL)
USB_SERIAL.print(F("Disabling I2C pullups on pins SDA|SCL: "));
USB_SERIAL.print(I2C_SDA);
USB_SERIAL.print(F("|"));
USB_SERIAL.println(I2C_SCL);
#endif
}
1 change: 1 addition & 0 deletions display_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ void setVersion();
void displayPins();
void displayVpinMap();
void processDisplayOutput();
void startupDisplay();

#endif
1 change: 1 addition & 0 deletions globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
extern pinDefinition pinMap[TOTAL_PINS];
extern pinName pinNameMap[TOTAL_PINS];
extern pinConfig exioPins[TOTAL_PINS];
extern uint8_t i2cAddress;
extern uint8_t numPins;
extern uint8_t numDigitalPins;
extern uint8_t numAnaloguePins;
Expand Down
2 changes: 1 addition & 1 deletion i2c_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ uint8_t numPWMPins = 0; // Number of PWM capable pins
bool setupComplete = false; // Flag when initial configuration/setup has been received
uint8_t outboundFlag; // Used to determine what data to send back to the CommandStation
byte commandBuffer[3]; // Command buffer to interact with device driver
byte responseBuffer[0]; // Buffer to send single response back to device driver
byte responseBuffer[1]; // Buffer to send single response back to device driver
uint8_t numReceivedPins = 0;

/*
Expand Down
8 changes: 4 additions & 4 deletions myConfig.example.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
// Enable test mode - ensure only one test mode is active at one time.
// This is handy if serial input doesn't work for commands for some reason.
//
// ANALOGUE - equivalent of <A>
// INPUT - equivalent of <I>
// OUTPUT - equivalent of <O>
// PULLUP - equivalent of <P>
// ANALOGUE - equivalent of <T A>
// INPUT - equivalent of <T I>
// OUTPUT - equivalent of <T O>
// PULLUP - equivalent of <T P>
//
// #define TEST_MODE ANALOGUE_TEST
// #define TEST_MODE INPUT_TEST
Expand Down
Loading

0 comments on commit 6ea6ff6

Please sign in to comment.