用参数较少的函数替换参数较多的函数

<target>

  # 用法就是通用函数特殊化

 

<具体>

  # 函数转换

#define spi_dma_txd(SrcAddress, DataLength)\
        spi_dma_start(SPI1, TXD, DMA2_Stream5, SrcAddress, (uint32_t)&SPI1->DR, DataLength)

  # 函数具体实现

 1 void spi_dma_start(SPI_TypeDef* SPIx, trans_dir_e dir, DMA_Stream_TypeDef * DMA_streamx, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
 2 {
 3     /* spi dma configuration */
 4     DMA_start_IT(DMA_streamx, SrcAddress, DstAddress, DataLength);
 5     
 6     /* Enable SPI peripheral */
 7     SPI_Cmd(SPIx, ENABLE);
 8     
 9     /* ÅäÖÃSPIÏòDMA·¢³ö/½ÓÊÕÇëÇó */
10     if(TXD == dir)
11     {
12         SPI_DMACmd(SPIx, SPI_DMAReq_Tx, ENABLE);
13     }
14     else
15     {
16         SPI_DMACmd(SPIx, SPI_DMAReq_Rx, ENABLE);
17     }    
18 }

 

<总结>

  # 通用函数特殊化,其中针对特定使用场合,可固定通用函数中的部分参数,剩余随机参数保留

  # 对应关系为:参数名一一对应

posted @ 2018-02-28 12:52  壹点灵异  阅读(235)  评论(0编辑  收藏  举报