Skip to content

Commit a6fd1d2

Browse files
STM32 Reorg: Remove SPI hardcodes (#716)
* Move import of hardware.h to the end of traget_defs.h * update flash_spi to new routines * Remove hardcoded SPI from SPI lcd drivers * Remove hardcoded SPI from touch and proto * Fix linting issues * Remove hardcodes for Devo12
1 parent f84aa67 commit a6fd1d2

41 files changed

Lines changed: 435 additions & 502 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/pages/128x64x1/tx_configure.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static int size_cb(int absrow, void *data)
5959
case ITEM_MODE:
6060
#endif
6161
case ITEM_BUZZ:
62-
#ifndef HAS_OLED_DISPLAY
62+
#if !HAS_OLED_DISPLAY
6363
case ITEM_BACKLIGHT:
6464
#else
6565
case ITEM_CONTRAST:
@@ -142,15 +142,15 @@ static int row_cb(int absrow, int relrow, int y, void *data)
142142
label = _tr_noop("PwrDn alert");
143143
value = _music_shutdown_cb; x = MED_SEL_X_OFFSET;
144144
break;
145-
#ifndef HAS_OLED_DISPLAY
145+
#if !HAS_OLED_DISPLAY
146146
case ITEM_BACKLIGHT:
147147
title = _tr_noop("LCD settings");
148148
label = _tr_noop("Backlight");
149149
value = backlight_select_cb; x = SMALL_SEL_X_OFFSET;
150150
break;
151151
#endif
152152
case ITEM_CONTRAST:
153-
#ifdef HAS_OLED_DISPLAY
153+
#if HAS_OLED_DISPLAY
154154
title = _tr_noop("LCD settings");
155155
#endif
156156
label = _tr_noop("Contrast");
@@ -229,7 +229,7 @@ static const char *_contrast_select_cb(guiObject_t *obj, int dir, void *data)
229229
0, 10, dir, 1, 1, &changed);
230230
if (changed) {
231231
LCD_Contrast(Transmitter.contrast);
232-
#ifdef HAS_OLED_DISPLAY
232+
#if HAS_OLED_DISPLAY
233233
Transmitter.backlight = Transmitter.contrast;
234234
#endif
235235
}

src/pages/common/_tx_configure.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum {
3131
ITEM_BATT,
3232
ITEM_ALARM_INTV,
3333
ITEM_PWRDN_ALARM,
34-
#ifndef HAS_OLED_DISPLAY
34+
#if !HAS_OLED_DISPLAY
3535
ITEM_BACKLIGHT,
3636
#endif
3737
ITEM_CONTRAST,
@@ -184,7 +184,7 @@ static const char *modeselect_cb(guiObject_t *obj, int dir, void *data)
184184
return tempstring;
185185
}
186186

