WXK
2024-09-18 05e2e954bd127de378a9d1dfbb0ed95d725aad63
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**************************************************************************//**
 * @file     spi.c
 * @version  V1.10
 * $Revision: 2 $
 * $Date:    19/10/22  17:18 $
 * @brief    PAN series SPI driver source file
 *
 * @note
 * Copyright (C) 2016 Panchip Technology Corp. All rights reserved.
*****************************************************************************/
#include "PanSeries.h"
#include "pan_spi.h"
 
/** @addtogroup PAN_Device_Driver PAN Device Driver
  @{
*/
 
/** @addtogroup PAN_SPI_Driver SPI Driver
  @{
*/
 
/** @addtogroup PAN_SPI_EXPORTED_FUNCTIONS SPI Exported Functions
  @{
*/
 
/**
  * @brief  Initializes the SPIx peripheral according to the specified
  *         parameters in the SPI_InitStruct .
  * @param  SPIx: where x can be 1, 2 to select the
  *         SPI peripheral.
  * @param  SPI_InitStruct: pointer to a SPI_InitTypeDef structure that contains
  *         the configuration information for the specified SPI peripheral.
  * @retval None
  */
void SPI_Init(SPI_T* SPIx, SPI_InitTypeDef* SPI_InitStruct)
{
    uint32_t tmpreg = 0;
 
    if(SPI_IsSpiEnabled(SPIx))
        SPI_DisableSpi(SPIx);
 
    tmpreg = SPIx->CR0;
 
    tmpreg &= ~(SPI_CR0_SPH_Msk | SPI_CR0_SPO_Msk  | SPI_CR0_DSS_Msk);
    tmpreg |= (
               ((SPI_InitStruct->SPI_CPHA           << SPI_CR0_SPH_Pos) & SPI_CR0_SPH_Msk )  |   \
                ((SPI_InitStruct->SPI_CPOL          << SPI_CR0_SPO_Pos) & SPI_CR0_SPO_Msk )  |   \
                ((SPI_InitStruct->SPI_dataFrameSize << SPI_CR0_DSS_Pos) & SPI_CR0_DSS_Msk )      \
              );
    tmpreg &= ~(SPI_CR0_FRF_Msk);
    tmpreg |= (SPI_InitStruct->SPI_format << SPI_CR0_FRF_Pos);
    tmpreg &= ~(SPI_CR0_SCR_Msk);
    tmpreg |= ((SPI_InitStruct->SPI_baudRateDiv-1) << SPI_CR0_SCR_Pos);   //Fixed SCR to 0, only use CPSR to config baudrate
 
    SPIx->CR0 = tmpreg;
 
    //Baudrate Config, expect_baudrate = apb_clk / ((1 + SCR) * CPSR)
    SPIx->CPSR = 2;//SPI_InitStruct->SPI_baudRateDiv;
 
    //Role Select
    if(SPI_InitStruct->SPI_role == SPI_RoleSlave)
    {
        SPIx->CR1 |= SPI_CR1_MS_Msk;
    }
    else
    {
        SPIx->CR1 &= ~SPI_CR1_MS_Msk;
    }
}
 
 
/*@}*/ /* end of group PAN_SPI_EXPORTED_FUNCTIONS */
 
/*@}*/ /* end of group PAN_SPI_Driver */
 
/*@}*/ /* end of group PAN_Device_Driver */
 
/*** (C) COPYRIGHT 2016 Panchip Technology Corp. ***/