aboutsummaryrefslogtreecommitdiff
path: root/src/mcu/peripheral/rtl876x_spi.c
blob: f831f064410dbc78ba3ad45806bd61970bc8a85a (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/**
*********************************************************************************************************
*               Copyright(c) 2015, Realtek Semiconductor Corporation. All rights reserved.
**********************************************************************************************************
* @file     rtl876x_spi.c
* @brief    This file provides all the Spi firmware functions.
* @details
* @author   elliot chen
* @date     2015-05-06
* @version  v0.1
*********************************************************************************************************
*/

/* Includes ------------------------------------------------------------------*/
#include "rtl876x_spi.h"
#include "rtl876x_rcc.h"

/**
  * @brief  Deinitializes the SPIx peripheral registers to their default reset values.
  * @param  SPIx: where x can be 0 or 1 to select the SPI peripheral.
  * @retval None
  */
void SPI_DeInit(SPI_TypeDef *SPIx)
{
    /* Check the parameters */
    assert_param(IS_SPI_ALL_PERIPH(SPIx));

    /*Disable SPI clock */
    if (SPIx == SPI0)
    {
        RCC_PeriphClockCmd(APBPeriph_SPI0, APBPeriph_SPI0_CLOCK, DISABLE);
    }
    else
    {
        RCC_PeriphClockCmd(APBPeriph_SPI1, APBPeriph_SPI1_CLOCK, DISABLE);
    }
}

/**
  * @brief  Initializes the SPIx peripheral according to the specified
  *   parameters in the SPI_InitStruct.
  * @param  SPIx: where x can be 0 or 1 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_TypeDef *SPIx, SPI_InitTypeDef *SPI_InitStruct)
{
    /* Check the parameters */
    assert_param(IS_SPI_ALL_PERIPH(SPIx));

    /* Check the SPI parameters */
    assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));
    assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));
    assert_param(IS_SPI_DATASIZE(SPI_InitStruct->SPI_DataSize));
    assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));
    assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));
    assert_param(IS_SPI_FRAME_FORMAT(SPI_InitStruct->SPI_FrameFormat));

    /* Disable SPI device before change configuration */
    SPIx->SSIENR &= ~0x01;

    /*Set SPI Rx sample delay */
    SPIx->RX_SAMPLE_DLY = 0x01;

    /* Configure SPI0 mode if select SPI0 */
    if (SPIx == SPI0)
    {
        if (SPI_InitStruct->SPI_Mode == SPI_Mode_Master)
        {
            /*Enable SPI0 master mode*/
            *((volatile uint32_t *)0x40000308) = *((volatile uint32_t *)0x40000308) | BIT(0);
            /* configure SPI parameters */
            SPIx->CTRLR0 = (SPI_InitStruct->SPI_DataSize << 16)\
                           | (SPI_InitStruct->SPI_FrameFormat << 4) \
                           | (SPI_InitStruct->SPI_CPHA << 6)\
                           | (SPI_InitStruct->SPI_CPOL << 7) \
                           | (SPI_InitStruct->SPI_Direction << 8) \
                           | (SPI_InitStruct->SPI_ToggleEn << 24);
        }
        else
        {
            /*Enable SPI0 slave mode*/
            *((volatile uint32_t *)0x40000308) = *((volatile uint32_t *)0x40000308) & (~(BIT(0)));
            /* configure SPI parameters */
            SPIx->CTRLR0 = (SPI_InitStruct->SPI_DataSize)
                           | (SPI_InitStruct->SPI_FrameFormat << 4)
                           | (SPI_InitStruct->SPI_CPHA << 6)
                           | (SPI_InitStruct->SPI_CPOL << 7)
                           | (SPI_InitStruct->SPI_Direction << 8)
                           | (SPI_InitStruct->SPI_SwapTxBitEn << 22)
                           | (SPI_InitStruct->SPI_SwapRxBitEn << 24)
                           | (SPI_InitStruct->SPI_SwapTxByteEn << 21)
                           | (SPI_InitStruct->SPI_SwapRxByteEn << 23)
                           | (SPI_InitStruct->SPI_ToggleEn << 31);
        }

    }
    else if (SPIx == SPI1)
    {
        /* configure SPI parameters */
        SPIx->CTRLR0 = (SPI_InitStruct->SPI_DataSize << 16)
                       | (SPI_InitStruct->SPI_FrameFormat << 4)
                       | (SPI_InitStruct->SPI_CPHA << 6)
                       | (SPI_InitStruct->SPI_CPOL << 7)
                       | (SPI_InitStruct->SPI_Direction << 8)
                       | (SPI_InitStruct->SPI_ToggleEn << 24);
    }

    /* configure SPI clock speed  in master mode or  enable slave output in slave mode */
    if (SPI_InitStruct->SPI_Mode == SPI_Mode_Master)
    {
        SPIx->BAUDR = (SPI_InitStruct->SPI_BaudRatePrescaler) % 0xFFFF;
        /* Enable slave Select function in master mode */
        SPIx->SER |= BIT(0);
    }
    else
    {
        /* Enable slave output */
        SPIx->CTRLR0 &= ~(BIT(10));
    }

    /*set SPI Tx and Rx threshold level ,below this level or equal this level would trigger Tx and Rx FIFO empty interrupt */
    SPIx->TXFTLR = SPI_InitStruct->SPI_TxThresholdLevel;
    SPIx->RXFTLR = SPI_InitStruct->SPI_RxThresholdLevel;

    /* mask SPI interrupt in REG_DW_SSI_IMR */
    SPIx->IMR = 0;

    /* set read length in SPI EEPROM & rx only mode */
    if ((SPI_InitStruct->SPI_Direction == 0x02) || (SPI_InitStruct->SPI_Direction == 0x03))
    {
        SPIx->CTRLR1 = SPI_InitStruct->SPI_NDF;
    }
