ESP32 Onchip Sensor

来自Jack's Lab
2016年12月14日 (三) 22:07Comcat (讨论 | 贡献)的版本

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转到: 导航, 搜索

目录

1 Overview

Quantum-onchip-sens-1024.jpg


The normal output:

ESP32 onchip Temperature = 143
ESP32 onchip Hall sensor = 5247

When you guys place a small magnetic block near the esp32 chip:

ESP32 onchip Temperature = 145
ESP32 onchip Hall sensor = 4988

Use the other pole near the chip:

ESP32 onchip Temperature = 147
ESP32 onchip Hall sensor = 5401



2 Hacked API

In librtc.a:

 uint8_t temprature_sens_read(); 
 uint32_t hall_sens_read();



3 Quick Start

Build
$ sudo apt-get install git wget make libncurses-dev flex bison gperf python python-serial
$ wget https://dl.espressif.com/dl/xtensa-esp32-elf-linux32-1.22.0-59.tar.gz
$ mkdir -p toolchain
$ tar zxf xtensa-esp32-elf-linux32-1.22.0-59.tar.gz -C toolchain
$ export PATH=$PATH:`pwd`/toolchain/xtensa-esp32-elf/bin
$
$ git clone --recursive git://github.com/icamgo/esp-idf.git
$ export IDF_PATH=`pwd`/esp-idf
$ cd esp-idf/examples/09_onchip_sensor
$ make menuconfig
$ make flash

You need to press the RST buttom after uploading the firmware into flash. If you guys do not like to do this please patch the /path/to/esp-idf/components/esptool_py/esptool/esptool.py :

diff --git a/esptool.py b/esptool.py
index 755f4cb..ff92c91 100755
--- a/esptool.py
+++ b/esptool.py
@@ -197,6 +197,12 @@ class ESPLoader(object):
               + '\xc0'
         self._port.write(buf)
 
+    def reset_to_app(self):
+        self._port.setDTR(False)
+        self._port.setRTS(True)
+        time.sleep(0.05)
+        self._port.setRTS(True)
+
     """ Calculate checksum of a blob, as it is defined by the ROM """
     @staticmethod
     def checksum(data, state=ESP_CHECKSUM_MAGIC):
@@ -1421,7 +1427,6 @@ def dump_mem(esp, args):
         sys.stdout.flush()
     print 'Done!'
 
-
 def write_flash(esp, args):
     """Write data to flash
     """
@@ -1503,6 +1508,7 @@ def write_flash(esp, args):
     if args.verify:
         print 'Verifying just-written flash...'
         verify_flash(esp, args, header_block)
+    esp.reset_to_app()
 
 
 def image_info(args):

Then Quantum can reset to run your app automatically after uploading the firmware into flash


Check output
$ picocom -b 115200 /dev/ttyUSB0
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0x00
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:QIO, clock div:1
load:0x3ffc0000,len:0
load:0x3ffc0000,len:920
load:0x40078000,len:3220
ho 0 tail 12 room 4
load:0x40080000,len:260
entry 0x40080034
I (63) heap_alloc_caps: Initializing heap allocator:
I (63) heap_alloc_caps: Region 19: 3FFB45D8 len 0002BA28 tag 0
I (65) heap_alloc_caps: Region 25: 3FFE8000 len 00018000 tag 1
I (74) cpu_start: Pro cpu up.
I (80) cpu_start: Single core mode
I (86) cpu_start: Pro cpu start user code
rtc v118 Oct 19 2016 15:22:11
XTAL 40M
I (121) cpu_start: Starting scheduler on PRO CPU.
Welcome to Noduino Quantum
Try to read the temperature sensor of the ESP32 onchip ... 
ESP32 onchip Temperature = 143
ESP32 onchip Hall sensor = 5247
ESP32 onchip Temperature = 145
ESP32 onchip Hall sensor = 5240



4 Code

/*
 * MaiKe Labs (2016 - 2026)
 *
 * Written by Jack Tan <jiankemeng@gmail.com>
 *
 * 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 <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"

uint8_t temprature_sens_read();	
uint32_t hall_sens_read();

void read_sens_task(void *pvParameters)
{
	while (1) {
		printf("ESP32 onchip Temperature = %d\n", temprature_sens_read());
		printf("ESP32 onchip Hall sensor = %d\n", hall_sens_read());
		vTaskDelay(4000 / portTICK_PERIOD_MS);
	}
}

void app_main()
{
	nvs_flash_init();
	system_init();
	printf("Welcome to Noduino Quantum\r\n");
	printf("Try to read the sensors inside the ESP32 chip ... \r\n");
	xTaskCreatePinnedToCore(&read_sens_task, "read_sens_task", 2048, NULL, 5,
				NULL, 0);
}



5 Study

  • The Hall Sensor uses Channel_0 and Channel_3 of ADC1.
  • Set the width of ADC1 module to 12bit by calling adc1_config_width before calling hall_sensor_read.
  • Please do not use Channel_0 and Channel_3 of ADC1 as ADC channels when you use the hall sensor.


static int hall_sensor_get_value()    //hall sensor without LNA
{
    int Sens_Vp0;
    int Sens_Vn0;
    int Sens_Vp1;
    int Sens_Vn1;
    int hall_value;

    portENTER_CRITICAL(&rtc_spinlock);

    // XPD HALL is controlled by SW
    SET_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL1_REG, SENS_XPD_HALL_FORCE_M);

    // Power on hall sensor and connect to VP and VN
    SET_PERI_REG_MASK(RTC_IO_HALL_SENS_REG, RTC_IO_XPD_HALL);

    // HALL PHASE is controlled by SW
    SET_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL1_REG, SENS_HALL_PHASE_FORCE_M);

    // normal phase of hall sensor
    CLEAR_PERI_REG_MASK(RTC_IO_HALL_SENS_REG, RTC_IO_HALL_PHASE);

    Sens_Vp0 = adc1_get_voltage(ADC1_CHANNEL_0);
    Sens_Vn0 = adc1_get_voltage(ADC1_CHANNEL_3);

    // Reverse phase of hall sensor
    SET_PERI_REG_MASK(RTC_IO_HALL_SENS_REG, RTC_IO_HALL_PHASE);

    Sens_Vp1 = adc1_get_voltage(ADC1_CHANNEL_0);
    Sens_Vn1 = adc1_get_voltage(ADC1_CHANNEL_3);

    // SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR
    SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 0, SENS_FORCE_XPD_SAR_S);

    CLEAR_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL1_REG, SENS_XPD_HALL_FORCE);
    CLEAR_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL1_REG, SENS_HALL_PHASE_FORCE);

    portEXIT_CRITICAL(&rtc_spinlock);

    hall_value = (Sens_Vp1 - Sens_Vp0) - (Sens_Vn1 - Sens_Vn0);

    return hall_value;
}

int hall_sensor_read()
{
    adc1_pad_init(ADC1_CHANNEL_0);
    adc1_pad_init(ADC1_CHANNEL_3);
    adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_0db);
    adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_0db);
    return hall_sensor_get_value();
}



6 Reference

For more information please refer to

















个人工具
名字空间

变换
操作
导航
工具箱