Skip to content

Commit ffe39f1

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 ffe39f1

File tree

9 files changed

+569
-3
lines changed

9 files changed

+569
-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: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
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_info(s...) do { \
58+
ai_2d_log("<ai_2d> "); \
59+
ai_2d_log(s); \
60+
ai_2d_log("\r\n"); \
61+
} while (0)
62+
63+
#define ai_2d_err(s...) do { \
64+
ai_2d_log("<err>[%s:%d] ", __func__, __LINE__); \
65+
ai_2d_log(s); \
66+
ai_2d_log("\r\n"); \
67+
} while (0)
68+
69+
static struct rt_device g_ai_2d_device = {0};
70+
static struct rt_event g_ai_2d_event = {0};
71+
extern void *gnne_base_addr;
72+
73+
static int ai_2d_device_open(struct dfs_file *file)
74+
{
75+
struct ai_2d_dev_handle *handle;
76+
rt_device_t device;
77+
78+
handle = rt_malloc(sizeof(struct ai_2d_dev_handle));
79+
if (handle == RT_NULL)
80+
{
81+
ai_2d_err("malloc failed\n");
82+
return -1;
83+
}
84+
device = (rt_device_t)file->vnode->data;
85+
handle->wait = &device->wait_queue;
86+
handle->is_lock = RT_FALSE;
87+
file->data = (void *)handle;
88+
return RT_EOK;
89+
}
90+
91+
static int ai_2d_device_close(struct dfs_file *file)
92+
{
93+
struct ai_2d_dev_handle *handle;
94+
95+
handle = (struct ai_2d_dev_handle *)file->data;
96+
if (handle == RT_NULL)
97+
{
98+
ai_2d_err("try to close a invalid handle");
99+
return -RT_EINVAL;
100+
}
101+
if (handle->is_lock)
102+
{
103+
kd_hardlock_unlock(g_ai2d_lock);
104+
}
105+
rt_free(handle);
106+
file->data = RT_NULL;
107+
return RT_EOK;
108+
}
109+
110+
static int ai_2d_device_ioctl(struct dfs_file *file, int cmd, void *args)
111+
{
112+
struct ai_2d_dev_handle *handle;
113+
int ret = -1;
114+
115+
handle = (struct ai_2d_dev_handle *)file->data;
116+
if (g_ai2d_lock == HARDLOCK_MAX)
117+
return ret;
118+
119+
if (cmd == AI2D_CMD_LOCK)
120+
{
121+
if (handle->is_lock == RT_TRUE)
122+
{
123+
return 0;
124+
}
125+
while (kd_hardlock_lock(g_ai2d_lock));
126+
handle->is_lock = RT_TRUE;
127+
ret = 0;
128+
}
129+
else if (cmd == AI2D_CMD_UNLOCK)
130+
{
131+
if (handle->is_lock == RT_FALSE)
132+
{
133+
return 0;
134+
}
135+
kd_hardlock_unlock(g_ai2d_lock);
136+
handle->is_lock = RT_FALSE;
137+
ret = 0;
138+
}
139+
else if (cmd == AI2D_CMD_TRYLOCK)
140+
{
141+
if (handle->is_lock == RT_TRUE)
142+
{
143+
return 0;
144+
}
145+
if (!kd_hardlock_lock(g_ai2d_lock))
146+
{
147+
handle->is_lock = RT_TRUE;
148+
ret = 0;
149+
}
150+
}
151+
return ret;
152+
}
153+
154+
int ai_2d_device_poll(struct dfs_file *file, struct rt_pollreq *req)
155+
{
156+
struct ai_2d_dev_handle *handle;
157+
unsigned int flags;
158+
handle = (struct ai_2d_dev_handle *)file->data;
159+
if (!handle)
160+
{
161+
ai_2d_err("ai_2d_dev_handle NULL!");
162+
return -EINVAL;
163+
}
164+
rt_event_recv(&g_ai_2d_event, 0x01, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, NULL);
165+
rt_poll_add(handle->wait, req);
166+
return POLLIN;
167+
}
168+
169+
static const struct dfs_file_ops ai_2d_input_fops =
170+
{
171+
.open = ai_2d_device_open,
172+
.close = ai_2d_device_close,
173+
.ioctl = ai_2d_device_ioctl,
174+
.poll = ai_2d_device_poll,
175+
};
176+
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_isr_handler_t old_handler;
197+
rt_device_t ai_2d_device = &g_ai_2d_device;
198+
199+
ret = rt_event_init(&g_ai_2d_event, "ai_2d_event", RT_IPC_FLAG_PRIO);
200+
if (ret)
201+
{
202+
ai_2d_err("event init failed\n");
203+
return -ENOMEM;
204+
}
205+
206+
ret = rt_device_register(ai_2d_device, "ai_2d_device", RT_DEVICE_FLAG_RDWR);
207+
if (ret)
208+
{
209+
ai_2d_err("ai_2d_device register fail\n");
210+
return ret;
211+
}
212+
213+
rt_wqueue_init(&ai_2d_device->wait_queue);
214+
old_handler = rt_hw_interrupt_install(K230_IRQ_AI_2D, irq_callback, &ai_2d_device->wait_queue, "ai_2d_irq");
215+
if (old_handler == RT_NULL)
216+
{
217+
ai_2d_err("ai_2d_device interrupt install fail\n");
218+
return -RT_ERROR;
219+
}
220+
rt_hw_interrupt_umask(K230_IRQ_AI_2D);
221+
222+
ai_2d_device->fops = &ai_2d_input_fops;
223+
224+
if (kd_request_lock(HARDLOCK_AI2D))
225+
{
226+
ai_2d_err("fail to request hardlock-%d\n", HARDLOCK_AI2D);
227+
}
228+
else
229+
{
230+
g_ai2d_lock = HARDLOCK_AI2D;
231+
}
232+
233+
#ifndef RT_FASTBOOT
234+
if (!ret)
235+
{
236+
ai_2d_info("%s OK\n", __func__);
237+
}
238+
#endif
239+
return ret;
240+
}
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)