AP_HAL_ChibiOS: add support for bank selection call outside SPI driver

This commit is contained in:
Siddharth Purohit 2020-07-30 19:35:25 +05:30 committed by Peter Barker
parent 4cdb4b74f3
commit 3ddd1d484a
2 changed files with 23 additions and 1 deletions

View File

@ -125,6 +125,7 @@ SPIDevice::SPIDevice(SPIBus &_bus, SPIDesc &_device_desc)
asprintf(&pname, "SPI:%s:%u:%u",
device_desc.name,
(unsigned)bus.bus, (unsigned)device_desc.device);
AP_HAL::SPIDevice::setup_bankselect_callback(device_desc.bank_select_cb);
//printf("SPI device %s on %u:%u at speed %u mode %u\n",
// device_desc.name,
// (unsigned)bus.bus, (unsigned)device_desc.device,

View File

@ -51,7 +51,8 @@ struct SPIDesc {
uint32_t _mode, uint32_t _lowspeed, uint32_t _highspeed)
: name(_name), bus(_bus), device(_device),
pal_line(_pal_line), mode(_mode),
lowspeed(_lowspeed), highspeed(_highspeed)
lowspeed(_lowspeed), highspeed(_highspeed),
bank_select_cb(nullptr)
{
}
@ -62,6 +63,7 @@ struct SPIDesc {
uint32_t mode;
uint32_t lowspeed;
uint32_t highspeed;
AP_HAL::Device::BankSelectCb bank_select_cb;
};
@ -82,6 +84,25 @@ public:
bool transfer_fullduplex(const uint8_t *send, uint8_t *recv,
uint32_t len) override;
/*
Links the bank select callback to the spi bus, so that even when
used outside of the driver bank selection can be done.
*/
void setup_bankselect_callback(BankSelectCb bank_select) override {
device_desc.bank_select_cb = bank_select;
AP_HAL::SPIDevice::setup_bankselect_callback(bank_select);
}
/*
Ensure to deregister bankselect callback in destructor of user
that could potentially be deleted. otherewise the orphaned functor
can be called causing memory corruption.
*/
void deregister_bankselect_callback() override {
device_desc.bank_select_cb = nullptr;
AP_HAL::SPIDevice::deregister_bankselect_callback();
}
/*
* send N bytes of clock pulses without taking CS. This is used
* when initialising microSD interfaces over SPI