42 #include <px4_platform_common/px4_config.h> 43 #include <px4_platform_common/getopt.h> 44 #include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp> 111 class BST :
public device::I2C,
public px4::ScheduledWorkItem
122 virtual int info() {
return 0; }
128 static void start_trampoline(
void *arg);
132 bool _should_run =
false;
133 unsigned _interval = 100;
143 template <
typename T>
146 packet.
length =
sizeof(packet) - 1;
147 packet.
crc =
crc8(reinterpret_cast<uint8_t *>(&packet.
type),
sizeof(packet) - 2);
149 transfer(reinterpret_cast<uint8_t *>(&packet),
sizeof(packet),
nullptr, 0);
152 template <
typename T_SEND,
typename T_RECV>
155 packet_send.
length =
sizeof(packet_send) - 1;
156 packet_send.
crc =
crc8(reinterpret_cast<uint8_t *>(&packet_send.
type),
sizeof(packet_send) - 2);
157 transfer(reinterpret_cast<uint8_t *>(&packet_send),
sizeof(packet_send), reinterpret_cast<uint8_t *>(&packet_recv),
158 sizeof(packet_recv));
161 static uint8_t
crc8(uint8_t *
data,
size_t len);
166 return (val << 8) | (val >> 8);
172 return (val << 8) | ((val >> 8) & 0xFF);
178 val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
179 return (val << 16) | (val >> 16);
185 val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
186 return (val << 16) | ((val >> 16) & 0xFFFF);
193 I2C(
"bst", nullptr, bus,
BST_ADDR, 100000),
194 ScheduledWorkItem(MODULE_NAME,
px4::device_bus_to_wq(get_device_id()))
207 int retries_prev = _retries;
211 dev_info_req.
type = 0x0A;
216 if (dev_info_reply.
type != 0x05) {
217 warnx(
"no devices found");
221 uint8_t *reply_raw =
reinterpret_cast<uint8_t *
>(&dev_info_reply);
222 uint8_t crc_calc =
crc8(reinterpret_cast<uint8_t *>(&dev_info_reply.
type), dev_info_reply.
length - 1);
223 uint8_t crc_recv = reply_raw[dev_info_reply.
length];
225 if (crc_recv != crc_calc) {
226 warnx(
"CRC error: got %02x, should be %02x", (
int)crc_recv, (
int)crc_calc);
231 warnx(
"device info: hardware ID: 0x%08X, firmware ID: 0x%04X, device name: %s",
235 _retries = retries_prev;
257 set_device_address(0x00);
281 bst_batt.
type = 0x08;
321 for (
int i = 0; i < 8; i++) {
322 crc = crc & 0x80 ? (crc << 1) ^ 0xD5 : crc << 1;
340 if (!strcmp(argv[1],
"start")) {
343 warnx(
"already running");
347 g_bst =
new BST(PX4_I2C_BUS_EXPANSION);
357 if (!strcmp(argv[1],
"stop")) {
359 warnx(
"not running");
370 if (!strcmp(argv[1],
"status")) {
375 warnx(
"is not running");
__EXPORT int bst_main(int argc, char *argv[])
uORB::Subscription _battery_sub
uORB::Subscription _attitude_sub
virtual int ioctl(device::file_t *filp, int cmd, unsigned long arg)
int32_t swap_int32(int32_t val)
Byte swap int.
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
static const char commandline_usage[]
void init()
Activates/configures the hardware registers.
uORB::Subscription _gps_sub
uint32_t swap_uint32(uint32_t val)
Byte swap unsigned int.
int16_t swap_int16(int16_t val)
Byte swap short.
static uint8_t crc8(uint8_t *p, uint8_t len)
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
bool updated()
Check if there is a new update.
void send_packet(BSTPacket< T > &packet)
bool copy(void *dst)
Copy the struct.
uint16_t swap_uint16(uint16_t val)
Byte swap unsigned short.
static uint8_t crc8(uint8_t *data, size_t len)
void send_packet(BSTPacket< T_SEND > &packet_send, BSTPacket< T_RECV > &packet_recv)
Base class for devices connected via I2C.