Skip to content

Commit

Permalink
Restructured code, added GD32 compatibility code (not finished)
Browse files Browse the repository at this point in the history
  • Loading branch information
mressl committed May 10, 2023
1 parent 0028c72 commit 4e54b0c
Show file tree
Hide file tree
Showing 34 changed files with 1,701 additions and 378 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ FILE(GLOB sources platform.io/src/*.c)
FILE(GLOB U8g2Sources platform.io/.pio/libdeps/fs2011stm32/U8g2/src/clib/*.c)
FILE(GLOB mcumaxSources platform.io/lib/mcu-max/*.c)

add_definitions(-DSDL_MODE)
add_definitions(-DSIMULATE_PULSES)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-D SDL_MODE)
add_definitions(-D SIMULATE_PULSES)
add_definitions(-D ERROR_HANDLERS)
add_definitions(-D _CRT_SECURE_NO_WARNINGS)

add_executable(fs2011pro ${sources} ${U8g2Sources} ${mcumaxSources})

Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Watch a demonstration of FS2011 Pro in action:
## Features

* Instantaneous rate, average rate, dose, and history modes.
* True random number generator to generate secure passwords (letters & numbres, full ASCII), random hexadecimal and decimal numbres, 6-sided dice throws, and coin flips.
* True random number generator to generate secure passwords (letters & numbres, full ASCII), random hexadecimal and decimal numbers, coin flips and dice throws (6-sided, 4-sided, 8-sided, 12-sided and 20-sided).
* Intuitive user interface for easy navigation and hassle-free use.
* Measurements in Sievert, rem, cpm and cps.
* Multiple history periods, including 2-minute, 10-minute, 1-hour, 6-hour and 24-hour options.
Expand Down Expand Up @@ -123,13 +123,11 @@ The history is calculated from the instantaneous rate, sampled once per second.

### Random number generator

The random number generator is a tool that generates random numbers for various purposes, such as generating secure passwords, random hexadecimal and decimal numbers, 6-sided dice throws, and coin flips. It works by comparing the period between the last two pulses to the period between the second pulse and the third last pulse, producing one bit for every pulse. The periods are measured with an accuracy of 1 µs.
The random number generator produces one bit for every two new pulses by comparing the interval between them with a precision of 125 ns. The bit is discarded if the intervals are the same length. To prevent bias, every second bit is flipped. The generator stores entropy in a 512-bit buffer.

To ensure high-quality randomness, if the last and previous-to-last periods have the same period counts, the bit is discarded. Moreover, to prevent bias, every second bit is flipped. There is a 512-bit buffer for storing entropy.
Random symbols are generated from the bits using the [Fast Dice Roller](https://arxiv.org/abs/1304.1916) algorithm. "Letters & numbers" consumes approximately 6 bits per symbol, "Full ASCII" consumes 7 bits, "20-sided dice" and "12-sided dice" consumes 5 bits, "Hexadecimal", "Decimal" consumes 4 bits, "8-sided dice" and "6-sided dice" consumes 3 bits, "4-sided dice" consumes 2 bits, and "Coin flip" consumes 1 bit.

The symbols are generated using the [Fast Dice Roller](https://arxiv.org/abs/1304.1916) algorithm. The number of bits required to generate different symbols varies depending on the application: "Letters & numbers" consumes approximately 6 bits per symbol, "Full ASCII" consumes 7 bits, "Hexadecimal" and "Decimal" consume 4 bits, "6-sided dice" consumes 3 bits, and "Coin flip" consumes 1 bit.

To generate bits more quickly, a radioactive source can be used.
To generate bits more quickly, use a radioactive source.

## Building

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"stm32cube",
"zephyr"
],
"name": "FS2011 STM32",
"name": "FS2011",
"upload": {
"maximum_ram_size": 8192,
"maximum_size": 65536,
Expand Down
43 changes: 0 additions & 43 deletions platform.io/boards/fs2011gd32.json

This file was deleted.

6 changes: 3 additions & 3 deletions platform.io/platformio.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[env:fs2011stm32]
[env:fs2011]
platform = ststm32
board = fs2011stm32
board = fs2011
framework = libopencm3

lib_compat_mode = off
lib_deps = olikraus/U8g2@^2.34.17

build_flags = -DCHECK_FIRMWARE
; build_flags = -DERROR_HANDLERS
; build_flags = -DERROR_HANDLERS
; build_flags = -DERROR_HANDLERS -DCHECK_FIRMWARE
10 changes: 4 additions & 6 deletions platform.io/src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
#include "events.h"
#include "main.h"

#ifdef ERROR_HANDLERS

void onHardFault(uint32_t *args);

#define DEBUG_BIT_LENGTH 100
#define DEBUG_BIT0_LENGTH 1
#define DEBUG_BIT1_LENGTH 15

static void debugWait(uint32_t ms)
void debugWait(uint32_t ms)
{
uint32_t n = 200 * ms;

Expand Down Expand Up @@ -63,6 +59,9 @@ void debugUInt32(uint32_t value)
}
}

#ifdef ERROR_HANDLERS
void onHardFault(uint32_t *args);

// args: r0, r1, r2, r3, r12, lr, pc, psr
void onHardFault(uint32_t *args)
{
Expand Down Expand Up @@ -90,5 +89,4 @@ void hard_fault_handler(void)
" ldr r2,=onHardFault \n"
" bx r2 \n");
}

#endif
1 change: 1 addition & 0 deletions platform.io/src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <stdint.h>

void debugWait(uint32_t ms);
void debugBit(bool value);
void debugUInt32(uint32_t value);

Expand Down
12 changes: 8 additions & 4 deletions platform.io/src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,16 @@ void initDisplay(void)
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_GPIOF);

gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
LCD_RESET_PIN | LCD_EN_PIN | LCD_RS_PIN | LCD_RW_PIN | LCD_BACKLIGHT_PIN);
gpio_set_output_options(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_HIGH,
LCD_RESET_PIN | LCD_EN_PIN | LCD_RS_PIN | LCD_RW_PIN | LCD_BACKLIGHT_PIN);
gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
LCD_D0_PIN | LCD_D1_PIN | LCD_D2_PIN | LCD_D3_PIN | LCD_D4_PIN | LCD_D7_PIN);
gpio_set_output_options(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_HIGH,
LCD_D0_PIN | LCD_D1_PIN | LCD_D2_PIN | LCD_D3_PIN | LCD_D4_PIN | LCD_D7_PIN);

gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
LCD_RESET_PIN | LCD_EN_PIN | LCD_RS_PIN | LCD_RW_PIN | LCD_BACKLIGHT_PIN);
gpio_set_output_options(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_HIGH,
LCD_RESET_PIN | LCD_EN_PIN | LCD_RS_PIN | LCD_RW_PIN | LCD_BACKLIGHT_PIN);

gpio_mode_setup(GPIOF, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
LCD_D5_PIN | LCD_D6_PIN);
gpio_set_output_options(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_HIGH,
Expand Down Expand Up @@ -240,6 +242,8 @@ static uint8_t onDisplayByte(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *a
GPIOF_BSRR = (0b01100000 << 17) | ((value & 0b01100000) << 1);

gpio_set(LCD_EN_PORT, LCD_EN_PIN);
asm("nop");
asm("nop");
gpio_clear(LCD_EN_PORT, LCD_EN_PIN);
}

Expand Down
13 changes: 5 additions & 8 deletions platform.io/src/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "settings.h"
#include "ui.h"

struct Events
struct
{
volatile uint32_t sysTickValue;

Expand Down Expand Up @@ -55,6 +55,8 @@ void initEvents(void)
systick_interrupt_enable();
systick_counter_enable();

nvic_set_priority(NVIC_SYSTICK_IRQ, 255);

// Watchdog
iwdg_set_period_ms(500 * 40000 / 32768);
iwdg_start();
Expand All @@ -64,14 +66,9 @@ void initEvents(void)
events.oneSecondTimer = SYS_TICK_FREQUENCY;
}

void enableEvents(void)
{
events.isEventsEnabled = true;
}

void disableEvents(void)
void setEventsEnabled(bool value)
{
events.isEventsEnabled = true;
events.isEventsEnabled = value;
}

static bool isTimerElapsed(volatile int32_t *timer)
Expand Down
3 changes: 1 addition & 2 deletions platform.io/src/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

void initEvents(void);

void enableEvents(void);
void disableEvents(void);
void setEventsEnabled(bool value);

void waitSysTicks(uint32_t value);

Expand Down
9 changes: 5 additions & 4 deletions platform.io/src/fwcheck.c → platform.io/src/firmware.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

#include "buzzer.h"
#include "events.h"
#include "fwcheck.h"
#include "firmware.h"
#include "main.h"

#define FIRMWARE_BASE FLASH_BASE
#define FIRMWARE_SIZE (0x8000 - 0x4)
#define FIRMWARE_SIZE (0xc000 - 0x4)

#define VALID_CRC (*(uint32_t *)(FIRMWARE_BASE + FIRMWARE_SIZE))

void checkFirmware(void)
{
Expand All @@ -25,8 +27,7 @@ void checkFirmware(void)

rcc_periph_clock_disable(RCC_CRC);

uint32_t validCRC = *(uint32_t *)(FIRMWARE_BASE + FIRMWARE_SIZE);
if (crc != validCRC)
if (crc != VALID_CRC)
{
for (uint32_t i = 0; i < 10; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions platform.io/src/fwcheck.h → platform.io/src/firmware.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* License: MIT
*/

#ifndef FWCHECK_H
#define FWCHECK_H
#ifndef FIRMWARE_H
#define FIRMWARE_H

void checkFirmware(void);

Expand Down
37 changes: 8 additions & 29 deletions platform.io/src/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,17 @@
#include "cmath.h"
#include "settings.h"

const char *formatPrefixes = "n\x7fm kMG";

static char getMetricPrefix(int32_t exponent)
{
int32_t metricPrefix = divideDown(exponent, 3);
switch (metricPrefix)
{
case -3:
return 'n';

case -2:
return '\x7f';

case -1:
return 'm';

case 0:
return ' ';

case 1:
return 'k';

case 2:
return 'M';

case 3:
return 'G';

default:
if (metricPrefix < -4)
return '-';
else
return '+';
}
if (metricPrefix < -3)
return '-';
else if (metricPrefix > 3)
return '+';
else
return formatPrefixes[metricPrefix + 3];
}

void strcatNumber(char *buffer, uint64_t value, uint32_t length)
Expand Down
2 changes: 1 addition & 1 deletion platform.io/src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ enum GameState
GAME_UNDO_MOVE,
};

