[PIC32/SAM] ATSAMD51用·Harmony 3生成代码的问题

[复制链接]
7838|1
 楼主| 比神乐 发表于 2023-1-20 09:12 | 显示全部楼层 |阅读模式
我的开发环境MPLAB X v5.50,XC32版本为v2.40,用Harmony生成了一个SPI的程序。
生成的SPI读写函数如下:
  1. // *****************************************************************************
  2. /* Function:
  3.     bool SERCOM6_SPI_WriteRead (void* pTransmitData, size_t txSize
  4.                                         void* pReceiveData, size_t rxSize);

  5.   Summary:
  6.     Write and Read data on SERCOM SERCOM6 SPI peripheral.

  7.   Description:
  8.     This function transmits "txSize" number of bytes and receives "rxSize"
  9.     number of bytes on SERCOM SERCOM6 SPI module. Data pointed by pTransmitData is
  10.     transmitted and received data is saved in the location pointed by
  11.     pReceiveData. The function will transfer the maximum of "txSize" or "rxSize"
  12.     data units towards completion.

  13.     When "Interrupt Mode" option is unchecked in MHC, this function will be
  14.     blocking in nature.  In this mode, the function will not return until all
  15.     the requested data is transferred.  The function returns true after
  16.     transferring all the data.  This indicates that the operation has been
  17.     completed.

  18.     When "Interrupt Mode" option is selected in MHC, the function will be
  19.     non-blocking in nature.  The function returns immediately. The data transfer
  20.     process continues in the peripheral interrupt.  The application specified
  21.     transmit and receive buffer  are ownerd by the library until the data
  22.     transfer is complete and should not be modified by the application till the
  23.     transfer is complete.  Only one transfer is allowed at any time. The
  24.     Application can use a callback function or a polling function to check for
  25.     completion of the transfer. If a callback is required, this should be
  26.     registered prior to calling the SERCOM6_SPI_WriteRead() function. The
  27.     application can use the SERCOM6_SPI_IsBusy() to poll for completion.

  28.   Remarks:
  29.     Refer plib_sercom6_spi.h file for more information.
  30. */

  31. bool SERCOM6_SPI_WriteRead (void* pTransmitData, size_t txSize, void* pReceiveData, size_t rxSize)
  32. {
  33.     bool isRequestAccepted = false;
  34.     uint32_t dummyData = 0U;

  35.     /* Verify the request */
  36.     if((((txSize > 0U) && (pTransmitData != NULL)) || ((rxSize > 0U) && (pReceiveData != NULL))) && (sercom6SPIObj.transferIsBusy == false))
  37.     {
  38.         if((SERCOM6_REGS->SPIM.SERCOM_CTRLB & SERCOM_SPIM_CTRLB_CHSIZE_Msk) == (uint32_t)SPI_DATA_BITS_9)
  39.         {
  40.             /* For 9-bit transmission, the txSize and rxSize must be an even number. */
  41.             if(((txSize > 0U) && ((txSize & 0x01U) != 0U)) || ((rxSize > 0U) && ((rxSize & 0x01U) != 0U)))
  42.             {
  43.                 return isRequestAccepted;
  44.             }
  45.         }

  46.         isRequestAccepted = true;
  47.         sercom6SPIObj.txBuffer = pTransmitData;
  48.         sercom6SPIObj.rxBuffer = pReceiveData;
  49.         sercom6SPIObj.rxCount = 0U;
  50.         sercom6SPIObj.txCount = 0U;
  51.         sercom6SPIObj.dummySize = 0U;

  52.         if(pTransmitData != NULL)
  53.         {
  54.             sercom6SPIObj.txSize = txSize;
  55.         }
  56.         else
  57.         {
  58.             sercom6SPIObj.txSize = 0U;
  59.         }

  60.         if(pReceiveData != NULL)
  61.         {
  62.             sercom6SPIObj.rxSize = rxSize;
  63.         }
  64.         else
  65.         {
  66.             sercom6SPIObj.rxSize = 0U;
  67.         }

  68.         sercom6SPIObj.transferIsBusy = true;

  69.         /* Flush out any unread data in SPI read buffer */
  70.         while((SERCOM6_REGS->SPIM.SERCOM_INTFLAG & SERCOM_SPIM_INTFLAG_RXC_Msk) == SERCOM_SPIM_INTFLAG_RXC_Msk)
  71.         {
  72.             dummyData = SERCOM6_REGS->SPIM.SERCOM_DATA;
  73.             (void)dummyData;
  74.         }

  75.         SERCOM6_REGS->SPIM.SERCOM_STATUS |= SERCOM_SPIM_STATUS_BUFOVF_Msk;

  76.         SERCOM6_REGS->SPIM.SERCOM_INTFLAG |= (uint8_t)SERCOM_SPIM_INTFLAG_ERROR_Msk;

  77.         if(sercom6SPIObj.rxSize > sercom6SPIObj.txSize)
  78.         {
  79.             sercom6SPIObj.dummySize = sercom6SPIObj.rxSize - sercom6SPIObj.txSize;
  80.         }

  81.         /* Start the first write here itself, rest will happen in ISR context */
  82.         if((SERCOM6_REGS->SPIM.SERCOM_CTRLB & SERCOM_SPIM_CTRLB_CHSIZE_Msk) == (uint32_t)SPI_DATA_BITS_8)
  83.         {
  84.             if(sercom6SPIObj.txCount < sercom6SPIObj.txSize)
  85.             {
  86.                 SERCOM6_REGS->SPIM.SERCOM_DATA = *((uint8_t*)sercom6SPIObj.txBuffer);

  87.                 sercom6SPIObj.txCount++;
  88.             }
  89.             else if(sercom6SPIObj.dummySize > 0U)
  90.             {
  91.                 SERCOM6_REGS->SPIM.SERCOM_DATA = 0xFFU;

  92.                 sercom6SPIObj.dummySize--;
  93.             }
  94.             else
  95.             {
  96.                 /* Do nothing */
  97.             }
  98.         }
  99.         else
  100.         {
  101.             sercom6SPIObj.txSize >>= 1U;
  102.             sercom6SPIObj.dummySize >>= 1U;
  103.             sercom6SPIObj.rxSize >>= 1U;

  104.             if(sercom6SPIObj.txCount < sercom6SPIObj.txSize)
  105.             {
  106.                 SERCOM6_REGS->SPIM.SERCOM_DATA = *((uint16_t*)sercom6SPIObj.txBuffer) & SERCOM_SPIM_DATA_Msk;

  107.                 sercom6SPIObj.txCount++;
  108.             }
  109.             else if(sercom6SPIObj.dummySize > 0U)
  110.             {
  111.                 SERCOM6_REGS->SPIM.SERCOM_DATA = 0xFFFFU & SERCOM_SPIM_DATA_Msk;

  112.                 sercom6SPIObj.dummySize--;
  113.             }
  114.             else
  115.             {
  116.                 /* Do nothing */
  117.             }
  118.         }

  119.         if(rxSize > 0U)
  120.         {
  121.             /* Enable ReceiveComplete  */
  122.             SERCOM6_REGS->SPIM.SERCOM_INTENSET = (uint8_t)SERCOM_SPIM_INTENSET_RXC_Msk;
  123.         }
  124.         else
  125.         {
  126.             /* Enable the DataRegisterEmpty  */
  127.             SERCOM6_REGS->SPIM.SERCOM_INTENSET = (uint8_t)SERCOM_SPIM_INTENSET_DRE_Msk;
  128.         }
  129.     }

  130.     return isRequestAccepted;
  131. }
