0

I am working project, using STM32F103C8T6 and W5500 Ethernet module. My project is something like network relay. My problem is that the STM32F103 can not boot up after pressing reset button when the BOOT0 jumper is on the position 0. But when I am pressing the reset button when the BOOT0 jumper is on the position 1, it resets successful, and removing old program from STM board.

STM board I am using: STM32F103C8T6

Library for W5500 I am using: Ethernet_STM

My sketch:

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet_STM.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <LiquidCrystal_I2C.h>
#include <EmonLib.h>

//write(0x00, 0x05, 128); // Software reset the W5500 chip
//line 333 in w5500.h    __SOCKET_REGISTER16(SnRX_WR,    0x002A)      // RX Write Pointer (supported?)


#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io
;
#else
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
#endif
IPAddress ip(192, 168, 0, 200);
IPAddress mydns(192,168,0,1);
unsigned int localPort = 8888;      // local port to listen on


char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,


EthernetUDP Udp;
EnergyMonitor enMonitor0, enMonitor1;
LiquidCrystal_I2C lcd(0x3F, 16, 2);

const int acVoltage0 = PA0;
const int acVoltage1 = PA1;
const int analogIn = PA3;
const int currentPin = PA2;
const int statusPin = PC13;
const int relay1 = PB0;
const int relay2 = PB1;
const int relay3 = PB3;
const int relay4 = PB4;
const float r_divider = 0.048122;
double V_AC0, V_AC1, Amps, voltageDC;
bool auth = false;
bool entering = false;
bool pchange = false;
char *key = "asdf";

void setup()
{
#if defined(WIZ550io_WITH_MACADDRESS)
  Ethernet.begin(ip);
#else
  Ethernet.begin(mac,ip, mydns);
#endif  
  Udp.begin(localPort);

  Serial.begin(9600);
  Serial.println("STARTED");
  disableDebugPorts();
  lcd.begin();
  lcd.backlight();
  pinMode(PB11, OUTPUT);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(statusPin, OUTPUT);
  pinMode(analogIn, INPUT_ANALOG);
  pinMode(currentPin, INPUT_ANALOG);
  digitalWrite(relay4, LOW);
  enMonitor0.voltage(acVoltage0, 227, 1.7);
  enMonitor1.voltage(acVoltage1, 227, 1.7);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" -++=EDISON=++-  ");
Serial.println("END of setup");
  //nvic_sys_reset();   //Reset STM32F103C
}



double measureVoltage_AC0()
{
  double V_AC;
  enMonitor0.calcVI(200, 2000);
  V_AC = enMonitor0.Vrms * 1.51;
  return V_AC;
}

double measureVoltage_AC1()
{
  double V_AC;
  enMonitor1.calcVI(200, 2000);
  V_AC = enMonitor1.Vrms * 1.51;
  return V_AC;
}

double measureVoltage_DC()
{
  uint16_t adcin;
  double voltage;
  adcin = analogRead(analogIn);
  voltage = 3.3 / 4095 * adcin;
  voltage = voltage * 20;
  return voltage;
}

double measureCurrent()
{
  uint16_t RawValue = 0;
  double Voltage = 0;
  double Amps = 0;
  RawValue = analogRead(currentPin);
  Voltage = (RawValue / 4095.0) * 5000; // Gets you mV
  Amps = ((Voltage - 600) / 60);
  if (RawValue < 50)  Amps = 0.0;
  return Amps;
}