#if 1

    /* Config SPI dma mode */
    SPIx->DMACR = ((SPI_InitStruct->SPI_RxDmaEn)\
                   | ((SPI_InitStruct->SPI_TxDmaEn) << 1));

    /* Config SPI waterlevel */
    SPIx->DMARDLR = SPI_InitStruct->SPI_RxWaterlevel;
    SPIx->DMATDLR = SPI_InitStruct->SPI_TxWaterlevel;
#endif
}

/**
  * @brief  Fills each SPI_InitStruct member with its default value.
  * @param  SPI_InitStruct : pointer to a SPI_InitTypeDef structure which will be initialized.
  * @retval None
  */
void SPI_StructInit(SPI_InitTypeDef *SPI_InitStruct)
{
    SPI_InitStruct->SPI_Mode              = SPI_Mode_Master;
    SPI_InitStruct->SPI_DataSize          = SPI_DataSize_8b;    /* 8-bit serial data transfer */
    SPI_InitStruct->SPI_FrameFormat       = 0;                  /* 0:FRF_MOTOROLA_SPI; 1,2,3:Reserved */
    SPI_InitStruct->SPI_Direction         =
        0;                  /* 0:TX and RX Mode; 1:TX Only Mode; 2:RX Only Mode;
                                                                   3:EEPROM Read Mode */
    SPI_InitStruct->SPI_CPOL              =
        1;                  /* 0:inactive state of clock is low; 1:inactive
                                                                   state of clock is high */
    SPI_InitStruct->SPI_CPHA              =
        1;                  /* 1:Serial clock toggles in first of first data bit;
                                                                   0:Serial clock toggles in middle of first data bit*/
    SPI_InitStruct->SPI_BaudRatePrescaler =
        SPI_BaudRatePrescaler_128;             /* Speed = SPI Clock source/ SPI_ClkDIV*/
    SPI_InitStruct->SPI_TxThresholdLevel  = 1;                  /* Transmit FIFO Threshold */
    SPI_InitStruct->SPI_RxThresholdLevel  = 0;                  /* Receive FIFO Threshold */
    SPI_InitStruct->SPI_NDF               = 1;
    SPI_InitStruct->SPI_SwapRxBitEn       = SPI_SWAP_DISABLE;       /* reverse the rx bit or not */
    SPI_InitStruct->SPI_SwapTxBitEn       = SPI_SWAP_DISABLE;
    SPI_InitStruct->SPI_SwapRxByteEn      = SPI_SWAP_DISABLE;
    SPI_InitStruct->SPI_SwapTxByteEn      = SPI_SWAP_DISABLE;
    SPI_InitStruct->SPI_ToggleEn          = DISABLE;

    SPI_InitStruct->SPI_RxDmaEn           = DISABLE;
    SPI_InitStruct->SPI_TxDmaEn           = DISABLE;
    SPI_InitStruct->SPI_RxWaterlevel      =
        1;                   /* SPI Rx waterlevel, should be less than fifo threshold, the best value is
                                                                    GDMA Msize */
    SPI_InitStruct->SPI_TxWaterlevel      =
        35;                  /* SPI Tx waterlevel, should be less than fifo threshold, the best value is
                                                                    SPI FIFO Depth minus GDMA Msize */
}

/**
  * @brief  Enables or disables the specified SPI peripheral.
  * @param  SPIx: where x can be 0 or 1 to select the SPI peripheral.
  * @param  NewState: new state of the SPIx peripheral.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void SPI_Cmd(SPI_TypeDef *SPIx, FunctionalState NewState)
{
    /* Check the parameters */
    assert_param(IS_SPI_ALL_PERIPH(SPIx));
    assert_param(IS_FUNCTIONAL_STATE(NewState));

    if (NewState != DISABLE)
    {
        /* Enable the selected SPI peripheral */
        SPIx->SSIENR |= 0x01;
    }
    else
    {
        /* Disable the selected SPI peripheral */
        SPIx->SSIENR &= ~0x01;
    }
}

/**
  * @brief  Transmits a number of bytes through the SPIx peripheral.
  * @param  SPIx: where x can be 0 or 1
  * @param  Data : bytes to be transmitted.
  * @retval None
  */
