Skip to content

Commit df25c9d

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 df25c9d

File tree

9 files changed

+559
-3
lines changed

9 files changed

+559
-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 GNNE"
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: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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+
36+
#if defined(RT_USING_POSIX_DEVIO)
37+
#include <dfs_posix.h>
38+
#include <poll.h>
39+
#include <termios.h>
40+
#endif
41+
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;
51+
};
52+
53+
54+
#define ai_2d_log(s...) rt_kprintf(s)
55+
56+
#define ai_2d_info(s...) do { \
57+
ai_2d_log("<ai_2d> "); \
58+
ai_2d_log(s); \
59+
ai_2d_log("\r\n"); \
60+
} while (0)
61+
62+
#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"); \
66+
} while (0)
67+
68+
static struct rt_device g_ai_2d_device = {0};
69+
static struct rt_event g_ai_2d_event = {0};
70+
extern void *gnne_base_addr;
71+
72+
static int ai_2d_device_open(struct dfs_file *file)
73+
{
74+
struct ai_2d_dev_handle *handle;
75+
rt_device_t device;
76+
77+
handle = rt_malloc(sizeof(struct ai_2d_dev_handle));
78+
if (handle == RT_NULL)
79+
{
80+
ai_2d_err("malloc failed\n");
81+
return -1;
82+
}
83+
device = (rt_device_t)file->vnode->data;
84+
handle->wait = &device->wait_queue;
85+
handle->is_lock = RT_FALSE;
86+
file->data = (void *)handle;
87+
return RT_EOK;
88+
}
89+
90+
static int ai_2d_device_close(struct dfs_file *file)
91+
{
92+
struct ai_2d_dev_handle *handle;
93+
94+
handle = (struct ai_2d_dev_handle *)file->data;
95+
if (handle == RT_NULL)
96+
{
97+
ai_2d_err("try to close a invalid handle");
98+
return -RT_EINVAL;
99+
}
100+
if (handle->is_lock)
101+
{
102+
kd_hardlock_unlock(g_ai2d_lock);
103+
}
104+
rt_free(handle);
105+
file->data = RT_NULL;
106+
return RT_EOK;
107+
}
108+
109+
static int ai_2d_device_ioctl(struct dfs_file *file, int cmd, void *args)
110+
{
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;
151+
}
152+
153+
int ai_2d_device_poll(struct dfs_file *file, struct rt_pollreq *req)
154+
{
155+
struct ai_2d_dev_handle *handle;
156+
unsigned int flags;
157+
handle = (struct ai_2d_dev_handle *)file->data;
158+
if (!handle)
159+
{
160+
ai_2d_err("ai_2d_dev_handle NULL!");
161+
return -EINVAL;
162+
}
163+
rt_event_recv(&g_ai_2d_event, 0x01, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, NULL);
164+
rt_poll_add(handle->wait, req);
165+
return POLLIN;
166+
}
167+
168+
static const struct dfs_file_ops ai_2d_input_fops =
169+
{
170+
.open = ai_2d_device_open,
171+
.close = ai_2d_device_close,
172+
.ioctl = ai_2d_device_ioctl,
173+
.poll = ai_2d_device_poll,
174+
};
175+
176+
#define IRQN_AI2D_INTERRUPT (16 + 175)
177+
static void irq_callback(int irq, void *data)
178+
{
179+
rt_wqueue_t *wait = (rt_wqueue_t *)data;
180+
volatile rt_uint32_t *write_addr = (rt_uint32_t *)((char *)gnne_base_addr + 0xca0);
181+
if (gnne_base_addr == RT_NULL)
182+
{
183+
ai_2d_err("ai2d interrupts while the hardware is not yet initialized\n");
184+
}
185+
write_addr[0] = 1;
186+
write_addr[1] = 0;
187+
write_addr[2] = 0;
188+
write_addr[3] = 0;
189+
rt_wqueue_wakeup(wait, (void *)POLLIN);
190+
rt_event_send(&g_ai_2d_event, 0x1);
191+
}
192+
193+
int ai_2d_device_init(void)
194+
{
195+
int ret = 0;
196+
rt_device_t ai_2d_device = &g_ai_2d_device;
197+
198+
ret = rt_event_init(&g_ai_2d_event, "ai_2d_event", RT_IPC_FLAG_PRIO);
199+
if (ret)
200+
{
201+
ai_2d_err("event init failed\n");
202+
return -ENOMEM;
203+
}
204+
205+
ret = rt_device_register(ai_2d_device, "ai_2d_device", RT_DEVICE_FLAG_RDWR);
206+
if (ret)
207+
{
208+
ai_2d_err("ai_2d_device register fail\n");
209+
return ret;
210+
}
211+
212+
rt_wqueue_init(&ai_2d_device->wait_queue);
213+
rt_hw_interrupt_install(IRQN_AI2D_INTERRUPT, irq_callback, &ai_2d_device->wait_queue, "ai_2d_irq");
214+
rt_hw_interrupt_umask(IRQN_AI2D_INTERRUPT);
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+
227+
#ifndef RT_FASTBOOT
228+
if (!ret)
229+
{
230+
ai_2d_info("%s OK\n", __func__);
231+
}
232+
#endif
233+
return ret;
234+
}
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)