Skip to content

Commit 55dc093

Browse files
committed
Allow direct register access through API.
1 parent 78bd3c3 commit 55dc093

File tree

4 files changed

+179
-12
lines changed

4 files changed

+179
-12
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*!
2+
* \name directRegisterControl
3+
* \author Infineon Technologies AG
4+
* \copyright 2025 Infineon Technologies AG
5+
* \brief This example demonstrates how to control two motors via direct register setting
6+
* \details
7+
* If motors are connected to the TLE94112 on certain half bridges, than they can be controlled via direct register
8+
* setting which reduces greatly the overhead and allows fast motor controlling.
9+
* therefore the half bridges HB1 to HB4, HB5 to HB8 and HB9 to HB12 must be used together, either for controlling
10+
* two motors on one tuple (0.9 A), or one motor on one tuple (1.8 A).
11+
*
12+
* SPDX-License-Identifier: MIT
13+
*
14+
*/
15+
16+
#include <tle94112-ino.hpp>
17+
18+
//! Tle94112 Object
19+
Tle94112Ino controller = Tle94112Ino();
20+
21+
//! Tle94112 registers for motor 1 and 2
22+
uint8_t motor[2][2] = {
23+
{REG_ACT_1, REG_PWM_DC_1},
24+
{REG_ACT_3, REG_PWM_DC_3}
25+
};
26+
27+
//! Tle94112 register for motor direction
28+
volatile uint8_t oldDirection [] = { LL_HH, HH_LL };
29+
30+
/**
31+
* @brief Changes the motor direction and speed
32+
* The motor direction is set by the change of the High/Low switching of the motor half bridges in one register.
33+
* This needs some time to change the direction, so its avoided if not needed.
34+
* Setting the speed is done by setting the PWM register.
35+
*
36+
* @param motorNum uint8_t motor number (0 or 1)
37+
* @param dir uint8_t direction (HH_LL or LL_HH for forward/backward of one motor on four half bridges)
38+
* @param speed uint8_t speed (0-255)
39+
* @param errorCheck bool if true, the error register is cleared after setting the speed
40+
*/
41+
void motorSet(uint8_t motorNum, uint8_t dir, uint8_t speed, bool errorCheck = false)
42+
{
43+
if (dir != oldDirection[motorNum]) {
44+
controller.directWriteReg(motor[motorNum][0], dir);
45+
oldDirection[motorNum] = dir;
46+
}
47+
controller.directWriteReg(motor[motorNum][1], speed);
48+
if (errorCheck) {
49+
controller.clearErrors();
50+
}
51+
}
52+
53+
54+
//! connect motor between halfbridge 1 and halfbridge 2
55+
void setup()
56+
{
57+
58+
Serial.begin(9600);
59+
while (!Serial) {
60+
; // wait for serial port to connect. Needed for native USB port only
61+
}
62+
Serial.println("TLE94112 Motor Speed Control Example");
63+
delay(1000);
64+
65+
// Enable MotorController Tle94112
66+
// Note: Required to be done before starting to configure the motor
67+
controller.begin();
68+
69+
// four half bridges and two PWM channels are used to control two motors
70+
controller.configHB(controller.TLE_HB1, controller.TLE_HIGH, controller.TLE_PWM1);
71+
controller.configHB(controller.TLE_HB2, controller.TLE_HIGH, controller.TLE_PWM1);
72+
controller.configHB(controller.TLE_HB3, controller.TLE_LOW, controller.TLE_NOPWM);
73+
controller.configHB(controller.TLE_HB4, controller.TLE_LOW, controller.TLE_NOPWM);
74+
75+
controller.configHB(controller.TLE_HB9, controller.TLE_HIGH, controller.TLE_PWM3);
76+
controller.configHB(controller.TLE_HB10, controller.TLE_HIGH, controller.TLE_PWM3);
77+
controller.configHB(controller.TLE_HB11, controller.TLE_LOW, controller.TLE_NOPWM);
78+
controller.configHB(controller.TLE_HB12, controller.TLE_LOW, controller.TLE_NOPWM);
79+
80+
// all stop
81+
controller.configPWM(controller.TLE_PWM1, controller.TLE_FREQ200HZ, 0);
82+
controller.configPWM(controller.TLE_PWM3, controller.TLE_FREQ200HZ, 0);
83+
controller.clearErrors();
84+
85+
delay(1000);
86+
87+
}
88+
89+
90+
void loop() {
91+
controller.clearErrors();
92+
93+
Serial.println("forward / backward");
94+
motorSet(0, HH_LL, 100);
95+
motorSet(1, LL_HH, 100);
96+
delay(1000);
97+
98+
Serial.println(" speed up / down");
99+
motorSet(0, HH_LL, 255);
100+
motorSet(1, LL_HH, 50);
101+
delay(1000);
102+
103+
Serial.print("backward / forward ");
104+
motorSet(0, LL_HH, 255);
105+
motorSet(1, HH_LL, 255);
106+
delay(1000);
107+
108+
}

src/tle94112-types.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,40 @@ namespace tle94112
3030
READ_ERROR = -3, /**< Read error */
3131
WRITE_ERROR = -4, /**< Write error */
3232
};
33+
34+
#define REG_ACT_1 0x03
35+
#define REG_ACT_2 0x43
36+
#define REG_ACT_3 0x23
37+
38+
#define REG_MODE_1 0x63
39+
#define REG_MODE_2 0x13
40+
#define REG_MODE_3 0x53
41+
42+
#define REG_PWM_CH_FREQ 0x33
43+
44+
#define REG_PWM_DC_1 0x73
45+
#define REG_PWM_DC_2 0x0B
46+
#define REG_PWM_DC_3 0x4B
47+
48+
#define REG_SYS_DIAG 0x1B
49+
#define REG_ERR1 0x5B
50+
#define REG_ERR2 0x3B
51+
#define REG_ERR3 0x7B
52+
#define REG_ERR4 0x07
53+
#define REG_ERR5 0x47
54+
#define REG_ERR6 0x27
55+
56+
#define REG_FW_OL 0x2B
57+
#define REG_FW_CTRL 0x6B
58+
59+
#define HL_HL 0b10011001
60+
#define HL_LH 0b10010110
61+
#define LH_HL 0b01101001
62+
#define LH_LH 0b01100110
63+
64+
#define HH_LL 0b10100101
65+
#define LL_HH 0b01011010
66+
3367
/** @} */
3468

3569
/** @} */

src/tle94112.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,21 @@ void Tle94112::init(void)
301301
/*! \brief time in milliseconds to wait for chipselect signal raised */
302302
#define TLE94112_CS_RISETIME 2
303303

304+
void Tle94112::directWriteReg(uint8_t reg, uint8_t data)
305+
{
306+
TLE94112_LOG_MSG(__FUNCTION__);
307+
308+
uint8_t address = reg | TLE94112_CMD_WRITE;
309+
uint8_t byte0;
310+
uint8_t byte1;
311+
312+
cs->disable();
313+
sBus->transfer(address, byte0);
314+
sBus->transfer(data, byte1);
315+
cs->enable();
316+
timer->delayMilli(TLE94112_CS_RISETIME);
317+
}
318+
304319
void Tle94112::writeReg(uint8_t reg, uint8_t mask, uint8_t shift, uint8_t data)
305320
{
306321
uint8_t address = mCtrlRegAddresses[reg];

src/tle94112.hpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,28 @@ class Tle94112
227227
//! \brief clears all clearable error flags
228228
void clearErrors();
229229

230+
/*! \brief writes data bits directly to a register of the TLE94112
231+
*
232+
* \param reg address of the register to be written to.
233+
* \param data the data byte that has to be written.
234+
*
235+
* \see mCtrlRegAddresses
236+
* \see mCtrlRegData
237+
*/
238+
void directWriteReg(uint8_t reg, uint8_t data);
239+
240+
/*! \brief reads one byte from a status register of the TLE94112
241+
*
242+
* \param reg status register number(mapping array index / StatusRegisters constant) of the register
243+
*
244+
* \return data byte that has been read
245+
*
246+
* \see StatusRegisters
247+
* \see TLE94112_NUM_STATUS_REGS
248+
* \see mStatusRegAddresses
249+
*/
250+
uint8_t readStatusReg(uint8_t reg);
251+
230252
SPIC *sBus; //<! \brief SPI cover class as representation of the SPI bus
231253
GPIOC *cs; //<! \brief shield enable GPIO to switch chipselect on/off
232254
GPIOC *en; //<! \brief shield enable GPIO to switch shield on/off
@@ -363,18 +385,6 @@ class Tle94112
363385
*/
364386
void writeReg(uint8_t reg, uint8_t mask, uint8_t shift, uint8_t data);
365387

366-
/*! \brief reads one byte from a status register of the TLE94112
367-
*
368-
* \param reg status register number(mapping array index / StatusRegisters constant) of the register
369-
*
370-
* \return data byte that has been read
371-
*
372-
* \see StatusRegisters
373-
* \see TLE94112_NUM_STATUS_REGS
374-
* \see mStatusRegAddresses
375-
*/
376-
uint8_t readStatusReg(uint8_t reg);
377-
378388
/*! \brief reads some bits from a status register of the TLE94112
379389
*
380390
* \param reg status register number(mapping array index / StatusRegisters constant) of the register

0 commit comments

Comments
 (0)