Browse Source

Merge remote-tracking branch 'origin/master'

master
zhaohe 10 months ago
parent
commit
67db42d0d7
  1. 93
      sysmgr/sys_mgr.cpp
  2. 23
      sysmgr/sys_mgr.hpp
  3. 83
      tmcdriver/tmc/tmc_reg_cache.hpp
  4. 54
      tmcdriver/tmc51x0/tmc51x0.cpp
  5. 12
      tmcdriver/tmc51x0/tmc51x0.hpp

93
sysmgr/sys_mgr.cpp

@ -2,7 +2,6 @@
#include <stdlib.h>
using namespace iflytop;
#define TAG "SysMgr"
@ -42,11 +41,62 @@ static size_t get_free_heap_size() {
/***********************************************************************************************************************
* STM32_ERROR_IRQ *
***********************************************************************************************************************/
void SysMgr_on_NMI_Handler(void) { ZLOGI(TAG, "on NMI_Handler"); }
void SysMgr_on_HardFault_Handler(void) { ZLOGI(TAG, "on HardFault_Handler"); }
void SysMgr_on_MemManage_Handler(void) { ZLOGI(TAG, "on MemManage_Handler"); }
void SysMgr_on_BusFault_Handler(void) { ZLOGI(TAG, "on BusFault_Handler"); }
void SysMgr_on_UsageFault_Handler(void) { ZLOGI(TAG, "on UsageFault_Handler"); }
// void SysMgr_on_NMI_Handler(void) { ZLOGI(TAG, "on NMI_Handler"); }
// void SysMgr_on_HardFault_Handler(void) { ZLOGI(TAG, "on HardFault_Handler"); }
// void SysMgr_on_MemManage_Handler(void) { ZLOGI(TAG, "on MemManage_Handler"); }
// void SysMgr_on_BusFault_Handler(void) { ZLOGI(TAG, "on BusFault_Handler"); }
// void SysMgr_on_UsageFault_Handler(void) { ZLOGI(TAG, "on UsageFault_Handler"); }
void NMI_Handler(void) {
printf("E:%s\n", __FUNCTION__);
NVIC_SystemReset();
}
void HardFault_Handler(void) {
printf("E:%s\n", __FUNCTION__);
NVIC_SystemReset();
}
void MemManage_Handler(void) {
printf("E:%s\n", __FUNCTION__);
NVIC_SystemReset();
}
void BusFault_Handler(void) {
printf("E:%s\n", __FUNCTION__);
NVIC_SystemReset();
}
void UsageFault_Handler(void) {
printf("E:%s\n", __FUNCTION__);
NVIC_SystemReset();
}
void DebugMon_Handler(void) {}
static void PVD_Init(void) {
PWR_PVDTypeDef PvdStruct;
HAL_PWR_EnablePVD();
PvdStruct.PVDLevel = PWR_PVDLEVEL_6; /* set PVD thr to 3.1v */
PvdStruct.Mode = PWR_PVD_MODE_IT_RISING;
HAL_PWR_ConfigPVD(&PvdStruct);
/* enable lost power irq */
HAL_NVIC_SetPriority(PVD_IRQn, 0, 0); /* */
HAL_NVIC_EnableIRQ(PVD_IRQn); /* */
}
void PVD_IRQHandler(void) {
if (__HAL_PWR_GET_FLAG(PWR_FLAG_PVDO)) /* 1为VDD小于PVD阈值,掉电情况 */
{
/* 掉电前的紧急处理 */
printf("low power detect......\n");
for (size_t i = 0; i < 1000; i++) {
// wait for lost power
printf("low power detect......\n");
}
NVIC_SystemReset();
}
}
/***********************************************************************************************************************
* FREERTOS_ERROR *
@ -76,6 +126,32 @@ SysMgr* SysMgr::ins() {
size_t SysMgr::osGetSysRunTime() { return HAL_GetTick(); }
UBaseType_t uxTaskGetSystemState(TaskStatus_t* const pxTaskStatusArray, const UBaseType_t uxArraySize, uint32_t* const pulTotalRunTime);
void SysMgr::checkRst(void) {
if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET) { // NRST 引脚复位
printf("RESET_REASON: PIN reset \n");
m_reset_reason = kreset_reason_pinrst;
} else if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET) { // 上电掉电复位
printf("RESET_REASON: POR/PDR reset \n");
m_reset_reason = kreset_reason_porrst;
} else if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) != RESET) { // 软件复位
printf("RESET_REASON: Software reset \n");
m_reset_reason = kreset_reason_sftrst;
} else if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) { // 独立看门狗复位
printf("RESET_REASON: Independent watchdog reset \n");
m_reset_reason = kreset_reason_iwdgrst;
} else if (__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST) != RESET) { // 窗口看门狗复位
printf("RESET_REASON: Window watchdog reset \n");
m_reset_reason = kreset_reason_wwdgrst;
} else if (__HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST) != RESET) { // 低功耗复位
printf("RESET_REASON: (Low-power reset \n");
m_reset_reason = kreset_reason_lpwrrst;
} else {
printf("RESET_REASON: Unkown reset \n");
m_reset_reason = kreset_reason_unkown;
}
__HAL_RCC_CLEAR_RESET_FLAGS(); // 清除复位标志
}
void SysMgr::initedFinished() { //
static TaskStatus_t pxTaskStatusArray[SDK_MAX_TASK + 1];
UBaseType_t uxArraySize = SDK_MAX_TASK + 1;
@ -85,6 +161,9 @@ void SysMgr::initedFinished() { //
m_task[i].Id = pxTaskStatusArray[i].xHandle;
}
ZASSERT_INFO(m_ntask < SDK_MAX_TASK, "task num is too large, please increase SDK_MAX_TASK");
PVD_Init();
checkRst();
}
size_t SysMgr::osGetMinimumEverFreeHeapSize() { return ::xPortGetMinimumEverFreeHeapSize(); }
@ -95,8 +174,6 @@ size_t SysMgr::osGetFreeSysHeapSize() { return get_free_heap_size(); }
int32_t SysMgr::getTaskNum() { return m_ntask; }
void SysMgr::dumpSysInfo() {
zlog("---------------Heap Info--------------\n");
zlog("MinimumEverFreeHeapSize : %d\n", osGetMinimumEverFreeHeapSize());
zlog("RTOS FreeHeapSize : %d\n", osGetFreeHeapSize());

23
sysmgr/sys_mgr.hpp

@ -7,13 +7,24 @@ class ZTaskInfo {
osThreadId Id;
};
typedef enum {
kreset_reason_pinrst = 1, // NRST引脚复位
kreset_reason_porrst = 2, // 上电掉电复位
kreset_reason_sftrst = 3, // 软件复位
kreset_reason_iwdgrst = 4, // 独立看门狗复位
kreset_reason_wwdgrst = 5, // 窗口看门狗复位
kreset_reason_lpwrrst = 6, // 低功耗复位
kreset_reason_unkown = 7, //
} reset_reason_t;
class SysMgr {
private:
/* data */
public:
ZTaskInfo m_task[30] = {0};
int m_ntask = 0;
ZTaskInfo m_task[30] = {0};
int m_ntask = 0;
reset_reason_t m_reset_reason = kreset_reason_unkown;
static SysMgr* ins();
void initedFinished();
@ -45,6 +56,14 @@ class SysMgr {
void osTaskGetState(osThreadId id, char* state);
int32_t getTaskNum();
/***********************************************************************************************************************
* CHIP *
***********************************************************************************************************************/
reset_reason_t chipGetResetReason() { return m_reset_reason; }
private:
void checkRst(void);
};
} // namespace iflytop

