2021-05-29 16:47:40 -03:00
|
|
|
/*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Code by
|
|
|
|
* Andy Piper
|
|
|
|
* Siddharth Bharat Purohit, Cubepilot Pty. Ltd.
|
|
|
|
*/
|
|
|
|
|
2022-02-20 23:44:56 -04:00
|
|
|
#include <hal.h>
|
2022-08-17 10:15:32 -03:00
|
|
|
#include "WSPIDevice.h"
|
2021-05-29 16:47:40 -03:00
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <AP_HAL/utility/OwnPtr.h>
|
|
|
|
#include <AP_InternalError/AP_InternalError.h>
|
|
|
|
#include "Util.h"
|
|
|
|
#include "Scheduler.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2022-08-17 10:15:32 -03:00
|
|
|
#if HAL_USE_WSPI == TRUE && defined(HAL_WSPI_DEVICE_LIST)
|
2021-05-29 16:47:40 -03:00
|
|
|
|
|
|
|
using namespace ChibiOS;
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2022-08-17 10:15:32 -03:00
|
|
|
static const struct WSPIDriverInfo {
|
2021-05-29 16:47:40 -03:00
|
|
|
WSPIDriver *driver;
|
|
|
|
uint8_t busid; // used for device IDs in parameters
|
2022-08-17 10:15:32 -03:00
|
|
|
} wspi_devices[] = { HAL_WSPI_BUS_LIST };
|
2021-05-29 16:47:40 -03:00
|
|
|
|
2022-08-17 10:15:32 -03:00
|
|
|
#define WSPIDEV_MODE1 0
|
2022-08-17 13:16:50 -03:00
|
|
|
#if HAL_USE_QUADSPI
|
2022-08-17 10:15:32 -03:00
|
|
|
#define WSPIDEV_MODE3 STM32_DCR_CK_MODE
|
2022-08-17 13:16:50 -03:00
|
|
|
#else
|
|
|
|
#define WSPIDEV_MODE3 STM32_DCR1_CK_MODE
|
|
|
|
#endif
|
2021-05-29 16:47:40 -03:00
|
|
|
|
|
|
|
// device list comes from hwdef.dat
|
2022-08-17 10:15:32 -03:00
|
|
|
WSPIDesc WSPIDeviceManager::device_table[] = { HAL_WSPI_DEVICE_LIST };
|
2021-05-29 16:47:40 -03:00
|
|
|
|
|
|
|
// Check clock sanity during runtime
|
2022-08-17 13:16:50 -03:00
|
|
|
#if defined(STM32_WSPI_USE_QUADSPI1) && STM32_WSPI_USE_QUADSPI1
|
2022-11-10 12:49:54 -04:00
|
|
|
#if (STM32_QSPICLK < HAL_QSPI1_CLK)
|
|
|
|
#error "Flash speed must not be greater than QSPI Clock"
|
|
|
|
#endif
|
2021-05-29 16:47:40 -03:00
|
|
|
#if (STM32_QSPICLK % HAL_QSPI1_CLK)
|
2022-11-10 12:49:54 -04:00
|
|
|
#warning "QSPI clock not an integer multiple of flash speed"
|
2021-05-29 16:47:40 -03:00
|
|
|
#endif
|
2022-08-17 13:16:50 -03:00
|
|
|
#endif
|
2021-05-29 16:47:40 -03:00
|
|
|
|
|
|
|
#if defined(STM32_WSPI_USE_QUADSPI2) && STM32_WSPI_USE_QUADSPI2
|
2022-11-10 12:49:54 -04:00
|
|
|
#if (STM32_QSPICLK < HAL_QSPI2_CLK)
|
|
|
|
#error "Flash speed must not be greater than QSPI Clock"
|
|
|
|
#endif
|
2021-05-29 16:47:40 -03:00
|
|
|
#if (STM32_QSPICLK % HAL_QSPI2_CLK)
|
2022-08-17 10:15:32 -03:00
|
|
|
#warning "WSPI clock not an integer multiple of flash speed"
|
2021-05-29 16:47:40 -03:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2022-08-17 13:16:50 -03:00
|
|
|
#if defined(STM32_WSPI_USE_OCTOSPI1) && STM32_WSPI_USE_OCTOSPI1
|
2023-03-29 16:22:49 -03:00
|
|
|
#if (STM32_OCTOSPICLK < HAL_OSPI1_CLK)
|
2022-08-17 13:16:50 -03:00
|
|
|
#error "Flash speed must not be greater than OSPI Clock"
|
|
|
|
#endif
|
2023-03-29 16:22:49 -03:00
|
|
|
#if (STM32_OCTOSPICLK % HAL_OSPI1_CLK)
|
2022-08-17 13:16:50 -03:00
|
|
|
#warning "OSPI clock not an integer multiple of flash speed"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(STM32_WSPI_USE_OCTOSPI2) && STM32_WSPI_USE_OCTOSPI2
|
2023-03-29 16:22:49 -03:00
|
|
|
#if (STM32_OCTOSPICLK < HAL_OSPI2_CLK)
|
2022-08-17 13:16:50 -03:00
|
|
|
#error "Flash speed must not be greater than OSPI Clock"
|
|
|
|
#endif
|
2023-03-29 16:22:49 -03:00
|
|
|
#if (STM32_OCTOSPICLK % HAL_OSPI2_CLK)
|
2022-08-17 13:16:50 -03:00
|
|
|
#warning "OSPI clock not an integer multiple of flash speed"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2022-08-31 11:25:33 -03:00
|
|
|
bool WSPIDevice::is_busy()
|
|
|
|
{
|
|
|
|
#if HAL_USE_OCTOSPI
|
|
|
|
return (wspi_devices[device_desc.bus].driver->ospi->SR & OCTOSPI_SR_BUSY) != 0U;
|
|
|
|
#else
|
|
|
|
return (wspi_devices[device_desc.bus].driver->qspi->SR & QUADSPI_SR_BUSY) != 0U;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-08-17 10:15:32 -03:00
|
|
|
bool WSPIDevice::transfer(const uint8_t *send, uint32_t send_len,
|
2021-05-29 16:47:40 -03:00
|
|
|
uint8_t *recv, uint32_t recv_len)
|
|
|
|
{
|
|
|
|
if (!acquire_bus(true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!bus.bouncebuffer_setup(send, send_len, recv, recv_len)) {
|
|
|
|
acquire_bus(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool ret = true;
|
|
|
|
if (send_len == 0 && recv_len == 0) {
|
|
|
|
// This is just a command
|
2022-08-17 10:15:32 -03:00
|
|
|
ret = !wspiCommand(wspi_devices[device_desc.bus].driver, &mode);
|
2021-05-29 16:47:40 -03:00
|
|
|
} else if (send_len > 0 && recv == 0) {
|
|
|
|
// This is a send cmd
|
2022-08-17 10:15:32 -03:00
|
|
|
ret = !wspiSend(wspi_devices[device_desc.bus].driver, &mode, send_len, send);
|
2021-05-29 16:47:40 -03:00
|
|
|
} else if (send_len == 0 && recv_len >= 1) {
|
|
|
|
// This is a receive cmd,
|
|
|
|
// we only consume first byte of send
|
2022-08-17 10:15:32 -03:00
|
|
|
ret = !wspiReceive(wspi_devices[device_desc.bus].driver, &mode, recv_len, recv);
|
2021-05-29 16:47:40 -03:00
|
|
|
} else {
|
|
|
|
// Can't handle this transaction type
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
bus.bouncebuffer_finish(send, recv, recv_len);
|
|
|
|
acquire_bus(false);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-08-17 10:15:32 -03:00
|
|
|
void WSPIDevice::set_cmd_header(const CommandHeader& cmd_hdr)
|
2021-05-29 16:47:40 -03:00
|
|
|
{
|
2021-06-08 04:43:55 -03:00
|
|
|
mode.cmd = cmd_hdr.cmd;
|
|
|
|
mode.cfg = cmd_hdr.cfg;
|
|
|
|
mode.addr = cmd_hdr.addr;
|
|
|
|
mode.alt = cmd_hdr.alt;
|
|
|
|
mode.dummy = cmd_hdr.dummy;
|
2021-05-29 16:47:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-17 10:15:32 -03:00
|
|
|
bool WSPIDevice::acquire_bus(bool acquire)
|
2021-05-29 16:47:40 -03:00
|
|
|
{
|
|
|
|
if (!bus.semaphore.check_owner()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (acquire) {
|
2022-08-17 10:15:32 -03:00
|
|
|
wspiAcquireBus(wspi_devices[device_desc.bus].driver);
|
|
|
|
if (wspi_devices[device_desc.bus].driver->config != &bus.wspicfg) {
|
2021-05-29 16:47:40 -03:00
|
|
|
// Initialise and Start WSPI driver
|
|
|
|
bus.wspicfg.end_cb = nullptr;
|
|
|
|
bus.wspicfg.error_cb = nullptr;
|
2022-08-17 13:16:50 -03:00
|
|
|
#if HAL_USE_QUADSPI
|
2021-05-29 16:47:40 -03:00
|
|
|
bus.wspicfg.dcr = STM32_DCR_FSIZE(device_desc.size_pow2) |
|
|
|
|
STM32_DCR_CSHT(device_desc.ncs_clk_delay - 1) |
|
|
|
|
device_desc.mode;
|
2022-08-17 13:16:50 -03:00
|
|
|
#else
|
|
|
|
bus.wspicfg.dcr1 = STM32_DCR1_DEVSIZE(device_desc.size_pow2) |
|
|
|
|
STM32_DCR1_CSHT(device_desc.ncs_clk_delay - 1) |
|
|
|
|
device_desc.mode;
|
|
|
|
#endif
|
2022-08-17 10:15:32 -03:00
|
|
|
wspiStart(wspi_devices[device_desc.bus].driver, &bus.wspicfg);
|
2021-05-29 16:47:40 -03:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-17 10:15:32 -03:00
|
|
|
wspiReleaseBus(wspi_devices[device_desc.bus].driver);
|
2021-05-29 16:47:40 -03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-08 04:43:55 -03:00
|
|
|
|
|
|
|
// Enters Memory mapped or eXecution In Place or 0-4-4 mode
|
2022-08-17 10:15:32 -03:00
|
|
|
bool WSPIDevice::enter_xip_mode(void** map_ptr)
|
2021-06-08 04:43:55 -03:00
|
|
|
{
|
|
|
|
if (!acquire_bus(true)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-17 10:15:32 -03:00
|
|
|
wspiMapFlash(wspi_devices[device_desc.bus].driver, &mode, (uint8_t**)map_ptr);
|
2021-06-08 04:43:55 -03:00
|
|
|
acquire_bus(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-17 10:15:32 -03:00
|
|
|
bool WSPIDevice::exit_xip_mode()
|
2021-06-08 04:43:55 -03:00
|
|
|
{
|
|
|
|
if (!acquire_bus(true)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-08-17 10:15:32 -03:00
|
|
|
wspiUnmapFlash(wspi_devices[device_desc.bus].driver);
|
2021-06-08 04:43:55 -03:00
|
|
|
acquire_bus(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-29 16:47:40 -03:00
|
|
|
/*
|
|
|
|
return a SPIDevice given a string device name
|
|
|
|
*/
|
2022-08-17 10:15:32 -03:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::WSPIDevice>
|
|
|
|
WSPIDeviceManager::get_device(const char *name)
|
2021-05-29 16:47:40 -03:00
|
|
|
{
|
|
|
|
/* Find the bus description in the table */
|
|
|
|
uint8_t i;
|
|
|
|
for (i = 0; i<ARRAY_SIZE(device_table); i++) {
|
|
|
|
if (strcmp(device_table[i].name, name) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == ARRAY_SIZE(device_table)) {
|
2022-08-17 10:15:32 -03:00
|
|
|
return AP_HAL::OwnPtr<AP_HAL::WSPIDevice>(nullptr);
|
2021-05-29 16:47:40 -03:00
|
|
|
}
|
|
|
|
|
2022-08-17 10:15:32 -03:00
|
|
|
WSPIDesc &desc = device_table[i];
|
2021-05-29 16:47:40 -03:00
|
|
|
|
|
|
|
// find the bus
|
2022-08-17 10:15:32 -03:00
|
|
|
WSPIBus *busp;
|
|
|
|
for (busp = buses; busp; busp = (WSPIBus *)busp->next) {
|
2021-05-29 16:47:40 -03:00
|
|
|
if (busp->bus == desc.bus) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (busp == nullptr) {
|
|
|
|
// create a new one
|
2022-08-17 10:15:32 -03:00
|
|
|
busp = new WSPIBus(desc.bus);
|
2021-05-29 16:47:40 -03:00
|
|
|
if (busp == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
busp->next = buses;
|
|
|
|
busp->bus = desc.bus;
|
|
|
|
|
|
|
|
buses = busp;
|
|
|
|
}
|
|
|
|
|
2022-08-17 10:15:32 -03:00
|
|
|
return AP_HAL::OwnPtr<AP_HAL::WSPIDevice>(new WSPIDevice(*busp, desc));
|
2021-05-29 16:47:40 -03:00
|
|
|
}
|
|
|
|
|
2022-08-17 10:15:32 -03:00
|
|
|
#endif // #if HAL_USE_WSPI == TRUE && defined(HAL_QPI_DEVICE_LIST)
|