142 lines
3.7 KiB
C
142 lines
3.7 KiB
C
/*
|
|
* usart_handler.h
|
|
*
|
|
* Created on: Sep 16, 2019
|
|
* Author: abody
|
|
*/
|
|
|
|
#ifndef USART_HANDLER_H_
|
|
#define USART_HANDLER_H_
|
|
#include <inttypes.h>
|
|
|
|
#include "f4ll_c/dma_helper.h"
|
|
#include "f4ll_c/crc_handler.h"
|
|
|
|
struct usart_buffer;
|
|
struct usartstatus_t;
|
|
|
|
typedef void (*PACKETRECEIVEDCALLBACK)(void *userParam, struct usart_buffer *buffer);
|
|
|
|
void InitUartStatus(
|
|
struct usartstatus_t *st, USART_TypeDef *usart, DMA_TypeDef *dma,
|
|
uint32_t stream_rx, uint32_t stream_tx,
|
|
struct crcstatus_t *crcStatus,
|
|
PACKETRECEIVEDCALLBACK packetReceivedCallback, void * packetReceivedCallbackParam);
|
|
|
|
uint8_t* GetTxBuffer(struct usartstatus_t *status);
|
|
|
|
uint8_t PostPacket(struct usartstatus_t *status, uint8_t const *payload, uint16_t length, struct crcstatus_t *crcStatus, uint8_t waitForCrcQueue);
|
|
void SetupReceive(struct usartstatus_t *status);
|
|
void SetupTransmit(USART_TypeDef *usart, DMA_TypeDef* dma, uint32_t stream, void *buffer, uint32_t length);
|
|
void ConsumePacket(struct usartstatus_t *status, uint8_t packetIndex);
|
|
|
|
void HandleUsartRxDmaIrq(struct usartstatus_t *status);
|
|
void HandleUsartTxDmaIrq(struct usartstatus_t *status);
|
|
void HandleUsartIrq(struct usartstatus_t *status);
|
|
|
|
/******************************************************************************************
|
|
*
|
|
*
|
|
*
|
|
*/
|
|
|
|
struct usart_stats {
|
|
uint32_t overrun;
|
|
uint32_t hdrError;
|
|
uint32_t lastErrHdr;
|
|
uint32_t payloadErrror;
|
|
uint32_t pep1, pep2;
|
|
uint32_t dmaError;
|
|
uint32_t rcvd;
|
|
uint32_t premature_hdr;
|
|
uint32_t premature_payload;
|
|
uint32_t sent;
|
|
uint32_t skiped;
|
|
};
|
|
|
|
struct usartpacketheader_t {
|
|
uint8_t startByte;
|
|
uint8_t serial;
|
|
uint8_t payloadLength;
|
|
uint8_t hash;
|
|
};
|
|
|
|
struct usartpacket_t {
|
|
struct usartpacketheader_t header;
|
|
//!!! should start on word offset !!!
|
|
uint8_t payload[256+sizeof(uint32_t)]; // extra room for crc32
|
|
} __attribute__((aligned));
|
|
|
|
struct usart_buffer {
|
|
struct usartpacket_t packet;
|
|
//transfer area ends here
|
|
volatile uint8_t busy;
|
|
volatile uint8_t error;
|
|
uint16_t requestedLength;
|
|
uint32_t errorInfo;
|
|
struct usartstatus_t *usartStatus;
|
|
};
|
|
|
|
struct usartstatus_t {
|
|
USART_TypeDef *usart;
|
|
DMAINFO rxDmaInfo;
|
|
DMAINFO txDmaInfo;
|
|
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;
|
|
PACKETRECEIVEDCALLBACK packetReceivedCallback;
|
|
void *packetReceivedCallbacParam;
|
|
struct usart_buffer txBuffer;
|
|
struct usart_buffer rxBuffers[2];
|
|
};
|
|
|
|
#ifndef USART_STATS_DISABLED
|
|
static inline void StatsIncOverrun(struct usart_stats *s) {
|
|
++s->overrun;
|
|
}
|
|
static inline void StatsIncHdrError(struct usart_stats *s, uint32_t hdr) {
|
|
++s->hdrError;
|
|
s->lastErrHdr = hdr;
|
|
}
|
|
static inline void StatsIncPayloadError(struct usart_stats *s, uint32_t pep1, uint32_t pep2) {
|
|
++s->payloadErrror;
|
|
s->pep1 = pep1;
|
|
s->pep2 = pep2;
|
|
}
|
|
static inline void StatsIncDmaError(struct usart_stats *s) {
|
|
++s->dmaError;
|
|
}
|
|
static inline void StatsIncRcvd(struct usart_stats *s) {
|
|
++s->rcvd;
|
|
}
|
|
static inline void StatsIncPremature_hdr(struct usart_stats *s) {
|
|
++s->premature_hdr;
|
|
}
|
|
static inline void StatsIncPremature_payload(struct usart_stats *s) {
|
|
++s->premature_payload;
|
|
}
|
|
static inline void StatsIncSent(struct usart_stats *s) {
|
|
++s->sent;
|
|
}
|
|
static inline void StatsAddSkiped(struct usart_stats *s, uint8_t cnt) {
|
|
s->skiped += s->rcvd > 2 ? cnt : 0;
|
|
}
|
|
|
|
#else // USART_STATS_DISABLED
|
|
#define StatsIncOverrun(x)
|
|
#define StatsIncHdrError(x,y)
|
|
#define StatsIncPayloadError(x,y,z)
|
|
#define StatsIncDmaError(x)
|
|
#define StatsIncRcvd(x)
|
|
#define StatsIncPremature_hdr(x)
|
|
#define StatsIncPremature_payload(x)
|
|
#define StatsIncSent(x)
|
|
#define StatsAddSkiped(x)
|
|
#endif // USART_STATS_DISABLED
|
|
|
|
#endif /* UART_HANDLER_H_ */
|