cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F401VDT TO ESP12E FOR SPI PROTOCOL

amar2
Associate II
Posted on December 16, 2016 at 14:08

Hi,

My name is amarr., am new to this forum, I am trying to send some AT commands to STM32F401VDT controller through

SPI protocol to Wifi module(ESP12E vendor Ai-Thinker). For that purpose i need to make Module into SPI mode.

For SPI mode:--GPIO5=HIGH,GPIO0=X, GPIO2=X

I attached my schematics and datasheet for wifi module..

please help me ,make it as work,

sorry for my english is poor.

@#thank you.

amarr.m

2 REPLIES 2
ST Renegade
Senior
Posted on December 16, 2016 at 21:32

Hello there,

Have you tried the STM32CubeMX tool to generate the source code for you? You can use it to generate the SPI initialization for you and afterwards it's sufficient for you to call HAL_SPI_Transmit.... IT/DMA etc.

Have a nice day,

Renegade

Posted on December 17, 2016 at 14:43

 ,

 ,

Hi ST Renegade,

Thank you for reply me.

Your saying right but am using SP drivers(for my complete project).

Now am sending AT Commands to ESP12E(wifi module), but i can't read response from module means i think controller won't enter into Irq_handler.

Here am using USB_VCP for see output.

I attached my code and am using SPI protocol with Interrupt based.

please help me.

Thank you,

regards,

Amarr.M ,

///////////////////SPI.C///////////////////////////////////////////////////////////////////

♯ include 'tm_stm32f4_usb_vcp.h'

 ,

// ♯ include 'tm_stm32f4_disco.h'

 ,

♯ include 'defines.h'

 ,

// ♯ include 'tm_stm32f4_usart.h'

 ,

♯ include 'tm_stm32f4_delay.h'

 ,

♯ include 'string.h'

 ,

♯ include 'stm32f4xx_usart.h'

 ,

♯ include 'wifi.h'

 ,

♯ define BUFFERSIZE 10

uint8_t RxIndex=0,

 ,

char RxBuffer[5],

 ,

SPI_ISR_FUNC Spi1Func,

 ,

uint8_t SPI1_send(uint8_t data),

 ,

//void SPI1_IRQHANDLER(void),

 ,

//char spiTxBuff[3]= 'AT',

 ,

//char spiRxBuff[3],

 ,

//static volatile int spiRxCount= 0, // used in SPI1_IRQHandler

 ,

//char* data,

void init_SPI1(void){

 ,

NVIC_InitTypeDef NVIC_InitStructure,

 ,

GPIO_InitTypeDef GPIO_InitStruct,

 ,

SPI_InitTypeDef SPI_InitStruct,

 ,

TM_USB_VCP_Puts('enter into spi\r\n'),

 ,

// enable clock for used GPIOA pins

 ,

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE),

 ,

 ,

/* configure pins used by SPI1

 ,

* PA5 = SCK

 ,

* PA6 = MISO

 ,

* PA7 = MOSI

 ,

*/

 ,

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5,

 ,

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF,

 ,

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP,

 ,

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz,

 ,

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL,

 ,

GPIO_Init(GPIOA, &,GPIO_InitStruct),

 ,

TM_USB_VCP_Puts('init gpio\r\n'),

 ,

// connect SPI1 pins to SPI alternate function

 ,

GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1),

 ,

GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1),

 ,

GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1),

 ,

 ,

// enable clock for used IO pins

 ,

//RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE), COMMENT THIS LINE AS PORTE IS NOT NEEDED NOW

 ,

 ,

/* Configure the chip select pin

 ,

in this case we will use PA4 */

 ,

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4,

 ,

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT,

 ,

GPIO_InitStruct.GPIO_OType = GPIO_OType_PP,

 ,

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz,

 ,

GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP,

 ,

GPIO_Init(GPIOA, &,GPIO_InitStruct),

 ,

 ,

GPIOA->,BSRRL |= 0x0010, // set PA4 HIGH,

 ,

TM_USB_VCP_Puts('PA4 high\r\n'),

 ,