187-
#ifndef HAS_OLED_DISPLAY
187+
#if !HAS_OLED_DISPLAY
188188
static const char *backlight_select_cb(guiObject_t *obj, int dir, void *data)
189189
{
190190
(void)data;

src/target/drivers/display/8080/480x272x16_s1d13517f00a1_hx8257.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313
along with Deviation. If not, see <http://www.gnu.org/licenses/>.
1414
*/
1515
#include <libopencm3/stm32/gpio.h>
16-
#include <libopencm3/stm32/rcc.h>
1716
#include <libopencm3/stm32/fsmc.h>
1817
#include <libopencm3/stm32/spi.h>
1918
#include "common.h"
20-
#include "lcd.h"
19+
#include "480x272x16_s1d13517f00a1.h"
20+
#include "target/drivers/mcu/stm32/rcc.h"
2121

2222
#define LCD_REG_ADDR ((uint32_t)0x6C000000) /* Register Address */
2323
#define LCD_DATA_ADDR ((uint32_t)(LCD_REG_ADDR + 2)) /* Data Address */
2424

2525
#define LCD_REG *(volatile uint16_t *)(LCD_REG_ADDR)
2626
#define LCD_DATA *(volatile uint16_t *)(LCD_DATA_ADDR)
2727

28-
#define CS_HI() gpio_set(GPIOB, GPIO1)
29-
#define CS_LO() gpio_clear(GPIOB, GPIO1)
28+
#define CS_HI() GPIO_pin_set(LCD_SPI.csn)
29+
#define CS_LO() GPIO_pin_clear(LCD_SPI.csn)
3030

3131
//See this thread: http://www.deviationtx.com/forum/6-general-discussions/4278-please-help-devo-12s-fonts-is-out-of-wack
3232
//The new Devo12 uses a different rev of the Epson chip, which doesn't like our transparency value
@@ -147,40 +147,39 @@ void lcd_clear(unsigned int color)
147147
void SPILCD_SetRegister(u8 address, u16 data)
148148
{
149149
CS_LO();
150-
spi_xfer(SPI1, 0x70); //Address
151-
spi_xfer(SPI1, 0x00);
152-
spi_xfer(SPI1, address);
150+
spi_xfer(LCD_SPI.spi, 0x70); // Address
151+
spi_xfer(LCD_SPI.spi, 0x00);
152+
spi_xfer(LCD_SPI.spi, address);
153153
CS_HI();
154154
usleep(1);
155155
CS_LO();
156-
spi_xfer(SPI1, 0x72); //Write Data
157-
spi_xfer(SPI1, data >> 8);
158-
spi_xfer(SPI1, data & 0xFF);
156+
spi_xfer(LCD_SPI.spi, 0x72); // Write Data
157+
spi_xfer(LCD_SPI.spi, data >> 8);
158+
spi_xfer(LCD_SPI.spi, data & 0xFF);
159159
CS_HI();
160160
}
161161

162162
u16 SPILCD_ReadRegister(u8 address)
163163
{
164164
CS_LO();
165-
spi_xfer(SPI1, 0x70); //Address
166-
spi_xfer(SPI1, 0x00);
167-
spi_xfer(SPI1, address);
165+
spi_xfer(LCD_SPI.spi, 0x70); // Address
166+
spi_xfer(LCD_SPI.spi, 0x00);
167+
spi_xfer(LCD_SPI.spi, address);
168168
CS_HI();
169169
usleep(1);
170170
CS_LO();
171-
spi_xfer(SPI1, 0x73); //Write Data
172-
u16 out = spi_xfer(SPI1, 0);
171+
spi_xfer(LCD_SPI.spi, 0x73); // Write Data
172+
u16 out = spi_xfer(LCD_SPI.spi, 0);
173173
out <<= 8;
174-
out |= spi_xfer(SPI1, 0);
174+
out |= spi_xfer(LCD_SPI.spi, 0);
175175
CS_HI();
176176
return out;
177177
}
178178

179179
void SPILCD_Init()
180180
{
181-
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
182-
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
183-
GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
181+
rcc_periph_clock_enable(get_rcc_from_pin(LCD_SPI.csn));
182+
GPIO_setup_output(LCD_SPI.csn, OTYPE_PUSHPULL);
184183
CS_HI();
185184
SPILCD_SetRegister(0x01, 0x7946);
186185
SPILCD_SetRegister(0x02, 0x2037);

src/target/drivers/display/spi/128x64x1_nt7538.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
#include <libopencm3/stm32/spi.h>
1818
#include "common.h"
1919
#include "gui/gui.h"
20+
#include "target/drivers/mcu/stm32/rcc.h"
2021

21-
#define CS_HI() gpio_set(GPIOB, GPIO0)
22-
#define CS_LO() gpio_clear(GPIOB, GPIO0)
23-
#define CMD_MODE() gpio_clear(GPIOC,GPIO5)
24-
#define DATA_MODE() gpio_set(GPIOC,GPIO5)
22+
#define CS_HI() GPIO_pin_set(LCD_SPI.csn)
23+
#define CS_LO() GPIO_pin_clear(LCD_SPI.csn)
24+
#define CMD_MODE() GPIO_pin_clear(LCD_SPI_MODE)
25+
#define DATA_MODE() GPIO_pin_set(LCD_SPI_MODE)
2526

2627
#ifndef HAS_LCD_FLIPPED
2728
#define HAS_LCD_FLIPPED 0
@@ -42,14 +43,14 @@ static s8 dir;
4243
void LCD_Cmd(unsigned cmd) {
4344
CMD_MODE();
4445
CS_LO();
45-
spi_xfer(SPI1, cmd);
46+
spi_xfer(LCD_SPI.spi, cmd);
4647
CS_HI();
4748
}
4849

4950
void LCD_Data(unsigned cmd) {
5051
DATA_MODE();
5152
CS_LO();
52-
spi_xfer(SPI1, cmd);
53+
spi_xfer(LCD_SPI.spi, cmd);
5354
CS_HI();
5455
}
5556

@@ -86,12 +87,10 @@ void LCD_Init()
8687
{
8788
//Initialization is mostly done in SPI Flash
8889
//Setup CS as B.0 Data/Control = C.5
89-
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
90-
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
91-
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
92-
GPIO_CNF_OUTPUT_PUSHPULL, GPIO0);
93-
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
94-
GPIO_CNF_OUTPUT_PUSHPULL, GPIO5);
90+
rcc_periph_clock_enable(get_rcc_from_pin(LCD_SPI.csn));
91+
rcc_periph_clock_enable(get_rcc_from_pin(LCD_SPI_MODE));
92+
GPIO_setup_output(LCD_SPI.csn, OTYPE_PUSHPULL);
93+
GPIO_setup_output(LCD_SPI_MODE, OTYPE_PUSHPULL);
9594
LCD_Cmd(0xE2); //Reset
9695
volatile int i = 0x8000;
9796
while(i) i--;

src/target/drivers/display/spi/128x64x1_oled_ssd1306.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
#include <libopencm3/stm32/spi.h>
1818
#include "common.h"
1919
#include "gui/gui.h"
20+
#include "target/drivers/mcu/stm32/rcc.h"
2021

21-
#define CS_HI() gpio_set(GPIOB, GPIO0)
22-
#define CS_LO() gpio_clear(GPIOB, GPIO0)
23-
#define CMD_MODE() gpio_clear(GPIOC,GPIO5)
24-
#define DATA_MODE() gpio_set(GPIOC,GPIO5)
22+
#define CS_HI() GPIO_pin_set(LCD_SPI.csn)
23+
#define CS_LO() GPIO_pin_clear(LCD_SPI.csn)
24+
#define CMD_MODE() GPIO_pin_clear(LCD_SPI_MODE)
25+
#define DATA_MODE() GPIO_pin_set(LCD_SPI_MODE)
2526

2627
#define PHY_LCD_WIDTH 128
2728
#define LCD_PAGES 8
@@ -34,14 +35,14 @@ static s8 dir;
3435
void LCD_Cmd(unsigned cmd) {
3536
CMD_MODE();
3637
CS_LO();
37-
spi_xfer(SPI1, cmd);
38+
spi_xfer(LCD_SPI.spi, cmd);
3839
CS_HI();
3940
}
4041

4142
void LCD_Data(unsigned cmd) {
4243
DATA_MODE();
4344
CS_LO();
44-
spi_xfer(SPI1, cmd);
45+
spi_xfer(LCD_SPI.spi, cmd);
4546
CS_HI();
4647
}
4748

@@ -77,12 +78,10 @@ void LCD_Init()
7778
{
7879
//Initialization is mostly done in SPI Flash
7980
//Setup CS as B.0 Data/Control = C.5
80-
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
81-
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
82-
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
83-
GPIO_CNF_OUTPUT_PUSHPULL, GPIO0);
84-
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
85-
GPIO_CNF_OUTPUT_PUSHPULL, GPIO5);
81+
rcc_periph_clock_enable(get_rcc_from_pin(LCD_SPI.csn));
82+
rcc_periph_clock_enable(get_rcc_from_pin(LCD_SPI_MODE));
83+
GPIO_setup_output(LCD_SPI.csn, OTYPE_PUSHPULL);
84+
GPIO_setup_output(LCD_SPI_MODE, OTYPE_PUSHPULL);
8685
_msleep(100);
8786
lcd_display(0); //Display Off
8887
LCD_Cmd(0xD5); //Set Display Clock Divide Ratio / OSC Frequency

