Statistics
| Branch: | Tag: | Revision:

amiro-os / boards / DiWheelDrive / board.c @ eef47799

History | View | Annotate | Download (3.931 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, const uint8_t sda_pad) {
100

    
101
  uint8_t i;
102

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

    
107
  // perform a 2-wire software reset for the eeprom (see AT24C01BN-SH-B datasheet, chapter 3)
108
  // note: clock is ~50kHz (20us per cycle)
109
  palSetPad(GPIOB, sda_pad);
110
  palClearPad(GPIOB, scl_pad);
111
  chThdSleepMicroseconds(10);
112
  palSetPad(GPIOB, scl_pad);
113
  chThdSleepMicroseconds(5);
114
  palClearPad(GPIOB, sda_pad);
115
  chThdSleepMicroseconds(5);
116
  palClearPad(GPIOB, scl_pad);
117
  chThdSleepMicroseconds(5);
118
  palSetPad(GPIOB, sda_pad);
119
  chThdSleepMicroseconds(5);
120
  for (i = 0; i < 9; ++i) {
121
    palSetPad(GPIOB, scl_pad);
122
    chThdSleepMicroseconds(10);
123
    palClearPad(GPIOB, scl_pad);
124
    chThdSleepMicroseconds(10);
125
  }
126
  palSetPad(GPIOB, scl_pad);
127
  chThdSleepMicroseconds(5);
128
  palClearPad(GPIOB, sda_pad);
129
  chThdSleepMicroseconds(5);
130
  palClearPad(GPIOB, scl_pad);
131
  chThdSleepMicroseconds(10);
132
  palSetPad(GPIOB, scl_pad);
133
  chThdSleepMicroseconds(5);
134
  palSetPad(GPIOB, sda_pad);
135
  chThdSleepMicroseconds(5);
136
  palClearPad(GPIOB, scl_pad);
137
  chThdSleepMicroseconds(10);
138

    
139
  // perform bus clear as per I²C Specification v6 3.1.16
140
  // note: clock is 100kHz (10us per cycle)
141
  for (i = 0; i < 10; i++) {
142
    palClearPad(GPIOB, scl_pad);
143
    chThdSleepMicroseconds(5);
144
    palSetPad(GPIOB, scl_pad);
145
    chThdSleepMicroseconds(5);
146
  }
147

    
148
  // reconfigure I²C SCL
149
  palSetPadMode(GPIOB, scl_pad, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
150
  palSetPadMode(GPIOB, sda_pad, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
151

    
152
  return;
153
}