热度 20
2012-4-20 10:12
1599 次阅读|
0 个评论
接上文 Arduino 和 WIZnet: http://forum.eet-cn.com/BLOG_ARTICLE_12057.HTM 具体代码如下: Code to test wiznet WIZ810MJ module See: Current features: * Read register/address values * Write register/address values * Configure networking to enable ping * Terrible hacked-together code License: LGPL (Although note spi_transfer comes from the Playground originally.) */ // Define SPI-related pins #define PIN_DATA_OUT 11 // MOSI (Master Out / Slave In) #define PIN_DATA_IN 12 // MISO (Master In / Slave Out) #define PIN_SPI_CLOCK 13 // SCK (Serial Clock) #define PIN_SLAVE_SELECT 10 // SS (Slave Select) #define PIN_RESET 9 // WIZnet module /RESET #define WIZNET_OPCODE_READ 0x0F #define WIZNET_OPCODE_WRITE 0xF0 #define DUMMY_DATA 0xFF void setup () { Serial.begin(9600); Serial.println("Setup enter..."); Serial.print("SPCR: "); Serial.println(SPCR, BIN); // Configure SPI // Configure I/O pins pinMode(PIN_DATA_OUT, OUTPUT); pinMode(PIN_DATA_IN, INPUT); pinMode(PIN_SPI_CLOCK, OUTPUT); pinMode(PIN_SLAVE_SELECT, OUTPUT); digitalWrite(PIN_SLAVE_SELECT, HIGH); // Disable slave // Configure SPI Control Register (SPCR) (All values initially 0) // Bit Description // 7 SPI Interrupt Enable -- disable (SPIE -- 0) // 6 SPI Enable -- enable (SPE -- 1) // 5 Data Order -- MSB 1st (DORD -- 0) (Slave specific) // 4 Master/Slave Select -- master (MSTR -- 1) // 3 Clock Polarity -- (CPOL -- 0) (Slave specific) ("Mode") // 2 Clock Phase -- (CPHA -- 0) (Slave specific) // 1 SPI Clock Rate Select 1 -- } (SPR1 -- 0) // 0 SPI Clock Rate Select 0 -- } fOSC/4 (SPR0 -- 0) ("Fastest" but see SPI2X in SPSR) SPCR = (1 Serial.print("SPCR: "); Serial.println(SPCR, BIN); // Clear previous data and status (TODO: Determine if necessary/better way.) // (Based on Playground SPI example.) byte dummy; dummy = SPSR; dummy = SPDR; delay(10); // Serial.println((1 // Serial.print("IODIR response: "); // iodirVal = getRegister(deviceOpcodeRead, REG_IODIR); // Serial.println(iodirVal, BIN); // setRegister(deviceOpcodeWrite, REG_IODIR, newVal); Serial.println("Triggering reset..."); pinMode(PIN_RESET, OUTPUT); digitalWrite(PIN_RESET, HIGH); // no reset delay(10); // Pretty arbitrary length digitalWrite(PIN_RESET, LOW); // reset delay(10); digitalWrite(PIN_RESET, HIGH); // no reset delay(10); Serial.println("Reset triggered..."); // Attempt read Serial.println("Read attempt..."); // digitalWrite(PIN_SLAVE_SELECT, LOW); // Enable slave /* byte data; data = spi_transfer(0x0F); // read Serial.print("Response: "); Serial.println(data, HEX); data = spi_transfer(0x00); // address part1 Serial.print("Response: "); Serial.println(data, HEX); data = spi_transfer(0x18); //address part2 //18 should return 0xD0 after reset. Serial.print("Response: "); Serial.println(data, HEX); data = spi_transfer(0xFF); // dummy Serial.print("Response: "); Serial.println(data, HEX); digitalWrite(PIN_SLAVE_SELECT, HIGH); // Disable slave */ // RTR Retry Time-value Register Serial.print("Read 0x0017: "); Serial.println(readAddressValue(0x00, 0x17), HEX); Serial.print("Read 0x0018: "); Serial.println(readAddressValue(0x00, 0x18), HEX); Serial.println("Read attempt end..."); Serial.println("Write attempt start..."); Serial.print("Initial read 0x0001: "); Serial.println(readAddressValue(0x00, 0x01), HEX); writeAddressValue(0x00, 0x01, 0xC0); Serial.print("Post-write read 0x0001: "); Serial.println(readAddressValue(0x00, 0x01), HEX); Serial.println("Write attempt end..."); Serial.println("Configure device..."); // default gateway writeAddressValue(0x00, 0x01, 0xC0); writeAddressValue(0x00, 0x02, 0xA8); writeAddressValue(0x00, 0x03, 0x02); writeAddressValue(0x00, 0x04, 0x65); // subnet mask writeAddressValue(0x00, 0x05, 0xFF); writeAddressValue(0x00, 0x06, 0xFF); writeAddressValue(0x00, 0x07, 0xFF); writeAddressValue(0x00, 0x08, 0x00); // source hardware address (MAC?) writeAddressValue(0x00, 0x09, 0x00); writeAddressValue(0x00, 0x0A, 0xDE); writeAddressValue(0x00, 0x0B, 0xAD); writeAddressValue(0x00, 0x0C, 0xBE); writeAddressValue(0x00, 0x0D, 0xEF); writeAddressValue(0x00, 0x0E, 0x00); // source ip address writeAddressValue(0x00, 0x0F, 0xC0); writeAddressValue(0x00, 0x10, 0xA8); writeAddressValue(0x00, 0x11, 0x02); writeAddressValue(0x00, 0x12, 0x69); Serial.println("End configure device..."); Serial.println("Setup exit..."); } byte readAddressValue(byte addressHiByte, byte addressLowByte) { // TODO: use a word for the address instead byte data = 0x00; digitalWrite(PIN_SLAVE_SELECT, LOW); // Enable slave // TODO: Check response values? e.g. 0x00, 0x01, 0x02 spi_transfer(WIZNET_OPCODE_READ); spi_transfer(addressHiByte); spi_transfer(addressLowByte); data = spi_transfer(DUMMY_DATA); digitalWrite(PIN_SLAVE_SELECT, HIGH); // Disable slave return data; } void writeAddressValue(byte addressHiByte, byte addressLowByte, byte targetValue) { // TODO: use a word for the address instead digitalWrite(PIN_SLAVE_SELECT, LOW); // Enable slave // TODO: Check response values? e.g. 0x00, 0x01, 0x02 spi_transfer(WIZNET_OPCODE_WRITE); spi_transfer(addressHiByte); spi_transfer(addressLowByte); spi_transfer(targetValue); digitalWrite(PIN_SLAVE_SELECT, HIGH); // Disable slave } /* byte getRegister(byte targetDeviceOpcode, byte registerAddress) { digitalWrite(PIN_SLAVE_SELECT, LOW); // Enable slave spi_transfer(targetDeviceOpcode); spi_transfer(registerAddress); // The register we want to read byte data; // Correct type? data = spi_transfer(0xFF); // Transfer dummy byte to get response digitalWrite(PIN_SLAVE_SELECT, HIGH); // Disable slave return data; } */ /* void setRegister(byte targetDeviceOpcode, byte registerAddress, byte value) { // TODO: Do Better? digitalWrite(PIN_SLAVE_SELECT, LOW); // Enable slave spi_transfer(targetDeviceOpcode); spi_transfer(registerAddress); // The register we want to write spi_transfer(value); digitalWrite(PIN_SLAVE_SELECT, HIGH); // Disable slave } */ // From Playground char spi_transfer(volatile char data) { SPDR = data; // Start the transmission while (!(SPSR (1 { }; return SPDR; // return the received byte } void loop() { } 欢迎大家留言评论~~