Subversion Repositories NaviCtrl

Rev

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

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