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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
| #include "hal_esp32c3.h" #include "driver/gpio.h" #include "driver/ledc.h" #include "esp_bt.h" #include "esp_gap_ble_api.h" #include "esp_gatts_api.h" #include "esp_bt_defs.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h"
#define TAG "HAL_ESP32C3"
void hal_gpio_init(gpio_pin_t pin, gpio_mode_t mode) { gpio_config_t io_conf; io_conf.intr_type = GPIO_INTR_DISABLE; io_conf.pin_bit_mask = (1ULL << pin);
switch (mode) { case GPIO_MODE_INPUT: io_conf.mode = GPIO_MODE_INPUT; io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; io_conf.pull_up_en = GPIO_PULLUP_DISABLE; break; case GPIO_MODE_OUTPUT: io_conf.mode = GPIO_MODE_OUTPUT; io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; io_conf.pull_up_en = GPIO_PULLUP_DISABLE; break; case GPIO_MODE_INPUT_PULLUP: io_conf.mode = GPIO_MODE_INPUT; io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; io_conf.pull_up_en = GPIO_PULLUP_ENABLE; break; case GPIO_MODE_INPUT_PULLDOWN: io_conf.mode = GPIO_MODE_INPUT; io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE; io_conf.pull_up_en = GPIO_PULLUP_DISABLE; break; default: ESP_LOGE(TAG, "Invalid GPIO mode"); return; } gpio_config(&io_conf); }
void hal_gpio_set_level(gpio_pin_t pin, gpio_level_t level) { gpio_set_level(pin, (level == GPIO_LEVEL_HIGH) ? 1 : 0); }
gpio_level_t hal_gpio_get_level(gpio_pin_t pin) { return (gpio_get_level(pin) == 1) ? GPIO_LEVEL_HIGH : GPIO_LEVEL_LOW; }
bool hal_pwm_init(const pwm_config_t *config) { ledc_timer_config_t timer_conf = { .speed_mode = LEDC_LOW_SPEED_MODE, .timer_num = config->channel, .duty_resolution = LEDC_TIMER_10_BIT, .freq_hz = config->frequency_hz, .clk_cfg = LEDC_AUTO_CLK, }; if (ledc_timer_config(&timer_conf) != ESP_OK) { ESP_LOGE(TAG, "PWM timer config failed"); return false; }
ledc_channel_config_t channel_conf = { .speed_mode = LEDC_LOW_SPEED_MODE, .channel = config->channel, .timer_sel = config->channel, .duty = 0, .gpio_num = config->pin, .hpoint = 0, .flags.update_duty_at_zero = false, }; if (ledc_channel_config(&channel_conf) != ESP_OK) { ESP_LOGE(TAG, "PWM channel config failed"); return false; } return true; }
bool hal_pwm_set_duty_cycle(pwm_channel_t channel, float duty_cycle) { if (duty_cycle < 0.0 || duty_cycle > 1.0) { ESP_LOGE(TAG, "Invalid duty cycle value: %f", duty_cycle); return false; } uint32_t duty = (uint32_t)(duty_cycle * ((1 << LEDC_TIMER_10_BIT) - 1)); if (ledc_set_duty(LEDC_LOW_SPEED_MODE, channel, duty) != ESP_OK) { ESP_LOGE(TAG, "PWM set duty failed"); return false; } if (ledc_update_duty(LEDC_LOW_SPEED_MODE, channel) != ESP_OK) { ESP_LOGE(TAG, "PWM update duty failed"); return false; } return true; }
bool hal_pwm_set_frequency(pwm_channel_t channel, uint32_t frequency_hz) { ledc_set_freq(LEDC_LOW_SPEED_MODE, channel, frequency_hz); return true; }
void hal_pwm_deinit(pwm_channel_t channel) { ledc_stop(LEDC_LOW_SPEED_MODE, channel, 0); }
static void (*bluetooth_receive_callback)(const bluetooth_data_t *data) = NULL;
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, .channel_map = ADV_CHNL_ALL, .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY, };
static esp_gatt_if_t gatt_if; static uint16_t connection_id; static uint16_t service_handle; static uint16_t char_handle;
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param); static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gattc_cb_param_t *param); static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatts_cb_param_t *param);
bool hal_bluetooth_init(const bluetooth_config_t *config) { esp_err_t ret;
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); ret = esp_bt_controller_init(&bt_cfg); if (ret) { ESP_LOGE(TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret)); return false; }
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE); if (ret) { ESP_LOGE(TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret)); return false; }
ret = esp_bluedroid_init(); if (ret) { ESP_LOGE(TAG, "%s init bluetooth failed: %s\n", __func__, esp_err_to_name(ret)); return false; }
ret = esp_bluedroid_enable(); if (ret) { ESP_LOGE(TAG, "%s enable bluetooth failed: %s\n", __func__, esp_err_to_name(ret)); return false; }
ret = esp_ble_gap_register_callback(gap_event_handler); if (ret){ ESP_LOGE(TAG, "%s gap register failed, error code = %x\n", __func__, ret); return false; } ret = esp_ble_gatts_register_callback(gatts_event_handler); if (ret){ ESP_LOGE(TAG, "%s gatts register failed, error code = %x\n", __func__, ret); return false; } ret = esp_ble_gattc_register_callback(gattc_event_handler); if(ret){ ESP_LOGE(TAG, "%s gattc register failed, error code = %x\n", __func__, ret); return false; }
ret = esp_ble_gatts_app_register(0); if (ret){ ESP_LOGE(TAG, "%s app register failed, error code = %x\n", __func__, ret); return false; }
return true; }
bool hal_bluetooth_start_advertising() { esp_err_t ret = esp_ble_gap_start_advertising(&adv_params); if (ret){ ESP_LOGE(TAG, "start advertising failed: %s\n", esp_err_to_name(ret)); return false; } return true; }
bool hal_bluetooth_send_data(const bluetooth_data_t *data) { if (connection_id == 0 || char_handle == 0) { ESP_LOGW(TAG, "Not connected or characteristic not found"); return false; } esp_ble_gatts_send_indicate(gatt_if, connection_id, char_handle, data->length, data->data, false); return true; }
bool hal_bluetooth_register_receive_callback(void (*callback)(const bluetooth_data_t *data)) { bluetooth_receive_callback = callback; return true; }
void hal_bluetooth_deinit() { esp_ble_gap_stop_advertising(); esp_bluedroid_disable(); esp_bluedroid_deinit(); esp_bt_controller_disable(); esp_bt_controller_deinit(); }
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_LOGI(TAG, "Advertising data set complete"); hal_bluetooth_start_advertising(); break; case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) { ESP_LOGE(TAG, "Advertising start failed"); } else { ESP_LOGI(TAG, "Advertising start success"); } break; case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS) { ESP_LOGE(TAG, "Advertising stop failed"); } else { ESP_LOGI(TAG, "Advertising stop success"); } break; case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT: ESP_LOGI(TAG, "update connection params status = %d, min_int = %d, max_int = %d,conn_int = %d,latency = %d, timeout = %d", param->update_conn_params.status, param->update_conn_params.min_int, param->update_conn_params.max_int, param->update_conn_params.conn_int, param->update_conn_params.latency, param->update_conn_params.timeout); break; default: ESP_LOGI(TAG, "GAP event: %d", event); break; } }
static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gattc_cb_param_t *param) { ESP_LOGI(TAG, "GATTC event: %d", event); }
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatts_cb_param_t *param) { ESP_LOGI(TAG, "GATTS event: %d", event); switch (event) { case ESP_GATTS_REG_EVT: { esp_ble_adv_data_t adv_data = { .set_scan_rsp = false, .include_name = true, .include_txpower = true, .min_interval = 0x0800, .max_interval = 0x1000, .appearance = 0x00, .manufacturer_len = 0, .p_manufacturer_data = NULL, .service_data_len = 0, .p_service_data = NULL, .service_uuid_len = 0, .p_service_uuid = NULL, .flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT), }; ESP_ERROR_CHECK(esp_ble_gap_set_device_name("MiniQuadruped"));
esp_err_t ret = esp_ble_gap_config_adv_data(&adv_data); if (ret){ ESP_LOGE(TAG, "config adv data failed, error code = %x", ret); } service_handle = param->reg.service_id.id.uuid.uuid.uuid16; ESP_LOGI(TAG, "Service registered, handle = %d", service_handle); break; } case ESP_GATTS_CONNECT_EVT: { connection_id = param->connect.conn_id; ESP_LOGI(TAG, "Client connected, connection ID: %d", connection_id); break; } case ESP_GATTS_DISCONNECT_EVT: { connection_id = 0; char_handle = 0; ESP_LOGI(TAG, "Client disconnected"); hal_bluetooth_start_advertising(); break; } case ESP_GATTS_CREATE_SERV_EVT: { ESP_LOGI(TAG, "Service created"); break; } case ESP_GATTS_ADD_CHAR_EVT: { char_handle = param->add_char.attr_handle; ESP_LOGI(TAG, "Characteristic added, handle = %d", char_handle); break; } case ESP_GATTS_WRITE_EVT: { if (bluetooth_receive_callback != NULL) { bluetooth_data_t received_data; received_data.data = param->write.value; received_data.length = param->write.len; bluetooth_receive_callback(&received_data); } break; } default: ESP_LOGI(TAG, "GATTS event: %d", event); break; } }
|