Hi, I am using the MCBSTM32C evaluation board (STM32F107VCT) and want to use SPI3. For this I need to remap SPI3 because it is used by the LCD display. I did this but I get no signal on any of the 3 SPI pins when running this code. Any ideas?
#include "stm32f10x.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "LED.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_spi.h"
#define CMD_WRITE_ENABLE 0x06
#define CMD_WRITE_DISABLE 0x04
#define CMD_READ_STATUS_1 0x05
#define CMD_READ_STATUS_2 0x35
#define CMD_WRITE_STATUS 0x01
#define CMD_READ_DATA 0x03
#define CMD_FAST_READ_DATA 0x0B
#define CMD_FAST_READ_DUAL_OUT 0x3B
#define CMD_FAST_READ_DUAL_I_O 0xBB
#define CMD_READ_OTP 0x4B
#define CMD_PROGRAM_OTP 0x42
#define CMD_PAGE_PROGRAM 0x02
#define CMD_DUAL_I_FAST_PROGRAM 0xA2
#define CMD_SECTOR_ERASE 0x20
#define CMD_BLOCK_ERASE 0x52
#define CMD_CHIP_ERASE 0xC7
#define CMD_DEEP_POWER_DOWN 0xB9
#define CMD_READ_DEV_ID 0x9F
#define CMD_READ_MANUFACT 0x90
#define CMD_RELEASE_DPD 0xAB
#define CMD_HIGH_PERFORMANCE 0xA3
#define CMD_CONT_READ_RST 0xFFFF
#define BUFFERSIZE 256
SPI_InitTypeDef SPI_InitStructure;
uint16_t Data_In = 0;
int initialized = 0;
uint8_t Rx_Buffer[BUFFERSIZE];
void FLASH_LowLevel_Init(void);
void sFLASH_WriteEnable(void);
void sFLASH_WaitForWriteEnd(void);
uint8_t sFLASH_SendByte(uint8_t byte);
void sFLASH_CS_LOW(void);
void sFLASH_CS_HIGH(void);
void sFLASH_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
FLASH_LowLevel_Init();
/*!< Deselect the FLASH: Chip Select high */
sFLASH_CS_HIGH();
/*!< SPI 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_High;//SPI_CPOL_High SPI_CPOL_Low
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;//SPI_CPHA_1Edge SPI_CPHA_2Edge
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI3, &SPI_InitStructure);
/*!< Enable the sFLASH_SPI */
SPI_Cmd(SPI3, ENABLE);
}
void FLASH_LowLevel_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);
/*!< sFLASH_SPI_CS_GPIO, sFLASH_SPI_MOSI_GPIO, sFLASH_SPI_MISO_GPIO
and sFLASH_SPI_SCK_GPIO Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
/*!< sFLASH_SPI Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
/*!< Configure sFLASH_SPI pins: SCK */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/*!< Configure sFLASH_SPI pins: MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/*!< Configure sFLASH_SPI pins: MISO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/*!< Configure sFLASH_CS_PIN pin: sFLASH Card CS pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void sFLASH_EraseBulk(void)
{
/*!< Send write enable instruction */
sFLASH_WriteEnable();
/*!< Bulk Erase */
/*!< Select the FLASH: Chip Select low */
sFLASH_CS_LOW();
/*!< Send Bulk Erase instruction */
sFLASH_SendByte(CMD_CHIP_ERASE);
/*!< Deselect the FLASH: Chip Select high */
sFLASH_CS_HIGH();
/*!< Wait the end of Flash writing */
sFLASH_WaitForWriteEnd();
}
void sFLASH_CS_LOW(){
//SPI_NSSInternalSoftwareConfig(SPI3, SPI_NSSInternalSoft_Reset);
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
}
void sFLASH_CS_HIGH(){
//SPI_NSSInternalSoftwareConfig(SPI3, SPI_NSSInternalSoft_Set);
GPIO_SetBits(GPIOA, GPIO_Pin_4);
}
void sFLASH_WriteEnable(void)
{
/*!< Select the FLASH: Chip Select low */
sFLASH_CS_LOW();
/*!< Send "Write Enable" instruction */
sFLASH_SendByte(CMD_WRITE_ENABLE);
/*!< Deselect the FLASH: Chip Select high */
sFLASH_CS_HIGH();
}
void sFLASH_WaitForWriteEnd(void)
{
uint8_t flashstatus = 0;
/*!< Select the FLASH: Chip Select low */
sFLASH_CS_LOW();
/*!< Send "Read Status Register" instruction */
sFLASH_SendByte(CMD_READ_STATUS_1);
/*!< Loop as long as the memory is busy with a write cycle */
do
{
/*!< Send a dummy byte to generate the clock needed by the FLASH
and put the value of the status register in FLASH_Status variable */
flashstatus = sFLASH_SendByte(0xFF);
}
while ((flashstatus & 0x1) == SET); /* Write in progress */
/*!< Deselect the FLASH: Chip Select high */
sFLASH_CS_HIGH();
}
uint8_t sFLASH_SendByte(uint8_t byte)
{
/*!< Loop while DR register in not empty */
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);
/*!< Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI3, byte);
/*!< Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);
/*!< Return the byte read from the SPI bus */
return SPI_I2S_ReceiveData(SPI3);
}
void sFLASH_ReadBuffer(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead)
{
/*!< Select the FLASH: Chip Select low */
sFLASH_CS_LOW();
/*!< Send "Read from Memory " instruction */
sFLASH_SendByte(CMD_READ_DATA);
/*!< Send ReadAddr high nibble address byte to read from */
sFLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
/*!< Send ReadAddr medium nibble address byte to read from */
sFLASH_SendByte((ReadAddr& 0xFF00) >> 8);
/*!< Send ReadAddr low nibble address byte to read from */
sFLASH_SendByte(ReadAddr & 0xFF);
while (NumByteToRead--) /*!< while there is data to be read */
{
/*!< Read a byte from the FLASH */
*pBuffer = sFLASH_SendByte(0xFF);
//USART1_Put(*pBuffer);
/*!< Point to the next location where the byte read will be saved */
pBuffer++;
}
/*!< Deselect the FLASH: Chip Select high */
sFLASH_CS_HIGH();
}
int main(void)
{
//USART1_Config();
LED_Initialize();
//USART1_SendData_s("Main start::\n\r");
// Initialize the SPI FLASH driver
sFLASH_Init();
sFLASH_EraseBulk();
LED_On(0);
sFLASH_ReadBuffer(Rx_Buffer, 0x00000, BUFFERSIZE);
while (1){
sFLASH_ReadBuffer(Rx_Buffer, 0x00000, BUFFERSIZE);
LED_On(0);
}
}