Skip to content

Commit

Permalink
Added sources draft
Browse files Browse the repository at this point in the history
  • Loading branch information
puzrin committed Oct 28, 2022
1 parent 36fc908 commit b9f2187
Show file tree
Hide file tree
Showing 53 changed files with 5,752 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: CI

on:
push:
pull_request:
schedule:
- cron: '0 0 * * 3'

jobs:
Linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1

- name: Install PlatformIO
run: |
python -m pip install --upgrade pip
pip install -U platformio
platformio update
- name: Build status check
run: |
platformio run -e hw_v1_stm32g030f6
- name: Run tests
run: platformio test -e test_native

macOS:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1

- name: Install PlatformIO
run: |
python -m pip install --upgrade pip
pip install -U platformio
platformio update
- name: Build status check
run: |
platformio run -e hw_v1_stm32g030f6
Windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v1

- name: Install PlatformIO
run: |
python -m pip install --upgrade pip
pip install -U platformio
platformio update
- name: Build status check
run: |
platformio run -e hw_v1_stm32g030f6
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.vscode/settings.json
hal/**/.mxproject
hal/**/Makefile
node_modules
10 changes: 10 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2022 authors

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
40 changes: 40 additions & 0 deletions boards/our_genericSTM32G030F6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"build": {
"core": "stm32",
"cpu": "cortex-m0plus",
"extra_flags": "-DSTM32G0xx -DSTM32G030xx",
"f_cpu": "48000000L",
"framework_extra_flags": {
"arduino": "-D__CORTEX_SC=0"
},
"mcu": "stm32g030f6",
"product_line": "STM32G030xx",
"variant": "STM32G0xx/G030F(4-8)"
},
"debug": {
"jlink_device": "STM32G030F6",
"openocd_target": "stm32g0x",
"svd_path": "STM32G030.svd"
},
"frameworks": [
"arduino",
"cmsis",
"libopencm3",
"stm32cube"
],
"name": "STM32G030F6",
"upload": {
"maximum_ram_size": 8192,
"maximum_size": 32768,
"protocol": "stlink",
"protocols": [
"blackmagic",
"cmsis-dap",
"jlink",
"stlink",
"serial"
]
},
"url": "http://www.st.com/en/microcontrollers/stm32g030f6.html",
"vendor": "ST"
}
110 changes: 110 additions & 0 deletions hal/stm32g030f6/app_hal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "tim.h"
#include "gpio.h"

#include "app_hal.h"
#include "app.h"

extern "C" void SystemClock_Config(void);


namespace hal {

// ADC data is transferred to double size DMA buffer. Interrupts happen on half
// transfer and full transfer. So, we can process received data without risk
// of override. While half of buffer is processed, another half os used to
// collect next data.

static volatile uint16_t ADCBuffer[ADC_FETCH_PER_TICK * ADC_CHANNELS_COUNT * 2];

// Resorted adc data for convenient use
static uint16_t adc_current_buf[ADC_FETCH_PER_TICK];
static uint16_t adc_knob_buf[ADC_FETCH_PER_TICK];

// Split raw ADC data by separate buffers
static void adc_raw_data_load(uint32_t adc_data_offset)
{
for (int sample = 0; sample < ADC_FETCH_PER_TICK; sample++)
{
adc_current_buf[sample] = ADCBuffer[adc_data_offset++];
adc_knob_buf[sample] = ADCBuffer[adc_data_offset++];
}
}

void on_adc_half_transfer_done(ADC_HandleTypeDef* AdcHandle)
{
(void)(AdcHandle);
adc_raw_data_load(0);
io.consume(adc_current_buf, adc_knob_buf);
}

void on_adc_transfer_done(ADC_HandleTypeDef* AdcHandle)
{
(void)(AdcHandle);
adc_raw_data_load(ADC_FETCH_PER_TICK * ADC_CHANNELS_COUNT);
io.consume(adc_current_buf, adc_knob_buf);
}

// PWM period
#define PWM_TIMER_CYCLES 2929

// Set PWM duty cycle, 0..1
void set_power(fix16_t duty_cycle)
{
static fix16_t prev = -1;

// Clamp value
fix16_t val = duty_cycle;
if (val > fix16_one) val = fix16_one;
if (val < 0) val = 0;

// Exit if nothing changed
if (val == prev) return;

__HAL_TIM_SET_COMPARE(
&htim1,
TIM_CHANNEL_1,
uint16_t((val * PWM_TIMER_CYCLES) >> 16)
);
}

// HW init
void setup(void)
{
HAL_Init();
SystemClock_Config();

MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_TIM1_Init();
MX_TIM14_Init();

set_power(0);

HAL_ADC_RegisterCallback(
&hadc1,
HAL_ADC_CONVERSION_HALF_CB_ID,
on_adc_half_transfer_done
);

HAL_ADC_RegisterCallback(
&hadc1,
HAL_ADC_CONVERSION_COMPLETE_CB_ID,
on_adc_transfer_done
);

HAL_ADCEx_Calibration_Start(&hadc1);

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

HAL_ADC_Start_DMA(
&hadc1,
(uint32_t*)ADCBuffer,
ADC_FETCH_PER_TICK * ADC_CHANNELS_COUNT * 2
);
}

}
28 changes: 28 additions & 0 deletions hal/stm32g030f6/app_hal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef __APP_HAL__
#define __APP_HAL__

