summaryrefslogtreecommitdiffstats
path: root/uc_str912/prj_blinky_complex_startup/src/91x_ssp.c
blob: 06bf30cfea1253c93bd5768c82495d87b6b19471 (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : 91x_ssp.c
* Author             : MCD Application Team
* Date First Issued  : 05/18/2006 : Version 1.0
* Description        : This file provides all the SSP software functions.
********************************************************************************
* History:
* 05/24/2006 : Version 1.1
* 05/18/2006 : Version 1.0
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "91x_ssp.h"
#include "91x_scu.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/* SSP peripheral Enable */
#define SSP_Enable   0x0002
#define SSP_Disable  0xFFFD

/* SSP Loop Back Mode Enable */
#define SSP_LoopBackMode_Enable   0x0001
#define SSP_LoopBackMode_Disable  0xFFFE

/* SSP Flag Mask */
#define SSP_Flag_Mask  0x001F

/* SSP DMA transmit/ receive enable/disable Masks */
#define SSP_DMA_TransmitEnable   0x0002
#define SSP_DMA_TransmitDisable  0xFFFD
#define SSP_DMA_ReceiveEnable    0x0001
#define SSP_DMA_ReceiveDisable   0xFFFE

/* SSP Masks */
#define SSP_FrameFormat_Mask     0xFFCF
#define SSP_DataSize_Mask        0xFFF0
#define SSP_ClockRate_Mask       0x00FF
#define SSP_ClockPrescaler_Mask  0xFF00

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : SSP_DeInit
* Description    : Deinitializes the SSPx peripheral registers to their default
*                  reset values.
* Input          : SSPx: where x can be 0 or 1 to select the SSP peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
void SSP_DeInit(SSP_TypeDef* SSPx)
{
  if(SSPx == SSP0)
  {
    /* Reset the SSP0 registers values*/
    SCU_APBPeriphReset(__SSP0,ENABLE);
    SCU_APBPeriphReset(__SSP0,DISABLE);
  }
  else if (SSPx == SSP1)
  {
    /* Reset the SSP1 registers values*/
    SCU_APBPeriphReset(__SSP1,ENABLE);
    SCU_APBPeriphReset(__SSP1,DISABLE);
  }
}

/*******************************************************************************
* Function Name  : SSP_Init
* Description    : Initializes the SSPx  peripheral according to the specified
*                  parameters in the SSP_InitTypeDef structure.
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
*                  - SSP_InitStruct: pointer to a SSP_InitTypeDef structure that
*                    contains the configuration information for the specified SSP
*                    peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
void SSP_Init(SSP_TypeDef* SSPx, SSP_InitTypeDef* SSP_InitStruct)
{
  if(SSP_InitStruct->SSP_FrameFormat == SSP_FrameFormat_Motorola)
  {
    /* Set the Motorola frame format */
    SSPx->CR0 &= SSP_FrameFormat_Motorola;
    /* Configure the Clock polarity */
    if(SSP_InitStruct->SSP_CPOL == SSP_CPOL_High)
    {
      /* SCK is held high when no data is being transfered */
      SSPx->CR0 |= SSP_CPOL_High;
    }
    else
    {
      /* SCK is held low when no data is being transfered */
      SSPx->CR0 &= SSP_CPOL_Low;
    }
    /* Configure the Clock Phase */
    if(SSP_InitStruct->SSP_CPHA == SSP_CPHA_2Edge)
    {
      /* Data captured on second clock edge */
      SSPx->CR0 |= SSP_CPHA_2Edge;
    }
    else
    {
      /* Data captured on first clock edge */
      SSPx->CR0 &= SSP_CPHA_1Edge;
    }
  }
   /* Configure the Frame format */
  else
  {
    /* Clear the FRF[1:0] bits */
    SSPx->CR0 &= SSP_FrameFormat_Mask;
    /* Set the TI frame format */
    SSPx->CR0 |= SSP_InitStruct->SSP_FrameFormat;
  }
  /* Configure the Mode */
  if(SSP_InitStruct->SSP_Mode == SSP_Mode_Slave)
  {
    /* Set the slave mode */
    SSPx->CR1 |= SSP_Mode_Slave;
    /* Configure the Slave output */
    if(SSP_InitStruct->SSP_SlaveOutput == SSP_SlaveOutput_Disable)
    {
      /* Slave output disabled */
      SSPx->CR1 |= SSP_SlaveOutput_Disable;
    }
    else
    {
      /* Slave output enabled */
      SSPx->CR1 &= SSP_SlaveOutput_Enable;
    }
  }
  else
  {
    /* Set the master mode */
    SSPx->CR1 &= SSP_Mode_Master;
    /* Clear clock rate SCR[7:0] bits */
    SSPx->CR0 &= SSP_ClockRate_Mask;
    /* Set the serial clock rate */
    SSPx->CR0 |= (SSP_InitStruct->SSP_ClockRate<<8);
    /* Clear clock prescaler CPSDVSR[7:0] bits */
    SSPx->PR &= SSP_ClockPrescaler_Mask;
    /* Set the serial clock prescaler */
    SSPx->PR |= SSP_InitStruct->SSP_ClockPrescaler;
  }

  /* Clear data size DSS[3:0] bits */
  SSPx->CR0 &= SSP_DataSize_Mask;
  /* Set the data size */
  SSPx->CR0 |= SSP_InitStruct->SSP_DataSize;
}
/*******************************************************************************
* Function Name  : SSP_StructInit
* Description    : Fills in a SSP_InitTypeDef structure with the reset value of
*                  each parameter.
* Input          : SSP_InitStruct : pointer to a SSP_InitTypeDef structure
                   which will be initialized.
* Output         : None
* Return         : None
*******************************************************************************/
void SSP_StructInit(SSP_InitTypeDef* SSP_InitStruct)
{
  /* Initialize the SSP_FrameFormat member */
  SSP_InitStruct->SSP_FrameFormat = SSP_FrameFormat_Motorola;

  /* Initialize the SSP_Mode member */
  SSP_InitStruct->SSP_Mode = SSP_Mode_Master;

  /* Initialize the SSP_CPOL member */
  SSP_InitStruct->SSP_CPOL = SSP_CPOL_Low;

  /* Initialize the SSP_CPHA member */
  SSP_InitStruct->SSP_CPHA = SSP_CPHA_1Edge;

  /* Initialize the SSP_DataSize member */
  SSP_InitStruct->SSP_DataSize = SSP_DataSize_8b;

  /* Initialize the SSP_SlaveOutput member */
  SSP_InitStruct->SSP_SlaveOutput = SSP_SlaveOutput_Enable;

  /* Initialize the SSP_ClockRate member */
  SSP_InitStruct->SSP_ClockRate = 0;

  /* Initialize the SSP_ClockPrescaler member */
  SSP_InitStruct->SSP_ClockPrescaler = 0;
}

