Skip to content

Quickstart

Read a node's device type over CAN and start it: wire a transceiver, write a small port class, call three driver functions.

Datasheet sections this page relies on
  • CAN node: transceiver and controller wiring: Texas Instruments SN65HVD230, SN65HVD231, SN65HVD232 (SLOS346) O, 11.2 Typical Application, p. 26
  • NMT commands and states: christiansandberg python-canopen nmt.py master as downloaded, NMT commands and state codes, p. 1

All of them, with the driver API for each: Datasheet map.

1. Wire it

MCU Transceiver Notes
CAN TX D (TXD) The controller's transmit output
CAN RX R (RXD) The controller's receive input
3.3 V VCC 100 nF to GND close to the pin
GND GND, RS RS to GND selects high speed on the SN65HVD230

CANH and CANL go to the bus; the two ends of the bus carry a 120 Ω termination each (Hardware). Every node on the bus must run the same bit rate; nothing here sets it, the port does.

2. Add the driver

git submodule add https://github.com/kokeb-labs-drivers/driver-canopen-client.git external/driver-canopen-client

3. Write the port

my_port.hpp
class MyPort : public canopen_client::CanInterface<MyPort> {
public:
  // Bring the CAN controller up at 500 kbit/s, once; later calls return true at once.
  bool EnsureInitialized() noexcept {
    if (!ready_) {
      ready_ = platform_can_start(500'000U);
    }
    return ready_;
  }

  // Queue one frame: false when the controller refused it.
  bool Send(const canopen_client::CanFrame& frame) noexcept {
    return platform_can_send(frame.id, frame.rtr, frame.dlc, frame.data.data());
  }

  // Wait up to `timeout_us` for one 11-bit frame; 0 only looks.
  canopen_client::RxStatus Receive(canopen_client::CanFrame& frame, uint32_t timeout_us) noexcept {
    frame = canopen_client::CanFrame{};
    const int status =
        platform_can_receive(timeout_us, frame.id, frame.rtr, frame.dlc, frame.data.data());
    if (status == 0) {
      return canopen_client::RxStatus::Frame;
    }
    return (status == 1) ? canopen_client::RxStatus::Timeout : canopen_client::RxStatus::Error;
  }

  // A clock that never stops: every timeout in the driver is measured with it.
  uint32_t NowUs() noexcept {
    return platform_micros();
  }

private:
  bool ready_ = false;
};

4. Talk to a node

main.cpp
#include "canopen_client.hpp"

int read_node_and_start_it(MyPort& port, uint32_t& device_type) {
  canopen_client::Client<MyPort> client(port); // no I/O yet

  if (auto r = client.EnsureInitialized(); !r) { // 1. CAN controller up
    return static_cast<int>(r.error);            //    Bus: the port could not start it
  }
  const auto type = client.ReadDeviceType(5); //    2. SDO read of object 0x1000 from node 5
  if (!type) {
    return static_cast<int>(type.error); //    Timeout: nobody answered; SdoAbort: the node refused
  }
  device_type = type.value;
  if (auto r = client.SendNmt(canopen_client::NmtCommand::Start, 5);
      !r) { // 3. node 5 to Operational
    return static_cast<int>(r.error);
  }
  return 0;
}
  1. EnsureInitialized() starts the port once; nothing else starts it.
  2. ReadDeviceType() reads object 0x1000 from node 5 with an SDO upload and waits for the reply.
  3. SendNmt(Start, 5) moves the node to Operational. NMT has no reply: the node's next heartbeat shows the result.

No hardware yet? examples/host runs the same calls against a simulated bus.

Next: API guide.