Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feature/i2c-spi
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Mar 14, 2024
2 parents cf3f23e + d1386a8 commit 54cc794
Show file tree
Hide file tree
Showing 29 changed files with 429 additions and 172 deletions.
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [](SUMMARY.md)
* 🍪 Chip family docs & info
* [Beken BK72xx](docs/platform/beken-72xx/README.md)
* [Finding encryption keys](docs/platform/beken-72xx/keys.md)
* [Realtek Ameba - info](docs/platform/realtek-amb/README.md)
* [Realtek AmebaZ](docs/platform/realtek-ambz/README.md)
* [Debugging](docs/platform/realtek-ambz/debugging.md)
Expand Down
20 changes: 11 additions & 9 deletions builder/utils/cores.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,17 @@ def env_add_arduino_libraries(env: Environment, queue, name: str, path: str) ->
srcs=[
"+<**/*.c*>",
],
includes=[
"!<*/.>",
"!<*/*>",
]
if name.startswith("common")
else [
"!<.>",
"!<*>",
],
includes=(
[
"!<*/.>",
"!<*/*>",
]
if name.startswith("common")
else [
"!<.>",
"!<*>",
]
),
)
return True

Expand Down
5 changes: 3 additions & 2 deletions cores/beken-72xx/arduino/libraries/WiFi/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
WiFiClass::WiFiClass() {
data = (WiFiData *)calloc(1, sizeof(WiFiData));

DATA->scanSem = xSemaphoreCreateBinary();
STA_CFG.dhcp_mode = DHCP_CLIENT;
DATA->scanSem = xSemaphoreCreateBinary();
STA_CFG.dhcp_mode = DHCP_CLIENT;
STA_ADV_CFG.dhcp_mode = DHCP_CLIENT;
}