void loop()
{
  Serial.println("loop");
  digitalWrite(PB11, LOW);
  delay(100);
  digitalWrite(PB11, HIGH);
  delay(100);
main:
Serial.println("mAIN");
  V_AC0 = measureVoltage_AC0();
  V_AC1 = measureVoltage_AC1();
  voltageDC = measureVoltage_DC();
  Amps = measureCurrent();
  Serial.println("lcd.printing");
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(V_AC0);
  lcd.setCursor(7, 0);
  lcd.print("V");
  lcd.setCursor(0, 1);
  lcd.print(V_AC1);
  lcd.setCursor(7, 1);
  lcd.print("V");
  lcd.setCursor(10, 0);
  lcd.print(voltageDC);
  lcd.setCursor(15, 0);
  lcd.print("V");
  lcd.setCursor(10, 1);
  lcd.print(Amps);
  lcd.setCursor(15, 1);
  lcd.print("A");
  Serial.println(" end lcd.printing");
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  Serial.println(packetSize);
  if (packetSize)
  {
    IPAddress remote = Udp.remoteIP();
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    if (auth == false && entering == false)
    {
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.write("Please Enter KEY");
      Udp.endPacket();
      entering = true;
    }
    else if (!auth && entering)
    {
      if (strstr(packetBuffer, key))
      {
        auth = true;
        entering = false;
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("Auth OK");
        Udp.endPacket();
      }
    }
    else if (auth)
    {
      if (strstr(packetBuffer, "R1=ON"))
      {
        digitalWrite(relay1, HIGH);
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("K1 turned ON");
        Udp.endPacket();
      }
      else if (strstr(packetBuffer, "R1=OFF"))
      {
        digitalWrite(relay1, LOW);
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("K1 turned OFF");
        Udp.endPacket();
      }
      else if (strstr(packetBuffer, "R2=ON"))
      {
        digitalWrite(relay2, HIGH);
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("K2 turned ON");
        Udp.endPacket();
      }
      else if (strstr(packetBuffer, "R2=OFF"))
      {
        digitalWrite(relay2, LOW);
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("K2 turned OFF");
        Udp.endPacket();
      }
      else if (strstr(packetBuffer, "R3=ON"))
      {
        digitalWrite(relay3, HIGH);
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("K3 turned ON");
        Udp.endPacket();
      }
      else if (strstr(packetBuffer, "R3=OFF"))
      {
        digitalWrite(relay3, LOW);
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("K3 turned OFF");
        Udp.endPacket();
      }
      else if (strstr(packetBuffer, "R4=ON"))
      {
        digitalWrite(relay4, HIGH);
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("K4 turned ON");
        Udp.endPacket();
      }
      else if (strstr(packetBuffer, "R4=OFF"))
      {
        digitalWrite(relay4, LOW);
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("K4 turned OFF");
        Udp.endPacket();
      }
      else if (strstr(packetBuffer, "MEASURES"))
      {
        char ac1[10], ac2[10], dc[10], amps[10];
        dtostrf(V_AC0, 4, 2, ac1);
        dtostrf(V_AC1, 4, 2, ac2);
        dtostrf(voltageDC, 4, 2, dc);
        dtostrf(Amps, 4, 2, amps);
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("VAC1 = ");
        Udp.write(ac1);
        Udp.write(" ");
        Udp.write("VAC2 = ");
        Udp.write(ac2);
        Udp.write(" ");
        Udp.write("VDC = ");
        Udp.write(dc);
        Udp.write(" ");
        Udp.write("A = ");
        Udp.write(amps);
        Udp.endPacket();
      }
      else if (strstr(packetBuffer, "CHPASS"))
      {
        pchange = true;
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("Enter new KEY");
        Udp.endPacket();
        while (pchange)
        {
          for (int i = 0; i < 24; i++)
          {
            packetBuffer[i] = 0;
          }
          int tmp = Udp.parsePacket();
          if (tmp)
          {
            Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
            int len = 0;
            for (int i = 0; i < 24; i++)
            {
              if (packetBuffer[i] > 0)
                len++;
            }
            Serial.println(len);
            if (len != 4)
            {
              Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
              Udp.write("Only 4 digit");
              Udp.endPacket();
            }
            else
            {
              key = packetBuffer;
              Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
              Udp.write("New KEY is: ");
              Udp.write(key);
              Udp.endPacket();
              pchange = false;
              goto main;
            }
          }
        }
      }
      else if (strstr(packetBuffer, "RESTART"))
      {
        nvic_sys_reset();
      }
      else if (strstr(packetBuffer, "LOGOUT"))
      {
        auth = false;
        entering = false;
        Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
        Udp.write("Log out OK");
        Udp.endPacket();
      }
      for (int i = 0; i < 24; i++)
      {
        packetBuffer[i] = 0;
      }
    }
  }
  delay(10);
}

In my opinion this problem causes from the ethernet library, because when I comment the lines which contains the declaration of the objects which is members of this library, the reset function works good.

  • Comments are not for extended discussion; this conversation has been moved to chat. – Majenko Mar 21 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.