// enable peripheral clock

 ,

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE), // 84 Mhz

 ,

 ,

/* configure SPI1 in Mode 0

 ,

* CPOL = 0 -->, clock is low when idle

 ,

* CPHA = 0 -->, data is sampled at the first edge

 ,

*/

 ,

SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex, // set to full duplex mode, seperate MOSI and MISO lines

 ,

SPI_InitStruct.SPI_Mode = SPI_Mode_Master, // transmit in master mode, NSS pin has to be always high

 ,

SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b, // one packet of data is 8 bits wide

 ,

SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low, // clock is low when idle

 ,

SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge, // data sampled at first edge

 ,

SPI_InitStruct.SPI_NSS = SPI_NSS_Soft,//| SPI_NSSInternalSoft_Set, set the NSS management to internal and pull internal NSS high

 ,

SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32, // 5.25Mhz, 2.625Mhz SPI frequency is APB2 frequency / 4 = 84Mhz/4 = 21Mhz

 ,

SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB,// data is transmitted MSB first

 ,

SPI_InitStruct.SPI_CRCPolynomial = 7, //amarr added

 ,

SPI_Init(SPI1, &,SPI_InitStruct),

 ,

TM_USB_VCP_Puts('SPI Init Over\r\n'),

 ,

SPI_Cmd(SPI1, ENABLE), // enable SPI1

 ,

TM_USB_VCP_Puts('spi cmd enabled\r\n'),

 ,

 ,

/* Configure the Priority Group to 1 bit */

 ,

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2),

 ,

 ,

/* Configure the SPI interrupt priority */

 ,

NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn,

 ,

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1,

 ,

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0,

 ,

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE,

 ,

NVIC_Init(&,NVIC_InitStructure),

 ,

TM_USB_VCP_Puts('NVIC init over\r\n'),

 ,

//Spi1Func = SPI1_send,

 ,

Spi1Func = Irq_spi1,

 ,

return,

 ,

}

/* This command used to send data to module*/

 ,

uint8_t SPI1_send(uint8_t data){

 ,

TM_USB_VCP_Puts('ENTER INTO SPI1_SEND\r\n'),

 ,

SPI1->,DR =data, // write data to be transmitted to the SPI data register

 ,

while( !(SPI1->,SR &, SPI_I2S_FLAG_TXE) ), // wait until transmit complete

 ,

while( !(SPI1->,SR &, SPI_I2S_FLAG_RXNE) ), // wait until receive complete

 ,

while( SPI1->,SR &, SPI_I2S_FLAG_BSY ), // wait until SPI is not busy anymore

 ,

TM_USB_VCP_Puts('EXIT FROM SPI1_SEND\r\n'),

 ,

return SPI1->,DR, // return received data from SPI data register

 ,

//TM_USB_VCP_Puts('EXIT FROM SPI1_SEND\r\n'),

 ,

}

-----------------------------------------------------------------------------------------------------------------------------------

/**

 ,

* @brief This function handles SPI interrupt request.

 ,

* @param None

 ,

* @retval None

 ,

*/

void Irq_spi1(SPI_TypeDef * base)

 ,

{

 ,

volatile unsigned int IIR,

 ,

IIR = base->,SR,

 ,

/* SPI in Receiver mode */

 ,

if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_RXNE) == SET)

 ,

{

 ,

if (RxIndex <, BUFFERSIZE)

 ,

{

 ,

/* Receive Transaction data */

 ,

RxBuffer[RxIndex++] = SPI_I2S_ReceiveData(SPI1),

 ,

}

 ,

else

 ,

{

 ,

TM_USB_VCP_Puts(RxBuffer),

 ,

RxIndex=0,

 ,

/* Disable the Rx buffer not empty interrupt */

 ,

SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, DISABLE),

 ,

}

 ,

TM_USB_VCP_Puts('ENTER INTO SPI_IRQ Handler\r\n'),

 ,

TM_USB_VCP_Puts(RxBuffer),

 ,

}

 ,

}

-----------------------------------------------------------------------------------------------------