2018-01-05 02:19:51 -04: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 Andrew Tridgell and Siddharth Bharat Purohit
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "AP_HAL_ChibiOS.h"
|
|
|
|
|
2018-08-29 10:14:12 -03:00
|
|
|
#if STM32_DMA_ADVANCED
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
#define SHARED_DMA_MAX_STREAM_ID (8*2)
|
|
|
|
|
|
|
|
// DMA stream ID for stream_id2 when only one is needed
|
|
|
|
#define SHARED_DMA_NONE 255
|
|
|
|
|
2018-01-13 00:02:05 -04:00
|
|
|
class ChibiOS::Shared_DMA
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
public:
|
2018-03-14 03:06:30 -03:00
|
|
|
FUNCTOR_TYPEDEF(dma_allocate_fn_t, void, Shared_DMA *);
|
|
|
|
FUNCTOR_TYPEDEF(dma_deallocate_fn_t, void, Shared_DMA *);
|
2018-01-05 02:19:51 -04:00
|
|
|
|
|
|
|
// the use of two stream IDs is for support of peripherals that
|
|
|
|
// need both a RX and TX DMA channel
|
|
|
|
Shared_DMA(uint8_t stream_id1, uint8_t stream_id2,
|
|
|
|
dma_allocate_fn_t allocate,
|
|
|
|
dma_allocate_fn_t deallocate);
|
|
|
|
|
|
|
|
// initialise the stream locks
|
|
|
|
static void init(void);
|
|
|
|
|
|
|
|
// blocking lock call
|
|
|
|
void lock(void);
|
|
|
|
|
2018-03-02 06:34:57 -04:00
|
|
|
// non-blocking lock call
|
|
|
|
bool lock_nonblock(void);
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
// unlock call. The DMA channel will not be immediately
|
|
|
|
// deallocated. Instead it will be deallocated if another driver
|
|
|
|
// needs it
|
|
|
|
void unlock(void);
|
|
|
|
|
|
|
|
// unlock call from an IRQ
|
|
|
|
void unlock_from_IRQ(void);
|
|
|
|
|
2018-02-05 23:59:10 -04:00
|
|
|
// unlock call from a chSysLock zone
|
|
|
|
void unlock_from_lockzone(void);
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
//should be called inside the destructor of Shared DMA participants
|
|
|
|
void unregister(void);
|
|
|
|
|
2018-03-14 05:50:32 -03:00
|
|
|
// return true if this DMA channel is being actively contended for
|
|
|
|
// by multiple drivers
|
|
|
|
bool has_contention(void) const { return contention; }
|
|
|
|
|
2018-01-28 21:43:55 -04:00
|
|
|
// lock all shared DMA channels. Used on reboot
|
|
|
|
static void lock_all(void);
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
private:
|
|
|
|
dma_allocate_fn_t allocate;
|
|
|
|
dma_allocate_fn_t deallocate;
|
|
|
|
uint8_t stream_id1;
|
|
|
|
uint8_t stream_id2;
|
2018-02-05 23:59:10 -04:00
|
|
|
bool have_lock;
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-03-14 05:50:32 -03:00
|
|
|
// we set the contention flag if two drivers are fighting over a DMA channel.
|
|
|
|
// the UART driver uses this to change its max transmit size to reduce latency
|
|
|
|
bool contention;
|
|
|
|
|
2018-03-02 06:34:57 -04:00
|
|
|
// core of lock call, after semaphores gained
|
|
|
|
void lock_core(void);
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
static struct dma_lock {
|
|
|
|
// semaphore to ensure only one peripheral uses a DMA channel at a time
|
2018-03-28 23:07:50 -03:00
|
|
|
#if CH_CFG_USE_SEMAPHORES == TRUE
|
2018-01-05 02:19:51 -04:00
|
|
|
binary_semaphore_t semaphore;
|
2018-03-28 23:07:50 -03:00
|
|
|
#endif // CH_CFG_USE_SEMAPHORES
|
2018-01-05 02:19:51 -04:00
|
|
|
|
|
|
|
// a de-allocation function that is called to release an existing user
|
|
|
|
dma_deallocate_fn_t deallocate;
|
|
|
|
|
|
|
|
// point to object that holds the allocation, if allocated
|
|
|
|
Shared_DMA *obj;
|
|
|
|
} locks[SHARED_DMA_MAX_STREAM_ID];
|
|
|
|
};
|
2018-08-29 10:14:12 -03:00
|
|
|
#endif //#if STM32_DMA_ADVANCED
|
|
|
|
|
2018-03-28 23:07:50 -03:00
|
|
|
|