Skip to content
Snippets Groups Projects

Hardware

Board DC Motor Driver Hat DFRobot DFR0592

Microcontroller STM32F103C8T6 (medium density)

Useful onboard peripherals

Peripheral N
Flash 64kiB
RAM 20kiB
Timers (general purpose) 3
Timers (advanced control) 1
CPU frequency 72MHz
I2C 2

The I2C2 is used (PB10=/=SCL, PB11=/=SDA) to communicate with the TB6612FNG motor driver.

Flash-memory organization

  • Main memory: 64 pages of 1KiB
  • Page erasable
  • Programmable by half-words
  • VTOR must be aligned with the number of exception entries or 128 bits, whichever is largest. To use all DMA channels (DMA2_Channel4_5 is at position 59), an alignment of 256 is required. In any case, aligning it on a page boundary will work.

Driver IC TB6612FNG for dual DC motor

Encoders FIT0450

Led pattern

The led pattern is in Morse code.

Code Meaning Pattern
I Regular mode ..
LP Low power (Vdd is below 2.9V) .-.. .–.
WW Last reset was due to the window watchdog trigerring .– .–

Motor controller command set

I²C address: 0x57

I²C uses clock stretching, so care must be taken when using a Raspberry Pi whose SOC is bogus. Using 400kHz instead of the default 100kHz seems to mask the problem if not outputting too much debug information. Using a software I²C solves the issue.

Multibyte values are exchanged in little endian format.

[IMPLEMENTED] 0x08 Firmware version (R)

  • Return three bytes containing the major, minor and patch version numbers derived from Cargo.toml.

[IMPLEMENTED] 0x0F Who am I? (R)

  • The I²C address

0x10 [IMPLEMENTED] PWM frequency (R/W)

  • Frequency in Hz on 3 bytes, from 1 to 100_000 (default: 10_000)

[IMPLEMENTED] 0x11 Max motor percentage (R/W)

  • Limit the maximum speed, between 1% and 100% of the duty cycle (default: 100)

0x14 Encoder to motor reduction (R/W)

  • Between 1 and 255. 0 disable encoder control (default 0)

0x20-0x22 PID coefficients (R/W)

  • P, I, and D between -327.68 and 327.67. Default values are P=1.0, I=0.0, D=0.0
  • In order, each on 2 bytes, signed: 100×P, 100×I, 100×D
  • Result is capped by the maximum motor value (max values for the motor)
  • Coefficients are to be sent separately

0x26 Ramp time (R/W)

  • In automatic motor distance, time in 100th of seconds to reach the maximum speed or to decrease the speed from the maximum to 0.

[IMPLEMENTED] 0x28 Motor shutdown timeout (R/W)

  • Number of tenths of seconds in before the motor shut down. The minimum is 1 (0.1 seconds) and the maximum is 100 (10 seconds).

[IMPLEMENTED] 0x30 Motor speed (R/W)

  • Left and right motor speed, between -127 and 127 each, max speed is ±127. -128 means standby. ±127 corresponds to the value limited by the max motor percentage.
  • This is the raw speed sent to the motor, no PID is computed.
  • If written while in automatic mode, the robot switches to manual mode

[IMPLEMENTED] 0x32 Encoder ticks (R)

  • Left and right encoder counters since last time, 2 complement, 2 bytes each

[IMPLEMENTED] 0x36 Status (R)

  • Bit 0: moving (1) or idle (0)

0x37-0x3A Automatic motor distance (R/W) [optional]

  • Left and right distance in number of encoder ticks, between -32768 and 32767 each.
  • Reading this value gives the distance to the target in ticks.
  • If left and right distances are not equal, they will be kept proportional as much as possible in order to progress the same way.

0x40-0x43 Music (W) [optional]

  • For each motor, left then right, frequency in Hz on 1 byte and duration in 10th of seconds on 1 byte. This cannot be used if the motor is moving.

[IMPLEMENTED] 0xE0 Reset (W)

  • Reset the device. Used mainly for testing.

[IMPLEMENTED] 0xE1 Reset to bootloader (W)

  • Reset the device in bootloader mode. Used when reprogramming.

[IMPLEMENTED] 0xF0 Unique device ID (R)

  • Unique device ID as found in addresses 0x1FFFF7E8-0x1FFFF7EF

