guanjiao
2018-05-16 df737bb92e7ff5f5c7495c06d0e231338805a86c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "Spi.h"
 
void Spi_Init(void)
{
    SPI_InitTypeDef SPI_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    
    /* Enable GPIO clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
    /* Enable SPI clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
    
    /* SPI GPIO setup */
    // SPIx SCK and MOSI pin setup
    GPIO_InitStructure.GPIO_Pin = SPIx_SCK | SPIx_MOSI;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(SPIx_GPIO, &GPIO_InitStructure);
 
    // SPIx MISO pin setup
    GPIO_InitStructure.GPIO_Pin = SPIx_MISO;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
    GPIO_Init(SPIx_GPIO, &GPIO_InitStructure);
 
    // SPIx CS pin setup
    GPIO_InitStructure.GPIO_Pin = SPIx_CS;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(SPIx_CS_GPIO, &GPIO_InitStructure);
    
    // Set CS high
    GPIO_SetBits(SPIx_CS_GPIO, SPIx_CS);
        
 
    SPI_I2S_DeInit(SPIx);
 
    // SPIx Mode setup
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPIx_CPOL;     
    SPI_InitStructure.SPI_CPHA = SPIx_CPHA;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPIx_PRESCALER_SLOW;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
 
    SPI_Init(SPIx, &SPI_InitStructure);
 
    // Disable SPIx SS Output
    SPI_SSOutputCmd(SPIx, DISABLE);
 
    // Enable SPIx
    SPI_Cmd(SPIx, ENABLE);
}
 
 
void Spi_ChangePrescaler(uint16_t scaling_factor)
{
    uint16_t tmpreg = 0;
 
    /* Get the SPIx CR1 value */
    tmpreg = SPIx->CR1;
 
    /*clear the scaling bits*/
    tmpreg &= 0xFFC7;
 
    /*set the scaling bits*/
    tmpreg |= scaling_factor;
 
    /* Write to SPIx CR1 */
    SPIx->CR1 = tmpreg;
}
 
/*! ------------------------------------------------------------------------------------------------------------------
 * Function: writetospi()
 *
 * Low level abstract function to write to the SPI
 * Takes two separate byte buffers for write header and write data
 * returns 0 for success, or -1 for error
 */
int writetospi_serial
(
    uint16_t       headerLength,
    const uint8_t *headerBuffer,
    uint32_t       bodylength,
    const uint8_t *bodyBuffer
)
{
 
    int i = 0;
 
    decaIrqStatus_t  stat ;
 
    stat = decamutexon() ;
 
    SPIx_CS_GPIO->BRR = SPIx_CS;
 
    for(i = 0; i < headerLength; i++)
    {
        SPIx->DR = headerBuffer[i];
 
        while ((SPIx->SR & SPI_I2S_FLAG_RXNE) == (uint16_t)RESET);
 
        SPIx->DR ;
    }
 
    for(i = 0; i < bodylength; i++)
    {
        SPIx->DR = bodyBuffer[i];
 
        while((SPIx->SR & SPI_I2S_FLAG_RXNE) == (uint16_t)RESET);
 
        SPIx->DR ;
    }
 
    SPIx_CS_GPIO->BSRR = SPIx_CS;
 
    decamutexoff(stat) ;
 
    return 0;
} // end writetospi()
 
 
/*! ------------------------------------------------------------------------------------------------------------------
 * Function: readfromspi()
 *
 * Low level abstract function to read from the SPI
 * Takes two separate byte buffers for write header and read data
 * returns the offset into read buffer where first byte of read data may be found,
 * or returns -1 if there was an error
 */
int readfromspi_serial
(
    uint16_t       headerLength,
    const uint8_t *headerBuffer,
    uint32_t       readlength,
    uint8_t       *readBuffer
)
{
 
    int i = 0;
 
    decaIrqStatus_t  stat ;
 
    stat = decamutexon() ;
 
    /* Wait for SPIx Tx buffer empty */
    //while (port_SPIx_busy_sending());
 
    SPIx_CS_GPIO->BRR = SPIx_CS;
 
    for(i = 0; i < headerLength; i++)
    {
        SPIx->DR = headerBuffer[i];
 
        while((SPIx->SR & SPI_I2S_FLAG_RXNE) == (uint16_t)RESET);
 
        readBuffer[0] = SPIx->DR ; // Dummy read as we write the header
    }
 
    for(i = 0; i < readlength; i++)
    {
        SPIx->DR = 0;  // Dummy write as we read the message body
 
        while((SPIx->SR & SPI_I2S_FLAG_RXNE) == (uint16_t)RESET);
 
        readBuffer[i] = SPIx->DR ;//port_SPIx_receive_data(); //this clears RXNE bit
    }
 
    SPIx_CS_GPIO->BSRR = SPIx_CS;
 
    decamutexoff(stat) ;
 
    return 0;
} // end readfromspi()