Skip to content

Commit 76dfabd

Browse files
committed
♻️ code refactor
1 parent 45e6dda commit 76dfabd

File tree

12 files changed

+729
-42
lines changed

12 files changed

+729
-42
lines changed

dma/.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{
88
"name": "Cortex Debug",
99
"cwd": "${workspaceFolder}",
10-
"executable": "./build/blink.elf",
10+
"executable": "./build/dma-usart.elf",
1111
"request": "launch",
1212
"type": "cortex-debug",
1313
"runToEntryPoint": "main",

dma/Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CXXSTD := c++17
1515

1616
# Project specific configuration
1717
BUILD_DIR := build
18-
BUILD_TYPE ?= Debug
18+
BUILD_TYPE ?= Release
1919
SRC_DIR := src
2020
INC_DIRS = include
2121

@@ -48,12 +48,14 @@ INC_FLAGS = $(addprefix -I,$(INC_DIRS))
4848
CPU_FLAGS ?= -mfloat-abi=$(FLOAT_ABI) -m$(INSTR_SET) -mcpu=$(CPU)
4949

5050
CPPFLAGS ?=$(DEFS) $(INC_FLAGS)
51-
CFLAGS ?=$(CPU_FLAGS) $(OPT)
51+
CFLAGS ?=$(CPU_FLAGS)
5252
CXXFLAGS :=$(CFLAGS) -fno-exceptions -fno-rtti
5353
LDFLAGS ?=$(CPU_FLAGS)
5454

5555
ifeq ($(BUILD_TYPE), Debug)
56-
CFLAGS += -g -gdwarf-2
56+
CFLAGS += -g -gdwarf-2
57+
else
58+
CFLAGS += $(OPT)
5759
endif
5860

5961
# Do not link stdlib with executable
@@ -70,7 +72,7 @@ CXXFLAGS += -Wall -Wextra -Wundef -Wshadow -Wredundant-decls -Weffc++ -Werror
7072
LDFLAGS += -T $(LDSCRIPT)
7173
LDFLAGS += -Wl,-Map=$(basename $@).map,--gc-sections,-cref,--print-memory-usage
7274

73-
all: bin size
75+
all: size bin
7476
size: $(BUILD_DIR)/$(TARGET).size
7577
elf: $(BUILD_DIR)/$(TARGET).elf
7678
bin: $(BUILD_DIR)/$(TARGET).bin

dma/include/delay.h

Lines changed: 0 additions & 7 deletions
This file was deleted.

dma/include/timer.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
#include<stdint.h>
3+
4+
/**
5+
* @brief Stores number of systick interrupts
6+
*
7+
*/
8+
extern volatile uint32_t msTicks;
9+
10+
11+
/**
12+
* @brief Add blocking delay
13+
*
14+
* @param ms delay in milliseconds
15+
*/
16+
void delay(uint32_t ms);
17+
18+
19+
/**
20+
* @brief Interrupt handler function
21+
*
22+
*/
23+
void SysTick_Handler(void);

dma/include/uart.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#pragma once
2+
3+
/**
4+
* @brief Initialize USART peripheral
5+
* Enable clock source for USART peripheral and associated GPIO port
6+
*/
7+
static inline void USART_init(USART_TypeDef *pusart)
8+
{
9+
if(pusart == USART1){
10+
// enable clock for GPIOA and USART1
11+
RCC->APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_IOPAEN;
12+
13+
// reset pin configurations for PA9 and PA10
14+
GPIOA->CRH &= ~(GPIO_CRH_MODE10 | GPIO_CRH_MODE9 | GPIO_CRH_CNF10 | GPIO_CRH_CNF9);
15+
16+
// PA9 as Output@50Hz Alternate function
17+
GPIOA->CRH |= GPIO_CRH_MODE9_0 | GPIO_CRH_MODE9_1 | GPIO_CRH_CNF9_1;
18+
19+
// PA10 as floating input
20+
GPIOA->CRH |= GPIO_CRH_CNF10_0;
21+
}
22+
23+
}
24+
25+
/**
26+
* @brief Enable clock for complete USART peripheral
27+
*
28+
* @param pusart USART instance
29+
*/
30+
static inline USART_enable(USART_TypeDef *pusart){
31+
pusart->CR1 |= USART_CR1_UE;
32+
}
33+
34+
35+
/**
36+
* @brief Enable clock for complete USART peripheral
37+
*
38+
* @param pusart USART instance
39+
*/
40+
static inline USART_disable(USART_TypeDef *pusart){
41+
pusart->CR1 &= ~USART_CR1_UE;
42+
}
43+
44+
/**
45+
* @brief Enable clock for complete USART peripheral
46+
*
47+
* @param pusart USART instance
48+
*/
49+
USART_set_baud(USART_TypeDef *pusart, uint32_t baudrate){
50+
uint32_t baud = (uint32_t)(SystemCoreClock / baudrate);
51+
USART1->BRR = baud;
52+
}
53+
54+
/**
55+
* @brief Enable transmitter clock
56+
*
57+
* @param pusart USART instance
58+
*/
59+
static inline USART_tx_enable(USART_TypeDef *pusart){
60+
pusart->CR1 |= USART_CR1_TE;
61+
}
62+
63+
/**
64+
* @brief Enable dma requests from transmitter
65+
*
66+
* @param pusart USART instance
67+
*/
68+
static inline USART_tx_dma_enable(USART_TypeDef *pusart){
69+
pusart->CR3 |= USART_CR3_DMAT;
70+
}
71+
/**
72+
* @brief Enable clock for complete USART peripheral
73+
*
74+
* @param pusart USART instance
75+
*/
76+
static inline USART_tx_dma_disable(USART_TypeDef *pusart){
77+
pusart->CR3 &= ~USART_CR3_DMAT;
78+
}
79+
80+
/**
81+
* @brief Enable receiver buffer
82+
*
83+
* @param pusart USART instance
84+
*/
85+
static inline USART_rx_enable(USART_TypeDef *pusart){
86+
pusart->CR1 |= USART_CR1_RE;
87+
}
88+
89+
/**
90+
* @brief Enable clock for complete USART peripheral
91+
*
92+
* @param pusart USART instance
93+
*/
94+
static inline USART_rx_interrupt_enable(USART_TypeDef *pusart){
95+
pusart->CR1 |= USART_CR1_TXEIE;
96+
}
97+
98+
/**
99+
* @brief Enable clock for complete USART peripheral
100+
*
101+
* @param pusart USART instance
102+
*/
103+
static inline USART_rx_dma_enable(USART_TypeDef *pusart){
104+
pusart->CR3 | USART_CR3_DMAR;
105+
}
106+
107+
/**
108+
* @brief Transmit 1 character
109+
*
110+
* @param c character to be transmitted
111+
*/
112+
void USART1_putch(char c);
113+
114+
/**
115+
* @brief Transmit string
116+
*
117+
* @param str pointer to the string
118+
*/
119+
void USART1_puts(const char *str);
120+
121+
/**
122+
* @brief Interrupt service routine for USART1 related interrupts
123+
*
124+
*/
125+
void USART1_IRQHandler(void);

dma/main.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
******************************************************************************
3+
* @file : main.c
4+
* @author : Rohit Nimkar <nehalnimkar@gmail.com> <https://csrohit.github.io>
5+
* @brief : Main program body
6+
******************************************************************************
7+
*/
8+
9+
#include <stm32f1xx.h>
10+
#include <uart.h>
11+
void SysTick_Handler(void);
12+
void delay(int ms);
13+
14+
15+
volatile uint32_t msTicks = 0;
16+
17+
/**
18+
* @brief Interrupt handler function
19+
*
20+
*/
21+
void SysTick_Handler(void)
22+
{
23+
msTicks++;
24+
}
25+
26+
/**
27+
* @brief Add blocking delay
28+
*
29+
* @param ms delay in milliseconds
30+
*/
31+
void delay(int ms)
32+
{
33+
uint32_t expected_ticks = msTicks + ms;
34+
while (msTicks < expected_ticks)
35+
{
36+
__asm("nop");
37+
}
38+
}
39+
40+
const char * msg = "Hello world\n\r\0";
41+
42+
43+
int main(void)
44+
{
45+
USART1_init(9600U);
46+
SysTick_Config(SystemCoreClock/1000);
47+
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
48+
GPIOC->CRH = 0x02 << ((13 - 8) << 2);
49+
while(1){
50+
GPIOC->ODR ^= 1 << 13;
51+
USART1_puts(msg);
52+
delay(5000U);
53+
}
54+
}

dma/src/delay.c

Lines changed: 0 additions & 21 deletions
This file was deleted.

dma/src/main.c

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,74 @@
44
* @author : Rohit Nimkar <nehalnimkar@gmail.com> <https://csrohit.github.io>
55
* @brief : Main program body
66
******************************************************************************
7+
* @attention
8+
*
9+
* This software component is licensed by Rohit Nimkar under BSD 3-Clause license,
10+
* the "License"; You may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at:
12+
* opensource.org/licenses/BSD-3-Clause
13+
*
14+
******************************************************************************
715
*/
8-
16+
#include <stdint.h>
17+
#include <stdio.h>
918
#include <stm32f1xx.h>
10-
#include <delay.h>
1119

20+
#include <timer.h>
21+
#include <uart.h>
1222

23+
char msg[28] = "Hello world\r\n\0";
1324

1425
int main(void)
1526
{
16-
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
17-
GPIOC->CRH = 0x02 << ((13 - 8) << 2);
18-
while(1){
19-
GPIOC->ODR ^= 1 << 13;
20-
ms_delay(1000);
27+
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
28+
29+
DMA1_Channel4->CPAR = (uint32_t) &USART1->DR;
30+
DMA1_Channel4->CMAR = (uint32_t) msg;
31+
DMA1_Channel4->CNDTR = 13U;
32+
DMA1_Channel4->CCR |= DMA_CCR_MINC | DMA_CCR_CIRC | DMA_CCR_DIR;
33+
34+
// enable clock for GPIOA and USART1
35+
RCC->APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_IOPAEN;
36+
37+
// reset pin configurations for PA9 and PA10
38+
GPIOA->CRH &= ~(GPIO_CRH_MODE10 | GPIO_CRH_MODE9 | GPIO_CRH_CNF10 | GPIO_CRH_CNF9);
39+
40+
// PA9 as Output@50Hz Alternate function
41+
GPIOA->CRH |= GPIO_CRH_MODE9_0 | GPIO_CRH_MODE9_1 | GPIO_CRH_CNF9_1;
42+
43+
// PA10 as floating input
44+
GPIOA->CRH |= GPIO_CRH_CNF10_0;
45+
46+
uint32_t baud = (uint32_t)(SystemCoreClock / 9600);
47+
48+
USART1->BRR = baud;
49+
50+
USART1->CR3 |= USART_CR3_DMAT;
51+
52+
DMA1_Channel4->CCR |= DMA_CCR_EN;
53+
// transmitter enable, receiver enable, receiver interrupt enable and USART enable
54+
USART1->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE | USART_CR1_UE;
55+
56+
57+
58+
// enable USART1 interrupt
59+
// NVIC_EnableIRQ(USART1_IRQn);
60+
61+
62+
63+
// USART1_init(9600U);
64+
65+
int ret = SysTick_Config(SystemCoreClock/1000);
66+
if (ret < 0)
67+
while (1)
68+
;
69+
70+
while (1)
71+
{
72+
// USART1_puts(msg);
73+
// delay(5000U);
2174
}
75+
76+
2277
}

dma/src/startup_stm32f1.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void DMA2_Channel4_5_IRQHandler(void) __attribute__((weak, alias("Default_Handle
8989

9090
// Define the veector table
9191
uint32_t vectors[] __attribute__((section(".isr_vector"))) = {
92-
&_stack_top,
92+
(uint32_t)&_stack_top,
9393
(uint32_t)Reset_Handler,
9494
(uint32_t)NMI_Handler,
9595
(uint32_t)HardFault_Handler,
@@ -193,7 +193,7 @@ void Reset_Handler(void)
193193
{
194194
*pBssData++ = 0;
195195
}
196-
196+
SystemCoreClockUpdate();
197197
// now invoke main
198198
main();
199199
}

0 commit comments

Comments
 (0)