/*******************************************************************************
* Function Name  : SSP_Cmd
* Description    : Enables or disables the specified SSP peripheral.
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
*                  - NewState: new state of the  SSPx peripheral. This parameter
*                    can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void SSP_Cmd(SSP_TypeDef* SSPx, FunctionalState NewState)
{
  if(NewState == ENABLE)
  {
    /* Enable the SSP peripheral */
    SSPx->CR1 |= SSP_Enable;
  }
  else
  {
    /* Disable the SSP peripheral */
    SSPx->CR1 &= SSP_Disable;
  }
}

/*******************************************************************************
* Function Name  : SSP_ITConfig
* Description    : Enables or disables the specified SSP interrupts.
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
*                  - SSP_IT: specifies the SSP interrupts sources to be enabled
*                    or disabled. This parameter can be any combination of the
*                    following values:
*                         - SSP_IT_TxFifo: Transmit FIFO half empty or less interrupt
*                         - SSP_IT_RxFifo: Receive FIFO half full or less interrupt
*                         - SSP_IT_RxTimeOut: Receive timeout interrupt
*                         - SSP_IT_RxOverrun: Receive overrun interrupt
*                  - NewState: new state of the specified SSP interrupts.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void SSP_ITConfig(SSP_TypeDef* SSPx, u16 SSP_IT, FunctionalState NewState)
{
  if(NewState == ENABLE)
  {
    /* Enable the selected SSP interrupts */
    SSPx->IMSCR |= SSP_IT;
  }
  else
  {
    /* Disable the selected SSP interrupts */
    SSPx->IMSCR &= ~SSP_IT;
  }
}