src/target/drivers/display/spi/video_ia9211.c

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "lcd.h"
2121
#include "ia9211_map.h"
2222

23+
#include "target/drivers/mcu/stm32/spi.h"
24+
2325
u32 IA9211_map_char(u32 c);
2426

2527
static const struct font_def default_font = {1, 1};
@@ -29,8 +31,8 @@ static u16 is_double_row = 0x00;
2931

3032
#define CUR_CHAR_SIZE (cur_str.font.zoom)
3133

32-
#define CS_HI() gpio_set(GPIOB, GPIO0)
33-
#define CS_LO() gpio_clear(GPIOB, GPIO0)
34+
#define CS_HI() GPIO_pin_set(LCD_SPI.csn)
35+
#define CS_LO() GPIO_pin_clear(LCD_SPI.csn)
3436

3537
// The IA911 chip defines
3638
#define LCD_IA911_CLEAR_VRAM 0x00
@@ -97,7 +99,7 @@ void LCD_CMDLength(const u8 cmd[], u8 length) {
9799
while(i) i--;
98100

99101
for(i = 0; i < length; i++) {
100-
spi_xfer(SPI1, cmd[i]);
102+
spi_xfer(LCD_SPI.spi, cmd[i]);
101103

102104
// Wait a couple of clock ticks
103105
j = 100;
@@ -242,54 +244,35 @@ void LCD_Init()
242244
{
243245
LCD_SetFont(1);
244246
/* Enable GPIOA and GPIOB clock. */
245-
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
246-
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
247-
248-
/* Set GPIO0, GPIO8, GPIO15, GPIO4 (in GPIO port A) to 'output push-pull'. */
249-
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
250-
GPIO_CNF_OUTPUT_PUSHPULL, GPIO0); //CS0
251-
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
252-
GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); //CS1
253-
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
254-
GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); //CS2
255-
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
256-
GPIO_CNF_OUTPUT_PUSHPULL, GPIO9); //ON, OFF?
247+
rcc_periph_clock_enable(get_rcc_from_pin(LCD_SPI.csn));
248+
rcc_periph_clock_enable(get_rcc_from_pin(LCD_VIDEO_CS0));
249+
rcc_periph_clock_enable(get_rcc_from_pin(LCD_VIDEO_CS1));
250+
rcc_periph_clock_enable(get_rcc_from_pin(LCD_VIDEO_CS2));
251+
rcc_periph_clock_enable(get_rcc_from_pin(LCD_VIDEO_PWR));
252+
GPIO_setup_output(LCD_SPI.csn, OTYPE_PUSHPULL);
253+
GPIO_setup_output(LCD_VIDEO_CS0, OTYPE_PUSHPULL);
254+
GPIO_setup_output(LCD_VIDEO_CS1, OTYPE_PUSHPULL);
255+
GPIO_setup_output(LCD_VIDEO_CS2, OTYPE_PUSHPULL);
256+
GPIO_setup_output(LCD_VIDEO_PWR, OTYPE_PUSHPULL);
257257