WiFiClass::~WiFiClass() {
Expand Down
2 changes: 2 additions & 0 deletions cores/beken-72xx/arduino/libraries/WiFi/WiFiPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ extern void wifiEventHandler(rw_evt_type event);

typedef struct {
network_InitTypeDef_st configSta;
network_InitTypeDef_adv_st configStaAdv;
network_InitTypeDef_ap_st configAp;
unsigned long scannedAt;
SemaphoreHandle_t scanSem;
Expand All @@ -73,6 +74,7 @@ typedef struct {
#define cDATA ((WiFiData *)cls->data)

#define STA_CFG (DATA->configSta)
#define STA_ADV_CFG (DATA->configStaAdv)
#define AP_CFG (DATA->configAp)
#define IP_STATUS (DATA->statusIp)
#define LINK_STATUS (DATA->statusLink)
Expand Down
55 changes: 41 additions & 14 deletions cores/beken-72xx/arduino/libraries/WiFi/WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,26 @@ WiFiClass::begin(const char *ssid, const char *passphrase, int32_t channel, cons

disconnect(false);

strcpy(STA_CFG.wifi_ssid, ssid);
if (passphrase) {
strcpy(STA_CFG.wifi_key, passphrase);
if (bssid) {
strcpy(STA_ADV_CFG.ap_info.ssid, ssid);
if (passphrase) {
strcpy(STA_ADV_CFG.key, passphrase);
STA_ADV_CFG.key_len = strlen(passphrase);
} else {
STA_ADV_CFG.key[0] = '\0';
STA_ADV_CFG.key_len = 0;
}
STA_ADV_CFG.ap_info.channel = channel;
STA_ADV_CFG.wifi_retry_interval = 100;
} else {
STA_CFG.wifi_bssid[0] = '\0';
strcpy(STA_CFG.wifi_ssid, ssid);
if (passphrase) {
strcpy(STA_CFG.wifi_key, passphrase);
} else {
STA_CFG.wifi_key[0] = '\0';
}
STA_CFG.wifi_retry_interval = 100;
STA_CFG.wifi_mode = BK_STATION;
}

if (reconnect(bssid))
Expand All @@ -27,21 +42,31 @@ WiFiClass::begin(const char *ssid, const char *passphrase, int32_t channel, cons
}

bool WiFiClass::config(IPAddress localIP, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2) {
STA_CFG.dhcp_mode = localIP ? DHCP_DISABLE : DHCP_CLIENT;
STA_CFG.dhcp_mode = localIP ? DHCP_DISABLE : DHCP_CLIENT;
STA_ADV_CFG.dhcp_mode = localIP ? DHCP_DISABLE : DHCP_CLIENT;
if (localIP) {
sprintf(STA_CFG.local_ip_addr, IP_FMT, localIP[0], localIP[1], localIP[2], localIP[3]);
sprintf(STA_CFG.net_mask, IP_FMT, subnet[0], subnet[1], subnet[2], subnet[3]);
sprintf(STA_CFG.gateway_ip_addr, IP_FMT, gateway[0], gateway[1], gateway[2], gateway[3]);
sprintf(STA_ADV_CFG.local_ip_addr, IP_FMT, localIP[0], localIP[1], localIP[2], localIP[3]);
sprintf(STA_ADV_CFG.net_mask, IP_FMT, subnet[0], subnet[1], subnet[2], subnet[3]);
sprintf(STA_ADV_CFG.gateway_ip_addr, IP_FMT, gateway[0], gateway[1], gateway[2], gateway[3]);
if (dns1) {
sprintf(STA_CFG.dns_server_ip_addr, IP_FMT, dns1[0], dns1[1], dns1[2], dns1[3]);
sprintf(STA_ADV_CFG.dns_server_ip_addr, IP_FMT, dns1[0], dns1[1], dns1[2], dns1[3]);
} else {
STA_CFG.dns_server_ip_addr[0] = '\0';
STA_CFG.dns_server_ip_addr[0] = '\0';
STA_ADV_CFG.dns_server_ip_addr[0] = '\0';
}
} else {
STA_CFG.local_ip_addr[0] = '\0';
STA_CFG.net_mask[0] = '\0';
STA_CFG.gateway_ip_addr[0] = '\0';
STA_CFG.dns_server_ip_addr[0] = '\0';
STA_CFG.local_ip_addr[0] = '\0';
STA_CFG.net_mask[0] = '\0';
STA_CFG.gateway_ip_addr[0] = '\0';
STA_CFG.dns_server_ip_addr[0] = '\0';
STA_ADV_CFG.local_ip_addr[0] = '\0';
STA_ADV_CFG.net_mask[0] = '\0';
STA_ADV_CFG.gateway_ip_addr[0] = '\0';
STA_ADV_CFG.dns_server_ip_addr[0] = '\0';
}

// from wlan_ui.c:1370
Expand Down Expand Up @@ -74,10 +99,8 @@ bool WiFiClass::reconnect(const uint8_t *bssid) {

LT_DM(WIFI, "Data = %p", DATA->configSta);

STA_CFG.wifi_mode = BK_STATION;
STA_CFG.wifi_retry_interval = 100;
if (bssid)
memcpy(STA_CFG.wifi_bssid, bssid, 6);
memcpy(STA_ADV_CFG.ap_info.bssid, bssid, 6);
else
memset(STA_CFG.wifi_bssid, 0x00, 6);

Expand All @@ -91,7 +114,11 @@ bool WiFiClass::reconnect(const uint8_t *bssid) {
LT_DM(WIFI, "Starting WiFi...");

__wrap_bk_printf_disable();
bk_wlan_start_sta(&STA_CFG);
if (bssid) {
bk_wlan_start_sta_adv(&STA_ADV_CFG);
} else {
bk_wlan_start_sta(&STA_CFG);
}
__wrap_bk_printf_enable();

LT_DM(WIFI, "Start OK");
Expand Down
16 changes: 13 additions & 3 deletions cores/beken-72xx/arduino/libraries/WiFi/WiFiScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@ static void scanHandler(void *ctx, uint8_t param) {
return;
}

uint8_t apNum = 0;
ScanResult_adv result;
result.ApNum = 0;
result.ApList = NULL;
if (wlan_sta_scan_result(&result)) {
LT_EM(WIFI, "Failed to get scan result");
goto end;
}
LT_IM(WIFI, "Found %d APs", result.ApNum);

cls->scanAlloc(result.ApNum);
if (!scan->ap) {
apNum = cls->scanAlloc(result.ApNum);
if (0 == apNum) {
LT_WM(WIFI, "scan->ap alloc failed");
goto end;
}

for (uint8_t i = 0; i < result.ApNum; i++) {
if (apNum < result.ApNum) {
LT_WM(WIFI, "alloc failed, only %d APs will be copied");
}

for (uint8_t i = 0; i < apNum; i++) {
scan->ap[i].ssid = strdup(result.ApList[i].ssid);
scan->ap[i].auth = securityTypeToAuthMode(result.ApList[i].security);
scan->ap[i].rssi = result.ApList[i].ApPower;
Expand All @@ -47,6 +54,9 @@ static void scanHandler(void *ctx, uint8_t param) {
scan->running = false;
xSemaphoreGive(cDATA->scanSem);
}
if (result.ApList) {
free(result.ApList);
}
LT_HEAP_I();
return;
}
Expand Down
11 changes: 7 additions & 4 deletions cores/beken-72xx/arduino/src/wiring.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,13 @@ void pinRemoveMode(PinInfo *pin, uint32_t mask) {
pinDisable(pin, PIN_IRQ);
}
if ((mask & PIN_PWM) && (pin->enabled & PIN_PWM)) {
data->pwm.cfg.bits.en = PWM_DISABLE;
__wrap_bk_printf_disable();
sddev_control(PWM_DEV_NAME, CMD_PWM_DEINIT_PARAM, &data->pwm);
__wrap_bk_printf_enable();
if (data->pwmState != LT_PWM_STOPPED) {
data->pwmState = LT_PWM_STOPPED;
data->pwm.cfg.bits.en = PWM_DISABLE;
__wrap_bk_printf_disable();
sddev_control(PWM_DEV_NAME, CMD_PWM_DEINIT_PARAM, &data->pwm);
__wrap_bk_printf_enable();
}
pinDisable(pin, PIN_PWM);
}
}
65 changes: 42 additions & 23 deletions cores/beken-72xx/arduino/src/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,21 @@ uint16_t analogReadVoltage(pin_size_t pinNumber) {
adc.pData = adcData;
adc.data_buff_size = 1;
handle = ddev_open(SARADC_DEV_NAME, &status, (uint32_t)&adc);
if (status)
if (handle == -1) {
return 0;
}

if (status != SARADC_SUCCESS) {
ddev_close(handle);
return 0;
}

// wait for data
while (!adc.has_data || adc.current_sample_data_cnt < 1) {
delay(1);
}
ddev_control(handle, SARADC_CMD_RUN_OR_STOP_ADC, (void *)false);
uint8_t run_stop = 0; // stop
ddev_control(handle, SARADC_CMD_RUN_OR_STOP_ADC, &run_stop);
ddev_close(handle);
return adcData[0];
}
Expand All @@ -86,10 +94,22 @@ void analogWrite(pin_size_t pinNumber, int value) {
// GPIO can't be used together with PWM
pinRemoveMode(pin, PIN_GPIO | PIN_IRQ);

float percent = value * 1.0 / ((1 << _analogWriteResolution) - 1);
uint32_t frequency = 26 * _analogWritePeriod - 1;

if (!pinEnabled(pin, PIN_PWM)) {
pinEnable(pin, PIN_PWM);
data->pwmState = LT_PWM_STOPPED;
data->pwm.channel = gpioToPwm(pin->gpio);
data->pwm.cfg.bits.en = PWM_ENABLE;
data->pwm.cfg.bits.int_en = PWM_INT_DIS;
data->pwm.cfg.bits.mode = PWM_PWM_MODE;
data->pwm.cfg.bits.clk = PWM_CLK_26M;
data->pwm.end_value = frequency;
data->pwm.p_Int_Handler = NULL;
}

float percent = value * 1.0 / ((1 << _analogWriteResolution) - 1);
uint32_t dutyCycle = percent * frequency;
data->pwm.channel = gpioToPwm(pin->gpio);
uint32_t channel = data->pwm.channel;
#if CFG_SOC_NAME != SOC_BK7231N
data->pwm.duty_cycle = dutyCycle;
Expand All @@ -99,33 +119,32 @@ void analogWrite(pin_size_t pinNumber, int value) {
data->pwm.duty_cycle3 = 0;
#endif

if (dutyCycle) {
if (!pinEnabled(pin, PIN_PWM)) {
if ((data->pwmState == LT_PWM_STOPPED) || (data->pwmState == LT_PWM_PAUSED)) {
if (dutyCycle) {
// enable PWM and set its value
data->pwm.cfg.bits.en = PWM_ENABLE;
data->pwm.cfg.bits.int_en = PWM_INT_DIS;
data->pwm.cfg.bits.mode = PWM_PWM_MODE;
data->pwm.cfg.bits.clk = PWM_CLK_26M;
data->pwm.end_value = frequency;
data->pwm.p_Int_Handler = NULL;

__wrap_bk_printf_disable();
sddev_control(PWM_DEV_NAME, CMD_PWM_INIT_PARAM, &data->pwm);
sddev_control(PWM_DEV_NAME, CMD_PWM_INIT_LEVL_SET_HIGH, &channel);
sddev_control(PWM_DEV_NAME, CMD_PWM_UNIT_ENABLE, &channel);
__wrap_bk_printf_enable();
pinEnable(pin, PIN_PWM);
} else {

data->pwmState = LT_PWM_RUNNING;
}
} else if (data->pwmState == LT_PWM_RUNNING) {
if (dutyCycle) {
__wrap_bk_printf_disable();
sddev_control(PWM_DEV_NAME, CMD_PWM_INIT_LEVL_SET_HIGH, &channel);
// update duty cycle
sddev_control(PWM_DEV_NAME, CMD_PWM_SET_DUTY_CYCLE, &data->pwm);
__wrap_bk_printf_enable();
} else {
__wrap_bk_printf_disable();
sddev_control(PWM_DEV_NAME, CMD_PWM_INIT_LEVL_SET_LOW, &channel);
sddev_control(PWM_DEV_NAME, CMD_PWM_SET_DUTY_CYCLE, &data->pwm);
sddev_control(PWM_DEV_NAME, CMD_PWM_UNIT_DISABLE, &channel);
__wrap_bk_printf_enable();

data->pwmState = LT_PWM_PAUSED;
}
} else {
if (pinEnabled(pin, PIN_PWM)) {
// disable PWM
pinRemoveMode(pin, PIN_PWM);
}
// force level as LOW
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
3 changes: 3 additions & 0 deletions cores/beken-72xx/arduino/src/wiring_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
extern "C" {
#endif

typedef enum lt_pwm_state_tag { LT_PWM_STOPPED, LT_PWM_RUNNING, LT_PWM_PAUSED } lt_pwm_state_t;

struct PinData_s {
pwm_param_t pwm;
lt_pwm_state_t pwmState;
PinMode gpioMode;
PinStatus irqMode;
void *irqHandler;
Expand Down
6 changes: 4 additions & 2 deletions cores/beken-72xx/base/api/lt_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ lt_reboot_reason_t lt_get_reboot_reason() {
case RESET_SOURCE_CRASH_UNUSED:
case RESET_SOURCE_CRASH_PER_XAT0:
return REBOOT_REASON_CRASH;
case RESET_SOURCE_DEEPPS_USB:
return REBOOT_REASON_SLEEP_USB;
case RESET_SOURCE_DEEPPS_GPIO:
return REBOOT_REASON_SLEEP_GPIO;
case RESET_SOURCE_DEEPPS_RTC:
case RESET_SOURCE_DEEPPS_USB:
return REBOOT_REASON_SLEEP;
return REBOOT_REASON_SLEEP_RTC;
default:
return REBOOT_REASON_UNKNOWN;
}
Expand Down
11 changes: 5 additions & 6 deletions cores/beken-72xx/base/api/lt_sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ void lt_deep_sleep_unset_gpio(uint32_t gpio_index_map) {
deep_sleep_param.gpio_index_map &= (~gpio_index_map);
}

void lt_deep_sleep_config_timer(uint32_t sleep_duration) {
void lt_deep_sleep_config_timer(uint32_t sleep_duration_ms) {
deep_sleep_param.wake_up_way |= PS_DEEP_WAKEUP_RTC;
uint64_t duration_math = 32768 * sleep_duration;
if (duration_math / 1000 > 0xFFFFFFFF) {
// Sleep forever
deep_sleep_param.sleep_time = 0xFFFFFFFF;
uint64_t sleep_ticks = 32.768 * sleep_duration_ms;
if (sleep_ticks >= 0xFFFFFFFF) {
deep_sleep_param.sleep_time = 0xFFFFFFFE;
} else {
deep_sleep_param.sleep_time = (duration_math / 1000) & 0xFFFFFFFF;
deep_sleep_param.sleep_time = sleep_ticks & 0xFFFFFFFF;
}
}

Expand Down
24 changes: 17 additions & 7 deletions cores/common/arduino/libraries/api/WiFi/WiFiScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@ void WiFiClass::scanDelete() {
}

uint8_t WiFiClass::scanAlloc(uint8_t count) {
uint8_t last = scan->count;
scan->count = count;
scan->ap = (WiFiScanAP *)realloc(scan->ap, count * sizeof(WiFiScanAP));
if (!scan->ap)
return 255;
memset(scan->ap + last, 0, sizeof(WiFiScanAP));
return last;
if ((!scan->ap) || (count > scan->count)) {
auto newMem = (WiFiScanAP *)realloc(scan->ap, count * sizeof(WiFiScanAP));
if (!newMem) {
return scan->count;
}
scan->ap = newMem;
}
if (!scan->ap) {
scan->count = 0;
return 0;
}
if (count > scan->count) {
// clear only new entries
memset(scan->ap + scan->count, 0, sizeof(WiFiScanAP) * (count - scan->count));
}
scan->count = count;
return count;
}

String WiFiClass::SSID(uint8_t networkItem) {
Expand Down
Loading

0 comments on commit 54cc794

Please sign in to comment.