/ src / bsp / bridge_v1_0 / bsp.cpp
bsp.cpp
 1  #include "bsp.h"
 2  #include "FreeRTOS.h"
 3  #include "task.h"
 4  
 5  #include "i2c.h"
 6  #include "io.h"
 7  #include "main.h"
 8  #include "pca9535.h"
 9  #include "spi.h"
10  #include "stm32_io.h"
11  // Peripheral includes for MX_ init functions
12  #include "gpdma.h"
13  #include "gpio.h"
14  #include "icache.h"
15  #include "iwdg.h"
16  #include "usart.h"
17  #include "usb_otg.h"
18  
19  extern __IO uint32_t uwTick;
20  static bool osStarted = false;
21  
22  uint32_t HAL_GetTick(void) {
23    if(osStarted) {
24      return xTaskGetTickCount();
25    } else {
26      return uwTick;
27    }
28  }
29  
30  void HAL_Delay(uint32_t Delay) {
31    if(osStarted) {
32      vTaskDelay(Delay);
33    } else {
34      uint32_t tickstart = HAL_GetTick();
35      uint32_t wait = Delay;
36  
37      /* Add a period to guaranty minimum wait */
38      if (wait < HAL_MAX_DELAY) {
39        wait += (uint32_t)uwTickFreq;
40      }
41  
42      while ((HAL_GetTick() - tickstart) < wait) {};
43    }
44  }
45  
46  extern SPI_HandleTypeDef hspi2;
47  extern SPI_HandleTypeDef hspi3;
48  SPIInterface_t spi2 = PROTECTED_SPI("SPI2", hspi2, MX_SPI2_Init, LPM_SPI2);
49  SPIInterface_t spi3 = PROTECTED_SPI("SPI3", hspi3, MX_SPI3_Init, LPM_SPI3);
50  
51  extern I2C_HandleTypeDef hi2c1;
52  I2CInterface_t i2c1 = PROTECTED_I2C("I2C1", hi2c1, MX_I2C1_Init, LPM_I2C1);
53  
54  adin_pins_t adin_pins = {&spi3, &ADIN_CS, &ADIN_INT, &ADIN_RST};
55  
56  void bspInit() {
57    // Switch HAL_GetTick to use freeRTOS tick
58    osStarted = true;
59    HAL_SuspendTick();
60  
61    spiInit(&spi2);
62    spiInit(&spi3);
63    i2cInit(&i2c1);
64  
65    // Turn on Adin2111
66    IOWrite(&ADIN_PWR, 1);
67  
68  }
69  
70  bool usb_is_connected() {
71    uint8_t vusb = 0;
72  
73    IORead(&VUSB_DETECT, &vusb);
74  
75    return (bool)vusb;
76  }
77  
78  void mxInit(void) {
79    MX_GPIO_Init();
80    MX_USART3_UART_Init();
81    MX_USB_OTG_FS_PCD_Init();
82    MX_GPDMA1_Init();
83    MX_ICACHE_Init();
84    MX_IWDG_Init();
85  }