~ruther/stm32h747i-disco-usb-image-viewer

ref: 8313b61dab467a7d9823e32aca9603f804ff3ca8 stm32h747i-disco-usb-image-viewer/src/main.c -rw-r--r-- 11.0 KiB
8313b61d — Rutherther feat: implement fmc sdram control 4 months ago
                                                                                
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <stdint.h>
#include <stm32h747xx.h>
#include <core_cm7.h>
#include <stdlib.h>
#include "delay.h"
#include "usb_device.h"
#include "usb_device_cdc.h"
#include "clocks.h"
#include "exti.h"
#include "registers.h"
#include "pin.h"
#include "fmc.h"

#define LED1_GPIO GPIOI
#define LED1_PIN 12
/* #define LED2_PIN 13 */

void hard_fault_handler() {
  volatile uint32_t cfsr = SCB->CFSR;
  volatile uint32_t hfsr = SCB->HFSR;
  volatile uint32_t bfar = SCB->BFAR;

  while(1) {}
}

void usage_fault_handler() {
  while(1) {}
}

void nmi_handler() {
  while(1) {}
}

void bus_fault_handler() {
  while(1) {}
}

void led_toggle()
{
  GPIOI->ODR ^= (1 << LED1_PIN);
}

void led_input()
{
  GPIOI->MODER = (1 << LED1_PIN*2);
}

void led_gpio_en()
{
  RCC->AHB4ENR |= (1 << RCC_AHB4ENR_GPIOIEN_Pos);
}

void exti15_10_handler(void)
{
  if (EXTI->PR1 & EXTI_PR1_PR13) {
    led_toggle();
    EXTI->PR1 &= EXTI_PR1_PR13;
  }
}