#include <stdint.h>
#include "libfixmath/fix16.h"
#include "stm32g0xx_hal.h"

#define SAMPLING_RATE 17442

// Oversampling ratio. Used to define buffer sizes
#define ADC_FETCH_PER_TICK 1

// How many channels are sampled "in parallel".
// Used to define global DMA buffer size.
#define ADC_CHANNELS_COUNT 2


#define GET_TIMESTAMP() HAL_GetTick()


namespace hal {

void setup();
void set_power(fix16_t duty_cycle);

} // namespace

#endif
68 changes: 68 additions & 0 deletions hal/stm32g030f6/eeprom_flash_driver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#ifndef __EEPROM_FLASH_DRIVER__
#define __EEPROM_FLASH_DRIVER__

#include "main.h"

// 2K page for stm32f072 => 2K bank => 4K total
#define EEPROM_EMU_BANK_PAGES 1
#define EEPROM_EMU_BANK_SIZE (FLASH_PAGE_SIZE * EEPROM_EMU_BANK_PAGES)
#define EEPROM_EMU_START_PAGE (FLASH_PAGE_NB - EEPROM_EMU_BANK_PAGES * 2)
#define EEPROM_EMU_FLASH_START (FLASH_BASE + (EEPROM_EMU_START_PAGE * FLASH_PAGE_SIZE))

/*
#define XSTR(x) STR(x)
#define STR(x) #x
#pragma message "$$$$$ EEPROM EMU MESSAGE: " XSTR(EEPROM_EMU_START_PAGE)
*/

class EepromFlashDriver
{
uint32_t ee_flash_start_page;
uint32_t ee_flash_start_address;

public:
enum { BankSize = EEPROM_EMU_BANK_SIZE };

EepromFlashDriver()
{
// FLASH_PAGE_NB is dynamic variable.
// Cache constatnt for faster access.
ee_flash_start_page = EEPROM_EMU_START_PAGE;
ee_flash_start_address = EEPROM_EMU_FLASH_START;
}

void erase(uint8_t bank)
{
FLASH_EraseInitTypeDef s_eraseinit;
s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES;
s_eraseinit.Page = ee_flash_start_page + (EEPROM_EMU_BANK_PAGES * bank);
s_eraseinit.NbPages = EEPROM_EMU_BANK_PAGES;
uint32_t page_error = 0;

HAL_FLASH_Unlock();
HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
HAL_FLASH_Lock();
}

FLASH_EE_RECORD read(uint8_t bank, uint32_t addr)
{
uint32_t abs_addr = ee_flash_start_address + bank*EEPROM_EMU_BANK_SIZE + addr;

FLASH_EE_RECORD r;
r.raw32[0] = *((uint32_t *)abs_addr);
r.raw32[1] = *((uint32_t *)(abs_addr + 4));

return r;
}

void write(uint8_t bank, uint32_t addr, FLASH_EE_RECORD r)
{
uint32_t flash_addr = ee_flash_start_address + bank*EEPROM_EMU_BANK_SIZE + addr;

HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_addr, r.raw64);
HAL_FLASH_Lock();
}
};

#endif
Loading

0 comments on commit b9f2187

Please sign in to comment.