mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-30 19:19:21 +00:00
Merge branch 'master' into feature/btdm_gatt_api
This commit is contained in:
@@ -17,7 +17,7 @@ void hello_task(void *pvParameter)
|
||||
printf("Hello world!\n");
|
||||
for (int i = 10; i >= 0; i--) {
|
||||
printf("Restarting in %d seconds...\n", i);
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
printf("Restarting now.\n");
|
||||
fflush(stdout);
|
||||
|
@@ -33,10 +33,10 @@ void blink_task(void *pvParameter)
|
||||
while(1) {
|
||||
/* Blink off (output low) */
|
||||
gpio_set_level(BLINK_GPIO, 0);
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
/* Blink on (output high) */
|
||||
gpio_set_level(BLINK_GPIO, 1);
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -115,7 +115,7 @@ static void http_get_task(void *pvParameters)
|
||||
|
||||
if(err != 0 || res == NULL) {
|
||||
ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res);
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ static void http_get_task(void *pvParameters)
|
||||
if(s < 0) {
|
||||
ESP_LOGE(TAG, "... Failed to allocate socket.");
|
||||
freeaddrinfo(res);
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
continue;
|
||||
}
|
||||
ESP_LOGI(TAG, "... allocated socket\r\n");
|
||||
@@ -138,7 +138,7 @@ static void http_get_task(void *pvParameters)
|
||||
ESP_LOGE(TAG, "... socket connect failed errno=%d", errno);
|
||||
close(s);
|
||||
freeaddrinfo(res);
|
||||
vTaskDelay(4000 / portTICK_RATE_MS);
|
||||
vTaskDelay(4000 / portTICK_PERIOD_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -148,7 +148,7 @@ static void http_get_task(void *pvParameters)
|
||||
if (write(s, REQUEST, strlen(REQUEST)) < 0) {
|
||||
ESP_LOGE(TAG, "... socket send failed");
|
||||
close(s);
|
||||
vTaskDelay(4000 / portTICK_RATE_MS);
|
||||
vTaskDelay(4000 / portTICK_PERIOD_MS);
|
||||
continue;
|
||||
}
|
||||
ESP_LOGI(TAG, "... socket send success");
|
||||
@@ -166,7 +166,7 @@ static void http_get_task(void *pvParameters)
|
||||
close(s);
|
||||
for(int countdown = 10; countdown >= 0; countdown--) {
|
||||
ESP_LOGI(TAG, "%d... ", countdown);
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
ESP_LOGI(TAG, "Starting again!");
|
||||
}
|
||||
|
@@ -362,7 +362,7 @@ static void https_get_task(void *pvParameters)
|
||||
|
||||
for(int countdown = 10; countdown >= 0; countdown--) {
|
||||
ESP_LOGI(TAG, "%d...", countdown);
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
ESP_LOGI(TAG, "Starting again!");
|
||||
}
|
||||
|
@@ -72,7 +72,7 @@ void app_main()
|
||||
// Restart module
|
||||
for (int i = 10; i >= 0; i--) {
|
||||
printf("Restarting in %d seconds...\n", i);
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
printf("Restarting now.\n");
|
||||
fflush(stdout);
|
||||
|
@@ -164,7 +164,7 @@ void app_main()
|
||||
*/
|
||||
while (1) {
|
||||
if (gpio_get_level(GPIO_NUM_0) == 0) {
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
if(gpio_get_level(GPIO_NUM_0) == 0) {
|
||||
err = save_run_time();
|
||||
if (err != ESP_OK) printf("Error (%d) saving run time blob to NVS!\n", err);
|
||||
@@ -173,6 +173,6 @@ void app_main()
|
||||
esp_restart();
|
||||
}
|
||||
}
|
||||
vTaskDelay(200 / portTICK_RATE_MS);
|
||||
vTaskDelay(200 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
6
examples/11_rmt_nec_tx_rx/README.md
Normal file
6
examples/11_rmt_nec_tx_rx/README.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# Example: rmt_nec_tx_rx
|
||||
|
||||
This example uses the remote control (RMT) peripheral to transmit and receive codes for the NEC infrared remote protocol.
|
||||
|
||||
Configuration (pin numbers, etc.) can be modified in top of the main/infrared_nec.c file.
|
||||
|
@@ -352,7 +352,7 @@ void rmt_nec_tx_task()
|
||||
rmt_wait_tx_done(channel);
|
||||
//before we free the data, make sure sending is already done.
|
||||
free(item);
|
||||
vTaskDelay(2000 / portTICK_RATE_MS);
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
@@ -30,6 +30,7 @@
|
||||
|
||||
#include "bt_trace.h"
|
||||
#include "bt_types.h"
|
||||
#include "bta_api.h"
|
||||
|
||||
#include "blufi.h"
|
||||
|
||||
|
@@ -69,7 +69,7 @@ static esp_err_t blufi_task_post(uint32_t sig, void *par, void *cb, void *arg)
|
||||
evt.cb = cb;
|
||||
evt.arg = arg;
|
||||
|
||||
if (xQueueSend(xBlufiTaskQueue, &evt, 10 / portTICK_RATE_MS) != pdTRUE) {
|
||||
if (xQueueSend(xBlufiTaskQueue, &evt, 10 / portTICK_PERIOD_MS) != pdTRUE) {
|
||||
LOG_ERROR("Blufi Post failed\n");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
@@ -46,15 +46,15 @@ const int CONNECTED_BIT = BIT0;
|
||||
static wifi_config_t sta_config;
|
||||
|
||||
static char tmp_ssid[33];
|
||||
static char tmp_passwd[33];
|
||||
static char tmp_passwd[65];
|
||||
static bool confirm = false;
|
||||
|
||||
void wifi_set_blue_config(char *ssid, char *passwd)
|
||||
{
|
||||
memset(tmp_ssid, 0, 33);
|
||||
memset(tmp_passwd, 0, 33);
|
||||
strcpy(tmp_ssid, ssid);
|
||||
strcpy(tmp_passwd, passwd);
|
||||
memset(tmp_ssid, 0, sizeof(tmp_ssid));
|
||||
memset(tmp_passwd, 0, sizeof(tmp_passwd));
|
||||
strlcpy(tmp_ssid, ssid, sizeof(tmp_ssid));
|
||||
strlcpy(tmp_passwd, passwd, sizeof(tmp_passwd));
|
||||
confirm = true;
|
||||
LOG_DEBUG("confirm true\n");
|
||||
}
|
||||
@@ -105,8 +105,8 @@ void wifiTestTask(void *pvParameters)
|
||||
if (confirm) {
|
||||
confirm = false;
|
||||
|
||||
strcpy(sta_config.sta.ssid, tmp_ssid);
|
||||
strcpy(sta_config.sta.password, tmp_passwd);
|
||||
memcpy(sta_config.sta.ssid, tmp_ssid, sizeof(sta_config.sta.ssid));
|
||||
memcpy(sta_config.sta.password, tmp_passwd, sizeof(sta_config.sta.password));
|
||||
sta_config.sta.bssid_set = 0;
|
||||
|
||||
ret = esp_wifi_disconnect();
|
||||
|
3
examples/13_timer_group/README.md
Normal file
3
examples/13_timer_group/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Example: timer_group
|
||||
|
||||
This example uses the timer group driver to generate timer interrupts at two specified alarm intervals.
|
@@ -83,7 +83,7 @@ void IRAM_ATTR timer_group0_isr(void *para)
|
||||
uint32_t intr_status = TIMERG0.int_st_timers.val;
|
||||
timer_event_t evt;
|
||||
if((intr_status & BIT(timer_idx)) && timer_idx == TIMER_0) {
|
||||
/*Timer0 is an example that don't reload counter value*/
|
||||
/*Timer0 is an example that doesn't reload counter value*/
|
||||
TIMERG0.hw_timer[timer_idx].update = 1;
|
||||
|
||||
/* We don't call a API here because they are not declared with IRAM_ATTR.
|
||||
@@ -197,9 +197,9 @@ void tg0_timer1_init()
|
||||
*/
|
||||
void app_main()
|
||||
{
|
||||
timer_queue = xQueueCreate(10, sizeof(timer_event_t));
|
||||
tg0_timer0_init();
|
||||
tg0_timer1_init();
|
||||
timer_queue = xQueueCreate(10, sizeof(timer_event_t));
|
||||
xTaskCreate(timer_evt_task, "timer_evt_task", 1024, NULL, 5, NULL);
|
||||
xTaskCreate(timer_evt_task, "timer_evt_task", 2048, NULL, 5, NULL);
|
||||
}
|
||||
|
||||
|
14
examples/16_pcnt/README.md
Normal file
14
examples/16_pcnt/README.md
Normal file
@@ -0,0 +1,14 @@
|
||||
# Example: pcnt
|
||||
|
||||
This example uses the pulse counter module (PCNT) to count the rising edges of pulses generated by the LED Controller module (LEDC).
|
||||
|
||||
By default GPIO18 is used as output pin, GPIO4 is used as pulse input pin and GPIO5 is used as control input pin. This configuration (pin numbers, etc.) can be modified in top of the main/pcnt_test.c file.
|
||||
|
||||
* Open serial port to view the message printed on your screen
|
||||
* To do this test, you should connect GPIO18 with GPIO4
|
||||
* GPIO5 is the control signal, you can leave it floating with internal pulled up, or connect it to ground. HIGH = Count increases, LOW = count decreases.
|
||||
* An interrupt is configured to trigger when the count reaches threshold values.
|
||||
* The counter will reset when it reaches the limit values.
|
||||
|
||||
|
||||
|
@@ -36,14 +36,14 @@
|
||||
* When counter value reaches thresh1 or thresh0 value, it will trigger interrupt.
|
||||
* When counter value reaches l_lim value or h_lim value, counter value will be reset to zero and trigger interrupt.
|
||||
*/
|
||||
#define PCNT_TEST_UNIT PCNT_UNIT_0
|
||||
#define PCNT_H_LIM_VAL (10)
|
||||
#define PCNT_L_LIM_VAL (-10)
|
||||
#define PCNT_THRESH1_VAL (5)
|
||||
#define PCNT_THRESH0_VAL (-5)
|
||||
#define PCNT_INPUT_SIG_IO (4)
|
||||
#define PCNT_INPUT_CTRL_IO (5)
|
||||
#define LEDC_OUPUT_IO (18)
|
||||
#define PCNT_TEST_UNIT PCNT_UNIT_0
|
||||
#define PCNT_H_LIM_VAL 10
|
||||
#define PCNT_L_LIM_VAL -10
|
||||
#define PCNT_THRESH1_VAL 5
|
||||
#define PCNT_THRESH0_VAL -5
|
||||
#define PCNT_INPUT_SIG_IO 4 /* Pulse Input GPIO */
|
||||
#define PCNT_INPUT_CTRL_IO 5 /* Control GPIO HIGH=count up, LOW=count down */
|
||||
#define LEDC_OUTPUT_IO 18 /* Output GPIO */
|
||||
|
||||
xQueueHandle pcnt_evt_queue; /*A queue to handle pulse counter event*/
|
||||
|
||||
@@ -96,8 +96,8 @@ void IRAM_ATTR pcnt_intr_handler(void* arg)
|
||||
static void ledc_init(void)
|
||||
{
|
||||
ledc_channel_config_t ledc_channel;
|
||||
/*use GPIO18 as output pin*/
|
||||
ledc_channel.gpio_num = LEDC_OUPUT_IO;
|
||||
/*use LEDC_OUTPUT_IO as output pin*/
|
||||
ledc_channel.gpio_num = LEDC_OUTPUT_IO;
|
||||
/*LEDC high speed mode */
|
||||
ledc_channel.speed_mode = LEDC_HIGH_SPEED_MODE;
|
||||
/*use LEDC channel 1*/
|
||||
@@ -125,9 +125,9 @@ static void ledc_init(void)
|
||||
static void pcnt_init(void)
|
||||
{
|
||||
pcnt_config_t pcnt_config = {
|
||||
/*Set GPIO4 as pulse input gpio */
|
||||
/*Set PCNT_INPUT_SIG_IO as pulse input gpio */
|
||||
.pulse_gpio_num = PCNT_INPUT_SIG_IO,
|
||||
/*set gpio5 as control gpio */
|
||||
/*set PCNT_INPUT_CTRL_IO as control gpio */
|
||||
.ctrl_gpio_num = PCNT_INPUT_CTRL_IO,
|
||||
/*Choose channel 0 */
|
||||
.channel = PCNT_CHANNEL_0,
|
||||
@@ -196,7 +196,7 @@ void app_main()
|
||||
portBASE_TYPE res;
|
||||
while(1)
|
||||
{
|
||||
res = xQueueReceive(pcnt_evt_queue, &evt, 1000 / portTICK_RATE_MS);
|
||||
res = xQueueReceive(pcnt_evt_queue, &evt, 1000 / portTICK_PERIOD_MS);
|
||||
if(res == pdTRUE) {
|
||||
pcnt_get_counter_value(PCNT_TEST_UNIT, &count);
|
||||
printf("Event PCNT unit[%d]; cnt: %d\n", evt.unit, count);
|
||||
|
@@ -32,30 +32,64 @@
|
||||
#include "tcpip_adapter.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "tlk110_phy.h"
|
||||
|
||||
static const char *TAG = "eth_demo";
|
||||
|
||||
#define DEFAULT_PHY_CONFIG (AUTO_MDIX_ENABLE|AUTO_NEGOTIATION_ENABLE|AN_1|AN_0|LED_CFG)
|
||||
|
||||
void phy_tlk110_check_phy_init(void)
|
||||
{
|
||||
while((esp_eth_smi_read(BASIC_MODE_STATUS_REG) & AUTO_NEGOTIATION_COMPLETE ) != AUTO_NEGOTIATION_COMPLETE)
|
||||
{};
|
||||
while((esp_eth_smi_read(PHY_STATUS_REG) & AUTO_NEGTIATION_STATUS ) != AUTO_NEGTIATION_STATUS)
|
||||
{};
|
||||
while((esp_eth_smi_read(CABLE_DIAGNOSTIC_CONTROL_REG) & DIAGNOSTIC_DONE ) != DIAGNOSTIC_DONE)
|
||||
{};
|
||||
}
|
||||
|
||||
eth_speed_mode_t phy_tlk110_get_speed_mode(void)
|
||||
{
|
||||
if((esp_eth_smi_read(PHY_STATUS_REG) & SPEED_STATUS ) != SPEED_STATUS) {
|
||||
return ETH_SPEED_MODE_100M;
|
||||
} else {
|
||||
return ETH_SPEED_MODE_10M;
|
||||
}
|
||||
}
|
||||
|
||||
eth_duplex_mode_t phy_tlk110_get_duplex_mode(void)
|
||||
{
|
||||
if((esp_eth_smi_read(PHY_STATUS_REG) & DUPLEX_STATUS ) == DUPLEX_STATUS) {
|
||||
return ETH_MDOE_FULLDUPLEX;
|
||||
} else {
|
||||
return ETH_MODE_HALFDUPLEX;
|
||||
}
|
||||
}
|
||||
|
||||
bool phy_tlk110_check_phy_link_status(void)
|
||||
{
|
||||
return ((esp_eth_smi_read(BASIC_MODE_STATUS_REG) & LINK_STATUS) == LINK_STATUS );
|
||||
}
|
||||
|
||||
void phy_tlk110_init(void)
|
||||
{
|
||||
esp_eth_smi_write(0x1f, 0x8000);
|
||||
ets_delay_us(20000);
|
||||
esp_eth_smi_write(PHY_RESET_CONTROL_REG, SOFTWARE_RESET);
|
||||
|
||||
while (esp_eth_smi_read(0x2) != 0x2000) {
|
||||
while (esp_eth_smi_read(PHY_IDENTIFIER_REG) != OUI_MSB_21TO6_DEF) {
|
||||
}
|
||||
|
||||
esp_eth_smi_write(0x9, 0x7400);
|
||||
esp_eth_smi_write(0x9, 0xf400);
|
||||
ets_delay_us(20000);
|
||||
esp_eth_smi_write(SOFTWARE_STAP_CONTROL_REG, DEFAULT_PHY_CONFIG |SW_STRAP_CONFIG_DONE);
|
||||
ets_delay_us(300);
|
||||
}
|
||||
|
||||
void eth_gpio_config_rmii(void)
|
||||
{
|
||||
//txd0 to gpio19 ,can not change
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO19_U, 5);
|
||||
//rx_en to gpio21 ,can not change
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO21_U, 5);
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO19_U, FUNC_GPIO19_EMAC_TXD0);
|
||||
//tx_en to gpio21 ,can not change
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO21_U, FUNC_GPIO21_EMAC_TX_EN);
|
||||
//txd1 to gpio22 , can not change
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO22_U, 5);
|
||||
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO22_U, FUNC_GPIO22_EMAC_TXD1);
|
||||
//rxd0 to gpio25 , can not change
|
||||
gpio_set_direction(25, GPIO_MODE_INPUT);
|
||||
//rxd1 to gpio26 ,can not change
|
||||
@@ -74,11 +108,11 @@ void eth_task(void *pvParameter)
|
||||
{
|
||||
tcpip_adapter_ip_info_t ip;
|
||||
memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
|
||||
vTaskDelay(2000 / portTICK_RATE_MS);
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
|
||||
while (1) {
|
||||
|
||||
vTaskDelay(2000 / portTICK_RATE_MS);
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
|
||||
if (tcpip_adapter_get_ip_info(ESP_IF_ETH, &ip) == 0) {
|
||||
ESP_LOGI(TAG, "\n~~~~~~~~~~~\n");
|
||||
@@ -102,8 +136,13 @@ void app_main()
|
||||
config.phy_init = phy_tlk110_init;
|
||||
config.gpio_config = eth_gpio_config_rmii;
|
||||
config.tcpip_input = tcpip_adapter_eth_input;
|
||||
config.phy_check_init = phy_tlk110_check_phy_init;
|
||||
config.phy_check_link = phy_tlk110_check_phy_link_status;
|
||||
config.phy_get_speed_mode = phy_tlk110_get_speed_mode;
|
||||
config.phy_get_duplex_mode = phy_tlk110_get_duplex_mode;
|
||||
|
||||
ret = esp_eth_init(&config);
|
||||
|
||||
if(ret == ESP_OK) {
|
||||
esp_eth_enable();
|
||||
xTaskCreate(eth_task, "eth_task", 2048, NULL, (tskIDLE_PRIORITY + 2), NULL);
|
||||
|
28
examples/17_ethernet/main/tlk110_phy.h
Normal file
28
examples/17_ethernet/main/tlk110_phy.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#define BASIC_MODE_STATUS_REG (0x1)
|
||||
#define AUTO_NEGOTIATION_COMPLETE BIT(5)
|
||||
#define LINK_STATUS BIT(2)
|
||||
|
||||
#define PHY_IDENTIFIER_REG (0x2)
|
||||
#define OUI_MSB_21TO6_DEF 0x2000
|
||||
|
||||
#define SOFTWARE_STAP_CONTROL_REG (0x9)
|
||||
#define SW_STRAP_CONFIG_DONE BIT(15)
|
||||
#define AUTO_MDIX_ENABLE BIT(14)
|
||||
#define AUTO_NEGOTIATION_ENABLE BIT(13)
|
||||
#define AN_1 BIT(12)
|
||||
#define AN_0 BIT(11)
|
||||
#define LED_CFG BIT(10)
|
||||
#define RMII_ENHANCED_MODE BIT(9)
|
||||
|
||||
#define PHY_STATUS_REG (0x10)
|
||||
#define AUTO_NEGTIATION_STATUS BIT(4)
|
||||
#define DUPLEX_STATUS BIT(2)
|
||||
#define SPEED_STATUS BIT(1)
|
||||
|
||||
#define CABLE_DIAGNOSTIC_CONTROL_REG (0x1e)
|
||||
#define DIAGNOSTIC_DONE BIT(1)
|
||||
|
||||
#define PHY_RESET_CONTROL_REG (0x1f)
|
||||
#define SOFTWARE_RESET BIT(15)
|
||||
|
||||
|
9
examples/18_i2c/Makefile
Normal file
9
examples/18_i2c/Makefile
Normal file
@@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := i2c
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
29
examples/18_i2c/README.md
Normal file
29
examples/18_i2c/README.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# I2C Example
|
||||
|
||||
|
||||
* This example will show you how to use I2C module by running two tasks on i2c bus:
|
||||
|
||||
* read external i2c sensor, here we use a BH1750 light sensor(GY-30 module) for instance.
|
||||
* Use one I2C port(master mode) to read or write the other I2C port(slave mode) on one ESP32 chip.
|
||||
|
||||
* Pin assignment:
|
||||
|
||||
* slave :
|
||||
* GPIO25 is assigned as the data signal of i2c slave port
|
||||
* GPIO26 is assigned as the clock signal of i2c slave port
|
||||
* master:
|
||||
* GPIO18 is assigned as the data signal of i2c master port
|
||||
* GPIO19 is assigned as the clock signal of i2c master port
|
||||
|
||||
* Connection:
|
||||
|
||||
* connect GPIO18 with GPIO25
|
||||
* connect GPIO19 with GPIO26
|
||||
* connect sda/scl of sensor with GPIO18/GPIO19
|
||||
* no need to add external pull-up resistors, driver will enable internal pull-up resistors.
|
||||
|
||||
* Test items:
|
||||
|
||||
* read the sensor data, if connected.
|
||||
* i2c master(ESP32) will write data to i2c slave(ESP32).
|
||||
* i2c master(ESP32) will read data from i2c slave(ESP32).
|
3
examples/18_i2c/main/component.mk
Normal file
3
examples/18_i2c/main/component.mk
Normal file
@@ -0,0 +1,3 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
303
examples/18_i2c/main/i2c_test.c
Normal file
303
examples/18_i2c/main/i2c_test.c
Normal file
@@ -0,0 +1,303 @@
|
||||
/* i2c - Example
|
||||
|
||||
For other examples please check:
|
||||
https://github.com/espressif/esp-idf/tree/master/examples
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "driver/i2c.h"
|
||||
|
||||
/**
|
||||
* TEST CODE BRIEF
|
||||
*
|
||||
* This example will show you how to use I2C module by running two tasks on i2c bus:
|
||||
*
|
||||
* - read external i2c sensor, here we use a BH1750 light sensor(GY-30 module) for instance.
|
||||
* - Use one I2C port(master mode) to read or write the other I2C port(slave mode) on one ESP32 chip.
|
||||
*
|
||||
* Pin assignment:
|
||||
*
|
||||
* - slave :
|
||||
* GPIO25 is assigned as the data signal of i2c slave port
|
||||
* GPIO26 is assigned as the clock signal of i2c slave port
|
||||
* - master:
|
||||
* GPIO18 is assigned as the data signal of i2c master port
|
||||
* GPIO19 is assigned as the clock signal of i2c master port
|
||||
*
|
||||
* Connection:
|
||||
*
|
||||
* - connect GPIO18 with GPIO25
|
||||
* - connect GPIO19 with GPIO26
|
||||
* - connect sda/scl of sensor with GPIO18/GPIO19
|
||||
* - no need to add external pull-up resistors, driver will enable internal pull-up resistors.
|
||||
*
|
||||
* Test items:
|
||||
*
|
||||
* - read the sensor data, if connected.
|
||||
* - i2c master(ESP32) will write data to i2c slave(ESP32).
|
||||
* - i2c master(ESP32) will read data from i2c slave(ESP32).
|
||||
*/
|
||||
|
||||
#define DATA_LENGTH 512 /*!<Data buffer length for test buffer*/
|
||||
#define RW_TEST_LENGTH 129 /*!<Data length for r/w test, any value from 0-DATA_LENGTH*/
|
||||
#define DELAY_TIME_BETWEEN_ITEMS_MS 1234 /*!< delay time between different test items */
|
||||
|
||||
#define I2C_SLAVE_SCL_IO 26 /*!<gpio number for i2c slave clock */
|
||||
#define I2C_SLAVE_SDA_IO 25 /*!<gpio number for i2c slave data */
|
||||
#define I2C_SLAVE_NUM I2C_NUM_0 /*!<I2C port number for slave dev */
|
||||
#define I2C_SLAVE_TX_BUF_LEN (2*DATA_LENGTH) /*!<I2C slave tx buffer size */
|
||||
#define I2C_SLAVE_RX_BUF_LEN (2*DATA_LENGTH) /*!<I2C slave rx buffer size */
|
||||
|
||||
#define I2C_MASTER_SCL_IO 19 /*!< gpio number for I2C master clock */
|
||||
#define I2C_MASTER_SDA_IO 18 /*!< gpio number for I2C master data */
|
||||
#define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */
|
||||
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
|
||||
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
|
||||
#define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
|
||||
|
||||
#define BH1750_SENSOR_ADDR 0x23 /*!< slave address for BH1750 sensor */
|
||||
#define BH1750_CMD_START 0x23 /*!< Command to set measure mode */
|
||||
#define ESP_SLAVE_ADDR 0x28 /*!< ESP32 slave address, you can set any 7bit value */
|
||||
#define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
|
||||
#define READ_BIT I2C_MASTER_READ /*!< I2C master read */
|
||||
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
|
||||
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
|
||||
#define ACK_VAL 0x0 /*!< I2C ack value */
|
||||
#define NACK_VAL 0x1 /*!< I2C nack value */
|
||||
|
||||
xSemaphoreHandle print_mux;
|
||||
|
||||
/**
|
||||
* @brief test code to read esp-i2c-slave
|
||||
* We need to fill the buffer of esp slave device, then master can read them out.
|
||||
*
|
||||
* _______________________________________________________________________________________
|
||||
* | start | slave_addr + rd_bit +ack | read n-1 bytes + ack | read 1 byte + nack | stop |
|
||||
* --------|--------------------------|----------------------|--------------------|------|
|
||||
*
|
||||
*/
|
||||
esp_err_t i2c_master_read_slave(i2c_port_t i2c_num, uint8_t* data_rd, size_t size)
|
||||
{
|
||||
if (size == 0) {
|
||||
return ESP_OK;
|
||||
}
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, ( ESP_SLAVE_ADDR << 1 ) | READ_BIT, ACK_CHECK_EN);
|
||||
if (size > 1) {
|
||||
i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
|
||||
}
|
||||
i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test code to write esp-i2c-slave
|
||||
* Master device write data to slave(both esp32),
|
||||
* the data will be stored in slave buffer.
|
||||
* We can read them out from slave buffer.
|
||||
*
|
||||
* ___________________________________________________________________
|
||||
* | start | slave_addr + wr_bit + ack | write n bytes + ack | stop |
|
||||
* --------|---------------------------|----------------------|------|
|
||||
*
|
||||
*/
|
||||
esp_err_t i2c_master_write_slave(i2c_port_t i2c_num, uint8_t* data_wr, size_t size)
|
||||
{
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, ( ESP_SLAVE_ADDR << 1 ) | WRITE_BIT, ACK_CHECK_EN);
|
||||
i2c_master_write(cmd, data_wr, size, ACK_CHECK_EN);
|
||||
i2c_master_stop(cmd);
|
||||
esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief test code to write esp-i2c-slave
|
||||
*
|
||||
* 1. set mode
|
||||
* _________________________________________________________________
|
||||
* | start | slave_addr + wr_bit + ack | write 1 byte + ack | stop |
|
||||
* --------|---------------------------|---------------------|------|
|
||||
* 2. wait more than 24 ms
|
||||
* 3. read data
|
||||
* ______________________________________________________________________________________
|
||||
* | start | slave_addr + rd_bit + ack | read 1 byte + ack | read 1 byte + nack | stop |
|
||||
* --------|---------------------------|--------------------|--------------------|------|
|
||||
*/
|
||||
esp_err_t i2c_master_sensor_test(i2c_port_t i2c_num, uint8_t* data_h, uint8_t* data_l)
|
||||
{
|
||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
|
||||
i2c_master_write_byte(cmd, BH1750_CMD_START, ACK_CHECK_EN);
|
||||
i2c_master_stop(cmd);
|
||||
int ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
if (ret == ESP_FAIL) {
|
||||
return ret;
|
||||
}
|
||||
vTaskDelay(30 / portTICK_RATE_MS);
|
||||
|
||||
cmd = i2c_cmd_link_create();
|
||||
i2c_master_start(cmd);
|
||||
i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | READ_BIT, ACK_CHECK_EN);
|
||||
i2c_master_read_byte(cmd, data_h, ACK_VAL);
|
||||
i2c_master_read_byte(cmd, data_l, NACK_VAL);
|
||||
i2c_master_stop(cmd);
|
||||
ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
|
||||
i2c_cmd_link_delete(cmd);
|
||||
if (ret == ESP_FAIL) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief i2c master initialization
|
||||
*/
|
||||
void i2c_master_init()
|
||||
{
|
||||
int i2c_master_port = I2C_MASTER_NUM;
|
||||
i2c_config_t conf;
|
||||
conf.mode = I2C_MODE_MASTER;
|
||||
conf.sda_io_num = I2C_MASTER_SDA_IO;
|
||||
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
|
||||
conf.scl_io_num = I2C_MASTER_SCL_IO;
|
||||
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
|
||||
conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
|
||||
i2c_param_config(i2c_master_port, &conf);
|
||||
i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief i2c slave initialization
|
||||
*/
|
||||
void i2c_slave_init()
|
||||
{
|
||||
int i2c_slave_port = I2C_SLAVE_NUM;
|
||||
i2c_config_t conf_slave;
|
||||
conf_slave.sda_io_num = I2C_SLAVE_SDA_IO;
|
||||
conf_slave.sda_pullup_en = GPIO_PULLUP_ENABLE;
|
||||
conf_slave.scl_io_num = I2C_SLAVE_SCL_IO;
|
||||
conf_slave.scl_pullup_en = GPIO_PULLUP_ENABLE;
|
||||
conf_slave.mode = I2C_MODE_SLAVE;
|
||||
conf_slave.slave.addr_10bit_en = 0;
|
||||
conf_slave.slave.slave_addr = ESP_SLAVE_ADDR;
|
||||
i2c_param_config(i2c_slave_port, &conf_slave);
|
||||
i2c_driver_install(i2c_slave_port, conf_slave.mode, I2C_SLAVE_RX_BUF_LEN, I2C_SLAVE_TX_BUF_LEN, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief test function to show buffer
|
||||
*/
|
||||
void disp_buf(uint8_t* buf, int len)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
printf("%02x ", buf[i]);
|
||||
if (( i + 1 ) % 16 == 0) {
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void i2c_test_task(void* arg)
|
||||
{
|
||||
int i = 0;
|
||||
int ret;
|
||||
uint32_t task_idx = (uint32_t) arg;
|
||||
uint8_t* data = (uint8_t*) malloc(DATA_LENGTH);
|
||||
uint8_t* data_wr = (uint8_t*) malloc(DATA_LENGTH);
|
||||
uint8_t* data_rd = (uint8_t*) malloc(DATA_LENGTH);
|
||||
uint8_t sensor_data_h, sensor_data_l;
|
||||
|
||||
while (1) {
|
||||
ret = i2c_master_sensor_test( I2C_MASTER_NUM, &sensor_data_h, &sensor_data_l);
|
||||
xSemaphoreTake(print_mux, portMAX_DELAY);
|
||||
printf("*******************\n");
|
||||
printf("TASK[%d] MASTER READ SENSOR( BH1750 )\n", task_idx);
|
||||
printf("*******************\n");
|
||||
if (ret == ESP_OK) {
|
||||
printf("data_h: %02x\n", sensor_data_h);
|
||||
printf("data_l: %02x\n", sensor_data_l);
|
||||
printf("sensor val: %f\n", ( sensor_data_h << 8 | sensor_data_l ) / 1.2);
|
||||
} else {
|
||||
printf("No ack, sensor not connected...skip...\n");
|
||||
}
|
||||
xSemaphoreGive(print_mux);
|
||||
vTaskDelay(( DELAY_TIME_BETWEEN_ITEMS_MS * ( task_idx + 1 ) ) / portTICK_RATE_MS);
|
||||
|
||||
//---------------------------------------------------
|
||||
for (i = 0; i < DATA_LENGTH; i++) {
|
||||
data[i] = i;
|
||||
}
|
||||
size_t d_size = i2c_slave_write_buffer(I2C_SLAVE_NUM, data, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
|
||||
if (d_size == 0) {
|
||||
printf("i2c slave tx buffer full\n");
|
||||
ret = i2c_master_read_slave(I2C_MASTER_NUM, data_rd, DATA_LENGTH);
|
||||
} else {
|
||||
ret = i2c_master_read_slave(I2C_MASTER_NUM, data_rd, RW_TEST_LENGTH);
|
||||
}
|
||||
xSemaphoreTake(print_mux, portMAX_DELAY);
|
||||
printf("*******************\n");
|
||||
printf("TASK[%d] MASTER READ FROM SLAVE\n", task_idx);
|
||||
printf("*******************\n");
|
||||
printf("====TASK[%d] Slave buffer data ====\n", task_idx);
|
||||
disp_buf(data, d_size);
|
||||
if (ret == ESP_OK) {
|
||||
printf("====TASK[%d] Master read ====\n", task_idx);
|
||||
disp_buf(data_rd, d_size);
|
||||
} else {
|
||||
printf("Master read slave error, IO not connected...\n");
|
||||
}
|
||||
xSemaphoreGive(print_mux);
|
||||
vTaskDelay(( DELAY_TIME_BETWEEN_ITEMS_MS * ( task_idx + 1 ) ) / portTICK_RATE_MS);
|
||||
//---------------------------------------------------
|
||||
int size;
|
||||
for (i = 0; i < DATA_LENGTH; i++) {
|
||||
data_wr[i] = i + 10;
|
||||
}
|
||||
//we need to fill the slave buffer so that master can read later
|
||||
ret = i2c_master_write_slave( I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
|
||||
if (ret == ESP_OK) {
|
||||
size = i2c_slave_read_buffer( I2C_SLAVE_NUM, data, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
|
||||
}
|
||||
xSemaphoreTake(print_mux, portMAX_DELAY);
|
||||
printf("*******************\n");
|
||||
printf("TASK[%d] MASTER WRITE TO SLAVE\n", task_idx);
|
||||
printf("*******************\n");
|
||||
printf("----TASK[%d] Master write ----\n", task_idx);
|
||||
disp_buf(data_wr, RW_TEST_LENGTH);
|
||||
if (ret == ESP_OK) {
|
||||
printf("----TASK[%d] Slave read: [%d] bytes ----\n", task_idx, size);
|
||||
disp_buf(data, size);
|
||||
} else {
|
||||
printf("TASK[%d] Master write slave error, IO not connected....\n", task_idx);
|
||||
}
|
||||
xSemaphoreGive(print_mux);
|
||||
vTaskDelay(( DELAY_TIME_BETWEEN_ITEMS_MS * ( task_idx + 1 ) ) / portTICK_RATE_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
print_mux = xSemaphoreCreateMutex();
|
||||
i2c_slave_init();
|
||||
i2c_master_init();
|
||||
|
||||
xTaskCreate(i2c_test_task, "i2c_test_task_0", 1024 * 2, (void* ) 0, 10, NULL);
|
||||
xTaskCreate(i2c_test_task, "i2c_test_task_1", 1024 * 2, (void* ) 1, 10, NULL);
|
||||
}
|
||||
|
7
examples/19_sigmadelta/README.md
Normal file
7
examples/19_sigmadelta/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# Example: sigma_delta modulation
|
||||
|
||||
This example uses the sigma_delta output modulation driver to generate modulated output on a GPIO.
|
||||
|
||||
By default the GPIO output is 4, however you can edit this in the `sigmadelta_init()` function inside `main/sigmadelta_test.c`.
|
||||
|
||||
If you connect an LED to the output GPIO, you will see it blinking slowly.
|
@@ -46,7 +46,7 @@ void app_main()
|
||||
while(1) {
|
||||
sigmadelta_set_duty(SIGMADELTA_CHANNEL_0, duty);
|
||||
/*by changing delay time, you can change the blink frequency of LED. */
|
||||
vTaskDelay(10 / portTICK_RATE_MS);
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
|
||||
duty += inc;
|
||||
if(duty == 127 || duty == -127) inc = (-1) * inc;
|
||||
|
9
examples/20_uart/Makefile
Normal file
9
examples/20_uart/Makefile
Normal file
@@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := uart
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
3
examples/20_uart/main/component.mk
Normal file
3
examples/20_uart/main/component.mk
Normal file
@@ -0,0 +1,3 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
183
examples/20_uart/main/uart_test.c
Normal file
183
examples/20_uart/main/uart_test.c
Normal file
@@ -0,0 +1,183 @@
|
||||
/* Uart Example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "driver/uart.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "esp_log.h"
|
||||
#include "soc/uart_struct.h"
|
||||
static const char *TAG = "uart_example";
|
||||
|
||||
/**
|
||||
* Test code brief
|
||||
* This example shows how to configure uart settings and install uart driver.
|
||||
*
|
||||
* uart_evt_test() is an example that read and write data on UART0, and handler some of the special events.
|
||||
* - port: UART0
|
||||
* - rx buffer: on
|
||||
* - tx buffer: on
|
||||
* - flow control: off
|
||||
* - event queue: on
|
||||
* - pin assignment: txd(default), rxd(default)
|
||||
*
|
||||
* uart_echo_test() is an example that read and write data on UART1, with hardware flow control turning on.
|
||||
* - port: UART1
|
||||
* - rx buffer: on
|
||||
* - tx buffer: off
|
||||
* - flow control: on
|
||||
* - event queue: off
|
||||
* - pin assignment: txd(io4), rxd(io5), rts(18), cts(19)
|
||||
*/
|
||||
|
||||
#define BUF_SIZE (1024)
|
||||
#define ECHO_TEST_TXD (4)
|
||||
#define ECHO_TEST_RXD (5)
|
||||
#define ECHO_TEST_RTS (18)
|
||||
#define ECHO_TEST_CTS (19)
|
||||
|
||||
QueueHandle_t uart0_queue;
|
||||
void uart_task(void *pvParameters)
|
||||
{
|
||||
int uart_num = (int) pvParameters;
|
||||
uart_event_t event;
|
||||
size_t buffered_size;
|
||||
uint8_t* dtmp = (uint8_t*) malloc(BUF_SIZE);
|
||||
for(;;) {
|
||||
//Waiting for UART event.
|
||||
if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
|
||||
ESP_LOGI(TAG, "uart[%d] event:", uart_num);
|
||||
switch(event.type) {
|
||||
//Event of UART receving data
|
||||
/*We'd better handler data event fast, there would be much more data events than
|
||||
other types of events. If we take too much time on data event, the queue might
|
||||
be full.
|
||||
in this example, we don't process data in event, but read data outside.*/
|
||||
case UART_DATA:
|
||||
uart_get_buffered_data_len(uart_num, &buffered_size);
|
||||
ESP_LOGI(TAG, "data, len: %d; buffered len: %d", event.size, buffered_size);
|
||||
break;
|
||||
//Event of HW FIFO overflow detected
|
||||
case UART_FIFO_OVF:
|
||||
ESP_LOGI(TAG, "hw fifo overflow\n");
|
||||
//If fifo overflow happened, you should consider adding flow control for your application.
|
||||
//We can read data out out the buffer, or directly flush the rx buffer.
|
||||
uart_flush(uart_num);
|
||||
break;
|
||||
//Event of UART ring buffer full
|
||||
case UART_BUFFER_FULL:
|
||||
ESP_LOGI(TAG, "ring buffer full\n");
|
||||
//If buffer full happened, you should consider encreasing your buffer size
|
||||
//We can read data out out the buffer, or directly flush the rx buffer.
|
||||
uart_flush(uart_num);
|
||||
break;
|
||||
//Event of UART RX break detected
|
||||
case UART_BREAK:
|
||||
ESP_LOGI(TAG, "uart rx break\n");
|
||||
break;
|
||||
//Event of UART parity check error
|
||||
case UART_PARITY_ERR:
|
||||
ESP_LOGI(TAG, "uart parity error\n");
|
||||
break;
|
||||
//Event of UART frame error
|
||||
case UART_FRAME_ERR:
|
||||
ESP_LOGI(TAG, "uart frame error\n");
|
||||
break;
|
||||
//UART_PATTERN_DET
|
||||
case UART_PATTERN_DET:
|
||||
ESP_LOGI(TAG, "uart pattern detected\n");
|
||||
break;
|
||||
//Others
|
||||
default:
|
||||
ESP_LOGI(TAG, "uart event type: %d\n", event.type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
free(dtmp);
|
||||
dtmp = NULL;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void uart_evt_test()
|
||||
{
|
||||
int uart_num = UART_NUM_0;
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = 115200,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.rx_flow_ctrl_thresh = 122,
|
||||
};
|
||||
//Set UART parameters
|
||||
uart_param_config(uart_num, &uart_config);
|
||||
//Set UART log level
|
||||
esp_log_level_set(TAG, ESP_LOG_INFO);
|
||||
//Install UART driver, and get the queue.
|
||||
uart_driver_install(uart_num, BUF_SIZE * 2, BUF_SIZE * 2, 10, &uart0_queue, 0);
|
||||
//Set UART pins,(-1: default pin, no change.)
|
||||
//For UART0, we can just use the default pins.
|
||||
//uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
|
||||
//Set uart pattern detect function.
|
||||
uart_enable_pattern_det_intr(uart_num, '+', 3, 10000, 10, 10);
|
||||
//Create a task to handler UART event from ISR
|
||||
xTaskCreate(uart_task, "uart_task", 2048, (void*)uart_num, 12, NULL);
|
||||
//process data
|
||||
uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
|
||||
do {
|
||||
int len = uart_read_bytes(uart_num, data, BUF_SIZE, 100 / portTICK_RATE_MS);
|
||||
if(len > 0) {
|
||||
ESP_LOGI(TAG, "uart read : %d", len);
|
||||
uart_write_bytes(uart_num, (const char*)data, len);
|
||||
}
|
||||
} while(1);
|
||||
}
|
||||
|
||||
//an example of echo test with hardware flow control on UART1
|
||||
void uart_echo_test()
|
||||
{
|
||||
int uart_num = UART_NUM_1;
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = 115200,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
|
||||
.rx_flow_ctrl_thresh = 122,
|
||||
};
|
||||
//Configure UART1 parameters
|
||||
uart_param_config(uart_num, &uart_config);
|
||||
//Set UART1 pins(TX: IO4, RX: I05, RTS: IO18, CTS: IO19)
|
||||
uart_set_pin(uart_num, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
|
||||
//Install UART driver( We don't need an event queue here)
|
||||
//In this example we don't even use a buffer for sending data.
|
||||
uart_driver_install(uart_num, BUF_SIZE * 2, 0, 0, NULL, 0);
|
||||
|
||||
uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
|
||||
while(1) {
|
||||
//Read data from UART
|
||||
int len = uart_read_bytes(uart_num, data, BUF_SIZE, 20 / portTICK_RATE_MS);
|
||||
//Write data back to UART
|
||||
uart_write_bytes(uart_num, (const char*) data, len);
|
||||
}
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
//A uart read/write example without event queue;
|
||||
xTaskCreate(uart_echo_test, "uart_echo_test", 1024, NULL, 10, NULL);
|
||||
|
||||
//A uart example with event queue.
|
||||
uart_evt_test();
|
||||
}
|
9
examples/21_gpio/Makefile
Normal file
9
examples/21_gpio/Makefile
Normal file
@@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := gpio
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
20
examples/21_gpio/README.md
Normal file
20
examples/21_gpio/README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
# Example: GPIO
|
||||
|
||||
###This test code shows how to configure gpio and how to use gpio interrupt.
|
||||
|
||||
|
||||
####GPIO functions:
|
||||
|
||||
* GPIO18: output
|
||||
* GPIO19: output
|
||||
* GPIO4: input, pulled up, interrupt from rising edge and falling edge
|
||||
* GPIO5: input, pulled up, interrupt from rising edge.
|
||||
|
||||
####Test:
|
||||
* Connect GPIO18 with GPIO4
|
||||
* Connect GPIO19 with GPIO5
|
||||
* Generate pulses on GPIO18/19, that triggers interrupt on GPIO4/5
|
||||
|
||||
|
||||
|
||||
|
3
examples/21_gpio/main/component.mk
Normal file
3
examples/21_gpio/main/component.mk
Normal file
@@ -0,0 +1,3 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
114
examples/21_gpio/main/gpio_test.c
Normal file
114
examples/21_gpio/main/gpio_test.c
Normal file
@@ -0,0 +1,114 @@
|
||||
/* GPIO Example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "driver/gpio.h"
|
||||
|
||||
/**
|
||||
* Brief:
|
||||
* This test code shows how to configure gpio and how to use gpio interrupt.
|
||||
*
|
||||
* GPIO status:
|
||||
* GPIO18: output
|
||||
* GPIO19: output
|
||||
* GPIO4: input, pulled up, interrupt from rising edge and falling edge
|
||||
* GPIO5: input, pulled up, interrupt from rising edge.
|
||||
*
|
||||
* Test:
|
||||
* Connect GPIO18 with GPIO4
|
||||
* Connect GPIO19 with GPIO5
|
||||
* Generate pulses on GPIO18/19, that triggers interrupt on GPIO4/5
|
||||
*
|
||||
*/
|
||||
|
||||
#define GPIO_OUTPUT_IO_0 18
|
||||
#define GPIO_OUTPUT_IO_1 19
|
||||
#define GPIO_OUTPUT_PIN_SEL ((1<<GPIO_OUTPUT_IO_0) | (1<<GPIO_OUTPUT_IO_1))
|
||||
#define GPIO_INPUT_IO_0 4
|
||||
#define GPIO_INPUT_IO_1 5
|
||||
#define GPIO_INPUT_PIN_SEL ((1<<GPIO_INPUT_IO_0) | (1<<GPIO_INPUT_IO_1))
|
||||
#define ESP_INTR_FLAG_DEFAULT 0
|
||||
|
||||
static xQueueHandle gpio_evt_queue = NULL;
|
||||
|
||||
void IRAM_ATTR gpio_isr_handler(void* arg)
|
||||
{
|
||||
uint32_t gpio_num = (uint32_t) arg;
|
||||
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
|
||||
}
|
||||
|
||||
void gpio_task_example(void* arg)
|
||||
{
|
||||
uint32_t io_num;
|
||||
for(;;) {
|
||||
if(xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
|
||||
printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
gpio_config_t io_conf;
|
||||
//disable interrupt
|
||||
io_conf.intr_type = GPIO_PIN_INTR_DISABLE;
|
||||
//set as output mode
|
||||
io_conf.mode = GPIO_MODE_OUTPUT;
|
||||
//bit mask of the pins that you want to set,e.g.GPIO18/19
|
||||
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
|
||||
//disable pull-down mode
|
||||
io_conf.pull_down_en = 0;
|
||||
//disable pull-up mode
|
||||
io_conf.pull_up_en = 0;
|
||||
//configure GPIO with the given settings
|
||||
gpio_config(&io_conf);
|
||||
|
||||
//interrupt of rising edge
|
||||
io_conf.intr_type = GPIO_PIN_INTR_POSEDGE;
|
||||
//bit mask of the pins, use GPIO4/5 here
|
||||
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
|
||||
//set as input mode
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
//enable pull-up mode
|
||||
io_conf.pull_up_en = 1;
|
||||
gpio_config(&io_conf);
|
||||
|
||||
//change gpio intrrupt type for one pin
|
||||
gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);
|
||||
|
||||
//create a queue to handle gpio event from isr
|
||||
gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
|
||||
//start gpio task
|
||||
xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
|
||||
|
||||
//install gpio isr service
|
||||
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
|
||||
//hook isr handler for specific gpio pin
|
||||
gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
|
||||
//hook isr handler for specific gpio pin
|
||||
gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void*) GPIO_INPUT_IO_1);
|
||||
|
||||
//remove isr handler for gpio number.
|
||||
gpio_isr_handler_remove(GPIO_INPUT_IO_0);
|
||||
//hook isr handler for specific gpio pin again
|
||||
gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);
|
||||
|
||||
int cnt = 0;
|
||||
while(1) {
|
||||
printf("cnt: %d\n", cnt++);
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_1, cnt % 2);
|
||||
}
|
||||
}
|
||||
|
8
examples/22_i2s/Makefile
Normal file
8
examples/22_i2s/Makefile
Normal file
@@ -0,0 +1,8 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
VERBOSE = 1
|
||||
PROJECT_NAME := esp32-i2s-driver-example
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
72
examples/22_i2s/main/app_main.c
Normal file
72
examples/22_i2s/main/app_main.c
Normal file
@@ -0,0 +1,72 @@
|
||||
/* I2S Example
|
||||
|
||||
This example code will output 100Hz sine wave and triangle wave to 2-channel of I2S driver
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "driver/i2s.h"
|
||||
#include <math.h>
|
||||
|
||||
|
||||
#define SAMPLE_RATE (36000)
|
||||
#define I2S_NUM (0)
|
||||
#define WAVE_FREQ_HZ (100)
|
||||
#define PI 3.14159265
|
||||
|
||||
#define SAMPLE_PER_CYCLE (SAMPLE_RATE/WAVE_FREQ_HZ)
|
||||
|
||||
void app_main()
|
||||
{
|
||||
unsigned int i, sample_val;
|
||||
float sin_float, triangle_float, triangle_step = 65536.0 / SAMPLE_PER_CYCLE;
|
||||
//for 36Khz sample rates, we create 100Hz sine wave, every cycle need 36000/100 = 360 samples (4-bytes each sample)
|
||||
//using 6 buffers, we need 60-samples per buffer
|
||||
//2-channels, 16-bit each channel, total buffer is 360*4 = 1440 bytes
|
||||
i2s_config_t i2s_config = {
|
||||
.mode = I2S_MODE_MASTER | I2S_MODE_TX, // Only TX
|
||||
.sample_rate = SAMPLE_RATE,
|
||||
.bits_per_sample = 16, //16-bit per channel
|
||||
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
|
||||
.communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
|
||||
.dma_buf_count = 6,
|
||||
.dma_buf_len = 60, //
|
||||
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1 //Interrupt level 1
|
||||
};
|
||||
i2s_pin_config_t pin_config = {
|
||||
.bck_io_num = 26,
|
||||
.ws_io_num = 25,
|
||||
.data_out_num = 22,
|
||||
.data_in_num = -1 //Not used
|
||||
};
|
||||
|
||||
nvs_flash_init();
|
||||
i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
|
||||
i2s_set_pin(I2S_NUM, &pin_config);
|
||||
|
||||
triangle_float = -32767;
|
||||
|
||||
for(i = 0; i < SAMPLE_PER_CYCLE; i++) {
|
||||
sin_float = sin(i * PI / 180.0);
|
||||
if(sin_float >= 0)
|
||||
triangle_float += triangle_step;
|
||||
else
|
||||
triangle_float -= triangle_step;
|
||||
sin_float *= 32767;
|
||||
|
||||
sample_val = 0;
|
||||
sample_val += (short)triangle_float;
|
||||
sample_val = sample_val << 16;
|
||||
sample_val += (short) sin_float;
|
||||
|
||||
i2s_push_sample(I2S_NUM, (char *)&sample_val, portMAX_DELAY);
|
||||
}
|
||||
}
|
10
examples/22_i2s/main/component.mk
Normal file
10
examples/22_i2s/main/component.mk
Normal file
@@ -0,0 +1,10 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
||||
# This Makefile should, at the very least, just include $(SDK_PATH)/make/component.mk. By default,
|
||||
# this will take the sources in the src/ directory, compile them and link them into
|
||||
# lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable,
|
||||
# please read the SDK documents if you need to do this.
|
||||
#
|
||||
|
||||
COMPONENT_ADD_INCLUDEDIRS := .
|
Reference in New Issue
Block a user