Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
398 changes: 398 additions & 0 deletions Firmware/AS5047校准.md

Large diffs are not rendered by default.

47 changes: 44 additions & 3 deletions Firmware/Board/v3/Inc/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,39 @@
#if HW_VERSION_MINOR <= 3
#define SHUNT_RESISTANCE (675e-6f)
#else
#define SHUNT_RESISTANCE (500e-6f)
//#define SHUNT_RESISTANCE (500e-6f)
#define SHUNT_RESISTANCE (2e-3f) //产品板
#endif

//qiaoxu add: 编码器类型 (二选一, 必须且只能定义一个)
// ENCODER_USE_HALL : 霍尔, cpr = 24
// ENCODER_USE_SPI_ABS_AMS : AS5047P 磁编, cpr = 16384, 支持 CAN 0x277 零位标定/OTP 烧写
#define ENCODER_USE_SPI_ABS_AMS
//#define ENCODER_USE_HALL
#define DEBUG_USB //是否用USB调试,打开的话不发CAN ID = 0x274
//qiaoxu add: 驻坡增益调度开关
// 定义 : 零速附近检测到较大 Iq 时临时提高 vel_gain / vel_integrator_gain (原行为)
// 注释掉 : 关闭调度, 增益始终等于上位机通过 0x208 / 0x221 下发的基准 PID
// 用途 : A/B 对比, 判断振荡是否由该调度引入
#define ENABLE_ZHUPO_GAIN_SCHED

//qiaoxu add: 通信外设裁剪开关 (不用的外设注释掉, 对应代码不参与编译, 可减小固件体积)
// USE_UART_A : UART4 (GPIO1/GPIO2, ODrive 的 uart0)
// USE_UART_B : USART2 (GPIO3/GPIO4, ODrive 的 uart1)
// USE_UART_C : 预留, 本板未接
// USE_I2C_A : I2C1 (GPIO13/GPIO14, 与 CAN_R/CAN_D 复用)
//#define USE_UART_A
//#define USE_UART_B
//#define USE_UART_C
//#define USE_I2C_A

//qiaoxu add: 通信协议裁剪开关
// USE_ASCII_PROTOCOL : USB CDC / UART 上的 ASCII 文本协议 ("p 0 1.0"、"r vbus_voltage" 等)
// 本机走 CAN + fibre(odrivetool), 不用 ASCII 文本协议, 关闭可省 ~29KB
//#define USE_ASCII_PROTOCOL

#if defined(USE_UART_A) || defined(USE_UART_B) || defined(USE_UART_C)
#define USE_UART_ANY
#endif

#define AXIS_COUNT (2)
Expand All @@ -46,10 +78,19 @@
#define DEFAULT_ERROR_PIN 0
#define DEFAULT_MIN_DC_VOLTAGE 8.0f

//qiaoxu add: 裁剪掉 UART_A 时 GPIO1/GPIO2 不再默认复用为 UART
#ifdef USE_UART_A
#define DEFAULT_GPIO_MODE_1 ODriveIntf::GPIO_MODE_UART_A
#define DEFAULT_GPIO_MODE_2 ODriveIntf::GPIO_MODE_UART_A
#else
#define DEFAULT_GPIO_MODE_1 ODriveIntf::GPIO_MODE_DIGITAL
#define DEFAULT_GPIO_MODE_2 ODriveIntf::GPIO_MODE_DIGITAL
#endif

#define DEFAULT_GPIO_MODES \
ODriveIntf::GPIO_MODE_DIGITAL, \
ODriveIntf::GPIO_MODE_UART_A, \
ODriveIntf::GPIO_MODE_UART_A, \
DEFAULT_GPIO_MODE_1, \
DEFAULT_GPIO_MODE_2, \
ODriveIntf::GPIO_MODE_ANALOG_IN, \
ODriveIntf::GPIO_MODE_ANALOG_IN, \
ODriveIntf::GPIO_MODE_ANALOG_IN, \
Expand Down
4 changes: 3 additions & 1 deletion Firmware/Board/v3/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@

/* Private define ------------------------------------------------------------*/
#define TIM_1_8_CLOCK_HZ 168000000
#define TIM_1_8_PERIOD_CLOCKS 3500
#define TIM_1_8_PERIOD_CLOCKS 3500//24khz 原程序24khz跑两个电机的中断时间不够,会报错:MOTOR_ERROR_CONTROL_DEADLINE_MISSED,磁编电机还会报ENCODER_ERROR_ABS_SPI_COM_FAIL
//#define TIM_1_8_PERIOD_CLOCKS 7000 //qiaoxu modify 12khz
//#define TIM_1_8_PERIOD_CLOCKS 5250 //qiaoxu modify 16khz
#define TIM_1_8_DEADTIME_CLOCKS 20
#define TIM_APB1_CLOCK_HZ 84000000
#define TIM_APB1_PERIOD_CLOCKS 4096
Expand Down
12 changes: 11 additions & 1 deletion Firmware/Board/v3/STM32F405RGTx_FLASH.ld
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 768K
/* FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 768K */
FLASH (rx) : ORIGIN = 0x800C000, LENGTH = 336k /*720K*/
NVM (r) : ORIGIN = 0x80C0000, LENGTH = 256K
}

