Hello everyone ,
i am currently working on a project where i have to use LPC11C14 to communicate with another measurement instrument through SPI. I have to send some commands and read data from the instrument every second. But i can't get any data from that, also i checked the SPI with oscilloscope there are no data coming out. The program i wrote is based on the example in the LPCOpen. Can someone check my code and give me some instruction here? I really tried to find problem but i get nothing. Here is the code.
#include "board.h"
#include <cr_section_macros.h>
/*****************************************************************************
* Private types/enumerations/variables
****************************************************************************
****************************************************************************/
// For SSP Communication
#define BUFFER_SIZE (0x10)
#define SSP_DATA_BITS (SSP_BITS_8)
#define SSP_DATA_BIT_NUM(databits) (databits + 1)
#define SSP_DATA_BYTES(databits) (((databits) > SSP_BITS_8) ? 2 : 1)
#define SSP_LO_BYTE_MSK(databits) ((SSP_DATA_BYTES(databits) > 1) ? 0xFF : (0xFF >> \
(8 - SSP_DATA_BIT_NUM(databits))))
#define SSP_HI_BYTE_MSK(databits) ((SSP_DATA_BYTES(databits) > 1) ? (0xFF >> \
(16 - SSP_DATA_BIT_NUM(databits))) : 0)
#define LPC_SSP LPC_SSP0
#define SSP_IRQ SSP0_IRQn
#define SSPIRQHANDLER SSP0_IRQHandler
/* Tx buffer */
static uint8_t Tx_Buf[24]; // RAM
/* Rx buffer */
static uint8_t Rx_Buf[8];
static SSP_ConfigFormat ssp_format;
static Chip_SSP_DATA_SETUP_T xf_setup;
static volatile uint8_t isXferCompleted = 0;
#define TICKRATE_HZ (1) /* 1 ticks per second */
/*****************************************************************************
* Public types/enumerations/variables
****************************************************************************/
/* Set pin mux for SSP operation */
static void Init_SSP_PinMux(void)
{
Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO0_8, (IOCON_FUNC1 | IOCON_MODE_INACT)); /* MISO0 */
Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO0_9, (IOCON_FUNC1 | IOCON_MODE_INACT)); /* MOSI0 */
Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO0_2, (IOCON_FUNC1 | IOCON_MODE_INACT)); /* SSEL0 */
Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO2_11, (IOCON_FUNC1 | IOCON_MODE_INACT)); /* SCK0 */
Chip_IOCON_PinLocSel(LPC_IOCON, IOCON_SCKLOC_PIO2_11);
}
/*****************************************************************************
* Private functions
****************************************************************************/
/* Initialize buffer */
static void Buffer_Init(void)
{
uint16_t i;
// send wake up signal
// wait for T_WAKE=100-300 us
//Configuration Commands
//use default configuration first
Tx_Buf[0]=0x80;
Tx_Buf[1]=0x01;
Tx_Buf[2]=0x4D;
Tx_Buf[3]=0x7A;
// write configuration register 0-5
Tx_Buf[4]=0xFC;
Tx_Buf[5]=0x00;
Tx_Buf[6]=0x00;
Tx_Buf[7]=0x00;
Tx_Buf[8]=0x00;
Tx_Buf[9]=0x00;
//send PEC
Tx_Buf[10]=0x4F;
Tx_Buf[11]=0x82;
// adc
Tx_Buf[12]=0x83;
Tx_Buf[13]=0x70;// CMD1 select all cells measurement DCP =1
Tx_Buf[14]=0xDF;//PEC
Tx_Buf[15]=0x56;
//polling
Tx_Buf[16]=0x87;
Tx_Buf[17]=0x14;
Tx_Buf[18]=0x83;
Tx_Buf[19]=0x78;
//GET ALL THE SUM OF ALL CELL MEASURMENT Read status register Group A
Tx_Buf[20]=0x80;
Tx_Buf[21]=0x10;
Tx_Buf[22]=0x9D;
Tx_Buf[23]=0x66;
// 6 bytes from status register Group A
for (i = 0; i < 8; i++) {
Rx_Buf[i] = 0xAA;
}
}
/*****************************************************************************
* Public functions
****************************************************************************/
/**
* @brief SSP interrupt handler sub-routine
* @return Nothing
*/
void SSPIRQHANDLER(void)
{
Chip_SSP_Int_Disable(LPC_SSP); /* Disable all interrupt */
Chip_SSP_Int_RWFrames8Bits(LPC_SSP, &xf_setup);
if ((xf_setup.tx_cnt != xf_setup.length) || xf_setup.rx_cnt <8) { //|| (xf_setup.rx_cnt != xf_setup.length)
Chip_SSP_Int_Enable(LPC_SSP); /* enable all interrupts */
}
else {
isXferCompleted = 1;
}
}
//
/**
* @brief timer interrupt handler sub-routine
* @return Nothing
*/
void TIMER32_0_IRQHandler(void)
{
if (Chip_TIMER_MatchPending(LPC_TIMER32_0, 1)) {
Chip_TIMER_ClearMatch(LPC_TIMER32_0, 1);
Chip_SSP_Enable(LPC_SSP); // enable SSP communication
Buffer_Init();
xf_setup.length = 24;
xf_setup.tx_data = Tx_Buf; // assign the address of the first number to the pointer
xf_setup.rx_data = Rx_Buf; // assign the address of the first number to the pointer
xf_setup.rx_cnt = 0;
xf_setup.tx_cnt = 0;
Chip_SSP_Int_FlushData(LPC_SSP);/* flush dummy data from SSP FiFO */
Chip_SSP_Int_RWFrames8Bits(LPC_SSP, &xf_setup);
Chip_SSP_Int_Enable(LPC_SSP); /* enable interrupt */
while (!isXferCompleted) {
}
isXferCompleted=0;
Chip_SSP_Disable(LPC_SSP);
}
}
int main(void){
/* =================this part is the main function part==============
* initialization */
SystemCoreClockUpdate();
Board_Init();
/* =================SSP initialization ============*/
Init_SSP_PinMux();
Chip_SSP_Init(LPC_SSP);
ssp_format.frameFormat = SSP_FRAMEFORMAT_SPI; // SPI Frame format
ssp_format.bits = SSP_DATA_BITS; // set 8 bits
ssp_format.clockMode = SSP_CLOCK_MODE3; // clock mode as CPHA=1, CPOL=1
Chip_SSP_SetFormat(LPC_SSP, ssp_format.bits, ssp_format.frameFormat, ssp_format.clockMode);
Chip_SSP_SetMaster(LPC_SSP, 1); // SET MCU as master
Chip_SSP_SetBitRate(LPC_SSP, 10000); //set bit rate here t_CLK>=1 us!
Chip_SSP_Enable(LPC_SSP); // enable SSP communication
NVIC_EnableIRQ(SSP_IRQ);
Buffer_Init();
xf_setup.length = 24;
xf_setup.tx_data = Tx_Buf; // assign the address of the first number to the pointer
xf_setup.rx_data = Rx_Buf; // assign the address of the first number to the pointer
xf_setup.rx_cnt = 0;
xf_setup.tx_cnt = 0;
/* Timer configuration*/
Chip_TIMER_Init(LPC_TIMER32_0);
uint32_t timerFreq = Chip_Clock_GetSystemClockRate();
Chip_TIMER_Reset(LPC_TIMER32_0);
Chip_TIMER_MatchEnableInt(LPC_TIMER32_0, 1);
Chip_TIMER_SetMatch(LPC_TIMER32_0, 1, (timerFreq / TICKRATE_HZ));
Chip_TIMER_ResetOnMatchEnable(LPC_TIMER32_0, 1);
Chip_TIMER_Enable(LPC_TIMER32_0);
// Enable timer interrupt
NVIC_ClearPendingIRQ(TIMER_32_0_IRQn);
NVIC_EnableIRQ(TIMER_32_0_IRQn);
while (1) {
__WFI();
}
return 0;
}
Hi Kangkang,
You can test the official lpc11c24 lpcopen code on your side directly, change the MCU from LPC11C24 to LPC11C14.
https://www.nxp.com/downloads/en/software/lpcopen_v2_00a_lpcxpresso_nxp_lpcxpresso_11c24.zip
I have test the official ssp project, the SPI master works OK.
This is my SPI pin wave:
I didn't connect MISO, so the MISO data is invalid, but you can see the MOSI, it is correct.
Besides, you need to check the pin:
Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO0_8, (IOCON_FUNC1 | IOCON_MODE_INACT)); /* MISO0 */
Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO0_9, (IOCON_FUNC1 | IOCON_MODE_INACT)); /* MOSI0 */
Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO0_2, (IOCON_FUNC1 | IOCON_MODE_INACT)); /* SSEL0 */
Chip_IOCON_PinMuxSet(LPC_IOCON, IOCON_PIO2_11, (IOCON_FUNC1 | IOCON_MODE_INACT)); /* SCK0 */
Please use the official code directly, after it works, then you can add your timer and test it again.
Wish it helps you!
Have a great day,
Kerry
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------