📄 main.c
字号:
/**
******************************************************************************
* @file SPI_FastCommunication\main.c
* @brief This file contains the main function for SPI fast communication example.
* @author STMicroelectronics - MCD Application Team
* @version V1.0.1
* @date 09/22/2008
******************************************************************************
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2008 STMicroelectronics</center></h2>
* @image html logo.bmp
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8s_lib.h"
#include "microsd.h"
#include "mono_lcd.h"
/**
* @addtogroup SPI_FastCommunication
* @{
*/
/* Private define ------------------------------------------------------------*/
#define BufferSize ((u16)512)
/* Private define ------------------------------------------------------------*/
/* Evalboard I/Os configuration */
#define LEDS_PORT (GPIOH)
#define LED1_PIN (GPIO_PIN_3)
#define LED2_PIN (GPIO_PIN_2)
#define LED3_PIN (GPIO_PIN_1)
#define LED4_PIN (GPIO_PIN_0)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u16 Status = 0;
u8 Buffer_Block_Tx[BufferSize];
u8 Buffer_Block_Rx[BufferSize];
u8 Buffer_MultiBlock_Tx[1024];
u8 Buffer_MultiBlock_Rx[1024];
ErrorStatus TransferStatus1 = ERROR;
ErrorStatus TransferStatus2 = ERROR;
ErrorStatus TransferStatus3 = ERROR;
/* Private function prototypes -----------------------------------------------*/
void Delay (u16 nCount);
void Fill_Buffer(u8 *pBuffer, u16 BufferLength, u8 Offset);
ErrorStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength);
/* Private functions ---------------------------------------------------------*/
void Delay(u16 nCount)
{
/* Decrement nCount value */
while (nCount != 0)
{
nCount--;
}
}
/* Global variables ----------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/
/**
* @brief Validation firmware main entry point.
* @par Parameters:
* None
* @retval void None
* @par Required preconditions:
* None
* @par Library functions called:
* - GPIO_Init()
* - GPIO_WriteReverse()
* - CLK_HSIPrescalerConfig()
* - SPI_DeInit()
* - SPI_Init()
* - SPI_Cmd()
*/
void main(void)
{
u8 i =0;
/* Initialize I/Os in Output Mode */
GPIO_Init(LEDS_PORT, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN,GPIO_MODE_OUT_PP_LOW_FAST);
/******************************Disable LCD*********************************/
/* Initialize SPI in Master mode for LCD */
SPI_DeInit();
SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_4, SPI_MODE_MASTER, SPI_CLOCKPOLARITY_HIGH, SPI_CLOCKPHASE_2EDGE, SPI_DATADIRECTION_1LINE_TX, SPI_NSS_SOFT, 0x07);
SPI_Cmd(ENABLE);
/* Initialize LCD*/
LCD_Init();
/* Clear LCD*/
LCD_Clear();
/* Disable Chip select*/
LCD_ChipSelect(DISABLE);
/* Read LCD status*/
LCD_ReadStatus();
/* Insert delay*/
Delay(0xFFFF);
/*************************Configue High Speed Clock**********************/
/*High speed internal clock prescaler: 1*/
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
/***********************SPI and MicroSD initialization******************/
/*SPI DeInit*/
SPI_DeInit();
/* Init the SPI in the fast communication fmaster/2 */
SPI_Init(SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_MODE_MASTER, SPI_CLOCKPOLARITY_HIGH, SPI_CLOCKPHASE_2EDGE, SPI_DATADIRECTION_2LINES_FULLDUPLEX, SPI_NSS_SOFT, 0x07);
SPI_Cmd(ENABLE);
while (MSD_Detect() == MICROSD_NOT_PRESENT)
{
/* Wait MicroSD card insertion */
}
Delay(0xFFFF);
/* Init the flash micro SD*/
Status = MSD_Init();
/***************************Block Read/Write******************************/
/* Fill the buffer to send */
Fill_Buffer(Buffer_Block_Tx, BufferSize, 0xAA);
/* Write block of 512 bytes on address 0 */
Status = MSD_WriteBlock(Buffer_Block_Tx, 0, BufferSize);
/* Read block of 512 bytes from address 0 */
Status = MSD_ReadBlock(Buffer_Block_Rx, 0, BufferSize);
/* Check data */
TransferStatus1 = Buffercmp(Buffer_Block_Tx, Buffer_Block_Rx, BufferSize);
if (TransferStatus1 != SUCCESS)
{
while (1) /* Error while programming complement value */
{
GPIO_WriteReverse(LEDS_PORT, LED1_PIN);
Delay((u16)0xFFFF);
Delay((u16)0xFFFF);
}
}
/***********************Multiple Block Read/Write**************************/
/* Fill the buffer to send */
Fill_Buffer(Buffer_MultiBlock_Tx, 1024, 0x10);
/* Write multiple block of many bytes on address 0 */
Status = MSD_WriteBuffer(Buffer_MultiBlock_Tx, 0, 1024);
/* Read block of many bytes from address 0 */
Status = MSD_ReadBuffer(Buffer_MultiBlock_Rx, 0, 1024);
/* Check data */
TransferStatus2 = Buffercmp(Buffer_MultiBlock_Tx, Buffer_MultiBlock_Rx, 1024);
if (TransferStatus2 != SUCCESS)
{
while (1) /* Error while programming complement value */
{
GPIO_WriteReverse(LEDS_PORT, LED2_PIN);
Delay((u16)0xFFFF);
Delay((u16)0xFFFF);
}
}
/****************** Successive Write/Read on many blocks*******************/
/* Write/read procedure */
for (i = 0; i < 10; i++)
{
/* Fill the buffer to send */
Fill_Buffer(Buffer_Block_Tx, BufferSize, i);
/* Write block of 512 bytes on address 0 */
Status = MSD_WriteBlock(Buffer_Block_Tx, (i * 512), BufferSize);
/* Read block of 512 bytes from address 0 */
Status = MSD_ReadBlock(Buffer_Block_Rx, (i * 512), BufferSize);
/* Check data */
TransferStatus3 = Buffercmp(Buffer_Block_Tx, Buffer_Block_Rx, BufferSize);
}
if (TransferStatus3 != SUCCESS)
{
while (1) /* Error while programming complement value */
{
GPIO_WriteReverse(LEDS_PORT, LED3_PIN);
Delay((u16)0xFFFF);
Delay((u16)0xFFFF);
}
}
while (1)
{
GPIO_WriteReverse(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN));
Delay((u16)0xFFFF);
Delay((u16)0xFFFF);
}
}
/**
* @brief Fill the gloal buffer.
* @par Parameters:
* - pBuffer: pointer on the Buffer to fill
* - BufferSize: size of the buffer to fill
* - Offset: first value to fill on the Buffer
* @retval
* None
* @par Required preconditions:
* None
* @par Library functions called:
* None
*/
void Fill_Buffer(u8 *pBuffer, u16 BufferLength, u8 Offset)
{
/* Put in global buffer same values */
while (BufferLength--)
{
*pBuffer = Offset;
pBuffer++;
}
}
/**
* @brief Compares two buffers.
* @par Parameters:
* - pBuffer1, pBuffer2: buffers to be compared.
* - BufferLength: buffer's length
* @retval
* - PASSED: pBuffer1 identical to pBuffer2
* - FAILED: pBuffer1 differs from pBuffer2
* @par Required preconditions:
* None
* @par Library functions called:
* None
*/
ErrorStatus Buffercmp(u8* pBuffer1, u8* pBuffer2, u16 BufferLength)
{
while (BufferLength--)
{
if (*pBuffer1 != *pBuffer2)
{
return ERROR;
}
pBuffer1++;
pBuffer2++;
}
return SUCCESS;
}
/**
* @brief Reports the name of the source file and the source line number where
* the assert error has occurred.
* 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)
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
*/
#ifdef FULL_ASSERT
void assert_failed(u8 *file, u16 line)
#else
void assert_failed(void)
#endif
{
/* Add your own code to manage an assert error */
/* Infinite loop */
while (1)
{
}
}
/**
* @}
*/
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -