static const size_t device_table_len = sizeof(device_table) / sizeof(device_table[0]);
时间: 2024-03-07 12:54:16 浏览: 25
这段代码定义了一个名为 `device_table_len` 的静态常量,该常量的值等于 `device_table` 数组的长度除以第一个元素的长度。具体来说,`sizeof(device_table)` 表示 `device_table` 数组占用的总字节数,`sizeof(device_table[0])` 表示 `device_table` 数组中第一个元素的字节数。将两者相除,就得到了 `device_table` 数组的长度,即存储设备的数量。这个常量的作用是在后续的代码中使用,以便能够遍历所有的存储设备。由于这是一个静态常量,其值在编译时就已经确定,因此不能在运行时修改。
相关问题
esp32 蓝牙启动流程_ESP32教你简单进行蓝牙串口调试
ESP32的蓝牙启动流程如下:
1. 初始化蓝牙协议栈
2. 配置蓝牙参数,如设备名称、服务UUID等
3. 注册蓝牙事件回调函数
4. 启动蓝牙广播
5. 开启蓝牙可见性
6. 等待连接
以下是一个简单的ESP32蓝牙串口调试的示例:
```C
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#define GATTS_TAG "GATTS_DEMO"
#define TEST_DEVICE_NAME "ESP32_BLE_UART"
#define TEST_MANUFACTURER_DATA_LEN 17
/* The max length of characteristic value. When the gatt client write or prepare write,
* the data length must be less than MAX_VALUE_LENGTH.
*/
#define MAX_VALUE_LENGTH 500
/* Declare global variable */
static uint8_t test_manufacturer[TEST_MANUFACTURER_DATA_LEN] = {0x4c, 0x00, 0x02, 0x15, 0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, 0xA1, 0x2F, 0x17, 0xD1, 0xAD};
static uint8_t test_service_uuid128[32] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
0x13, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
//second uuid, 32bit, [12], [13], [14], [15] is the value
0x14, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
};
static uint8_t test_service_uuid[16] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
0x01, 0x2B,
//second uuid, 32bit, [12], [13], [14], [15] is the value
0x01, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
};
static uint8_t test_char_uuid[16] = {
/* LSB <--------------------------------------------------------------------------------> MSB */
//first uuid, 16bit, [12],[13] is the value
0x02, 0x2B,
//second uuid, 32bit, [12], [13], [14], [15] is the value
0x02, 0x2B, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
};
static esp_gatt_char_prop_t test_property = 0;
static uint8_t char1_str[] = {0x11,0x22,0x33};
static esp_attr_value_t gatts_demo_char1_val = {
.attr_max_len = MAX_VALUE_LENGTH,
.attr_len = sizeof(char1_str),
.attr_value = char1_str,
};
static uint16_t gatts_demo_handle_table[3];
/* Full Database Description - Used to add attributes into the database */
static const esp_gatts_attr_db_t gatt_db[HRS_IDX_NB] =
{
// Service Declaration
[IDX_SVC] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ,
sizeof(test_service_uuid), sizeof(test_service_uuid), (uint8_t *)&test_service_uuid},
ESP_GATT_UUID_PRI_SERVICE,
ESP_GATT_PERM_READ,
sizeof(test_service_uuid),
sizeof(test_service_uuid),
(uint8_t *)&test_service_uuid,
0
},
/* Characteristic Declaration */
[IDX_CHAR_READ] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&test_property},
ESP_GATT_UUID_CHAR_DECLARE,
ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE,
CHAR_DECLARATION_SIZE,
(uint8_t *)&test_property,
0
},
/* Characteristic Value */
[IDX_CHAR_VAL_READ] =
{
{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_128, (uint8_t *)&test_char_uuid, ESP_GATT_PERM_READ,
MAX_VALUE_LENGTH, sizeof(gatts_demo_char1_val), gatts_demo_char1_val.attr_value},
ESP_UUID_LEN_128,
ESP_GATT_PERM_READ,
MAX_VALUE_LENGTH,
sizeof(gatts_demo_char1_val),
gatts_demo_char1_val.attr_value,
0
},
};
static esp_ble_adv_data_t adv_data = {
.set_scan_rsp = false,
.include_name = true,
.include_txpower = true,
.min_interval = 0x20,
.max_interval = 0x40,
.appearance = 0x00,
.manufacturer_len = TEST_MANUFACTURER_DATA_LEN,
.p_manufacturer_data = test_manufacturer,
.service_data_len = 0,
.p_service_data = NULL,
.service_uuid_len = sizeof(test_service_uuid),
.p_service_uuid = test_service_uuid,
.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT),
};
static esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x20,
.adv_int_max = 0x40,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.peer_addr = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
.peer_addr_type = BLE_ADDR_TYPE_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
switch (event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
esp_ble_gap_start_advertising(&adv_params);
break;
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) {
ESP_LOGE(GATTS_TAG, "advertising start failed");
}
break;
default:
break;
}
}
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
{
switch (event) {
case ESP_GATTS_REG_EVT:
esp_ble_gap_set_device_name(TEST_DEVICE_NAME);
esp_ble_gap_config_adv_data(&adv_data);
break;
case ESP_GATTS_CREAT_ATTR_TAB_EVT:
if (param->add_attr_tab.status != ESP_GATT_OK){
ESP_LOGE(GATTS_TAG, "create attribute table failed, error code=0x%x", param->add_attr_tab.status);
}
else if (param->add_attr_tab.num_handle != HRS_IDX_NB) {
ESP_LOGE(GATTS_TAG, "create attribute table abnormally, num_handle (%d) \
doesn't equal to HRS_IDX_NB(%d)", param->add_attr_tab.num_handle, HRS_IDX_NB);
}
else {
ESP_LOGI(GATTS_TAG, "create attribute table successfully, the number handle = %d\n",param->add_attr_tab.num_handle);
memcpy(gatts_demo_handle_table, param->add_attr_tab.handles, sizeof(gatts_demo_handle_table));
esp_ble_gatts_start_service(gatts_demo_handle_table[IDX_SVC]);
}
break;
case ESP_GATTS_CONNECT_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONNECT_EVT");
break;
case ESP_GATTS_DISCONNECT_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_DISCONNECT_EVT");
esp_ble_gap_start_advertising(&adv_params);
break;
case ESP_GATTS_WRITE_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_WRITE_EVT");
break;
case ESP_GATTS_MTU_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_MTU_EVT, MTU %d", param->mtu.mtu);
break;
case ESP_GATTS_CONF_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONF_EVT");
break;
case ESP_GATTS_EXEC_WRITE_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_EXEC_WRITE_EVT");
break;
case ESP_GATTS_START_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_START_EVT");
break;
case ESP_GATTS_STOP_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_STOP_EVT");
break;
case ESP_GATTS_OPEN_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_OPEN_EVT");
break;
case ESP_GATTS_CANCEL_OPEN_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CANCEL_OPEN_EVT");
break;
case ESP_GATTS_CLOSE_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CLOSE_EVT");
break;
case ESP_GATTS_LISTEN_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_LISTEN_EVT");
break;
case ESP_GATTS_CONGEST_EVT:
ESP_LOGI(GATTS_TAG, "ESP_GATTS_CONGEST_EVT");
break;
case ESP_GATTS_UNREG_EVT:
case ESP_GATTS_DELETE_EVT:
default:
break;
}
}
void app_main()
{
esp_err_t ret;
ESP_LOGI(GATTS_TAG, "ESP_BLUETOOTH_BLE example started.");
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret) {
ESP_LOGE(GATTS_TAG, "%s initialize controller failed\n", __func__);
return;
}
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret) {
ESP_LOGE(GATTS_TAG, "%s enable controller failed\n", __func__);
return;
}
ret = esp_bluedroid_init();
if (ret) {
ESP_LOGE(GATTS_TAG, "%s init bluetooth failed\n", __func__);
return;
}
ret = esp_bluedroid_enable();
if (ret) {
ESP_LOGE(GATTS_TAG, "%s enable bluetooth failed\n", __func__);
return;
}
ret = esp_ble_gatts_register_callback(gatts_event_handler);
if (ret){
ESP_LOGE(GATTS_TAG, "gatts register error, error code = %x", ret);
return;
}
ret = esp_ble_gap_register_callback(gap_event_handler);
if (ret){
ESP_LOGE(GATTS_TAG, "gap register error, error code = %x", ret);
return;
}
ret = esp_ble_gatts_app_register(ESP_APP_ID);
if (ret){
ESP_LOGE(GATTS_TAG, "gatts app register error, error code = %x", ret);
return;
}
esp_err_t local_mtu_ret = esp_ble_gatt_set_local_mtu(500);
if (local_mtu_ret){
ESP_LOGE(GATTS_TAG, "set local MTU failed, error code = %x", local_mtu_ret);
}
ret = esp_ble_gatts_create_attr_tab(gatt_db, gatts_if, HRS_IDX_NB, ESP_APP_ID);
if (ret){
ESP_LOGE(GATTS_TAG, "create attr table failed, error code = %x", ret);
}
}
```
在这个示例中,我们使用了ESP-IDF提供的蓝牙协议栈和GATT Server框架,实现了一个简单的GATT Server,并且开启了蓝牙广播和可见性,使得其他蓝牙设备可以扫描到并连接我们的设备。在连接建立后,我们可以向GATT Server中的特定Characteristic写入数据,也可以从特定Characteristic读取数据。
基于Linux4.19内核,写一个详细的lpc_master的驱动
LPC(Low Pin Count)总线是一种低速、低成本的系统总线,广泛应用于嵌入式系统中。在Linux内核中,LPC总线的驱动程序已经被实现,并且已经被集成到了内核中。但是,如果需要开发一个新的LPC设备的驱动程序,需要编写LPC Master控制器的驱动程序。下面是一个基于Linux4.19内核的LPC Master驱动程序的示例:
1. 定义驱动程序
```c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/ioport.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#define DRIVER_NAME "lpc_master"
struct lpc_master_data {
struct resource *res;
void __iomem *io_base;
};
static struct platform_driver lpc_master_driver = {
.probe = lpc_master_probe,
.remove = lpc_master_remove,
.driver = {
.name = DRIVER_NAME,
.of_match_table = of_match_ptr(lpc_master_of_match),
},
};
static int __init lpc_master_init(void)
{
return platform_driver_register(&lpc_master_driver);
}
static void __exit lpc_master_exit(void)
{
platform_driver_unregister(&lpc_master_driver);
}
module_init(lpc_master_init);
module_exit(lpc_master_exit);
MODULE_DESCRIPTION("LPC Master Driver");
MODULE_AUTHOR("Your Name");
MODULE_LICENSE("GPL");
```
2. 定义设备树匹配表
```c
static const struct of_device_id lpc_master_of_match[] = {
{ .compatible = "your,device,compatible", },
{ },
};
MODULE_DEVICE_TABLE(of, lpc_master_of_match);
```
3. 实现probe函数
```c
static int lpc_master_probe(struct platform_device *pdev)
{
struct resource *res;
struct lpc_master_data *data;
int ret;
data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
if (!data) {
dev_err(&pdev->dev, "Failed to allocate driver data\n");
return -ENOMEM;
}
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res) {
dev_err(&pdev->dev, "Failed to get IORESOURCE_MEM\n");
return -ENODEV;
}
data->io_base = devm_ioremap_resource(&pdev->dev, res);
if (IS_ERR(data->io_base)) {
dev_err(&pdev->dev, "Failed to ioremap IO resource\n");
return PTR_ERR(data->io_base);
}
data->res = res;
platform_set_drvdata(pdev, data);
/* TODO: Initialize LPC master controller */
return 0;
}
```
4. 实现remove函数
```c
static int lpc_master_remove(struct platform_device *pdev)
{
struct lpc_master_data *data = platform_get_drvdata(pdev);
/* TODO: Release any resources allocated by the driver */
return 0;
}
```
5. 初始化LPC Master控制器
在probe函数中,需要初始化LPC Master控制器。下面是一个示例函数:
```c
static int lpc_master_init_controller(struct lpc_master_data *data)
{
/* TODO: Initialize LPC controller registers */
return 0;
}
```
6. 发送和接收数据
在LPC Master驱动程序中,需要实现发送和接收数据的功能。下面是一个示例函数:
```c
static int lpc_master_send_recv(struct lpc_master_data *data, u8 *tx_buf, u8 *rx_buf, size_t len)
{
/* TODO: Send and receive data using the LPC controller */
return 0;
}
```
完成以上步骤后,就可以编译和安装LPC Master驱动程序了。在使用LPC设备时,只需要加载驱动程序,就可以通过/dev/lpc_master设备文件进行数据传输。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)