mbed library for using USART DMA in STM32F401RE.

Dependencies:   mbed

Committer:
Match314
Date:
Fri Mar 06 11:06:18 2015 +0000
Revision:
0:1c6e43f7bfd5
Up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Match314 0:1c6e43f7bfd5 1 /* mbed library for using USART DMA in STM32F401RE.
Match314 0:1c6e43f7bfd5 2 * Copyright (c) 2015 Match
Match314 0:1c6e43f7bfd5 3 *
Match314 0:1c6e43f7bfd5 4 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Match314 0:1c6e43f7bfd5 5 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Match314 0:1c6e43f7bfd5 6 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Match314 0:1c6e43f7bfd5 7 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Match314 0:1c6e43f7bfd5 8 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Match314 0:1c6e43f7bfd5 9 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Match314 0:1c6e43f7bfd5 10 * THE SOFTWARE.
Match314 0:1c6e43f7bfd5 11 */
Match314 0:1c6e43f7bfd5 12
Match314 0:1c6e43f7bfd5 13 #include "mbed.h"
Match314 0:1c6e43f7bfd5 14
Match314 0:1c6e43f7bfd5 15 uint8_t data[] = "Hello.\n"
Match314 0:1c6e43f7bfd5 16 "How are you?\n"
Match314 0:1c6e43f7bfd5 17 "abcdefghijklmnopqrstuvwxyz\n"
Match314 0:1c6e43f7bfd5 18 "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
Match314 0:1c6e43f7bfd5 19 "See you.\n\n";
Match314 0:1c6e43f7bfd5 20
Match314 0:1c6e43f7bfd5 21 DMA_HandleTypeDef usartTxDma;
Match314 0:1c6e43f7bfd5 22 USART_HandleTypeDef usart;
Match314 0:1c6e43f7bfd5 23
Match314 0:1c6e43f7bfd5 24 void Init();
Match314 0:1c6e43f7bfd5 25
Match314 0:1c6e43f7bfd5 26 int main() {
Match314 0:1c6e43f7bfd5 27 Init();
Match314 0:1c6e43f7bfd5 28
Match314 0:1c6e43f7bfd5 29 HAL_USART_Transmit_DMA(&usart, data, sizeof(data));
Match314 0:1c6e43f7bfd5 30
Match314 0:1c6e43f7bfd5 31 while(1) {
Match314 0:1c6e43f7bfd5 32
Match314 0:1c6e43f7bfd5 33 }
Match314 0:1c6e43f7bfd5 34 }
Match314 0:1c6e43f7bfd5 35
Match314 0:1c6e43f7bfd5 36 void Init() {
Match314 0:1c6e43f7bfd5 37 // DMA
Match314 0:1c6e43f7bfd5 38 // USART2 TX is DMA1 channel 4 stream 6. Look at table 28 in STM32F401 reference manual.
Match314 0:1c6e43f7bfd5 39 __DMA1_CLK_ENABLE();
Match314 0:1c6e43f7bfd5 40 usartTxDma.Instance = DMA1_Stream6;
Match314 0:1c6e43f7bfd5 41 usartTxDma.Init.Channel = DMA_CHANNEL_4;
Match314 0:1c6e43f7bfd5 42 usartTxDma.Init.Direction = DMA_MEMORY_TO_PERIPH;
Match314 0:1c6e43f7bfd5 43 usartTxDma.Init.PeriphInc = DMA_PINC_DISABLE;
Match314 0:1c6e43f7bfd5 44 usartTxDma.Init.MemInc = DMA_MINC_ENABLE;
Match314 0:1c6e43f7bfd5 45 usartTxDma.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; // 8 bit
Match314 0:1c6e43f7bfd5 46 usartTxDma.Init.MemDataAlignment = DMA_PDATAALIGN_BYTE; // 8 bit
Match314 0:1c6e43f7bfd5 47 usartTxDma.Init.Mode = DMA_NORMAL;
Match314 0:1c6e43f7bfd5 48 usartTxDma.Init.Priority = DMA_PRIORITY_VERY_HIGH;
Match314 0:1c6e43f7bfd5 49 usartTxDma.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
Match314 0:1c6e43f7bfd5 50 HAL_DMA_Init(&usartTxDma);
Match314 0:1c6e43f7bfd5 51
Match314 0:1c6e43f7bfd5 52 // USART pin
Match314 0:1c6e43f7bfd5 53 __GPIOA_CLK_ENABLE();
Match314 0:1c6e43f7bfd5 54 GPIO_InitTypeDef usartTxd;
Match314 0:1c6e43f7bfd5 55 usartTxd.Pin = GPIO_PIN_2;
Match314 0:1c6e43f7bfd5 56 usartTxd.Mode = GPIO_MODE_AF_PP;
Match314 0:1c6e43f7bfd5 57 usartTxd.Pull = GPIO_NOPULL;
Match314 0:1c6e43f7bfd5 58 usartTxd.Speed = GPIO_SPEED_HIGH;
Match314 0:1c6e43f7bfd5 59 usartTxd.Alternate = GPIO_AF7_USART2;
Match314 0:1c6e43f7bfd5 60 HAL_GPIO_Init(GPIOA, &usartTxd);
Match314 0:1c6e43f7bfd5 61
Match314 0:1c6e43f7bfd5 62 // USART
Match314 0:1c6e43f7bfd5 63 __USART2_CLK_ENABLE();
Match314 0:1c6e43f7bfd5 64 usart.Init.BaudRate = 921600;
Match314 0:1c6e43f7bfd5 65 usart.Init.WordLength = USART_WORDLENGTH_8B;
Match314 0:1c6e43f7bfd5 66 usart.Init.StopBits = USART_STOPBITS_1;
Match314 0:1c6e43f7bfd5 67 usart.Init.Parity = USART_PARITY_NONE;
Match314 0:1c6e43f7bfd5 68 usart.Init.Mode = USART_MODE_TX; // transmit only mode
Match314 0:1c6e43f7bfd5 69 usart.Instance = USART2;
Match314 0:1c6e43f7bfd5 70 usart.hdmatx = &usartTxDma;
Match314 0:1c6e43f7bfd5 71 HAL_USART_Init(&usart);
Match314 0:1c6e43f7bfd5 72 }