Skip to content

Commit b7b252a

Browse files
committed
Requirement: The BSP for the k230 platform in the RT-Thread repository does not yet have a gnne driver.
Solution: Provide gnne driver for the k230 platform in the RT-Thread repository. - Implements mutex lock mechanism for AI2D and GNNE modules. Adds HARDLOCK_AI2D support in hardlock driver for mutual exclusion. - Implements poll operation for device status monitoring. - Updates documentation in bsp/README.md. Signed-off-by: ChuanN-sudo <[email protected]>
1 parent b0cc8d3 commit b7b252a

File tree

9 files changed

+176
-82
lines changed

9 files changed

+176
-82
lines changed

bsp/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,9 +760,9 @@ This document is based on the RT-Thread mainline repository and categorizes the
760760

761761
#### 🟢 K230 (RT-Smart)
762762

763-
| BSP Name | GPIO | UART | I2C | RTC | ADC | PWM | SDIO | HWTimer | WDT | SPI |
764-
|----------|------|------|-----|-----|-----|-----|------|---------|-----|-----|
765-
| [k230](k230) |||||||||||
763+
| BSP Name | GPIO | UART | I2C | RTC | ADC | PWM | SDIO | HWTimer | WDT | SPI | GNNE |
764+
|----------|------|------|-----|-----|-----|-----|------|---------|-----|-----|------|
765+
| [k230](k230) ||||||||||||
766766

767767
#### 🟢 Xuantie (RT-Smart)
768768

bsp/k230/.ci/attachconfig/ci.attachconfig.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
scons.args: &scons
22
scons_arg:
33
- '--strict'
4+
devices.gnee:
5+
<<: *scons
6+
kconfig:
7+
- CONFIG_BSP_USING_GNNE=n
48
devices.spi:
59
<<: *scons
610
kconfig:

bsp/k230/board/Kconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
menu "Drivers Configuration"
2-
3-
config BSP_USING_GNEE
2+
3+
config BSP_USING_GNNE
44
bool "Enable GNNE"
55
default n
66

bsp/k230/drivers/interdrv/gnne/SConscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ cwd = GetCurrentDir()
66
src = Glob('*.c')
77
CPPPATH = [cwd]
88

9-
group = DefineGroup('Gnne', src, depend = ['BSP_USING_GNEE'], CPPPATH = CPPPATH)
9+
group = DefineGroup('Gnne', src, depend = ['BSP_USING_GNNE'], CPPPATH = CPPPATH)
1010

1111
objs = [group]
1212

bsp/k230/drivers/interdrv/gnne/ai2d_dev.c

Lines changed: 97 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,38 +31,38 @@
3131

3232
#include <rtthread.h>
3333
#include <rthw.h>
34+
#include "drv_hardlock.h"
3435

3536
#if defined(RT_USING_POSIX_DEVIO)
36-
#include <dfs_posix.h>
37-
#include <poll.h>
38-
#include <termios.h>
37+
#include <dfs_posix.h>
38+
#include <poll.h>
39+
#include <termios.h>
3940
#endif
4041

41-
#define AI2D_LOG_ENABLE
42-
struct ai_2d_dev_handle {
43-
rt_wqueue_t *wait;
42+
#define AI2D_CMD_LOCK 0
43+
#define AI2D_CMD_TRYLOCK 1
44+
#define AI2D_CMD_UNLOCK 2
45+
static hardlock_type g_ai2d_lock = HARDLOCK_MAX;
46+
47+
struct ai_2d_dev_handle
48+
{
49+
rt_wqueue_t *wait;
50+
rt_bool_t is_lock;
4451
};
4552

4653

47-
#ifdef AI2D_LOG_ENABLE
4854
#define ai_2d_log(s...) rt_kprintf(s)
49-
#else
50-
static int ai_2d_log(const char *fmt, ...)
51-
{
52-
return 0;
53-
}
54-
#endif
5555

56-
#define ai_2d_info(s...) do { \
57-
ai_2d_log("<ai_2d> "); \
58-
ai_2d_log(s); \
59-
ai_2d_log("\r\n"); \
56+
#define ai_2d_info(s...) do { \
57+
ai_2d_log("<ai_2d> "); \
58+
ai_2d_log(s); \
59+
ai_2d_log("\r\n"); \
6060
} while (0)
6161

