打印
[STM32F2]

使用Stm32 Cubemx的SPI操作SST25VF016B,读写数据有问题;读取ID正常

[复制链接]
1211|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
chenyu360583472|  楼主 | 2016-6-28 15:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "SST25VF016B.h"
#include "spi.h"




//¶Áȡ״̬¼Ä´æÆ÷
uint8_t SST25VF016B_RDSR(void)
{
        uint8_t Read_RDSR[1];
        uint8_t cmd[1];
        cmd[0]=0x05;
        
        SPI_CS_L;
        HAL_SPI_Transmit(&hspi1,cmd,1,1000);
        HAL_SPI_Receive(&hspi1,&Read_RDSR[0],1,1000);
        SPI_CS_H;
        
        return Read_RDSR[0];
}
//æ¼ì²â
void SST25VF016B_BUSY(void)
{
        uint8_t state=1;
        do
        {
                state=SST25VF016B_RDSR();
        }
        while((state&0x01)==0x01);   //״̬¼Ä´æÆ÷×îµÍλΪæ¼ì²âλ
}
//дʹÄÜ
void SST25VF016B_WREN(void)
{
        uint8_t cmd[1];
        cmd[0]=0x06;
        
        SST25VF016B_BUSY();
        
        SPI_CS_L;        
        HAL_SPI_Transmit(&hspi1,cmd,1,1000);
        SPI_CS_H;
}
//д½ûÖ¹
void SST25VF016B_WRDI(void)
{
        uint8_t cmd[1];
        cmd[0]=0x04;
        
        SST25VF016B_BUSY();
        
        SPI_CS_L;        
        HAL_SPI_Transmit(&hspi1,cmd,1,1000);
        SPI_CS_H;
}
//ʹÄÜд״̬¼Ä´æÆ÷£¨EWSR£©;д״̬¼Ä´æÆ÷£¨WRSR£©
void SST25VF016B_EWSR_WRSR(void)
{
        uint8_t cmd[2];
        cmd[0]=0x50;
        cmd[1]=0x01;
        
        SST25VF016B_BUSY();
        
        SPI_CS_L;        
        HAL_SPI_Transmit(&hspi1,&cmd[0],1,1000);
        SPI_CS_H;
        HAL_Delay(2);
        
        SPI_CS_L;        
        HAL_SPI_Transmit(&hspi1,&cmd[1],1,1000);
        HAL_SPI_Transmit(&hspi1,0,1,1000);       //״̬¼Ä´æÆ÷ÇåÁã
        SPI_CS_H;
        SST25VF016B_BUSY();
}
//¶ÁÈ¡ID
void SST25VF016B_READ_ID(uint8_t *ID)
{
        uint8_t cmd[4]={0x90,0x00,0x00,0x00};
//        uint8_t data[2]={0x00,0x00};
        //¼ì²âæ״̬
        SST25VF016B_BUSY();
        
        SST25VF016B_EWSR_WRSR();
        SST25VF016B_WREN();
        
        SPI_CS_L;
        HAL_SPI_Transmit(&hspi1,cmd,4,1000);//·¢ËͶÁÈ¡IDÃüÁî¡£ÁíÍâ·¢ËÍ3¸ö×Ö½ÚµØַλ
//        HAL_SPI_TransmitReceive(&hspi1,data,ID,2,1000);
        HAL_SPI_Receive(&hspi1,ID,2,1000);    //¶ÁÈ¡ID
        SPI_CS_H;        
        SST25VF016B_BUSY();
}
//4K²Á³ý
void  SST25VF016B_4K_Sector(uint32_t addr)
{
        uint8_t cmd[4];
        cmd[0]=0x20;
        cmd[1]=(addr&0xFFFFFF)>>16;
        cmd[2]=(addr&0xFFFF)>>8;
        cmd[3]=addr&0xFF;
        
        SST25VF016B_BUSY();
        SST25VF016B_EWSR_WRSR();
        SST25VF016B_WREN();
        
        SPI_CS_L;
        HAL_SPI_Transmit(&hspi1,cmd,4,1000);
        SPI_CS_H;
        SST25VF016B_BUSY();
}
//ȫƬ²Á³ý
void  SST25VF016B_All_Sector(void)
{
        uint8_t cmd[1];
        cmd[0]=0xc7;
        
        SST25VF016B_BUSY();
        SST25VF016B_EWSR_WRSR();
        SST25VF016B_WREN();
        
        SPI_CS_L;
        HAL_SPI_Transmit(&hspi1,cmd,1,1000);
        SPI_CS_H;
        
        HAL_Delay(25);
        SST25VF016B_BUSY();
}
//дÉÈÇø
//ʹÓÃÈí¼þд²Ù×÷½áÊø¼ì²éʱµÄ×Ô¶¯µÝÔö(AAI)×Ö±à³ÌÐòÁÐ
void SST25VF016B_Write_Sector(uint32_t addr,uint8_t *buffer,uint16_t length)
{
        SPI_CS_L;        
        uint8_t cmd[4];
        uint16_t i=2;
        uint8_t x;
        cmd[0]=0xAD;
        cmd[1]=(addr&0xFFFFFF)>>16;
        cmd[2]=(addr&0xFFFF)>>8;
        cmd[3]=addr&0xFF;
        
        SST25VF016B_BUSY();
        
        SST25VF016B_4K_Sector(addr);
        
        SST25VF016B_EWSR_WRSR();
        SST25VF016B_WREN();        
        
        SPI_CS_L;
        HAL_SPI_Transmit(&hspi1,cmd,4,1000);
        HAL_SPI_Transmit(&hspi1,buffer,2,1000);    //дÁ½Î»Êý¾Ý
        SPI_CS_H;
               
        for(i=2;i<length;i++)
        {
                SST25VF016B_BUSY();
                SST25VF016B_EWSR_WRSR();
                SST25VF016B_WREN();        
               
                HAL_Delay(1);
                SPI_CS_L;
                HAL_SPI_Transmit(&hspi1,&cmd[0],1,1000);
                HAL_SPI_Transmit(&hspi1,&buffer[i++],1,1000);
                HAL_SPI_Transmit(&hspi1,&buffer,1,1000);
                SPI_CS_H;
        }
        HAL_Delay(1);
        
        x=SST25VF016B_RDSR();
        printf(" ´«ËÍÍêÊý¾ÝÒÔºó״̬¼Ä´æÆ÷״̬Æ÷µÄֵΪ:0x%02x \r\n",x);
        SST25VF016B_WRDI();    //д½ûÖ¹
        SST25VF016B_BUSY();
}
//¶ÁÉÈÇø
void SST25VF016B_Read_Sector(uint32_t addr,uint8_t *buffer,uint16_t length)
{
        uint8_t cmd[5];
        uint16_t i;
        cmd[0]=0x0B;             //¸ßËÙ¶ÁÖ¸Áî
        cmd[1]=(addr&0xFFFFFF)>>16;
        cmd[2]=(addr&0xFFFF)>>8;
        cmd[3]=addr&0xFF;
        cmd[4]=0;                     //·¢ËÍÒ»¸ö¿Õ×Ö½Ú
        
        SST25VF016B_BUSY();
        SST25VF016B_WREN();        
        
        SPI_CS_L;
        HAL_SPI_Transmit(&hspi1,cmd,5,1000);
//        HAL_SPI_Receive(&hspi1,buffer,length,1000);
        for(i=0;i<length;i++)
        {
                HAL_SPI_Receive(&hspi1,&buffer,1,1000);
        }
        SPI_CS_H;
}


