I am using STM32f207IG.
/**
*****************************************************************************
**
** File : main.c
**
** Abstract : main function.
**
** Functions : main
**
** Environment : Atollic TrueSTUDIO/STM32
** STMicroelectronics STM32F2xx Standard Peripherals Library
**
** Distribution: The file is distributed “as is,” without any warranty
** of any kind.
**
** (c)Copyright Atollic AB.
** You may use this file as-is or modify it according to the needs of your
** project. Distribution of this file (unmodified or modified) is not
** permitted. Atollic AB permit registered Atollic TrueSTUDIO(R) users the
** rights to distribute the assembled, compiled & linked contents of this
** file as part of an application binary file, provided that it is built
** using the Atollic TrueSTUDIO(R) toolchain.
**
**
*****************************************************************************
*/
/* Includes */
#include <stddef.h>
#include "stm32f2xx.h"
#ifdef USE_STM322xG_EVAL
#include "stm322xg_eval.h"
#include "stm322xg_eval_lcd.h"
#include "stm322xg_eval_i2c_ee.h"
#define USE_BOARD
#define USE_LED
#define USE_SEE
#define USE_EVAL_AUDIO
#endif
/* Private typedef */
/* Private define */
#ifdef USE_STM322xG_EVAL
#define MESSAGE1 " High-performance "
#define MESSAGE2 " STM32 F-2 "
#define MESSAGE3 " running on "
#define MESSAGE4 " STM3220F-EVAL "
#endif
#define MESSAGE5 " program built with "
#define MESSAGE6 " Atollic TrueSTUDIO "
/* Private macro */
/* Private variables */
USART_InitTypeDef USART_InitStructure;
/* Private function prototypes */
/* Private functions */
/**
**===========================================================================
**
** Abstract: main program
**
**===========================================================================
*/
void init_SPI1(void){
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
// enable clock for used IO pins
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* configure pins used by SPI1
* PA5 = SCK
* PA6 = MISO
* PA7 = MOSI
* PB0 - CS
* PB1 - HLD
* PB2 - WP
*/
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);
// 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_GPIOB, ENABLE);
/* Configure the chip select pin
in this case we will use PE7 */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
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(GPIOB, &GPIO_InitStruct);
GPIOB->BSRRL |= GPIO_Pin_0; // set PB0 high
GPIOB->BSRRL |= GPIO_Pin_0; // set PB1 high
GPIOB->BSRRL |= GPIO_Pin_0; // set PB2 high
// enable peripheral clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* 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_4; // SPI frequency is APB2 frequency / 4
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
SPI_Init(SPI1, &SPI_InitStruct);
SPI_Cmd(SPI1, ENABLE); // enable SPI1
}
uint8_t SPI1_send(uint8_t data){
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
return SPI1->DR; // return received data from SPI data register
}
int main(void)
{
int i = 0;
/**
* IMPORTANT NOTE!
* The symbol VECT_TAB_SRAM needs to be defined when building the project
* if code has been located to RAM and interrupts are used.
* Otherwise the interrupt table located in flash will be used.
* See also the <system_*.c> file and how the SystemInit() function updates
* SCB->VTOR register.
* E.g. SCB->VTOR = 0x20000000;
*/
/* TODO - Add your application code here */
uint8_t received_val = 0;
uint8_t a1=0,a2=0;
init_SPI1();
while(1){
GPIOB->BSRRH |= GPIO_Pin_0; // set PE7 (CS) low
received_val=SPI1_send(0x05); //Read the status register
GPIOB->BSRRL |= GPIO_Pin_0; // set PE7 (CS) high
}
#ifdef USE_LED
/* Initialize LEDs */
STM_EVAL_LEDInit(LED1);
STM_EVAL_LEDInit(LED2);
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
/* Turn on LEDs */
STM_EVAL_LEDOn(LED1);
STM_EVAL_LEDOn(LED2);
STM_EVAL_LEDOn(LED3);
STM_EVAL_LEDOn(LED4);
#endif
#ifdef USE_BOARD
/* Initialize the LCD */
#ifdef USE_STM322xG_EVAL
STM322xG_LCD_Init();
#endif
/* Display message on STM3210X-EVAL LCD */
/* Clear the LCD */
LCD_Clear(White);
/* Set the LCD Back Color */
LCD_SetBackColor(Blue);
/* Set the LCD Text Color */
LCD_SetTextColor(White);
LCD_DisplayStringLine(Line0, (uint8_t *)MESSAGE1);
LCD_DisplayStringLine(Line1, (uint8_t *)MESSAGE2);
LCD_DisplayStringLine(Line2, (uint8_t *)MESSAGE3);
LCD_DisplayStringLine(Line3, (uint8_t *)MESSAGE4);
LCD_DisplayStringLine(Line4, (uint8_t *)MESSAGE5);
LCD_DisplayStringLine(Line5, (uint8_t *)MESSAGE6);
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
STM_EVAL_COMInit(COM1, &USART_InitStructure);
#endif
/* Infinite loop */
while (1)
{
i++;
#ifdef USE_LED
STM_EVAL_LEDToggle(LED1);
#endif
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/*
* Minimal __assert_func used by the assert() macro
* */
void __assert_func(const char *file, int line, const char *func, const char *failedexpr)
{
while(1)
{}
}
/*
* Minimal __assert() uses __assert__func()
* */
void __assert(const char *file, int line, const char *failedexpr)
{
__assert_func (file, line, NULL, failedexpr);
}
#ifdef USE_SEE
#ifndef USE_DEFAULT_TIMEOUT_CALLBACK
/**
* @brief Basic management of the timeout situation.
* @param None.
* @retval sEE_FAIL.
*/
uint32_t sEE_TIMEOUT_UserCallback(void)
{
/* Return with error code */
return sEE_FAIL;
}
#endif
#endif /* USE_SEE */
#ifdef USE_EVAL_AUDIO
/**
* @brief Basic management of the timeout situation.
* @param None.
* @retval sEE_FAIL.
*/
void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size)
{
/* TODO, implement your code here */
return;
}
#endif /* USE_EVAL_AUDIO */
When I am trying to interface the board with the EEPROM and read the status register I get a '0'. However, whenever I re run the same code the value that is initially there in the received_val is '224'.
When I run the code and make received_val '0', how come it becomes '224' when I re run the code? Does that mean that my code works?(I am trying to read the status register of the EEPROM)