标题:数据类型转化的为问题
只看楼主
chen3bing
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:507
专家分:104
注 册:2008-11-12
结帖率:91.22%
已结贴  问题点数:10 回复次数:2 
数据类型转化的为问题
先贴上程序:

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);

高手指教,谢谢!
搜索更多相关主题的帖子: return the bool NULL void 
2023-02-01 14:17
阳光上的桥
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:25
帖 子:82
专家分:525
注 册:2023-1-12
得分:5 
我感觉你这样是对的,我认为void *的参数可以传各种指针,直接SERCOM6_SPI_Write(dat, 1);就可以
2023-02-03 12:43
apull
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:三体星系
等 级:版主
威 望:185
帖 子:1404
专家分:8479
注 册:2010-3-16
得分:5 
直接SERCOM6_SPI_Write(dat, 1);  就可以
2023-02-03 19:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-511204-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.075652 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved