Subversion Repositories NaviCtrl

Rev

Rev 196 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
2
* File Name          : 91x_ssp.c
3
* Author             : MCD Application Team
4
* Date First Issued  : 05/18/2006 : Version 1.0
5
* Description        : This file provides all the SSP software functions.
6
********************************************************************************
7
* History:
8
* 05/22/2007 : Version 1.2
9
* 05/24/2006 : Version 1.1
10
* 05/18/2006 : Version 1.0
11
********************************************************************************
12
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
14
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
15
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
16
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
17
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18
*******************************************************************************/
19
 
20
/* Includes ------------------------------------------------------------------*/
21
#include "91x_ssp.h"
22
#include "91x_scu.h"
23
 
24
/* Private typedef -----------------------------------------------------------*/
25
/* Private define ------------------------------------------------------------*/
26
/* Private macro -------------------------------------------------------------*/
27
/* Private variables ---------------------------------------------------------*/
28
 
29
/* SSP peripheral Enable */
30
#define SSP_Enable   0x0002
31
#define SSP_Disable  0xFFFD
32
 
33
/* SSP Loop Back Mode Enable */
34
#define SSP_LoopBackMode_Enable   0x0001
35
#define SSP_LoopBackMode_Disable  0xFFFE
36
 
37
/* SSP Flag Mask */
38
#define SSP_Flag_Mask  0x001F
39
 
40
/* SSP DMA transmit/ receive enable/disable Masks */
41
#define SSP_DMA_TransmitEnable   0x0002
42
#define SSP_DMA_TransmitDisable  0xFFFD
43
#define SSP_DMA_ReceiveEnable    0x0001
44
#define SSP_DMA_ReceiveDisable   0xFFFE
45
 
46
/* SSP Masks */
47
#define SSP_FrameFormat_Mask     0xFFCF
48
#define SSP_DataSize_Mask        0xFFF0
49
#define SSP_ClockRate_Mask       0x00FF
50
#define SSP_ClockPrescaler_Mask  0xFF00
51
 
52
/* Private function prototypes -----------------------------------------------*/
53
/* Private functions ---------------------------------------------------------*/
54
 
55
/*******************************************************************************
56
* Function Name  : SSP_DeInit
57
* Description    : Deinitializes the SSPx peripheral registers to their default
58
*                  reset values.
59
* Input          : SSPx: where x can be 0 or 1 to select the SSP peripheral.
60
* Output         : None
61
* Return         : None
62
*******************************************************************************/
63
void SSP_DeInit(SSP_TypeDef* SSPx)
64
{
65
  if(SSPx == SSP0)
66
  {
67
    /* Reset the SSP0 registers values*/
68
    SCU_APBPeriphReset(__SSP0,ENABLE);
69
    SCU_APBPeriphReset(__SSP0,DISABLE);
70
  }
71
  else if (SSPx == SSP1)
72
  {
73
    /* Reset the SSP1 registers values*/
74
    SCU_APBPeriphReset(__SSP1,ENABLE);
75
    SCU_APBPeriphReset(__SSP1,DISABLE);
76
  }
77
}
78
 
