2013-10-23

SPI loop back test again

/***********************************************************************//**
 * @file : LPC11xx_ssp.h
 * @brief : Contains all macro definitions and function prototypes
 * support for SSP firmware library on LPC11xx
 * @version : 1.0
 * @date : 21. Jan. 2010
 * @author : Coocox
 **************************************************************************/

void SSP_SendData(SSP_TypeDef* SSPx, uint16_t Data);
uint16_t SSP_ReceiveData(SSP_TypeDef* SSPx);
void SSP_LoopBackCmd(SSP_TypeDef* SSPx, FunctionalState NewState);





/***********************************************************************//**
 * @file : lpc11xx_ssp.c
 * @brief : Contains all functions support for SSP firmware library on LPC11xx
 * @version : 1.0
 * @date : 21. Jan. 2010
 * @author : Coocox
 **********************************************************************/

/*********************************************************************//**
 * @brief Transmit a single data through SSPx peripheral
 * @param[in] SSPx SSP peripheral selected, should be SSP
 * @param[in] Data Data to transmit (must be 16 or 8-bit long,
 * this depend on SSP data bit number configured)
 * @return none
 **********************************************************************/
void SSP_SendData(SSP_TypeDef* SSPx, uint16_t Data)
{
CHECK_PARAM(PARAM_SSPx(SSPx));

SSPx->DR = SSP_DR_BITMASK(Data);
}

/*********************************************************************//**
 * @brief Receive a single data from SSPx peripheral
 * @param[in] SSPx SSP peripheral selected, should be SSP
 * @return Data received (16-bit long)
 **********************************************************************/
uint16_t SSP_ReceiveData(SSP_TypeDef* SSPx)
{
CHECK_PARAM(PARAM_SSPx(SSPx));

return ((uint16_t) (SSP_DR_BITMASK(SSPx->DR)));
}


/*********************************************************************//**
 * @brief Enable or disable Loop Back mode function in SSP peripheral
 * @param[in] SSPx SSP peripheral selected, should be SSP0 or SSP1
 * @param[in] NewState New State of Loop Back mode, should be:
 * - ENABLE: Enable this function
 * - DISABLE: Disable this function
 * @return None
 **********************************************************************/
void SSP_LoopBackCmd(SSP_TypeDef* SSPx, FunctionalState NewState)
{
CHECK_PARAM(PARAM_SSPx(SSPx));
CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));

if (NewState == ENABLE)
{
SSPx->CR1 |= SSP_CR1_LBM_EN;
}
else
{
SSPx->CR1 &= (~SSP_CR1_LBM_EN) & SSP_CR1_BITMASK;
}
}




*** C pointer tutorial ***

http://www.tutorialspoint.com/cprogramming/c_pointers.htm




***** ssp1_test ***

http://www.coocox.org/forum/topic.php?id=218

void ssp1_test(void)
{
   SSP_CFG_Type cfg;
   SSP_DATA_SETUP_Type dcfg;
   uint8_t wbuf[3];
   uint8_t rbuf[3];      

   SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP1, ENABLE);
   SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_SSP1, DISABLE);    

   SYSCON_SetSPI1ClockDiv (10);  

   SSP_ConfigStructInit(&cfg);
  
   SSP_SSP1PinsInit(ENABLE);      
   SSP_Init(LPC_SSP1, &cfg);
   SSP_Cmd(LPC_SSP1, ENABLE);

   dcfg.rx_data = (void*)rbuf;
   dcfg.tx_data = (void*)wbuf;
   dcfg.length = 3;
   
   SSP_ReadWrite(LPC_SSP1, &dcfg, SSP_TRANSFER_POLLING);
}

/**
 * @brief SPI Data configuration structure definitions
 */

typedef struct {
void *tx_data; /**< Pointer to transmit data */
uint32_t tx_cnt; /**< Transmit counter */
void *rx_data; /**< Pointer to transmit data */
uint32_t rx_cnt; /**< Receive counter */
uint32_t length; /**< Length of transfer data */
uint32_t status; /**< Current status of SSP activity */
void (*callback)(void); /**< Pointer to Call back function when transmission complete
used in interrupt transfer mode */
} SSP_DATA_SETUP_Type;




int32_t SSP_ReadWrite (SSP_TypeDef *SSPx, SSP_DATA_SETUP_Type *dataCfg, \
SSP_TRANSFER_Type xfType);




/*********************************************************************//**
 * @brief SSP Read write data function
 * @param[in] SSPx Pointer to SSP peripheral, should be SSP0 or SSP1
 * @param[in] dataCfg Pointer to a SSP_DATA_SETUP_Type structure that
 * contains specified information about transmit
 * data configuration.
 * @param[in] xfType Transfer type, should be:
 * - SSP_TRANSFER_POLLING: Polling mode
 * - SSP_TRANSFER_INTERRUPT: Interrupt mode
 * @return Actual Data length has been transferred in polling mode.
 * In interrupt mode, always return (0)
 * Return (-1) if error.
 * Note: This function can be used in both master and slave mode.
 ***********************************************************************/
int32_t SSP_ReadWrite (SSP_TypeDef *SSPx, SSP_DATA_SETUP_Type *dataCfg, \
SSP_TRANSFER_Type xfType)
{
uint8_t *rdata8;
    uint8_t *wdata8;
uint16_t *rdata16;
    uint16_t *wdata16;
    uint32_t stat;
    uint32_t tmp;
    int32_t sspnum;
    int32_t dataword;

    dataCfg->rx_cnt = 0;
    dataCfg->tx_cnt = 0;
    dataCfg->status = 0;

/* Clear all remaining data in RX FIFO */
    ... 

// Clear status
    ...

// Polling mode ----------------------------------------------------------------------
if (xfType == SSP_TRANSFER_POLLING){
if (dataword == 0){
rdata8 = (uint8_t *)dataCfg->rx_data;
wdata8 = (uint8_t *)dataCfg->tx_data;
} else {
rdata16 = (uint16_t *)dataCfg->rx_data;
wdata16 = (uint16_t *)dataCfg->tx_data;
}
while ((dataCfg->tx_cnt != dataCfg->length) || (dataCfg->rx_cnt != dataCfg->length)){
if ((SSPx->SR & SSP_SR_TNF) && (dataCfg->tx_cnt != dataCfg->length)){
// Write data to buffer
if(dataCfg->tx_data == NULL){
if (dataword == 0){
SSP_SendData(SSPx, 0xFF);
dataCfg->tx_cnt++;
} else {
SSP_SendData(SSPx, 0xFFFF);
dataCfg->tx_cnt += 2;
}
} else {
if (dataword == 0){
SSP_SendData(SSPx, *wdata8);
wdata8++;
dataCfg->tx_cnt++;
} else {
SSP_SendData(SSPx, *wdata16);
wdata16++;
dataCfg->tx_cnt += 2;
}
}
}

// Check overrun error
...

// Check for any data available in RX FIFO
            ...

// save status
        ... 
}

// Interrupt mode ----------------------------------------------------------------------

    ...

return (-1);
}

/*********************************************************************//**
 * @brief Transmit a single data through SSPx peripheral
 * @param[in] SSPx SSP peripheral selected, should be SSP
 * @param[in] Data Data to transmit (must be 16 or 8-bit long,
 * this depend on SSP data bit number configured)
 * @return none
 **********************************************************************/
void SSP_SendData(SSP_TypeDef* SSPx, uint16_t Data)
{
CHECK_PARAM(PARAM_SSPx(SSPx));

SSPx->DR = SSP_DR_BITMASK(Data);
}

.END



No comments:

Post a Comment