258258
// Set the 5.8GHz receiver off (default)
259-
gpio_clear(GPIOB, GPIO9);
259+
GPIO_pin_clear(LCD_VIDEO_PWR);
260260

261261
// Set the 5.8GHz channel
262-
gpio_clear(GPIOA, GPIO0);
263-
gpio_clear(GPIOA, GPIO8);
264-
gpio_clear(GPIOA, GPIO15);
262+
GPIO_pin_clear(LCD_VIDEO_CS0);
263+
GPIO_pin_clear(LCD_VIDEO_CS1);
264+
GPIO_pin_clear(LCD_VIDEO_CS2);
265265

266266
// Initialization is mostly done in SPI Flash
267267
// UNLESS we use SPI2 for 4-in-1 module
268-
if (_SPI_FLASH_PORT != 1) {
268+
if (FLASH_SPI.spi != LCD_SPI.spi) {
269269
/* Enable SPI1 */
270-
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_SPI1EN);
271-
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
272-
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO5);
273-
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
274-
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO7);
275-
gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
276-
GPIO_CNF_INPUT_FLOAT, GPIO6);
277-
278-
spi_init_master(SPI1,
279-
SPI_CR1_BAUDRATE_FPCLK_DIV_4,
280-
SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE,
281-
SPI_CR1_CPHA_CLK_TRANSITION_1,
282-
SPI_CR1_DFF_8BIT,
283-
SPI_CR1_MSBFIRST);
284-
spi_enable_software_slave_management(SPI1);
285-
spi_set_nss_high(SPI1);
286-
287-
spi_enable(SPI1);
270+
_spi_init(LCD_SPI_CFG);
288271
}
289272
// Setup CS as B.0 Data/Control = C.5
290273
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
291274
GPIO_CNF_OUTPUT_PUSHPULL, GPIO0);
292-
gpio_set(GPIOB, GPIO0); // CS_HI();
275+
GPIO_pin_set(LCD_SPI.csn); // CS_HI();
293276

