Skip to content

Commit 4f7fd0f

Browse files
committed
[automotive] Update the legacy drivers to align with hardware
1 parent 3f6ef8c commit 4f7fd0f

4 files changed

Lines changed: 37 additions & 28 deletions

File tree

examples/automotive/legacy/ksz8851.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@
1616
#include "../../../third_party/sonata-system/sw/legacy/common/spi.h"
1717
#include "../../../third_party/sonata-system/sw/legacy/common/timer.h"
1818

19-
// Define our own GPIO_OUT as the version from `sonata-system` uses void
20-
// pointer arithmetic, which clang-tidy forbids.
21-
#define GPIO_OUT_ETH GPIO_FROM_BASE_ADDR((GPIO_BASE + GPIO_OUT_REG))
22-
2319
enum
2420
{
2521
// Ethernet IRQs
26-
EthIntrIrq = 47,
27-
// Ethernet GPIO Output Pins
28-
EthCsPin = 13,
29-
EthRstPin = 14,
22+
EthIntrIrq = 2,
23+
// Ethernet chip-select
24+
EthSpiCs = 0,
25+
EthSpiRst = 1,
3026
};
3127

3228
static struct Netif *ethNetif;
@@ -47,11 +43,11 @@ static uint16_t ksz8851_reg_read(spi_t *spi, uint8_t reg)
4743
bytes[0] = (0b00 << 6) | (be << 2) | (reg >> 6);
4844
bytes[1] = (reg << 2) & 0b11110000;
4945

50-
set_output_bit(GPIO_OUT_ETH, EthCsPin, 0);
46+
spi_set_cs(spi, EthSpiCs, 0);
5147
spi_tx(spi, bytes, 2);
5248
uint16_t val;
5349
spi_rx(spi, (uint8_t *)&val, 2);
54-
set_output_bit(GPIO_OUT_ETH, EthCsPin, 1);
50+
spi_set_cs(spi, EthSpiCs, 1);
5551
return val;
5652
}
5753

@@ -62,11 +58,11 @@ static void ksz8851_reg_write(spi_t *spi, uint8_t reg, uint16_t val)
6258
bytes[0] = (0b01 << 6) | (be << 2) | (reg >> 6);
6359
bytes[1] = (reg << 2) & 0b11110000;
6460

65-
set_output_bit(GPIO_OUT_ETH, EthCsPin, 0);
61+
spi_set_cs(spi, EthSpiCs, 0);
6662
spi_tx(spi, bytes, 2);
6763
spi_tx(spi, (uint8_t *)&val, 2);
6864
spi_wait_idle(spi);
69-
set_output_bit(GPIO_OUT_ETH, EthCsPin, 1);
65+
spi_set_cs(spi, EthSpiCs, 1);
7066
}
7167

7268
static void ksz8851_reg_set(spi_t *spi, uint8_t reg, uint16_t mask)
@@ -150,7 +146,7 @@ bool ksz8851_output(struct Netif *netif, struct Fbuf *buf)
150146

151147
// Start transmission.
152148
uint8_t cmd = 0b11 << 6;
153-
set_output_bit(GPIO_OUT_ETH, EthCsPin, 0);
149+
spi_set_cs(spi, EthSpiCs, 0);
154150
spi_tx(spi, &cmd, 1);
155151

156152
uint32_t header = 0x8000 | (buf->len << 16);
@@ -171,7 +167,7 @@ bool ksz8851_output(struct Netif *netif, struct Fbuf *buf)
171167
}
172168

173169
spi_wait_idle(spi);
174-
set_output_bit(GPIO_OUT_ETH, EthCsPin, 1);
170+
spi_set_cs(spi, EthSpiCs, 1);
175171

176172
// Stop QMU DMA transfer operation
177173
ksz8851_reg_clear(spi, ETH_RXQCR, StartDmaAccess);
@@ -210,9 +206,9 @@ bool ksz8851_init(struct Netif *netif, uint8_t hwaddr[6])
210206
return false;
211207

212208
// Reset chip
213-
set_output_bit(GPIO_OUT_ETH, EthRstPin, 0);
209+
spi_set_cs(spi, EthSpiRst, 0);
214210
timer_delay(150);
215-
set_output_bit(GPIO_OUT_ETH, EthRstPin, 0x1);
211+
spi_set_cs(spi, EthSpiRst, 1);
216212

217213
uint16_t cider = ksz8851_reg_read(spi, ETH_CIDER);
218214
#ifdef KSZ8851_DEBUG_PRINT

examples/automotive/legacy/lcd.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../../../third_party/display_drivers/src/core/m3x6_16pt.h"
99
#include "../../../third_party/display_drivers/src/st7735/lcd_st7735.h"
1010
#include "../../../third_party/sonata-system/sw/legacy/common/gpio.h"
11+
#include "../../../third_party/sonata-system/sw/legacy/common/pwm.h"
1112
#include "../../../third_party/sonata-system/sw/legacy/common/sonata_system.h"
1213
#include "../../../third_party/sonata-system/sw/legacy/common/spi.h"
1314
#include "../../../third_party/sonata-system/sw/legacy/common/timer.h"
@@ -34,17 +35,19 @@ static uint32_t spi_write(void *handle, uint8_t *data, size_t len)
3435