/*******************************************************************************
* Function Name  : SSP_DMACmd
* Description    : Configures the SSP0 DMA interface.
* Input          :  - SSPx: where x can be 0 or 1 to select the SSP peripheral.
*                   - SSP_DMATransfert : specifies the DMA transfert to be
*                    enabled or disabled. This parameter can be one of the
*                    following values:
*                         - SSP_DMA_Transmit: transmit Fifo DMA transfert
*                         - SSP_DMA_Receive : receive Fifo DMA transfert
*                  - NewState: new state of the DMA transfert.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void SSP_DMACmd(SSP_TypeDef* SSPx, u16 SSP_DMATransfert, FunctionalState NewState)
{
  if(NewState == ENABLE)
  {
    if(SSP_DMATransfert == SSP_DMA_Transmit)
    {
      /* Enable DMA for the transmit FIFO */
      SSPx->DMACR |= SSP_DMA_TransmitEnable;
    }
    else
    {
      /* Enable DMA for the receive FIFO */
      SSPx->DMACR |= SSP_DMA_ReceiveEnable;
    }
  }
  else
  {
    if(SSP_DMATransfert == SSP_DMA_Transmit)
    {
      /* Disable DMA for the transmit FIFO */
      SSPx->DMACR &= SSP_DMA_TransmitDisable;
    }
    else
    {
      /* Disable DMA for the receive FIFO */
      SSPx->DMACR &= SSP_DMA_ReceiveDisable;
    }
  }
}

/*******************************************************************************
* Function Name  : SSP_SendData.
* Description    : Transmits a Data through the SSP peripheral.
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
*                  - Data : Data to be transmitted.
* Output         : None
* Return         : None
*******************************************************************************/
void SSP_SendData(SSP_TypeDef* SSPx, u16 Data)
{
  /* Write in the DR register the data to be sent */
  SSPx->DR = Data;
}

/*******************************************************************************
* Function Name  : SSP_ReceiveData.
* Description    : Returns the most recent received data by the SSP peripheral.
* Input          : SSPx: where x can be 0 or 1 to select the SSP peripheral.
* Output         : None
* Return         : The value of the received data.
*******************************************************************************/
u16 SSP_ReceiveData(SSP_TypeDef* SSPx)
{
  /* Return the data in the DR register */	
  return SSPx->DR;
}

/*******************************************************************************
* Function Name  : SSP_LoopBackMode
* Description    : Enable or disable the Loop back mode for the selected SSPx peripheral.
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
*                  - NewState: new state of the Loop Back mode.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None.
*******************************************************************************/
void SSP_LoopBackMode(SSP_TypeDef* SSPx, FunctionalState NewState)
{
  if(NewState == ENABLE)
  {
    /* Enable loop back mode */
    SSPx->CR1 |= SSP_LoopBackMode_Enable;
  }
  else
  {
    /* Disable loop back mode */
    SSPx->CR1 &= SSP_LoopBackMode_Disable;
  }
}



