Monday, August 28, 2023

simplify C# null check . if(~ !=null) { } is ~? syntax.

if( X != null)

{

     X.doSomething();

}


make simple like this


X?.doSomething();




Wednesday, June 14, 2023

ESP32 Clear(disable) esp_deep_sleep_enable_gpio_wakeup

in ESP32 C3

esp32 c3 have no disable gpio wakeup function. so, gpio wakeup masks adds only. 

PROBLEM:

step 1: 
    esp_deep_sleep_enable_gpio_wakeup( 1<<GPIO_NUM_2 , 0 );
    esp_deep_sleep_enable_gpio_wakeup( 1<<GPIO_NUM_5 , 0 );
    esp_deep_sleep_start();
step 2: 
    wakeup 
step 3:
    esp_deep_sleep_enable_gpio_wakeup( 1<<GPIO_NUM_5 , 0 );
    esp_deep_sleep_start();
step 4 (problem):
    ESP32 can wakeup by GPIO_NUM_2 !!!!
    

Reason: ESP32 SDK 

esp_deep_sleep_enable_gpio_wakeup does not clear for other pins.

You can modify SDK source code.

sleep_modes.c   esp_deep_sleep_enable_gpio_wakeup function


esp_err_t esp_deep_sleep_enable_gpio_wakeup(uint64_t gpio_pin_mask, esp_deepsleep_gpio_wake_up_mode_t mode)
{
    if (mode > ESP_GPIO_WAKEUP_GPIO_HIGH) {
        ESP_LOGE(TAG, "invalid mode");
        return ESP_ERR_INVALID_ARG;
    }
    gpio_int_type_t intr_type = ((mode == ESP_GPIO_WAKEUP_GPIO_LOW) ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL);
    esp_err_t err = ESP_OK;
   
    /*s_config.gpio_wakeup_mask = 0;
    for (gpio_num_t gpio_idx = GPIO_NUM_0; gpio_idx < GPIO_NUM_MAX; gpio_idx++, gpio_pin_mask >>= 1)
    {
       gpio_deep_sleep_wakeup_disable(gpio_idx);
   }*/
   
   
    for (gpio_num_t gpio_idx = GPIO_NUM_0; gpio_idx < 6; gpio_idx++, gpio_pin_mask >>= 1) {
        if ((gpio_pin_mask & 1) == 0) {
           s_config.gpio_wakeup_mask &= ~BIT(gpio_idx);
           gpio_deep_sleep_wakeup_disable(gpio_idx);
           
            continue;
        }
        if (!esp_sleep_is_valid_wakeup_gpio(gpio_idx)) {
            ESP_LOGE(TAG, "invalid mask, please ensure gpio number is no more than 5");
            return ESP_ERR_INVALID_ARG;
        }
        err = gpio_deep_sleep_wakeup_enable(gpio_idx, intr_type);

        s_config.gpio_wakeup_mask |= BIT(gpio_idx);
        if (mode == ESP_GPIO_WAKEUP_GPIO_HIGH) {
            s_config.gpio_trigger_mode |= (mode << gpio_idx);
        } else {
            s_config.gpio_trigger_mode &= ~(mode << gpio_idx);
        }
    }
    s_config.wakeup_triggers |= RTC_GPIO_TRIG_EN;
   
    rtc_hal_gpio_clear_wakeup_status();
    return err;
}


after modify , If you want use multiple pins for wakeup source, 
call esp_deep_sleep_enable_gpio_wakeup with pin bitmask. 
example: esp_deep_sleep_enable_gpio_wakeup(  1<<GPIO_NUM_2 |  1<<GPIO_NUM_5 );



In my case , sleep_modes.c located on C:\Espressif\frameworks\esp-idf-5.0\components\esp_hw_support


Wednesday, April 26, 2023

Get serial pin event and status(level) c#(ring, cts, dsr, rlsd)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO.Ports;

using System.Reflection;

using System.Runtime.InteropServices;

using Microsoft.Win32.SafeHandles;


namespace SerialEventMonitor

{

    class Program

    {

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]

        static extern Boolean GetCommModemStatus(SafeFileHandle hFile, ref UInt32 Status);


        const UInt32 MS_CTS_ON = 0x0010;

        const UInt32 MS_DSR_ON = 0x0020;

        const UInt32 MS_RING_ON = 0x0040;

        const UInt32 MS_RLSD_ON = 0x0080;


        static void Main(string[] args)

        {


            Console.WriteLine( string.Join( ",",SerialPort.GetPortNames()));

            

            string com_port_name = Console.ReadLine();



            SerialPort port = new SerialPort(com_port_name.Trim(), 9600, Parity.None, 8, StopBits.One);

            port.PinChanged += PinChanged;

            

            port.Open();


            DateTime dt_Start = DateTime.MinValue;            

            while(true)

            {

                if( port.ReadExisting() == "q")

                    break;


                System.Threading.Thread.Sleep(1000);

            }

            port.Close();

            port.Dispose();

        }


