Skip to content

Commit d61c236

Browse files
committed
[bsp][k230]:add gnne driver
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 30e1e5e commit d61c236

File tree

9 files changed

+545
-3
lines changed

9 files changed

+545
-3
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=y
48
devices.spi:
59
<<: *scons
610
kconfig:

bsp/k230/.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,7 @@ CONFIG_PKG_ZLIB_VER="latest"
16241624
#
16251625
# Drivers Configuration
16261626
#
1627+
# CONFIG_BSP_USING_GNNE is not set
16271628
# CONFIG_BSP_USING_SPI is not set
16281629
# CONFIG_BSP_USING_I2C is not set
16291630
# CONFIG_BSP_USING_RTC is not set

bsp/k230/board/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
menu "Drivers Configuration"
2+
3+
config BSP_USING_GNNE
4+
bool "Enable KPU and AI2D"
5+
default n
26

37
menuconfig BSP_USING_SPI
48
bool "Enable SPI"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# RT-Thread building script for gnne component
2+
3+
from building import *
4+
5+
cwd = GetCurrentDir()
6+
src = Glob('*.c')
7+
CPPPATH = [cwd]
8+
9+
group = DefineGroup('Gnne', src, depend = ['BSP_USING_GNNE'], CPPPATH = CPPPATH)
10+
11+
objs = [group]
12+
13+
list = os.listdir(cwd)
14+
15+
for item in list:
16+
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
17+
objs = objs + SConscript(os.path.join(item, 'SConscript'))
18+
19+
Return('objs')
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
/* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
2+
*
3+
* Redistribution and use in source and binary forms, with or without
4+
* modification, are permitted provided that the following conditions are met:
5+
* 1. Redistributions of source code must retain the above copyright
6+
* notice, this list of conditions and the following disclaimer.
7+
* 2. Redistributions in binary form must reproduce the above copyright
8+
* notice, this list of conditions and the following disclaimer in the
9+
* documentation and/or other materials provided with the distribution.
10+
*
11+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
12+
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
13+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
16+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
/*
27+
* Copyright (c) 2006-2025 RT-Thread Development Team
28+
*
29+
* SPDX-License-Identifier: Apache-2.0
30+
*/
31+
32+
#include <rtthread.h>
33+
#include <rthw.h>
34+
#include "drv_hardlock.h"
35+
#include "board.h"
36+
37+
#if defined(RT_USING_POSIX_DEVIO)
38+
#include <dfs_posix.h>
39+
#include <poll.h>
40+
#include <termios.h>
41+
#endif
42+
43+
#define AI2D_CMD_LOCK 0
44+
#define AI2D_CMD_TRYLOCK 1
45+
#define AI2D_CMD_UNLOCK 2
46+
static hardlock_type g_ai2d_lock = HARDLOCK_MAX;
47+
48+
struct ai_2d_dev_handle
49+
{
50+
rt_wqueue_t *wait;
51+
rt_bool_t is_lock;
52+
};
53+
54+
55+
#define ai_2d_log(s...) rt_kprintf(s)
56+
57+
#define ai_2d_err(s...) do { \
58+
ai_2d_log("<err>[%s:%d] ", __func__, __LINE__); \
59+
ai_2d_log(s); \
60+
ai_2d_log("\r\n"); \
61+
} while (0)
62+
63+
static struct rt_device g_ai_2d_device = {0};
64+
static struct rt_event g_ai_2d_event = {0};
65+
extern void *gnne_base_addr;
66+
67+
static int ai_2d_device_open(struct dfs_file *file)
68+
{
69+
struct ai_2d_dev_handle *handle;
70+
rt_device_t device;
71+
72+
handle = rt_malloc(sizeof(struct ai_2d_dev_handle));
73+
if (handle == RT_NULL)
74+
{
75+
ai_2d_err("malloc failed\n");
76+
return -1;
77+
}
78+
device = (rt_device_t)file->vnode->data;
79+
handle->wait = &device->wait_queue;
80+
handle->is_lock = RT_FALSE;
81+
file->data = (void *)handle;
82+
return RT_EOK;
83+
}
84+
85+
static int ai_2d_device_close(struct dfs_file *file)
86+
{
87+
struct ai_2d_dev_handle *handle;
88+
89+
handle = (struct ai_2d_dev_handle *)file->data;
90+
if (handle == RT_NULL)
91+
{
92+
ai_2d_err("try to close a invalid handle");
93+
return -RT_EINVAL;
94+
}
95+
if (handle->is_lock)
96+
{
97+
kd_hardlock_unlock(g_ai2d_lock);
98+
}
99+
rt_free(handle);
100+
file->data = RT_NULL;
101+
return RT_EOK;
102+
}
103+
104+
static int ai_2d_device_ioctl(struct dfs_file *file, int cmd, void *args)
105+
{
106+
struct ai_2d_dev_handle *handle;
107+
int ret = -1;
108+
109+
handle = (struct ai_2d_dev_handle *)file->data;
110+
if (g_ai2d_lock == HARDLOCK_MAX)
111+
return ret;
112+
113+
if (cmd == AI2D_CMD_LOCK)
114+
{
115+
if (handle->is_lock == RT_TRUE)
116+
{
117+
return 0;
118+
}
119+
while (kd_hardlock_lock(g_ai2d_lock));
120+
handle->is_lock = RT_TRUE;
121+
ret = 0;
122+
}
123+
else if (cmd == AI2D_CMD_UNLOCK)
124+
{
125+
if (handle->is_lock == RT_FALSE)
126+
{
127+
return 0;
128+
}
129+
kd_hardlock_unlock(g_ai2d_lock);
130+
handle->is_lock = RT_FALSE;
131+
ret = 0;
132+
}
133+
else if (cmd == AI2D_CMD_TRYLOCK)
134+
{
135+
if (handle->is_lock == RT_TRUE)
136+
{
137+
return 0;
138+
}
139+
if (!kd_hardlock_lock(g_ai2d_lock))
140+
{
141+
handle->is_lock = RT_TRUE;
142+
ret = 0;
143+
}
144+
}
145+
return ret;
146+
}
147+
148+
int ai_2d_device_poll(struct dfs_file *file, struct rt_pollreq *req)
149+
{
150+
struct ai_2d_dev_handle *handle;
151+
unsigned int flags;
152+
handle = (struct ai_2d_dev_handle *)file->data;
153+
if (!handle)
154+
{
155+
ai_2d_err("ai_2d_dev_handle NULL!");
156+
return -EINVAL;
157+
}
158+
rt_event_recv(&g_ai_2d_event, 0x01, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, NULL);
159+
rt_poll_add(handle->wait, req);
160+
return POLLIN;
161+
}
162+
163+
static const struct dfs_file_ops ai_2d_input_fops =
164+
{
165+
.open = ai_2d_device_open,
166+
.close = ai_2d_device_close,
167+
.ioctl = ai_2d_device_ioctl,
168+
.poll = ai_2d_device_poll,
169+
};
170+
171+
static void irq_callback(int irq, void *data)
172+
{
173+
rt_wqueue_t *wait = (rt_wqueue_t *)data;
174+
volatile rt_uint32_t *write_addr = (rt_uint32_t *)((char *)gnne_base_addr + 0xca0);
175+
if (gnne_base_addr == RT_NULL)
176+
{
177+
ai_2d_err("ai2d interrupts while the hardware is not yet initialized\n");
178+
}
179+
write_addr[0] = 1;
180+
write_addr[1] = 0;
181+
write_addr[2] = 0;
182+
write_addr[3] = 0;
183+
rt_wqueue_wakeup(wait, (void *)POLLIN);
184+
rt_event_send(&g_ai_2d_event, 0x1);
185+
}
186+
187+
int ai_2d_device_init(void)
188+
{
189+
int ret = 0;
190+
rt_isr_handler_t old_handler;
191+
rt_device_t ai_2d_device = &g_ai_2d_device;
192+
193+
ret = rt_event_init(&g_ai_2d_event, "ai_2d_event", RT_IPC_FLAG_PRIO);
194+
if (ret)
195+
{
196+
ai_2d_err("event init failed\n");
197+
return -ENOMEM;
198+
}
199+
200+
ret = rt_device_register(ai_2d_device, "ai_2d_device", RT_DEVICE_FLAG_RDWR);
201+
if (ret)
202+
{
203+
ai_2d_err("ai_2d_device register fail\n");
204+
return ret;
205+
}
206+
207+
rt_wqueue_init(&ai_2d_device->wait_queue);
208+
old_handler = rt_hw_interrupt_install(K230_IRQ_AI_2D, irq_callback, &ai_2d_device->wait_queue, "ai_2d_irq");
209+
if (old_handler == RT_NULL)
210+
{
211+
ai_2d_err("ai_2d_device interrupt install fail\n");
212+
return -RT_ERROR;
213+
}
214+
rt_hw_interrupt_umask(K230_IRQ_AI_2D);
215+
216+
ai_2d_device->fops = &ai_2d_input_fops;
217+
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+
return RT_EOK;
227+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
2+
*
3+
* Redistribution and use in source and binary forms, with or without
4+
* modification, are permitted provided that the following conditions are met:
5+
* 1. Redistributions of source code must retain the above copyright
6+
* notice, this list of conditions and the following disclaimer.
7+
* 2. Redistributions in binary form must reproduce the above copyright
8+
* notice, this list of conditions and the following disclaimer in the
9+
* documentation and/or other materials provided with the distribution.
10+
*
11+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
12+
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
13+
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
16+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
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+
33+
/*
34+
* Copyright (c) 2006-2025 RT-Thread Development Team
35+
*
36+
* SPDX-License-Identifier: Apache-2.0
37+
*/
38+
39+
#include <rtthread.h>
40+
41+
extern int gnne_device_init(void);
42+
extern int ai_2d_device_init(void);
43+
44+
int ai_module_init(void)
45+
{
46+
gnne_device_init();
47+
ai_2d_device_init();
48+
}
49+
INIT_COMPONENT_EXPORT(ai_module_init);

0 commit comments

Comments
 (0)