2016-05-17 23:26:57 -03:00
|
|
|
#include "SPIUARTDriver.h"
|
2014-11-07 06:17:50 -04:00
|
|
|
|
2016-07-22 02:06:03 -03:00
|
|
|
#include <assert.h>
|
2014-11-07 06:17:50 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <cstdio>
|
2016-05-17 23:26:57 -03:00
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2016-10-05 12:56:34 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2014-11-07 06:17:50 -04:00
|
|
|
|
2016-07-22 02:06:03 -03:00
|
|
|
extern const AP_HAL::HAL &hal;
|
2014-11-07 06:17:50 -04:00
|
|
|
|
2016-07-22 02:06:03 -03:00
|
|
|
#ifdef SPIUART_DEBUG
|
|
|
|
#include <stdio.h>
|
2015-04-01 10:02:54 -03:00
|
|
|
#define debug(fmt, args ...) do {hal.console->printf("[SPIUARTDriver]: %s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## args); } while(0)
|
2014-11-07 06:17:50 -04:00
|
|
|
#define error(fmt, args ...) do {fprintf(stderr,"%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## args); } while(0)
|
|
|
|
#else
|
2016-05-17 23:26:57 -03:00
|
|
|
#define debug(fmt, args ...)
|
|
|
|
#define error(fmt, args ...)
|
2014-11-07 06:17:50 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace Linux;
|
|
|
|
|
2016-07-22 02:06:03 -03:00
|
|
|
SPIUARTDriver::SPIUARTDriver()
|
|
|
|
: UARTDriver(false)
|
2014-11-07 06:17:50 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-07-07 05:46:52 -03:00
|
|
|
void SPIUARTDriver::_begin(uint32_t b, uint16_t rxS, uint16_t txS)
|
2014-11-07 06:17:50 -04:00
|
|
|
{
|
2016-10-30 02:24:21 -03:00
|
|
|
if (device_path != nullptr) {
|
2016-07-22 02:06:03 -03:00
|
|
|
UARTDriver::begin(b, rxS, txS);
|
|
|
|
if (is_initialized()) {
|
2014-11-07 06:17:50 -04:00
|
|
|
_external = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 02:03:18 -03:00
|
|
|
if (!is_initialized()) {
|
|
|
|
_dev = hal.spi->get_device("ublox");
|
|
|
|
if (!_dev) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-11-07 06:17:50 -04:00
|
|
|
|
2016-07-22 02:06:03 -03:00
|
|
|
if (rxS < 1024) {
|
|
|
|
rxS = 2048;
|
|
|
|
}
|
|
|
|
if (txS < 1024) {
|
|
|
|
txS = 2048;
|
|
|
|
}
|
|
|
|
|
2016-07-29 21:40:56 -03:00
|
|
|
_readbuf.set_size(rxS);
|
|
|
|
_writebuf.set_size(txS);
|
2016-07-22 02:06:03 -03:00
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_buffer == nullptr) {
|
2016-07-22 02:06:03 -03:00
|
|
|
/* Do not allocate new buffer, if we're just changing speed */
|
2024-05-26 22:24:12 -03:00
|
|
|
_buffer = NEW_NOTHROW uint8_t[rxS];
|
2016-10-30 02:24:21 -03:00
|
|
|
if (_buffer == nullptr) {
|
2016-07-22 02:06:03 -03:00
|
|
|
hal.console->printf("Not enough memory\n");
|
2024-12-13 05:15:44 -04:00
|
|
|
AP_HAL::panic("Not enough memory");
|
2016-07-22 02:06:03 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (b) {
|
|
|
|
case 4000000U:
|
|
|
|
if (is_initialized()) {
|
|
|
|
/* Do not allow speed changes before device is initialized, because
|
2015-04-01 10:02:54 -03:00
|
|
|
* it can lead to misconfiguraration. Once the device is initialized,
|
|
|
|
* it's sage to update speed
|
|
|
|
*/
|
2016-07-22 02:06:03 -03:00
|
|
|
_dev->set_speed(AP_HAL::Device::SPEED_HIGH);
|
2024-05-06 22:42:24 -03:00
|
|
|
high_speed_set = true;
|
2016-07-22 02:06:03 -03:00
|
|
|
debug("Set higher SPI-frequency");
|
|
|
|
} else {
|
|
|
|
_dev->set_speed(AP_HAL::Device::SPEED_LOW);
|
2024-05-06 22:42:24 -03:00
|
|
|
high_speed_set = false;
|
2016-07-22 02:06:03 -03:00
|
|
|
debug("Set lower SPI-frequency");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
_dev->set_speed(AP_HAL::Device::SPEED_LOW);
|
2024-05-06 22:42:24 -03:00
|
|
|
high_speed_set = false;
|
2016-07-22 02:06:03 -03:00
|
|
|
debug("Set lower SPI-frequency");
|
|
|
|
debug("%s: wrong baudrate (%u) for SPI-driven device. setting default speed", __func__, b);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_initialised = true;
|
2014-11-07 06:17:50 -04:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
int SPIUARTDriver::_write_fd(const uint8_t *buf, uint16_t size)
|
2014-11-07 06:17:50 -04:00
|
|
|
{
|
|
|
|
if (_external) {
|
2015-10-20 18:13:25 -03:00
|
|
|
return UARTDriver::_write_fd(buf,size);
|
2016-05-17 23:26:57 -03:00
|
|
|
}
|
2014-11-07 06:17:50 -04:00
|
|
|
|
2016-07-22 02:06:03 -03:00
|
|
|
if (!_dev->get_semaphore()->take_nonblocking()) {
|
2014-11-07 06:17:50 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-22 01:50:41 -03:00
|
|
|
_dev->transfer_fullduplex(buf, _buffer, size);
|
2014-11-07 06:17:50 -04:00
|
|
|
|
2016-07-22 02:06:03 -03:00
|
|
|
_dev->get_semaphore()->give();
|
2015-02-03 11:41:41 -04:00
|
|
|
|
2014-11-07 06:17:50 -04:00
|
|
|
uint16_t ret = size;
|
|
|
|
|
|
|
|
/* Since all SPI-transactions are transfers we need update
|
2016-05-17 23:26:57 -03:00
|
|
|
* the _readbuf. I do believe there is a way to encapsulate
|
2014-11-07 06:17:50 -04:00
|
|
|
* this operation since it's the same as in the
|
2016-05-17 23:26:57 -03:00
|
|
|
* UARTDriver::write().
|
2014-11-07 06:17:50 -04:00
|
|
|
*/
|
2016-07-29 21:40:56 -03:00
|
|
|
_readbuf.write(_buffer, size);
|
2014-11-07 06:17:50 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
int SPIUARTDriver::_read_fd(uint8_t *buf, uint16_t n)
|
2014-11-07 06:17:50 -04:00
|
|
|
{
|
2016-10-05 12:58:49 -03:00
|
|
|
static uint8_t ff_stub[100] = {0xff};
|
|
|
|
|
2014-11-07 06:17:50 -04:00
|
|
|
if (_external) {
|
2015-10-20 18:13:25 -03:00
|
|
|
return UARTDriver::_read_fd(buf, n);
|
2014-11-07 06:17:50 -04:00
|
|
|
}
|
|
|
|
|
2015-02-02 14:40:55 -04:00
|
|
|
/* Make SPI transactions shorter. It can save SPI bus from keeping too
|
2016-05-17 23:26:57 -03:00
|
|
|
* long. It's essential for NavIO as MPU9250 is on the same bus and
|
2015-02-02 14:40:55 -04:00
|
|
|
* doesn't like to be waiting. Making transactions more frequent but shorter
|
|
|
|
* is a win.
|
2016-05-17 23:26:57 -03:00
|
|
|
*/
|
2016-10-05 12:56:34 -03:00
|
|
|
n = MIN(n, 100);
|
2015-02-02 14:40:55 -04:00
|
|
|
|
2016-07-22 02:06:03 -03:00
|
|
|
if (!_dev->get_semaphore()->take_nonblocking()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-07 06:17:50 -04:00
|
|
|
|
2016-07-22 02:06:03 -03:00
|
|
|
_dev->transfer_fullduplex(ff_stub, buf, n);
|
|
|
|
_dev->get_semaphore()->give();
|
2015-02-02 14:40:55 -04:00
|
|
|
|
2014-11-07 06:17:50 -04:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void SPIUARTDriver::_timer_tick(void)
|
2014-11-07 06:17:50 -04:00
|
|
|
{
|
|
|
|
if (_external) {
|
2015-10-20 18:13:25 -03:00
|
|
|
UARTDriver::_timer_tick();
|
2014-11-07 06:17:50 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-07-22 02:06:03 -03:00
|
|
|
|
2014-11-07 06:17:50 -04:00
|
|
|
/* lower the update rate */
|
2015-11-19 23:10:58 -04:00
|
|
|
if (AP_HAL::micros() - _last_update_timestamp < 10000) {
|
2014-11-07 06:17:50 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
UARTDriver::_timer_tick();
|
2014-11-07 06:17:50 -04:00
|
|
|
|
2015-11-19 23:10:58 -04:00
|
|
|
_last_update_timestamp = AP_HAL::micros();
|
2014-11-07 06:17:50 -04:00
|
|
|
}
|