/* * 函数:user_spi_read_byte * 说明:SPI读取一个字节 */ u8 ICACHE_FLASH_ATTR user_spi_read_byte(void) { u8 read = 0; #if defined(HARD_SPI) u32 recv_data[1]; SpiData spiData; spiData.cmd = MASTER_READ_DATA_FROM_SLAVE_CMD; spiData.cmdLen = 0; spiData.addr = NULL; spiData.addrLen = 0; spiData.data = recv_data; spiData.dataLen = 1; SPIMasterRecvData(SpiNum_HSPI, &spiData); read = (u8)(recv_data[0]&0xFF); #elif defined(SOFT_SPI) u8 i; CS_0(); for (i = 0; i < 8; i++){ SCK_0(); //user_spi_delay_us(50); read = read<<1; if (MISO_IS_HIGH()){ read++; } SCK_1(); //user_spi_delay_us(50); } CS_1(); #endif return read; }
/* * 函数:user_spi_read_4byte * 说明:SPI读取4个字节 */ u32 ICACHE_FLASH_ATTR user_spi_read_4byte(void) { u32 read; #if defined(HARD_SPI) u32 recv_data[1]; SpiData spiData; spiData.cmd = MASTER_READ_DATA_FROM_SLAVE_CMD; spiData.cmdLen = 0; spiData.addr = NULL; spiData.addrLen = 0; spiData.data = recv_data; spiData.dataLen = 4; SPIMasterRecvData(SpiNum_HSPI, &spiData); read = recv_data[0]; #elif defined(SOFT_SPI) CS_0(); read = user_spi_read_byte(); read <<= 8; read += user_spi_read_byte(); read <<= 8; read += user_spi_read_byte(); read <<= 8; read += user_spi_read_byte(); CS_1(); #endif return read; }
static uint8 fn_spi_read_byte(uint8 addr) { uint8 read_bytes; os_printf("\r\n Master receive 24 bytes data from slave(8266)\r\n"); spiData.cmd = MASTER_READ_DATA_FROM_SLAVE_CMD; spiData.cmdLen = 1; spiData.addr = &addr; spiData.addrLen = 1; spiData.data = &read_bytes; spiData.dataLen = 1; SPIMasterRecvData(SpiNum_HSPI, &spiData); os_printf("Recv Slave data0[0x%x]\r\n", read_bytes); uint32 value = SPIMasterRecvStatus(SpiNum_HSPI); os_printf("\r\n Master read slave(sx1278) status[0x%02x]\r\n", value); SPIMasterSendStatus(SpiNum_HSPI, 0x99); os_printf("\r\n Master write status[0x99] to slavue(8266).\r\n"); // SHOWSPIREG(SpiNum_HSPI); #if 0 uint8 i, j; j = 0; for (i = 0; i < 8; i++) { RF_CKL_H; j = (j << 1); // shift 1 place to the left or shift in 0 if (SX1278_SDO) // check to see if bit is high { j = j | 0x01; // if high, make bit high } // toggle clock high RF_CKL_L; // toggle clock low } return j; // toggle clock low // #endif }