Skip to content

Commit 857bffe

Browse files
non-working camera
1 parent 8136ebe commit 857bffe

7 files changed

Lines changed: 286 additions & 11 deletions

File tree

platforms/BK723x/OpenBeken.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ APP_C += $(OBK_DIR)/hal/bk7231/hal_pins_bk7231.c
3535
APP_C += $(OBK_DIR)/hal/bk7231/hal_wifi_bk7231.c
3636
APP_C += $(OBK_DIR)/hal/bk7231/hal_uart_bk7231.c
3737
APP_C += $(OBK_DIR)/hal/bk7231/hal_ota_bk7231.c
38+
APP_C += $(OBK_DIR)/driver/drv_bk7252_camera.c
3839

3940
OBK_SRCS = $(OBK_DIR)/
4041
include $(OBK_DIR)/../platforms/obk_main.mk

sdk/beken_freertos_sdk

Submodule beken_freertos_sdk updated 34 files

src/driver/drv_bk7252_camera.c

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
2+
#include "../new_common.h"
3+
#include "../new_pins.h"
4+
#include "../new_cfg.h"
5+
// Commands register, execution API and cmd tokenizer
6+
#include "../cmnds/cmd_public.h"
7+
#include "../cmnds/cmd_local.h"
8+
#include "../logging/logging.h"
9+
10+
#if PLATFORM_BK7252 || PLATFORM_BK7252N
11+
12+
#include "net.h"
13+
#include "typedef.h"
14+
#include "arm_arch.h"
15+
#include "uart_pub.h"
16+
#include "rtos_pub.h"
17+
#include "error.h"
18+
#include "video_transfer.h"
19+
#include "i2c_pub.h"
20+
#include "jpeg_encoder_pub.h"
21+
22+
#define MJPEG_BOUNDARY "boundarydonotcross"
23+
#define MAX_BUF_SIZE 40*1024
24+
static int g_mjpeg_stop = 0;
25+
static char g_send_buf[1024];
26+
static beken_thread_t thread;
27+
28+
int send_first_response(int client)
29+
{
30+
g_send_buf[0] = 0;
31+
32+
snprintf(g_send_buf, 1024,
33+
"HTTP/1.0 200 OK\r\n"
34+
"Connection: close\r\n"
35+
"Server: MJPG-Streamer/0.2\r\n"
36+
"Cache-Control: no-store, no-cache, must-revalidate, pre-check=0,"
37+
" post-check=0, max-age=0\r\n"
38+
"Pragma: no-cache\r\n"
39+
"Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n"
40+
"Content-Type: multipart/x-mixed-replace;boundary="
41+
MJPEG_BOUNDARY "\r\n"
42+
"\r\n"
43+
"--" MJPEG_BOUNDARY "\r\n");
44+
if(send(client, g_send_buf, strlen(g_send_buf), 0) < 0)
45+
{
46+
close(client);
47+
return -1;
48+
}
49+
50+
return 0;
51+
}
52+
53+
int mjpeg_send_stream(int client, void* data, int size)
54+
{
55+
g_send_buf[0] = 0;
56+
57+
if(!g_mjpeg_stop)
58+
{
59+
snprintf(g_send_buf, 1024,
60+
"Content-Type: image/jpeg\r\n"
61+
"Content-Length: %d\r\n"
62+
"\r\n", size);
63+
if(send(client, g_send_buf, strlen(g_send_buf), 0) < 0)
64+
{
65+
close(client);
66+
return -1;
67+
}
68+
69+
if(send(client, data, size, 0) < 0)
70+
{
71+
close(client);
72+
return -1;
73+
}
74+
75+
g_send_buf[0] = 0;
76+
snprintf(g_send_buf, 1024, "\r\n--" MJPEG_BOUNDARY "\r\n");
77+
if(send(client, g_send_buf, strlen(g_send_buf), 0) < 0)
78+
{
79+
close(client);
80+
return -1;
81+
}
82+
83+
return 0;
84+
}
85+
86+
return -1;
87+
}
88+
89+
void mjpeg_server_thread(void* arg)
90+
{
91+
int on;
92+
int srv_sock = -1;
93+
int fream_length = 0;
94+
struct sockaddr_in addr;
95+
socklen_t sock_len = sizeof(struct sockaddr_in);
96+
97+
//int bufsz = 50 * 1024;
98+
uint8_t* buf = (uint8_t*)malloc(MAX_BUF_SIZE);
99+
100+
if(!buf)
101+
{
102+
bk_printf("no buffer yet!\n");
103+
return;
104+
}
105+
106+
107+
srv_sock = socket(AF_INET, SOCK_STREAM, 0);
108+
if(srv_sock < 0)
109+
{
110+
bk_printf("mjpeg_server: create server socket failed due to (%s)\n",
111+
strerror(errno));
112+
goto exit;
113+
}
114+
115+
bzero(&addr, sock_len);
116+
addr.sin_family = AF_INET;
117+
addr.sin_port = htons(7070);
118+
addr.sin_addr.s_addr = INADDR_ANY;
119+
120+
/* ignore "socket already in use" errors */
121+
on = 1;
122+
lwip_setsockopt(srv_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
123+
lwip_setsockopt(srv_sock, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
124+
125+
if(bind(srv_sock, (struct sockaddr*)&addr, sock_len) != 0)
126+
{
127+
bk_printf("mjpeg_server: bind() failed due to (%s)\n",
128+
strerror(errno));
129+
goto exit;
130+
}
131+
132+
if(listen(srv_sock, 2) != 0)
133+
{
134+
bk_printf("mjpeg_server: listen() failed due to (%s)\n",
135+
strerror(errno));
136+
goto exit;
137+
}
138+
139+
g_mjpeg_stop = 0;
140+
while(!g_mjpeg_stop)
141+
{
142+
struct sockaddr_in client_addr;
143+
int client = accept(srv_sock, (struct sockaddr*)&client_addr, &sock_len);
144+
if(client < 0)
145+
continue;
146+
147+
bk_printf("mjpeg_server: client connected\n");
148+
if(send_first_response(client) < 0)
149+
{
150+
client = -1;
151+
continue;
152+
}
153+
154+
while(1)
155+
{
156+
fream_length = 0;
157+
/* capture a jpeg frame */
158+
fream_length = video_buffer_read_frame(buf, MAX_BUF_SIZE, NULL, 1000);
159+
bk_printf("len:%d\r\n",fream_length);
160+
if(fream_length != 0)
161+
{
162+
/* send out this frame */
163+
if(mjpeg_send_stream(client, (void*)buf, fream_length) < 0)
164+
{
165+
bk_printf("client disconnected!\n");
166+
break;
167+
}
168+
}
169+
if(g_mjpeg_stop) break;
170+
taskYIELD();
171+
}
172+
taskYIELD();
173+
}
174+
175+
exit:
176+
if(srv_sock >= 0)
177+
close(srv_sock);
178+
if(buf)
179+
{
180+
free(buf);
181+
buf = NULL;
182+
}
183+
}
184+
185+
void scan_camera_sensors()
186+
{
187+
188+
DJPEG_DESC_ST ejpeg_cfg;
189+
190+
UINT32 status;
191+
192+
DD_HANDLE ejpeg_hdl = ddev_open(EJPEG_DEV_NAME, &status, (UINT32)&ejpeg_cfg);
193+
194+
bk_printf("open EJPEG %p\r\n", ejpeg_hdl);
195+
bk_printf("status: %d\r\n", status);
196+
197+
UINT32 i2c2_trans_mode = (0 & (~I2C2_MSG_WORK_MODE_MS_BIT) // master
198+
& (~I2C2_MSG_WORK_MODE_AL_BIT)) // 7bit address
199+
| (I2C2_MSG_WORK_MODE_IA_BIT);
200+
201+
//UINT32 oflag = 0;
202+
203+
DD_HANDLE i2c_hdl = ddev_open(I2C2_DEV_NAME, &status, i2c2_trans_mode);
204+
bk_printf("open I2C2\r\n");
205+
bk_printf("status: %d\r\n", status);
206+
207+
unsigned char data;
208+
I2C_OP_ST i2c_operater;
209+
210+
211+
//status = ddev_write(i2c_hdl, (char *)&data, 1, (UINT32)&i2c_operater);
212+
213+
214+
//i2c_operater.addr_width = ADDR_WIDTH_8;
215+
216+
217+
for(int i = 0; i < 128; i++)
218+
{
219+
220+
i2c_operater.salve_id = (i << 1) | 1;
221+
222+
i2c_operater.op_addr = 0x0F0;
223+
224+
data = 0x00;
225+
226+
//status = ddev_write(i2c_hdl, (char *) data, 1, (UINT32)&i2c_operater);
227+
228+
//os_printf("\nREAD -> Status: %d, Data: %d %d %d %d", status, data[0], data[1], data[2], data[3]);
229+
230+
status = ddev_read(i2c_hdl, (char*)&data, 1, (UINT32)&i2c_operater);
231+
232+
bk_printf("READ -> Status: %d, Device: %x, Addr: %x, Data: %x \n\n", status, i2c_operater.salve_id, i2c_operater.op_addr, data);
233+
234+
}
235+
236+
237+
}
238+
239+
void BK_Cam_Init(void)
240+
{
241+
scan_camera_sensors();
242+
video_buffer_open();
243+
rtos_create_thread(thread, 20, "jpeg_stream", mjpeg_server_thread, 2048, NULL);
244+
}
245+
246+
void BK_Cam_Deinit(void)
247+
{
248+
g_mjpeg_stop = 0;
249+
rtos_delete_thread(thread);
250+
video_transfer_deinit();
251+
}
252+
253+
void BK_Cam_RunEverySecond(void)
254+
{
255+
256+
}
257+
258+
#endif

src/driver/drv_local.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ void DRV_DDPSend_AppendInformationToHTTPIndexPage(http_request_t* request);
236236
void TXW_Cam_Init(void);
237237
void TXW_Cam_RunEverySecond(void);
238238

239+
void BK_Cam_Init(void);
240+
void BK_Cam_RunEverySecond(void);
241+
void BK_Cam_Deinit(void);
242+
239243
#define SM2135_DELAY 4
240244

241245
// Software I2C

src/driver/drv_main.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,14 +551,21 @@ static driver_t g_drivers[] = {
551551
//drvdetail:"title":"TODO",
552552
//drvdetail:"descr":"UART to TCP bridge, mainly for WiFi Zigbee coordinators.",
553553
//drvdetail:"requires":""}
554-
{ "UartTCP", UART_TCP_Init, NULL, NULL, NULL, UART_TCP_Deinit, NULL, NULL, false }
554+
{ "UartTCP", UART_TCP_Init, NULL, NULL, NULL, UART_TCP_Deinit, NULL, NULL, false },
555555
#endif
556556
#if PLATFORM_TXW81X
557557
//drvdetail:{"name":"TXWCAM",
558558
//drvdetail:"title":"TODO",
559559
//drvdetail:"descr":"TXW81X Camera.",
560560
//drvdetail:"requires":""}
561-
{ "TXWCAM", TXW_Cam_Init, TXW_Cam_RunEverySecond, NULL, NULL, NULL, NULL, NULL, false }
561+
{ "TXWCAM", TXW_Cam_Init, TXW_Cam_RunEverySecond, NULL, NULL, NULL, NULL, NULL, false },
562+
#endif
563+
#if PLATFORM_BK7252 || PLATFORM_BK7252N
564+
//drvdetail:{"name":"BKCAM",
565+
//drvdetail:"title":"TODO",
566+
//drvdetail:"descr":"BK7252(N) Camera.",
567+
//drvdetail:"requires":""}
568+
{ "BKCAM", BK_Cam_Init, BK_Cam_RunEverySecond, NULL, NULL, BK_Cam_Deinit, NULL, NULL, false },
562569
#endif
563570
};
564571

src/i2c/drv_i2c_main.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313

1414

15-
#if PLATFORM_BK7231T
15+
#if PLATFORM_BK7231T || PLATFORM_BEKEN_NEW
1616

1717
#include "i2c_pub.h"
1818
static I2C_OP_ST i2c_operater;
@@ -35,7 +35,7 @@ void DRV_I2C_Write(byte addr, byte data)
3535
Soft_I2C_Stop(&g_softI2C);
3636
return;
3737
}
38-
#if PLATFORM_BK7231T
38+
#if PLATFORM_BK7231T || PLATFORM_BEKEN_NEW
3939
i2c_operater.op_addr = addr;
4040
ddev_write(i2c_hdl, (char*)&data, 1, (UINT32)&i2c_operater);
4141
#endif
@@ -63,7 +63,7 @@ void DRV_I2C_WriteBytes(byte addr, byte *data, int len) {
6363
Soft_I2C_Stop(&g_softI2C);
6464
return;
6565
}
66-
#if PLATFORM_BK7231T
66+
#if PLATFORM_BK7231T || PLATFORM_BEKEN_NEW
6767
i2c_operater.op_addr = addr;
6868
ddev_write(i2c_hdl, (char*)data, len, (UINT32)&i2c_operater);
6969
#endif
@@ -79,7 +79,7 @@ void DRV_I2C_Read(byte addr, byte *data)
7979
Soft_I2C_Stop(&g_softI2C);
8080
return;
8181
}
82-
#if PLATFORM_BK7231T
82+
#if PLATFORM_BK7231T || PLATFORM_BEKEN_NEW
8383
i2c_operater.op_addr = addr;
8484
ddev_read(i2c_hdl, (char*)data, 1, (UINT32)&i2c_operater);
8585
#endif
@@ -97,14 +97,14 @@ void DRV_I2C_ReadBytes(byte addr, byte *data, int size)
9797
Soft_I2C_Stop(&g_softI2C);
9898
return;
9999
}
100-
#if PLATFORM_BK7231T
100+
#if PLATFORM_BK7231T || PLATFORM_BEKEN_NEW
101101
i2c_operater.op_addr = addr;
102102
ddev_read(i2c_hdl, (char*)data, size, (UINT32)&i2c_operater);
103103
#endif
104104
}
105105
int DRV_I2C_Begin(int dev_adr, int busID) {
106106

107-
#if PLATFORM_BK7231T
107+
#if PLATFORM_BK7231T || PLATFORM_BEKEN_NEW
108108
UINT32 status;
109109
UINT32 oflag;
110110
oflag = I2C_DEF_DIV;
@@ -119,7 +119,7 @@ int DRV_I2C_Begin(int dev_adr, int busID) {
119119
Soft_I2C_PreInit(&g_softI2C);
120120
return 0;
121121
}
122-
#if PLATFORM_BK7231T
122+
#if PLATFORM_BK7231T || PLATFORM_BEKEN_NEW
123123
else if(busID == I2C_BUS_I2C1) {
124124
i2c_hdl = ddev_open("i2c1", &status, oflag);
125125
}
@@ -147,7 +147,7 @@ void DRV_I2C_Close() {
147147

148148
return;
149149
}
150-
#if PLATFORM_BK7231T
150+
#if PLATFORM_BK7231T || PLATFORM_BEKEN_NEW
151151
ddev_close(i2c_hdl);
152152
#endif
153153
}

src/logging/logging.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,13 @@ commandResult_t log_command(const void* context, const char* cmd, const char* ar
139139
// to get uart.h
140140
#include "command_line.h"
141141

142+
#if PLATFORM_BK7252N || PLATFORM_BK7252
143+
int UART_PORT = UART1_PORT;
144+
int UART_PORT_INDEX = 0;
145+
#else
142146
int UART_PORT = UART2_PORT;
143147
int UART_PORT_INDEX = 1;
148+
#endif
144149

145150

146151
commandResult_t log_port(const void* context, const char* cmd, const char* args, int cmdFlags) {

0 commit comments

Comments
 (0)