[lightning-ln882h] Add support for Lightning LN882H family (#312)
* fix mbedtls bad pointer in function call (prototype mismatch) * fix issue with weak families functions implemented in static library, it will never be linked. fixed by redefining prototypes inside families * [ln882x] add support for lightning ln882x & ln882h families * add i2c (wire) support * add analog (adc) support * add watchdog support * [ln882x] changed default uart 0/1 pins; added board wl2s * [ln882x] fix IRQ & ADC pins * [ln882x] boards cosmetic * [ln882x] wifi sta use otp mac addr by default; re-enabled wifi powersave mode * [ln882x] clang-format clean code * [ln882x] clang-format clean code * Update families.json * Apply suggestions from code review * [ln882x] reformat json board files * [ln882x] os_queue cleanup * [ln882x] removed Beken auto-download command * [ln882x] removed personal script file * [ln882x] removed unusefull pi section in debugging.md * [ln882x] removed Arduino.h and changed private I2C definition * [ln882x] updated README.md * [ln882x] changed pin naming scheme to PA/PB * [ln882x] clean code * [ln882x] clean code * [ln882x] add ota image verification * Update push-dev.yml * [ln882x] fix boards ADC missing inputs] * [ln882x] removed reg_xxx fixup files and use include guards instead * [ln882x] cleanup code * [ln882x] cleanup code * [ln882x] fix lt_init weak functions linking * [ln882x] revert lt_api.h modification, fixed with previous commit * [ln882x] setup UF2 firmware for flasher with partitions * [ln882x] update README.md * [ln882x] include ln_wifi.h and ln_serial.h to avoid including bad headers on case insensitive systems * [ln882x] Replace RingBuffer by SerialRingBuffer * [ln882x] clang-format * [ln882x] update README.md * Apply suggestions from code review * Reformat board JSON files * Add mkdocs link redirect * Update ltchiptool to v4.12.0 --------- Co-authored-by: Kuba Szczodrzyński <kuba@szczodrzynski.pl>
This commit is contained in:
99
cores/lightning-ln882h/arduino/libraries/Serial/Serial.cpp
Normal file
99
cores/lightning-ln882h/arduino/libraries/Serial/Serial.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-02-10. */
|
||||
|
||||
#include "SerialPrivate.h"
|
||||
|
||||
extern Serial_t *serial_handles[SER_PORT_NUM];
|
||||
|
||||
#if LT_HW_UART0
|
||||
SerialClass Serial0(0, PIN_SERIAL0_RX, PIN_SERIAL0_TX);
|
||||
|
||||
static void callback_uart0(void) {
|
||||
SerialData *data = (SerialData *)Serial0.data;
|
||||
char ch;
|
||||
while (serial_read(serial_handles[0], &ch, 1)) {
|
||||
data->buf.store_char(ch);
|
||||
}
|
||||
}
|
||||
#else
|
||||
#define callback_uart0 NULL
|
||||
#endif
|
||||
#if LT_HW_UART1
|
||||
SerialClass Serial1(1, PIN_SERIAL1_RX, PIN_SERIAL1_TX);
|
||||
|
||||
static void callback_uart1(void) {
|
||||
SerialData *data = (SerialData *)Serial1.data;
|
||||
char ch;
|
||||
while (serial_read(serial_handles[1], &ch, 1)) {
|
||||
data->buf.store_char(ch);
|
||||
}
|
||||
}
|
||||
#else
|
||||
#define callback_uart1 NULL
|
||||
#endif
|
||||
#if LT_HW_UART2
|
||||
SerialClass Serial2(2, PIN_SERIAL2_RX, PIN_SERIAL2_TX);
|
||||
|
||||
static void callback_uart2(void) {
|
||||
SerialData *data = (SerialData *)Serial2.data;
|
||||
char ch;
|
||||
while (serial_read(serial_handles[2], &ch, 1)) {
|
||||
data->buf.store_char(ch);
|
||||
}
|
||||
}
|
||||
#else
|
||||
#define callback_uart2 NULL
|
||||
#endif
|
||||
|
||||
// clang-format off
|
||||
static const serial_rx_callbcak serial_rx_callbacks[SER_PORT_NUM] = {
|
||||
callback_uart0,
|
||||
callback_uart1,
|
||||
callback_uart2
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
void SerialClass::begin(unsigned long baudrate, uint16_t config) {
|
||||
if (!this->data) {
|
||||
this->data = new SerialData();
|
||||
this->buf = &BUF;
|
||||
}
|
||||
|
||||
if (this->baudrate != baudrate || this->config != config)
|
||||
this->configure(baudrate, config);
|
||||
}
|
||||
|
||||
void SerialClass::configure(unsigned long baudrate, uint16_t config) {
|
||||
if (!this->data)
|
||||
return;
|
||||
|
||||
serial_init(serial_handles[port], (serial_port_id_t)port, baudrate, serial_rx_callbacks[port]);
|
||||
|
||||
this->baudrate = baudrate;
|
||||
this->config = config;
|
||||
}
|
||||
|
||||
void SerialClass::end() {
|
||||
if (!this->data)
|
||||
return;
|
||||
|
||||
serial_deinit(serial_handles[port]);
|
||||
|
||||
delete (SerialData *)this->data;
|
||||
delete this->buf;
|
||||
this->data = NULL;
|
||||
this->buf = NULL;
|
||||
this->baudrate = 0;
|
||||
}
|
||||
|
||||
void SerialClass::flush() {
|
||||
if (!this->data)
|
||||
return;
|
||||
serial_flush(serial_handles[port]);
|
||||
}
|
||||
|
||||
size_t SerialClass::write(uint8_t c) {
|
||||
if (!this->data)
|
||||
return 0;
|
||||
serial_putchar(serial_handles[port], c);
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-02-10. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <sdk_private.h>
|
||||
|
||||
typedef struct {
|
||||
SerialRingBuffer buf;
|
||||
void (*callback)(void);
|
||||
} SerialData;
|
||||
|
||||
#define DATA ((SerialData *)this->data)
|
||||
#define BUF (DATA->buf)
|
||||
59
cores/lightning-ln882h/arduino/libraries/WiFi/WiFi.cpp
Normal file
59
cores/lightning-ln882h/arduino/libraries/WiFi/WiFi.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-02-18. */
|
||||
|
||||
#include "WiFiPrivate.h"
|
||||
|
||||
WiFiClass::WiFiClass() {
|
||||
data = (WiFiData *)calloc(1, sizeof(WiFiData));
|
||||
|
||||
DATA->scanSem = xSemaphoreCreateBinary();
|
||||
}
|
||||
|
||||
WiFiClass::~WiFiClass() {
|
||||
vSemaphoreDelete(DATA->scanSem);
|
||||
free(data);
|
||||
data = NULL;
|
||||
}
|
||||
|
||||
WiFiAuthMode securityTypeToAuthMode(uint8_t type) {
|
||||
switch (type) {
|
||||
case LN_WIFI_AUTH_OPEN:
|
||||
return WIFI_AUTH_OPEN;
|
||||
case LN_WIFI_AUTH_WEP:
|
||||
return WIFI_AUTH_WEP;
|
||||
case LN_WIFI_AUTH_WPA_PSK:
|
||||
return WIFI_AUTH_WPA_PSK;
|
||||
case LN_WIFI_AUTH_WPA2_PSK:
|
||||
return WIFI_AUTH_WPA2_PSK;
|
||||
case LN_WIFI_AUTH_WPA_WPA2_PSK:
|
||||
return WIFI_AUTH_WPA_WPA2_PSK;
|
||||
case LN_WIFI_AUTH_WPA2_ENTERPRISE:
|
||||
return WIFI_AUTH_WPA2_ENTERPRISE;
|
||||
case LN_WIFI_AUTH_WPA3_SAE:
|
||||
return WIFI_AUTH_WPA3_PSK;
|
||||
case LN_WIFI_AUTH_WPA2_PSK_WPA3_SAE:
|
||||
return WIFI_AUTH_WPA2_WPA3_PSK;
|
||||
}
|
||||
return WIFI_AUTH_INVALID;
|
||||
}
|
||||
|
||||
uint8_t authModeToSecurityType(WiFiAuthMode auth) {
|
||||
switch (auth) {
|
||||
case WIFI_AUTH_OPEN:
|
||||
return LN_WIFI_AUTH_OPEN;
|
||||
case WIFI_AUTH_WEP:
|
||||
return LN_WIFI_AUTH_WEP;
|
||||
case WIFI_AUTH_WPA_PSK:
|
||||
return LN_WIFI_AUTH_WPA_PSK;
|
||||
case WIFI_AUTH_WPA2_PSK:
|
||||
return LN_WIFI_AUTH_WPA2_PSK;
|
||||
case WIFI_AUTH_WPA_WPA2_PSK:
|
||||
return LN_WIFI_AUTH_WPA_WPA2_PSK;
|
||||
case WIFI_AUTH_WPA2_ENTERPRISE:
|
||||
return LN_WIFI_AUTH_WPA2_ENTERPRISE;
|
||||
case WIFI_AUTH_WPA3_PSK:
|
||||
return LN_WIFI_AUTH_WPA3_SAE;
|
||||
case WIFI_AUTH_WPA2_WPA3_PSK:
|
||||
return LN_WIFI_AUTH_WPA2_PSK_WPA3_SAE;
|
||||
}
|
||||
return WIFI_AUTH_INVALID;
|
||||
}
|
||||
134
cores/lightning-ln882h/arduino/libraries/WiFi/WiFiAP.cpp
Normal file
134
cores/lightning-ln882h/arduino/libraries/WiFi/WiFiAP.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-02-18. */
|
||||
|
||||
#include "WiFiPrivate.h"
|
||||
|
||||
static uint8_t psk_value[40] = {0x0};
|
||||
static uint8_t mac_addr[6] = {0x00, 0x50, 0xC2, 0x7F, 0xBC, 0x01};
|
||||
|
||||
bool WiFiClass::softAP(const char *ssid, const char *passphrase, int channel, bool ssidHidden, int maxClients) {
|
||||
if (!enableAP(true))
|
||||
return false;
|
||||
if (!validate(ssid, passphrase))
|
||||
return false;
|
||||
|
||||
LT_HEAP_I();
|
||||
|
||||
WiFiNetworkInfo &info = DATA->ap;
|
||||
if (info.ssid != ssid)
|
||||
// free network info, if not called from restoreAPConfig()
|
||||
resetNetworkInfo(info);
|
||||
|
||||
if (info.ssid != ssid)
|
||||
info.ssid = strdup(ssid);
|
||||
if (info.password != passphrase)
|
||||
info.password = strdup(passphrase);
|
||||
info.ssidHidden = ssidHidden;
|
||||
info.bssid = softAPmacAddress(mac_addr);
|
||||
info.channel = channel;
|
||||
info.auth = passphrase ? LN_WIFI_AUTH_WPA2_PSK : LN_WIFI_AUTH_OPEN;
|
||||
|
||||
LT_IM(WIFI, "Creating SoftAP %s", ssid);
|
||||
|
||||
// clang-format off
|
||||
wifi_softap_cfg_t ap_cfg = {
|
||||
.ssid = info.ssid,
|
||||
.pwd = info.password,
|
||||
.bssid = info.bssid,
|
||||
.ext_cfg = {
|
||||
.channel = (uint8_t)info.channel,
|
||||
.authmode = (ln_wifi_auth_mode_t)info.auth,
|
||||
.ssid_hidden = info.ssidHidden,
|
||||
.beacon_interval = 100,
|
||||
.psk_value = NULL,
|
||||
}
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
if ((strlen(ap_cfg.pwd) != 0) && (ap_cfg.ext_cfg.authmode != LN_WIFI_AUTH_OPEN) &&
|
||||
(ap_cfg.ext_cfg.authmode != LN_WIFI_AUTH_WEP)) {
|
||||
memset(psk_value, 0, sizeof(psk_value));
|
||||
if (0 == ln_psk_calc(ap_cfg.ssid, ap_cfg.pwd, psk_value, sizeof(psk_value))) {
|
||||
ap_cfg.ext_cfg.psk_value = psk_value;
|
||||
hexdump(psk_value, sizeof(psk_value));
|
||||
}
|
||||
}
|
||||
|
||||
// wifi start
|
||||
__wrap_ln_printf_disable();
|
||||
int ret = wifi_softap_start(&ap_cfg);
|
||||
__wrap_ln_printf_enable();
|
||||
if (ret != 0) {
|
||||
LT_EM(WIFI, "SoftAP failed; ret=%d", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiFiClass::softAPConfig(IPAddress localIP, IPAddress gateway, IPAddress subnet) {
|
||||
if (!enableAP(true))
|
||||
return false;
|
||||
WiFiNetworkInfo &info = DATA->ap;
|
||||
|
||||
// dhcp server config
|
||||
server_config_t server_config;
|
||||
server_config.server.addr = localIP;
|
||||
server_config.port = 67;
|
||||
server_config.lease = 2880;
|
||||
server_config.renew = 2880;
|
||||
server_config.ip_start.addr = IPAddress(localIP[0], localIP[1], localIP[2], 100);
|
||||
server_config.ip_end.addr = IPAddress(localIP[0], localIP[1], localIP[2], 150);
|
||||
server_config.client_max = 3;
|
||||
dhcpd_curr_config_set(&server_config);
|
||||
|
||||
// net device config
|
||||
tcpip_ip_info_t ip_info;
|
||||
ip_info.ip.addr = info.localIP = localIP;
|
||||
ip_info.netmask.addr = info.subnet = subnet;
|
||||
ip_info.gw.addr = info.gateway = gateway;
|
||||
netdev_set_ip_info(NETIF_IDX_AP, &ip_info);
|
||||
netdev_set_active(NETIF_IDX_AP);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiFiClass::softAPdisconnect(bool wifiOff) {
|
||||
return enableAP(false);
|
||||
}
|
||||
|
||||
uint8_t WiFiClass::softAPgetStationNum() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::softAPIP() {
|
||||
return netif_ip_addr4(netdev_get_netif(NETIF_IDX_AP))->addr;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::softAPSubnetMask() {
|
||||
return netif_ip_netmask4(netdev_get_netif(NETIF_IDX_AP))->addr;
|
||||
}
|
||||
|
||||
const char *WiFiClass::softAPgetHostname() {
|
||||
return netif_get_hostname(netdev_get_netif(NETIF_IDX_AP));
|
||||
}
|
||||
|
||||
bool WiFiClass::softAPsetHostname(const char *hostname) {
|
||||
netif_set_hostname(netdev_get_netif(NETIF_IDX_AP), (char *)hostname);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *WiFiClass::softAPmacAddress(uint8_t *mac) {
|
||||
memcpy(mac, netdev_get_netif(NETIF_IDX_AP)->hwaddr, MAC_ADDRESS_LEN);
|
||||
return mac;
|
||||
}
|
||||
|
||||
String WiFiClass::softAPmacAddress(void) {
|
||||
uint8_t mac[MAC_ADDRESS_LEN];
|
||||
softAPmacAddress(mac);
|
||||
return macToString(mac);
|
||||
}
|
||||
|
||||
const String WiFiClass::softAPSSID(void) {
|
||||
WiFiNetworkInfo &info = DATA->ap;
|
||||
return (char *)info.ssid;
|
||||
}
|
||||
122
cores/lightning-ln882h/arduino/libraries/WiFi/WiFiEvents.cpp
Normal file
122
cores/lightning-ln882h/arduino/libraries/WiFi/WiFiEvents.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-03-10. */
|
||||
|
||||
#include "WiFiPrivate.h"
|
||||
|
||||
void wifiEventSendArduino(EventId event) {
|
||||
EventInfo eventInfo;
|
||||
memset(&eventInfo, 0, sizeof(EventInfo));
|
||||
if (!pWiFi)
|
||||
return; // failsafe
|
||||
pWiFi->postEvent(event, eventInfo);
|
||||
}
|
||||
|
||||
static void wifiEventStaStartup(void *arg) {
|
||||
wifiEventSendArduino(ARDUINO_EVENT_WIFI_STA_START);
|
||||
}
|
||||
|
||||
static void wifiEventStaConnected(void *arg) {
|
||||
EventInfo eventInfo;
|
||||
memset(&eventInfo, 0, sizeof(EventInfo));
|
||||
if (!pWiFi)
|
||||
return; // failsafe
|
||||
|
||||
String ssid = pWiFi->SSID();
|
||||
eventInfo.wifi_sta_connected.ssid_len = ssid.length();
|
||||
eventInfo.wifi_sta_connected.channel = pWiFi->channel();
|
||||
eventInfo.wifi_sta_connected.authmode = pWiFi->getEncryption();
|
||||
memcpy(eventInfo.wifi_sta_connected.ssid, ssid.c_str(), eventInfo.wifi_sta_connected.ssid_len + 1);
|
||||
memcpy(eventInfo.wifi_sta_connected.bssid, pWiFi->BSSID(), 6);
|
||||
|
||||
pWiFi->postEvent(ARDUINO_EVENT_WIFI_STA_CONNECTED, eventInfo);
|
||||
}
|
||||
|
||||
static void wifiEventStaDisconnected(void *arg) {
|
||||
EventInfo eventInfo;
|
||||
memset(&eventInfo, 0, sizeof(EventInfo));
|
||||
if (!pWiFi)
|
||||
return; // failsafe
|
||||
|
||||
eventInfo.wifi_sta_disconnected.ssid_len = 0;
|
||||
eventInfo.wifi_sta_disconnected.reason = WIFI_REASON_ASSOC_LEAVE;
|
||||
pWiFi->postEvent(ARDUINO_EVENT_WIFI_STA_DISCONNECTED, eventInfo);
|
||||
}
|
||||
|
||||
static void wifiEventStaConnectFailed(void *arg) {
|
||||
wifi_sta_connect_failed_reason_t reason = *(wifi_sta_connect_failed_reason_t *)arg;
|
||||
EventInfo eventInfo;
|
||||
memset(&eventInfo, 0, sizeof(EventInfo));
|
||||
if (!pWiFi)
|
||||
return; // failsafe
|
||||
|
||||
eventInfo.wifi_sta_disconnected.ssid_len = 0;
|
||||
eventInfo.wifi_sta_disconnected.reason = WIFI_REASON_UNSPECIFIED;
|
||||
switch (reason) {
|
||||
case WIFI_STA_CONN_WRONG_PWD:
|
||||
eventInfo.wifi_sta_disconnected.reason = WIFI_REASON_AUTH_FAIL;
|
||||
break;
|
||||
case WIFI_STA_CONN_TARGET_AP_NOT_FOUND:
|
||||
eventInfo.wifi_sta_disconnected.reason = WIFI_REASON_NO_AP_FOUND;
|
||||
break;
|
||||
case WIFI_STA_CONN_TIMEOUT:
|
||||
eventInfo.wifi_sta_disconnected.reason = WIFI_REASON_BEACON_TIMEOUT;
|
||||
break;
|
||||
case WIFI_STA_CONN_REFUSED:
|
||||
eventInfo.wifi_sta_disconnected.reason = WIFI_REASON_CONNECTION_FAIL;
|
||||
break;
|
||||
}
|
||||
pWiFi->postEvent(ARDUINO_EVENT_WIFI_STA_DISCONNECTED, eventInfo);
|
||||
}
|
||||
|
||||
static void wifiEventSoftAPStartup(void *arg) {
|
||||
netdev_set_state(NETIF_IDX_AP, NETDEV_UP);
|
||||
wifiEventSendArduino(ARDUINO_EVENT_WIFI_AP_START);
|
||||
}
|
||||
|
||||
static void wifiEventSoftAPAssociated(void *arg) {
|
||||
const uint8_t *mac_addr = (const uint8_t *)arg;
|
||||
EventInfo eventInfo;
|
||||
memset(&eventInfo, 0, sizeof(EventInfo));
|
||||
if (!pWiFi)
|
||||
return; // failsafe
|
||||
|
||||
memcpy(eventInfo.wifi_ap_staconnected.mac, mac_addr, 6);
|
||||
pWiFi->postEvent(ARDUINO_EVENT_WIFI_AP_STACONNECTED, eventInfo);
|
||||
}
|
||||
|
||||
static void wifiEventSoftAPDisassociated(void *arg) {
|
||||
const uint8_t *mac_addr = (const uint8_t *)arg;
|
||||
EventInfo eventInfo;
|
||||
memset(&eventInfo, 0, sizeof(EventInfo));
|
||||
if (!pWiFi)
|
||||
return; // failsafe
|
||||
|
||||
memcpy(eventInfo.wifi_ap_staconnected.mac, mac_addr, 6);
|
||||
pWiFi->postEvent(ARDUINO_EVENT_WIFI_AP_STADISCONNECTED, eventInfo);
|
||||
}
|
||||
|
||||
static void wifiEventIpReceived(struct netif *nif) {
|
||||
EventInfo eventInfo;
|
||||
memset(&eventInfo, 0, sizeof(EventInfo));
|
||||
if (!pWiFi || !nif)
|
||||
return; // failsafe
|
||||
|
||||
eventInfo.got_ip.if_index = 0;
|
||||
eventInfo.got_ip.ip_changed = true;
|
||||
eventInfo.got_ip.ip_info.ip.addr = nif->ip_addr.addr;
|
||||
eventInfo.got_ip.ip_info.gw.addr = nif->gw.addr;
|
||||
eventInfo.got_ip.ip_info.netmask.addr = nif->netmask.addr;
|
||||
pWiFi->postEvent(ARDUINO_EVENT_WIFI_STA_GOT_IP, eventInfo);
|
||||
}
|
||||
|
||||
void registerWifiHandlers() {
|
||||
wifi_manager_reg_event_callback(WIFI_MGR_EVENT_STA_STARTUP, &wifiEventStaStartup);
|
||||
wifi_manager_reg_event_callback(WIFI_MGR_EVENT_STA_CONNECTED, &wifiEventStaConnected);
|
||||
wifi_manager_reg_event_callback(WIFI_MGR_EVENT_STA_DISCONNECTED, &wifiEventStaDisconnected);
|
||||
wifi_manager_reg_event_callback(WIFI_MGR_EVENT_STA_CONNECT_FAILED, &wifiEventStaConnectFailed);
|
||||
|
||||
netdev_get_ip_cb_set(&wifiEventIpReceived);
|
||||
|
||||
wifi_manager_reg_event_callback(WIFI_MGR_EVENT_SOFTAP_STARTUP, &wifiEventSoftAPStartup);
|
||||
wifi_manager_reg_event_callback(WIFI_MGR_EVENT_SOFTAP_ASSOCIATED, &wifiEventSoftAPAssociated);
|
||||
wifi_manager_reg_event_callback(WIFI_MGR_EVENT_SOFTAP_DISASSOCIATED, &wifiEventSoftAPDisassociated);
|
||||
}
|
||||
144
cores/lightning-ln882h/arduino/libraries/WiFi/WiFiGeneric.cpp
Normal file
144
cores/lightning-ln882h/arduino/libraries/WiFi/WiFiGeneric.cpp
Normal file
@@ -0,0 +1,144 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-03-10. */
|
||||
|
||||
#include "WiFiPrivate.h"
|
||||
|
||||
static uint8_t psk_value[40] = {0x0};
|
||||
|
||||
bool WiFiClass::modePriv(WiFiMode mode, WiFiModeAction sta, WiFiModeAction ap) {
|
||||
__wrap_ln_printf_disable();
|
||||
|
||||
if (!DATA->initialized) {
|
||||
// rf preprocess,img cal
|
||||
wifi_rf_calibration();
|
||||
// Init wifi stack.
|
||||
wifi_init();
|
||||
// Init lwip stack.
|
||||
LT_IM(WIFI, "Initializing LwIP");
|
||||
lwip_tcpip_init();
|
||||
// Init wifi manager
|
||||
wifi_manager_init();
|
||||
// Register event handlers
|
||||
registerWifiHandlers();
|
||||
DATA->mode = WIFI_MODE_NULL;
|
||||
DATA->initialized = true;
|
||||
}
|
||||
|
||||
LT_HEAP_I();
|
||||
WiFiMode currentMode = DATA->mode;
|
||||
WiFiNetworkInfo &staInfo = DATA->sta;
|
||||
WiFiNetworkInfo &apInfo = DATA->ap;
|
||||
|
||||
if (mode == WIFI_MODE_APSTA) {
|
||||
LT_EM(WIFI, "AP+STA mode not supported!");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (sta == WLMODE_ENABLE) {
|
||||
LT_DM(WIFI, "Enabling STA");
|
||||
|
||||
// 1. sta mac get
|
||||
uint8_t mac_addr[6];
|
||||
setMacAddress(macAddress(mac_addr));
|
||||
|
||||
// 2. net device(lwip)
|
||||
netdev_set_active(NETIF_IDX_STA);
|
||||
|
||||
// 3. wifi start
|
||||
wifi_sta_start(mac_addr, WIFI_NO_POWERSAVE);
|
||||
|
||||
wifiEventSendArduino(ARDUINO_EVENT_WIFI_STA_START);
|
||||
} else if (sta == WLMODE_DISABLE) {
|
||||
LT_DM(WIFI, "Disabling STA");
|
||||
wifi_sta_disconnect();
|
||||
netdev_set_state(NETIF_IDX_STA, NETDEV_DOWN);
|
||||
wifiEventSendArduino(ARDUINO_EVENT_WIFI_STA_STOP);
|
||||
}
|
||||
|
||||
LT_HEAP_I();
|
||||
|
||||
if (ap == WLMODE_ENABLE) {
|
||||
LT_DM(WIFI, "Enabling AP");
|
||||
|
||||
// 1. ap mac get
|
||||
uint8_t mac_addr[6];
|
||||
sysparam_softap_mac_get(mac_addr);
|
||||
netdev_set_mac_addr(NETIF_IDX_AP, mac_addr);
|
||||
|
||||
// 2. net device(lwip)
|
||||
netdev_set_active(NETIF_IDX_AP);
|
||||
|
||||
wifiEventSendArduino(ARDUINO_EVENT_WIFI_AP_START);
|
||||
} else if (ap == WLMODE_DISABLE) {
|
||||
LT_DM(WIFI, "Disabling AP");
|
||||
wifi_softap_deauth_all();
|
||||
netdev_set_state(NETIF_IDX_AP, NETDEV_DOWN);
|
||||
|
||||
wifiEventSendArduino(ARDUINO_EVENT_WIFI_AP_STOP);
|
||||
}
|
||||
|
||||
DATA->mode = mode;
|
||||
|
||||
LT_HEAP_I();
|
||||
|
||||
__wrap_ln_printf_enable();
|
||||
return true;
|
||||
|
||||
error:
|
||||
__wrap_ln_printf_enable();
|
||||
return false;
|
||||
}
|
||||
|
||||
WiFiMode WiFiClass::getMode() {
|
||||
if (!DATA->initialized)
|
||||
return WIFI_MODE_NULL;
|
||||
|
||||
// return fake value because the chip doesn't report the mode before it is started
|
||||
return DATA->mode;
|
||||
|
||||
/*switch (wifi_current_mode_get()) {
|
||||
case LN_WIFI_MODE_STATION:
|
||||
return WIFI_MODE_STA;
|
||||
case LN_WIFI_MODE_AP:
|
||||
return WIFI_MODE_AP;
|
||||
case LN_WIFI_MODE_AP_STATION:
|
||||
return WIFI_MODE_APSTA;
|
||||
}
|
||||
return WIFI_MODE_NULL;*/
|
||||
}
|
||||
|
||||
WiFiStatus WiFiClass::status() {
|
||||
wifi_sta_status_t status = WIFI_STA_STATUS_STARTUP;
|
||||
wifi_get_sta_status(&status);
|
||||
if (status == WIFI_STA_STATUS_CONNECTED) {
|
||||
return WL_CONNECTED;
|
||||
} else {
|
||||
return WL_DISCONNECTED;
|
||||
}
|
||||
}
|
||||
|
||||
bool WiFiClass::setSleep(bool enable) {
|
||||
LT_DM(WIFI, "WiFi sleep mode %u", enable);
|
||||
|
||||
if (enable) {
|
||||
if (wifi_sta_set_powersave(WIFI_MAX_POWERSAVE))
|
||||
return false;
|
||||
} else {
|
||||
if (wifi_sta_set_powersave(WIFI_NO_POWERSAVE))
|
||||
return false;
|
||||
}
|
||||
DATA->sleep = enable;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiFiClass::getSleep() {
|
||||
return DATA->sleep;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::hostByName(const char *hostname) {
|
||||
ip_addr_t ip;
|
||||
int ret = netconn_gethostbyname(hostname, &ip);
|
||||
if (ret == ERR_OK) {
|
||||
return ip.addr;
|
||||
}
|
||||
return IPAddress();
|
||||
}
|
||||
47
cores/lightning-ln882h/arduino/libraries/WiFi/WiFiPrivate.h
Normal file
47
cores/lightning-ln882h/arduino/libraries/WiFi/WiFiPrivate.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-02-10. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <WiFi.h>
|
||||
#include <sdk_private.h>
|
||||
|
||||
extern "C" {
|
||||
// copy defines from PIO builder (for IDE to understand)
|
||||
#define LWIP_TIMEVAL_PRIVATE 0
|
||||
#define LWIP_NETIF_HOSTNAME 1
|
||||
#define LWIP_SO_RCVBUF 1
|
||||
|
||||
#include <lwip/api.h>
|
||||
#include <lwip/dns.h>
|
||||
#include <lwip/ip_addr.h>
|
||||
#include <lwip/netif.h>
|
||||
#include <lwip/netifapi.h>
|
||||
#include <netif/ethernetif.h>
|
||||
|
||||
#include <FreeRTOS.h>
|
||||
#include <semphr.h>
|
||||
|
||||
} // extern "C"
|
||||
|
||||
// WiFi.cpp
|
||||
extern WiFiAuthMode securityTypeToAuthMode(uint8_t type);
|
||||
uint8_t authModeToSecurityType(WiFiAuthMode auth);
|
||||
|
||||
// WiFiEvents.cpp
|
||||
extern void wifiEventSendArduino(EventId event);
|
||||
extern void registerWifiHandlers();
|
||||
|
||||
typedef struct {
|
||||
bool initialized;
|
||||
bool sleep;
|
||||
WiFiMode mode;
|
||||
SemaphoreHandle_t scanSem;
|
||||
WiFiNetworkInfo sta;
|
||||
WiFiNetworkInfo ap;
|
||||
} WiFiData;
|
||||
|
||||
#define DATA ((WiFiData *)data)
|
||||
#define pDATA ((WiFiData *)pWiFi->data)
|
||||
#define cDATA ((WiFiData *)cls->data)
|
||||
|
||||
#define IP_FMT "%u.%u.%u.%u"
|
||||
213
cores/lightning-ln882h/arduino/libraries/WiFi/WiFiSTA.cpp
Normal file
213
cores/lightning-ln882h/arduino/libraries/WiFi/WiFiSTA.cpp
Normal file
@@ -0,0 +1,213 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-03-10. */
|
||||
|
||||
#include "WiFiPrivate.h"
|
||||
|
||||
WiFiStatus WiFiClass::begin(
|
||||
const char *ssid,
|
||||
const char *passphrase,
|
||||
int32_t channel,
|
||||
const uint8_t *bssid,
|
||||
bool connect
|
||||
) {
|
||||
if (!enableSTA(true))
|
||||
return WL_CONNECT_FAILED;
|
||||
if (!validate(ssid, passphrase))
|
||||
return WL_CONNECT_FAILED;
|
||||
|
||||
LT_HEAP_I();
|
||||
|
||||
WiFiNetworkInfo &info = DATA->sta;
|
||||
if (info.ssid != ssid)
|
||||
// free network info, if not called from restoreSTAConfig()
|
||||
resetNetworkInfo(info);
|
||||
|
||||
if (info.ssid != ssid)
|
||||
info.ssid = strdup(ssid);
|
||||
info.channel = channel;
|
||||
info.auth = LN_WIFI_AUTH_OPEN;
|
||||
|
||||
if (passphrase) {
|
||||
if (info.password != passphrase)
|
||||
info.password = strdup(passphrase);
|
||||
info.auth = LN_WIFI_AUTH_WPA_WPA2_PSK;
|
||||
}
|
||||
|
||||
if (reconnect(bssid))
|
||||
return WL_CONNECTED;
|
||||
else
|
||||
return WL_CONNECT_FAILED;
|
||||
}
|
||||
|
||||
bool WiFiClass::config(IPAddress localIP, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) {
|
||||
if (!enableSTA(true))
|
||||
return false;
|
||||
WiFiNetworkInfo &info = DATA->sta;
|
||||
struct netif *ifs = netdev_get_netif(NETIF_IDX_STA);
|
||||
|
||||
ip4_addr_t d1, d2;
|
||||
d1.addr = info.dns1 = dns1;
|
||||
d2.addr = info.dns2 = dns2;
|
||||
if (d1.addr)
|
||||
dns_setserver(0, &d1);
|
||||
if (d2.addr)
|
||||
dns_setserver(0, &d2);
|
||||
|
||||
if (!localIP[0]) {
|
||||
info.localIP = 0;
|
||||
netifapi_dhcp_start(ifs);
|
||||
return true;
|
||||
}
|
||||
ip4_addr_t ipaddr, netmask, gw;
|
||||
ipaddr.addr = info.localIP = localIP;
|
||||
netmask.addr = info.subnet = subnet;
|
||||
gw.addr = info.gateway = gateway;
|
||||
netif_set_addr(ifs, &ipaddr, &netmask, &gw);
|
||||
netifapi_dhcp_release_and_stop(ifs);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiFiClass::reconnect(const uint8_t *bssid) {
|
||||
WiFiNetworkInfo &info = DATA->sta;
|
||||
LT_IM(WIFI, "Connecting to %s (bssid=%p)", info.ssid, bssid);
|
||||
|
||||
if (bssid != info.bssid) {
|
||||
if (!info.bssid)
|
||||
info.bssid = (uint8_t *)malloc(BSSID_LEN);
|
||||
memcpy(info.bssid, bssid, BSSID_LEN);
|
||||
}
|
||||
|
||||
uint8_t psk_value[40] = {0x0};
|
||||
wifi_sta_connect_t connect = {
|
||||
.ssid = info.ssid,
|
||||
.pwd = info.password,
|
||||
.bssid = NULL, // info.bssid,
|
||||
.psk_value = NULL,
|
||||
};
|
||||
wifi_scan_cfg_t scan_cfg = {
|
||||
.channel = (uint8_t)info.channel,
|
||||
.scan_type = WIFI_SCAN_TYPE_ACTIVE,
|
||||
.scan_time = 5,
|
||||
};
|
||||
|
||||
if (0 == ln_psk_calc(connect.ssid, connect.pwd, psk_value, sizeof(psk_value)))
|
||||
connect.psk_value = psk_value;
|
||||
|
||||
LT_DM(WIFI, "Starting WiFi...");
|
||||
|
||||
__wrap_ln_printf_disable();
|
||||
int ret = wifi_sta_connect(&connect, &scan_cfg);
|
||||
// int ret = wifi_sta_connect_v2(&connect, &scan_cfg, 10);
|
||||
__wrap_ln_printf_enable();
|
||||
|
||||
LT_DM(WIFI, "Start OK (%d)", ret);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool WiFiClass::disconnect(bool wifiOff) {
|
||||
free(DATA->sta.ssid);
|
||||
DATA->sta.ssid = NULL;
|
||||
int ret = wifi_sta_disconnect();
|
||||
if (wifiOff)
|
||||
enableSTA(false);
|
||||
return ret == 0;
|
||||
}
|
||||
|
||||
bool WiFiClass::setAutoReconnect(bool autoReconnect) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool WiFiClass::getAutoReconnect() {
|
||||
return false;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::localIP() {
|
||||
tcpip_ip_info_t ip_info;
|
||||
netdev_get_ip_info(NETIF_IDX_STA, &ip_info);
|
||||
return IPAddress(ip_info.ip.addr);
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::subnetMask() {
|
||||
tcpip_ip_info_t ip_info;
|
||||
netdev_get_ip_info(NETIF_IDX_STA, &ip_info);
|
||||
return IPAddress(ip_info.netmask.addr);
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::gatewayIP() {
|
||||
tcpip_ip_info_t ip_info;
|
||||
netdev_get_ip_info(NETIF_IDX_STA, &ip_info);
|
||||
return IPAddress(ip_info.gw.addr);
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::dnsIP(uint8_t dns_no) {
|
||||
return dns_getserver(dns_no)->addr;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::broadcastIP() {
|
||||
return calculateBroadcast(localIP(), subnetMask());
|
||||
}
|
||||
|
||||
const char *WiFiClass::getHostname() {
|
||||
struct netif *ifs = netdev_get_netif(NETIF_IDX_STA);
|
||||
return netif_get_hostname(ifs);
|
||||
}
|
||||
|
||||
bool WiFiClass::setHostname(const char *hostname) {
|
||||
struct netif *ifs = netdev_get_netif(NETIF_IDX_STA);
|
||||
netif_set_hostname(ifs, (char *)hostname);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *WiFiClass::macAddress(uint8_t *mac) {
|
||||
if (SYSPARAM_ERR_NONE == sysparam_sta_mac_get(mac))
|
||||
return mac;
|
||||
else
|
||||
LT_WM(WIFI, "sysparam sta mac get failed!");
|
||||
|
||||
lt_get_device_mac(mac);
|
||||
return mac;
|
||||
}
|
||||
|
||||
bool WiFiClass::setMacAddress(const uint8_t *mac) {
|
||||
sysparam_sta_mac_update(mac);
|
||||
return netdev_set_mac_addr(NETIF_IDX_STA, (uint8_t *)mac) == 0;
|
||||
}
|
||||
|
||||
const String WiFiClass::SSID() {
|
||||
const char *ssid = NULL;
|
||||
const uint8_t *bssid = NULL;
|
||||
wifi_get_sta_conn_info(&ssid, &bssid);
|
||||
return ssid;
|
||||
}
|
||||
|
||||
const String WiFiClass::psk() {
|
||||
if (!isConnected() || !DATA->sta.password)
|
||||
return "";
|
||||
return DATA->sta.password;
|
||||
}
|
||||
|
||||
uint8_t *WiFiClass::BSSID() {
|
||||
const char *ssid = NULL;
|
||||
const uint8_t *bssid = NULL;
|
||||
wifi_get_sta_conn_info(&ssid, &bssid);
|
||||
return (uint8_t *)bssid;
|
||||
}
|
||||
|
||||
int32_t WiFiClass::channel() {
|
||||
uint8_t channel = 0;
|
||||
wifi_get_channel(&channel);
|
||||
return (int32_t)channel;
|
||||
}
|
||||
|
||||
int8_t WiFiClass::RSSI() {
|
||||
int8_t rssi = 0;
|
||||
wifi_sta_get_rssi(&rssi);
|
||||
return rssi;
|
||||
}
|
||||
|
||||
WiFiAuthMode WiFiClass::getEncryption() {
|
||||
ln_wifi_auth_mode_t mode;
|
||||
wifi_sta_get_connected_ap_security(&mode);
|
||||
return securityTypeToAuthMode(mode);
|
||||
}
|
||||
114
cores/lightning-ln882h/arduino/libraries/WiFi/WiFiScan.cpp
Normal file
114
cores/lightning-ln882h/arduino/libraries/WiFi/WiFiScan.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-03-13. */
|
||||
|
||||
#include "WiFiPrivate.h"
|
||||
|
||||
static void scanHandler(void *arg) {
|
||||
WiFiClass *cls = (WiFiClass *)pWiFi;
|
||||
WiFiScanData *scan = cls->scan;
|
||||
|
||||
LT_HEAP_I();
|
||||
|
||||
ln_list_t *list;
|
||||
uint8_t n = 0, node_count = 0;
|
||||
ap_info_node_t *pnode;
|
||||
|
||||
wifi_manager_ap_list_update_enable(LN_FALSE);
|
||||
wifi_manager_reg_event_callback(WIFI_MGR_EVENT_STA_SCAN_COMPLETE, NULL);
|
||||
|
||||
// 1.get ap info list.
|
||||
if (wifi_manager_get_ap_list(&list, &node_count)) {
|
||||
LT_EM(WIFI, "Failed to get scan result");
|
||||
goto end;
|
||||
}
|
||||
LT_IM(WIFI, "Found %d APs", node_count);
|
||||
|
||||
cls->scanAlloc(node_count);
|
||||
if (!scan->ap) {
|
||||
LT_WM(WIFI, "scan->ap alloc failed");
|
||||
goto end;
|
||||
}
|
||||
|
||||
// 2.get all ap info in the list.
|
||||
LN_LIST_FOR_EACH_ENTRY(pnode, ap_info_node_t, list, list) {
|
||||
uint8_t *mac = (uint8_t *)pnode->info.bssid;
|
||||
ap_info_t *ap_info = &pnode->info;
|
||||
|
||||
scan->ap[n].ssid = strdup(ap_info->ssid);
|
||||
scan->ap[n].auth = securityTypeToAuthMode(ap_info->authmode);
|
||||
scan->ap[n].rssi = ap_info->rssi;
|
||||
scan->ap[n].channel = ap_info->channel;
|
||||
memcpy(scan->ap[n].bssid.addr, mac, 6);
|
||||
n++;
|
||||
}
|
||||
|
||||
end:
|
||||
scan->timeout = 0;
|
||||
if (scan->running) {
|
||||
// running == false means it was discarded (timeout)
|
||||
scan->running = false;
|
||||
xSemaphoreGive(cDATA->scanSem);
|
||||
|
||||
// Send event scan finished
|
||||
EventInfo eventInfo;
|
||||
memset(&eventInfo, 0, sizeof(EventInfo));
|
||||
eventInfo.wifi_scan_done.status = 0;
|
||||
eventInfo.wifi_scan_done.number = scan->count;
|
||||
pWiFi->postEvent(ARDUINO_EVENT_WIFI_SCAN_DONE, eventInfo);
|
||||
}
|
||||
// wifi_manager_ap_list_update_enable(LN_TRUE);
|
||||
// wifi_sta_disconnect();
|
||||
LT_HEAP_I();
|
||||
return;
|
||||
}
|
||||
|
||||
int16_t WiFiClass::scanNetworks(bool async, bool showHidden, bool passive, uint32_t maxMsPerChannel, uint8_t channel) {
|
||||
if (scan && scan->running) {
|
||||
if (scan->timeout && millis() > scan->timeout) {
|
||||
LT_WM(WIFI, "Scan timeout, discarding");
|
||||
scan->running = false;
|
||||
} else {
|
||||
return WIFI_SCAN_RUNNING;
|
||||
}
|
||||
}
|
||||
enableSTA(true);
|
||||
scanDelete();
|
||||
scanInit();
|
||||
|
||||
LT_IM(WIFI, "Starting WiFi scan");
|
||||
|
||||
__wrap_ln_printf_disable();
|
||||
|
||||
wifi_manager_reg_event_callback(WIFI_MGR_EVENT_STA_SCAN_COMPLETE, &scanHandler);
|
||||
wifi_manager_ap_list_update_enable(LN_TRUE);
|
||||
wifi_manager_cleanup_scan_results();
|
||||
|
||||
wifi_scan_cfg_t scan_cfg = {
|
||||
.channel = 0,
|
||||
.scan_type = WIFI_SCAN_TYPE_ACTIVE,
|
||||
.scan_time = 5,
|
||||
};
|
||||
wifi_sta_scan(&scan_cfg);
|
||||
|
||||
LT_HEAP_I();
|
||||
|
||||
scan->running = true;
|
||||
scan->timeout = millis() + maxMsPerChannel * 20 + 1000;
|
||||
|
||||
int16_t ret = WIFI_SCAN_RUNNING;
|
||||
if (!async) {
|
||||
LT_IM(WIFI, "Waiting for results");
|
||||
xSemaphoreTake(DATA->scanSem, 1); // reset the semaphore quickly
|
||||
xSemaphoreTake(DATA->scanSem, pdMS_TO_TICKS(maxMsPerChannel * 20));
|
||||
if (scan->running) {
|
||||
scanDelete();
|
||||
ret = WIFI_SCAN_FAILED;
|
||||
goto exit;
|
||||
}
|
||||
ret = scan->count;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
exit:
|
||||
__wrap_ln_printf_enable();
|
||||
return ret;
|
||||
}
|
||||
295
cores/lightning-ln882h/arduino/libraries/Wire/Wire.cpp
Normal file
295
cores/lightning-ln882h/arduino/libraries/Wire/Wire.cpp
Normal file
@@ -0,0 +1,295 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2025-01-19. */
|
||||
|
||||
#include "wiring_private.h"
|
||||
#include <sdk_private.h>
|
||||
|
||||
#define I2C_PRIV i2c_init_t_def
|
||||
#include "Wire.h"
|
||||
|
||||
// Functions from I2C demo of SDK
|
||||
static uint8_t hal_i2c_master_7bit_write(uint32_t i2c_x_base, uint8_t dev_addr, const uint8_t *buf, uint16_t buf_len);
|
||||
static uint8_t hal_i2c_master_7bit_read(uint32_t i2c_x_base, uint8_t dev_addr, uint8_t *buf, uint16_t buf_len);
|
||||
|
||||
#ifdef PIN_WIRE0_SDA
|
||||
// Wire object associated to I2C0 interface.
|
||||
TwoWire Wire(PIN_WIRE0_SDA, PIN_WIRE0_SCL);
|
||||
#endif
|
||||
|
||||
TwoWire::TwoWire(int8_t sda, int8_t scl) {
|
||||
_sda = sda;
|
||||
_scl = scl;
|
||||
}
|
||||
|
||||
TwoWire::~TwoWire() {}
|
||||
|
||||
bool TwoWire::setPins(int8_t sda, int8_t scl) {
|
||||
// return true when changing pins on initialized I2C
|
||||
if (_inSetPins)
|
||||
return true;
|
||||
// check if pins are provided
|
||||
if (sda == -1 || scl == -1)
|
||||
return false;
|
||||
// set private pins
|
||||
_sda = sda;
|
||||
_scl = scl;
|
||||
uint32_t pin_sda = pinInfo(sda)->gpio;
|
||||
uint32_t pin_scl = pinInfo(scl)->gpio;
|
||||
|
||||
hal_gpio_pin_afio_select(GPIO_GET_BASE(pin_sda), GPIO_GET_PIN(pin_sda), I2C0_SDA); // TODO: check pin value
|
||||
hal_gpio_pin_afio_select(GPIO_GET_BASE(pin_scl), GPIO_GET_PIN(pin_scl), I2C0_SCL);
|
||||
hal_gpio_pin_afio_en(GPIO_GET_BASE(pin_sda), GPIO_GET_PIN(pin_sda), HAL_ENABLE);
|
||||
hal_gpio_pin_afio_en(GPIO_GET_BASE(pin_scl), GPIO_GET_PIN(pin_scl), HAL_ENABLE);
|
||||
|
||||
// restart I2C if changing pins
|
||||
// this will never be called from begin()
|
||||
if (_i2c) {
|
||||
_inSetPins = true;
|
||||
end();
|
||||
begin();
|
||||
_inSetPins = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TwoWire::begin(int8_t sda, int8_t scl, uint32_t frequency) {
|
||||
if (_i2c)
|
||||
return true;
|
||||
// set private i2c pins
|
||||
if (!setPins(sda, scl))
|
||||
return false;
|
||||
// use default frequency
|
||||
if (!frequency)
|
||||
frequency = WIRE_DEFAULT_FREQ;
|
||||
|
||||
/* disable the i2c */
|
||||
hal_i2c_en(I2C_BASE, HAL_DISABLE);
|
||||
|
||||
_freq = frequency;
|
||||
_i2c = new i2c_init_t_def;
|
||||
memset(&_i2c, 0, sizeof(_i2c));
|
||||
_i2c->i2c_peripheral_clock_freq = 4;
|
||||
_i2c->i2c_master_mode_sel = I2C_FM_MODE;
|
||||
_i2c->i2c_fm_mode_duty_cycle = I2C_FM_MODE_DUTY_CYCLE_2;
|
||||
/*
|
||||
* TPCLK1 = 1/80MHz = 0.0125us
|
||||
* Thigh = 9 x CCR x TPCLK1
|
||||
* Tlow = 16 x CCR x TPCLK1
|
||||
* Thigh + Tlow = 1/frequency
|
||||
* ccr = 3.200.000/frequency
|
||||
*/
|
||||
_i2c->i2c_ccr = 3200000 / frequency;
|
||||
_i2c->i2c_trise = 0xF;
|
||||
|
||||
hal_i2c_init(I2C_BASE, _i2c);
|
||||
|
||||
hal_i2c_en(I2C_BASE, HAL_ENABLE);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TwoWire::begin(uint8_t address, int8_t sda, int8_t scl, uint32_t frequency) {
|
||||
if (_i2c)
|
||||
return true;
|
||||
// init master bus first, return if failed
|
||||
if (!begin(sda, scl, frequency))
|
||||
return false;
|
||||
|
||||
hal_i2c_slave_set_add_mode(I2C_BASE, I2C_ADD_7BIT_MODE);
|
||||
hal_i2c_slave_set_add1(I2C_BASE, address);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TwoWire::end() {
|
||||
hal_i2c_deinit();
|
||||
delete _i2c;
|
||||
_i2c = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TwoWire::setClock(uint32_t freq) {
|
||||
if (_i2c) {
|
||||
_i2c->i2c_ccr = 3200000 / freq;
|
||||
hal_i2c_init(I2C_BASE, _i2c);
|
||||
}
|
||||
_freq = freq;
|
||||
return true;
|
||||
}
|
||||
|
||||
void TwoWire::beginTransmission(uint8_t address) {
|
||||
_txAddr = address;
|
||||
_txBuf.clear();
|
||||
}
|
||||
|
||||
// Errors:
|
||||
// 0 : Success
|
||||
// 1 : Data too long
|
||||
// 2 : NACK on transmit of address
|
||||
// 3 : NACK on transmit of data
|
||||
// 4 : Other error
|
||||
uint8_t TwoWire::endTransmission(bool stopBit) {
|
||||
if (!_i2c || !_txAddr)
|
||||
return 4;
|
||||
uint8_t *buf = (uint8_t *)malloc(_txBuf.available());
|
||||
uint8_t i = 0;
|
||||
while (_txBuf.available()) {
|
||||
buf[i++] = _txBuf.read_char();
|
||||
}
|
||||
if (hal_i2c_master_7bit_write(I2C_BASE, _txAddr, buf, i) == HAL_RESET)
|
||||
return 4;
|
||||
free(buf);
|
||||
_txAddr = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t TwoWire::requestFrom(uint8_t address, size_t len, bool stopBit) {
|
||||
if (!len)
|
||||
return 0;
|
||||
|
||||
if (len > SERIAL_BUFFER_SIZE)
|
||||
len = SERIAL_BUFFER_SIZE;
|
||||
|
||||
_rxBuf.clear();
|
||||
|
||||
uint8_t *buf = (uint8_t *)malloc(_txBuf.available());
|
||||
if (hal_i2c_master_7bit_read(I2C_BASE, address, buf, len) == HAL_RESET)
|
||||
return 0;
|
||||
|
||||
uint8_t i = 0;
|
||||
while (len) {
|
||||
_rxBuf.store_char(buf[i++]);
|
||||
len--;
|
||||
}
|
||||
free(buf);
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t TwoWire::write(uint8_t data) {
|
||||
if (!_txAddr || _txBuf.isFull())
|
||||
return 0;
|
||||
_txBuf.store_char(data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t TwoWire::write(const uint8_t *data, size_t len) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (!write(data[i]))
|
||||
return i;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
int TwoWire::available() {
|
||||
return _rxBuf.available();
|
||||
}
|
||||
|
||||
int TwoWire::read() {
|
||||
return _rxBuf.read_char();
|
||||
}
|
||||
|
||||
int TwoWire::peek() {
|
||||
return _rxBuf.peek();
|
||||
}
|
||||
|
||||
void TwoWire::flush() {}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------
|
||||
#define TIMEOUT_CYCLE 4000
|
||||
|
||||
static uint8_t hal_i2c_master_7bit_write(uint32_t i2c_x_base, uint8_t dev_addr, const uint8_t *buf, uint16_t buf_len) {
|
||||
// 1. check busy
|
||||
if (hal_i2c_wait_bus_idle(i2c_x_base, TIMEOUT_CYCLE) == HAL_RESET) {
|
||||
hal_i2c_master_reset(i2c_x_base);
|
||||
return HAL_RESET;
|
||||
}
|
||||
|
||||
// 2. send start
|
||||
if (hal_i2c_master_start(i2c_x_base, TIMEOUT_CYCLE) == HAL_RESET)
|
||||
return HAL_RESET;
|
||||
|
||||
// 3. send addr
|
||||
hal_i2c_master_send_data(i2c_x_base, dev_addr);
|
||||
|
||||
// 4. wait send complete
|
||||
if (hal_i2c_master_wait_addr(i2c_x_base, TIMEOUT_CYCLE) == HAL_RESET)
|
||||
return HAL_RESET;
|
||||
|
||||
// 5. clear addr flag
|
||||
hal_i2c_clear_sr(i2c_x_base);
|
||||
|
||||
// 6. send data
|
||||
for (uint32_t i = 0; i < buf_len; i++) {
|
||||
// wait tx empty flag
|
||||
if (hal_i2c_wait_txe(i2c_x_base, TIMEOUT_CYCLE) == HAL_RESET) {
|
||||
return HAL_RESET;
|
||||
} else {
|
||||
hal_i2c_master_send_data(i2c_x_base, buf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// 7. wait send complete.
|
||||
if (hal_i2c_wait_btf(i2c_x_base, TIMEOUT_CYCLE) == HAL_RESET)
|
||||
return HAL_RESET;
|
||||
|
||||
// 8. stop the i2c.
|
||||
hal_i2c_master_stop(i2c_x_base);
|
||||
|
||||
return HAL_SET;
|
||||
}
|
||||
|
||||
static uint8_t hal_i2c_master_7bit_read(uint32_t i2c_x_base, uint8_t dev_addr, uint8_t *buf, uint16_t buf_len) {
|
||||
// 1. check busy
|
||||
if (hal_i2c_wait_bus_idle(i2c_x_base, TIMEOUT_CYCLE) == HAL_RESET) {
|
||||
hal_i2c_master_reset(i2c_x_base);
|
||||
return HAL_RESET;
|
||||
}
|
||||
|
||||
// 2. send start
|
||||
if (hal_i2c_master_start(i2c_x_base, TIMEOUT_CYCLE) == HAL_RESET)
|
||||
return HAL_RESET;
|
||||
|
||||
// 3. send addr (+1 is read operation)
|
||||
hal_i2c_master_send_data(i2c_x_base, dev_addr + 1);
|
||||
|
||||
// 4. Wait for an ack after sending the address
|
||||
if (hal_i2c_master_wait_addr(i2c_x_base, TIMEOUT_CYCLE) == HAL_RESET)
|
||||
return HAL_RESET;
|
||||
|
||||
// 5. clear addr flag
|
||||
hal_i2c_clear_sr(i2c_x_base);
|
||||
|
||||
// 6. clear the DR
|
||||
hal_i2c_master_recv_data(i2c_x_base);
|
||||
|
||||
// 7. receive data
|
||||
for (int i = buf_len; i > 0; i--) {
|
||||
// when reading the last byte,do not send the ack
|
||||
if (buf_len == 1) {
|
||||
// do not send the ack
|
||||
hal_i2c_ack_en(i2c_x_base, HAL_DISABLE);
|
||||
|
||||
/// wait rx not empty
|
||||
if (hal_i2c_wait_rxne(i2c_x_base, TIMEOUT_CYCLE) == HAL_RESET) {
|
||||
return HAL_RESET;
|
||||
} else {
|
||||
*buf = hal_i2c_master_recv_data(i2c_x_base);
|
||||
}
|
||||
// read data
|
||||
} else {
|
||||
// send ack
|
||||
hal_i2c_ack_en(i2c_x_base, HAL_ENABLE);
|
||||
|
||||
// wait rx not empty
|
||||
if (hal_i2c_wait_rxne(i2c_x_base, TIMEOUT_CYCLE) == HAL_RESET) {
|
||||
return HAL_RESET;
|
||||
} else {
|
||||
*buf = hal_i2c_master_recv_data(i2c_x_base);
|
||||
}
|
||||
}
|
||||
buf_len--;
|
||||
buf++;
|
||||
}
|
||||
|
||||
// 8. stop the i2c.
|
||||
hal_i2c_master_stop(i2c_x_base);
|
||||
|
||||
return HAL_SET;
|
||||
}
|
||||
66
cores/lightning-ln882h/arduino/libraries/Wire/Wire.h
Normal file
66
cores/lightning-ln882h/arduino/libraries/Wire/Wire.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2025-01-19. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <HardwareI2C.h>
|
||||
#include <api/RingBuffer.h>
|
||||
|
||||
#define Wire1 Wire
|
||||
|
||||
#define WIRE_HAS_END 1
|
||||
#define WIRE_DEFAULT_FREQ 100000
|
||||
|
||||
#ifndef I2C_PRIV
|
||||
#define I2C_PRIV void
|
||||
#endif
|
||||
|
||||
using arduino::RingBuffer;
|
||||
|
||||
class TwoWire : public HardwareI2C {
|
||||
private:
|
||||
I2C_PRIV *_i2c = NULL;
|
||||
|
||||
RingBuffer _rxBuf;
|
||||
RingBuffer _txBuf;
|
||||
uint8_t _txAddr = 0;
|
||||
bool _inSetPins = false;
|
||||
|
||||
public:
|
||||
TwoWire();
|
||||
TwoWire(int8_t sda, int8_t scl);
|
||||
~TwoWire();
|
||||
|
||||
bool setPins(int8_t sda, int8_t scl);
|
||||
|
||||
bool begin(int8_t sda, int8_t scl, uint32_t frequency = 0);
|
||||
bool begin(uint8_t address, int8_t sda, int8_t scl, uint32_t frequency = 0);
|
||||
bool end();
|
||||
|
||||
bool setClock(uint32_t freq);
|
||||
|
||||
void beginTransmission(uint8_t address);
|
||||
uint8_t endTransmission(bool stopBit);
|
||||
|
||||
size_t requestFrom(uint8_t address, size_t len, bool stopBit);
|
||||
size_t write(uint8_t data);
|
||||
size_t write(const uint8_t *data, size_t len);
|
||||
|
||||
int available();
|
||||
int read();
|
||||
int peek();
|
||||
void flush();
|
||||
|
||||
using HardwareI2C::begin;
|
||||
using HardwareI2C::endTransmission;
|
||||
using HardwareI2C::requestFrom;
|
||||
using HardwareI2C::write;
|
||||
using Print::write;
|
||||
};
|
||||
|
||||
#ifdef PIN_WIRE0_SDA
|
||||
extern TwoWire Wire;
|
||||
#endif
|
||||
#ifdef PIN_WIRE1_SDA
|
||||
extern TwoWire Wire1;
|
||||
#endif
|
||||
27
cores/lightning-ln882h/arduino/src/ArduinoFamily.h
Normal file
27
cores/lightning-ln882h/arduino/src/ArduinoFamily.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-02-10. */
|
||||
|
||||
#pragma once
|
||||
|
||||
// Provide GPIO names to variant.cpp files
|
||||
#define LT_VARIANT_INCLUDE "sdk_private.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif // __cplusplus
|
||||
|
||||
extern void vPortClearInterruptMask(uint32_t ulNewMaskValue);
|
||||
extern uint32_t ulPortSetInterruptMask(void);
|
||||
|
||||
// TODO
|
||||
// #define clockCyclesPerMicrosecond() (SystemCoreClock / 1000000L)
|
||||
// #define clockCyclesToMicroseconds(a) (a * 1000L / (SystemCoreClock / 1000L))
|
||||
// #define microsecondsToClockCycles(a) (a * (SystemCoreClock / 1000000L))
|
||||
|
||||
#define interrupts() vPortClearInterruptMask(0)
|
||||
#define noInterrupts() ulPortSetInterruptMask()
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
8
cores/lightning-ln882h/arduino/src/lt_defs.h
Normal file
8
cores/lightning-ln882h/arduino/src/lt_defs.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#error "Don't include this file directly"
|
||||
|
||||
#define LT_ARD_HAS_WIFI 1
|
||||
#define LT_ARD_HAS_SERIAL 1
|
||||
#define LT_ARD_HAS_WIRE 1
|
||||
#define LT_ARD_MD5_MBEDTLS 1
|
||||
35
cores/lightning-ln882h/arduino/src/main.cpp
Normal file
35
cores/lightning-ln882h/arduino/src/main.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-02-17. */
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <sdk_private.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
#define MAIN_TASK_STACK_SIZE 8192
|
||||
|
||||
static OS_Thread_t g_mainTask_thread;
|
||||
|
||||
bool startMainTask() {
|
||||
OS_Status ret = OS_ThreadCreate(
|
||||
&g_mainTask_thread,
|
||||
"main",
|
||||
(OS_ThreadEntry_t)mainTask,
|
||||
NULL,
|
||||
OS_PRIORITY_BELOW_NORMAL,
|
||||
MAIN_TASK_STACK_SIZE
|
||||
);
|
||||
|
||||
if (ret != OS_OK)
|
||||
return false;
|
||||
OS_ThreadStartScheduler();
|
||||
return true;
|
||||
}
|
||||
|
||||
void wait_for_debug() {
|
||||
while (((CoreDebug->DHCSR) & CoreDebug_DHCSR_C_DEBUGEN_Msk) == 0) {
|
||||
asm("nop");
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
66
cores/lightning-ln882h/arduino/src/wiring.c
Normal file
66
cores/lightning-ln882h/arduino/src/wiring.c
Normal file
@@ -0,0 +1,66 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-02-17. */
|
||||
|
||||
#include "wiring_private.h"
|
||||
|
||||
#ifndef portNVIC_SYSTICK_CURRENT_VALUE_REG
|
||||
#define portNVIC_SYSTICK_CURRENT_VALUE_REG (*((volatile uint32_t *)0xe000e018))
|
||||
#endif
|
||||
|
||||
void delayMicroseconds(unsigned int us) {
|
||||
int i;
|
||||
uint32_t t0, tn;
|
||||
int dfactor = 20 * us - 10 + (81 * us / 100);
|
||||
|
||||
if (us > 100) {
|
||||
t0 = micros();
|
||||
do {
|
||||
tn = micros();
|
||||
} while (tn >= t0 && tn < (t0 + us - 1));
|
||||
} else {
|
||||
for (i = 0; i < dfactor; i++) {
|
||||
asm("nop");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long millis(void) {
|
||||
return (__get_IPSR() == 0) ? xTaskGetTickCount() : xTaskGetTickCountFromISR();
|
||||
}
|
||||
|
||||
unsigned long micros(void) {
|
||||
uint32_t tick1, tick2;
|
||||
uint32_t us;
|
||||
uint32_t tick_per_us = F_CPU / 1000;
|
||||
|
||||
if (__get_IPSR() == 0) {
|
||||
tick1 = xTaskGetTickCount();
|
||||
us = portNVIC_SYSTICK_CURRENT_VALUE_REG;
|
||||
tick2 = xTaskGetTickCount();
|
||||
} else {
|
||||
tick1 = xTaskGetTickCountFromISR();
|
||||
us = portNVIC_SYSTICK_CURRENT_VALUE_REG;
|
||||
tick2 = xTaskGetTickCountFromISR();
|
||||
}
|
||||
|
||||
if (tick1 == tick2) {
|
||||
return tick1 * 1000 - us * 1000 / tick_per_us;
|
||||
} else if ((us * 1000 / tick_per_us) < 500) {
|
||||
return tick1 * 1000 - us * 1000 / tick_per_us;
|
||||
} else {
|
||||
return tick1 * 1000;
|
||||
}
|
||||
}
|
||||
|
||||
void pinRemoveMode(PinInfo *pin, uint32_t mask) {
|
||||
PinData *data = pinData(pin);
|
||||
if ((mask & PIN_GPIO) && (pin->enabled & PIN_GPIO)) {
|
||||
hal_gpio_pin_direction_set(data->gpio_base, data->gpio->pin, GPIO_INPUT);
|
||||
free(data->gpio);
|
||||
pinDisable(pin, PIN_GPIO);
|
||||
}
|
||||
if ((mask & PIN_IRQ) && (pin->enabled & PIN_IRQ)) {
|
||||
data->irqHandler = NULL;
|
||||
hal_gpio_pin_it_en(data->gpio_base, data->gpio->pin, HAL_DISABLE);
|
||||
pinDisable(pin, PIN_IRQ);
|
||||
}
|
||||
}
|
||||
80
cores/lightning-ln882h/arduino/src/wiring_analog.c
Normal file
80
cores/lightning-ln882h/arduino/src/wiring_analog.c
Normal file
@@ -0,0 +1,80 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2025-01-19. */
|
||||
|
||||
#include "wiring_private.h"
|
||||
|
||||
#ifndef PIN_ADC1
|
||||
#define PIN_ADC1 -1
|
||||
#endif
|
||||
#ifndef PIN_ADC2
|
||||
#define PIN_ADC2 -1
|
||||
#endif
|
||||
#ifndef PIN_ADC3
|
||||
#define PIN_ADC3 -1
|
||||
#endif
|
||||
#ifndef PIN_ADC4
|
||||
#define PIN_ADC4 -1
|
||||
#endif
|
||||
#ifndef PIN_ADC5
|
||||
#define PIN_ADC5 -1
|
||||
#endif
|
||||
#ifndef PIN_ADC6
|
||||
#define PIN_ADC6 -1
|
||||
#endif
|
||||
#ifndef PIN_ADC7
|
||||
#define PIN_ADC7 -1
|
||||
#endif
|
||||
|
||||
const uint32_t adc_channels[] = {
|
||||
-1,
|
||||
PIN_ADC1,
|
||||
PIN_ADC2,
|
||||
PIN_ADC3,
|
||||
PIN_ADC4,
|
||||
PIN_ADC5,
|
||||
PIN_ADC6,
|
||||
PIN_ADC7,
|
||||
};
|
||||
|
||||
static adc_ch_t pinToAdcCh(uint32_t gpio) {
|
||||
for (uint8_t i = 0; i < ADC_CH_NUM; i++) {
|
||||
if (adc_channels[i] == gpio)
|
||||
return (adc_ch_t)(1 << i);
|
||||
}
|
||||
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);
|
||||
|
||||
adc_ch_t ch = pinToAdcCh(pin->gpio);
|
||||
adc_init_t_def adc_init;
|
||||
|
||||
memset(&adc_init, 0, sizeof(adc_init_t_def));
|
||||
adc_init.adc_ch = ch;
|
||||
adc_init.adc_conv_mode = ADC_CONV_MODE_CONTINUE;
|
||||
adc_init.adc_presc = 80;
|
||||
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);
|
||||
|
||||
while (hal_adc_get_conv_status(ADC_BASE, ch) == 0)
|
||||
;
|
||||
|
||||
ret = hal_adc_get_data(ADC_BASE, ch);
|
||||
|
||||
hal_adc_clr_conv_status(ADC_BASE, ch);
|
||||
|
||||
return (uint16_t)(3300UL * ret / 4095);
|
||||
}
|
||||
|
||||
uint16_t analogReadMaxVoltage(pin_size_t pinNumber) {
|
||||
return 3300;
|
||||
}
|
||||
27
cores/lightning-ln882h/arduino/src/wiring_data.h
Normal file
27
cores/lightning-ln882h/arduino/src/wiring_data.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-02-17. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <sdk_private.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct PinData_s {
|
||||
gpio_init_t_def *gpio;
|
||||
PinMode gpioMode;
|
||||
uint32_t gpio_base;
|
||||
PinStatus irqMode;
|
||||
void *irqHandler;
|
||||
void *irqParam;
|
||||
};
|
||||
|
||||
#define GPIO_GET_PORT(pin) (pin >> 4)
|
||||
#define GPIO_GET_PIN(pin) ((gpio_pin_t)(1 << (pin & 0xF)))
|
||||
#define GPIO_GET_BASE(pin) (GPIO_GET_PORT(pin) == 1 ? GPIOB_BASE : GPIOA_BASE)
|
||||
|
||||
#ifdef __cplusplus
|
||||
} // extern "C"
|
||||
#endif
|
||||
67
cores/lightning-ln882h/arduino/src/wiring_digital.c
Normal file
67
cores/lightning-ln882h/arduino/src/wiring_digital.c
Normal file
@@ -0,0 +1,67 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-02-17. */
|
||||
|
||||
#include "wiring_private.h"
|
||||
|
||||
void pinMode(pin_size_t pinNumber, PinMode pinMode) {
|
||||
pinCheckGetData(pinNumber, PIN_GPIO, );
|
||||
|
||||
if (pinEnabled(pin, PIN_GPIO) && data->gpioMode == pinMode)
|
||||
return;
|
||||
|
||||
// GPIO can't be used together with PWM
|
||||
pinRemoveMode(pin, PIN_PWM);
|
||||
|
||||
gpio_init_t_def *gpio = data->gpio;
|
||||
if (!gpio) {
|
||||
// allocate memory if pin not used before
|
||||
data->gpio = gpio = malloc(sizeof(gpio_init_t_def));
|
||||
pinEnable(pin, PIN_GPIO);
|
||||
}
|
||||
|
||||
gpio->pin = GPIO_GET_PIN(pinNumber);
|
||||
gpio->speed = GPIO_NORMAL_SPEED;
|
||||
data->gpio_base = GPIO_GET_BASE(pinNumber);
|
||||
data->gpioMode = pinMode;
|
||||
|
||||
switch (pinMode) {
|
||||
case INPUT:
|
||||
gpio->dir = GPIO_INPUT;
|
||||
gpio->pull = GPIO_PULL_NONE;
|
||||
break;
|
||||
case INPUT_PULLDOWN:
|
||||
gpio->dir = GPIO_INPUT;
|
||||
gpio->pull = GPIO_PULL_DOWN;
|
||||
break;
|
||||
case INPUT_PULLUP:
|
||||
gpio->dir = GPIO_INPUT;
|
||||
gpio->pull = GPIO_PULL_UP;
|
||||
break;
|
||||
case OUTPUT:
|
||||
gpio->dir = GPIO_OUTPUT;
|
||||
gpio->pull = GPIO_PULL_NONE;
|
||||
break;
|
||||
case OUTPUT_OPENDRAIN:
|
||||
gpio->dir = GPIO_INPUT;
|
||||
gpio->pull = GPIO_PULL_NONE;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
hal_gpio_init(data->gpio_base, gpio);
|
||||
}
|
||||
|
||||
void digitalWrite(pin_size_t pinNumber, PinStatus status) {
|
||||
pinCheckGetData(pinNumber, PIN_GPIO, );
|
||||
pinSetOutputPull(pin, data, pinNumber, status);
|
||||
if (status)
|
||||
hal_gpio_pin_set(data->gpio_base, data->gpio->pin);
|
||||
else
|
||||
hal_gpio_pin_reset(data->gpio_base, data->gpio->pin);
|
||||
}
|
||||
|
||||
PinStatus digitalRead(pin_size_t pinNumber) {
|
||||
pinCheckGetData(pinNumber, PIN_GPIO, LOW);
|
||||
pinSetInputMode(pin, data, pinNumber);
|
||||
return hal_gpio_pin_input_read(data->gpio_base, data->gpio->pin);
|
||||
}
|
||||
91
cores/lightning-ln882h/arduino/src/wiring_irq.c
Normal file
91
cores/lightning-ln882h/arduino/src/wiring_irq.c
Normal file
@@ -0,0 +1,91 @@
|
||||
/* Copyright (c) Etienne Le Cousin 2024-03-04. */
|
||||
|
||||
#include "wiring_private.h"
|
||||
|
||||
void GPIOA_IRQHandler() {
|
||||
for (pin_size_t pinNumber = 0; pinNumber < 16; pinNumber++) {
|
||||
gpio_pin_t gpio = GPIO_GET_PIN(pinNumber);
|
||||
|
||||
if (hal_gpio_pin_get_it_flag(GPIOA_BASE, gpio) == HAL_SET) {
|
||||
hal_gpio_pin_clr_it_flag(GPIOA_BASE, gpio);
|
||||
|
||||
PinInfo *pin = pinInfo(pinNumber);
|
||||
if (!pin)
|
||||
continue;
|
||||
|
||||
PinData *data = pinData(pin);
|
||||
if (!data->irqHandler)
|
||||
continue;
|
||||
if (!data->irqParam)
|
||||
((voidFuncPtr)data->irqHandler)();
|
||||
else
|
||||
((voidFuncPtrParam)data->irqHandler)(data->irqParam);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GPIOB_IRQHandler() {
|
||||
for (pin_size_t pinNumber = 16; pinNumber < 32; pinNumber++) {
|
||||
gpio_pin_t gpio = GPIO_GET_PIN(pinNumber);
|
||||
|
||||
if (hal_gpio_pin_get_it_flag(GPIOB_BASE, gpio) == HAL_SET) {
|
||||
hal_gpio_pin_clr_it_flag(GPIOB_BASE, gpio);
|
||||
|
||||
PinInfo *pin = pinInfo(pinNumber);
|
||||
if (!pin)
|
||||
continue;
|
||||
|
||||
PinData *data = pinData(pin);
|
||||
if (!data->irqHandler)
|
||||
continue;
|
||||
if (!data->irqParam)
|
||||
((voidFuncPtr)data->irqHandler)();
|
||||
else
|
||||
((voidFuncPtrParam)data->irqHandler)(data->irqParam);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void attachInterruptParam(pin_size_t interruptNumber, voidFuncPtrParam callback, PinStatus mode, void *param) {
|
||||
pinCheckGetData(interruptNumber, PIN_IRQ, );
|
||||
|
||||
data->irqHandler = callback;
|
||||
data->irqParam = param;
|
||||
|
||||
if (pinEnabled(pin, PIN_IRQ) && data->irqMode == mode)
|
||||
return;
|
||||
|
||||
// GPIO can't be used together with PWM
|
||||
pinRemoveMode(pin, PIN_PWM);
|
||||
|
||||
uint32_t event = 0;
|
||||
switch (mode) {
|
||||
case LOW:
|
||||
LT_W("LOW interrupts not supported, setting FALLING instead");
|
||||
case FALLING:
|
||||
event = GPIO_INT_FALLING;
|
||||
break;
|
||||
case HIGH:
|
||||
LT_W("HIGH interrupts not supported, setting RISING instead");
|
||||
case RISING:
|
||||
event = GPIO_INT_RISING;
|
||||
break;
|
||||
case CHANGE:
|
||||
event = GPIO_INT_RISING_FALLING;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
pinEnable(pin, PIN_IRQ);
|
||||
data->irqMode = mode;
|
||||
|
||||
hal_gpio_pin_it_cfg(data->gpio_base, data->gpio->pin, event);
|
||||
hal_gpio_pin_it_en(data->gpio_base, data->gpio->pin, HAL_ENABLE);
|
||||
}
|
||||
|
||||
void detachInterrupt(pin_size_t interruptNumber) {
|
||||
pinCheckGetData(interruptNumber, PIN_IRQ, );
|
||||
|
||||
hal_gpio_pin_it_en(data->gpio_base, data->gpio->pin, HAL_DISABLE);
|
||||
pinModeRemove(interruptNumber, PIN_IRQ);
|
||||
}
|
||||
Reference in New Issue
Block a user