LPC4337 SPI接口做从模式,硬件对接口外围有什么具体的要求?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPC4337 SPI接口做从模式,硬件对接口外围有什么具体的要求?

822 Views
weipingid
Contributor I

现在LPC4337 SPI接口做从模式,时钟中断请求可以。但是数据通讯有问题。

Labels (1)
0 Kudos
3 Replies

664 Views
kerryzhou
NXP TechSupport
NXP TechSupport

楼主你好。

对于硬件,你在配置引脚的时候,使能上拉就可以了。

关于数据有问题,不知道具体是什么问题, 我建议你可以参考官方的代码去测试看看。

lpcopen代码的下载链接:

LPCOpen Software for LPC43XX | NXP 


Have a great day,
Kerry

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos

664 Views
weipingid
Contributor I

你好!

I/O 已经做了上拉,SSP1_SCK,CS中断也正常数据主设备有发送数据,LCP4337,中断应对数据有但是无法读取数据。软件代码如下:

#include "lpc43xx_ssp.h"
#include "lpc43xx_scu.h"
#include "lpc43xx_cgu.h"
#include "lpc43xx_libcfg.h"
#include "debug_frmwrk.h"


/************************** PRIVATE DEFINTIONS *************************/
/** Max buffer length */
#define BUFFER_SIZE   0x40

// SSP Configuration structure variable
SSP_CFG_Type SSP_ConfigStruct;
//SSP Data setup structure variable
SSP_DATA_SETUP_Type xferConfig;

// Tx buffer
uint8_t Tx_Buf[BUFFER_SIZE];
// Rx buffer
uint8_t Rx_Buf[BUFFER_SIZE];

/* Status Flag indicates current SSP transmission complete or not */
__IO FlagStatus complete;

/************************** PRIVATE FUNCTIONS *************************/
void SSP1_IRQHandler(void);


void Buffer_Init(void);
void Error_Loop(void);


/*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/
/*********************************************************************//**
 * @brief   SSP1 Interrupt used for reading and writing handler
 * @param  None
 * @return   None
 ***********************************************************************/
void SSP1_IRQHandler(void)
{
 SSP_DATA_SETUP_Type *xf_setup;
 uint16_t tmp;
 uint8_t dataword;
 
 
 printf("SSP1_IRQHandler start\r\n");

 // Disable interrupt
 LPC_SSP1->IMSC = 0;

 if(SSP_GetDataSize(LPC_SSP1) > SSP_DATABIT_8)
  dataword = 1;
 else
  dataword = 0;
 xf_setup = &xferConfig;
 // save status
 tmp = LPC_SSP1->RIS;
 xf_setup->status = tmp;

 // Check overrun error
 if (tmp & SSP_RIS_ROR){
  // Clear interrupt
  LPC_SSP1->ICR = SSP_RIS_ROR;
  // update status
  xf_setup->status |= SSP_STAT_ERROR;
  // Set Complete Flag
  complete = SET;
  
  printf("tmp:%2x\r\n",(uint8_t) tmp);
  
  LPC_SSP1->IMSC = SSP_IMSC_BITMASK;
  
  printf("SSP1_IRQHandler Check overrun error 1\r\n");
  
  return;
 }

 if ((xf_setup->tx_cnt != xf_setup->length) || (xf_setup->rx_cnt != xf_setup->length)){
  /* check if RX FIFO contains data */
  while ((LPC_SSP1->SR & SSP_SR_RNE) && (xf_setup->rx_cnt != xf_setup->length)){
   // Read data from SSP data
   tmp = SSP_ReceiveData(LPC_SSP1);

   // Store data to destination
   if (xf_setup->rx_data != NULL)
   {
    if (dataword == 0){
     *(uint8_t *)((uint32_t)xf_setup->rx_data + xf_setup->rx_cnt) = (uint8_t) tmp;
     printf("rx:%2x\r\n",(uint8_t) tmp);
    } else {
     *(uint16_t *)((uint32_t)xf_setup->rx_data + xf_setup->rx_cnt) = (uint16_t) tmp;
    }
   }
   // Increase counter
   if (dataword == 0){
    xf_setup->rx_cnt++;
   } else {
    xf_setup->rx_cnt += 2;
   }
  }

  while ((LPC_SSP1->SR & SSP_SR_TNF) && (xf_setup->tx_cnt != xf_setup->length)){
   // Write data to buffer
   if(xf_setup->tx_data == NULL){
    if (dataword == 0){
     SSP_SendData(LPC_SSP1, 0xEE);
     xf_setup->tx_cnt++;
    } else {
     SSP_SendData(LPC_SSP1, 0xEEEE);
     xf_setup->tx_cnt += 2;
    }
   } else {
    if (dataword == 0){
     SSP_SendData(LPC_SSP1, (*(uint8_t *)((uint32_t)xf_setup->tx_data + xf_setup->tx_cnt)));
     xf_setup->tx_cnt++;
    } else {
     SSP_SendData(LPC_SSP1, (*(uint16_t *)((uint32_t)xf_setup->tx_data + xf_setup->tx_cnt)));
     xf_setup->tx_cnt += 2;
    }
   }

   // Check overrun error
   if ((tmp = LPC_SSP1->RIS) & SSP_RIS_ROR){
    // update status
    xf_setup->status |= SSP_STAT_ERROR;
    // Set Complete Flag
    complete = SET;
    LPC_SSP1->IMSC = SSP_IMSC_BITMASK;
    printf("SSP1_IRQHandler Check overrun error 2\r\n");
    return;
   }

   // Check for any data available in RX FIFO
   while ((LPC_SSP1->SR & SSP_SR_RNE) && (xf_setup->rx_cnt != xf_setup->length)){
    // Read data from SSP data
    tmp = SSP_ReceiveData(LPC_SSP1);

    // Store data to destination
    if (xf_setup->rx_data != NULL)
    {
     if (dataword == 0){
      *(uint8_t *)((uint32_t)xf_setup->rx_data + xf_setup->rx_cnt) = (uint8_t) tmp;
      printf("rx1:%2x\r\n",(uint8_t) tmp);
     } else {
      *(uint16_t *)((uint32_t)xf_setup->rx_data + xf_setup->rx_cnt) = (uint16_t) tmp;
     }
    }
    // Increase counter
    if (dataword == 0){
     xf_setup->rx_cnt++;
    } else {
     xf_setup->rx_cnt += 2;
    }
   }
  }
 }

 
 // If there more data to sent or receive
 if ((xf_setup->rx_cnt != xf_setup->length) || (xf_setup->tx_cnt != xf_setup->length)){
  // Enable all interrupt
  LPC_SSP1->IMSC = SSP_IMSC_BITMASK;
  
  printf("SSP1_IRQHandler more data need to send\r\n");
  
 } else {
  // Save status
  xf_setup->status = SSP_STAT_DONE;
  // Set Complete Flag
  complete = SET;
  
  
  LPC_SSP1->IMSC = SSP_IMSC_BITMASK;
  printf("SSP1_IRQHandler data end\r\n");
  
 }
 
 //SSP_SendData(LPC_SSP1, 0x12);
 
 printf("SSP1_IRQHandler end\r\n");
 
}

/*-------------------------PRIVATE FUNCTIONS------------------------------*/
/*********************************************************************//**
 * @brief  Initialize buffer
 * @param[in] None
 * @return   None
 **********************************************************************/
void Buffer_Init(void)
{
 uint8_t i;

 for (i = 0; i < BUFFER_SIZE; i++) {
  Tx_Buf[i] = i;
  Rx_Buf[i] = 0;
 }
}


int spi_slave_init(void)
{


 /* Configure SSP1 pins*/
 /*scu_pinmux(0xF,4,MD_PLN_FAST,FUNC0); //  func2=SSP1 SCK0
  scu_pinmux(0xF,5,MD_PLN_FAST,FUNC2); //  func2=SSP1 SSEL0
  scu_pinmux(0xF,6,MD_PLN_FAST,FUNC2); //  func2=SSP1 MISO0
  scu_pinmux(0xF,7,MD_PLN_FAST,FUNC2); //  func2=SSP1 MOSI0*/
 
 scu_pinmux(0x0F, 4, MD_PLN_FAST, FUNC0);     // SCK0
  scu_pinmux(0x01, 3, MD_PLN_FAST, FUNC5); // MISO0
  scu_pinmux(0x01, 4, MD_PLN_FAST, FUNC5); // MOSI
 scu_pinmux(0x01, 5, MD_PLN_FAST, FUNC5); // CS
 

 // initialize SSP configuration structure to default
 SSP_ConfigStructInit(&SSP_ConfigStruct);
 
 SSP_ConfigStruct.CPHA = SSP_CPHA_SECOND;
 SSP_ConfigStruct.CPOL = SSP_CPOL_LO;
 SSP_ConfigStruct.ClockRate = 5000000;
 SSP_ConfigStruct.Databit = SSP_DATABIT_8;
 SSP_ConfigStruct.Mode = SSP_SLAVE_MODE;
 
 // Initialize SSP peripheral with parameter given in structure above
 SSP_Init(LPC_SSP1, &SSP_ConfigStruct);

    /* preemption = 1, sub-priority = 1 */
    NVIC_SetPriority(SSP1_IRQn, ((0x01<<3)|0x01));
    /* Enable SSP1 interrupt */
    NVIC_EnableIRQ(SSP1_IRQn);

 // Enable SSP peripheral
 SSP_Cmd(LPC_SSP1, ENABLE);

 Buffer_Init();

 xferConfig.tx_data = Tx_Buf;
 xferConfig.rx_data = Rx_Buf;
 xferConfig.length = BUFFER_SIZE;
 SSP_ReadWrite(LPC_SSP1, &xferConfig, SSP_TRANSFER_INTERRUPT);
 
 printf("spi_slave_init ok\r\n");


}


/**
 * @}
 */

0 Kudos

664 Views
kerryzhou
NXP TechSupport
NXP TechSupport

楼主你好,

    有没有跑过官方代码?

   另外,你有没有测试过SPI 总线上数据,是否有数据在MOSI上面?如果MOSI上面没数据,从机肯定接不到。如果有数据,从机接不到,就是代码问题,用官方代码试试。

  


Have a great day,
Kerry

-------------------------------------------------------------------------------
Note:
- If this post answers your question, please click the "Mark Correct" button. Thank you!

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos