feat: H7 support (#5738)
Co-authored-by: Richard Li <richard.li@ces.hk> Co-authored-by: philmoz <phil.a.mitchell@gmail.com> Co-authored-by: raphaelcoeffic <1050031+raphaelcoeffic@users.noreply.github.com>
This commit is contained in:
co-authored by
Richard Li
philmoz
raphaelcoeffic
parent
5a15ffb017
commit
3d25c2a0f8
@@ -25,6 +25,7 @@ if(NOT NATIVE_BUILD)
|
||||
${STM32USB_DIR}/Core/Src/usbd_ctlreq.c
|
||||
${STM32USB_DIR}/Core/Src/usbd_ioreq.c
|
||||
${STM32USB_DIR}/Class/CDC/Src/usbd_cdc.c
|
||||
${STM32USB_DIR}/Class/DFU/Src/usbd_dfu.c
|
||||
${STM32USB_DIR}/Class/MSC/Src/usbd_msc_data.c
|
||||
${STM32USB_DIR}/Class/MSC/Src/usbd_msc_scsi.c
|
||||
${STM32USB_DIR}/Class/MSC/Src/usbd_msc_bot.c
|
||||
@@ -34,9 +35,14 @@ if(NOT NATIVE_BUILD)
|
||||
target_include_directories(stm32usb PRIVATE
|
||||
${STM32USB_DIR}/Core/Inc
|
||||
${STM32USB_DIR}/Class/CDC/Inc
|
||||
${STM32USB_DIR}/Class/DFU/Inc
|
||||
${STM32USB_DIR}/Class/HID/Inc
|
||||
${STM32USB_DIR}/Class/MSC/Inc)
|
||||
|
||||
target_include_directories(board_bl PRIVATE
|
||||
${STM32USB_DIR}/Core/Inc
|
||||
${STM32USB_DIR}/Class/DFU/Inc)
|
||||
|
||||
set(FIRMWARE_SRC ${FIRMWARE_SRC} $<TARGET_OBJECTS:stm32usb>)
|
||||
set(BOOTLOADER_SRC ${BOOTLOADER_SRC} $<TARGET_OBJECTS:stm32usb>)
|
||||
|
||||
@@ -44,14 +50,15 @@ if(NOT NATIVE_BUILD)
|
||||
add_library(stm32usb_device_bl OBJECT EXCLUDE_FROM_ALL
|
||||
${STM32_DRIVER_DIR}/usbd_conf.c
|
||||
${STM32_DRIVER_DIR}/usbd_desc.c
|
||||
${STM32_DRIVER_DIR}/usb_driver.cpp
|
||||
${STM32_DRIVER_DIR}/usbd_storage_msd.cpp
|
||||
${STM32_DRIVER_DIR}/usb_driver.cpp
|
||||
)
|
||||
|
||||
target_compile_options(stm32usb_device_bl PRIVATE -DBOOT)
|
||||
target_include_directories(stm32usb_device_bl PRIVATE
|
||||
${STM32USB_DIR}/Core/Inc
|
||||
${STM32USB_DIR}/Class/CDC/Inc
|
||||
${STM32USB_DIR}/Class/DFU/Inc
|
||||
${STM32USB_DIR}/Class/HID/Inc
|
||||
${STM32USB_DIR}/Class/MSC/Inc)
|
||||
|
||||
@@ -74,6 +81,7 @@ if(NOT NATIVE_BUILD)
|
||||
target_include_directories(stm32usb_device_fw PRIVATE
|
||||
${STM32USB_DIR}/Core/Inc
|
||||
${STM32USB_DIR}/Class/CDC/Inc
|
||||
${STM32USB_DIR}/Class/DFU/Inc
|
||||
${STM32USB_DIR}/Class/HID/Inc
|
||||
${STM32USB_DIR}/Class/MSC/Inc)
|
||||
|
||||
@@ -146,3 +154,11 @@ set(FIRMWARE_SRC ${FIRMWARE_SRC} syscalls.c)
|
||||
|
||||
set(TARGET_SRC ${TARGET_SRC} ../common/arm/stm32/cpu_id.cpp)
|
||||
set(SRC ${SRC} io/bootloader_flash.cpp)
|
||||
|
||||
if(FIRMWARE_FORMAT_UF2)
|
||||
add_definitions(-DFIRMWARE_FORMAT_UF2)
|
||||
set(SRC ${SRC}
|
||||
io/uf2.cpp
|
||||
io/uf2_flash.cpp
|
||||
)
|
||||
endif()
|
||||
|
||||
@@ -83,3 +83,13 @@ uint32_t abnormalRebootGetCause()
|
||||
_init_reboot_cause();
|
||||
return _reboot_cause;
|
||||
}
|
||||
|
||||
uint32_t abnormalRebootGetCmd()
|
||||
{
|
||||
return _reboot_cmd;
|
||||
}
|
||||
|
||||
void abnormalRebootResetCmd()
|
||||
{
|
||||
_reboot_cmd = 0;
|
||||
}
|
||||
|
||||
@@ -24,9 +24,11 @@
|
||||
#include <inttypes.h>
|
||||
#include "cpu_id.h"
|
||||
|
||||
#if !defined(SIMU) && !defined(BACKUP)
|
||||
#if !defined(SIMU) && !defined(BACKUP) && !defined(YAML_GENERATOR)
|
||||
#if defined(STM32H7)
|
||||
#include "stm32h7xx.h"
|
||||
#elif defined(STM32H7RS)
|
||||
#include "stm32h7rsxx.h"
|
||||
#elif defined(STM32F4)
|
||||
#include "stm32f4xx.h"
|
||||
#elif defined(STM32F2)
|
||||
|
||||
@@ -12,9 +12,22 @@ set(BOOTLOADER_SRC ${BOOTLOADER_SRC}
|
||||
${RADIO_SRC_DIR}/syscalls.c
|
||||
${RADIO_SRC_DIR}/crc.cpp
|
||||
boot.cpp
|
||||
bin_files.cpp
|
||||
)
|
||||
|
||||
if(FIRMWARE_FORMAT_UF2)
|
||||
set(BOOTLOADER_SRC ${BOOTLOADER_SRC}
|
||||
boot_uf2.cpp
|
||||
boot_dfu.cpp
|
||||
${RADIO_SRC_DIR}/io/uf2.cpp
|
||||
${RADIO_SRC_DIR}/drivers/uf2_ghostfat.cpp
|
||||
)
|
||||
else()
|
||||
set(BOOTLOADER_SRC ${BOOTLOADER_SRC}
|
||||
boot_menu.cpp
|
||||
bin_fw_files.cpp
|
||||
)
|
||||
endif()
|
||||
|
||||
if(DEBUG_SEGGER_RTT)
|
||||
set(BOOTLOADER_SRC ${BOOTLOADER_SRC}
|
||||
${RADIO_SRC_DIR}/thirdparty/Segger_RTT/RTT/SEGGER_RTT.c
|
||||
|
||||
+121
-30
@@ -25,19 +25,52 @@
|
||||
#include "boot.h"
|
||||
#include "board.h"
|
||||
#include "sdcard.h"
|
||||
#include "bin_files.h"
|
||||
#include "firmware_files.h"
|
||||
#include "fw_version.h"
|
||||
#include "strhelpers.h"
|
||||
#include "hal/storage.h"
|
||||
#include "flash_driver.h"
|
||||
|
||||
// Size of the block read when checking / writing BIN files
|
||||
#define BLOCK_LEN 4096
|
||||
|
||||
// 'private'
|
||||
static DIR dir;
|
||||
static FIL FlashFile;
|
||||
|
||||
static uint32_t firmwareSize;
|
||||
static uint32_t firmwareAddress = FIRMWARE_ADDRESS;
|
||||
static uint32_t firmwareWritten = 0;
|
||||
uint32_t unlocked = 0;
|
||||
|
||||
struct FWFileInfo {
|
||||
TCHAR name[FF_MAX_LFN + 1];
|
||||
unsigned int size;
|
||||
};
|
||||
|
||||
// 'public' variables
|
||||
BinFileInfo binFiles[MAX_BIN_FILES];
|
||||
uint8_t Block_buffer[BLOCK_LEN];
|
||||
UINT BlockCount;
|
||||
static FWFileInfo fwFiles[MAX_FW_FILES];
|
||||
static uint8_t Block_buffer[BLOCK_LEN];
|
||||
static UINT BlockCount;
|
||||
|
||||
static void flashWriteBlock()
|
||||
{
|
||||
// TODO: use some board provided driver instead
|
||||
uint32_t blockOffset = 0;
|
||||
#if !defined(SIMU)
|
||||
while (BlockCount) {
|
||||
flashWrite((uint32_t *)firmwareAddress, (uint32_t *)&Block_buffer[blockOffset]);
|
||||
blockOffset += FLASH_PAGESIZE;
|
||||
firmwareAddress += FLASH_PAGESIZE;
|
||||
if (BlockCount > FLASH_PAGESIZE) {
|
||||
BlockCount -= FLASH_PAGESIZE;
|
||||
}
|
||||
else {
|
||||
BlockCount = 0;
|
||||
}
|
||||
}
|
||||
#endif // SIMU
|
||||
}
|
||||
|
||||
void sdInit(void)
|
||||
{
|
||||
@@ -56,14 +89,41 @@ void sdDone()
|
||||
storageDeInit();
|
||||
}
|
||||
|
||||
FRESULT openBinDir(MemoryType mt)
|
||||
FRESULT openFirmwareDir()
|
||||
{
|
||||
FRESULT fr = f_chdir(getBinaryPath(mt));
|
||||
FRESULT fr = f_chdir(getFirmwarePath());
|
||||
if (fr != FR_OK) return fr;
|
||||
|
||||
|
||||
return f_opendir(&dir, ".");
|
||||
}
|
||||
|
||||
static uint32_t isValidBufferStart(const uint8_t * buffer)
|
||||
{
|
||||
#if !defined(SIMU)
|
||||
return isFirmwareStart(buffer);
|
||||
#else
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
FlashCheckRes checkFirmwareFile(unsigned int index, FlashCheckRes res)
|
||||
{
|
||||
if (res != FC_UNCHECKED)
|
||||
return res;
|
||||
|
||||
if (openFirmwareFile(index) != FR_OK)
|
||||
return FC_ERROR;
|
||||
|
||||
if (closeFirmwareFile() != FR_OK)
|
||||
return FC_ERROR;
|
||||
|
||||
if (!isValidBufferStart(Block_buffer))
|
||||
return FC_ERROR;
|
||||
|
||||
return FC_OK;
|
||||
}
|
||||
|
||||
|
||||
static FRESULT findNextBinFile(FILINFO* fno)
|
||||
{
|
||||
FRESULT fr;
|
||||
@@ -102,7 +162,7 @@ static FRESULT findNextBinFile(FILINFO* fno)
|
||||
return fr;
|
||||
}
|
||||
|
||||
unsigned int fetchBinFiles(unsigned int index)
|
||||
unsigned int fetchFirmwareFiles(unsigned int index)
|
||||
{
|
||||
FILINFO file_info;
|
||||
|
||||
@@ -117,8 +177,8 @@ unsigned int fetchBinFiles(unsigned int index)
|
||||
return 0;
|
||||
}
|
||||
|
||||
strAppend(binFiles[0].name, file_info.fname);
|
||||
binFiles[0].size = file_info.fsize;
|
||||
strAppend(fwFiles[0].name, file_info.fname);
|
||||
fwFiles[0].size = file_info.fsize;
|
||||
|
||||
unsigned int i = 1;
|
||||
for (; i < MAX_NAMES_ON_SCREEN+1; i++) {
|
||||
@@ -126,49 +186,57 @@ unsigned int fetchBinFiles(unsigned int index)
|
||||
if (findNextBinFile(&file_info) != FR_OK || file_info.fname[0] == 0)
|
||||
return i;
|
||||
|
||||
strAppend(binFiles[i].name, file_info.fname);
|
||||
binFiles[i].size = file_info.fsize;
|
||||
strAppend(fwFiles[i].name, file_info.fname);
|
||||
fwFiles[i].size = file_info.fsize;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
FRESULT openBinFile(MemoryType mt, unsigned int index)
|
||||
const char* getFirmwareFileNameByIndex(unsigned int index)
|
||||
{
|
||||
if(index >= MAX_FW_FILES)
|
||||
return "";
|
||||
|
||||
return fwFiles[index].name;
|
||||
}
|
||||
|
||||
FRESULT openFirmwareFile(unsigned int index)
|
||||
{
|
||||
TCHAR full_path[FF_MAX_LFN+1];
|
||||
FRESULT fr;
|
||||
FRESULT res;
|
||||
|
||||
// build full_path: [bin path]/[filename]
|
||||
char* s = strAppend(full_path, getBinaryPath(mt));
|
||||
char* s = strAppend(full_path, getFirmwarePath());
|
||||
s = strAppend(s, "/");
|
||||
strAppend(s, binFiles[index].name);
|
||||
strAppend(s, fwFiles[index].name);
|
||||
|
||||
BlockCount = 0;
|
||||
|
||||
|
||||
// open the file
|
||||
if ((fr = f_open(&FlashFile, full_path, FA_READ)) != FR_OK)
|
||||
return fr;
|
||||
res = f_open(&FlashFile, full_path, FA_READ);
|
||||
if (res != FR_OK) return res;
|
||||
|
||||
// skip bootloader in firmware
|
||||
if (mt == MEM_FLASH &&
|
||||
((fr = f_lseek(&FlashFile, BOOTLOADER_SIZE)) != FR_OK))
|
||||
return fr;
|
||||
res = f_lseek(&FlashFile, BOOTLOADER_SIZE);
|
||||
if (res != FR_OK) return res;
|
||||
|
||||
// ... and fetch BLOCK_LEN bytes
|
||||
fr = f_read(&FlashFile, Block_buffer, BLOCK_LEN, &BlockCount);
|
||||
|
||||
if (BlockCount == BLOCK_LEN)
|
||||
return fr;
|
||||
res = f_read(&FlashFile, Block_buffer, BLOCK_LEN, &BlockCount);
|
||||
if (BlockCount == BLOCK_LEN && isValidBufferStart(Block_buffer)) return res;
|
||||
|
||||
f_close(&FlashFile);
|
||||
return FR_INVALID_OBJECT;
|
||||
}
|
||||
|
||||
void extractFirmwareVersion(VersionTag* tag)
|
||||
void getFileFirmwareVersion(VersionTag* tag)
|
||||
{
|
||||
const char * vers = getFirmwareVersion((const char *)Block_buffer);
|
||||
memset(tag->flavour, 0, sizeof(tag->flavour));
|
||||
const char * vers = getFirmwareVersion((const uint8_t *)Block_buffer);
|
||||
if (!vers || (vers[0] == 'n' && vers[1] == 'o')) { // "no version found"
|
||||
memcpy(tag->flavour, "unknown", sizeof("unknown"));
|
||||
tag->version = "unknown";
|
||||
tag->flavour[sizeof(tag->flavour)-1] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -180,18 +248,41 @@ void extractFirmwareVersion(VersionTag* tag)
|
||||
char* fl = tag->flavour;
|
||||
while(*vers != '-')
|
||||
*(fl++) = *(vers++);
|
||||
tag->flavour[sizeof(tag->flavour)-1] = 0;
|
||||
|
||||
// skip '-'
|
||||
tag->version = ++vers;
|
||||
}
|
||||
|
||||
FRESULT readBinFile()
|
||||
FRESULT readFirmwareFile()
|
||||
{
|
||||
BlockCount = 0;
|
||||
return f_read(&FlashFile, Block_buffer, sizeof(Block_buffer), &BlockCount);
|
||||
}
|
||||
|
||||
FRESULT closeBinFile()
|
||||
FRESULT closeFirmwareFile()
|
||||
{
|
||||
return f_close(&FlashFile);
|
||||
}
|
||||
|
||||
void firmwareInitWrite(uint32_t index)
|
||||
{
|
||||
firmwareSize = fwFiles[index].size - BOOTLOADER_SIZE;
|
||||
firmwareAddress = FIRMWARE_ADDRESS + BOOTLOADER_SIZE;
|
||||
firmwareWritten = 0;
|
||||
}
|
||||
|
||||
bool firmwareWriteBlock(uint32_t* progress)
|
||||
{
|
||||
flashWriteBlock();
|
||||
firmwareWritten += sizeof(Block_buffer);
|
||||
*progress = (100 * firmwareWritten) / firmwareSize;
|
||||
|
||||
readFirmwareFile();
|
||||
if (BlockCount == 0 || firmwareWritten >= FLASHSIZE - BOOTLOADER_SIZE) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -19,45 +19,27 @@
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
// #if !defined(SIMU)
|
||||
// #include "stm32_hal_ll.h"
|
||||
// #include "stm32_hal.h"
|
||||
// #include "stm32_timer.h"
|
||||
// #endif
|
||||
#include "boot.h"
|
||||
|
||||
#include "hal/usb_driver.h"
|
||||
#include "hal/abnormal_reboot.h"
|
||||
#include "hal/rotary_encoder.h"
|
||||
|
||||
#include "board.h"
|
||||
#include "debug.h"
|
||||
|
||||
#include "timers_driver.h"
|
||||
|
||||
#if defined(BLUETOOTH)
|
||||
#include "bluetooth_driver.h"
|
||||
#include "stm32_serial_driver.h"
|
||||
#endif
|
||||
|
||||
#include "board.h"
|
||||
#include "boot.h"
|
||||
#include "bin_files.h"
|
||||
#include "lcd.h"
|
||||
#include "debug.h"
|
||||
|
||||
#include "timers_driver.h"
|
||||
#include "flash_driver.h"
|
||||
|
||||
#include "hal/abnormal_reboot.h"
|
||||
#include "hal/rotary_encoder.h"
|
||||
|
||||
#if defined(DEBUG_SEGGER_RTT)
|
||||
#include "thirdparty/Segger_RTT/RTT/SEGGER_RTT.h"
|
||||
#endif
|
||||
|
||||
#if defined(SPI_FLASH)
|
||||
#define MAIN_MENU_LEN 3
|
||||
#else
|
||||
#define MAIN_MENU_LEN 2
|
||||
#endif
|
||||
|
||||
#if defined(SPI_FLASH)
|
||||
#include "diskio_spi_flash.h"
|
||||
#define SEL_CLEAR_FLASH_STORAGE_MENU_LEN 2
|
||||
#endif
|
||||
#define REBOOT_CMD_DFU 0x55464442
|
||||
|
||||
#if !defined(SIMU)
|
||||
// Bootloader marker:
|
||||
@@ -72,104 +54,6 @@ const uint8_t bootloaderVersion[] __attribute__ ((section(".version"), used)) =
|
||||
#define __weak __attribute__((weak))
|
||||
#endif
|
||||
|
||||
volatile tmr10ms_t g_tmr10ms;
|
||||
volatile uint8_t tenms = 1;
|
||||
|
||||
uint32_t firmwareSize;
|
||||
uint32_t firmwareAddress = FIRMWARE_ADDRESS;
|
||||
uint32_t firmwareWritten = 0;
|
||||
|
||||
FlashCheckRes valid;
|
||||
MemoryType memoryType;
|
||||
uint32_t unlocked = 0;
|
||||
|
||||
void per5ms() {} // make linker happy
|
||||
|
||||
void per10ms()
|
||||
{
|
||||
tenms |= 1u; // 10 mS has passed
|
||||
g_tmr10ms++;
|
||||
|
||||
keysPollingCycle();
|
||||
|
||||
#if defined(ROTARY_ENCODER_NAVIGATION) && !defined(USE_HATS_AS_KEYS)
|
||||
static rotenc_t rePreviousValue;
|
||||
|
||||
rotenc_t reNewValue = rotaryEncoderGetValue();
|
||||
int8_t scrollRE = reNewValue - rePreviousValue;
|
||||
if (scrollRE) {
|
||||
rePreviousValue = reNewValue;
|
||||
pushEvent(scrollRE < 0 ? EVT_ROTARY_LEFT : EVT_ROTARY_RIGHT);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t isValidBufferStart(const uint8_t * buffer)
|
||||
{
|
||||
#if !defined(SIMU)
|
||||
return isFirmwareStart(buffer);
|
||||
#else
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
FlashCheckRes checkFlashFile(unsigned int index, FlashCheckRes res)
|
||||
{
|
||||
if (res != FC_UNCHECKED)
|
||||
return res;
|
||||
|
||||
if (openBinFile(memoryType, index) != FR_OK)
|
||||
return FC_ERROR;
|
||||
|
||||
if (closeBinFile() != FR_OK)
|
||||
return FC_ERROR;
|
||||
|
||||
if (!isValidBufferStart(Block_buffer))
|
||||
return FC_ERROR;
|
||||
|
||||
return FC_OK;
|
||||
}
|
||||
|
||||
int menuFlashFile(uint32_t index, event_t event)
|
||||
{
|
||||
valid = checkFlashFile(index, valid);
|
||||
|
||||
if (valid == FC_ERROR) {
|
||||
if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER))
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (event == EVT_KEY_LONG(KEY_ENTER)) {
|
||||
|
||||
return (openBinFile(memoryType, index) == FR_OK) && isValidBufferStart(Block_buffer);
|
||||
}
|
||||
else if (event == EVT_KEY_BREAK(KEY_EXIT))
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void flashWriteBlock()
|
||||
{
|
||||
// TODO: use some board provided driver instead
|
||||
uint32_t blockOffset = 0;
|
||||
#if !defined(SIMU)
|
||||
while (BlockCount) {
|
||||
flashWrite((uint32_t *)firmwareAddress, (uint32_t *)&Block_buffer[blockOffset]);
|
||||
blockOffset += FLASH_PAGESIZE;
|
||||
firmwareAddress += FLASH_PAGESIZE;
|
||||
if (BlockCount > FLASH_PAGESIZE) {
|
||||
BlockCount -= FLASH_PAGESIZE;
|
||||
}
|
||||
else {
|
||||
BlockCount = 0;
|
||||
}
|
||||
}
|
||||
#endif // SIMU
|
||||
}
|
||||
|
||||
#if !defined(SIMU)
|
||||
|
||||
typedef void (*fctptr_t)(void);
|
||||
@@ -184,13 +68,14 @@ static __attribute__((noreturn)) void jumpTo(uint32_t addr)
|
||||
}
|
||||
|
||||
// Optional board hook
|
||||
__weak void boardBLInit() {}
|
||||
__weak void boardBLEarlyInit() {}
|
||||
__weak bool boardBLStartCondition() { return false; }
|
||||
__weak void boardBLPreJump() {}
|
||||
__weak void boardBLInit() {}
|
||||
|
||||
void bootloaderInitApp()
|
||||
{
|
||||
boardBLInit();
|
||||
boardBLEarlyInit();
|
||||
|
||||
#if defined(DEBUG_SEGGER_RTT)
|
||||
SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP);
|
||||
@@ -198,18 +83,28 @@ void bootloaderInitApp()
|
||||
|
||||
pwrInit();
|
||||
keysInit();
|
||||
delaysInit();
|
||||
|
||||
// wait a bit for the inputs to stabilize...
|
||||
if (!WAS_RESET_BY_WATCHDOG_OR_SOFTWARE()) {
|
||||
for (uint32_t i = 0; i < 150000; i++) {
|
||||
__ASM volatile("nop");
|
||||
bool boot_dfu = abnormalRebootGetCmd() == REBOOT_CMD_DFU;
|
||||
if (!boot_dfu) {
|
||||
bool start_firmware = true;
|
||||
|
||||
// wait a bit for the inputs to stabilize...
|
||||
if (!WAS_RESET_BY_WATCHDOG_OR_SOFTWARE()) {
|
||||
delay_us(200);
|
||||
}
|
||||
}
|
||||
|
||||
if (!boardBLStartCondition()) {
|
||||
// Start main application
|
||||
boardBLPreJump();
|
||||
jumpTo(APP_START_ADDRESS);
|
||||
start_firmware = !boardBLStartCondition();
|
||||
if (start_firmware) {
|
||||
// Start main application
|
||||
boardBLPreJump();
|
||||
jumpTo(APP_START_ADDRESS);
|
||||
}
|
||||
} else {
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
abnormalRebootResetCmd();
|
||||
setSelectedUsbMode(USB_DFU_MODE);
|
||||
#endif
|
||||
}
|
||||
|
||||
// TODO: move all this into board specifics
|
||||
@@ -219,8 +114,6 @@ void bootloaderInitApp()
|
||||
rotaryEncoderInit();
|
||||
#endif
|
||||
|
||||
delaysInit(); // needed for lcdInit()
|
||||
|
||||
#if defined(DEBUG)
|
||||
initSerialPorts();
|
||||
#endif
|
||||
@@ -238,9 +131,8 @@ void bootloaderInitApp()
|
||||
|
||||
timersInit();
|
||||
|
||||
// SD card detect pin
|
||||
sdInit();
|
||||
usbInit();
|
||||
boardBLInit();
|
||||
}
|
||||
|
||||
int main()
|
||||
@@ -249,286 +141,28 @@ void bootloaderInitApp() {}
|
||||
int bootloaderMain()
|
||||
#endif
|
||||
{
|
||||
BootloaderState state = ST_START;
|
||||
uint32_t vpos = 0;
|
||||
uint32_t radioMenuItem = 0;
|
||||
uint8_t index = 0;
|
||||
FRESULT fr;
|
||||
uint32_t nameCount = 0;
|
||||
|
||||
// init hardware (may jump to app)
|
||||
bootloaderInitApp();
|
||||
|
||||
// init screen
|
||||
bootloaderInitScreen();
|
||||
|
||||
#if defined(FIRMWARE_FORMAT_UF2)
|
||||
if (getSelectedUsbMode() == USB_DFU_MODE) {
|
||||
bootloaderDFU();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(PWR_BUTTON_PRESS)
|
||||
// wait until power button is released
|
||||
while (pwrPressed()) {}
|
||||
#endif
|
||||
|
||||
for (;;) {
|
||||
|
||||
if (tenms) {
|
||||
tenms = 0;
|
||||
|
||||
if (state != ST_USB && state != ST_FLASHING
|
||||
&& state != ST_FLASH_DONE && state != ST_RADIO_MENU) {
|
||||
if (usbPlugged()) {
|
||||
state = ST_USB;
|
||||
if (!unlocked) {
|
||||
unlocked = 1;
|
||||
unlockFlash();
|
||||
}
|
||||
#if !defined(SIMU)
|
||||
usbStart();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (state == ST_USB) {
|
||||
if (usbPlugged() == 0) {
|
||||
vpos = 0;
|
||||
#if !defined(SIMU)
|
||||
usbStop();
|
||||
#endif
|
||||
if (unlocked) {
|
||||
lockFlash();
|
||||
unlocked = 0;
|
||||
}
|
||||
state = ST_START;
|
||||
}
|
||||
bootloaderDrawScreen(state, 0);
|
||||
}
|
||||
|
||||
lcdRefreshWait();
|
||||
event_t event = getEvent();
|
||||
|
||||
if (state == ST_START) {
|
||||
|
||||
bootloaderDrawScreen(state, vpos);
|
||||
|
||||
if (IS_NEXT_EVENT(event)) {
|
||||
if (vpos < bootloaderGetMenuItemCount(MAIN_MENU_LEN) - 1) { vpos++; }
|
||||
continue;
|
||||
}
|
||||
else if (IS_PREVIOUS_EVENT(event)) {
|
||||
if (vpos > 0) { vpos--; }
|
||||
continue;
|
||||
}
|
||||
else if (event == EVT_KEY_BREAK(KEY_ENTER)) {
|
||||
switch (vpos) {
|
||||
case 0:
|
||||
memoryType = MEM_FLASH;
|
||||
state = ST_DIR_CHECK;
|
||||
break;
|
||||
#if defined(SPI_FLASH)
|
||||
case 1:
|
||||
state = ST_CLEAR_FLASH_CHECK;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
if(vpos < bootloaderGetMenuItemCount(MAIN_MENU_LEN-1))
|
||||
{
|
||||
state = ST_RADIO_MENU;
|
||||
radioMenuItem = vpos - MAIN_MENU_LEN - 1;
|
||||
} else {
|
||||
state = ST_REBOOT;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// next loop
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (state == ST_DIR_CHECK) {
|
||||
fr = openBinDir(memoryType);
|
||||
|
||||
if (fr == FR_OK) {
|
||||
index = vpos = 0;
|
||||
state = ST_FILE_LIST;
|
||||
nameCount = fetchBinFiles(index);
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
bootloaderDrawScreen(state, fr);
|
||||
|
||||
if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER)) {
|
||||
vpos = 0;
|
||||
state = ST_START;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (state == ST_FILE_LIST) {
|
||||
uint32_t limit = MAX_NAMES_ON_SCREEN;
|
||||
if (nameCount < limit) {
|
||||
limit = nameCount;
|
||||
}
|
||||
|
||||
if (IS_NEXT_EVENT(event)) {
|
||||
if (vpos < limit - 1) {
|
||||
vpos += 1;
|
||||
}
|
||||
else {
|
||||
if (nameCount > limit) {
|
||||
index += 1;
|
||||
nameCount = fetchBinFiles(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (IS_PREVIOUS_EVENT(event)) {
|
||||
if (vpos > 0) {
|
||||
vpos -= 1;
|
||||
}
|
||||
else {
|
||||
if (index) {
|
||||
index -= 1;
|
||||
nameCount = fetchBinFiles(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bootloaderDrawScreen(state, 0);
|
||||
|
||||
for (uint32_t i = 0; i < limit; i++) {
|
||||
bootloaderDrawFilename(binFiles[i].name, i, (vpos == i));
|
||||
}
|
||||
|
||||
if (event == EVT_KEY_BREAK(KEY_ENTER)) {
|
||||
// Select file to flash
|
||||
state = ST_FLASH_CHECK;
|
||||
valid = FC_UNCHECKED;
|
||||
continue;
|
||||
}
|
||||
else if (event == EVT_KEY_BREAK(KEY_EXIT)) {
|
||||
state = ST_START;
|
||||
vpos = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (state == ST_FLASH_CHECK) {
|
||||
bootloaderDrawScreen(state, valid, binFiles[vpos].name);
|
||||
|
||||
int result = menuFlashFile(vpos, event);
|
||||
if (result == 0) {
|
||||
// canceled
|
||||
state = ST_FILE_LIST;
|
||||
}
|
||||
else if (result == 1) {
|
||||
// confirmed
|
||||
|
||||
if (memoryType == MEM_FLASH) {
|
||||
firmwareSize = FIRMWARE_LEN(binFiles[vpos].size);
|
||||
firmwareAddress = APP_START_ADDRESS;
|
||||
firmwareWritten = 0;
|
||||
}
|
||||
state = ST_FLASHING;
|
||||
}
|
||||
}
|
||||
else if (state == ST_FLASHING) {
|
||||
// commit to flashing
|
||||
if (!unlocked && (memoryType == MEM_FLASH)) {
|
||||
unlocked = 1;
|
||||
unlockFlash();
|
||||
}
|
||||
|
||||
int progress = 0;
|
||||
if (memoryType == MEM_FLASH) {
|
||||
flashWriteBlock();
|
||||
firmwareWritten += sizeof(Block_buffer);
|
||||
progress = (100 * firmwareWritten) / firmwareSize;
|
||||
}
|
||||
|
||||
bootloaderDrawScreen(state, progress);
|
||||
|
||||
fr = readBinFile();
|
||||
if (BlockCount == 0) {
|
||||
state = ST_FLASH_DONE; // EOF
|
||||
}
|
||||
else if (memoryType == MEM_FLASH && firmwareWritten >= FIRMWARE_MAX_LEN) {
|
||||
state = ST_FLASH_DONE; // Backstop
|
||||
}
|
||||
#if defined(SPI_FLASH)
|
||||
} else if (state == ST_CLEAR_FLASH_CHECK) {
|
||||
bootloaderDrawScreen(state, vpos);
|
||||
if (IS_NEXT_EVENT(event)) {
|
||||
if (vpos < SEL_CLEAR_FLASH_STORAGE_MENU_LEN - 1) { vpos++; }
|
||||
continue;
|
||||
}
|
||||
if (IS_PREVIOUS_EVENT(event)) {
|
||||
if (vpos > 0) { vpos--; }
|
||||
continue;
|
||||
}
|
||||
if (event == EVT_KEY_LONG(KEY_ENTER) && vpos == 0)
|
||||
{
|
||||
state = ST_CLEAR_FLASH;
|
||||
} else if (event == EVT_KEY_BREAK(KEY_EXIT) ||
|
||||
(event == EVT_KEY_BREAK(KEY_ENTER) && vpos == 1) ) {
|
||||
vpos = 0;
|
||||
state = ST_START;
|
||||
continue;
|
||||
}
|
||||
} else if (state == ST_CLEAR_FLASH) {
|
||||
bootloaderDrawScreen(state, 0);
|
||||
lcdRefresh();
|
||||
sdDone();
|
||||
spiFlashDiskEraseAll();
|
||||
sdInit();
|
||||
vpos = 0;
|
||||
state = ST_START;
|
||||
#endif
|
||||
} else if (state == ST_RADIO_MENU) {
|
||||
if(bootloaderRadioMenu(radioMenuItem, event))
|
||||
{
|
||||
state = ST_START;
|
||||
vpos = 0;
|
||||
bootloaderDrawScreen(state, vpos);
|
||||
}
|
||||
}
|
||||
|
||||
if (state == ST_FLASH_DONE) {
|
||||
if (unlocked) {
|
||||
lockFlash();
|
||||
unlocked = 0;
|
||||
}
|
||||
|
||||
if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER)) {
|
||||
state = ST_START;
|
||||
vpos = 0;
|
||||
}
|
||||
|
||||
bootloaderDrawScreen(state, 100);
|
||||
}
|
||||
|
||||
if (event == EVT_KEY_LONG(KEY_EXIT)) {
|
||||
// Start the main application
|
||||
state = ST_REBOOT;
|
||||
}
|
||||
|
||||
lcdRefresh();
|
||||
|
||||
}
|
||||
|
||||
if (state != ST_FLASHING && state != ST_USB) {
|
||||
if (pwrOffPressed()) {
|
||||
sdDone();
|
||||
boardOff();
|
||||
}
|
||||
}
|
||||
|
||||
if (state == ST_REBOOT) {
|
||||
sdDone();
|
||||
#if !defined(SIMU)
|
||||
blExit();
|
||||
NVIC_SystemReset();
|
||||
#if !defined(FIRMWARE_FORMAT_UF2)
|
||||
bootloaderMenu();
|
||||
#else
|
||||
exit(1);
|
||||
bootloaderUF2();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
#include "stamp.h"
|
||||
#include "keys.h"
|
||||
|
||||
#include "hal/flash_driver.h"
|
||||
|
||||
#if LCD_W < 212
|
||||
#if defined(VERSION_TAG)
|
||||
#define BOOTLOADER_TITLE " Bootloader - " VERSION_TAG
|
||||
@@ -61,11 +63,14 @@ enum BootloaderState {
|
||||
ST_REBOOT,
|
||||
};
|
||||
|
||||
enum FlashCheckRes {
|
||||
FC_UNCHECKED=0,
|
||||
FC_OK,
|
||||
FC_ERROR
|
||||
};
|
||||
// interactive bootloader
|
||||
void bootloaderMenu();
|
||||
|
||||
// non-interactive UF2 bootloader
|
||||
void bootloaderUF2();
|
||||
|
||||
// non-interactive DFU bootloader
|
||||
void bootloaderDFU();
|
||||
|
||||
// Declarations of functions that need to be implemented
|
||||
// for each target with a bootloader
|
||||
@@ -87,4 +92,7 @@ uint32_t bootloaderGetMenuItemCount(int baseCount);
|
||||
// returns true on submenu exit
|
||||
bool bootloaderRadioMenu(uint32_t menuItem, event_t event);
|
||||
|
||||
void bootloaderDrawDFUScreen();
|
||||
|
||||
void sdInit();
|
||||
void blExit();
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "stm32_hal.h"
|
||||
|
||||
#include "boot.h"
|
||||
#include "hal/usb_driver.h"
|
||||
#include "lcd.h"
|
||||
|
||||
extern volatile uint8_t tenms;
|
||||
|
||||
void bootloaderDFU()
|
||||
{
|
||||
usbPlugged();
|
||||
usbStart();
|
||||
|
||||
bootloaderDrawDFUScreen();
|
||||
|
||||
for (;;) {
|
||||
if (tenms) {
|
||||
if (!usbPlugged()) break;
|
||||
bootloaderDrawDFUScreen();
|
||||
lcdRefresh();
|
||||
}
|
||||
}
|
||||
|
||||
blExit();
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
#include "boot.h"
|
||||
#include "firmware_files.h"
|
||||
|
||||
#include "hal/usb_driver.h"
|
||||
#include "hal/rotary_encoder.h"
|
||||
|
||||
#include "lcd.h"
|
||||
|
||||
#if defined(SPI_FLASH)
|
||||
#define MAIN_MENU_LEN 3
|
||||
#else
|
||||
#define MAIN_MENU_LEN 2
|
||||
#endif
|
||||
|
||||
#if defined(SPI_FLASH)
|
||||
#include "diskio_spi_flash.h"
|
||||
#define SEL_CLEAR_FLASH_STORAGE_MENU_LEN 2
|
||||
#endif
|
||||
|
||||
volatile tmr10ms_t g_tmr10ms;
|
||||
volatile uint8_t tenms = 1;
|
||||
|
||||
void per5ms() {} // make linker happy
|
||||
|
||||
void per10ms()
|
||||
{
|
||||
tenms |= 1u; // 10 mS has passed
|
||||
g_tmr10ms++;
|
||||
|
||||
keysPollingCycle();
|
||||
|
||||
// TODO: use rotaryEncoderPollingCycle() instead
|
||||
#if defined(ROTARY_ENCODER_NAVIGATION) && !defined(USE_HATS_AS_KEYS)
|
||||
static rotenc_t rePreviousValue;
|
||||
|
||||
rotenc_t reNewValue = rotaryEncoderGetValue();
|
||||
int8_t scrollRE = reNewValue - rePreviousValue;
|
||||
if (scrollRE) {
|
||||
rePreviousValue = reNewValue;
|
||||
pushEvent(scrollRE < 0 ? EVT_ROTARY_LEFT : EVT_ROTARY_RIGHT);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
FlashCheckRes valid;
|
||||
|
||||
int menuFlashFile(uint32_t index, event_t event)
|
||||
{
|
||||
valid = checkFirmwareFile(index, valid);
|
||||
|
||||
if (valid == FC_ERROR) {
|
||||
if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER))
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (event == EVT_KEY_LONG(KEY_ENTER)) {
|
||||
return openFirmwareFile(index) == FR_OK ? 1 : -1;
|
||||
} else if (event == EVT_KEY_BREAK(KEY_EXIT))
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void bootloaderMenu()
|
||||
{
|
||||
BootloaderState state = ST_START;
|
||||
uint32_t vpos = 0;
|
||||
uint32_t radioMenuItem = 0;
|
||||
uint8_t index = 0;
|
||||
FRESULT fr;
|
||||
uint32_t nameCount = 0;
|
||||
|
||||
sdInit();
|
||||
|
||||
for (;;) {
|
||||
|
||||
if (tenms) {
|
||||
tenms = 0;
|
||||
|
||||
if (state != ST_USB && state != ST_FLASHING
|
||||
&& state != ST_FLASH_DONE && state != ST_RADIO_MENU) {
|
||||
if (usbPlugged()) {
|
||||
state = ST_USB;
|
||||
#if !defined(SIMU)
|
||||
usbStart();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (state == ST_USB) {
|
||||
if (usbPlugged() == 0) {
|
||||
vpos = 0;
|
||||
#if !defined(SIMU)
|
||||
usbStop();
|
||||
#endif
|
||||
state = ST_START;
|
||||
}
|
||||
bootloaderDrawScreen(state, 0);
|
||||
}
|
||||
|
||||
lcdRefreshWait();
|
||||
event_t event = getEvent();
|
||||
|
||||
if (state == ST_START) {
|
||||
|
||||
bootloaderDrawScreen(state, vpos);
|
||||
|
||||
if (IS_NEXT_EVENT(event)) {
|
||||
if (vpos < bootloaderGetMenuItemCount(MAIN_MENU_LEN) - 1) { vpos++; }
|
||||
continue;
|
||||
}
|
||||
else if (IS_PREVIOUS_EVENT(event)) {
|
||||
if (vpos > 0) { vpos--; }
|
||||
continue;
|
||||
}
|
||||
else if (event == EVT_KEY_BREAK(KEY_ENTER)) {
|
||||
switch (vpos) {
|
||||
case 0:
|
||||
state = ST_DIR_CHECK;
|
||||
break;
|
||||
#if defined(SPI_FLASH)
|
||||
case 1:
|
||||
state = ST_CLEAR_FLASH_CHECK;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
if(vpos < bootloaderGetMenuItemCount(MAIN_MENU_LEN-1))
|
||||
{
|
||||
state = ST_RADIO_MENU;
|
||||
radioMenuItem = vpos - MAIN_MENU_LEN - 1;
|
||||
} else {
|
||||
state = ST_REBOOT;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// next loop
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if (state == ST_DIR_CHECK) {
|
||||
fr = openFirmwareDir();
|
||||
|
||||
if (fr == FR_OK) {
|
||||
index = vpos = 0;
|
||||
state = ST_FILE_LIST;
|
||||
nameCount = fetchFirmwareFiles(index);
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
bootloaderDrawScreen(state, fr);
|
||||
|
||||
if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER)) {
|
||||
vpos = 0;
|
||||
state = ST_START;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (state == ST_FILE_LIST) {
|
||||
uint32_t limit = MAX_NAMES_ON_SCREEN;
|
||||
if (nameCount < limit) {
|
||||
limit = nameCount;
|
||||
}
|
||||
|
||||
if (IS_NEXT_EVENT(event)) {
|
||||
if (vpos < limit - 1) {
|
||||
vpos += 1;
|
||||
} else {
|
||||
if (nameCount > limit) {
|
||||
index += 1;
|
||||
nameCount = fetchFirmwareFiles(index);
|
||||
}
|
||||
}
|
||||
} else if (IS_PREVIOUS_EVENT(event)) {
|
||||
if (vpos > 0) {
|
||||
vpos -= 1;
|
||||
}
|
||||
else {
|
||||
if (index) {
|
||||
index -= 1;
|
||||
nameCount = fetchFirmwareFiles(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bootloaderDrawScreen(state, 0);
|
||||
|
||||
for (uint32_t i = 0; i < limit; i++) {
|
||||
bootloaderDrawFilename(getFirmwareFileNameByIndex(i), i, (vpos == i));
|
||||
}
|
||||
|
||||
if (event == EVT_KEY_BREAK(KEY_ENTER)) {
|
||||
// Select file to flash
|
||||
state = ST_FLASH_CHECK;
|
||||
valid = FC_UNCHECKED;
|
||||
continue;
|
||||
} else if (event == EVT_KEY_BREAK(KEY_EXIT)) {
|
||||
state = ST_START;
|
||||
vpos = 0;
|
||||
continue;
|
||||
}
|
||||
} else if (state == ST_FLASH_CHECK) {
|
||||
bootloaderDrawScreen(state, valid, getFirmwareFileNameByIndex(vpos));
|
||||
|
||||
int result = menuFlashFile(vpos, event);
|
||||
if (result == 0) {
|
||||
// canceled
|
||||
state = ST_FILE_LIST;
|
||||
} else if (result == 1) {
|
||||
// confirmed
|
||||
firmwareInitWrite(vpos);
|
||||
state = ST_FLASHING;
|
||||
}
|
||||
} else if (state == ST_FLASHING) {
|
||||
uint32_t progress = 0;
|
||||
bool done = firmwareWriteBlock(&progress);
|
||||
bootloaderDrawScreen(state, progress);
|
||||
if(done) {
|
||||
state = ST_FLASH_DONE;
|
||||
}
|
||||
#if defined(SPI_FLASH)
|
||||
} else if (state == ST_CLEAR_FLASH_CHECK) {
|
||||
bootloaderDrawScreen(state, vpos);
|
||||
if (IS_NEXT_EVENT(event)) {
|
||||
if (vpos < SEL_CLEAR_FLASH_STORAGE_MENU_LEN - 1) { vpos++; }
|
||||
continue;
|
||||
}
|
||||
if (IS_PREVIOUS_EVENT(event)) {
|
||||
if (vpos > 0) { vpos--; }
|
||||
continue;
|
||||
}
|
||||
if (event == EVT_KEY_LONG(KEY_ENTER) && vpos == 0)
|
||||
{
|
||||
state = ST_CLEAR_FLASH;
|
||||
} else if (event == EVT_KEY_BREAK(KEY_EXIT) ||
|
||||
(event == EVT_KEY_BREAK(KEY_ENTER) && vpos == 1) ) {
|
||||
vpos = 0;
|
||||
state = ST_START;
|
||||
continue;
|
||||
}
|
||||
} else if (state == ST_CLEAR_FLASH) {
|
||||
bootloaderDrawScreen(state, 0);
|
||||
lcdRefresh();
|
||||
sdDone();
|
||||
spiFlashDiskEraseAll();
|
||||
sdInit();
|
||||
vpos = 0;
|
||||
state = ST_START;
|
||||
#endif
|
||||
} else if (state == ST_RADIO_MENU) {
|
||||
if(bootloaderRadioMenu(radioMenuItem, event))
|
||||
{
|
||||
state = ST_START;
|
||||
vpos = 0;
|
||||
bootloaderDrawScreen(state, vpos);
|
||||
}
|
||||
}
|
||||
|
||||
if (state == ST_FLASH_DONE) {
|
||||
if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER)) {
|
||||
state = ST_START;
|
||||
vpos = 0;
|
||||
}
|
||||
|
||||
bootloaderDrawScreen(state, 100);
|
||||
}
|
||||
|
||||
if (event == EVT_KEY_LONG(KEY_EXIT)) {
|
||||
// Start the main application
|
||||
state = ST_REBOOT;
|
||||
}
|
||||
|
||||
lcdRefresh();
|
||||
}
|
||||
|
||||
if (state != ST_FLASHING && state != ST_USB) {
|
||||
if (pwrOffPressed()) {
|
||||
sdDone();
|
||||
boardOff();
|
||||
}
|
||||
}
|
||||
|
||||
if (state == ST_REBOOT) {
|
||||
sdDone();
|
||||
#if !defined(SIMU)
|
||||
blExit();
|
||||
NVIC_SystemReset();
|
||||
#else
|
||||
exit(1);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
#include "boot.h"
|
||||
|
||||
#include "hal/usb_driver.h"
|
||||
#include "hal/storage.h"
|
||||
#include "hal/fatfs_diskio.h"
|
||||
|
||||
#include "thirdparty/FatFs/diskio.h"
|
||||
#include "drivers/uf2_ghostfat.h"
|
||||
|
||||
#include "board.h"
|
||||
#include "lcd.h"
|
||||
|
||||
extern uf2_fat_write_state_t _uf2_write_state;
|
||||
|
||||
volatile tmr10ms_t g_tmr10ms;
|
||||
volatile uint8_t tenms = 1;
|
||||
|
||||
void per5ms() {} // make linker happy
|
||||
|
||||
void per10ms()
|
||||
{
|
||||
tenms |= 1u; // 10 mS has passed
|
||||
g_tmr10ms++;
|
||||
}
|
||||
|
||||
void bootloaderUF2()
|
||||
{
|
||||
BootloaderState state = ST_START;
|
||||
|
||||
// register SD storage
|
||||
storageInit();
|
||||
disk_initialize(0);
|
||||
|
||||
for (;;) {
|
||||
|
||||
if (tenms) {
|
||||
tenms = 0;
|
||||
|
||||
if (state != ST_USB && state != ST_FLASHING && state != ST_FLASH_DONE) {
|
||||
if (usbPlugged()) {
|
||||
state = ST_USB;
|
||||
#if !defined(SIMU)
|
||||
usbStart();
|
||||
#endif
|
||||
} else if (pwrOffPressed()) {
|
||||
storageDeInit();
|
||||
boardOff();
|
||||
}
|
||||
}
|
||||
|
||||
if (state == ST_USB || state == ST_FLASH_DONE) {
|
||||
if (usbPlugged() == 0) {
|
||||
#if !defined(SIMU)
|
||||
usbStop();
|
||||
#endif
|
||||
state = (state == ST_FLASH_DONE) ? ST_REBOOT : ST_START;
|
||||
} else {
|
||||
auto wr_st = uf2_fat_get_state();
|
||||
if (wr_st->num_blocks != 0 && wr_st->num_blocks <= UF2_MAX_BLOCKS) {
|
||||
state = ST_FLASHING;
|
||||
}
|
||||
}
|
||||
bootloaderDrawScreen(state, 0);
|
||||
}
|
||||
|
||||
if (state == ST_START) {
|
||||
bootloaderDrawScreen(state, 0);
|
||||
} else if (state == ST_FLASHING) {
|
||||
auto wr_st = uf2_fat_get_state();
|
||||
if (wr_st->num_blocks == 0 || wr_st->num_blocks > UF2_MAX_BLOCKS) {
|
||||
state = ST_USB;
|
||||
} else {
|
||||
uint32_t progress = (wr_st->num_written * 100) / wr_st->num_blocks;
|
||||
if (wr_st->num_written == wr_st->num_blocks) {
|
||||
state = ST_FLASH_DONE;
|
||||
uf2_fat_reset_state();
|
||||
}
|
||||
bootloaderDrawScreen(state, progress);
|
||||
}
|
||||
} else if (state == ST_FLASH_DONE) {
|
||||
bootloaderDrawScreen(state, 100);
|
||||
}
|
||||
|
||||
lcdRefresh();
|
||||
}
|
||||
|
||||
if (state == ST_REBOOT) {
|
||||
storageDeInit();
|
||||
#if !defined(SIMU)
|
||||
blExit();
|
||||
NVIC_SystemReset();
|
||||
#else
|
||||
exit(1);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
+23
-39
@@ -26,13 +26,9 @@
|
||||
#if defined(COLORLCD)
|
||||
#include "bitmaps.h"
|
||||
#endif
|
||||
#include "boards/FirmwareVersionTag.h"
|
||||
|
||||
enum MemoryType {
|
||||
MEM_FLASH,
|
||||
MEM_EEPROM
|
||||
};
|
||||
|
||||
#define getBinaryPath(mt) (FIRMWARES_PATH)
|
||||
#define getFirmwarePath() (FIRMWARES_PATH)
|
||||
|
||||
#if LCD_H == 480
|
||||
#define MAX_NAMES_ON_SCREEN 13
|
||||
@@ -40,56 +36,44 @@ enum MemoryType {
|
||||
#define MAX_NAMES_ON_SCREEN 6
|
||||
#endif
|
||||
|
||||
#define MAX_BIN_FILES (MAX_NAMES_ON_SCREEN+1)
|
||||
#define MAX_FW_FILES (MAX_NAMES_ON_SCREEN+1)
|
||||
|
||||
// Size of the block read when checking / writing BIN files
|
||||
#define BLOCK_LEN 4096
|
||||
|
||||
// File info struct while browsing files on SD card
|
||||
struct BinFileInfo {
|
||||
TCHAR name[FF_MAX_LFN + 1];
|
||||
unsigned int size;
|
||||
};
|
||||
|
||||
// File info storage while browsing files on SD card
|
||||
extern BinFileInfo binFiles[MAX_BIN_FILES];
|
||||
|
||||
// Block buffer used when checking / writing BIN files
|
||||
extern uint8_t Block_buffer[BLOCK_LEN];
|
||||
|
||||
// Bytes read into the Block_buffer
|
||||
extern UINT BlockCount;
|
||||
|
||||
// Open directory for EEPROM / firmware files
|
||||
FRESULT openBinDir(MemoryType mt);
|
||||
// Open directory for firmware files
|
||||
FRESULT openFirmwareDir();
|
||||
|
||||
// Fetch file names and sizes into binFiles,
|
||||
// starting at the provided index.
|
||||
// Only files ending with ".bin" (case-insensitive)
|
||||
// will be considered.
|
||||
unsigned int fetchBinFiles(unsigned int index);
|
||||
unsigned int fetchFirmwareFiles(unsigned int index);
|
||||
const char* getFirmwareFileNameByIndex(unsigned int index);
|
||||
|
||||
// Open file indexed in binFiles and read the first BLOCK_LEN bytes
|
||||
// Bootloader is skipped in firmware files
|
||||
FRESULT openBinFile(MemoryType mt, unsigned int index);
|
||||
FRESULT openFirmwareFile(unsigned int index);
|
||||
|
||||
struct VersionTag
|
||||
{
|
||||
char flavour[11];
|
||||
const char* version;
|
||||
const char* fork;
|
||||
};
|
||||
// Ensure flavour can hold FLAVOUR defined in target cmakefile
|
||||
static_assert(sizeof(((VersionTag){}).flavour) >= sizeof(FLAVOUR), "VersionTag flavour size too small");
|
||||
|
||||
|
||||
// Can be called right after openBinFile() to extract the version information
|
||||
// from a firmware file
|
||||
void extractFirmwareVersion(VersionTag* tag);
|
||||
void getFileFirmwareVersion(VersionTag* tag);
|
||||
|
||||
// Read the next BLOCK_LEN bytes into 'Block_buffer'
|
||||
// Check 'BlockCount' for # of bytes read
|
||||
FRESULT readBinFile();
|
||||
FRESULT readFirmwareFile();
|
||||
|
||||
// Close the previously opened file
|
||||
FRESULT closeBinFile();
|
||||
FRESULT closeFirmwareFile();
|
||||
|
||||
enum FlashCheckRes {
|
||||
FC_UNCHECKED=0,
|
||||
FC_OK,
|
||||
FC_ERROR
|
||||
};
|
||||
FlashCheckRes checkFirmwareFile(unsigned int index, FlashCheckRes res);
|
||||
|
||||
void firmwareInitWrite(uint32_t index);
|
||||
bool firmwareEraseBlock(uint32_t* progress);
|
||||
bool firmwareWriteBlock(uint32_t* progress);
|
||||
|
||||
@@ -125,17 +125,28 @@
|
||||
#define BLOCK_SIZE FF_MAX_SS
|
||||
#define SD_TIMEOUT 300 /* 300ms */
|
||||
|
||||
#if defined(STM32H7) || defined(STM32H7RS) || defined(STM32F4)
|
||||
#if defined(STM32F4)
|
||||
extern uint32_t _sram;
|
||||
extern uint32_t _heap_start;
|
||||
#if defined(SDRAM)
|
||||
#define _IS_DMA_BUFFER(addr) \
|
||||
#define _IS_DMA_BUFFER(addr) \
|
||||
(((((intptr_t)(addr)) & 0xFF000000) == (((intptr_t)&_sram) & 0xFF000000)) || \
|
||||
((((intptr_t)(addr)) & 0xFC000000) == (((intptr_t)&_heap_start) & 0xFC000000)))
|
||||
((((intptr_t)(addr)) & 0xFC000000) == \
|
||||
(((intptr_t)&_heap_start) & 0xFC000000)))
|
||||
#else
|
||||
#define _IS_DMA_BUFFER(addr) \
|
||||
((((intptr_t)(addr)) & 0xFF000000) == (((intptr_t)&_sram) & 0xFF000000))
|
||||
#endif
|
||||
#elif defined(STM32H7) || defined(STM32H7RS)
|
||||
extern uint32_t _sram;
|
||||
extern uint32_t _s_dram;
|
||||
extern uint32_t _heap_start;
|
||||
#define _IS_DMA_BUFFER(addr) \
|
||||
(((((intptr_t)(addr)) & 0xFF000000) == (((intptr_t)&_sram) & 0xFF000000)) || \
|
||||
((((intptr_t)(addr)) & 0xFFF60000) == \
|
||||
(((intptr_t)&_s_dram) & 0xFFF60000)) || \
|
||||
((((intptr_t)(addr)) & 0xFC000000) == \
|
||||
(((intptr_t)&_heap_start) & 0xFC000000)))
|
||||
#else
|
||||
#define _IS_DMA_BUFFER(addr) (true)
|
||||
#endif
|
||||
@@ -343,14 +354,14 @@ static DRESULT _read_dma(BYTE* buff, DWORD sector, UINT count)
|
||||
return RES_ERROR;
|
||||
}
|
||||
|
||||
#if __CORTEX_M >= 0x07
|
||||
SCB_CleanInvalidateDCache();
|
||||
#endif
|
||||
|
||||
// Wait for the reading process to complete or a timeout to occur
|
||||
uint32_t timeout = HAL_GetTick();
|
||||
while((ReadStatus == 0) && ((HAL_GetTick() - timeout) < SD_TIMEOUT));
|
||||
|
||||
#if __CORTEX_M >= 0x07
|
||||
SCB_CleanInvalidateDCache();
|
||||
#endif
|
||||
|
||||
if (ReadStatus == 0) {
|
||||
TRACE("SD read timeout (s:%u c:%u)", sector, (uint32_t)count);
|
||||
return RES_ERROR;
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "hal/gpio.h"
|
||||
#include "sdcard_spi.h"
|
||||
|
||||
|
||||
#include "hal.h"
|
||||
|
||||
#if defined(SD_SPI)
|
||||
@@ -39,10 +40,10 @@
|
||||
|
||||
static const stm32_spi_t _sd_spi_hw = {
|
||||
.SPIx = SD_SPI,
|
||||
.SPI_GPIOx = SD_GPIO,
|
||||
.SPI_Pins = SD_GPIO_PIN_SCK | SD_GPIO_PIN_MISO | SD_GPIO_PIN_MOSI,
|
||||
.CS_GPIOx = SD_GPIO,
|
||||
.CS_Pin = SD_GPIO_PIN_CS,
|
||||
.SCK = SD_GPIO_PIN_SCK,
|
||||
.MISO = SD_GPIO_PIN_MISO,
|
||||
.MOSI = SD_GPIO_PIN_MOSI,
|
||||
.CS = SD_GPIO_PIN_CS,
|
||||
.DMA = SD_SPI_DMA,
|
||||
.DMA_Channel = SD_SPI_DMA_CHANNEL,
|
||||
.txDMA_Stream = SD_SPI_DMA_TX_STREAM,
|
||||
|
||||
@@ -62,6 +62,8 @@ set(FIRMWARE_SRC ${FIRMWARE_SRC} $<TARGET_OBJECTS:stm32_stdperiph>)
|
||||
set(BOOTLOADER_SRC ${BOOTLOADER_SRC} $<TARGET_OBJECTS:stm32_stdperiph>)
|
||||
|
||||
if(NOT NATIVE_BUILD)
|
||||
include_directories(targets/common/arm/stm32/f2)
|
||||
|
||||
#
|
||||
# FreeRTOS port specific include
|
||||
#
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define __CCMRAM
|
||||
#define __DMA
|
||||
#define __DMA_NO_CACHE
|
||||
#define __IRAM
|
||||
#define __SDRAM
|
||||
#define __SDRAMFONTS
|
||||
|
||||
@@ -97,6 +97,8 @@ set(FIRMWARE_SRC ${FIRMWARE_SRC} $<TARGET_OBJECTS:stm32_stdperiph>)
|
||||
set(BOOTLOADER_SRC ${BOOTLOADER_SRC} $<TARGET_OBJECTS:stm32_stdperiph>)
|
||||
|
||||
if(NOT NATIVE_BUILD)
|
||||
include_directories(targets/common/arm/stm32/f4)
|
||||
|
||||
#
|
||||
# FreeRTOS port specific include
|
||||
#
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define __CCMRAM __attribute__((section(".ccm"), aligned(4)))
|
||||
#define __DMA __attribute__((section(".ram"), aligned(4)))
|
||||
#define __DMA_NO_CACHE __DMA
|
||||
#define __IRAM
|
||||
|
||||
#if defined(SDRAM)
|
||||
#define __SDRAM __attribute__((section(".sdram"), aligned(4)))
|
||||
#define __SDRAMFONTS __attribute__((section(".sdram_fonts"), aligned(4)))
|
||||
#else
|
||||
#define __SDRAM
|
||||
#define __SDRAMFONTS
|
||||
#endif
|
||||
|
||||
@@ -19,10 +19,14 @@
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "flash_driver.h"
|
||||
|
||||
#include "stm32_cmsis.h"
|
||||
#include "stm32_hal.h"
|
||||
|
||||
uint32_t stm32_flash_get_size_kb()
|
||||
#include <string.h>
|
||||
|
||||
static uint32_t stm32_flash_get_size_kb()
|
||||
{
|
||||
uint32_t flash_size_kb = *(const uint32_t*)FLASHSIZE_BASE;
|
||||
return flash_size_kb;
|
||||
@@ -30,7 +34,7 @@ uint32_t stm32_flash_get_size_kb()
|
||||
|
||||
#if defined(STM32F2) || defined(STM32F4)
|
||||
|
||||
uint32_t stm32_flash_get_sector(uint32_t address)
|
||||
static uint32_t stm32_flash_get_sector(uint32_t address)
|
||||
{
|
||||
uint32_t sector;
|
||||
uint32_t sector_addr = address & 0xFFFFF;
|
||||
@@ -68,12 +72,12 @@ static uint32_t _flash_sector_address(uint32_t sector, uint32_t bank)
|
||||
return addr;
|
||||
}
|
||||
|
||||
uint32_t stm32_flash_get_sector_size(uint32_t sector)
|
||||
static uint32_t stm32_flash_get_sector_size(uint32_t sector)
|
||||
{
|
||||
// bank 2
|
||||
if (sector >= 12) sector -= 12;
|
||||
|
||||
// first 4: 16KB
|
||||
// first 4: 16KB
|
||||
if (sector < 4) return 16 * 1024;
|
||||
// sector 4: 64KB
|
||||
if (sector == 4) return 64 * 1024;
|
||||
@@ -83,7 +87,7 @@ uint32_t stm32_flash_get_sector_size(uint32_t sector)
|
||||
|
||||
#elif defined(STM32H7) || defined(STM32H7RS)
|
||||
|
||||
uint32_t stm32_flash_get_sector(uint32_t address)
|
||||
static uint32_t stm32_flash_get_sector(uint32_t address)
|
||||
{
|
||||
uint32_t sector_addr = address & 0xFFFFF;
|
||||
return sector_addr / FLASH_SECTOR_SIZE;
|
||||
@@ -99,7 +103,7 @@ static uint32_t _flash_sector_address(uint32_t sector, uint32_t bank)
|
||||
return addr;
|
||||
}
|
||||
|
||||
uint32_t stm32_flash_get_sector_size(uint32_t sector)
|
||||
static uint32_t stm32_flash_get_sector_size(uint32_t sector)
|
||||
{
|
||||
(void)sector;
|
||||
return FLASH_SECTOR_SIZE;
|
||||
@@ -107,7 +111,7 @@ uint32_t stm32_flash_get_sector_size(uint32_t sector)
|
||||
|
||||
#endif
|
||||
|
||||
uint32_t stm32_flash_get_bank(uint32_t address)
|
||||
static uint32_t stm32_flash_get_bank(uint32_t address)
|
||||
{
|
||||
#if defined(DUAL_BANK)
|
||||
return ((address & 0x100000) != 0x100000) ? FLASH_BANK_1 : FLASH_BANK_2;
|
||||
@@ -120,10 +124,10 @@ uint32_t stm32_flash_get_bank(uint32_t address)
|
||||
#endif
|
||||
}
|
||||
|
||||
void stm32_flash_unlock() { HAL_FLASH_Unlock(); }
|
||||
void stm32_flash_lock() { HAL_FLASH_Lock(); }
|
||||
static inline void stm32_flash_unlock() { HAL_FLASH_Unlock(); }
|
||||
static inline void stm32_flash_lock() { HAL_FLASH_Lock(); }
|
||||
|
||||
int stm32_flash_erase_sector(uint32_t address)
|
||||
static int stm32_flash_erase_sector(uint32_t address)
|
||||
{
|
||||
FLASH_EraseInitTypeDef eraseInit;
|
||||
eraseInit.TypeErase = FLASH_TYPEERASE_SECTORS;
|
||||
@@ -138,12 +142,16 @@ int stm32_flash_erase_sector(uint32_t address)
|
||||
eraseInit.VoltageRange = FLASH_VOLTAGE_RANGE_3;
|
||||
#endif
|
||||
|
||||
int ret = 0;
|
||||
uint32_t sector_errors = 0;
|
||||
|
||||
stm32_flash_unlock();
|
||||
if (HAL_FLASHEx_Erase(&eraseInit, §or_errors) != HAL_OK) {
|
||||
return -1;
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
stm32_flash_lock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if defined(STM32H7)
|
||||
@@ -160,23 +168,42 @@ int stm32_flash_erase_sector(uint32_t address)
|
||||
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, *p_data)
|
||||
#endif
|
||||
|
||||
int stm32_flash_program(uint32_t address, uint8_t* data, uint32_t len)
|
||||
static int stm32_flash_program(uint32_t address, void* data, uint32_t len)
|
||||
{
|
||||
uint32_t* p_data = (uint32_t*)data;
|
||||
uint32_t end_addr = address + len;
|
||||
|
||||
int ret = 0;
|
||||
stm32_flash_unlock();
|
||||
while (address < end_addr) {
|
||||
if (_FLASH_PROGRAM(address, p_data) != HAL_OK) {
|
||||
return -1;
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
address += sizeof(uint32_t) * FLASH_PROG_WORDS;
|
||||
p_data += FLASH_PROG_WORDS;
|
||||
}
|
||||
|
||||
stm32_flash_lock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int stm32_flash_read(uint32_t address, void* data, uint32_t len)
|
||||
{
|
||||
memcpy(data, (void*)address, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const etx_flash_driver_t stm32_flash_driver = {
|
||||
.get_size_kb = stm32_flash_get_size_kb,
|
||||
.get_sector = stm32_flash_get_sector,
|
||||
.get_sector_size = stm32_flash_get_sector_size,
|
||||
.erase_sector = stm32_flash_erase_sector,
|
||||
.program = stm32_flash_program,
|
||||
.read = stm32_flash_read,
|
||||
};
|
||||
|
||||
// Legacy API
|
||||
|
||||
#define FLASH_PAGESIZE 256
|
||||
@@ -215,6 +242,21 @@ uint32_t isFirmwareStart(const uint8_t * buffer)
|
||||
if ((block[2] & 0xFFC00000) != 0x08000000) {
|
||||
return 0;
|
||||
}
|
||||
#elif defined(STM32H7)
|
||||
// // Stack pointer in D1 RAM
|
||||
// if ((block[0] & 0xFFFC0000) != 0x24080000) {
|
||||
// Stack pointer in DTCM RAM
|
||||
if ((block[0] & 0xFFFF0000) != 0x20020000) {
|
||||
return 0;
|
||||
}
|
||||
// First ISR pointer in FLASH
|
||||
if ((block[1] & 0xF0000000) != 0x90000000) {
|
||||
return 0;
|
||||
}
|
||||
// Second ISR pointer in FLASH
|
||||
if ((block[2] & 0xF0000000) != 0xC0000000) {
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
// Stack pointer in RAM
|
||||
if ((block[0] & 0xFFFC0000) != 0x20000000) {
|
||||
|
||||
@@ -23,24 +23,26 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t stm32_flash_get_size_kb();
|
||||
uint32_t stm32_flash_get_sector(uint32_t address);
|
||||
uint32_t stm32_flash_get_sector_size(uint32_t sector);
|
||||
uint32_t stm32_flash_get_bank(uint32_t address);
|
||||
#include "hal/flash_driver.h"
|
||||
|
||||
void stm32_flash_unlock();
|
||||
void stm32_flash_lock();
|
||||
// uint32_t stm32_flash_get_size_kb();
|
||||
// uint32_t stm32_flash_get_sector(uint32_t address);
|
||||
// uint32_t stm32_flash_get_sector_size(uint32_t sector);
|
||||
// uint32_t stm32_flash_get_bank(uint32_t address);
|
||||
|
||||
int stm32_flash_erase_sector(uint32_t address);
|
||||
int stm32_flash_program(uint32_t address, uint8_t* data, uint32_t len);
|
||||
// void stm32_flash_unlock();
|
||||
// void stm32_flash_lock();
|
||||
|
||||
// int stm32_flash_erase_sector(uint32_t address);
|
||||
// int stm32_flash_program(uint32_t address, uint8_t* data, uint32_t len);
|
||||
|
||||
extern const etx_flash_driver_t stm32_flash_driver;
|
||||
extern const void* stm32_flash_dfu_media;
|
||||
|
||||
// Legacy API
|
||||
|
||||
uint32_t isFirmwareStart(const uint8_t * buffer);
|
||||
uint32_t isBootloaderStart(const uint8_t * buffer);
|
||||
|
||||
void unlockFlash();
|
||||
void lockFlash();
|
||||
|
||||
void flashWrite(uint32_t* address, const uint32_t* buffer);
|
||||
|
||||
|
||||
@@ -85,7 +85,9 @@ static void _fs_parse(STRUCT_HALL *hallBuffer, unsigned char ch)
|
||||
hallBuffer->length = ch;
|
||||
hallBuffer->dataIndex = 0;
|
||||
hallBuffer->status = GET_DATA;
|
||||
if (0 == hallBuffer->length) {
|
||||
if(hallBuffer->length > HALLSTICK_BUFF_SIZE - 5) { // buffer size - header size (1 byte header + 1 byte ID + 1 byte length + 2 bytes CRC = 5 bytes)
|
||||
hallBuffer->status = GET_START;
|
||||
} else if (0 == hallBuffer->length) {
|
||||
hallBuffer->status = GET_CHECKSUM;
|
||||
hallBuffer->checkSum = 0;
|
||||
}
|
||||
@@ -142,6 +144,7 @@ static void flysky_gimbal_loop(void*)
|
||||
|
||||
switch (HallProtocol.hallID.hall_Id.receiverID) {
|
||||
case TRANSFER_DIR_TXMCU:
|
||||
case TRANSFER_DIR_RFMODULE:
|
||||
if (HallProtocol.hallID.hall_Id.packetID ==
|
||||
FLYSKY_HALL_RESP_TYPE_VALUES) {
|
||||
int16_t* p_values = (int16_t*)HallProtocol.data;
|
||||
|
||||
@@ -22,8 +22,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "hal/serial_port.h"
|
||||
|
||||
#define HALLSTICK_BUFF_SIZE ( 512 )
|
||||
|
||||
// Max packet size = 1 byte header + 1 byte ID + 1 byte length + 25 bytes payload + 2 bytes CRC = 30 bytes
|
||||
// using 32 bytes is more than enough
|
||||
#define HALLSTICK_BUFF_SIZE ( 32 )
|
||||
#define FLYSKY_HALL_BAUDRATE ( 921600 )
|
||||
#define FLYSKY_HALL_CHANNEL_COUNT ( 4 )
|
||||
|
||||
|
||||
@@ -2,89 +2,149 @@ set(CPU_FAMILY STM32)
|
||||
set(MCU cortex-m7)
|
||||
set(FPU_FLAGS "-mfloat-abi=hard -mfpu=fpv4-sp-d16")
|
||||
|
||||
if(NOT NATIVE_BUILD)
|
||||
# STM32 HAL/LL library
|
||||
set(STM32CUBE_DIR ${THIRDPARTY_DIR}/STM32H7xx_HAL_Driver)
|
||||
include_directories(${STM32CUBE_DIR}/Inc)
|
||||
|
||||
add_library(stm32cube_ll OBJECT EXCLUDE_FROM_ALL
|
||||
# LL drivers
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_ll_gpio.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_ll_fmc.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_ll_tim.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_ll_dma.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_ll_usart.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_ll_rcc.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_ll_exti.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_ll_sdmmc.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_ll_adc.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_ll_spi.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_ll_usb.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_ll_dma2d.c
|
||||
# HAL drivers
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_pwr.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_pwr_ex.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_rcc.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_rcc_ex.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_rtc.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_i2c.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_i2c_ex.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_sd.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_dma.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_mdma.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_pcd.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_pcd_ex.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_qspi.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_flash.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_flash_ex.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_cortex.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_sdram.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_gpio.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_dsi.c
|
||||
${STM32CUBE_DIR}/Src/stm32h7xx_hal_ltdc.c
|
||||
)
|
||||
# CMSIS library
|
||||
set(CMSIS_DIR ${THIRDPARTY_DIR}/CMSIS)
|
||||
include_directories(${CMSIS_DIR}/Include)
|
||||
|
||||
if(CPU_TYPE STREQUAL STM32H7RS)
|
||||
include_directories(${CMSIS_DIR}/Device/ST/STM32H7RS/Include)
|
||||
set(CMSIS_SRC
|
||||
targets/common/arm/stm32/h7/system_stm32h7rsxx.c
|
||||
)
|
||||
else()
|
||||
include_directories(${CMSIS_DIR}/Device/ST/STM32H7xx/Include)
|
||||
set(CMSIS_SRC
|
||||
targets/common/arm/stm32/h7/system_stm32h7xx.c
|
||||
)
|
||||
endif()
|
||||
|
||||
|
||||
if(NOT NATIVE_BUILD)
|
||||
if(CPU_TYPE STREQUAL STM32H7RS)
|
||||
set(STM32CUBE_PREFIX stm32h7rsxx)
|
||||
set(STM32CUBE_DIR ${THIRDPARTY_DIR}/STM32H7RS_HAL_Driver)
|
||||
set(STM32CUBE_SRC_PREFIX ${STM32CUBE_DIR}/Src/${STM32CUBE_PREFIX})
|
||||
set(STM32CUBE_SRC
|
||||
# LL drivers
|
||||
${STM32CUBE_SRC_PREFIX}_ll_gpio.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_fmc.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_tim.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_dma.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_usart.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_rcc.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_exti.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_sdmmc.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_adc.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_spi.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_usb.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_dma2d.c
|
||||
# HAL drivers
|
||||
${STM32CUBE_SRC_PREFIX}_hal.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_pwr.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_pwr_ex.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_rcc.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_rcc_ex.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_rtc.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_i2c.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_i2c_ex.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_sd.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_dma.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_pcd.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_pcd_ex.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_xspi.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_flash.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_flash_ex.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_cortex.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_sdram.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_gpio.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_ltdc.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_lptim.c
|
||||
)
|
||||
else()
|
||||
set(STM32CUBE_PREFIX stm32h7xx)
|
||||
set(STM32CUBE_DIR ${THIRDPARTY_DIR}/STM32H7xx_HAL_Driver)
|
||||
set(STM32CUBE_SRC_PREFIX ${STM32CUBE_DIR}/Src/${STM32CUBE_PREFIX})
|
||||
set(STM32CUBE_SRC
|
||||
# LL drivers
|
||||
${STM32CUBE_SRC_PREFIX}_ll_gpio.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_fmc.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_tim.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_dma.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_usart.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_rcc.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_exti.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_sdmmc.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_adc.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_spi.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_usb.c
|
||||
${STM32CUBE_SRC_PREFIX}_ll_dma2d.c
|
||||
# HAL drivers
|
||||
${STM32CUBE_SRC_PREFIX}_hal.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_pwr.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_pwr_ex.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_rcc.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_rcc_ex.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_rtc.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_i2c.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_i2c_ex.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_sd.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_dma.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_mdma.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_pcd.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_pcd_ex.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_qspi.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_flash.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_flash_ex.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_cortex.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_sdram.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_gpio.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_dsi.c
|
||||
${STM32CUBE_SRC_PREFIX}_hal_ltdc.c
|
||||
)
|
||||
endif()
|
||||
|
||||
# STM32 HAL/LL library
|
||||
include_directories(${STM32CUBE_DIR}/Inc)
|
||||
|
||||
add_library(stm32cube_ll OBJECT EXCLUDE_FROM_ALL ${STM32CUBE_SRC})
|
||||
target_compile_definitions(stm32cube_ll PRIVATE USE_FULL_LL_DRIVER)
|
||||
target_include_directories(stm32cube_ll PRIVATE
|
||||
${THIRDPARTY_DIR}/CMSIS/Device/ST/STM32H7xx/Include
|
||||
${THIRDPARTY_DIR}/CMSIS/Include)
|
||||
|
||||
set(FIRMWARE_SRC ${FIRMWARE_SRC} $<TARGET_OBJECTS:stm32cube_ll>)
|
||||
set(BOOTLOADER_SRC ${BOOTLOADER_SRC} $<TARGET_OBJECTS:stm32cube_ll>)
|
||||
endif()
|
||||
|
||||
# StdPeriph library
|
||||
set(STM32LIB_DIR ${THIRDPARTY_DIR})
|
||||
include_directories(${STM32LIB_DIR}/CMSIS/Device/ST/STM32H7xx/Include)
|
||||
include_directories(${STM32LIB_DIR}/CMSIS/Include)
|
||||
|
||||
add_definitions(-DSTM32H7)
|
||||
|
||||
set(STM32_STDPERIPH_SRC
|
||||
targets/common/arm/stm32/h7/system_stm32h7xx.c
|
||||
)
|
||||
if(CPU_TYPE STREQUAL STM32H7RS)
|
||||
add_definitions(-DSTM32H7RS)
|
||||
else()
|
||||
add_definitions(-DSTM32H7)
|
||||
endif()
|
||||
|
||||
if(CPU_TYPE_FULL STREQUAL STM32H750Ix)
|
||||
set(STM32_STDPERIPH_SRC ${STM32_STDPERIPH_SRC}
|
||||
set(CMSIS_SRC ${CMSIS_SRC}
|
||||
boards/generic_stm32/startup/startup_stm32h750xx.s
|
||||
)
|
||||
)
|
||||
elseif(CPU_TYPE_FULL STREQUAL STM32H747Ix)
|
||||
set(STM32_STDPERIPH_SRC ${STM32_STDPERIPH_SRC}
|
||||
set(CMSIS_SRC ${CMSIS_SRC}
|
||||
boards/generic_stm32/startup/startup_stm32h747xx.s
|
||||
)
|
||||
elseif(CPU_TYPE_FULL STREQUAL STM32H7S7xx)
|
||||
set(CMSIS_SRC ${CMSIS_SRC}
|
||||
boards/generic_stm32/startup/startup_stm32h7s7xx.s
|
||||
)
|
||||
else()
|
||||
message("### ERROR ####: ${CPU_TYPE_FULL}")
|
||||
endif()
|
||||
|
||||
add_library(stm32_stdperiph OBJECT EXCLUDE_FROM_ALL
|
||||
${STM32_STDPERIPH_SRC}
|
||||
add_library(cmsis OBJECT EXCLUDE_FROM_ALL
|
||||
${CMSIS_SRC}
|
||||
)
|
||||
|
||||
set(FIRMWARE_SRC ${FIRMWARE_SRC} $<TARGET_OBJECTS:stm32_stdperiph>)
|
||||
set(BOOTLOADER_SRC ${BOOTLOADER_SRC} $<TARGET_OBJECTS:stm32_stdperiph>)
|
||||
set(FIRMWARE_SRC ${FIRMWARE_SRC} $<TARGET_OBJECTS:cmsis>)
|
||||
set(BOOTLOADER_SRC ${BOOTLOADER_SRC} $<TARGET_OBJECTS:cmsis>)
|
||||
|
||||
if(NOT NATIVE_BUILD)
|
||||
include_directories(targets/common/arm/stm32/h7)
|
||||
|
||||
#
|
||||
# FreeRTOS port specific include
|
||||
#
|
||||
@@ -112,4 +172,9 @@ if(NOT NATIVE_BUILD)
|
||||
# System and FreeRTOS port files
|
||||
#
|
||||
set(FIRMWARE_SRC ${FIRMWARE_SRC} $<TARGET_OBJECTS:freertos>)
|
||||
|
||||
#
|
||||
# Firmware description
|
||||
#
|
||||
set(FIRMWARE_SRC ${FIRMWARE_SRC} fw_desc.c)
|
||||
endif()
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define __CCMRAM __attribute__((section(".ccm"), aligned(4)))
|
||||
#define __DMA __attribute__((section(".ram"), aligned(4)))
|
||||
#define __DMA_NO_CACHE __attribute__((section(".dram"), aligned(4)))
|
||||
#define __IRAM __attribute__((section(".iram")))
|
||||
#define __SDRAM __attribute__((section(".sdram"), aligned(4)))
|
||||
#define __SDRAMFONTS __attribute__((section(".sdram_fonts"), aligned(4)))
|
||||
|
||||
|
||||
@@ -0,0 +1,285 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file system_stm32h7rsxx.c
|
||||
* @author MCD Application Team
|
||||
* @brief CMSIS Cortex-M7 Device Peripheral Access Layer System Source File
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2022 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*
|
||||
* This file provides two functions and one global variable to be called from
|
||||
* user application:
|
||||
* - SystemInit(): This function is called at startup just after reset and
|
||||
* before branch to main program. This call is made inside
|
||||
* the "startup_stm32h7rsxx.s" file.
|
||||
*
|
||||
* - SystemCoreClock variable: Contains the core clock (sys_cpu_ck), it can
|
||||
* be used by the user application to setup the
|
||||
* SysTick timer or configure other parameters.
|
||||
*
|
||||
* - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
|
||||
* be called whenever the core clock is changed
|
||||
* during program execution.
|
||||
*
|
||||
* After each device reset the HSI (64 MHz) is used as system clock source.
|
||||
* Then SystemInit() function is called, in "startup_stm32h7rsxx.s" file, to
|
||||
* optionally configure the system clock before to branch to main program.
|
||||
*
|
||||
*=============================================================================
|
||||
*/
|
||||
|
||||
/** @addtogroup CMSIS
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32H7RSxx_System
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32H7RSxx_System_Private_Includes
|
||||
* @{
|
||||
*/
|
||||
|
||||
#include "stm32h7rsxx.h"
|
||||
#include <math.h>
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32H7RSxx_System_Private_TypesDefinitions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32H7RSxx_System_Private_Defines
|
||||
* @{
|
||||
*/
|
||||
#if !defined (HSE_VALUE)
|
||||
#define HSE_VALUE 24000000UL /*!< Value of the High-Speed External oscillator in Hz */
|
||||
#endif /* HSE_VALUE */
|
||||
|
||||
#if !defined (HSI_VALUE)
|
||||
#define HSI_VALUE 64000000UL /*!< Value of the High-Speed Internal oscillator in Hz */
|
||||
#endif /* HSI_VALUE */
|
||||
|
||||
#if !defined (CSI_VALUE)
|
||||
#define CSI_VALUE 4000000UL /*!< Value of the Low-power Internal oscillator in Hz */
|
||||
#endif /* CSI_VALUE */
|
||||
|
||||
/*!< The VTOR location information is based on information from the linker with a dependency
|
||||
on the IDE, the cortex register is updated using the INTVECT_START.
|
||||
*/
|
||||
#if defined(__ICCARM__)
|
||||
extern uint32_t __vector_table;
|
||||
#define INTVECT_START ((uint32_t)& __vector_table)
|
||||
#elif defined(__CC_ARM) || defined(__ARMCC_VERSION)
|
||||
extern void * __Vectors;
|
||||
#define INTVECT_START ((uint32_t) & __Vectors)
|
||||
#elif defined(__GNUC__)
|
||||
extern void * g_pfnVectors;
|
||||
#define INTVECT_START ((uint32_t)& g_pfnVectors)
|
||||
#endif /* __ICCARM__*/
|
||||
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32H7RSxx_System_Private_Macros
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32H7RSxx_System_Private_Variables
|
||||
* @{
|
||||
*/
|
||||
/* The SystemCoreClock variable is updated in two ways:
|
||||
1) by calling CMSIS function SystemCoreClockUpdate()
|
||||
2) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
|
||||
Note: If you use this function to configure the system clock; then there
|
||||
is no need to call the first function listed above, since SystemCoreClock
|
||||
variable is updated automatically.
|
||||
*/
|
||||
uint32_t SystemCoreClock = HSI_VALUE;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32H7RSxx_System_Private_FunctionPrototypes
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @addtogroup STM32H7RSxx_System_Private_Functions
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Setup the microcontroller system.
|
||||
* @retval None
|
||||
*/
|
||||
|
||||
void SystemInit(void)
|
||||
{
|
||||
/* Configure the Vector Table location -------------------------------------*/
|
||||
SCB->VTOR = INTVECT_START;
|
||||
|
||||
/* FPU settings ------------------------------------------------------------*/
|
||||
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
|
||||
SCB->CPACR |= ((3UL << 20U)|(3UL << 22U)); /* set CP10 and CP11 Full Access */
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update SystemCoreClock variable according to RCC registers values.
|
||||
* The SystemCoreClock variable contains the core clock (sys_cpu_ck), it can
|
||||
* be used by the user application to setup the SysTick timer or configure
|
||||
* other parameters.
|
||||
*
|
||||
* @note Each time the core clock changes, this function must be called
|
||||
* to update SystemCoreClock variable value. Otherwise, any configuration
|
||||
* based on this variable will be incorrect.
|
||||
*
|
||||
* @note - The system frequency computed by this function is not the real
|
||||
* frequency in the chip. It is calculated based on the predefined
|
||||
* constant and the selected clock source:
|
||||
*
|
||||
* - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(*)
|
||||
*
|
||||
* - If SYSCLK source is CSI, SystemCoreClock will contain the CSI_VALUE(**)
|
||||
*
|
||||
* - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
|
||||
*
|
||||
* - If SYSCLK source is PLL, SystemCoreClock will contain the HSI_VALUE(*)
|
||||
* or CSI_VALUE(**) or HSE_VALUE(***) multiplied/divided by the PLL factors.
|
||||
*
|
||||
* (*) HSI_VALUE is a constant defined in stm32h7rsxx_hal.h file (default value
|
||||
* 64 MHz) but the real value may vary depending on the variations
|
||||
* in voltage and temperature.
|
||||
*
|
||||
* (**) CSI_VALUE is a constant defined in stm32h7rsxx_hal.h file (default value
|
||||
* 4 MHz) but the real value may vary depending on the variations
|
||||
* in voltage and temperature.
|
||||
*
|
||||
* (***) HSE_VALUE is a constant defined in stm32h7rsxx_hal.h file (default value
|
||||
* 24 MHz), user has to ensure that HSE_VALUE is same as the real
|
||||
* frequency of the crystal used. Otherwise, this function may
|
||||
* have wrong result.
|
||||
*
|
||||
* - The result of this function could be not correct when using fractional
|
||||
* value for HSE crystal.
|
||||
*
|
||||
* @retval None
|
||||
*/
|
||||
void SystemCoreClockUpdate(void)
|
||||
{
|
||||
uint32_t sysclk, hsivalue, pllsource, pllm, pllp, core_presc;
|
||||
float_t pllfracn, pllvco;
|
||||
|
||||
/* Get SYSCLK source -------------------------------------------------------*/
|
||||
switch (RCC->CFGR & RCC_CFGR_SWS)
|
||||
{
|
||||
case 0x00: /* HSI used as system clock source (default after reset) */
|
||||
sysclk = (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV) >> RCC_CR_HSIDIV_Pos));
|
||||
break;
|
||||
|
||||
case 0x08: /* CSI used as system clock source */
|
||||
sysclk = CSI_VALUE;
|
||||
break;
|
||||
|
||||
case 0x10: /* HSE used as system clock source */
|
||||
sysclk = HSE_VALUE;
|
||||
break;
|
||||
|
||||
case 0x18: /* PLL1 used as system clock source */
|
||||
/* PLL1_VCO = (HSE_VALUE or HSI_VALUE or CSI_VALUE/ PLLM) * PLLN
|
||||
SYSCLK = PLL1_VCO / PLL1R
|
||||
*/
|
||||
pllsource = (RCC->PLLCKSELR & RCC_PLLCKSELR_PLLSRC);
|
||||
pllm = ((RCC->PLLCKSELR & RCC_PLLCKSELR_DIVM1) >> RCC_PLLCKSELR_DIVM1_Pos) ;
|
||||
if ((RCC->PLLCFGR & RCC_PLLCFGR_PLL1FRACEN) != 0U)
|
||||
{
|
||||
pllfracn = (float_t)(uint32_t)(((RCC->PLL1FRACR & RCC_PLL1FRACR_FRACN)>> RCC_PLL1FRACR_FRACN_Pos));
|
||||
}
|
||||
else
|
||||
{
|
||||
pllfracn = (float_t)0U;
|
||||
}
|
||||
|
||||
if (pllm != 0U)
|
||||
{
|
||||
switch (pllsource)
|
||||
{
|
||||
case 0x02: /* HSE used as PLL1 clock source */
|
||||
pllvco = ((float_t)HSE_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR1 & RCC_PLL1DIVR1_DIVN) + (pllfracn/(float_t)0x2000) +(float_t)1 );
|
||||
break;
|
||||
|
||||
case 0x01: /* CSI used as PLL1 clock source */
|
||||
pllvco = ((float_t)CSI_VALUE / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR1 & RCC_PLL1DIVR1_DIVN) + (pllfracn/(float_t)0x2000) +(float_t)1 );
|
||||
break;
|
||||
|
||||
case 0x00: /* HSI used as PLL1 clock source */
|
||||
default:
|
||||
hsivalue = (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV) >> RCC_CR_HSIDIV_Pos));
|
||||
pllvco = ( (float_t)hsivalue / (float_t)pllm) * ((float_t)(uint32_t)(RCC->PLL1DIVR1 & RCC_PLL1DIVR1_DIVN) + (pllfracn/(float_t)0x2000) +(float_t)1 );
|
||||
break;
|
||||
}
|
||||
pllp = (((RCC->PLL1DIVR1 & RCC_PLL1DIVR1_DIVP) >> RCC_PLL1DIVR1_DIVP_Pos) + 1U ) ;
|
||||
sysclk = (uint32_t)(float_t)(pllvco/(float_t)pllp);
|
||||
}
|
||||
else
|
||||
{
|
||||
sysclk = 0U;
|
||||
}
|
||||
break;
|
||||
|
||||
default: /* Unexpected, default to HSI used as system clock source (default after reset) */
|
||||
sysclk = (HSI_VALUE >> ((RCC->CR & RCC_CR_HSIDIV) >> RCC_CR_HSIDIV_Pos));
|
||||
break;
|
||||
}
|
||||
|
||||
/* system clock frequency : CM7 CPU frequency */
|
||||
core_presc = (RCC->CDCFGR & RCC_CDCFGR_CPRE);
|
||||
if (core_presc >= 8U)
|
||||
{
|
||||
SystemCoreClock = (sysclk >> (core_presc - RCC_CDCFGR_CPRE_3 + 1U));
|
||||
}
|
||||
else
|
||||
{
|
||||
SystemCoreClock = sysclk;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -178,7 +178,10 @@
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void SystemInit (void)
|
||||
|
||||
#define BOOTSTRAP __attribute__((section(".bootstrap")))
|
||||
|
||||
BOOTSTRAP void SystemInit (void)
|
||||
{
|
||||
#if defined (DATA_IN_D2_SRAM)
|
||||
__IO uint32_t tmpreg;
|
||||
@@ -481,21 +484,21 @@ void I2C2_EV_IRQHandler(void){asm("BKPT");}
|
||||
void I2C2_ER_IRQHandler(void){asm("BKPT");}
|
||||
void SPI1_IRQHandler(void){asm("BKPT");}
|
||||
void SPI2_IRQHandler(void){asm("BKPT");}
|
||||
void EXTI15_10_IRQHandler(void){asm("BKPT");}
|
||||
//void EXTI15_10_IRQHandler(void){asm("BKPT");}
|
||||
void RTC_Alarm_IRQHandler(void){asm("BKPT");}
|
||||
void TIM8_CC_IRQHandler(void){asm("BKPT");}
|
||||
void DMA1_Stream7_IRQHandler(void){asm("BKPT");}
|
||||
//void DMA1_Stream7_IRQHandler(void){asm("BKPT");}
|
||||
void FMC_IRQHandler(void){asm("BKPT");}
|
||||
void TIM5_IRQHandler(void){asm("BKPT");}
|
||||
void SPI3_IRQHandler(void){asm("BKPT");}
|
||||
void UART5_IRQHandler(void){asm("BKPT");}
|
||||
//void UART5_IRQHandler(void){asm("BKPT");}
|
||||
void TIM7_IRQHandler(void){asm("BKPT");}
|
||||
//void DMA2_Stream0_IRQHandler(void){asm("BKPT");}
|
||||
void DMA2_Stream2_IRQHandler(void){asm("BKPT");}
|
||||
void DMA2_Stream3_IRQHandler(void){asm("BKPT");}
|
||||
//void DMA2_Stream4_IRQHandler(void){asm("BKPT");}
|
||||
void ETH_IRQHandler(void){asm("BKPT");}
|
||||
void ETH_WKUP_IRQHandler(void){asm("BKPT");}
|
||||
//void ETH_WKUP_IRQHandler(void){asm("BKPT");}
|
||||
void FDCAN_CAL_IRQHandler(void){asm("BKPT");}
|
||||
void DMA2_Stream5_IRQHandler(void){asm("BKPT");}
|
||||
void DMA2_Stream6_IRQHandler(void){asm("BKPT");}
|
||||
@@ -510,7 +513,7 @@ void DCMI_IRQHandler(void){asm("BKPT");}
|
||||
void CRYP_IRQHandler(void){asm("BKPT");}
|
||||
void HASH_RNG_IRQHandler(void){asm("BKPT");}
|
||||
void FPU_IRQHandler(void){asm("BKPT");}
|
||||
void UART8_IRQHandler(void){asm("BKPT");}
|
||||
//void UART8_IRQHandler(void){asm("BKPT");}
|
||||
void SPI4_IRQHandler(void){asm("BKPT");}
|
||||
void SPI5_IRQHandler(void){asm("BKPT");}
|
||||
void SPI6_IRQHandler(void){asm("BKPT");}
|
||||
@@ -541,7 +544,6 @@ void DFSDM1_FLT2_IRQHandler(void){asm("BKPT");}
|
||||
void DFSDM1_FLT3_IRQHandler(void){asm("BKPT");}
|
||||
void SAI3_IRQHandler(void){asm("BKPT");}
|
||||
void SWPMI1_IRQHandler(void){asm("BKPT");}
|
||||
void TIM15_IRQHandler(void){asm("BKPT");}
|
||||
void TIM16_IRQHandler(void){asm("BKPT");}
|
||||
void MDIOS_WKUP_IRQHandler(void){asm("BKPT");}
|
||||
void MDIOS_IRQHandler(void){asm("BKPT");}
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
|
||||
void pwrInit()
|
||||
{
|
||||
|
||||
#if defined(SD_PRESENT_GPIO)
|
||||
gpio_init(SD_PRESENT_GPIO, GPIO_IN_PU, GPIO_PIN_SPEED_LOW);
|
||||
#endif
|
||||
@@ -37,13 +38,13 @@ void pwrInit()
|
||||
#endif
|
||||
|
||||
// Internal module power
|
||||
#if defined(HARDWARE_INTERNAL_MODULE)
|
||||
#if defined(HARDWARE_INTERNAL_MODULE) && defined(INTMODULE_PWR_GPIO)
|
||||
gpio_init(INTMODULE_PWR_GPIO, GPIO_OUT, GPIO_PIN_SPEED_LOW);
|
||||
INTERNAL_MODULE_OFF();
|
||||
#endif
|
||||
|
||||
// External module power
|
||||
#if defined(HARDWARE_EXTERNAL_MODULE)
|
||||
#if defined(HARDWARE_EXTERNAL_MODULE) && defined(EXTMODULE_PWR_GPIO)
|
||||
gpio_init(EXTMODULE_PWR_GPIO, GPIO_OUT, GPIO_PIN_SPEED_LOW);
|
||||
EXTERNAL_MODULE_PWR_OFF();
|
||||
#endif
|
||||
|
||||
@@ -72,7 +72,7 @@ void rtcInit()
|
||||
// Enable LSE Oscillator
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct;
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
// RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
|
||||
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) {
|
||||
|
||||
@@ -247,7 +247,7 @@ static sd_init_fsm_state_t _init_sd_fsm_step(const stm32_spi_t* spi,
|
||||
switch (state) {
|
||||
case SD_INIT_START:
|
||||
TRACE("SD_INIT_START");
|
||||
stm32_spi_init(spi, LL_SPI_DATAWIDTH_8BIT);
|
||||
stm32_spi_init(spi, LL_SPI_DATAWIDTH_8BIT, true);
|
||||
stm32_spi_set_max_baudrate(spi, SD_SPI_CLK_400K);
|
||||
return SD_INIT_SPI_POWER_SEQ;
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "stm32_spi.h"
|
||||
#include "delays_driver.h"
|
||||
#include "stm32_gpio.h"
|
||||
|
||||
#include "hal.h"
|
||||
|
||||
@@ -70,10 +71,10 @@ struct SpiFlashDescriptor {
|
||||
|
||||
const stm32_spi_t _flash_spi = {
|
||||
.SPIx = FLASH_SPI,
|
||||
.SPI_GPIOx = FLASH_SPI_GPIO,
|
||||
.SPI_Pins = FLASH_SPI_SCK_GPIO_PIN | FLASH_SPI_MISO_GPIO_PIN | FLASH_SPI_MOSI_GPIO_PIN,
|
||||
.CS_GPIOx = FLASH_SPI_CS_GPIO,
|
||||
.CS_Pin = FLASH_SPI_CS_GPIO_PIN,
|
||||
.SCK = FLASH_SPI_SCK_GPIO,
|
||||
.MISO = FLASH_SPI_MISO_GPIO,
|
||||
.MOSI = FLASH_SPI_MOSI_GPIO,
|
||||
.CS = FLASH_SPI_CS_GPIO,
|
||||
.DMA = FLASH_SPI_DMA,
|
||||
.DMA_Channel = FLASH_SPI_DMA_CHANNEL,
|
||||
.txDMA_Stream = FLASH_SPI_DMA_TX_STREAM,
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
*/
|
||||
|
||||
#include "stm32_adc.h"
|
||||
#include "memory_sections.h"
|
||||
#include "stm32_dma.h"
|
||||
#include "stm32_gpio_driver.h"
|
||||
|
||||
@@ -61,7 +62,7 @@ static uint32_t _adc_input_inhibt_mask = 0;
|
||||
static volatile uint32_t _adc_inhibit_mask;
|
||||
|
||||
// DMA buffers
|
||||
static uint16_t _adc_dma_buffer[MAX_ADC_INPUTS] __DMA;
|
||||
static uint16_t _adc_dma_buffer[MAX_ADC_INPUTS] __DMA_NO_CACHE;
|
||||
|
||||
// ADCs started
|
||||
static volatile uint8_t _adc_started_mask;
|
||||
|
||||
@@ -27,6 +27,8 @@ extern "C" {
|
||||
|
||||
#if defined(STM32H7)
|
||||
#include "CMSIS/Device/ST/STM32H7xx/Include/stm32h7xx.h"
|
||||
#elif defined(STM32H7RS)
|
||||
#include "CMSIS/Device/ST/STM32H7RS/Include/stm32h7rsxx.h"
|
||||
#elif defined(STM32F4)
|
||||
#include "CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h"
|
||||
#elif defined(STM32F2)
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#include "stm32_exti_driver.h"
|
||||
#include "hal.h"
|
||||
|
||||
#if defined(EXTI_PR1_PR)
|
||||
#if defined(EXTI_PR1_PR) || defined(EXTI_PR1_PR0)
|
||||
# define _PR_Reg PR1
|
||||
#else
|
||||
# define _PR_Reg PR
|
||||
@@ -67,7 +67,6 @@
|
||||
if (!_has_handler(_EXTI_HANDLERS(irq_name), last - first + 1)) { \
|
||||
NVIC_DisableIRQ(irq_name ## n); \
|
||||
} \
|
||||
return; \
|
||||
}
|
||||
|
||||
#if defined(USE_EXTI1_IRQ)
|
||||
@@ -229,3 +228,170 @@ void stm32_exti_disable(uint32_t line)
|
||||
|
||||
LL_EXTI_DisableIT_0_31(line);
|
||||
}
|
||||
|
||||
void stm32_exti_trigger_swi(uint32_t line)
|
||||
{
|
||||
LL_EXTI_GenerateSWI_0_31(line);
|
||||
}
|
||||
|
||||
#if defined(USE_CUSTOM_EXTI_IRQ)
|
||||
|
||||
|
||||
#if defined(EXTI_PR1_PR) || defined(EXTI_PR1_PR0)
|
||||
|
||||
#pragma GCC diagnostic push // disable warning created by codepaths never run when those warning would be correct
|
||||
#pragma GCC diagnostic ignored "-Wshift-count-overflow"
|
||||
#pragma GCC diagnostic ignored "-Wshift-count-negative"
|
||||
|
||||
#define _DEFINE_CUSTOM_EXTI_IRQ_HANDLER(irq_name, first, last) \
|
||||
__DEFINE_CUSTOM_EXTI_IRQ_HANDLER(irq_name, first, last)
|
||||
#define __DEFINE_CUSTOM_EXTI_IRQ_HANDLER(irq_name, first, last) \
|
||||
static stm32_exti_handler_t _EXTI_HANDLERS(irq_name) [last - first + 1] = { nullptr }; \
|
||||
extern "C" void irq_name ## Handler() \
|
||||
{ \
|
||||
/* Read Pending register */ \
|
||||
/* (shifted by start line) */ \
|
||||
/* since first and last are macro parameters */ \
|
||||
/* the strange looking code should be optimized away */ \
|
||||
uint32_t pr = 0; \
|
||||
int region = 0; \
|
||||
\
|
||||
if (first < 32) { \
|
||||
region = 0; \
|
||||
pr = LL_EXTI_ReadReg(PR1) >> first; \
|
||||
pr &= _PR_MASK(first, last); \
|
||||
} else if (first < 64) { \
|
||||
region = 1; \
|
||||
pr = LL_EXTI_ReadReg(PR2) >> (first - 32); \
|
||||
pr &= _PR_MASK(first - 32, last - 32); \
|
||||
} else { \
|
||||
region = 2; \
|
||||
pr = LL_EXTI_ReadReg(PR3) >> (first - 64); \
|
||||
pr &= _PR_MASK(first - 64, last - 64); \
|
||||
} \
|
||||
while (pr) { \
|
||||
uint32_t i = POSITION_VAL(pr); \
|
||||
/* Clear Pending Flag */ \
|
||||
if(region == 0) \
|
||||
LL_EXTI_ClearFlag_0_31(1 << (first + i)); \
|
||||
else if (region == 1) \
|
||||
LL_EXTI_ClearFlag_32_63(1 << (first - 32 + i)); \
|
||||
else \
|
||||
LL_EXTI_ClearFlag_64_95(1 << (first - 64 + i)); \
|
||||
pr &= ~(1 << i); \
|
||||
/* ... and trigger handler */ \
|
||||
stm32_exti_handler_t h_fct = _EXTI_HANDLERS(irq_name) [i]; \
|
||||
if (h_fct) h_fct(); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
#else
|
||||
# define _PR_Reg PR
|
||||
|
||||
|
||||
#define _DEFINE_CUSTOM_EXTI_IRQ_HANDLER(irq_name, first, last) \
|
||||
static stm32_exti_handler_t _EXTI_HANDLERS(irq_name) [last - first + 1] = { nullptr }; \
|
||||
extern "C" void irq_name ## Handler() \
|
||||
{ \
|
||||
/* Read Pending register */ \
|
||||
/* (shifted by start line) */ \
|
||||
uint32_t pr = LL_EXTI_ReadReg(_PR_Reg) >> first; \
|
||||
pr &= _PR_MASK(first,last); \
|
||||
while (pr) { \
|
||||
uint32_t i = POSITION_VAL(pr); \
|
||||
/* Clear Pending Flag */ \
|
||||
LL_EXTI_ClearFlag_0_31(1 << (first + i)); \
|
||||
pr &= ~(1 << i); \
|
||||
/* ... and trigger handler */ \
|
||||
stm32_exti_handler_t h_fct = _EXTI_HANDLERS(irq_name) [i]; \
|
||||
if (h_fct) h_fct(); \
|
||||
} \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#define _SET_CUSTOM_EXTI_IRQ_HANDLER(irq_name, first, last, line, cb) \
|
||||
__SET_CUSTOM_EXTI_IRQ_HANDLER(irq_name, first, last, line, cb)
|
||||
#define __SET_CUSTOM_EXTI_IRQ_HANDLER(irq_name, first, last, line, cb) \
|
||||
if (line >= first && line <= last) { \
|
||||
_EXTI_HANDLERS(irq_name)[line - first] = cb; \
|
||||
NVIC_SetPriority(irq_name ## n, irq_name ## _Priority); \
|
||||
NVIC_EnableIRQ(irq_name ## n); \
|
||||
return; \
|
||||
}
|
||||
|
||||
#define _CLEAR_CUSTOM_EXTI_IRQ_HANDLER(irq_name, first, last, line_pos) \
|
||||
__CLEAR_CUSTOM_EXTI_IRQ_HANDLER(irq_name, first, last, line_pos)
|
||||
#define __CLEAR_CUSTOM_EXTI_IRQ_HANDLER(irq_name, first, last, line_pos) \
|
||||
if (line >= first && line <= last) { \
|
||||
_EXTI_HANDLERS(irq_name)[line - first] = nullptr; \
|
||||
if (!_has_handler(_EXTI_HANDLERS(irq_name), last - first + 1)) { \
|
||||
NVIC_DisableIRQ(irq_name ## n); \
|
||||
} \
|
||||
}
|
||||
|
||||
_DEFINE_CUSTOM_EXTI_IRQ_HANDLER(CUSTOM_EXTI_IRQ_NAME, CUSTOM_EXTI_IRQ_LINE, CUSTOM_EXTI_IRQ_LINE);
|
||||
|
||||
|
||||
#pragma GCC diagnostic pop // re-enable all warnings
|
||||
|
||||
static bool _custom_has_handler(stm32_exti_handler_t* _handlers, uint8_t len)
|
||||
{
|
||||
for (uint8_t i = 0; i < len; i++)
|
||||
if (_handlers[i]) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void stm32_exti_custom_enable(uint32_t line, uint8_t trigger, stm32_exti_handler_t cb)
|
||||
{
|
||||
#if defined(LL_APB2_GRP1_PERIPH_EXTI)
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_EXTI);
|
||||
#elif defined(LL_CKGA_PERIPH_EXTI)
|
||||
LL_CKGA_Enable(LL_CKGA_PERIPH_EXTI);
|
||||
#endif
|
||||
|
||||
|
||||
LL_EXTI_InitTypeDef EXTI_init;
|
||||
LL_EXTI_StructInit(&EXTI_init);
|
||||
|
||||
if(line < 32) {
|
||||
EXTI_init.Line_0_31 = 1 << line;
|
||||
} else if (line < 64) {
|
||||
EXTI_init.Line_32_63 = 1 << (line - 32);
|
||||
} else {
|
||||
EXTI_init.Line_64_95 = 1 << (line - 64);
|
||||
}
|
||||
EXTI_init.Mode = LL_EXTI_MODE_IT;
|
||||
EXTI_init.Trigger = trigger;
|
||||
EXTI_init.LineCommand = ENABLE;
|
||||
|
||||
LL_EXTI_Init(&EXTI_init);
|
||||
|
||||
_SET_CUSTOM_EXTI_IRQ_HANDLER(CUSTOM_EXTI_IRQ_NAME, CUSTOM_EXTI_IRQ_LINE, CUSTOM_EXTI_IRQ_LINE, line, cb);
|
||||
}
|
||||
|
||||
void stm32_exti_custom_disable(uint32_t line)
|
||||
{
|
||||
_CLEAR_CUSTOM_EXTI_IRQ_HANDLER(CUSTOM_EXTI_IRQ_NAME, CUSTOM_EXTI_IRQ_LINE, CUSTOM_EXTI_IRQ_LINE, line);
|
||||
|
||||
if(line < 32)
|
||||
LL_EXTI_DisableIT_0_31(1 << line);
|
||||
else if (line < 64)
|
||||
LL_EXTI_DisableIT_32_63(1 << (line - 32));
|
||||
else
|
||||
LL_EXTI_DisableIT_64_95(1 << (line - 64));
|
||||
}
|
||||
|
||||
void stm32_exti_custom_trigger_swi(uint32_t line)
|
||||
{
|
||||
if(line < 32)
|
||||
LL_EXTI_GenerateSWI_0_31(1<<line);
|
||||
else if (line < 64)
|
||||
LL_EXTI_GenerateSWI_32_63(1<<(line-32));
|
||||
else
|
||||
LL_EXTI_GenerateSWI_64_95(1<<(line-64));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -38,3 +38,19 @@ void stm32_exti_enable(uint32_t line, uint8_t trigger, stm32_exti_handler_t cb);
|
||||
|
||||
// Reset callback and disable IRQ if no more handlers
|
||||
void stm32_exti_disable(uint32_t line);
|
||||
|
||||
// trigger software interrupt
|
||||
void stm32_exti_trigger_swi(uint32_t line);
|
||||
|
||||
|
||||
#if defined(USE_CUSTOM_EXTI_IRQ)
|
||||
// Set callback and enable IRQ
|
||||
void stm32_exti_custom_enable(uint32_t line, uint8_t trigger, stm32_exti_handler_t cb);
|
||||
|
||||
// Reset callback and disable IRQ if no more handlers
|
||||
void stm32_exti_custom_disable(uint32_t line);
|
||||
|
||||
// trigger software interrupt
|
||||
void stm32_exti_custom_trigger_swi(uint32_t line);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -202,3 +202,13 @@ void gpio_write(gpio_t pin, int value)
|
||||
gpio_clear(pin);
|
||||
}
|
||||
}
|
||||
|
||||
GPIO_TypeDef* gpio_get_port(gpio_t pin)
|
||||
{
|
||||
return _port(pin);
|
||||
}
|
||||
|
||||
uint32_t gpio_get_pin(gpio_t pin)
|
||||
{
|
||||
return _pin_num(pin);
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
#include "stm32_cmsis.h"
|
||||
#include <stdint.h>
|
||||
#include "hal/gpio.h"
|
||||
|
||||
#define GPIO_UNDEF (0xffffffff)
|
||||
|
||||
@@ -72,3 +73,6 @@ enum {
|
||||
GPIO_PIN_SPEED_HIGH = 0x02,
|
||||
GPIO_PIN_SPEED_VERY_HIGH = 0x03,
|
||||
};
|
||||
|
||||
GPIO_TypeDef* gpio_get_port(gpio_t pin);
|
||||
uint32_t gpio_get_pin(gpio_t pin);
|
||||
|
||||
@@ -33,6 +33,9 @@ extern "C" {
|
||||
#elif defined(STM32H7)
|
||||
#include "CMSIS/Device/ST/STM32H7xx/Include/stm32h7xx.h"
|
||||
#include "stm32h7xx_hal.h"
|
||||
#elif defined(STM32H7RS)
|
||||
#include "CMSIS/Device/ST/STM32H7RS/Include/stm32h7rsxx.h"
|
||||
#include "stm32h7rsxx_hal.h"
|
||||
#endif
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -79,6 +79,22 @@ extern "C" {
|
||||
#include "STM32H7xx_HAL_Driver/Inc/stm32h7xx_ll_iwdg.h"
|
||||
#include "STM32H7xx_HAL_Driver/Inc/stm32h7xx_ll_usb.h"
|
||||
#include "STM32H7xx_HAL_Driver/Inc/stm32h7xx_ll_pwr.h"
|
||||
#elif defined(STM32H7RS)
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_bus.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_fmc.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_dma2d.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_gpio.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_tim.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_dma.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_usart.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_exti.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_system.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_rcc.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_adc.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_spi.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_iwdg.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_usb.h"
|
||||
#include "STM32H7RS_HAL_Driver/Inc/stm32h7rsxx_ll_pwr.h"
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus)
|
||||
|
||||
@@ -78,10 +78,10 @@ static void init_dma_arr_mode(const stm32_pulse_timer_t* tim)
|
||||
int stm32_pulse_init(const stm32_pulse_timer_t* tim, uint32_t freq)
|
||||
{
|
||||
// check if timer is free first (clock disabled)
|
||||
if (stm32_timer_is_clock_enabled(tim->TIMx)) return -1;
|
||||
// if (stm32_timer_is_clock_enabled(tim->TIMx)) return -1;
|
||||
|
||||
// .. and GPIO pin is not used
|
||||
if(gpio_get_mode(tim->GPIO) != GPIO_IN) return -1;
|
||||
// if(gpio_get_mode(tim->GPIO) != GPIO_IN) return -1;
|
||||
|
||||
if (tim->DMA_TC_CallbackPtr) {
|
||||
memset(tim->DMA_TC_CallbackPtr, 0, sizeof(stm32_pulse_dma_tc_cb_t));
|
||||
|
||||
@@ -0,0 +1,439 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief QSPI MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param hqspi: QSPI handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
|
||||
#include "stm32_hal.h"
|
||||
#include "stm32_hal_ll.h"
|
||||
|
||||
#include "stm32_gpio.h"
|
||||
#include "hal/gpio.h"
|
||||
|
||||
#include "stm32_qspi.h"
|
||||
#include "delays_driver.h"
|
||||
#include "hal.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <cstddef>
|
||||
|
||||
#if !defined(QSPI_MAX_FREQ)
|
||||
#define QSPI_MAX_FREQ 100000000U // 100 MHz
|
||||
#endif
|
||||
|
||||
#define QSPI_PRESCALER_MAX 255
|
||||
|
||||
#define SPI_NOR_WIP_BIT (1 << 0)
|
||||
#define SPI_NOR_WEL_BIT (1 << 1)
|
||||
|
||||
#define SPI_NOR_CMD_WRSR 0x01
|
||||
#define SPI_NOR_CMD_RDSR 0x05
|
||||
#define SPI_NOR_CMD_WRSR2 0x31
|
||||
#define SPI_NOR_CMD_RDSR2 0x35
|
||||
|
||||
#define SPI_NOR_CMD_WREN 0x06
|
||||
#define SPI_NOR_CMD_PP_1_1_4 0x32
|
||||
#define SPI_NOR_CMD_BE 0xD8
|
||||
|
||||
#define SPI_NOR_CMD_4READ 0xEB
|
||||
|
||||
#define QSPI_STATUS_REG 1
|
||||
|
||||
// TODO: we might need to read this via SFDP
|
||||
// -> 15th DWORD "Quad Enable Requirements"
|
||||
//
|
||||
// GD25Q127C: does not expose these via SFDP
|
||||
#if !defined(QSPI_QE_REG)
|
||||
#define QSPI_QE_REG 2
|
||||
#endif
|
||||
|
||||
#if !defined(QSPI_QE_BIT)
|
||||
#define QSPI_QE_BIT (1 << 1)
|
||||
#endif
|
||||
|
||||
// TODO: use SFDP to detect this
|
||||
// -> 3rd DWORD "Instruction / Mode Clocks / Dummy cycles"
|
||||
//
|
||||
// GD25Q127C: 2 mode clocks + 4 dummy cycles
|
||||
#if !defined(QSPI_READ_DUMMY_CYCLES)
|
||||
#define QSPI_READ_DUMMY_CYCLES 6
|
||||
#endif
|
||||
|
||||
// reduce read time by not repeating read command
|
||||
// #define QSPI_CONTINUOUS_READ (1 << 5)
|
||||
// #define QSPI_READ_DUMMY_CYCLES 4
|
||||
|
||||
QSPI_HandleTypeDef hqspi = { 0 };
|
||||
|
||||
static const QSPI_CommandTypeDef cmd_write_en = {
|
||||
.Instruction = SPI_NOR_CMD_WREN,
|
||||
.InstructionMode = QSPI_INSTRUCTION_1_LINE
|
||||
};
|
||||
|
||||
static int qspi_read(QSPI_CommandTypeDef* cmd, uint8_t* data, size_t size)
|
||||
{
|
||||
cmd->NbData = size;
|
||||
int ret = HAL_QSPI_Command(&hqspi, cmd, HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
|
||||
|
||||
if (ret == HAL_OK) {
|
||||
ret = HAL_QSPI_Receive(&hqspi, data, HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
|
||||
}
|
||||
|
||||
return ret == HAL_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
static int qspi_write(QSPI_CommandTypeDef* cmd, uint8_t* data, size_t size)
|
||||
{
|
||||
cmd->NbData = size;
|
||||
int ret = HAL_QSPI_Command(&hqspi, cmd, HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
|
||||
|
||||
if (ret == HAL_OK) {
|
||||
ret = HAL_QSPI_Transmit(&hqspi, data, HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
|
||||
}
|
||||
|
||||
return ret == HAL_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
static int qspi_read_status_register(uint8_t reg_num, uint8_t* value)
|
||||
{
|
||||
QSPI_CommandTypeDef cmd = {
|
||||
.InstructionMode = QSPI_INSTRUCTION_1_LINE,
|
||||
.DataMode = QSPI_DATA_1_LINE,
|
||||
};
|
||||
|
||||
switch(reg_num) {
|
||||
case 1:
|
||||
cmd.Instruction = SPI_NOR_CMD_RDSR;
|
||||
break;
|
||||
case 2:
|
||||
cmd.Instruction = SPI_NOR_CMD_RDSR2;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return qspi_read(&cmd, value, sizeof(value));
|
||||
}
|
||||
|
||||
static int qspi_write_status_register(uint8_t reg_num, uint8_t value)
|
||||
{
|
||||
QSPI_CommandTypeDef cmd = {
|
||||
.Instruction = SPI_NOR_CMD_WRSR,
|
||||
.InstructionMode = QSPI_INSTRUCTION_1_LINE,
|
||||
.DataMode = QSPI_DATA_1_LINE,
|
||||
};
|
||||
|
||||
switch(reg_num) {
|
||||
case 1:
|
||||
cmd.Instruction = SPI_NOR_CMD_WRSR;
|
||||
break;
|
||||
case 2:
|
||||
// TODO: some parts can only write upper
|
||||
// registers together with lower ones.
|
||||
cmd.Instruction = SPI_NOR_CMD_WRSR2;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
return qspi_write(&cmd, &value, sizeof(value));
|
||||
}
|
||||
|
||||
static int qspi_write_enable()
|
||||
{
|
||||
int ret = HAL_QSPI_Command(&hqspi, (QSPI_CommandTypeDef*)&cmd_write_en,
|
||||
HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
|
||||
|
||||
if (ret == HAL_OK) {
|
||||
uint8_t reg;
|
||||
do {
|
||||
ret = qspi_read_status_register(1U, ®);
|
||||
} while (!ret && !(reg & SPI_NOR_WEL_BIT));
|
||||
}
|
||||
|
||||
return ret == HAL_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
static int qspi_wait_until_ready()
|
||||
{
|
||||
uint8_t reg;
|
||||
int ret;
|
||||
|
||||
QSPI_CommandTypeDef cmd = {
|
||||
.Instruction = SPI_NOR_CMD_RDSR,
|
||||
.InstructionMode = QSPI_INSTRUCTION_1_LINE,
|
||||
.DataMode = QSPI_DATA_1_LINE,
|
||||
};
|
||||
|
||||
do {
|
||||
ret = qspi_read(&cmd, ®, sizeof(reg));
|
||||
} while (!ret && (reg & SPI_NOR_WIP_BIT));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int qspi_enable_quad_io()
|
||||
{
|
||||
uint8_t reg;
|
||||
|
||||
if (qspi_read_status_register(QSPI_QE_REG, ®) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// check if already enabled
|
||||
if ((reg & QSPI_QE_BIT) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// enable quad-io
|
||||
reg |= QSPI_QE_BIT;
|
||||
|
||||
if (qspi_write_enable() != 0) return -1;
|
||||
|
||||
if (qspi_write_status_register(QSPI_QE_REG, reg) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (qspi_wait_until_ready() != 0) return -1;
|
||||
|
||||
// check if now enabled
|
||||
if (qspi_read_status_register(QSPI_QE_REG, ®) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (reg & QSPI_QE_BIT) != 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
static void qspi_gpio_init()
|
||||
{
|
||||
gpio_init_af(QSPI_CLK_GPIO, QSPI_CLK_GPIO_AF, GPIO_PIN_SPEED_VERY_HIGH);
|
||||
gpio_init_af(QSPI_CS_GPIO, QSPI_CS_GPIO_AF, GPIO_PIN_SPEED_VERY_HIGH);
|
||||
gpio_init_af(QSPI_MISO_GPIO, QSPI_MISO_GPIO_AF, GPIO_PIN_SPEED_VERY_HIGH);
|
||||
gpio_init_af(QSPI_MOSI_GPIO, QSPI_MOSI_GPIO_AF, GPIO_PIN_SPEED_VERY_HIGH);
|
||||
gpio_init_af(QSPI_WP_GPIO , QSPI_WP_GPIO_AF, GPIO_PIN_SPEED_VERY_HIGH);
|
||||
gpio_init_af(QSPI_HOLD_GPIO, QSPI_HOLD_GPIO_AF, GPIO_PIN_SPEED_VERY_HIGH);
|
||||
}
|
||||
|
||||
static void qspi_gpio_deinit()
|
||||
{
|
||||
gpio_init(QSPI_CLK_GPIO, GPIO_IN, GPIO_PIN_SPEED_LOW);
|
||||
gpio_init(QSPI_CS_GPIO, GPIO_IN, GPIO_PIN_SPEED_LOW);
|
||||
gpio_init(QSPI_MISO_GPIO, GPIO_IN, GPIO_PIN_SPEED_LOW);
|
||||
gpio_init(QSPI_MOSI_GPIO, GPIO_IN, GPIO_PIN_SPEED_LOW);
|
||||
gpio_init(QSPI_WP_GPIO, GPIO_IN, GPIO_PIN_SPEED_LOW);
|
||||
gpio_init(QSPI_HOLD_GPIO, GPIO_IN, GPIO_PIN_SPEED_LOW);
|
||||
}
|
||||
|
||||
static uint32_t qspi_compute_prescaler(uint32_t max_freq)
|
||||
{
|
||||
uint32_t qspi_freq = LL_RCC_GetQSPIClockFreq(LL_RCC_QSPI_CLKSOURCE);
|
||||
uint32_t prescaler = 0;
|
||||
for (; prescaler <= QSPI_PRESCALER_MAX; prescaler++) {
|
||||
uint32_t clk = qspi_freq / (prescaler + 1);
|
||||
if (clk <= max_freq) break;
|
||||
}
|
||||
|
||||
return prescaler;
|
||||
}
|
||||
|
||||
static void qspi_init_hal_struct()
|
||||
{
|
||||
hqspi.Instance = QUADSPI;
|
||||
hqspi.Init.ClockPrescaler = qspi_compute_prescaler(QSPI_MAX_FREQ);
|
||||
hqspi.Init.FifoThreshold = 8;
|
||||
hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_NONE;
|
||||
hqspi.Init.FlashSize = POSITION_VAL(QSPI_FLASH_SIZE);
|
||||
hqspi.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_1_CYCLE;
|
||||
hqspi.Init.ClockMode = QSPI_CLOCK_MODE_0;
|
||||
hqspi.Init.FlashID = QSPI_FLASH_ID_1;
|
||||
hqspi.Init.DualFlash = QSPI_DUALFLASH_DISABLE;
|
||||
}
|
||||
|
||||
int stm32_qspi_nor_init()
|
||||
{
|
||||
qspi_gpio_init();
|
||||
|
||||
// Peripheral clock enable
|
||||
__HAL_RCC_QSPI_CLK_ENABLE();
|
||||
|
||||
qspi_init_hal_struct();
|
||||
if (HAL_QSPI_Init(&hqspi) != HAL_OK) return -1;
|
||||
|
||||
return qspi_enable_quad_io();
|
||||
}
|
||||
|
||||
static bool qspi_is_memory_mapped(QSPI_HandleTypeDef* h)
|
||||
{
|
||||
return ((READ_BIT(h->Instance->CCR, QUADSPI_CCR_FMODE) == QUADSPI_CCR_FMODE)
|
||||
? true
|
||||
: false);
|
||||
}
|
||||
|
||||
static int qspi_abort(QSPI_HandleTypeDef* h)
|
||||
{
|
||||
if (HAL_QSPI_Abort(h) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void stm32_qspi_no_init_runtime()
|
||||
{
|
||||
qspi_init_hal_struct();
|
||||
hqspi.Timeout = HAL_QSPI_TIMEOUT_DEFAULT_VALUE;
|
||||
hqspi.State = HAL_QSPI_STATE_BUSY_MEM_MAPPED;
|
||||
}
|
||||
|
||||
void stm32_qspi_nor_deinit()
|
||||
{
|
||||
if (qspi_is_memory_mapped(&hqspi)) {
|
||||
qspi_abort(&hqspi);
|
||||
}
|
||||
|
||||
HAL_QSPI_DeInit(&hqspi);
|
||||
qspi_gpio_deinit();
|
||||
}
|
||||
|
||||
int stm32_qspi_nor_memory_mapped()
|
||||
{
|
||||
// assume quad mode / 3-byte address
|
||||
QSPI_CommandTypeDef cmd = {
|
||||
.Instruction = SPI_NOR_CMD_4READ,
|
||||
.AddressSize = QSPI_ADDRESS_24_BITS,
|
||||
.DummyCycles = QSPI_READ_DUMMY_CYCLES,
|
||||
.InstructionMode = QSPI_INSTRUCTION_1_LINE,
|
||||
.AddressMode = QSPI_ADDRESS_4_LINES,
|
||||
.DataMode = QSPI_DATA_4_LINES,
|
||||
};
|
||||
|
||||
// not detectable via SFDP (apart from matching on part ID)
|
||||
#if defined(QSPI_CONTINUOUS_READ)
|
||||
cmd.AlternateBytes = QSPI_CONTINUOUS_READ;
|
||||
cmd.AlternateBytesSize = QSPI_ALTERNATE_BYTES_8_BITS;
|
||||
cmd.AlternateByteMode = QSPI_ALTERNATE_BYTES_4_LINES;
|
||||
cmd.SIOOMode = QSPI_SIOO_INST_ONLY_FIRST_CMD;
|
||||
#endif
|
||||
|
||||
QSPI_MemoryMappedTypeDef mem_mapped = {
|
||||
.TimeOutActivation = QSPI_TIMEOUT_COUNTER_DISABLE,
|
||||
};
|
||||
|
||||
if (HAL_QSPI_MemoryMapped(&hqspi, &cmd, &mem_mapped) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_qspi_nor_read(uint32_t address, void* data, uint32_t size)
|
||||
{
|
||||
if (!qspi_is_memory_mapped(&hqspi)) {
|
||||
if (stm32_qspi_nor_memory_mapped() != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
uintptr_t mmap_addr = QSPI_BASE + address;
|
||||
memcpy(data, (void *)mmap_addr, size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_qspi_nor_erase_sector(uint32_t address)
|
||||
{
|
||||
// verify block alignment
|
||||
if (address & 0xFFFF) return -1;
|
||||
|
||||
// backup & diable memory-mapped mode
|
||||
uint32_t qspi_ccr_backup = 0;
|
||||
if (qspi_is_memory_mapped(&hqspi)) {
|
||||
qspi_ccr_backup = READ_REG(hqspi.Instance->CCR);
|
||||
if (qspi_abort(&hqspi) != 0) return -1;
|
||||
}
|
||||
|
||||
// write enable
|
||||
int ret = HAL_QSPI_Command(&hqspi, (QSPI_CommandTypeDef*)&cmd_write_en,
|
||||
HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
|
||||
|
||||
if (ret == HAL_OK) {
|
||||
// block erase
|
||||
QSPI_CommandTypeDef s_command = {
|
||||
.Instruction = SPI_NOR_CMD_BE,
|
||||
.Address = (uint32_t)address,
|
||||
.AddressSize = QSPI_ADDRESS_24_BITS,
|
||||
.InstructionMode = QSPI_INSTRUCTION_1_LINE,
|
||||
.AddressMode = QSPI_ADDRESS_1_LINE,
|
||||
};
|
||||
ret = HAL_QSPI_Command(&hqspi, &s_command, HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
|
||||
}
|
||||
|
||||
if (ret == HAL_OK) {
|
||||
ret = qspi_wait_until_ready();
|
||||
}
|
||||
|
||||
if (qspi_ccr_backup) {
|
||||
WRITE_REG(hqspi.Instance->CCR, qspi_ccr_backup);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int stm32_qspi_nor_program(uint32_t address, void* data, uint32_t len)
|
||||
{
|
||||
uint32_t qspi_ccr_backup = 0;
|
||||
if (qspi_is_memory_mapped(&hqspi)) {
|
||||
qspi_ccr_backup = READ_REG(hqspi.Instance->CCR);
|
||||
if (qspi_abort(&hqspi) != 0) return -1;
|
||||
}
|
||||
|
||||
// write enable
|
||||
int ret = HAL_QSPI_Command(&hqspi, (QSPI_CommandTypeDef*)&cmd_write_en,
|
||||
HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
|
||||
|
||||
// page program
|
||||
if (ret == HAL_OK) {
|
||||
QSPI_CommandTypeDef s_command = {
|
||||
.Instruction = SPI_NOR_CMD_PP_1_1_4,
|
||||
.Address = address,
|
||||
.AddressSize = QSPI_ADDRESS_24_BITS,
|
||||
.InstructionMode = QSPI_INSTRUCTION_1_LINE,
|
||||
.AddressMode = QSPI_ADDRESS_1_LINE,
|
||||
.DataMode = QSPI_DATA_4_LINES,
|
||||
.NbData = len,
|
||||
};
|
||||
ret = HAL_QSPI_Command(&hqspi, &s_command, HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
|
||||
}
|
||||
|
||||
// xmit data
|
||||
if (ret == HAL_OK) {
|
||||
ret = HAL_QSPI_Transmit(&hqspi, (uint8_t*)data,
|
||||
HAL_QSPI_TIMEOUT_DEFAULT_VALUE);
|
||||
}
|
||||
|
||||
if (ret == HAL_OK) {
|
||||
ret = qspi_wait_until_ready();
|
||||
}
|
||||
|
||||
if (qspi_ccr_backup) {
|
||||
WRITE_REG(hqspi.Instance->CCR, qspi_ccr_backup);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
int stm32_qspi_nor_init();
|
||||
void stm32_qspi_nor_deinit();
|
||||
|
||||
void stm32_qspi_no_init_runtime();
|
||||
|
||||
int stm32_qspi_nor_memory_mapped();
|
||||
int stm32_qspi_nor_read(uint32_t address, void* data, uint32_t len);
|
||||
|
||||
int stm32_qspi_nor_erase_sector(uint32_t address);
|
||||
int stm32_qspi_nor_program(uint32_t address, void* data, uint32_t len);
|
||||
|
||||
@@ -63,6 +63,10 @@ enum _STM32_USART {
|
||||
_STM32_UART4,
|
||||
#endif
|
||||
|
||||
#if defined(UART5) && (defined(STM32H7) || defined(STM32H7RS))
|
||||
_STM32_UART5,
|
||||
#endif
|
||||
|
||||
#if defined(USART6)
|
||||
_STM32_USART6,
|
||||
#endif
|
||||
@@ -71,6 +75,10 @@ enum _STM32_USART {
|
||||
_STM32_UART7,
|
||||
#endif
|
||||
|
||||
#if defined(UART8)
|
||||
_STM32_UART8,
|
||||
#endif
|
||||
|
||||
_STM32_MAX_UARTS
|
||||
};
|
||||
|
||||
@@ -154,6 +162,10 @@ static inline void _usart_isr_handler(_STM32_USART n)
|
||||
DEFINE_USART_IRQ(UART4);
|
||||
#endif
|
||||
|
||||
#if defined (UART5) && (defined(STM32H7) || defined(STM32H7RS))
|
||||
DEFINE_USART_IRQ(UART5);
|
||||
#endif
|
||||
|
||||
#if defined (USART6)
|
||||
DEFINE_USART_IRQ(USART6);
|
||||
#endif
|
||||
@@ -162,6 +174,11 @@ static inline void _usart_isr_handler(_STM32_USART n)
|
||||
DEFINE_USART_IRQ(UART7);
|
||||
#endif
|
||||
|
||||
#if defined (UART8)
|
||||
DEFINE_USART_IRQ(UART8);
|
||||
#endif
|
||||
|
||||
|
||||
static stm32_serial_state* stm32_serial_find_state(const stm32_usart_t* usart)
|
||||
{
|
||||
#if defined (USART1)
|
||||
@@ -176,12 +193,18 @@ static stm32_serial_state* stm32_serial_find_state(const stm32_usart_t* usart)
|
||||
#if defined (UART4)
|
||||
if (usart->USARTx == UART4) return &_serial_states[_STM32_UART4];
|
||||
#endif
|
||||
#if defined (UART5) && (defined(STM32H7) || defined(STM32H7RS))
|
||||
if (usart->USARTx == UART5) return &_serial_states[_STM32_UART5];
|
||||
#endif
|
||||
#if defined (USART6)
|
||||
if (usart->USARTx == USART6) return &_serial_states[_STM32_USART6];
|
||||
#endif
|
||||
#if defined (UART7)
|
||||
if (usart->USARTx == UART7) return &_serial_states[_STM32_UART7];
|
||||
#endif
|
||||
#if defined (UART8)
|
||||
if (usart->USARTx == UART8) return &_serial_states[_STM32_UART8];
|
||||
#endif
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
@@ -325,6 +348,9 @@ static void stm32_serial_send_buffer(void* ctx, const uint8_t* data, uint32_t si
|
||||
auto sp = st->sp;
|
||||
auto usart = sp->usart;
|
||||
if (usart->txDMA && !IS_CCM_RAM(data)) {
|
||||
#if defined(STM32H7) || defined(STM32H7RS)
|
||||
SCB_CleanDCache_by_Addr((void*)data, size);
|
||||
#endif
|
||||
stm32_usart_send_buffer(usart, data, size);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -24,8 +24,7 @@
|
||||
#include "stm32_usart_driver.h"
|
||||
#include "hal/serial_driver.h"
|
||||
|
||||
// __DMA
|
||||
#include "definitions.h"
|
||||
#include "memory_sections.h"
|
||||
|
||||
// Serial buffer to be defined in boards
|
||||
struct stm32_serial_buffer {
|
||||
@@ -48,8 +47,8 @@ struct stm32_serial_port {
|
||||
extern const etx_serial_driver_t STM32SerialDriver;
|
||||
|
||||
#define DEFINE_STM32_SERIAL_PORT(p,usart,rx_buf_len,tx_buf_len) \
|
||||
static uint8_t p ## _RXBuffer[rx_buf_len] __DMA; \
|
||||
static uint8_t p ## _TXBuffer[tx_buf_len] __DMA; \
|
||||
static uint8_t p ## _RXBuffer[rx_buf_len] __DMA_NO_CACHE; \
|
||||
static uint8_t p ## _TXBuffer[tx_buf_len] __DMA_NO_CACHE; \
|
||||
static const stm32_serial_port p ## _STM32Serial = { \
|
||||
&usart, \
|
||||
{ p ## _RXBuffer, rx_buf_len }, \
|
||||
|
||||
@@ -94,11 +94,11 @@ struct stm32_softserial_tx_port {
|
||||
stm32_softserial_tx_state* st;
|
||||
};
|
||||
|
||||
#define DEFINE_STM32_SOFTSERIAL_PORT(p,timer) \
|
||||
static stm32_softserial_tx_state p ## _SoftserialState __DMA; \
|
||||
static const stm32_softserial_tx_port p ## _STM32Softserial = { \
|
||||
&timer, \
|
||||
& p ## _SoftserialState, \
|
||||
#define DEFINE_STM32_SOFTSERIAL_PORT(p, timer) \
|
||||
static stm32_softserial_tx_state p##_SoftserialState __DMA_NO_CACHE; \
|
||||
static const stm32_softserial_tx_port p##_STM32Softserial = { \
|
||||
&timer, \
|
||||
&p##_SoftserialState, \
|
||||
}
|
||||
|
||||
#define REF_STM32_SOFTSERIAL_PORT(p) ((void*)& p ## _STM32Softserial)
|
||||
|
||||
@@ -20,9 +20,11 @@
|
||||
*/
|
||||
|
||||
#include "stm32_spi.h"
|
||||
#include "memory_sections.h"
|
||||
#include "stm32_dma.h"
|
||||
#include "stm32_gpio_driver.h"
|
||||
#include "stm32_gpio.h"
|
||||
#include "definitions.h"
|
||||
#include "stm32_hal.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -36,35 +38,31 @@
|
||||
void stm32_spi_enable_clock(SPI_TypeDef *SPIx)
|
||||
{
|
||||
if (SPIx == SPI1) {
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);
|
||||
__HAL_RCC_SPI1_CLK_ENABLE();
|
||||
}
|
||||
#if defined(SPI2)
|
||||
else if (SPIx == SPI2) {
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI2);
|
||||
__HAL_RCC_SPI2_CLK_ENABLE();
|
||||
}
|
||||
#endif
|
||||
#if defined(SPI3)
|
||||
else if (SPIx == SPI3) {
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_SPI3);
|
||||
__HAL_RCC_SPI3_CLK_ENABLE();
|
||||
}
|
||||
#endif
|
||||
#if defined(SPI4)
|
||||
else if (SPIx == SPI4) {
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI4);
|
||||
__HAL_RCC_SPI4_CLK_ENABLE();
|
||||
}
|
||||
#endif
|
||||
#if defined(SPI5)
|
||||
else if (SPIx == SPI5) {
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI5);
|
||||
__HAL_RCC_SPI5_CLK_ENABLE();
|
||||
}
|
||||
#endif
|
||||
#if defined(SPI6)
|
||||
else if (SPIx == SPI6) {
|
||||
#if defined(LL_APB2_GRP1_PERIPH_SPI6)
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI6);
|
||||
#elif defined(LL_APB4_GRP1_PERIPH_SPI6)
|
||||
LL_APB4_GRP1_EnableClock(LL_APB4_GRP1_PERIPH_SPI6);
|
||||
#endif
|
||||
__HAL_RCC_SPI6_CLK_ENABLE();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -77,11 +75,41 @@ static inline uint32_t _get_spi_af(SPI_TypeDef *SPIx)
|
||||
return LL_GPIO_AF_5;
|
||||
}
|
||||
|
||||
#if defined(STM32H7) || defined(STM32H7RS)
|
||||
static inline uint32_t _get_spi_clocksource(SPI_TypeDef* SPIx)
|
||||
{
|
||||
#if defined(LL_RCC_SPI123_CLKSOURCE)
|
||||
return LL_RCC_SPI123_CLKSOURCE;
|
||||
#endif
|
||||
|
||||
#if defined(LL_RCC_SPI1_CLKSOURCE)
|
||||
if (SPIx == SPI1) return LL_RCC_SPI1_CLKSOURCE;
|
||||
#endif
|
||||
|
||||
#if defined(LL_RCC_SPI23_CLKSOURCE)
|
||||
if (SPIx == SPI2 || SPIx == SPI3) return LL_RCC_SPI23_CLKSOURCE;
|
||||
#endif
|
||||
|
||||
#if defined(LL_RCC_SPI45_CLKSOURCE)
|
||||
if (SPIx == SPI4 || SPIx == SPI5) return LL_RCC_SPI45_CLKSOURCE;
|
||||
#endif
|
||||
|
||||
#if defined(LL_RCC_SPI6_CLKSOURCE)
|
||||
if (SPIx == SPI6) return LL_RCC_SPI6_CLKSOURCE;
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static uint32_t _get_spi_prescaler(SPI_TypeDef *SPIx, uint32_t max_freq)
|
||||
{
|
||||
LL_RCC_ClocksTypeDef RCC_Clocks;
|
||||
LL_RCC_GetSystemClocksFreq(&RCC_Clocks);
|
||||
|
||||
#if defined(STM32H7) || defined(STM32H7RS)
|
||||
uint32_t pclk = LL_RCC_GetSPIClockFreq(_get_spi_clocksource(SPIx));
|
||||
#else
|
||||
uint32_t pclk = RCC_Clocks.PCLK2_Frequency;
|
||||
#if defined(SPI2)
|
||||
if (SPIx == SPI2) {
|
||||
@@ -93,7 +121,7 @@ static uint32_t _get_spi_prescaler(SPI_TypeDef *SPIx, uint32_t max_freq)
|
||||
pclk = RCC_Clocks.PCLK1_Frequency;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // STM32H7 || STM32H7RS
|
||||
uint32_t divider = (pclk + max_freq) / max_freq;
|
||||
uint32_t presc;
|
||||
if (divider > 128) {
|
||||
@@ -117,28 +145,19 @@ static uint32_t _get_spi_prescaler(SPI_TypeDef *SPIx, uint32_t max_freq)
|
||||
return presc;
|
||||
}
|
||||
|
||||
static void _init_gpios(const stm32_spi_t* spi)
|
||||
static void _init_gpios(const stm32_spi_t* spi, bool enableMisoPullUp)
|
||||
{
|
||||
stm32_gpio_enable_clock(spi->SPI_GPIOx);
|
||||
stm32_gpio_enable_clock(spi->CS_GPIOx);
|
||||
if(enableMisoPullUp)
|
||||
{
|
||||
gpio_init(spi->MISO, GPIO_IN_PU, GPIO_PIN_SPEED_VERY_HIGH);
|
||||
gpio_set_af(spi->MISO, _get_spi_af(spi->SPIx));
|
||||
} else {
|
||||
gpio_init_af(spi->MISO, _get_spi_af(spi->SPIx), GPIO_PIN_SPEED_VERY_HIGH);
|
||||
}
|
||||
gpio_init_af(spi->SCK, _get_spi_af(spi->SPIx), GPIO_PIN_SPEED_VERY_HIGH);
|
||||
gpio_init_af(spi->MOSI, _get_spi_af(spi->SPIx), GPIO_PIN_SPEED_VERY_HIGH);
|
||||
|
||||
LL_GPIO_InitTypeDef pinInit;
|
||||
LL_GPIO_StructInit(&pinInit);
|
||||
|
||||
// SCK, MISO, MOSI
|
||||
pinInit.Pin = spi->SPI_Pins;
|
||||
pinInit.Mode = LL_GPIO_MODE_ALTERNATE;
|
||||
pinInit.Alternate = _get_spi_af(spi->SPIx);
|
||||
pinInit.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
pinInit.Pull = LL_GPIO_PULL_UP;
|
||||
LL_GPIO_Init(spi->SPI_GPIOx, &pinInit);
|
||||
|
||||
// CS
|
||||
pinInit.Pin = spi->CS_Pin;
|
||||
pinInit.Mode = LL_GPIO_MODE_OUTPUT;
|
||||
pinInit.Alternate = LL_GPIO_AF_0;
|
||||
pinInit.Pull = LL_GPIO_PULL_NO;
|
||||
LL_GPIO_Init(spi->CS_GPIOx, &pinInit);
|
||||
gpio_init(spi->CS, GPIO_OUT, GPIO_PIN_SPEED_HIGH);
|
||||
}
|
||||
|
||||
#if defined(USE_SPI_DMA)
|
||||
@@ -197,9 +216,9 @@ static void _config_dma_streams(const stm32_spi_t* spi)
|
||||
}
|
||||
#endif
|
||||
|
||||
void stm32_spi_init(const stm32_spi_t* spi, uint32_t data_width)
|
||||
void stm32_spi_init(const stm32_spi_t* spi, uint32_t data_width, bool misoPullUp)
|
||||
{
|
||||
_init_gpios(spi);
|
||||
_init_gpios(spi, misoPullUp);
|
||||
|
||||
auto SPIx = spi->SPIx;
|
||||
stm32_spi_enable_clock(SPIx);
|
||||
@@ -215,6 +234,9 @@ void stm32_spi_init(const stm32_spi_t* spi, uint32_t data_width)
|
||||
|
||||
LL_SPI_Init(SPIx, &spiInit);
|
||||
LL_SPI_Enable(SPIx);
|
||||
#if defined(STM32H7) || defined(STM32H7RS)
|
||||
LL_SPI_StartMasterTransfer(SPIx);
|
||||
#endif
|
||||
|
||||
#if defined(USE_SPI_DMA)
|
||||
if (spi->DMA) {
|
||||
@@ -227,19 +249,26 @@ void stm32_spi_init(const stm32_spi_t* spi, uint32_t data_width)
|
||||
|
||||
void stm32_spi_select(const stm32_spi_t* spi)
|
||||
{
|
||||
LL_GPIO_ResetOutputPin(spi->CS_GPIOx, spi->CS_Pin);
|
||||
gpio_clear(spi->CS);
|
||||
}
|
||||
|
||||
void stm32_spi_unselect(const stm32_spi_t* spi)
|
||||
{
|
||||
LL_GPIO_SetOutputPin(spi->CS_GPIOx, spi->CS_Pin);
|
||||
gpio_set(spi->CS);
|
||||
}
|
||||
|
||||
void stm32_spi_set_max_baudrate(const stm32_spi_t* spi, uint32_t baudrate)
|
||||
{
|
||||
auto* SPIx = spi->SPIx;
|
||||
uint32_t presc = _get_spi_prescaler(SPIx, baudrate);
|
||||
#if defined(STM32H7) || defined(STM32H7RS)
|
||||
LL_SPI_Disable(SPIx);
|
||||
#endif
|
||||
LL_SPI_SetBaudRatePrescaler(SPIx, presc);
|
||||
#if defined(STM32H7) || defined(STM32H7RS)
|
||||
LL_SPI_Enable(SPIx);
|
||||
LL_SPI_StartMasterTransfer(SPIx);
|
||||
#endif
|
||||
}
|
||||
|
||||
uint8_t stm32_spi_transfer_byte(const stm32_spi_t* spi, uint8_t out)
|
||||
@@ -299,14 +328,20 @@ uint16_t stm32_spi_transfer_word(const stm32_spi_t* spi, uint16_t out)
|
||||
}
|
||||
|
||||
#if defined(USE_SPI_DMA)
|
||||
static uint16_t _scratch_byte __DMA;
|
||||
static uint8_t _scratch_buffer[512] __DMA;
|
||||
static uint16_t _scratch_byte __DMA_NO_CACHE;
|
||||
static uint8_t _scratch_buffer[512] __DMA_NO_CACHE;
|
||||
|
||||
#if defined(STM32H7) || defined(STM32H7RS) || defined(STM32F4)
|
||||
#if defined(STM32F4)
|
||||
extern uint32_t _sram;
|
||||
extern uint32_t _eram;
|
||||
#define _IS_DMA_BUFFER(addr) \
|
||||
((intptr_t)(addr) >= (intptr_t)&_sram && (intptr_t)(addr) <= (intptr_t)&_eram)
|
||||
#elif defined(STM32H7) || defined(STM32H7RS)
|
||||
extern uint32_t _s_dram;
|
||||
extern uint32_t _e_dram;
|
||||
#define _IS_DMA_BUFFER(addr) \
|
||||
((intptr_t)(addr) >= (intptr_t)&_s_dram && \
|
||||
(intptr_t)(addr) <= (intptr_t)&_e_dram)
|
||||
#else
|
||||
#define _IS_DMA_BUFFER(addr) (true)
|
||||
#endif
|
||||
|
||||
@@ -22,15 +22,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "stm32_hal_ll.h"
|
||||
#include "hal/gpio.h"
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
struct stm32_spi_t {
|
||||
SPI_TypeDef* SPIx;
|
||||
GPIO_TypeDef* SPI_GPIOx;
|
||||
uint32_t SPI_Pins; // SCK, MISO, MOSI
|
||||
GPIO_TypeDef* CS_GPIOx;
|
||||
uint32_t CS_Pin; // CS
|
||||
gpio_t SCK;
|
||||
gpio_t MISO;
|
||||
gpio_t MOSI;
|
||||
gpio_t CS;
|
||||
|
||||
DMA_TypeDef* DMA;
|
||||
#if defined(STM32H7) || defined(STM32H7RS)
|
||||
@@ -49,7 +50,7 @@ struct stm32_spi_t {
|
||||
|
||||
void stm32_spi_enable_clock(SPI_TypeDef *SPIx);
|
||||
|
||||
void stm32_spi_init(const stm32_spi_t* spi, uint32_t data_width);
|
||||
void stm32_spi_init(const stm32_spi_t* spi, uint32_t data_width, bool misoPullUp = false);
|
||||
void stm32_spi_deinit(const stm32_spi_t* spi);
|
||||
|
||||
void stm32_spi_select(const stm32_spi_t* spi);
|
||||
|
||||
@@ -69,6 +69,7 @@ static inline void _half_duplex_input(const stm32_usart_t* usart)
|
||||
if (usart->set_input) {
|
||||
usart->set_input(true);
|
||||
LL_USART_EnableDirectionRx(usart->USARTx);
|
||||
LL_USART_DisableDirectionTx(usart->USARTx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,6 +78,7 @@ static inline void _half_duplex_output(const stm32_usart_t* usart)
|
||||
if (usart->set_input) {
|
||||
usart->set_input(false);
|
||||
LL_USART_DisableDirectionRx(usart->USARTx);
|
||||
LL_USART_EnableDirectionTx(usart->USARTx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,6 +120,11 @@ static void enable_usart_clock(USART_TypeDef* USARTx)
|
||||
} else if (USARTx == UART4) {
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_UART4);
|
||||
}
|
||||
#if defined(UART5)
|
||||
else if (USARTx == UART5) {
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_UART5);
|
||||
}
|
||||
#endif
|
||||
#if defined(USART6)
|
||||
else if (USARTx == USART6) {
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART6);
|
||||
@@ -128,6 +135,12 @@ static void enable_usart_clock(USART_TypeDef* USARTx)
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_UART7);
|
||||
}
|
||||
#endif
|
||||
#if defined(UART8)
|
||||
else if (USARTx == UART8) {
|
||||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_UART8);
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
static void disable_usart_clock(USART_TypeDef* USARTx)
|
||||
@@ -141,6 +154,11 @@ static void disable_usart_clock(USART_TypeDef* USARTx)
|
||||
} else if (USARTx == UART4) {
|
||||
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_UART4);
|
||||
}
|
||||
#if defined(UART5)
|
||||
else if (USARTx == UART5) {
|
||||
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_UART5);
|
||||
}
|
||||
#endif
|
||||
#if defined(USART6)
|
||||
else if (USARTx == USART6) {
|
||||
LL_APB2_GRP1_DisableClock(LL_APB2_GRP1_PERIPH_USART6);
|
||||
@@ -151,6 +169,11 @@ static void disable_usart_clock(USART_TypeDef* USARTx)
|
||||
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_UART7);
|
||||
}
|
||||
#endif
|
||||
#if defined(UART8)
|
||||
else if (USARTx == UART8) {
|
||||
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_UART8);
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
@@ -169,10 +192,7 @@ static gpio_speed_t _get_pin_speed(uint32_t baudrate)
|
||||
return GPIO_PIN_SPEED_LOW;
|
||||
}
|
||||
|
||||
#if defined(STM32H7)
|
||||
# define _AF7_USART(x) \
|
||||
(x == USART1 || x == USART2 || x == USART3 || x == USART6 || x == UART7)
|
||||
#elif defined(STM32H7RS)
|
||||
#if defined(STM32H7RS)
|
||||
# define _AF7_USART(x) \
|
||||
(x == USART1 || x == USART2 || x == USART3 || x == UART7)
|
||||
#else // F2, F4
|
||||
@@ -180,9 +200,42 @@ static gpio_speed_t _get_pin_speed(uint32_t baudrate)
|
||||
(x == USART1 || x == USART2 || x == USART3)
|
||||
#endif
|
||||
|
||||
static gpio_af_t _get_usart_af(USART_TypeDef* USARTx)
|
||||
static gpio_af_t _get_usart_af(gpio_t pin, USART_TypeDef* USARTx)
|
||||
{
|
||||
#if defined(STM32H7)
|
||||
GPIO_TypeDef* port = gpio_get_port(pin);
|
||||
uint32_t pinNr = gpio_get_pin(pin);
|
||||
if(USARTx == USART1)
|
||||
{
|
||||
if(port == GPIOA)
|
||||
{
|
||||
return GPIO_AF7;
|
||||
} else { // GPIOB
|
||||
if(pinNr < 14)
|
||||
return GPIO_AF7;
|
||||
return GPIO_AF4;
|
||||
}
|
||||
} else if (USARTx == USART2 || USARTx == USART3 || USARTx == USART6) {
|
||||
return GPIO_AF7;
|
||||
} else if (USARTx == UART4) {
|
||||
if(port == GPIOA && (pin == 11 || pin == 12))
|
||||
return GPIO_AF6;
|
||||
return GPIO_AF8;
|
||||
} else if (USARTx == UART5) {
|
||||
if(port == GPIOB)
|
||||
return GPIO_AF14;
|
||||
return GPIO_AF8;
|
||||
} else if (USARTx == UART7) {
|
||||
if (port == GPIOA || port == GPIOB)
|
||||
return GPIO_AF11;
|
||||
return GPIO_AF7;
|
||||
} else if (USARTx == UART8) {
|
||||
return GPIO_AF8;
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
return _AF7_USART(USARTx) ? GPIO_AF7 : GPIO_AF8;
|
||||
#endif
|
||||
}
|
||||
|
||||
// from stm32f4xx_ll_usart.c
|
||||
@@ -313,6 +366,34 @@ void stm32_usart_deinit_rx_dma(const stm32_usart_t* usart)
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(STM32H7) || defined(STM32H7RS)
|
||||
void stm32_usart_rx_inversion(const stm32_usart_t* usart, bool on)
|
||||
{
|
||||
bool enableUart = false;
|
||||
if(LL_USART_IsEnabled(usart->USARTx))
|
||||
{
|
||||
LL_USART_Disable(usart->USARTx);
|
||||
enableUart = true;
|
||||
}
|
||||
LL_USART_SetRXPinLevel(usart->USARTx, on?LL_USART_RXPIN_LEVEL_INVERTED:LL_USART_RXPIN_LEVEL_STANDARD);
|
||||
if(enableUart)
|
||||
LL_USART_Enable(usart->USARTx);
|
||||
}
|
||||
|
||||
void stm32_usart_tx_inversion(const stm32_usart_t* usart, bool on)
|
||||
{
|
||||
bool enableUart = false;
|
||||
if(LL_USART_IsEnabled(usart->USARTx))
|
||||
{
|
||||
LL_USART_Disable(usart->USARTx);
|
||||
enableUart = true;
|
||||
}
|
||||
LL_USART_SetTXPinLevel(usart->USARTx, on?LL_USART_TXPIN_LEVEL_INVERTED:LL_USART_TXPIN_LEVEL_STANDARD);
|
||||
if(enableUart)
|
||||
LL_USART_Enable(usart->USARTx);
|
||||
}
|
||||
#endif
|
||||
|
||||
// TODO: probably needs to be a separate API on the serial driver
|
||||
//
|
||||
// - USART_OneBitMethodCmd(TELEMETRY_USART, ENABLE);
|
||||
@@ -334,11 +415,12 @@ bool stm32_usart_init(const stm32_usart_t* usart, const etx_serial_init* params)
|
||||
enable_usart_clock(usart->USARTx);
|
||||
LL_USART_DeInit(usart->USARTx);
|
||||
|
||||
gpio_af_t af = _get_usart_af(usart->USARTx);
|
||||
if (usart->rxGPIO != GPIO_UNDEF) {
|
||||
gpio_af_t af = _get_usart_af(usart->rxGPIO, usart->USARTx);
|
||||
gpio_init_af(usart->rxGPIO, af, _get_pin_speed(params->baudrate));
|
||||
}
|
||||
if (usart->txGPIO != GPIO_UNDEF) {
|
||||
gpio_af_t af = _get_usart_af(usart->txGPIO, usart->USARTx);
|
||||
gpio_init_af(usart->txGPIO, af, _get_pin_speed(params->baudrate));
|
||||
}
|
||||
|
||||
@@ -377,8 +459,15 @@ bool stm32_usart_init(const stm32_usart_t* usart, const etx_serial_init* params)
|
||||
usartInit.TransferDirection |= LL_USART_DIRECTION_TX;
|
||||
|
||||
LL_USART_Init(usart->USARTx, &usartInit);
|
||||
#if defined(STM32H7) || defined (STM32H7RS)
|
||||
if(usart->rxGPIO == GPIO_UNDEF)
|
||||
{
|
||||
LL_USART_EnableHalfDuplex(usart->USARTx);
|
||||
}
|
||||
#endif
|
||||
LL_USART_Enable(usart->USARTx);
|
||||
|
||||
|
||||
if (params->direction & ETX_Dir_TX) {
|
||||
// Enable TX DMA request
|
||||
if (usart->txDMA) {
|
||||
@@ -471,7 +560,7 @@ void stm32_usart_send_buffer(const stm32_usart_t* usart, const uint8_t * data, u
|
||||
|
||||
#if defined(LL_USART_DMA_REG_DATA_RECEIVE)
|
||||
dmaInit.PeriphOrM2MSrcAddress =
|
||||
LL_USART_DMA_GetRegAddr(usart->USARTx, LL_USART_DMA_REG_DATA_RECEIVE);
|
||||
LL_USART_DMA_GetRegAddr(usart->USARTx, LL_USART_DMA_REG_DATA_TRANSMIT);
|
||||
#else
|
||||
dmaInit.PeriphOrM2MSrcAddress = LL_USART_DMA_GetRegAddr(usart->USARTx);
|
||||
#endif
|
||||
@@ -523,14 +612,26 @@ void stm32_usart_wait_for_tx_dma(const stm32_usart_t* usart)
|
||||
if (LL_DMA_IsEnabledStream(usart->txDMA, usart->txDMA_Stream)) {
|
||||
|
||||
switch(usart->txDMA_Stream) {
|
||||
case LL_DMA_STREAM_0:
|
||||
while (!LL_DMA_IsActiveFlag_TC0(usart->txDMA));
|
||||
LL_DMA_ClearFlag_TC0(usart->txDMA);
|
||||
break;
|
||||
case LL_DMA_STREAM_1:
|
||||
while (!LL_DMA_IsActiveFlag_TC1(usart->txDMA));
|
||||
LL_DMA_ClearFlag_TC1(usart->txDMA);
|
||||
break;
|
||||
case LL_DMA_STREAM_2:
|
||||
while (!LL_DMA_IsActiveFlag_TC2(usart->txDMA));
|
||||
LL_DMA_ClearFlag_TC2(usart->txDMA);
|
||||
break;
|
||||
case LL_DMA_STREAM_3:
|
||||
while (!LL_DMA_IsActiveFlag_TC3(usart->txDMA));
|
||||
LL_DMA_ClearFlag_TC3(usart->txDMA);
|
||||
break;
|
||||
case LL_DMA_STREAM_4:
|
||||
while (!LL_DMA_IsActiveFlag_TC4(usart->txDMA));
|
||||
LL_DMA_ClearFlag_TC4(usart->txDMA);
|
||||
break;
|
||||
case LL_DMA_STREAM_5:
|
||||
while (!LL_DMA_IsActiveFlag_TC5(usart->txDMA));
|
||||
LL_DMA_ClearFlag_TC5(usart->txDMA);
|
||||
@@ -588,7 +689,7 @@ void stm32_usart_set_hw_option(const stm32_usart_t* usart, uint32_t option)
|
||||
#if defined(STM32H7) || defined(STM32H7RS)
|
||||
# define USART_STATUS_REG ISR
|
||||
# define USART_IDLE USART_ISR_IDLE
|
||||
# define USART_TXE USART_ISR_TXFE
|
||||
# define USART_TXE USART_ISR_TXE_TXFNF
|
||||
# define USART_TC USART_ISR_TC
|
||||
# define USART_RXNE USART_ISR_RXNE_RXFNE
|
||||
# define USART_FLAG_ERRORS \
|
||||
|
||||
@@ -61,6 +61,12 @@ void stm32_usart_enable_tx_irq(const stm32_usart_t* usart);
|
||||
void stm32_usart_set_idle_irq(const stm32_usart_t* usart, uint32_t enabled);
|
||||
void stm32_usart_deinit(const stm32_usart_t* usart);
|
||||
void stm32_usart_deinit_rx_dma(const stm32_usart_t* usart);
|
||||
|
||||
#if defined(STM32H7) || defined(STM32H7RS)
|
||||
void stm32_usart_rx_inversion(const stm32_usart_t* usart, bool on);
|
||||
void stm32_usart_tx_inversion(const stm32_usart_t* usart, bool off);
|
||||
#endif
|
||||
|
||||
void stm32_usart_send_byte(const stm32_usart_t* usart, uint8_t byte);
|
||||
void stm32_usart_send_buffer(const stm32_usart_t* usart, const uint8_t * data, uint32_t size);
|
||||
uint8_t stm32_usart_tx_completed(const stm32_usart_t* usart);
|
||||
@@ -71,3 +77,4 @@ void stm32_usart_set_baudrate(const stm32_usart_t* usart, uint32_t baudrate);
|
||||
void stm32_usart_set_hw_option(const stm32_usart_t* usart, uint32_t option);
|
||||
void stm32_usart_isr(const stm32_usart_t* usart, etx_serial_callbacks_t* cb);
|
||||
void stm32_usart_tx_dma_isr(const stm32_usart_t* usart);
|
||||
|
||||
|
||||
@@ -88,9 +88,9 @@ uint8_t pulse_inc = 1;
|
||||
// DMA buffer contains pulses for 2 LED at a time
|
||||
// (allows for refill at HT and TC)
|
||||
#if defined(STM32_SUPPORT_32BIT_TIMERS)
|
||||
static led_timer_value_t _led_dma_buffer[WS2821_DMA_BUFFER_LEN * 2] __DMA;
|
||||
static led_timer_value_t _led_dma_buffer[WS2821_DMA_BUFFER_LEN * 2] __DMA_NO_CACHE;
|
||||
#else
|
||||
static led_timer_value_t _led_dma_buffer[WS2821_DMA_BUFFER_LEN] __DMA;
|
||||
static led_timer_value_t _led_dma_buffer[WS2821_DMA_BUFFER_LEN] __DMA_NO_CACHE;
|
||||
#endif
|
||||
|
||||
static uint8_t _led_seq_cnt;
|
||||
@@ -194,7 +194,7 @@ static void _init_timer(const stm32_pulse_timer_t* tim)
|
||||
LL_DMA_SetMode(tim->DMAx, tim->DMA_Stream, LL_DMA_MODE_CIRCULAR);
|
||||
LL_DMA_SetDataLength(tim->DMAx, tim->DMA_Stream, WS2821_DMA_BUFFER_LEN);
|
||||
LL_DMA_SetMemoryAddress(tim->DMAx, tim->DMA_Stream, (uint32_t)_led_dma_buffer);
|
||||
|
||||
|
||||
// we need to use a higher prio to avoid having
|
||||
// issues with some other things used during boot
|
||||
NVIC_SetPriority(tim->DMA_IRQn, WS2812_DMA_IRQ_PRIO);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTx
|
||||
* Copyright (C) EdgeTx
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
@@ -46,4 +46,4 @@ bool ws2812_get_state(uint8_t led);
|
||||
void ws2812_dma_isr(const stm32_pulse_timer_t* timer);
|
||||
|
||||
void ws2812_set_color(uint8_t led, uint8_t r, uint8_t g, uint8_t b);
|
||||
uint32_t ws2812_get_color(uint8_t led);
|
||||
uint32_t ws2812_get_color(uint8_t led);
|
||||
@@ -0,0 +1,411 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "stm32_xspi_nor.h"
|
||||
|
||||
#define AUTO_POLLING_INTERVAL 0x10
|
||||
|
||||
#define SPI_NOR_CMD_RDSR 0x05
|
||||
#define SPI_NOR_CMD_WREN 0x06
|
||||
#define SPI_NOR_CMD_READ_FAST_4B 0x0C
|
||||
#define SPI_NOR_CMD_PP_4B 0x12
|
||||
#define SPI_NOR_CMD_WR_CFGREG2 0x72
|
||||
|
||||
#define SPI_NOR_DUMMY_REG_OCTAL 4U
|
||||
|
||||
#define SPI_NOR_OCMD_RDSR 0x05FA
|
||||
#define SPI_NOR_OCMD_WREN 0x06F9
|
||||
#define SPI_NOR_OCMD_READ 0xEC13
|
||||
#define SPI_NOR_OCMD_DTR_READ 0xEE11
|
||||
#define SPI_NOR_OCMD_PAGE_PRG 0x12ED
|
||||
#define SPI_NOR_OCMD_ERASE_64K 0xDC23;
|
||||
|
||||
#define SPI_NOR_DUMMY_REG_OCTAL_DTR 5U
|
||||
#define SPI_NOR_DUMMY_READ 20U
|
||||
|
||||
#define SPI_NOR_REG2_ADDR1 0x0000000
|
||||
#define SPI_NOR_CR2_STR_OPI_EN 0x01
|
||||
#define SPI_NOR_CR2_DTR_OPI_EN 0x02
|
||||
|
||||
#define SPI_NOR_REG2_ADDR3 0x00000300
|
||||
|
||||
#define MEMORY_READY_MATCH_VALUE 0x00
|
||||
#define MEMORY_READY_MASK_VALUE 0x01
|
||||
|
||||
#define WRITE_ENABLE_MATCH_VALUE 0x02
|
||||
#define WRITE_ENABLE_MASK_VALUE 0x02
|
||||
|
||||
static uint32_t xspi_nor_read_instr(const uint8_t mode, const uint8_t rate)
|
||||
{
|
||||
if (rate == XSPI_STR_TRANSFER) {
|
||||
return (mode == XSPI_SPI_MODE) ? SPI_NOR_CMD_PP_4B : SPI_NOR_OCMD_READ;
|
||||
} else {
|
||||
return SPI_NOR_OCMD_DTR_READ;
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t xspi_nor_page_prg_instr(const uint8_t mode)
|
||||
{
|
||||
if (mode == XSPI_SPI_MODE) {
|
||||
return SPI_NOR_CMD_PP_4B;
|
||||
} else {
|
||||
return SPI_NOR_OCMD_PAGE_PRG;
|
||||
}
|
||||
}
|
||||
|
||||
static XSPI_RegularCmdTypeDef xspi_prepare_cmd(const uint8_t transfer_mode,
|
||||
const uint8_t transfer_rate)
|
||||
{
|
||||
XSPI_RegularCmdTypeDef cmd_tmp = {
|
||||
.OperationType = HAL_XSPI_OPTYPE_COMMON_CFG,
|
||||
.InstructionDTRMode = ((transfer_rate == XSPI_DTR_TRANSFER)
|
||||
? HAL_XSPI_INSTRUCTION_DTR_ENABLE
|
||||
: HAL_XSPI_INSTRUCTION_DTR_DISABLE),
|
||||
.AddressWidth = HAL_XSPI_ADDRESS_32_BITS,
|
||||
.AddressDTRMode =
|
||||
((transfer_rate == XSPI_DTR_TRANSFER) ? HAL_XSPI_ADDRESS_DTR_ENABLE
|
||||
: HAL_XSPI_ADDRESS_DTR_DISABLE),
|
||||
.AlternateBytesMode = HAL_XSPI_ALT_BYTES_NONE,
|
||||
.DataDTRMode =
|
||||
((transfer_rate == XSPI_DTR_TRANSFER) ? HAL_XSPI_DATA_DTR_ENABLE
|
||||
: HAL_XSPI_DATA_DTR_DISABLE),
|
||||
.DQSMode = (transfer_rate == XSPI_DTR_TRANSFER) ? HAL_XSPI_DQS_ENABLE
|
||||
: HAL_XSPI_DQS_DISABLE,
|
||||
};
|
||||
|
||||
switch (transfer_mode) {
|
||||
case XSPI_OCTO_MODE:
|
||||
cmd_tmp.InstructionMode = HAL_XSPI_INSTRUCTION_8_LINES;
|
||||
cmd_tmp.InstructionWidth = HAL_XSPI_INSTRUCTION_16_BITS;
|
||||
cmd_tmp.AddressMode = HAL_XSPI_ADDRESS_8_LINES;
|
||||
cmd_tmp.DataMode = HAL_XSPI_DATA_8_LINES;
|
||||
break;
|
||||
|
||||
case XSPI_SPI_MODE:
|
||||
default:
|
||||
cmd_tmp.InstructionMode = HAL_XSPI_INSTRUCTION_1_LINE;
|
||||
cmd_tmp.InstructionWidth = HAL_XSPI_INSTRUCTION_8_BITS;
|
||||
cmd_tmp.AddressMode = HAL_XSPI_ADDRESS_1_LINE;
|
||||
cmd_tmp.DataMode = HAL_XSPI_DATA_1_LINE;
|
||||
break;
|
||||
}
|
||||
|
||||
return cmd_tmp;
|
||||
}
|
||||
|
||||
static int xspi_auto_polling(const stm32_xspi_nor_t* dev, uint32_t value,
|
||||
uint32_t mask)
|
||||
{
|
||||
XSPI_AutoPollingTypeDef s_config = {
|
||||
.MatchValue = value,
|
||||
.MatchMask = mask,
|
||||
.MatchMode = HAL_XSPI_MATCH_MODE_AND,
|
||||
.AutomaticStop = HAL_XSPI_AUTOMATIC_STOP_ENABLE,
|
||||
.IntervalTime = AUTO_POLLING_INTERVAL,
|
||||
};
|
||||
|
||||
if (HAL_XSPI_AutoPolling(dev->hxspi, &s_config,
|
||||
HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int xspi_nor_auto_polling(const stm32_xspi_nor_t* dev, const uint8_t mode,
|
||||
const uint8_t rate, uint32_t value,
|
||||
uint32_t mask)
|
||||
{
|
||||
XSPI_RegularCmdTypeDef s_command = xspi_prepare_cmd(mode, rate);
|
||||
|
||||
if (mode == XSPI_OCTO_MODE) {
|
||||
s_command.Instruction = SPI_NOR_OCMD_RDSR;
|
||||
s_command.AddressMode = HAL_XSPI_ADDRESS_8_LINES;
|
||||
s_command.DataMode = HAL_XSPI_DATA_8_LINES;
|
||||
s_command.DummyCycles = (rate == XSPI_DTR_TRANSFER)
|
||||
? SPI_NOR_DUMMY_REG_OCTAL_DTR
|
||||
: SPI_NOR_DUMMY_REG_OCTAL;
|
||||
} else {
|
||||
s_command.Instruction = SPI_NOR_CMD_RDSR;
|
||||
s_command.InstructionMode = HAL_XSPI_INSTRUCTION_1_LINE;
|
||||
s_command.AddressMode = HAL_XSPI_ADDRESS_1_LINE;
|
||||
s_command.DataMode = HAL_XSPI_DATA_1_LINE;
|
||||
s_command.DummyCycles = 0;
|
||||
}
|
||||
s_command.DataLength = ((rate == XSPI_DTR_TRANSFER) ? 2U : 1U);
|
||||
s_command.Address = 0U;
|
||||
|
||||
if (HAL_XSPI_Command(dev->hxspi, &s_command,
|
||||
HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return xspi_auto_polling(dev, value, mask);
|
||||
}
|
||||
|
||||
static int xspi_nor_auto_polling_mem_ready(const stm32_xspi_nor_t* dev,
|
||||
const uint8_t mode,
|
||||
const uint8_t rate)
|
||||
{
|
||||
return xspi_nor_auto_polling(dev, mode, rate, MEMORY_READY_MATCH_VALUE,
|
||||
MEMORY_READY_MASK_VALUE);
|
||||
}
|
||||
|
||||
static int xspi_nor_write_enable(const stm32_xspi_nor_t* dev, const uint8_t mode,
|
||||
const uint8_t rate)
|
||||
{
|
||||
XSPI_RegularCmdTypeDef s_command = xspi_prepare_cmd(mode, rate);
|
||||
// Initialize the write enable command
|
||||
if (mode == XSPI_OCTO_MODE) {
|
||||
s_command.Instruction = SPI_NOR_OCMD_WREN;
|
||||
} else {
|
||||
s_command.Instruction = SPI_NOR_CMD_WREN;
|
||||
s_command.InstructionMode = HAL_XSPI_INSTRUCTION_1_LINE;
|
||||
}
|
||||
s_command.AddressMode = HAL_XSPI_ADDRESS_NONE;
|
||||
s_command.DataMode = HAL_XSPI_DATA_NONE;
|
||||
s_command.DummyCycles = 0U;
|
||||
|
||||
if (HAL_XSPI_Command(dev->hxspi, &s_command,
|
||||
HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return xspi_nor_auto_polling(dev, mode, rate, WRITE_ENABLE_MATCH_VALUE,
|
||||
WRITE_ENABLE_MASK_VALUE);
|
||||
}
|
||||
|
||||
static int xspi_nor_write_enable(const stm32_xspi_nor_t* dev)
|
||||
{
|
||||
uint8_t mode = dev->data_mode;
|
||||
uint8_t rate = dev->data_rate;
|
||||
return xspi_nor_write_enable(dev, mode, rate);
|
||||
}
|
||||
|
||||
static int xspi_nor_config_mem(const stm32_xspi_nor_t* dev)
|
||||
{
|
||||
if (dev->data_mode != XSPI_OCTO_MODE) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t mode = XSPI_SPI_MODE;
|
||||
uint8_t rate = XSPI_STR_TRANSFER;
|
||||
|
||||
int err = xspi_nor_write_enable(dev, mode, rate);
|
||||
if (err != 0) return -1;
|
||||
|
||||
XSPI_RegularCmdTypeDef s_command = xspi_prepare_cmd(mode, rate);
|
||||
s_command.Instruction = SPI_NOR_CMD_WR_CFGREG2;
|
||||
s_command.DummyCycles = 0;
|
||||
|
||||
s_command.Address = SPI_NOR_REG2_ADDR1;
|
||||
s_command.DataLength = 1U;
|
||||
|
||||
uint8_t reg = dev->data_rate == XSPI_STR_TRANSFER ? SPI_NOR_CR2_STR_OPI_EN
|
||||
: SPI_NOR_CR2_DTR_OPI_EN;
|
||||
|
||||
if (HAL_XSPI_Command(dev->hxspi, &s_command,
|
||||
HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (HAL_XSPI_Transmit(dev->hxspi, ®, HAL_XSPI_TIMEOUT_DEFAULT_VALUE) !=
|
||||
HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return xspi_nor_auto_polling(dev, mode, rate, WRITE_ENABLE_MATCH_VALUE,
|
||||
WRITE_ENABLE_MASK_VALUE);
|
||||
}
|
||||
|
||||
int stm32_xspi_nor_init(const stm32_xspi_nor_t* dev)
|
||||
{
|
||||
if (HAL_XSPI_Init(dev->hxspi) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if defined(BOOT)
|
||||
#if defined(HAL_XSPIM_IOPORT_1) || defined(HAL_XSPIM_IOPORT_2)
|
||||
// XSPI I/O manager init Function
|
||||
XSPIM_CfgTypeDef xspi_mgr_cfg;
|
||||
|
||||
if (dev->hxspi->Instance == XSPI1) {
|
||||
xspi_mgr_cfg.IOPort = HAL_XSPIM_IOPORT_1;
|
||||
} else if (dev->hxspi->Instance == XSPI2) {
|
||||
xspi_mgr_cfg.IOPort = HAL_XSPIM_IOPORT_2;
|
||||
}
|
||||
// HAL_XSPI_CSSEL_OVR_NCS1 ??
|
||||
xspi_mgr_cfg.nCSOverride = HAL_XSPI_CSSEL_OVR_DISABLED;
|
||||
xspi_mgr_cfg.Req2AckTime = 1;
|
||||
|
||||
if (HAL_XSPIM_Config(dev->hxspi, &xspi_mgr_cfg,
|
||||
HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
#endif /* XSPIM */
|
||||
#endif
|
||||
|
||||
if (xspi_nor_config_mem(dev) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_xspi_nor_memory_mapped(const stm32_xspi_nor_t* dev)
|
||||
{
|
||||
XSPI_MemoryMappedTypeDef s_mem_mapped_cfg = {0};
|
||||
|
||||
if (xspi_nor_write_enable(dev) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t mode = dev->data_mode;
|
||||
uint8_t rate = dev->data_rate;
|
||||
|
||||
XSPI_RegularCmdTypeDef s_command = xspi_prepare_cmd(mode, rate);
|
||||
s_command.OperationType = HAL_XSPI_OPTYPE_WRITE_CFG;
|
||||
s_command.Instruction = xspi_nor_page_prg_instr(mode);
|
||||
s_command.Address = 0x0;
|
||||
s_command.DummyCycles = 0;
|
||||
|
||||
if (HAL_XSPI_Command(dev->hxspi, &s_command,
|
||||
HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
s_command.OperationType = HAL_XSPI_OPTYPE_READ_CFG;
|
||||
s_command.Instruction = xspi_nor_read_instr(mode, rate);
|
||||
s_command.DummyCycles = SPI_NOR_DUMMY_READ;
|
||||
|
||||
if (HAL_XSPI_Command(dev->hxspi, &s_command,
|
||||
HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
s_mem_mapped_cfg.TimeOutActivation = HAL_XSPI_TIMEOUT_COUNTER_DISABLE;
|
||||
if (HAL_XSPI_MemoryMapped(dev->hxspi, &s_mem_mapped_cfg) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool xspi_nor_is_memory_mapped(const stm32_xspi_nor_t* dev)
|
||||
{
|
||||
return ((READ_BIT(dev->hxspi->Instance->CR, XSPI_CR_FMODE) == XSPI_CR_FMODE)
|
||||
? true
|
||||
: false);
|
||||
}
|
||||
|
||||
static int xspi_nor_abort(const stm32_xspi_nor_t* dev)
|
||||
{
|
||||
if (HAL_XSPI_Abort(dev->hxspi) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_xspi_nor_read(const stm32_xspi_nor_t* dev, uint32_t address, void* data,
|
||||
uint32_t len)
|
||||
{
|
||||
uint8_t mode = dev->data_mode;
|
||||
uint8_t rate = dev->data_rate;
|
||||
|
||||
XSPI_RegularCmdTypeDef s_command = xspi_prepare_cmd(mode, rate);
|
||||
s_command.Instruction = xspi_nor_read_instr(mode, rate);
|
||||
s_command.Address = address;
|
||||
s_command.DataLength = len;
|
||||
s_command.DummyCycles = SPI_NOR_DUMMY_READ;
|
||||
|
||||
if (HAL_XSPI_Command(dev->hxspi, &s_command,
|
||||
HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (HAL_XSPI_Receive(dev->hxspi, (uint8_t *)data,
|
||||
HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stm32_xspi_nor_erase_sector(const stm32_xspi_nor_t* dev, uint32_t address)
|
||||
{
|
||||
if (xspi_nor_is_memory_mapped(dev)) {
|
||||
if (xspi_nor_abort(dev) != 0) return -1;
|
||||
}
|
||||
|
||||
if (xspi_nor_write_enable(dev) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t mode = dev->data_mode;
|
||||
uint8_t rate = dev->data_rate;
|
||||
|
||||
XSPI_RegularCmdTypeDef s_command = xspi_prepare_cmd(mode, rate);
|
||||
s_command.Instruction = SPI_NOR_OCMD_ERASE_64K;
|
||||
s_command.Address = address;
|
||||
s_command.DataMode = HAL_XSPI_DATA_NONE;
|
||||
s_command.DummyCycles = 0;
|
||||
|
||||
if (HAL_XSPI_Command(dev->hxspi, &s_command,
|
||||
HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return xspi_nor_auto_polling_mem_ready(dev, mode, rate);
|
||||
}
|
||||
|
||||
int stm32_xspi_nor_program(const stm32_xspi_nor_t* dev, uint32_t address,
|
||||
void* data, uint32_t len)
|
||||
{
|
||||
if (xspi_nor_is_memory_mapped(dev)) {
|
||||
if (xspi_nor_abort(dev) != 0) return -1;
|
||||
}
|
||||
|
||||
if (xspi_nor_write_enable(dev) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t mode = dev->data_mode;
|
||||
uint8_t rate = dev->data_rate;
|
||||
|
||||
XSPI_RegularCmdTypeDef s_command = xspi_prepare_cmd(mode, rate);
|
||||
s_command.Instruction = xspi_nor_page_prg_instr(mode);
|
||||
s_command.Address = address;
|
||||
s_command.DataLength = len;
|
||||
s_command.DummyCycles = 0;
|
||||
|
||||
if (HAL_XSPI_Command(dev->hxspi, &s_command,
|
||||
HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (HAL_XSPI_Transmit(dev->hxspi, (uint8_t*)data,
|
||||
HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return xspi_nor_auto_polling_mem_ready(dev, mode, rate);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "stm32_hal_ll.h"
|
||||
#include "stm32_hal.h"
|
||||
|
||||
typedef enum {
|
||||
XSPI_SPI_MODE = 0,
|
||||
XSPI_OCTO_MODE,
|
||||
} stm32_xspi_mode_t;
|
||||
|
||||
typedef enum {
|
||||
XSPI_STR_TRANSFER = 0,
|
||||
XSPI_DTR_TRANSFER,
|
||||
} stm32_xspi_rate_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t flash_size;
|
||||
uint32_t page_size;
|
||||
stm32_xspi_mode_t data_mode;
|
||||
stm32_xspi_rate_t data_rate;
|
||||
|
||||
XSPI_HandleTypeDef* hxspi;
|
||||
} stm32_xspi_nor_t;
|
||||
|
||||
int stm32_xspi_nor_init(const stm32_xspi_nor_t* dev);
|
||||
int stm32_xspi_nor_memory_mapped(const stm32_xspi_nor_t* dev);
|
||||
int stm32_xspi_nor_read(const stm32_xspi_nor_t* dev, uint32_t address,
|
||||
void* data, uint32_t len);
|
||||
int stm32_xspi_nor_erase_sector(const stm32_xspi_nor_t* dev, uint32_t address);
|
||||
int stm32_xspi_nor_program(const stm32_xspi_nor_t* dev, uint32_t address,
|
||||
void* data, uint32_t len);
|
||||
@@ -0,0 +1,507 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32h7rsxx_hal_conf.h
|
||||
* @author MCD Application Team
|
||||
* @brief HAL configuration template file.
|
||||
* This file should be copied to the application folder and renamed
|
||||
* to stm32h7rsxx_hal_conf.h.
|
||||
*
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* Copyright (c) 2022 STMicroelectronics.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef STM32H7RSxx_HAL_CONF_H
|
||||
#define STM32H7RSxx_HAL_CONF_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
|
||||
/* ########################## Module Selection ############################## */
|
||||
/**
|
||||
* @brief This is the list of modules to be used in the HAL driver
|
||||
*/
|
||||
#define HAL_MODULE_ENABLED
|
||||
// #define HAL_ADC_MODULE_ENABLED
|
||||
// #define HAL_CEC_MODULE_ENABLED
|
||||
// #define HAL_CORDIC_MODULE_ENABLED
|
||||
#define HAL_CORTEX_MODULE_ENABLED
|
||||
// #define HAL_CRC_MODULE_ENABLED
|
||||
// #define HAL_CRYP_MODULE_ENABLED
|
||||
// #define HAL_DCMIPP_MODULE_ENABLED
|
||||
#define HAL_DMA_MODULE_ENABLED
|
||||
#define HAL_DMA2D_MODULE_ENABLED
|
||||
// #define HAL_DTS_MODULE_ENABLED
|
||||
// #define HAL_ETH_MODULE_ENABLED
|
||||
// #define HAL_EXTI_MODULE_ENABLED
|
||||
// #define HAL_FDCAN_MODULE_ENABLED
|
||||
#define HAL_FLASH_MODULE_ENABLED
|
||||
// #define HAL_GFXMMU_MODULE_ENABLED
|
||||
// #define HAL_GFXTIM_MODULE_ENABLED
|
||||
#define HAL_GPIO_MODULE_ENABLED
|
||||
// #define HAL_GPU2D_MODULE_ENABLED
|
||||
// #define HAL_HASH_MODULE_ENABLED
|
||||
// #define HAL_HCD_MODULE_ENABLED
|
||||
#define HAL_I2C_MODULE_ENABLED
|
||||
// #define HAL_I2S_MODULE_ENABLED
|
||||
// #define HAL_I3C_MODULE_ENABLED
|
||||
// #define HAL_ICACHE_MODULE_ENABLED
|
||||
// #define HAL_IRDA_MODULE_ENABLED
|
||||
// #define HAL_IWDG_MODULE_ENABLED
|
||||
// #define HAL_JPEG_MODULE_ENABLED
|
||||
#define HAL_LPTIM_MODULE_ENABLED
|
||||
#define HAL_LTDC_MODULE_ENABLED
|
||||
// #define HAL_MCE_MODULE_ENABLED
|
||||
// #define HAL_MDF_MODULE_ENABLED
|
||||
// #define HAL_MMC_MODULE_ENABLED
|
||||
// #define HAL_NAND_MODULE_ENABLED
|
||||
// #define HAL_NOR_MODULE_ENABLED
|
||||
#define HAL_PCD_MODULE_ENABLED
|
||||
// #define HAL_PKA_MODULE_ENABLED
|
||||
// #define HAL_PSSI_MODULE_ENABLED
|
||||
#define HAL_PWR_MODULE_ENABLED
|
||||
// #define HAL_RAMECC_MODULE_ENABLED
|
||||
#define HAL_RCC_MODULE_ENABLED
|
||||
// #define HAL_RNG_MODULE_ENABLED
|
||||
#define HAL_RTC_MODULE_ENABLED
|
||||
// #define HAL_SAI_MODULE_ENABLED
|
||||
#define HAL_SD_MODULE_ENABLED
|
||||
// #define HAL_SDRAM_MODULE_ENABLED
|
||||
// #define HAL_SMARTCARD_MODULE_ENABLED
|
||||
// #define HAL_SMBUS_MODULE_ENABLED
|
||||
// #define HAL_SPDIFRX_MODULE_ENABLED
|
||||
#define HAL_SPI_MODULE_ENABLED
|
||||
// #define HAL_SRAM_MODULE_ENABLED
|
||||
// #define HAL_TIM_MODULE_ENABLED
|
||||
// #define HAL_UART_MODULE_ENABLED
|
||||
// #define HAL_USART_MODULE_ENABLED
|
||||
// #define HAL_WWDG_MODULE_ENABLED
|
||||
#define HAL_XSPI_MODULE_ENABLED
|
||||
|
||||
/* ########################## Oscillator Values adaptation ####################*/
|
||||
/**
|
||||
* @brief Adjust the value of External High Speed oscillator (HSE) used in your application.
|
||||
* This value is used by the RCC HAL module to compute the system frequency
|
||||
* (when HSE is used as system clock source, directly or through the PLL).
|
||||
*/
|
||||
#if !defined (HSE_VALUE)
|
||||
#define HSE_VALUE 24000000UL /*!< Value of the External oscillator in Hz */
|
||||
#endif /* HSE_VALUE */
|
||||
|
||||
#if !defined (HSE_STARTUP_TIMEOUT)
|
||||
#define HSE_STARTUP_TIMEOUT 100UL /*!< Time out for HSE start up (in ms) */
|
||||
#endif /* HSE_STARTUP_TIMEOUT */
|
||||
|
||||
/**
|
||||
* @brief Internal High Speed oscillator (HSI) value.
|
||||
* This value is used by the RCC HAL module to compute the system frequency
|
||||
* (when HSI is used as system clock source, directly or through the PLL).
|
||||
*/
|
||||
#if !defined (HSI_VALUE)
|
||||
#define HSI_VALUE 64000000UL /*!< Value of the Internal oscillator in Hz */
|
||||
#endif /* HSI_VALUE */
|
||||
|
||||
/**
|
||||
* @brief Internal Low-power oscillator (CSI) default value.
|
||||
* This value is the default CSI range value after Reset.
|
||||
*/
|
||||
#if !defined (CSI_VALUE)
|
||||
#define CSI_VALUE 4000000UL /*!< Value of the Internal oscillator in Hz */
|
||||
#endif /* CSI_VALUE */
|
||||
|
||||
/**
|
||||
* @brief Internal High Speed oscillator (HSI48) value for USB OTG FS and RNG.
|
||||
* This internal oscillator is mainly dedicated to provide a high precision clock to
|
||||
* the USB peripheral by means of a special Clock Recovery System (CRS) circuitry.
|
||||
* When the CRS is not used, the HSI48 RC oscillator runs on it default frequency
|
||||
* which is subject to manufacturing process variations.
|
||||
*/
|
||||
#if !defined (HSI48_VALUE)
|
||||
#define HSI48_VALUE 48000000UL /*!< Value of the Internal High Speed oscillator for USB OTG FS/RNG in Hz.
|
||||
The real value my vary depending on manufacturing process variations. */
|
||||
#endif /* HSI48_VALUE */
|
||||
|
||||
/**
|
||||
* @brief Internal Low Speed oscillator (LSI) value.
|
||||
*/
|
||||
#if !defined (LSI_VALUE)
|
||||
#define LSI_VALUE 32000UL /*!< LSI Typical Value in Hz.
|
||||
Value of the Internal Low Speed oscillator in Hz.
|
||||
The real value may vary depending on the variations
|
||||
in voltage and temperature. */
|
||||
#endif /* LSI_VALUE */
|
||||
|
||||
/**
|
||||
* @brief External Low Speed oscillator (LSE) value.
|
||||
*/
|
||||
#if !defined (LSE_VALUE)
|
||||
#define LSE_VALUE 32768UL /*!< Value of the External oscillator in Hz */
|
||||
#endif /* LSE_VALUE */
|
||||
|
||||
#if !defined (LSE_STARTUP_TIMEOUT)
|
||||
#define LSE_STARTUP_TIMEOUT 5000UL /*!< Time out for LSE start up (in ms) */
|
||||
#endif /* LSE_STARTUP_TIMEOUT */
|
||||
|
||||
/**
|
||||
* @brief External clock source for digital audio interfaces: SPI/I2S, SAI and ADF
|
||||
* This value is used by the RCC HAL module to provide the digital audio interfaces
|
||||
* frequency. This clock source is inserted directly through I2S_CKIN pad.
|
||||
*/
|
||||
#if !defined (EXTERNAL_CLOCK_VALUE)
|
||||
#define EXTERNAL_CLOCK_VALUE 48000UL /*!< Value of the external clock source in Hz */
|
||||
#endif /* EXTERNAL_CLOCK_VALUE */
|
||||
|
||||
/* Tip: To avoid modifying this file each time you need to use different HSE,
|
||||
=== you can define the HSE value in your toolchain compiler preprocessor. */
|
||||
|
||||
/* ########################### System Configuration ######################### */
|
||||
/**
|
||||
* @brief This is the HAL system configuration section
|
||||
*/
|
||||
#define VDD_VALUE 3300UL /*!< Value of VDD in mv */
|
||||
#define TICK_INT_PRIORITY ((1UL<<__NVIC_PRIO_BITS) - 1UL) /*!< tick interrupt priority (lowest by default) */
|
||||
#define USE_RTOS 0U
|
||||
|
||||
/* ########################## Assert Selection ############################## */
|
||||
/**
|
||||
* @brief Uncomment the line below to expanse the "assert_param" macro in the
|
||||
* HAL drivers code
|
||||
*/
|
||||
/* #define USE_FULL_ASSERT 1U */
|
||||
|
||||
/* ################## Register callback feature configuration ############### */
|
||||
/**
|
||||
* @brief Set below the peripheral configuration to "1U" to add the support
|
||||
* of HAL callback registration/unregistration feature for the HAL
|
||||
* driver(s). This allows user application to provide specific callback
|
||||
* functions thanks to HAL_PPP_RegisterCallback() rather than overwriting
|
||||
* the default weak callback functions (see each stm32h7rsxx_hal_ppp.h file
|
||||
* for possible callback identifiers defined in HAL_PPP_CallbackIDTypeDef
|
||||
* for each PPP peripheral).
|
||||
*/
|
||||
#define USE_HAL_ADC_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_CEC_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_CORDIC_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_CRYP_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_DCMIPP_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_FDCAN_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_GFXMMU_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_HASH_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_I2C_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_I2S_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_JPEG_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_LPTIM_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_MDF_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_MMC_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_NAND_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_NOR_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_PCD_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_PKA_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_PSSI_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_RNG_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_RTC_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_SAI_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_SD_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_SDRAM_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_SPDIFRX_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_SPI_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_SRAM_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_TIM_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_UART_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_USART_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U
|
||||
#define USE_HAL_XSPI_REGISTER_CALLBACKS 0U
|
||||
|
||||
/* ################## SPI peripheral configuration ########################## */
|
||||
|
||||
/* CRC FEATURE: Use to activate CRC feature inside HAL SPI Driver
|
||||
* Activated: CRC code is present inside driver
|
||||
* Deactivated: CRC code cleaned from driver
|
||||
*/
|
||||
|
||||
#define USE_SPI_CRC 1U
|
||||
|
||||
/* ################## CRYP peripheral configuration ########################## */
|
||||
|
||||
/**
|
||||
* @brief For code optimization purpose, uncomment and set to "1U" the USE_HAL_CRYP_ONLY or USE_HAL_SAES_ONLY,
|
||||
* to use only one peripheral. Both defines cannot be set to "1U" at the same time.
|
||||
*/
|
||||
|
||||
|
||||
/* #define USE_HAL_CRYP_ONLY 1U */
|
||||
/* #define USE_HAL_SAES_ONLY 0U */
|
||||
|
||||
#define USE_HAL_CRYP_SUSPEND_RESUME 0U
|
||||
|
||||
/* ################## HASH peripheral configuration ########################## */
|
||||
|
||||
#define USE_HAL_HASH_SUSPEND_RESUME 0U
|
||||
|
||||
/* ################## SDMMC peripheral configuration ######################### */
|
||||
|
||||
#define USE_SD_TRANSCEIVER 0U
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief Include module's header file
|
||||
*/
|
||||
|
||||
#ifdef HAL_RCC_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_rcc.h"
|
||||
#endif /* HAL_RCC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_GPIO_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_gpio.h"
|
||||
#endif /* HAL_GPIO_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_DMA_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_dma.h"
|
||||
#endif /* HAL_DMA_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_CORTEX_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_cortex.h"
|
||||
#endif /* HAL_CORTEX_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_ADC_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_adc.h"
|
||||
#endif /* HAL_ADC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_CEC_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_cec.h"
|
||||
#endif /* HAL_CEC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_CORDIC_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_cordic.h"
|
||||
#endif /* HAL_CORDIC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_CRC_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_crc.h"
|
||||
#endif /* HAL_CRC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_CRYP_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_cryp.h"
|
||||
#endif /* HAL_CRYP_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_DCMIPP_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_dcmipp.h"
|
||||
#endif /* HAL_DCMIPP_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_DMA2D_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_dma2d.h"
|
||||
#endif /* HAL_DMA2D_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_DTS_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_dts.h"
|
||||
#endif /* HAL_DTS_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_ETH_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_eth.h"
|
||||
#endif /* HAL_ETH_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_EXTI_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_exti.h"
|
||||
#endif /* HAL_EXTI_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_FDCAN_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_fdcan.h"
|
||||
#endif /* HAL_FDCAN_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_FLASH_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_flash.h"
|
||||
#endif /* HAL_FLASH_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_GFXMMU_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_gfxmmu.h"
|
||||
#endif /* HAL_GFXMMU_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_GFXTIM_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_gfxtim.h"
|
||||
#endif /* HAL_GFXTIM_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_GPU2D_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_gpu2d.h"
|
||||
#endif /* HAL_GPU2D_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_HASH_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_hash.h"
|
||||
#endif /* HAL_HASH_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_HCD_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_hcd.h"
|
||||
#endif /* HAL_HCD_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_I2C_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_i2c.h"
|
||||
#endif /* HAL_I2C_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_I2S_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_i2s.h"
|
||||
#endif /* HAL_I2S_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_I3C_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_i3c.h"
|
||||
#endif /* HAL_I3C_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_ICACHE_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_icache.h"
|
||||
#endif /* HAL_ICACHE_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_IRDA_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_irda.h"
|
||||
#endif /* HAL_IRDA_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_IWDG_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_iwdg.h"
|
||||
#endif /* HAL_IWDG_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_JPEG_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_jpeg.h"
|
||||
#endif /* HAL_JPEG_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_LTDC_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_ltdc.h"
|
||||
#endif /* HAL_LTDC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_LPTIM_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_lptim.h"
|
||||
#endif /* HAL_LPTIM_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_MCE_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_mce.h"
|
||||
#endif /* HAL_MCE_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_MDF_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_mdf.h"
|
||||
#endif /* HAL_MDF_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_MMC_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_mmc.h"
|
||||
#endif /* HAL_MMC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_NAND_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_nand.h"
|
||||
#endif /* HAL_NAND_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_NOR_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_nor.h"
|
||||
#endif /* HAL_NOR_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_PCD_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_pcd.h"
|
||||
#endif /* HAL_PCD_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_PKA_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_pka.h"
|
||||
#endif /* HAL_PKA_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_PSSI_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_pssi.h"
|
||||
#endif /* HAL_PSSI_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_PWR_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_pwr.h"
|
||||
#endif /* HAL_PWR_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_RAMECC_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_ramecc.h"
|
||||
#endif /* HAL_RAMECC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_RNG_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_rng.h"
|
||||
#endif /* HAL_RNG_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_RTC_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_rtc.h"
|
||||
#endif /* HAL_RTC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_SAI_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_sai.h"
|
||||
#endif /* HAL_SAI_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_SD_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_sd.h"
|
||||
#endif /* HAL_SD_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_SDRAM_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_sdram.h"
|
||||
#endif /* HAL_SDRAM_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_SMARTCARD_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_smartcard.h"
|
||||
#endif /* HAL_SMARTCARD_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_SMBUS_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_smbus.h"
|
||||
#endif /* HAL_SMBUS_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_SPDIFRX_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_spdifrx.h"
|
||||
#endif /* HAL_SPDIFRX_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_SPI_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_spi.h"
|
||||
#endif /* HAL_SPI_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_SRAM_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_sram.h"
|
||||
#endif /* HAL_SRAM_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_TIM_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_tim.h"
|
||||
#endif /* HAL_TIM_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_UART_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_uart.h"
|
||||
#endif /* HAL_UART_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_USART_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_usart.h"
|
||||
#endif /* HAL_USART_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_WWDG_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_wwdg.h"
|
||||
#endif /* HAL_WWDG_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_XSPI_MODULE_ENABLED
|
||||
#include "stm32h7rsxx_hal_xspi.h"
|
||||
#endif /* HAL_XSPI_MODULE_ENABLED */
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
#ifdef USE_FULL_ASSERT
|
||||
/**
|
||||
* @brief The assert_param macro is used for function's parameters check.
|
||||
* @param expr If expr is false, it calls assert_failed function
|
||||
* which reports the name of the source file and the source
|
||||
* line number of the call that failed.
|
||||
* If expr is true, it returns no value.
|
||||
* @retval None
|
||||
*/
|
||||
#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__))
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void assert_failed(uint8_t *file, uint32_t line);
|
||||
#else
|
||||
#define assert_param(expr) ((void)0U)
|
||||
#endif /* USE_FULL_ASSERT */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* STM32H7RSxx_HAL_CONF_H */
|
||||
@@ -28,12 +28,13 @@
|
||||
#endif
|
||||
|
||||
extern "C" {
|
||||
#include "usbd_conf.h"
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_msc.h"
|
||||
#include "usbd_desc.h"
|
||||
#include "usbd_hid.h"
|
||||
#include "usbd_cdc.h"
|
||||
#include "usbd_conf.h"
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_msc.h"
|
||||
#include "usbd_desc.h"
|
||||
#include "usbd_hid.h"
|
||||
#include "usbd_cdc.h"
|
||||
#include "usbd_dfu.h"
|
||||
}
|
||||
|
||||
#include "stm32_hal_ll.h"
|
||||
@@ -53,13 +54,21 @@ extern "C" {
|
||||
#define DEVICE_ID DEVICE_FS
|
||||
#endif
|
||||
|
||||
static bool usbDriverStarted = false;
|
||||
#if defined(BOOT)
|
||||
static usbMode selectedUsbMode = USB_MASS_STORAGE_MODE;
|
||||
#else
|
||||
static usbMode selectedUsbMode = USB_UNSELECTED_MODE;
|
||||
|
||||
// TODO: configured where needed
|
||||
#define DEFAULT_USB_MODE USB_MASS_STORAGE_MODE;
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
static const USBD_DFU_MediaTypeDef* _dfu_media[USBD_DFU_MAX_ITF_NUM] = {nullptr};
|
||||
#endif
|
||||
|
||||
#else
|
||||
#define DEFAULT_USB_MODE USB_UNSELECTED_MODE;
|
||||
#endif
|
||||
|
||||
static bool usbDriverStarted = false;
|
||||
static usbMode selectedUsbMode = DEFAULT_USB_MODE;
|
||||
|
||||
USBD_HandleTypeDef hUsbDevice;
|
||||
|
||||
int getSelectedUsbMode()
|
||||
@@ -114,6 +123,9 @@ extern "C" void OTG_FS_IRQHandler()
|
||||
|
||||
void usbInit()
|
||||
{
|
||||
#if defined(STM32H7)
|
||||
HAL_PWREx_EnableUSBVoltageDetector();
|
||||
#endif
|
||||
gpio_init_af(USB_GPIO_DM, USB_GPIO_AF, GPIO_PIN_SPEED_VERY_HIGH);
|
||||
gpio_init_af(USB_GPIO_DP, USB_GPIO_AF, GPIO_PIN_SPEED_VERY_HIGH);
|
||||
|
||||
@@ -144,12 +156,32 @@ extern void usbInitLUNs();
|
||||
extern USBD_HandleTypeDef hUsbDevice;
|
||||
extern "C" USBD_StorageTypeDef USBD_Storage_Interface_fops;
|
||||
extern USBD_CDC_ItfTypeDef USBD_Interface_fops;
|
||||
extern USBD_DFU_MediaTypeDef USBD_DFU_MEDIA_fops;
|
||||
|
||||
void usbStart()
|
||||
{
|
||||
USBD_Init(&hUsbDevice, &FS_Desc, DEVICE_ID);
|
||||
switch (getSelectedUsbMode()) {
|
||||
#if !defined(BOOT)
|
||||
case USB_MASS_STORAGE_MODE:
|
||||
// initialize USB as MSC device
|
||||
usbInitLUNs();
|
||||
USBD_RegisterClass(&hUsbDevice, &USBD_MSC);
|
||||
USBD_MSC_RegisterStorage(&hUsbDevice, &USBD_Storage_Interface_fops);
|
||||
break;
|
||||
|
||||
#if defined(BOOT)
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
case USB_DFU_MODE:
|
||||
USBD_RegisterClass(&hUsbDevice, &USBD_DFU);
|
||||
for (unsigned i = 0; i < USBD_DFU_MAX_ITF_NUM; i++){
|
||||
if (_dfu_media[i] != nullptr) {
|
||||
USBD_DFU_RegisterMedia(&hUsbDevice,
|
||||
(USBD_DFU_MediaTypeDef*)_dfu_media[i]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
#endif // !defined(STM32H7) && !defined(STM32H7RS)
|
||||
#else
|
||||
case USB_JOYSTICK_MODE:
|
||||
// initialize USB as HID device
|
||||
#if defined(USBJ_EX)
|
||||
@@ -165,13 +197,9 @@ void usbStart()
|
||||
break;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
default:
|
||||
case USB_MASS_STORAGE_MODE:
|
||||
// initialize USB as MSC device
|
||||
usbInitLUNs();
|
||||
USBD_RegisterClass(&hUsbDevice, &USBD_MSC);
|
||||
USBD_MSC_RegisterStorage(&hUsbDevice, &USBD_Storage_Interface_fops);
|
||||
break;
|
||||
return;
|
||||
}
|
||||
|
||||
if (USBD_Start(&hUsbDevice) == USBD_OK) {
|
||||
@@ -191,7 +219,20 @@ bool usbStarted()
|
||||
return usbDriverStarted;
|
||||
}
|
||||
|
||||
#if !defined(BOOT)
|
||||
#if defined(BOOT)
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
int usbRegisterDFUMedia(const void* dfu_media)
|
||||
{
|
||||
for (unsigned i = 0; i < USBD_DFU_MAX_ITF_NUM; i++) {
|
||||
if (_dfu_media[i] == nullptr) {
|
||||
_dfu_media[i] = (const USBD_DFU_MediaTypeDef*)dfu_media;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
#endif // !defined(FIRMWARE_QSPI)
|
||||
#else // !BOOT
|
||||
|
||||
#if defined(USBJ_EX)
|
||||
extern "C" void delay_ms(uint32_t count);
|
||||
|
||||
@@ -107,6 +107,13 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
|
||||
NVIC_EnableIRQ(OTG_FS_IRQn);
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* Disable DP pull-up for 50ms
|
||||
* to force re-enumeration
|
||||
*/
|
||||
HAL_PCD_DevDisconnect(pcdHandle);
|
||||
HAL_Delay(50);
|
||||
HAL_PCD_DevConnect(pcdHandle);
|
||||
}
|
||||
|
||||
void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
|
||||
|
||||
@@ -88,8 +88,12 @@
|
||||
/*---------- -----------*/
|
||||
#define MSC_MEDIA_PACKET MASS_STORAGE_BUFFER_SIZE
|
||||
|
||||
/* Required for DFU media description */
|
||||
#define USBD_SUPPORT_USER_STRING_DESC 1U
|
||||
#define USBD_DFU_XFER_SIZE 4096U
|
||||
|
||||
#if !defined(USBJ_EX)
|
||||
#define HID_IN_PACKET 19
|
||||
#define HID_IN_PACKET 19
|
||||
#endif
|
||||
|
||||
/****************************************/
|
||||
|
||||
@@ -69,7 +69,11 @@
|
||||
|
||||
#define USBD_LANGID_STRING 0x409
|
||||
#define USBD_MANUFACTURER_STRING "OpenTX"
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
#define USB_SIZ_STRING_SERIAL 0x1A
|
||||
#else
|
||||
#define USBD_SERIALNUMBER_FS_STRING "00000000001B"
|
||||
#endif
|
||||
|
||||
#if defined(BOOT)
|
||||
#define USBD_MSC_PRODUCT_FS_STRING USB_NAME " Bootloader"
|
||||
@@ -94,6 +98,14 @@
|
||||
#define USBD_CDC_CONFIGURATION_FS_STRING "VSP Config"
|
||||
#define USBD_CDC_INTERFACE_FS_STRING "VSP Interface"
|
||||
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
#define USBD_DFU_VID USBD_VID_STM
|
||||
#define USBD_DFU_PID 0xDF11
|
||||
#define USBD_DFU_PRODUCT_STRING USB_NAME " DFU"
|
||||
#define USBD_DFU_CONFIGURATION_STRING "DFU Config"
|
||||
#define USBD_DFU_INTERFACE_STRING "DFU Interface"
|
||||
#endif
|
||||
|
||||
/* USER CODE BEGIN PRIVATE_DEFINES */
|
||||
|
||||
/* USER CODE END PRIVATE_DEFINES */
|
||||
@@ -123,10 +135,10 @@
|
||||
* @brief Private functions declaration.
|
||||
* @{
|
||||
*/
|
||||
|
||||
//static void Get_SerialNum(void);
|
||||
//static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
|
||||
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
static void Get_SerialNum(void);
|
||||
static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
|
||||
#endif
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -254,6 +266,16 @@ __ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
|
||||
/* Internal string descriptor. */
|
||||
__ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
|
||||
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
#if defined ( __ICCARM__ ) /* !< IAR Compiler */
|
||||
#pragma data_alignment=4
|
||||
#endif
|
||||
__ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
|
||||
USB_SIZ_STRING_SERIAL,
|
||||
USB_DESC_TYPE_STRING,
|
||||
};
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -290,7 +312,12 @@ uint8_t * USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
vid = USBD_MSC_VID;
|
||||
pid = USBD_MSC_PID;
|
||||
break;
|
||||
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
case USB_DFU_MODE:
|
||||
vid = USBD_DFU_VID;
|
||||
pid = USBD_DFU_PID;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
vid = 0;
|
||||
pid = 0;
|
||||
@@ -360,6 +387,11 @@ uint8_t * USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length
|
||||
case USB_MASS_STORAGE_MODE:
|
||||
USBD_GetString ((uint8_t*)USBD_MSC_PRODUCT_FS_STRING, USBD_StrDesc, length);
|
||||
break;
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
case USB_DFU_MODE:
|
||||
USBD_GetString ((uint8_t*)USBD_DFU_PRODUCT_STRING, USBD_StrDesc, length);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
return USBD_StrDesc;
|
||||
}
|
||||
@@ -385,9 +417,18 @@ uint8_t * USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *l
|
||||
*/
|
||||
uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
*length = USB_SIZ_STRING_SERIAL;
|
||||
|
||||
/* Update the serial number string descriptor with the data from the unique
|
||||
* ID */
|
||||
Get_SerialNum();
|
||||
|
||||
return (uint8_t *)USBD_StringSerial;
|
||||
#else
|
||||
USBD_GetString ((uint8_t*)USBD_SERIALNUMBER_FS_STRING, USBD_StrDesc, length);
|
||||
return USBD_StrDesc;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -408,6 +449,11 @@ uint8_t * USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
case USB_MASS_STORAGE_MODE:
|
||||
USBD_GetString ((uint8_t*)USBD_MSC_CONFIGURATION_FS_STRING, USBD_StrDesc, length);
|
||||
break;
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
case USB_DFU_MODE:
|
||||
USBD_GetString ((uint8_t*)USBD_DFU_CONFIGURATION_STRING, USBD_StrDesc, length);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
return USBD_StrDesc;
|
||||
}
|
||||
@@ -430,6 +476,11 @@ uint8_t * USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *leng
|
||||
case USB_MASS_STORAGE_MODE:
|
||||
USBD_GetString ((uint8_t*)USBD_MSC_INTERFACE_FS_STRING, USBD_StrDesc, length);
|
||||
break;
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
case USB_DFU_MODE:
|
||||
USBD_GetString ((uint8_t*)USBD_DFU_INTERFACE_STRING, USBD_StrDesc, length);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
return USBD_StrDesc;
|
||||
}
|
||||
@@ -449,29 +500,30 @@ uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
}
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
#if defined(FIRMWARE_QSPI)
|
||||
/**
|
||||
* @brief Create the serial number string descriptor
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
//static void Get_SerialNum(void)
|
||||
//{
|
||||
// uint32_t deviceserial0;
|
||||
// uint32_t deviceserial1;
|
||||
// uint32_t deviceserial2;
|
||||
//
|
||||
// deviceserial0 = *(uint32_t *) DEVICE_ID1;
|
||||
// deviceserial1 = *(uint32_t *) DEVICE_ID2;
|
||||
// deviceserial2 = *(uint32_t *) DEVICE_ID3;
|
||||
//
|
||||
// deviceserial0 += deviceserial2;
|
||||
//
|
||||
// if (deviceserial0 != 0)
|
||||
// {
|
||||
// IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
|
||||
// IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
|
||||
// }
|
||||
//}
|
||||
static void Get_SerialNum(void)
|
||||
{
|
||||
uint32_t deviceserial0;
|
||||
uint32_t deviceserial1;
|
||||
uint32_t deviceserial2;
|
||||
|
||||
deviceserial0 = *(uint32_t *) DEVICE_ID1;
|
||||
deviceserial1 = *(uint32_t *) DEVICE_ID2;
|
||||
deviceserial2 = *(uint32_t *) DEVICE_ID3;
|
||||
|
||||
deviceserial0 += deviceserial2;
|
||||
|
||||
if (deviceserial0 != 0)
|
||||
{
|
||||
IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
|
||||
IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert Hex 32Bits value into char
|
||||
@@ -480,26 +532,27 @@ uint8_t * USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
* @param len: buffer length
|
||||
* @retval None
|
||||
*/
|
||||
//static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
|
||||
//{
|
||||
// uint8_t idx = 0;
|
||||
//
|
||||
// for (idx = 0; idx < len; idx++)
|
||||
// {
|
||||
// if (((value >> 28)) < 0xA)
|
||||
// {
|
||||
// pbuf[2 * idx] = (value >> 28) + '0';
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// pbuf[2 * idx] = (value >> 28) + 'A' - 10;
|
||||
// }
|
||||
//
|
||||
// value = value << 4;
|
||||
//
|
||||
// pbuf[2 * idx + 1] = 0;
|
||||
// }
|
||||
//}
|
||||
static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
|
||||
{
|
||||
uint8_t idx = 0;
|
||||
|
||||
for (idx = 0; idx < len; idx++)
|
||||
{
|
||||
if (((value >> 28)) < 0xA)
|
||||
{
|
||||
pbuf[2 * idx] = (value >> 28) + '0';
|
||||
}
|
||||
else
|
||||
{
|
||||
pbuf[2 * idx] = (value >> 28) + 'A' - 10;
|
||||
}
|
||||
|
||||
value = value << 4;
|
||||
|
||||
pbuf[2 * idx + 1] = 0;
|
||||
}
|
||||
}
|
||||
#endif // FIRMWARE_QSPI
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
@@ -46,91 +46,101 @@
|
||||
* @brief Constants.
|
||||
* @{
|
||||
*/
|
||||
#define DEVICE_ID1 (UID_BASE)
|
||||
#define DEVICE_ID2 (UID_BASE + 0x4)
|
||||
#define DEVICE_ID3 (UID_BASE + 0x8)
|
||||
#if defined(STM32H7)
|
||||
#define DEVICE_ID1 (0x5C001000)
|
||||
#define DEVICE_ID2 (0x5C001004)
|
||||
#define DEVICE_ID3 (0x5C001008)
|
||||
#else
|
||||
#define DEVICE_ID1 (UID_BASE)
|
||||
#define DEVICE_ID2 (UID_BASE + 0x4)
|
||||
#define DEVICE_ID3 (UID_BASE + 0x8)
|
||||
#endif
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_CONSTANTS */
|
||||
#define USB_SIZ_STRING_SERIAL 0x1A
|
||||
|
||||
/* USER CODE END EXPORTED_CONSTANTS */
|
||||
/* USER CODE BEGIN EXPORTED_CONSTANTS */
|
||||
|
||||
/**
|
||||
/* USER CODE END EXPORTED_CONSTANTS */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_Defines USBD_DESC_Exported_Defines
|
||||
/** @defgroup USBD_DESC_Exported_Defines USBD_DESC_Exported_Defines
|
||||
* @brief Defines.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_DEFINES */
|
||||
/* USER CODE BEGIN EXPORTED_DEFINES */
|
||||
|
||||
/* USER CODE END EXPORTED_DEFINES */
|
||||
/* USER CODE END EXPORTED_DEFINES */
|
||||
|
||||
/**
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_TypesDefinitions USBD_DESC_Exported_TypesDefinitions
|
||||
/** @defgroup USBD_DESC_Exported_TypesDefinitions
|
||||
* USBD_DESC_Exported_TypesDefinitions
|
||||
* @brief Types.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_TYPES */
|
||||
/* USER CODE BEGIN EXPORTED_TYPES */
|
||||
|
||||
/* USER CODE END EXPORTED_TYPES */
|
||||
/* USER CODE END EXPORTED_TYPES */
|
||||
|
||||
/**
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_Macros USBD_DESC_Exported_Macros
|
||||
/** @defgroup USBD_DESC_Exported_Macros USBD_DESC_Exported_Macros
|
||||
* @brief Aliases.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_MACRO */
|
||||
/* USER CODE BEGIN EXPORTED_MACRO */
|
||||
|
||||
/* USER CODE END EXPORTED_MACRO */
|
||||
/* USER CODE END EXPORTED_MACRO */
|
||||
|
||||
/**
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_Variables USBD_DESC_Exported_Variables
|
||||
/** @defgroup USBD_DESC_Exported_Variables USBD_DESC_Exported_Variables
|
||||
* @brief Public variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** Descriptor for the Usb device. */
|
||||
extern USBD_DescriptorsTypeDef FS_Desc;
|
||||
/** Descriptor for the Usb device. */
|
||||
extern USBD_DescriptorsTypeDef FS_Desc;
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_VARIABLES */
|
||||
/* USER CODE BEGIN EXPORTED_VARIABLES */
|
||||
|
||||
/* USER CODE END EXPORTED_VARIABLES */
|
||||
/* USER CODE END EXPORTED_VARIABLES */
|
||||
|
||||
/**
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_FunctionsPrototype USBD_DESC_Exported_FunctionsPrototype
|
||||
/** @defgroup USBD_DESC_Exported_FunctionsPrototype
|
||||
* USBD_DESC_Exported_FunctionsPrototype
|
||||
* @brief Public functions declaration.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_FUNCTIONS */
|
||||
/* USER CODE BEGIN EXPORTED_FUNCTIONS */
|
||||
|
||||
/* USER CODE END EXPORTED_FUNCTIONS */
|
||||
/* USER CODE END EXPORTED_FUNCTIONS */
|
||||
|
||||
/**
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
@@ -19,5 +19,10 @@
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#define MASS_STORAGE_BUFFER_SIZE 512U
|
||||
#if defined(BOOT)
|
||||
#define MASS_STORAGE_BUFFER_SIZE 4096U
|
||||
#else
|
||||
#define MASS_STORAGE_BUFFER_SIZE 512U
|
||||
#endif
|
||||
|
||||
#define USBD_CLASS_BOS_ENABLED 0
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
|
||||
#include "hal/fatfs_diskio.h"
|
||||
#include "hal/storage.h"
|
||||
|
||||
#include "stm32_hal.h"
|
||||
#include "stm32_hal_ll.h"
|
||||
|
||||
@@ -38,7 +39,6 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#if FF_MAX_SS != FF_MIN_SS
|
||||
#error "Variable sector size is not supported"
|
||||
#endif
|
||||
@@ -52,21 +52,30 @@
|
||||
#define WATCHDOG_SUSPEND(...)
|
||||
#endif
|
||||
|
||||
#if !defined(SIMU)
|
||||
extern "C" {
|
||||
|
||||
#include "usb_conf.h"
|
||||
|
||||
#if defined(FIRMWARE_FORMAT_UF2) && defined(BOOT)
|
||||
#define USE_UF2_DRIVE 1
|
||||
#else
|
||||
#define USE_UF2_DRIVE 0
|
||||
#endif
|
||||
|
||||
#if USE_UF2_DRIVE
|
||||
#include "drivers/uf2_ghostfat.h"
|
||||
#endif
|
||||
|
||||
enum MassstorageLuns {
|
||||
STORAGE_SDCARD_LUN,
|
||||
#if defined(FWDRIVE)
|
||||
STORAGE_EEPROM_LUN,
|
||||
|
||||
#if USE_UF2_DRIVE
|
||||
STORAGE_UF2_LUN,
|
||||
#endif
|
||||
|
||||
STORAGE_LUN_NBR
|
||||
};
|
||||
|
||||
/** USB Mass storage Standard Inquiry Data. */
|
||||
const uint8_t STORAGE_Inquirydata_FS[] = {/* 36 */
|
||||
const uint8_t STORAGE_Inquirydata[] = {/* 36 */
|
||||
/* LUN 0 */
|
||||
0x00,
|
||||
0x80,
|
||||
@@ -80,13 +89,14 @@ const uint8_t STORAGE_Inquirydata_FS[] = {/* 36 */
|
||||
USB_PRODUCT, /* Product : 16 Bytes */
|
||||
'R', 'a', 'd', 'i', 'o', ' ', ' ', ' ',
|
||||
'1', '.', '0', '0', /* Version : 4 Bytes */
|
||||
#if defined(FWDRIVE)
|
||||
|
||||
#if USE_UF2_DRIVE
|
||||
/* LUN 1 */
|
||||
0x00,
|
||||
0x80,
|
||||
0x02,
|
||||
0x02,
|
||||
(USBD_STD_INQUIRY_LENGTH - 5),
|
||||
(STANDARD_INQUIRY_DATA_LEN - 5),
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
@@ -97,47 +107,39 @@ const uint8_t STORAGE_Inquirydata_FS[] = {/* 36 */
|
||||
#endif
|
||||
};
|
||||
|
||||
#if defined(FWDRIVE)
|
||||
#define RESERVED_SECTORS (1 /*Boot*/ + 2 /*Fat table */ + 1 /*Root dir*/ + 8 /* one cluster for firmware.txt */)
|
||||
|
||||
int32_t fat12Write(const uint8_t * buffer, uint16_t sector, uint16_t count);
|
||||
int32_t fat12Read(uint8_t * buffer, uint16_t sector, uint16_t count );
|
||||
#endif
|
||||
|
||||
int8_t STORAGE_Init (uint8_t lun);
|
||||
|
||||
static int8_t STORAGE_Init_FS(uint8_t lun);
|
||||
static int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
|
||||
|
||||
static int8_t STORAGE_IsReady_FS(uint8_t lun);
|
||||
|
||||
static int8_t STORAGE_IsWriteProtected_FS(uint8_t lun);
|
||||
static int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
|
||||
static int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
|
||||
static int8_t STORAGE_GetMaxLun_FS(void);
|
||||
static int8_t STORAGE_Init(uint8_t lun);
|
||||
static int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
|
||||
static int8_t STORAGE_IsReady(uint8_t lun);
|
||||
static int8_t STORAGE_IsWriteProtected(uint8_t lun);
|
||||
static int8_t STORAGE_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
|
||||
static int8_t STORAGE_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
|
||||
static int8_t STORAGE_GetMaxLun(void);
|
||||
|
||||
/* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
|
||||
|
||||
|
||||
USBD_StorageTypeDef USBD_Storage_Interface_fops =
|
||||
{
|
||||
STORAGE_Init_FS,
|
||||
STORAGE_GetCapacity_FS,
|
||||
STORAGE_IsReady_FS,
|
||||
STORAGE_IsWriteProtected_FS,
|
||||
STORAGE_Read_FS,
|
||||
STORAGE_Write_FS,
|
||||
STORAGE_GetMaxLun_FS,
|
||||
(int8_t *)STORAGE_Inquirydata_FS
|
||||
STORAGE_Init,
|
||||
STORAGE_GetCapacity,
|
||||
STORAGE_IsReady,
|
||||
STORAGE_IsWriteProtected,
|
||||
STORAGE_Read,
|
||||
STORAGE_Write,
|
||||
STORAGE_GetMaxLun,
|
||||
(int8_t *)STORAGE_Inquirydata
|
||||
};
|
||||
|
||||
}
|
||||
int8_t STORAGE_Init(uint8_t lun)
|
||||
{
|
||||
#if USE_UF2_DRIVE
|
||||
if (lun == STORAGE_UF2_LUN) {
|
||||
uf2_fat_reset_state();
|
||||
return USBD_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
int8_t STORAGE_Init_FS(uint8_t lun)
|
||||
{
|
||||
// TODO: call generic storage init
|
||||
return (USBD_OK);
|
||||
disk_initialize(0);
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,13 +149,15 @@ int8_t STORAGE_Init_FS(uint8_t lun)
|
||||
* @param block_size : size of a physical block
|
||||
* @retval Status
|
||||
*/
|
||||
int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
|
||||
int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
|
||||
{
|
||||
#if defined(FWDRIVE)
|
||||
if (lun == STORAGE_EEPROM_LUN) {
|
||||
if (lun >= STORAGE_LUN_NBR) return USBD_FAIL;
|
||||
|
||||
#if USE_UF2_DRIVE
|
||||
if (lun == STORAGE_UF2_LUN) {
|
||||
*block_size = BLOCK_SIZE;
|
||||
*block_num = RESERVED_SECTORS + FLASHSIZE/BLOCK_SIZE;
|
||||
return 0;
|
||||
*block_num = UF2_NUM_BLOCKS;
|
||||
return USBD_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -172,17 +176,16 @@ int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_
|
||||
}
|
||||
|
||||
*block_num = sector_count;
|
||||
return (USBD_OK);
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
uint8_t lunReady[STORAGE_LUN_NBR];
|
||||
|
||||
void usbInitLUNs()
|
||||
{
|
||||
lunReady[STORAGE_SDCARD_LUN] = 1;
|
||||
#if defined(FWDRIVE)
|
||||
lunReady[STORAGE_EEPROM_LUN] = 1;
|
||||
#endif
|
||||
for (int i = 0; i < STORAGE_LUN_NBR; i++) {
|
||||
lunReady[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -190,9 +193,22 @@ void usbInitLUNs()
|
||||
* @param lun : logical unit number
|
||||
* @retval Status
|
||||
*/
|
||||
int8_t STORAGE_IsReady_FS(uint8_t lun)
|
||||
int8_t STORAGE_IsReady(uint8_t lun)
|
||||
{
|
||||
return (lunReady[STORAGE_SDCARD_LUN] != 0 && storageIsPresent()) ? USBD_OK : USBD_FAIL;
|
||||
switch (lun) {
|
||||
case STORAGE_SDCARD_LUN:
|
||||
return (lunReady[STORAGE_SDCARD_LUN] != 0 && storageIsPresent())
|
||||
? USBD_OK
|
||||
: USBD_FAIL;
|
||||
|
||||
#if USE_UF2_DRIVE
|
||||
case STORAGE_UF2_LUN:
|
||||
return USBD_OK;
|
||||
#endif
|
||||
|
||||
default:
|
||||
return USBD_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -200,7 +216,7 @@ int8_t STORAGE_IsReady_FS(uint8_t lun)
|
||||
* @param lun : logical unit number
|
||||
* @retval Status
|
||||
*/
|
||||
int8_t STORAGE_IsWriteProtected_FS(uint8_t lun)
|
||||
int8_t STORAGE_IsWriteProtected(uint8_t lun)
|
||||
{
|
||||
return (USBD_OK);
|
||||
}
|
||||
@@ -214,16 +230,17 @@ int8_t STORAGE_IsWriteProtected_FS(uint8_t lun)
|
||||
* @retval Status
|
||||
*/
|
||||
|
||||
int8_t STORAGE_Read_FS (uint8_t lun,
|
||||
uint8_t *buf,
|
||||
uint32_t blk_addr,
|
||||
uint16_t blk_len)
|
||||
int8_t STORAGE_Read (uint8_t lun,
|
||||
uint8_t *buf,
|
||||
uint32_t blk_addr,
|
||||
uint16_t blk_len)
|
||||
{
|
||||
WATCHDOG_SUSPEND(100/*1s*/);
|
||||
|
||||
#if defined(FWDRIVE)
|
||||
if (lun == STORAGE_EEPROM_LUN) {
|
||||
return (fat12Read(buf, blk_addr, blk_len) == 0) ? 0 : -1;
|
||||
#if USE_UF2_DRIVE
|
||||
if (lun == STORAGE_UF2_LUN) {
|
||||
uf2_fat_read_block(blk_addr, buf);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -238,13 +255,19 @@ int8_t STORAGE_Read_FS (uint8_t lun,
|
||||
* @param blk_len : nmber of blocks to be read
|
||||
* @retval Status
|
||||
*/
|
||||
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
|
||||
int8_t STORAGE_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
|
||||
{
|
||||
WATCHDOG_SUSPEND(500/*5s*/);
|
||||
|
||||
#if defined(FWDRIVE)
|
||||
if (lun == STORAGE_EEPROM_LUN) {
|
||||
return (fat12Write(buf, blk_addr, blk_len) == 0) ? 0 : -1;
|
||||
#if USE_UF2_DRIVE
|
||||
if (lun == STORAGE_UF2_LUN) {
|
||||
int wr_ret;
|
||||
while ((blk_len--) && (wr_ret = uf2_fat_write_block(blk_addr, buf)) > 0) {
|
||||
blk_addr += 512;
|
||||
buf += 512;
|
||||
}
|
||||
// return wr_ret > 0 ? USBD_OK : USBD_FAIL;
|
||||
return USBD_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -258,308 +281,7 @@ int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t b
|
||||
* @retval number of logical unit
|
||||
*/
|
||||
|
||||
int8_t STORAGE_GetMaxLun_FS(void)
|
||||
int8_t STORAGE_GetMaxLun(void)
|
||||
{
|
||||
return (STORAGE_LUN_NBR - 1);
|
||||
}
|
||||
|
||||
#if defined(FWDRIVE)
|
||||
/* Firmware.txt */
|
||||
const char firmware_txt[] =
|
||||
#if defined(BOOT)
|
||||
"EdgeTX Bootloader"
|
||||
#else
|
||||
"EdgeTX Firmware"
|
||||
#endif
|
||||
" for " FLAVOUR "\r\n\r\n"
|
||||
#if defined(BOOT)
|
||||
"BOOTVER "
|
||||
#else
|
||||
"FWVERSION "
|
||||
#endif
|
||||
"edgetx-" FLAVOUR "-" VERSION " (" GIT_STR ")\r\n"
|
||||
"DATE " DATE "\r\n"
|
||||
"TIME " TIME "\r\n"
|
||||
#if !defined(BOOT)
|
||||
"BOOTVER "
|
||||
#else
|
||||
"FWVERSION "
|
||||
#endif
|
||||
;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/**
|
||||
* FAT12 boot sector partition.
|
||||
*/
|
||||
#define TOTALSECTORS (RESERVED_SECTORS + (FLASHSIZE/BLOCK_SIZE))
|
||||
const char g_FATboot[BLOCK_SIZE] =
|
||||
{
|
||||
0xeb, 0x3c, 0x90, // Jump instruction.
|
||||
'O', 'p', 'e', 'n', 'T', 'x', 0x00, 0x00, // OEM Name
|
||||
0x00, 0x02, // Bytes per sector
|
||||
0x08, // Sectors per FS cluster.
|
||||
0x01, 0x00, // Reserved sector count
|
||||
|
||||
0x01, // Number of FATs
|
||||
0x10, 0x00, // Number of root directory entries
|
||||
TOTALSECTORS & 0x00ff, (TOTALSECTORS & 0xff00) >> 8, // Total sectors
|
||||
0xf8, // Media descriptor
|
||||
0x02, 0x00, // Sectors per FAT table
|
||||
0x20, 0x00, // Sectors per track
|
||||
0x40, 0x00, // Number of heads
|
||||
0x00, 0x00, 0x00, 0x00, // Number of hidden sectors
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, // Large number of sectors.
|
||||
0x00, // Physical drive number
|
||||
0x00, // Reserved
|
||||
0x29, // Extended boot signature
|
||||
'O', 'T', 'x', 0xD1, // Disk ID (serial number)
|
||||
'V', 'I', 'R', 'T', 'F', 'S', ' ', ' ', ' ', ' ', ' ', // Volume label
|
||||
'F', 'A', 'T', '1', '2', ' ', ' ', ' ', // FAT file system type
|
||||
0x00, 0x00, // OS boot code
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa
|
||||
};
|
||||
|
||||
|
||||
// File Attributes
|
||||
// 0 0x01 Read Only.
|
||||
// 1 0x02 Hidden.
|
||||
// 2 0x04 System.
|
||||
// 3 0x08 Volume Label.
|
||||
// 4 0x10 Subdirectory.
|
||||
// 5 0x20 Archive.
|
||||
// 6 0x40 Device.
|
||||
// 7 0x80 Reserved.
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t name[8];
|
||||
uint8_t ext[3];
|
||||
uint8_t attribute;
|
||||
uint8_t reserved;
|
||||
uint8_t create_time_ms;
|
||||
uint16_t create_time;
|
||||
uint16_t create_date;
|
||||
uint16_t access_date;
|
||||
uint16_t ea_index;
|
||||
uint16_t modify_time;
|
||||
uint16_t modify_date;
|
||||
uint16_t start_cluster;
|
||||
uint32_t file_size;
|
||||
} FATDirEntry_t;
|
||||
|
||||
// First 16 FAT root directory entries (1 sector)
|
||||
const FATDirEntry_t g_DIRroot[] =
|
||||
{
|
||||
{
|
||||
{ USB_PRODUCT },
|
||||
{ ' ', ' ', ' '},
|
||||
0x08, // Volume
|
||||
0x00,
|
||||
0x00,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x0000,
|
||||
0x00000000
|
||||
},
|
||||
{
|
||||
{ 'F', 'I', 'R', 'M', 'W', 'A', 'R', 'E'},
|
||||
{ 'T', 'X', 'T'},
|
||||
0x21, // Readonly+Archive
|
||||
0x00,
|
||||
0x3E,
|
||||
0xA301,
|
||||
0x3D55,
|
||||
0x3D55,
|
||||
0x0000,
|
||||
0xA302,
|
||||
0x3D55,
|
||||
0x0002,
|
||||
sizeof(firmware_txt) + strlen(getFirmwareVersion())
|
||||
},
|
||||
{
|
||||
{ 'F', 'I', 'R', 'M', 'W', 'A', 'R', 'E'},
|
||||
{ 'B', 'I', 'N'},
|
||||
#if defined(BOOT)
|
||||
0x20, // Archive
|
||||
#else
|
||||
0x21, // Readonly+Archive
|
||||
#endif
|
||||
0x00,
|
||||
0x3E,
|
||||
0xA301,
|
||||
0x3D55,
|
||||
0x3D55,
|
||||
0x0000,
|
||||
0xA302,
|
||||
0x3D55,
|
||||
0x0003,
|
||||
FLASHSIZE
|
||||
},
|
||||
// Emty entries are 0x00, omitted here. Up to 16 entries can be defined here
|
||||
};
|
||||
|
||||
static void writeByte(uint8_t *buffer, uint16_t sector, int byte, uint8_t value)
|
||||
{
|
||||
if (byte >= sector* BLOCK_SIZE && byte < (sector+1)* BLOCK_SIZE)
|
||||
buffer[byte - sector*BLOCK_SIZE] = value;
|
||||
}
|
||||
|
||||
static void pushCluster(uint8_t *buffer, uint16_t sector, uint16_t & cluster, int & rest, uint16_t value)
|
||||
{
|
||||
// boot sector is in front of FAT
|
||||
sector= sector-1;
|
||||
|
||||
// First byte of the cluster
|
||||
int startbyte = cluster *3/2;
|
||||
if (cluster % 2 == 0) {
|
||||
// First 12 bit half
|
||||
rest = value >> 8;
|
||||
writeByte(buffer, sector, startbyte, value & 0xff);
|
||||
} else {
|
||||
// second 12 bit half, write rest and next byte
|
||||
writeByte(buffer, sector, startbyte, value << 4 | rest );
|
||||
writeByte(buffer, sector, startbyte+1, (value >> 4) & 0xff);
|
||||
}
|
||||
cluster++;
|
||||
}
|
||||
|
||||
// count is number of 512 byte sectors
|
||||
int32_t fat12Read(uint8_t * buffer, uint16_t sector, uint16_t count)
|
||||
{
|
||||
while(count) {
|
||||
memset(buffer, 0x00, BLOCK_SIZE);
|
||||
if (sector == 0) {
|
||||
memcpy(buffer, g_FATboot, sizeof(g_FATboot) ) ;
|
||||
}
|
||||
else if (sector == 1 || sector == 2) {
|
||||
// FAT table. Generate on the fly to save the 1024 byte flash space
|
||||
uint16_t cluster=0;
|
||||
int rest;
|
||||
pushCluster (buffer, sector, cluster, rest, (uint16_t) 0xFF8);
|
||||
pushCluster (buffer, sector, cluster, rest, (uint16_t) 0xFFF);
|
||||
|
||||
// Entry for firmware.txt, exactly one cluster
|
||||
pushCluster (buffer, sector, cluster, rest, (uint16_t) 0xFFF);
|
||||
|
||||
// Entry for firmware.bin
|
||||
for (int i=0;i<FLASHSIZE/BLOCK_SIZE/8 -1;i++)
|
||||
pushCluster (buffer, sector, cluster, rest, cluster+1);
|
||||
pushCluster (buffer, sector, cluster, rest, (uint16_t) 0xFFF);
|
||||
|
||||
// Ensure last cluster is written if it is the first half
|
||||
pushCluster (buffer, sector, cluster, rest, (uint16_t) 0x000);
|
||||
|
||||
// Rest is 0x0 as per memset
|
||||
}
|
||||
else if (sector == 3) {
|
||||
memcpy(buffer, g_DIRroot, sizeof(g_DIRroot) ) ;
|
||||
}
|
||||
else if (sector == 4) {
|
||||
memcpy(buffer, firmware_txt, sizeof(firmware_txt));
|
||||
strcpy((char *)(buffer + sizeof(firmware_txt)), getFirmwareVersion());
|
||||
}
|
||||
else if (sector < RESERVED_SECTORS) {
|
||||
// allocated to firmware.txt
|
||||
}
|
||||
else if (sector < RESERVED_SECTORS + (FLASHSIZE/BLOCK_SIZE )) {
|
||||
uint32_t address;
|
||||
address = sector - RESERVED_SECTORS;
|
||||
address *= BLOCK_SIZE;
|
||||
address += FIRMWARE_ADDRESS;
|
||||
memcpy(buffer, (uint8_t *)address, BLOCK_SIZE);
|
||||
}
|
||||
buffer += BLOCK_SIZE ;
|
||||
sector++ ;
|
||||
count-- ;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int32_t fat12Write(const uint8_t * buffer, uint16_t sector, uint16_t count)
|
||||
{
|
||||
enum FatWriteOperation {
|
||||
FATWRITE_NONE,
|
||||
FATWRITE_EEPROM,
|
||||
FATWRITE_FIRMWARE
|
||||
};
|
||||
|
||||
// Silence compiler warning that this is not used on X10
|
||||
__attribute__((unused)) static uint8_t operation = FATWRITE_NONE;
|
||||
|
||||
TRACE("FAT12 Write(sector=%d, count=%d)", sector, count);
|
||||
|
||||
if (sector < RESERVED_SECTORS) {
|
||||
// reserved, read-only
|
||||
}
|
||||
else if (sector < RESERVED_SECTORS + (FLASHSIZE/BLOCK_SIZE)) {
|
||||
#if !defined(BOOT) // Don't allow overwrite of running firmware
|
||||
return -1;
|
||||
#else
|
||||
// firmware
|
||||
uint32_t address;
|
||||
address = sector - RESERVED_SECTORS;
|
||||
address *= BLOCK_SIZE;
|
||||
address += FIRMWARE_ADDRESS;
|
||||
while (count) {
|
||||
for (uint32_t i=0; i<BLOCK_SIZE/FLASH_PAGESIZE; i++) {
|
||||
if (address >= FIRMWARE_ADDRESS+BOOTLOADER_SIZE/*protect bootloader*/ && address <= FIRMWARE_ADDRESS+FLASHSIZE-FLASH_PAGESIZE) {
|
||||
if (address == FIRMWARE_ADDRESS+BOOTLOADER_SIZE && isFirmwareStart(buffer)) {
|
||||
TRACE("FIRMWARE start found in sector %d", sector);
|
||||
operation = FATWRITE_FIRMWARE;
|
||||
}
|
||||
if (operation == FATWRITE_FIRMWARE) {
|
||||
flashWrite((uint32_t *)address, (uint32_t *)buffer);
|
||||
}
|
||||
}
|
||||
address += FLASH_PAGESIZE;
|
||||
buffer += FLASH_PAGESIZE;
|
||||
}
|
||||
sector++;
|
||||
count--;
|
||||
if (sector-RESERVED_SECTORS >= (FLASHSIZE/BLOCK_SIZE)) {
|
||||
TRACE("FIRMWARE end written at sector %d", sector-1);
|
||||
operation = FATWRITE_NONE;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
return 0 ;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -19,16 +19,18 @@
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "vs1053b.h"
|
||||
|
||||
#include "stm32_hal_ll.h"
|
||||
#include "stm32_gpio_driver.h"
|
||||
#include "stm32_gpio.h"
|
||||
#include "stm32_spi.h"
|
||||
|
||||
#include "hal/gpio.h"
|
||||
#include "hal/audio_driver.h"
|
||||
|
||||
#include "board.h"
|
||||
#include "edgetx.h"
|
||||
#include "timers_driver.h"
|
||||
|
||||
#define VS_WRITE_COMMAND 0x02
|
||||
#define VS_READ_COMMAND 0x03
|
||||
@@ -66,101 +68,80 @@
|
||||
#define SM_LINE1 0x4000
|
||||
#define SM_CLK_RANGE 0x8000
|
||||
|
||||
#define SPI_SPEED_2 0
|
||||
#define SPI_SPEED_4 1
|
||||
#define SPI_SPEED_8 2
|
||||
#define SPI_SPEED_16 3
|
||||
#define SPI_SPEED_32 4
|
||||
#define SPI_SPEED_64 5
|
||||
#define SPI_SPEED_128 6
|
||||
#define SPI_SPEED_256 7
|
||||
#define SPI_LOW_SPEED 1755000
|
||||
#define SPI_HIGH_SPEED 9216000
|
||||
|
||||
#define MP3_BUFFER_SIZE 32
|
||||
#define VS1053_BUFFER_SIZE 32
|
||||
|
||||
#define CS_HIGH() stm32_spi_unselect(&_audio_spi)
|
||||
#define CS_LOW() stm32_spi_select(&_audio_spi)
|
||||
#define XDCS_HIGH() do { AUDIO_XDCS_GPIO->BSRR = AUDIO_XDCS_GPIO_PIN; } while (0)
|
||||
#define XDCS_LOW() do { AUDIO_XDCS_GPIO->BSRR = AUDIO_XDCS_GPIO_PIN << 16; } while (0)
|
||||
#define RST_HIGH() do { AUDIO_RST_GPIO->BSRR = AUDIO_RST_GPIO_PIN; } while (0)
|
||||
#define RST_LOW() do { AUDIO_RST_GPIO->BSRR = AUDIO_RST_GPIO_PIN << 16; } while (0)
|
||||
#define XDCS_HIGH() _xdcs_high()
|
||||
#define XDCS_LOW() _xdcs_low()
|
||||
#define READ_DREQ() _read_dreq()
|
||||
|
||||
#define READ_DREQ() (LL_GPIO_IsInputPinSet(AUDIO_DREQ_GPIO, AUDIO_DREQ_GPIO_PIN))
|
||||
static const vs1053b_t* _instance = nullptr;
|
||||
|
||||
static const stm32_spi_t _audio_spi = {
|
||||
.SPIx = AUDIO_SPI,
|
||||
.SPI_GPIOx = AUDIO_SPI_SCK_GPIO,
|
||||
.SPI_Pins = AUDIO_SPI_SCK_GPIO_PIN | AUDIO_SPI_MISO_GPIO_PIN | AUDIO_SPI_MOSI_GPIO_PIN,
|
||||
.CS_GPIOx = AUDIO_CS_GPIO,
|
||||
.CS_Pin = AUDIO_CS_GPIO_PIN,
|
||||
};
|
||||
// mute support
|
||||
static bool _is_muted = false;
|
||||
static uint32_t _last_play_ts = 0;
|
||||
|
||||
static void audioSpiInit(void)
|
||||
static uint8_t* _audio_buffer = nullptr;
|
||||
static uint32_t _audio_buffer_len = 0;
|
||||
|
||||
// volume is set asynchronously:
|
||||
// - first saved in _async_volume
|
||||
// - then vs1053b_update_volume() is called periodically
|
||||
// to actually send the new setting to the device.
|
||||
static int16_t _async_volume = -1;
|
||||
|
||||
static inline void _xdcs_high() { gpio_set(_instance->XDCS); }
|
||||
static inline void _xdcs_low() { gpio_clear(_instance->XDCS); }
|
||||
static inline uint32_t _read_dreq() { return gpio_read(_instance->DREQ); }
|
||||
|
||||
static inline void _reset_high()
|
||||
{
|
||||
stm32_gpio_enable_clock(AUDIO_XDCS_GPIO);
|
||||
stm32_gpio_enable_clock(AUDIO_RST_GPIO);
|
||||
stm32_gpio_enable_clock(AUDIO_DREQ_GPIO);
|
||||
|
||||
LL_GPIO_InitTypeDef pinInit;
|
||||
LL_GPIO_StructInit(&pinInit);
|
||||
|
||||
pinInit.Mode = LL_GPIO_MODE_OUTPUT;
|
||||
pinInit.Pin = AUDIO_XDCS_GPIO_PIN;
|
||||
LL_GPIO_Init(AUDIO_XDCS_GPIO, &pinInit);
|
||||
|
||||
pinInit.Pin = AUDIO_RST_GPIO_PIN;
|
||||
LL_GPIO_Init(AUDIO_RST_GPIO, &pinInit);
|
||||
|
||||
pinInit.Pin = AUDIO_DREQ_GPIO_PIN;
|
||||
pinInit.Mode = LL_GPIO_MODE_INPUT;
|
||||
LL_GPIO_Init(AUDIO_DREQ_GPIO, &pinInit);
|
||||
|
||||
stm32_spi_init(&_audio_spi, LL_SPI_DATAWIDTH_8BIT);
|
||||
}
|
||||
|
||||
// static void audioWaitReady()
|
||||
// {
|
||||
// // The audio amp needs ~2s to start
|
||||
// RTOS_WAIT_MS(2000); // 2s
|
||||
// }
|
||||
|
||||
static void audioSpiSetSpeed(uint8_t speed)
|
||||
{
|
||||
AUDIO_SPI->CR1 &= 0xFFC7; // Fsck=Fcpu/256
|
||||
switch (speed) {
|
||||
case SPI_SPEED_2:
|
||||
AUDIO_SPI->CR1 |= 0x00 << 3; // Fsck=Fpclk/2=36Mhz
|
||||
break;
|
||||
case SPI_SPEED_4:
|
||||
AUDIO_SPI->CR1 |= 0x01 << 3; // Fsck=Fpclk/4=18Mhz
|
||||
break;
|
||||
case SPI_SPEED_8:
|
||||
AUDIO_SPI->CR1 |= 0x02 << 3; // Fsck=Fpclk/8=9Mhz
|
||||
break;
|
||||
case SPI_SPEED_16:
|
||||
AUDIO_SPI->CR1 |= 0x03 << 3; // Fsck=Fpclk/16=4.5Mhz
|
||||
break;
|
||||
case SPI_SPEED_32:
|
||||
AUDIO_SPI->CR1 |= 0x04 << 3; // Fsck=Fpclk/32=2.25Mhz
|
||||
break;
|
||||
case SPI_SPEED_64:
|
||||
AUDIO_SPI->CR1 |= 0x05 << 3; // Fsck=Fpclk/16=1.125Mhz
|
||||
break;
|
||||
case SPI_SPEED_128:
|
||||
AUDIO_SPI->CR1 |= 0x06 << 3; // Fsck=Fpclk/16=562.5Khz
|
||||
break;
|
||||
case SPI_SPEED_256:
|
||||
AUDIO_SPI->CR1 |= 0x07 << 3; // Fsck=Fpclk/16=281.25Khz
|
||||
break;
|
||||
if (_instance->RST != GPIO_UNDEF) {
|
||||
gpio_set(_instance->RST);
|
||||
}
|
||||
AUDIO_SPI->CR1 |= 0x01 << 6;
|
||||
}
|
||||
|
||||
static uint8_t audioSpiReadWriteByte(uint8_t value)
|
||||
static inline void _reset_low()
|
||||
{
|
||||
return stm32_spi_transfer_byte(&_audio_spi, value);
|
||||
if (_instance->RST != GPIO_UNDEF) {
|
||||
gpio_clear(_instance->RST);
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t audioWaitDreq(int32_t delay_us)
|
||||
static inline void _set_mute_pin(bool muted)
|
||||
{
|
||||
if (_instance->MUTE != GPIO_UNDEF) {
|
||||
_is_muted = muted;
|
||||
bool inv = _instance->flags & VS1053B_MUTE_INVERTED;
|
||||
gpio_write(_instance->MUTE, inv ? !muted : muted);
|
||||
}
|
||||
}
|
||||
|
||||
static void vs1053b_gpio_init(void)
|
||||
{
|
||||
gpio_init(_instance->XDCS, GPIO_OUT, GPIO_PIN_SPEED_HIGH);
|
||||
gpio_init(_instance->DREQ, GPIO_IN, GPIO_PIN_SPEED_HIGH);
|
||||
|
||||
if (_instance->RST != GPIO_UNDEF) {
|
||||
gpio_init(_instance->RST, GPIO_OUT, GPIO_PIN_SPEED_HIGH);
|
||||
}
|
||||
|
||||
if (_instance->MUTE != GPIO_UNDEF) {
|
||||
gpio_init(_instance->MUTE, GPIO_OUT, GPIO_PIN_SPEED_HIGH);
|
||||
_set_mute_pin(true);
|
||||
}
|
||||
|
||||
stm32_spi_init(_instance->spi, LL_SPI_DATAWIDTH_8BIT);
|
||||
}
|
||||
|
||||
static uint8_t vs1053b_spi_rw_byte(uint8_t value)
|
||||
{
|
||||
return stm32_spi_transfer_byte(_instance->spi, value);
|
||||
}
|
||||
|
||||
static uint8_t vs1053b_wait_dreq(int32_t delay_us)
|
||||
{
|
||||
while (READ_DREQ() == 0) {
|
||||
if (delay_us-- == 0) return 0;
|
||||
@@ -169,115 +150,120 @@ static uint8_t audioWaitDreq(int32_t delay_us)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static uint16_t audioSpiReadReg(uint8_t address)
|
||||
static uint16_t vs1053b_read_reg(uint8_t address)
|
||||
{
|
||||
if (!audioWaitDreq(100))
|
||||
if (!vs1053b_wait_dreq(100))
|
||||
return 0;
|
||||
|
||||
audioSpiSetSpeed(SPI_SPEED_64);
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_LOW_SPEED);
|
||||
XDCS_HIGH();
|
||||
CS_LOW();
|
||||
audioSpiReadWriteByte(VS_READ_COMMAND);
|
||||
audioSpiReadWriteByte(address);
|
||||
uint16_t result = audioSpiReadWriteByte(0xff) << 8;
|
||||
result += audioSpiReadWriteByte(0xff);
|
||||
stm32_spi_select(_instance->spi);
|
||||
vs1053b_spi_rw_byte(VS_READ_COMMAND);
|
||||
vs1053b_spi_rw_byte(address);
|
||||
volatile uint16_t result = vs1053b_spi_rw_byte(0xff) << 8;
|
||||
result += vs1053b_spi_rw_byte(0xff);
|
||||
delay_01us(100); // 10us
|
||||
CS_HIGH();
|
||||
audioSpiSetSpeed(SPI_SPEED_8);
|
||||
stm32_spi_unselect(_instance->spi);
|
||||
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_HIGH_SPEED);
|
||||
return result;
|
||||
}
|
||||
|
||||
static uint8_t audioSpiWriteCmd(uint8_t address, uint16_t data)
|
||||
static uint8_t vs1053b_write_cmd(uint8_t address, uint16_t data)
|
||||
{
|
||||
if (!audioWaitDreq(100))
|
||||
if (!vs1053b_wait_dreq(100))
|
||||
return 0;
|
||||
|
||||
audioSpiSetSpeed(SPI_SPEED_64);
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_LOW_SPEED);
|
||||
XDCS_HIGH();
|
||||
CS_LOW();
|
||||
audioSpiReadWriteByte(VS_WRITE_COMMAND);
|
||||
audioSpiReadWriteByte(address);
|
||||
audioSpiReadWriteByte(data >> 8);
|
||||
audioSpiReadWriteByte(data);
|
||||
stm32_spi_select(_instance->spi);
|
||||
vs1053b_spi_rw_byte(VS_WRITE_COMMAND);
|
||||
vs1053b_spi_rw_byte(address);
|
||||
vs1053b_spi_rw_byte(data >> 8);
|
||||
vs1053b_spi_rw_byte(data);
|
||||
delay_01us(50); // 5us
|
||||
CS_HIGH();
|
||||
audioSpiSetSpeed(SPI_SPEED_8);
|
||||
stm32_spi_unselect(_instance->spi);
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_HIGH_SPEED);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void audioResetDecodeTime(void)
|
||||
static void vs1053b_reset_decode_time()
|
||||
{
|
||||
audioSpiWriteCmd(SPI_DECODE_TIME, 0x0000);
|
||||
audioSpiWriteCmd(SPI_DECODE_TIME, 0x0000);
|
||||
vs1053b_write_cmd(SPI_DECODE_TIME, 0x0000);
|
||||
vs1053b_write_cmd(SPI_DECODE_TIME, 0x0000);
|
||||
}
|
||||
|
||||
static uint8_t audioHardReset(void)
|
||||
static uint8_t vs1053b_hard_reset()
|
||||
{
|
||||
XDCS_HIGH();
|
||||
CS_HIGH();
|
||||
RST_LOW();
|
||||
stm32_spi_unselect(_instance->spi);
|
||||
_reset_low();
|
||||
delay_ms(100); // 100ms
|
||||
RST_HIGH();
|
||||
_reset_high();
|
||||
|
||||
if (!audioWaitDreq(5000))
|
||||
if (!vs1053b_wait_dreq(5000))
|
||||
return 0;
|
||||
|
||||
delay_ms(20); // 20ms
|
||||
return 1;
|
||||
}
|
||||
|
||||
static uint8_t audioSoftReset(void)
|
||||
static uint8_t vs1053b_soft_reset()
|
||||
{
|
||||
audioSpiSetSpeed(SPI_SPEED_64);
|
||||
if (!audioWaitDreq(100))
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_LOW_SPEED);
|
||||
if (!vs1053b_wait_dreq(100))
|
||||
return 0;
|
||||
|
||||
audioSpiReadWriteByte(0x00); // start the transfer
|
||||
vs1053b_spi_rw_byte(0x00); // start the transfer
|
||||
|
||||
uint8_t retry = 0;
|
||||
uint16_t mode = SM_SDINEW;
|
||||
while (audioSpiReadReg(SPI_MODE) != mode && retry < 100) {
|
||||
while (vs1053b_read_reg(SPI_MODE) != mode && retry < 100) {
|
||||
retry++;
|
||||
audioSpiWriteCmd(SPI_MODE, mode | SM_RESET);
|
||||
vs1053b_write_cmd(SPI_MODE, mode | SM_RESET);
|
||||
delay_ms(2);
|
||||
}
|
||||
|
||||
// wait for set up successful
|
||||
retry = 0;
|
||||
while (audioSpiReadReg(SPI_CLOCKF) != 0x9800 && retry < 100) {
|
||||
while (vs1053b_read_reg(SPI_CLOCKF) != 0x9800 && retry < 100) {
|
||||
retry++;
|
||||
audioSpiWriteCmd(SPI_CLOCKF, 0x9800);
|
||||
vs1053b_write_cmd(SPI_CLOCKF, 0x9800);
|
||||
}
|
||||
|
||||
audioResetDecodeTime(); // reset the decoding time
|
||||
audioSpiSetSpeed(SPI_SPEED_8);
|
||||
vs1053b_reset_decode_time(); // reset the decoding time
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_HIGH_SPEED);
|
||||
|
||||
XDCS_LOW();
|
||||
audioSpiReadWriteByte(0X0);
|
||||
audioSpiReadWriteByte(0X0);
|
||||
audioSpiReadWriteByte(0X0);
|
||||
audioSpiReadWriteByte(0X0);
|
||||
vs1053b_spi_rw_byte(0X0);
|
||||
vs1053b_spi_rw_byte(0X0);
|
||||
vs1053b_spi_rw_byte(0X0);
|
||||
vs1053b_spi_rw_byte(0X0);
|
||||
delay_01us(100); // 10us
|
||||
XDCS_HIGH();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static uint32_t audioSpiWriteData(const uint8_t * buffer, uint32_t size)
|
||||
static uint32_t vs1053b_send_data(const uint8_t * buffer, uint32_t size)
|
||||
{
|
||||
XDCS_LOW();
|
||||
|
||||
uint32_t index = 0;
|
||||
while (index < size && READ_DREQ() != 0) {
|
||||
for (int i=0; i<MP3_BUFFER_SIZE && index<size; i++) {
|
||||
audioSpiReadWriteByte(buffer[index++]);
|
||||
for (int i = 0; i < VS1053_BUFFER_SIZE && index < size; i++) {
|
||||
vs1053b_spi_rw_byte(buffer[index++]);
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
static void audioSpiWriteBuffer(const uint8_t * buffer, uint32_t size)
|
||||
static void vs1053b_send_buffer(const uint8_t * buffer, uint32_t size)
|
||||
{
|
||||
const uint8_t * p = buffer;
|
||||
while (size > 0) {
|
||||
uint32_t written = audioSpiWriteData(p, size);
|
||||
uint32_t written = vs1053b_send_data(p, size);
|
||||
p += written;
|
||||
size -= written;
|
||||
}
|
||||
@@ -304,150 +290,117 @@ const uint8_t RiffHeader[] = {
|
||||
0x00, 0x00, 0x00, 0x00, // data
|
||||
};
|
||||
|
||||
static void audioSendRiffHeader()
|
||||
static void vs1053b_send_riff_header()
|
||||
{
|
||||
audioSpiWriteBuffer(RiffHeader, sizeof(RiffHeader));
|
||||
vs1053b_send_buffer(RiffHeader, sizeof(RiffHeader));
|
||||
}
|
||||
|
||||
#if defined(AUDIO_MUTE_GPIO)
|
||||
static inline void setMutePin(bool enabled)
|
||||
static void vs1053b_update_volume()
|
||||
{
|
||||
#if defined(INVERTED_MUTE_PIN)
|
||||
enabled = !enabled;
|
||||
#endif
|
||||
gpio_write(AUDIO_MUTE_GPIO, enabled);
|
||||
}
|
||||
|
||||
static inline bool getMutePin(void)
|
||||
{
|
||||
bool muted = gpio_read(AUDIO_MUTE_GPIO) ? 1 : 0;
|
||||
#if defined(INVERTED_MUTE_PIN)
|
||||
muted = !muted;
|
||||
#endif
|
||||
return muted;
|
||||
}
|
||||
|
||||
static void audioMute()
|
||||
{
|
||||
#if defined(AUDIO_UNMUTE_DELAY)
|
||||
tmr10ms_t now = get_tmr10ms();
|
||||
if (!audioQueue.lastAudioPlayTime) {
|
||||
// we start the mute delay now
|
||||
audioQueue.lastAudioPlayTime = now;
|
||||
} else if (now - audioQueue.lastAudioPlayTime > AUDIO_MUTE_DELAY / 10) {
|
||||
// delay expired, we may mute
|
||||
setMutePin(true);
|
||||
if (_async_volume >= 0) {
|
||||
uint8_t value = _async_volume;
|
||||
vs1053b_write_cmd(SPI_VOL, (value << 8) + value);
|
||||
_async_volume = -1;
|
||||
}
|
||||
#else
|
||||
// mute
|
||||
setMutePin(true);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void audioUnmute()
|
||||
static void vs1053b_mute()
|
||||
{
|
||||
if (_is_muted) return;
|
||||
|
||||
if (_instance->mute_delay_ms) {
|
||||
uint32_t now = timersGetMsTick();
|
||||
if (!_last_play_ts) {
|
||||
// we start the mute delay now
|
||||
_last_play_ts = now;
|
||||
} else if (now - _last_play_ts < _instance->mute_delay_ms) {
|
||||
// delay not expired, we may not mute yet
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_set_mute_pin(true);
|
||||
}
|
||||
|
||||
static void vs1053b_unmute()
|
||||
{
|
||||
if(isFunctionActive(FUNCTION_DISABLE_AUDIO_AMP)) {
|
||||
setMutePin(true);
|
||||
_set_mute_pin(true);
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(AUDIO_UNMUTE_DELAY)
|
||||
// if muted
|
||||
if (getMutePin()) {
|
||||
// ..un-mute
|
||||
setMutePin(false);
|
||||
RTOS_WAIT_MS(AUDIO_UNMUTE_DELAY);
|
||||
if (_instance->unmute_delay_ms) {
|
||||
// if muted
|
||||
if (_is_muted) {
|
||||
// ..un-mute
|
||||
_set_mute_pin(false);
|
||||
RTOS_WAIT_MS(_instance->unmute_delay_ms);
|
||||
}
|
||||
// reset the mute delay
|
||||
_last_play_ts = 0;
|
||||
} else {
|
||||
_set_mute_pin(false);
|
||||
}
|
||||
// reset the mute delay
|
||||
audioQueue.lastAudioPlayTime = 0;
|
||||
#else
|
||||
setMutePin(false);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(PCBX12S)
|
||||
static void audioShutdownInit()
|
||||
void vs1053b_init(const vs1053b_t* dev)
|
||||
{
|
||||
gpio_init(AUDIO_SHUTDOWN_GPIO, GPIO_OUT, GPIO_PIN_SPEED_LOW);
|
||||
_instance = dev;
|
||||
|
||||
// we never RESET it, there is a 2s delay on STARTUP
|
||||
gpio_set(AUDIO_SHUTDOWN_GPIO);
|
||||
}
|
||||
#endif
|
||||
vs1053b_gpio_init();
|
||||
vs1053b_hard_reset();
|
||||
vs1053b_soft_reset();
|
||||
|
||||
void audioInit()
|
||||
{
|
||||
#if defined(AUDIO_MUTE_GPIO)
|
||||
// Mute before init anything
|
||||
gpio_init(AUDIO_MUTE_GPIO, GPIO_OUT, GPIO_PIN_SPEED_LOW);
|
||||
setMutePin(true);
|
||||
#endif
|
||||
|
||||
#if defined(PCBX12S)
|
||||
audioShutdownInit();
|
||||
#endif
|
||||
|
||||
audioSpiInit();
|
||||
audioHardReset();
|
||||
audioSoftReset();
|
||||
audioSpiSetSpeed(SPI_SPEED_8);
|
||||
stm32_spi_set_max_baudrate(_instance->spi, SPI_HIGH_SPEED);
|
||||
delay_ms(1); // 1ms
|
||||
audioSendRiffHeader();
|
||||
}
|
||||
|
||||
static uint8_t * currentBuffer = nullptr;
|
||||
static uint32_t currentSize = 0;
|
||||
static int16_t newVolume = -1;
|
||||
vs1053b_send_riff_header();
|
||||
}
|
||||
|
||||
static void set_volume(uint8_t volume)
|
||||
{
|
||||
newVolume = volume;
|
||||
_async_volume = volume;
|
||||
}
|
||||
|
||||
static void audioSetCurrentBuffer(const AudioBuffer * buffer)
|
||||
{
|
||||
if (buffer) {
|
||||
currentBuffer = (uint8_t *)buffer->data;
|
||||
currentSize = buffer->size * 2;
|
||||
_audio_buffer = (uint8_t *)buffer->data;
|
||||
_audio_buffer_len = buffer->size * 2;
|
||||
}
|
||||
else {
|
||||
currentBuffer = nullptr;
|
||||
currentSize = 0;
|
||||
_audio_buffer = nullptr;
|
||||
_audio_buffer_len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static const uint8_t nullBytes[32] = {0};
|
||||
|
||||
void audioConsumeCurrentBuffer()
|
||||
{
|
||||
if (newVolume >= 0) {
|
||||
uint8_t value = newVolume;
|
||||
audioSpiWriteCmd(SPI_VOL, (value << 8) + value);
|
||||
// audioSendRiffHeader();
|
||||
newVolume = -1;
|
||||
}
|
||||
vs1053b_update_volume();
|
||||
|
||||
if (!currentBuffer) {
|
||||
if (!_audio_buffer) {
|
||||
audioSetCurrentBuffer(audioQueue.buffersFifo.getNextFilledBuffer());
|
||||
}
|
||||
|
||||
if (currentBuffer) {
|
||||
#if defined(AUDIO_MUTE_GPIO)
|
||||
audioUnmute();
|
||||
#endif
|
||||
uint32_t written = audioSpiWriteData(currentBuffer, currentSize);
|
||||
currentBuffer += written;
|
||||
currentSize -= written;
|
||||
if (currentSize == 0) {
|
||||
if (_audio_buffer) {
|
||||
vs1053b_unmute();
|
||||
|
||||
uint32_t written = vs1053b_send_data(_audio_buffer, _audio_buffer_len);
|
||||
_audio_buffer += written;
|
||||
_audio_buffer_len -= written;
|
||||
if (_audio_buffer_len == 0) {
|
||||
audioQueue.buffersFifo.freeNextFilledBuffer();
|
||||
currentBuffer = nullptr;
|
||||
currentSize = 0;
|
||||
_audio_buffer = nullptr;
|
||||
_audio_buffer_len = 0;
|
||||
}
|
||||
} else {
|
||||
if(READ_DREQ()) {
|
||||
vs1053b_send_data(nullBytes, sizeof(nullBytes));
|
||||
}
|
||||
vs1053b_mute();
|
||||
}
|
||||
#if defined(AUDIO_MUTE_GPIO)
|
||||
else {
|
||||
audioMute();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// adjust this value for a volume level just above the silence
|
||||
@@ -462,10 +415,10 @@ void audioSetVolume(uint8_t volume)
|
||||
}
|
||||
// maximum volume is 0x00 and total silence is 0xFE
|
||||
if (volume == 0) {
|
||||
set_volume(0xFE); // silence
|
||||
}
|
||||
else {
|
||||
uint32_t vol = (VOLUME_MIN_DB * 2) - ((uint32_t)volume * (VOLUME_MIN_DB * 2)) / VOLUME_LEVEL_MAX;
|
||||
set_volume(0xFE); // silence
|
||||
} else {
|
||||
uint32_t vol = (VOLUME_MIN_DB * 2) -
|
||||
((uint32_t)volume * (VOLUME_MIN_DB * 2)) / VOLUME_LEVEL_MAX;
|
||||
set_volume(vol);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (C) EdgeTX
|
||||
*
|
||||
* Based on code named
|
||||
* opentx - https://github.com/opentx/opentx
|
||||
* th9x - http://code.google.com/p/th9x
|
||||
* er9x - http://code.google.com/p/er9x
|
||||
* gruvin9x - http://code.google.com/p/gruvin9x
|
||||
*
|
||||
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License version 2 as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "hal/gpio.h"
|
||||
#include "stm32_spi.h"
|
||||
|
||||
#define VS1053B_MUTE_INVERTED (1 << 0)
|
||||
|
||||
typedef struct {
|
||||
stm32_spi_t* spi;
|
||||
|
||||
gpio_t XDCS;
|
||||
gpio_t DREQ;
|
||||
gpio_t RST;
|
||||
gpio_t MUTE;
|
||||
|
||||
uint32_t flags;
|
||||
uint32_t mute_delay_ms;
|
||||
uint32_t unmute_delay_ms;
|
||||
} vs1053b_t;
|
||||
|
||||
void vs1053b_init(const vs1053b_t* dev);
|
||||
Reference in New Issue
Block a user