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

Error initializing library on Arduino Nano #427

Open
rbp9802 opened this issue Apr 1, 2023 · 5 comments
Open

Error initializing library on Arduino Nano #427

rbp9802 opened this issue Apr 1, 2023 · 5 comments

Comments

@rbp9802
Copy link

rbp9802 commented Apr 1, 2023

Hello everyone, I'm trying the backsoon example without success.

Program get stuck in if statement in line 46. if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0) 'if' does not return any value.

From what a read from #406 and #397 I understand that this is an Arduino Nano Issue but I did not get the final solution to the problem.

I also can't upload any script to the board while my ENC28J60 module is grounded to the board (should I start another issue?). I presume it is a module wiring error but have not found any solution yet.

Hope anyone can help me, thanks!

@Qix-
Copy link

Qix- commented May 19, 2023

I'm hitting this as well. For some reason the MISTAT_BUSY bit isn't being cleared after initializing MII read/write. Investigating a bit...


Weird, the busy bit is high even directly after a soft reset... so MI* register writes and reads are failing and the inner busy loops never terminate.


My own barebones code that initializes the chip isn't showing that it's high upon boot. The library is doing something specific to make it go high.


Okay by replacing all of the SPI code with the SPI library instead of the code using some sort of registers (never seen that before) I got things to work.

void ENC28J60::initSPI () {
    pinMode(SS, OUTPUT);
    pinMode(MOSI, INPUT_PULLUP);
    pinMode(MISO, OUTPUT);
    pinMode(SCK, OUTPUT);

    digitalWrite(SS, LOW);
    digitalWrite(MOSI, HIGH);
    digitalWrite(MOSI, LOW);

    SPI.begin();
}

static void enableChip () {
    cli();
    digitalWrite(SS, LOW);
    SPI.beginTransaction(spi_settings);
}

static void disableChip () {
    SPI.endTransaction();
    digitalWrite(SS, HIGH);
    sei();
}

inline static byte xferSPI (byte data) {
   return SPI.transfer(data);
}

static byte readOp (byte op, byte address) {
    enableChip();
    xferSPI(op | (address & ADDR_MASK));
    byte result = xferSPI(0x00);
    if (address & 0x80)
        result = xferSPI(0x00);
    disableChip();
    return result;
}

static void writeOp (byte op, byte address, byte data) {
    enableChip();
    xferSPI(op | (address & ADDR_MASK));
    xferSPI(data);
    disableChip();
}

static void readBuf(uint16_t len, byte* data) {
    uint8_t nextbyte;

    enableChip();
    if (len != 0) {
        xferSPI(ENC28J60_READ_BUF_MEM);

        while (len--) {
            *data++ = xferSPI(0x00);
        }
    }
    disableChip();
}

static void writeBuf(uint16_t len, const byte* data) {
    enableChip();
    if (len != 0) {
        xferSPI(ENC28J60_WRITE_BUF_MEM);

        while (len--) {
            xferSPI(*data++);
        };
    }
    disableChip();
}

@Qix-
Copy link

Qix- commented May 19, 2023

Specifically, the key was the missing SPI.beginTransaction(). The spi_settings I used were:

const SPISettings spi_settings {
  250000,
  MSBFIRST,
  SPI_MODE0
};

@Qix-
Copy link

Qix- commented May 19, 2023

In fact this might actually be about clock speed. For me, anything above 250000 seems to cause the same problem. Maybe the clock speed on Nanos needs to be fixed. I have no idea what the register-based system is doing, I'm only familiar with the SPI library itself, so perhaps the maintainers know which values to set it to to go down to 250KHz.

@barrydegraaff
Copy link

In my case it seems the 3.3V regulator on Arduino Nano is not enough to power the ENC28J60, not even if I supply 9V into vin. On Mega it works, probably cheap Nano clone issue.

@Qix-
Copy link

Qix- commented Jun 14, 2023

It's definitely not enough, but that's not related to this issue. You really do need to power it with an external 3V3 supply. The low current from the Nano has nothing to do with this library hanging, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants