1 /******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
\r
2 * File Name : 75x_ssp.c
\r
3 * Author : MCD Application Team
\r
4 * Date First Issued : 03/10/2006
\r
5 * Description : This file provides all the SSP software functions.
\r
6 ********************************************************************************
\r
10 ********************************************************************************
\r
11 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
\r
13 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
\r
14 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
\r
15 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
\r
16 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
17 *******************************************************************************/
\r
19 /* Includes ------------------------------------------------------------------*/
\r
20 #include "75x_ssp.h"
\r
21 #include "75x_mrcc.h"
\r
23 /* Private typedef -----------------------------------------------------------*/
\r
24 /* Private define ------------------------------------------------------------*/
\r
25 /* Private macro -------------------------------------------------------------*/
\r
26 /* Private variables ---------------------------------------------------------*/
\r
28 /* SSP peripheral Enable */
\r
29 #define SSP_Enable 0x0002
\r
30 #define SSP_Disable 0xFFFD
\r
32 /* SSP Loop Back Mode Enable */
\r
33 #define SSP_LoopBackMode_Enable 0x0001
\r
34 #define SSP_LoopBackMode_Disable 0xFFFE
\r
37 #define SSP_Flag_Mask 0x001F
\r
39 /* SSP DMA transmit/ receive enable/disable Masks */
\r
40 #define SSP0_DMA_TransmitEnable 0x0002
\r
41 #define SSP0_DMA_TransmitDisable 0xFFFD
\r
42 #define SSP0_DMA_ReceiveEnable 0x0001
\r
43 #define SSP0_DMA_ReceiveDisable 0xFFFE
\r
46 #define SSP_FrameFormat_Mask 0xFFCF
\r
47 #define SSP_DataSize_Mask 0xFFF0
\r
48 #define SSP_ClockRate_Mask 0x00FF
\r
49 #define SSP_ClockPrescaler_Mask 0xFF00
\r
50 #define SSP_SSI_Set_Mask 0x0020
\r
51 #define SSP_SSI_Reset_Mask 0xFFDF
\r
54 /* Private function prototypes -----------------------------------------------*/
\r
55 /* Private functions ---------------------------------------------------------*/
\r
57 /*******************************************************************************
\r
58 * Function Name : SSP_DeInit
\r
59 * Description : Deinitializes the SSPx peripheral registers to their default
\r
61 * Input : SSPx: where x can be 0 or 1 to select the SSP peripheral.
\r
64 *******************************************************************************/
\r
65 void SSP_DeInit(SSP_TypeDef* SSPx)
\r
69 /* Reset the SSP0 registers values*/
\r
70 MRCC_PeripheralSWResetConfig(MRCC_Peripheral_SSP0,ENABLE);
\r
71 MRCC_PeripheralSWResetConfig(MRCC_Peripheral_SSP0,DISABLE);
\r
73 else if (SSPx == SSP1)
\r
75 /* Reset the SSP1 registers values*/
\r
76 MRCC_PeripheralSWResetConfig(MRCC_Peripheral_SSP1,ENABLE);
\r
77 MRCC_PeripheralSWResetConfig(MRCC_Peripheral_SSP1,DISABLE);
\r
81 /*******************************************************************************
\r
82 * Function Name : SSP_Init
\r
83 * Description : Initializes the SSPx peripheral according to the specified
\r
84 * parameters in the SSP_InitTypeDef structure.
\r
85 * Input : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
\r
86 * - SSP_InitStruct: pointer to a SSP_InitTypeDef structure that
\r
87 * contains the configuration information for the specified SSP
\r
91 *******************************************************************************/
\r
92 void SSP_Init(SSP_TypeDef* SSPx, SSP_InitTypeDef* SSP_InitStruct)
\r
94 /* Configure the Frame format */
\r
95 if(SSP_InitStruct->SSP_FrameFormat == SSP_FrameFormat_TI)
\r
97 /* Clear the FRF[1:0] bits */
\r
98 SSPx->CR0 &= SSP_FrameFormat_Mask;
\r
99 /* Set the TI frame format */
\r
100 SSPx->CR0 |= SSP_FrameFormat_TI;
\r
104 /* Set the Motorola frame format */
\r
105 SSPx->CR0 &= SSP_FrameFormat_Motorola;
\r
106 /* Configure the Clock polarity */
\r
107 if(SSP_InitStruct->SSP_CPOL == SSP_CPOL_High)
\r
109 /* SCK is held high when no data is being transfered */
\r
110 SSPx->CR0 |= SSP_CPOL_High;
\r
114 /* SCK is held low when no data is being transfered */
\r
115 SSPx->CR0 &= SSP_CPOL_Low;
\r
117 /* Configure the Clock Phase */
\r
118 if(SSP_InitStruct->SSP_CPHA == SSP_CPHA_2Edge)
\r
120 /* Data captured on second clock edge */
\r
121 SSPx->CR0 |= SSP_CPHA_2Edge;
\r
125 /* Data captured on first clock edge */
\r
126 SSPx->CR0 &= SSP_CPHA_1Edge;
\r
130 /* Configure the Mode */
\r
131 if(SSP_InitStruct->SSP_Mode == SSP_Mode_Slave)
\r
133 /* Set the slave mode */
\r
134 SSPx->CR1 |= SSP_Mode_Slave;
\r
135 /* Configure the Slave output */
\r
136 if(SSP_InitStruct->SSP_SlaveOutput == SSP_SlaveOutput_Disable)
\r
138 /* Slave output disabled */
\r
139 SSPx->CR1 |= SSP_SlaveOutput_Disable;
\r
143 /* Slave output enabled */
\r
144 SSPx->CR1 &= SSP_SlaveOutput_Enable;
\r
146 /* Configure the NSS pin */
\r
147 if(SSP_InitStruct->SSP_NSS == SSP_NSS_Soft)
\r
149 /* Slave selected by software through SSI bit */
\r
150 SSPx->CR1 |= SSP_NSS_Soft;
\r
151 SSPx->CR1 &= SSP_SSI_Reset_Mask;
\r
155 /* Slave selected by hardware through external SSpin */
\r
156 SSPx->CR1 &= SSP_NSS_Hard;
\r
158 /* Configure the Clock rate and prescaler in TI slave mode */
\r
159 if(SSP_InitStruct->SSP_FrameFormat == SSP_FrameFormat_TI)
\r
161 /* Clear clock rate SCR[7:0] bits */
\r
162 SSPx->CR0 &= SSP_ClockRate_Mask;
\r
163 /* Set the serial clock rate */
\r
164 SSPx->CR0 |= (SSP_InitStruct->SSP_ClockRate<<8);
\r
165 /* Clear clock prescaler CPSDVSR[7:0] bits */
\r
166 SSPx->PR &= SSP_ClockPrescaler_Mask;
\r
167 /* Set the serial clock prescaler */
\r
168 SSPx->PR |= SSP_InitStruct->SSP_ClockPrescaler;
\r
173 /* Set the master mode */
\r
174 SSPx->CR1 &= SSP_Mode_Master;
\r
175 /* Configure the NSS pin */
\r
176 if(SSP_InitStruct->SSP_NSS == SSP_NSS_Soft)
\r
178 /* Master selected by software through SSI bit */
\r
179 SSPx->CR1 |= SSP_NSS_Soft;
\r
180 SSPx->CR1 |= SSP_SSI_Set_Mask;
\r
184 /* Master selected by hardware through external SSpin */
\r
185 SSPx->CR1 &= SSP_NSS_Hard;
\r
187 /* Clear clock rate SCR[7:0] bits */
\r
188 SSPx->CR0 &= SSP_ClockRate_Mask;
\r
189 /* Set the serial clock rate */
\r
190 SSPx->CR0 |= (SSP_InitStruct->SSP_ClockRate<<8);
\r
191 /* Clear clock prescaler CPSDVSR[7:0] bits */
\r
192 SSPx->PR &= SSP_ClockPrescaler_Mask;
\r
193 /* Set the serial clock prescaler */
\r
194 SSPx->PR |= SSP_InitStruct->SSP_ClockPrescaler;
\r
197 /* Clear data size DSS[3:0] bits */
\r
198 SSPx->CR0 &= SSP_DataSize_Mask;
\r
199 /* Set the data size */
\r
200 SSPx->CR0 |= SSP_InitStruct->SSP_DataSize;
\r
203 /*******************************************************************************
\r
204 * Function Name : SSP_StructInit
\r
205 * Description : Fills each SSP_InitStruct member with its default value.
\r
206 * Input : SSP_InitStruct : pointer to a SSP_InitTypeDef structure
\r
207 which will be initialized.
\r
210 *******************************************************************************/
\r
211 void SSP_StructInit(SSP_InitTypeDef* SSP_InitStruct)
\r
213 /* Initialize the SSP_FrameFormat member */
\r
214 SSP_InitStruct->SSP_FrameFormat = SSP_FrameFormat_Motorola;
\r
216 /* Initialize the SSP_Mode member */
\r
217 SSP_InitStruct->SSP_Mode = SSP_Mode_Master;
\r
219 /* Initialize the SSP_CPOL member */
\r
220 SSP_InitStruct->SSP_CPOL = SSP_CPOL_Low;
\r
222 /* Initialize the SSP_CPHA member */
\r
223 SSP_InitStruct->SSP_CPHA = SSP_CPHA_1Edge;
\r
225 /* Initialize the SSP_DataSize member */
\r
226 SSP_InitStruct->SSP_DataSize = SSP_DataSize_8b;
\r
228 /* Initialize the SSP_NSS member */
\r
229 SSP_InitStruct->SSP_NSS = SSP_NSS_Hard;
\r
231 /* Initialize the SSP_SlaveOutput member */
\r
232 SSP_InitStruct->SSP_SlaveOutput = SSP_SlaveOutput_Enable;
\r
234 /* Initialize the SSP_ClockRate member */
\r
235 SSP_InitStruct->SSP_ClockRate = 0;
\r
237 /* Initialize the SSP_ClockPrescaler member */
\r
238 SSP_InitStruct->SSP_ClockPrescaler = 0;
\r
241 /*******************************************************************************
\r
242 * Function Name : SSP_Cmd
\r
243 * Description : Enables or disables the specified SSP peripheral.
\r
244 * Input : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
\r
245 * - NewState: new state of the SSPx peripheral.
\r
246 * This parameter can be: ENABLE or DISABLE.
\r
249 *******************************************************************************/
\r
250 void SSP_Cmd(SSP_TypeDef* SSPx, FunctionalState NewState)
\r
252 if(NewState == ENABLE)
\r
254 /* Enable the SSP peripheral */
\r
255 SSPx->CR1 |= SSP_Enable;
\r
259 /* Disable the SSP peripheral */
\r
260 SSPx->CR1 &= SSP_Disable;
\r
264 /*******************************************************************************
\r
265 * Function Name : SSP_ITConfig
\r
266 * Description : Enables or disables the specified SSP interrupts.
\r
267 * Input : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
\r
268 * - SSP_IT: specifies the SSP interrupts sources to be enabled
\r
269 * or disabled. This parameter can be any combination of the
\r
270 * following values:
\r
271 * - SSP_IT_TxFifo: Transmit FIFO half empty or less interrupt
\r
272 * - SSP_IT_RxFifo: Receive FIFO half full or less interrupt
\r
273 * - SSP_IT_RxTimeOut: Receive timeout interrupt
\r
274 * - SSP_IT_RxOverrun: Receive overrun interrupt
\r
275 * - NewState: new state of the specified SSP interrupts.
\r
276 * This parameter can be: ENABLE or DISABLE.
\r
279 *******************************************************************************/
\r
280 void SSP_ITConfig(SSP_TypeDef* SSPx, u16 SSP_IT, FunctionalState NewState)
\r
282 if(NewState == ENABLE)
\r
284 /* Enable the selected SSP interrupts */
\r
285 SSPx->IMSCR |= SSP_IT;
\r
289 /* Disable the selected SSP interrupts */
\r
290 SSPx->IMSCR &= ~SSP_IT;
\r
294 /*******************************************************************************
\r
295 * Function Name : SSP_DMACmd
\r
296 * Description : Configures the SSP0 DMA interface.
\r
297 * Input : - SSP0_DMAtransfer : specifies the DMA transfer to be
\r
298 * enabled or disabled. This parameter can be one of the
\r
299 * following values:
\r
300 * - SSP0_DMA_Transmit: transmit Fifo DMA transfer
\r
301 * - SSP0_DMA_Receive: receive Fifo DMA transfer
\r
302 * - NewState: new state of SSP0 DMA transfer.
\r
303 * This parameter can be: ENABLE or DISABLE.
\r
306 *******************************************************************************/
\r
307 void SSP_DMACmd(u16 SSP0_DMAtransfer, FunctionalState NewState)
\r
309 if(NewState == ENABLE)
\r
311 if(SSP0_DMAtransfer == SSP0_DMA_Transmit)
\r
313 /* Enable DMA for the transmit FIFO */
\r
314 SSP0->DMACR |= SSP0_DMA_TransmitEnable;
\r
318 /* Enable DMA for the receive FIFO */
\r
319 SSP0->DMACR |= SSP0_DMA_ReceiveEnable;
\r
324 if(SSP0_DMAtransfer == SSP0_DMA_Transmit)
\r
326 /* Disable DMA for the transmit FIFO */
\r
327 SSP0->DMACR &= SSP0_DMA_TransmitDisable;
\r
331 /* Disable DMA for the receive FIFO */
\r
332 SSP0->DMACR &= SSP0_DMA_ReceiveDisable;
\r
337 /*******************************************************************************
\r
338 * Function Name : SSP_DMATxConfig
\r
339 * Description : Configures the SSP0 DMA transmit transfer.
\r
340 * Input : - SSP0_DMATxReq : specifies the SSP0 DMA transmit request to
\r
341 * be enabled. This parameter can be one of the following
\r
343 * - SSP0_DMATxReq_Single: Transmit FIFO DMA single
\r
345 * - SSP0_DMATxReq_Burst: Transmit FIFO DMA burst request
\r
349 *******************************************************************************/
\r
350 void SSP_DMATxConfig(u16 SSP0_DMATxReq)
\r
352 if(SSP0_DMATxReq == SSP0_DMATxReq_Burst)
\r
354 /* Enable DMA transmit burst request */
\r
355 SSP0->DMACR |= SSP0_DMATxReq_Burst;
\r
359 /* Enable DMA transmit single request */
\r
360 SSP0->DMACR &= SSP0_DMATxReq_Single;
\r
364 /*******************************************************************************
\r
365 * Function Name : SSP_DMARxConfig
\r
366 * Description : Configures the SSP0 DMA receive transfer.
\r
367 * Input : - SSP0_DMARxReq : specifies the SSP0 DMA receive request to
\r
368 * be enabled. This parameter can be one of the following
\r
370 * - SSP0_DMARxReq_Single: Receive FIFO DMA burst request
\r
372 * - SSP0_DMARxReq_Burst: Receive FIFO DMA single request
\r
376 *******************************************************************************/
\r
377 void SSP_DMARxConfig(u16 SSP0_DMARxReq)
\r
379 if(SSP0_DMARxReq == SSP0_DMARxReq_Burst)
\r
381 /* Enable DMA receive burst request */
\r
382 SSP0->DMACR |= SSP0_DMARxReq_Burst;
\r
386 /* Enable DMA receive single request */
\r
387 SSP0->DMACR &= SSP0_DMARxReq_Single;
\r
391 /*******************************************************************************
\r
392 * Function Name : SSP_SendData
\r
393 * Description : Transmits a Data through the SSP peripheral.
\r
394 * Input : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
\r
395 * - Data : Data to be transmitted.
\r
398 *******************************************************************************/
\r
399 void SSP_SendData(SSP_TypeDef* SSPx, u16 Data)
\r
401 /* Write in the DR register the data to be sent */
\r
405 /*******************************************************************************
\r
406 * Function Name : SSP_ReceiveData
\r
407 * Description : Returns the most recent received data by the SSP peripheral.
\r
408 * Input : SSPx: where x can be 0 or 1 to select the SSP peripheral.
\r
410 * Return : The value of the received data.
\r
411 *******************************************************************************/
\r
412 u16 SSP_ReceiveData(SSP_TypeDef* SSPx)
\r
414 /* Return the data in the DR register */
\r
418 /*******************************************************************************
\r
419 * Function Name : SSP_LoopBackConfig
\r
420 * Description : Enables or disables the Loop back mode for the selected SSP
\r
422 * Input : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
\r
423 * - NewState: new state of the Loop Back mode.
\r
424 * This parameter can be: ENABLE or DISABLE.
\r
427 *******************************************************************************/
\r
428 void SSP_LoopBackConfig(SSP_TypeDef* SSPx, FunctionalState NewState)
\r
430 if(NewState == ENABLE)
\r
432 /* Enable loop back mode */
\r
433 SSPx->CR1 |= SSP_LoopBackMode_Enable;
\r
437 /* Disable loop back mode */
\r
438 SSPx->CR1 &= SSP_LoopBackMode_Disable;
\r
442 /*******************************************************************************
\r
443 * Function Name : SSP_NSSInternalConfig
\r
444 * Description : Configures by software the NSS pin.
\r
445 * Input : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
\r
446 * - SSP_NSSState: NSS internal state.This parameter can be one
\r
447 * of the following values:
\r
448 * - SSP_NSSInternal_Set: Set NSS pin internally
\r
449 * - SSP_NSSInternal_Reset: Reset NSS pin internally
\r
452 *******************************************************************************/
\r
453 void SSP_NSSInternalConfig(SSP_TypeDef* SSPx, u16 SSP_NSSState)
\r
455 if(SSP_NSSState == SSP_NSSInternal_Set)
\r
457 /* Set NSS pin internally */
\r
458 SSPx->CR1 |= SSP_NSSInternal_Set;
\r
462 /* Reset NSS pin internally */
\r
463 SSPx->CR1 &= SSP_NSSInternal_Reset;
\r
467 /*******************************************************************************
\r
468 * Function Name : SSP_GetFlagStatus
\r
469 * Description : Checks whether the specified SSP flag is set or not.
\r
470 * Input : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
\r
471 * - SSP_FLAG: specifies the flag to check. This parameter can
\r
472 * be one of the following values:
\r
473 * - SSP_FLAG_Busy: busy flag
\r
474 * - SSP_FLAG_RxFifoFull: Receive FIFO full flag
\r
475 * - SSP_FLAG_RxFifoNotEmpty: Receive FIFO not empty flag
\r
476 * - SSP_FLAG_TxFifoNotFull: Transmit FIFO not full flag
\r
477 * - SSP_FLAG_TxFifoEmpty: Transmit FIFO empty flag
\r
478 * - SSP_FLAG_TxFifo: Transmit FIFO half empty or less flag
\r
479 * - SSP_FLAG_RxFifo: Receive FIFO half full or less flag
\r
480 * - SSP_FLAG_RxTimeOut: Receive timeout flag
\r
481 * - SSP_FLAG_RxOverrun: Receive overrun flag
\r
483 * Return : The new state of SSP_FLAG(SET or RESET).
\r
484 *******************************************************************************/
\r
485 FlagStatus SSP_GetFlagStatus(SSP_TypeDef* SSPx, u16 SSP_FLAG)
\r
487 u32 SSPReg = 0, FlagPos = 0;
\r
490 /* Get the SSP register index */
\r
491 SSPReg = SSP_FLAG >> 5;
\r
493 /* Get the flag position */
\r
494 FlagPos = SSP_FLAG & SSP_Flag_Mask;
\r
496 /* Find the register of the flag to check */
\r
499 /* The flag to check is in SR register */
\r
500 StatusReg = SSPx->SR;
\r
502 else if (SSPReg == 2)
\r
504 /* The flag to check is in RISR register */
\r
505 StatusReg = SSPx->RISR;
\r
508 /* Check the status of the specified SSP flag */
\r
509 if((StatusReg & (1 << FlagPos)) != RESET)
\r
511 /* Return SET if the SSP flag is set */
\r
516 /* Return RESET if the SSP flag is reset */
\r
521 /*******************************************************************************
\r
522 * Function Name : SSP_ClearFlag
\r
523 * Description : Clears the SSPx
\92s pending flags.
\r
524 * Input : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
\r
525 * - SSP_FLAG: specifies the flag to clear. This parameter can
\r
526 * be one of the following values:
\r
527 * - SSP_FLAG_RxTimeOut: Receive timeout flag
\r
528 * - SSP_FLAG_RxOverrun: Receive overrun flag
\r
531 *******************************************************************************/
\r
532 void SSP_ClearFlag(SSP_TypeDef* SSPx, u16 SSP_FLAG)
\r
536 /* Get the flag position */
\r
537 FlagPos = SSP_FLAG & SSP_Flag_Mask;
\r
539 /* Clear the selected SSP flag */
\r
540 SSPx->ICR = (1 << FlagPos);
\r
543 /*******************************************************************************
\r
544 * Function Name : SSP_GetITStatus
\r
545 * Description : Checks whether the specified SSP interrupt has occurred or not.
\r
546 * Input : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
\r
547 * - SSP_IT: specifies the interrupt source to check.
\r
548 * This parameter can be one of the following values:
\r
549 * - SSP_IT_TxFifo: Transmit FIFO half empty or less interrupt
\r
550 * - SSP_IT_RxFifo: Receive FIFO half full or less interrupt
\r
551 * - SSP_IT_RxTimeOut: Receive timeout interrupt
\r
552 * - SSP_IT_RxOverrun: Receive overrun interrupt
\r
554 * Return : The new state of SSP_IT(SET or RESET).
\r
555 *******************************************************************************/
\r
556 ITStatus SSP_GetITStatus(SSP_TypeDef* SSPx, u16 SSP_IT)
\r
558 /* Check the status of the specified interrupt flag */
\r
559 if((SSPx->MISR & SSP_IT) != RESET)
\r
561 /* Return SET if the SSP interrupt flag is set */
\r
566 /* Return RESET if SSP interrupt flag is reset */
\r
571 /*******************************************************************************
\r
572 * Function Name : SSP_ClearITPendingBit
\r
573 * Description : Clears the SSPx
\92s interrupt pending bits.
\r
574 * Input : - SSPx: where x can be 0 or 1 to select the SSP peripheral.
\r
575 * - SSP_IT: specifies the interrupt pending bit to clear.
\r
576 * This parameter can be any combination of the following values:
\r
577 * - SSP_IT_RxTimeOut: Receive timeout interrupt
\r
578 * - SSP_IT_RxOverrun: Receive overrun interrupt
\r
581 *******************************************************************************/
\r
582 void SSP_ClearITPendingBit(SSP_TypeDef* SSPx, u16 SSP_IT)
\r
584 /* Clear the selected SSP interrupts pending bits */
\r
585 SSPx->ICR = SSP_IT;
\r
588 /******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/
\r