Advertisement
Guest User

sas

a guest
Dec 12th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. MAKEFILE
  2.  
  3. BUILD_DIR = build
  4.  
  5. TARGET = lab1
  6.  
  7. SOURCES_S = \
  8. startup_code.s
  9.  
  10. SOURCES_C = \
  11. Src/main.c
  12.  
  13. LINKER_SCRIPT = linker_script.ld
  14.  
  15. PREFIX = C:\Users\Sladjan\Desktop\Tools\GnuMcu_Toolchain\GNU MCU Eclipse\ARM Embedded GCC\8.3.1-1.1\bin
  16.  
  17. CC = $(PREFIX)/arm-none-eabi-gcc.exe -c
  18. AS = $(PREFIX)/arm-none-eabi-as.exe
  19. LD = $(PREFIX)/arm-none-eabi-ld.exe
  20. OBJCOPY = $(PREFIX)/arm-none-eabi-objcopy.exe
  21.  
  22. MCU = -mcpu=cortex-m3 -mthumb
  23.  
  24. OBJECTS = $(addprefix $(BUILD_DIR)/, $(SOURCES_S:.s=.o))
  25. vpath %.s $(sort $(dir $(SOURCES_S)))
  26.  
  27. OBJECTS += $(addprefix $(BUILD_DIR)/, $(notdir $(SOURCES_C:.c=.o)))
  28. vpath %.c $(sort $(dir $(SOURCES_C)))
  29.  
  30. all : $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex
  31.  
  32. $(BUILD_DIR)/$(TARGET).elf : $(OBJECTS) makefile
  33. $(LD) -T $(LINKER_SCRIPT) -o $(@) $(OBJECTS)
  34.  
  35. $(BUILD_DIR)/$(TARGET).hex : $(BUILD_DIR)/$(TARGET).elf | $(BUILD_DIR)
  36. $(OBJCOPY) --output-target ihex $(<) $(@)
  37.  
  38. $(BUILD_DIR)/%.o : %.s makefile | $(BUILD_DIR)
  39. $(AS) $(MCU) -o $(@) $(<)
  40.  
  41. $(BUILD_DIR)/%.o : %.c makefile | $(BUILD_DIR)
  42. $(CC) $(MCU) -o $(@) $(<)
  43.  
  44. $(BUILD_DIR) :
  45. mkdir $(@)
  46.  
  47. clean :
  48. rm -rf $(BUILD_DIR)
  49.  
  50. LINKER_SCRIPT
  51.  
  52. _msp_stack_pointer = 0x20002800;
  53.  
  54. MEMORY
  55. {
  56. FLASH(rx) : ORIGIN = 0x08000000, LENGTH = 32K
  57. SRAM(rwx) : ORIGIN = 0x20000000, LENGTH = 10K
  58. }
  59.  
  60. SECTIONS
  61. {
  62. .output_vector_table :
  63. {
  64. *(.vector_table)
  65. } > FLASH
  66.  
  67. .output_text :
  68. {
  69. *(.text)
  70. *(.text*)
  71. } > FLASH
  72.  
  73. .output_data :
  74. {
  75. _vma_data_start = .;
  76. *(.data)
  77. *(.data*)
  78. _vma_data_end = .;
  79. } > SRAM AT > FLASH
  80.  
  81. _lma_data_start = LOADADDR(.output_data);
  82.  
  83. .output_bss :
  84. {
  85. *(.bss)
  86. *(.bss*)
  87. } > SRAM
  88. }
  89.  
  90. STARTUP_CODE_S
  91.  
  92. .syntax unified
  93. .cpu cortex-m3
  94. .fpu softvfp
  95. .thumb
  96.  
  97. .extern _msp_stack_pointer
  98.  
  99. .weak exti_1_handler
  100. .thumb_set exti_1_handler, default_handler
  101.  
  102. .weak tim1_update_handler
  103. .thumb_set tim1_update_handler, default_handler
  104.  
  105. .section .vector_table, "a"
  106. .word _msp_stack_pointer
  107. .word reset_handler
  108. .rept 21
  109. .word default_handler
  110. .endr
  111. .word exti_1_handler
  112. .rept 17
  113. .word default_handler
  114. .endr
  115. .word tim1_update_handler
  116. .rept 42
  117. .word default_handler
  118. .endr
  119.  
  120. .extern main
  121.  
  122. .extern _vma_data_start
  123. .extern _vma_data_end
  124. .extern _lma_data_start
  125.  
  126. .section .text.reset_handler
  127. .type reset_handler, %function
  128. reset_handler:
  129. ldr r0, = _lma_data_start
  130. ldr r1, = _vma_data_start
  131. ldr r2, = _vma_data_end
  132. cmp r1, r2
  133. beq branch_to_main
  134. copy_loop:
  135. ldr r3, [r0], #4
  136. str r3, [r1], #4
  137. cmp r1, r2
  138. blo copy_loop
  139. branch_to_main:
  140. b main
  141. infinite_loop_1:
  142. b infinite_loop_1
  143.  
  144. .section .text.default_handler
  145. .type default_handler, %function
  146. default_handler:
  147. infinite_loop_2:
  148. b infinite_loop_2
  149. MAIN_C
  150.  
  151. #include <stdint.h>
  152.  
  153. #define RCC_APB2ENR (*((uint32_t *) (0x40021000 + 0x18)))
  154. #define EXTI_IMR (*((uint32_t *) (0x40010400 + 0x00)))
  155. #define EXTI_RTSR (*((uint32_t *) (0x40010400 + 0x08)))
  156. #define EXTI_PR (*((uint32_t *) (0x40010400 + 0x14)))
  157. #define NVIC_ISER0 (*((uint32_t *) (0xE000E100 + 0x00)))
  158. #define GPIOC_CRL (*((uint32_t *) (0x40011000 + 0x00)))
  159. #define GPIOC_CRH (*((uint32_t *) (0x40011000 + 0x04)))
  160. #define GPIOC_ODR (*((uint32_t *) (0x40011000 + 0x0C)))
  161. #define GPIOA_CRL (*((uint32_t *) (0x40010800 + 0x00)))
  162. #define GPIOA_CRH (*((uint32_t *) (0x40010800 + 0x04)))
  163. #define GPIOA_ODR (*((uint32_t *) (0x40010800 + 0x0C)))
  164. #define AFIO_EXTICR1 (*((uint32_t *) (0x40010000 + 0x08)))
  165. #define TIM1_DIER (*((uint32_t *) (0x40012C00 + 0x0C)))
  166. #define TIM1_SR (*((uint32_t *) (0x40012C00 + 0x10)))
  167. #define TIM1_PSC (*((uint32_t *) (0x40012C00 + 0x28)))
  168. #define TIM1_ARR (*((uint32_t *) (0x40012C00 + 0x2C)))
  169. #define TIM1_CR1 (*((uint32_t *) (0x40012C00 + 0x00)))
  170.  
  171. void init_RCC()
  172. {
  173. RCC_APB2ENR |= (0x01 << 11) | (0x01 << 4) | (0x01 << 2);
  174. }
  175.  
  176. void init_EXTI()
  177. {
  178. EXTI_IMR |= 0x01 << 1;
  179. EXTI_RTSR |= 0x01 << 1;
  180. }
  181.  
  182. void init_NVIC()
  183. {
  184. NVIC_ISER0 = (0x01 << 25) | (0x01 << 7);
  185. }
  186.  
  187. void init_GPIOC()
  188. {
  189. uint32_t gpioc_crl_value = GPIOC_CRL;
  190. for(int i = 0; i < 8; i++)
  191. {
  192. gpioc_crl_value &= ~(0x0F << (4 * i));
  193. gpioc_crl_value |= 0x02 << (4 * i) | 0x00 << (2 + 4 * i);
  194. }
  195. GPIOC_CRL = gpioc_crl_value;
  196. uint32_t gpioc_crh_value = GPIOC_CRH;
  197. for(int i = 0; i < 4; i++)
  198. {
  199. gpioc_crh_value &= ~(0x0F << (4 * i));
  200. gpioc_crh_value |= 0x02 << (4 * i) | 0x00 << (2 + 4 * i);
  201. }
  202. GPIOC_CRH = gpioc_crh_value;
  203. GPIOC_ODR = 0x00;
  204. }
  205.  
  206. void init_GPIOA()
  207. {
  208. uint32_t gpioa_crl_value = GPIOA_CRL;
  209. gpioa_crl_value &= ~(0x0F << (4 * 1));
  210. gpioa_crl_value |= 0x00 << (4 * 1) | 0x02 << (2 + 4 * 1);
  211. gpioa_crl_value &= ~(0x0F << (4 * 2));
  212. gpioa_crl_value |= 0x02 << (4 * 2) | 0x00 << (2 + 4 * 2);
  213. GPIOA_CRL = gpioa_crl_value;
  214. GPIOA_ODR = 0x00;
  215. AFIO_EXTICR1 |= 0x00 << (4 * 1);
  216. }
  217.  
  218. void init_TIM1()
  219. {
  220. TIM1_DIER |= 0x01;
  221. TIM1_SR = 0;
  222. TIM1_PSC = 7999;
  223. TIM1_ARR = 2;
  224. TIM1_CR1 |= 0x01;
  225. }
  226.  
  227. uint8_t encoding[] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F };
  228. uint8_t digits[] = { 0, 0, 0, 0};
  229. uint8_t current_digit = 0;
  230. uint8_t sec = 0, min = 0;
  231. uint8_t interrupt_count = 0;
  232. uint8_t interrupt_dioda_count = 0;
  233.  
  234. void process_tim1_update_interrupt()
  235. {
  236. if(++interrupt_dioda_count == 50)
  237. {
  238. interrupt_dioda_count = 0;
  239. GPIOA_ODR ^= 0x04;
  240. }
  241.  
  242. if(++interrupt_count == 100)
  243. {
  244. interrupt_count = 0;
  245. if(++sec == 60)
  246. {
  247. sec = 0;
  248. if(++min == 60)
  249. {
  250. min = 0;
  251. }
  252. }
  253. digits[0] = min / 10;
  254. digits[1] = min % 10;
  255. digits[2] = sec / 10;
  256. digits[3] = sec % 10;
  257. }
  258. GPIOC_ODR |= 0x0F << 8;
  259. GPIOC_ODR &= ~0xFF;
  260. GPIOC_ODR |= encoding[digits[current_digit]];
  261. GPIOC_ODR &= ~(0x01 << (current_digit + 8));
  262. current_digit = (current_digit + 1) % 4;
  263. }
  264.  
  265. void exti_1_handler()
  266. {
  267. if((EXTI_PR & 0x02) != 0)
  268. {
  269. EXTI_PR = 0x02;
  270. sec = -1;
  271. min = 0;
  272. }
  273. }
  274.  
  275. void tim1_update_handler()
  276. {
  277. if((TIM1_SR & 0x01) != 0)
  278. {
  279. TIM1_SR = 0;
  280. process_tim1_update_interrupt();
  281. }
  282. }
  283.  
  284. int main()
  285. {
  286. init_RCC();
  287. init_EXTI();
  288. init_NVIC();
  289. init_GPIOC();
  290. init_GPIOA();
  291. init_TIM1();
  292. while(1) {}
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement