Shield Code 6.0
Loading...
Searching...
No Matches
PDC.hpp
Go to the documentation of this file.
1
14#ifndef PDC_HPP
15#define PDC_HPP
16#include <Arduino.h>
17
18// Defining relevant UART registers
19#define UART_BASE 0x400E0800
20#define UART_PERIPH_TPR_ADDR (UART_BASE + 0x108) // Transmit pointer register
21#define UART_PERIPH_TCR_ADDR (UART_BASE + 0x10C) // Transmit counter register
22#define UART_PERIPH_TNPR_ADDR (UART_BASE + 0x118) // Transmit next pointer register
23#define UART_PERIPH_TNCR_ADDR (UART_BASE + 0x11C) // Transmit next counter register
24#define UART_PERIPH_PTCR_ADDR (UART_BASE + 0x120) // Peripheral transfer control register
25#define UART_PERIPH_PTSR_ADDR (UART_BASE + 0x124) // Peripheral transfer status register
26#define UART_SR (UART_BASE + 0x0014) // Status register
27
28#define TXTEN (1<<8) //mask used to enable UART transmitter
29#define TXBUFE (1<<11) //check if UART is ready
33class PDC {
34private:
35 // pointers to UART registers
36 volatile uint32_t* const p_UART_TPR;
37 volatile uint32_t* const p_UART_TCR;
38 volatile uint32_t* const p_UART_TNPR;
39 volatile uint32_t* const p_UART_TNCR;
40 volatile uint32_t* const p_UART_PTCR;
41 volatile uint32_t* const p_UART_PTSR;
42 volatile uint32_t* const p_UART_SR;
43
44 uint8_t backup_buffer[294];
45
46
47
48
49public:
54 : p_UART_TPR((uint32_t*)UART_PERIPH_TPR_ADDR),
55 p_UART_TCR((uint32_t*)UART_PERIPH_TCR_ADDR),
56 p_UART_TNPR((uint32_t*)UART_PERIPH_TNPR_ADDR),
57 p_UART_TNCR((uint32_t*)UART_PERIPH_TNCR_ADDR),
58 p_UART_PTCR((uint32_t*)UART_PERIPH_PTCR_ADDR),
59 p_UART_PTSR((uint32_t*)UART_PERIPH_PTSR_ADDR),
60 p_UART_SR((uint32_t*)UART_SR)
61 {}
62
63
67 void init(){
68 //enable pdc transmitter
69 *p_UART_PTCR |= TXTEN;
70 //wait until ready
71 while(! PDC::is_on()){
72 ;
73 }
74
75 }
95 template <typename T>
96 void send(T* buffer, int size){
97 //check if UART is ready for transmit
98 if(*p_UART_SR & TXBUFE){
99 //set buffer and size
100 *(volatile uint32_t*)p_UART_TPR = (uint32_t)buffer;
101 *p_UART_TCR = size;
102
103 } else{
104 memcpy(backup_buffer, buffer, size);
105 enableUARTInterrupt();
106 /* //wait until ready
107 //digitalWrite(7, HIGH);
108 while(!(*p_UART_SR & TXBUFE)){
109 ;
110 }
111 //digitalWrite(7, LOW);
112 //same as above
113 *(volatile uint32_t*)p_UART_TPR = (uint32_t)buffer;
114 *p_UART_TCR = size; */
115 }
116
117 }
121 template <typename T>
122 void send_next(T* buffer, int size){
123 //set buffer and size
124 if(*p_UART_SR & TXBUFE){
125 send(buffer, size);
126 } else if (*p_UART_TNCR==0){
127 *(volatile uint32_t*)p_UART_TNPR = (uint32_t)buffer;
128 *p_UART_TNCR = size;
129 }
130
131 else if (!(*p_UART_SR & TXBUFE)&&(*p_UART_TNCR!=1)){
132 while(*p_UART_TNCR!=0){
133 ;
134 }
135 *(volatile uint32_t*)p_UART_TNPR = (uint32_t)buffer;
136 *p_UART_TNCR = size;
137 } else{
138 send(buffer, size);
139 }
140
141 }
145 bool is_on(){
146 return (*p_UART_PTSR & (1<<8));
147 }
148
150 //enable UART interrupt
151 NVIC_EnableIRQ(UART_IRQn);
152 //interrupt on TXBUFE
153 UART->UART_IER = UART_IER_TXBUFE;
154 UART->UART_IDR = ~UART_IER_TXBUFE;
155 }
156
158 send(backup_buffer, sizeof(backup_buffer));
159 NVIC_DisableIRQ(UART_IRQn);
160 }
161};
162#endif
#define TXTEN
Definition: PDC.hpp:28
#define TXBUFE
Definition: PDC.hpp:29
#define UART_PERIPH_TPR_ADDR
Definition: PDC.hpp:20
#define UART_PERIPH_TNPR_ADDR
Definition: PDC.hpp:22
#define UART_SR
Definition: PDC.hpp:26
#define UART_PERIPH_TNCR_ADDR
Definition: PDC.hpp:23
#define UART_PERIPH_PTCR_ADDR
Definition: PDC.hpp:24
#define UART_PERIPH_TCR_ADDR
Definition: PDC.hpp:21
#define UART_PERIPH_PTSR_ADDR
Definition: PDC.hpp:25
volatile uint32_t *const p_UART_TNCR
Definition: PDC.hpp:39
volatile uint32_t *const p_UART_TNPR
Definition: PDC.hpp:38
void init()
Turns on PDC, waits until ready. Must be called AFTER Serial.begin() in setup().
Definition: PDC.hpp:67
void send(T *buffer, int size)
Adds data to the PDC buffer to be sent out through UART.
Definition: PDC.hpp:96
bool is_on()
Checks if the PDC and UART are on.
Definition: PDC.hpp:145
PDC()
Default constructor for the PDC class. Initializes all relevant register pointers.
Definition: PDC.hpp:53
void UART_Handler()
Definition: PDC.hpp:157
volatile uint32_t *const p_UART_TCR
Definition: PDC.hpp:37
volatile uint32_t *const p_UART_PTCR
Definition: PDC.hpp:40
volatile uint32_t *const p_UART_PTSR
Definition: PDC.hpp:41
volatile uint32_t *const p_UART_SR
Definition: PDC.hpp:42
void enableUARTInterrupt()
Definition: PDC.hpp:149
void send_next(T *buffer, int size)
Adds data to the backup PDC buffer. Identical to send().
Definition: PDC.hpp:122
volatile uint32_t *const p_UART_TPR
Definition: PDC.hpp:36