完整程序源代码工程文件下载地址: 读写X5043.RAR
- /*******************************************************************************
- title : X5043.C
- device : XICOR 512*8bit Programmable Watchdog Supervisory E2PROM
- author : szlihongtao
- date : 2010-08-25
- version : 1.0
- note : X25045 驱动程序
- *******************************************************************************/
- #include "STM32f10x.h"
- #include "stm32_m.h"
- #include "x5043.h"
- //******************************************************************************
- static void delay1uS(void)
- {
- INT8U i;
- for(i=0;i<36;++i);
- }
- //*****************************************************************************
- static void set_sck(void)
- {
- GPIO_SetBits(GPIOC, GPIO_Pin_7);
- delay1uS();
- }
- //*****************************************************************************
- static void clr_sck(void)
- {
- GPIO_ResetBits(GPIOC, GPIO_Pin_7);
- delay1uS();
- }
- //*****************************************************************************
- static void set_wp(void)
- {
- delay1uS();
- }
- //*****************************************************************************
- static void clr_wp(void)
- {
- delay1uS();
- }
- //*****************************************************************************
- static void set_cs(void)
- {
- GPIO_SetBits(GPIOC, GPIO_Pin_4);
- delay1uS();
- }
- //*****************************************************************************
- static void clr_cs(void)
- {
- GPIO_ResetBits(GPIOC, GPIO_Pin_4);
- delay1uS();
- }
- //*****************************************************************************
- static void set_si(void)
- {
- GPIO_SetBits(GPIOC, GPIO_Pin_6);
- delay1uS();
- }
- //*****************************************************************************
- static void clr_si(void)
- {
- GPIO_ResetBits(GPIOC, GPIO_Pin_6);
- delay1uS();
- }
- //*****************************************************************************
- static INT8U get_so_x5043(void)
- {
- return (GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_6));
- }
- //*****************************************************************************
- static void set_sio_input(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- delay1uS();
- }
- //*****************************************************************************
- static void set_sio_output(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- delay1uS();
- }
- //******************************************************************************
- /*25045操作子程序集*/
- /*;*****************************************************************************
- *
- ;* Name: RST_WDOG
- ;* Description: Reset Watchdog Timer
- ;* Function: This routine resets the watchdog timer without sending a command
- ;* Calls: None
- ;* Input: None
- ;* Outputs: None
- ;* Register Usage: None
- ;*******************************************************************************
- */
- /*复位DOG*/
- void rst_wdog (void)
- {
- clr_cs();
- set_cs();
- }
- //******************************************************************************
- //上电以后,在对25045操作之前必须执行本子程序一次
- //******************************************************************************
- static void x25045_start(void)
- {
- set_cs();
- clr_cs();
- set_cs();
- }
- /*;*****************************************************************************
- ;*
- ;* Name: OUTBYT
- ;* Description: Sends byte to EEPROM
- ;* Function: This routine shifts out a byte, starting with the MSB, to the EEPROM
- ;* Calls: None
- ;* Input: A = byte to be sent
- ;* Outputs: None
- ;* Register Usage: R0, A
- ;*******************************************************************************
- */
- /* 输出一个数据到25045,此数据可能为地址,先导字,写入的数据等*/
- // data_write(INT8U aa)
- //*****************************************************************************
- static void outbyt(INT8U data_wr)
- {
- INT8U i;
- set_sio_output();
- for (i=0;i<8;++i)
- {
- clr_sck();
- // SI=(aa&0x80);
- if (data_wr&0x80)
- set_si();
- else
- clr_si();
- set_sck();
- data_wr <<= 1;
- }
- clr_si(); /*使SI处于确定的状态*/
- }
- /*;****************************************************************************
- *
- ;* Name: INPUTBYT
- ;* Description: Recieves byte from EEPROM
- ;* Function: This routine recieves a byte, MSB first, from the EEPROM
- ;* Calls: None
- ;* Input: None
- ;* Outputs: A = recieved byte
- ;* Register Usage: R0, A
- ;******************************************************************************
- */
- /*得到一个数据,此数据可能为状态寄存器数据,读出的单元数据等*/
- // data_read
- //*****************************************************************************
- static INT8U inputbyt(void)
- {
- INT8U temp,i;
- temp=0;
- set_sio_input();
- for (i=0;i<8;++i)
- {
- temp<<=1;
- set_sck();
- clr_sck();
- if (get_so_x5043())
- temp |=0x1;
- }
- return (temp);
- }
- /*;***************************************************************************
- *
- ;* Name: RDSR_CMD
- ;* Description: Read Status Register
- ;* Function: This routine sends the command to read the status register
- ;* Calls: outbyt, inputbyt
- ;* Input: None
- ;* Outputs: A = status registerXicor Application Note AN21
- ;* Register Usage: A
- ;****************************************************************************
- */
- /*读状态寄存器,读出的数据放入到data_rd中*/
- //*****************************************************************************
- INT8U rdsr_cmd (void)
- {
- INT8U data_rd;
- clr_cs();
- clr_sck();
- outbyt(RDSR_INST);
- data_rd=inputbyt();
- clr_sck();
- set_cs();
- return (data_rd);
- }
- /*;***************************************************************************
- *
- ;* Name: WIP_POLL
- ;* Description: Write-In-Progress Polling
- ;* Function: This routine polls for completion of a nonvolatile write cycle by examining the
- ;* WIP bit of the status register
- ;* Calls: rdsr_cmdXicor Application Note AN21
- ;* Input: None
- ;* Outputs: None
- ;* Register Usage: R1, A
- ;*****************************************************************************
- */
- /*检测写入的过程是否结束*/
- //*****************************************************************************
- static void wip_poll(void)
- {
- INT8U status1,cnt;
- for (cnt=0;cnt {
- delay1uS();
- status1=rdsr_cmd();
- if ((status1&0x01)==0)
- break;
- }
- }
- /*;***************************************************************************
- *
- ;* Name: WRSR_CMD
- ;* Description: Write Status Register
- ;* Function: This routine sends the command to write the WD0, WD1, BP0 and BP0 EEPROM
- ;* bits in the status register
- ;* Calls: outbyt, wip_poll
- ;* Input: None
- ;* Outputs: None
- ;* Register Usage: A
- ;*****************************************************************************
- */
- /*写状态寄存器子程序
- 用于设置看门狗时间,区块保护等
- */
- //*****************************************************************************
- void wrsr_cmd(void)
- {
- x25045_start();
- wren_cmd();
- clr_sck(); /* Bring SCK low */
- clr_cs(); /* Bring /CS low */
- outbyt(WRSR_INST) ; /* Send WRSR instruction */
- outbyt(STATUS_REG); /* Send status register */
- clr_sck(); /* Bring SCK low */
- set_cs(); /* Bring /CS high */
- wip_poll(); /* Poll for completion of write cycle */
- wrdi_cmd();
- return;
- }
- /*;****************************************************************************
- *
- ;* Name: BYTE_WRITE
- ;* Description: Single Byte Write
- ;* Function: This routine sends the command to write a single byte to the EEPROM memory
- array
- ;* Calls: outbyt, wip_poll
- ;* Input: None
- ;* Outputs: None
- ;* Register Usage: A, B
- ;******************************************************************************
- */
- /*字节写入,ee_data为写入的数据,address为写入的地址,对于25045而言为000-1FF*/
- /*
- Write Enable Latch
- The X25043/45 contains a “write enable” latch. This
- latch must be SET before a write operation will be
- completed internally. The WREN instruction will set the
- latch and the WRDI instruction will reset the latch. This
- latch is automatically reset upon a power-up condition
- and after the completion of a byte, page, or status
- register write cycle. The latch is also reset if WP is
- brought LOW.
- 字节,页面,状态字写周期结束后,自动会禁止写操作
- */
- //*****************************************************************************
- void byte_write(INT16U address,INT8U ee_data)
- {
- INT8U temp;
- wren_cmd();
- set_wp();
- clr_sck();
- clr_cs();
- temp=(INT8U)((address&0x0100)>>5);
- temp=temp|WRITE_INST;
- outbyt(temp); /* Send WRITE instruction including MSB of address */
- /*将高位地址左移3位与写入先导字相或,得到正确的先导字写入25045*/
- temp=address&0xff;
- outbyt(temp); /*输出低位地址到25045*/
- outbyt(ee_data); /*写入数据到25045的对应单元*/
- clr_sck();
- set_cs();
- wip_poll(); /*检测是否写完*/
- clr_wp();
- wrdi_cmd();
- return;
- }
- /*;****************************************************************************
- ;* Name: BYTE_READ
- ;* Description: Single Byte Read
- ;* Function: This routine sends the command to read a single byte from the EEPROM memory
- array
- ;* Calls: outbyt, inputbyt
- ;* Input: None
- ;* Outputs: A = read byte
- ;* Register Usage: A, BXicor Application Note AN21
- ;******************************************************************************
- */
- /*字节读出,其中dd为读出的地址,返回的值为读出的数据*/
- //*****************************************************************************
- INT8U byte_read(INT16U addr)
- {
- INT8U temp;
- clr_sck();
- clr_cs();
- temp=(INT8U)((addr&0x0100)>>5);
- temp=temp|READ_INST;
- outbyt(temp); /* Send READ_INST instruction including MSB of address */
- /*将高位地址左移3位与读出先导字相或,得到正确的先导字写入25045*/
- temp=addr&0xff;
- outbyt(temp); /*输出低位地址到25045*/
- temp=inputbyt(); /*得到读出的数据*/
- clr_sck();
- set_cs();
- return (temp);
- }
- //*****************************************************************************
- // 连续读多个字节
- //*****************************************************************************
- void byte_read_sequence(INT16U addr,INT8U arr[],INT16U length)
- {
- INT8U temp;
- INT16U i;
- clr_sck();
- clr_cs();
- temp=(INT8U)((addr&0x0100)>>5);
- temp=temp|READ_INST;
- outbyt(temp); /* Send READ_INST instruction including MSB of address */
- /*将高位地址左移3位与读出先导字相或,得到正确的先导字写入25045*/
- temp=addr&0xff;
- outbyt(temp); /*输出低位地址到25045*/
- length&=512-1;
- for(i=0;i arr=inputbyt(); /*得到读出的数据*/
- clr_sck();
- set_cs();
- return;
- }
- /*****************************************************************************
- *
- ;* Name: WREN_CMD
- ;* Description: Set write enable latch
- ;* Function: This routine sends the command to enable writes to the EEPROM memory array or
- ;* status register
- ;* Calls: outbyt
- ;* Input: None
- ;* Outputs: None
- ;* Register Usage: A
- ;******************************************************************************
- */
- //*****************************************************************************
- // 允许写入数据到X5043
- // 写使能子程序
- //*****************************************************************************
- void wren_cmd(void)
- {
- set_wp();
- clr_sck(); /* Bring SCK low */
- clr_cs(); /* Bring /CS low */
- outbyt(WREN_INST); /* Send WREN instruction */
- clr_sck(); /* Bring SCK low */
- set_cs(); /* Bring /CS high */
- clr_wp();
- return;
- }
- //*****************************************************************************
- // 禁止写入数据到X5043
- //*****************************************************************************
- void wrdi_cmd(void)
- {
- set_wp();
- clr_sck(); /* Bring SCK low */
- clr_cs(); /* Bring /CS low */
- outbyt(WRDI_INST); /* Send WRDI_INST instruction */
- clr_sck(); /* Bring SCK low */
- set_cs(); /* Bring /CS high */
- clr_wp();
- return;
- }
- //*****************************************************************************
- #if 1
- #define BASE_ADDR 10
- INT8U i,arr[8];
- void test_x5043(void)
- {
- wrsr_cmd();
- i=rdsr_cmd();
- for(i=0;i<8;++i) arr=0;
- for(i=0;i<8;++i)
- arr=x5043_rd(i+BASE_ADDR);
- //----------------------------------------
- for(i=0;i<8;++i)
- x5043_wr(i+BASE_ADDR,6*i);
- for(i=0;i<8;++i) arr=0;
- for(i=0;i<8;++i)
- arr=x5043_rd(i+BASE_ADDR);
- for(i=0;i<8;++i) arr=0;
- byte_read_sequence(BASE_ADDR,arr,8);
- //----------------------------------------
- x5043_wr(2+BASE_ADDR,17);
- for(i=0;i<8;++i)
- arr=0;
- for(i=0;i<8;++i)
- arr=x5043_rd(i+BASE_ADDR);
- delay1uS();
- }
- #endif
- //*****************************************************************************
- //************************* END ******************************************
- //*****************************************************************************
- \032
- /*******************************************************************************
- STM32学习日志(202)----读写X5043
- 编译环境: EWARM V5.30
- 硬件环境:51hei
- STM32 FW: V3.0.0
- 作者 : szlihongtao
- 时间 : 2017-08-25
- *******************************************************************************/
- /**
- ******************************************************************************
- * @file Project/Template/main.c
- * @author MCD Application Team
- * @version V3.0.0
- * @date 04/06/2009
- * @brief Main program body
- ******************************************************************************
- * @copy
- *
- * 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.
- *
- *
© COPYRIGHT 2009 STMicroelectronics - */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include "stm32_m.h"
- //******************************************************************************
- static void delayms(INT16U cnt)
- {
- INT16U i;
- while(cnt--)
- for (i=0; i<7333; i++);
- }
- //******************************************************************************
- // 时钟设置初始化
- //******************************************************************************
- static void RCC_Configuration(void)
- {
- ErrorStatus HSEStartUpStatus;
- /*
- RCC_AdjustHSICalibrationValue 调整内部高速晶振(HSI)校准值
- RCC_ITConfig 使能或者失能指定的RCC中断
- RCC_ClearFlag 清除RCC的复位标志位
- RCC_GetITStatus 检查指定的RCC中断发生与否
- RCC_ClearITPendingBit 清除RCC的中断待处理位
- */
- /* RCC system reset(for debug purpose) */
- // 时钟系统复位
- RCC_DeInit();
- // 使能外部的8M晶振
- // 设置外部高速晶振(HSE)
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- // 使能或者失能内部高速晶振(HSI)
- RCC_HSICmd(DISABLE);
- // 等待HSE起振
- // 该函数将等待直到HSE就绪,或者在超时的情况下退出
- /* Wait till HSE is ready */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- /* HCLK = SYSCLK */
- // 设置AHB时钟(HCLK)
- RCC_HCLKConfig(RCC_SYSCLK_Div1); // 72 MHz
- /* PCLK1 = HCLK/2 */
- // 设置低速AHB时钟(PCLK1)
- RCC_PCLK1Config(RCC_HCLK_Div2); // 36 MHz
- /* PCLK2 = HCLK */
- // 设置高速AHB时钟(PCLK2)
- RCC_PCLK2Config(RCC_HCLK_Div1); // 72 MHz
- /* ADCCLK = PCLK2/8 */
- // 设置ADC时钟(ADCCLK)
- RCC_ADCCLKConfig(RCC_PCLK2_Div8);
- // 设置USB时钟(USBCLK)
- // USB时钟 = PLL时钟除以1.5
- RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
- // 设置外部低速晶振(LSE)
- RCC_LSEConfig(RCC_LSE_OFF);
- // 使能或者失能内部低速晶振(LSI)
- // LSE晶振OFF
- RCC_LSICmd(DISABLE);
- // 设置RTC时钟(RTCCLK)
- // 选择HSE时钟频率除以128作为RTC时钟
- RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
- // 使能或者失能RTC时钟
- // RTC时钟的新状态
- RCC_RTCCLKCmd(DISABLE);
- /* Flash 2 wait state */
- FLASH_SetLatency(FLASH_Latency_2);
- /* Enable Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* PLLCLK = 8MHz * 9 = 72 MHz */
- // 设置PLL时钟源及倍频系数
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* Enable PLL */
- // 使能或者失能PLL
- RCC_PLLCmd(ENABLE);
- /* Wait till PLL is ready */
- // 检查指定的RCC标志位设置与否
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- {
- }
- /* Select PLL as system clock source */
- // 设置系统时钟(SYSCLK)
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- // 返回用作系统时钟的时钟源
- while(RCC_GetSYSCLKSource() != 0x08)
- {
- }
- }
- // 使能或者失能AHB外设时钟
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
- |RCC_AHBPeriph_DMA2
- |RCC_AHBPeriph_SRAM
- |RCC_AHBPeriph_FLITF
- |RCC_AHBPeriph_CRC
- |RCC_AHBPeriph_FSMC
- |RCC_AHBPeriph_SDIO,DISABLE);
- // 使能或者失能APB1外设时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);
- // 强制或者释放高速APB(APB2)外设复位
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
- // 退出复位状态
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);
- // 强制或者释放低速APB(APB1)外设复位
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);
- // 强制或者释放后备域复位
- RCC_BackupResetCmd(ENABLE);
- // 使能或者失能时钟安全系统
- RCC_ClockSecuritySystemCmd(DISABLE);
- }
- //******************************************************************************
- // NVIC设置
- //******************************************************************************
- static void NVIC_Configuration(void)
- {
- }
- //******************************************************************************
- // SysTick设置初始化
- //******************************************************************************
- static void SysTick_Config1(void)
- {
- #define SystemFreq 72000000.0 // 单位为Hz
- #define TB_SysTick 100000.0 // 单位为uS,与示波器实测一致
- INT32U ticks;
- ticks=(INT32U)((TB_SysTick/1000000.0)*SystemFreq);
- ticks=ticks;
- //SysTick_Config(ticks);
- }
- //******************************************************************************
- // GPIO设置
- //******************************************************************************
- static void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC
- |RCC_APB2Periph_GPIOD, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- }
- //******************************************************************************
- // 主程序
- //******************************************************************************
- int main(void)
- {
- RCC_Configuration();
- GPIO_Configuration();
- NVIC_Configuration();
- SysTick_Config1();
- delayms(10);
- test_x5043();
- for (;;)
- {
- delayms(100);
- GPIOC->ODR ^= GPIO_Pin_5; // led4 toogle
- }
- }
- //******************************************************************************
- #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 %drn", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /**
- * @}
- */
- //******************************************************************************
- /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
- //******************************************************************************
- /*
- LED2---------PC7
- LED3---------PC6
- LED4---------PC5
- LED5---------PC4
- KEY2---------PD3
- KEY3---------PD4
- */
|
0
|
|
|
|