/
drv_esp32.c
121 lines (104 loc) · 2.33 KB
/
drv_esp32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* drv_esp32.c
*
* Created on: Nov 7, 2019
* Author: HoangSHC
*/
#include "drv_esp32.h"
#include "drv_spi1.h"
#include "debug_serial.h"
/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "timers.h"
#define delay_ms(ms) vTaskDelay( ( TickType_t )ms / portTICK_PERIOD_MS ) //delay ms
void drv_esp32_init(void)
{
drr_esp32_init();
}
int drv_esp32_send(const char* cmd)
{
int send_len = strlen(cmd);
char at_flag_buf[4] = {0x02, 0x00, 0x00, 0x00};
char len_buf[4] = {0x00, 0x00, 0x00, 'A'};
debug_puts(cmd);
/* Send SEND request */
driver_spi1_transfer(4, at_flag_buf, 0);
delay_ms(5);
/* Slice the length if it over than 127 */
if (send_len <= ESP_AT_BUFFER_SIZE) {
len_buf[0] = send_len & 127;
len_buf[1] = send_len >> 7;
len_buf[3] = 'A'; // check info
} else {
return -1; // Too large data
}
/* Send data length */
driver_spi1_transfer(4, len_buf, 0);
delay_ms(5);
/* Send data */
driver_spi1_transfer(send_len, cmd, 0);
delay_ms(15);
return 0;
}
//int drv_esp32_is_ready(int wait_ms)
//{
// TickType_t endwait = xTaskGetTickCount() + wait_ms;
// while (!(gpio0_input_val & WIFI_INT_PIN_MASK)) {
// if (xTaskGetTickCount() > endwait) {
// return 0; /* If timeout */
// }
// }
// return 1;
//}
int drv_esp32_recv(char* recvbuf, int* len)
{
char at_flag_buf[4] = {0x01, 0x00, 0x00, 0x00};
char len_buf[4] = {0x00, 0x00, 0x00, 0x00};
int recv_len = 0;
/* Wait for handsake pin is set to high */
// if(drv_esp32_is_ready(500) == 0)
// {
// /* re-Send SEND request */
// delay_ms(50);
// at_flag_buf[0] = 0x2;
// driver_spi1_transfer(4, at_flag_buf, 0);
// delay_ms(50);
// }
/* Send RECV request */
at_flag_buf[0] = 0x1;
if(driver_spi1_transfer(4, at_flag_buf, 0) != 0)
{
return -1;
}
delay_ms(5);
/* Receive data length */
if(driver_spi1_transfer(4, 0, len_buf) != 0)
{
return -1;
}
delay_ms(5);
/* Check info */
if(len_buf[3] == 'B')
{
recv_len = (len_buf[1] << 7) + len_buf[0];
}
/* Receive data */
if (recv_len < ESP_AT_BUFFER_SIZE) {
if(driver_spi1_transfer(recv_len, 0, recvbuf) != 0)
{
return -1;
}
recvbuf[recv_len] = 0;
// debug_puts(recvbuf);
} else {
debug_puts("recv_len over size\n");
}
// return length
if(len) {
*len = recv_len;
}
delay_ms(15);
return 0;
}