aboutsummaryrefslogtreecommitdiff
path: root/02-usart/src/main.c
blob: b050d3e89934745fdf8ab4705ada6ebd2644de52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

#include "arch.h"
#include "core/clock.h"
#include "core/dma.h"
#include "core/gpio.h"
#include "core/system.h"
#include "core/usart.h"
#include "core/nvic.h"
#include "core/irq.h"

#include "peri/dma.h"
#include "delay.h"
#include "mem.h"
#include "spin.h"
#include "string.h"

/** Overrides the default systick irq handler. */
void on_systick()
{
  static int is_on = 0;

  __IO gpio_port_t* port_b = enable_gpio(GPIO_PORT_B);
  gpio_output_pin_t pin3 = set_gpio_pin_output(port_b, PIN_3);

  if (is_on) {
    pin_off(pin3);
  } else {
    pin_on(pin3);
  }

  is_on = ! is_on;
}

void setup_usart2(uint32_t baud_rate)
{
  __IO gpio_port_t* port_a = enable_gpio(GPIO_PORT_A);
  enable_hsi(&RCC, true);

  set_usart2_clock_src(&RCC, USART_CLK_SRC_HSI16);
  set_usart2_clock_enabled(&RCC, USART_CLK_SRC_HSI16);

  set_gpio_pin_mode(port_a, PIN_2, MODE_ALTERNATE);
  set_gpio_pin_mode(port_a, PIN_15, MODE_ALTERNATE);
  set_gpio_alternate_function(port_a, PIN_2, AFN_7);
  set_gpio_alternate_function(port_a, PIN_15, AFN_3);

  /* De-assert reset of USART2 */
  regset(RCC.apb1rst1_r, rcc_usart2rst, 0);

  USART2.c_r1 = 0;
  USART2.c_r2 = 0;
  USART2.c_r3 = 0;

  usart_set_divisor(&USART2, 16000000 / baud_rate);
}

#ifdef ARCH_STM32L4

/* Main function. This gets executed from the interrupt vector defined above. */
int main()
{
  /* Enable a higher clock frequency. */
  set_system_clock_MHz(80);

  setup_usart2(115200);
  regset(USART2.c_r1, usart_txeie, 1);
  regset(USART2.c_r1, usart_rxneie, 1);
  usart_enable_dma(&USART2, USART_ENABLE_TX);
  usart_set_enabled(&USART2, USART_ENABLE_TX | USART_ENABLE_RX);


  dma_opts_t opts = DEFAULT_DMA_OPTS;
  opts.transfer_complete_interrupt_enable = 1;
  int ec = 0;
  dma_mem2p_channel_t dma_chan =
    select_dma_channel_mem2p(DMA1_PERIPH_USART2_TX, &opts, &ec);
  enable_interrupt(dma_channel_get_interrupt(dma_chan.c_));

  if (ec) {
    usart_printf(&USART2, "Select DMA channel failed :( %d\n", ec);
    for (;;);
  }

  // const char* thing = "Good Thing This Works!";

  char* str = halloc(128);
  strcpy(str, "Hello, Heap!");

  usart_printf(&USART2, "DATA_SEGMENT_START %p\n", &DATA_SEGMENT_START);
  usart_printf(&USART2, "DATA_SEGMENT_STOP: %p\n", &DATA_SEGMENT_STOP);
  usart_printf(&USART2, "str at: %p\n", str);
  usart_printf(&USART2, "str: %s\n", str);
  // usart_printf(&USART2, "%s\n", thing);
  // regset(USART2.ic_r, usart_tccf, 1);
  // dma_mem2p_initiate_transfer(dma_chan, thing, strlen(thing));

  __IO gpio_port_t* port_b = enable_gpio(GPIO_PORT_B);
  gpio_output_pin_t pin3 = set_gpio_pin_output(port_b, PIN_3);
  pin_on(pin3);

  // usart_printf(&USART2, "Start Configuring Countdown!\n");

  /* Set the countdown to start from 1,000,0000. */
  // SCB.strv_r = 10000000;

  /* Enable interrupts. */
  // regset(SCB.stcs_r, scb_tickint, 1);

  /* Start the systick. */
  // regset(SCB.stcs_r, scb_enable, 1);

  // usart_printf(&USART2, "Start Countdown Started!\n");
}

#endif