【问题标题】:stm32f429, spi dr register not write datastm32f429,spi dr寄存器不写数据
【发布时间】:2021-06-17 23:05:59
【问题描述】:

code_1

code_2

register on debug

logic analyzer

void SPI_SendData(SPI_RegDef_t *pSPIx,uint8_t *pTxBuffer , uint32_t Len) {

while(Len > 0)
{

    // 1 . wait until TXE is set ,
    while(SPI_GetFlagStatus(pSPIx, SPI_TXE_FLAG) == FLAG_RESET);

    // 2. check the DFF bit in CR1
    if( (pSPIx->CR1 & (1 << SPI_CR1_DFF) ) )
    {
        // 16 BIT DFF
        pSPIx->DR = *((uint16_t*)pTxBuffer); // dereferencing(typecasting );
        Len--;
        Len--;
        (uint16_t*)pTxBuffer++; // typecasting this pointer to uint16 type and incrementing by 2.
        /* The buffer is a uint8_t pointer type. When using the 16-bit data frame,
         * we pick up 16-bits of data, increment pointer by 2 bytes,*/
    }else
    {

        // 8 BIT DFF
         pSPIx->DR = *pTxBuffer;
        Len--;
        pTxBuffer++;
        /*
         *(( uint8_t*)&hspi->Instance->DR) = (*pData);
            pData += sizeof(uint8_t);
            hspi->TxXferCount--;

         */
    }

}

}

我明白了,MOSI 总是在逻辑分析仪上发送 255 但数据错误。

【问题讨论】:

    标签: stm32 stm32f4discovery stm32f4


    【解决方案1】:

    (uint16_t*)pTxBuffer++; 将指针增加 1 个字节,而不是您在评论中说希望它增加的两个字节。

    如果您想通过转换为半字指针并递增来实现,那么您需要执行以下操作:

    pTxBuffer = (uint8_t*)(((uint16_t*)pTxBuffer) + 1);
    

    但这只是pTxBuffer += 2;的愚蠢说法

    确实在循环中包含 if 是没有意义的,因为 DFF 位的值不会改变,除非你写入它,而你不会。我建议你在单词上写一个循环,在字节上写一个循环,并将 if 放在顶层。

    【讨论】:

      猜你喜欢
      • 2014-10-15
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 2019-06-21
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多