f407ve_hs_uart/lib/usart_handler.h
2019-09-30 19:22:22 +02:00

81 lines
1.9 KiB
C

/*
* uart_handler.h
*
* Created on: Sep 16, 2019
* Author: abody
*/
#ifndef UART_HANDLER_H_
#define UART_HANDLER_H_
#include <inttypes.h>
#include <dma.h>
#include <stats.h>
#include "dma_helper.h"
#include "crc_handler.h"
/*
* TH UART RXDMA TXDMA
* 0 1 D2S5 D2S7
* 1 3 D1S1 D1S3
* 2 2 D1S5 D1S6
* 3 6 D2S2 D2S6
*/
typedef struct {
uint8_t startByte;
uint8_t serial;
uint8_t payloadLength;
uint8_t hash;
} UARTPACKETHEADER;
typedef struct
{
UARTPACKETHEADER header;
//!!! should start on word offset !!!
uint8_t payload[256+sizeof(uint32_t)]; // extra room for crc32
} __attribute__((aligned)) UARTPACKET;
typedef struct _UARTBUFFER {
UARTPACKET packet;
//transfer area ends here
volatile uint8_t busy;
volatile uint8_t error;
uint16_t requestedLength;
uint32_t errorInfo;
} UARTBUFFER;
typedef struct {
USART_TypeDef *uart;
DMAINFO rxDmaInfo;
DMAINFO txDmaInfo;
struct crcstatus_t *crcStatus;
uint8_t rxSerial;
uint8_t txSerial;
STATS stats;
uint8_t activeRxBuf;
uint8_t rxCrcSlot;
uint8_t txCrcSlot;
UARTBUFFER txBuffer;
UARTBUFFER rxBuffers[2];
} UARTSTATUS;
#define STARTMARKER 0xa5
void InitUartStatus(
UARTSTATUS *st, USART_TypeDef *uart, DMA_TypeDef *dma,
uint32_t stream_rx, uint32_t stream_tx,
struct crcstatus_t *crcStatus, uint8_t rxCrcSlot, uint8_t txCrcSlot);
static inline uint8_t* GetTxBuffer(UARTSTATUS *status) {
return status->txBuffer.packet.payload;
}
uint8_t PostPacket(UARTSTATUS *status, uint8_t const *payload, uint16_t length, struct crcstatus_t *crcStatus);
void SetupReceive(UARTSTATUS *status);
void SetupTransmit(USART_TypeDef *uart, DMA_TypeDef* dma, uint32_t stream, void *buffer, uint32_t length);
void ConsumePacket(UARTSTATUS *status, uint8_t packetIndex, struct crcstatus_t *crcStatus);
void HandleUsartRxDmaIrq(UARTSTATUS *status);
void HandleUsartTxDmaIrq(UARTSTATUS *status);
void HandleUsartIrq(UARTSTATUS *status);
#endif /* UART_HANDLER_H_ */