Przeglądaj źródła

add:非阻塞的SPI和I2C传输接口

alienwalker 4 lat temu
rodzic
commit
b14a7fb756

+ 21 - 0
application/src/luat_i2c_air105.c

@@ -68,3 +68,24 @@ int luat_i2c_transfer(int id, int addr, uint8_t *reg, size_t reg_len, uint8_t *b
 		return I2C_BlockWrite(id, addr, (const uint8_t *)buff, len, 100, NULL, NULL);
 	}
 }
+
+int luat_i2c_no_block_transfer(int id, int addr, uint8_t is_read, uint8_t *reg, size_t reg_len, uint8_t *buff, size_t len, uint16_t Toms, void *CB, void *pParam) {
+	int32_t Result;
+	if (!I2C_WaitResult(id, &Result)) {
+		return -1;
+	}
+	I2C_Prepare(id, addr, 1, CB, pParam);
+	if (reg && reg_len)
+	{
+		I2C_MasterXfer(id, I2C_OP_READ_REG, reg, reg_len, buff, len, Toms);
+	}
+	else if (is_read)
+	{
+		I2C_MasterXfer(id, I2C_OP_READ, NULL, 0, buff, len, Toms);
+	}
+	else
+	{
+		I2C_MasterXfer(id, I2C_OP_WRITE, NULL, 0, buff, len, Toms);
+	}
+	return 0;
+}

+ 8 - 0
application/src/luat_spi_air105.c

@@ -202,6 +202,14 @@ int luat_spi_send(int spi_id, const char* send_buf, size_t length) {
     return length;
 }
 
+int luat_spi_no_block_transfer(int spi_id, uint8_t *tx_buff, uint8_t *rx_buff, size_t len, void *CB, void *pParam)
+{
+	if (SPI_IsTransferBusy(luat_spi[spi_id].id)) return -1;
+	SPI_SetCallbackFun(luat_spi[spi_id].id, CB, pParam);
+	return SPI_Transfer(luat_spi[spi_id].id, tx_buff, rx_buff, len, 0);
+
+}
+
 int luat_lcd_draw_no_block(luat_lcd_conf_t* conf, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, luat_color_t* color, uint8_t last_flush)
 {
 	uint32_t retry_cnt = 0;

+ 3 - 3
bsp/air105/hal/core_i2c.c

@@ -70,7 +70,7 @@ static void prvI2C_Done(uint8_t I2CID, int32_t Result)
 #ifdef __BUILD_OS__
 	if (prvI2C.IsBlockMode) OS_MutexRelease(prvI2C.Sem);
 #endif
-	prvI2C.Callback(I2C_ID0, prvI2C.pParam);
+	prvI2C.Callback(((-prvI2C.Result) << 16) | I2C_ID0, prvI2C.pParam);
 
 }
 
@@ -269,7 +269,7 @@ void I2C_MasterXfer(uint8_t I2CID, uint8_t Operate, uint8_t *RegAddress, uint32_
 		I2C->IC_ENABLE |= I2C_IC_ENABLE_ABORT;
 		prvI2C.IsBusy = 0;
 		prvI2C.Result = -ERROR_OPERATION_FAILED;
-		prvI2C.Callback(I2C_ID0, prvI2C.pParam);
+		prvI2C.Callback(((-prvI2C.Result) << 16) | I2C_ID0, prvI2C.pParam);
 		while(I2C->IC_ENABLE & I2C_IC_ENABLE_ABORT){;}
 	}
     ISR_SetHandler(prvI2C.IrqLine, I2C_IrqHandle, NULL);
@@ -331,7 +331,7 @@ void I2C_MasterXfer(uint8_t I2CID, uint8_t Operate, uint8_t *RegAddress, uint32_
 		Timer_Stop(prvI2C.ToTimer);
 		prvI2C.IsBusy = 0;
 		prvI2C.Result = -ERROR_PARAM_INVALID;
-		prvI2C.Callback(I2C_ID0, prvI2C.pParam);
+		prvI2C.Callback(((-prvI2C.Result) << 16) | I2C_ID0, prvI2C.pParam);
 		return;
 	}
 

+ 1 - 1
bsp/air105/hal/core_spi.c

@@ -579,7 +579,7 @@ int32_t SPI_Transfer(uint8_t SpiID, const uint8_t *TxData, uint8_t *RxData, uint
 		return -ERROR_DEVICE_BUSY;
 	}
 	prvSPI[SpiID].IsBusy = 1;
-
+	prvSPI[SpiID].IsBlockMode = 0;
 	uint32_t RxLevel, i, TxLen;
 	Buffer_StaticInit(&prvSPI[SpiID].TxBuf, TxData, Len);
 	Buffer_StaticInit(&prvSPI[SpiID].RxBuf, RxData, Len);