PageRenderTime 29ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libopencm3/examples/stm32/f1/lisa-m-2/usart_dma/usart_dma.c

https://bitbucket.org/PhracturedBlue/deviation
C | 199 lines | 124 code | 41 blank | 34 comment | 10 complexity | 32603796b026cf04e0e2803d766c05f1 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /*
  2. * This file is part of the libopencm3 project.
  3. *
  4. * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
  5. *
  6. * This library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this library. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <libopencm3/stm32/f1/rcc.h>
  20. #include <libopencm3/stm32/f1/gpio.h>
  21. #include <libopencm3/stm32/usart.h>
  22. #include <libopencm3/stm32/f1/dma.h>
  23. #include <libopencm3/cm3/nvic.h>
  24. void clock_setup(void)
  25. {
  26. rcc_clock_setup_in_hse_12mhz_out_72mhz();
  27. /* Enable GPIOA, GPIOB, GPIOC clock. */
  28. rcc_peripheral_enable_clock(&RCC_APB2ENR,
  29. RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN |
  30. RCC_APB2ENR_IOPCEN);
  31. /* Enable clocks for GPIO port B (for GPIO_USART3_TX) and USART3. */
  32. rcc_peripheral_enable_clock(&RCC_APB1ENR,
  33. RCC_APB1ENR_USART2EN);
  34. /* Enable DMA1 clock */
  35. rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_DMA1EN);
  36. }
  37. void usart_setup(void)
  38. {
  39. /* Setup GPIO pin GPIO_USART2_TX and GPIO_USART2_RX. */
  40. gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
  41. GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
  42. gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
  43. GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);
  44. /* Setup UART parameters. */
  45. usart_set_baudrate(USART2, 38400);
  46. usart_set_databits(USART2, 8);
  47. usart_set_stopbits(USART2, USART_STOPBITS_1);
  48. usart_set_mode(USART2, USART_MODE_TX_RX);
  49. usart_set_parity(USART2, USART_PARITY_NONE);
  50. usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
  51. /* Finally enable the USART. */
  52. usart_enable(USART2);
  53. nvic_set_priority(NVIC_DMA1_CHANNEL7_IRQ, 0);
  54. nvic_enable_irq(NVIC_DMA1_CHANNEL7_IRQ);
  55. nvic_set_priority(NVIC_DMA1_CHANNEL6_IRQ, 0);
  56. nvic_enable_irq(NVIC_DMA1_CHANNEL6_IRQ);
  57. }
  58. void dma_write(char *data, int size)
  59. {
  60. /*
  61. * Using channel 7 for USART2_TX
  62. */
  63. /* Reset DMA channel*/
  64. dma_channel_reset(DMA1, DMA_CHANNEL7);
  65. dma_set_peripheral_address(DMA1, DMA_CHANNEL7, (u32)&USART2_DR);
  66. dma_set_memory_address(DMA1, DMA_CHANNEL7, (u32)data);
  67. dma_set_number_of_data(DMA1, DMA_CHANNEL7, size);
  68. dma_set_read_from_memory(DMA1, DMA_CHANNEL7);
  69. dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL7);
  70. dma_set_peripheral_size(DMA1, DMA_CHANNEL7, DMA_CCR_PSIZE_8BIT);
  71. dma_set_memory_size(DMA1, DMA_CHANNEL7, DMA_CCR_MSIZE_8BIT);
  72. dma_set_priority(DMA1, DMA_CHANNEL7, DMA_CCR_PL_VERY_HIGH);
  73. dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL7);
  74. dma_enable_channel(DMA1, DMA_CHANNEL7);
  75. usart_enable_tx_dma(USART2);
  76. }
  77. volatile int transfered = 0;
  78. void dma1_channel7_isr(void)
  79. {
  80. if ((DMA1_ISR &DMA_ISR_TCIF7) != 0) {
  81. DMA1_IFCR |= DMA_IFCR_CTCIF7;
  82. transfered = 1;
  83. }
  84. dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL7);
  85. usart_disable_tx_dma(USART2);
  86. dma_disable_channel(DMA1, DMA_CHANNEL7);
  87. }
  88. void dma_read(char *data, int size)
  89. {
  90. /*
  91. * Using channel 6 for USART2_RX
  92. */
  93. /* Reset DMA channel*/
  94. dma_channel_reset(DMA1, DMA_CHANNEL6);
  95. dma_set_peripheral_address(DMA1, DMA_CHANNEL6, (u32)&USART2_DR);
  96. dma_set_memory_address(DMA1, DMA_CHANNEL6, (u32)data);
  97. dma_set_number_of_data(DMA1, DMA_CHANNEL6, size);
  98. dma_set_read_from_peripheral(DMA1, DMA_CHANNEL6);
  99. dma_enable_memory_increment_mode(DMA1, DMA_CHANNEL6);
  100. dma_set_peripheral_size(DMA1, DMA_CHANNEL6, DMA_CCR_PSIZE_8BIT);
  101. dma_set_memory_size(DMA1, DMA_CHANNEL6, DMA_CCR_MSIZE_8BIT);
  102. dma_set_priority(DMA1, DMA_CHANNEL6, DMA_CCR_PL_HIGH);
  103. dma_enable_transfer_complete_interrupt(DMA1, DMA_CHANNEL6);
  104. dma_enable_channel(DMA1, DMA_CHANNEL6);
  105. usart_enable_rx_dma(USART2);
  106. }
  107. volatile int received = 0;
  108. void dma1_channel6_isr(void)
  109. {
  110. if ((DMA1_ISR &DMA_ISR_TCIF6) != 0) {
  111. DMA1_IFCR |= DMA_IFCR_CTCIF6;
  112. received = 1;
  113. }
  114. dma_disable_transfer_complete_interrupt(DMA1, DMA_CHANNEL6);
  115. usart_disable_rx_dma(USART2);
  116. dma_disable_channel(DMA1, DMA_CHANNEL6);
  117. }
  118. void gpio_setup(void)
  119. {
  120. /* Set GPIO8 (in GPIO port A) to 'output push-pull'. */
  121. gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
  122. GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
  123. }
  124. int main(void)
  125. {
  126. char tx[10] = "abcdefg\r\n";
  127. int tx_len = 10;
  128. char rx[7] = "bcdefg";
  129. int rx_len = 6;
  130. clock_setup();
  131. gpio_setup();
  132. usart_setup();
  133. transfered = 0;
  134. dma_write(tx, tx_len);
  135. received = 0;
  136. dma_read(rx, rx_len);
  137. /* Blink the LED (PA8) on the board with every transmitted byte. */
  138. while (1) {
  139. gpio_toggle(GPIOA, GPIO8); /* LED on/off */
  140. while ( transfered != 1) {
  141. if (received == 1) {
  142. tx[1] = rx[0];
  143. tx[2] = rx[1];
  144. tx[3] = rx[2];
  145. tx[4] = rx[3];
  146. tx[5] = rx[4];
  147. tx[6] = rx[5];
  148. received = 0;
  149. dma_read(rx, rx_len);
  150. }
  151. }
  152. tx[0]++;
  153. if (tx[0] > 'z') tx[0] = 'a';
  154. transfered = 0;
  155. dma_write(tx, tx_len);
  156. }
  157. return 0;
  158. }