2014-08-07 03:50 AM
hello, I want to send some data using spi but I see anything on my scope when I use this code.
does anyone know where is the problem? Thanks#include <
stddef.h
>
#include ''stm32f0xx_rcc.h''
#include ''stm32f0xx_gpio.h''
#include ''stm32f0xx_spi.h''
#define cs GPIO_Pin_12
void set_SPI(void){
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/* Configure the I2C clock source. The clock is derived from the HSI */
GPIO_InitStructure.GPIO_Pin = cs;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,cs);
//Configure pins: SCL and SDA ------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14| GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_0);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_0);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_0);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
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_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
}
int main(void)
{
set_SPI();
for(;;)
{
GPIO_ResetBits(GPIOB,cs);
SPI_I2S_SendData16(SPI2,0b01010101);
}
}
2014-08-07 09:39 AM
What part EXACTLY are you using? This would impact if you should be using SPI1 or SPI2
Try to make sure the Tx buffer is empty before stuffing data into the SPI2014-08-08 04:00 AM
hello, this time, I am using an spi eeprom the 25AA Now my code works and I am able to see data on my logic analyser but the chip select doesn't work proprely : I am oblige to put a for() delay to get it working.
#include <
stddef.h
>
#include ''stm32f0xx_rcc.h''
#include ''stm32f0xx_gpio.h''
#include ''stm32f0xx_spi.h''
#define cs GPIO_Pin_8
int i;
void set_SPI(void){
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
//Configure pins: SCL and SDA ------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14| GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_0);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_0);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_0);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_0);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
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_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
}
void cs_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* Configure the I2C clock source. The clock is derived from the HSI */
GPIO_InitStructure.GPIO_Pin = cs;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, cs);
}
void byte_write(int adress,int adress2,int data)
{
SPI_SendData8(SPI2,0b00000010);
SPI_SendData8(SPI2,adress);
SPI_SendData8(SPI2,adress2);
SPI_SendData8(SPI2,data);
}
void byte_read(int adres1,int adres2)
{
GPIO_ResetBits(GPIOA,cs);
SPI_SendData8(SPI2,0b00000011);
SPI_SendData8(SPI2,adres1);
SPI_SendData8(SPI2,adres2);
SPI_SendData8(SPI2,0b00000000);
SPI_ReceiveData8(SPI2);
}
int main(void)
{
set_SPI();
cs_init();
GPIO_ResetBits(GPIOA, cs);
SPI_SendData8(SPI2,0b00000010);
for(i=100;i>=0;i--);
GPIO_SetBits(GPIOA, cs);
while(1);
}