/************************************************* // W5500_SPI.c // by Wastl *************************************************/ #include #include #include #include "W5500_SPI.h" // W5500 Data Modes #define OM10_VDM 00 /* variable data mode */ #define OM10_FDM1 01 /* fixed data mode, 1 byte */ #define OM10_FDM2 10 /* fixed data mode, 2 byte */ #define OM10_FDM3 11 /* fixed data mode, 4 byte */ #define OPMODE_SINGLE_READ (0x00 | OM10_FDM1) // BSB = %00000, RW=0(read), OM =%00 #define OPMODE_SINGLE_WRITE (0x04 | OM10_FDM1) // BSB = %00000, RW=1(write), OM =%00 #define OPMODE_READ (0x00 | OM10_VDM) // BSB = %00000, RW=0(read), OM =%00 #define OPMODE_WRITE (0x04 | OM10_VDM) // BSB = %00000, RW=1(write), OM =%00 /* FUNCTION ************************************/ void W5500_CS_low (void) { // your code for I/O port P1OUT &= ~BIT1; } /* --- W5500_CS_low --- */ /* FUNCTION ************************************/ void W5500_CS_high (void) { // your code for I/O port P1OUT |= BIT1; } /* --- W5500_CS_low --- */ /* FUNCTION ************************************/ uint8_t W5500_SPI_transfer (uint8_t val) { uint8_t ret_val; while (!(IFG2 & UCB0TXIFG)); UCB0TXBUF = val; while (!(IFG2 & UCB0RXIFG)); ret_val = UCB0RXBUF; return ret_val; } /* --- W5500_SPI_transfer --- */ /* FUNCTION ************************************/ uint8_t W5500_spi_read (uint32_t addr) /* * Single byte read from W5500 register ***********************************************/ { uint8_t rd_val; W5500_CS_low(); // CS=0, SPI start W5500_SPI_transfer ( (uint8_t)(addr >> 16) ); W5500_SPI_transfer ( (uint8_t)(addr >> 8) ); W5500_SPI_transfer ( ((uint8_t)addr) | OPMODE_SINGLE_READ); rd_val = W5500_SPI_transfer (0); W5500_CS_high(); // CS=1, SPI end return rd_val; } /* --- W5500_spi_read --- */ /* FUNCTION ************************************/ void W5500_spi_write (uint32_t addr, uint8_t val) /* * Single byte write to W5500 register ***********************************************/ { W5500_CS_low(); // CS=0, SPI start W5500_SPI_transfer ( (uint8_t)(addr >> 16) ); W5500_SPI_transfer ( (uint8_t)(addr >> 8) ); W5500_SPI_transfer ( ((uint8_t)addr) | OPMODE_SINGLE_WRITE); W5500_SPI_transfer (val); W5500_CS_high(); // CS=1, SPI end } /* --- W5500_spi_write --- */ /* FUNCTION ************************************/ void W5500_spi_read_buf (uint32_t addr, uint8_t* p_buf, uint16_t numbytes) /* * read from W5500 memory (Buffer) * ***********************************************/ { W5500_CS_low(); // CS=0, SPI start W5500_SPI_transfer(addr >> 16); W5500_SPI_transfer(addr >> 8); W5500_SPI_transfer((uint8_t)addr | OPMODE_READ); while (numbytes > 0) { *p_buf = W5500_SPI_transfer(0); p_buf++; numbytes--; } W5500_CS_high(); // CS=1, SPI end } /* --- W5500_spi_read_buf --- */ /* FUNCTION ************************************/ void W5500_spi_write_buf (uint32_t addr, uint8_t* p_buf, uint16_t numbytes) /* * write into W5500 memory (Buffer) * ***********************************************/ { W5500_CS_low(); // CS=0, SPI start W5500_SPI_transfer ( (uint8_t)(addr >> 16) ); W5500_SPI_transfer ( (uint8_t)(addr >> 8) ); W5500_SPI_transfer ( ((uint8_t)addr) | OPMODE_WRITE); while (numbytes > 0) { W5500_SPI_transfer (*p_buf); p_buf++; numbytes--; } W5500_CS_high(); // CS=1, SPI end } /* --- W5500_spi_write_buf --- */ /* ------------------------------------------ */ /* -------- End of file W5500_SPI.c --------- */ /* ------------------------------------------ */