#ifndef F_CPU #define F_CPU 16000000UL #endif #include #include #include static void uart_init(void) { UCSR0A = 0; UBRR0H = 0; UBRR0L = 103; UCSR0C = (1U << UCSZ01) | (1U << UCSZ00); UCSR0B = (1U << TXEN0); } static void uart_tx(char c) { while (!(UCSR0A & (1U << UDRE0))) { } UDR0 = (uint8_t)c; } static void uart_puts(const char *s) { while (*s) { uart_tx(*s++); } } static uint8_t spi_transfer(uint8_t data) { SPDR = data; while (!(SPSR & (1U << SPIF))) { } return SPDR; } uint8_t w5500_read_byte(uint16_t addr, uint8_t bsb) { PORTB &= (uint8_t)~(1U << PB2); spi_transfer((uint8_t)(addr >> 8)); spi_transfer((uint8_t)addr); spi_transfer((uint8_t)((bsb & 0x1FU) << 3)); /* RWB=0, OM=00 */ uint8_t value = spi_transfer(0x00); PORTB |= (1U << PB2); return value; } int main(void) { PORTB |= (1U << PB2) | (1U << PB0); DDRB |= (1U << PB2) | (1U << PB3) | (1U << PB5) | (1U << PB0); DDRB &= (uint8_t)~(1U << PB4); SPCR = (1U << SPE) | (1U << MSTR) | (1U << SPR0); /* Mode 0, 1 MHz */ SPSR = 0; uart_init(); PORTB &= (uint8_t)~(1U << PB0); _delay_ms(2); PORTB |= (1U << PB0); _delay_ms(100); static const char hex[] = "0123456789ABCDEF"; while (1) { uint8_t version = w5500_read_byte(0x0039, 0); uart_puts("VERSIONR=0x"); uart_tx(hex[version >> 4]); uart_tx(hex[version & 0x0F]); uart_puts(version == 0x04 ? " OK\r\n" : " ERROR\r\n"); _delay_ms(1000); } }