编写一个支持zynq 32M flash大小的fsbl代码
时间: 2024-05-08 13:21:44 浏览: 204
zynq_fsbl_flash.elf
以下是一个简单的支持 Zynq 32M Flash 大小的 FSBL 代码示例:
```c
#include <stdio.h>
#include "xil_printf.h"
#include "xil_io.h"
#include "xil_types.h"
#include "xparameters.h"
#include "xil_exception.h"
#include "xil_cache.h"
#include "xil_mmu.h"
#include "xil_ddr.h"
#define FLASH_BASEADDR XPAR_MICROBLAZE_0_S_AXI_BASEADDR
// Flash commands
#define RDID_CMD 0x9F // Read Identification
#define RDSR_CMD 0x05 // Read Status Register
#define WREN_CMD 0x06 // Write Enable
#define PP_CMD 0x02 // Page Program
#define SE_CMD 0xD8 // Sector Erase
// Flash status register bits
#define WIP_BIT 0x01 // Write In Progress
// Function prototypes
void init_mmu();
u32 read_flash_id();
void erase_flash();
void program_flash(u32 address, u8 *data, u32 size);
int main() {
init_mmu();
xil_printf("FSBL Booting...\r\n");
// Read the Flash ID
u32 flash_id = read_flash_id();
xil_printf("Flash ID: %08x\r\n", flash_id);
// Erase the Flash
erase_flash();
// Program the Flash
u8 data[4096] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
program_flash(FLASH_BASEADDR, data, sizeof(data));
xil_printf("FSBL Done.\r\n");
return 0;
}
void init_mmu() {
// Initialize the MMU
Xil_SetTlbAttributes(0x0, 0x14de2);
Xil_SetTlbAttributes(0x80000000, 0x14de2);
Xil_SetTlbAttributes(0xC0000000, 0x14de2);
Xil_SetTlbAttributes(0xE0000000, 0x14de2);
Xil_SetTlbAttributes(0xF8000000, 0x14de2);
}
u32 read_flash_id() {
// Read the Flash ID
u8 cmd = RDID_CMD;
Xil_Out8(FLASH_BASEADDR, cmd);
u32 id = Xil_In32(FLASH_BASEADDR + 1);
return id;
}
void erase_flash() {
// Enable write access
u8 cmd = WREN_CMD;
Xil_Out8(FLASH_BASEADDR, cmd);
// Send sector erase command
cmd = SE_CMD;
Xil_Out8(FLASH_BASEADDR, cmd);
Xil_Out8(FLASH_BASEADDR + 1, 0x00);
Xil_Out8(FLASH_BASEADDR + 2, 0x00);
Xil_Out8(FLASH_BASEADDR + 3, 0x00);
// Wait for erase to complete
u8 status = 0;
do {
cmd = RDSR_CMD;
Xil_Out8(FLASH_BASEADDR, cmd);
status = Xil_In8(FLASH_BASEADDR + 1);
} while (status & WIP_BIT);
}
void program_flash(u32 address, u8 *data, u32 size) {
// Enable write access
u8 cmd = WREN_CMD;
Xil_Out8(FLASH_BASEADDR, cmd);
// Program the data
u32 i;
for (i = 0; i < size; i += 4) {
cmd = PP_CMD;
Xil_Out8(FLASH_BASEADDR, cmd);
Xil_Out8(FLASH_BASEADDR + 1, (address >> 16) & 0xFF);
Xil_Out8(FLASH_BASE
阅读全文