After a few failed attempts at getting the SPI interface to work I finally have something that is communicating. I know this because I can turn off and on an LED on the enc28j60-h board. Here is the code used to check the interface.

void initMAC(void)
{
 initSPI(); // initialise the SPI
 unsigned char op = 0xFF; //Reset
 SEL_MAC(TRUE);
 SPIWrite(&op,1);
 SEL_MAC(FALSE);
 __delay_cycles(1600); //Only need 50us but best give it extra
 op = 0x5F; //Write to ECON1 Reg
 SEL_MAC(TRUE);
 SPIWrite(&op,1);
 op = 0x02; //Write 2 as we want to select bank 2.
 SPIWrite(&op,1);
 SEL_MAC(FALSE);
 op = 0x54; //Write to MIREGADR Reg
 SEL_MAC(TRUE);
 SPIWrite(&op,1);
 op = 0x14; //Select PHLCON reg.
 SPIWrite(&op,1);
 SEL_MAC(FALSE);
 op = 0x56; //Write to MIWRL Reg
 SEL_MAC(TRUE);
 SPIWrite(&op,1);
 op = 0x80; //Turn on LEDB.
 SPIWrite(&op,1);
 SEL_MAC(FALSE);
 op = 0x57; //Write to MIWRH Reg
 SEL_MAC(TRUE);
 SPIWrite(&op,1);
 op = 0x38; //Turn on LEDA.
 SPIWrite(&op,1);
 SEL_MAC(FALSE);
}

Of course this is horribly formatted and illegible but it works. The only thing not tested here is reading data. As a read data test the version number can be read.

 op = 0x5F; //Write to ECON1 Reg
 SEL_MAC(TRUE);
 SPIWrite(&op,1);
 op = 0x03; //Write 3 as we want to select bank 3.
 SPIWrite(&op,1);
 SEL_MAC(FALSE);
 op = 0x12; //Read EREVID Reg
 SEL_MAC(TRUE);
 SPIWrite(&op,1);
 SPIRead(&op,1);
 SEL_MAC(FALSE);

For me the version was 7. Which is apparently a valid number so it seems as though the device is working. This later turned out to be occasionally corrupt and the solution is explained in the next post. Next update will change this code into something useful.

 

Advertisements