83
tmcdriver/tmc/tmc_reg_cache.hpp

@ -0,0 +1,83 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <functional>
//
namespace iflytop {
using namespace std;
class TMCReg {
private:
/* data */
public:
uint8_t flag = 0;
uint8_t reg = 0;
int32_t val = 0;
void setActiveFlag() { flag = flag | 0x01; }
bool isActive() { return flag & 0x01; }
void setUpdateFlag() { flag = flag | 0x02; }
bool isUpdate() { return flag & 0x02; }
};
class TMCRegCache {
public:
TMCReg cache[20];
public:
void foreach (function<void(TMCReg* item)> fn) {
for (size_t i = 0; i < sizeof(cache) / sizeof(cache[0]); i++) {
if (cache[i].isActive() && cache[i].isUpdate()) {
fn(&cache[i]);
}
}
}
bool activeReg(uint8_t reg) {
TMCReg* item = findReg(reg);
if (item) return true;
item = findPassive();
if (item) {
item->reg = reg;
item->setActiveFlag();
return true;
}
return false;
}
void setReg(uint8_t reg, int32_t val) {
TMCReg* item = findReg(reg);
if (item) {
item->val = val;
item->setUpdateFlag();
}
}
private:
TMCReg* findPassive() {
for (size_t i = 0; i < sizeof(cache) / sizeof(cache[0]); i++) {
if (!cache[i].isActive()) {
return &cache[i];
}
}
return nullptr;
}
TMCReg* findReg(uint8_t reg) {
for (size_t i = 0; i < sizeof(cache) / sizeof(cache[0]); i++) {
if (cache[i].isActive() && cache[i].reg == reg) {
return &cache[i];
}
}
return nullptr;
}
};
} // namespace iflytop