void SPI_SendBuffer(SPI_TypeDef *SPIx, uint8_t *pBuf, uint16_t len)
{
    uint16_t i = 0;

    /* Check the parameters */
    assert_param(IS_SPI_ALL_PERIPH(SPIx));

    for (i = 0; i < len; i++)
    {
        SPIx->DR[0] = (*pBuf++);
        /*read TFNF bit status in SR register: SET is Tx FIFO is not full*/
        while (!(SPIx->SR & BIT(1)));
    }
}

/**
  * @brief  Transmits a number of words through the SPIx peripheral.
  * @param  SPIx: where x can be 0 or 1
  * @param  Data : words to be transmitted.
  * @retval None
  */
void SPI_SendWord(SPI_TypeDef *SPIx, uint32_t *pBuf, uint16_t len)
{
    uint16_t i = 0;

    /* Check the parameters */
    assert_param(IS_SPI_ALL_PERIPH(SPIx));

    for (i = 0; i < len; i++)
    {
        SPIx->DR[0] = (*pBuf++);
        /*read TFNF bit status in SR register: SET is Tx FIFO is not full*/
        while (!(SPIx->SR & BIT(1)));
    }
}

/**
  * @brief  Transmits a number of halfWords through the SPIx peripheral.
  * @param  SPIx: where x can be 0 or 1
  * @param  Data : Halfwords to be transmitted.
  * @retval None
  */
void SPI_SendHalfWord(SPI_TypeDef *SPIx, uint16_t *pBuf, uint16_t len)
{
    uint16_t i = 0;

    /* Check the parameters */
    assert_param(IS_SPI_ALL_PERIPH(SPIx));

    for (i = 0; i < len; i++)
    {
        SPIx->DR[0] = (*pBuf++);
        /*read TFNF bit status in SR register: SET is Tx FIFO is not full*/
        while (!(SPIx->SR & BIT(1)));
    }
}

/**
  * @brief  Enables or disables the specified SPI/I2S interrupts.
  * @param  SPIx: where x can be 0 or 1
  * @param  SPI_IT: specifies the SPI/I2S interrupt source to be enabled or disabled.
  *   This parameter can be one of the following values:
  *     @arg SPI_INT_TXE: Tx buffer empty interrupt mask
  *     @arg SPI_INT_TXO: Tx buffer overflow interrupt mask
  *     @arg SPI_INT_RXU: receive FIFO Underflow Interrupt mask
  *     @arg SPI_INT_RXO: receive FIFO Overflow Interrupt mask
  *     @arg SPI_INT_RXF: receive FIFO Full Interrupt mask which equal RXNE Interrupt!!!
  *     @arg SPI_INT_MST: multi-Master Contention Interrupt mask
  * @param  NewState: new state of the specified SPI interrupt.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void SPI_INTConfig(SPI_TypeDef *SPIx, uint8_t SPI_IT, FunctionalState NewState)
{
    /* Check the parameters */
    assert_param(IS_SPI_ALL_PERIPH(SPIx));
    assert_param(IS_FUNCTIONAL_STATE(NewState));
    assert_param(IS_SPI_CONFIG_IT(SPI_IT));

    if (NewState != DISABLE)
    {
        /* Enable the selected SPI interrupt */
        SPIx->IMR |= SPI_IT;
    }
    else
    {
        /* Disable the selected SPI interrupt */
        SPIx->IMR &= (uint16_t)~(SPI_IT);
    }
}

/**
  * @brief  Clear the specified SPI interrupt.
  * @param  SPIx: where x can be 0 or 1
  * @param  SPI_IT: specifies the SPI interrupt to clear.
  *   This parameter can be one of the following values:
  *     @arg SPI_INT_MST: Multi-Master Contention Interrupt.
  *     @arg SPI_INT_RXO: Receive FIFO Overflow Interrupt.
  *     @arg SPI_INT_RXU: Receive FIFO Underflow Interrupt.
  *     @arg SPI_INT_TXO: Transmit FIFO Overflow Interrupt .
  * @retval None.
  */
void SPI_ClearINTPendingBit(SPI_TypeDef *SPIx, uint16_t SPI_IT)
{
    /* Check the parameters */
    assert_param(IS_SPI_ALL_PERIPH(SPIx));
    assert_param(IS_SPI_CONFIG_IT(SPI_IT));

    switch (SPI_IT)
    {
    case SPI_INT_RXO:
        SPIx->RXOICR;
        break;
    case SPI_INT_RXU:
        SPIx->RXUICR;
        break;
    case SPI_INT_TXO:
        SPIx->TXOICR;
        break;
    case SPI_INT_MST:
        SPIx->FAEICR;
        break;
    case SPI_INT_TUF:
        SPIx->TXUICR;
        break;
    case SPI_INT_RIG:
        SPIx->SSRICR;
        break;
    default:
        break;
    }

}
/******************* (C) COPYRIGHT 2015 Realtek Semiconductor Corporation *****END OF FILE****/