main firmware: WIP

very rough WIP of main badge code.

some LED programs are here, and it will build if the btn code is excluded.
This commit is contained in:
true
2023-08-03 18:48:16 -07:00
parent 46a3ab6007
commit 7bbd46fe5f
94 changed files with 40861 additions and 0 deletions

View File

@@ -0,0 +1,178 @@
/*
* adxl.h
*
* Created on: Jul 18, 2023
* Author: true
*/
#ifndef CODE_INC_ADXL_H_
#define CODE_INC_ADXL_H_
#include "hk32f030m.h"
#include "spi.h"
typedef struct adxl345_axes {
int16_t x;
int16_t y;
int16_t z;
} adxl345_axes;
#define ADXL345_SPI_DEV SPI1
#define ADXL345_SPI_CS_PORT GPIOC
#define ADXL345_SPI_CS_PIN GPIO_Pin_4
#define ADXL345_MODE_RD 0x80
#define ADXL345_MODE_WR 0x00
#define ADXL345_MODE_MB 0x40 // multi-byte read or write
#define ADXL345_REG_DEVID 0x00 // read-only, should == 0xE5
#define ADXL345_REG_THRESH_TAP 0x1D // 0xff = 16g
#define ADXL345_REG_OFSX 0x1E // 0x7f = 2g
#define ADXL345_REG_OFSY 0x1F // ""
#define ADXL345_REG_OFSZ 0x20 // ""
#define ADXL345_REG_TAP_DUR 0x21 // 625usec/LSB, 0 disables
#define ADXL345_REG_TAP_LATENTCY 0x22 // 1.25ms/LSB, 0 disables double tap
#define ADXL345_REG_TAP_WINDOW 0x23 // 1.25ms/LSB, 0 disables double tap
#define ADXL345_REG_THRESH_ACT 0x24 // 0xFF = 16g
#define ADXL345_REG_THRESH_INACT 0x25 // 0xFF = 16g
#define ADXL345_REG_TIME_INACT 0x26 // 1sec/LSB
#define ADXL345_REG_ACT_INACT_CTL 0x27
#define ADXL345_REG_THRESH_FF 0x28 // 0xFF = 16g, recommend 0x05-0x09
#define ADXL345_REG_TIME_FF 0x29 // 5ms/LSB, recommend 0x14-0x46
#define ADXL345_REG_TAP_AXES 0x2A
#define ADXL345_REG_ACT_TAP_STATUS 0x2B // read-only
#define ADXL345_REG_BW_RATE 0x2C
#define ADXL345_REG_POWER_CTL 0x2D
#define ADXL345_REG_INT_ENABLE 0x2E
#define ADXL345_REG_INT_MAP 0x2F
#define ADXL345_REG_INT_SOURCE 0x30 // read-only
#define ADXL345_REG_DATA_FORMAT 0x31
#define ADXL345_REG_DATAX0 0x32 // read-only
#define ADXL345_REG_DATAX1 0x33 // read-only
#define ADXL345_REG_DATAY0 0x34 // read-only
#define ADXL345_REG_DATAY1 0x35 // read-only
#define ADXL345_REG_DATAZ0 0x36 // read-only
#define ADXL345_REG_DATAZ1 0x37 // read-only
#define ADXL345_REG_FIFO_CTL 0x38
#define ADXL345_REG_FIFO_STATUS 0x39
#define ADXL345_TAP_AXES_X_ENABLE (1 << 2)
#define ADXL345_TAP_AXES_Y_ENABLE (1 << 1)
#define ADXL345_TAP_AXES_Z_ENABLE (1 << 0)
#define ADXL345_CTL_ACT_AC (1 << 7)
#define ADXL345_CTL_ACT_X_ENA (1 << 6)
#define ADXL345_CTL_ACT_Y_ENA (1 << 5)
#define ADXL345_CTL_ACT_Z_ENA (1 << 4)
#define ADXL345_CTL_INACT_AC (1 << 3)
#define ADXL345_CTL_INACT_X_ENA (1 << 2)
#define ADXL345_CTL_INACT_Y_ENA (1 << 1)
#define ADXL345_CTL_INACT_Z_ENA (1 << 0)
#define ADXL345_ACT_TAP_ACT_X (1 << 6)
#define ADXL345_ACT_TAP_ACT_Y (1 << 5)
#define ADXL345_ACT_TAP_ACT_Z (1 << 4)
#define ADXL345_ACT_TAP_TAP_X (1 << 2)
#define ADXL345_ACT_TAP_TAP_Y (1 << 1)
#define ADXL345_ACT_TAP_TAP_Z (1 << 0)
#define ADXL345_INT_DATA_READY (1 << 7)
#define ADXL345_INT_SINGLE_TAP (1 << 6)
#define ADXL345_INT_DOUBLE_TAP (1 << 5)
#define ADXL345_INT_ACTIVITY (1 << 4)
#define ADXL345_INT_INACTIVITY (1 << 3)
#define ADXL345_INT_FREE_FALL (1 << 2)
#define ADXL345_INT_WATERMARK (1 << 1)
#define ADXL345_INT_OVERRUN (1 << 0)
#define ADXL345_INT_ALL 0xff
#define ADXL345_DFMT_SELF_TEST (1 << 7)
#define ADXL345_DFMT_SPI (1 << 6)
#define ADXL345_DFMT_INT_INVERT (1 << 5)
#define ADXL345_DFMT_FULL_RES (1 << 3)
#define ADXL345_DFMT_JUSTIFY (1 << 2)
#define ADXL345_DFMT_RANGE (3 << 0)
#define ADXL345_INTR_ACTIVE_HI 1
#define ADXL345_INTR_ACTIVE_LO 0
#define ADXL345_BW_RATE_LOW_POWER (1 << 4)
#define ADXL345_BW_RATE_MASK (0xf)
#define ADXL345_BW_RATE_3200 0x0f
#define ADXL345_BW_RATE_1600 0x0e
#define ADXL345_BW_RATE_800 0x0d
#define ADXL345_BW_RATE_400 0x0c // low-power compat
#define ADXL345_BW_RATE_200 0x0b // low-power compat
#define ADXL345_BW_RATE_100 0x0a // low-power compat
#define ADXL345_BW_RATE_50 0x09 // low-power compat
#define ADXL345_BW_RATE_25 0x08 // low-power compat
#define ADXL345_BW_RATE_12_5 0x07 // low-power compat
#define ADXL345_BW_RATE_6_25 0x06
#define ADXL345_BW_RATE_3_13 0x05
#define ADXL345_BW_RATE_1_56 0x04
#define ADXL345_BW_RATE_0_78 0x03
#define ADXL345_BW_RATE_0_39 0x02
#define ADXL345_BW_RATE_0_20 0x01
#define ADXL345_BW_RATE_0_10 0x00
#define ADXL345_POWER_CTL_LINK (1 << 5)
#define ADXL345_POWER_CTL_AUTO_SLP (1 << 4)
#define ADXL345_POWER_CTL_MEASURE (1 << 3)
#define ADXL345_POWER_CTL_SLEEP (1 << 2)
#define ADXL345_POWER_CTL_WAKEUP_MASK (2 << 0)
#define ADXL345_POWER_CTL_WAKEUP_1HZ (0x03)
#define ADXL345_POWER_CTL_WAKEUP_2HZ (0x02)
#define ADXL345_POWER_CTL_WAKEUP_4HZ (0x01)
#define ADXL345_POWER_CTL_WAKEUP_8HZ (0x00)
#define ADXL345_DATA_FORMAT_SELF_TEST (1 << 7)
#define ADXL345_DATA_FORMAT_SPI (1 << 6)
#define ADXL345_DATA_FORMAT_INT_INV (1 << 5)
#define ADXL345_DATA_FORMAT_FULL_RES (1 << 3)
#define ADXL345_DATA_FORMAT_JUSTIFY (1 << 2)
#define ADXL345_DATA_FORMAT_RANGE_MASK (2 << 0)
#define ADXL345_DATA_FORMAT_RANGE_16G (0x03)
#define ADXL345_DATA_FORMAT_RANGE_8G (0x03)
#define ADXL345_DATA_FORMAT_RANGE_4G (0x03)
#define ADXL345_DATA_FORMAT_RANGE_2G (0x03)
// r = receive buffer, t = transmit buffer, s = length
#define adxl_spi_xfer(r, t, s) ADXL345_SPI_CS_PORT->BRR = ADXL345_SPI_CS_PIN; \
spi_mosi_sel(SPI_MOSI_SPI); \
spi_xfer(t, r, s, 0); \
ADXL345_SPI_CS_PORT->BSRR = ADXL345_SPI_CS_PIN;
extern adxl345_axes adxl;
extern uint16_t movement_worst;
void adxl345_init();
void adxl345_tick();
void adxl345_get_axes(struct adxl345_axes *adxl);
int8_t adxl345_get_rotation(struct adxl345_axes *adxl);
int16_t adxl345_movement();
void adxl345_set_reg8(uint8_t reg, uint8_t val);
void adxl345_set_intr(uint8_t int2_map, uint8_t interrupt_flags);
void adxl345_set_intr_polarity(uint8_t active_polarity);
uint8_t adxl345_get_intr_flag();
#endif /* CODE_INC_ADXL_H_ */

