Skip to content

Commit 2935d89

Browse files
committed
Cleanups and added devid command
1 parent bc050d3 commit 2935d89

9 files changed

Lines changed: 65 additions & 69 deletions

File tree

Firmware/FFBoard/Inc/SystemCommands.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "CommandHandler.h"
1212

1313
enum class FFBoardMain_commands : uint32_t{
14-
help=0,save=1,reboot=2,dfu=3,swver=4,hwtype=5,lsmain,main,lsactive,format,errors,errorsclr,flashdump,flashraw,vint,vext,mallinfo,heapfree,taskstats,debug,minVerGui
14+
help=0,save=1,reboot=2,dfu=3,swver=4,hwtype=5,lsmain,main,lsactive,format,errors,errorsclr,flashdump,flashraw,vint,vext,mallinfo,heapfree,taskstats,debug,devid
1515
};
1616

1717
class SystemCommands : public CommandHandler {

Firmware/FFBoard/Src/AxesManager.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,3 @@ void AxesManager::resetPosZero() {
135135
axis->setPos(0);
136136
}
137137
}
138-
139-
140-
// ---- AXis Commands ----
141-
142-
//ParseStatus AxesManager::command(ParsedCommand* cmd,std::string* reply){
143-
// if (cmd->axis >= 0 && cmd->axis < axes.size()) {
144-
// return axes[cmd->axis]->command(cmd, reply);
145-
// }
146-
// return ParseStatus::NOT_FOUND;
147-
//}
148-
149-
//void AxesManager::processHidCommand(HID_Custom_Data_t *data){
150-
// uint8_t axis = (data->cmd >> 6) & 0x3;
151-
// if(axis<axis_count) {
152-
// return axes[axis]->processHidCommand(data);
153-
// }else{
154-
// return false;
155-
// }
156-
//}

Firmware/FFBoard/Src/SystemCommands.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ void SystemCommands::registerCommands(){
6565
#endif
6666
CommandHandler::registerCommand("format", FFBoardMain_commands::format, "set format=1 to erase all stored values",CMDFLAG_SET);
6767
CommandHandler::registerCommand("debug", FFBoardMain_commands::debug, "Enable or disable debug commands",CMDFLAG_SET | CMDFLAG_GET);
68+
CommandHandler::registerCommand("devid", FFBoardMain_commands::devid, "Get chip dev id and rev id",CMDFLAG_GET);
6869
}
6970

