Skip to content

Commit cab0504

Browse files
committed
bsp/nxp/mcx/mcxe: Add SPI driver and RW007 sample.
Signed-off-by: Yilin Sun <[email protected]>
1 parent 4222d5f commit cab0504

File tree

7 files changed

+314
-9
lines changed

7 files changed

+314
-9
lines changed

bsp/nxp/mcx/mcxe/Libraries/drivers/drv_i2c.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ static rt_ssize_t lpc_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg
135135
}
136136

137137
static const struct rt_i2c_bus_device_ops i2c_ops = {
138-
.master_xfer = lpc_i2c_xfer,
139-
.slave_xfer = RT_NULL,
140-
.i2c_bus_control = RT_NULL,
138+
.master_xfer = lpc_i2c_xfer,
139+
.slave_xfer = RT_NULL,
140+
.i2c_bus_control = RT_NULL,
141141
};
142142

143143
int rt_hw_i2c_init(void)
@@ -148,7 +148,8 @@ int rt_hw_i2c_init(void)
148148
for (i = 0; i < ARRAY_SIZE(i2c_buses); i++)
149149
{
150150
struct mcx_i2c_bus *priv = &i2c_buses[i];
151-
CLOCK_SetIpSrc(i2c_buses[i].clock_ip_name, priv->clock_ip_src);
151+
152+
CLOCK_SetIpSrc(priv->clock_ip_name, priv->clock_ip_src);
152153

153154
LPI2C_MasterGetDefaultConfig(&masterConfig);
154155
masterConfig.baudRate_Hz = priv->baud;
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2024-08-1 hywing The first version for MCXA
9+
*/
10+
#include "rtdevice.h"
11+
#include "drv_spi.h"
12+
#include "fsl_lpspi.h"
13+
14+
15+
#ifdef RT_USING_SPI
16+
17+
#define DBG_TAG "drv.spi"
18+
#define DBG_LVL DBG_INFO
19+
#include <rtdbg.h>
20+
21+
enum
22+
{
23+
#ifdef BSP_USING_SPI0
24+
SPI0_INDEX,
25+
#endif
26+
#ifdef BSP_USING_SPI1
27+
SPI1_INDEX,
28+
#endif
29+
};
30+
31+
struct mcx_spi_bus
32+
{
33+
struct rt_spi_bus spi_bus;
34+
LPSPI_Type *spi_base;
35+
clock_ip_name_t clock_ip_name;
36+
clock_ip_src_t clock_ip_src;
37+
clock_name_t clock_name;
38+
39+
40+
rt_sem_t sem;
41+
char *name;
42+
};
43+
44+
static struct mcx_spi_bus mcx_spi_buses[] =
45+
{
46+
#ifdef BSP_USING_SPI0
47+
{
48+
.spi_base = LPSPI0,
49+
.clock_ip_name = kCLOCK_Lpspi0,
50+
.clock_ip_src = kCLOCK_IpSrcSysOscAsync,
51+
.name = "spi0",
52+
},
53+
#endif
54+
#ifdef BSP_USING_SPI1
55+
{
56+
.spi_base = LPSPI1,
57+
.clock_ip_name = kCLOCK_Lpspi1,
58+
.clock_ip_src = kCLOCK_IpSrcSysOscAsync,
59+
.name = "spi1",
60+
},
61+
#endif
62+
#ifdef BSP_USING_SPI2
63+
{
64+
.spi_base = LPSPI2,
65+
.clock_ip_name = kCLOCK_Lpspi2,
66+
.clock_ip_src = kCLOCK_IpSrcSysOscAsync,
67+
.name = "spi2",
68+
},
69+
#endif
70+
};
71+
72+
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin)
73+
{
74+
struct rt_spi_device *spi_device = rt_malloc(sizeof(struct rt_spi_device));
75+
if (!spi_device)
76+
{
77+
return -RT_ENOMEM;
78+
}
79+
80+
return rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, pin, NULL);
81+
}
82+
83+
static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
84+
{
85+
return RT_EOK;
86+
}
87+
88+
89+
static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
90+
{
91+
lpspi_transfer_t transfer = {0};
92+
status_t status;
93+
94+
RT_ASSERT(device != RT_NULL);
95+
RT_ASSERT(device->bus != RT_NULL);
96+
RT_ASSERT(device->bus->parent.user_data != RT_NULL);
97+
98+
struct mcx_spi_bus *spi = device->bus->parent.user_data;
99+
100+
if (message->cs_take)
101+
{
102+
rt_pin_write(device->cs_pin, PIN_LOW);
103+
}
104+
105+
transfer.dataSize = message->length;
106+
transfer.rxData = (uint8_t *)(message->recv_buf);
107+
transfer.txData = (uint8_t *)(message->send_buf);
108+
transfer.configFlags = kLPSPI_MasterPcs0;
109+
110+
// Use blocking transfer instead of DMA
111+
status = LPSPI_MasterTransferBlocking(spi->spi_base, &transfer);
112+
113+
if (message->cs_release)
114+
{
115+
rt_pin_write(device->cs_pin, PIN_HIGH);
116+
}
117+
118+
if (status != kStatus_Success)
119+
{
120+
return 0; // Transfer failed
121+
}
122+
123+
return message->length;
124+
}
125+
126+
127+
static struct rt_spi_ops lpc_spi_ops =
128+
{
129+
.configure = spi_configure,
130+
.xfer = spixfer};
131+
132+
int rt_hw_spi_init(void)
133+
{
134+
int i;
135+
136+
for (i = 0; i < ARRAY_SIZE(mcx_spi_buses); i++)
137+
{
138+
struct mcx_spi_bus *priv = &mcx_spi_buses[i];
139+
140+
141+
CLOCK_SetIpSrc(priv->clock_ip_name, priv->clock_ip_src);
142+
143+
priv->spi_bus.parent.user_data = &mcx_spi_buses[i];
144+
priv->sem = rt_sem_create("sem_spi", 0, RT_IPC_FLAG_FIFO);
145+
146+
lpspi_master_config_t masterConfig;
147+
LPSPI_MasterGetDefaultConfig(&masterConfig);
148+
masterConfig.baudRate = 10 * 1000 * 1000;
149+
masterConfig.pcsToSckDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
150+
masterConfig.lastSckToPcsDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
151+
masterConfig.betweenTransferDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
152+
153+
LPSPI_MasterInit(priv->spi_base, &masterConfig, CLOCK_GetFreq(priv->clock_name));
154+
155+
rt_spi_bus_register(&priv->spi_bus, priv->name, &lpc_spi_ops);
156+
}
157+
return RT_EOK;
158+
}
159+
INIT_DEVICE_EXPORT(rt_hw_spi_init);
160+
161+
#endif /* RT_USING_SPI */
162+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2024-03-22 Jisheng Zhang The first version for mcxn
9+
*/
10+
11+
#ifndef DRV_SPI_H
12+
#define DRV_SPI_H
13+
14+
#include <rtdevice.h>
15+
16+
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin);
17+
18+
int rt_hw_spi_init(void);
19+
20+
#endif //DRV_SPI_H
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <rtthread.h>
2+
3+
#ifdef BSP_USING_RW007
4+
#include <rtdbg.h>
5+
#include <rtdevice.h>
6+
#include <board.h>
7+
#include <spi_wifi_rw007.h>
8+
9+
#define BOARD_RW007_DEVICE_NAME "rw007"
10+
11+
extern void spi_wifi_isr(int vector);
12+
13+
static void rw007_gpio_init(void)
14+
{
15+
/* Configure IO */
16+
rt_pin_mode(BOARD_RW007_RST_PIN, PIN_MODE_OUTPUT);
17+
rt_pin_mode(BOARD_RW007_INT_BUSY_PIN, PIN_MODE_INPUT_PULLDOWN);
18+
19+
/* Reset rw007 and config mode */
20+
rt_pin_write(BOARD_RW007_RST_PIN, PIN_LOW);
21+
22+
rt_thread_delay(rt_tick_from_millisecond(100));
23+
rt_pin_write(BOARD_RW007_RST_PIN, PIN_HIGH);
24+
25+
/* Wait rw007 ready(exit busy stat) */
26+
while (!rt_pin_read(BOARD_RW007_INT_BUSY_PIN))
27+
{
28+
rt_thread_delay(5);
29+
}
30+
31+
rt_thread_delay(rt_tick_from_millisecond(200));
32+
rt_pin_mode(BOARD_RW007_INT_BUSY_PIN, PIN_MODE_INPUT_PULLUP);
33+
}
34+
35+
int wifi_spi_device_init(void)
36+
{
37+
int ret = 0;
38+
char sn_version[32];
39+
40+
struct rt_spi_device *spi_device = rt_malloc(sizeof(struct rt_spi_device));
41+
if (!spi_device) return -1;
42+
43+
rw007_gpio_init();
44+
ret = rt_spi_bus_attach_device_cspin(spi_device, BOARD_RW007_DEVICE_NAME, BOARD_RW007_SPI_BUS_NAME, BOARD_RW007_CS_PIN, RT_NULL);
45+
if (ret != RT_EOK) return -2;
46+
47+
rt_hw_wifi_init("rw007");
48+
49+
rt_wlan_set_mode(RT_WLAN_DEVICE_STA_NAME, RT_WLAN_STATION);
50+
rt_wlan_set_mode(RT_WLAN_DEVICE_AP_NAME, RT_WLAN_AP);
51+
52+
rw007_sn_get(sn_version);
53+
rt_kprintf("\nrw007 sn: [%s]\n", sn_version);
54+
rw007_version_get(sn_version);
55+
rt_kprintf("rw007 ver: [%s]\n\n", sn_version);
56+
57+
return 0;
58+
}
59+
INIT_APP_EXPORT(wifi_spi_device_init);
60+
61+
static void int_wifi_irq(void *p)
62+
{
63+
((void)p);
64+
spi_wifi_isr(0);
65+
}
66+
67+
void spi_wifi_hw_init(void)
68+
{
69+
rt_pin_attach_irq(BOARD_RW007_INT_BUSY_PIN, PIN_IRQ_MODE_FALLING, int_wifi_irq, 0);
70+
rt_pin_irq_enable(BOARD_RW007_INT_BUSY_PIN, RT_TRUE);
71+
}
72+
73+
#endif