6262
#define ai_2d_err(s...) do { \
63-
ai_2d_log("<err>[%s:%d] ", __func__, __LINE__); \
64-
ai_2d_log(s); \
65-
ai_2d_log("\r\n"); \
63+
ai_2d_log("<err>[%s:%d] ", __func__, __LINE__); \
64+
ai_2d_log(s); \
65+
ai_2d_log("\r\n"); \
6666
} while (0)
6767

6868
static struct rt_device g_ai_2d_device = {0};
@@ -74,14 +74,16 @@ static int ai_2d_device_open(struct dfs_file *file)
7474
struct ai_2d_dev_handle *handle;
7575
rt_device_t device;
7676

77-
handle = rt_malloc(sizeof (struct ai_2d_dev_handle));
78-
if(handle == RT_NULL) {
77+
handle = rt_malloc(sizeof(struct ai_2d_dev_handle));
78+
if (handle == RT_NULL)
79+
{
7980
ai_2d_err("malloc failed\n");
8081
return -1;
8182
}
8283
device = (rt_device_t)file->vnode->data;
8384
handle->wait = &device->wait_queue;
84-
file->data = (void*)handle;
85+
handle->is_lock = RT_FALSE;
86+
file->data = (void *)handle;
8587
return RT_EOK;
8688
}
8789

@@ -90,9 +92,14 @@ static int ai_2d_device_close(struct dfs_file *file)
9092
struct ai_2d_dev_handle *handle;
9193

