cancel
Showing results for 
Search instead for 
Did you mean: 

SPI and Ethernet Controller 28j60

ram_alonso
Associate
Posted on December 02, 2011 at 06:03

Hi everyone,

I'm starting with the stm32f100rb and trying to set up the communication with 28j60(SPI). As far as know the 28j60 only support the spi mode 0 and  run at frequencies of at least  8 MHz.

To compile with spi mode 0 I should use  SPI_CPOL_Low and  SPI_CPHA_1Edge.

But then I have a real mess trying to figure out  how to select the frequency

Let's show you some pieces of code: 

/* Enable SPI1 and GPIOA clocks */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

/* Configure SPI1 pins: NSS, SCK, MISO and MOSI */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

//SPI1 NSS 

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

    GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_SetBits(GPIOA, GPIO_Pin_4);   

/* SPI1 configuration */ 

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;                              

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStructure.SPI_CRCPolynomial = 7;

SPI_Init(SPI1, &SPI_InitStructure);

The external oscillator it's 8MHz but, if I'm not wrong, then it's increased by the pll through:

/* #define SYSCLK_FREQ_HSE    HSE_VALUE*/

     #define SYSCLK_FREQ_24MHz  24000000

#else

/* #define SYSCLK_FREQ_HSE    HSE_VALUE */

/* #define SYSCLK_FREQ_24MHz  24000000*/

/* #define SYSCLK_FREQ_36MHz  36000000 */

/* #define SYSCLK_FREQ_48MHz  48000000 */

/* #define SYSCLK_FREQ_56MHz  56000000 */

    #define SYSCLK_FREQ_72MHz  72000000

And finally wich SPI_BaudRatePrescaler should I choose depending on the previous frequency rank.

Every answer will be appreciate. 
2 REPLIES 2
gsantha
Associate II
Posted on December 03, 2011 at 18:59

Hi Rama,

I'm new to these STM32 chips but worked a lot with several other MCUs before so let me share you a general approach working with SPI.

First, try to find a working SPI example - afaik there is an example for SPI in the STM32VL environment. Modify the code to have a forever loop sending out a dummy valued byte with a delay loop. Like this:

while (1){

SPI_Write(dummy);

for (int i=0; i<delay;i++){}

}

Next run the example and observe the SCK (SCLK) pin with your scope. If your SPI peripherial and your code is working then you'll see a nice pulse train. Measure the time between two rising edges and convert it to frequency. Compare this value to your CPU freq divided by the baudrate_prescaler. Experiment with changing this prescaler and observe the SCK frequency changes.

These steps should be the firsts when you setup an SPI communication in an unknown enviroment. Later, when you are more comfortable with your code and environment, you don't need these tests.

595776
Associate
Posted on October 11, 2012 at 22:47

Hi Rama , did you get SPI and Ethernet Controller 28j60 working with STM32 discovery ?

I would be very happy if you could send me the code .

best regards rudolf