3536
static uint32_t gpio_write(void *handle, bool cs, bool dc)
3637
{
37-
set_output_bit(GPIO_OUT_LCD, LcdDcPin, dc);
38-
set_output_bit(GPIO_OUT_LCD, LcdCsPin, cs);
38+
spi_set_cs((spi_t *)handle, LcdDcPin, dc);
39+
spi_set_cs((spi_t *)handle, LcdCsPin, cs);
3940
return 0;
4041
}
4142

42-
int lcd_init(spi_t *spi, St7735Context *lcd, LCD_Interface *interface)
43+
int lcd_init(spi_t *spi,
44+
pwm_t backlight,
45+
St7735Context *lcd,
46+
LCD_Interface *interface)
4347
{
4448
// Set the initial state of the LCD control pins
45-
set_output_bit(GPIO_OUT_LCD, LcdDcPin, 0x0);
46-
set_output_bit(GPIO_OUT_LCD, LcdBlPin, 0x1);
47-
set_output_bit(GPIO_OUT_LCD, LcdCsPin, 0x0);
49+
spi_set_cs(spi, LcdDcPin, 0x00);
50+
spi_set_cs(spi, LcdCsPin, 0x00);
4851

4952
// Reset the LCD
5053
set_output_bit(GPIO_OUT_LCD, LcdRstPin, 0x0);
@@ -68,6 +71,7 @@ int lcd_init(spi_t *spi, St7735Context *lcd, LCD_Interface *interface)
6871

6972
// Clean display with a white rectangle.
7073
lcd_st7735_clean(lcd);
74+
set_pwm(backlight, 1, 255);
7175

7276
return 0;
7377
}

examples/automotive/legacy/lcd.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
#define LCD_H
77

88
#include "../../../third_party/display_drivers/src/st7735/lcd_st7735.h"
9+
#include "../../../third_party/sonata-system/sw/legacy/common/pwm.h"
910
#include "../../../third_party/sonata-system/sw/legacy/common/spi.h"
1011

1112
// Constants.
1213
enum
1314
{
1415
// Pin out mapping.
1516
LcdCsPin = 0,
16-
LcdRstPin,
1717
LcdDcPin,
18-
LcdBlPin,
18+
LcdRstPin,
1919
LcdMosiPin,
2020
LcdSclkPin,
2121
// Spi clock rate.
@@ -31,6 +31,9 @@ enum
3131
BGRColorWhite = 0xFFFFFF,
3232
};
3333

34-
int lcd_init(spi_t *spi, St7735Context *lcd, LCD_Interface *interface);
34+
int lcd_init(spi_t *spi,
35+
pwm_t backlight,
36+
St7735Context *lcd,
37+
LCD_Interface *interface);
3538

3639
#endif // LCD_H

examples/automotive/legacy/send.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "../../../third_party/display_drivers/src/core/m3x6_16pt.h"
1414
#include "../../../third_party/display_drivers/src/st7735/lcd_st7735.h"
1515
#include "../../../third_party/sonata-system/sw/legacy/common/gpio.h"
16+
#include "../../../third_party/sonata-system/sw/legacy/common/pwm.h"
1617
#include "../../../third_party/sonata-system/sw/legacy/common/rv_plic.h"
1718
#include "../../../third_party/sonata-system/sw/legacy/common/sonata_system.h"
1819
#include "../../../third_party/sonata-system/sw/legacy/common/spi.h"
@@ -191,7 +192,7 @@ void lcd_draw_img(uint32_t x,
191192
*/
192193
uint8_t read_joystick()
193194
{
194-
return ((uint8_t)read_gpio(GPIO_IN_DBNC_AM)) & 0x1f;
195+
return ((uint8_t)(read_gpio(GPIO_IN_DBNC_AM) >> 8u) & 0x1f);
195196
}
196197

197198
/**
@@ -361,25 +362,30 @@ int main()
361362
timer_init();
362363
timer_enable(SYSCLK_FREQ / 1000);
363364

365+
pwm_t lcd_bl = PWM_FROM_ADDR_AND_INDEX(PWM_BASE, PWM_LCD);
366+
364367
// Initialise LCD display driver
365368
LCD_Interface lcdInterface;
366369
spi_t lcdSpi;
367370
spi_init(&lcdSpi, LCD_SPI, LcdSpiSpeedHz);
368-
lcd_init(&lcdSpi, &lcd, &lcdInterface);
371+
lcd_init(&lcdSpi, lcd_bl, &lcd, &lcdInterface);
369372
lcd_clean(BACKGROUND_COLOUR);
370373
const LCD_Point Centre = {lcd.parent.width / 2, lcd.parent.height / 2};
374+
write_to_uart("%s:%d\n", __func__, __LINE__);
371375

372376
// Initialise Ethernet support for use via callback
373377
rv_plic_init();
374378
spi_t ethernetSpi;
375-
spi_init(&ethernetSpi, ETH_SPI, /*speed=*/0);
379+
spi_init(&ethernetSpi, ETH_SPI, 1 * 1000 * 1000);
376380
ethernetInterface.spi = &ethernetSpi;
377381
uint8_t macSource[6];
378382
for (uint8_t i = 0; i < 6; i++)
379383
{
380384
macSource[i] = FixedDemoHeader.macSource[i];
381385
}
386+
write_to_uart("%s:%d\n", __func__, __LINE__);
382387
ksz8851_init(&ethernetInterface, macSource);
388+
write_to_uart("%s:%d\n", __func__, __LINE__);
383389

384390
// Wait until a good physical ethernet link to start the demo
385391
if (!ksz8851_get_phy_status(&ethernetInterface))

0 commit comments

Comments
 (0)