diff --git a/boards/generic-ln882hki.json b/boards/generic-ln882hki.json new file mode 100644 index 0000000..a531dff --- /dev/null +++ b/boards/generic-ln882hki.json @@ -0,0 +1,118 @@ +{ + "_base": [ + "generic", + "lightning-ln882x", + "lightning-ln882hki", + "ic/ln882hk" + ], + "build": { + "mcu": "ln882hk", + "variant": "generic-ln882hki" + }, + "name": "Generic - LN882HKI", + "symbol": "LN882HKI", + "url": "https://docs.libretiny.eu/boards/generic-ln882hki/", + "vendor": "Generic", + "pcb": { + "pinout": { + "1": { + "IC": 8, + "ARD": [ + "D0", + "A2" + ] + }, + "2": { + "IC": 9, + "ARD": [ + "D1", + "A3" + ] + }, + "3": { + "IC": 10, + "ARD": "D2" + }, + "4": { + "IC": 11, + "ARD": "D3" + }, + "5": { + "IC": 12, + "ARD": [ + "D4", + "A4" + ] + }, + "6": { + "IC": 13, + "ARD": "D5" + }, + "7": { + "IC": 14, + "ARD": "D6" + }, + "8": { + "IC": 15, + "ARD": "D7" + }, + "9": { + "IC": 16, + "ARD": "D8" + }, + "10": { + "IC": 17, + "ARD": "D9" + }, + "11": { + "IC": 21, + "ARD": "D10" + }, + "12": { + "IC": 22, + "ARD": "D11" + }, + "13": { + "IC": 23, + "ARD": "D12" + }, + "14": { + "IC": 24, + "ARD": [ + "D13", + "A5" + ] + }, + "15": { + "IC": 25, + "ARD": [ + "D14", + "A6" + ] + }, + "16": { + "IC": 26, + "ARD": [ + "D15", + "A7" + ] + }, + "17": { + "IC": 27, + "ARD": "D16" + }, + "18": { + "IC": 28, + "ARD": "D17" + }, + "19": { + "IC": 29, + "ARD": "D18" + }, + "20": { + "IC": 30, + "ARD": "D19" + } + } + } +} diff --git a/boards/variants/generic-ln882hki.c b/boards/variants/generic-ln882hki.c new file mode 100644 index 0000000..3dcddb5 --- /dev/null +++ b/boards/variants/generic-ln882hki.c @@ -0,0 +1,75 @@ +/* This file was auto-generated from generic-ln882hki.json using boardgen */ + +#include + +#ifdef LT_VARIANT_INCLUDE +#include LT_VARIANT_INCLUDE +#endif + +// clang-format off +PinInfo lt_arduino_pin_info_list[PINS_COUNT] = { + // D0: PA00, ADC2, I2C0_SCL, I2C0_SDA + {PA_0, PIN_I2C | PIN_ADC | PIN_IRQ | PIN_GPIO, PIN_NONE, 0}, + // D1: PA01, ADC3, SWDIO, I2C0_SCL, I2C0_SDA + {PA_1, PIN_SWD | PIN_I2C | PIN_ADC | PIN_IRQ | PIN_GPIO, PIN_NONE, 0}, + // D2: PA02, UART0_TX, I2C0_SCL, I2C0_SDA + {PA_2, PIN_UART | PIN_I2C | PIN_IRQ | PIN_GPIO, PIN_NONE, 0}, + // D3: PA03, UART0_RX, I2C0_SCL, I2C0_SDA + {PA_3, PIN_UART | PIN_I2C | PIN_IRQ | PIN_GPIO, PIN_NONE, 0}, + // D4: PA04, ADC4, SWCLK, I2C0_SCL, I2C0_SDA + {PA_4, PIN_SWD | PIN_I2C | PIN_ADC | PIN_GPIO, PIN_NONE, 0}, + // D5: PA05, I2C0_SCL, I2C0_SDA + {PA_5, PIN_I2C | PIN_IRQ | PIN_GPIO, PIN_NONE, 0}, + // D6: PA06, SD_D2, I2C0_SCL, I2C0_SDA + {PA_6, PIN_I2C | PIN_IRQ | PIN_GPIO, PIN_NONE, 0}, + // D7: PA07, SD_D3, I2S0_RX, I2C0_SCL, I2C0_SDA + {PA_7, PIN_I2S | PIN_I2C | PIN_IRQ | PIN_GPIO, PIN_NONE, 0}, + // D8: PA08, BOOT0, SD_CMD, I2S0_WS, I2C0_SCL, I2C0_SDA + {PA_8, PIN_I2S | PIN_I2C | PIN_GPIO, PIN_NONE, 0}, + // D9: PA09, BOOT1, SD_CLK, I2S0_SCLK, I2C0_SCL, I2C0_SDA + {PA_9, PIN_I2S | PIN_I2C | PIN_GPIO, PIN_NONE, 0}, + // D10: PA10, SD_D0, I2S0_TX, I2C0_SCL, I2C0_SDA + {PA_10, PIN_I2S | PIN_I2C | PIN_GPIO, PIN_NONE, 0}, + // D11: PA11, SD_D1, I2C0_SCL, I2C0_SDA + {PA_11, PIN_I2C | PIN_GPIO, PIN_NONE, 0}, + // D12: PA12, I2C0_SCL, I2C0_SDA + {PA_12, PIN_I2C | PIN_GPIO, PIN_NONE, 0}, + // D13: PB03, ADC5, I2C0_SCL, I2C0_SDA + {PB_3, PIN_I2C | PIN_ADC | PIN_GPIO, PIN_NONE, 0}, + // D14: PB04, ADC6, I2C0_SCL, I2C0_SDA + {PB_4, PIN_I2C | PIN_ADC | PIN_GPIO, PIN_NONE, 0}, + // D15: PB05, ADC7, I2C0_SCL, I2C0_SDA + {PB_5, PIN_I2C | PIN_ADC | PIN_GPIO, PIN_NONE, 0}, + // D16: PB06, I2C0_SCL, I2C0_SDA + {PB_6, PIN_I2C | PIN_GPIO, PIN_NONE, 0}, + // D17: PB07, I2C0_SCL, I2C0_SDA + {PB_7, PIN_I2C | PIN_GPIO, PIN_NONE, 0}, + // D18: PB08, UART1_RX, I2C0_SCL, I2C0_SDA + {PB_8, PIN_UART | PIN_I2C | PIN_GPIO, PIN_NONE, 0}, + // D19: PB09, UART1_TX, I2C0_SCL, I2C0_SDA + {PB_9, PIN_UART | PIN_I2C | PIN_IRQ | PIN_GPIO, PIN_NONE, 0}, +}; + +PinInfo *lt_arduino_pin_gpio_map[] = { + [0] = &(lt_arduino_pin_info_list[0]), // PA_0 (D0) + [1] = &(lt_arduino_pin_info_list[1]), // PA_1 (D1) + [2] = &(lt_arduino_pin_info_list[2]), // PA_2 (D2) + [3] = &(lt_arduino_pin_info_list[3]), // PA_3 (D3) + [4] = &(lt_arduino_pin_info_list[4]), // PA_4 (D4) + [5] = &(lt_arduino_pin_info_list[5]), // PA_5 (D5) + [6] = &(lt_arduino_pin_info_list[6]), // PA_6 (D6) + [7] = &(lt_arduino_pin_info_list[7]), // PA_7 (D7) + [8] = &(lt_arduino_pin_info_list[8]), // PA_8 (D8) + [9] = &(lt_arduino_pin_info_list[9]), // PA_9 (D9) + [10] = &(lt_arduino_pin_info_list[10]), // PA_10 (D10) + [11] = &(lt_arduino_pin_info_list[11]), // PA_11 (D11) + [12] = &(lt_arduino_pin_info_list[12]), // PA_12 (D12) + [19] = &(lt_arduino_pin_info_list[13]), // PB_3 (D13) + [20] = &(lt_arduino_pin_info_list[14]), // PB_4 (D14) + [21] = &(lt_arduino_pin_info_list[15]), // PB_5 (D15) + [22] = &(lt_arduino_pin_info_list[16]), // PB_6 (D16) + [23] = &(lt_arduino_pin_info_list[17]), // PB_7 (D17) + [24] = &(lt_arduino_pin_info_list[18]), // PB_8 (D18) + [25] = &(lt_arduino_pin_info_list[19]), // PB_9 (D19) +}; +// clang-format on diff --git a/boards/variants/generic-ln882hki.h b/boards/variants/generic-ln882hki.h new file mode 100644 index 0000000..981ce63 --- /dev/null +++ b/boards/variants/generic-ln882hki.h @@ -0,0 +1,168 @@ +/* This file was auto-generated from generic-ln882hki.json using boardgen */ + +#pragma once + +// clang-format off + +// Pins +// ---- +#define PINS_COUNT 20 // Total GPIO count +#define NUM_DIGITAL_PINS 20 // Digital inputs/outputs +#define NUM_ANALOG_INPUTS 6 // ADC inputs +#define NUM_ANALOG_OUTPUTS 0 // PWM & DAC outputs +#define PINS_GPIO_MAX 25 // Last usable GPIO number + +// Wire Interfaces +// --------------- +#define PIN_WIRE0_SCL_0 0u // PA_0 +#define PIN_WIRE0_SCL_1 1u // PA_1 +#define PIN_WIRE0_SCL_2 2u // PA_2 +#define PIN_WIRE0_SCL_3 3u // PA_3 +#define PIN_WIRE0_SCL_4 4u // PA_4 +#define PIN_WIRE0_SCL_5 5u // PA_5 +#define PIN_WIRE0_SCL_6 6u // PA_6 +#define PIN_WIRE0_SCL_7 7u // PA_7 +#define PIN_WIRE0_SCL_8 8u // PA_8 +#define PIN_WIRE0_SCL_9 9u // PA_9 +#define PIN_WIRE0_SCL_10 10u // PA_10 +#define PIN_WIRE0_SCL_11 11u // PA_11 +#define PIN_WIRE0_SCL_12 12u // PA_12 +#define PIN_WIRE0_SCL_13 19u // PB_3 +#define PIN_WIRE0_SCL_14 20u // PB_4 +#define PIN_WIRE0_SCL_15 21u // PB_5 +#define PIN_WIRE0_SCL_16 22u // PB_6 +#define PIN_WIRE0_SCL_17 23u // PB_7 +#define PIN_WIRE0_SCL_18 24u // PB_8 +#define PIN_WIRE0_SCL_19 25u // PB_9 +#define PIN_WIRE0_SDA_0 0u // PA_0 +#define PIN_WIRE0_SDA_1 1u // PA_1 +#define PIN_WIRE0_SDA_2 2u // PA_2 +#define PIN_WIRE0_SDA_3 3u // PA_3 +#define PIN_WIRE0_SDA_4 4u // PA_4 +#define PIN_WIRE0_SDA_5 5u // PA_5 +#define PIN_WIRE0_SDA_6 6u // PA_6 +#define PIN_WIRE0_SDA_7 7u // PA_7 +#define PIN_WIRE0_SDA_8 8u // PA_8 +#define PIN_WIRE0_SDA_9 9u // PA_9 +#define PIN_WIRE0_SDA_10 10u // PA_10 +#define PIN_WIRE0_SDA_11 11u // PA_11 +#define PIN_WIRE0_SDA_12 12u // PA_12 +#define PIN_WIRE0_SDA_13 19u // PB_3 +#define PIN_WIRE0_SDA_14 20u // PB_4 +#define PIN_WIRE0_SDA_15 21u // PB_5 +#define PIN_WIRE0_SDA_16 22u // PB_6 +#define PIN_WIRE0_SDA_17 23u // PB_7 +#define PIN_WIRE0_SDA_18 24u // PB_8 +#define PIN_WIRE0_SDA_19 25u // PB_9 +#define PINS_WIRE0_SCL (pin_size_t[]){0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u, 12u, 19u, 20u, 21u, 22u, 23u, 24u, 25u} +#define PINS_WIRE0_SDA (pin_size_t[]){0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u, 12u, 19u, 20u, 21u, 22u, 23u, 24u, 25u} + +// Serial ports +// ------------ +#define PIN_SERIAL0_RX 3u // PA_3 +#define PIN_SERIAL0_TX 2u // PA_2 +#define PIN_SERIAL1_RX 24u // PB_8 +#define PIN_SERIAL1_TX 25u // PB_9 +#define PINS_SERIAL0_RX (pin_size_t[]){3u} +#define PINS_SERIAL0_TX (pin_size_t[]){2u} +#define PINS_SERIAL1_RX (pin_size_t[]){24u} +#define PINS_SERIAL1_TX (pin_size_t[]){25u} + +// Pin function macros +// ------------------- +#define PIN_ADC2 0u // PA_0 +#define PIN_ADC3 1u // PA_1 +#define PIN_ADC4 4u // PA_4 +#define PIN_ADC5 19u // PB_3 +#define PIN_ADC6 20u // PB_4 +#define PIN_ADC7 21u // PB_5 +#define PIN_PA00 0u // PA_0 +#define PIN_PA01 1u // PA_1 +#define PIN_PA02 2u // PA_2 +#define PIN_PA03 3u // PA_3 +#define PIN_PA04 4u // PA_4 +#define PIN_PA05 5u // PA_5 +#define PIN_PA06 6u // PA_6 +#define PIN_PA07 7u // PA_7 +#define PIN_PA08 8u // PA_8 +#define PIN_PA09 9u // PA_9 +#define PIN_PA10 10u // PA_10 +#define PIN_PA11 11u // PA_11 +#define PIN_PA12 12u // PA_12 +#define PIN_PB03 19u // PB_3 +#define PIN_PB04 20u // PB_4 +#define PIN_PB05 21u // PB_5 +#define PIN_PB06 22u // PB_6 +#define PIN_PB07 23u // PB_7 +#define PIN_PB08 24u // PB_8 +#define PIN_PB09 25u // PB_9 +#define PIN_RX0 3u // PA_3 +#define PIN_RX1 24u // PB_8 +#define PIN_TX0 2u // PA_2 +#define PIN_TX1 25u // PB_9 + +// Port availability +// ----------------- +#define HAS_SERIAL0 1 +#define HAS_SERIAL1 1 +#define HAS_WIRE0 1 +#define SERIAL_INTERFACES_COUNT 2 +#define WIRE_INTERFACES_COUNT 1 + +// Arduino pin names +// ----------------- +#define PIN_D0 0u // PA_0 +#define PIN_D1 1u // PA_1 +#define PIN_D2 2u // PA_2 +#define PIN_D3 3u // PA_3 +#define PIN_D4 4u // PA_4 +#define PIN_D5 5u // PA_5 +#define PIN_D6 6u // PA_6 +#define PIN_D7 7u // PA_7 +#define PIN_D8 8u // PA_8 +#define PIN_D9 9u // PA_9 +#define PIN_D10 10u // PA_10 +#define PIN_D11 11u // PA_11 +#define PIN_D12 12u // PA_12 +#define PIN_D13 19u // PB_3 +#define PIN_D14 20u // PB_4 +#define PIN_D15 21u // PB_5 +#define PIN_D16 22u // PB_6 +#define PIN_D17 23u // PB_7 +#define PIN_D18 24u // PB_8 +#define PIN_D19 25u // PB_9 +#define PIN_A2 0u // PA_0 +#define PIN_A3 1u // PA_1 +#define PIN_A4 4u // PA_4 +#define PIN_A5 19u // PB_3 +#define PIN_A6 20u // PB_4 +#define PIN_A7 21u // PB_5 + +// Static pin names +// ---------------- +static const unsigned char A2 = PIN_A2; +static const unsigned char A3 = PIN_A3; +static const unsigned char A4 = PIN_A4; +static const unsigned char A5 = PIN_A5; +static const unsigned char A6 = PIN_A6; +static const unsigned char A7 = PIN_A7; +static const unsigned char D0 = PIN_D0; +static const unsigned char D1 = PIN_D1; +static const unsigned char D2 = PIN_D2; +static const unsigned char D3 = PIN_D3; +static const unsigned char D4 = PIN_D4; +static const unsigned char D5 = PIN_D5; +static const unsigned char D6 = PIN_D6; +static const unsigned char D7 = PIN_D7; +static const unsigned char D8 = PIN_D8; +static const unsigned char D9 = PIN_D9; +static const unsigned char D10 = PIN_D10; +static const unsigned char D11 = PIN_D11; +static const unsigned char D12 = PIN_D12; +static const unsigned char D13 = PIN_D13; +static const unsigned char D14 = PIN_D14; +static const unsigned char D15 = PIN_D15; +static const unsigned char D16 = PIN_D16; +static const unsigned char D17 = PIN_D17; +static const unsigned char D18 = PIN_D18; +static const unsigned char D19 = PIN_D19; diff --git a/cores/lightning-ln882h/arduino/libraries/WiFi/WiFiSTA.cpp b/cores/lightning-ln882h/arduino/libraries/WiFi/WiFiSTA.cpp index 3a259f9..97b73de 100644 --- a/cores/lightning-ln882h/arduino/libraries/WiFi/WiFiSTA.cpp +++ b/cores/lightning-ln882h/arduino/libraries/WiFi/WiFiSTA.cpp @@ -175,7 +175,7 @@ bool WiFiClass::setMacAddress(const uint8_t *mac) { } const String WiFiClass::SSID() { - const char *ssid = NULL; + const char *ssid = ""; const uint8_t *bssid = NULL; wifi_get_sta_conn_info(&ssid, &bssid); return ssid; diff --git a/cores/lightning-ln882h/arduino/src/main.cpp b/cores/lightning-ln882h/arduino/src/main.cpp index d2c7eae..15b372c 100644 --- a/cores/lightning-ln882h/arduino/src/main.cpp +++ b/cores/lightning-ln882h/arduino/src/main.cpp @@ -5,9 +5,13 @@ extern "C" { -#define MAIN_TASK_STACK_SIZE 8192 +extern void cal_temp_app_task_entry(); + +#define MAIN_TASK_STACK_SIZE 8192 +#define TEMP_APP_TASK_STACK_SIZE 1024 static OS_Thread_t g_mainTask_thread; +static OS_Thread_t g_temp_cal_thread; bool startMainTask() { OS_Status ret = OS_ThreadCreate( @@ -21,6 +25,20 @@ bool startMainTask() { if (ret != OS_OK) return false; + + ret = OS_ThreadCreate( + &g_temp_cal_thread, + "TempCal", + (OS_ThreadEntry_t)cal_temp_app_task_entry, + NULL, + OS_PRIORITY_BELOW_NORMAL, + TEMP_APP_TASK_STACK_SIZE + ); + + if (ret != OS_OK) { + return false; + } + OS_ThreadStartScheduler(); return true; } diff --git a/cores/lightning-ln882h/arduino/src/wiring_analog.c b/cores/lightning-ln882h/arduino/src/wiring_analog.c index 0f2a996..6039381 100644 --- a/cores/lightning-ln882h/arduino/src/wiring_analog.c +++ b/cores/lightning-ln882h/arduino/src/wiring_analog.c @@ -43,12 +43,11 @@ static adc_ch_t pinToAdcCh(uint32_t gpio) { return ADC_CH0; } -// WARN: adc values are quite bad (zero value of ~ 1000lsb and full scale value ~ 3450lsb) uint16_t analogReadVoltage(pin_size_t pinNumber) { uint16_t ret = 0; pinCheckGetInfo(pinNumber, PIN_ADC, 0); - hal_gpio_pin_mode_set(GPIO_GET_BASE(pin->gpio), pin->gpio, GPIO_MODE_ANALOG); + hal_gpio_pin_mode_set(GPIO_GET_BASE(pin->gpio), GPIO_GET_PIN(pin->gpio), GPIO_MODE_ANALOG); adc_ch_t ch = pinToAdcCh(pin->gpio); adc_init_t_def adc_init; @@ -72,7 +71,7 @@ uint16_t analogReadVoltage(pin_size_t pinNumber) { hal_adc_clr_conv_status(ADC_BASE, ch); - return (uint16_t)(3300UL * ret / 4095); + return (uint16_t)(3700UL * ret / 4095); } uint16_t analogReadMaxVoltage(pin_size_t pinNumber) { diff --git a/cores/lightning-ln882h/base/fixups/wifi_cal.c b/cores/lightning-ln882h/base/fixups/wifi_cal.c new file mode 100644 index 0000000..dcd71d2 --- /dev/null +++ b/cores/lightning-ln882h/base/fixups/wifi_cal.c @@ -0,0 +1,57 @@ +/* Copyright (c) Etienne Le Cousin 2025-05-04. */ + +#include + +void cal_adc_init(void) +{ + adc_init_t_def adc_init; + + memset(&adc_init, 0, sizeof(adc_init_t_def)); + adc_init.adc_ch = ADC_CH0; + adc_init.adc_conv_mode = ADC_CONV_MODE_CONTINUE; + adc_init.adc_presc = 0xFF; + adc_init.adc_ov_smp_ratio = ADC_OVER_SAMPLING_RATIO_X8; + adc_init.adc_ov_smp_ratio_en = ADC_OVER_SAMPLING_EN_STATUS_BIT0; + hal_adc_init(ADC_BASE, &adc_init); + + hal_adc_en(ADC_BASE, HAL_ENABLE); + + hal_adc_start_conv(ADC_BASE); +} + +uint16_t cal_adc_read(adc_ch_t ch) +{ + uint16_t read_adc = 0; + + while(hal_adc_get_conv_status(ADC_BASE, ch) == 0); + + read_adc = hal_adc_get_data(ADC_BASE, ch); + + hal_adc_clr_conv_status(ADC_BASE,ch); + + return read_adc; +} + +void cal_temp_app_task_entry() +{ + int8_t cap_comp = 0; + uint16_t adc_val = 0; + + if (NVDS_ERR_OK == ln_nvds_get_xtal_comp_val((uint8_t *)&cap_comp)) { + if ((uint8_t)cap_comp == 0xFF) { + cap_comp = 0; + } + } + + cal_adc_init(); + + wifi_temp_cal_init(cal_adc_read(ADC_CH0), cap_comp); + + while (1) + { + OS_MsDelay(1000); + + adc_val = cal_adc_read(ADC_CH0); + wifi_do_temp_cal_period(adc_val); + } +}