View File

@@ -0,0 +1,68 @@
/*
* btn.h
*
* Created on: Aug 3, 2023
* Author: true
*/
#ifndef CODE_INC_BTN_H_
#define CODE_INC_BTN_H_
#define BTN_MODE 0
#define BTN_PROG 1
#define BTN_SET 2
#define BTN_COUNT 3
#define BTN_PUSH (1 << 0)
#define BTN_HELD (1 << 1)
#define BTN_RELEASE (1 << 2)
#define BTN_PUSH_CB (1 << 4)
#define BTN_HELD_CB (1 << 5)
#define BTN_RELEASE_CB (1 << 6)
#define BTN_DEBOUNCE 11 // how many button ticks to wait before registering press
#define BTN_MAX_HOLD (512*30) // longest reported / processed hold time (30s)
#define BTN_HOLD_SHIFT 6 // rshift value to get 1/16th sec hold time
typedef struct Btn_t {
uint16_t state; // current state of button (pushed, held, callback done, etc)
uint16_t held; // current hold counts, in button tickrate
uint16_t hold_thresh; // hold time trigger threshold in 1/16 second increments
uint16_t hold_retrig; // hold time retrigger threshold in button ticks, 0 disables
uint16_t hold_rt_ctr; // hold retrigger counter
void (*push_cb)();
void (*held_cb)();
void (*release_cb)();
} Btn_t;
/*
* as long as button is pushed, .held will be incrementing
* when .held exceeds debounce, a BTN_PUSH event will occur and push_cb will be called
* .held will continue to increment while being held until BTN_MAX_HOLD is reached
* when .hold_thresh is matched, a BTN_HELD event will occur and held_cb will be called
* at this point, .hold_rt_ctr will begin counting with each button tick
* when .hold_rt_ctr matches .hold_retrig, held_cb will be called again, and .hold_rt_ctr will be 0'd
* even after .held stops counting, the .hold_rt_ctr will continue to run
* if .hold_thresh is 0, then no BTN_HELD event will occur, and thus no retriggering either
* if .hold_retrig is 0, then BTN_HELD will occur, but retriggers will not occur
* upon release, BTN_RELEASED event will occur and release_cb will be called
* after finishing release callback, .state will be cleared, .held will be 0
*/
extern Btn_t btn[BTN_COUNT];
void btn_init();
void btn_tick();
void btn_callback();
#endif /* CODE_INC_BTN_H_ */