struct Game
struct
{
uint8_t playerIndex;

Expand Down
15 changes: 15 additions & 0 deletions platform.io/src/gd32f1x0.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* FS2011 Pro
* GD32 firmware library fake header
*
* Parts: Copyright (c) 2022, GigaDevice Semiconductor Inc.
*/

#include "gd32f1x0.h"

#define OB_RDP (*((uint32_t *)0x1ffff800))

bool isGD32(void)
{
return (OB_RDP & 0xff) == 0xa5;
}
50 changes: 50 additions & 0 deletions platform.io/src/gd32f1x0.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* FS2011 Pro
* GD32 firmware library fake header
*
* Parts: Copyright (c) 2022, GigaDevice Semiconductor Inc.
*/

#ifndef GD32F1X0_H
#define GD32F1X0_H

#include <stdbool.h>
#include <stdint.h>

#include "main.h"

typedef enum
{
DISABLE = 0,
ENABLE = !DISABLE
} EventStatus,
ControlStatus;

enum
{
FALSE = 0,
TRUE = !FALSE
};

typedef enum
{
RESET = 0,
SET = !RESET
} FlagStatus;

typedef enum
{
ERROR = 0,
SUCCESS = !ERROR
} ErrStatus;

#define REG32(addr) (*(volatile uint32_t *)(uint32_t)(addr))
#define REG16(addr) (*(volatile uint16_t *)(uint32_t)(addr))
#define REG8(addr) (*(volatile uint8_t *)(uint32_t)(addr))
#define BIT(x) ((uint32_t)((uint32_t)0x01U << (x)))
#define BITS(start, end) ((0xFFFFFFFFUL << (start)) & (0xFFFFFFFFUL >> (31U - (uint32_t)(end))))
#define GET_BITS(regval, start, end) (((regval)&BITS((start), (end))) >> (start))

bool isGD32(void);

#endif

0 comments on commit 4e54b0c

Please sign in to comment.