问题描述
[需求]: 支持SPI升级FPGA - openUBMC/vpd_09040B0102 - AtomGit | GitCode
当前想要参考这个链接中的信息,调用spi驱动接口刷fpga的固件信息到flash
环境信息
-
操作系统:[如 Ubuntu 24.04]
-
软件版本:[如 OpenUBMC2509]
-
硬件配置:[如 CPU、内存等]
重现步骤
spi_driver.lua
local log = require 'mc.logging'
local stream = require 'stream.base'
local class = require 'mc.class'
-- 系统时钟频率
local SYS_CLOCK_FREQ = 100000000
local SPI_BAUD_MIN = math.floor(SYS_CLOCK_FREQ / 65534 + 1)
-- SPI 驱动类
local SpiDriver = class(stream)
function SpiDriver:ctor(property, bus_name)
self.ctrl_id = property.CtrId or 0
self.chip_sel_id = property.ChipSelId or 0
self.pro_type = property.ProType or 0
self.baud = property.Baud or SPI_BAUD_MIN
self.frame_len = property.FrameLen or 8
self.clk_edge = property.ClkEdge or 0
self.idle_clk_pol = property.IdleClkPol or 0
end
function SpiDriver:init()
self.drv = stream.open_drive('libsoc_adapter.spi')
local init_data = self.drv.DEV_SPI_IOCTL_STD_INIT_S.new()
init_data.ulCtrl_ID = self.ctrl_id
init_data.ulChipSelId = self.chip_sel_id
init_data.ulProType = self.pro_type
init_data.ulBaud = self.baud
init_data.ulFrameLen = self.frame_len - 1
init_data.ulClkEdge = self.clk_edge
init_data.ulIdleClkPol = self.idle_clk_pol
self.drv:init(init_data)
log:notice('[SPI] SPI driver initialized: ctrl_id=%d, chip_sel_id=%d, baud=%d',
self.ctrl_id, self.chip_sel_id, self.baud)
end
function SpiDriver:read(input)
local read_data = self.drv.DEV_SPI_IOCTL_STD_READ_S.new()
read_data.ulCtrl_ID = self.ctrl_id
read_data.ulChipSelId = self.chip_sel_id
read_data.ulDataLen = input.len or 0
return self.drv:read(read_data, input.buffer)
end
function SpiDriver:write(input)
self.drv:write(self.ctrl_id, self.chip_sel_id, input.buffer)
end
function SpiDriver:transfer(tx_data, rx_len)
-- 写入数据
self:write({buffer = tx_data})
-- 读取数据
if rx_len and rx_len > 0 then
local rx_buffer = string.rep('\0', rx_len)
local result = self:read({buffer = rx_buffer, len = rx_len})
return result
end
return nil
end
return SpiDriver
实际调用demo如下
local SpiDriver = require 'unit_manager.class.logic_fw.upgrade.spi_driver'
-- Flash命令定义,具体参考Flash芯片手册
local FLASH_CMD = {
READ_STATUS_REG = 0x05,
}
-- 创建SPI驱动实例
function spi_upgrade.create_spi_driver(property, bus_name)
local driver = SpiDriver.new(property, bus_name)
driver:init()
return driver
end
-- Flash操作类
local flash_operator = {}
flash_operator.__index = flash_operator
--读取flash寄存器状态
-- 读取状态寄存器
function flash_operator:init(property, bus_name)
local driver = SpiDriver.new(property, bus_name)
driver:init()
self.spi_drv= driver
function flash_operator:read_status_reg()
local tx_data = string.char(FLASH_CMD.READ_STATUS_REG, 0x00)
local rx_data = self.spi_drv:transfer(tx_data, 2)
if not rx_data or #rx_data < 2 then
log:error('[Flash] Failed to read status register')
return nil
end
local status = string.byte(rx_data, 2)
log:debug('[Flash] Status register: 0x%02X', status)
return status
end
期望结果
期望可调用spi驱动接口升级fpga[需求]: 支持SPI升级FPGA - openUBMC/vpd_09040B0102 - AtomGit | GitCode,当前这么调用是否合理