7071
CommandStatus SystemCommands::internalCommand(const ParsedCommand& cmd,std::vector<CommandReply>& replies,CommandInterface* interface){
@@ -184,8 +185,15 @@ CommandStatus SystemCommands::internalCommand(const ParsedCommand& cmd,std::vect
184185
case FFBoardMain_commands::hwtype:
185186
{
186187
replies.push_back(CommandReply(HW_TYPE,HW_TYPE_INT));
188+
//replies.push_back(CommandReply(HAL_GetDEVID()));
187189
break;
188190
}
191+
case FFBoardMain_commands::devid:
192+
{
193+
replies.push_back(CommandReply(HAL_GetDEVID(),HAL_GetREVID()));
194+
break;
195+
}
196+
189197

190198
case FFBoardMain_commands::mallinfo: // UNUSED since freertos
191199
{

Firmware/FFBoard/Src/cppmain.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ StaticTask_t usb_device_taskdef;
3333
RessourceManager ressourceManager = RessourceManager();
3434

3535
void cppmain() {
36+
#ifdef FW_DEVID
37+
if(HAL_GetDEVID() != FW_DEVID){
38+
/**
39+
* Firmware is not intended for this chip!
40+
* This can be caused by accidentially flashing an incorrect firmware file and likely screws up clock and pin configs
41+
* Do not proceed.
42+
*/
43+
while(true){ // Block forever to prevent an incorrect firmware from damaging hardware
44+
Error_Handler();
45+
}
46+
}
47+
#endif
48+
3649
// Flash init
3750
HAL_FLASH_Unlock();
3851
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

Firmware/FFBoard/UserExtensions/Inc/TMC4671.h

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,11 @@ struct TMC4671HardwareTypeConf{
7272
bool temperatureEnabled = false; // Enables temperature readings
7373
float temp_limit = 90;
7474
float currentScaler = 2.5 / (0x7fff * 60.0 * 0.0015); // Converts from adc counts to current in Amps
75-
75+
uint16_t brakeLimLow = 50700;
76+
uint16_t brakeLimHigh = 50900;
7677
// Todo restrict allowed motor and encoder types
7778
};
7879

79-
//#define TMCTEMP
80-
//TODO make this depend on the hw version
81-
//int thermistor_R2 = 1500, tmcOffset = 1000; //400? Offset is how many ADC counts too high does the tmc read due to current flowing from the ADC. Can be calculated by measuring the pin voltage with adc on and off.
82-
//
83-
//// Beta-Model
84-
//float thermistor_Beta = 4300, thermistor_R = 22000;
85-
//const float temp_limit = 100; //°C
8680

8781
// Mapping of bits in status flag register and mask
8882
union StatusFlags {
@@ -187,7 +181,7 @@ struct TMC4671FlashAddrs{
187181
};
188182

189183
struct TMC4671ABNConf{
190-
uint32_t cpr = 8192;
184+
uint32_t cpr = 10000;
191185
bool apol = true;
192186
bool bpol = true;
193187
bool npol = true;
@@ -255,7 +249,7 @@ class TMC4671 : public MotorDriver, public PersistentStorage, public Encoder, pu
255249
torqueP,torqueI,fluxP,fluxI,velocityP,velocityI,posP,posI,
256250
tmctype,pidPrec,phiesrc,fluxoffset,seqpi,tmcIscale,encdir,temp,reg
257251
};
258-
//pidprec "bit0=I bit1=P. 0=Q8.8 1= Q4.12";
252+
259253
public:
260254

261255
static ClassIdentifier info;
@@ -265,19 +259,13 @@ class TMC4671 : public MotorDriver, public PersistentStorage, public Encoder, pu
265259
static TMC4671MotConf decodeMotFromInt(uint16_t val);
266260
static uint16_t encodeMotToInt(TMC4671MotConf mconf);
267261

268-
// static uint8_t checkSpiAddrNotInUse(uint8_t requestedChannel);
269-
// void recordSpiAddrUsed(uint8_t chan);
270-
// static uint8_t getSpiAddrInUseForAxis(char axis);
271262

272-
//SPI_HandleTypeDef* spi = &HSPIDRV,GPIO_TypeDef* csport=SPI1_SS1_GPIO_Port,uint16_t cspin=SPI1_SS1_Pin
273-
//TMC4671(SPI_HandleTypeDef* spi,GPIO_TypeDef* csport,uint16_t cspin,TMC4671MainConfig conf);
274263
TMC4671(SPIPort& spiport,OutputPin cspin = OutputPin(*SPI1_SS1_GPIO_Port, SPI1_SS1_Pin),uint8_t address=1);
275264

276265
void setHwType(TMC_HW_Ver type);
277266

278267
void setAddress(uint8_t address);
279-
// void setAxis(char axis);
280-
// uint8_t getAxis();
268+
281269
uint8_t getSpiAddr();
282270
bool setSpiAddr(uint8_t chan);
283271
virtual ~TMC4671();

Firmware/FFBoard/UserExtensions/Src/FFBWheel.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ void FFBWheel::restoreFlash(){
8888
}
8989

9090
}
91-
// Saves parameters to flash
91+
/**
92+
* Save parameters to flash
93+
*/
9294
void FFBWheel::saveFlash(){
9395

9496
Flash_Write(ADR_FFBWHEEL_BUTTONCONF,this->btnsources);
@@ -151,9 +153,7 @@ void FFBWheel::update(){
151153
// Buttons
152154
void FFBWheel::clearBtnTypes(){
153155
// Destruct all button sources
154-
// for(ButtonSource* btn : this->btns){
155-
// delete btn;
156-
// }
156+
157157
this->btns.clear();
158158
}
159159

@@ -184,9 +184,7 @@ void FFBWheel::addBtnType(uint16_t id){
184184
// Analog
185185
void FFBWheel::clearAinTypes(){
186186
// Destruct all button sources
187-
// for(auto &ain : this->analog_inputs){
188-
// delete ain;
189-
// }
187+
190188
this->analog_inputs.clear();
191189
}
192190

Firmware/FFBoard/UserExtensions/Src/TMC4671.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ ClassIdentifier TMC4671::info = {
4949
TMC4671::TMC4671(SPIPort& spiport,OutputPin cspin,uint8_t address) :CommandHandler("tmc", CLSID_MOT_TMC0), SPIDevice{motor_spi,cspin},Thread("TMC", TMC_THREAD_MEM, TMC_THREAD_PRIO){
5050
setAddress(address);
5151
setInstance(address-1);
52-
// this->cspin = SPI1_SS1_Pin;
53-
// this->csport = SPI1_SS1_GPIO_Port;
54-
// this->spi = &HSPIDRV;
55-
5652
spiConfig.peripheral.Mode = SPI_MODE_MASTER;
5753
spiConfig.peripheral.Direction = SPI_DIRECTION_2LINES;
5854
spiConfig.peripheral.DataSize = SPI_DATASIZE_8BIT;
@@ -233,23 +229,22 @@ bool TMC4671::initialize(){
233229
return false;
234230
}
235231
// brake res failsafe.
236-
/*
237-
* Single ended input raw value
238-
* 0V = 0x7fff
239-
* 4.7k / (360k+4.7k) Divider on old board.
240-
* 1.5k / (71.5k+1.5k) 16.121 counts 60V new. 100V VM => 2V
241-
* 13106 counts/V input.
242-
*/
243-
if(oldTMCdetected){
244-
setBrakeLimits(52400,52800);
245-
}else{
246-
setBrakeLimits(50700,50900); // Activates around 60V as last resort failsave. Check offsets from tmc leakage. ~ 1.426V
247-
}
232+
// /*
233+
// * Single ended input raw value
234+
// * 0V = 0x7fff
235+
// * 4.7k / (360k+4.7k) Divider on old board.
236+
// * 1.5k / (71.5k+1.5k) 16.121 counts 60V new. 100V VM => 2V
237+
// * 13106 counts/V input.
238+
// */
239+
setBrakeLimits(this->conf.hwconf.brakeLimLow,this->conf.hwconf.brakeLimHigh); // update limit from previously loaded constants or defaults
248240

249241
// Status mask
250242
if(oldTMCdetected){
251243
setStatusMask(0); // ES Version status output is broken
252244
}else{
245+
/*
246+
* Enable adc clipping and pll errors
247+
*/
253248
statusMask.asInt = 0;
254249
statusMask.flags.adc_i_clipped = 1;
255250
statusMask.flags.not_PLL_locked = 1;
@@ -319,9 +314,10 @@ void TMC4671::Run(){
319314
if(tick - lastStatTime > 2000){ // Every 2s
320315
lastStatTime = tick;
321316
statusCheck();
322-
// Get enable input
323-
bool tmc_en = (readReg(0x76) >> 15) & 0x01;
324-
if(!tmc_en && active){ // Hardware emergency. If tmc does not reply the enable will still read false
317+
// Get enable input. If tmc does not reply the result will read 0 or 0xffffffff (not possible normally)
318+
uint32_t pins = readReg(0x76);
319+
bool tmc_en = ((pins >> 15) & 0x01) && pins != 0xffffffff;
320+
if(!tmc_en && active){ // Hardware emergency.
325321
this->estopTriggered = true;
326322
this->emergencyStop();
327323
changeState(TMC_ControlState::HardError);
@@ -1834,9 +1830,10 @@ void TMC4671::setHwType(TMC_HW_Ver type){
18341830
.temperatureEnabled = true,
18351831
.temp_limit = 90,
18361832
.currentScaler = 2.5 / (0x7fff * 0.1), // w. TMCS1100A2 sensor 100mV/A
1833+
.brakeLimLow = 50700,
1834+
.brakeLimHigh = 50900,
18371835
};
18381836
this->conf.hwconf = newHwConf;
1839-
setBrakeLimits(50700,50900);
18401837
break;
18411838
}
18421839
case TMC_HW_Ver::v1_2_1_LEM20:
@@ -1851,9 +1848,10 @@ void TMC4671::setHwType(TMC_HW_Ver type){
18511848
.temperatureEnabled = true,
18521849
.temp_limit = 90,
18531850
.currentScaler = 2.5 / (0x7fff * 0.04), // w. LEM 20 sensor 40mV/A
1851+
.brakeLimLow = 50700,
1852+
.brakeLimHigh = 50900
18541853
};
18551854
this->conf.hwconf = newHwConf;
1856-
setBrakeLimits(50700,50900);
18571855
break;
18581856
}
18591857
case TMC_HW_Ver::v1_2_1:
@@ -1868,9 +1866,10 @@ void TMC4671::setHwType(TMC_HW_Ver type){
18681866
.temperatureEnabled = true,
18691867
.temp_limit = 90,
18701868
.currentScaler = 2.5 / (0x7fff * 0.08), // w. LEM 10 sensor 80mV/A
1869+
.brakeLimLow = 50700,
1870+
.brakeLimHigh = 50900,
18711871
};
18721872
this->conf.hwconf = newHwConf;
1873-
setBrakeLimits(50700,50900);
18741873
break;
18751874
}
18761875

@@ -1885,10 +1884,11 @@ void TMC4671::setHwType(TMC_HW_Ver type){
18851884
.temperatureEnabled = true,
18861885
.temp_limit = 90,
18871886
.currentScaler = 2.5 / (0x7fff * 60.0 * 0.0015), // w. 60x 1.5mOhm sensor
1887+
.brakeLimLow = 50700,
1888+
.brakeLimHigh = 50900,
18881889
};
18891890
this->conf.hwconf = newHwConf;
1890-
setBrakeLimits(50700,50900); // Activates around 60V as last resort failsave. Check offsets from tmc leakage. ~ 1.426V
1891-
1891+
// Activates around 60V as last resort failsave. Check offsets from tmc leakage. ~ 1.426V
18921892
break;
18931893
}
18941894

@@ -1904,9 +1904,11 @@ void TMC4671::setHwType(TMC_HW_Ver type){
19041904
.temperatureEnabled = false,
19051905
.temp_limit = 90,
19061906
.currentScaler = 2.5 / (0x7fff * 60.0 * 0.0015), // w. 60x 1.5mOhm sensor
1907+
.brakeLimLow = 52400,
1908+
.brakeLimHigh = 52800,
19071909
};
19081910
this->conf.hwconf = newHwConf;
1909-
setBrakeLimits(52400,52800);
1911+
19101912
break;
19111913
}
19121914

@@ -1922,6 +1924,7 @@ void TMC4671::setHwType(TMC_HW_Ver type){
19221924
break;
19231925
}
19241926
}
1927+
setBrakeLimits(this->conf.hwconf.brakeLimLow,this->conf.hwconf.brakeLimHigh);
19251928
}
19261929

19271930
void TMC4671::registerCommands(){

Firmware/Targets/F407VG/Core/Inc/target_constants.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// Hardware name string
1616
#define HW_TYPE "F407VG"
1717
#define HW_TYPE_INT 2
18+
#define FW_DEVID 0x413 // F407
19+
1820
#include "main.h"
1921

2022
// Enabled features

Firmware/Targets/F411RE/Core/Inc/target_constants.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
// Hardware name string
1616
#define HW_TYPE "F411RE"
1717
#define HW_TYPE_INT 1
18+
#define FW_DEVID 0x431 // Firmware should run on this chip devid
19+
20+
1821
// Enabled features
1922

2023
// Main classes

0 commit comments

Comments
 (0)