View File

@@ -0,0 +1,108 @@
/**
******************************************************************************
* @file hk32f030m_conf.h
* @brief configuration file.
******************************************************************************
* @attention
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __HK32F030M_CONF_H
#define __HK32F030M_CONF_H
#ifdef __cplusplus
extern "C" {
#endif
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* ########################## HSE/HSI Values adaptation ##################### */
/**
* @brief Adjust the value of External High Speed oscillator (HSE) used in your application.
* This value is used by the RCC module to compute the system frequency
* (when HSE is used as system clock source, directly or through the PLL).
*/
#define EXTCLK_VALUE ((uint32_t)32000000) /*!< Value of the External oscillator in Hz */
/**
* @brief Internal High Speed oscillator (HSI) value.
* This value is used by the RCC module to compute the system frequency
* (when HSI is used as system clock source, directly or through the PLL).
*/
#define HSI_VALUE ((uint32_t)32000000) /*!< Value of the Internal oscillator in Hz */
/**
* @brief In the following line adjust the Internal High Speed oscillator (HSI) Startup
* Timeout value
*/
#define HSI_STARTUP_TIMEOUT ((uint32_t)0xFFFF) /*!< Time out for start up */
/**
* @brief Internal Low Speed oscillator (LSI) value.
*/
#define LSI_VALUE ((uint32_t)128000)
/*!< Value of the Internal Low Speed oscillator in Hz
The real value may vary depending on the variations */
/* Includes ------------------------------------------------------------------*/
/**
* @brief Include module's header file
*/
#include "hk32f030m_rcc.h"
//#include "hk32f030m_crc.h"
#include "hk32f030m_exti.h"
#include "hk32f030m_flash.h"
#include "hk32f030m_gpio.h"
#include "hk32f030m_misc.h"
//#include "hk32f030m_adc.h"
#include "hk32f030m_syscfg.h"
//#include "hk32f030m_def.h"
//#include "hk32f030m_i2c.h"
//#include "hk32f030m_iwdg.h"
#include "hk32f030m_pwr.h"
#include "hk32f030m_spi.h"
#include "hk32f030m_tim.h"
//#include "hk32f030m_usart.h"
//#include "hk32f030m_iwdg.h"
//#include "hk32f030m_wwdg.h"
//#include "hk32f030m_awu.h"
//#include "hk32f030m_beep.h"
#include "hk32f030m_dbgmcu.h"
/* Exported macro ------------------------------------------------------------*/
/* ########################## Assert Selection ############################## */
/**
* @brief Uncomment the line below to expanse the "assert_param" macro in the
* drivers code
*/
//#define USE_FULL_ASSERT (1U)
#ifdef USE_FULL_ASSERT
/**
* @brief The assert_param macro is used for function's parameters check.
* @param expr: If expr is false, it calls assert_failed function
* which reports the name of the source file and the source
* line number of the call that failed.
* If expr is true, it returns no value.
* @retval None
*/
#define assert_param(expr) ((expr) ? (void)0U : assert_failed((char *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
void assert_failed(char* file, uint32_t line);
#else
#define assert_param(expr) ((void)0U)
#endif /* USE_FULL_ASSERT */
#ifdef __cplusplus
}
#endif
#endif /* __HK32F030M_CONF_H */
/************************ (C) COPYRIGHT MKMcircoChuip *****END OF FILE****/