bsp/nxp/mcx/mcxe/frdm-mcxe247/board/Kconfig

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,16 @@ menu "On-chip Peripheral Drivers"
5757
default y
5858

5959
if BSP_USING_SPI
60+
config BSP_USING_SPI0
61+
bool "Enable LPSPI0"
62+
default n
63+
6064
config BSP_USING_SPI1
6165
bool "Enable LPSPI1"
66+
default y
67+
68+
config BSP_USING_SPI2
69+
bool "Enable LPSPI2"
6270
default n
6371
endif
6472

@@ -145,16 +153,16 @@ menu "Board extended module Drivers"
145153
default "spi1"
146154

147155
config BOARD_RW007_CS_PIN
148-
hex "CS pin index"
149-
default 107
156+
int "CS pin index"
157+
default 16
150158

151159
config BOARD_RW007_INT_BUSY_PIN
152-
hex "INT/BUSY pin index"
153-
default 109
160+
int "INT/BUSY pin index"
161+
default 143
154162

155163
config BOARD_RW007_RST_PIN
156164
hex "RESET pin index"
157-
default 131
165+
default 42
158166
endif
159167

160168

bsp/nxp/mcx/mcxe/frdm-mcxe247/board/MCUX_Config/board/pin_mux.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
static void BOARD_InitUARTPins(void);
1616
static void BOARD_InitI2CPins(void);
17+
static void BOARD_InitSPIPins(void);
1718