我不知道它搞得这么复杂,弄得我不知道怎么调用了。
下面还有:                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  1. bool SERCOM6_SPI_Write(void* pTransmitData, size_t txSize)
  2. {
  3.     return SERCOM6_SPI_WriteRead(pTransmitData, txSize, NULL, 0U);
  4. }

  5. bool SERCOM6_SPI_Read(void* pReceiveData, size_t rxSize)
  6. {
  7.     return SERCOM6_SPI_WriteRead(NULL, 0U, pReceiveData, rxSize);
  8. }


 楼主| 比神乐 发表于 2023-1-20 09:51 | 显示全部楼层
其实我是想搞网口,我参考STM32的例程,如下
  1. /*******************************************************************************
  2. * º¯ÊýÃû  : SPI1_Send_Byte
  3. * ÃèÊö    : SPI1·¢ËÍ1¸ö×Ö½ÚÊý¾Ý
  4. * ÊäÈë    : dat:´ý·¢Ë͵ÄÊý¾Ý
  5. * Êä³ö    : ÎÞ
  6. * ·µ»ØÖµ  : ÎÞ
  7. * ˵Ã÷    : ÎÞ
  8. *******************************************************************************/
  9. void SPI1_Send_Byte(unsigned char dat)
  10. {
  11.         SPI_I2S_SendData(SPI1,dat);//д1¸ö×Ö½ÚÊý¾Ý
  12.         while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);//µÈ´ýÊý¾Ý¼Ä´æÆ÷¿Õ
  13. }
我尝试着这么改:
  1. void SPI1_Send_Byte(unsigned char dat)
  2. {        
  3.     void* Data;
  4.     Data=(void*)dat;
  5.         SERCOM6_SPI_Write(Data, 1);
  6. }
可是编译出错:
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
../src/W5500.c: In function 'SPI1_Send_Byte':
../src/W5500.c:66:10: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
     Data=(void*)dat;

您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部