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

6107f1b19e4b0b25332d1656af10b712d5f7ee90 — Rutherther 3 months ago f31e760
chore: split main functions
1 files changed, 95 insertions(+), 98 deletions(-)

M src/main.c
M src/main.c => src/main.c +95 -98
@@ 15,83 15,17 @@
#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;
pin_t led1;
exti_t exti_wkup;

  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()
void init_fmc(fmc_t *fmc)
{
  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 -> need VCOSEL 1
  // HSE / 25 * 400 = FVCO = 400 MHz
  // PLL2 R = 200 MHz
  clocks_pll_configure(CLOCK_PLL2, 1, 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);
  fmc_init(fmc);

  #define BURST_LENGTH_1 0x0000
  #define BURST_TYPE_SEQUENTIAL 0x0000


@@ 260,35 194,12 @@ void main()
    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);
  }

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

  for (uint32_t i = 0; i < 1024; i++) {
    *(sdram_data + i) = i;
    fmc_sdram_configure(fmc, SDRAM_BANK2, sdram_config, sdram_timing, sdram_mode_register);
  }
}

  // 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();

usb_device_t* init_usb()
{
  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;


@@ 328,8 239,75 @@ void main()
  cdc_acm_configure(usb_dev, 512);
  usb_device_setup(usb_dev);

  return usb_dev;
}

void app_loop(usb_device_t* usb_dev);

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 -> need VCOSEL 1
  // HSE / 25 * 400 = FVCO = 400 MHz
  // PLL2 R = 200 MHz
  clocks_pll_configure(CLOCK_PLL2, 1, 25, PLL_SOURCE_HSE, 400, 2, 2, 2);
  clocks_pll_enable(CLOCK_PLL2);
  clocks_pll_wait_ready(CLOCK_PLL2, 300);

  fmc_t fmc;
  init_fmc(&fmc);

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

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

  // Pins init
  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_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();

  usb_device_t* usb_dev = init_usb();
  usb_device_wait_for_handshake(usb_dev);

  app_loop(usb_dev);
}

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


@@ 347,3 325,22 @@ void main()
    }
  }
}


// Interrupt handlers

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 exti15_10_handler(void)
{
  if (exti_is_interrupt(&exti_wkup)) {
    exti_clear_interrupt(&exti_wkup);
    pin_toggle(&led1);
  }
}

Do not follow this link