Expand All @@ -59,6 +60,15 @@ SECTIONS
. = ALIGN(4);
} >FLASH

/* 添加版本信息段 */
.version_info :
{
. = ALIGN(4);
/* 版本信息位于0x0800C188*/
KEEP(*(.version_info))
. = ALIGN(4);
} >FLASH

/* The program code and other data goes into FLASH */
.text :
{
Expand Down
11 changes: 11 additions & 0 deletions Firmware/Board/v3/Src/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
#include "gpio.h"
#include "dma.h"

//qiaoxu add: 引入 USE_I2C_A 外设裁剪开关
#include "board.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */
Expand All @@ -61,6 +64,7 @@ I2C_HandleTypeDef hi2c1;
DMA_HandleTypeDef hdma_i2c1_rx;
DMA_HandleTypeDef hdma_i2c1_tx;

#ifdef USE_I2C_A
/* I2C1 init function */
void MX_I2C1_Init(uint8_t addr)
{
Expand All @@ -80,10 +84,13 @@ void MX_I2C1_Init(uint8_t addr)
}

}
#endif /* USE_I2C_A */

void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{
(void)i2cHandle;

#ifdef USE_I2C_A
if(i2cHandle->Instance==I2C1)
{
/* USER CODE BEGIN I2C1_MspInit 0 */
Expand Down Expand Up @@ -139,11 +146,14 @@ void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)

/* USER CODE END I2C1_MspInit 1 */
}
#endif /* USE_I2C_A */
}

void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
{
(void)i2cHandle;

#ifdef USE_I2C_A
if(i2cHandle->Instance==I2C1)
{
/* USER CODE BEGIN I2C1_MspDeInit 0 */
Expand All @@ -169,6 +179,7 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)

/* USER CODE END I2C1_MspDeInit 1 */
}
#endif /* USE_I2C_A */
}

/* USER CODE BEGIN 1 */
Expand Down
6 changes: 6 additions & 0 deletions Firmware/Board/v3/Src/stm32f4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

/* USER CODE BEGIN 0 */
#include <Drivers/STM32/stm32_system.h>
//qiaoxu add: 引入 USE_UART_A / USE_UART_B 外设裁剪开关
#include "board.h"
/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
Expand Down Expand Up @@ -353,6 +355,7 @@ void CAN1_SCE_IRQHandler(void)
/* USER CODE END CAN1_SCE_IRQn 1 */
}

#ifdef USE_UART_B
/**
* @brief This function handles USART2 global interrupt.
*/
Expand All @@ -366,6 +369,7 @@ void USART2_IRQHandler(void)

/* USER CODE END USART2_IRQn 1 */
}
#endif /* USE_UART_B */

/**
* @brief This function handles TIM8 trigger and commutation interrupts and TIM14 global interrupt.
Expand Down Expand Up @@ -396,6 +400,7 @@ void SPI3_IRQHandler(void)
/* USER CODE END SPI3_IRQn 1 */
}

#ifdef USE_UART_A
/**
* @brief This function handles UART4 global interrupt.
*/
Expand All @@ -409,6 +414,7 @@ void UART4_IRQHandler(void)

/* USER CODE END UART4_IRQn 1 */
}
#endif /* USE_UART_A */

/* USER CODE BEGIN 1 */

Expand Down
24 changes: 22 additions & 2 deletions Firmware/Board/v3/Src/usart.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
#include "gpio.h"
#include "dma.h"

//qiaoxu add: 引入 USE_UART_A / USE_UART_B 外设裁剪开关
#include "board.h"

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */
Expand All @@ -64,6 +67,7 @@ DMA_HandleTypeDef hdma_uart4_tx;
DMA_HandleTypeDef hdma_usart2_rx;
DMA_HandleTypeDef hdma_usart2_tx;

#ifdef USE_UART_A
/* UART4 init function */
void MX_UART4_Init(void)
{
Expand All @@ -82,6 +86,9 @@ void MX_UART4_Init(void)
}

}
#endif /* USE_UART_A */

#ifdef USE_UART_B
/* USART2 init function */

