Game & Watch: The Legend of Zelda >> Assembly

UART


參考資訊:
1. pm_pdf
2. ds_pdf
3. game-and-watch-backup
4. game-and-watch-flashloader

UART使用到的腳位為PA-2(USART2_TX)、PA-3(USART2_RX)


USART2EN=1


USART234578SEL=3(HSI Clock)


TE=1、UE=1


Baudrate計算方式如下:

Baudrate = USART_KER_CKPRES/USART_DIV = 64000000/556 = 115108 ~= 115200



TC是傳送完成旗標


傳送資料


GPIOAEN=1


MODER2=2


AFR2=7(USART2)



main.s

  .equiv PORTA_BASE,    0x58020000
  .equiv PORTC_BASE,    0x58020800
  .equiv PORTE_BASE,    0x58021000
  .equiv GPIO_MODER,    0x0000
  .equiv GPIO_ODR,      0x0014
  .equiv GPIO_AFRL,     0x0020
 
  .equiv RCC_BASE,      0x58024400
  .equiv RCC_AHB4ENR,   0x0140
  .equiv RCC_APB4ENR,   0x0154
  .equiv RCC_CDCCIP2R,  0x0054
  .equiv RCC_APB1LENR,  0x0148

  .equiv USART2_BASE,   0x40004400
  .equiv USART_CR1,     0x0000
  .equiv USART_BRR,     0x000c
  .equiv USART_ISR,     0x001c
  .equiv USART_TDR,     0x0028

  .thumb
  .cpu cortex-m7
  .syntax unified
 
  .global _start
 
  .text
  .org 0x0000
_start:
  .word 0x20020000
  .word reset
 
  .org 0x0100
  .thumb_func
reset:
  ldr r0, =RCC_BASE
  ldr r1, [r0, #RCC_APB1LENR]
  orr r1, #(1 << 17)
  str r1, [r0, #RCC_APB1LENR]

  ldr r1, [r0, #RCC_AHB4ENR]
  orr r1, #(1 << 0)
  str r1, [r0, #RCC_AHB4ENR]

  ldr r0, =RCC_BASE
  ldr r1, [r0, #RCC_CDCCIP2R]
  bic r1, #0x07
  orr r1, #0x03
  str r1, [r0, #RCC_CDCCIP2R]

  ldr r0, =PORTA_BASE
  ldr r1, [r0, #GPIO_MODER]
  bic r1, #0xf0
  orr r1, #(2 << 4)
  str r1, [r0, #GPIO_MODER]

  ldr r1, [r0, #GPIO_AFRL]
  bic r1, #0xf00
  orr r1, #(7 << 8)
  str r1, [r0, #GPIO_AFRL]
 
  ldr r0, =USART2_BASE
  ldr r1, =0x22c
  str r1, [r0, #USART_BRR]
  ldr r1, [r0, #USART_CR1]
  orr r1, #0x09
  str r1, [r0, #USART_CR1]

  ldr r0, =USART2_BASE
  ldr r2, =hello
1:
  ldr r1, [r0, #USART_ISR]
  tst r1, #(1 << 6)
  beq 1b
  ldrb r1, [r2]
  strb r1, [r0, #USART_TDR]
  add r2, #1
  cmp r1, #0
  bne 1b
  b .

  .align
hello: .asciz "Hello, world!"
  .end

接線如下


完成


返回上一頁