c code refactor

This commit is contained in:
Attila Body 2019-11-12 09:36:56 +01:00
parent 76ba80db36
commit 180f2ef624
14 changed files with 369 additions and 130 deletions

View file

@ -20,34 +20,48 @@
#define CRCTASKCOUNT 2
#endif
typedef struct {
DMAINFO dmaInfo;
volatile uint8_t activeSlot;
struct crctask_t {
void *address;
uint16_t wordCount;
void (*callback)(void*, uint32_t, uint8_t);
void *callbackParam;
} volatile crcTasks[CRCTASKCOUNT];
} CRCSTATUS;
struct crcslottask_t {
void *address;
uint16_t wordCount;
void (*callback)(void*, uint32_t, uint8_t);
void *callbackParam;
};
void InitCrcStatus(CRCSTATUS *status, DMA_TypeDef *dma, uint32_t stream);
static inline uint8_t GetActiveSlot(CRCSTATUS *status) {
return status->activeSlot;
struct crcslotlistitem_t {
uint16_t count;
struct crcslotlistitem_t *next;
struct crcslottask_t *tasks;
};
struct crcstatus_t {
DMAINFO dmaInfo;
volatile struct crcslotlistitem_t *activeSlot;
volatile uint8_t activeTask;
struct crcslotlistitem_t *first;
};
void InitCrcStatus(struct crcstatus_t *status, DMA_TypeDef *dma, uint32_t stream);
uint8_t GetActiveSlot(struct crcslotlistitem_t **slot_out, struct crcstatus_t *status);
static inline uint8_t IsSlotQueued(struct crcslotlistitem_t *slot, uint8_t task) {
return slot->tasks[task].address != NULL;
}
static inline uint8_t IsSlotQueued(CRCSTATUS *status, uint8_t slot) {
return status->crcTasks[slot].address != NULL;
static inline uint8_t IsSlotActive(struct crcslotlistitem_t *slot, uint8_t task) {
return slot->tasks[task].callback != NULL || slot->tasks[task].callbackParam != NULL;
}
static inline uint8_t IsSlotActive(CRCSTATUS *status, uint8_t slot) {
return status->crcTasks[slot].callback != NULL || status->crcTasks[slot].callbackParam != NULL;
}
uint8_t EnqueueCrcTask(CRCSTATUS *crcStatus, uint8_t slot, uint8_t *address, uint16_t len,
void (*callback)(void*, uint32_t, uint8_t), void* callbackParam);
void WaitCrcResults(CRCSTATUS *status, uint8_t slot);
uint32_t ComputeCrc(CRCSTATUS *status, uint8_t slot, uint8_t *address, uint16_t len);
void ComputeCrcAsync(CRCSTATUS *status, uint8_t slot,
void AttachCrcTask(struct crcstatus_t *status, struct crcslotlistitem_t *slot, struct crcslottask_t *tasks, uint8_t taskCount);
uint8_t EnqueueCrcTask(struct crcstatus_t *status, struct crcslotlistitem_t *slot, uint8_t task,
uint8_t *address, uint16_t len, void (*callback)(void*, uint32_t, uint8_t), void* callbackParam);
void WaitCrcResults(struct crcstatus_t *status, struct crcslotlistitem_t *slot, uint8_t task);
uint32_t ComputeCrc(struct crcstatus_t *status, struct crcslotlistitem_t *slot, uint8_t task, uint8_t *address, uint16_t len);
void ComputeCrcAsync(struct crcstatus_t *status, uint8_t slot,
uint8_t *address, uint16_t len,
void (*callback)(void*, uint32_t, uint8_t), void* callbackParam);
void HandleCrcDmaIrq(CRCSTATUS *status);
void HandleCrcDmaIrq(struct crcstatus_t *status);
#endif /* CRC_HANDLER_H_ */

View file

@ -21,15 +21,15 @@ typedef void (*PACKETRECEIVEDCALLBACK)(void *userParam, struct usart_buffer *buf
void InitUartStatus(
USARTSTATUS *st, USART_TypeDef *usart, DMA_TypeDef *dma,
uint32_t stream_rx, uint32_t stream_tx,
CRCSTATUS *crcStatus, uint8_t rxCrcSlot, uint8_t txCrcSlot,
struct crcstatus_t *crcStatus,
PACKETRECEIVEDCALLBACK packetReceivedCallback, void * packetReceivedCallbackParam);
uint8_t* GetTxBuffer(USARTSTATUS *status);
uint8_t PostPacket(USARTSTATUS *status, uint8_t const *payload, uint16_t length, CRCSTATUS *crcStatus);
uint8_t PostPacket(USARTSTATUS *status, uint8_t const *payload, uint16_t length, struct crcstatus_t *crcStatus);
void SetupReceive(USARTSTATUS *status);
void SetupTransmit(USART_TypeDef *usart, DMA_TypeDef* dma, uint32_t stream, void *buffer, uint32_t length);
void ConsumePacket(USARTSTATUS *status, uint8_t packetIndex, CRCSTATUS *crcStatus);
void ConsumePacket(USARTSTATUS *status, uint8_t packetIndex);
void HandleUsartRxDmaIrq(USARTSTATUS *status);
void HandleUsartTxDmaIrq(USARTSTATUS *status);
@ -82,13 +82,14 @@ struct _usart_status {
USART_TypeDef *usart;
DMAINFO rxDmaInfo;
DMAINFO txDmaInfo;
CRCSTATUS *crcStatus;
struct crcstatus_t *crcStatus;
struct crcslotlistitem_t crcSlot;
struct crcslottask_t crcTasks[2];
uint8_t rxSerial;
uint8_t txSerial;
struct usart_stats stats;
uint8_t activeRxBuf;
uint8_t rxCrcSlot;
uint8_t txCrcSlot;
PACKETRECEIVEDCALLBACK packetReceivedCallback;
void *packetReceivedCallbacParam;
struct usart_buffer txBuffer;