Make common_arm to hardware abstraction layer.

This commit is contained in:
dxl authored and Philippe Teuwen committed 2026-08-19 15:32:39 +02:00
1 parent 51d0ff260a
commit 36f1c7e44c
61 files changed
+8050 -1670

No files matched your search

+130
View File
@@ -0,0 +1,130 @@
//-----------------------------------------------------------------------------
// Borrowed initially from Arduino SPIFlash Library v.2.5.0
// Copyright (C) 2015 by Prajwal Bhattaram.
//
// 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.
//-----------------------------------------------------------------------------
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// Common Instructions //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
#ifndef FLASHMEM_H_
#define FLASHMEM_H_
#include "common.h"
#include "pmflash.h"
// Used Command
#define ID 0x90
#define MANID 0x90
#define JEDECID 0x9F
#define READSTAT1 0x05
#define READSTAT2 0x35
#define WRITESTAT 0x01
#define WRITEDISABLE 0x04
#define WRITEENABLE 0x06
#define READDATA 0x03
#define FASTREAD 0x0B
#define FASTREAD_QO 0x6B // Fast Read Quad Output, qspi, some platform unsupported(at91, haha).
#define PAGEPROG 0x02
#define SECTORERASE 0x20
#define BLOCK32ERASE 0x52
#define BLOCK64ERASE 0xD8
#define CHIPERASE 0xC7
#define UNIQUE_ID 0x4B
// Not used or not support command
#define RELEASE 0xAB
#define POWERDOWN 0xB9
#define SUSPEND 0x75
#define RESUME 0x7A
// Flash busy timeout: 20ms is the strict minimum when writing 256kb
#define BUSY_TIMEOUT 200000L
#define PAGESIZE 0x100
#define WINBOND_WRITE_DELAY 0x02
#define BUSY 0x01
#define WRTEN 0x02
#define SUS 0x40
#define DUMMYBYTE 0xEE
#define NULLBYTE 0x00
#define NULLINT 0x0000
#define NO_CONTINUE 0x00
#define PASS 0x01
#define FAIL 0x00
// List of blocks
#define MAX_BLOCKS 4
#define MAX_SECTORS 16
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// The default values returned by different platforms are different.
// This function is implemented by the platform.
uint32_t Flash_DefaultBaudrate(void);
bool FlashInit(void);
bool FlashSetup(uint32_t baudrate);
void FlashStop(void);
bool Flash_UniqueID(uint8_t *uid);
bool Flash_CheckBusy(uint32_t timeout);
bool Flash_ReadStat1(uint8_t *status);
bool Flash_ReadStat2(uint8_t *status);
#ifndef AS_BOOTROM
uint32_t Flash_GetSpiBaudrate(void);
void Flash_SetSpiBaudrate(uint32_t baudrate);
bool Flash_WriteEnable(void);
bool Flash_WipeMemoryPage(uint8_t page);
bool Flash_WipeMemory(void);
bool Flash_Erase4k(uint8_t block, uint8_t sector);
//bool Flash_Erase32k(uint32_t address);
bool Flash_Erase64k(uint8_t block);
// defs see: https://chromium.googlesource.com/chromiumos/third_party/flashrom/+/798d2adc9527f724bc5096a646cf99efdbb6b59e/flashchips.h
typedef struct {
uint8_t manufacturer_id;
uint8_t device_id;
uint8_t device_id2;
} flash_device_type_t; // extra device_id used for the JEDEC ID read via cmd 9F
bool Flash_ReadID(flash_device_type_t *result, bool read_jedec);
uint16_t Flash_ReadData(uint32_t address, uint8_t *out, uint16_t len);
uint16_t Flash_ReadDataCont(uint32_t address, uint8_t *out, uint16_t len);
uint16_t Flash_Write(uint32_t address, uint8_t *in, uint16_t len);
uint16_t Flash_WriteData(uint32_t address, uint8_t *in, uint16_t len);
uint16_t Flash_WriteDataCont(uint32_t address, uint8_t *in, uint16_t len);
void Flashmem_print_status(void);
spi_flash_t *flash_get_info(void);
extern uint8_t spi_flash_pages64k;
bool FlashDetect(void);
#ifndef ARRAYLEN
# define ARRAYLEN(x) (sizeof(x)/sizeof((x)[0]))
#endif
#endif // #ifndef AS_BOOTROM
#endif // FLASHMEM_H_
+286
View File
@@ -0,0 +1,286 @@
#include "flashmem.h"
#include "pmflash.h"
#include "string.h"
#include "ticks_apis.h"
#ifndef AS_BOOTROM
#include "dbprint.h"
#endif // AS_BOOTROM
// default is 0, first set when FlashInit() call.
static uint32_t flashmem_spibaudrate = 0;
#ifndef AS_BOOTROM
// flash ids, first set when FlashInit() call.
static spi_flash_t spi_flash_data = {0};
// The capacity information calculated after the flash information is detected.
// This variable is referenced in many places, so it cannot be modified with static.
uint8_t spi_flash_pages64k = 4;
// Get spi baudrate
uint32_t Flash_GetSpiBaudrate(void) {
return flashmem_spibaudrate;
}
// Set spi baudrate, not updated immediately.
// The new baud rate will take effect the next time the FlashSetup function is executed.
// And depending on the platform, the baud rate that is finally set may not be your expected value.
// Maybe some platforms can only communicate at certain fixed baud rates.
void Flash_SetSpiBaudrate(uint32_t baudrate) {
flashmem_spibaudrate = baudrate;
Dbprintf("Spi Baudrate : %dMHz", flashmem_spibaudrate / 1000000);
}
// WARNING -- if callers are using a file system (such as SPIFFS),
// they should inform the file system of this change
// e.g., rdv40_spiffs_check()
bool Flash_WipeMemoryPage(uint8_t page) {
if (!FlashInit()) {
if (g_dbglevel > DBG_DEBUG) Dbprintf("Flash_WriteData init fail");
return false;
}
// Each block is 64Kb. One block erase takes 1s ( 1000ms )
Flash_WriteEnable();
Flash_Erase64k(page);
Flash_CheckBusy(BUSY_TIMEOUT);
FlashStop();
return true;
}
// Wipes flash memory completely, fills with 0xFF
bool Flash_WipeMemory(void) {
if (!FlashInit()) {
if (g_dbglevel > DBG_DEBUG) Dbprintf("Flash_WriteData init fail");
return false;
}
// Each block is 64Kb. Four blocks
// one block erase takes 1s ( 1000ms )
for (uint8_t i = 0; i < spi_flash_pages64k; i++) {
Flash_WriteEnable();
Flash_Erase64k(i);
Flash_CheckBusy(BUSY_TIMEOUT);
}
FlashStop();
return true;
}
// ReadData with spi initialization
uint16_t Flash_ReadData(uint32_t address, uint8_t *out, uint16_t len) {
if (!FlashInit()) return 0;
// check busy only
if (Flash_CheckBusy(BUSY_TIMEOUT)) return 0;
// function reused, length check inside.
len = Flash_ReadDataCont(address, out, len);
FlashStop();
return len;
}
// Write data can only program one page. A page has 256 bytes.
// if len > 256, it might wrap around and overwrite pos 0.
uint16_t Flash_WriteData(uint32_t address, uint8_t *in, uint16_t len) {
if (!FlashInit()) {
if (g_dbglevel > DBG_DEBUG) Dbprintf("Flash_WriteData init fail");
return 0;
}
Flash_CheckBusy(BUSY_TIMEOUT);
Flash_WriteEnable();
// function reused, len and addr check inside.
len = Flash_WriteDataCont(address, in, len);
FlashStop();
return len;
}
// assumes valid start 256 based 00 address
// Start writing flash from the specified location.
// Write FLASH_MEM_BLOCK_SIZE bytes at most each time. If the writing is nearly complete, write it as bytes_remaining bytes.
uint16_t Flash_Write(uint32_t address, uint8_t *in, uint16_t len) {
bool isok;
uint16_t res, bytes_sent = 0, bytes_remaining = len;
uint8_t buf[FLASH_MEM_BLOCK_SIZE];
while (bytes_remaining > 0) {
Flash_CheckBusy(BUSY_TIMEOUT);
Flash_WriteEnable();
uint32_t bytes_in_packet = MIN(FLASH_MEM_BLOCK_SIZE, bytes_remaining);
memcpy(buf, in + bytes_sent, bytes_in_packet);
res = Flash_WriteDataCont(address + bytes_sent, buf, bytes_in_packet);
bytes_remaining -= bytes_in_packet;
bytes_sent += bytes_in_packet;
isok = (res == bytes_in_packet);
if (!isok)
goto out;
}
out:
FlashStop();
return len;
}
void Flashmem_print_status(void) {
DbpString(_CYAN_("Flash memory"));
Dbprintf(" Baudrate................ " _GREEN_("%d MHz"), flashmem_spibaudrate / 1000000);
if (FlashInit() == false) {
DbpString(" Init.................... " _RED_("failed"));
return;
}
DbpString(" Init.................... " _GREEN_("ok"));
if (spi_flash_data.device_id > 0) {
Dbprintf(" Mfr ID / Dev ID......... " _YELLOW_("%02X / %02X"),
spi_flash_data.manufacturer_id,
spi_flash_data.device_id
);
}
if (spi_flash_data.jedec_id > 0) {
Dbprintf(" JEDEC Mfr ID / Dev ID... " _YELLOW_("%02X / %04X"),
spi_flash_data.manufacturer_id,
spi_flash_data.jedec_id
);
}
Dbprintf(" Memory size............. " _YELLOW_("%d Kb") " ( %d pages * 64k )", spi_flash_pages64k * 64, spi_flash_pages64k);
uint8_t uid[8] = {0, 0, 0, 0, 0, 0, 0, 0};
Flash_UniqueID(uid);
Dbprintf(" Unique ID (be).......... " _YELLOW_("0x%02X%02X%02X%02X%02X%02X%02X%02X"),
uid[0], uid[1], uid[2], uid[3],
uid[4], uid[5], uid[6], uid[7]
);
if (g_dbglevel > DBG_DEBUG) {
Dbprintf(" Unique ID (le).......... " _YELLOW_("0x%02X%02X%02X%02X%02X%02X%02X%02X"),
uid[7], uid[6], uid[5], uid[4],
uid[3], uid[2], uid[1], uid[0]
);
}
FlashStop();
}
spi_flash_t *flash_get_info(void) {
return &spi_flash_data;
}
bool FlashDetect(void) {
flash_device_type_t flash_data = {0};
bool ret = false;
// read using 0x9F (JEDEC)
if (Flash_ReadID(&flash_data, true)) {
spi_flash_data.manufacturer_id = flash_data.manufacturer_id;
spi_flash_data.jedec_id = (flash_data.device_id << 8) + flash_data.device_id2;
ret = true;
} else {
if (g_dbglevel > DBG_DEBUG) Dbprintf("Flash_ReadID failed reading JEDEC (0x9F)");
}
// read using 0x90 (Manufacturer / Device ID)
if (Flash_ReadID(&flash_data, false)) {
if (spi_flash_data.manufacturer_id == 0) {
spi_flash_data.manufacturer_id = flash_data.manufacturer_id;
}
spi_flash_data.device_id = flash_data.device_id;
ret = true;
} else {
if (g_dbglevel > DBG_DEBUG) Dbprintf("Flash_ReadID failed reading Mfr/Dev (0x90)");
}
// Check JEDEC data is valid, compare the reported device types and then calculate the number of pages
// It is covering the most (known) cases of devices but probably there are vendors with different data
// They will be handled when there is such cases
if (ret) {
if (spi_flash_data.jedec_id > 0 && spi_flash_data.jedec_id < 0xFFFF) {
if (((spi_flash_data.device_id + 1) & 0x0F) == (spi_flash_data.jedec_id & 0x000F)) {
spi_flash_pages64k = 1 << (spi_flash_data.jedec_id & 0x000F);
}
}
}
spi_flash_data.pages64k = spi_flash_pages64k;
return ret;
}
#endif // #ifndef AS_BOOTROM
// initialize
bool FlashInit(void) {
// set default baud rate from platform specific
if (!flashmem_spibaudrate) { // only set if current value == 0
flashmem_spibaudrate = Flash_DefaultBaudrate();
}
// Prioritize call the StartTicks, as the subsequent initialization process may rely on the counter
// to determine if there is a communication timeout.
StartTicks();
// If it is a QSPI communication interface, an attempt will be made to enable 4-wire communication at this stage.
// If the enable fails, it indicates that the chip does not support QSPI or has poor soldering.
// Tip: Some platform related steps only need to be executed once during initialization, which will be done in this function.
if (!FlashSetup(flashmem_spibaudrate)) {
StopTicks();
return false;
}
if (Flash_CheckBusy(BUSY_TIMEOUT)) {
StopTicks();
return false;
}
#ifndef AS_BOOTROM
if (spi_flash_data.manufacturer_id == 0) {
if (FlashDetect() == false) {
return false;
}
}
#endif // #ifndef AS_BOOTROM
return true;
}
// check flash write/erase working.
bool Flash_CheckBusy(uint32_t timeout) {
WaitUS(WINBOND_WRITE_DELAY);
StartCountUS();
uint32_t _time = GetCountUS();
uint8_t status;
do {
// Read status register failed!
if (!Flash_ReadStat1(&status)) {
// The chip may not be working properly, so it is meaningless to determine whether it is busy.
// We will return false first. If we consider returning true in the future, please modify it.
return false;
}
// Flash is busy for wipe/write
if (!(status & BUSY)) {
return false;
}
} while ((GetCountUS() - _time) < timeout);
if (timeout <= (GetCountUS() - _time)) {
return true;
}
return false;
}
+491
View File
@@ -0,0 +1,491 @@
#include <stdbool.h>
#include "ticks_apis.h"
#include "gpio_hw_at32.h"
#include "flashmem.h"
#include "flashmem_hw_at32.h"
#ifndef AS_BOOTROM
#include "dbprint.h"
#endif // AS_BOOTROM
static qspi_cmd_type w25q_cmd_config;
// Initialization of gpio related to qspi
static void qspi_gpio_config(void) {
gpio_init_type gpio_init_struct;
/* enable the gpio clock */
AT32_GPIO_PERIPH_CLKS_ENABLE(AT32_GPIO_PERIPH_QSPI_FLASH_CLK);
/* set default parameter */
gpio_default_para_init(&gpio_init_struct);
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_pull = GPIO_PULL_NONE;
/* configure the io0 gpio */
gpio_init_struct.gpio_pins = AT32_GPIO_QSPI_FLASH_IO0_PIN;
gpio_init(AT32_GPIO_QSPI_FLASH_IO0, &gpio_init_struct);
gpio_pin_mux_config(AT32_GPIO_QSPI_FLASH_IO0, AT32_GPIO_QSPI_FLASH_IO0_SOURCE, AT32_GPIO_QSPI_FLASH_IO0_MUX);
/* configure the io1 gpio */
gpio_init_struct.gpio_pins = AT32_GPIO_QSPI_FLASH_IO1_PIN;
gpio_init(AT32_GPIO_QSPI_FLASH_IO1, &gpio_init_struct);
gpio_pin_mux_config(AT32_GPIO_QSPI_FLASH_IO1, AT32_GPIO_QSPI_FLASH_IO1_SOURCE, AT32_GPIO_QSPI_FLASH_IO1_MUX);
/* configure the io2 gpio */
gpio_init_struct.gpio_pins = AT32_GPIO_QSPI_FLASH_IO2_PIN;
gpio_init(AT32_GPIO_QSPI_FLASH_IO2, &gpio_init_struct);
gpio_pin_mux_config(AT32_GPIO_QSPI_FLASH_IO2, AT32_GPIO_QSPI_FLASH_IO2_SOURCE, AT32_GPIO_QSPI_FLASH_IO2_MUX);
/* configure the io3 gpio */
gpio_init_struct.gpio_pins = AT32_GPIO_QSPI_FLASH_IO3_PIN;
gpio_init(AT32_GPIO_QSPI_FLASH_IO3, &gpio_init_struct);
gpio_pin_mux_config(AT32_GPIO_QSPI_FLASH_IO3, AT32_GPIO_QSPI_FLASH_IO3_SOURCE, AT32_GPIO_QSPI_FLASH_IO3_MUX);
/* configure the sck gpio */
gpio_init_struct.gpio_pins = AT32_GPIO_QSPI_FLASH_SCK_PIN;
gpio_init(AT32_GPIO_QSPI_FLASH_SCK, &gpio_init_struct);
gpio_pin_mux_config(AT32_GPIO_QSPI_FLASH_SCK, AT32_GPIO_QSPI_FLASH_SCK_SOURCE, AT32_GPIO_QSPI_FLASH_SCK_MUX);
/* configure the cs gpio */
gpio_init_struct.gpio_pins = AT32_GPIO_QSPI_FLASH_CS_PIN;
gpio_init(AT32_GPIO_QSPI_FLASH_CS, &gpio_init_struct);
gpio_pin_mux_config(AT32_GPIO_QSPI_FLASH_CS, AT32_GPIO_QSPI_FLASH_CS_SOURCE, AT32_GPIO_QSPI_FLASH_CS_MUX);
}
// Wait for flag setting within timeout, return false if timeout occurs.
static bool wait_flag_set(uint32_t flag, uint32_t timeoutMS) {
uint32_t start_tick = GET_TICKS;
while (qspi_flag_get(AT32_QSPI_FLASH, flag) == RESET) {
if (GetTicksDelta(start_tick) >= (timeoutMS * 1000 * 1.5)) {
// 100ms
return false; // timeout
}
}
return true;
}
// Wait command completed
static bool wait_cmd_completed(void) {
if (wait_flag_set(QSPI_CMDSTS_FLAG, 100)) {
qspi_flag_clear(AT32_QSPI_FLASH, QSPI_CMDSTS_FLAG);
return true;
}
return false;
}
// Read out data from qspi pio, no dma.
static bool read_wait_rx_done(uint8_t *out, uint32_t length) {
// wait rx ready for read out.
if (!wait_flag_set(QSPI_RXFIFORDY_FLAG, 200)) return false;
for (uint32_t i = 0; i < length; ++i) out[i] = qspi_byte_read(AT32_QSPI_FLASH);
return wait_cmd_completed();
}
// Get QSPI frequency division value
static qspi_clk_div_type from_baudrate_to_clk_div(uint32_t baudrate) {
uint32_t sck_candidate_value = 0;
uint8_t i;
// The clock of the QSPI of the AT32 is from the AHB clock, see datasheet: system architecture & crm
// so we need get current ahb clk speed, to calc div value.
crm_clocks_freq_type clk_freq_info;
crm_clocks_freq_get(&clk_freq_info);
// The ahb clock may be modified, so dynamic calculation is required!
uint32_t sck_lut[8]; // map div value to clk value.
sck_lut[QSPI_CLK_DIV_2] = clk_freq_info.ahb_freq / 2; // see datasheet 28.4.5
sck_lut[QSPI_CLK_DIV_4] = clk_freq_info.ahb_freq / 4;
sck_lut[QSPI_CLK_DIV_6] = clk_freq_info.ahb_freq / 6;
sck_lut[QSPI_CLK_DIV_8] = clk_freq_info.ahb_freq / 8;
sck_lut[QSPI_CLK_DIV_3] = clk_freq_info.ahb_freq / 3;
sck_lut[QSPI_CLK_DIV_5] = clk_freq_info.ahb_freq / 5;
sck_lut[QSPI_CLK_DIV_10] = clk_freq_info.ahb_freq / 10;
sck_lut[QSPI_CLK_DIV_12] = clk_freq_info.ahb_freq / 12;
// map search, step1, get a maximum value from sck_lut
for (i = 0; i < 8; ++i) {
// Take the maximum value downward, that is, if the incoming value is 1000000,
// take the maximum clk speed value in the mapping table that is smaller than the incoming value
if (baudrate <= sck_lut[i]) {
if (sck_lut[i] > sck_candidate_value) {
sck_candidate_value = sck_lut[i];
}
}
}
if (sck_candidate_value == 0) {
return QSPI_CLK_DIV_6; // return a default div value if no candidate clk found.
}
// map search, step2, from clk value to div value
for (i = 0; i < 8; ++i) {
if (sck_candidate_value == sck_lut[i]) {
return i; // 'i' is div value
}
}
return QSPI_CLK_DIV_6; // never come here...
}
// Default baud rate for spi of current platform.
uint32_t Flash_DefaultBaudrate(void) {
return 24000000; // TODO DXL 测试时,功能优先,速度先降下去。
}
// When returning false, it indicates that the communication with flash has timed out.
// Under normal circumstances, the return value should be within the range of U8.
static bool Flash_ReadStatReg(uint8_t reg, uint8_t *status) {
// config update for read status register
w25q_cmd_config.pe_mode_enable = FALSE;
w25q_cmd_config.pe_mode_operate_code = 0;
w25q_cmd_config.instruction_code = reg;
w25q_cmd_config.instruction_length = QSPI_CMD_INSLEN_1_BYTE;
w25q_cmd_config.address_code = 0;
w25q_cmd_config.address_length = QSPI_CMD_ADRLEN_0_BYTE; // no address
w25q_cmd_config.data_counter = 0;
w25q_cmd_config.second_dummy_cycle_num = 0;
w25q_cmd_config.operation_mode = QSPI_OPERATE_MODE_111;
w25q_cmd_config.read_status_config = QSPI_RSTSC_SW_ONCE; // self to read.
w25q_cmd_config.read_status_enable = TRUE;
w25q_cmd_config.write_data_enable = FALSE;
qspi_cmd_operation_kick(AT32_QSPI_FLASH, &w25q_cmd_config);
if (!wait_cmd_completed()) return false;
*status = AT32_QSPI_FLASH->rsts_bit.spists; // see rm doc: 28.4.10
return true;
}
// Read state register 1
bool Flash_ReadStat1(uint8_t *status) {
return Flash_ReadStatReg(READSTAT1, status);
}
// Read state register 1
bool Flash_ReadStat2(uint8_t *status) {
return Flash_ReadStatReg(READSTAT2, status);
}
#ifndef AS_BOOTROM
// Write data and wait finish, no dma.
static bool write_wait_tx_done(uint8_t *in, uint32_t length) {
// send data via qspi
for (uint32_t i = 0; i < length; ++i) {
if (!wait_flag_set(QSPI_TXFIFORDY_FLAG, 100)) return false;
qspi_byte_write(AT32_QSPI_FLASH, in[i]);
}
return wait_cmd_completed();
}
// Write status register by CMD(0x01 or 0x31)
// If CMD == 0x01, will write status1 & status2 register
// If CMD == 0x31, will write status2 register only
static bool Flash_WriteStatReg(uint8_t reg, uint8_t *in, uint8_t length) {
w25q_cmd_config.pe_mode_enable = FALSE;
w25q_cmd_config.pe_mode_operate_code = 0;
w25q_cmd_config.instruction_code = reg;
w25q_cmd_config.instruction_length = QSPI_CMD_INSLEN_1_BYTE;
w25q_cmd_config.address_code = 0;
w25q_cmd_config.address_length = QSPI_CMD_ADRLEN_0_BYTE;
w25q_cmd_config.data_counter = length;
w25q_cmd_config.second_dummy_cycle_num = 0;
w25q_cmd_config.operation_mode = QSPI_OPERATE_MODE_111;
w25q_cmd_config.read_status_config = QSPI_RSTSC_HW_AUTO;
w25q_cmd_config.read_status_enable = FALSE;
w25q_cmd_config.write_data_enable = TRUE;
qspi_cmd_operation_kick(AT32_QSPI_FLASH, &w25q_cmd_config);
return write_wait_tx_done(in, length);
}
// Flash quad line communication enable
// Some chips already enable QE bit default. such as: W25Q64FVSSIQ, the Q suffix is QE bit enable default.
static bool Flash_QE_Enable(void) {
uint8_t status[2];
if (!Flash_ReadStat1(&status[0])) return false;
if (!Flash_ReadStat2(&status[1])) return false;
// Check if the QE bit has been enabled. On some winbond chips with Q as the suffix, this bit defaults to 1.
if ((status[1] & 0x02) != 0) return true;
// Set 'Quad Enable (QE)'(S9 bit) to 1.
status[1] |= 1 << 1;
// QE bit is a non-volatile Status Register bits, a standard Write Enable (06h) instruction must previously have
// been executed for the device to accept the Write Status Register instruction (Status Register bit WEL must equal 1).
if (!Flash_WriteEnable()) return false;
// Some new chips support 31H instruction to set the status register 2,
// but we need to use the 01H standard instruction to set the status register 2 for compatibility.
if (!Flash_WriteStatReg(WRITESTAT, status, 2)) return false;
// The BUSY bit is a 1 during the Write Status Register cycle
// and a 0 when the cycle is finished and ready to accept other instructions again. After the Write Status
// Register cycle has finished, the Write Enable Latch (WEL) bit in the Status Register will be cleared to 0.
return !Flash_CheckBusy(BUSY_TIMEOUT); // Waiting for write done.
}
#endif
// Flash spi & gpio setup
bool FlashSetup(uint32_t baudrate) {
qspi_gpio_config();
// enable the qspi clock
crm_periph_clock_enable(AT32_CRM_QSPI_FLASH_CLK, TRUE);
// switch to cmd port
qspi_xip_enable(AT32_QSPI_FLASH, FALSE);
// set clk
qspi_clk_division_set(AT32_QSPI_FLASH, from_baudrate_to_clk_div(baudrate));
// set sck idle mode 0
qspi_sck_mode_set(AT32_QSPI_FLASH, QSPI_SCK_MODE_0);
// set wip in bit 0
qspi_busy_config(AT32_QSPI_FLASH, QSPI_BUSY_OFFSET_0);
// disable encrypt
qspi_encryption_enable(AT32_QSPI_FLASH, FALSE);
// enable auto ispc
qspi_auto_ispc_enable(AT32_QSPI_FLASH);
#ifndef AS_BOOTROM
return Flash_QE_Enable();
#else
return true;
#endif
}
// Flash spi deinit
void FlashStop(void) {
// Do not turn off the clock of GPIO. If you really want to turn off the clock,
// you must ensure that GPIO is not currently used in other codes
// crm_periph_clock_enable(CRM_GPIO?_PERIPH_CLOCK, TRUE);
// crm_periph_clock_enable(CRM_GPIO?_PERIPH_CLOCK, TRUE);
// crm_periph_clock_enable(CRM_GPIO?_PERIPH_CLOCK, TRUE);
// disable qspi
crm_periph_clock_enable(AT32_CRM_QSPI_FLASH_CLK, FALSE);
qspi_interrupt_enable(AT32_QSPI_FLASH, FALSE);
}
// Read unique id for chip.
bool Flash_UniqueID(uint8_t *uid) {
if (Flash_CheckBusy(BUSY_TIMEOUT)) return false;
w25q_cmd_config.pe_mode_enable = FALSE;
w25q_cmd_config.pe_mode_operate_code = 0;
w25q_cmd_config.instruction_code = UNIQUE_ID;
w25q_cmd_config.instruction_length = QSPI_CMD_INSLEN_1_BYTE;
w25q_cmd_config.address_code = 0;
// see rm doc 28.4.2, if address_length = 0, second_dummy_cycle_num will no working.
// so we need use addr to create dummy clk
w25q_cmd_config.address_length = 4;
w25q_cmd_config.data_counter = 8; // 64bit unique id
w25q_cmd_config.second_dummy_cycle_num = 0; // dummy clk
w25q_cmd_config.operation_mode = QSPI_OPERATE_MODE_111;
w25q_cmd_config.read_status_config = QSPI_RSTSC_HW_AUTO;
w25q_cmd_config.read_status_enable = FALSE;
w25q_cmd_config.write_data_enable = FALSE;
qspi_cmd_operation_kick(AT32_QSPI_FLASH, &w25q_cmd_config);
read_wait_rx_done(uid, 8);
return true;
}
#ifndef AS_BOOTROM
// Read JEDEC id
static void read_jedecid(uint8_t *jedecid) {
w25q_cmd_config.pe_mode_enable = FALSE;
w25q_cmd_config.pe_mode_operate_code = 0;
w25q_cmd_config.instruction_code = JEDECID;
w25q_cmd_config.instruction_length = QSPI_CMD_INSLEN_1_BYTE;
w25q_cmd_config.address_code = 0;
w25q_cmd_config.address_length = 0;
w25q_cmd_config.data_counter = 3; // 24bit JEDECID info
w25q_cmd_config.second_dummy_cycle_num = 0; // no dummy clk
w25q_cmd_config.operation_mode = QSPI_OPERATE_MODE_111;
w25q_cmd_config.read_status_config = QSPI_RSTSC_HW_AUTO;
w25q_cmd_config.read_status_enable = FALSE;
w25q_cmd_config.write_data_enable = FALSE;
qspi_cmd_operation_kick(AT32_QSPI_FLASH, &w25q_cmd_config);
read_wait_rx_done(jedecid, 3);
}
// Read Manufacturer / Device ID
// the difference between this function and the read_jedecid function is that the capacity information is missing
// so only 2byte device_id readout.
static void read_deviceid(uint8_t *device_id) {
w25q_cmd_config.pe_mode_enable = FALSE;
w25q_cmd_config.pe_mode_operate_code = 0;
w25q_cmd_config.instruction_code = ID;
w25q_cmd_config.instruction_length = QSPI_CMD_INSLEN_1_BYTE;
w25q_cmd_config.address_code = 0;
w25q_cmd_config.address_length = 3; // for 3 byte dummy clk
w25q_cmd_config.data_counter = 2; // 16bit device id
w25q_cmd_config.second_dummy_cycle_num = 0; // no dummy clk
w25q_cmd_config.operation_mode = QSPI_OPERATE_MODE_111;
w25q_cmd_config.read_status_config = QSPI_RSTSC_HW_AUTO;
w25q_cmd_config.read_status_enable = FALSE;
w25q_cmd_config.write_data_enable = FALSE;
qspi_cmd_operation_kick(AT32_QSPI_FLASH, &w25q_cmd_config);
read_wait_rx_done(device_id, 2);
}
// Read ID out
bool Flash_ReadID(flash_device_type_t *result, bool read_jedec) {
if (Flash_CheckBusy(BUSY_TIMEOUT)) return false;
if (read_jedec) {
uint8_t juid[3];
read_jedecid(juid);
result->manufacturer_id = juid[0];
result->device_id = juid[1];
result->device_id2 = juid[2];
} else {
uint8_t duid[2];
read_deviceid(duid);
result->manufacturer_id = duid[0];
result->device_id = duid[1];
}
return true;
}
uint16_t Flash_ReadDataCont(uint32_t address, uint8_t *out, uint16_t len) {
// length should never be zero
if (!len) return 0;
// cmd
w25q_cmd_config.pe_mode_enable = FALSE;
w25q_cmd_config.pe_mode_operate_code = 0;
w25q_cmd_config.instruction_code = FASTREAD_QO;
w25q_cmd_config.instruction_length = QSPI_CMD_INSLEN_1_BYTE;
// address
w25q_cmd_config.address_code = address;
w25q_cmd_config.address_length = QSPI_CMD_ADRLEN_3_BYTE; // 24bit address
// dummy clk for qspi fast read output
w25q_cmd_config.second_dummy_cycle_num = 8;
// more...
w25q_cmd_config.data_counter = len;
w25q_cmd_config.operation_mode = QSPI_OPERATE_MODE_114;
w25q_cmd_config.read_status_config = QSPI_RSTSC_SW_ONCE;
w25q_cmd_config.read_status_enable = FALSE;
w25q_cmd_config.write_data_enable = FALSE;
qspi_cmd_operation_kick(AT32_QSPI_FLASH, &w25q_cmd_config);
// readout from flash
read_wait_rx_done(out, len);
return len;
}
bool Flash_WriteEnable(void) {
w25q_cmd_config.pe_mode_enable = FALSE;
w25q_cmd_config.pe_mode_operate_code = 0;
w25q_cmd_config.instruction_code = WRITEENABLE;
w25q_cmd_config.instruction_length = QSPI_CMD_INSLEN_1_BYTE;
w25q_cmd_config.address_code = 0;
w25q_cmd_config.address_length = 0;
w25q_cmd_config.data_counter = 0;
w25q_cmd_config.second_dummy_cycle_num = 0;
w25q_cmd_config.operation_mode = QSPI_OPERATE_MODE_111;
w25q_cmd_config.read_status_config = QSPI_RSTSC_HW_AUTO;
w25q_cmd_config.read_status_enable = FALSE;
w25q_cmd_config.write_data_enable = TRUE;
qspi_cmd_operation_kick(AT32_QSPI_FLASH, &w25q_cmd_config);
if (!wait_cmd_completed()) return false;
if (g_dbglevel > 3) Dbprintf("Flash Write enabled");
return true;
}
uint16_t Flash_WriteDataCont(uint32_t address, uint8_t *in, uint16_t len) {
if (!len)
return 0;
if (((address & 0xFF) + len) > 256) {
Dbprintf("Flash_WriteDataCont 256 fail [ 0x%02x ] [ %u ]", (address & 0xFF) + len, len);
return 0;
}
if (((address >> 16) & 0xFF) > spi_flash_pages64k) {
Dbprintf("Flash_WriteDataCont, block out-of-range %02x > %02x", (address >> 16) & 0xFF, spi_flash_pages64k);
return 0;
}
w25q_cmd_config.pe_mode_enable = FALSE;
w25q_cmd_config.pe_mode_operate_code = 0;
w25q_cmd_config.instruction_code = PAGEPROG;
w25q_cmd_config.instruction_length = QSPI_CMD_INSLEN_1_BYTE;
w25q_cmd_config.address_code = address;
w25q_cmd_config.address_length = QSPI_CMD_ADRLEN_3_BYTE;
w25q_cmd_config.data_counter = len;
w25q_cmd_config.second_dummy_cycle_num = 0;
/*
* WHY using 111 single line mode?
*
* The Quad Page Program can improve performance for PROM Programmer and applications that have slow clock speeds <5MHz.
* Systems with faster clock speed will not realize much benefit for the Quad Page Program instruction since
* the inherent page program time is much greater than the time it take to clock-in the data.
*/
w25q_cmd_config.operation_mode = QSPI_OPERATE_MODE_111;
w25q_cmd_config.read_status_config = QSPI_RSTSC_HW_AUTO;
w25q_cmd_config.read_status_enable = FALSE;
w25q_cmd_config.write_data_enable = TRUE;
qspi_cmd_operation_kick(AT32_QSPI_FLASH, &w25q_cmd_config);
write_wait_tx_done(in, len);
return len;
}
bool Flash_Erase4k(uint8_t block, uint8_t sector) {
if (block > spi_flash_pages64k || sector > MAX_SECTORS) return false;
w25q_cmd_config.pe_mode_enable = FALSE;
w25q_cmd_config.pe_mode_operate_code = 0;
w25q_cmd_config.instruction_code = SECTORERASE;
w25q_cmd_config.instruction_length = QSPI_CMD_INSLEN_1_BYTE;
w25q_cmd_config.address_code = block << 16 | (sector << 4) << 8;
w25q_cmd_config.address_length = QSPI_CMD_ADRLEN_3_BYTE;
w25q_cmd_config.data_counter = 0;
w25q_cmd_config.second_dummy_cycle_num = 0;
w25q_cmd_config.operation_mode = QSPI_OPERATE_MODE_111;
w25q_cmd_config.read_status_config = QSPI_RSTSC_HW_AUTO;
w25q_cmd_config.read_status_enable = FALSE;
w25q_cmd_config.write_data_enable = TRUE;
qspi_cmd_operation_kick(AT32_QSPI_FLASH, &w25q_cmd_config);
return wait_cmd_completed();
}
bool Flash_Erase64k(uint8_t block) {
if (block > spi_flash_pages64k) return false;
w25q_cmd_config.pe_mode_enable = FALSE;
w25q_cmd_config.pe_mode_operate_code = 0;
w25q_cmd_config.instruction_code = BLOCK64ERASE;
w25q_cmd_config.instruction_length = QSPI_CMD_INSLEN_1_BYTE;
w25q_cmd_config.address_code = block;
w25q_cmd_config.address_length = QSPI_CMD_ADRLEN_3_BYTE;
w25q_cmd_config.data_counter = 0;
w25q_cmd_config.second_dummy_cycle_num = 0;
w25q_cmd_config.operation_mode = QSPI_OPERATE_MODE_111;
w25q_cmd_config.read_status_config = QSPI_RSTSC_HW_AUTO;
w25q_cmd_config.read_status_enable = FALSE;
w25q_cmd_config.write_data_enable = TRUE;
qspi_cmd_operation_kick(AT32_QSPI_FLASH, &w25q_cmd_config);
return wait_cmd_completed();
}
#endif // #ifndef AS_BOOTROM
+14
View File
@@ -0,0 +1,14 @@
//
// Created by dxl on 2026/2/7.
//
#ifndef FLASHMEM_HW_AT32_H
#define FLASHMEM_HW_AT32_H
#include "at32f435_437_crm.h"
#include "at32f435_437_qspi.h"
#define AT32_CRM_QSPI_FLASH_CLK CRM_QSPI1_PERIPH_CLOCK
#define AT32_QSPI_FLASH QSPI1
#endif // FLASHMEM_HW_AT32_H
+374
View File
@@ -0,0 +1,374 @@
//-----------------------------------------------------------------------------
// Borrowed initially from Arduino SPIFlash Library v.2.5.0
// Copyright (C) 2015 by Prajwal Bhattaram.
// 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.
//-----------------------------------------------------------------------------
#include "pmflash.h"
#include "flashmem_hw_at91.h"
#include "proxmark3_arm.h"
#include "ticks_apis.h"
#ifndef AS_BOOTROM
#include "dbprint.h"
#endif // AS_BOOTROM
// send one byte over SPI
static uint16_t FlashSendByte(uint32_t data) {
// wait until SPI is ready for transfer
//if you are checking for incoming data returned then the TXEMPTY flag is redundant
//while ((AT91C_BASE_SPI->SPI_SR & AT91C_SPI_TXEMPTY) == 0) {};
// send the data
AT91C_BASE_SPI->SPI_TDR = data;
//while ((AT91C_BASE_SPI->SPI_SR & AT91C_SPI_TDRE) == 0){};
// wait receive transfer is complete
while ((AT91C_BASE_SPI->SPI_SR & AT91C_SPI_RDRF) == 0) {};
// reading incoming data
return ((AT91C_BASE_SPI->SPI_RDR) & 0xFFFF);
}
// send last byte over SPI
static uint16_t FlashSendLastByte(uint32_t data) {
return FlashSendByte(data | AT91C_SPI_LASTXFER);
}
#ifndef AS_BOOTROM
// send address after R/W cmd.
static void Flash_TransferAddress(uint32_t address) {
FlashSendByte((address >> 16) & 0xFF);
FlashSendByte((address >> 8) & 0xFF);
FlashSendByte((address >> 0) & 0xFF);
}
// read ID out
bool Flash_ReadID(flash_device_type_t *result, bool read_jedec) {
if (Flash_CheckBusy(BUSY_TIMEOUT)) return false;
if (read_jedec) {
// 0x9F JEDEC
FlashSendByte(JEDECID);
result->manufacturer_id = (FlashSendByte(0xFF) & 0xFF);
result->device_id = (FlashSendByte(0xFF) & 0xFF);
result->device_id2 = (FlashSendLastByte(0xFF) & 0xFF);
} else {
// 0x90 Manufacture ID / device ID
FlashSendByte(ID);
FlashSendByte(0x00);
FlashSendByte(0x00);
FlashSendByte(0x00);
result->manufacturer_id = (FlashSendByte(0xFF) & 0xFF);
result->device_id = (FlashSendLastByte(0xFF) & 0xFF);
}
return true;
}
/* This ensures we can ReadData without having to cycle through initialization every time */
uint16_t Flash_ReadDataCont(uint32_t address, uint8_t *out, uint16_t len) {
// length should never be zero
if (!len) return 0;
uint8_t cmd = (FASTFLASH) ? FASTREAD : READDATA;
FlashSendByte(cmd);
Flash_TransferAddress(address);
if (FASTFLASH) {
FlashSendByte(DUMMYBYTE);
}
uint16_t i = 0;
for (; i < (len - 1); i++) {
out[i] = (FlashSendByte(0xFF) & 0xFF);
}
out[i] = (FlashSendLastByte(0xFF) & 0xFF);
return len;
}
// enable the flash write
bool Flash_WriteEnable(void) {
FlashSendLastByte(WRITEENABLE);
if (g_dbglevel > 3) Dbprintf("Flash Write enabled");
return true;
}
// length should never be zero
// Max 256 bytes write
// out-of-range
uint16_t Flash_WriteDataCont(uint32_t address, uint8_t *in, uint16_t len) {
if (!len)
return 0;
if (((address & 0xFF) + len) > 256) {
Dbprintf("Flash_WriteDataCont 256 fail [ 0x%02x ] [ %u ]", (address & 0xFF) + len, len);
return 0;
}
if (((address >> 16) & 0xFF) > spi_flash_pages64k) {
Dbprintf("Flash_WriteDataCont, block out-of-range %02x > %02x", (address >> 16) & 0xFF, spi_flash_pages64k);
return 0;
}
FlashSendByte(PAGEPROG);
Flash_TransferAddress(address);
uint16_t i = 0;
for (; i < (len - 1); i++)
FlashSendByte(in[i]);
FlashSendLastByte(in[i]);
return len;
}
// erase 4K at one time
// execution time: 0.8ms / 800us
bool Flash_Erase4k(uint8_t block, uint8_t sector) {
if (block > spi_flash_pages64k || sector > MAX_SECTORS) return false;
FlashSendByte(SECTORERASE);
FlashSendByte(block);
FlashSendByte(sector << 4);
FlashSendLastByte(00);
return true;
}
/*
// erase 32K at one time
// execution time: 0,3s / 300ms
bool Flash_Erase32k(uint32_t address) {
if (address & (32*1024 - 1)) {
if ( g_dbglevel > 1 ) Dbprintf("Flash_Erase32k : Address is not align at 4096");
return false;
}
FlashSendByte(BLOCK32ERASE);
FlashSendByte((address >> 16) & 0xFF);
FlashSendByte((address >> 8) & 0xFF);
FlashSendLastByte((address >> 0) & 0xFF);
return true;
}
*/
// erase 64k at one time
// since a block is 64kb, and there is four blocks.
// we only need block number, as MSB
// execution time: 1s / 1000ms
// 0x00 00 00 -- 0x 00 FF FF == block 0
// 0x01 00 00 -- 0x 01 FF FF == block 1
// 0x02 00 00 -- 0x 02 FF FF == block 2
// 0x03 00 00 -- 0x 03 FF FF == block 3
bool Flash_Erase64k(uint8_t block) {
if (block > spi_flash_pages64k) return false;
FlashSendByte(BLOCK64ERASE);
FlashSendByte(block);
FlashSendByte(0x00);
FlashSendLastByte(0x00);
return true;
}
/*
// Erase chip
void Flash_EraseChip(void) {
FlashSendLastByte(CHIPERASE);
}
*/
#endif // #ifndef AS_BOOTROM
// default baud rate for spi of current platform.
uint32_t Flash_DefaultBaudrate(void) {
return FLASH_BAUD;
}
// flash spi deinit
void FlashStop(void) {
//Bof
//* 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;
#ifndef AS_BOOTROM
if (g_dbglevel > 3) Dbprintf("FlashStop");
#endif // AS_BOOTROM
StopTicks();
}
// flash spi&gpio setup
bool FlashSetup(uint32_t baudrate) {
//WDT_DISABLE
AT91C_BASE_WDTC->WDTC_WDMR = AT91C_WDTC_WDDIS;
// PA10 -> SPI_NCS2 chip select (FLASHMEM)
// 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 | GPIO_NCS2);
// Pull-up Enable
AT91C_BASE_PIOA->PIO_PPUER |= (GPIO_NCS0 | GPIO_MISO | GPIO_MOSI | GPIO_SPCK | GPIO_NCS2);
// 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);
//reset spi needs double SWRST, see atmel's errata on this case
AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SWRST;
AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SWRST;
// Enable SPI
AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIEN;
// NPCS2 Mode 0
AT91C_BASE_SPI->SPI_MR =
(0 << 24) | // Delay between chip selects = DYLBCS/MCK BUT:
// If DLYBCS is less than or equal to six, six MCK periods
// will be inserted by default.
SPI_PCS(SPI_CSR_NUM) | // Peripheral Chip Select (selects SPI_NCS2 or PA10)
(0 << 7) | // Disable LLB (1=MOSI2MISO test mode)
(1 << 4) | // Disable ModeFault Protection
(0 << 3) | // makes spi operate at MCK (1 is MCK/2)
(0 << 2) | // Chip selects connected directly to peripheral
AT91C_SPI_PS_FIXED | // Fixed Peripheral Select
AT91C_SPI_MSTR; // Master Mode
uint8_t csaat = 1;
uint32_t dlybct = 0;
uint8_t ncpha = 1;
uint8_t cpol = 0;
if (baudrate > FLASH_MINFAST) {
baudrate = FLASH_FASTBAUD;
//csaat = 0;
dlybct = 1500;
ncpha = 0;
cpol = 0;
}
AT91C_BASE_SPI->SPI_CSR[2] =
SPI_DLYBCT(dlybct, MCK) | // Delay between Consecutive Transfers (32 MCK periods)
SPI_DLYBS(0, MCK) | // Delay Beforce SPCK CLock
SPI_SCBR(baudrate, MCK) | // SPI Baudrate Selection
AT91C_SPI_BITS_8 | // Bits per Transfer (8 bits)
//AT91C_SPI_CSAAT | // Chip Select inactive after transfer
// 40.4.6.2 SPI: Bad tx_ready Behavior when CSAAT = 1 and SCBR = 1
// If the SPI is programmed with CSAAT = 1, SCBR(baudrate) = 1 and two transfers are performed consecutively on
// the same slave with an IDLE state between them, the tx_ready signal does not rise after the second data has been
// transferred in the shifter. This can imply for example, that the second data is sent twice.
// COLIN :: For now we STILL use CSAAT=1 to avoid having to (de)assert NPCS manually via PIO lines and we deal with delay
(csaat << 3) |
/* Spi modes:
Mode CPOL CPHA NCPHA
0 0 0 1 clock normally low read on rising edge
1 0 1 0 clock normally low read on falling edge
2 1 0 1 clock normally high read on falling edge
3 1 1 0 clock normally high read on rising edge
However, page 512 of the AT91SAM7Sx datasheet say "Note that in SPI
master mode the ATSAM7S512/256/128/64/321/32 does not sample the data
(MISO) on the opposite edge where data clocks out (MOSI) but the same
edge is used as shown in Figure 36-3 and Figure 36-4." Figure 36-3
shows that CPOL=NCPHA=0 or CPOL=NCPHA=1 samples on the rising edge and
that the data changes sometime after the rising edge (about 2 ns). To
be consistent with normal SPI operation, it is probably safe to say
that the data changes on the falling edge and should be sampled on the
rising edge. Therefore, it appears that NCPHA should be treated the
same as CPHA. Thus:
Mode CPOL CPHA NCPHA
0 0 0 0 clock normally low read on rising edge
1 0 1 1 clock normally low read on falling edge
2 1 0 0 clock normally high read on falling edge
3 1 1 1 clock normally high read on rising edge
Update: for 24MHz, writing is more stable with ncpha=1, else bitflips occur.
*/
(ncpha << 1) | // Clock Phase data captured on leading edge, changes on following edge
(cpol << 0); // Clock Polarity inactive state is logic 0
// read first, empty buffer
if (AT91C_BASE_SPI->SPI_RDR == 0) {};
return true;
}
// read unique id for chip.
bool Flash_UniqueID(uint8_t *uid) {
if (Flash_CheckBusy(BUSY_TIMEOUT)) false;
// reading unique serial number
FlashSendByte(UNIQUE_ID);
FlashSendByte(0xFF);
FlashSendByte(0xFF);
FlashSendByte(0xFF);
FlashSendByte(0xFF);
uid[7] = (FlashSendByte(0xFF) & 0xFF);
uid[6] = (FlashSendByte(0xFF) & 0xFF);
uid[5] = (FlashSendByte(0xFF) & 0xFF);
uid[4] = (FlashSendByte(0xFF) & 0xFF);
uid[3] = (FlashSendByte(0xFF) & 0xFF);
uid[2] = (FlashSendByte(0xFF) & 0xFF);
uid[1] = (FlashSendByte(0xFF) & 0xFF);
uid[0] = (FlashSendLastByte(0xFF) & 0xFF);
return true;
}
// read state register 1
bool Flash_ReadStat1(uint8_t *status) {
if (!status) return false;
FlashSendByte(READSTAT1);
*status = FlashSendLastByte(0xFF);
return true;
}
// read state register 2
bool Flash_ReadStat2(uint8_t *status) {
if (!status) return false;
FlashSendByte(READSTAT2);
*status = FlashSendLastByte(0xFF);
return true;
}
+29
View File
@@ -0,0 +1,29 @@
//
// Created by dxl on 2026/2/7.
//
#ifndef FLASHMEM_HW_AT91_H
#define FLASHMEM_HW_AT91_H
#include "flashmem.h"
#include "proxmark3_arm.h"
/* here: use NCPS2 @ PA10: */
#define SPI_CSR_NUM 2
#define SPI_PCS(npcs) ((~(1 << (npcs)) & 0xF) << 16)
/// Calculates the value of the CSR SCBR field given the baudrate and MCK.
#define SPI_SCBR(baudrate, masterClock) ((uint32_t) ((masterClock) / (baudrate)) << 8)
/// Calculates the value of the CSR DLYBS field given the desired delay (in ns)
#define SPI_DLYBS(delay, masterClock) ((uint32_t) ((((masterClock) / 1000000) * (delay)) / 1000) << 16)
/// Calculates the value of the CSR DLYBCT field given the desired delay (in ns)
#define SPI_DLYBCT(delay, masterClock) ((uint32_t) ((((masterClock) / 1000000) * (delay)) / 32000) << 24)
// com speed
#define FLASH_MINFAST 24000000
#define FLASH_BAUD (MCK / 2)
#define FLASH_FASTBAUD MCK
#define FLASH_MINBAUD FLASH_FASTBAUD
#define FASTFLASH (Flash_GetSpiBaudrate() > FLASH_MINFAST)
#endif // FLASHMEM_HW_AT91_H