void MX_USART2_UART_Init(void)
Expand All @@ -101,10 +108,13 @@ void MX_USART2_UART_Init(void)
}

}
#endif /* USE_UART_B */

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
(void)uartHandle;

#ifdef USE_UART_A
if(uartHandle->Instance==UART4)
{
/* USER CODE BEGIN UART4_MspInit 0 */
Expand Down Expand Up @@ -157,7 +167,10 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)

/* USER CODE END UART4_MspInit 1 */
}
else if(uartHandle->Instance==USART2)
#endif /* USE_UART_A */

#ifdef USE_UART_B
if(uartHandle->Instance==USART2)
{
/* USER CODE BEGIN USART2_MspInit 0 */

Expand Down Expand Up @@ -209,11 +222,14 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)

/* USER CODE END USART2_MspInit 1 */
}
#endif /* USE_UART_B */
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
(void)uartHandle;

#ifdef USE_UART_A
if(uartHandle->Instance==UART4)
{
/* USER CODE BEGIN UART4_MspDeInit 0 */
Expand All @@ -232,7 +248,10 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)

/* USER CODE END UART4_MspDeInit 1 */
}
else if(uartHandle->Instance==USART2)
#endif /* USE_UART_A */

#ifdef USE_UART_B
if(uartHandle->Instance==USART2)
{
/* USER CODE BEGIN USART2_MspDeInit 0 */

Expand All @@ -250,6 +269,7 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)

/* USER CODE END USART2_MspDeInit 1 */
}
#endif /* USE_UART_B */
}

/* USER CODE BEGIN 1 */
Expand Down
16 changes: 16 additions & 0 deletions Firmware/Board/v3/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ uint8_t __attribute__((section(".testdata"))) fake_otp[FLASH_OTP_END + 1 - FLASH
Stm32SpiArbiter spi3_arbiter{&hspi3};
Stm32SpiArbiter& ext_spi_arbiter = spi3_arbiter;

#ifdef USE_UART_A
UART_HandleTypeDef* uart_a = &huart4;
#else
UART_HandleTypeDef* uart_a = nullptr;
#endif
#ifdef USE_UART_B
UART_HandleTypeDef* uart_b = &huart2; // TODO: this could be supported in ODrive v3.6 (or similar) using STM32's USART2
#else
UART_HandleTypeDef* uart_b = nullptr;
#endif
UART_HandleTypeDef* uart_c = nullptr;

Drv8301 m0_gate_driver{
Expand Down Expand Up @@ -327,16 +335,21 @@ bool board_init() {
HAL_NVIC_SetPriority(TIM8_UP_TIM13_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM8_UP_TIM13_IRQn);

#ifdef USE_UART_A
if (odrv.config_.enable_uart_a) {
uart_a->Init.BaudRate = odrv.config_.uart_a_baudrate;
MX_UART4_Init();
}
#endif

#ifdef USE_UART_B
if (odrv.config_.enable_uart_b) {
uart_b->Init.BaudRate = odrv.config_.uart_b_baudrate;
MX_USART2_UART_Init();
}
#endif

#ifdef USE_I2C_A
if (odrv.config_.enable_i2c_a) {
// Set up the direction GPIO as input
get_gpio(3).config(GPIO_MODE_INPUT, GPIO_PULLUP);
Expand All @@ -350,6 +363,7 @@ bool board_init() {
i2c_stats_.addr |= get_gpio(5).read() ? 0x4 : 0;
MX_I2C1_Init(i2c_stats_.addr);
}
#endif

if (odrv.config_.enable_can_a) {
// The CAN initialization will (and must) init its own GPIOs before the
Expand Down Expand Up @@ -574,6 +588,7 @@ void ControlLoop_IRQHandler(void) {
TaskTimer::enabled = false;
}

#ifdef USE_I2C_A
void I2C1_EV_IRQHandler(void) {
COUNT_IRQ(I2C1_EV_IRQn);
HAL_I2C_EV_IRQHandler(&hi2c1);
Expand All @@ -583,6 +598,7 @@ void I2C1_ER_IRQHandler(void) {
COUNT_IRQ(I2C1_ER_IRQn);
HAL_I2C_ER_IRQHandler(&hi2c1);
}
#endif

extern PCD_HandleTypeDef hpcd_USB_OTG_FS; // defined in usbd_conf.c
void OTG_FS_IRQHandler(void) {
Expand Down
3 changes: 2 additions & 1 deletion Firmware/Board/v3/startup_stm32f405xx.s
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ LoopFillZerobss:

/* Call the clock system intitialization function.*/
bl SystemInit
bl early_start_checks
/* qiaoxu delete */
/* bl early_start_checks qiaoxu*/

/* Call static constructors */
bl __libc_init_array
Expand Down
Loading