View File

@@ -0,0 +1,27 @@
/**
******************************************************************************
* @file hk32f030m_it.h
* @brief This file contains the headers of the interrupt handlers.
******************************************************************************
*
******************************************************************************
*/
#ifndef __HK32F030M_IT_H
#define __HK32F030M_IT_H
#ifdef __cplusplus
extern "C" {
#endif
/* Exported functions prototypes ---------------------------------------------*/
void NMI_Handler(void);
void HardFault_Handler(void);
void SVC_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
#ifdef __cplusplus
}
#endif
#endif /* __HK32F030M_IT_H */

View File

@@ -0,0 +1,105 @@
/*
* hsv2rgb.h
*
* Created on: Aug 3, 2023
* Author: true
*/
#ifndef CODE_INC_HSV2RGB_H_
#define CODE_INC_HSV2RGB_H_
#include <stdint.h>
typedef struct color_rgb {
uint8_t r;
uint8_t g;
uint8_t b;
} color_rgb;
typedef struct color_hsv {
uint16_t h;
uint8_t s;
uint8_t v;
} color_hsv;
#define HSV_HUE_SEXTANT 256
#define HSV_HUE_STEPS (6 * HSV_HUE_SEXTANT)
#define HSV_HUE_MIN 0
#define HSV_HUE_MAX (HSV_HUE_STEPS - 1)
#define HSV_SAT_MIN 0
#define HSV_SAT_MAX 255
#define HSV_VAL_MIN 0
#define HSV_VAL_MAX 255
/* Options: */
#define HSV_USE_SEXTANT_TEST /* Limit the hue to 0...360 degrees */
void hsv2rgb_8b(uint16_t h, uint8_t s, uint8_t v, uint8_t *r, uint8_t *g , uint8_t *b);
/*
* Macros that are common to all implementations
*/
#ifdef HSV_USE_SEXTANT_TEST
#define HSV_SEXTANT_TEST(sextant) \
if((sextant) > 5) { \
(sextant) = 5; \
}
#else
#define HSV_SEXTANT_TEST(sextant)
#endif
/*
* Pointer swapping:
* sext. r g b r<>b g<>b r <> g result
* 0 0 0 v u c !u v c u v c
* 0 0 1 d v c d v c
* 0 1 0 c v u u v c u v c
* 0 1 1 c d v v d c d v c d v c
* 1 0 0 u c v u v c u v c
* 1 0 1 v c d v d c d v c d v c
*
* if(sextant & 2)
* r <-> b
*
* if(sextant & 4)
* g <-> b
*
* if(!(sextant & 6) {
* if(!(sextant & 1))
* r <-> g
* } else {
* if(sextant & 1)
* r <-> g
* }
*/
#define HSV_SWAPPTR(a,b) do { uint8_t *tmp = (a); (a) = (b); (b) = tmp; } while(0)
#define HSV_POINTER_SWAP(sextant,r,g,b) \
do { \
if((sextant) & 2) { \
HSV_SWAPPTR((r), (b)); \
} \
if((sextant) & 4) { \
HSV_SWAPPTR((g), (b)); \
} \
if(!((sextant) & 6)) { \
if(!((sextant) & 1)) { \
HSV_SWAPPTR((r), (g)); \
} \
} else { \
if((sextant) & 1) { \
HSV_SWAPPTR((r), (g)); \
} \
} \
} while(0)
#endif /* CODE_INC_HSV2RGB_H_ */