54
tmcdriver/tmc51x0/tmc51x0.cpp

@ -28,7 +28,9 @@ static const uint8_t tmc5130_defaultRegisterAccess[TMC5130_REGISTER_COUNT] = {
0x42, 0x01, 0x02, 0x01, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____, ____ // 0x70 - 0x7F
};
#endif
#define TMC5160_GLOBAL_SCALER 0x0B
#define TMC5160_GLOBAL_SCALER_MASK 0xFF
#define TMC5160_GLOBAL_SCALER_SHIFT 0
/***********************************************************************************************************************
* ? *
***********************************************************************************************************************/
@ -57,6 +59,25 @@ void TMC51X0::initialize(int mid, TMC51X0Cfg cfg) {
stop();
ZASSERT(m_cache.activeReg(TMC5130_CHOPCONF));
ZASSERT(m_cache.activeReg(TMC5130_PWMCONF));
ZASSERT(m_cache.activeReg(TMC5130_GCONF));
ZASSERT(m_cache.activeReg(TMC5130_IHOLD_IRUN));
ZASSERT(m_cache.activeReg(TMC5160_GLOBAL_SCALER));
ZASSERT(m_cache.activeReg(TMC5130_VMAX));
ZASSERT(m_cache.activeReg(TMC5130_VSTART));
ZASSERT(m_cache.activeReg(TMC5130_A1));
ZASSERT(m_cache.activeReg(TMC5130_AMAX));
ZASSERT(m_cache.activeReg(TMC5130_V1));
ZASSERT(m_cache.activeReg(TMC5130_DMAX));
ZASSERT(m_cache.activeReg(TMC5130_D1));
ZASSERT(m_cache.activeReg(TMC5130_VSTOP));
ZASSERT(m_cache.activeReg(TMC5130_TZEROWAIT));
ZASSERT(m_cache.activeReg(TMC5130_ENCMODE));
ZASSERT(m_cache.activeReg(TMC5130_ENC_CONST));
ZASSERT(m_cache.activeReg(TMC5130_XENC));
writeInt(TMC5130_PWMCONF, 0x000500C8);
writeInt(TMC5130_CHOPCONF, 0x000100c3);
writeInt(TMC5130_IHOLD_IRUN, 0x00051A00);
@ -67,11 +88,11 @@ void TMC51X0::initialize(int mid, TMC51X0Cfg cfg) {
writeInt(TMC5130_TZEROWAIT, 0);
setVstart(100);
setA1(30);
setAmax(50);
setA1(15);
setAmax(15);
setV1(500);
setDmax(50);
setD1(30);
setDmax(15);
setD1(15);
setVstop(100);
setTzerowait(100);
@ -79,6 +100,13 @@ void TMC51X0::initialize(int mid, TMC51X0Cfg cfg) {
enable(false);
}
void TMC51X0::refreshcfg() {
m_cache.foreach ([this](TMCReg *item) {
ZLOGI(TAG, "refreshcfg %x,%x", item->reg, item->val);
writeInt(item->reg, item->val);
});
}
/*******************************************************************************
* *
*******************************************************************************/
@ -95,6 +123,7 @@ void TMC51X0::writeDatagram(uint8_t address, uint8_t x1, uint8_t x2, uint8_t x3,
readWriteArray(&data[0], 5);
}
void TMC51X0::writeInt(uint8_t address, int32_t value) { //
m_cache.setReg(address, value);
writeDatagram(address, BYTE(value, 3), BYTE(value, 2), BYTE(value, 1), BYTE(value, 0));
}
@ -187,6 +216,8 @@ void TMC51X0::stop() {
} else {
rotate(0);
}
m_isMoveToEnd = false;
// rotate(0);
}
void TMC51X0::rotate(int32_t velocity) {
zlock_guard lkg(m_mutex);
@ -194,6 +225,7 @@ void TMC51X0::rotate(int32_t velocity) {
velocity = to_motor_vel(velocity);
writeInt(TMC5130_VMAX, abs(velocity));
writeInt(TMC5130_RAMPMODE, (velocity >= 0) ? TMC5130_MODE_VELPOS : TMC5130_MODE_VELNEG);
m_isMoveToEnd = false;
}
void TMC51X0::moveTo(int32_t position, uint32_t velocityMax) {
zlock_guard lkg(m_mutex);
@ -203,10 +235,15 @@ void TMC51X0::moveTo(int32_t position, uint32_t velocityMax) {
writeInt(TMC5130_RAMPMODE, TMC5130_MODE_POSITION);
writeInt(TMC5130_VMAX, velocityMax);
writeInt(TMC5130_XTARGET, position);
m_isMoveToEnd = false;
}
void TMC51X0::moveToEnd(int32_t direction, uint32_t velocityMax) {
zlock_guard lkg(m_mutex);
// @WARNING:
// the motor will stop running(1000RPM) for about 10 minutes
// If you really need to use this feature, reset XACTUAL every 30s
setXACTUAL(0);
if (direction >= 0) {
writeInt(TMC5130_RAMPMODE, TMC5130_MODE_POSITION);
writeInt(TMC5130_VMAX, to_motor_vel(velocityMax));
@ -216,8 +253,12 @@ void TMC51X0::moveToEnd(int32_t direction, uint32_t velocityMax) {
writeInt(TMC5130_VMAX, to_motor_vel(velocityMax));
writeInt(TMC5130_XTARGET, INT32_MIN / 1.5 + 1000);
}
m_isMoveToEnd = true;
}
void TMC51X0::refreshMoveToEnd() { setXACTUAL(0); }
bool TMC51X0::isMoveToEnd() { return m_isMoveToEnd; }
void TMC51X0::moveBy(int32_t relativePosition, uint32_t velocityMax) { // determine actual position and add numbers of ticks to move
zlock_guard lkg(m_mutex);
@ -322,9 +363,6 @@ void TMC51X0::setIHOLD_IRUN(uint8_t ihold, uint8_t irun, uint16_t iholddelay) {
writeInt(TMC5130_IHOLD_IRUN, (iholddelay << TMC5130_IHOLDDELAY_SHIFT) | (irun << TMC5130_IRUN_SHIFT) | (ihold << TMC5130_IHOLD_SHIFT));
}
void TMC51X0::setGlobalScale(uint8_t globalscale) {
#define TMC5160_GLOBAL_SCALER 0x0B
#define TMC5160_GLOBAL_SCALER_MASK 0xFF
#define TMC5160_GLOBAL_SCALER_SHIFT 0
zlock_guard lkg(m_mutex);
if (isTMC5130()) {
return;

12
tmcdriver/tmc51x0/tmc51x0.hpp

@ -6,6 +6,7 @@
#include <string.h>
//
#include "../tmc/tmc_reg_cache.hpp"
#include "../tmc/tmc_type.h"
#include "reg/TMC5130_Type.h"
#include "stm32basic/stm32basic.hpp"
@ -26,7 +27,8 @@ class TMC51X0Cfg {
class TMC51X0 {
protected:
TMC51X0Cfg m_cfg;
TMC51X0Cfg m_cfg;
TMCRegCache m_cache;
SPI_HandleTypeDef *m_hspi = NULL;
ZGPIO m_csnpin;
@ -39,8 +41,9 @@ class TMC51X0 {
double m_onecirclepulse = 51200;
int32_t m_enc_resolution = 0; //
zmutex m_mutex = {"TMC51X0"};
bool m_driErr = false;
zmutex m_mutex = {"TMC51X0"};
bool m_driErr = false;
bool m_isMoveToEnd = false;
public:
void initialize(int mid, TMC51X0Cfg cfg);
@ -56,6 +59,7 @@ class TMC51X0 {
public:
bool ping();
void enable(bool enable);
void refreshcfg();
void setdriErr(bool val) { m_driErr = val; }
bool getdriErr() { return m_driErr; }
@ -65,6 +69,8 @@ class TMC51X0 {
void rotate(int32_t velocity);
void moveTo(int32_t position, uint32_t velocityMax);
void moveToEnd(int32_t direction, uint32_t velocityMax);
void refreshMoveToEnd();
bool isMoveToEnd();
void moveBy(int32_t relativePosition, uint32_t velocityMax);
void stop();
/***********************************************************************************************************************

Loading…
Cancel
Save