blob: fec543830f57cc5ddbb83326bbd57171d06522a7 [file] [log] [blame]
// Copyright 2021 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*
* Access via MCP2221A device.
*/
#ifndef HPS_HAL_MCP_H_
#define HPS_HAL_MCP_H_
#include <memory>
#include <libusb-1.0/libusb.h>
#include "hps/dev.h"
namespace hps {
inline static constexpr int kMcpTransferSize = 64; // Transfer buffer size
class Mcp : public DevInterface {
public:
virtual ~Mcp();
void Close();
bool Read(uint8_t cmd, uint8_t* data, size_t len) override;
bool Write(uint8_t cmd, const uint8_t* data, size_t len) override;
size_t BlockSizeBytes() override;
static std::unique_ptr<DevInterface> Create(uint8_t address,
uint32_t speedKHz);
private:
explicit Mcp(uint8_t addr)
: address_(addr << 1), div_(0), context_(0), handle_(0) {}
bool Init(uint32_t speedKHz);
bool PrepareBus();
bool Cmd();
void Clear();
uint8_t address_;
uint8_t div_;
libusb_context* context_;
libusb_device_handle* handle_;
uint8_t in_[kMcpTransferSize];
uint8_t out_[kMcpTransferSize];
};
} // namespace hps
#endif // HPS_HAL_MCP_H_