1819
void BOARD_InitBootPins(void)
1920
{
2021
BOARD_InitPins();
2122
BOARD_InitUARTPins();
2223
BOARD_InitI2CPins();
24+
BOARD_InitSPIPins();
2325
}
2426

2527
static void BOARD_InitUARTPins(void)
@@ -48,6 +50,21 @@ static void BOARD_InitI2CPins(void)
4850
#endif
4951
}
5052

53+
static void BOARD_InitSPIPins(void)
54+
{
55+
#if defined(BSP_USING_SPI0)
56+
BOARD_InitSPI0Pins();
57+
#endif
58+
59+
#if defined(BSP_USING_SPI1)
60+
BOARD_InitSPI1Pins();
61+
#endif
62+
63+
#if defined(BSP_USING_SPI2)
64+
BOARD_InitSPI2Pins();
65+
#endif
66+
}
67+
5168
void BOARD_InitPins(void)
5269
{
5370
CLOCK_EnableClock(kCLOCK_PortA);
@@ -85,3 +102,24 @@ void BOARD_InitI2C1Pins(void)
85102
PORT_SetPinMux(PORTD, 8U, kPORT_MuxAlt2); /* Default route to Arduino D18/D19 */
86103
PORT_SetPinMux(PORTD, 9U, kPORT_MuxAlt2); /* Default route to Arduino D18/D19 */
87104
}
105+
106+
void BOARD_InitSPI0Pins(void)
107+
{
108+
PORT_SetPinMux(PORTB, 3U, kPORT_MuxAlt3); /* Default route to SPI SCK/SIN/SOUT pins on MikroBUS */
109+
PORT_SetPinMux(PORTE, 0U, kPORT_MuxAlt2); /* Default route to SPI SCK/SIN/SOUT pins on MikroBUS */
110+
PORT_SetPinMux(PORTE, 2U, kPORT_MuxAlt2); /* Default route to SPI SCK/SIN/SOUT pins on MikroBUS */
111+
}
112+
113+
void BOARD_InitSPI1Pins(void)
114+
{
115+
PORT_SetPinMux(PORTB, 14U, kPORT_MuxAlt3); /* Default route to Arduino D11/D12/D13 */
116+
PORT_SetPinMux(PORTB, 15U, kPORT_MuxAlt3); /* Default route to Arduino D11/D12/D13 */
117+
PORT_SetPinMux(PORTD, 2U, kPORT_MuxAlt3); /* Default route to Arduino D11/D12/D13 */
118+
}
119+
120+
void BOARD_InitSPI2Pins(void)
121+
{
122+
PORT_SetPinMux(PORTA, 8U, kPORT_MuxAlt3); /* Default route to SPI SCK/SIN/SOUT pins on PMOD (DNP by default) */
123+
PORT_SetPinMux(PORTC, 15U, kPORT_MuxAlt3); /* Default route to SPI SCK/SIN/SOUT pins on PMOD (DNP by default) */
124+
PORT_SetPinMux(PORTE, 16U, kPORT_MuxAlt3); /* Default route to SPI SCK/SIN/SOUT pins on PMOD (DNP by default) */
125+
}

bsp/nxp/mcx/mcxe/frdm-mcxe247/board/MCUX_Config/board/pin_mux.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ void BOARD_InitUART1Pins(void);
1919
void BOARD_InitUART2Pins(void);
2020
void BOARD_InitI2C0Pins(void);
2121
void BOARD_InitI2C1Pins(void);
22+
void BOARD_InitSPI0Pins(void);
23+
void BOARD_InitSPI1Pins(void);
24+
void BOARD_InitSPI2Pins(void);
2225

2326
#if defined(__cplusplus)
2427
}

0 commit comments

Comments
 (0)