-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDmaSerial.h
118 lines (94 loc) · 3.48 KB
/
DmaSerial.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. - Robin Lilja
*
* @see page 519 in ATMEL SAM3X/A datasheet/manual for Peripheral DMA Controller (PDC) usage.
*
* @file DmaSerial.h
* @author Robin Lilja
* @date 18 Jan 2015
*/
#ifndef _DMA_SERIAL_H_
#define _DMA_SERIAL_H_
#include <string.h> /**< strlen() */
#include "sam.h" /**< Used for register mapping */
#define DMA_SERIAL_CLK (SystemCoreClock) /**< Used for baud rate calculation */
#define DMA_SERIAL_RX_BUFFER_LENGTH (128) /**< Receiver buffer length */
#define DMA_SERIAL_TX_BUFFER_LENGTH (128) /**< Transmit buffer length */
/**
* DMA based serial communication class.
*/
class DmaSerial {
public:
/**
* Constructor.
* @code
* DmaSerial((Uart*)USART0, ID_USART0);
* @endcode
* @param uart pointer to U(S)ART.
* @param uart_id id of U(S)ART.
*/
DmaSerial(Uart* uart, uint32_t uart_id);
/**
* Initiates the the U(S)ART for DMA.
* @todo add support for other than 8N1 setups.
* @param baud serial baud rate.
*/
void begin(const uint32_t baud);
/**
* Check if there are available bytes in the receiver buffer.
* @return the number of available bytes.
*/
uint8_t available();
/**
* Gets bytes from DMA receiving buffer.
* @param bytes pointer to beginning of buffer for retrieval.
* @param length number of bytes to be read to retrieval buffer.
* @return the number of retrieved bytes.
*/
uint8_t get(uint8_t* bytes, uint8_t length);
/**
* Gets bytes up to and including a new line character from DMA receiving buffer.
* @param bytes pointer to beginning of buffer for retrieval.
* @param length maximum number of bytes to be read to retrieval buffer.
* @return the number of retrieved bytes.
*/
uint8_t getln(uint8_t* bytes, uint8_t length);
/**
* Puts a char array in the DMA transmit buffer.
* Char array is assumed to be null-terminated.
* @param str the char array.
* @return the number of chars added to the transmit buffer.
*/
uint8_t put(const char* str);
uint8_t putln(const char* str);
/**
* Puts bytes in the DMA transmit buffer.
* @param bytes pointer to beginning of buffer containing data to be transmitted.
* @param length number of bytes to be transmitted.
* @return the number of bytes added to the transmit buffer.
*/
uint8_t put(uint8_t* bytes, uint8_t length);
/**
* Enable communication.
*/
void disable() { uart->UART_PTCR = UART_PTCR_TXTDIS; uart->UART_PTCR = UART_PTCR_RXTDIS; };
/**
* Disable communication.
*/
void enable() { uart->UART_PTCR = UART_PTCR_TXTEN; uart->UART_PTCR = UART_PTCR_RXTEN; };
private:
Uart* uart; /**< Pointer to U(S)ART */
uint32_t uart_id; /**< U(SART) id */
uint8_t tx_buffer[DMA_SERIAL_TX_BUFFER_LENGTH]; /**< Transmit ring buffer */
uint8_t tx_head; /**< Position were the DMA reads from the ring buffer */
uint8_t tx_tail; /**< Position were the user writes to the ring buffer */
uint8_t tx_count; /**< Number of bytes stored in the buffer */
uint8_t rx_buffer[DMA_SERIAL_RX_BUFFER_LENGTH]; /**< Receiver ring buffer */
uint8_t rx_head; /**< Position were the user reads from the ring buffer */
uint8_t rx_tail; /**< Position were the DMA writes to the ring buffer */
uint8_t rx_count; /**< Number of available bytes in buffer */
};
#endif /* _DMA_SERIAL_H_ */