XMC4500 SPI communictaion (as Master)

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
kondicvl
Employee
Employee
Hi,

I started programming with the XMC4500, so my experience is limited. I want to communicate with peripheral device via SPI. I tried to do it with the Apps, but there not really clear to me. I wrote the follwing code, based on the USIC guide instructions. But it doesn't work. And I can't get even the SCLK on an osci. Do you have any sugesstions how I can make it run? Or where I can find tutorial for SPI communication with the XMC4500?

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include
#include
#include "xmc_common.h"
#include "xmc_gpio.h"
#include "XMC4500.h"


void initSPI(void) {
/* SPI Master initialization */

uint32_t spiFdrStep = 0, spiFsclk = 500000;
uint8_t spiBrgPdiv = 0;

/* if PPPEN = 0
* fsclk = fsys x STEP/1024 x 1/2 x 1/PDIV+1
*
* if PPPEN = 1
* fsclk = fsys x 1/2 x STEP/1024 x 1/2 x 1/PDIV+1
*/

/* Calculate settings for desired fsclk, PPPEN = 1 */
while(1) {
spiFdrStep = (spiFsclk * 1024 * 2 * 2 * (spiBrgPdiv+1)) / SystemCoreClock;
if(!((0 <= spiFdrStep) && (spiFdrStep < 1024))) {
spiBrgPdiv++;
} else {
break;
}
}


SystemInit();
//Clear the reset for USIC1
SCU_RESET->PRCLR0 |= 0x80;

/* Enable the module kernel clock and the module functionality (Kernel State Configuration Register) */
/*Enabling write access*/
USIC0_CH0->KSCFG |= (1UL<< USIC_CH_KSCFG_MODEN_Pos)
| (1UL<< USIC_CH_KSCFG_BPMODEN_Pos);


// Disable SPI
USIC0_CH0->CCR &= ~( ((uint32_t)(0x01U & USIC_CH_CCR_MODE_Msk)));
// Enable SPI
USIC0_CH0->CCR |= 0x0001;


// Switch on the Baud rate generation for master mode
USIC0_CH0->FDR=(0b01 << USIC_CH_FDR_DM_Pos);
USIC0_CH0->BRG=0;


/* Configure Baud Rate Generator Register */
USIC0_CH0->BRG |= (0UL<< USIC_CH_BRG_CLKSEL_Pos)
| (0UL<< USIC_CH_BRG_PPPEN_Pos)
| (0UL<< USIC_CH_BRG_PCTQ_Pos)
| (9UL << USIC_CH_BRG_DCTQ_Pos)
| (spiBrgPdiv << USIC_CH_BRG_PDIV_Pos);

/* Configuration of Protocol Control Register */
USIC0_CH0->PCR_SSCMode |= (1ul << USIC_CH_PCR_SSCMode_MSLSEN_Pos) /* Enables/disables the generation of the master slave select signal */
| (1ul << USIC_CH_PCR_SSCMode_SELINV_Pos) /* Active low for CS */
| (1ul << USIC_CH_PCR_SSCMode_SELCTR_Pos) /* Direct select */
| (1ul << USIC_CH_PCR_SSCMode_SELO_Pos) /* Activate SELO */
| (1ul << USIC_CH_PCR_SSCMode_MCLK_Pos); /* Master Clock Enable */

/* Configure Shift Control Register */
USIC0_CH0->SCTR |= (1ul << USIC_CH_SCTR_SDIR_Pos) /* Shift out MSB first */
| (0ul << USIC_CH_SCTR_DSM_Pos) /* Data Shift Mode */
| (0ul << USIC_CH_SCTR_TRM_Pos) /* Transmission Mode */
| (15ul << USIC_CH_SCTR_FLE_Pos) /* Frame Length */
| (15ul << USIC_CH_SCTR_WLE_Pos); /* Word Length */

/* Configure Transmission Control Status */
USIC0_CH0->TCSR |= (1ul << USIC_CH_TCSR_FLEMD_Pos) /* FLE Mode */
| (1ul << USIC_CH_TCSR_WLEMD_Pos) /* WLE Mode */
| (1ul << USIC_CH_TCSR_SELMD_Pos) /* Select Mode */
| (1ul << USIC_CH_TCSR_SOF_Pos) /* Start Of Frame */
| (1ul << USIC_CH_TCSR_EOF_Pos) /* End Of Frame */
| (1ul << USIC_CH_TCSR_TDV_Pos) /* Transmit Data Valid */
| (1ul << USIC_CH_TCSR_TDSSM_Pos) /* TBUF Data Single Shot Mode */
| (1ul << USIC_CH_TCSR_TDEN_Pos); /* TBUF Data Enable */


/* Set P1.4 as Data Input, MISO => P1.4/U0C0.DX0B*/
USIC0_CH0-> DX0CR |= (1UL<< USIC_CH_DX0CR_DSEL_Pos) /* Select DX0B */
| (1UL<< USIC_CH_DX0CR_INSW_Pos);
/* Set P1.5 as Data Output, MOSI => P1.5/U0C0.DOUT0 */
PORT1->IOCR4 |=(0b10001<<11);
/* Set P1.10 as Clock Output, SCLK => P1.10/U0C0.SCLKOUT */
PORT1->IOCR8 |=(0b10001<<19);
/* Set P0.9 as Chip Select Output, CS => P1.11/U0C0.SELO0 */
PORT0->IOCR8 |=(0b10001<<11);

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0 Likes
2 Replies
DRubeša
Employee
Employee
First solution authored First like received
Hi,

I see this is the fairly common question:"I´m beginner and I want to communicate with xyz peripheral but I don´t understand USIC module so I´m kind of stuck". For this reason I would like to provide same steps which I hope are going to benefit you and also other forum users. First thing would be to acquired related documentation and for the USIC module following application note is the obvious choice:
http://www.infineon.com/dgdl/Infineon-USIC-XMC1000_XMC4000-AP32303-AN-v01_00-EN.pdf?fileId=5546d4624...

Mentioned application note has also some code sections that are useful to understand basic usage of the module, but that´s not enough for you, you want to see more code examples. For this reason we provided you with example code for every protocol supported by USIC module. You can find the example code at following link:
http://www.infineon.com/dgdl/Infineon-USIC-XMC1000_XMC4000-AP32303_Example_Code-AN-v01_00-EN.exe?fil...

However, maybe you just want some quick and simple solution, something that is available and working under 5 minutes. There is also solution for this and it´s located inside DAVE tool. Once you opened DAVE, select "Help -> Install DAVE APP/Example/Device Library". When asked for the Dave site which is going to be used select "Dave Project Library Manager". There you can find the related example project for your microcontroller. You´re using microcontroller that is very well covered with examples so you just need to select the example as you can see in the following figure and example project will be downloaded in your workspace. (In this figure I showed the selection for the DAVE APP example, but of course you can also select the example based on XMCLibs).

2373.attach

Once you have downloaded the example, try to build it and run in on the board. Once everything is working as expected, take a look at the generated code....it´s located under directory "DAVE -> Generated". Become familiar with it and try to change some settings inside the APP and see how this influences the generated files.

I hope this quick tutorial shed some light on how to use USIC module.

Best regards,
Deni
0 Likes
kondicvl
Employee
Employee
Thanks a lot. It works now 🙂
0 Likes