#include "stm32f0xx_hal.h" #include "w5500_port.h" #include "Globaldef.h" #include "KMachine.h" /** * @brief enter critical section * @param none * @return none */ static void w5500_cris_enter(void) { __disable_irq(); } /** * @brief exit critical section * @param none * @return none */ static void w5500_cris_exit(void) { __enable_irq(); } #define setW5500CLK_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_3) #define setW5500CLK_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_3) #define SetW5500SER_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_5) #define SetW5500SER_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_5) #define GetW5500SER() LL_GPIO_IsInputPinSet(GPIOB,LL_GPIO_PIN_4) /** * @brief select chip * @param none * @return none */ int nIndexCount=0; static void w5500_cs_select(void) { // setW5500CLK_1(); LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_15); nIndexCount=0; } /** * @brief deselect chip * @param none * @return none */ static void w5500_cs_deselect(void) { LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_15); } #ifdef SOFT_SPI static uint8_t w5500_spi_readbyte_sw(void) { uint8_t value=0; unsigned int mask1=0x0080; // volatile uint32_t * p1=&GPIOA->IDR; SetW5500SER_1(); for (;mask1;) { setW5500CLK_0(); //*p1=LL_GPIO_PIN_13; setW5500CLK_1(); if (GetW5500SER()) value |= mask1; mask1>>=1; // __nop(); //*p2=LL_GPIO_PIN_13; } KMem.SDB[128+nIndexCount] = value; nIndexCount++; if (nIndexCount>40) {nIndexCount=0;} return value; } static void w5500_spi_writebyte_sw(uint8_t wb) { unsigned int mask1=0x0080; //volatile uint32_t * p1 = &GPIOB->BRR; //volatile uint32_t * p2 = &GPIOB->BSRR; KMem.SDB[128+nIndexCount] = wb; nIndexCount++; if (nIndexCount>40) {nIndexCount=0;} for (;mask1;) { setW5500CLK_0(); //*p1=LL_GPIO_PIN_13; if (wb&mask1) {SetW5500SER_1();} else {SetW5500SER_0();} mask1>>=1; setW5500CLK_1(); // __nop(); //*p2=LL_GPIO_PIN_13; } } #else /** * @brief read byte in SPI interface * @param none * @return the value of the byte read */ #define TIMEOUT 50 static uint8_t w5500_spi_readbyte(void) { uint8_t value; int timeout_cnt= 0; while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) { timeout_cnt++; if (timeout_cnt>TIMEOUT) break;} KMem.SDT[88]=timeout_cnt; value = LL_SPI_ReceiveData8( SPI1); LL_SPI_TransmitData8(SPI1,0x5a); timeout_cnt= 0; while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) {timeout_cnt++; if (timeout_cnt>TIMEOUT) break; } KMem.SDT[89]=timeout_cnt; timeout_cnt= 0; while (LL_SPI_IsActiveFlag_RXNE(SPI1) == RESET) { timeout_cnt++; if (timeout_cnt>TIMEOUT) break;} KMem.SDT[89]+=timeout_cnt; value = LL_SPI_ReceiveData8( SPI1); KMem.SDB[128+nIndexCount] = value; nIndexCount++; if (nIndexCount>40) {nIndexCount=0;} return value; } /** * @brief write byte in SPI interface * @param wb the value to write * @return none */ static void w5500_spi_writebyte(uint8_t wb) { int timeout_cnt= 0; uint8_t value; while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) {timeout_cnt++; if (timeout_cnt>TIMEOUT) break; } KMem.SDT[90]=timeout_cnt; LL_SPI_TransmitData8(SPI1,wb); timeout_cnt= 0; while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) {timeout_cnt++; if (timeout_cnt>TIMEOUT) break; } KMem.SDT[91]=timeout_cnt; timeout_cnt= 0; while (LL_SPI_IsActiveFlag_RXNE(SPI1) == RESET) { timeout_cnt++; if (timeout_cnt>TIMEOUT) break;} KMem.SDT[91]+=timeout_cnt; value = LL_SPI_ReceiveData8( SPI1); } #endif /** * @brief burst read byte in SPI interface * @param pBuf pointer of data buf * @param len number of bytes to read * @return none */ static void w5500_spi_readburst(uint8_t* pBuf, uint16_t len) { if (!pBuf) { return; } uint8_t value; for (int i=0;i