9294
handle = (struct ai_2d_dev_handle *)file->data;
93-
if(handle == RT_NULL) {
95+
if (handle == RT_NULL)
96+
{
9497
ai_2d_err("try to close a invalid handle");
95-
return -RT_EINVAL;
98+
return -RT_EINVAL;
99+
}
100+
if (handle->is_lock)
101+
{
102+
kd_hardlock_unlock(g_ai2d_lock);
96103
}
97104
rt_free(handle);
98105
file->data = RT_NULL;
@@ -101,15 +108,55 @@ static int ai_2d_device_close(struct dfs_file *file)
101108

102109
static int ai_2d_device_ioctl(struct dfs_file *file, int cmd, void *args)
103110
{
104-
return 0;
111+
struct ai_2d_dev_handle *handle;
112+
int ret = -1;
113+
114+
handle = (struct ai_2d_dev_handle *)file->data;
115+
if (g_ai2d_lock == HARDLOCK_MAX)
116+
return ret;
117+
118+
if (cmd == AI2D_CMD_LOCK)
119+
{
120+
if (handle->is_lock == RT_TRUE)
121+
{
122+
return 0;
123+
}
124+
while (kd_hardlock_lock(g_ai2d_lock));
125+
handle->is_lock = RT_TRUE;
126+
ret = 0;
127+
}
128+
else if (cmd == AI2D_CMD_UNLOCK)
129+
{
130+
if (handle->is_lock == RT_FALSE)
131+
{
132+
return 0;
133+
}
134+
kd_hardlock_unlock(g_ai2d_lock);
135+
handle->is_lock = RT_FALSE;
136+
ret = 0;
137+
}
138+
else if (cmd == AI2D_CMD_TRYLOCK)
139+
{
140+
if (handle->is_lock == RT_TRUE)
141+
{
142+
return 0;
143+
}
144+
if (!kd_hardlock_lock(g_ai2d_lock))
145+
{
146+
handle->is_lock = RT_TRUE;
147+
ret = 0;
148+
}
149+
}
150+
return ret;
105151
}
106152

107153
int ai_2d_device_poll(struct dfs_file *file, struct rt_pollreq *req)
108154
{
109155
struct ai_2d_dev_handle *handle;
110156
unsigned int flags;
111-
handle = (struct ai_2d_dev_handle*)file->data;
112-
if (!handle) {
157+
handle = (struct ai_2d_dev_handle *)file->data;
158+
if (!handle)
159+
{
113160
ai_2d_err("ai_2d_dev_handle NULL!");
114161
return -EINVAL;
115162
}
@@ -118,7 +165,8 @@ int ai_2d_device_poll(struct dfs_file *file, struct rt_pollreq *req)
118165
return POLLIN;
119166
}
120167

121-
static const struct dfs_file_ops ai_2d_input_fops = {
168+
static const struct dfs_file_ops ai_2d_input_fops =
169+
{
122170
.open = ai_2d_device_open,
123171
.close = ai_2d_device_close,
124172
.ioctl = ai_2d_device_ioctl,
@@ -130,14 +178,15 @@ static void irq_callback(int irq, void *data)
130178
{
131179
rt_wqueue_t *wait = (rt_wqueue_t *)data;
132180
volatile rt_uint32_t *write_addr = (rt_uint32_t *)((char *)gnne_base_addr + 0xca0);
133-
if(gnne_base_addr == RT_NULL) {
181+
if (gnne_base_addr == RT_NULL)
182+
{
134183
ai_2d_err("ai2d interrupts while the hardware is not yet initialized\n");
135184
}
136185
write_addr[0] = 1;
137186
write_addr[1] = 0;
138187
write_addr[2] = 0;
139188
write_addr[3] = 0;
140-
rt_wqueue_wakeup(wait, (void*)POLLIN);
189+
rt_wqueue_wakeup(wait, (void *)POLLIN);
141190
rt_event_send(&g_ai_2d_event, 0x1);
142191
}
143192

@@ -147,13 +196,15 @@ int ai_2d_device_init(void)
147196
rt_device_t ai_2d_device = &g_ai_2d_device;
148197

149198
ret = rt_event_init(&g_ai_2d_event, "ai_2d_event", RT_IPC_FLAG_PRIO);
150-
if (ret) {
199+
if (ret)
200+
{
151201
ai_2d_err("event init failed\n");
152202
return -ENOMEM;
153203
}
154204

155205
ret = rt_device_register(ai_2d_device, "ai_2d_device", RT_DEVICE_FLAG_RDWR);
156-
if(ret) {
206+
if (ret)
207+
{
157208
ai_2d_err("ai_2d_device register fail\n");
158209
return ret;
159210
}
@@ -164,9 +215,20 @@ int ai_2d_device_init(void)
164215

165216
ai_2d_device->fops = &ai_2d_input_fops;
166217

218+
if (kd_request_lock(HARDLOCK_AI2D))
219+
{
220+
ai_2d_err("fail to request hardlock-%d\n", HARDLOCK_AI2D);
221+
}
222+
else
223+
{
224+
g_ai2d_lock = HARDLOCK_AI2D;
225+
}
226+
167227
#ifndef RT_FASTBOOT
168-
if(!ret)
228+
if (!ret)
229+
{
169230
ai_2d_info("%s OK\n", __func__);
231+
}
170232
#endif
171233
return ret;
172-
}
234+
}

bsp/k230/drivers/interdrv/gnne/ai_module.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2424
*/
2525

26+
/*
27+
* NOTE: Currently untested - nncase cannot run due to missing mmz library.
28+
* The mmz library depends on MPP driver, which relies on deprecated RT-Thread
29+
* kernel APIs (rt_proc_entry_create, rt_dma_chan_request, etc.) that are no
30+
* longer available in the mainline kernel. Awaiting resolution.
31+
*/
32+
2633
/*
2734
* Copyright (c) 2006-2025 RT-Thread Development Team
2835
*
@@ -39,5 +46,4 @@ int ai_module_init(void)
3946
gnne_device_init();
4047
ai_2d_device_init();
4148
}
42-
// INIT_DEVICE_EXPORT(ai_module_init);
43-
INIT_COMPONENT_EXPORT(ai_module_init);
49+
INIT_COMPONENT_EXPORT(ai_module_init);

0 commit comments

Comments
 (0)