void main()
{
  clocks_wait_ready(CLOCK_HSI);

  // Clock gating
  RCC->APB4ENR |= RCC_APB4ENR_SYSCFGEN;
  RCC->AHB4ENR |= RCC_AHB4ENR_GPIOAEN | RCC_AHB4ENR_GPIOBEN |
                  RCC_AHB4ENR_GPIOCEN | RCC_AHB4ENR_GPIODEN |
                  RCC_AHB4ENR_GPIOEEN | RCC_AHB4ENR_GPIOFEN |
                  RCC_AHB4ENR_GPIOGEN | RCC_AHB4ENR_GPIOHEN |
                  RCC_AHB4ENR_GPIOIEN | RCC_AHB4ENR_GPIOJEN;

  systick_configure();

  // Clocks section
  // Enable hsi48 for usb
  clocks_enable(CLOCK_HSI48);
  clocks_wait_ready(CLOCK_HSI48);

  clocks_enable(CLOCK_HSE);
  clocks_wait_ready(CLOCK_HSE);

  // HSE 25 MHz -> divide by 25 -> 1 MHz
  // HSE / 25 * 400 = FVCO = 400 MHz
  // PLL2 R = 200 MHz
  reg_set_bits(&RCC->PLLCFGR, RCC_PLLCFGR_PLL3VCOSEL_Msk);
  clocks_pll_configure(CLOCK_PLL2, 25, PLL_SOURCE_HSE, 400, 2, 2, 2);
  clocks_pll_enable(CLOCK_PLL2);
  clocks_pll_wait_ready(CLOCK_PLL2, 300);

  fmc_t fmc;
  // select pll 2 r as fmc clock
  reg_write_bits(&RCC->D1CCIPR, 2 << (RCC_D1CCIPR_FMCSEL_Pos), RCC_D1CCIPR_FMCSEL_Msk);
  RCC->AHB3ENR |= RCC_AHB3ENR_FMCEN;
  fmc_init(&fmc);

  #define BURST_LENGTH_1 0x0000
  #define BURST_TYPE_SEQUENTIAL 0x0000
  #define CAS_LATENCY_3 0x0030
  #define OPERATING_MODE_STANDARD 0x0000
  #define WRITEBURST_MODE_SINGLE 0x0200
  uint16_t sdram_mode_register = BURST_LENGTH_1 | BURST_TYPE_SEQUENTIAL |
                                 CAS_LATENCY_3 | OPERATING_MODE_STANDARD |
                                 WRITEBURST_MODE_SINGLE;

  fmc_sdram_timing_t sdram_timing = {
    .startup_delay_us =  100,        // 100 µs
    .max_sd_clock_hz =  100000000,   // 100 MHz
    .refresh_period_ns =  15625,     // 64ms / (4096 rows) = 15625ns
    .mode_register_to_active =  2,   // tMRD = 2 cycles
    .exit_self_refresh =  7,         // tXSR = 70ns
    .active_to_precharge =  4,       // tRAS = 42ns
    .row_cycle =  7,                 // tRC = 70ns
    .row_precharge =  2,             // tRP = 18ns
    .row_to_column =  2,             // tRCD = 18ns
  };
  fmc_sdram_configuration_t sdram_config = {
    .column_bits =  9,
    .row_bits =  12,
    .memory_data_width =  32, // 32-bit
    .internal_banks =  4,     // 4 internal banks
    .cas_latency =  3,        // CAS latency = 3
    .write_protection =  false,
    .read_burst =  true,
    .read_pipe_delay_cycles =  0,
  };

  {
    pin_t pin_addr[12];
    pin_t pin_data[32];
    pin_t pin_ba[2];
    pin_t pin_nbl[4];
    pin_t pin_sdcke1;
    pin_t pin_sdclk;
    pin_t pin_sdncas;
    pin_t pin_sdne1;
    pin_t pin_sdras;
    pin_t pin_sdnwe;

    // A0-A11
    pin_init(&pin_addr[0], GPIOF, 0);
    pin_init(&pin_addr[1], GPIOF, 1);
    pin_init(&pin_addr[2], GPIOF, 2);
    pin_init(&pin_addr[3], GPIOF, 3);
    pin_init(&pin_addr[4], GPIOF, 4);
    pin_init(&pin_addr[5], GPIOF, 5);
    pin_init(&pin_addr[6], GPIOF, 12);
    pin_init(&pin_addr[7], GPIOF, 13);
    pin_init(&pin_addr[8], GPIOF, 14);
    pin_init(&pin_addr[9], GPIOF, 15);
    pin_init(&pin_addr[10], GPIOG, 0);
    pin_init(&pin_addr[11], GPIOG, 1);
    // BA0-BA1
    pin_init(&pin_ba[0], GPIOG, 4);
    pin_init(&pin_ba[1], GPIOG, 5);
    // D0-D31
    pin_init(&pin_data[0], GPIOD, 14);
    pin_init(&pin_data[1], GPIOD, 15);
    pin_init(&pin_data[2], GPIOD, 0);
    pin_init(&pin_data[3], GPIOD, 1);
    pin_init(&pin_data[4], GPIOE, 7);
    pin_init(&pin_data[5], GPIOE, 8);
    pin_init(&pin_data[6], GPIOE, 9);
    pin_init(&pin_data[7], GPIOE, 10);
    pin_init(&pin_data[8], GPIOE, 11);
    pin_init(&pin_data[9], GPIOE, 12);
    pin_init(&pin_data[10], GPIOE, 13);
    pin_init(&pin_data[11], GPIOE, 14);
    pin_init(&pin_data[12], GPIOE, 15);
    pin_init(&pin_data[13], GPIOD, 8);
    pin_init(&pin_data[14], GPIOD, 9);
    pin_init(&pin_data[15], GPIOD, 10);
    pin_init(&pin_data[16], GPIOH, 8);
    pin_init(&pin_data[17], GPIOH, 9);
    pin_init(&pin_data[18], GPIOH, 10);
    pin_init(&pin_data[19], GPIOH, 11);
    pin_init(&pin_data[20], GPIOH, 12);
    pin_init(&pin_data[21], GPIOH, 13);
    pin_init(&pin_data[22], GPIOH, 14);
    pin_init(&pin_data[23], GPIOH, 15);
    pin_init(&pin_data[24], GPIOI, 0);
    pin_init(&pin_data[25], GPIOI, 1);
    pin_init(&pin_data[26], GPIOI, 2);
    pin_init(&pin_data[27], GPIOI, 3);
    pin_init(&pin_data[28], GPIOI, 6);
    pin_init(&pin_data[29], GPIOI, 7);
    pin_init(&pin_data[30], GPIOI, 9);
    pin_init(&pin_data[31], GPIOI, 10);

    pin_init(&pin_nbl[0], GPIOE, 0);
    pin_init(&pin_nbl[1], GPIOE, 1);
    pin_init(&pin_nbl[2], GPIOI, 4);
    pin_init(&pin_nbl[3], GPIOI, 5);

    pin_init(&pin_sdcke1, GPIOH, 7);
    pin_init(&pin_sdclk, GPIOG, 8);
    pin_init(&pin_sdncas, GPIOG, 15);
    pin_init(&pin_sdne1, GPIOH, 6);
    pin_init(&pin_sdras, GPIOF, 11);
    pin_init(&pin_sdnwe, GPIOH, 5);

    // A0-A11
    pin_into_alternate_highspeed(&pin_addr[0], 12);
    pin_into_alternate_highspeed(&pin_addr[1], 12);
    pin_into_alternate_highspeed(&pin_addr[2], 12);
    pin_into_alternate_highspeed(&pin_addr[3], 12);
    pin_into_alternate_highspeed(&pin_addr[4], 12);
    pin_into_alternate_highspeed(&pin_addr[5], 12);
    pin_into_alternate_highspeed(&pin_addr[6], 12);
    pin_into_alternate_highspeed(&pin_addr[7], 12);
    pin_into_alternate_highspeed(&pin_addr[8], 12);
    pin_into_alternate_highspeed(&pin_addr[9], 12);
    pin_into_alternate_highspeed(&pin_addr[10], 12);
    pin_into_alternate_highspeed(&pin_addr[11], 12);
    // BA0-BA1
    pin_into_alternate_highspeed(&pin_ba[0], 12);
    pin_into_alternate_highspeed(&pin_ba[1], 12);
    // D0-D31
    pin_into_alternate_highspeed(&pin_data[0], 12);
    pin_into_alternate_highspeed(&pin_data[1], 12);
    pin_into_alternate_highspeed(&pin_data[2], 12);
    pin_into_alternate_highspeed(&pin_data[3], 12);
    pin_into_alternate_highspeed(&pin_data[4], 12);
    pin_into_alternate_highspeed(&pin_data[5], 12);
    pin_into_alternate_highspeed(&pin_data[6], 12);
    pin_into_alternate_highspeed(&pin_data[7], 12);
    pin_into_alternate_highspeed(&pin_data[8], 12);
    pin_into_alternate_highspeed(&pin_data[9], 12);
    pin_into_alternate_highspeed(&pin_data[10], 12);
    pin_into_alternate_highspeed(&pin_data[11], 12);
    pin_into_alternate_highspeed(&pin_data[12], 12);
    pin_into_alternate_highspeed(&pin_data[13], 12);
    pin_into_alternate_highspeed(&pin_data[14], 12);
    pin_into_alternate_highspeed(&pin_data[15], 12);
    pin_into_alternate_highspeed(&pin_data[16], 12);
    pin_into_alternate_highspeed(&pin_data[17], 12);
    pin_into_alternate_highspeed(&pin_data[18], 12);
    pin_into_alternate_highspeed(&pin_data[19], 12);
    pin_into_alternate_highspeed(&pin_data[20], 12);
    pin_into_alternate_highspeed(&pin_data[21], 12);
    pin_into_alternate_highspeed(&pin_data[22], 12);
    pin_into_alternate_highspeed(&pin_data[23], 12);
    pin_into_alternate_highspeed(&pin_data[24], 12);
    pin_into_alternate_highspeed(&pin_data[25], 12);
    pin_into_alternate_highspeed(&pin_data[26], 12);
    pin_into_alternate_highspeed(&pin_data[27], 12);
    pin_into_alternate_highspeed(&pin_data[28], 12);
    pin_into_alternate_highspeed(&pin_data[29], 12);
    pin_into_alternate_highspeed(&pin_data[30], 12);
    pin_into_alternate_highspeed(&pin_data[31], 12);

    pin_into_alternate_highspeed(&pin_nbl[0], 12);
    pin_into_alternate_highspeed(&pin_nbl[1], 12);
    pin_into_alternate_highspeed(&pin_nbl[2], 12);
    pin_into_alternate_highspeed(&pin_nbl[3], 12);

    pin_into_alternate_highspeed(&pin_sdcke1, 12);
    pin_into_alternate_highspeed(&pin_sdclk, 12);
    pin_into_alternate_highspeed(&pin_sdncas, 12);
    pin_into_alternate_highspeed(&pin_sdne1, 12);
    pin_into_alternate_highspeed(&pin_sdras, 12);
    pin_into_alternate_highspeed(&pin_sdnwe, 12);

    fmc_sdram_configure(&fmc, SDRAM_BANK2, sdram_config, sdram_timing, sdram_mode_register);
  }

  uint32_t* sdram_data = (uint32_t*)fmc_sdram_allocate(&fmc, SDRAM_BANK2, 1024);

  for (uint32_t i = 0; i < 1024/4; i++) {
    *(sdram_data + i) = i;
  }

  // Pins init
  pin_t led1;
  pin_init(&led1, LED1_GPIO, LED1_PIN);
  pin_into_output_pushpull(&led1);
  pin_toggle(&led1);
  pin_toggle(&led1);

  // pc13 input - wakeup button
  pin_t wkup;
  pin_init(&wkup, GPIOC, 13);
  pin_into_input(&wkup);

  exti_t exti_wkup;
  exti_init(&exti_wkup, 13, EXTI, SYSCFG);
  exti_external_interrupt(&exti_wkup, EXTI_GPIOC);
  exti_rising_interrupt(&exti_wkup);
  exti_enable_interrupt(&exti_wkup);

  __enable_irq();

  pin_t ulpi_clk, ulpi_stp, ulpi_dir, ulpi_nxt,
    ulpi_d0, ulpi_d1, ulpi_d2, ulpi_d3, ulpi_d4,
    ulpi_d5, ulpi_d6, ulpi_d7, otg_hs_overcurrent;
  pin_init(&ulpi_clk, GPIOA, 5);
  pin_init(&ulpi_stp, GPIOC, 0);
  pin_init(&ulpi_dir, GPIOI, 11);
  pin_init(&ulpi_nxt, GPIOH, 4);

  pin_init(&ulpi_d0, GPIOA, 3);
  pin_init(&ulpi_d1, GPIOB, 0);
  pin_init(&ulpi_d2, GPIOB, 1);
  pin_init(&ulpi_d3, GPIOB, 10);
  pin_init(&ulpi_d4, GPIOB, 11);
  pin_init(&ulpi_d5, GPIOB, 12);
  pin_init(&ulpi_d6, GPIOB, 13);
  pin_init(&ulpi_d7, GPIOB, 5);
  pin_init(&otg_hs_overcurrent, GPIOJ, 1);

  pin_into_alternate_highspeed(&ulpi_clk, 10);
  pin_into_alternate_highspeed(&ulpi_stp, 10);
  pin_into_alternate_highspeed(&ulpi_dir, 10);
  pin_into_alternate_highspeed(&ulpi_nxt, 10);
  pin_into_alternate_highspeed(&ulpi_d0, 10);
  pin_into_alternate_highspeed(&ulpi_d1, 10);
  pin_into_alternate_highspeed(&ulpi_d2, 10);
  pin_into_alternate_highspeed(&ulpi_d3, 10);
  pin_into_alternate_highspeed(&ulpi_d4, 10);
  pin_into_alternate_highspeed(&ulpi_d5, 10);
  pin_into_alternate_highspeed(&ulpi_d6, 10);
  pin_into_alternate_highspeed(&ulpi_d7, 10);

  pin_into_input_highspeed(&otg_hs_overcurrent);

  void *usb_dev = usb_device_init(USB_OTG_HS1, &USB_CLASS_CDC_ACM,
                                  0x1234, 0x1111, u"Frantisek Bohacek",
                                  u"Display", 1, NULL);
  cdc_acm_configure(usb_dev, 512);
  usb_device_setup(usb_dev);

  usb_device_wait_for_handshake(usb_dev);

  uint8_t data[64];
  while (1) {
    uint16_t received = cdc_data_receive(usb_dev, data, 64);

    for (uint16_t i = 0; i < received; i++) {
      data[i] = data[i] + 1;
    }

    if (received > 0) {
      cdc_data_send_blocking(usb_dev, data, received);
    }

    if (pin_read(&wkup)) {
      cdc_data_send_blocking(usb_dev, (uint8_t*)"Hello world!\r\n", 0);
    }
  }
}
Do not follow this link