294277
LCD_Cmd(LCD_IA911_CLEAR_VRAM); //Clear the VRAM
295278
// Wait for a couple of clock ticks
@@ -343,11 +326,11 @@ void LCD_ShowVideo(u8 enable)
343326
return;
344327

345328
if(enable) {
346-
gpio_set(GPIOB, GPIO9);
329+
GPIO_pin_set(LCD_VIDEO_PWR);
347330
LCD_Cmd(LCD_IA911_CLOCK | LCD_IA911_XOSC); // Set to external mode en enable ossilator
348331
}
349332
else {
350-
gpio_clear(GPIOB, GPIO9);
333+
GPIO_pin_clear(LCD_VIDEO_PWR);
351334
LCD_Cmd(LCD_IA911_CLOCK | LCD_IA911_I_MODE | LCD_IA911_XOSC); // Set to external mode en enable ossilator
352335
}
353336

@@ -366,19 +349,19 @@ void VIDEO_Enable(int on)
366349
void VIDEO_SetChannel(int ch)
367350
{
368351
if(ch & 0x01)
369-
gpio_clear(GPIOA, GPIO0);
352+
GPIO_pin_clear(LCD_VIDEO_CS0);
370353
else
371-
gpio_set(GPIOA, GPIO0);
354+
GPIO_pin_set(LCD_VIDEO_CS0);
372355

373356
if(ch & 0x02)
374-
gpio_clear(GPIOA, GPIO8);
357+
GPIO_pin_clear(LCD_VIDEO_CS1);
375358
else
376-
gpio_set(GPIOA, GPIO8);
359+
GPIO_pin_set(LCD_VIDEO_CS1);
377360

378361
if(ch & 0x04)
379-
gpio_clear(GPIOA, GPIO15);
362+
GPIO_pin_clear(LCD_VIDEO_CS2);
380363
else
381-
gpio_set(GPIOA, GPIO15);
364+
GPIO_pin_set(LCD_VIDEO_CS2);
382365
}
383366

384367

src/target/drivers/mcu/stm32/f1/gpio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ static inline uint16_t GPIO_pin_get(struct mcu_pin pin)
6161
{
6262
return gpio_get(pin.port, pin.pin);
6363
}
64+
6465
#endif // _DTX_STM32F1_GPIO_H_

0 commit comments

Comments
 (0)