/**
  ******************************************************************************
  * File Name          : main.c
  * Description        : Main program body
  ******************************************************************************
  *
  * COPYRIGHT(c) 2016 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "stm32f2xx_hal.h"
#include "i2c.h"
#include "spi.h"
#include "usart.h"
#include "gpio.h"


/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
#include "SST25VF016B.h"


/* USER CODE END Includes */


/* Private variables ---------------------------------------------------------*/


/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/


/* USER CODE END PV */


/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void Error_Handler(void);


/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/


/* USER CODE END PFP */


/* USER CODE BEGIN 0 */

//SPI¹¦ÄܵIJÎÊý
uint8_t ID_Data[2];


#define Sector_Size 100
uint8_t Tx_Buffer[]="123456789";
uint8_t Rx_Buffer[Sector_Size];
uint16_t Tx_size;




/* USER CODE END 0 */


int main(void)
{


  /* USER CODE BEGIN 1 */
         
  /* USER CODE END 1 */


  /* MCU Configuration----------------------------------------------------------*/


  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();


  /* Configure the system clock */
  SystemClock_Config();


  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_SPI1_Init();
  MX_USART1_UART_Init();
  MX_USART3_UART_Init();


  /* USER CODE BEGIN 2 */
#if 1
        printf("\r\n /***SPI Falsh ID Test***/ \r\n");
        i=SST25VF016B_RDSR();
        printf(" ״̬¼Ä´æÆ÷״̬: 0x%02x \r\n",i);
        
        i=HAL_SPI_GetState(&hspi1);
        printf(" SPI state is: %d \r\n",i);
        
        SST25VF016B_READ_ID(ID_Data);
        printf(" READ_ID is : 0x%02X 0x%02X \r\n\r\n",ID_Data[0],ID_Data[1]);
        
        printf(" /***SPI R/W Data TEST***/ \r\n");
        
        i=SST25VF016B_RDSR();
        printf(" ״̬¼Ä´æÆ÷״̬: 0x%02x \r\n",i);
        
        Tx_size=(uint16_t)(sizeof(Tx_Buffer)/sizeof(Tx_Buffer[0]));
        printf(" ´ýдÈëµÄÊý¾Ý1: %s\r\n",Tx_Buffer);
        printf(" ´ýдÈëµÄÊý¾ÝµÄ³¤¶È: %d\r\n",Tx_size);
        for(i=0;i<Tx_size;i++)
        {
                Rx_Buffer=Tx_Buffer;
        }
        
        printf(" ´«µÝµÄÊý¾Ý: %s \r\n",Rx_Buffer);
        
        //ÏòÉÈÇøдÈëÊý¾Ý
        SST25VF016B_Write_Sector(0,Tx_Buffer,Sector_Size);
        //¶Áȡ״̬¼Ä´æÆ÷µÄ״ֵ̬
        i=SST25VF016B_RDSR();
        printf(" дÍêÊý¾ÝÒÔºó½ûֹд,״̬¼Ä´æÆ÷״̬Æ÷µÄֵΪ:0x%02x \r\n",i);
        //ÇåÁã½ÓÊÕBuffer
        memset(Rx_Buffer,0,Sector_Size);
        printf(" ½ÓÊÕBufferÇåÁãºóRx_BufferµÄÖµ:%s\r\n",Rx_Buffer);
        
        HAL_Delay(100);
        //´ÓÉÈÇø¶ÁÈ¡Êý¾Ý
        SST25VF016B_Read_Sector(0,Rx_Buffer,Sector_Size);
        printf(" ½ÓÊÕµ½µÄÊý¾Ý: %s \r\n",Rx_Buffer);
        
//        memset(Rx_Buffer,0,Sector_Size);
//        printf(" ½ÓÊÕBufferÇåÁãºóRx_BufferµÄÖµ: %s\r\n",Rx_Buffer);
////        SST25VF016B_4K_Sector(0);    //²Á³ý4K¿Õ¼äÊý¾Ý
////        SST25VF016B_All_Sector();    //²Á³ýÈ«²¿¿Õ¼äÊý¾Ý
//        SST25VF016B_Read_Sector(0,Rx_Buffer,Sector_Size);
//        printf(" ²Á³ýÍê³Éºó£¬½ÓÊÕµ½µÄÊý¾Ý: %s \r\n",Rx_Buffer);
#endif        






  /* USER CODE END 2 */


  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */


  /* USER CODE BEGIN 3 */
//        printf("\r\nchenyu");
        HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_RESET);
        HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_RESET);
        HAL_GPIO_WritePin(GPIOE,GPIO_PIN_1,GPIO_PIN_RESET);
        HAL_Delay(500);
        HAL_GPIO_WritePin(GPIOC,GPIO_PIN_10,GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_SET);
        HAL_GPIO_WritePin(GPIOE,GPIO_PIN_1,GPIO_PIN_SET);
        HAL_Delay(500);
  }
  /* USER CODE END 3 */


}


/** System Clock Configuration
*/
void SystemClock_Config(void)
{


  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;


  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 240;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }


  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }


  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);


  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);


  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}


/* USER CODE BEGIN 4 */


/* USER CODE END 4 */


/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler */
  /* User can add his own implementation to report the HAL error return state */
  while(1)
  {
  }
  /* USER CODE END Error_Handler */
}

#endif
沙发
chenyu360583472|  楼主 | 2016-6-28 15:41 | 只看该作者
各路大神 跪求指导

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

3

帖子

0

粉丝