/*******************************************************************************
* Function Name  : SSP_GetFlagStatus
* Description    : Checks whether the specified SSP flag is set or not.
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
*                  - SSP_FLAG: flag to check. This parameter can be one of the
*                    following values:
*                         - SSP_FLAG_Busy: busy flag
*                         - SSP_FLAG_RxFifoFull: Receive FIFO full flag
*                         - SSP_FLAG_RxFifoNotEmpty: Receive FIFO not empty flag
*                         - SSP_FLAG_TxFifoNotFull: Transmit FIFO not full flag
*                         - SSP_FLAG_TxFifoEmpty: Transmit FIFO empty flag
*                         - SSP_FLAG_TxFifo: Transmit FIFO half empty or less flag
*                         - SSP_FLAG_RxFifo: Receive FIFO half full or less flag
*                         - SSP_FLAG_RxTimeOut: Receive timeout flag
*                         - SSP_FLAG_RxOverrun: Receive overrun flag
* Output         : None
* Return         : The new state of SSP_Flag (SET or RESET).
*******************************************************************************/
FlagStatus SSP_GetFlagStatus(SSP_TypeDef* SSPx, u16 SSP_FLAG)
{
    u32 SSPReg = 0, FlagPos = 0;
    u32 StatusReg = 0;

  /* Get the SSP register index */
  SSPReg = SSP_FLAG >> 5;

  /* Get the flag position */
  FlagPos = SSP_FLAG & SSP_Flag_Mask;

  /* Find the register of the flag to check */
  if(SSPReg == 1)
  {
    /* The flag to check is in SR register */
    StatusReg = SSPx->SR;  	
  }
  else if (SSPReg == 2)
  {
    /* The flag to check is in RISR register */
    StatusReg = SSPx->RISR;
  }

  /* Check the status of the specified SSP flag */
  if((StatusReg & (1 << FlagPos)) != RESET)
  {
    /* Return SET if the SSP flag is set */
    return SET;
  }
  else
  {
    /* Return RESET if the SSP flag is reset */
    return RESET;
  }
}

/*******************************************************************************
* Function Name  : SSP_ClearFlag
* Description    : Clears the SSPx flags.
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
*                  - SSP_FLAG: flags to clear. This parameter one of the
*                    following values:
*                         - SSP_FLAG_RxTimeOut: Receive timeout flag
*                         - SSP_FLAG_RxOverrun: Receive overrun flag
* Output         : None
* Return         : None
*******************************************************************************/
void SSP_ClearFlag(SSP_TypeDef* SSPx, u16 SSP_FLAG)
{
    u8 FlagPos = 0;

  /* Get the flag position */
  FlagPos = SSP_FLAG & SSP_Flag_Mask;

  /* Clear the selected SSP flag */
  SSPx->ICR = (1 << FlagPos);
}

/*******************************************************************************
* Function Name  : SSP_GetITStatus
* Description    : Checks whether the specified SSP interrupt flag is set or not.
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
*                  - SSP_IT: interrupt flag to check. This parameter can be one
*                    of the following values:
*                         - SSP_IT_TxFifo: Transmit FIFO half empty or less interrupt
*                         - SSP_IT_RxFifo: Receive FIFO half full or less interrupt
*                         - SSP_IT_RxTimeOut: Receive timeout interrupt
*                         - SSP_IT_RxOverrun: Receive overrun interrupt
* Output         : None
* Return         : The new state of SSP_IT flag (SET or RESET).
*******************************************************************************/
ITStatus SSP_GetITStatus(SSP_TypeDef* SSPx, u16 SSP_IT)
{
  /* Check the status of the specified interrupt flag */
  if((SSPx->MISR & SSP_IT) != RESET)
  {
    /* Return SET if the SSP interrupt flag is set */
    return SET;
  }
  else
  {
    /* Return RESET if SSP interrupt flag is reset */
    return RESET;
  }
}

/*******************************************************************************
* Function Name  : SSP_ClearITPendingBit
* Description    : Clears the pending interrupt flags.
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
*                  - SSP_IT: interrupts pending bits to clear. This parameter
*                    can be any combination of the following values:
*                         - SSP_IT_RxTimeOut: Receive timeout interrupt
*                         - SSP_IT_RxOverrun: Receive overrun interrupt
* Output         : None
* Return         : None
*******************************************************************************/
void SSP_ClearITPendingBit(SSP_TypeDef* SSPx, u16 SSP_IT)
{
  /* Clear the selected SSP interrupts pending bits */
  SSPx->ICR = SSP_IT;
}

/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/

OpenPOWER on IntegriCloud