0xFE Firmware capabilities (R)

  • Bit 0: automatic motor mode is activated
  • Bit 1: music feature is activated
  • Bit 2: bootloader feature is activated

0xFF Special features (R/W)

Special features can be activated or deactivated by writing a variable-length byte sequence in one I²C write operation (this address does not auto-increment).

  • [C0 FF EE 01]: enter I²C bootloader (if enabled)
  • [FE FF 10 03]: reset the board (always enabled even after a failure)

The following features can only be used 10 seconds after the board has been reset. In case of a failed command, no other of those feature can be used until the next reset.

  • [AB 07 XX XX XX XX XX]: enable automatic motor mode
  • [BA 71 XX XX XX XX XX]: enable music feature
  • [90 70 XX XX XX XX XX]: enable bootloader

XX values are chose at compile time and the board owner may chose not be publicize them. One value can be tested every 10 seconds at most, needing a mean time of ~174 years to randomly find the right value.

**Note:** those could be dependent on the unique device ID in order to be able to selectively enable those features.

The result of a command can be read as one byte at this address unless the feature executes an alternate firmware.

  • 0x00: success
  • 0x01: failure
  • 0x02: command executed too soon
  • 0x03: features locked because of previous failure

Bootloader command set

If a firmware is present, the bootloader will start it unless the firmware has explicitly chosen to reboot in bootloader mode.

I²C address: 0x58

The note about clock stretching also applies for the bootloader.

[IMPLEMENTED] 0x08 Firmware version (R)

  • Return three bytes containing the major, minor and patch version numbers derived from Cargo.toml.

[IMPLEMENTED] 0x0F Who am I? (R)

  • The I²C address

[IMPLEMENTED] 0x10 Status (R)

Returns two bytes:

  • One set of flags ORed together:
    • 0x01: system is in programming mode
    • 0x02: at least one error was detected since the last time the system has been put in programming mode
    • 0x04: a program is currently present in flash
  • A XORed version of all data received so far since the programming address was last set, either by entering programming mode, or by using the SET PROGRAMMING ADDRESS command.

[IMPLEMENTED] 0x20 Change programming mode (W)

  • 0: leave programming mode without marking the application as succesfully written
  • 1: leave programming mode and mark the application as succesfully written
  • [0x17, 0x27, 0x65, 0x40]: enter programming mode

[IMPLEMENTED] 0x24 Erase pages (W) [PROGRAMMING MODE]

  • First byte: index of the first 1kB page in the application area
  • Second byte: number of pages to erase (up to the end of the application area)
  • Third byte: XOR of the first two bytes, as a safety

The memory will be erased. The active programming address is then set to the beginning of the erased area.

Even if the last page of the application does not belong to the erased page set, it will be erased with the first erase request of the current session (or after having being set again) to ensure that the application is no longer considered valid.

Designating address outside the application area, or having a wrong checksum, will be an error.

[IMPLEMENTED] 0x28 Set programming address (W) [PROGRAMMING MODE]

  • 4 bytes containing the next address to program, relative to the application start
  • One byte XORing the previous ones

[IMPLEMENTED] 0x2c Program data (W) [PROGRAMMING MODE]

  • 2, 4, 6, or 8 data bytes
  • One byte XORing the previous ones

In case of error, the bootloader leaves programming mode immediately.

The programming address will advance with the right number of bytes.

[IMPLEMENTED] 0x30 Read memory (R) [PROGRAMMING MODE]

  • Read 8 consecutive bytes in memory from the programming address (relative to the applicatin start), and advance it by 8.

0 will be returned outside the application memory area.

[IMPLEMENTED] 0x34 Set checksum (W) [PROGRAMMING_MODE]

  • Set the checksum value to the given byte.

[IMPLEMENTED] 0xE0 Reset (W)

  • Reset the bootloader in automatic mode

[IMPLEMENTED] 0xE1 Reset to the bootloader (W)

  • Reset the bootloader in bootloader mode

[IMPLEMENTED] 0xE2 Reset to the application (W)

  • Reset the bootloader in application mode

[IMPLEMENTED] 0xF0-0xF7 Unique device ID (R)

  • Unique device ID as found in addresses 0x1FFFF7E8-0x1FFFF7EF