aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/test-usart-dma.cpp37
1 files changed, 19 insertions, 18 deletions
diff --git a/examples/test-usart-dma.cpp b/examples/test-usart-dma.cpp
index 7dafc3a..b3c766e 100644
--- a/examples/test-usart-dma.cpp
+++ b/examples/test-usart-dma.cpp
@@ -29,9 +29,9 @@
* Configuration and state
*/
-// USART and DMA configuration
-#define USART USART2
-#define USART_HWSER Serial2
+// USART and DMA configuration. You can change these to suit your
+// purposes.
+HardwareSerial *serial = &Serial2;
#define USART_DMA_DEV DMA1
#define USART_RX_DMA_CHANNEL DMA_CH6
#define BAUD 9600
@@ -60,16 +60,17 @@ void rx_dma_irq(void) {
// 1. Turn it on.
// 2. Set the "DMA request on RX" bit in USART_CR3 (USART_CR3_DMAR).
void init_usart(void) {
- USART_HWSER.begin(BAUD);
- USART->regs->CR3 = USART_CR3_DMAR;
+ serial->begin(BAUD);
+ usart_dev *serial_dev = serial->c_dev();
+ serial_dev->regs->CR3 = USART_CR3_DMAR;
}
// Configure the DMA controller to serve DMA requests from the USART.
void init_dma_xfer(void) {
dma_init(USART_DMA_DEV);
dma_setup_transfer(USART_DMA_DEV, USART_RX_DMA_CHANNEL,
- &USART->regs->DR, DMA_SIZE_8BITS,
- rx_buf, DMA_SIZE_8BITS,
+ &serial->c_dev()->regs->DR, DMA_SIZE_8BITS,
+ rx_buf, DMA_SIZE_8BITS,
(DMA_MINC_MODE | DMA_CIRC_MODE | DMA_TRNS_CMPLT));
dma_set_num_transfers(USART_DMA_DEV, USART_RX_DMA_CHANNEL,
BUF_SIZE - 1);
@@ -95,15 +96,15 @@ void loop(void) {
// See if the interrupt handler got called since the last time we
// checked.
if (irq_fired) {
- USART_HWSER.println("** IRQ **");
+ serial->println("** IRQ **");
// Notice how the ISR bits show transfer complete _and_
// half-complete here, but the ISR bits we print next will be
// zero. That's because the variable "isr" gets set _inside_
// rx_dma_irq(). After it exits, libmaple cleans up by
// clearing the ISR bits. (If it didn't, and we forgot to, the
// interrupt would repeatedly fire forever.)
- USART_HWSER.print("ISR bits: 0x");
- USART_HWSER.println(isr, HEX);
+ serial->print("ISR bits: 0x");
+ serial->println(isr, HEX);
irq_fired = 0;
}
@@ -116,20 +117,20 @@ void loop(void) {
// right times no matter what; we just don't get interrupted
// unless we asked. (If an error or other problem occurs, the
// relevant ISR bits will get set in the same way).
- USART_HWSER.print("[");
- USART_HWSER.print(millis());
- USART_HWSER.print("]\tISR bits: 0x");
+ serial->print("[");
+ serial->print(millis());
+ serial->print("]\tISR bits: 0x");
uint8 isr_bits = dma_get_isr_bits(USART_DMA_DEV, USART_RX_DMA_CHANNEL);
- USART_HWSER.print(isr_bits, HEX);
+ serial->print(isr_bits, HEX);
// Print the contents of rx_buf. If you keep typing after it fills
// up, the new characters will overwrite the old ones, thanks to
// DMA_CIRC_MODE.
- USART_HWSER.print("\tCharacter buffer contents: '");
- USART_HWSER.print(rx_buf);
- USART_HWSER.println("'");
+ serial->print("\tCharacter buffer contents: '");
+ serial->print(rx_buf);
+ serial->println("'");
if (isr_bits == 0x7) {
- USART_HWSER.println("** Clearing ISR bits.");
+ serial->println("** Clearing ISR bits.");
dma_clear_isr_bits(USART_DMA_DEV, USART_RX_DMA_CHANNEL);
}
}