2011-04-08 04:23:01 +02:00
|
|
|
/**
|
2012-05-08 22:22:41 +02:00
|
|
|
* @file examples/test-usart-dma.cpp
|
2011-04-08 04:23:01 +02:00
|
|
|
* @author Marti Bolivar <mbolivar@leaflabs.com>
|
|
|
|
*
|
|
|
|
* Simple test of DMA used with a USART receiver.
|
|
|
|
*
|
|
|
|
* Configures a USART receiver for use with DMA. Received bytes are
|
|
|
|
* placed into a buffer, with an interrupt firing when the buffer is
|
|
|
|
* full. At that point, the USART transmitter will print the contents
|
|
|
|
* of the byte buffer. The buffer is continually filled and refilled
|
|
|
|
* in this manner.
|
|
|
|
*
|
|
|
|
* This example isn't very robust; don't use it in production. In
|
|
|
|
* particular, since the buffer keeps filling (DMA_CIRC_MODE is set),
|
2012-06-13 17:57:16 +02:00
|
|
|
* if you keep sending characters after filling the buffer, you'll
|
|
|
|
* overwrite earlier bytes; this may happen before those earlier bytes
|
2012-06-13 19:53:00 +02:00
|
|
|
* are done printing. (Typing quickly and seeing how it affects the
|
|
|
|
* output is a fun way to make sense of how the interrupts and the
|
|
|
|
* main thread of execution interleave.)
|
2011-04-08 04:23:01 +02:00
|
|
|
*
|
|
|
|
* This code is released into the public domain.
|
|
|
|
*/
|
|
|
|
|
Move public headers to include directories; related cleanups.
Move libmaple/*.h to (new) libmaple/include/libmaple/. The new
accepted way to include a libmaple header foo.h is with:
#include <libmaple/foo.h>
This is more polite in terms of the include namespace. It also allows
us to e.g. implement the Arduino SPI library at all (which has header
SPI.h; providing it was previously impossible on case-insensitive
filesystems due to libmaple's spi.h).
Similarly for Wirish.
The old include style (#include "header.h") is now deprecated.
libmaple/*.h:
- Change include guard #defines from _FOO_H_ to _LIBMAPLE_FOO_H_.
- Add license headers where they're missing
- Add conditional extern "C" { ... } blocks where they're missing
(they aren't always necessary, but we might was well do it against
the future, while we're at it.).
- Change includes from #include "foo.h" to #include <libmaple/foo.h>.
- Move includes after extern "C".
- Remove extra trailing newlines
Note that this doesn't include the headers under libmaple/usb/ or
libmaple/usb/usb_lib. These will get fixed later.
libmaple/*.c:
- Change includes from #include "foo.h" to #include <libmaple/foo.h>.
Makefile:
- Add I$(LIBMAPLE_PATH)/include/libmaple to GLOBAL_FLAGS. This allows
for users (including Wirish) to migrate their code, but should go
away ASAP, since it slows down compilation.
Wirish:
- Move wirish/**/*.h to (new) wirish/include/wirish/. This ignores
the USB headers, which, as usual, are getting handled after
everything else.
- Similarly generify wirish/boards/ structure. For each supported
board "foo", move wirish/boards/foo.h and wirish/boards/foo.cpp to
wirish/boards/foo/include/board/board.h and
wirish/boards/foo/board.cpp, respectively. Also remove the #ifdef
hacks around the .cpp files.
- wirish/rules.mk: put wirish/boards/foo/include in the include path
(and add wirish/boards/foo/board.cpp to the list of sources to be
compiled). This allows saying:
#include <board/board.h>
instead of the hack currently in place. We can allow the user to
override this setting later to make adding custom board definitions
easier.
- Disable -Werror in libmaple/rules.mk, as the current USB warnings
don't let the olimex_stm32_h103 board compile. We can re-enable
-Werror once we've moved the board-specific bits out of libmaple
proper.
libraries, examples:
- Update includes accordingly.
- Miscellaneous cosmetic fixups.
Signed-off-by: Marti Bolivar <mbolivar@leaflabs.com>
2011-11-15 18:45:43 +01:00
|
|
|
#include <libmaple/dma.h>
|
|
|
|
#include <libmaple/usart.h>
|
|
|
|
#include <libmaple/gpio.h>
|
2011-04-08 04:23:01 +02:00
|
|
|
|
Move public headers to include directories; related cleanups.
Move libmaple/*.h to (new) libmaple/include/libmaple/. The new
accepted way to include a libmaple header foo.h is with:
#include <libmaple/foo.h>
This is more polite in terms of the include namespace. It also allows
us to e.g. implement the Arduino SPI library at all (which has header
SPI.h; providing it was previously impossible on case-insensitive
filesystems due to libmaple's spi.h).
Similarly for Wirish.
The old include style (#include "header.h") is now deprecated.
libmaple/*.h:
- Change include guard #defines from _FOO_H_ to _LIBMAPLE_FOO_H_.
- Add license headers where they're missing
- Add conditional extern "C" { ... } blocks where they're missing
(they aren't always necessary, but we might was well do it against
the future, while we're at it.).
- Change includes from #include "foo.h" to #include <libmaple/foo.h>.
- Move includes after extern "C".
- Remove extra trailing newlines
Note that this doesn't include the headers under libmaple/usb/ or
libmaple/usb/usb_lib. These will get fixed later.
libmaple/*.c:
- Change includes from #include "foo.h" to #include <libmaple/foo.h>.
Makefile:
- Add I$(LIBMAPLE_PATH)/include/libmaple to GLOBAL_FLAGS. This allows
for users (including Wirish) to migrate their code, but should go
away ASAP, since it slows down compilation.
Wirish:
- Move wirish/**/*.h to (new) wirish/include/wirish/. This ignores
the USB headers, which, as usual, are getting handled after
everything else.
- Similarly generify wirish/boards/ structure. For each supported
board "foo", move wirish/boards/foo.h and wirish/boards/foo.cpp to
wirish/boards/foo/include/board/board.h and
wirish/boards/foo/board.cpp, respectively. Also remove the #ifdef
hacks around the .cpp files.
- wirish/rules.mk: put wirish/boards/foo/include in the include path
(and add wirish/boards/foo/board.cpp to the list of sources to be
compiled). This allows saying:
#include <board/board.h>
instead of the hack currently in place. We can allow the user to
override this setting later to make adding custom board definitions
easier.
- Disable -Werror in libmaple/rules.mk, as the current USB warnings
don't let the olimex_stm32_h103 board compile. We can re-enable
-Werror once we've moved the board-specific bits out of libmaple
proper.
libraries, examples:
- Update includes accordingly.
- Miscellaneous cosmetic fixups.
Signed-off-by: Marti Bolivar <mbolivar@leaflabs.com>
2011-11-15 18:45:43 +01:00
|
|
|
#include <wirish/wirish.h>
|
2011-04-08 04:23:01 +02:00
|
|
|
|
2012-06-13 17:57:16 +02:00
|
|
|
/*
|
|
|
|
* Configuration and state
|
|
|
|
*/
|
2011-04-08 04:23:01 +02:00
|
|
|
|
2012-06-13 19:31:12 +02:00
|
|
|
// Serial port and DMA configuration. You can change these to suit
|
|
|
|
// your purposes.
|
2012-06-13 17:57:56 +02:00
|
|
|
HardwareSerial *serial = &Serial2;
|
2011-04-08 04:23:01 +02:00
|
|
|
#define USART_DMA_DEV DMA1
|
2012-06-15 00:32:01 +02:00
|
|
|
#if STM32_MCU_SERIES == STM32_SERIES_F1
|
2012-06-13 19:53:00 +02:00
|
|
|
// On STM32F1 microcontrollers (like what's on Maple and Maple Mini),
|
|
|
|
// dma tubes are channels.
|
|
|
|
#define USART_RX_DMA_TUBE DMA_CH6
|
2012-06-15 00:32:01 +02:00
|
|
|
#elif (STM32_MCU_SERIES == STM32_SERIES_F2 || \
|
|
|
|
STM32_MCU_SERIES == STM32_SERIES_F4)
|
|
|
|
// On STM32F2 and STM32F4 microcontrollers (Maple 2 will have an F4),
|
|
|
|
// dma tubes are streams.
|
|
|
|
#define USART_RX_DMA_TUBE DMA_S5
|
|
|
|
#else
|
|
|
|
#error "unsupported stm32 series"
|
|
|
|
#endif
|
2012-06-13 19:53:00 +02:00
|
|
|
// The serial port will make a DMA request each time it receives data.
|
|
|
|
// This is the dma_request_src we use to tell the DMA tube to handle
|
|
|
|
// that DMA request.
|
|
|
|
#define USART_DMA_REQ_SRC DMA_REQ_SRC_USART2_RX
|
2012-06-13 17:57:16 +02:00
|
|
|
#define BAUD 9600
|
2011-04-08 04:23:01 +02:00
|
|
|
|
2012-06-13 19:53:00 +02:00
|
|
|
// This will store the DMA configuration for USART RX.
|
|
|
|
dma_tube_config tube_config;
|
|
|
|
|
2012-06-13 17:57:16 +02:00
|
|
|
// This will store received USART characters.
|
|
|
|
#define BUF_SIZE 20
|
|
|
|
char rx_buf[BUF_SIZE];
|
2011-04-08 04:23:01 +02:00
|
|
|
|
2012-06-13 17:57:16 +02:00
|
|
|
// The interrupt handler, rx_dma_irq(), sets this to 1.
|
2011-07-06 17:04:37 +02:00
|
|
|
volatile uint32 irq_fired = 0;
|
2012-06-13 19:31:12 +02:00
|
|
|
// Used to store DMA interrupt status register (ISR) bits inside
|
|
|
|
// rx_dma_irq(). This helps explain what's going on inside loop(); see
|
|
|
|
// comments below.
|
2012-06-13 17:57:16 +02:00
|
|
|
volatile uint32 isr = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
// This is our DMA interrupt handler.
|
|
|
|
void rx_dma_irq(void) {
|
|
|
|
irq_fired = 1;
|
2012-06-13 19:53:00 +02:00
|
|
|
isr = dma_get_isr_bits(USART_DMA_DEV, USART_RX_DMA_TUBE);
|
2012-06-13 17:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Configure the USART receiver for use with DMA:
|
|
|
|
// 1. Turn it on.
|
|
|
|
// 2. Set the "DMA request on RX" bit in USART_CR3 (USART_CR3_DMAR).
|
2012-06-13 19:31:12 +02:00
|
|
|
void setup_usart(void) {
|
2012-06-13 17:57:56 +02:00
|
|
|
serial->begin(BAUD);
|
|
|
|
usart_dev *serial_dev = serial->c_dev();
|
|
|
|
serial_dev->regs->CR3 = USART_CR3_DMAR;
|
2012-06-13 17:57:16 +02:00
|
|
|
}
|
|
|
|
|
2012-06-13 19:53:00 +02:00
|
|
|
// Set up our dma_tube_config structure. (We could have done this
|
|
|
|
// above, when we declared tube_config, but having this function makes
|
|
|
|
// it easier to explain what's going on).
|
|
|
|
void setup_tube_config(void) {
|
|
|
|
// We're receiving from the USART data register. serial->c_dev()
|
|
|
|
// returns a pointer to the libmaple usart_dev for that serial
|
|
|
|
// port, so this is a pointer to its data register.
|
|
|
|
tube_config.tube_src = &serial->c_dev()->regs->DR;
|
|
|
|
// We're only interested in the bottom 8 bits of that data register.
|
|
|
|
tube_config.tube_src_size = DMA_SIZE_8BITS;
|
|
|
|
// We're storing to rx_buf.
|
|
|
|
tube_config.tube_dst = rx_buf;
|
|
|
|
// rx_buf is a char array, and a "char" takes up 8 bits on STM32.
|
|
|
|
tube_config.tube_dst_size = DMA_SIZE_8BITS;
|
|
|
|
// Only fill BUF_SIZE - 1 characters, to leave a null byte at the end.
|
|
|
|
tube_config.tube_nr_xfers = BUF_SIZE - 1;
|
|
|
|
// Flags:
|
|
|
|
// - DMA_CFG_DST_INC so we start at the beginning of rx_buf and
|
|
|
|
// fill towards the end.
|
|
|
|
// - DMA_CFG_CIRC so we go back to the beginning and start over when
|
|
|
|
// rx_buf fills up.
|
|
|
|
// - DMA_CFG_CMPLT_IE to turn on interrupts on transfer completion.
|
|
|
|
tube_config.tube_flags = DMA_CFG_DST_INC | DMA_CFG_CIRC | DMA_CFG_CMPLT_IE;
|
|
|
|
// Target data: none. It's important to set this to NULL if you
|
|
|
|
// don't have any special (microcontroller-specific) configuration
|
|
|
|
// in mind, which we don't.
|
|
|
|
tube_config.target_data = NULL;
|
|
|
|
// DMA request source.
|
|
|
|
tube_config.tube_req_src = USART_DMA_REQ_SRC;
|
|
|
|
}
|
|
|
|
|
2012-06-13 17:57:16 +02:00
|
|
|
// Configure the DMA controller to serve DMA requests from the USART.
|
2012-06-13 19:31:12 +02:00
|
|
|
void setup_dma_xfer(void) {
|
2012-06-13 19:53:00 +02:00
|
|
|
// First, turn it on.
|
2012-06-13 17:57:16 +02:00
|
|
|
dma_init(USART_DMA_DEV);
|
2012-06-13 19:53:00 +02:00
|
|
|
// Next, configure it by calling dma_tube_cfg(), and check to make
|
|
|
|
// sure it succeeded. DMA tubes have many restrictions on their
|
|
|
|
// configuration, and there are configurations which work on some
|
|
|
|
// types of STM32 but not others. libmaple tries hard to make
|
|
|
|
// things just work, but checking the return status is important!
|
|
|
|
int status = dma_tube_cfg(USART_DMA_DEV, USART_RX_DMA_TUBE, &tube_config);
|
|
|
|
ASSERT(status == DMA_TUBE_CFG_SUCCESS);
|
|
|
|
// Now we'll perform any other configuration we want. For this
|
|
|
|
// example, we attach an interrupt handler.
|
|
|
|
dma_attach_interrupt(USART_DMA_DEV, USART_RX_DMA_TUBE, rx_dma_irq);
|
|
|
|
// Turn on the DMA tube. It will now begin serving requests.
|
|
|
|
dma_enable(USART_DMA_DEV, USART_RX_DMA_TUBE);
|
2012-06-13 17:57:16 +02:00
|
|
|
}
|
2011-04-08 04:23:01 +02:00
|
|
|
|
2012-06-13 17:57:16 +02:00
|
|
|
/*
|
|
|
|
* setup() and loop()
|
|
|
|
*/
|
2011-04-08 04:23:01 +02:00
|
|
|
|
|
|
|
void setup(void) {
|
|
|
|
pinMode(BOARD_LED_PIN, OUTPUT);
|
2012-06-13 19:53:00 +02:00
|
|
|
setup_tube_config();
|
2012-06-13 19:31:12 +02:00
|
|
|
setup_dma_xfer();
|
|
|
|
setup_usart();
|
2011-04-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop(void) {
|
|
|
|
toggleLED();
|
|
|
|
delay(100);
|
|
|
|
|
2012-06-13 17:57:16 +02:00
|
|
|
// See if the interrupt handler got called since the last time we
|
|
|
|
// checked.
|
2011-04-08 04:23:01 +02:00
|
|
|
if (irq_fired) {
|
2012-06-13 17:57:56 +02:00
|
|
|
serial->println("** IRQ **");
|
2012-06-13 19:31:12 +02:00
|
|
|
// Notice how the interrupt status register (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 tube's ISR
|
|
|
|
// bits. (If it didn't, and we forgot to, the interrupt would
|
|
|
|
// repeatedly fire forever.)
|
2012-06-13 17:57:56 +02:00
|
|
|
serial->print("ISR bits: 0x");
|
|
|
|
serial->println(isr, HEX);
|
2011-06-20 19:24:11 +02:00
|
|
|
irq_fired = 0;
|
2011-04-08 04:23:01 +02:00
|
|
|
}
|
2012-06-13 17:57:16 +02:00
|
|
|
|
2012-06-13 19:31:12 +02:00
|
|
|
// Print the ISR bits.
|
2012-06-13 17:57:16 +02:00
|
|
|
//
|
|
|
|
// Notice that the "transfer half-complete" ISR flag gets set when
|
|
|
|
// we reach the rx_buf half-way point. This is true even though we
|
|
|
|
// don't tell the DMA controller to interrupt us on a
|
|
|
|
// half-complete transfer. That is, the ISR bits get set at the
|
|
|
|
// 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).
|
2012-06-13 17:57:56 +02:00
|
|
|
serial->print("[");
|
|
|
|
serial->print(millis());
|
|
|
|
serial->print("]\tISR bits: 0x");
|
2012-06-13 19:53:00 +02:00
|
|
|
uint8 isr_bits = dma_get_isr_bits(USART_DMA_DEV, USART_RX_DMA_TUBE);
|
2012-06-13 17:57:56 +02:00
|
|
|
serial->print(isr_bits, HEX);
|
2012-06-13 17:57:16 +02:00
|
|
|
|
|
|
|
// 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.
|
2012-06-13 17:57:56 +02:00
|
|
|
serial->print("\tCharacter buffer contents: '");
|
|
|
|
serial->print(rx_buf);
|
|
|
|
serial->println("'");
|
2011-04-08 04:23:01 +02:00
|
|
|
if (isr_bits == 0x7) {
|
2012-06-13 17:57:56 +02:00
|
|
|
serial->println("** Clearing ISR bits.");
|
2012-06-13 19:53:00 +02:00
|
|
|
dma_clear_isr_bits(USART_DMA_DEV, USART_RX_DMA_TUBE);
|
2011-04-08 04:23:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-13 17:57:16 +02:00
|
|
|
// ------- init() and main() --------------------------------------------------
|
2011-04-08 04:23:01 +02:00
|
|
|
|
|
|
|
// Force init to be called *first*, i.e. before static object allocation.
|
|
|
|
// Otherwise, statically allocated objects that need libmaple may fail.
|
|
|
|
__attribute__((constructor)) void premain() {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
setup();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|