        private static void PinChanged(object sender, SerialPinChangedEventArgs e)

        {

            SerialPort port = sender as SerialPort;


            Console.WriteLine( e.EventType.ToString());

            ShowPinStates(port);

        }

        static private void ShowPinStates(System.IO.Ports.SerialPort serialPort)

        {

            UInt32 Status = 0;

            Boolean CTS = false;

            Boolean DSR = false;

            Boolean DCD = false;

            Boolean RI = false;


            if (serialPort == null) return;

            if (serialPort.IsOpen)

            {

                object baseStream = serialPort.BaseStream;

                Type baseStreamType = baseStream.GetType();


                // Get the Win32 file handle for the port

                SafeFileHandle portFileHandle = (SafeFileHandle)baseStreamType.GetField("_handle", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(baseStream);


                if (GetCommModemStatus(portFileHandle, ref Status))

                {

                    CTS = ((Status & MS_CTS_ON) == MS_CTS_ON);

                    DSR = ((Status & MS_DSR_ON) == MS_DSR_ON);

                    DCD = ((Status & MS_RLSD_ON) == MS_RLSD_ON);

                    RI = ((Status & MS_RING_ON) == MS_RING_ON);


                    Console.WriteLine(RI);


                }

            }

        }

    }

}


Friday, June 24, 2022

How to use Mitutoyo Digimatic Indicator Interface cable ( USB-ITN ) by VCP(serial) API




Mitsutoyo digimatic dial indicator has 10 pin idc connector. port interface is not standard spec. It needs USB cable for this device and computer programming.



USB-ITN cable runs as HID device, HID device has no COM port. 

Mitsutoyo supplies VCP driver for using as Serial port(COM port).


1. download VCP driver bellow link.

https://drive.google.com/file/d/1Jqvw-fO2zItXSliuVXSOlYRzt5G7uoM9/view?usp=sharing

2. download API manual 
3. Install VCP driver.
   Right click the file "usb-itn.inf" and run "install driver".
4. connect USB-ITN cable to computer.
   it will be installed automatically.
5. Open serial terminal program and connect to COM port.
6. typing "1" and press Enter key.



---------------------------------------------------
command syntax
---------------------------------------------------
request:
[1 byte command ][carridge return]

response:
[1 byte command ][1byte channel][1 byte type][ variable length data ......][carridge return]




---------------------------------------------------
command list 
---------------------------------------------------

V : DEVICE INFORMATION
1 : DATA REQ CMD
0 : MEASURED DATA RESPONSE
8 : FOOT SWITCH
9 : STATUS

ETX : 0x0d (\r)


---------------------------------------------------
example
---------------------------------------------------

REQ RES
-------------------------------
V 1ITN49001557
1 01A+0003.770


---------------------------------------------------
packet description
---------------------------------------------------
0 data command
1 channel (1, fixed)
A normal data(A, fixed)
+ sign
0 value
0
0
3
.
7
7
0
0x0d(\r)



Thursday, August 27, 2020

TDA7468이 소리를 내지 않을때. (TDA7468 not works)

TDA7468 의 Surround 기능을 off 하면 출력이 나오지 않는다.

register 0x02 에  0x19를 넣어주자.


--------------------------
if TDA7468 not works

write 0x19 into register 0x02  (surround)


ISE Design suite에서 File Open 을 하면 프로그램이 꺼진다면 (Crash on ISE Design Suite File Open Menu)

설치되어있는 디렉토리 밑에 

예를 들면 


C:\Xilinx\14.4\ISE_DS\ISE\bin\nt64

에 있는

libPortabilityNOSH.dll 을 libPortability.dll 로 대체한다.


즉. 원래있던 libPortability.dll 를 LibPortability_old.dll 식으로 백업해놓고

libPortabilityNOSH.dll 을 libPortability.dll 로 복사해넣으면됨.


1. Backup libPortability.dll 

2. Delete libPortability.dll 

3. Duplicate  libPortabilityNOSH.dll 

4. Rename "libPortabilityNOSH.dll 's copy"  to libPortability.dll 


Saturday, February 23, 2019

Raspberry pi zero w Blank monitor problem with MiniHDMI gender connector

라즈베리파이 제로의 HDMI 커넥터가 mini hdmi 이다 보니 일반 hdmi 케이블을 사용하기 위하여 gender 를 꼽아쓰게 되는데

이 젠더가 안좋으면 화면이 안나온다.

무조건 안나오는것은 아니고 모니터에 따라서 다르다.

차라리 고급진 mini hdmi - hdmi 케이블을 사서 쓰자.



Use mini HDMI to HDMI cable.

Do not use mini HDMI - HDMI gender ( watch upper picture )