Statistics
| Branch: | Tag: | Revision:

amiro-os / boards / DiWheelDrive / board.c @ 58fe0e0b

History | View | Annotate | Download (2.77 KB)

1
#include "ch.h"
2
#include "hal.h"
3

    
4
/**
5
 * @brief   PAL setup.
6
 * @details Digital I/O ports static configuration as defined in @p board.h.
7
 *          This variable is used by the HAL when initializing the PAL driver.
8
 */
9
#if HAL_USE_PAL || defined(__DOXYGEN__)
10
const PALConfig pal_default_config =
11
{
12
  {VAL_GPIOAODR, VAL_GPIOACRL, VAL_GPIOACRH},
13
  {VAL_GPIOBODR, VAL_GPIOBCRL, VAL_GPIOBCRH},
14
  {VAL_GPIOCODR, VAL_GPIOCCRL, VAL_GPIOCCRH},
15
  {VAL_GPIODODR, VAL_GPIODCRL, VAL_GPIODCRH},
16
  {VAL_GPIOEODR, VAL_GPIOECRL, VAL_GPIOECRH},
17
  {VAL_GPIOFODR, VAL_GPIOFCRL, VAL_GPIOFCRH},
18
  {VAL_GPIOGODR, VAL_GPIOGCRL, VAL_GPIOGCRH},
19
};
20

    
21
#endif
22

    
23
/*
24
 * Early initialization code.
25
 * This initialization must be performed just after stack setup and before
26
 * any other initialization.
27
 */
28
void __early_init(void) {
29

    
30
  stm32_clock_init();
31
}
32

    
33
/*
34
 * Board-specific initialization code.
35
 */
36
void boardInit(void) {
37
  /*
38
   * Several I/O pins are re-mapped:
39
   *   JTAG disabled and SWJ enabled
40
   *   TIM2 to the PA15/PB3/PA2/PA3 pins.
41
   *   TIM3 to PC6/PC7 pins.
42
   *   USART3 to the PC10/PC11 pins.
43
   *   I2C1 to the PB8/PB9 pins.
44
   */
45
  AFIO->MAPR = AFIO_MAPR_SWJ_CFG_DISABLE |
46
               AFIO_MAPR_TIM2_REMAP_PARTIALREMAP1 |
47
               AFIO_MAPR_TIM3_REMAP_FULLREMAP |
48
               AFIO_MAPR_USART3_REMAP_PARTIALREMAP |
49
               AFIO_MAPR_I2C1_REMAP;
50
}
51

    
52
inline void boardWriteIoPower(const uint8_t value)
53
{
54
    if (value) {
55
        // drive pins
56
        palSetPadMode(GPIOA, GPIOA_CAN_TX, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
57
    } else {
58
        // float pins
59
        palSetPadMode(GPIOA, GPIOA_CAN_TX, PAL_MODE_INPUT);
60
    }
61
}
62

    
63
inline void boardWriteLed(int value)
64
{
65
    palWritePad(GPIOA, GPIOA_LED, !value);
66
}
67

    
68
inline void boardRequestShutdown(void)
69
{
70
  palClearPad(GPIOC, GPIOC_SYS_PD_N);
71
}
72

    
73
inline void boardStandby(void)
74
{
75

    
76
  palSetPad(GPIOC, GPIOC_SYS_PD_N);
77
  chSysLock();
78
  // Standby
79
  // set deepsleep bit
80
  SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
81
  // enable wakeup pin
82
  //PWR->CSR |= PWR_CSR_EWUP;
83
  // set PDDS, clear WUF, clear SBF
84
  PWR->CR |= (PWR_CR_CWUF | PWR_CR_PDDS | PWR_CR_CSBF);
85
  // clear RTC wakeup source flags
86
  RTC->CRL &= ~(RTC_CRL_ALRF);
87
  // Wait for Interrupt
88
  __WFI();
89

    
90
}
91

    
92
inline void boardWakeup(void) {
93

    
94
  palClearPad(GPIOC, GPIOC_SYS_PD_N);
95
  chThdSleepMicroseconds(10);
96
  palSetPad(GPIOC, GPIOC_SYS_PD_N);
97
}
98

    
99
inline void boardClearI2CBus(const uint8_t scl_pad) {
100

    
101
  uint8_t i;
102

    
103
  // configure I²C SCL open drain
104
  palSetPadMode(GPIOB, scl_pad, PAL_MODE_OUTPUT_OPENDRAIN);
105

    
106
  // perform bus clear as per I²C Specification v5 3.1.16
107
  for (i = 0x00u; i < 0x09u; i++) {
108
    palClearPad(GPIOB, scl_pad);
109
    chThdSleepMicroseconds(5);
110
    palSetPad(GPIOB, scl_pad);
111
    chThdSleepMicroseconds(5);
112
  }
113

    
114
  // reconfigure I²C SCL
115
  palSetPadMode(GPIOB, scl_pad, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
116

    
117
}