#2
阳光上的桥2023-02-03 12:43
|
先贴上程序:
bool SERCOM6_SPI_WriteRead (void* pTransmitData, size_t txSize, void* pReceiveData, size_t rxSize)
{
bool isRequestAccepted = false;
uint32_t dummyData = 0U;
/* Verify the request */
if((((txSize > 0U) && (pTransmitData != NULL)) || ((rxSize > 0U) && (pReceiveData != NULL))) && (sercom6SPIObj.transferIsBusy == false))
{
if((SERCOM6_REGS->SPIM.SERCOM_CTRLB & SERCOM_SPIM_CTRLB_CHSIZE_Msk) == (uint32_t)SPI_DATA_BITS_9)
{
/* For 9-bit transmission, the txSize and rxSize must be an even number. */
if(((txSize > 0U) && ((txSize & 0x01U) != 0U)) || ((rxSize > 0U) && ((rxSize & 0x01U) != 0U)))
{
return isRequestAccepted;
}
}
isRequestAccepted = true;
sercom6SPIObj.txBuffer = pTransmitData;
sercom6SPIObj.rxBuffer = pReceiveData;
sercom6SPIObj.rxCount = 0U;
sercom6SPIObj.txCount = 0U;
sercom6SPIObj.dummySize = 0U;
if(pTransmitData != NULL)
{
sercom6SPIObj.txSize = txSize;
}
else
{
sercom6SPIObj.txSize = 0U;
}
if(pReceiveData != NULL)
{
sercom6SPIObj.rxSize = rxSize;
}
else
{
sercom6SPIObj.rxSize = 0U;
}
sercom6SPIObj.transferIsBusy = true;
/* Flush out any unread data in SPI read buffer */
while((SERCOM6_REGS->SPIM.SERCOM_INTFLAG & SERCOM_SPIM_INTFLAG_RXC_Msk) == SERCOM_SPIM_INTFLAG_RXC_Msk)
{
dummyData = SERCOM6_REGS->SPIM.SERCOM_DATA;
(void)dummyData;
}
SERCOM6_REGS->SPIM.SERCOM_STATUS |= SERCOM_SPIM_STATUS_BUFOVF_Msk;
SERCOM6_REGS->SPIM.SERCOM_INTFLAG |= (uint8_t)SERCOM_SPIM_INTFLAG_ERROR_Msk;
if(sercom6SPIObj.rxSize > sercom6SPIObj.txSize)
{
sercom6SPIObj.dummySize = sercom6SPIObj.rxSize - sercom6SPIObj.txSize;
}
/* Start the first write here itself, rest will happen in ISR context */
if((SERCOM6_REGS->SPIM.SERCOM_CTRLB & SERCOM_SPIM_CTRLB_CHSIZE_Msk) == (uint32_t)SPI_DATA_BITS_8)
{
if(sercom6SPIObj.txCount < sercom6SPIObj.txSize)
{
SERCOM6_REGS->SPIM.SERCOM_DATA = *((uint8_t*)sercom6SPIObj.txBuffer);
sercom6SPIObj.txCount++;
}
else if(sercom6SPIObj.dummySize > 0U)
{
SERCOM6_REGS->SPIM.SERCOM_DATA = 0xFFU;
sercom6SPIObj.dummySize--;
}
else
{
/* Do nothing */
}
}
else
{
sercom6SPIObj.txSize >>= 1U;
sercom6SPIObj.dummySize >>= 1U;
sercom6SPIObj.rxSize >>= 1U;
if(sercom6SPIObj.txCount < sercom6SPIObj.txSize)
{
SERCOM6_REGS->SPIM.SERCOM_DATA = *((uint16_t*)sercom6SPIObj.txBuffer) & SERCOM_SPIM_DATA_Msk;
sercom6SPIObj.txCount++;
}
else if(sercom6SPIObj.dummySize > 0U)
{
SERCOM6_REGS->SPIM.SERCOM_DATA = 0xFFFFU & SERCOM_SPIM_DATA_Msk;
sercom6SPIObj.dummySize--;
}
else
{
/* Do nothing */
}
}
if(rxSize > 0U)
{
/* Enable ReceiveComplete */
SERCOM6_REGS->SPIM.SERCOM_INTENSET = (uint8_t)SERCOM_SPIM_INTENSET_RXC_Msk;
}
else
{
/* Enable the DataRegisterEmpty */
SERCOM6_REGS->SPIM.SERCOM_INTENSET = (uint8_t)SERCOM_SPIM_INTENSET_DRE_Msk;
}
}
return isRequestAccepted;
}
bool SERCOM6_SPI_Write(void* pTransmitData, size_t txSize)
{
return SERCOM6_SPI_WriteRead(pTransmitData, txSize, NULL, 0U);
}
bool SERCOM6_SPI_Read(void* pReceiveData, size_t rxSize)
{
return SERCOM6_SPI_WriteRead(NULL, 0U, pReceiveData, rxSize);
}
void SPI1_Send_Byte(unsigned char dat)
{
}
我想在SPI1_Send_Byte函数里调用SERCOM6_SPI_Write函数,可是SERCOM6_SPI_Write函数的参数是void*类型,那么
我这样写行不行呢?我感觉有点不对。
void* pTransmitData;
pTransmitData=(void *)(dat);
SERCOM6_SPI_Write(pTransmitData, 1);
高手指教,谢谢!