-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrx-hal-diy-e22-1W-g441kb.h
210 lines (162 loc) · 5.64 KB
/
rx-hal-diy-e22-1W-g441kb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//*******************************************************
// Copyright (c) MLRS project
// GPL3
// https://www.gnu.org/licenses/gpl-3.0.de.html
// OlliW @ www.olliw.eu
//*******************************************************
// hal
//********************************************************
//-------------------------------------------------------
// RX DIY E22 v007 STM32G441KB
//-------------------------------------------------------
#define DEVICE_HAS_OUT
//-- Timers, Timing, EEPROM, and such stuff
#define DELAY_USE_DWT
#define SYSTICK_TIMESTEP 1000
#define SYSTICK_DELAY_MS(x) (uint16_t)(((uint32_t)(x)*(uint32_t)1000)/SYSTICK_TIMESTEP)
#define EE_START_PAGE 60 // 128 kB flash, 2 kB page
//-- UARTS
// UARTB = serial port
// UART = output port, SBus or whatever
// UARTC = debug port
#define UARTB_USE_UART2_REMAPPED2 // serial
#define UARTB_BAUD TX_SERIAL_BAUDRATE
#define UARTB_USE_TX
#define UARTB_TXBUFSIZE TX_SERIAL_TXBUFSIZE
#define UARTB_USE_TX_ISR
#define UARTB_USE_RX
#define UARTB_RXBUFSIZE TX_SERIAL_RXBUFSIZE
//#define UARTB_USE_RX_IO IO_PA15 // normally would be PB4!! Fixed in hardware
#define UART_USE_UART1 // out pin
#define UART_BAUD 100000 // SBus normal baud rate, is being set later anyhow
#define UART_USE_TX
#define UART_TXBUFSIZE 256 // 512
#define UART_USE_TX_ISR
//#define UART_USE_RX
//#define UART_RXBUFSIZE 512
#define UARTC_USE_LPUART1 // debug
#define UARTC_BAUD 115200
#define UARTC_USE_TX
#define UARTC_TXBUFSIZE 512
#define UARTC_USE_TX_ISR
//#define UARTC_USE_RX
//#define UARTC_RXBUFSIZE 512
//-- SX1: SX12xx & SPI
#define SPI_USE_SPI1 // PA5, PA6, PA7
#define SPI_CS_IO IO_PA4
#define SPI_USE_CLK_LOW_1EDGE // datasheet says CPHA = 0 CPOL = 0
#define SPI_USE_CLOCKSPEED_9MHZ
#define SX_RESET IO_PB0
#define SX_DIO1 IO_PB4
#define SX_BUSY IO_PA8
#define SX_RX_EN IO_PB6
#define SX_TX_EN IO_PB5
#define SX_DIO1_SYSCFG_EXTI_PORTx LL_SYSCFG_EXTI_PORTB
#define SX_DIO1_SYSCFG_EXTI_LINEx LL_SYSCFG_EXTI_LINE4
#define SX_DIO_EXTI_LINE_x LL_EXTI_LINE_4
#define SX_DIO_EXTI_IRQn EXTI4_IRQn
#define SX_DIO_EXTI_IRQHandler EXTI4_IRQHandler
//#define SX_DIO_EXTI_IRQ_PRIORITY 11
void sx_init_gpio(void)
{
gpio_init(SX_RESET, IO_MODE_OUTPUT_PP_HIGH, IO_SPEED_VERYFAST);
gpio_init(SX_DIO1, IO_MODE_INPUT_PD, IO_SPEED_VERYFAST);
gpio_init(SX_BUSY, IO_MODE_INPUT_PU, IO_SPEED_VERYFAST);
gpio_init(SX_TX_EN, IO_MODE_OUTPUT_PP_LOW, IO_SPEED_VERYFAST);
gpio_init(SX_RX_EN, IO_MODE_OUTPUT_PP_LOW, IO_SPEED_VERYFAST);
}
bool sx_busy_read(void)
{
return (gpio_read_activehigh(SX_BUSY)) ? true : false;
}
void sx_amp_transmit(void)
{
gpio_low(SX_RX_EN);
gpio_high(SX_TX_EN);
}
void sx_amp_receive(void)
{
gpio_low(SX_TX_EN);
gpio_high(SX_RX_EN);
}
void sx_dio_init_exti_isroff(void)
{
LL_SYSCFG_SetEXTISource(SX_DIO1_SYSCFG_EXTI_PORTx, SX_DIO1_SYSCFG_EXTI_LINEx);
// let's not use LL_EXTI_Init(), but let's do it by hand, is easier to allow enabling isr later
LL_EXTI_DisableEvent_0_31(SX_DIO_EXTI_LINE_x);
LL_EXTI_DisableIT_0_31(SX_DIO_EXTI_LINE_x);
LL_EXTI_DisableFallingTrig_0_31(SX_DIO_EXTI_LINE_x);
LL_EXTI_EnableRisingTrig_0_31(SX_DIO_EXTI_LINE_x);
NVIC_SetPriority(SX_DIO_EXTI_IRQn, SX_DIO_EXTI_IRQ_PRIORITY);
NVIC_EnableIRQ(SX_DIO_EXTI_IRQn);
}
void sx_dio_enable_exti_isr(void)
{
LL_EXTI_ClearFlag_0_31(SX_DIO_EXTI_LINE_x);
LL_EXTI_EnableIT_0_31(SX_DIO_EXTI_LINE_x);
}
void sx_dio_exti_isr_clearflag(void)
{
LL_EXTI_ClearFlag_0_31(SX_DIO_EXTI_LINE_x);
}
//-- Out port
#ifdef UART_USE_UART2
#define OUT_UARTx USART2
#elif defined UART_USE_LPUART1
#define OUT_UARTx LPUART1
#elif defined UART_USE_UART1
#define OUT_UARTx USART1
#endif
void out_init_gpio(void)
{
}
void out_set_normal(void)
{
LL_USART_Disable(OUT_UARTx);
LL_USART_SetTXPinLevel(OUT_UARTx, LL_USART_TXPIN_LEVEL_STANDARD);
LL_USART_Enable(OUT_UARTx);
}
void out_set_inverted(void)
{
LL_USART_Disable(OUT_UARTx);
LL_USART_SetTXPinLevel(OUT_UARTx, LL_USART_TXPIN_LEVEL_INVERTED);
LL_USART_Enable(OUT_UARTx);
}
//-- Button
#define BUTTON IO_PB7
void button_init(void)
{
gpio_init(BUTTON, IO_MODE_INPUT_PU, IO_SPEED_DEFAULT);
}
bool button_pressed(void)
{
return gpio_read_activelow(BUTTON);
}
//-- LEDs
#define LED_GREEN IO_PA1
#define LED_RED IO_PA0
void leds_init(void)
{
gpio_init(LED_GREEN, IO_MODE_OUTPUT_PP_LOW, IO_SPEED_DEFAULT);
gpio_init(LED_RED, IO_MODE_OUTPUT_PP_LOW, IO_SPEED_DEFAULT);
gpio_low(LED_GREEN); // LED_GREEN_OFF
gpio_low(LED_RED); // LED_RED_OFF
}
void led_green_off(void) { gpio_low(LED_GREEN); }
void led_green_on(void) { gpio_high(LED_GREEN); }
void led_green_toggle(void) { gpio_toggle(LED_GREEN); }
void led_red_off(void) { gpio_low(LED_RED); }
void led_red_on(void) { gpio_high(LED_RED); }
void led_red_toggle(void) { gpio_toggle(LED_RED); }
//-- POWER
#define POWER_GAIN_DBM 13 // gain of a PA stage if present
#define POWER_SX126X_MAX_DBM SX126X_POWER_20_DBM // maximum allowed sx power
#define POWER_USE_DEFAULT_RFPOWER_CALC
#define RFPOWER_DEFAULT 1 // index into rfpower_list array
const rfpower_t rfpower_list[] = {
{ .dbm = POWER_10_DBM, .mW = 10 },
{ .dbm = POWER_20_DBM, .mW = 100 },
{ .dbm = POWER_24_DBM, .mW = 250 },
{ .dbm = POWER_27_DBM, .mW = 500 },
{ .dbm = POWER_30_DBM, .mW = 1000 },
};