Make common_arm to hardware abstraction layer.
This commit is contained in:
1 parent
51d0ff260a
commit
36f1c7e44c
61 files changed
+8050
-1670
No files matched your search
@@ -0,0 +1,377 @@
|
||||
#ifndef FPGA_APIS_H_
|
||||
#define FPGA_APIS_H_
|
||||
|
||||
#include "common.h"
|
||||
#include "fpga.h"
|
||||
|
||||
|
||||
/*
|
||||
Communication between ARM / FPGA is done inside armsrc/fpgaloader.c see: function FpgaSendCommand()
|
||||
Send 16 bit command / data pair to FPGA with the bit format:
|
||||
|
||||
+------ frame layout circa 2020 ------------------+
|
||||
| 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
|
||||
+-------------------------------------------------+
|
||||
| C C C C M M M M P P P P P P P P | C = FPGA_CMD_SET_CONFREG, M = FPGA_MAJOR_MODE_*, P = FPGA_LF_* or FPGA_HF_* parameter
|
||||
| C C C C D D D D D D D D | C = FPGA_CMD_SET_DIVISOR, D = divisor
|
||||
| C C C C T T T T T T T T | C = FPGA_CMD_SET_EDGE_DETECT_THRESHOLD, T = threshold
|
||||
| C C C C E | C = FPGA_CMD_TRACE_ENABLE, E=0 off, E=1 on
|
||||
| C C C C P P P P P P P P P P P P | C = FPGA_CMD_SET_PWR_PWM_LOW_COUNT, P = low count value for HF/LF driver power PWM (PM5)
|
||||
+-------------------------------------------------+
|
||||
|
||||
+------ frame layout current ---------------------+
|
||||
| 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
|
||||
+-------------------------------------------------+
|
||||
| C C C C M M M P P P P P P | C = FPGA_CMD_SET_CONFREG, M = FPGA_MAJOR_MODE_*, P = FPGA_LF_* or FPGA_HF_* parameter
|
||||
| C C C C D D D D D D D D | C = FPGA_CMD_SET_DIVISOR, D = divisor
|
||||
| C C C C T T T T T T T T | C = FPGA_CMD_SET_EDGE_DETECT_THRESHOLD, T = threshold
|
||||
| C C C C E | C = FPGA_CMD_TRACE_ENABLE, E=0 off, E=1 on
|
||||
+-------------------------------------------------+
|
||||
|
||||
shift_reg receive this 16bit frame
|
||||
|
||||
LF command
|
||||
----------
|
||||
shift_reg[15:12] == 4bit command
|
||||
LF has three commands (FPGA_CMD_SET_CONFREG, FPGA_CMD_SET_DIVISOR, FPGA_CMD_SET_EDGE_DETECT_THRESHOLD)
|
||||
Current commands uses only 2bits. We have room for up to 4bits of commands total (7).
|
||||
|
||||
LF data
|
||||
-------
|
||||
shift_reg[11:0] == 12bit data
|
||||
lf data is divided into MAJOR MODES and configuration values.
|
||||
|
||||
The major modes uses 3bits (0,1,2,3,7 | 000, 001, 010, 011, 111)
|
||||
000 FPGA_MAJOR_MODE_LF_READER = Act as LF reader (modulate)
|
||||
001 FPGA_MAJOR_MODE_LF_EDGE_DETECT = Simulate LF
|
||||
010 FPGA_MAJOR_MODE_LF_PASSTHRU = Passthrough mode, CROSS_LO line connected to SSP_DIN. SSP_DOUT logic level controls if we modulate / listening
|
||||
011 FPGA_MAJOR_MODE_LF_ADC = refactor hitag 2, clear ADC sampling
|
||||
111 FPGA_MAJOR_MODE_OFF = turn off sampling.
|
||||
|
||||
Each one of this major modes can have options. Currently these two major modes uses options.
|
||||
- FPGA_MAJOR_MODE_LF_READER
|
||||
- FPGA_MAJOR_MODE_LF_EDGE_DETECT
|
||||
|
||||
FPGA_MAJOR_MODE_LF_READER
|
||||
-------------------------------------
|
||||
lf_field = 1bit (FPGA_LF_ADC_READER_FIELD)
|
||||
|
||||
You can send FPGA_CMD_SET_DIVISOR to set with FREQUENCY the fpga should sample at
|
||||
divisor = 8bits shift_reg[7:0]
|
||||
|
||||
FPGA_MAJOR_MODE_LF_EDGE_DETECT
|
||||
------------------------------------------
|
||||
lf_ed_toggle_mode = 1bits
|
||||
lf_ed_threshold = 8bits threshold defaults to 127
|
||||
|
||||
You can send FPGA_CMD_SET_EDGE_DETECT_THRESHOLD to set a custom threshold
|
||||
lf_ed_threshold = 8bits threshold value.
|
||||
|
||||
conf_word 12bits
|
||||
conf_word[7:5] = 3bit major mode.
|
||||
conf_word[0] = 1bit lf_field
|
||||
conf_word[1] = 1bit lf_ed_toggle_mode
|
||||
conf_word[7:0] = 8bit divisor
|
||||
conf_word[7:0] = 8bit threshold
|
||||
|
||||
*/
|
||||
// Defining commands, modes and options. This must be aligned to the definitions in fpga/define.v
|
||||
#define FPGA_MAJOR_MODE_MASK 0x01C0
|
||||
#define FPGA_MINOR_MODE_MASK 0x003F
|
||||
|
||||
// Definitions for the FPGA commands.
|
||||
#define FPGA_CMD_SET_CONFREG (1<<12)
|
||||
#define FPGA_CMD_SET_DIVISOR (2<<12)
|
||||
#define FPGA_CMD_SET_EDGE_DETECT_THRESHOLD (3<<12)
|
||||
#define FPGA_CMD_TRACE_ENABLE (2<<12)
|
||||
#define FPGA_CMD_SET_PWR_PWM_LOW_COUNT (4<<12) // For PM5
|
||||
|
||||
// Major modes
|
||||
#define FPGA_MAJOR_MODE_LF_READER (0<<6)
|
||||
#define FPGA_MAJOR_MODE_LF_EDGE_DETECT (1<<6)
|
||||
#define FPGA_MAJOR_MODE_LF_PASSTHRU (2<<6)
|
||||
#define FPGA_MAJOR_MODE_LF_ADC (3<<6)
|
||||
|
||||
#define FPGA_MAJOR_MODE_HF_READER (0<<6)
|
||||
#define FPGA_MAJOR_MODE_HF_SIMULATOR (1<<6)
|
||||
#define FPGA_MAJOR_MODE_HF_ISO14443A (2<<6)
|
||||
#define FPGA_MAJOR_MODE_HF_SNIFF (3<<6)
|
||||
#define FPGA_MAJOR_MODE_HF_ISO18092 (4<<6)
|
||||
#define FPGA_MAJOR_MODE_HF_GET_TRACE (5<<6)
|
||||
#define FPGA_MAJOR_MODE_OFF (7<<6)
|
||||
|
||||
// Options for LF_READER
|
||||
#define FPGA_LF_ADC_READER_FIELD ( 1 )
|
||||
|
||||
// Options for LF_EDGE_DETECT
|
||||
#define FPGA_LF_EDGE_DETECT_READER_FIELD ( 1 )
|
||||
#define FPGA_LF_EDGE_DETECT_TOGGLE_MODE ( 2 )
|
||||
|
||||
// Options for the generic HF reader
|
||||
#define FPGA_HF_READER_MODE_RECEIVE_IQ ( 0 )
|
||||
#define FPGA_HF_READER_MODE_RECEIVE_AMPLITUDE ( 1 )
|
||||
#define FPGA_HF_READER_MODE_RECEIVE_PHASE ( 2 )
|
||||
#define FPGA_HF_READER_MODE_SEND_FULL_MOD ( 3 )
|
||||
#define FPGA_HF_READER_MODE_SEND_SHALLOW_MOD ( 4 )
|
||||
#define FPGA_HF_READER_MODE_SNIFF_IQ ( 5 )
|
||||
#define FPGA_HF_READER_MODE_SNIFF_AMPLITUDE ( 6 )
|
||||
#define FPGA_HF_READER_MODE_SNIFF_PHASE ( 7 )
|
||||
#define FPGA_HF_READER_MODE_SEND_JAM ( 8 )
|
||||
#define FPGA_HF_READER_MODE_SEND_SHALLOW_MOD_RDV4 ( 9 )
|
||||
|
||||
#define FPGA_HF_READER_SUBCARRIER_848_KHZ (0<<4)
|
||||
#define FPGA_HF_READER_SUBCARRIER_424_KHZ (1<<4)
|
||||
#define FPGA_HF_READER_SUBCARRIER_212_KHZ (2<<4)
|
||||
#define FPGA_HF_READER_2SUBCARRIERS_424_484_KHZ (3<<4)
|
||||
|
||||
// Options for the HF simulated tag, how to modulate
|
||||
#define FPGA_HF_SIMULATOR_NO_MODULATION ( 0 )
|
||||
#define FPGA_HF_SIMULATOR_MODULATE_BPSK ( 1 )
|
||||
#define FPGA_HF_SIMULATOR_MODULATE_212K ( 2 )
|
||||
#define FPGA_HF_SIMULATOR_MODULATE_424K ( 4 )
|
||||
#define FPGA_HF_SIMULATOR_MODULATE_424K_8BIT ( 5 )
|
||||
|
||||
// Options for ISO14443A
|
||||
#define FPGA_HF_ISO14443A_SNIFFER ( 0 )
|
||||
#define FPGA_HF_ISO14443A_TAGSIM_LISTEN ( 1 )
|
||||
#define FPGA_HF_ISO14443A_TAGSIM_MOD ( 2 )
|
||||
#define FPGA_HF_ISO14443A_READER_LISTEN ( 3 )
|
||||
#define FPGA_HF_ISO14443A_READER_MOD ( 4 )
|
||||
|
||||
// Options for ISO18092 / Felica
|
||||
#define FPGA_HF_ISO18092_FLAG_NOMOD ( 1 ) // 0001 disable modulation module
|
||||
#define FPGA_HF_ISO18092_FLAG_424K ( 2 ) // 0010 should enable 414k mode (untested). No autodetect
|
||||
#define FPGA_HF_ISO18092_FLAG_READER ( 4 ) // 0100 enables antenna power, to act as a reader instead of tag
|
||||
|
||||
// Options for adc mux.
|
||||
// The mux is no longer set directly through the GPIO PIN to solve the problem of high coupling with the platform.
|
||||
typedef enum {
|
||||
ADC_MUXSEL_HIPKD = 0U,
|
||||
ADC_MUXSEL_LOPKD,
|
||||
ADC_MUXSEL_LORAW,
|
||||
ADC_MUXSEL_HIRAW,
|
||||
} adc_mux_io_t;
|
||||
|
||||
// Block and wait for SSC data to be ready.
|
||||
#define FPGA_SSC_RX_READY_WAIT() while(!FPGA_SSC_RX_Ready()) {}
|
||||
|
||||
// Check if data already ready.
|
||||
// On the AT91 platform, There is no need to consider overflow, as the data is always up-to-date.
|
||||
// On the AT32 platform, You must call this function to confirm that the data is ready before reading the value.
|
||||
// Warn: Continuously call this function to refresh the rx state, to avoid receiving stopped due to unused old data!
|
||||
// If no this function call after DELAY/SlowTask, you may always get a fixed old data!!!
|
||||
// !!! For maximum platform compatibility, it is essential to call this function !!!
|
||||
STATIC_FORCE_INLINE bool FPGA_SSC_RX_Ready(void);
|
||||
|
||||
// Check if data can transmit next one.
|
||||
// The data clk is from fpga, so if fpga rx & process done, the next byte can transmit.
|
||||
STATIC_FORCE_INLINE bool FPGA_SSC_TX_Ready(void);
|
||||
|
||||
// Check if RX by DMA is done.
|
||||
// Note: Only call this function to check data ready when DMA running.
|
||||
STATIC_FORCE_INLINE bool FPGA_SSC_DMA_RX_Done(void);
|
||||
|
||||
// Check if TX done. if done? next byte can put in DT register by FPGA_SSC_TX_Value() function.
|
||||
// Note: Call this function before FPGA_SSC_TX_Value() calling.
|
||||
STATIC_FORCE_INLINE bool FPGA_SSC_TX_Done(void);
|
||||
|
||||
// Read the data received by SSC. The number of bits and bits order of the data are determined when configuring SSC.
|
||||
// Note: this function has different characteristics on different platforms.
|
||||
// On the AT91 platform, you can always get the latest received data.
|
||||
// On the AT32 platform, if you don't check if the data is already ready, you may get an old data.
|
||||
// Warn: We must first ensure that the data is ready, call the FPGA_SSC_RX_READY_WAIT() or FPGA_SSC_RX_Ready()
|
||||
STATIC_FORCE_INLINE uint32_t FPGA_SSC_RX_Value(void);
|
||||
|
||||
// Send the data by SSC, no DMA.
|
||||
// Warn: Before sending, it is necessary to check if the previous sending has been completed!
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_TX_Value(uint32_t v);
|
||||
|
||||
// Some platforms' send(data) registers may not automatically reset to zero.
|
||||
// We need to ensure that a clearing action is performed before and after sending.
|
||||
// Problem solved: If the sending (data) register is not cleared or non-zero data is received,
|
||||
// it may cause erroneous modulation by continuing to send non-zero data after the transmission is completed.
|
||||
// Note: This function will not wait for the sending to complete(Just waiting for TX ready).
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_TX_Clear(void);
|
||||
|
||||
// DMA rx disable
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Disable(void);
|
||||
|
||||
// DMA rx enable
|
||||
// Note: Just started DMA transfer, will not reconfigure DMA.
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Enable(void);
|
||||
|
||||
// The buf address currently receiving and storing data (not the address of data that has already been received)
|
||||
// |done|done|done|working| <- you will get 'working' address.
|
||||
STATIC_FORCE_INLINE uint32_t* FPGA_SSC_DMA_RX_Current_Address(void);
|
||||
|
||||
// How much data still needs to be received?
|
||||
// After receiving an item each time, subtract 1 from this value.
|
||||
// Note: Is not bytes count, the bytes count is from FPGA_SSC_DMA_RX_Remaining_Count() * SSC_DATA_WIDTH
|
||||
STATIC_FORCE_INLINE uint16_t FPGA_SSC_DMA_RX_Remaining_Count(void);
|
||||
|
||||
// Continuing to trigger the next reception,
|
||||
// DMA will automatically perform address rotation when the device supports NEXT BUF.
|
||||
// Attention: This may result in data being overwritten.
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Repeat(void *buf, uint16_t len);
|
||||
|
||||
// Continue to trigger the next reception.
|
||||
// This function will only trigger one reception and will not automatically trigger two receptions using the same address.
|
||||
// It can be used when data processing speed is slow or when asynchronous reception processing with multiple buffers is required.
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Single(void *buf, uint16_t len);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Provide a 24MHz clock from ARM to FPGA
|
||||
// This is the most important main clock for FPGA, so it must be implemented!
|
||||
//-----------------------------------------------------------------------------
|
||||
void FpgaSetup24MHzClk(void);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Reset the fpga communication interface of Fpga
|
||||
// 1. SPI for CMD
|
||||
// 2. SSC for DataStream
|
||||
// In AT32, it's reset the both spi, no SSC.
|
||||
//-----------------------------------------------------------------------------
|
||||
void FpgaResetComInterface(void);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// The working mode of FPGA corresponds to the SSC communication frame mode. For platform compatibility,
|
||||
// the cross-platform design only supports 8 or 16 bits.
|
||||
// If the function returns 1, it is 16 bits data and MSB,
|
||||
// otherwise, it is 8 bits data and MSB.
|
||||
//-----------------------------------------------------------------------------
|
||||
bool FpgaIs16BitMsbMode(uint16_t fpga_mode);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Set up the synchronous serial port with the set of options that fits
|
||||
// the FPGA mode. Both RX and TX are always enabled.
|
||||
// For AT91, it is SSC, and for AT32, it is SPI-TI_MODE
|
||||
// Note: at32 spi 16bit max, so please try to use 8-bit or 16 bit transmission,
|
||||
// otherwise platform compatibility cannot be handled.
|
||||
//-----------------------------------------------------------------------------
|
||||
void FpgaSetupSsc(uint16_t fpga_mode);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Modify the mode settings for rx&tx frames
|
||||
// bits: How many bits are received each time, 8 or 16
|
||||
// msb: Should we transfer MSB first?
|
||||
// Note: It can only be used to overwrite the settings of FpgaSetupSsc.
|
||||
// Warn: RX&TX must use data of the same width! Avoid platform compatibility issues.
|
||||
//-----------------------------------------------------------------------------
|
||||
void FpgaUpdateFrameMode(uint8_t bits, bool rx_msb, bool tx_msb);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Set up DMA to receive samples from the FPGA. We will use the PDC, with
|
||||
// a single buffer as a circular buffer (so that we just chain back to
|
||||
// ourselves, not to another buffer).
|
||||
//-----------------------------------------------------------------------------
|
||||
bool FpgaSetupSscRxDmaRepeat(void *buf, uint16_t len);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Set up DMA to receive samples from the FPGA. We will use the PDC, with
|
||||
// a single buffer not circular buffer (So it will only trigger one collection to this buffer
|
||||
// to avoid data being overwritten.).
|
||||
//-----------------------------------------------------------------------------
|
||||
bool FpgaSetupSscRxDmaSingle(void *buf, uint16_t len);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Send a 16 bit command/data pair to the FPGA.
|
||||
// The bit format is: C3 C2 C1 C0 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
|
||||
// where C is the 4 bit command and D is the 12 bit data
|
||||
//
|
||||
// @params cmd and v gets OR:ED over each other. Take careful note of overlapping bits.
|
||||
//-----------------------------------------------------------------------------
|
||||
void FpgaSendCommand(uint16_t cmd, uint16_t v);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Write the FPGA setup word (that determines what mode the logic is in, read
|
||||
// vs. clone vs. etc.). This is now a special case of FpgaSendCommand() to
|
||||
// avoid changing this function's occurrence everywhere in the source code.
|
||||
//-----------------------------------------------------------------------------
|
||||
void FpgaWriteConfWord(uint16_t v);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// enable FPGA internal tracing
|
||||
//-----------------------------------------------------------------------------
|
||||
void FpgaEnableTracing(void);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// disable FPGA internal tracing
|
||||
//-----------------------------------------------------------------------------
|
||||
void FpgaDisableTracing(void);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Print the current FPGA information.
|
||||
//-----------------------------------------------------------------------------
|
||||
void Fpga_print_status(void);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Set up the CMOS switches that mux the ADC: four switches, independently
|
||||
// closable, but should only close one at a time. Not an FPGA thing, but
|
||||
// the samples from the ADC always flow through the FPGA.
|
||||
//-----------------------------------------------------------------------------
|
||||
void SetAdcMuxFor(adc_mux_io_t muxTo);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// general turn off the antenna method
|
||||
//-----------------------------------------------------------------------------
|
||||
void switch_off(void);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Start FPGA bitstream configuration. Once started, the configuration will
|
||||
// restart from the beginning of the bitstream (any previous configuration
|
||||
// progress will be discarded).
|
||||
// configSram: If true, configure into SRAM; if false, configure into Flash.
|
||||
// Note: FPGAs on certain platforms may not support Flash
|
||||
// configuration. If Flash configuration is not supported,
|
||||
// PM3_EDEVNOTSUPP will be returned.
|
||||
// fileLength: The length of the bitstream file in bytes. Some platforms may
|
||||
// require this parameter to determine the configuration result or
|
||||
// perform pre-configuration preparations. If the length exceeds
|
||||
// the maximum size supported by the FPGA, PM3_EOVFLOW will be
|
||||
// returned.
|
||||
// return: PM3_XXX error code. Returns PM3_SUCCESS on successful configuration,
|
||||
// or an error code on failure. If the error code is PM3_EFAILED, refer
|
||||
// to FpgaConfigPlatformStatus() for more detailed error information.
|
||||
//-----------------------------------------------------------------------------
|
||||
int FpgaStartConfig(bool configSram, uint32_t fileLength);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Write bitstream data to the FPGA.
|
||||
// data: Pointer to the bitstream data. Memory alignment is not required for
|
||||
// the passed pointer, as each platform implementation handles buffered
|
||||
// writing based on its minimum write unit.
|
||||
// data_length: Length of the bitstream data in bytes. If the length exceeds
|
||||
// the maximum size supported by the FPGA, PM3_EOVFLOW will be
|
||||
// returned.
|
||||
// return: PM3_XXX error code. Returns PM3_SUCCESS on successful configuration,
|
||||
// or an error code on failure. If the error code is PM3_EFAILED, refer
|
||||
// to FpgaConfigPlatformStatus() for more detailed error information.
|
||||
//-----------------------------------------------------------------------------
|
||||
int FpgaConfigWrite(uint8_t *data, uint32_t data_length);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Stop FPGA bitstream configuration and release resources allocated during
|
||||
// the configuration process.
|
||||
// return: PM3_XXX error code. Returns PM3_SUCCESS if the configuration is
|
||||
// successfully stopped, or an error code if stopping fails. If the
|
||||
// error code is PM3_EFAILED, refer to FpgaConfigPlatformStatus() for
|
||||
// more detailed error information.
|
||||
//-----------------------------------------------------------------------------
|
||||
int FpgaStopConfig(void);
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Get the FPGA configuration status. The return value is a platform-specific
|
||||
// status code. Please refer to the platform-related documentation or source
|
||||
// code for specific status information.
|
||||
// This function is primarily used to obtain more detailed error information
|
||||
// when configuration fails, facilitating debugging and issue troubleshooting.
|
||||
//-----------------------------------------------------------------------------
|
||||
uint32_t FpgaConfigPlatformStatus(void);
|
||||
|
||||
#ifdef PM5
|
||||
#include "fpga_hw_at32.h"
|
||||
#else
|
||||
#include "fpga_hw_at91.h"
|
||||
#endif
|
||||
|
||||
#endif // FPGA_APIS_H_
|
||||
@@ -0,0 +1,130 @@
|
||||
#include "fpga_apis.h"
|
||||
#include "gpio_apis.h"
|
||||
#include "fpga_loader.h"
|
||||
#include "fpga.h"
|
||||
#include "dbprint.h"
|
||||
#include "util.h"
|
||||
#include "BigBuf.h"
|
||||
#include "appmain.h"
|
||||
|
||||
bool FpgaIs16BitMsbMode(uint16_t fpga_mode) {
|
||||
if (((fpga_mode & FPGA_MAJOR_MODE_MASK) == FPGA_MAJOR_MODE_HF_READER) &&
|
||||
(FpgaGetCurrent() == FPGA_BITSTREAM_HF || FpgaGetCurrent() == FPGA_BITSTREAM_HF_15)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void FpgaWriteConfWord(uint16_t v) {
|
||||
const int current = FpgaGetCurrent();
|
||||
|
||||
// Keep track of whether or not we should be monitoring the HF field timeout
|
||||
if (current == FPGA_BITSTREAM_HF || current == FPGA_BITSTREAM_HF_15 || current == FPGA_BITSTREAM_HF_FELICA) {
|
||||
const uint16_t major = v & FPGA_MAJOR_MODE_MASK;
|
||||
const uint16_t minor = v & FPGA_MINOR_MODE_MASK;
|
||||
|
||||
switch (major) {
|
||||
case FPGA_MAJOR_MODE_HF_READER:
|
||||
g_hf_field_timeout_active = true;
|
||||
break;
|
||||
case FPGA_MAJOR_MODE_HF_ISO14443A:
|
||||
g_hf_field_timeout_active = (minor == FPGA_HF_ISO14443A_READER_LISTEN || minor == FPGA_HF_ISO14443A_READER_MOD);
|
||||
break;
|
||||
case FPGA_MAJOR_MODE_HF_ISO18092:
|
||||
g_hf_field_timeout_active = (minor & FPGA_HF_ISO18092_FLAG_READER) != 0;
|
||||
break;
|
||||
default:
|
||||
g_hf_field_timeout_active = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
g_hf_field_timeout_active = false;
|
||||
}
|
||||
|
||||
FpgaSendCommand(FPGA_CMD_SET_CONFREG, v);
|
||||
}
|
||||
|
||||
void FpgaEnableTracing(void) {
|
||||
FpgaSendCommand(FPGA_CMD_TRACE_ENABLE, 1);
|
||||
}
|
||||
|
||||
void FpgaDisableTracing(void) {
|
||||
FpgaSendCommand(FPGA_CMD_TRACE_ENABLE, 0);
|
||||
}
|
||||
|
||||
void SetAdcMuxFor(adc_mux_io_t muxTo) {
|
||||
|
||||
#ifdef PM5 // fpga_switch pin resue to switch adc mux.
|
||||
if ((muxTo == ADC_MUXSEL_LORAW) || (muxTo == ADC_MUXSEL_HIRAW))
|
||||
return;
|
||||
|
||||
gpio_adc_mux_setup();
|
||||
|
||||
if (muxTo == ADC_MUXSEL_HIPKD) {
|
||||
Gpio_FPGA_SWITCH_High();
|
||||
} else {
|
||||
Gpio_FPGA_SWITCH_Low();
|
||||
}
|
||||
return;
|
||||
#endif
|
||||
|
||||
#ifndef WITH_FPC_USART
|
||||
|
||||
gpio_adc_mux_setup();
|
||||
|
||||
Gpio_MUXSEL_HIPKD_Low();
|
||||
Gpio_MUXSEL_LOPKD_Low();
|
||||
Gpio_MUXSEL_HIRAW_Low();
|
||||
Gpio_MUXSEL_LORAW_Low();
|
||||
|
||||
switch (muxTo) {
|
||||
case ADC_MUXSEL_HIPKD:
|
||||
Gpio_MUXSEL_HIPKD_High();
|
||||
break;
|
||||
case ADC_MUXSEL_LOPKD:
|
||||
Gpio_MUXSEL_LOPKD_High();
|
||||
break;
|
||||
case ADC_MUXSEL_HIRAW:
|
||||
Gpio_MUXSEL_HIRAW_High();
|
||||
break;
|
||||
case ADC_MUXSEL_LORAW:
|
||||
Gpio_MUXSEL_LORAW_High();
|
||||
break;
|
||||
}
|
||||
|
||||
#else
|
||||
if ((muxTo == ADC_MUXSEL_LORAW) || (muxTo == ADC_MUXSEL_HIRAW))
|
||||
return;
|
||||
|
||||
gpio_adc_mux_setup();
|
||||
|
||||
Gpio_MUXSEL_HIPKD_Low();
|
||||
Gpio_MUXSEL_LOPKD_Low();
|
||||
|
||||
if (muxTo == ADC_MUXSEL_HIPKD) {
|
||||
Gpio_MUXSEL_HIPKD_High();
|
||||
}
|
||||
if (muxTo == ADC_MUXSEL_LOPKD) {
|
||||
Gpio_MUXSEL_LOPKD_High();
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
// Turns off the antenna,
|
||||
// log message
|
||||
// if HF, Disable SSC DMA
|
||||
// turn off trace and leds off.
|
||||
void switch_off(void) {
|
||||
if (g_dbglevel > DBG_DEBUG) {
|
||||
Dbprintf("switch_off");
|
||||
}
|
||||
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||
if (FpgaGetCurrent() == FPGA_BITSTREAM_HF || FpgaGetCurrent() == FPGA_BITSTREAM_HF_15) {
|
||||
FPGA_SSC_DMA_RX_Disable();
|
||||
}
|
||||
|
||||
set_tracing(false);
|
||||
LEDsoff();
|
||||
}
|
||||
@@ -0,0 +1,790 @@
|
||||
/*
|
||||
* GOWIN fpga JTAG software implement
|
||||
*
|
||||
* @Author DXL
|
||||
* GPL license
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "fpga_gw_jtag.h"
|
||||
|
||||
#define INST_BYPASS 0xFF
|
||||
#define INST_IDCODE 0x11
|
||||
#define INST_STATUS 0x41
|
||||
#define INST_USERCODE 0x13
|
||||
#define INST_CONFIG_ENABLE 0x15
|
||||
#define INST_CONFIG_DISABLE 0x3A
|
||||
#define INST_NOOP 0x02
|
||||
#define INST_SRAM_ERASE 0x05
|
||||
#define INST_SRAM_ERASE_DONE 0x09
|
||||
#define INST_EFLASH_ERASE 0x75
|
||||
#define INST_EF_PROGRAM 0x71
|
||||
#define INST_EF_READ 0x73
|
||||
#define INST_REPROGRAM 0x3C
|
||||
#define INST_TRANSFER_CFG 0x17
|
||||
#define INST_ADDR_INIT 0x12
|
||||
#define INST_SRAM_READ 0x03
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint16_t sram_erase_ms; // 在发送 EraseSram(0x05)指令、Noop(0x02)之后,要给足够的时间等待其擦除完毕
|
||||
uint16_t y_page_w_wait_us; // 写一个y-page完成后需要延迟的时间长度
|
||||
uint16_t x_page_w_wait_us; // 写一个x-page完成后需要延迟的时间长度
|
||||
} gowin_timing_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t idcode;
|
||||
gowin_device_t device;
|
||||
const char *name;
|
||||
gowin_flash_type_t flash_type;
|
||||
bool reprogram; // 在部分器件中,如果 JTAG 的 4 个管脚或 JTAGSEL_N 复用为 GPIO,此时若需重新配置,需要先发送一次 reprogram 指令。
|
||||
gowin_timing_t timing;
|
||||
} device_map_t;
|
||||
|
||||
static gowin_device_t detected_device = GW_DEVICE_UNKNOWN;
|
||||
static uint32_t cached_idcode = 0;
|
||||
static bool m_flash_bg_update = false;
|
||||
static uint8_t *m_flash_xpage_buf = NULL;
|
||||
static uint16_t m_flash_xpage_pos = 0;
|
||||
|
||||
static const device_map_t device_map[] = {
|
||||
{
|
||||
0x0900281B, GW_DEVICE_GW1N_1, "GW1N-1", GW_FLASH_TYPE_HL, true,
|
||||
{1, 0, 2400}
|
||||
},
|
||||
{
|
||||
0x0900381B, GW_DEVICE_GW1N_1S, "GW1N-1S", GW_FLASH_TYPE_HL, true,
|
||||
{1, 0, 2400}
|
||||
},
|
||||
{
|
||||
0x0100681B, GW_DEVICE_GW1NZ_1, "GW1NZ-1", GW_FLASH_TYPE_TSMC, true,
|
||||
{1, 0, 6}
|
||||
},
|
||||
{
|
||||
0x0120681B, GW_DEVICE_GW1N_R_Z_2_2B_2C, "GW1N(R/Z)-2/2B/2C/1P5/1P5B/1P5C", GW_FLASH_TYPE_TSMC, false,
|
||||
{2, 16, 6}
|
||||
},
|
||||
// 以上将 GW1N-2 和 GW1N-1P5 系列合并映射 {0x0120681B, GW_DEVICE_GW1N_1P5_1P5B_1P5C, "GW1N-1P5/1P5B/1P5C", {2, 120, 0, 32}},
|
||||
{
|
||||
0x0100381B, GW_DEVICE_GW1N_R_4, "GW1N(R)-4", GW_FLASH_TYPE_TSMC, true,
|
||||
{2, 16, 6}
|
||||
},
|
||||
{
|
||||
0x1100381B, GW_DEVICE_GW1N_R_4B, "GW1N(R)-4B/4D", GW_FLASH_TYPE_TSMC, true,
|
||||
{2, 16, 6}
|
||||
},
|
||||
// 以上将 GW1NR-4B 和 GW1NR-4D 系列合并映射 {0x1100381B, GW_DEVICE_GW1N_R_4D, "GW1N(R)-4D", {2, 120, 0, 32}},
|
||||
{
|
||||
0x0100881B, GW_DEVICE_GW1NS_4, "GW1NS-4", GW_FLASH_TYPE_TSMC, false,
|
||||
{2, 16, 6}
|
||||
},
|
||||
{
|
||||
0x0100981B, GW_DEVICE_GW1NS_ER_4C, "GW1NS(ER)-4C", GW_FLASH_TYPE_TSMC, false,
|
||||
{2, 16, 6}
|
||||
},
|
||||
{
|
||||
0x1100581B, GW_DEVICE_GW1N_R_9, "GW1N(R)-9", GW_FLASH_TYPE_TSMC, true,
|
||||
{4, 16, 6}
|
||||
},
|
||||
{
|
||||
0x1100481B, GW_DEVICE_GW1N_R_9C, "GW1N(R)-9C", GW_FLASH_TYPE_TSMC, true,
|
||||
{4, 16, 6}
|
||||
},
|
||||
|
||||
// 根据手册描述: GW2ANR-18/GW2AN-55 内部封了一颗 SPI-Flash,编程方式与 GW2A-18、GW2A-55 相同
|
||||
// 也就是说,GW2A 系列是 spi-flash,需要让JTAG接口转接到MSPI的情况下,用SPI指令去操作最终的片上SPI—FLASH或者外部FLASH
|
||||
// 大概流程就是JATG -> 0x16指令 -> MSPI -> 0x06(写使能) -> 0xC7(擦除)...
|
||||
// 由此总结就是,除了转接到MSPI之前需要用到JTAG,其他时候都是和 SPI-FLASH 有关的操作了,因此擦除不需要像内部FLASH一样必须提供一个指定速率的时钟
|
||||
// {0x0000081B, GW_DEVICE_GW2A_R_18_18C, "GW2A(R)-18/18C", {6, 120, 0, 32}},
|
||||
// {0x0000281B, GW_DEVICE_GW2A_55_55C, "GW2A-55/55C", {10, 120, 0, 32}},
|
||||
|
||||
// 暂时不考虑这两个旧的型号的适配,这俩芯片官方貌似已经停产了,官方已经把芯片标记为old然后手册也删除了相关的信息,弄样品测试也麻烦。
|
||||
// 注:这俩芯片的内部FLASH工艺是SMIC,官方有STM32的例程和代码有封装了此芯片的内部FLASH烧录
|
||||
// 此系列要求的 Y page 写入之后的延迟时长是 30-35us
|
||||
// #define ID_GW1NS_2 0x0300081B
|
||||
// #define ID_GW1NS_2C 0x0300181B
|
||||
};
|
||||
|
||||
// 计算当前设备映射表中的设备型号数量
|
||||
#define DEVICE_SIZE (sizeof(device_map) / sizeof(device_map[0]))
|
||||
|
||||
// 根据ID索引到具体的设备信息映射表上,如果没有发现对应的设备存在,则返回NULL
|
||||
static const device_map_t *get_device_map_by_idcode(void) {
|
||||
for (size_t i = 0; i < DEVICE_SIZE; i++) {
|
||||
if (device_map[i].idcode == cached_idcode) {
|
||||
return &device_map[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 适用于只需要考虑JTAG的时钟速率上限的情况,不可以用于擦除和编程
|
||||
static void jtag_pulse_tck(gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) {
|
||||
return;
|
||||
}
|
||||
if (jtag_ops->tck_2m) {
|
||||
// 如果实现了tck脉冲接口,则优先调用
|
||||
jtag_ops->tck_2m(0);
|
||||
return;
|
||||
}
|
||||
if (!jtag_ops->delay_us) {
|
||||
// delay_us 作为后备方案,如果未实现此后备接口,则通信无法正常执行。
|
||||
return;
|
||||
}
|
||||
// 理想情况下,是 500kHZ
|
||||
jtag_ops->set_tck(0);
|
||||
jtag_ops->delay_us(1);
|
||||
jtag_ops->set_tck(1);
|
||||
jtag_ops->delay_us(1);
|
||||
}
|
||||
|
||||
// 设置tap状态机并且产生一次驱动时钟,驱动时钟的速度取决于 jtag_pulse_tck() 函数
|
||||
static void jtag_tap_clock(bool tms, gowin_jtag_ops_t *jtag_ops) {
|
||||
jtag_ops->set_tms(tms);
|
||||
jtag_pulse_tck(jtag_ops);
|
||||
}
|
||||
|
||||
// 从 Run-Test/Idle 进入 Shift-IR(标准 IEEE 1149.1 路径)
|
||||
static void jtag_goto_shift_ir(gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) return;
|
||||
jtag_tap_clock(1, jtag_ops); // -> Select-DR-Scan
|
||||
jtag_tap_clock(1, jtag_ops); // -> Select-IR-Scan
|
||||
jtag_tap_clock(0, jtag_ops); // -> Capture-IR
|
||||
jtag_tap_clock(0, jtag_ops); // -> Shift-IR
|
||||
}
|
||||
|
||||
static void jtag_shift_ir_safe(uint8_t inst, gowin_jtag_ops_t *jtag_ops) {
|
||||
jtag_goto_shift_ir(jtag_ops);
|
||||
for (int i = 0; i < 8; i++) {
|
||||
jtag_ops->set_tdi((inst >> i) & 1);
|
||||
jtag_tap_clock(i == 7, jtag_ops); // -> Exit1-IR if is last bit
|
||||
}
|
||||
// Exit1-IR -> Update-IR -> Run-Test/Idle
|
||||
jtag_tap_clock(1, jtag_ops); // -> Update-IR
|
||||
jtag_tap_clock(0, jtag_ops); // -> Run-Test/Idle
|
||||
// Per Gowin spec: ≥3 TCK cycles in Run-Test/Idle after IR load
|
||||
for (int i = 0; i < 6; i++) {
|
||||
jtag_pulse_tck(jtag_ops);
|
||||
}
|
||||
}
|
||||
|
||||
// 从 Run-Test/Idle 进入 Shift-DR(标准 IEEE 1149.1 路径)
|
||||
static void jtag_goto_shift_dr(gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) return;
|
||||
jtag_tap_clock(1, jtag_ops); // -> Select-DR
|
||||
jtag_tap_clock(0, jtag_ops); // -> Capture-DR
|
||||
jtag_tap_clock(0, jtag_ops); // -> Shift-DR
|
||||
}
|
||||
|
||||
// 仅用于从LSB开始发送的数据
|
||||
static void jtag_shift_dr_safe(const uint8_t *tx, uint8_t *rx, uint32_t bits, gowin_jtag_ops_t *jtag_ops) {
|
||||
// From Run-Test/Idle -> Select-DR-Scan -> Capture-DR -> Shift-DR
|
||||
jtag_goto_shift_dr(jtag_ops);
|
||||
|
||||
uint8_t byte = 0;
|
||||
for (uint32_t i = 0; i < bits; i++) {
|
||||
int byte_idx = i / 8;
|
||||
int bit_idx = i % 8;
|
||||
bool tdi = tx ? ((tx[byte_idx] >> bit_idx) & 1) : false;
|
||||
jtag_ops->set_tdi(tdi);
|
||||
jtag_tap_clock(i == bits - 1, jtag_ops); // -> Exit1-DR if is last bit
|
||||
|
||||
if (rx) {
|
||||
bool tdo = jtag_ops->get_tdo();
|
||||
byte |= (tdo << bit_idx);
|
||||
if (bit_idx == 7 || i == bits - 1) {
|
||||
rx[byte_idx] = byte;
|
||||
byte = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Exit1-DR -> Update-DR -> Run-Test/Idle
|
||||
jtag_tap_clock(1, jtag_ops); // -> Update-DR
|
||||
jtag_tap_clock(0, jtag_ops); // -> Run-Test/Idle
|
||||
}
|
||||
|
||||
#if DEBUG_GW_JTAG
|
||||
static void print_gowin_status(gowin_status_reg_t *status, gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) return;
|
||||
if (!jtag_ops->dbg_printf) return;
|
||||
jtag_ops->dbg_printf("Gowin Status Register (raw = 0x%08X):", status->raw);
|
||||
/*
|
||||
jtag_ops->dbg_print(" crc_error : %u // CRC Error Flag", status->bits.crc_error);
|
||||
jtag_ops->dbg_print(" bad_command_error : %u // Bad Command Error Flag", status->bits.bad_command_error);
|
||||
jtag_ops->dbg_print(" id_verify_failed : %u // ID Verify Failed Error Flag", status->bits.id_verify_failed);
|
||||
jtag_ops->dbg_print(" timeout_error : %u // Timeout Error Flag", status->bits.timeout_error);
|
||||
jtag_ops->dbg_print(" reserved_4 : %u // Reserved (should be 0)", status->bits.reserved_4);
|
||||
jtag_ops->dbg_print(" memory_erase : %u // Memory Erase Flag", status->bits.memory_erase);
|
||||
jtag_ops->dbg_print(" preamble : %u // Preamble Flag", status->bits.preamble);
|
||||
|
||||
*/
|
||||
|
||||
jtag_ops->dbg_printf(" edit_mode : %u // Edit Mode Flag", status->bits.edit_mode);
|
||||
|
||||
/*
|
||||
|
||||
jtag_ops->dbg_print(" program_spi_directly : %u // Program SPI Directly Flag", status->bits.program_spi_directly);
|
||||
jtag_ops->dbg_print(" autoboot_state : %u // AutoBoot State", status->bits.autoboot_state);
|
||||
jtag_ops->dbg_print(" non_jtag_active : %u // Non-JTAG Active Flag", status->bits.non_jtag_active);
|
||||
jtag_ops->dbg_print(" bypass_state : %u // Bypass State Flag", status->bits.bypass_state);
|
||||
jtag_ops->dbg_print(" vld : %u // VLD (1=normal)", status->bits.vld);
|
||||
|
||||
*/
|
||||
|
||||
jtag_ops->dbg_printf(" done_final : %u // Done Final (1=success)", status->bits.done_final);
|
||||
jtag_ops->dbg_printf(" security_final : %u // Security Final (1=secured)", status->bits.security_final);
|
||||
jtag_ops->dbg_printf(" ready : %u // Ready (1=normal)", status->bits.ready);
|
||||
jtag_ops->dbg_printf(" por : %u // POR (1=normal)", status->bits.por);
|
||||
/*
|
||||
jtag_ops->dbg_print(" flash_lock : %u // Flash Lock (1=locked)", status->bits.flash_lock);
|
||||
jtag_ops->dbg_print(" reserved_18_31 : %u // Reserved bits [31:18] (should be 0)", status->bits.reserved_18_31);
|
||||
*/
|
||||
}
|
||||
#endif
|
||||
|
||||
static uint32_t gowin_jtag_read_idcode_u32(gowin_jtag_ops_t *jtag_ops) {
|
||||
uint8_t buf[4] = {0};
|
||||
jtag_shift_ir_safe(INST_IDCODE, jtag_ops);
|
||||
jtag_shift_dr_safe(NULL, buf, 32, jtag_ops);
|
||||
return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据传入的JTAG硬件实现初始化jtag接口
|
||||
*
|
||||
* @param ops 此库会保存此引用,因此不可以在函数栈内进行非static定义,否则此init函数退出后,ops将会变为野指针导致后续操作随机跑飞
|
||||
* @return 初始化成功时,返回 GOWIN_JTAG_OK
|
||||
*/
|
||||
gowin_jtag_status_t gowin_jtag_init(gowin_jtag_ops_t *ops) {
|
||||
if (!ops || !ops->tck_2m) {
|
||||
return GOWIN_JTAG_ERROR_NULL_POINTER;
|
||||
}
|
||||
// 如果实现了 JTAGSEL 引脚的设置函数,则需要在启动JTAG操作之前,拉低 JTAGSEL 引脚,确保取消FPGA的JTAG复用
|
||||
if (ops->set_jtagsel) {
|
||||
ops->set_jtagsel(false);
|
||||
}
|
||||
// 重置TAP状态机,确保和设备从 Run-Test/Idle 位置开始通信
|
||||
gowin_jtag_reset(ops);
|
||||
// 读取IDCODE,并且缓存到全局域
|
||||
cached_idcode = gowin_jtag_read_idcode_u32(ops);
|
||||
const device_map_t *dm = get_device_map_by_idcode();
|
||||
detected_device = dm ? dm->device : GW_DEVICE_UNKNOWN;
|
||||
return detected_device == GW_DEVICE_UNKNOWN ? GOWIN_JTAG_ERROR_INVALID_IDCODE : GOWIN_JTAG_OK;
|
||||
}
|
||||
|
||||
void gowin_jtag_deinit(gowin_jtag_ops_t *jtag_ops) {
|
||||
// 拉高JTAGSEL脚,恢复JTAG脚复用为GPIO
|
||||
if (jtag_ops->set_jtagsel) {
|
||||
jtag_ops->set_jtagsel(true);
|
||||
}
|
||||
}
|
||||
|
||||
gowin_device_t gowin_jtag_get_device_type(void) {
|
||||
return detected_device;
|
||||
}
|
||||
|
||||
const char *gowin_jtag_get_device_name(void) {
|
||||
const device_map_t *dm = get_device_map_by_idcode();
|
||||
return dm ? dm->name : "Unknown";
|
||||
}
|
||||
|
||||
gowin_flash_type_t gowin_get_flash_type(void) {
|
||||
const device_map_t *dm = get_device_map_by_idcode();
|
||||
return dm ? dm->flash_type : GW_FLASH_TYPE_UNKNOWN;
|
||||
}
|
||||
|
||||
uint32_t gowin_jtag_get_idcode(void) {
|
||||
return cached_idcode;
|
||||
}
|
||||
|
||||
void gowin_jtag_reset(gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) return;
|
||||
jtag_ops->set_tms(1);
|
||||
for (int i = 0; i < 6; i++) {
|
||||
jtag_pulse_tck(jtag_ops); // 替代原来的 set_tck toggle
|
||||
}
|
||||
// Enter Run-Test/Idle explicitly
|
||||
jtag_tap_clock(0, jtag_ops);
|
||||
}
|
||||
|
||||
static uint32_t gowin_jtag_read_status_u32(gowin_jtag_ops_t *jtag_ops) {
|
||||
jtag_shift_ir_safe(INST_STATUS, jtag_ops);
|
||||
uint8_t buf[4] = {0};
|
||||
jtag_shift_dr_safe(NULL, buf, 32, jtag_ops);
|
||||
return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
|
||||
}
|
||||
|
||||
uint32_t gowin_jtag_read_status(gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) return 0;
|
||||
return gowin_jtag_read_status_u32(jtag_ops);
|
||||
}
|
||||
|
||||
uint32_t gowin_jtag_read_usercode(gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) return 0;
|
||||
jtag_shift_ir_safe(INST_USERCODE, jtag_ops);
|
||||
uint8_t buf[4] = {0};
|
||||
jtag_shift_dr_safe(NULL, buf, 32, jtag_ops);
|
||||
return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
|
||||
}
|
||||
|
||||
void gowin_jtag_reprogram(gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) return;
|
||||
jtag_shift_ir_safe(INST_REPROGRAM, jtag_ops);
|
||||
jtag_shift_ir_safe(INST_NOOP, jtag_ops);
|
||||
jtag_ops->delay_ms(200);
|
||||
}
|
||||
|
||||
gowin_jtag_status_t gowin_jtag_read_status_reg(gowin_status_reg_t *reg_out, gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) return GOWIN_JTAG_ERROR_NULL_POINTER;
|
||||
if (reg_out) {
|
||||
reg_out->raw = gowin_jtag_read_status_u32(jtag_ops);
|
||||
}
|
||||
#if DEBUG_GW_JTAG
|
||||
print_gowin_status(reg_out, jtag_ops);
|
||||
#endif
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
|
||||
static gowin_jtag_status_t gowin_jtag_cfg_enable(bool enable, gowin_jtag_ops_t *jtag_ops) {
|
||||
gowin_jtag_status_t status;
|
||||
gowin_status_reg_t status_reg;
|
||||
|
||||
// send command
|
||||
if (enable) {
|
||||
jtag_shift_ir_safe(INST_CONFIG_ENABLE, jtag_ops);
|
||||
} else {
|
||||
jtag_shift_ir_safe(INST_CONFIG_DISABLE, jtag_ops);
|
||||
jtag_shift_ir_safe(INST_NOOP, jtag_ops);
|
||||
}
|
||||
|
||||
// check status and waiting for edit mode enter.
|
||||
uint32_t retry = 100000; // timeout
|
||||
while (retry--) {
|
||||
status = gowin_jtag_read_status_reg(&status_reg, jtag_ops);
|
||||
if (status != GOWIN_JTAG_OK) {
|
||||
return status;
|
||||
}
|
||||
if (enable && status_reg.bits.edit_mode) {
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
if (!enable && !status_reg.bits.edit_mode) {
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return GOWIN_JTAG_ERROR_ENABLE_CFG;
|
||||
}
|
||||
|
||||
gowin_jtag_status_t gowin_jtag_sram_config_start(uint32_t *tx_bits_pos, gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) return GOWIN_JTAG_ERROR_NULL_POINTER;
|
||||
if (detected_device == GW_DEVICE_UNKNOWN) return GOWIN_JTAG_ERROR_INVALID_IDCODE;
|
||||
// TAP 复位,非常重要,让FPGA的TAP的状态机回到 Run-Test-Idle 状态
|
||||
gowin_jtag_reset(jtag_ops);
|
||||
// 无论如何,总是在启动配置SRAM的时候,首先擦除SRAM
|
||||
gowin_jtag_status_t status = gowin_jtag_sram_erase(jtag_ops);
|
||||
if (status != GOWIN_JTAG_OK) {
|
||||
return status;
|
||||
}
|
||||
jtag_shift_ir_safe(INST_CONFIG_ENABLE, jtag_ops); // 发送 ConfigEnable 指令 0x15
|
||||
jtag_shift_ir_safe(INST_ADDR_INIT, jtag_ops); // 发送 Address Initialize 指令 0x12
|
||||
jtag_shift_ir_safe(INST_TRANSFER_CFG, jtag_ops); // 发送 Transfer Configuration Data 指令 0x17
|
||||
jtag_goto_shift_dr(jtag_ops); // 移动状态到 Shift-DR(数据寄存器)
|
||||
|
||||
// 将 Bitstream Data 从最高位开始(MSB),逐位发送,发送全部数据流文件内容,并回到 Run-Test-Idle状态
|
||||
// 注:在配置接口中进行此操作,对于配置接口来说,此操作可以分多步执行,一点点发送文件知道全部发送完毕
|
||||
*tx_bits_pos = 0; // 在此处进行传输的比特流位置的重置
|
||||
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
|
||||
gowin_jtag_status_t gowin_jtag_sram_config_write(uint8_t *data, uint32_t data_length, uint32_t *tx_bytes_pos,
|
||||
uint32_t tx_bytes_total, gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops || !data) return GOWIN_JTAG_ERROR_NULL_POINTER;
|
||||
if (detected_device == GW_DEVICE_UNKNOWN) return GOWIN_JTAG_ERROR_INVALID_IDCODE;
|
||||
|
||||
// 将 Bitstream Data 从最高位开始(MSB),逐位发送,发送全部数据流文件内容,并回到 Run-Test-Idle状态
|
||||
for (uint32_t i = 0; i < data_length; i++) {
|
||||
// Send byte
|
||||
for (uint8_t j = 0; j < 8; j++) {
|
||||
// Send bits
|
||||
jtag_ops->set_tdi(data[i] >> (7 - j) & 0x01); // MSB first
|
||||
if (j == 7) {
|
||||
// Increment tx_bytes_pos if one byte transfer finish.
|
||||
(*tx_bytes_pos)++;
|
||||
// -> Exit1-DR if is last bit and is last byte
|
||||
jtag_tap_clock(*tx_bytes_pos == tx_bytes_total, jtag_ops);
|
||||
} else {
|
||||
jtag_tap_clock(0, jtag_ops); // One clock, no Exit1-DR
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
|
||||
gowin_jtag_status_t gowin_jtag_sram_config_finish(gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) return GOWIN_JTAG_ERROR_NULL_POINTER;
|
||||
jtag_shift_ir_safe(INST_CONFIG_DISABLE, jtag_ops);
|
||||
jtag_shift_ir_safe(INST_NOOP, jtag_ops);
|
||||
|
||||
// SRAM 写完后等待 60ms, 以待 status code 刷新
|
||||
jtag_ops->delay_ms(60);
|
||||
|
||||
// 记得,一定要重置状态机,让fpga回到 Run-Test/Idle 的状态,不然新固件不启动
|
||||
gowin_jtag_reset(jtag_ops);
|
||||
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
|
||||
gowin_jtag_status_t gowin_jtag_sram_erase(gowin_jtag_ops_t *jtag_ops) {
|
||||
const device_map_t *dm = get_device_map_by_idcode();
|
||||
|
||||
if (!dm) return GOWIN_JTAG_ERROR_NULL_POINTER;
|
||||
if (!jtag_ops) return GOWIN_JTAG_ERROR_NULL_POINTER;
|
||||
|
||||
jtag_shift_ir_safe(INST_CONFIG_ENABLE, jtag_ops);
|
||||
jtag_shift_ir_safe(INST_SRAM_ERASE, jtag_ops);
|
||||
jtag_shift_ir_safe(INST_NOOP, jtag_ops);
|
||||
|
||||
jtag_ops->tck_2m(dm->timing.sram_erase_ms * 1000);
|
||||
// jtag_ops->delay_ms(dm->timing.sram_erase_ms);
|
||||
|
||||
jtag_shift_ir_safe(INST_SRAM_ERASE_DONE, jtag_ops);
|
||||
jtag_shift_ir_safe(INST_NOOP, jtag_ops);
|
||||
jtag_shift_ir_safe(INST_CONFIG_DISABLE, jtag_ops);
|
||||
jtag_shift_ir_safe(INST_NOOP, jtag_ops);
|
||||
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
|
||||
// readout status and check POR & VLD
|
||||
static gowin_jtag_status_t gowin_check_status_gw1n(gowin_status_reg_t *reg_out, gowin_jtag_ops_t *jtag_ops) {
|
||||
gowin_jtag_read_status_reg(reg_out, jtag_ops);
|
||||
if (!reg_out->bits.vld) {
|
||||
return GOWIN_JTAG_ERROR_VLD_STATUS;
|
||||
}
|
||||
if (!reg_out->bits.por) {
|
||||
return GOWIN_JTAG_ERROR_POR_STATUS;
|
||||
}
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
|
||||
// 读出并且检查是否擦除成功,此函数仅用于gw1n系列
|
||||
static gowin_jtag_status_t gowin_check_erase_gw1n(gowin_status_reg_t *reg_out, gowin_jtag_ops_t *jtag_ops) {
|
||||
gowin_jtag_read_status_reg(reg_out, jtag_ops);
|
||||
// 不检查 Security Final 位
|
||||
if (reg_out->bits.vld && reg_out->bits.por && reg_out->bits.ready && reg_out->bits.done_final) {
|
||||
return GOWIN_JTAG_ERROR_ERASE_FAIL;
|
||||
}
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
|
||||
gowin_jtag_status_t gowin_jtag_flash_erase(gowin_jtag_ops_t *jtag_ops) {
|
||||
gowin_status_reg_t status_reg;
|
||||
|
||||
if (!jtag_ops) return GOWIN_JTAG_ERROR_NULL_POINTER;
|
||||
if (detected_device == GW_DEVICE_UNKNOWN) return GOWIN_JTAG_ERROR_INVALID_IDCODE;
|
||||
|
||||
const device_map_t *dm = get_device_map_by_idcode();
|
||||
if (dm == NULL) {
|
||||
return GOWIN_JTAG_ERROR_INVALID_IDCODE;
|
||||
}
|
||||
|
||||
// 读一下状态值,确认当前没问题
|
||||
gowin_jtag_status_t api_status = gowin_check_status_gw1n(&status_reg, jtag_ops);
|
||||
if (api_status != GOWIN_JTAG_OK) {
|
||||
return api_status;
|
||||
}
|
||||
|
||||
// if (jtag_ops->dbg_print) jtag_ops->dbg_print("m_flash_bg_update = %d", m_flash_bg_update);
|
||||
|
||||
// 如果不是背景烧录的话,就得关注 done_final 位,如果 done_final位是高的,就得清除SRAM,否则不需要清除
|
||||
// 因为在背景烧录的情况下,我们仍需要保留SRAM中的FPGA固件,使其正常运行,更新操作只会操作FLASH,不会导致SRAM被覆盖,因此不会中断服务
|
||||
if (m_flash_bg_update == false && status_reg.bits.done_final) {
|
||||
// Do sram erase
|
||||
api_status = gowin_jtag_sram_erase(jtag_ops);
|
||||
if (api_status != GOWIN_JTAG_OK) {
|
||||
return api_status;
|
||||
}
|
||||
// Verify for erase sram result
|
||||
api_status = gowin_check_erase_gw1n(&status_reg, jtag_ops);
|
||||
if (api_status != GOWIN_JTAG_OK) {
|
||||
return api_status;
|
||||
}
|
||||
if (jtag_ops->dbg_printf) jtag_ops->dbg_printf("erase the SRAM is finish, next step erase the FLASH");
|
||||
}
|
||||
|
||||
// 擦除过程,FLASH工艺不同,所进行的操作也不同
|
||||
gowin_jtag_cfg_enable(true, jtag_ops);
|
||||
|
||||
jtag_shift_ir_safe(INST_EFLASH_ERASE, jtag_ops); // 发送内嵌FLASH的擦除指令 0x75
|
||||
if (dm->flash_type == GW_FLASH_TYPE_HL) {
|
||||
for (int i = 0; i < 65; i++) {
|
||||
// H工艺要求重复此步骤65次,这是手册要求的
|
||||
// 移动状态到 Shift-DR(数据寄存器),并且产生32个时钟(TDI保持低电平)
|
||||
jtag_shift_dr_safe(NULL, NULL, 32, jtag_ops);
|
||||
}
|
||||
jtag_ops->tck_2m(95 * 1000); // H 工艺要求后续在 Run-Test-Idle 状态下持续产生时钟95ms
|
||||
// if (jtag_ops->dbg_print) jtag_ops->dbg_print("erase for GW_FLASH_TYPE_HL");
|
||||
}
|
||||
if (dm->flash_type == GW_FLASH_TYPE_TSMC) {
|
||||
jtag_shift_dr_safe(NULL, NULL, 32, jtag_ops); // T 工艺只要求产生一次32bit的输出传输时钟
|
||||
jtag_ops->tck_2m(150 * 1000); // T 工艺要求后续在 Run-Test-Idle 状态下持续产生时钟 120-150 ms
|
||||
// if (jtag_ops->dbg_print) jtag_ops->dbg_print("erase for GW_FLASH_TYPE_TSMC");
|
||||
}
|
||||
|
||||
gowin_jtag_cfg_enable(false, jtag_ops);
|
||||
|
||||
// 官方的代码里,H工艺在发送了 0x02 之后延迟了 500ms才继续干活,T工艺则是200ms
|
||||
if (dm->flash_type == GW_FLASH_TYPE_HL) {
|
||||
jtag_ops->delay_ms(500);
|
||||
if (m_flash_bg_update == false) {
|
||||
// 如果背景烧录使能,则不需要检查任何状态码相关的异常,因为这个时候固件是在正常运行的
|
||||
api_status = gowin_check_erase_gw1n(&status_reg, jtag_ops);
|
||||
if (api_status != GOWIN_JTAG_OK) {
|
||||
// 擦除失败了,直接报错
|
||||
return api_status;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (dm->flash_type == GW_FLASH_TYPE_TSMC) {
|
||||
jtag_ops->delay_ms(200);
|
||||
if (m_flash_bg_update == false) {
|
||||
// 如果背景烧录使能,则不可以触发重新配置,否则会导致被清空的FLASH的数据加载到SRAM覆盖正在运行的固件
|
||||
gowin_jtag_reprogram(jtag_ops);
|
||||
// 读取固件重新配置的结果,理论上应当是要停止运行的,非done和ready状态
|
||||
api_status = gowin_check_erase_gw1n(&status_reg, jtag_ops);
|
||||
if (api_status != GOWIN_JTAG_OK) {
|
||||
// 擦除失败了,直接报错
|
||||
return api_status;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
|
||||
gowin_jtag_status_t gowin_jtag_flash_config_start(uint8_t *xbuf_256, uint32_t *tx_bits_pos,
|
||||
bool bg_update, gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) return GOWIN_JTAG_ERROR_NULL_POINTER;
|
||||
if (detected_device == GW_DEVICE_UNKNOWN) return GOWIN_JTAG_ERROR_INVALID_IDCODE;
|
||||
|
||||
*tx_bits_pos = 0; // 在此处进行传输的比特流位置的重置
|
||||
m_flash_bg_update = bg_update; // 缓存背景升级的操作标志
|
||||
m_flash_xpage_buf = xbuf_256; // 由外部提供一个256byte的缓冲区,所有传过来的固件数据都依靠此buf进行整xpage的缓存
|
||||
m_flash_xpage_pos = 0; // 重置xpage的缓存位置,也就是将当前xpage的buf的有效字节数量归零
|
||||
|
||||
// TAP 复位,非常重要,让FPGA的TAP的状态机回到 Run-Test-Idle 状态
|
||||
gowin_jtag_reset(jtag_ops);
|
||||
// 无论如何,总是在启动配置FLASH的时候,首先擦除FLASH
|
||||
gowin_jtag_status_t status = gowin_jtag_flash_erase(jtag_ops);
|
||||
if (status != GOWIN_JTAG_OK) {
|
||||
return status;
|
||||
}
|
||||
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
|
||||
static void gowin_jtag_flash_config_xpage(const uint8_t data[256], uint32_t page_index, gowin_jtag_ops_t *jtag_ops) {
|
||||
const device_map_t *dm = get_device_map_by_idcode();
|
||||
|
||||
jtag_shift_ir_safe(INST_CONFIG_ENABLE, jtag_ops); // 发送配置使能指令 0x15
|
||||
jtag_shift_ir_safe(INST_EF_PROGRAM, jtag_ops); // 发送写内部FLASH指令 0x71
|
||||
|
||||
// 根据手册描述,在编程的页面地址大于0时,需要等待16us
|
||||
if (page_index > 0) {
|
||||
jtag_ops->tck_2m(16);
|
||||
}
|
||||
|
||||
// 地址数据格式共 32bits,其中低 6 位保留,例如地址为 b’00010011(0x13)时,写入的地
|
||||
// 址为 b’ 00000000000000000000010011000000,该地址数据遵循 LSB 方式写入,最后一个 bit 跳出 Shift-DR。
|
||||
uint32_t addr = (page_index << 6) & 0xFFFFFFC0;
|
||||
uint8_t addr_bytes[4] = {addr >> 0, addr >> 8, addr >> 16, addr >> 24};
|
||||
jtag_shift_dr_safe(addr_bytes, NULL, 32, jtag_ops);
|
||||
// 在地址传输完毕之后,也需要保持TCK时钟并且等待一段时间
|
||||
jtag_ops->tck_2m(16);
|
||||
|
||||
// 开始编程Y-PAGE,固定64个,总数据字节长度为 256 也就是一个 X-PAGE 的大小
|
||||
for (int y = 0; y < 64; y++) {
|
||||
const uint8_t *ypage = &data[y * 4];
|
||||
// 数据从 Configuration Data 取高位 4Bytes,在 Shift-DR 写数据时要从最低位开始写入(LSB)。
|
||||
uint8_t tx[4] = {ypage[3], ypage[2], ypage[1], ypage[0]};
|
||||
jtag_shift_dr_safe(tx, NULL, 32, jtag_ops);
|
||||
// 每次写完一个 Y-page, GW1N(Z)-2/4/6/9 系列要求 Run-Test 13-15μs,GW1N-2(C)系列要求 Run-Test 30-35μs,其他系列器件不需要
|
||||
jtag_ops->tck_2m(dm->timing.y_page_w_wait_us);
|
||||
}
|
||||
|
||||
// 整个 X-PAGE 编程完成了,按照手册描述:
|
||||
// GW1N-1(S)器件需要执行 2400μs 时长的时钟,GW1N(Z)-2/4/6/9 系列器件需要执行 6μs 时长的时钟,其他系列器件不需要额外时钟。
|
||||
jtag_ops->tck_2m(dm->timing.x_page_w_wait_us);
|
||||
}
|
||||
|
||||
// 给数据源的头部替换为指定的保留数据,根据官方FAE的描述,可以放心替换,头部有预留字节是给某些配置用的
|
||||
// type 为 1 时,替换为 Autoboot-pattern
|
||||
// type 为 0 时,替换为 Readable-pattern
|
||||
static void gowin_pattern_replace(uint8_t *data, const uint8_t type) {
|
||||
// H 工艺器件:Readable-pattern 0x07,0x07,0x30,0x40
|
||||
// T 工艺器件:Readable-pattern 0xF7,0xF7,0x3F,0x4F
|
||||
// 目前两个工艺的器件的 Autoboot-pattern 都是一样的 0x47,0x57,0x31,0x4E
|
||||
if (type == 1) {
|
||||
data[0] = 0x47;
|
||||
data[1] = 0x57;
|
||||
data[2] = 0x31;
|
||||
data[3] = 0x4E;
|
||||
return;
|
||||
}
|
||||
if (type == 0) {
|
||||
const device_map_t *dm = get_device_map_by_idcode();
|
||||
if (dm == NULL) return;
|
||||
if (dm->flash_type == GW_FLASH_TYPE_TSMC) {
|
||||
data[0] = 0xF7;
|
||||
data[1] = 0xF7;
|
||||
data[2] = 0x3F;
|
||||
data[3] = 0x4F;
|
||||
}
|
||||
if (dm->flash_type == GW_FLASH_TYPE_HL) {
|
||||
data[0] = 0x07;
|
||||
data[1] = 0x07;
|
||||
data[2] = 0x30;
|
||||
data[3] = 0x40;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gowin_jtag_status_t gowin_jtag_flash_config_write(uint8_t *data, uint32_t data_length, uint32_t *tx_bytes_pos,
|
||||
uint32_t tx_bytes_total, gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops || !data) return GOWIN_JTAG_ERROR_NULL_POINTER;
|
||||
if (detected_device == GW_DEVICE_UNKNOWN) return GOWIN_JTAG_ERROR_INVALID_IDCODE;
|
||||
if (m_flash_xpage_buf == NULL) return GOWIN_JTAG_ERROR_NULL_POINTER;
|
||||
|
||||
// 在xbuf里面已经有缓存的数据的情况下,我们需要先确认本次攒够了一个xpage的大小,才去开工写入xbuf里面的数据
|
||||
if (data_length < 256 || m_flash_xpage_pos > 0) {
|
||||
// 确保新到来的数据加上旧的数据的长度不会溢出,如果溢出的话,那我们就只取一部分写入到xbuf里,让xbuf先满一个page
|
||||
uint16_t copy_length = data_length;
|
||||
if (m_flash_xpage_pos + data_length > 256) {
|
||||
copy_length = 256 - m_flash_xpage_pos; // 计算不会溢出xbuf的可复制数据的长度
|
||||
}
|
||||
memcpy(m_flash_xpage_buf + m_flash_xpage_pos, data, copy_length);
|
||||
data += copy_length; // 此时我们复制了一部分数据到xbuf里头,外部传进来的剩下的数据的指针要往前移,传入长度也要减去这部分
|
||||
data_length -= copy_length;
|
||||
m_flash_xpage_pos += copy_length; // 复制之后,记录当前xpage的内容长度
|
||||
// 如果当前不是最后一包并且数据不够一个xbuf大小,那就得先把数据缓存下来,等足够一个xpage(256字节)了再去传
|
||||
if (m_flash_xpage_pos != 256) {
|
||||
if (*tx_bytes_pos + data_length + m_flash_xpage_pos < tx_bytes_total) {
|
||||
return GOWIN_JTAG_OK; // 此处直接返回,因为不够一个xbuf大小并且不是最后一包数据,仍需等待传输
|
||||
}
|
||||
// 已经是最后一包了,不够256的话那我们就默认用 0x00 补齐剩下的数据,当作足额给发过去
|
||||
memset(m_flash_xpage_buf + m_flash_xpage_pos, 0x00, 256 - m_flash_xpage_pos);
|
||||
// m_flash_xpage_pos = 256; 为了正确统计xbuf里面的自己数量,此处不要赋值为 256,否则padding的数据也会被计算进去 tx_bytes_pos 里
|
||||
}
|
||||
}
|
||||
|
||||
// 计算当前已传输的字节数量对应到的page位置
|
||||
uint32_t page_index = *tx_bytes_pos / 256;
|
||||
// 处理 Readable-pattern / Autoboot-pattern,我们暂时不加入对 Verify 的支持,自然也就不需要考虑 Readable-pattern
|
||||
if (page_index == 0) {
|
||||
gowin_pattern_replace(m_flash_xpage_pos == 0 ? data : m_flash_xpage_buf, 1);
|
||||
}
|
||||
|
||||
// 完事儿了开始写X-PAGE,我们有两个BUF,一个是256大小的xbuf暂存区,一个是外部传入的数据源,
|
||||
// 我们优先把xbuf暂存区给发出去(如果里面有数据的话)
|
||||
if (m_flash_xpage_pos > 0) {
|
||||
gowin_jtag_flash_config_xpage(m_flash_xpage_buf, page_index, jtag_ops);
|
||||
*tx_bytes_pos += m_flash_xpage_pos; // 一个x-page传完了就记到总传输的字节数量里,记住,我们此处要加实际有效的字节数量
|
||||
page_index++;
|
||||
m_flash_xpage_pos = 0; // 传完了记得归零xbuf的字节计数
|
||||
}
|
||||
// xbuf传完了以后,还得继续看看外部数据源里有没有完整的x-page的数据,如果有的话,就继续传
|
||||
for (uint32_t p = 0; p < data_length / 256; p++) {
|
||||
gowin_jtag_flash_config_xpage(&data[p * 256], page_index, jtag_ops);
|
||||
*tx_bytes_pos += 256; // 同上描述
|
||||
page_index++;
|
||||
}
|
||||
// 如果有剩余数据,那剩余的数据一定是没发出去的,需要等到有完整的一包x-page才能发,所以我们计算余数,将其拷贝到xbuf里面暂存等待下一包
|
||||
uint8_t remain_bytes = data_length % 256; // 用u8是安全的,因为不可能有256个字节剩余,直接整除了
|
||||
if (remain_bytes > 0) {
|
||||
memset(&m_flash_xpage_buf[remain_bytes], 0x00, 256 - remain_bytes); // 把后面的无效数据归零
|
||||
memcpy(m_flash_xpage_buf, &data[data_length - remain_bytes], remain_bytes); // 复制数据到缓冲区的开头
|
||||
m_flash_xpage_pos += remain_bytes; // 记录本次传输剩余的字节数
|
||||
// 如果是最后一包了的话,那就直接传过去,不要再缓存了
|
||||
if (*tx_bytes_pos + remain_bytes >= tx_bytes_total) {
|
||||
gowin_jtag_flash_config_xpage(m_flash_xpage_buf, page_index, jtag_ops);
|
||||
m_flash_xpage_pos = 0;
|
||||
*tx_bytes_pos += remain_bytes;
|
||||
}
|
||||
}
|
||||
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
|
||||
gowin_jtag_status_t gowin_jtag_flash_config_finish(gowin_jtag_ops_t *jtag_ops) {
|
||||
if (!jtag_ops) return GOWIN_JTAG_ERROR_NULL_POINTER;
|
||||
if (detected_device == GW_DEVICE_UNKNOWN) return GOWIN_JTAG_ERROR_INVALID_IDCODE;
|
||||
|
||||
jtag_shift_ir_safe(INST_CONFIG_DISABLE, jtag_ops); // 发送配置禁用指令 0x3A
|
||||
gowin_jtag_reprogram(jtag_ops); // 经测试,flash的烧录只要执行 reprogram 就可以让程序开始执行,不需要重置JTAG端口
|
||||
|
||||
return GOWIN_JTAG_OK;
|
||||
}
|
||||
|
||||
void gowin_jtag_start_config(gowin_config_ctx_t *cctx) {
|
||||
// 发起jtag初始化和读取ID
|
||||
cctx->status = gowin_jtag_init(cctx->jtag_ops);
|
||||
if (cctx->status == GOWIN_JTAG_OK) {
|
||||
uint32_t idcode = gowin_jtag_get_idcode();
|
||||
const char* name = gowin_jtag_get_device_name();
|
||||
if (cctx->jtag_ops->dbg_printf) cctx->jtag_ops->dbg_printf("gowin_jtag OK: idcode = 0x%04lX, name = %s", idcode, name);
|
||||
// 读取和打印详细的状态表
|
||||
gowin_status_reg_t status_reg;
|
||||
gowin_jtag_read_status_reg(&status_reg, cctx->jtag_ops);
|
||||
} else {
|
||||
if (cctx->jtag_ops->dbg_printf) cctx->jtag_ops->dbg_printf("gowin_jtag NOT OK");
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化启动配置
|
||||
if (cctx->is_cfg_sram) {
|
||||
if (cctx->jtag_ops->dbg_printf) cctx->jtag_ops->dbg_printf("Erase sram started");
|
||||
cctx->status = gowin_jtag_sram_config_start(&cctx->tx_pos, cctx->jtag_ops);
|
||||
if (cctx->status != GOWIN_JTAG_OK) {
|
||||
if (cctx->jtag_ops->dbg_printf) cctx->jtag_ops->dbg_printf("Failed to start sram config: %d", cctx->status);
|
||||
return;
|
||||
}
|
||||
if (cctx->jtag_ops->dbg_printf) cctx->jtag_ops->dbg_printf("Erase sram done");
|
||||
} else {
|
||||
if (cctx->jtag_ops->dbg_printf) cctx->jtag_ops->dbg_printf("Erase flash started");
|
||||
// 暂时只进行非背景升级(会终止FPGA的执行)
|
||||
cctx->status = gowin_jtag_flash_config_start(cctx->x_page_buf, &cctx->tx_pos, false, cctx->jtag_ops);
|
||||
if (cctx->status != GOWIN_JTAG_OK) {
|
||||
if (cctx->jtag_ops->dbg_printf) cctx->jtag_ops->dbg_printf("Failed to start flash config: %d", cctx->status);
|
||||
return;
|
||||
}
|
||||
if (cctx->jtag_ops->dbg_printf) cctx->jtag_ops->dbg_printf("Erase flash done");
|
||||
}
|
||||
|
||||
// 打印个消息告知一下启动完成了
|
||||
if (cctx->jtag_ops->dbg_printf) {
|
||||
cctx->jtag_ops->dbg_printf("gowin_jtag %s config started: %d", cctx->is_cfg_sram ? "sram" : "flash" , cctx->status);
|
||||
}
|
||||
}
|
||||
|
||||
void gowin_jtag_config_write(uint8_t *data, uint32_t data_length, gowin_config_ctx_t *cctx) {
|
||||
// 根据当前的配置类型,选择性调用对应的逻辑
|
||||
if (cctx->is_cfg_sram) {
|
||||
cctx->status = gowin_jtag_sram_config_write(data, data_length, &cctx->tx_pos, cctx->tx_total, cctx->jtag_ops);
|
||||
} else {
|
||||
cctx->status = gowin_jtag_flash_config_write(data, data_length, &cctx->tx_pos, cctx->tx_total, cctx->jtag_ops);
|
||||
}
|
||||
}
|
||||
|
||||
void gowin_jtag_stop_config(gowin_config_ctx_t *cctx) {
|
||||
// 根据当前烧录模式的不同选择不同的收尾
|
||||
if (cctx->is_cfg_sram) {
|
||||
cctx->status = gowin_jtag_sram_config_finish(cctx->jtag_ops);
|
||||
} else {
|
||||
cctx->status = gowin_jtag_flash_config_finish(cctx->jtag_ops);
|
||||
}
|
||||
gowin_jtag_deinit(cctx->jtag_ops); // 反初始化gowinjtag库,退出某些状态并且释放某些资源
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
/*
|
||||
* GOWIN fpga JTAG software implement
|
||||
*
|
||||
* @Author DXL
|
||||
* MIT license
|
||||
*/
|
||||
#ifndef GOWIN_JTAG_H_
|
||||
#define GOWIN_JTAG_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// 是否将调试打印信息编译进当前模块中
|
||||
#define DEBUG_GW_JTAG 1
|
||||
|
||||
typedef enum {
|
||||
GOWIN_JTAG_OK = 0U,
|
||||
GOWIN_JTAG_ERROR_INVALID_IDCODE,
|
||||
GOWIN_JTAG_ERROR_NULL_POINTER,
|
||||
GOWIN_JTAG_ERROR_OUT_OF_RANGE,
|
||||
GOWIN_JTAG_ERROR_POR_STATUS,
|
||||
GOWIN_JTAG_ERROR_VLD_STATUS,
|
||||
GOWIN_JTAG_ERROR_ERASE_FAIL,
|
||||
GOWIN_JTAG_ERROR_ENABLE_CFG,
|
||||
} gowin_jtag_status_t;
|
||||
|
||||
typedef enum {
|
||||
GW_DEVICE_UNKNOWN = 0,
|
||||
GW_DEVICE_GW1N_1,
|
||||
GW_DEVICE_GW1N_1S,
|
||||
GW_DEVICE_GW1NZ_1,
|
||||
GW_DEVICE_GW1N_R_Z_2_2B_2C,
|
||||
GW_DEVICE_GW1N_1P5_1P5B_1P5C,
|
||||
GW_DEVICE_GW1N_R_4,
|
||||
GW_DEVICE_GW1N_R_4B,
|
||||
GW_DEVICE_GW1N_R_4D,
|
||||
GW_DEVICE_GW1NS_4,
|
||||
GW_DEVICE_GW1NS_ER_4C,
|
||||
GW_DEVICE_GW1N_R_9,
|
||||
GW_DEVICE_GW1N_R_9C,
|
||||
GW_DEVICE_GW2A_R_18_18C,
|
||||
GW_DEVICE_GW2A_55_55C,
|
||||
} gowin_device_t;
|
||||
|
||||
typedef enum {
|
||||
GW_FLASH_TYPE_UNKNOWN = 0U, // 未知工艺?
|
||||
GW_FLASH_TYPE_TSMC, // T 工艺
|
||||
GW_FLASH_TYPE_HL, // H 工艺
|
||||
GW_FLASH_TYPE_SMIC, // SMIC 工艺有 ID_GW1NS_2 和 ID_GW1NS_2C,但是我们暂时不打算对接
|
||||
GW_FLASH_TYPE_SPI_FLASH, // 核封了一颗SPI-FLASH或者是只支持外部FLASH
|
||||
} gowin_flash_type_t;
|
||||
|
||||
typedef struct {
|
||||
/**
|
||||
* 设置TCK电平状态,true为高,false为低。
|
||||
*/
|
||||
void (*set_tck)(bool level);
|
||||
/**
|
||||
* 设置TMS电平状态,true为高,false为低。
|
||||
*/
|
||||
void (*set_tms)(bool level);
|
||||
/**
|
||||
* 设置TDI电平状态,true为高,false为低。
|
||||
*/
|
||||
void (*set_tdi)(bool level);
|
||||
/**
|
||||
* 获取TDO电平状态,true为高,false为低。
|
||||
*/
|
||||
bool (*get_tdo)(void);
|
||||
/**
|
||||
* 微秒延迟,不要求太高精度,在 tck_pulse 未实现时,此延时接口作为一个后备方案提供大概500kHZ的TCK时钟输出
|
||||
*/
|
||||
void (*delay_us)(int us);
|
||||
/**
|
||||
* 毫秒延迟,不要求太高精度
|
||||
*/
|
||||
void (*delay_ms)(int ms);
|
||||
/**
|
||||
* 产生 2mhz 的tck时钟,持续指定的us时长,如果传入参数为0,则只产生一个时钟周期的tck波形
|
||||
* 也就是拉低tck持续半周期 250ns,然后拉高tck持续半周期 250ns
|
||||
* 注意:实际精度不能低于 1.8mhz 和高于 2.2mhz,也就是正负200K的精度都在可接受范围内
|
||||
*/
|
||||
void (*tck_2m)(uint32_t us);
|
||||
/**
|
||||
* 可选的实现,如果不实现,则不会输出任何调试信息,并且你可选将当前模块的所有打印信息编译进模块中
|
||||
* 如果你是在资源紧张的平台,则可以通过 DEBUG_GW_JTAG 去除当前模块的所有调试信息
|
||||
*/
|
||||
void (*dbg_printf)(const char *fmt, ...);
|
||||
/**
|
||||
* 可选的实现,如果复用了JTAG脚为普通IO,则需要在烧录之前,拉低 JTAGSEL_N 引脚
|
||||
*/
|
||||
void (*set_jtagsel)(bool level);
|
||||
} gowin_jtag_ops_t;
|
||||
|
||||
/**
|
||||
* @brief Gowin FPGA Device Status Register (32-bit)
|
||||
*
|
||||
* Reference:
|
||||
* - 表7-12: GW1N(R)-(1/4B/4C/4D)/GW1NRF-4B 系列
|
||||
* - 表7-13: GW1N(R)-(1P5/2/6/9/9C)/GW1NS-4(4C)/GW1NSR-4(4C)/GW1NSE-4C/GW1NSER-4C/GW1NZ-(1/2) 系列
|
||||
*
|
||||
* Note:
|
||||
* - Bit 编号从 LSB (bit 0) 到 MSB (bit 31)
|
||||
* - 某些位在不同系列中含义一致,部分位仅在特定系列存在(见注释)
|
||||
*/
|
||||
typedef union {
|
||||
uint32_t raw;
|
||||
struct {
|
||||
/* Bit 0 */
|
||||
uint32_t crc_error : 1; ///< CRC Error Flag (1=发生错误, 0=正常). 所有系列通用.
|
||||
/* Bit 1 */
|
||||
uint32_t bad_command_error : 1; ///< Bad Command Error Flag (1=发生错误). 所有系列通用.
|
||||
/* Bit 2 */
|
||||
uint32_t id_verify_failed : 1; ///< ID Verify Failed Error Flag (1=ID校验失败). 所有系列通用.
|
||||
/* Bit 3 */
|
||||
uint32_t timeout_error : 1; ///< Timeout Error Flag (1=超时错误). 所有系列通用.
|
||||
/* Bit 4 */
|
||||
uint32_t reserved_4 : 1; ///< 保留位,固定为0.
|
||||
/* Bit 5 */
|
||||
uint32_t memory_erase : 1; ///< Memory Erase 标志. 所有系列通用.
|
||||
/* Bit 6 */
|
||||
uint32_t preamble : 1; ///< Preamble 标志. 所有系列通用.
|
||||
/* Bit 7 */
|
||||
uint32_t edit_mode : 1; ///< Edit Mode 标志. 所有系列通用.
|
||||
/* Bit 8 */
|
||||
uint32_t program_spi_directly : 1; ///< Program SPI Directly 标志. 所有系列通用.
|
||||
/* Bit 9 */
|
||||
uint32_t autoboot_state : 1; ///< AutoBoot State.
|
||||
///< - 表7-13: 存在此字段(用于支持AutoBoot的型号)
|
||||
///< - 表7-12: 此位为0(即不支持AutoBoot的型号如GW1N-1/4B等)
|
||||
/* Bit 10 */
|
||||
uint32_t non_jtag_active : 1; ///< Non-JTAG Active 标志(例如MSPI/SSPI配置中激活). 所有系列通用.
|
||||
/* Bit 11 */
|
||||
uint32_t bypass_state : 1; ///< Bypass State 标志. 所有系列通用.
|
||||
/* Bit 12 */
|
||||
uint32_t vld : 1; ///< Gowin VLD (1=正常, 0=异常). 内置Flash相关参数.
|
||||
///< 适用于所有带内置Flash的型号(如GW1NS、GW1NZ等).
|
||||
/* Bit 13 */
|
||||
uint32_t done_final : 1; ///< Done Final (1=配置成功完成, 0=失败). 所有系列通用.
|
||||
/* Bit 14 */
|
||||
uint32_t security_final : 1; ///< Security Final (1=已设置安全位, 0=未设置). 所有系列通用.
|
||||
/* Bit 15 */
|
||||
uint32_t ready : 1; ///< Ready (1=正常, 0=异常). 所有系列通用.
|
||||
/* Bit 16 */
|
||||
uint32_t por : 1; ///< POR (Power-On Reset) 状态 (1=正常, 0=异常). 所有系列通用.
|
||||
/* Bit 17 */
|
||||
uint32_t flash_lock : 1; ///< Flash Lock 标志:
|
||||
///< - 1 = Flash锁定(禁止回读,但允许擦除)
|
||||
///< - 仅存在于表7-13所列型号(即带内置Flash的系列,如GW1NS/GW1NZ/GW1NSE等)
|
||||
///< - 表7-12型号(如GW1N-1/4B)此位为0(无Flash Lock功能)
|
||||
/* Bits 18–31 */
|
||||
uint32_t reserved_18_31 : 14; ///< 保留位,固定为0(两表均明确说明).
|
||||
} bits;
|
||||
} gowin_status_reg_t;
|
||||
|
||||
/**
|
||||
* Information required during the configuration process
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t x_page_buf[256]; // Buffer for incomplete data of x-page
|
||||
uint32_t tx_pos; // Current position of data to be sent, in bytes
|
||||
uint32_t tx_total; // Total size of the data to be sent, in bytes
|
||||
bool is_cfg_sram; // true: config sram, false: config flash
|
||||
gowin_jtag_ops_t *jtag_ops; // Pointer to the JTAG operations structure, used for functions during configuration
|
||||
gowin_jtag_status_t status; // Status of the current configuration process, used to track errors of platform
|
||||
} gowin_config_ctx_t;
|
||||
|
||||
gowin_jtag_status_t gowin_jtag_init(gowin_jtag_ops_t *ops);
|
||||
void gowin_jtag_deinit(gowin_jtag_ops_t *jtag_ops);
|
||||
|
||||
gowin_device_t gowin_jtag_get_device_type(void);
|
||||
const char *gowin_jtag_get_device_name(void);
|
||||
gowin_flash_type_t gowin_get_flash_type(void);
|
||||
uint32_t gowin_jtag_get_idcode(void);
|
||||
|
||||
void gowin_jtag_reset(gowin_jtag_ops_t *jtag_ops);
|
||||
uint32_t gowin_jtag_read_status(gowin_jtag_ops_t *jtag_ops);
|
||||
uint32_t gowin_jtag_read_usercode(gowin_jtag_ops_t *jtag_ops);
|
||||
void gowin_jtag_reprogram(gowin_jtag_ops_t *jtag_ops);
|
||||
gowin_jtag_status_t gowin_jtag_read_status_reg(gowin_status_reg_t *reg_out, gowin_jtag_ops_t *jtag_ops);
|
||||
|
||||
gowin_jtag_status_t gowin_jtag_sram_erase(gowin_jtag_ops_t *jtag_ops);
|
||||
gowin_jtag_status_t gowin_jtag_sram_config_start(uint32_t *tx_bits_pos, gowin_jtag_ops_t *jtag_ops);
|
||||
gowin_jtag_status_t gowin_jtag_sram_config_write(
|
||||
uint8_t *data,
|
||||
uint32_t data_length,
|
||||
uint32_t *tx_bytes_pos,
|
||||
uint32_t tx_bytes_total,
|
||||
gowin_jtag_ops_t *jtag_ops);
|
||||
gowin_jtag_status_t gowin_jtag_sram_config_finish(gowin_jtag_ops_t *jtag_ops);
|
||||
|
||||
gowin_jtag_status_t gowin_jtag_flash_erase(gowin_jtag_ops_t *jtag_ops);
|
||||
gowin_jtag_status_t gowin_jtag_flash_config_start(
|
||||
uint8_t *xbuf_256,
|
||||
uint32_t *tx_bits_pos,
|
||||
bool bg_update,
|
||||
gowin_jtag_ops_t *jtag_ops);
|
||||
gowin_jtag_status_t gowin_jtag_flash_config_write(
|
||||
uint8_t *data,
|
||||
uint32_t data_length,
|
||||
uint32_t *tx_bytes_pos,
|
||||
uint32_t tx_bytes_total,
|
||||
gowin_jtag_ops_t *jtag_ops);
|
||||
gowin_jtag_status_t gowin_jtag_flash_config_finish(gowin_jtag_ops_t *jtag_ops);
|
||||
|
||||
void gowin_jtag_start_config(gowin_config_ctx_t *cctx);
|
||||
void gowin_jtag_config_write(uint8_t *data, uint32_t data_length, gowin_config_ctx_t *cctx);
|
||||
void gowin_jtag_stop_config(gowin_config_ctx_t *cctx);
|
||||
|
||||
#endif // GOWIN_JTAG_H_
|
||||
@@ -0,0 +1,439 @@
|
||||
#include "gpio_hw_at32.h"
|
||||
#include "fpga_apis.h"
|
||||
#include "fpga_gw_jtag.h"
|
||||
#include "ticks_apis.h"
|
||||
#include "gpio_apis.h"
|
||||
#include "dbprint.h"
|
||||
#include "pm3_cmd.h"
|
||||
#include "string.h"
|
||||
|
||||
uint16_t g_ssc_dma_rx_count;
|
||||
uint8_t g_ssc_data_byte_width;
|
||||
bool g_tx_lsb_first;
|
||||
|
||||
void FpgaSetup24MHzClk(void) {
|
||||
gpio_init_type gpio_init_struct;
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
// gpio clk enable
|
||||
crm_periph_clock_enable(AT32_GPIO_PERIPH_FPGA_24M_CLK, TRUE); // ARM2FPGA_PCK0 = PA8_CRM_CLKO1
|
||||
// clkout gpio init
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
|
||||
gpio_init_struct.gpio_pins = AT32_GPIO_FPGA_24M_CLK_PIN;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(AT32_GPIO_FPGA_24M_CLK, &gpio_init_struct);
|
||||
// config clkout division, 288/3/4=24mhz
|
||||
crm_clkout_div_set(CRM_CLKOUT_INDEX_1, CRM_CLKOUT_DIV1_3, CRM_CLKOUT_DIV2_4);
|
||||
crm_clock_out1_set(CRM_CLKOUT1_PLL); // config clkout1 clock
|
||||
|
||||
/* 48m pll -> 24m clkout
|
||||
gpio_init_type gpio_init_struct;
|
||||
// enable periph clock
|
||||
crm_periph_clock_enable(AT32_GPIO_PERIPH_FPGA_24M_CLK, TRUE); // ARM2FPGA_PCK0 = PA8_CRM_CLKO1
|
||||
// set default parameter
|
||||
gpio_default_para_init(&gpio_init_struct);
|
||||
// config gpio mux function
|
||||
gpio_pin_mux_config(AT32_GPIO_FPGA_24M_CLK, GPIO_PINS_SOURCE8, GPIO_MUX_0);
|
||||
// config gpio
|
||||
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
|
||||
gpio_init_struct.gpio_pins = AT32_GPIO_FPGA_24M_CLK_PIN;
|
||||
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_init(AT32_GPIO_FPGA_24M_CLK, &gpio_init_struct);
|
||||
// config clkout1 output clock source
|
||||
crm_clock_out1_set(CRM_CLKOUT1_PLL);
|
||||
// config clkout1 div
|
||||
crm_clkout_div_set(CRM_CLKOUT_INDEX_1, CRM_CLKOUT_DIV1_2, CRM_CLKOUT_DIV2_1);
|
||||
*/
|
||||
}
|
||||
|
||||
// gpio for spi-timode init
|
||||
static void spi_ssc_gpio_setup(void) {
|
||||
gpio_init_type gpio_initstructure;
|
||||
|
||||
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
|
||||
|
||||
// PB9_SPI4_MOSI = fpga -> arm
|
||||
// PB8_SPI4_MISO = arm -> fpga
|
||||
// PB7_SPI4_SCK = clk
|
||||
// PB6_SPI4_CS = frame
|
||||
|
||||
gpio_default_para_init(&gpio_initstructure);
|
||||
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_initstructure.gpio_pull = GPIO_PULL_DOWN;
|
||||
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
|
||||
|
||||
/* cs pin -> frame pin */
|
||||
gpio_initstructure.gpio_pull = GPIO_PULL_DOWN;
|
||||
gpio_initstructure.gpio_pins = AT32_GPIO_SSC_FRAME_PIN;
|
||||
gpio_init(AT32_GPIO_SSC_FRAME, &gpio_initstructure);
|
||||
gpio_pin_mux_config(AT32_GPIO_SSC_FRAME, AT32_GPIO_SSC_FRAME_SOURCE, AT32_GPIO_SSC_FRAME_MUX);
|
||||
|
||||
/* sck pin -> clk pin */
|
||||
gpio_initstructure.gpio_pull = GPIO_PULL_DOWN;
|
||||
gpio_initstructure.gpio_pins = AT32_GPIO_SSC_CLK_PIN;
|
||||
gpio_init(AT32_GPIO_SSC_CLK, &gpio_initstructure);
|
||||
gpio_pin_mux_config(AT32_GPIO_SSC_CLK, AT32_GPIO_SSC_CLK_SOURCE, AT32_GPIO_SSC_CLK_MUX);
|
||||
|
||||
/**
|
||||
* miso pin -> SSC_DOUT
|
||||
* ---
|
||||
* SPI is configured in TI mode and ARM is the slave.
|
||||
* In this case, according to the document description:
|
||||
* "If the slave still does not detect a valid CS pulse when receiving the last bit of the current data frame,
|
||||
* then after 1/2T SCK+3T PCLK, the output function of MISO will be turned off to control MISO floating. ”,
|
||||
* If we do not perform weak pull-down, it will cause DOUT to be in an uncontrollable state,
|
||||
* and some modules will use this pin for RF field modulation.
|
||||
* ---
|
||||
* It is best to configure it as a weak pull-down,
|
||||
* otherwise the lf_init function of the lfadc.c module will collect the voltage value of the modulated field due to default pull-up.
|
||||
* ---
|
||||
* What would happen if gpio_pull is GPIO_PULL_UP?
|
||||
* 1. call the FpgaSetupSsc to setup spi-timode
|
||||
* ssc_dout pin will be controlled by SPI
|
||||
* 2. call the gpio_fpga_mod_only_setup() to steal the dout pin for modulation
|
||||
* and call Gpio_SSC_DOUT_Low()
|
||||
* 3. call the adc read value by fpga immediately, you will get a wrong adc value,
|
||||
* because it has been always modulation and no time to wait stable.
|
||||
*/
|
||||
gpio_initstructure.gpio_pull = GPIO_PULL_DOWN; // So, make sure the dout pin to be GPIO_PULL_DOWN is a good idea.
|
||||
gpio_initstructure.gpio_pins = AT32_GPIO_SSC_DOUT_PIN;
|
||||
gpio_init(AT32_GPIO_SSC_DOUT, &gpio_initstructure);
|
||||
gpio_pin_mux_config(AT32_GPIO_SSC_DOUT, AT32_GPIO_SSC_DOUT_SOURCE, AT32_GPIO_SSC_DOUT_MUX);
|
||||
|
||||
/* mosi pin -> SSC_DIN */
|
||||
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
|
||||
gpio_initstructure.gpio_pins = AT32_GPIO_SSC_DIN_PIN;
|
||||
gpio_init(AT32_GPIO_SSC_DIN, &gpio_initstructure);
|
||||
gpio_pin_mux_config(AT32_GPIO_SSC_DIN, AT32_GPIO_SSC_DIN_SOURCE, AT32_GPIO_SSC_DIN_MUX);
|
||||
}
|
||||
|
||||
void FpgaSetupSsc(uint16_t fpga_mode) {
|
||||
spi_init_type spi_init_struct;
|
||||
|
||||
crm_periph_clock_enable(SPI_CRM_CLOCK_SSC, TRUE);
|
||||
spi_ssc_gpio_setup();
|
||||
|
||||
spi_default_para_init(&spi_init_struct);
|
||||
spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
|
||||
spi_init_struct.master_slave_mode = SPI_MODE_SLAVE; // 配置为从机模式,数据传输的时钟由fpga提供
|
||||
spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_8;
|
||||
spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB; // msb always default
|
||||
g_tx_lsb_first = false; // msb always default
|
||||
// 8 or 16 bits data for current fpga mode.
|
||||
if (FpgaIs16BitMsbMode(fpga_mode)) {
|
||||
spi_init_struct.frame_bit_num = SPI_FRAME_16BIT;
|
||||
g_ssc_data_byte_width = 2;
|
||||
} else {
|
||||
spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;
|
||||
g_ssc_data_byte_width = 1;
|
||||
}
|
||||
// The setting in clock_polarity/clock_phase/cs_mode_selection invalid for ti-mode
|
||||
// spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_LOW;
|
||||
// spi_init_struct.clock_phase = SPI_CLOCK_PHASE_2EDGE;
|
||||
// spi_init_struct.cs_mode_selection = SPI_CS_HARDWARE_MODE;
|
||||
// spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
|
||||
spi_i2s_reset(SPI_SSC); // full reset spi-ti_mode
|
||||
spi_init(SPI_SSC, &spi_init_struct);
|
||||
spi_ti_mode_enable(SPI_SSC, TRUE); // enable ti mode(Somewhat similar to SSC of AT91)
|
||||
spi_i2s_dma_receiver_enable(SPI_SSC,TRUE); // RX DMA enabled.
|
||||
spi_enable(SPI_SSC, TRUE);
|
||||
}
|
||||
|
||||
void FpgaUpdateFrameMode(uint8_t bits, bool rx_msb, bool tx_msb) {
|
||||
// Update the data width
|
||||
g_ssc_data_byte_width = bits / 8;
|
||||
// spi_frame_bit_num_set(SPI_SSC, SPI_FRAME_8BIT);
|
||||
SPI_SSC->ctrl1_bit.fbn = g_ssc_data_byte_width - 1; // SPI_FRAME_8BIT = 0, SPI_FRAME_16BIT = 1
|
||||
// The AT32 encapsulation library does not provide a function to update the LFT register.
|
||||
SPI_SSC->ctrl1_bit.ltf = rx_msb ? SPI_FIRST_BIT_MSB : SPI_FIRST_BIT_LSB; // SPI_FIRST_BIT_MSB = 0, SPI_FIRST_BIT_LSB = 1
|
||||
// Is the order of bits for tx and rx different?
|
||||
g_tx_lsb_first = tx_msb == false;
|
||||
// modify data width & bits order don't need spi disable.
|
||||
}
|
||||
|
||||
bool FpgaSetupSscRxDmaRepeat(void *buf, uint16_t len) {
|
||||
// FpgaSetupSscRxDmaRepeat() 函数是替代原先的 FpgaSetupSscDma 的操作
|
||||
// 而 FpgaSetupSscRxDmaSingle() 函数是即将要实现的新的函数,作用是只设置主缓冲,对于AT91来说,就是下一buf不会被设置,避免覆盖数据
|
||||
// 对于at32来说,两者功能是一致的,所以 FpgaSetupSscRxDmaRepeat 内部直接封装调用 FpgaSetupSscRxDmaSingle 即可,两者功能是一致的。
|
||||
return FpgaSetupSscRxDmaSingle(buf, len);
|
||||
}
|
||||
|
||||
bool FpgaSetupSscRxDmaSingle(void *buf, uint16_t len) {
|
||||
dma_init_type dma_init_struct;
|
||||
|
||||
if (buf == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
g_ssc_dma_rx_count = len; // Be sure to save the length value to this variable.
|
||||
|
||||
crm_periph_clock_enable(DMA_CRM_CLOCK_SSC, TRUE);
|
||||
dmamux_enable(DMA_SSC, TRUE);
|
||||
|
||||
dma_reset(DMA_CHANNEL_SSC);
|
||||
dma_default_para_init(&dma_init_struct);
|
||||
dma_init_struct.buffer_size = len;
|
||||
dma_init_struct.memory_inc_enable = TRUE; // address of buffer in memory need increment.
|
||||
dma_init_struct.peripheral_inc_enable = FALSE; // peripheral data register is fixed.
|
||||
// 我们在初始化DMA的时候,需要指定数据的宽度值但此函数是不具备宽度参数的,需要从SSC(SPI-TIMODE) 中了解到当前选择的数据宽度,然后做出映射。
|
||||
dma_init_struct.memory_data_width = g_ssc_data_byte_width == 1 ? DMA_MEMORY_DATA_WIDTH_BYTE : DMA_MEMORY_DATA_WIDTH_HALFWORD;
|
||||
dma_init_struct.peripheral_data_width = g_ssc_data_byte_width == 1 ? DMA_PERIPHERAL_DATA_WIDTH_BYTE : DMA_PERIPHERAL_DATA_WIDTH_HALFWORD;
|
||||
dma_init_struct.priority = DMA_PRIORITY_HIGH;
|
||||
dma_init_struct.loop_mode_enable = FALSE; // loop disabled, only one time running.
|
||||
dma_init_struct.memory_base_addr = (uint32_t)buf;
|
||||
dma_init_struct.peripheral_base_addr = (uint32_t)&(SPI_SSC->dt);
|
||||
dma_init_struct.direction = DMA_DIR_PERIPHERAL_TO_MEMORY; // receive data from SPI-TI_MODE(SSC)
|
||||
dma_init(DMA_CHANNEL_SSC, &dma_init_struct);
|
||||
dmamux_init(DMA_CHANNEL_MUX_SSC, DMA_MUX_REQ_ID_SSC);
|
||||
|
||||
if (FPGA_SSC_RX_Ready()) {
|
||||
((uint8_t*)buf)[0] = FPGA_SSC_RX_Value(); // Readout and discard old byte. It's important!
|
||||
}
|
||||
|
||||
FPGA_SSC_DMA_RX_Enable(); // Start rx channel
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// gpio for spi-cmd init
|
||||
static void spi_cmd_gpio_setup(void) {
|
||||
gpio_init_type gpio_initstructure;
|
||||
|
||||
AT32_GPIO_PERIPH_CLKS_ENABLE(AT32_GPIO_PERIPH_SPI_CLK);
|
||||
|
||||
// init gpio structure
|
||||
gpio_default_para_init(&gpio_initstructure);
|
||||
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
|
||||
|
||||
/* sck pin */
|
||||
gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_initstructure.gpio_pins = AT32_GPIO_SPI_SCK_PIN;
|
||||
gpio_init(AT32_GPIO_SPI_SCK, &gpio_initstructure);
|
||||
gpio_pin_mux_config(AT32_GPIO_SPI_SCK, AT32_GPIO_SPI_SCK_SOURCE, AT32_GPIO_SPI_SCK_MUX);
|
||||
|
||||
/* miso pin */
|
||||
gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_initstructure.gpio_pins = AT32_GPIO_SPI_MISO_PIN;
|
||||
gpio_init(AT32_GPIO_SPI_MISO, &gpio_initstructure);
|
||||
gpio_pin_mux_config(AT32_GPIO_SPI_MISO, AT32_GPIO_SPI_MISO_SOURCE, AT32_GPIO_SPI_MISO_MUX);
|
||||
|
||||
/* mosi pin */
|
||||
gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_initstructure.gpio_pins = AT32_GPIO_SPI_MOSI_PIN;
|
||||
gpio_init(AT32_GPIO_SPI_MOSI, &gpio_initstructure);
|
||||
gpio_pin_mux_config(AT32_GPIO_SPI_MOSI, AT32_GPIO_SPI_MOSI_SOURCE, AT32_GPIO_SPI_MOSI_MUX);
|
||||
|
||||
// cs software
|
||||
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
||||
gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
|
||||
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
||||
gpio_initstructure.gpio_pins = AT32_GPIO_SPI_CS_PIN;
|
||||
gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
|
||||
gpio_init(AT32_GPIO_SPI_CS, &gpio_initstructure);
|
||||
gpio_bits_set(AT32_GPIO_SPI_CS, AT32_GPIO_SPI_CS_PIN); // default CS set to high for deselect
|
||||
}
|
||||
|
||||
static void spi_cmd_setup(void) {
|
||||
spi_init_type spi_init_struct;
|
||||
|
||||
// master spi initialization
|
||||
crm_periph_clock_enable(SPI_CRM_CLOCK_CMD, TRUE);
|
||||
spi_cmd_gpio_setup();
|
||||
|
||||
spi_default_para_init(&spi_init_struct);
|
||||
|
||||
spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
|
||||
spi_init_struct.master_slave_mode = SPI_MODE_MASTER; // arm master, fpga slave
|
||||
spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_4; // 144MHZ / 4 == 36MHZ(48MHZ MAX)
|
||||
spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;
|
||||
spi_init_struct.frame_bit_num = SPI_FRAME_16BIT;
|
||||
spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_LOW;
|
||||
spi_init_struct.clock_phase = SPI_CLOCK_PHASE_1EDGE;
|
||||
spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
|
||||
spi_init(SPI_CMD, &spi_init_struct);
|
||||
spi_enable(SPI_CMD, TRUE);
|
||||
}
|
||||
|
||||
void FpgaSendCommand(uint16_t cmd, uint16_t v) {
|
||||
// Init spi
|
||||
spi_cmd_setup();
|
||||
// Send data
|
||||
gpio_bits_reset(AT32_GPIO_SPI_CS, AT32_GPIO_SPI_CS_PIN); // CS LOW
|
||||
while(spi_i2s_flag_get(SPI_CMD, SPI_I2S_TDBE_FLAG) == RESET) {}
|
||||
spi_i2s_data_transmit(SPI_CMD, cmd | v);
|
||||
while(spi_i2s_flag_get(SPI_CMD, SPI_I2S_BF_FLAG) != RESET) {} // Waiting for SPI transmit finish.
|
||||
gpio_bits_set(AT32_GPIO_SPI_CS, AT32_GPIO_SPI_CS_PIN); // CS HIGH
|
||||
}
|
||||
|
||||
void Fpga_print_status(void) {
|
||||
DbpString(_CYAN_("Current FPGA image"));
|
||||
Dbprintf(" mode.................... All-In-One");
|
||||
}
|
||||
|
||||
static void set_tck(bool level) {
|
||||
if (level) {
|
||||
GPIOC->scr = GPIO_PINS_10;
|
||||
} else {
|
||||
GPIOC->clr = GPIO_PINS_10;
|
||||
}
|
||||
}
|
||||
|
||||
static void set_tms(bool level) {
|
||||
if (level) {
|
||||
GPIOA->scr = GPIO_PINS_15;
|
||||
} else {
|
||||
GPIOA->clr = GPIO_PINS_15;
|
||||
}
|
||||
}
|
||||
|
||||
static void set_tdi(bool level) {
|
||||
if (level) {
|
||||
GPIOC->scr = GPIO_PINS_12;
|
||||
} else {
|
||||
GPIOC->clr = GPIO_PINS_12;
|
||||
}
|
||||
}
|
||||
|
||||
static bool get_tdo(void) {
|
||||
return GpioInputStatus(GPIOC, GPIO_PINS_11);
|
||||
}
|
||||
|
||||
static void set_jtagsel(bool level) {
|
||||
if (level) {
|
||||
GPIOD->scr = GPIO_PINS_2;
|
||||
} else {
|
||||
GPIOD->clr = GPIO_PINS_2;
|
||||
}
|
||||
}
|
||||
|
||||
// tck输出2mhz的时钟,实测2.01mhz左右,理论上可以稳定使用此方法,只要最终实现的误差在 ±200khz 都没问题
|
||||
// 一般只会更慢,不会更快,因为考虑到MCU的架构,主频,编译优化等级之类的,因此最终量产使用前还是得通过示波器测量实际输出频率
|
||||
static void tck_2mhz(uint32_t us) {
|
||||
// 2MHz => period = 500ns, half = 250ns
|
||||
// SysTick = 36MHz => 1 tick = 27.78ns
|
||||
// 250ns / 27.78ns ≈ 9 ticks => LOAD = 8 (because 8+1=9)
|
||||
// 8 - 1 = 7, because reserve one SysTick cycle (27.78ns) for loop, IO register operations, and SysTick operations.
|
||||
const uint32_t HALF_PERIOD_TICKS = 7; // for 250ns at 36MHz
|
||||
uint32_t cycles = us * 2; // each us has 2 half-cycles at 2MHz
|
||||
|
||||
// If 'us' is zero, the cycles will also be 0. So we need pulse only one time.
|
||||
if (cycles == 0) {
|
||||
cycles = 1;
|
||||
}
|
||||
|
||||
// Configure SysTick: use AHB/8 = 36MHz
|
||||
SysTick->CTRL = 0; // CLKSOURCE=0 => AHB/8 (if available), no interrupt, disable
|
||||
SysTick->LOAD = HALF_PERIOD_TICKS;
|
||||
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
|
||||
|
||||
// 等待N个周期,因为我们是2mhz左右的频率,所以最终一次tck脉冲就是500ns,脉冲时间*2就差不多是实际要等待的us时长
|
||||
// 实际上,考虑到如果说执行速度比较慢的情况下,那么可能最终输出的频率达不到2mhz,此时等待的时间只会更长,对于gowin的要求来说,是允许的
|
||||
// 因为高云要求的是持续产生tck时钟多少毫秒,是为了正常驱动flash的擦除过程,一般来说只能长,不能短。
|
||||
while (cycles--) {
|
||||
GPIOC->clr = GPIO_PINS_10; // low
|
||||
|
||||
SysTick->VAL = HALF_PERIOD_TICKS;
|
||||
while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0) {}
|
||||
|
||||
GPIOC->scr = GPIO_PINS_10; // high
|
||||
|
||||
SysTick->VAL = HALF_PERIOD_TICKS;
|
||||
while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0) {}
|
||||
}
|
||||
}
|
||||
|
||||
// 定义fpga的jtag实现
|
||||
static gowin_jtag_ops_t gjo = {
|
||||
.delay_ms = SpinDelay,
|
||||
.delay_us = SpinDelayUs,
|
||||
.get_tdo = get_tdo,
|
||||
.set_tck = set_tck,
|
||||
.set_tms = set_tms,
|
||||
.set_tdi = set_tdi,
|
||||
.tck_2m = tck_2mhz,
|
||||
// .dbg_print = Dbprintf, // For debug to print some msg.
|
||||
.set_jtagsel = set_jtagsel,
|
||||
};
|
||||
|
||||
// 定义fpga的jtag配置信息
|
||||
static gowin_config_ctx_t gci = {
|
||||
.tx_pos = 0,
|
||||
.tx_total = 0,
|
||||
.is_cfg_sram = false,
|
||||
.jtag_ops = &gjo,
|
||||
};
|
||||
|
||||
int FpgaStartConfig(bool configSram, uint32_t fileLength) {
|
||||
|
||||
// TODO DXL: Check the file length is valid in this platform?
|
||||
// if not, return the PM3_EOVFLOW
|
||||
|
||||
// Init jtag hardware link.
|
||||
gpio_fpga_download_setup();
|
||||
|
||||
gjo.dbg_printf = Dbprintf; // Debug start
|
||||
|
||||
// Reset for restart a new transfer
|
||||
gci.tx_pos = 0;
|
||||
gci.tx_total = fileLength;
|
||||
gci.is_cfg_sram = configSram; // 标记当前正在配置sram而非flash
|
||||
|
||||
gowin_jtag_start_config(&gci);
|
||||
if (gci.status != GOWIN_JTAG_OK) {
|
||||
return PM3_EFAILED;
|
||||
}
|
||||
|
||||
gjo.dbg_printf = NULL; // Debug stop
|
||||
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
int FpgaConfigWrite(uint8_t *data, uint32_t data_length) {
|
||||
|
||||
// TODO DXL: Check the data_length length is valid in this platform?
|
||||
// if not, return the PM3_EOVFLOW
|
||||
|
||||
gowin_jtag_config_write(data, data_length, &gci);
|
||||
if (gci.status != GOWIN_JTAG_OK) {
|
||||
return PM3_EFAILED;
|
||||
}
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
int FpgaStopConfig(void) {
|
||||
gowin_jtag_stop_config(&gci);
|
||||
if (gci.status != GOWIN_JTAG_OK) {
|
||||
return PM3_EFAILED;
|
||||
}
|
||||
return PM3_SUCCESS;
|
||||
}
|
||||
|
||||
uint32_t FpgaConfigPlatformStatus(void) {
|
||||
return gci.status;
|
||||
}
|
||||
|
||||
void FpgaResetComInterface(void) {
|
||||
spi_i2s_reset(SPI_SSC); // full reset spi
|
||||
spi_i2s_reset(SPI_CMD); // full reset spi
|
||||
crm_periph_clock_enable(SPI_CRM_CLOCK_SSC, FALSE);
|
||||
crm_periph_clock_enable(SPI_CRM_CLOCK_CMD, FALSE);
|
||||
// Do not reset GPIO, or disable GPIO clock, as other functions may depend on GPIO.
|
||||
|
||||
// Init JTAG link of FPGA to waiting for fpga work status check.
|
||||
gpio_fpga_download_setup();
|
||||
while (1) {
|
||||
gowin_jtag_status_t status = gowin_jtag_init(&gjo);
|
||||
if (status == GOWIN_JTAG_OK) {
|
||||
break;
|
||||
}
|
||||
SpinDelay(100); // Wait for 100ms before retrying
|
||||
Gpio_LED_B_Inv(); // Show some indication that we are retrying to init JTAG link, which means waiting for FPGA to be ready.
|
||||
}
|
||||
gowin_jtag_deinit(&gjo);
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
#ifndef _FPGA_HW_AT32_H_
|
||||
#define _FPGA_HW_AT32_H_
|
||||
|
||||
#include "common.h"
|
||||
#include "at32f435_437_spi.h"
|
||||
#include "at32f435_437_dma.h"
|
||||
#include "fpga_gw_jtag.h"
|
||||
|
||||
// The DMA memory address of AT32 does not self increment,
|
||||
// and there are no useful registers to know the initial set count value,
|
||||
// so we can only use one variable to store the set count value.
|
||||
extern uint16_t g_ssc_dma_rx_count;
|
||||
// Save the data width in bytes required for the fpga_mode parameter passed by the FpgaSetupSsc function.
|
||||
extern uint8_t g_ssc_data_byte_width;
|
||||
// Is tx lsb first? If diff with rx frame settings, the data will reverse before send.
|
||||
extern bool g_tx_lsb_first;
|
||||
|
||||
// TODO DXL 纠正SPI和DMA通道选择,为了方便修改,此处可先暂时定义SPI和DMA外设和DMA通道的对应宏
|
||||
// spi-ti_mode 用到了 SPI4, DMA1
|
||||
// spi-cmd 用到了 SPI3,无DMA
|
||||
#define SPI_SSC SPI4
|
||||
#define SPI_CRM_CLOCK_SSC CRM_SPI4_PERIPH_CLOCK
|
||||
#define DMA_SSC DMA1
|
||||
#define DMA_CHANNEL_SSC DMA1_CHANNEL1
|
||||
#define DMA_CRM_CLOCK_SSC CRM_DMA1_PERIPH_CLOCK
|
||||
#define DMA_CHANNEL_MUX_SSC DMA1MUX_CHANNEL1
|
||||
#define DMA_MUX_REQ_ID_SSC DMAMUX_DMAREQ_ID_SPI4_RX
|
||||
#define DMA_SSC_RX_DONE_FLAG DMA1_FDT1_FLAG // If the channel is changed, this FLAG also needs to be modified.
|
||||
#define SPI_CMD SPI3
|
||||
#define SPI_CRM_CLOCK_CMD CRM_SPI3_PERIPH_CLOCK
|
||||
|
||||
STATIC_FORCE_INLINE bool FPGA_SSC_RX_Ready(void) {
|
||||
/*
|
||||
* Note that according to the manual description, if SPI receives data but does not read it after startup,
|
||||
* the SPI peripheral will generate an overflow interrupt and no longer receive new data. At this time,
|
||||
* the RXRDY flag will remain set. If we read and use this data, we may obtain an incorrect result,
|
||||
* resulting in decoding failure.
|
||||
*/
|
||||
|
||||
// When the following conditions are met, we can consider the data to have been effectively received.
|
||||
// 1. spi_i2s_flag_get(SPI_SSC, SPI_I2S_RDBF_FLAG) == SET
|
||||
// 2. spi_i2s_flag_get(SPI_SSC, SPI_I2S_ROERR_FLAG) == RESET
|
||||
// Easy understand: Not overflow error and data buffer is full, when sts & 0x41 == 0x01, ROERR == 0.
|
||||
// ---
|
||||
// Reading SPI_DT register and SPI_STS register sequentially can clear ROERR(Must to read DT reg)
|
||||
// Only when the ROERR flag is set, it is necessary to read DT, so the '&&' condition is very important.
|
||||
// If the former does not hold, the DT register will not be read.
|
||||
return ((SPI_SSC->sts & (SPI_I2S_RDBF_FLAG | SPI_I2S_ROERR_FLAG)) == SPI_I2S_RDBF_FLAG)
|
||||
|| (((SPI_SSC->sts & SPI_I2S_ROERR_FLAG) == SPI_I2S_ROERR_FLAG) && (SPI_SSC->dt & 0)); // Readout data for clear the ROERR flag. IMPORTANT!
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE bool FPGA_SSC_TX_Ready(void) {
|
||||
// spi_i2s_flag_get(SPI_SSC, SPI_I2S_TDBE_FLAG) == SET
|
||||
return (SPI_SSC->sts & SPI_I2S_TDBE_FLAG) == SPI_I2S_TDBE_FLAG;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE bool FPGA_SSC_TX_Done(void) {
|
||||
// spi_i2s_flag_get(SPI_SSC, SPI_I2S_BF_FLAG) == RESET
|
||||
return (SPI_SSC->sts & SPI_I2S_BF_FLAG) != SPI_I2S_BF_FLAG; // SPI currently has no transmission transactions.
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE uint32_t FPGA_SSC_RX_Value(void) {
|
||||
// spi_i2s_data_receive(SPI_SSC)
|
||||
return (uint16_t)SPI_SSC->dt;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_TX_Value(uint32_t v) {
|
||||
// 'SPI_SSC->dt' is from 'spi_i2s_data_transmit()'
|
||||
if (SPI_SSC->ctrl1_bit.ltf == g_tx_lsb_first) {
|
||||
SPI_SSC->dt = (uint16_t)v; // The order of bits for tx and rx is the same, so we can send them directly.
|
||||
} else { // Is different between tx&rx, need to reverse the data.
|
||||
if (SPI_SSC->ctrl1_bit.fbn) {
|
||||
SPI_SSC->dt = (__RBIT(v) >> 16) & 0xFFFF;
|
||||
} else {
|
||||
SPI_SSC->dt = (__RBIT(v) >> 24) & 0xFF;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_TX_Clear(void) {
|
||||
while (!FPGA_SSC_TX_Ready()) {
|
||||
// Waiting for last transfer finish.
|
||||
// Nothing to do here...
|
||||
}
|
||||
FPGA_SSC_TX_Value(0x00); // Send a dummy data to clear the shift register and make the last data out.
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE bool FPGA_SSC_DMA_RX_Done(void) {
|
||||
// dma_flag_get(DMA_SSC_RX_DONE_FLAG) == RESET
|
||||
// Note: Reading this register will not automatically clear the flag,
|
||||
// and we need to write to the DMA_CR register to clear it. However,
|
||||
// we can write it in the FPGA_SSC_DMA_RX_Refresh_XXX function
|
||||
// because that function will always be called after FPGA_SSC_DMA_RX_Done returns true.
|
||||
// see: FPGA_SSC_DMA_RX_Refresh_Single()
|
||||
return DMA_SSC->sts & DMA_SSC_RX_DONE_FLAG;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Disable(void) {
|
||||
// dma_channel_enable(DMA_CHANNEL_SSC, FALSE);
|
||||
DMA_CHANNEL_SSC->ctrl_bit.chen = 0;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Enable(void) {
|
||||
// dma_channel_enable(DMA_CHANNEL_SSC, TRUE);
|
||||
DMA_CHANNEL_SSC->ctrl_bit.chen = 1;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE uint32_t* FPGA_SSC_DMA_RX_Current_Address(void) {
|
||||
|
||||
// The DMA address of AT32 does not self increment. So reading the maddr register yields a fixed initial BUF starting address
|
||||
// We can calculate the current rx address: Starting address + Current rx count
|
||||
// Note: the count value register will increment on working, so we need save it.
|
||||
// ret = starting_address(uint8) + ((g_ssc_dma_rx_count - remaining_count) * g_ssc_data_byte_width)
|
||||
// > starting_address = DMA_CHANNEL_SSC->maddr
|
||||
// > remaining_count = FPGA_SSC_DMA_RX_Remaining_Count()
|
||||
|
||||
// Warn: Calc byte count first, last to convert to U32*
|
||||
|
||||
return (uint32_t*)((uint8_t*)DMA_CHANNEL_SSC->maddr + ((g_ssc_dma_rx_count - FPGA_SSC_DMA_RX_Remaining_Count()) * g_ssc_data_byte_width));
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE uint16_t FPGA_SSC_DMA_RX_Remaining_Count(void) {
|
||||
// dma_data_number_get(DMA_CHANNEL_SSC) or dma_init()
|
||||
return (uint16_t)DMA_CHANNEL_SSC->dtcnt_bit.cnt;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Repeat(void *buf, uint16_t len) {
|
||||
// AT32 no next buf, so repeat & single is same logic.
|
||||
FPGA_SSC_DMA_RX_Refresh_Single(buf, len);
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Single(void *buf, uint16_t len) {
|
||||
g_ssc_dma_rx_count = len;
|
||||
DMA_SSC->clr = DMA_SSC_RX_DONE_FLAG & 0x0FFFFFFF; // dma_flag_clear(DMA_SSC)
|
||||
FPGA_SSC_DMA_RX_Disable(); // Writing to the CNT & ADDR registers requires closing the channel first.
|
||||
DMA_CHANNEL_SSC->dtcnt_bit.cnt = len;
|
||||
DMA_CHANNEL_SSC->maddr = (uint32_t)buf;
|
||||
FPGA_SSC_DMA_RX_Enable();
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,221 @@
|
||||
#include "at91sam7s512.h"
|
||||
#include "fpga_apis.h"
|
||||
#include "fpga_loader.h"
|
||||
#include "common.h"
|
||||
#include "proxmark3_arm.h"
|
||||
#include "dbprint.h"
|
||||
|
||||
void FpgaSetup24MHzClk(void) {
|
||||
// The FPGA gets its clock from us from PCK0 output, so set that up.
|
||||
AT91C_BASE_PIOA->PIO_BSR = GPIO_PCK0;
|
||||
AT91C_BASE_PIOA->PIO_PDR = GPIO_PCK0;
|
||||
AT91C_BASE_PMC->PMC_SCER |= AT91C_PMC_PCK0;
|
||||
// PCK0 is PLL clock / 4 = 96MHz / 4 = 24MHz
|
||||
AT91C_BASE_PMC->PMC_PCKR[0] = AT91C_PMC_CSS_PLL_CLK | AT91C_PMC_PRES_CLK_4; // 4 for 24MHz pck0, 2 for 48 MHZ pck0
|
||||
AT91C_BASE_PIOA->PIO_OER = GPIO_PCK0;
|
||||
}
|
||||
|
||||
void FpgaResetComInterface(void) {
|
||||
// Reset SPI
|
||||
AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SWRST;
|
||||
AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SWRST; // errata says it needs twice to be correctly set.
|
||||
|
||||
// Reset SSC
|
||||
AT91C_BASE_SSC->SSC_CR = AT91C_SSC_SWRST;
|
||||
}
|
||||
|
||||
void FpgaSetupSsc(uint16_t fpga_mode) {
|
||||
// First configure the GPIOs, and get ourselves a clock.
|
||||
AT91C_BASE_PIOA->PIO_ASR =
|
||||
GPIO_SSC_FRAME |
|
||||
GPIO_SSC_DIN |
|
||||
GPIO_SSC_DOUT |
|
||||
GPIO_SSC_CLK;
|
||||
AT91C_BASE_PIOA->PIO_PDR = GPIO_SSC_DOUT;
|
||||
|
||||
AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_SSC);
|
||||
|
||||
// Now set up the SSC proper, starting from a known state.
|
||||
AT91C_BASE_SSC->SSC_CR = AT91C_SSC_SWRST;
|
||||
|
||||
// RX clock comes from TX clock, RX starts on Transmit Start,
|
||||
// data and frame signal is sampled on falling edge of RK
|
||||
AT91C_BASE_SSC->SSC_RCMR = SSC_CLOCK_MODE_SELECT(1) | SSC_CLOCK_MODE_START(1);
|
||||
|
||||
// 8 or 16 per transfer, no loopback, MSB first, 1 transfer per sync pulse, no output sync
|
||||
if (FpgaIs16BitMsbMode(fpga_mode)) {
|
||||
AT91C_BASE_SSC->SSC_RFMR = SSC_FRAME_MODE_BITS_IN_WORD(16) | AT91C_SSC_MSBF | SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
|
||||
} else {
|
||||
AT91C_BASE_SSC->SSC_RFMR = SSC_FRAME_MODE_BITS_IN_WORD(8) | AT91C_SSC_MSBF | SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
|
||||
}
|
||||
|
||||
// TX clock comes from TK pin, no clock output, outputs change on rising edge of TK,
|
||||
// TF (frame sync) is sampled on falling edge of TK, start TX on rising edge of TF
|
||||
AT91C_BASE_SSC->SSC_TCMR = SSC_CLOCK_MODE_SELECT(2) | SSC_CLOCK_MODE_START(5);
|
||||
|
||||
// tx framing is the same as the rx framing
|
||||
AT91C_BASE_SSC->SSC_TFMR = AT91C_BASE_SSC->SSC_RFMR;
|
||||
|
||||
AT91C_BASE_SSC->SSC_CR = AT91C_SSC_RXEN | AT91C_SSC_TXEN;
|
||||
}
|
||||
|
||||
void FpgaUpdateFrameMode(uint8_t bits, bool rx_msb, bool tx_msb) {
|
||||
// AT91C_SSC_MSBF = (0x1 << 7)
|
||||
// It's a magic, if we need msb, the msb param is 1, so we can set a valid enable bit to msb reg.
|
||||
// 0 = 0 << 7, so lsb will skip update.
|
||||
AT91C_BASE_SSC->SSC_RFMR = SSC_FRAME_MODE_BITS_IN_WORD(bits) | (rx_msb << 7);
|
||||
AT91C_BASE_SSC->SSC_TFMR = SSC_FRAME_MODE_BITS_IN_WORD(bits) | (tx_msb << 7);
|
||||
}
|
||||
|
||||
bool FpgaSetupSscRxDmaRepeat(void *buf, uint16_t len) {
|
||||
if (buf == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
FPGA_SSC_DMA_RX_Disable();
|
||||
AT91C_BASE_PDC_SSC->PDC_RPR = (uint32_t) buf; // transfer to this memory address
|
||||
AT91C_BASE_PDC_SSC->PDC_RCR = len; // transfer this many bytes
|
||||
AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) buf; // next transfer to same memory address
|
||||
AT91C_BASE_PDC_SSC->PDC_RNCR = len; // ... with same number of bytes
|
||||
FPGA_SSC_DMA_RX_Enable();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FpgaSetupSscRxDmaSingle(void *buf, uint16_t len) {
|
||||
if (buf == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
FPGA_SSC_DMA_RX_Disable(); // Disable DMA Transfer
|
||||
AT91C_BASE_PDC_SSC->PDC_RPR = (uint32_t) buf; // start transfer to this memory address
|
||||
AT91C_BASE_PDC_SSC->PDC_RCR = len; // transfer this many samples
|
||||
((uint8_t*)buf)[0] = (uint8_t)FPGA_SSC_RX_Value(); // clear receive register
|
||||
FPGA_SSC_DMA_RX_Enable(); // Start DMA transfer
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Set up the Serial Peripheral Interface as master
|
||||
// Used to write the FPGA config word
|
||||
// May also be used to write to other SPI attached devices like an LCD
|
||||
//-----------------------------------------------------------------------------
|
||||
static void DisableSpi(void) {
|
||||
//* Reset all the Chip Select register
|
||||
AT91C_BASE_SPI->SPI_CSR[0] = 0;
|
||||
AT91C_BASE_SPI->SPI_CSR[1] = 0;
|
||||
AT91C_BASE_SPI->SPI_CSR[2] = 0;
|
||||
AT91C_BASE_SPI->SPI_CSR[3] = 0;
|
||||
|
||||
// Reset the SPI mode
|
||||
AT91C_BASE_SPI->SPI_MR = 0;
|
||||
|
||||
// Disable all interrupts
|
||||
AT91C_BASE_SPI->SPI_IDR = 0xFFFFFFFF;
|
||||
|
||||
// SPI disable
|
||||
AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIDIS;
|
||||
}
|
||||
|
||||
static void SetupSpi(int mode) {
|
||||
// PA1 -> SPI_NCS3 chip select (MEM)
|
||||
// PA10 -> SPI_NCS2 chip select (LCD)
|
||||
// PA11 -> SPI_NCS0 chip select (FPGA)
|
||||
// PA12 -> SPI_MISO Master-In Slave-Out
|
||||
// PA13 -> SPI_MOSI Master-Out Slave-In
|
||||
// PA14 -> SPI_SPCK Serial Clock
|
||||
|
||||
// Disable PIO control of the following pins, allows use by the SPI peripheral
|
||||
AT91C_BASE_PIOA->PIO_PDR = GPIO_NCS0 | GPIO_MISO | GPIO_MOSI | GPIO_SPCK;
|
||||
|
||||
// Peripheral A
|
||||
AT91C_BASE_PIOA->PIO_ASR = GPIO_NCS0 | GPIO_MISO | GPIO_MOSI | GPIO_SPCK;
|
||||
|
||||
// Peripheral B
|
||||
//AT91C_BASE_PIOA->PIO_BSR |= GPIO_NCS2;
|
||||
|
||||
//enable the SPI Peripheral clock
|
||||
AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_SPI);
|
||||
// Enable SPI
|
||||
AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIEN;
|
||||
|
||||
switch (mode) {
|
||||
case SPI_FPGA_MODE:
|
||||
AT91C_BASE_SPI->SPI_MR =
|
||||
(0 << 24) | // Delay between chip selects (take default: 6 MCK periods)
|
||||
(0xE << 16) | // Peripheral Chip Select (selects FPGA SPI_NCS0 or PA11)
|
||||
(0 << 7) | // Local Loopback Disabled
|
||||
AT91C_SPI_MODFDIS | // Mode Fault Detection disabled
|
||||
(0 << 2) | // Chip selects connected directly to peripheral
|
||||
AT91C_SPI_PS_FIXED | // Fixed Peripheral Select
|
||||
AT91C_SPI_MSTR; // Master Mode
|
||||
|
||||
AT91C_BASE_SPI->SPI_CSR[0] =
|
||||
(1 << 24) | // Delay between Consecutive Transfers (32 MCK periods)
|
||||
(1 << 16) | // Delay Before SPCK (1 MCK period)
|
||||
(6 << 8) | // Serial Clock Baud Rate (baudrate = MCK/6 = 24MHz/6 = 4M baud
|
||||
AT91C_SPI_BITS_16 | // Bits per Transfer (16 bits)
|
||||
(0 << 3) | // Chip Select inactive after transfer
|
||||
AT91C_SPI_NCPHA | // Clock Phase data captured on leading edge, changes on following edge
|
||||
(0 << 0); // Clock Polarity inactive state is logic 0
|
||||
break;
|
||||
/*
|
||||
case SPI_LCD_MODE:
|
||||
AT91C_BASE_SPI->SPI_MR =
|
||||
( 0 << 24) | // Delay between chip selects (take default: 6 MCK periods)
|
||||
(0xB << 16) | // Peripheral Chip Select (selects LCD SPI_NCS2 or PA10)
|
||||
( 0 << 7) | // Local Loopback Disabled
|
||||
( 1 << 4) | // Mode Fault Detection disabled
|
||||
( 0 << 2) | // Chip selects connected directly to peripheral
|
||||
( 0 << 1) | // Fixed Peripheral Select
|
||||
( 1 << 0); // Master Mode
|
||||
|
||||
AT91C_BASE_SPI->SPI_CSR[2] =
|
||||
( 1 << 24) | // Delay between Consecutive Transfers (32 MCK periods)
|
||||
( 1 << 16) | // Delay Before SPCK (1 MCK period)
|
||||
( 6 << 8) | // Serial Clock Baud Rate (baudrate = MCK/6 = 24MHz/6 = 4M baud
|
||||
AT91C_SPI_BITS_9 | // Bits per Transfer (9 bits)
|
||||
( 0 << 3) | // Chip Select inactive after transfer
|
||||
( 1 << 1) | // Clock Phase data captured on leading edge, changes on following edge
|
||||
( 0 << 0); // Clock Polarity inactive state is logic 0
|
||||
break;
|
||||
*/
|
||||
default:
|
||||
DisableSpi();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void FpgaSendCommand(uint16_t cmd, uint16_t v) {
|
||||
SetupSpi(SPI_FPGA_MODE);
|
||||
while ((AT91C_BASE_SPI->SPI_SR & AT91C_SPI_TXEMPTY) == 0); // wait for the transfer to complete
|
||||
AT91C_BASE_SPI->SPI_TDR = AT91C_SPI_LASTXFER | cmd | v; // send the data
|
||||
while (!(AT91C_BASE_SPI->SPI_SR & AT91C_SPI_RDRF)) {}; // wait till transfer is complete
|
||||
}
|
||||
|
||||
void Fpga_print_status(void) {
|
||||
DbpString(_CYAN_("Current FPGA image"));
|
||||
Dbprintf(" mode.................... %s", FpgaGetCurrentVersionString());
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// Config bitstream for FPGA
|
||||
// Waiting for impl...
|
||||
|
||||
int FpgaStartConfig(bool configSram, uint32_t fileLength) {
|
||||
// TODO DXL: Not implemented
|
||||
return PM3_ENOTIMPL;
|
||||
}
|
||||
|
||||
int FpgaConfigWrite(uint8_t *data, uint32_t data_length) {
|
||||
return PM3_ENOTIMPL;
|
||||
}
|
||||
int FpgaStopConfig(void) {
|
||||
return PM3_ENOTIMPL;
|
||||
}
|
||||
|
||||
uint32_t FpgaConfigPlatformStatus(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
@@ -0,0 +1,86 @@
|
||||
#ifndef _FPGA_HW_AT91_H_
|
||||
#define _FPGA_HW_AT91_H_
|
||||
|
||||
#include "common.h"
|
||||
#include "at91sam7s512.h"
|
||||
|
||||
STATIC_FORCE_INLINE bool FPGA_SSC_RX_Ready(void) {
|
||||
// There is no need to check the overflow flag,
|
||||
// as according to the datasheet description,
|
||||
// the latest data always moves from the shift register to the RHR register for overwriting.
|
||||
return (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) == AT91C_SSC_RXRDY;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE bool FPGA_SSC_TX_Ready(void) {
|
||||
return (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXRDY) == AT91C_SSC_TXRDY;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE bool FPGA_SSC_DMA_RX_Done(void) {
|
||||
return (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_ENDRX) == AT91C_SSC_ENDRX;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE bool FPGA_SSC_TX_Done(void) {
|
||||
return (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXEMPTY) == AT91C_SSC_TXEMPTY;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE uint32_t FPGA_SSC_RX_Value(void) {
|
||||
return AT91C_BASE_SSC->SSC_RHR;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_TX_Value(uint32_t v) {
|
||||
AT91C_BASE_SSC->SSC_THR = v;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_TX_Clear(void) {
|
||||
// TODO DXL: It is best to perform a clearing,
|
||||
// but currently it seems that not clearing on RDV4 will not result in erroneous modulation.
|
||||
// Afterwards, when we have time, we can conduct a test to see if adding the clearing logic affects anything.
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Disable(void) {
|
||||
AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Enable(void) {
|
||||
AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE uint32_t* FPGA_SSC_DMA_RX_Current_Address(void) {
|
||||
return (uint32_t*)AT91C_BASE_PDC_SSC->PDC_RPR;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE uint16_t FPGA_SSC_DMA_RX_Remaining_Count(void) {
|
||||
return AT91C_BASE_PDC_SSC->PDC_RCR;
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Repeat(void *buf, uint16_t len) {
|
||||
// primary buffer was stopped( <-- we lost data!
|
||||
if (AT91C_BASE_PDC_SSC->PDC_RCR == 0) {
|
||||
AT91C_BASE_PDC_SSC->PDC_RPR = (uint32_t) buf;
|
||||
AT91C_BASE_PDC_SSC->PDC_RCR = len;
|
||||
// Dbprintf("[-] RxEmpty ERROR | data length %d", len); // temporary
|
||||
}
|
||||
// secondary buffer sets as primary, secondary buffer was stopped
|
||||
if (AT91C_BASE_PDC_SSC->PDC_RNCR == 0) {
|
||||
AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) buf;
|
||||
AT91C_BASE_PDC_SSC->PDC_RNCR = len;
|
||||
}
|
||||
}
|
||||
|
||||
STATIC_FORCE_INLINE void FPGA_SSC_DMA_RX_Refresh_Single(void *buf, uint16_t len) {
|
||||
// The previous code logic was to update the NEXT BUF information first and then wait for the event of receiving completion to arrive (the main receiving register count is reset to zero)
|
||||
// Achieve the effect of setting buf ->waiting for reception completion and data processing (automatic rotation buf) ->setting buf (next cycle) ->waiting for reception completion and data processing (automatic rotation buf)
|
||||
// Seamlessly initiate the next reception and ensure that data is not overwritten, as the address of the buf set each time is different. Therefore, the logic of AT91 can be implemented using NEXT buf, and the key is to prevent the main buf from stopping
|
||||
// Otherwise, once the main buf stops, NEXT BUF will not be able to continue refreshing the next reception. Only when the main buf works normally until it ends, will it automatically rotate the reception information of NEXT BUF
|
||||
// Therefore, based on the timing of the call, if the end of reception is judged first, the main buf should be used for refreshing. If the buf is refreshed first, the end of reception should be judged later!
|
||||
// But in reality, for the sake of compatibility between platforms, we can only use the logic of first judging the end of the reception and then refreshing the reception buf! Because AT32 does not support NEXT BUF.
|
||||
|
||||
// Warn: This code cannot be used because the NEXT BUF will only work when the MAIN BUF is working.
|
||||
// AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t)next_buf;
|
||||
// AT91C_BASE_PDC_SSC->PDC_RNCR = PM3_CMD_DATA_SIZE;
|
||||
|
||||
AT91C_BASE_PDC_SSC->PDC_RPR = (uint32_t) buf; // start transfer to this memory address
|
||||
AT91C_BASE_PDC_SSC->PDC_RCR = len; // transfer this many samples
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,414 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) Jonathan Westhues, April 2006
|
||||
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// See LICENSE.txt for the text of the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Routines to load the FPGA image, and then to configure the FPGA's major
|
||||
// mode once it is configured.
|
||||
//-----------------------------------------------------------------------------
|
||||
#include "fpga_loader.h"
|
||||
#include "fpga_apis.h"
|
||||
|
||||
#include "proxmark3_arm.h"
|
||||
#include "appmain.h"
|
||||
#include "BigBuf.h"
|
||||
#include "ticks_apis.h"
|
||||
#include "dbprint.h"
|
||||
#include "fpga.h"
|
||||
#include "string.h"
|
||||
|
||||
#include "lz4.h" // uncompress
|
||||
|
||||
typedef struct {
|
||||
LZ4_streamDecode_t *lz4StreamDecode;
|
||||
char *next_in;
|
||||
int avail_in;
|
||||
} lz4_stream_t;
|
||||
|
||||
typedef lz4_stream_t *lz4_streamp_t;
|
||||
|
||||
// this is where the bitstreams are located in memory:
|
||||
extern uint32_t _binary_obj_fpga_all_bit_z_start[], _binary_obj_fpga_all_bit_z_end[];
|
||||
|
||||
static uint8_t *fpga_image_ptr = NULL;
|
||||
static uint32_t uncompressed_bytes_cnt;
|
||||
|
||||
// remember which version of the bitstream we have already downloaded to the FPGA
|
||||
// For high-capacity FPGA chips, the FPGA firmware may have been merged,
|
||||
// and this flag can let us know which mode it is running in?
|
||||
static int downloaded_bitstream = FPGA_BITSTREAM_UNKNOWN;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// Uncompress (inflate) the FPGA data. Returns one decompressed byte with each call.
|
||||
//----------------------------------------------------------------------------
|
||||
static int get_from_fpga_combined_stream(lz4_streamp_t compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
if (fpga_image_ptr == output_buffer + FPGA_RING_BUFFER_BYTES) { // need more data
|
||||
fpga_image_ptr = output_buffer;
|
||||
int cmp_bytes;
|
||||
memcpy(&cmp_bytes, compressed_fpga_stream->next_in, sizeof(int));
|
||||
compressed_fpga_stream->next_in += 4;
|
||||
compressed_fpga_stream->avail_in -= cmp_bytes + 4;
|
||||
int res = LZ4_decompress_safe_continue(compressed_fpga_stream->lz4StreamDecode,
|
||||
compressed_fpga_stream->next_in,
|
||||
(char *)output_buffer,
|
||||
cmp_bytes,
|
||||
FPGA_RING_BUFFER_BYTES);
|
||||
if (res <= 0) {
|
||||
Dbprintf("inflate returned: %d", res);
|
||||
return res;
|
||||
}
|
||||
compressed_fpga_stream->next_in += cmp_bytes;
|
||||
}
|
||||
uncompressed_bytes_cnt++;
|
||||
return *fpga_image_ptr++;
|
||||
}
|
||||
|
||||
static int bitstream_target_to_index(FPGA_config bitstream_target) {
|
||||
static int8_t bitstream_index_map[FPGA_CONFIG_COUNT] = {-1};
|
||||
|
||||
// Initialize
|
||||
if (bitstream_index_map[FPGA_BITSTREAM_UNKNOWN] == -1) {
|
||||
bitstream_index_map[FPGA_BITSTREAM_UNKNOWN] = 0;
|
||||
|
||||
for (size_t i = 0; i < g_fpga_bitstream_num; i++) {
|
||||
FPGA_VERSION_INFORMATION info = g_fpga_version_information[i];
|
||||
bitstream_index_map[info.target_config] = i;
|
||||
}
|
||||
}
|
||||
|
||||
return bitstream_index_map[bitstream_target];
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// Undo the interleaving of several FPGA config files. FPGA config files
|
||||
// are combined into one big file:
|
||||
// 288 bytes from FPGA file 1, followed by 288 bytes from FGPA file 2, etc.
|
||||
//----------------------------------------------------------------------------
|
||||
static int get_from_fpga_stream(int bitstream_target, lz4_streamp_t compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
int bitstream_index = bitstream_target_to_index(bitstream_target);
|
||||
while ((uncompressed_bytes_cnt / FPGA_INTERLEAVE_SIZE) % g_fpga_bitstream_num != bitstream_index) {
|
||||
// skip undesired data belonging to other bitstream_targets
|
||||
get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer);
|
||||
}
|
||||
|
||||
return get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// Initialize decompression of the respective (HF or LF) FPGA stream
|
||||
//----------------------------------------------------------------------------
|
||||
static bool reset_fpga_stream(int bitstream_target, lz4_streamp_t compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
uint8_t header[FPGA_BITSTREAM_FIXED_HEADER_SIZE];
|
||||
|
||||
uncompressed_bytes_cnt = 0;
|
||||
|
||||
// initialize z_stream structure for inflate:
|
||||
compressed_fpga_stream->next_in = (char *)_binary_obj_fpga_all_bit_z_start;
|
||||
compressed_fpga_stream->avail_in = (uint32_t)_binary_obj_fpga_all_bit_z_end - (uint32_t)_binary_obj_fpga_all_bit_z_start;
|
||||
|
||||
int res = LZ4_setStreamDecode(compressed_fpga_stream->lz4StreamDecode, NULL, 0);
|
||||
if (res == 0)
|
||||
return false;
|
||||
|
||||
fpga_image_ptr = output_buffer + FPGA_RING_BUFFER_BYTES;
|
||||
|
||||
for (uint16_t i = 0; i < FPGA_BITSTREAM_FIXED_HEADER_SIZE; i++)
|
||||
header[i] = get_from_fpga_stream(bitstream_target, compressed_fpga_stream, output_buffer);
|
||||
|
||||
// Check for a valid .bit file (starts with bitparse_fixed_header)
|
||||
if (memcmp(bitparse_fixed_header, header, FPGA_BITSTREAM_FIXED_HEADER_SIZE) == 0)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void DownloadFPGA_byte(uint8_t w) {
|
||||
#define SEND_BIT(x) { if(w & (1<<x) ) Gpio_FPGA_DIN_High(); else Gpio_FPGA_DIN_Low(); Gpio_FPGA_CCLK_High(); Gpio_FPGA_CCLK_Low(); }
|
||||
SEND_BIT(7);
|
||||
SEND_BIT(6);
|
||||
SEND_BIT(5);
|
||||
SEND_BIT(4);
|
||||
SEND_BIT(3);
|
||||
SEND_BIT(2);
|
||||
SEND_BIT(1);
|
||||
SEND_BIT(0);
|
||||
}
|
||||
|
||||
// TODO DXL: Abstract this function
|
||||
// Download the fpga image starting at current stream position with length FpgaImageLen bytes
|
||||
static void DownloadFPGA(int bitstream_target, int FpgaImageLen, lz4_streamp_t compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
int i = 0;
|
||||
|
||||
#if !defined XC3 && !defined PM5
|
||||
gpio_fpga_on_setup();
|
||||
Gpio_FPGA_ON_High(); // ensure everything is powered on
|
||||
#endif
|
||||
|
||||
SpinDelay(50);
|
||||
|
||||
LED_D_ON();
|
||||
|
||||
// setup initial logic state
|
||||
Gpio_FPGA_NPROGRAM_High();
|
||||
Gpio_FPGA_CCLK_Low();
|
||||
Gpio_FPGA_DIN_Low();
|
||||
|
||||
// setup gpio function
|
||||
gpio_fpga_download_setup();
|
||||
|
||||
#if defined XC3
|
||||
// ICopyX(3S100E) M2 & M3 OUTPUT HIGH, for 'Slave Serial' mode select.
|
||||
Gpio_FPGA_XC3_M1_High();
|
||||
Gpio_FPGA_XC3_M2_High();
|
||||
#endif
|
||||
|
||||
// enter FPGA configuration mode
|
||||
Gpio_FPGA_NPROGRAM_Low();
|
||||
SpinDelay(50);
|
||||
Gpio_FPGA_NPROGRAM_High();
|
||||
|
||||
i = 100000;
|
||||
// wait for FPGA ready to accept data signal
|
||||
while ((i) && (!Gpio_FPGA_NINIT_Read())) {
|
||||
i--;
|
||||
}
|
||||
|
||||
// crude error indicator, leave both red LEDs on and return
|
||||
if (i == 0) {
|
||||
LED_C_ON();
|
||||
LED_D_ON();
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined XC3
|
||||
// ICopyX(3S100E) M2 & M3 return to SPI peripheral
|
||||
Gpio_FPGA_XC3_M1_Low();
|
||||
Gpio_FPGA_XC3_M2_Low();
|
||||
AT91C_BASE_PIOA->PIO_PDR = GPIO_SPCK | GPIO_MOSI;
|
||||
#endif
|
||||
|
||||
for (i = 0; i < FpgaImageLen; i++) {
|
||||
int b = get_from_fpga_stream(bitstream_target, compressed_fpga_stream, output_buffer);
|
||||
if (b < 0) {
|
||||
Dbprintf("Error %d during FpgaDownload", b);
|
||||
break;
|
||||
}
|
||||
DownloadFPGA_byte(b);
|
||||
}
|
||||
|
||||
// continue to clock FPGA until ready signal goes high
|
||||
i = 100000;
|
||||
while ((i--) && (!Gpio_FPGA_DONE_Read())) {
|
||||
Gpio_FPGA_CCLK_High();
|
||||
Gpio_FPGA_CCLK_Low();
|
||||
}
|
||||
// crude error indicator, leave both red LEDs on and return
|
||||
if (i == 0) {
|
||||
LED_C_ON();
|
||||
LED_D_ON();
|
||||
return;
|
||||
}
|
||||
LED_D_OFF();
|
||||
}
|
||||
|
||||
/* Simple Xilinx .bit parser. The file starts with the fixed opaque byte sequence
|
||||
* 00 09 0f f0 0f f0 0f f0 0f f0 00 00 01
|
||||
* After that the format is 1 byte section type (ASCII character), 2 byte length
|
||||
* (big endian), <length> bytes content. Except for section 'e' which has 4 bytes
|
||||
* length.
|
||||
*/
|
||||
static int bitparse_find_section(int bitstream_target, char section_name, uint32_t *section_length, lz4_streamp_t compressed_fpga_stream, uint8_t *output_buffer) {
|
||||
|
||||
#define MAX_FPGA_BIT_STREAM_HEADER_SEARCH 100 // maximum number of bytes to search for the requested section
|
||||
|
||||
int result = 0;
|
||||
uint16_t numbytes = 0;
|
||||
while (numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH) {
|
||||
char current_name = get_from_fpga_stream(bitstream_target, compressed_fpga_stream, output_buffer);
|
||||
numbytes++;
|
||||
uint32_t current_length = 0;
|
||||
if (current_name < 'a' || current_name > 'e') {
|
||||
/* Strange section name, abort */
|
||||
break;
|
||||
}
|
||||
current_length = 0;
|
||||
switch (current_name) {
|
||||
case 'e':
|
||||
/* Four byte length field */
|
||||
current_length += get_from_fpga_stream(bitstream_target, compressed_fpga_stream, output_buffer) << 24;
|
||||
current_length += get_from_fpga_stream(bitstream_target, compressed_fpga_stream, output_buffer) << 16;
|
||||
current_length += get_from_fpga_stream(bitstream_target, compressed_fpga_stream, output_buffer) << 8;
|
||||
current_length += get_from_fpga_stream(bitstream_target, compressed_fpga_stream, output_buffer) << 0;
|
||||
numbytes += 4;
|
||||
if (current_length > 300 * 1024) {
|
||||
/* section e should never exceed about 300KB, if the length is too big limit it but still send the bitstream just in case */
|
||||
current_length = 300 * 1024;
|
||||
}
|
||||
break;
|
||||
default: /* Two byte length field */
|
||||
current_length += get_from_fpga_stream(bitstream_target, compressed_fpga_stream, output_buffer) << 8;
|
||||
current_length += get_from_fpga_stream(bitstream_target, compressed_fpga_stream, output_buffer) << 0;
|
||||
numbytes += 2;
|
||||
if (current_length > 64) {
|
||||
/* if text field is too long, keep it but truncate it */
|
||||
current_length = 64;
|
||||
}
|
||||
}
|
||||
|
||||
if (current_name == section_name) {
|
||||
/* Found it */
|
||||
*section_length = current_length;
|
||||
result = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < current_length && numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH; i++) {
|
||||
get_from_fpga_stream(bitstream_target, compressed_fpga_stream, output_buffer);
|
||||
numbytes++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// Change FPGA image status, if image loaded.
|
||||
// bitstream_target is your new fpga image version
|
||||
// return true if can change.
|
||||
// return false if image is unloaded.
|
||||
//----------------------------------------------------------------------------
|
||||
#if defined XC3 || defined PM5
|
||||
static bool FpgaConfCurrentMode(int bitstream_target) {
|
||||
// fpga "XC3S100E" image is merged. If fpga image is no init, We need load hf_lf_allinone.bit.
|
||||
if (downloaded_bitstream != FPGA_BITSTREAM_UNKNOWN) {
|
||||
// gpio function setup
|
||||
gpio_fpga_switch_setup();
|
||||
|
||||
// try to turn off antenna
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||
|
||||
if (bitstream_target == FPGA_BITSTREAM_LF) {
|
||||
Gpio_FPGA_SWITCH_Low();
|
||||
} else {
|
||||
Gpio_FPGA_SWITCH_High();
|
||||
}
|
||||
// update downloaded_bitstream
|
||||
downloaded_bitstream = bitstream_target;
|
||||
// turn off antenna
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// Check which FPGA image is currently loaded (if any). If necessary
|
||||
// decompress and load the correct (HF or LF) image to the FPGA
|
||||
//----------------------------------------------------------------------------
|
||||
static void FpgaDownloadAndGoEx(int bitstream_target, bool keep_em) {
|
||||
|
||||
// check whether or not the bitstream is already loaded
|
||||
if (downloaded_bitstream == bitstream_target) {
|
||||
FpgaEnableTracing();
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined PM5
|
||||
// The FPGA of PM5 comes with built-in FLASH, so there is no need to download it at startup anymore.
|
||||
downloaded_bitstream = bitstream_target; // FpgaConfCurrentMode() requires downloading for the first time, but we skipped it.
|
||||
FpgaConfCurrentMode(bitstream_target);
|
||||
return; // always return
|
||||
#endif
|
||||
|
||||
#if defined XC3
|
||||
// If we can change image version
|
||||
// direct return.
|
||||
if (FpgaConfCurrentMode(bitstream_target)) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Send waiting time extension request as this will take a while
|
||||
send_wtx(FPGA_LOAD_WAIT_TIME);
|
||||
|
||||
bool verbose = (g_dbglevel > 3);
|
||||
|
||||
// make sure that we have enough memory to decompress
|
||||
if (keep_em) {
|
||||
BigBuf_free_keep_EM();
|
||||
BigBuf_Clear_keep_EM();
|
||||
} else {
|
||||
BigBuf_free();
|
||||
BigBuf_Clear_ext(verbose);
|
||||
}
|
||||
|
||||
lz4_stream_t compressed_fpga_stream;
|
||||
LZ4_streamDecode_t lz4StreamDecode_body = {{ 0 }};
|
||||
compressed_fpga_stream.lz4StreamDecode = &lz4StreamDecode_body;
|
||||
uint8_t *output_buffer = BigBuf_calloc(FPGA_RING_BUFFER_BYTES);
|
||||
|
||||
if (reset_fpga_stream(bitstream_target, &compressed_fpga_stream, output_buffer) == false) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t bitstream_length;
|
||||
if (bitparse_find_section(bitstream_target, 'e', &bitstream_length, &compressed_fpga_stream, output_buffer)) {
|
||||
DownloadFPGA(bitstream_target, bitstream_length, &compressed_fpga_stream, output_buffer);
|
||||
downloaded_bitstream = bitstream_target;
|
||||
}
|
||||
|
||||
#if defined XC3
|
||||
// first download fpga image to hf
|
||||
// we need to change fpga status to hf
|
||||
FpgaConfCurrentMode(bitstream_target);
|
||||
#endif
|
||||
|
||||
// turn off antenna
|
||||
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
||||
|
||||
// free eventually allocated BigBuf memory
|
||||
if (keep_em) {
|
||||
BigBuf_free_keep_EM();
|
||||
BigBuf_Clear_keep_EM();
|
||||
} else {
|
||||
BigBuf_free();
|
||||
BigBuf_Clear_ext(false);
|
||||
}
|
||||
}
|
||||
|
||||
void FpgaDownloadAndGo(int bitstream_target) {
|
||||
FpgaDownloadAndGoEx(bitstream_target, false);
|
||||
}
|
||||
|
||||
void FpgaDownloadAndGo_keep_EM(int bitstream_target) {
|
||||
FpgaDownloadAndGoEx(bitstream_target, true);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// Which FPGA bitstream has been downloaded currently.
|
||||
//----------------------------------------------------------------------------
|
||||
int FpgaGetCurrent(void) {
|
||||
return downloaded_bitstream;
|
||||
}
|
||||
|
||||
void FpgaResetBitstream(void) {
|
||||
downloaded_bitstream = FPGA_BITSTREAM_UNKNOWN;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// The information of the bitstream of the FPGA that has been downloaded currently.
|
||||
//----------------------------------------------------------------------------
|
||||
const char* FpgaGetCurrentVersionString(void) {
|
||||
return g_fpga_version_information[bitstream_target_to_index(downloaded_bitstream)].versionString;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// See LICENSE.txt for the text of the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Routines to load the FPGA image, and then to configure the FPGA's major
|
||||
// mode once it is configured.
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifndef __FPGALOADER_H
|
||||
#define __FPGALOADER_H
|
||||
|
||||
#include "common.h"
|
||||
#include "fpga.h"
|
||||
|
||||
int FpgaGetCurrent(void);
|
||||
const char* FpgaGetCurrentVersionString(void);
|
||||
void FpgaDownloadAndGo(int bitstream_target);
|
||||
void FpgaDownloadAndGo_keep_EM(int bitstream_target);
|
||||
void FpgaResetBitstream(void);
|
||||
// void FpgaGatherVersion(int bitstream_target, char *dst, int len);
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user