79
/*******************************************************************************
80
* Function Name  : SSP_Init
81
* Description    : Initializes the SSPx  peripheral accordingto the specified
82
*                  parameters in the SSP_InitTypeDef structure.
83
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
84
*                  - SSP_InitStruct: pointer to a SSP_InitTypeDef structure that
85
*                    contains the configuration information for the specified SSP
86
*                    peripheral.
87
* Output         : None
88
* Return         : None
89
*******************************************************************************/
90
void SSP_Init(SSP_TypeDef* SSPx, SSP_InitTypeDef* SSP_InitStruct)
91
{
92
  if(SSP_InitStruct->SSP_FrameFormat == SSP_FrameFormat_Motorola)
93
  {
94
    /* Set the Motorola frame format */
95
    SSPx->CR0 &= SSP_FrameFormat_Motorola;
96
    /* Configure the Clock polarity */
97
    if(SSP_InitStruct->SSP_CPOL == SSP_CPOL_High)
98
    {
99
      /* SCK is held high when no data is being transfered */
100
      SSPx->CR0 |= SSP_CPOL_High;
101
    }
102
    else
103
    {
104
      /* SCK is held low when no data is being transfered */
105
      SSPx->CR0 &= SSP_CPOL_Low;
106
    }
107
    /* Configure the Clock Phase */
108
    if(SSP_InitStruct->SSP_CPHA == SSP_CPHA_2Edge)
109
    {
110
      /* Data captured on second clock edge */
111
      SSPx->CR0 |= SSP_CPHA_2Edge;
112
    }
113
    else
114
    {
115
      /* Data captured on first clock edge */
116
      SSPx->CR0 &= SSP_CPHA_1Edge;
117
    }
118
  }
119
   /* Configure the Frame format */
120
  else
121
  {
122
    /* Clear the FRF[1:0] bits */
123
    SSPx->CR0 &= SSP_FrameFormat_Mask;
124
    /* Set the TI frame format */
125
    SSPx->CR0 |= SSP_InitStruct->SSP_FrameFormat;
126
  }
127
  /* Configure the Mode */
128
  if(SSP_InitStruct->SSP_Mode == SSP_Mode_Slave)
129
  {
130
    /* Set the slave mode */
131
    SSPx->CR1 |= SSP_Mode_Slave;
132
    /* Configure the Slave output */
133
    if(SSP_InitStruct->SSP_SlaveOutput == SSP_SlaveOutput_Disable)
134
    {
135
      /* Slave output disabled */
136
      SSPx->CR1 |= SSP_SlaveOutput_Disable;
137
    }
138
    else
139
    {
140
      /* Slave output enabled */
141
      SSPx->CR1 &= SSP_SlaveOutput_Enable;
142
    }
143
  }
144
  else
145
  {
146
    /* Set the master mode */
147
    SSPx->CR1 &= SSP_Mode_Master;
148
    /* Clear clock rate SCR[7:0] bits */
149
    SSPx->CR0 &= SSP_ClockRate_Mask;
150
    /* Set the serial clock rate */
151
    SSPx->CR0 |= (SSP_InitStruct->SSP_ClockRate<<8);
152
    /* Clear clock prescaler CPSDVSR[7:0] bits */
153
    SSPx->PR &= SSP_ClockPrescaler_Mask;
154
    /* Set the serial clock prescaler */
155
    SSPx->PR |= SSP_InitStruct->SSP_ClockPrescaler;
156
  }
157
 
158
  /* Clear data size DSS[3:0] bits */
159
  SSPx->CR0 &= SSP_DataSize_Mask;
160
  /* Set the data size */
161
  SSPx->CR0 |= SSP_InitStruct->SSP_DataSize;
162
}
163
/*******************************************************************************
164
* Function Name  : SSP_StructInit
165
* Description    : Fills in a SSP_InitTypeDef structure with the reset value of
166
*                  each parameter.
167
* Input          : SSP_InitStruct : pointer to a SSP_InitTypeDef structure
168
                   which will be initialized.
169
* Output         : None
170
* Return         : None
171
*******************************************************************************/
172
void SSP_StructInit(SSP_InitTypeDef* SSP_InitStruct)
173
{
174
  /* Initialize the SSP_FrameFormat member */
175
  SSP_InitStruct->SSP_FrameFormat = SSP_FrameFormat_Motorola;
176
 
177
  /* Initialize the SSP_Mode member */
178
  SSP_InitStruct->SSP_Mode = SSP_Mode_Master;
179
 
180
  /* Initialize the SSP_CPOL member */
181
  SSP_InitStruct->SSP_CPOL = SSP_CPOL_Low;
182
 
183
  /* Initialize the SSP_CPHA member */
184
  SSP_InitStruct->SSP_CPHA = SSP_CPHA_1Edge;
185
 
186
  /* Initialize the SSP_DataSize member */
187
  SSP_InitStruct->SSP_DataSize = SSP_DataSize_8b;
188
 
189
  /* Initialize the SSP_SlaveOutput member */
190
  SSP_InitStruct->SSP_SlaveOutput = SSP_SlaveOutput_Enable;
191
 
192
  /* Initialize the SSP_ClockRate member */
193
  SSP_InitStruct->SSP_ClockRate = 0;
194
 
195
  /* Initialize the SSP_ClockPrescaler member */
196
  SSP_InitStruct->SSP_ClockPrescaler = 0;
197
}
198
 
199
/*******************************************************************************
200
* Function Name  : SSP_Cmd
201
* Description    : Enables or disables the specified SSP peripheral.
202
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
203
*                  - NewState: new state of the  SSPx peripheral. This parameter
204
*                    can be: ENABLE or DISABLE.
205
* Output         : None
206
* Return         : None
207
*******************************************************************************/
208
void SSP_Cmd(SSP_TypeDef* SSPx, FunctionalState NewState)
209
{
210
  if(NewState == ENABLE)
211
  {
212
    /* Enable the SSP peripheral */
213
    SSPx->CR1 |= SSP_Enable;
214
  }
215
  else
216
  {
217
    /* Disable the SSP peripheral */
218
    SSPx->CR1 &= SSP_Disable;
219
  }
220
}
221
 
222
/*******************************************************************************
223
* Function Name  : SSP_ITConfig
224
* Description    : Enables or disables the specified SSP interrupts.
225
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
226
*                  - SSP_IT: specifies the SSP interrupts sources to be enabled
227
*                    or disabled. This parameter can be any combination of the
228
*                    following values:
229
*                         - SSP_IT_TxFifo: Transmit FIFO half empty or less interrupt
230
*                         - SSP_IT_RxFifo: Receive FIFO half full or less interrupt
231
*                         - SSP_IT_RxTimeOut: Receive timeout interrupt
232
*                         - SSP_IT_RxOverrun: Receive overrun interrupt
233
*                  - NewState: new state of the specified SSP interrupts.
234
*                    This parameter can be: ENABLE or DISABLE.
235
* Output         : None
236
* Return         : None
237
*******************************************************************************/
238
void SSP_ITConfig(SSP_TypeDef* SSPx, u16 SSP_IT, FunctionalState NewState)
239
{
240
  if(NewState == ENABLE)
241
  {
242
    /* Enable the selected SSP interrupts */
243
    SSPx->IMSCR |= SSP_IT;
244
  }
245
  else
246
  {
247
    /* Disable the selected SSP interrupts */
248
    SSPx->IMSCR &= ~SSP_IT;
249
  }
250
}
251
 
252
/*******************************************************************************
253
* Function Name  : SSP_DMACmd
254
* Description    : Configures the SSP0 DMA interface.
255
* Input          :  - SSPx: where x can be 0 or 1 to select the SSP peripheral.
256
*                   - SSP_DMATransfert : specifies the DMA transfert to be
257
*                    enabled or disabled. This parameter can be one of the
258
*                    following values:
259
*                         - SSP_DMA_Transmit: transmit Fifo DMA transfert
260
*                         - SSP_DMA_Receive : receive Fifo DMA transfert
261
*                  - NewState: new state of the DMA transfert.
262
*                    This parameter can be: ENABLE or DISABLE.
263
* Output         : None
264
* Return         : None
265
*******************************************************************************/
266
void SSP_DMACmd(SSP_TypeDef* SSPx, u16 SSP_DMATransfert, FunctionalState NewState)
267
{
268
  if(NewState == ENABLE)
269
  {
270
    if(SSP_DMATransfert == SSP_DMA_Transmit)
271
    {
272
      /* Enable DMA for the transmit FIFO */
273
      SSPx->DMACR |= SSP_DMA_TransmitEnable;
274
    }
275
    else
276
    {
277
      /* Enable DMA for the receive FIFO */
278
      SSPx->DMACR |= SSP_DMA_ReceiveEnable;
279
    }
280
  }
281
  else
282
  {
283
    if(SSP_DMATransfert == SSP_DMA_Transmit)
284
    {
285
      /* Disable DMA for the transmit FIFO */
286
      SSPx->DMACR &= SSP_DMA_TransmitDisable;
287
    }
288
    else
289
    {
290
      /* Disable DMA for the receive FIFO */
291
      SSPx->DMACR &= SSP_DMA_ReceiveDisable;
292
    }
293
  }
294
}
295
 
296
/*******************************************************************************
297
* Function Name  : SSP_SendData.
298
* Description    : Transmits a Data through the SSP peripheral.
299
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
300
*                  - Data : Data to be transmitted.
301
* Output         : None
302
* Return         : None
303
*******************************************************************************/
304
void SSP_SendData(SSP_TypeDef* SSPx, u16 Data)
305
{
306
  /* Write in the DR register the data to be sent */
307
  SSPx->DR = Data;
308
}
309
 
310
/*******************************************************************************
311
* Function Name  : SSP_ReceiveData.
312
* Description    : Returns the most recent received data by the SSP peripheral.
313
* Input          : SSPx: where x can be 0 or 1 to select the SSP peripheral.
314
* Output         : None
315
* Return         : The value of the received data.
316
*******************************************************************************/
317
u16 SSP_ReceiveData(SSP_TypeDef* SSPx)
318
{
319
  /* Return the data in the DR register */     
320
  return SSPx->DR;
321
}
322
 
323
/*******************************************************************************
324
* Function Name  : SSP_LoopBackConfig
325
* Description    : Enable or disable the Loop back mode for the selected SSPx peripheral.
326
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
327
*                  - NewState: new state of the Loop Back mode.
328
*                    This parameter can be: ENABLE or DISABLE.
329
* Output         : None
330
* Return         : None.
331
*******************************************************************************/
332
void SSP_LoopBackConfig(SSP_TypeDef* SSPx, FunctionalState NewState)
333
{
334
  if(NewState == ENABLE)
335
  {
336
    /* Enable loop back mode */
337
    SSPx->CR1 |= SSP_LoopBackMode_Enable;
338
  }
339
  else
340
  {
341
    /* Disable loop back mode */
342
    SSPx->CR1 &= SSP_LoopBackMode_Disable;
343
  }
344
}
345
 
346
 
347
 
348
/*******************************************************************************
349
* Function Name  : SSP_GetFlagStatus
350
* Description    : Checks whether the specified SSP flag is set or not.
351
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
352
*                  - SSP_FLAG: flag to check. This parameter can be one of the
353
*                    following values:
354
*                         - SSP_FLAG_Busy: busy flag
355
*                         - SSP_FLAG_RxFifoFull: Receive FIFO full flag
356
*                         - SSP_FLAG_RxFifoNotEmpty: Receive FIFO not empty flag
357
*                         - SSP_FLAG_TxFifoNotFull: Transmit FIFO not full flag
358
*                         - SSP_FLAG_TxFifoEmpty: Transmit FIFO empty flag
359
*                         - SSP_FLAG_TxFifo: Transmit FIFO half empty or less flag
360
*                         - SSP_FLAG_RxFifo: Receive FIFO half full or less flag
361
*                         - SSP_FLAG_RxTimeOut: Receive timeout flag
362
*                         - SSP_FLAG_RxOverrun: Receive overrun flag
363
* Output         : None
364
* Return         : The new state of SSP_Flag (SET or RESET).
365
*******************************************************************************/
366
FlagStatus SSP_GetFlagStatus(SSP_TypeDef* SSPx, u16 SSP_FLAG)
367
{
368
    u32 SSPReg = 0, FlagPos = 0;
369
    u32 StatusReg = 0;
370
 
371
  /* Get the SSP register index */
372
  SSPReg = SSP_FLAG >> 5;
373
 
374
  /* Get the flag position */
375
  FlagPos = SSP_FLAG & SSP_Flag_Mask;
376
 
377
  /* Find the register of the flag to check */
378
  if(SSPReg == 1)
379
  {
380
    /* The flag to check is in SR register */
381
    StatusReg = SSPx->SR;      
382
  }
383
  else if (SSPReg == 2)
384
  {
385
    /* The flag to check is in RISR register */
386
    StatusReg = SSPx->RISR;
387
  }
388
 
389
  /* Check the status of the specified SSP flag */
390
  if((StatusReg & (1 << FlagPos)) != RESET)
391
  {
392
    /* Return SET if the SSP flag is set */
393
    return SET;
394
  }
395
  else
396
  {
397
    /* Return RESET if the SSP flag is reset */
398
    return RESET;
399
  }
400
}
401
 
