c code refactor
This commit is contained in:
parent
9924e704db
commit
366d0f991d
4 changed files with 50 additions and 50 deletions
|
@ -23,8 +23,8 @@ class LL_CrcHandler : public Singleton<LL_CrcHandler>
|
||||||
public:
|
public:
|
||||||
struct ICallback
|
struct ICallback
|
||||||
{
|
{
|
||||||
virtual void CrcSucceeded(uintptr_t callbackParam, uint32_t crc, int prio) = 0;
|
virtual void CrcSucceeded(uintptr_t callbackParam, uint32_t crc, uint8_t task) = 0;
|
||||||
virtual void CrcFailed(uintptr_t callbackParam, uint32_t crc, int prio) = 0;
|
virtual void CrcFailed(uintptr_t callbackParam, uint32_t crc, uint8_t task) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SlotBase
|
class SlotBase
|
||||||
|
@ -41,6 +41,7 @@ public:
|
||||||
private:
|
private:
|
||||||
SlotBase *m_next = nullptr;
|
SlotBase *m_next = nullptr;
|
||||||
uint8_t m_taskCount;
|
uint8_t m_taskCount;
|
||||||
|
|
||||||
virtual CrcTask& operator[](int index) = 0;
|
virtual CrcTask& operator[](int index) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -49,7 +50,6 @@ public:
|
||||||
SlotBase(SlotBase const &other) = delete;
|
SlotBase(SlotBase const &other) = delete;
|
||||||
};
|
};
|
||||||
|
|
||||||
// DON't try this at home! we "extend" LL_CrcHandler::m_tasks this way
|
|
||||||
template <uint8_t n> class Slot : public SlotBase
|
template <uint8_t n> class Slot : public SlotBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -61,12 +61,12 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
void AttachSlot(SlotBase &slot);
|
void AttachSlot(SlotBase &slot);
|
||||||
bool Enqueue(SlotBase &slot, uint8_t prio, void const *address, uint16_t len, ICallback *cb, uintptr_t cbParam);
|
bool Enqueue(SlotBase &slot, uint8_t task, void const *address, uint16_t len, ICallback *cb, uintptr_t cbParam);
|
||||||
uint32_t Compute(SlotBase &slot, uint8_t prio, void const *address, uint16_t len);
|
uint32_t Compute(SlotBase &slot, uint8_t task, void const *address, uint16_t len);
|
||||||
|
|
||||||
bool IsActive(SlotBase &slot, uint8_t prio) const;
|
bool IsActive(SlotBase &slot, uint8_t task) const;
|
||||||
bool IsQueued(SlotBase &slot, uint8_t prio) const;
|
bool IsQueued(SlotBase &slot, uint8_t task) const;
|
||||||
bool IsRunning(SlotBase &slot, uint8_t prio) const;
|
bool IsRunning(SlotBase &slot, uint8_t task) const;
|
||||||
|
|
||||||
void DmaTransferCompleted(void);
|
void DmaTransferCompleted(void);
|
||||||
|
|
||||||
|
@ -75,12 +75,12 @@ private:
|
||||||
|
|
||||||
friend void ::_HandleCrcDmaIrq(void);
|
friend void ::_HandleCrcDmaIrq(void);
|
||||||
void StartNextTask(void);
|
void StartNextTask(void);
|
||||||
void WaitResults(SlotBase &slot, uint8_t prio) const;
|
void WaitResults(SlotBase &slot, uint8_t task) const;
|
||||||
|
|
||||||
LL_DmaHelper m_dma;
|
LL_DmaHelper m_dma;
|
||||||
SlotBase * volatile m_firstSlot = nullptr;
|
SlotBase * volatile m_firstSlot = nullptr;
|
||||||
SlotBase * volatile m_activeSlot = nullptr;
|
SlotBase * volatile m_activeSlot = nullptr;
|
||||||
int volatile m_activePrio;
|
int volatile m_activeTask;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -52,8 +52,8 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
// LL_CRCHandler::ICallback interface functions
|
// LL_CRCHandler::ICallback interface functions
|
||||||
virtual void CrcSucceeded(uintptr_t callbackParam, uint32_t crc, int prio);
|
virtual void CrcSucceeded(uintptr_t callbackParam, uint32_t crc, uint8_t task);
|
||||||
virtual void CrcFailed(uintptr_t callbackParam, uint32_t crc, int prio);
|
virtual void CrcFailed(uintptr_t callbackParam, uint32_t crc, uint8_t task);
|
||||||
|
|
||||||
// LL_UsartCore pure virtual function implementations
|
// LL_UsartCore pure virtual function implementations
|
||||||
virtual void ReceiverIdle(void);
|
virtual void ReceiverIdle(void);
|
||||||
|
|
|
@ -26,31 +26,31 @@ void LL_CrcHandler::AttachSlot(SlotBase &slot)
|
||||||
task.m_callback = nullptr;
|
task.m_callback = nullptr;
|
||||||
task.m_callbackParam = 0;
|
task.m_callbackParam = 0;
|
||||||
}
|
}
|
||||||
slot.m_next = m_firstSlot;
|
|
||||||
uint32_t prim = __get_PRIMASK();
|
uint32_t prim = __get_PRIMASK();
|
||||||
__disable_irq();
|
__disable_irq();
|
||||||
|
slot.m_next = m_firstSlot;
|
||||||
m_firstSlot = &slot;
|
m_firstSlot = &slot;
|
||||||
__set_PRIMASK(prim);
|
__set_PRIMASK(prim);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool LL_CrcHandler::Enqueue(SlotBase &slot, uint8_t prio, void const *address, uint16_t len, ICallback *cb, uintptr_t cbParam)
|
bool LL_CrcHandler::Enqueue(SlotBase &slot, uint8_t task, void const *address, uint16_t len, ICallback *cb, uintptr_t cbParam)
|
||||||
{
|
{
|
||||||
uint32_t prim = __get_PRIMASK();
|
uint32_t prim = __get_PRIMASK();
|
||||||
bool immediate;
|
bool immediate;
|
||||||
|
|
||||||
// TODO: do we need sanity check here? (is slot attached, is prio in range, etc...?)
|
// TODO: do we need sanity check here? (is slot attached, is task in range, etc...?)
|
||||||
|
|
||||||
while(IsActive(slot,prio));
|
while(IsActive(slot,task));
|
||||||
__disable_irq();
|
__disable_irq();
|
||||||
immediate = m_activeSlot == nullptr;
|
immediate = m_activeSlot == nullptr;
|
||||||
slot[prio].m_address = (!immediate) ? address : nullptr;
|
slot[task].m_address = (!immediate) ? address : nullptr;
|
||||||
slot[prio].m_wordCount = (len+3)/4;
|
slot[task].m_wordCount = (len+3)/4;
|
||||||
slot[prio].m_callback = cb;
|
slot[task].m_callback = cb;
|
||||||
slot[prio].m_callbackParam = cbParam;
|
slot[task].m_callbackParam = cbParam;
|
||||||
if(immediate) {
|
if(immediate) {
|
||||||
m_activeSlot = &slot;
|
m_activeSlot = &slot;
|
||||||
m_activePrio = prio;
|
m_activeTask = task;
|
||||||
}
|
}
|
||||||
__set_PRIMASK(prim);
|
__set_PRIMASK(prim);
|
||||||
|
|
||||||
|
@ -63,19 +63,19 @@ bool LL_CrcHandler::Enqueue(SlotBase &slot, uint8_t prio, void const *address, u
|
||||||
return immediate;
|
return immediate;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LL_CrcHandler::IsActive(SlotBase &slot, uint8_t prio) const
|
bool LL_CrcHandler::IsActive(SlotBase &slot, uint8_t task) const
|
||||||
{
|
{
|
||||||
return prio < slot.m_taskCount && slot[prio].m_wordCount != 0;
|
return task < slot.m_taskCount && slot[task].m_wordCount != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LL_CrcHandler::IsQueued(SlotBase &slot, uint8_t prio) const
|
bool LL_CrcHandler::IsQueued(SlotBase &slot, uint8_t task) const
|
||||||
{
|
{
|
||||||
return prio < slot.m_taskCount && slot[prio].m_address != nullptr;
|
return task < slot.m_taskCount && slot[task].m_address != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LL_CrcHandler::IsRunning(SlotBase &slot, uint8_t prio) const
|
bool LL_CrcHandler::IsRunning(SlotBase &slot, uint8_t task) const
|
||||||
{
|
{
|
||||||
return prio < slot.m_taskCount && slot[prio].m_wordCount && ! slot[prio].m_address;
|
return task < slot.m_taskCount && slot[task].m_wordCount && ! slot[task].m_address;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LL_CrcHandler::DmaTransferCompleted(void)
|
void LL_CrcHandler::DmaTransferCompleted(void)
|
||||||
|
@ -84,25 +84,25 @@ void LL_CrcHandler::DmaTransferCompleted(void)
|
||||||
* m_dma.GetIfcReg() = m_dma.GetTcMask();
|
* m_dma.GetIfcReg() = m_dma.GetTcMask();
|
||||||
LL_DMA_DisableStream(m_dma.GetDma(), m_dma.GetStream());
|
LL_DMA_DisableStream(m_dma.GetDma(), m_dma.GetStream());
|
||||||
if(m_activeSlot) {
|
if(m_activeSlot) {
|
||||||
if((*m_activeSlot)[m_activePrio].m_callback)
|
if((*m_activeSlot)[m_activeTask].m_callback)
|
||||||
(*m_activeSlot)[m_activePrio].m_callback->CrcSucceeded((*m_activeSlot)[m_activePrio].m_callbackParam, CRC->DR, m_activePrio);
|
(*m_activeSlot)[m_activeTask].m_callback->CrcSucceeded((*m_activeSlot)[m_activeTask].m_callbackParam, CRC->DR, m_activeTask);
|
||||||
else if((*m_activeSlot)[m_activePrio].m_callbackParam)
|
else if((*m_activeSlot)[m_activeTask].m_callbackParam)
|
||||||
*reinterpret_cast<uint32_t*>((*m_activeSlot)[m_activePrio].m_callbackParam) = CRC->DR;
|
*reinterpret_cast<uint32_t*>((*m_activeSlot)[m_activeTask].m_callbackParam) = CRC->DR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(*m_dma.GetIsReg() & m_dma.GetTeMask()) { // DMA transfer error
|
else if(*m_dma.GetIsReg() & m_dma.GetTeMask()) { // DMA transfer error
|
||||||
*m_dma.GetIfcReg() = m_dma.GetTeMask();
|
*m_dma.GetIfcReg() = m_dma.GetTeMask();
|
||||||
LL_DMA_DisableStream(m_dma.GetDma(), m_dma.GetStream());
|
LL_DMA_DisableStream(m_dma.GetDma(), m_dma.GetStream());
|
||||||
if(m_activeSlot) {
|
if(m_activeSlot) {
|
||||||
if((*m_activeSlot)[m_activePrio].m_callback)
|
if((*m_activeSlot)[m_activeTask].m_callback)
|
||||||
(*m_activeSlot)[m_activePrio].m_callback->CrcFailed((*m_activeSlot)[m_activePrio].m_callbackParam, CRC->DR, m_activePrio);
|
(*m_activeSlot)[m_activeTask].m_callback->CrcFailed((*m_activeSlot)[m_activeTask].m_callbackParam, CRC->DR, m_activeTask);
|
||||||
else if((*m_activeSlot)[m_activePrio].m_callbackParam)
|
else if((*m_activeSlot)[m_activeTask].m_callbackParam)
|
||||||
*reinterpret_cast<uint32_t*>((*m_activeSlot)[m_activePrio].m_callbackParam) = -1;
|
*reinterpret_cast<uint32_t*>((*m_activeSlot)[m_activeTask].m_callbackParam) = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(*m_activeSlot)[m_activePrio].m_callback = nullptr;
|
(*m_activeSlot)[m_activeTask].m_callback = nullptr;
|
||||||
(*m_activeSlot)[m_activePrio].m_callbackParam = 0;
|
(*m_activeSlot)[m_activeTask].m_callbackParam = 0;
|
||||||
(*m_activeSlot)[m_activePrio].m_wordCount = 0;
|
(*m_activeSlot)[m_activeTask].m_wordCount = 0;
|
||||||
StartNextTask();
|
StartNextTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,7 +118,7 @@ void LL_CrcHandler::StartNextTask(void)
|
||||||
if(index < slot->m_taskCount) {
|
if(index < slot->m_taskCount) {
|
||||||
if((*slot)[index].m_address) {
|
if((*slot)[index].m_address) {
|
||||||
m_activeSlot = slot;
|
m_activeSlot = slot;
|
||||||
m_activePrio = index;
|
m_activeTask = index;
|
||||||
CRC->CR = 1;
|
CRC->CR = 1;
|
||||||
LL_DMA_SetM2MSrcAddress(m_dma.GetDma(), m_dma.GetStream(), reinterpret_cast<uint32_t>((*slot)[index].m_address));
|
LL_DMA_SetM2MSrcAddress(m_dma.GetDma(), m_dma.GetStream(), reinterpret_cast<uint32_t>((*slot)[index].m_address));
|
||||||
LL_DMA_SetDataLength(m_dma.GetDma(), m_dma.GetStream(), (*slot)[index].m_wordCount);
|
LL_DMA_SetDataLength(m_dma.GetDma(), m_dma.GetStream(), (*slot)[index].m_wordCount);
|
||||||
|
@ -137,19 +137,19 @@ void LL_CrcHandler::StartNextTask(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void LL_CrcHandler::WaitResults(SlotBase &slot, uint8_t prio) const
|
void LL_CrcHandler::WaitResults(SlotBase &slot, uint8_t task) const
|
||||||
{
|
{
|
||||||
while(IsQueued(slot, prio));
|
while(IsQueued(slot, task));
|
||||||
while(IsActive(slot, prio));
|
while(IsActive(slot, task));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint32_t LL_CrcHandler::Compute(
|
uint32_t LL_CrcHandler::Compute(
|
||||||
SlotBase &slot, uint8_t prio, void const *address, uint16_t len)
|
SlotBase &slot, uint8_t task, void const *address, uint16_t len)
|
||||||
{
|
{
|
||||||
uint32_t result;
|
uint32_t result;
|
||||||
Enqueue(slot, prio, address, len, nullptr, reinterpret_cast<uintptr_t>(&result));
|
Enqueue(slot, task, address, len, nullptr, reinterpret_cast<uintptr_t>(&result));
|
||||||
while(IsActive(slot, prio));
|
while(IsActive(slot, task));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -208,7 +208,7 @@ void LL_HsUsart::SwitchRxBuffers(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void LL_HsUsart::CrcSucceeded(uintptr_t callbackParam, uint32_t crc, int prio)
|
void LL_HsUsart::CrcSucceeded(uintptr_t callbackParam, uint32_t crc, uint8_t task)
|
||||||
{
|
{
|
||||||
Buffer &buf(m_rxBuffers[static_cast<int>(callbackParam)]);
|
Buffer &buf(m_rxBuffers[static_cast<int>(callbackParam)]);
|
||||||
|
|
||||||
|
@ -223,7 +223,7 @@ void LL_HsUsart::CrcSucceeded(uintptr_t callbackParam, uint32_t crc, int prio)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void LL_HsUsart::CrcFailed(uintptr_t callbackParam, uint32_t crc, int prio)
|
void LL_HsUsart::CrcFailed(uintptr_t callbackParam, uint32_t crc, uint8_t task)
|
||||||
{
|
{
|
||||||
Buffer &buf(m_rxBuffers[static_cast<int>(callbackParam)]);
|
Buffer &buf(m_rxBuffers[static_cast<int>(callbackParam)]);
|
||||||
buf.busy = buf.error = true;
|
buf.busy = buf.error = true;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue