mirror of
https://github.com/elua/elua.git
synced 2025-01-25 01:02:54 +08:00
added SPI support for STR912
This commit is contained in:
parent
1ca3b324b5
commit
abefe29667
466
src/platform/str9/91x_ssp.c
Normal file
466
src/platform/str9/91x_ssp.c
Normal file
@ -0,0 +1,466 @@
|
||||
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
|
||||
* File Name : 91x_ssp.c
|
||||
* Author : MCD Application Team
|
||||
* Version : V2.1
|
||||
* Date : 12/22/2008
|
||||
* Description : This file provides all the SSP firmware functions.
|
||||
********************************************************************************
|
||||
* 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_LoopBackConfig
|
||||
* 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_LoopBackConfig(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 2008 STMicroelectronics *****END OF FILE****/
|
||||
|
116
src/platform/str9/91x_ssp.h
Normal file
116
src/platform/str9/91x_ssp.h
Normal file
@ -0,0 +1,116 @@
|
||||
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
|
||||
* File Name : 91x_ssp.h
|
||||
* Author : MCD Application Team
|
||||
* Version : V2.1
|
||||
* Date : 12/22/2008
|
||||
* Description : This file contains all the functions prototypes for the
|
||||
* SSP firmware library.
|
||||
********************************************************************************
|
||||
* 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.
|
||||
*******************************************************************************/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __91x_SSP_H
|
||||
#define __91x_SSP_H
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "91x_map.h"
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* SSP Init structure definition */
|
||||
typedef struct
|
||||
{
|
||||
u16 SSP_FrameFormat ;
|
||||
u16 SSP_Mode ;
|
||||
u16 SSP_CPOL ;
|
||||
u16 SSP_CPHA ;
|
||||
u16 SSP_DataSize ;
|
||||
u16 SSP_SlaveOutput ;
|
||||
u8 SSP_ClockRate ;
|
||||
u8 SSP_ClockPrescaler ;
|
||||
}SSP_InitTypeDef;
|
||||
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* SSP Frame Format Select */
|
||||
#define SSP_FrameFormat_TI 0x0010
|
||||
#define SSP_FrameFormat_Motorola 0xFFCF
|
||||
#define SSP_FrameFormat_Microwire 0x0020
|
||||
|
||||
/* SSP Master/Slave Select */
|
||||
#define SSP_Mode_Master 0xFFFB
|
||||
#define SSP_Mode_Slave 0x0004
|
||||
|
||||
/* SSP Clock Polarity */
|
||||
#define SSP_CPOL_Low 0xFFBF
|
||||
#define SSP_CPOL_High 0x0040
|
||||
|
||||
/* SSP Clock Phase */
|
||||
#define SSP_CPHA_1Edge 0xFF7F
|
||||
#define SSP_CPHA_2Edge 0x0080
|
||||
|
||||
/* SSP Data Size */
|
||||
#define SSP_DataSize_16b 0x000F
|
||||
#define SSP_DataSize_15b 0x000E
|
||||
#define SSP_DataSize_14b 0x000D
|
||||
#define SSP_DataSize_13b 0x000C
|
||||
#define SSP_DataSize_12b 0x000B
|
||||
#define SSP_DataSize_11b 0x000A
|
||||
#define SSP_DataSize_10b 0x0009
|
||||
#define SSP_DataSize_9b 0x0008
|
||||
#define SSP_DataSize_8b 0x0007
|
||||
#define SSP_DataSize_7b 0x0006
|
||||
#define SSP_DataSize_6b 0x0005
|
||||
#define SSP_DataSize_5b 0x0004
|
||||
#define SSP_DataSize_4b 0x0003
|
||||
|
||||
/* SSP Slave output config */
|
||||
#define SSP_SlaveOutput_Enable 0xFFF7
|
||||
#define SSP_SlaveOutput_Disable 0x0008
|
||||
|
||||
/* SSP Interrupts */
|
||||
#define SSP_IT_TxFifo 0x0008
|
||||
#define SSP_IT_RxFifo 0x0004
|
||||
#define SSP_IT_RxTimeOut 0x0002
|
||||
#define SSP_IT_RxOverrun 0x0001
|
||||
|
||||
/* SSP Flags */
|
||||
#define SSP_FLAG_Busy 0x0024
|
||||
#define SSP_FLAG_RxFifoFull 0x0023
|
||||
#define SSP_FLAG_RxFifoNotEmpty 0x0022
|
||||
#define SSP_FLAG_TxFifoNotFull 0x0021
|
||||
#define SSP_FLAG_TxFifoEmpty 0x0020
|
||||
#define SSP_FLAG_TxFifo 0x0043
|
||||
#define SSP_FLAG_RxFifo 0x0042
|
||||
#define SSP_FLAG_RxTimeOut 0x0041
|
||||
#define SSP_FLAG_RxOverrun 0x0040
|
||||
|
||||
/* SSP DMA Requests */
|
||||
#define SSP_DMA_Transmit 0x0002
|
||||
#define SSP_DMA_Receive 0x0001
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
|
||||
void SSP_DeInit(SSP_TypeDef* SSPx);
|
||||
void SSP_Init(SSP_TypeDef* SSPx, SSP_InitTypeDef* SSP_InitStruct);
|
||||
void SSP_StructInit(SSP_InitTypeDef* SSP_InitStruct);
|
||||
void SSP_Cmd(SSP_TypeDef* SSPx, FunctionalState NewState);
|
||||
void SSP_ITConfig(SSP_TypeDef* SSPx, u16 SSP_IT, FunctionalState NewState);
|
||||
void SSP_DMACmd(SSP_TypeDef* SSPx, u16 SSP_DMATransfert, FunctionalState NewState);
|
||||
void SSP_SendData(SSP_TypeDef* SSPx, u16 Data);
|
||||
u16 SSP_ReceiveData(SSP_TypeDef* SSPx);
|
||||
void SSP_LoopBackConfig(SSP_TypeDef* SSPx, FunctionalState NewState);
|
||||
FlagStatus SSP_GetFlagStatus(SSP_TypeDef* SSPx, u16 SSP_FLAG);
|
||||
void SSP_ClearFlag(SSP_TypeDef* SSPx, u16 SSP_FLAG);
|
||||
ITStatus SSP_GetITStatus(SSP_TypeDef* SSPx, u16 SSP_IT);
|
||||
void SSP_ClearITPendingBit(SSP_TypeDef* SSPx, u16 SSP_IT);
|
||||
|
||||
#endif /* __91x_SSP_H */
|
||||
|
||||
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
local cpumode = ( builder:get_option( 'cpumode' ) or 'arm' ):lower()
|
||||
|
||||
specific_files = "startup912.s startup_generic.s platform.c 91x_scu.c 91x_fmi.c 91x_gpio.c 91x_uart.c 91x_tim.c 91x_vic.c interrupt.c str9_pio.c 91x_i2c.c 91x_wiu.c 91x_adc.c platform_int.c"
|
||||
specific_files = "startup912.s startup_generic.s platform.c 91x_scu.c 91x_fmi.c 91x_gpio.c 91x_uart.c 91x_tim.c 91x_vic.c interrupt.c str9_pio.c 91x_i2c.c 91x_wiu.c 91x_adc.c platform_int.c 91x_ssp.c"
|
||||
|
||||
local ldscript = "str912fw44.lds"
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
cpumode = ARGUMENTS.get( 'cpumode', 'arm' ).lower()
|
||||
|
||||
specific_files = "startup912.s startup_generic.s platform.c 91x_scu.c 91x_fmi.c 91x_gpio.c 91x_uart.c 91x_tim.c 91x_vic.c interrupt.c str9_pio.c 91x_i2c.c 91x_wiu.c 91x_adc.c platform_int.c"
|
||||
specific_files = "startup912.s startup_generic.s platform.c 91x_scu.c 91x_fmi.c 91x_gpio.c 91x_uart.c 91x_tim.c 91x_vic.c interrupt.c str9_pio.c 91x_i2c.c 91x_wiu.c 91x_adc.c platform_int.c 91x_ssp.c"
|
||||
|
||||
# Check CPU
|
||||
if comp[ 'cpu' ] == 'STR912FAW44':
|
||||
|
@ -24,6 +24,8 @@
|
||||
#include "buf.h"
|
||||
#include "elua_adc.h"
|
||||
#include "91x_adc.h"
|
||||
#include "91x_ssp.h"
|
||||
#include "utils.h"
|
||||
|
||||
// ****************************************************************************
|
||||
// Platform initialization
|
||||
@ -51,6 +53,8 @@ static void platform_config_scu()
|
||||
SCU_PCLKDivisorConfig(SCU_PCLK_Div2);
|
||||
/* Set the HCLK Clock to MCLK */
|
||||
SCU_HCLKDivisorConfig(SCU_HCLK_Div1);
|
||||
/* Set the BRCLK Clock to MCLK */
|
||||
SCU_BRCLKDivisorConfig(SCU_BRCLK_Div1);
|
||||
|
||||
// Enable VIC clock
|
||||
SCU_AHBPeriphClockConfig(__VIC, ENABLE);
|
||||
@ -80,6 +84,12 @@ static void platform_config_scu()
|
||||
|
||||
// Enable the ADC clocks
|
||||
SCU_APBPeriphClockConfig(__ADC, ENABLE);
|
||||
|
||||
// Enable the SSP clocks
|
||||
SCU_APBPeriphClockConfig(__SSP0,ENABLE);
|
||||
SCU_APBPeriphReset(__SSP0,DISABLE);
|
||||
SCU_APBPeriphClockConfig(__SSP1,ENABLE);
|
||||
SCU_APBPeriphReset(__SSP1,DISABLE);
|
||||
}
|
||||
|
||||
// Port/pin definitions of the eLua UART connection for different boards
|
||||
@ -785,6 +795,99 @@ int platform_i2c_recv_byte( unsigned id, int ack )
|
||||
return I2C_ReceiveData( pi2c );
|
||||
}
|
||||
|
||||
// ****************************************************************************
|
||||
// SPI
|
||||
|
||||
#define SPI_MAX_PRESCALER ( 254 * 256 )
|
||||
#define SPI_MIN_PRESCALER 2
|
||||
|
||||
u32 platform_spi_setup( unsigned id, int mode, u32 clock, unsigned cpol, unsigned cpha, unsigned databits )
|
||||
{
|
||||
const u32 basefreq = CPU_FREQUENCY;
|
||||
u32 prescaler, divider = 1, temp, mindiff = 0xFFFFFFFF, minp;
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
SSP_InitTypeDef SSP_InitStructure;
|
||||
|
||||
clock = UMIN( clock, basefreq >> 1 );
|
||||
prescaler = UMIN( basefreq / clock, SPI_MAX_PRESCALER );
|
||||
if( basefreq / prescaler > clock )
|
||||
prescaler ++;
|
||||
if( prescaler & 1 )
|
||||
prescaler ++;
|
||||
if( prescaler > 254 )
|
||||
{
|
||||
temp = prescaler;
|
||||
for( prescaler = minp = 2; prescaler <= 254; prescaler += 2 )
|
||||
{
|
||||
divider = temp / prescaler;
|
||||
if( divider <= 255 )
|
||||
{
|
||||
if( ABSDIFF( divider * prescaler, temp ) < mindiff )
|
||||
{
|
||||
mindiff = ABSDIFF( divider * prescaler, temp );
|
||||
minp = prescaler;
|
||||
if( mindiff == 0 )
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
prescaler = minp;
|
||||
divider = temp / prescaler;
|
||||
}
|
||||
|
||||
// GPIO setup
|
||||
// Fixed assignment:
|
||||
// P5.4 - SCLK
|
||||
// P5.5 - MOSI
|
||||
// P5.6 - MISO
|
||||
// P5.7 - CS (not explicitly handled by the SPI module)
|
||||
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
|
||||
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
|
||||
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2 ;
|
||||
GPIO_Init(GPIO5, &GPIO_InitStructure);
|
||||
GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
|
||||
GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
|
||||
GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ;
|
||||
GPIO_Init(GPIO5, &GPIO_InitStructure);
|
||||
|
||||
// Actual SPI setup
|
||||
SSP_DeInit(SSP0);
|
||||
SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
|
||||
SSP_InitStructure.SSP_Mode = SSP_Mode_Master;
|
||||
SSP_InitStructure.SSP_CPOL = cpol == 0 ? SSP_CPOL_Low : SSP_CPOL_High;
|
||||
SSP_InitStructure.SSP_CPHA = cpha == 0 ? SSP_CPHA_1Edge : SSP_CPHA_2Edge;
|
||||
SSP_InitStructure.SSP_DataSize = databits - 1;
|
||||
SSP_InitStructure.SSP_ClockRate = divider - 1;
|
||||
SSP_InitStructure.SSP_ClockPrescaler = prescaler;
|
||||
SSP_Init(SSP0, &SSP_InitStructure);
|
||||
|
||||
// Enable peripheral
|
||||
SSP_Cmd(SSP0, ENABLE);
|
||||
|
||||
// All done
|
||||
return basefreq / ( prescaler * divider );
|
||||
}
|
||||
|
||||
spi_data_type platform_spi_send_recv( unsigned id, spi_data_type data )
|
||||
{
|
||||
// Send byte through the SSP0 peripheral
|
||||
SSP0->DR = data;
|
||||
// Loop while Transmit FIFO is full
|
||||
while(SSP_GetFlagStatus(SSP0, SSP_FLAG_TxFifoEmpty) == RESET);
|
||||
// Loop while Receive FIFO is empty
|
||||
while(SSP_GetFlagStatus(SSP0, SSP_FLAG_RxFifoNotEmpty) == RESET);
|
||||
// Return the byte read from the SSP bus
|
||||
return SSP0->DR;
|
||||
}
|
||||
|
||||
void platform_spi_select( unsigned id, int is_select )
|
||||
{
|
||||
id = id;
|
||||
is_select = is_select;
|
||||
}
|
||||
|
||||
// ****************************************************************************
|
||||
// Platform specific modules go here
|
||||
|
||||
@ -811,7 +914,6 @@ LUALIB_API int luaopen_platform( lua_State *L )
|
||||
lua_newtable( L );
|
||||
luaL_register( L, NULL, str9_pio_map );
|
||||
lua_setfield( L, -2, "pio" );
|
||||
|
||||
return 1;
|
||||
#endif // #if LUA_OPTIMIZE_MEMORY > 0
|
||||
}
|
||||
|
@ -46,7 +46,7 @@
|
||||
|
||||
// Number of resources (0 if not available/not implemented)
|
||||
#define NUM_PIO 10
|
||||
#define NUM_SPI 0
|
||||
#define NUM_SPI 1
|
||||
#define NUM_UART 3
|
||||
#define NUM_PWM 4
|
||||
#define NUM_ADC 8
|
||||
@ -130,6 +130,8 @@ u32 SCU_GetMCLKFreqValue();
|
||||
_ROM( AUXLIB_CPU, luaopen_cpu, cpu_map)\
|
||||
_ROM( AUXLIB_CPU, luaopen_elua, elua_map)\
|
||||
_ROM( AUXLIB_I2C, luaopen_i2c, i2c_map)\
|
||||
_ROM( AUXLIB_SPI, luaopen_spi, spi_map)\
|
||||
_ROM( AUXLIB_ELUA, luaopen_elua, elua_map)\
|
||||
RPCLINE\
|
||||
_ROM( AUXLIB_PWM, luaopen_pwm, pwm_map)\
|
||||
_ROM( LUA_MATHLIBNAME, luaopen_math, math_map )\
|
||||
|
Loading…
x
Reference in New Issue
Block a user