Skip to content

Commit

Permalink
Move to RTC as time base; HSEM3 critical regions; RTOS rework; BLE 1.…
Browse files Browse the repository at this point in the history
…14.1 stack
  • Loading branch information
GrumpyOldPizza committed Jan 18, 2023
1 parent 7192ecd commit c9865e8
Show file tree
Hide file tree
Showing 70 changed files with 7,448 additions and 17,311 deletions.
1 change: 0 additions & 1 deletion cores/arduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ extern "C"{

#define ARM_MATH_CM4
#include "armv7m.h"
#include "rtos_api.h"
#include "stm32wbxx.h"

#undef STM32WB
Expand Down
8 changes: 4 additions & 4 deletions cores/arduino/CDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ CDC::CDC(void (*serialEventRun)(void)) {

m_work = K_WORK_INIT((k_work_routine_t)&CDC::notifyRoutine, this);

m_event = K_EVENT_INIT();
m_sem = K_SEM_INIT(0, 1);
}

void CDC::begin(unsigned long baudrate) {
Expand Down Expand Up @@ -135,7 +135,7 @@ int CDC::read(uint8_t *buffer, size_t size) {
void CDC::flush() {
if (k_task_is_in_progress()) {
while (m_tx_busy) {
k_event_receive(&m_event, 1, (K_EVENT_ANY | K_EVENT_CLEAR), K_TIMEOUT_FOREVER, NULL);
k_sem_acquire(&m_sem, K_TIMEOUT_FOREVER);
}
}
}
Expand Down Expand Up @@ -217,7 +217,7 @@ size_t CDC::write(const uint8_t *buffer, size_t size) {
}

if (m_tx_busy) {
k_event_receive(&m_event, 1, (K_EVENT_ANY | K_EVENT_CLEAR), K_TIMEOUT_FOREVER, NULL);
k_sem_acquire(&m_sem, K_TIMEOUT_FOREVER);
}
}
} while (tx_size == 0);
Expand Down Expand Up @@ -401,7 +401,7 @@ bool CDC::rts() {
void CDC::transmitCallback(class CDC *self) {
uint32_t tx_read, tx_write, tx_count;

k_event_send(&self->m_event, 1);
k_sem_release(&self->m_sem);

tx_count = self->m_tx_count;

Expand Down
2 changes: 1 addition & 1 deletion cores/arduino/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int wiring_stderr_write(const char *data, int nbytes) {
return Serial.write((const uint8_t*)data, nbytes);
}

retirn 0;
return 0;
}

}
Expand Down
3 changes: 2 additions & 1 deletion cores/arduino/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ OBJCOPY = $(TOOLS)/bin/arm-none-eabi-objcopy
CFLAGS = -c -g3 -Os $(WARNINGS) -std=gnu11 -ffast-math -ffunction-sections -fdata-sections -nostdlib -MMD $(EXTRAS) $(DEFINES) $(INCLUDES)
CXXFLAGS = -c -g3 -Os $(WARNINGS) -std=gnu++11 -ffast-math -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib -fno-use-cxa-atexit -fno-rtti -fno-exceptions -MMD $(EXTRAS) $(DEFINES) $(INCLUDES)
ASFLAGS = -c -g -x assembler-with-cpp $(EXTRAS) $(DEFINES) $(INCLUDES)
LDFLAGS = -g -Os $(EXTRAS) -Wl,--gc-sections,--no-undefined -T../../system/STM32WBxx/LinkScripts/STM32WB55xx_FLASH.ld --specs=nano.specs --specs=nosys.specs -Wl,--defsym=__RTC_EPOCH__=$(shell date +"%s")
LDFLAGS = -g -Os $(EXTRAS) -Wl,--gc-sections,--no-undefined -T../../system/STM32WBxx/LdScripts/STM32WB55xx_FLASH.ld --specs=nano.specs --specs=nosys.specs -Wl,--defsym=__RTC_EPOCH__=$(shell date +"%s")
#LDFLAGS = -g -Os $(EXTRAS) -Wl,--gc-sections,--no-undefined -T../../system/STM32WBxx/LinkScripts/STM32WB55xx_FLASH.ld --specs=nano.specs --specs=nosys.specs
WARNINGS = -Wall -Wextra
EXTRAS = -mcpu=cortex-m4 -march=armv7e-m+fp -mthumb -mabi=aapcs -mfpu=fpv4-sp-d16 -mfloat-abi=hard
DEFINES = -DSTM32WB55xx -D__SYSTEM_CORE_CLOCK__=64000000 -DDOSFS_SDCARD=0 -DDOSFS_SFLASH=0 -DARDUINO_MAKEFILE
Expand Down
2 changes: 1 addition & 1 deletion cores/arduino/USBAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class CDC : public HardwareSerial
Callback m_transmit_callback;

k_work_t m_work;
k_event_t m_event;
k_sem_t m_sem;

static void transmitCallback(class CDC *self);
static void eventCallback(class CDC *self, uint32_t events);
Expand Down
20 changes: 10 additions & 10 deletions cores/arduino/Uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Uart::Uart(struct _stm32wb_uart_t *uart, const struct _stm32wb_uart_params_t *pa

m_work = K_WORK_INIT((k_work_routine_t)&Uart::notifyRoutine, this);

m_event = K_EVENT_INIT();
m_sem = K_SEM_INIT(0, 1);

stm32wb_uart_create(uart, params);
}
Expand Down Expand Up @@ -145,7 +145,7 @@ int Uart::read(uint8_t *buffer, size_t size) {
void Uart::flush() {
if (k_task_is_in_progress()) {
while (m_tx_busy) {
k_event_receive(&m_event, 1, (K_EVENT_ANY | K_EVENT_CLEAR), K_TIMEOUT_FOREVER, NULL);
k_sem_acquire(&m_sem, K_TIMEOUT_FOREVER);
}
}
}
Expand Down Expand Up @@ -206,8 +206,8 @@ size_t Uart::write(const uint8_t *buffer, size_t size) {
tx_count = m_tx_size - tx_read;
}

if (tx_count > CDC_TX_PACKET_SIZE) {
tx_count = CDC_TX_PACKET_SIZE;
if (tx_count > UART_TX_PACKET_SIZE) {
tx_count = UART_TX_PACKET_SIZE;
}

if (tx_count) {
Expand All @@ -227,7 +227,7 @@ size_t Uart::write(const uint8_t *buffer, size_t size) {
}

if (m_tx_busy) {
k_event_receive(&m_event, 1, (K_EVENT_ANY | K_EVENT_CLEAR), K_TIMEOUT_FOREVER, NULL);
k_sem_acquire(&m_sem, K_TIMEOUT_FOREVER);
}
}
} while (tx_size == 0);
Expand Down Expand Up @@ -270,8 +270,8 @@ size_t Uart::write(const uint8_t *buffer, size_t size) {
tx_count = m_tx_size - tx_read;
}

if (tx_count > CDC_TX_PACKET_SIZE) {
tx_count = CDC_TX_PACKET_SIZE;
if (tx_count > UART_TX_PACKET_SIZE) {
tx_count = UART_TX_PACKET_SIZE;
}

if (tx_count) {
Expand Down Expand Up @@ -359,7 +359,7 @@ Uart::operator bool() {
void Uart::transmitCallback(class Uart *self) {
uint32_t tx_read, tx_write, tx_count;

k_event_send(&self->m_event, 1);
k_sem_release(&self->m_sem);

tx_count = self->m_tx_count;

Expand All @@ -381,8 +381,8 @@ void Uart::transmitCallback(class Uart *self) {
tx_count = (self->m_tx_size - tx_read);
}

if (tx_count > CDC_TX_PACKET_SIZE) {
tx_count = CDC_TX_PACKET_SIZE;
if (tx_count > UART_TX_PACKET_SIZE) {
tx_count = UART_TX_PACKET_SIZE;
}

self->m_tx_count = tx_count;
Expand Down
2 changes: 1 addition & 1 deletion cores/arduino/Uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Uart : public HardwareSerial
Callback m_transmit_callback;

k_work_t m_work;
k_event_t m_event;
k_sem_t m_sem;

static void transmitCallback(class Uart *self);
static void eventCallback(class Uart *self, uint32_t events);
Expand Down
55 changes: 0 additions & 55 deletions cores/arduino/avr/fdevopen.c

This file was deleted.

2 changes: 0 additions & 2 deletions cores/arduino/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

#if defined(ARDUINO_MAKEFILE)

#include "STM32WB.h"

void setup(void) {
}

Expand Down
1 change: 0 additions & 1 deletion cores/arduino/syscalls_stm32wb.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <sys/unistd.h>

#include "armv7m.h"
#include "rtos_api.h"

extern int wiring_stdin_read(char *data, int nbytes);
extern int wiring_stdout_write(const char *data, int nbytes);
Expand Down
2 changes: 0 additions & 2 deletions cores/arduino/wiring.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ extern "C" {

#undef ARDUINO_RV2

k_event_t g_wakeup_event = K_EVENT_INIT();

#if defined(USBCON)
stm32wb_uart_t g_Serial1;
extern const stm32wb_uart_params_t g_Serial1Params;
Expand Down
5 changes: 2 additions & 3 deletions cores/arduino/wiring_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ extern "C" {

extern void dosfs_sflash_initialize();

extern k_event_t g_wakeup_event;

#define WIRING_EVENT_WAKEUP 0x00000001
#define WIRING_EVENT_TRANSIENT 0x80000000
#define WIRING_EVENT_WAKEUP 0x40000000

extern const uint32_t g_pinModeConfiguration[];

Expand Down
11 changes: 9 additions & 2 deletions libraries/BLE/src/BLE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ extern "C" {
#define BLE_LSE_SOURCE_NOCALIB 0x00
#define BLE_LSE_SOURCE_CALIB 0x01
#define BLE_LSE_SOURCE_MOD5MM 0x02
#define BLE_LSE_SOURCE_HSE 0x04 /* HSE/1024 vs LSE */

#define BLE_OPTION_LL_ONLY 0x01
#define BLE_OPTION_NO_SERVICE_CHANGED 0x02
Expand All @@ -120,6 +121,9 @@ extern "C" {
#define BLE_RX_MODEL_AGC_RSSI_LEGACY 0x00
#define BLE_RX_MODEL_AGC_RSSI_BLOCKER 0x01

#define BLE_CORE_VERSION_5_2 11
#define BLE_CORE_VERSION_5_3 12

#define BLE_VALUE_ATTRIB_HANDLE_OFFSET 1
#define BLE_CCCD_ATTRIB_HANDLE_OFFSET 2

Expand Down Expand Up @@ -1762,7 +1766,7 @@ static const uint8_t ER[16] = { 0xfe,0xdc,0xba,0x09,0x87,0x65,0x43,0x21,0xfe,0xd
static const uint8_t IR[16] = { 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0 };

static void hci_done_req(k_task_t *task) {
k_task_wakeup(task);
k_event_send(task, WIRING_EVENT_TRANSIENT);
}

int hci_send_req(stm32wb_ipcc_ble_command_t *command, bool async) {
Expand All @@ -1779,7 +1783,7 @@ int hci_send_req(stm32wb_ipcc_ble_command_t *command, bool async) {
return BLE_STATUS_ERROR;
}

if (k_task_sleep(K_TIMEOUT_FOREVER) != K_NO_ERROR) {
if (k_event_receive(WIRING_EVENT_TRANSIENT, (K_EVENT_ANY | K_EVENT_CLEAR), K_TIMEOUT_FOREVER, NULL) != K_NO_ERROR) {
return BLE_STATUS_ERROR;
}

Expand Down Expand Up @@ -1886,6 +1890,9 @@ bool BLELocalDevice::begin(int mtu, uint32_t options) {
0, /* MaxAdvDataLen */
BLE_TX_PATH_COMPENSATION, /* TxPathCompensation */
BLE_RX_PATH_COMPENSATION /* RxPacthCompensation */
#if 0
BLE_CORE_VERSION_5_2, /* BleCoreVersion */
#endif
};

if (!stm32wb_ipcc_sys_enable()) {
Expand Down
12 changes: 6 additions & 6 deletions libraries/GNSS/src/utility/gnss_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ static void ubx_start_message(gnss_device_t *device, unsigned int message)

device->rx_chunk = 40;

stm32wb_lptim_timeout_cancel(&device->ubx.timeout);
stm32wb_lptim_timeout_stop(&device->ubx.timeout);
}
}

Expand Down Expand Up @@ -2874,14 +2874,14 @@ static void ubx_table(gnss_device_t *device, const uint8_t * const * table)

ubx_send(device, data);

stm32wb_lptim_timeout_relative(&device->ubx.timeout, STM32WB_LPTIM_TIMEOUT_MILLIS_TO_TICKS(125), (stm32wb_lptim_timeout_callback_t)ubx_timeout, NULL); // 125ms
stm32wb_lptim_timeout_start(&device->ubx.timeout, STM32WB_LPTIM_TIMEOUT_MILLIS_TO_TICKS(125), (stm32wb_lptim_timeout_callback_t)ubx_timeout, NULL); // 125ms
}

static void ubx_configure(gnss_device_t *device, __attribute__((unused)) unsigned int response, uint32_t command)
{
const uint8_t *data = NULL;

stm32wb_lptim_timeout_cancel(&device->ubx.timeout);
stm32wb_lptim_timeout_stop(&device->ubx.timeout);

// printf("CONFIGURE %04x\r\n", command);

Expand Down Expand Up @@ -2946,7 +2946,7 @@ static void ubx_configure(gnss_device_t *device, __attribute__((unused)) unsigne
{
ubx_send(device, data);

stm32wb_lptim_timeout_relative(&device->ubx.timeout, STM32WB_LPTIM_TIMEOUT_MILLIS_TO_TICKS(125), (stm32wb_lptim_timeout_callback_t)ubx_timeout, NULL); // 125ms
stm32wb_lptim_timeout_start(&device->ubx.timeout, STM32WB_LPTIM_TIMEOUT_MILLIS_TO_TICKS(125), (stm32wb_lptim_timeout_callback_t)ubx_timeout, NULL); // 125ms
}
}

Expand Down Expand Up @@ -3028,7 +3028,7 @@ static void ubx_timeout(void)

ubx_send(device, data);

stm32wb_lptim_timeout_relative(&device->ubx.timeout, STM32WB_LPTIM_TIMEOUT_MILLIS_TO_TICKS(125), (stm32wb_lptim_timeout_callback_t)ubx_timeout, NULL); // 125ms
stm32wb_lptim_timeout_start(&device->ubx.timeout, STM32WB_LPTIM_TIMEOUT_MILLIS_TO_TICKS(125), (stm32wb_lptim_timeout_callback_t)ubx_timeout, NULL); // 125ms
}
}

Expand All @@ -3042,7 +3042,7 @@ static void gnss_send_callback(void)
{
device->command = ~0l;

stm32wb_lptim_timeout_relative(&device->ubx.sleep, STM32WB_LPTIM_TIMEOUT_MILLIS_TO_TICKS(125), (stm32wb_lptim_timeout_callback_t)ubx_sleep, NULL); // 125ms
stm32wb_lptim_timeout_start(&device->ubx.sleep, STM32WB_LPTIM_TIMEOUT_MILLIS_TO_TICKS(125), (stm32wb_lptim_timeout_callback_t)ubx_sleep, NULL); // 125ms
}
else
{
Expand Down
11 changes: 6 additions & 5 deletions libraries/RTC/src/RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ struct RTCClass::RTCAlarm RTCClass::m_alarm = {
.day = 0,
.month = 0,
.year = 0,
.tod = { },
.callback = Callback(),
.work = K_WORK_INIT(RTCClass::alarmNotify, nullptr)
.work = K_WORK_INIT(RTCClass::alarmNotify, nullptr),
.tod = { },
.alarm = STM32WB_RTC_ALARM_INIT()
};

RTCClass::RTCClass() {
Expand Down Expand Up @@ -816,11 +817,11 @@ void RTCClass::alarmStart() {

alarm_seconds = (alarm_seconds + Y2K_TO_GPS_OFFSET) + stm32wb_rtc_get_leap_seconds() - stm32wb_rtc_get_zone() - stm32wb_rtc_get_dst();

stm32wb_rtc_alarm_start(alarm_seconds, alarm_ticks, RTCClass::alarmCallback, NULL);
stm32wb_rtc_alarm_start(&m_alarm.alarm, stm32wb_rtc_time_to_clock(alarm_seconds, alarm_ticks), RTCClass::alarmCallback, NULL);
}

void RTCClass::alarmStop() {
stm32wb_rtc_alarm_stop();
stm32wb_rtc_alarm_stop(&m_alarm.alarm);
}

void RTCClass::alarmEvent(__attribute__((unused)) void *context, __attribute__((unused)) uint32_t events) {
Expand All @@ -829,7 +830,7 @@ void RTCClass::alarmEvent(__attribute__((unused)) void *context, __attribute__((
}
}

void RTCClass::alarmCallback(__attribute__((unused)) void *context) {
void RTCClass::alarmCallback(__attribute__((unused)) void *context, uint64_t reference) {
if (m_alarm.match == RTC_MATCH_YYMMDDHHMMSS) {
m_alarm.match = RTC_MATCH_OFF;
} else {
Expand Down
5 changes: 3 additions & 2 deletions libraries/RTC/src/RTC.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ class RTCClass {
uint8_t day;
uint8_t month;
uint8_t year;
stm32wb_rtc_tod_t tod;
Callback callback;
k_work_t work;
stm32wb_rtc_tod_t tod;
stm32wb_rtc_alarm_t alarm;
} m_alarm;

static void getTod(stm32wb_rtc_tod_t *tod);
Expand All @@ -155,7 +156,7 @@ class RTCClass {
static void alarmSync();
static void alarmStart();
static void alarmStop();
static void alarmCallback(void *context);
static void alarmCallback(void *context, uint64_t reference);
static void alarmNotify(void *context);
static void alarmEvent(void *context, uint32_t events);
};
Expand Down
Loading

0 comments on commit c9865e8

Please sign in to comment.