View File

@@ -0,0 +1,17 @@
/*
* i8atan2.h
*
* Created on: Jul 18, 2023
* Author: true
*/
#ifndef CODE_INC_I8ATAN2_H_
#define CODE_INC_I8ATAN2_H_
int8_t i8atan2(int8_t y, int8_t x);
#endif /* CODE_INC_I8ATAN2_H_ */

View File

@@ -0,0 +1,22 @@
/*
* led_prog.h
*
* Created on: Aug 3, 2023
* Author: true
*/
#ifndef CODE_INC_LED_PROG_H_
#define CODE_INC_LED_PROG_H_
extern void (*ledprog)();
void ledprog_default();
void ledprog_change();
#endif /* CODE_INC_LED_PROG_H_ */

View File

@@ -0,0 +1,36 @@
/*
* led_sk6x_spi.h
*
* Created on: Jun 19, 2023
* Author: true
*/
#ifndef CODE_INC_LED_SK6X_SPI_H_
#define CODE_INC_LED_SK6X_SPI_H_
#include <stdint.h>
#define SK6X_LED_MAX_COUNT 8
#define SK6X_BUF_SIZE (((SK6X_LED_MAX_COUNT * 24) * 5) / 8) // always a whole number
#define SK6X_HI 0x10 // 0b000_10000, or 0.25/1.00uS on/off
#define SK6X_LO 0x1c // 0b000_11100, or 0.75/0.50uS on/off
//#define SK6X_HI 0xc0 // 0b11000000, or 0.25/0.75uS on/off
//#define SK6X_LO 0xfc // 0b11111100, or 0.75/0.25uS on/off
void led_sk6x_init();
void led_sk6x_set(uint8_t index, uint8_t r, uint8_t g, uint8_t b);
void led_sk6x_set_all(uint8_t r, uint8_t g, uint8_t b);
void led_sk6x_process();
void led_sk6x_update();
#endif /* CODE_INC_LED_SK6X_SPI_H_ */

View File

@@ -0,0 +1,13 @@
/*
* led_user.h
*
* Created on: Aug 3, 2023
* Author: true
*/
#ifndef CODE_INC_LED_USER_H_
#define CODE_INC_LED_USER_H_
#endif /* CODE_INC_LED_USER_H_ */

View File

@@ -0,0 +1,37 @@
/*
* prng.h
*
* Created on: Aug 3, 2023
* Author: true
*/
#ifndef CODE_INC_PRNG_H_
#define CODE_INC_PRNG_H_
/**
* tinymt32 internal state vector and parameters
*/
typedef struct {
uint32_t status[4];
uint32_t mat1;
uint32_t mat2;
uint32_t tmat;
} tinymt32_t;
extern tinymt32_t tinymt32_s;
void tinymt32_init(tinymt32_t *s, uint32_t seed);
uint32_t tinymt32_get_uint32(tinymt32_t* s);
#define prng_get8() (tinymt32_get_uint32(&tinymt32_s) & 0xff)
#define prng_get16() (tinymt32_get_uint32(&tinymt32_s) & 0xffff)
#define prng_get32() tinymt32_get_uint32(&tinymt32_s)
#endif /* CODE_INC_PRNG_H_ */

View File

@@ -0,0 +1,45 @@
/*
* spi.h
*
* Created on: Jun 19, 2023
* Author: true
*/
#ifndef CODE_INC_SPI_H_
#define CODE_INC_SPI_H_
#include <stdint.h>
#define SPI SPI1
#define SPI_MOSI_NONE 0
#define SPI_MOSI_SPI 1
#define SPI_MOSI_SPI_PORT GPIOD
#define SPI_MOSI_SPI_PIN 2
#define SPI_MOSI_SPI_PINSRC GPIO_PinSource_2
#define SPI_CLK_PORT GPIOD
#define SPI_CLK_PIN 3
#define SPI_MOSI_LED 2
#define SPI_MOSI_LED_PORT GPIOD
#define SPI_MOSI_LED_PIN 4
#define SPI_MOSI_LED_PINSRC GPIO_PinSource_4
#define SPI_NO_CALLBACK (void (*)(void))(0x0)
void spi_init();
void spi_xfer(uint8_t *tx, uint8_t *rx, uint16_t len, void (*cb)(void));
void spi_mosi_sel(uint8_t which);
#endif /* CODE_INC_SPI_H_ */

View File

@@ -0,0 +1,17 @@
/*
* timer.h
*
* Created on: Jun 25, 2023
* Author: true
*/
#ifndef CODE_INC_TIMER_H_
#define CODE_INC_TIMER_H_
void tim6_init();
#endif /* CODE_INC_TIMER_H_ */