Skip to content

Commit

Permalink
add: random 700~900ms timer event, seed from systick
Browse files Browse the repository at this point in the history
  • Loading branch information
luftaquila committed Jul 29, 2024
1 parent 3f59d1c commit a7194db
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
8 changes: 7 additions & 1 deletion device/firmware/Core/Inc/energymeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define BIT_SET(target, pos) ((target) |= (1 << (pos)))
#define BIT_CLEAR(target, pos) ((target) &= ~(1 << (pos)))
#define BIT_TOGGLE(target, pos) ((target) ^= (1 << (pos)))
#define BIT_CHECK(target, pos) ((target) & (1 << (pos)))

/******************************************************************************
* module configuration
Expand All @@ -31,7 +32,6 @@ extern uint8_t error_status;

// max 8 entries
typedef enum {
EEM_NO_ERROR,
EEM_ERR_INVALID_ID,
EEM_ERR_SD_CARD,
EEM_ERR_HARDFAULT,
Expand Down Expand Up @@ -122,6 +122,12 @@ extern TIM_HandleTypeDef htim1;

#define TIMER_100ms htim1

typedef enum {
TIMER_FLAG_100ms,
TIMER_FLAG_random,
TIMER_FLAG_1000ms,
} timer_flag_t;

#define UART_CONS huart1

#define SPI_SD hspi1
Expand Down
60 changes: 58 additions & 2 deletions device/firmware/Core/Src/energymeter.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdint.h>
#include <stdlib.h>

#include "energymeter.h"
#include "ff.h"
Expand All @@ -12,6 +13,9 @@
/* boot time from the start of the month in millisecond */
uint32_t boot_time;

/* random seed from the systick clock */
uint32_t seed = 0;

/* SD card write buffer */
ring_buffer_t sd_buffer;
uint8_t sd_buffer_arr[1 << 10]; // 1KB
Expand All @@ -23,8 +27,11 @@ uint8_t rf_buffer_arr[1 << 10]; // 1KB
/* device id from flash */
uint32_t device_id = DEVICE_ID_INVALID;

/* timer flag */
uint32_t timer_flag = 0;

/* ERROR STATUS */
uint8_t error_status = EEM_NO_ERROR;
uint8_t error_status = 0;

/* USB CDC debug print buffer */
char debug_buffer[MAX_LEN_DEBUG_STR];
Expand Down Expand Up @@ -68,20 +75,69 @@ void mode_energymeter(void) {

if (f_mount(&fat, "", 1) != FR_OK) {
BIT_SET(error_status, EEM_ERR_SD_CARD);
HAL_GPIO_WritePin(LED_SD_GPIO_Port, LED_SD_Pin, GPIO_PIN_RESET);
}

FIL fp;

if (f_open(&fp, path, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) {
BIT_SET(error_status, EEM_ERR_SD_CARD);
HAL_GPIO_WritePin(LED_SD_GPIO_Port, LED_SD_Pin, GPIO_PIN_RESET);
};

/* set random seed from the systick */
srand(SysTick->VAL);

/* start 100ms timer */
HAL_TIM_Base_Start_IT(&TIMER_100ms);

while (1) {
if (BIT_CHECK(timer_flag, TIMER_FLAG_100ms)) {
BIT_CLEAR(timer_flag, TIMER_FLAG_100ms);
}

if (BIT_CHECK(timer_flag, TIMER_FLAG_random)) {
BIT_CLEAR(timer_flag, TIMER_FLAG_random);
}

if (BIT_CHECK(timer_flag, TIMER_FLAG_1000ms)) {
BIT_CLEAR(timer_flag, TIMER_FLAG_1000ms);
}
}
}

/******************************************************************************
* EEM energymeter mode 100ms periodic job
*****************************************************************************/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
static uint32_t counter = 0;
++counter;

BIT_SET(timer_flag, TIMER_FLAG_100ms);

static uint32_t random_counter = 7;

if (counter == random_counter) {
BIT_SET(timer_flag, TIMER_FLAG_random);

// set next random event to 700~900ms
// to reduce prevent collisions from multiple transmitters
// i know rand() is a bad PRNG, but that is not important
random_counter += 7 + rand() % 3;

if (random_counter > 10) {
random_counter -= 10;
}
}

if (counter == 10) {
BIT_SET(timer_flag, TIMER_FLAG_1000ms);
counter = 0;
}

/* flash status led; OK 1 Hz, Error 10 Hz */
if (error_status || !counter) {
HAL_GPIO_TogglePin(LED_STATUS_GPIO_Port, LED_STATUS_Pin);
HAL_Delay(500);
}
}

Expand Down
2 changes: 1 addition & 1 deletion device/firmware/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int main(void)
/* Infinite loop */
/* USER CODE BEGIN WHILE */

// switch mode by USB_SENSE on boot time
// set mode on boot by USB_SENSE status
if (HAL_GPIO_ReadPin(USB_SENSE_GPIO_Port, USB_SENSE_Pin) == GPIO_PIN_RESET) {
mode_energymeter();
} else {
Expand Down
2 changes: 1 addition & 1 deletion device/firmware/FSK-EEM.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Generated by STM32CubeIDE
# Take care that such file, as generated, may be overridden without any early notice. Please have a look to debug launch configuration setup(s)

source [find interface/stlink-v2-1.cfg]
source [find interface/stlink.cfg]

set CPUTAPID 0x2ba01477

Expand Down

0 comments on commit a7194db

Please sign in to comment.