⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sst25vf040b.c

📁 TMS320VC5509A烧写片外串行FLASH的程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/********************************************************************* * Filename:      sst25vf040b.c *                 * Description:    *                 * Copyright (C) 2009,  Vi.Wang * Author:        Vi.Wang <redfox_ww@126.com> * Created at:    Fri Apr 24 23:47:39 2009 *                 * Modified by:   Vi.Wang <redfox_ww@126.com> * Modified at:   Sat Apr 25 19:24:43 2009 *                 * Status:        Experimental, do not distribute. * Update count:  0 *                 ********************************************************************/                                                                     #include <stdio.h>#include <stdlib.h>#include <csl.h>#include <csl_mcbsp.h>#include <csl_gpio.h>/************************************************************************//* PROCEDURE: set_cs													*//*																		*//* This procedure set CE = High or CE = Low.							*//*																		*//* Input:																*//*		sta:  1 - H                                                     *//*            0 - L                                                     *//*																		*//* Output:																*//*		CE																*//*																		*//************************************************************************/static void set_cs(unsigned char sta) {    GPIO_FSET(IODATA, IO4D, sta ? 1 : 0);}/************************************************************************//* PROCEDURE: set_clk													*//*																		*//* This procedure set CLK = High or CLK = Low.							*//*																		*//* Input:																*//*		sta:  1 - H                                                     *//*            0 - L                                                     *//*																		*//* Output:																*//*		CLK																*//*																		*//************************************************************************/static void set_clk(unsigned char sta){    unsigned char i;        MCBSP_FSET(PCR0, CLKXP, sta ? 1 : 0);    for (i=0; i<5; i++);}/************************************************************************//* PROCEDURE: set_si													*//*																		*//* This procedure set SI = High or SI = Low.							*//*																		*//* Input:																*//*		sta:  1 - H                                                     *//*            0 - L                                                     *//*																		*//* Output:																*//*		SI																*//*																		*//************************************************************************/static void set_si(unsigned char sta){    MCBSP_FSET(PCR0, DXSTAT, sta ? 1 : 0);}/************************************************************************//* PROCEDURE: get_so													*//*																		*//* This procedure get SO status.										*//*																		*//* Input:																*//*		SO																*//* Output:																*//*																		*//* return:                                                              *//*     SO status                                                        *//*																		*//************************************************************************/static unsigned char get_so(void){    return(MCBSP_FGET(PCR0, DRSTAT));}/************************************************************************//* PROCEDURE: send_byte													*//*																		*//* This procedure outputs a byte shifting out 1-bit per clock rising	*//* edge on the the SI pin(LSB 1st).										*//*																		*//* Input:																*//*		dat																*//*																		*//* Output:																*//*		SI																*//************************************************************************/static void send_byte(unsigned char dat){		unsigned char i = 0;	for (i = 0; i < 8; i++)	{				if ((dat & 0x80) == 0x80)	/* check if MSB is high */			set_si(1);		else			set_si(0);					/* if not, set to low */		set_clk(1);					/* toggle clock high */		dat = (dat << 1);			/* shift 1 place for next bit */		set_clk(0);					/* toggle clock low */	}}/************************************************************************//* PROCEDURE: get_byte													*//*																		*//* This procedure inputs a byte shifting in 1-bit per clock falling		*//* edge on the SO pin(LSB 1st).											*//*																		*//* Input:																*//*		SO																*//*																		*//* Output:																*//*		None															*//************************************************************************/static unsigned char get_byte(void){	unsigned char i = 0, in = 0, temp = 0;	for (i = 0; i < 8; i++)	{		in = (in << 1);		/* shift 1 place to the left or shift in 0 */		temp = get_so();			/* save input */		set_clk(1);			/* toggle clock high */		if (temp == 1)			/* check to see if bit is high */			in |= 0x01;		/* if high, make bit high */		set_clk(0);			/* toggle clock low */	}	return in;}/************************************************************************//* PROCEDURE: flash_init												*//*																		*//* This procedure initializes the SCK to low and the CE to high.        *//* Must be called prior to up mode 0.									*//*																		*//* Input:																*//*		None															*//*																		*//* Output:																*//*		CE																*//*		SCK																*//************************************************************************/void flash_init(void){    set_cs(1);	set_clk(0);	/* set clock to low initial state */}/************************************************************************//* PROCEDURE: flash_read_status_register										*//*																		*//* This procedure read the status register and returns the byte.		*//*																		*//* Input:																*//*		None															*//*																		*//* Returns:																*//*		byte															*//************************************************************************/unsigned char flash_read_status_register(void){	unsigned char byte = 0;    	set_cs(0);				/* enable device */	send_byte(0x05);		/* send RDSR command */	byte = get_byte();		/* receive byte */	set_cs(1);				/* disable device */	return byte;}/************************************************************************//* PROCEDURE: flash_write_register_enable														*//*																		*//* This procedure Enables Write status register.  						*//*																		*//* Input:																*//*		None															*//*																		*//* Returns:																*//*		Nothing															*//************************************************************************/static void write_status_register_enable(void){    set_cs(0);				/* enable device */	send_byte(0x50);		/* enable writing to the status register */	set_cs(1);				/* disable device */}/************************************************************************//* PROCEDURE: flash_write_status_register														*//*																		*//* This procedure writes a byte to the status register.					*//*																		*//* Input:																*//*		byte															*//*																		*//* Returns:																*//*		Nothing															*//************************************************************************/void flash_write_status_register(unsigned char byte){    write_status_register_enable();    	set_cs(0);				/* enable device */	send_byte(0x01);		/* select write to status register */	send_byte(byte);		/* data that will change the status of BPx 							   or BPL (only bits 2,3,4,5,7 can be written) */	set_cs(1);				/* disable the device */}/************************************************************************//* PROCEDURE: wait_busy													*//*																		*//* This procedure waits until device is no longer busy (can be used by	*//* Byte-Program, Sector-erase, block-erase, Chip-erase).				*//*																		*//* Input:																*//*		None															*//*																		*//* Returns:																*//*		Nothing															*//************************************************************************/static void wait_busy(){	while ((flash_read_status_register() & 0x03) == 0x03)	/* waste time until not busy */		flash_read_status_register();}/************************************************************************//* PROCEDURE: wait_Busy_AAI												*//*																		*//* This procedure waits until device is no longer busy for AAI mode.	*//*																		*//* Input:																*//*		None															*//*																		*//* Returns:																*//*		Nothing															*//************************************************************************/static void wait_busy_aai(){	while ((flash_read_status_register() & 0x43) == 0x43)	/* waste time until not busy */		flash_read_status_register();}/************************************************************************//* PROCEDURE: flash_write_enable										*//*																		*//* This procedure enables the Write Enable Latch.  It can also be used 	*//* to Enables Write status register.									*//*																		*//* Input:																*//*		None															*//*																		*//* Returns:																*//*		Nothing															*//************************************************************************/void flash_write_enable(void){    write_status_register_enable();    	set_cs(0);				/* enable device */	send_byte(0x06);		/* send WREN command */	set_cs(1);				/* disable device */}/************************************************************************//* PROCEDURE: flash_write_disable										*//*																		*//* This procedure disables the Write Enable Latch.						*//*																		*//* Input:																*//*		None															*//*																		*//* Returns:																*//*		Nothing															*//************************************************************************/void flash_write_disable(void){    write_status_register_enable();    	set_cs(0);				/* enable device */	send_byte(0x04);		/* send WRDI command */	set_cs(1);				/* disable device */}/************************************************************************//* PROCEDURE: flash_jedec_id_read										*//*																		*//* This procedure reads the manufacturer's ID (BFh), memory type (25h)  *//* and device ID (8Dh).  It will use 9Fh as the JEDEC ID command.    	*//* Please see the product datasheet for details.						*//*																		*//* Input:																*//*		None															*//*																		*//* Returns:																*//*		IDs_read:ID1(Manufacture's ID = BFh, Memory Type (25h),			*//*		 and Device ID (8Dh)											*//*																		*//************************************************************************/unsigned long flash_jedec_id_read(void) {	unsigned long temp;		temp = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -