amiro-os / boards / LightRing / board.c @ b24df8ad
History | View | Annotate | Download (3.122 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 SWD enabled
|
40 |
*/
|
41 |
AFIO->MAPR = AFIO_MAPR_SWJ_CFG_JTAGDISABLE | |
42 |
AFIO_MAPR_USART3_REMAP_PARTIALREMAP; |
43 |
} |
44 |
|
45 |
inline void boardRequestShutdown(void) |
46 |
{ |
47 |
palClearPad(GPIOC, GPIOC_SYS_PD_N); |
48 |
} |
49 |
|
50 |
inline void boardStandby(void) |
51 |
{ |
52 |
|
53 |
palSetPad(GPIOC, GPIOC_SYS_PD_N); |
54 |
chSysLock(); |
55 |
// Standby
|
56 |
// set deepsleep bit
|
57 |
SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; |
58 |
// set PDDS, clear WUF, clear SBF
|
59 |
PWR->CR |= (PWR_CR_CWUF | PWR_CR_PDDS | PWR_CR_CSBF); |
60 |
// clear RTC wakeup source flags
|
61 |
RTC->CRL &= ~(RTC_CRL_ALRF); |
62 |
// Wait for Interrupt
|
63 |
__WFI(); |
64 |
|
65 |
} |
66 |
|
67 |
inline void boardClearI2CBus(const uint8_t scl_pad, const uint8_t sda_pad) { |
68 |
|
69 |
uint8_t i; |
70 |
|
71 |
// configure I²C SCL and SDA open drain
|
72 |
palSetPadMode(GPIOB, scl_pad, PAL_MODE_OUTPUT_OPENDRAIN); |
73 |
palSetPadMode(GPIOB, sda_pad, PAL_MODE_OUTPUT_OPENDRAIN); |
74 |
|
75 |
// perform a 2-wire software reset for the eeprom (see AT24C01BN-SH-B datasheet, chapter 3)
|
76 |
// note: clock is ~50kHz (20us per cycle)
|
77 |
palSetPad(GPIOB, sda_pad); |
78 |
palClearPad(GPIOB, scl_pad); |
79 |
chThdSleepMicroseconds(10);
|
80 |
palSetPad(GPIOB, scl_pad); |
81 |
chThdSleepMicroseconds(5);
|
82 |
palClearPad(GPIOB, sda_pad); |
83 |
chThdSleepMicroseconds(5);
|
84 |
palClearPad(GPIOB, scl_pad); |
85 |
chThdSleepMicroseconds(5);
|
86 |
palSetPad(GPIOB, sda_pad); |
87 |
chThdSleepMicroseconds(5);
|
88 |
for (i = 0; i < 9; ++i) { |
89 |
palSetPad(GPIOB, scl_pad); |
90 |
chThdSleepMicroseconds(10);
|
91 |
palClearPad(GPIOB, scl_pad); |
92 |
chThdSleepMicroseconds(10);
|
93 |
} |
94 |
palSetPad(GPIOB, scl_pad); |
95 |
chThdSleepMicroseconds(5);
|
96 |
palClearPad(GPIOB, sda_pad); |
97 |
chThdSleepMicroseconds(5);
|
98 |
palClearPad(GPIOB, scl_pad); |
99 |
chThdSleepMicroseconds(10);
|
100 |
palSetPad(GPIOB, scl_pad); |
101 |
chThdSleepMicroseconds(5);
|
102 |
palSetPad(GPIOB, sda_pad); |
103 |
chThdSleepMicroseconds(5);
|
104 |
palClearPad(GPIOB, scl_pad); |
105 |
chThdSleepMicroseconds(10);
|
106 |
|
107 |
// perform bus clear as per I²C Specification v6 3.1.16
|
108 |
// note: clock is 100kHz (10us per cycle)
|
109 |
for (i = 0; i < 10; i++) { |
110 |
palClearPad(GPIOB, scl_pad); |
111 |
chThdSleepMicroseconds(5);
|
112 |
palSetPad(GPIOB, scl_pad); |
113 |
chThdSleepMicroseconds(5);
|
114 |
} |
115 |
|
116 |
// reconfigure I²C SCL
|
117 |
palSetPadMode(GPIOB, scl_pad, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); |
118 |
palSetPadMode(GPIOB, sda_pad, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); |
119 |
|
120 |
return;
|
121 |
} |