402
/*******************************************************************************
403
* Function Name  : SSP_ClearFlag
404
* Description    : Clears the SSPx flags.
405
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
406
*                  - SSP_FLAG: flags to clear. This parameter one of the
407
*                    following values:
408
*                         - SSP_FLAG_RxTimeOut: Receive timeout flag
409
*                         - SSP_FLAG_RxOverrun: Receive overrun flag
410
* Output         : None
411
* Return         : None
412
*******************************************************************************/
413
void SSP_ClearFlag(SSP_TypeDef* SSPx, u16 SSP_FLAG)
414
{
415
    u8 FlagPos = 0;
416
 
417
  /* Get the flag position */
418
  FlagPos = SSP_FLAG & SSP_Flag_Mask;
419
 
420
  /* Clear the selected SSP flag */
421
  SSPx->ICR = (1 << FlagPos);
422
}
423
 
424
/*******************************************************************************
425
* Function Name  : SSP_GetITStatus
426
* Description    : Checks whether the specified SSP interrupt flag is set or not.
427
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
428
*                  - SSP_IT: interrupt flag to check. This parameter can be one
429
*                    of the following values:
430
*                         - SSP_IT_TxFifo: Transmit FIFO half empty or less interrupt
431
*                         - SSP_IT_RxFifo: Receive FIFO half full or less interrupt
432
*                         - SSP_IT_RxTimeOut: Receive timeout interrupt
433
*                         - SSP_IT_RxOverrun: Receive overrun interrupt
434
* Output         : None
435
* Return         : The new state of SSP_IT flag (SET or RESET).
436
*******************************************************************************/
437
ITStatus SSP_GetITStatus(SSP_TypeDef* SSPx, u16 SSP_IT)
438
{
439
  /* Check the status of the specified interrupt flag */
440
  if((SSPx->MISR & SSP_IT) != RESET)
441
  {
442
    /* Return SET if the SSP interrupt flag is set */
443
    return SET;
444
  }
445
  else
446
  {
447
    /* Return RESET if SSP interrupt flag is reset */
448
    return RESET;
449
  }
450
}
451
 
452
/*******************************************************************************
453
* Function Name  : SSP_ClearITPendingBit
454
* Description    : Clears the pending interrupt flags.
455
* Input          : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
456
*                  - SSP_IT: interrupts pending bits to clear. This parameter
457
*                    can be any combination of the following values:
458
*                         - SSP_IT_RxTimeOut: Receive timeout interrupt
459
*                         - SSP_IT_RxOverrun: Receive overrun interrupt
460
* Output         : None
461
* Return         : None
462
*******************************************************************************/
463
void SSP_ClearITPendingBit(SSP_TypeDef* SSPx, u16 SSP_IT)
464
{
465
  /* Clear the selected SSP interrupts pending bits */
466
  SSPx->ICR = SSP_IT;
467
}
468
 
469
/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/
470