diff --git a/device/firmware/Core/Inc/energymeter.h b/device/firmware/Core/Inc/energymeter.h index 538a985..7840ba7 100644 --- a/device/firmware/Core/Inc/energymeter.h +++ b/device/firmware/Core/Inc/energymeter.h @@ -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 @@ -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, @@ -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 diff --git a/device/firmware/Core/Src/energymeter.c b/device/firmware/Core/Src/energymeter.c index 82da409..fab42bc 100644 --- a/device/firmware/Core/Src/energymeter.c +++ b/device/firmware/Core/Src/energymeter.c @@ -1,4 +1,5 @@ #include +#include #include "energymeter.h" #include "ff.h" @@ -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 @@ -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]; @@ -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); } } diff --git a/device/firmware/Core/Src/main.c b/device/firmware/Core/Src/main.c index 735cb97..e8abc57 100644 --- a/device/firmware/Core/Src/main.c +++ b/device/firmware/Core/Src/main.c @@ -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 { diff --git a/device/firmware/FSK-EEM.cfg b/device/firmware/FSK-EEM.cfg index b263236..bfd3f33 100644 --- a/device/firmware/FSK-EEM.cfg +++ b/device/firmware/FSK-EEM.cfg @@ -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