From a4047b23bc3049f75e0d4f6744a19ea040a82f1b Mon Sep 17 00:00:00 2001 From: crinq Date: Thu, 19 Apr 2018 01:21:56 +0200 Subject: [PATCH] stmsp port --- shared/comps/dq_sp.c | 59 +++ shared/comps/map.c | 238 ++++++++++ shared/comps/pole.c | 32 ++ shared/comps/rl.c | 133 ++++++ shared/flashconf.c | 67 +++ stmsp/stm32f303/Makefile | 249 ++++++++++ stmsp/stm32f303/STM32F303CBTx_FLASH.ld | 216 +++++++++ stmsp/stm32f303/inc/adc.h | 92 ++++ stmsp/stm32f303/inc/commandslist.h | 47 ++ stmsp/stm32f303/inc/hal_tbl.h | 270 +++++++++++ stmsp/stm32f303/inc/main.h | 67 +++ stmsp/stm32f303/inc/opamp.h | 92 ++++ stmsp/stm32f303/inc/stm32f3xx_hal_conf.h | 340 ++++++++++++++ stmsp/stm32f303/inc/stm32f3xx_it.h | 63 +++ stmsp/stm32f303/inc/stmsp_f3hw.h | 49 ++ stmsp/stm32f303/inc/usb_device.h | 74 +++ stmsp/stm32f303/inc/usbd_cdc_if.h | 13 + stmsp/stm32f303/inc/usbd_conf.h | 185 ++++++++ stmsp/stm32f303/inc/usbd_desc.h | 114 +++++ stmsp/stm32f303/src/adc.c | 417 +++++++++++++++++ stmsp/stm32f303/src/comps/hv.c | 102 +++++ stmsp/stm32f303/src/comps/io.c | 207 +++++++++ stmsp/stm32f303/src/comps/tle.c | 184 ++++++++ stmsp/stm32f303/src/hal_tbl.c | 312 +++++++++++++ stmsp/stm32f303/src/main.c | 560 +++++++++++++++++++++++ stmsp/stm32f303/src/opamp.c | 186 ++++++++ stmsp/stm32f303/src/stm32f3xx_hal_msp.c | 96 ++++ stmsp/stm32f303/src/stm32f3xx_it.c | 177 +++++++ stmsp/stm32f303/src/usb_device.c | 75 +++ stmsp/stm32f303/src/usbd_cdc_if.c | 170 +++++++ stmsp/stm32f303/src/usbd_conf.c | 494 ++++++++++++++++++++ stmsp/stm32f303/src/usbd_desc.c | 288 ++++++++++++ stmsp/stm32f303/src/version.c | 8 + 33 files changed, 5676 insertions(+) create mode 100644 shared/comps/dq_sp.c create mode 100644 shared/comps/map.c create mode 100644 shared/comps/pole.c create mode 100644 shared/comps/rl.c create mode 100644 shared/flashconf.c create mode 100644 stmsp/stm32f303/Makefile create mode 100644 stmsp/stm32f303/STM32F303CBTx_FLASH.ld create mode 100644 stmsp/stm32f303/inc/adc.h create mode 100644 stmsp/stm32f303/inc/commandslist.h create mode 100644 stmsp/stm32f303/inc/hal_tbl.h create mode 100644 stmsp/stm32f303/inc/main.h create mode 100644 stmsp/stm32f303/inc/opamp.h create mode 100644 stmsp/stm32f303/inc/stm32f3xx_hal_conf.h create mode 100644 stmsp/stm32f303/inc/stm32f3xx_it.h create mode 100644 stmsp/stm32f303/inc/stmsp_f3hw.h create mode 100644 stmsp/stm32f303/inc/usb_device.h create mode 100644 stmsp/stm32f303/inc/usbd_cdc_if.h create mode 100644 stmsp/stm32f303/inc/usbd_conf.h create mode 100644 stmsp/stm32f303/inc/usbd_desc.h create mode 100644 stmsp/stm32f303/src/adc.c create mode 100644 stmsp/stm32f303/src/comps/hv.c create mode 100644 stmsp/stm32f303/src/comps/io.c create mode 100644 stmsp/stm32f303/src/comps/tle.c create mode 100644 stmsp/stm32f303/src/hal_tbl.c create mode 100644 stmsp/stm32f303/src/main.c create mode 100644 stmsp/stm32f303/src/opamp.c create mode 100644 stmsp/stm32f303/src/stm32f3xx_hal_msp.c create mode 100644 stmsp/stm32f303/src/stm32f3xx_it.c create mode 100644 stmsp/stm32f303/src/usb_device.c create mode 100644 stmsp/stm32f303/src/usbd_cdc_if.c create mode 100644 stmsp/stm32f303/src/usbd_conf.c create mode 100644 stmsp/stm32f303/src/usbd_desc.c create mode 100644 stmsp/stm32f303/src/version.c diff --git a/shared/comps/dq_sp.c b/shared/comps/dq_sp.c new file mode 100644 index 00000000..43c969f7 --- /dev/null +++ b/shared/comps/dq_sp.c @@ -0,0 +1,59 @@ +#include "commands.h" +#include "common.h" +#include "hal.h" +#include "math.h" +#include "defines.h" +#include "angle.h" + +HAL_COMP(dq_sp); + +HAL_PIN(mode); + +//rotor position +HAL_PIN(pos); +HAL_PIN(polecount); //1 + +//a,b input +HAL_PIN(a); +HAL_PIN(b); + +//d,q output +HAL_PIN(d); +HAL_PIN(q); + +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct dq_ctx_t * ctx = (struct dq_ctx_t *)ctx_ptr; + struct dq_pin_ctx_t *pins = (struct dq_pin_ctx_t *)pin_ptr; + + //clarke transformation + float a = PIN(a); + float b = PIN(b); + + float p = (int)MAX(PIN(polecount), 1.0); + float pos = PIN(pos) * p; + + float si = 0.0; + float co = 0.0; + sincos_fast(pos, &si, &co); + + //park transformation + float d = a * co + b * si; + float q = -a * si + b * co; + + PIN(d) = d; + PIN(q) = q; +} + +hal_comp_t dq_comp_struct = { + .name = "dq_sp", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct dq_pin_ctx_t) / sizeof(struct hal_pin_inst_t), +}; diff --git a/shared/comps/map.c b/shared/comps/map.c new file mode 100644 index 00000000..1467f7b6 --- /dev/null +++ b/shared/comps/map.c @@ -0,0 +1,238 @@ +#include "commands.h" +#include "hal.h" +#include "math.h" +#include "defines.h" +#include "angle.h" + +#define POLES 50.0 + +HAL_COMP(map); + +HAL_PIN(pos_in); +HAL_PIN(pos_out); +HAL_PIN(pos_out2); +HAL_PIN(start); +HAL_PIN(freq); +HAL_PIN(over); +HAL_PIN(print); +HAL_PIN(state); +HAL_PIN(counter); +HAL_PIN(index); +HAL_PINA(m, 50); + +float interp(float value, float *array, uint32_t array_size) { + value = CLAMP(value, 0.0, 1.0); + array_size = MAX(array_size, 1); + + uint32_t min_i = (uint32_t)(value * array_size) % array_size; + uint32_t max_i = (uint32_t)(min_i + 1.0) % array_size; + + float dy = minus(array[max_i], array[min_i]); + float dx = 1.0 / array_size; + + return (array[min_i] + (value - (float)min_i / array_size) * dy / dx); +} + +float interpd(float value, float *array, uint32_t array_size) { + value = CLAMP(value, 0.0, 1.0); + array_size = MAX(array_size, 1); + + uint32_t min_i = (uint32_t)(value * array_size) % array_size; + uint32_t max_i = (uint32_t)(min_i + 1.0) % array_size; + + float dy = minus(array[max_i], array[min_i]); + float dx = 1.0 / array_size; + + return (dy / dx); +} + +struct map_ctx_t { + float map[(int)POLES]; + float rmap[(int)POLES]; + int state; + int index; + int counter; + float value; + float pos; +}; + +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct map_ctx_t *ctx = (struct map_ctx_t *)ctx_ptr; + struct map_pin_ctx_t *pins = (struct map_pin_ctx_t *)pin_ptr; + PIN(over) = 500.0; + for(int i = 0; i < POLES; i++) { + ctx->map[i] = 0.0; + ctx->rmap[i] = 0.0; + } +} + +static void rt_start(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct map_ctx_t *ctx = (struct map_ctx_t *)ctx_ptr; + struct map_pin_ctx_t *pins = (struct map_pin_ctx_t *)pin_ptr; + + for(int i = 0; i < POLES; i++){ + ctx->rmap[i] = PINA(m, i); + } +} + + +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct map_ctx_t *ctx = (struct map_ctx_t *)ctx_ptr; + struct map_pin_ctx_t *pins = (struct map_pin_ctx_t *)pin_ptr; + + PIN(state) = ctx->state; + PIN(counter) = ctx->counter; + PIN(index) = ctx->index; + + switch(ctx->state) { + case 0: // pipe through + //PIN(pos_out) = PIN(pos_in); + PIN(pos_out) = 0.0; + PIN(pos_out2) = interp(PIN(pos_in) / 2.0 / M_PI + 0.5, ctx->rmap, POLES); + + if(PIN(start) > 0.0) { + PIN(pos_out) = 0.0; + ctx->counter++; + if(ctx->counter > 1000) { + ctx->state = 1; + ctx->index = 0; + ctx->pos = 0.0; + } + } + break; + + case 1: // move motor + + ctx->pos += 2.0 * M_PI * PIN(freq) * period; + PIN(pos_out) = mod(ctx->pos); + + if(ctx->pos >= (float)ctx->index * 2.0 * M_PI / POLES) { + ctx->value = 0.0; + ctx->counter = 0; + ctx->state = 2; + //PIN(pos_out) = mod((float)ctx->index / POLES * 2.0 * M_PI); + } + + if(ctx->index >= POLES) { + ctx->state = 3; + } + break; + + case 2: // measure + ctx->value = PIN(pos_in); // PIN(over); + ctx->counter++; + if(ctx->counter > PIN(over)) { + ctx->map[ctx->index] = mod(ctx->value); + ctx->state = 1; + ctx->index++; + } + break; + + + case 3: // move motor + 1 + ctx->pos += 2.0 * M_PI * PIN(freq) * period; + PIN(pos_out) = mod(ctx->pos); + + if(ctx->pos >= (float)(ctx->index + 1) * 2.0 * M_PI / POLES) { + ctx->state = 4; + } + break; + + case 4: // move motor - + ctx->pos -= 2.0 * M_PI * PIN(freq) * period; + PIN(pos_out) = mod(ctx->pos); + + if(ctx->pos <= (float)ctx->index * 2.0 * M_PI / POLES) { + ctx->value = 0.0; + ctx->counter = 0; + ctx->state = 5; + //PIN(pos_out) = mod((float)ctx->index / POLES * 2.0 * M_PI); + } + + if(ctx->index <= 0) { + ctx->state = 6; + } + break; + + case 5: // measure + ctx->value += minus(PIN(pos_in), ctx->value) / 2.0; // PIN(over); + ctx->counter++; + if(ctx->counter > PIN(over)) { + ctx->map[ctx->index] = mod(ctx->value); + ctx->state = 4; + ctx->index--; + } + break; + + + case 6: // remap + break; + + case 7: // map + PIN(pos_out2) = interp(PIN(pos_in) / 2.0 / M_PI + 0.5, ctx->rmap, POLES); + + if(PIN(start) <= 0.0) { + ctx->state = 0; + } + break; + } +} + + +static void nrt_func(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct map_ctx_t *ctx = (struct map_ctx_t *)ctx_ptr; + struct map_pin_ctx_t *pins = (struct map_pin_ctx_t *)pin_ptr; + + if(PIN(print) > 0.0) { + PIN(print) = 0.0; + printf("\nmap(mot_pos) -> fb_pos\n"); + printf("index, map[index], pos(index)\n"); + for(int i = 0; i < POLES; i++) { + printf("%i, %f, %f\n", i, ctx->map[i], (float)i / POLES * 2.0 * M_PI); + } + printf("\nrmap(fb_pos) -> mot_pos\n"); + printf("index, map[index], pos(index)\n"); + for(int i = 0; i < POLES; i++) { + printf("%i, %f, %f\n", i, ctx->rmap[i], (float)i / POLES * 2.0 * M_PI); + } + } + + if(ctx->state == 6) { // remap + float p = 0.0; + float pp = 0.0; + float error = 0.0; + int j = 0; + + for(int i = 0; i < POLES; i++) { + p = (float)i * 2.0 * M_PI / POLES - M_PI; + j = 0; + do { + pp += error * 0.6 / interpd(pp / 2.0 / M_PI + 0.5, ctx->map, POLES); + pp = mod(pp); + error = minus(p, interp(pp / 2.0 / M_PI + 0.5, ctx->map, POLES)); + j++; + } while(j < 200 && ABS(error) >= 2.0 * M_PI / 32768.0 * 20.0); + //printf("index %u, error %f, it %u\n", i, error, j); + error = 0.0; + + ctx->rmap[i] = pp; + PINA(m, i) = pp; + printf("map0.m%i=%f\n", i, pp); + } + + ctx->state = 7; + } +} + +hal_comp_t map_comp_struct = { + .name = "map", + .nrt = nrt_func, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = rt_start, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct map_ctx_t), + .pin_count = sizeof(struct map_pin_ctx_t) / sizeof(struct hal_pin_inst_t), +}; diff --git a/shared/comps/pole.c b/shared/comps/pole.c new file mode 100644 index 00000000..ffd94b41 --- /dev/null +++ b/shared/comps/pole.c @@ -0,0 +1,32 @@ +#include "commands.h" +#include "hal.h" +#include "math.h" +#include "defines.h" +#include "angle.h" + +HAL_COMP(pole); + +HAL_PIN(pos); +HAL_PIN(cpos); +HAL_PIN(p); + +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct pole_ctx_t * ctx = (struct pole_ctx_t *)ctx_ptr; + struct pole_pin_ctx_t *pins = (struct pole_pin_ctx_t *)pin_ptr; + + PIN(cpos) = mod(PIN(pos) * PIN(p)); +} + +hal_comp_t pole_comp_struct = { + .name = "pole", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = 0, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct pole_pin_ctx_t) / sizeof(struct hal_pin_inst_t), +}; diff --git a/shared/comps/rl.c b/shared/comps/rl.c new file mode 100644 index 00000000..c82567ac --- /dev/null +++ b/shared/comps/rl.c @@ -0,0 +1,133 @@ +#include "commands.h" +#include "hal.h" +#include "math.h" +#include "defines.h" +#include "angle.h" + +HAL_COMP(rl); + +HAL_PIN(ra); +HAL_PIN(rb); +HAL_PIN(ld); +HAL_PIN(lq); +HAL_PIN(la); +HAL_PIN(lb); + +HAL_PIN(max_cur); +HAL_PIN(udc); +HAL_PIN(start); +HAL_PIN(state); + +HAL_PIN(ki); +HAL_PIN(time); +HAL_PIN(t); + +HAL_PIN(ia_fb); +HAL_PIN(ib_fb); +HAL_PIN(ua_fb); +HAL_PIN(ub_fb); +HAL_PIN(ua); +HAL_PIN(ub); + +struct rl_ctx_t{ + uint32_t state; + float error_sum; + float time; + float ra; + float rb; +}; + +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct rl_ctx_t * ctx = (struct rl_ctx_t *)ctx_ptr; + struct rl_pin_ctx_t *pins = (struct rl_pin_ctx_t *)pin_ptr; + + PIN(time) = 1.0; + PIN(ki) = 100.0; + PIN(max_cur) = 1.0; + ctx->ra = 0.0; + ctx->rb = 0.0; + ctx->state = 0; + ctx->time = 0.0; + ctx->error_sum = 0.0; +} + +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + struct rl_ctx_t * ctx = (struct rl_ctx_t *)ctx_ptr; + struct rl_pin_ctx_t *pins = (struct rl_pin_ctx_t *)pin_ptr; + + switch(ctx->state){ + case 0: + if(PIN(start) > 0.0){ + ctx->state = 1; + ctx->error_sum = 0.0; + ctx->time = 0.0; + } + PIN(ua) = 0.0; + PIN(ub) = 0.0; + break; + + case 1: // measure ra + ctx->time += period; + ctx->error_sum += (PIN(max_cur) - PIN(ia_fb)) * period * PIN(ki); + PIN(ua) = ctx->error_sum; + + if(ABS(PIN(ia_fb) > 0.1)){ + ctx->ra = ctx->ra * 0.95 + PIN(ua_fb) / PIN(ia_fb) * 0.05; + } + + if(ctx->time >= PIN(time)){ + ctx->time = 0.0; + ctx->state = 2; + ctx->error_sum = 0.0; + PIN(ua) = 0.0; + PIN(ub) = 0.0; + } + break; + + case 2: // measure rb + ctx->time += period; + ctx->error_sum += (PIN(max_cur) - PIN(ib_fb)) * period * PIN(ki); + PIN(ub) = ctx->error_sum; + + if(ABS(PIN(ib_fb) > 0.1)){ + ctx->rb = ctx->rb * 0.95 + PIN(ub_fb) / PIN(ib_fb) * 0.05; + } + + if(ctx->time >= PIN(time)){ + ctx->time = 0.0; + ctx->state = 3; + ctx->error_sum = 0.0; + PIN(ua) = 0.0; + PIN(ub) = 0.0; + } + break; + + default: + if(PIN(start) <= 0.0){ + ctx->state = 0; + } + ctx->time = 0.0; + PIN(ua) = 0.0; + PIN(ub) = 0.0; + break; + } + + PIN(state) = ctx->state; + PIN(t) = ctx->time; + PIN(ra) = ctx->ra; + PIN(rb) = ctx->rb; +} + +hal_comp_t rl_comp_struct = { + .name = "rl", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct rl_pin_ctx_t) / sizeof(struct hal_pin_inst_t), +}; diff --git a/shared/flashconf.c b/shared/flashconf.c new file mode 100644 index 00000000..7912aaf6 --- /dev/null +++ b/shared/flashconf.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include "commands.h" +#include "stm32f3xx_hal.h" +#include "hal.h" + +char config[1 * 1024]; +const char *config_ro = (char *)0x0801fc00; + +void flashloadconf(char *ptr) { + strncpy(config, config_ro, sizeof(config)); +} +COMMAND("flashloadconf", flashloadconf, "load config from flash"); + +void flashsaveconf(char *ptr) { + printf("erasing flash page...\n"); + HAL_FLASH_Unlock(); + uint32_t PageError = 0; + HAL_StatusTypeDef status; + FLASH_EraseInitTypeDef eraseinitstruct; + eraseinitstruct.TypeErase = FLASH_TYPEERASE_PAGES; + eraseinitstruct.PageAddress = (uint32_t)config_ro; + eraseinitstruct.NbPages = 1; + status = HAL_FLASHEx_Erase(&eraseinitstruct, &PageError); + + if(status != HAL_OK) { + printf("error!\n"); + HAL_FLASH_Lock(); + return; + } + printf("saving conf\n"); + int ret = 0; + for(int i = 0; i < sizeof(config); i += 4) { + uint32_t word = (config[i + 0]) + (config[i + 1] << 8) + (config[i + 2] << 16) + (config[i + 3] << 24); + ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)config_ro + i, word) != HAL_OK; + if(ret) { + printf("error writing %i\n", ret); + break; + } + } + printf("OK %i bytes written\n", sizeof(config)); + HAL_FLASH_Lock(); +} +COMMAND("flashsaveconf", flashsaveconf, "save config to flash"); + +void loadconf(char *ptr) { + hal_parse(config); +} +COMMAND("loadconf", loadconf, "parse config"); + +void showconf(char *ptr) { + printf("%s", config_ro); +} +COMMAND("showconf", showconf, "show config"); + +void appendconf(char *ptr) { + printf("adding %s\n", ptr); + strncat(config, ptr, sizeof(config) - 1); + strncat(config, "\n", sizeof(config) - 1); +} +COMMAND("appendconf", appendconf, "append string to config"); + +void deleteconf(char *ptr) { + config[0] = '\0'; +} +COMMAND("deleteconf", deleteconf, "delete config"); diff --git a/stmsp/stm32f303/Makefile b/stmsp/stm32f303/Makefile new file mode 100644 index 00000000..e349bef0 --- /dev/null +++ b/stmsp/stm32f303/Makefile @@ -0,0 +1,249 @@ +# Optimization level, can be [0, 1, 2, 3, s]. +# 0 = turn off optimization. s = optimize for size. +# +OPT = -O3 +# OPT = -O1 # for debugging + +# Object files directory +# Warning: this will be removed by make clean! +# +OBJDIR = obj_stmspf3 + +# Target file name (without extension) +TARGET = $(OBJDIR)/stmspf3 + +# Define all C source files (dependencies are generated automatically) +INCDIRS += stmsp/stm32f303/inc +SOURCES += stmsp/stm32f303/src/main.c +SOURCES += stmsp/stm32f303/src/adc.c +SOURCES += stmsp/stm32f303/src/opamp.c +SOURCES += stmsp/stm32f303/src/stm32f3xx_hal_msp.c +SOURCES += stmsp/stm32f303/src/stm32f3xx_it.c +SOURCES += stmsp/stm32f303/src/usb_device.c +SOURCES += stmsp/stm32f303/src/usbd_cdc_if.c +SOURCES += stmsp/stm32f303/src/usbd_conf.c +SOURCES += stmsp/stm32f303/src/usbd_desc.c +SOURCES += stmsp/stm32f303/src/version.c + +SOURCES += stmsp/stm32f303/src/hal_tbl.c + +CFLAGS += -DHAL_MAX_CTX=1024 +COMPS += stmsp/stm32f303/src/comps/hv.c +COMPS += stmsp/stm32f303/src/comps/io.c +COMPS += stmsp/stm32f303/src/comps/tle.c + +COMPS += shared/comps/sim.c +COMPS += shared/comps/term.c +COMPS += shared/comps/curpid.c +COMPS += shared/comps/svm.c +COMPS += shared/comps/dq.c +COMPS += shared/comps/idq.c +COMPS += shared/comps/pole.c +COMPS += shared/comps/map.c +COMPS += shared/comps/vel.c +# COMPS += shared/comps/trc.c +COMPS += shared/comps/rl.c +COMPS += shared/comps/ypid.c + +SOURCES += $(COMPS) + +INCDIRS += shared +SOURCES += shared/ringbuf.c +SOURCES += shared/crc8.c +SOURCES += shared/angle.c +SOURCES += shared/hal.c +SOURCES += shared/commands.c +SOURCES += shared/flashconf.c + +#USB Driver +USB_DEVICE_DIR = lib/STM32_USB_Device_Library-2.4.2 + +#USB class +INCDIRS += $(USB_DEVICE_DIR)/Class/CDC/Inc/ +SOURCES += $(USB_DEVICE_DIR)/Class/CDC/Src/usbd_cdc.c + +#USB core +INCDIRS += $(USB_DEVICE_DIR)/Core/Inc/ +SOURCES += $(USB_DEVICE_DIR)/Core/Src/usbd_core.c +SOURCES += $(USB_DEVICE_DIR)/Core/Src/usbd_ctlreq.c +SOURCES += $(USB_DEVICE_DIR)/Core/Src/usbd_ioreq.c + +#CMSIS +CPPFLAGS += -DSTM32F303xC +INCDIRS += lib/CMSIS/Include/ +INCDIRS += lib/CMSIS/Device/ST/STM32F3xx/Include/ +SOURCES += lib/CMSIS/Device/ST/STM32F3xx/Source/Templates/gcc/startup_stm32f303xc.s +SOURCES += lib/CMSIS/Device/ST/STM32F3xx/Source/Templates/system_stm32f3xx.c + +#stm32f3 HAL Driver +HAL_DRV_DIR = lib/STM32F3xx_HAL_Driver/ + +INCDIRS += $(HAL_DRV_DIR)/Inc +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_adc.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_adc_ex.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_cortex.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_crc.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_crc_ex.c +# SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_dac.c +# SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_dac_ex.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_gpio.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_opamp.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_opamp_ex.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_rcc.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_rcc_ex.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_tim.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_tim_ex.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_uart.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_uart_ex.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_spi.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_spi_ex.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_flash.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_flash_ex.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_pcd.c +SOURCES += $(HAL_DRV_DIR)/Src/stm32f3xx_hal_pcd_ex.c + +LDSCRIPT = stmsp/stm32f303/STM32F303CBTx_FLASH.ld + +#============================================================================ +OBJECTS += $(addprefix $(OBJDIR)/,$(addsuffix .o,$(basename $(SOURCES)))) +CPPFLAGS += $(addprefix -I,$(INCDIRS)) + +#---------------- Preprocessor Options ---------------- +# -fsingle... make better use of the single-precision FPU +# -g generate debugging information +# -save-temps preserve .s and .i-files +# +#CPPFLAGS += +# CPPFLAGS += -g +# CPPFLAGS += -save-temps=obj + +#---------------- C Compiler Options ---------------- +# -O* optimization level +# -f... tuning, see GCC documentation +# -Wall... warning level +# +CFLAGS += $(OPT) +CFLAGS += -std=gnu11 +CFLAGS += -ffunction-sections +CFLAGS += -fdata-sections +CFLAGS += -Wall +CFLAGS += -fno-builtin ## from old +CFLAGS += -nostartfiles +CFLAGS += -Wfatal-errors +CFLAGS += -Wno-pointer-sign #for usb lib... +#CFLAGS += -Wdouble-promotion +CFLAGS += -Wfloat-conversion +CFLAGS += -fsingle-precision-constant +# CFLAGS += -ffast-math +CFLAGS += -ffinite-math-only +CFLAGS += -fno-trapping-math +CFLAGS += -fno-signaling-nans +CFLAGS += -fno-rounding-math +CFLAGS += -fno-signed-zeros +CFLAGS += -fno-math-errno +#CFLAGS += -Wstrict-prototypes +#CFLAGS += -Wextra +#CFLAGS += -Wpointer-arith +#CFLAGS += -Winline +#CFLAGS += -Wunreachable-code +#CFLAGS += -Wundef + +# Use a friendly C dialect +CPPFLAGS += -fno-strict-aliasing +CPPFLAGS += -fwrapv + +#---------------- C++ Compiler Options ---------------- +# +CXXFLAGS += $(OPT) +CXXFLAGS += -ffunction-sections +CXXFLAGS += -fdata-sections +CXXFLAGS += -Wall + +#---------------- Assembler Options ---------------- +# -Wa,... tell GCC to pass this to the assembler +# + +#---------------- Linker Options ---------------- +# -Wl,... tell GCC to pass this to linker +# -Map create map file +# --cref add cross reference to map file +# +LDFLAGS += $(OPT) +LDFLAGS += -lm +LDFLAGS += -Wl,-Map=$(TARGET).map,--cref +LDFLAGS += -Wl,--gc-sections + +# LDFLAGS += -specs=nano.specs -u _printf_float -u _scanf_float +LDFLAGS += -lc -specs=nosys.specs #fixes sbrk missing? present in eclipse? +LDFLAGS += -T$(LDSCRIPT) + +#============================================================================ + +POSTLD = $(PYTHON) tools/add_version_info.py # -q + +# Compiler flags to generate dependency files +# +GENDEPFLAGS = -MMD -MP + +# Combine all necessary flags and optional flags +# Add target processor to flags. +# +CPU = -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 + +CFLAGS += $(CPU) +CXXFLAGS += $(CPU) +ASFLAGS += $(CPU) +LDFLAGS += $(CPU) + +ADDRESS = 0x8004000 + +# Default target +# +all: gccversion build showsize + +build: tbl elf hex bin lss sym + +elf: $(TARGET).elf +hex: $(TARGET).hex +bin: tbl $(TARGET).bin +lss: $(TARGET).lss +sym: $(TARGET).sym + +# Display compiler version information +# + +stmsp/stm32f303/inc/commandslist.h: tbl +stmsp/stm32f303/inc/hal_tbl.h: tbl +stmsp/stm32f303/src/hal_tbl.c: tbl + +tbl: + @echo Generating tables + @$(PYTHON) tools/create_hal_tbl.py stmsp/stm32f303/ $(COMPS) + #shared/comps/*.c src/comps/hw/*.c src/comps/*.c + @$(PYTHON) tools/create_cmd.py $(SOURCES) > stmsp/stm32f303/inc/commandslist.h + +# Target: clean project +# +clean: + @echo Cleaning project: + rm -rf $(OBJDIR) + rm -rf stmsp/stm32f303/inc/commandslist.h + rm -rf stmsp/stm32f303/inc/hal_tbl.h + rm -rf stmsp/stm32f303/src/hal_tbl.c + rm -rf hv_firmware.o + +# Include the base rules +# +include base.mak + +# Include the dependency files +# +-include $(OBJECTS:.o=.d) + +# Listing of phony targets +# +.PHONY: all build flash clean \ + boot boot_clean boot_flash btburn boot_btflash boot_flash\ + elf lss sym \ + showsize gccversion tbl diff --git a/stmsp/stm32f303/STM32F303CBTx_FLASH.ld b/stmsp/stm32f303/STM32F303CBTx_FLASH.ld new file mode 100644 index 00000000..c8876fea --- /dev/null +++ b/stmsp/stm32f303/STM32F303CBTx_FLASH.ld @@ -0,0 +1,216 @@ +/* +***************************************************************************** +** + +** File : LinkerScript.ld +** +** Abstract : Linker script for STM32F303CBTx Device with +** 128KByte FLASH, 32KByte RAM +** +** Set heap size, stack size and stack location according +** to application requirements. +** +** Set memory bank area and size if external memory is used. +** +** Target : STMicroelectronics STM32 +** +** +** Distribution: The file is distributed as is, without any warranty +** of any kind. +** +***************************************************************************** +** @attention +** +**

© COPYRIGHT(c) 2014 Ac6

+** +** Redistribution and use in source and binary forms, with or without modification, +** are permitted provided that the following conditions are met: +** 1. Redistributions of source code must retain the above copyright notice, +** this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright notice, +** this list of conditions and the following disclaimer in the documentation +** and/or other materials provided with the distribution. +** 3. Neither the name of Ac6 nor the names of its contributors +** may be used to endorse or promote products derived from this software +** without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +** +***************************************************************************** +*/ + +/* Entry Point */ +ENTRY(Reset_Handler) + +/* Highest address of the user mode stack */ +_estack = 0x20008000; /* end of RAM */ +/* Generate a link error if heap and stack don't fit into RAM */ +_Min_Heap_Size = 0x200; /* required amount of heap */ +_Min_Stack_Size = 0x800; /* required amount of stack */ + +/* Specify the memory areas */ +MEMORY +{ +RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K +CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 8K +FLASH (rx) : ORIGIN = 0x8004000, LENGTH = 111K +} + +/* Define output sections */ +SECTIONS +{ + /* The startup code goes first into FLASH */ + .isr_vector : + { + . = ALIGN(4); + KEEP(*(.isr_vector)) /* Startup code */ + . = ALIGN(4); + } >FLASH + + /* version info needs to have a section, so the bootloader and python script can find it */ + .version_info : + { + . = ALIGN(4); + KEEP(*(.version_info)) + . = ALIGN(4); + } >FLASH + + /* The program code and other data goes into FLASH */ + .text : + { + . = ALIGN(4); + *(.text) /* .text sections (code) */ + *(.text*) /* .text* sections (code) */ + *(.glue_7) /* glue arm to thumb code */ + *(.glue_7t) /* glue thumb to arm code */ + *(.eh_frame) + + KEEP (*(.init)) + KEEP (*(.fini)) + + . = ALIGN(4); + _etext = .; /* define a global symbols at end of code */ + } >FLASH + + /* Constant data goes into FLASH */ + .rodata : + { + . = ALIGN(4); + *(.rodata) /* .rodata sections (constants, strings, etc.) */ + *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ + . = ALIGN(4); + } >FLASH + + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH + .ARM : { + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; + } >FLASH + + .preinit_array : + { + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP (*(.preinit_array*)) + PROVIDE_HIDDEN (__preinit_array_end = .); + } >FLASH + .init_array : + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array*)) + PROVIDE_HIDDEN (__init_array_end = .); + } >FLASH + .fini_array : + { + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP (*(SORT(.fini_array.*))) + KEEP (*(.fini_array*)) + PROVIDE_HIDDEN (__fini_array_end = .); + } >FLASH + + /* used by the startup to initialize data */ + _sidata = LOADADDR(.data); + + /* Initialized data sections goes into RAM, load LMA copy after code */ + .data : + { + . = ALIGN(4); + _sdata = .; /* create a global symbol at data start */ + *(.data) /* .data sections */ + *(.data*) /* .data* sections */ + + . = ALIGN(4); + _edata = .; /* define a global symbol at data end */ + } >RAM AT> FLASH + + _siccmram = LOADADDR(.ccmram); + + /* CCM-RAM section + * + * IMPORTANT NOTE! + * If initialized variables will be placed in this section, + * the startup code needs to be modified to copy the init-values. + */ + .ccmram : + { + . = ALIGN(4); + _sccmram = .; /* create a global symbol at ccmram start */ + *(.ccmram) + *(.ccmram*) + + . = ALIGN(4); + _eccmram = .; /* create a global symbol at ccmram end */ + } >CCMRAM AT> FLASH + + + /* Uninitialized data section */ + . = ALIGN(4); + .bss : + { + /* This is used by the startup in order to initialize the .bss secion */ + _sbss = .; /* define a global symbol at bss start */ + __bss_start__ = _sbss; + *(.bss) + *(.bss*) + *(COMMON) + + . = ALIGN(4); + _ebss = .; /* define a global symbol at bss end */ + __bss_end__ = _ebss; + } >RAM + + /* User_heap_stack section, used to check that there is enough RAM left */ + ._user_heap_stack : + { + . = ALIGN(8); + PROVIDE ( end = . ); + PROVIDE ( _end = . ); + . = . + _Min_Heap_Size; + . = . + _Min_Stack_Size; + . = ALIGN(8); + } >RAM + + + + /* Remove information from the standard libraries */ + /DISCARD/ : + { + libc.a ( * ) + libm.a ( * ) + libgcc.a ( * ) + } + + .ARM.attributes 0 : { *(.ARM.attributes) } +} + + diff --git a/stmsp/stm32f303/inc/adc.h b/stmsp/stm32f303/inc/adc.h new file mode 100644 index 00000000..e216970a --- /dev/null +++ b/stmsp/stm32f303/inc/adc.h @@ -0,0 +1,92 @@ +/** + ****************************************************************************** + * File Name : ADC.h + * Description : This file provides code for the configuration + * of the ADC instances. + ****************************************************************************** + * + * Copyright (c) 2016 STMicroelectronics International N.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted, provided that the following conditions are met: + * + * 1. Redistribution of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific written permission. + * 4. This software, including modifications and/or derivative works of this + * software, must execute solely and exclusively on microcontroller or + * microprocessor devices manufactured by or for STMicroelectronics. + * 5. Redistribution and use of this software other than as permitted under + * this license is void and will automatically terminate your rights under + * this license. + * + * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY + * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT + * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __adc_H +#define __adc_H +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f3xx_hal.h" +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern ADC_HandleTypeDef hadc1; +extern ADC_HandleTypeDef hadc2; +extern ADC_HandleTypeDef hadc3; +extern ADC_HandleTypeDef hadc4; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +extern void Error_Handler(void); + +void MX_ADC1_Init(void); +void MX_ADC2_Init(void); +void MX_ADC3_Init(void); +void MX_ADC4_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif +#endif /*__ adc_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/inc/commandslist.h b/stmsp/stm32f303/inc/commandslist.h new file mode 100644 index 00000000..73e40157 --- /dev/null +++ b/stmsp/stm32f303/inc/commandslist.h @@ -0,0 +1,47 @@ +//generated by tools/create_cmd.py DO NOT EDIT +extern void about(char *); // found in stmsp/stm32f303/src/main.c line: 188 +extern void bootloader(char *); // found in stmsp/stm32f303/src/main.c line: 203 +extern void reset(char *); // found in stmsp/stm32f303/src/main.c line: 208 +extern void hal_term_print_info(char *); // found in shared/hal.c line: 277 +extern void load(char *); // found in shared/hal.c line: 550 +extern void show(char *); // found in shared/hal.c line: 565 +extern void list(char *); // found in shared/hal.c line: 595 +extern void show_hal(char *); // found in shared/hal.c line: 605 +extern void hal_start(char *); // found in shared/hal.c line: 713 +extern void hal_stop(char *); // found in shared/hal.c line: 740 +extern void fault(char *); // found in shared/hal.c line: 987 +extern void debug_level(char *); // found in shared/hal.c line: 1002 +extern void hal_linked_pins(char *); // found in shared/hal.c line: 1027 +extern void listcmd(char *); // found in shared/commands.c line: 36 +extern void uptime(char *); // found in shared/commands.c line: 47 +extern void flashloadconf(char *); // found in shared/flashconf.c line: 14 +extern void flashsaveconf(char *); // found in shared/flashconf.c line: 45 +extern void loadconf(char *); // found in shared/flashconf.c line: 50 +extern void showconf(char *); // found in shared/flashconf.c line: 55 +extern void appendconf(char *); // found in shared/flashconf.c line: 62 +extern void deleteconf(char *); // found in shared/flashconf.c line: 67 + + +cmd_t cmd[] = { + {"about", "show system infos", about}, // found in stmsp/stm32f303/src/main.c line: 188 + {"bootloader", "enter bootloader", bootloader}, // found in stmsp/stm32f303/src/main.c line: 203 + {"reset", "reset STMBL", reset}, // found in stmsp/stm32f303/src/main.c line: 208 + {"hal", "print HAL stats", hal_term_print_info}, // found in shared/hal.c line: 277 + {"load", "load comp from flash", load}, // found in shared/hal.c line: 550 + {"show", "show comps in flash", show}, // found in shared/hal.c line: 565 + {"list", "show comp instances", list}, // found in shared/hal.c line: 595 + {"show_hal", "show hal structure", show_hal}, // found in shared/hal.c line: 605 + {"start", "start rt system", hal_start}, // found in shared/hal.c line: 713 + {"stop", "stop rt system", hal_stop}, // found in shared/hal.c line: 740 + {"fault", "trigger fault", fault}, // found in shared/hal.c line: 987 + {"debug_level", "set hal debug level, 0 = print all, 1 = print errors, 2 = no output", debug_level}, // found in shared/hal.c line: 1002 + {"linked", "show linked pins", hal_linked_pins}, // found in shared/hal.c line: 1027 + {"help", "print this", listcmd}, // found in shared/commands.c line: 36 + {"uptime", "display uptime", uptime}, // found in shared/commands.c line: 47 + {"flashloadconf", "load config from flash", flashloadconf}, // found in shared/flashconf.c line: 14 + {"flashsaveconf", "save config to flash", flashsaveconf}, // found in shared/flashconf.c line: 45 + {"loadconf", "parse config", loadconf}, // found in shared/flashconf.c line: 50 + {"showconf", "show config", showconf}, // found in shared/flashconf.c line: 55 + {"appendconf", "append string to config", appendconf}, // found in shared/flashconf.c line: 62 + {"deleteconf", "delete config", deleteconf}, // found in shared/flashconf.c line: 67 +}; diff --git a/stmsp/stm32f303/inc/hal_tbl.h b/stmsp/stm32f303/inc/hal_tbl.h new file mode 100644 index 00000000..d5d99b00 --- /dev/null +++ b/stmsp/stm32f303/inc/hal_tbl.h @@ -0,0 +1,270 @@ +#pragma once +//generated by tools/create_hal_tbl.py DO NOT EDIT + +struct pin_ctx_t{ + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; +}; + +struct hv_pin_ctx_t{ // found in stmsp/stm32f303/src/comps/hv.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t a; + hal_pin_inst_t b; + hal_pin_inst_t a_fb; + hal_pin_inst_t b_fb; + hal_pin_inst_t udc; + hal_pin_inst_t hv_temp; + hal_pin_inst_t en; + hal_pin_inst_t ena; + hal_pin_inst_t enb; + hal_pin_inst_t fault; + hal_pin_inst_t min_on; + hal_pin_inst_t min_off; +}; + +struct io_pin_ctx_t{ // found in stmsp/stm32f303/src/comps/io.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t led; + hal_pin_inst_t oc1; + hal_pin_inst_t oc2; + hal_pin_inst_t ena; + hal_pin_inst_t enb; + hal_pin_inst_t hv_temp; + hal_pin_inst_t dc_link; + hal_pin_inst_t bemf0; + hal_pin_inst_t bemf1; + hal_pin_inst_t in0; + hal_pin_inst_t in1; + hal_pin_inst_t iap; + hal_pin_inst_t ian; + hal_pin_inst_t ibp; + hal_pin_inst_t ibn; + hal_pin_inst_t ip; + hal_pin_inst_t in; + hal_pin_inst_t ia; + hal_pin_inst_t ib; +}; + +struct tle_pin_ctx_t{ // found in stmsp/stm32f303/src/comps/tle.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t pos; + hal_pin_inst_t error; +}; + +struct sim_pin_ctx_t{ // found in shared/comps/sim.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t amp; + hal_pin_inst_t freq; + hal_pin_inst_t sin; + hal_pin_inst_t msin; + hal_pin_inst_t sin2; + hal_pin_inst_t msin2; + hal_pin_inst_t sin3; + hal_pin_inst_t msin3; + hal_pin_inst_t square; + hal_pin_inst_t vel; + hal_pin_inst_t res; + hal_pin_inst_t offset; +}; + +struct term_pin_ctx_t{ // found in shared/comps/term.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t wave[8]; + hal_pin_inst_t offset[8]; + hal_pin_inst_t gain[8]; + hal_pin_inst_t send_step; + hal_pin_inst_t con; +}; + +struct curpid_pin_ctx_t{ // found in shared/comps/curpid.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t en; + hal_pin_inst_t cmd_mode; + hal_pin_inst_t id_cmd; + hal_pin_inst_t iq_cmd; + hal_pin_inst_t id_fb; + hal_pin_inst_t iq_fb; + hal_pin_inst_t ac_current; + hal_pin_inst_t ud; + hal_pin_inst_t uq; + hal_pin_inst_t max_cur; + hal_pin_inst_t pwm_volt; + hal_pin_inst_t rd; + hal_pin_inst_t rq; + hal_pin_inst_t ld; + hal_pin_inst_t lq; + hal_pin_inst_t psi; + hal_pin_inst_t ff; + hal_pin_inst_t kp; + hal_pin_inst_t ki; + hal_pin_inst_t kind; + hal_pin_inst_t vel; + hal_pin_inst_t id_error; + hal_pin_inst_t iq_error; +}; + +struct svm_pin_ctx_t{ // found in shared/comps/svm.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t u; + hal_pin_inst_t v; + hal_pin_inst_t w; + hal_pin_inst_t udc; + hal_pin_inst_t su; + hal_pin_inst_t sv; + hal_pin_inst_t sw; + hal_pin_inst_t cmode; + hal_pin_inst_t mode; + hal_pin_inst_t enu; + hal_pin_inst_t env; + hal_pin_inst_t enw; +}; + +struct dq_pin_ctx_t{ // found in shared/comps/dq.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t mode; + hal_pin_inst_t u; + hal_pin_inst_t v; + hal_pin_inst_t w; + hal_pin_inst_t pos; + hal_pin_inst_t polecount; + hal_pin_inst_t a; + hal_pin_inst_t b; + hal_pin_inst_t y; + hal_pin_inst_t d; + hal_pin_inst_t q; +}; + +struct idq_pin_ctx_t{ // found in shared/comps/idq.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t mode; + hal_pin_inst_t d; + hal_pin_inst_t q; + hal_pin_inst_t pos; + hal_pin_inst_t polecount; + hal_pin_inst_t a; + hal_pin_inst_t b; + hal_pin_inst_t u; + hal_pin_inst_t v; + hal_pin_inst_t w; +}; + +struct pole_pin_ctx_t{ // found in shared/comps/pole.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t pos; + hal_pin_inst_t cpos; + hal_pin_inst_t p; +}; + +struct map_pin_ctx_t{ // found in shared/comps/map.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t pos_in; + hal_pin_inst_t pos_out; + hal_pin_inst_t pos_out2; + hal_pin_inst_t start; + hal_pin_inst_t freq; + hal_pin_inst_t over; + hal_pin_inst_t print; + hal_pin_inst_t state; + hal_pin_inst_t counter; + hal_pin_inst_t index; + hal_pin_inst_t m[50]; +}; + +struct vel_pin_ctx_t{ // found in shared/comps/vel.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t pos_in; + hal_pin_inst_t pos_out; + hal_pin_inst_t vel; + hal_pin_inst_t acc; + hal_pin_inst_t w; + hal_pin_inst_t d; + hal_pin_inst_t g; + hal_pin_inst_t h; + hal_pin_inst_t j; + hal_pin_inst_t lp; + hal_pin_inst_t torque; + hal_pin_inst_t vel_ff; + hal_pin_inst_t en; + hal_pin_inst_t pos_error; +}; + +struct rl_pin_ctx_t{ // found in shared/comps/rl.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t ra; + hal_pin_inst_t rb; + hal_pin_inst_t ld; + hal_pin_inst_t lq; + hal_pin_inst_t la; + hal_pin_inst_t lb; + hal_pin_inst_t max_cur; + hal_pin_inst_t udc; + hal_pin_inst_t start; + hal_pin_inst_t state; + hal_pin_inst_t ki; + hal_pin_inst_t time; + hal_pin_inst_t t; + hal_pin_inst_t ia_fb; + hal_pin_inst_t ib_fb; + hal_pin_inst_t ua_fb; + hal_pin_inst_t ub_fb; + hal_pin_inst_t ua; + hal_pin_inst_t ub; +}; + +struct ypid_pin_ctx_t{ // found in shared/comps/ypid.c + hal_pin_inst_t rt_prio; + hal_pin_inst_t frt_prio; + hal_pin_inst_t pos_ext_cmd; + hal_pin_inst_t pos_fb; + hal_pin_inst_t pos_error; + hal_pin_inst_t vel_ext_cmd; + hal_pin_inst_t vel_fb; + hal_pin_inst_t vel_cmd; + hal_pin_inst_t vel_error; + hal_pin_inst_t vel_min; + hal_pin_inst_t enable; + hal_pin_inst_t out; + hal_pin_inst_t pos_p; + hal_pin_inst_t vel_p; + hal_pin_inst_t vel_i; + hal_pin_inst_t vel_ff; + hal_pin_inst_t max_vel; + hal_pin_inst_t max_acc; + hal_pin_inst_t max_out; + hal_pin_inst_t vel_sat; + hal_pin_inst_t out_sat; + hal_pin_inst_t saturated; +}; + +extern const hal_comp_t * comps[]; +extern const pin_t pins[]; +extern const uint32_t comp_count; +extern const uint32_t pin_count; + +extern const hal_comp_t hv_comp_struct; // found in stmsp/stm32f303/src/comps/hv.c +extern const hal_comp_t io_comp_struct; // found in stmsp/stm32f303/src/comps/io.c +extern const hal_comp_t tle_comp_struct; // found in stmsp/stm32f303/src/comps/tle.c +extern const hal_comp_t sim_comp_struct; // found in shared/comps/sim.c +extern const hal_comp_t term_comp_struct; // found in shared/comps/term.c +extern const hal_comp_t curpid_comp_struct; // found in shared/comps/curpid.c +extern const hal_comp_t svm_comp_struct; // found in shared/comps/svm.c +extern const hal_comp_t dq_comp_struct; // found in shared/comps/dq.c +extern const hal_comp_t idq_comp_struct; // found in shared/comps/idq.c +extern const hal_comp_t pole_comp_struct; // found in shared/comps/pole.c +extern const hal_comp_t map_comp_struct; // found in shared/comps/map.c +extern const hal_comp_t vel_comp_struct; // found in shared/comps/vel.c +extern const hal_comp_t rl_comp_struct; // found in shared/comps/rl.c +extern const hal_comp_t ypid_comp_struct; // found in shared/comps/ypid.c diff --git a/stmsp/stm32f303/inc/main.h b/stmsp/stm32f303/inc/main.h new file mode 100644 index 00000000..3af9be31 --- /dev/null +++ b/stmsp/stm32f303/inc/main.h @@ -0,0 +1,67 @@ +/** + ****************************************************************************** + * File Name : main.h + * Description : This file contains the common defines of the application + ****************************************************************************** + * + * Copyright (c) 2016 STMicroelectronics International N.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted, provided that the following conditions are met: + * + * 1. Redistribution of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific written permission. + * 4. This software, including modifications and/or derivative works of this + * software, must execute solely and exclusively on microcontroller or + * microprocessor devices manufactured by or for STMicroelectronics. + * 5. Redistribution and use of this software other than as permitted under + * this license is void and will automatically terminate your rights under + * this license. + * + * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY + * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT + * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __MAIN_H +#define __MAIN_H +/* Includes ------------------------------------------------------------------*/ + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +/* Private define ------------------------------------------------------------*/ + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +/** + * @} + */ + +/** + * @} +*/ + +#endif /* __MAIN_H */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/inc/opamp.h b/stmsp/stm32f303/inc/opamp.h new file mode 100644 index 00000000..8f523aaf --- /dev/null +++ b/stmsp/stm32f303/inc/opamp.h @@ -0,0 +1,92 @@ +/** + ****************************************************************************** + * File Name : OPAMP.h + * Description : This file provides code for the configuration + * of the OPAMP instances. + ****************************************************************************** + * + * Copyright (c) 2016 STMicroelectronics International N.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted, provided that the following conditions are met: + * + * 1. Redistribution of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific written permission. + * 4. This software, including modifications and/or derivative works of this + * software, must execute solely and exclusively on microcontroller or + * microprocessor devices manufactured by or for STMicroelectronics. + * 5. Redistribution and use of this software other than as permitted under + * this license is void and will automatically terminate your rights under + * this license. + * + * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY + * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT + * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __opamp_H +#define __opamp_H +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f3xx_hal.h" +#include "main.h" + +/* USER CODE BEGIN Includes */ + +/* USER CODE END Includes */ + +extern OPAMP_HandleTypeDef hopamp1; +extern OPAMP_HandleTypeDef hopamp2; +extern OPAMP_HandleTypeDef hopamp3; +extern OPAMP_HandleTypeDef hopamp4; + +/* USER CODE BEGIN Private defines */ + +/* USER CODE END Private defines */ + +extern void Error_Handler(void); + +void MX_OPAMP1_Init(void); +void MX_OPAMP2_Init(void); +void MX_OPAMP3_Init(void); +void MX_OPAMP4_Init(void); + +/* USER CODE BEGIN Prototypes */ + +/* USER CODE END Prototypes */ + +#ifdef __cplusplus +} +#endif +#endif /*__ opamp_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/inc/stm32f3xx_hal_conf.h b/stmsp/stm32f303/inc/stm32f3xx_hal_conf.h new file mode 100644 index 00000000..f080963e --- /dev/null +++ b/stmsp/stm32f303/inc/stm32f3xx_hal_conf.h @@ -0,0 +1,340 @@ +/** + ****************************************************************************** + * @file stm32f3xx_hal_conf.h + * @brief HAL configuration file. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT(c) 2016 STMicroelectronics

+ * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F3xx_HAL_CONF_H +#define __STM32F3xx_HAL_CONF_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "main.h" +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +/* ########################## Module Selection ############################## */ +/** + * @brief This is the list of modules to be used in the HAL driver + */ + +#define HAL_MODULE_ENABLED +#define HAL_ADC_MODULE_ENABLED +/*#define HAL_CAN_MODULE_ENABLED */ +/*#define HAL_CEC_MODULE_ENABLED */ +/*#define HAL_NAND_MODULE_ENABLED */ +/*#define HAL_NOR_MODULE_ENABLED */ +/*#define HAL_PCCARD_MODULE_ENABLED */ +/*#define HAL_SRAM_MODULE_ENABLED */ +/*#define HAL_HRTIM_MODULE_ENABLED */ +#define HAL_OPAMP_MODULE_ENABLED +/*#define HAL_SDADC_MODULE_ENABLED */ +/*#define HAL_TSC_MODULE_ENABLED */ +/*#define HAL_COMP_MODULE_ENABLED */ +#define HAL_CRC_MODULE_ENABLED +/*#define HAL_CRYP_MODULE_ENABLED */ +#define HAL_DAC_MODULE_ENABLED +/*#define HAL_I2S_MODULE_ENABLED */ +/*#define HAL_IWDG_MODULE_ENABLED */ +/*#define HAL_LCD_MODULE_ENABLED */ +/*#define HAL_LPTIM_MODULE_ENABLED */ +/*#define HAL_RNG_MODULE_ENABLED */ +/*#define HAL_RTC_MODULE_ENABLED */ +#define HAL_SPI_MODULE_ENABLED +#define HAL_TIM_MODULE_ENABLED +#define HAL_UART_MODULE_ENABLED +/*#define HAL_USART_MODULE_ENABLED */ +/*#define HAL_IRDA_MODULE_ENABLED */ +/*#define HAL_SMARTCARD_MODULE_ENABLED */ +/*#define HAL_SMBUS_MODULE_ENABLED */ +/*#define HAL_WWDG_MODULE_ENABLED */ +#define HAL_PCD_MODULE_ENABLED +#define HAL_GPIO_MODULE_ENABLED +#define HAL_DMA_MODULE_ENABLED +#define HAL_RCC_MODULE_ENABLED +#define HAL_FLASH_MODULE_ENABLED +// #define HAL_PWR_MODULE_ENABLED +#define HAL_CORTEX_MODULE_ENABLED +// #define HAL_I2C_MODULE_ENABLED +/* ########################## 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 HAL module to compute the system frequency + * (when HSE is used as system clock source, directly or through the PLL). + */ +#if !defined(HSE_VALUE) +#define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */ +#endif /* HSE_VALUE */ + +/** + * @brief In the following line adjust the External High Speed oscillator (HSE) Startup + * Timeout value + */ +#if !defined(HSE_STARTUP_TIMEOUT) +#define HSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for HSE start up, in ms */ +#endif /* HSE_STARTUP_TIMEOUT */ + +/** + * @brief Internal High Speed oscillator (HSI) value. + * This value is used by the RCC HAL module to compute the system frequency + * (when HSI is used as system clock source, directly or through the PLL). + */ +#if !defined(HSI_VALUE) +#define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/ +#endif /* HSI_VALUE */ + +/** + * @brief In the following line adjust the Internal High Speed oscillator (HSI) Startup + * Timeout value + */ +#if !defined(HSI_STARTUP_TIMEOUT) +#define HSI_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSI start up */ +#endif /* HSI_STARTUP_TIMEOUT */ + +/** + * @brief Internal Low Speed oscillator (LSI) value. + */ +#if !defined(LSI_VALUE) +#define LSI_VALUE ((uint32_t)40000) +#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz \ + The real value may vary depending on the variations \ + in voltage and temperature. */ +/** + * @brief External Low Speed oscillator (LSE) value. + */ +#if !defined(LSE_VALUE) +#define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */ +#endif /* LSE_VALUE */ + +/** + * @brief Time out for LSE start up value in ms. + */ +#if !defined(LSE_STARTUP_TIMEOUT) +#define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */ +#endif /* LSE_STARTUP_TIMEOUT */ + +/** + * @brief External clock source for I2S peripheral + * This value is used by the I2S HAL module to compute the I2S clock source + * frequency, this source is inserted directly through I2S_CKIN pad. + * - External clock generated through external PLL component on EVAL 303 (based on MCO or crystal) + * - External clock not generated on EVAL 373 + */ +#if !defined(EXTERNAL_CLOCK_VALUE) +#define EXTERNAL_CLOCK_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz*/ +#endif /* EXTERNAL_CLOCK_VALUE */ + +/* Tip: To avoid modifying this file each time you need to use different HSE, + === you can define the HSE value in your toolchain compiler preprocessor. */ + +/* ########################### System Configuration ######################### */ +/** + * @brief This is the HAL system configuration section + */ + +#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */ +#define TICK_INT_PRIORITY ((uint32_t)0) /*!< tick interrupt priority (lowest by default) */ +#define USE_RTOS 0 +#define PREFETCH_ENABLE 1 +#define INSTRUCTION_CACHE_ENABLE 0 +#define DATA_CACHE_ENABLE 0 + +/* ########################## Assert Selection ############################## */ +/** + * @brief Uncomment the line below to expanse the "assert_param" macro in the + * HAL drivers code + */ +/* #define USE_FULL_ASSERT 1 */ + +/* Includes ------------------------------------------------------------------*/ +/** + * @brief Include module's header file + */ + +#ifdef HAL_RCC_MODULE_ENABLED +#include "stm32f3xx_hal_rcc.h" +#endif /* HAL_RCC_MODULE_ENABLED */ + +#ifdef HAL_GPIO_MODULE_ENABLED +#include "stm32f3xx_hal_gpio.h" +#endif /* HAL_GPIO_MODULE_ENABLED */ + +#ifdef HAL_DMA_MODULE_ENABLED +#include "stm32f3xx_hal_dma.h" +#endif /* HAL_DMA_MODULE_ENABLED */ + +#ifdef HAL_CORTEX_MODULE_ENABLED +#include "stm32f3xx_hal_cortex.h" +#endif /* HAL_CORTEX_MODULE_ENABLED */ + +#ifdef HAL_ADC_MODULE_ENABLED +#include "stm32f3xx_hal_adc.h" +#endif /* HAL_ADC_MODULE_ENABLED */ + +#ifdef HAL_CAN_MODULE_ENABLED +#include "stm32f3xx_hal_can.h" +#endif /* HAL_CAN_MODULE_ENABLED */ + +#ifdef HAL_CEC_MODULE_ENABLED +#include "stm32f3xx_hal_cec.h" +#endif /* HAL_CEC_MODULE_ENABLED */ + +#ifdef HAL_COMP_MODULE_ENABLED +#include "stm32f3xx_hal_comp.h" +#endif /* HAL_COMP_MODULE_ENABLED */ + +#ifdef HAL_CRC_MODULE_ENABLED +#include "stm32f3xx_hal_crc.h" +#endif /* HAL_CRC_MODULE_ENABLED */ + +#ifdef HAL_DAC_MODULE_ENABLED +#include "stm32f3xx_hal_dac.h" +#endif /* HAL_DAC_MODULE_ENABLED */ + +#ifdef HAL_FLASH_MODULE_ENABLED +#include "stm32f3xx_hal_flash.h" +#endif /* HAL_FLASH_MODULE_ENABLED */ + +#ifdef HAL_SRAM_MODULE_ENABLED +#include "stm32f3xx_hal_sram.h" +#endif /* HAL_SRAM_MODULE_ENABLED */ + +#ifdef HAL_NOR_MODULE_ENABLED +#include "stm32f3xx_hal_nor.h" +#endif /* HAL_NOR_MODULE_ENABLED */ + +#ifdef HAL_NAND_MODULE_ENABLED +#include "stm32f3xx_hal_nand.h" +#endif /* HAL_NAND_MODULE_ENABLED */ + +#ifdef HAL_PCCARD_MODULE_ENABLED +#include "stm32f3xx_hal_pccard.h" +#endif /* HAL_PCCARD_MODULE_ENABLED */ + +#ifdef HAL_HRTIM_MODULE_ENABLED +#include "stm32f3xx_hal_hrtim.h" +#endif /* HAL_HRTIM_MODULE_ENABLED */ + +#ifdef HAL_I2C_MODULE_ENABLED +#include "stm32f3xx_hal_i2c.h" +#endif /* HAL_I2C_MODULE_ENABLED */ + +#ifdef HAL_I2S_MODULE_ENABLED +#include "stm32f3xx_hal_i2s.h" +#endif /* HAL_I2S_MODULE_ENABLED */ + +#ifdef HAL_IRDA_MODULE_ENABLED +#include "stm32f3xx_hal_irda.h" +#endif /* HAL_IRDA_MODULE_ENABLED */ + +#ifdef HAL_IWDG_MODULE_ENABLED +#include "stm32f3xx_hal_iwdg.h" +#endif /* HAL_IWDG_MODULE_ENABLED */ + +#ifdef HAL_OPAMP_MODULE_ENABLED +#include "stm32f3xx_hal_opamp.h" +#endif /* HAL_OPAMP_MODULE_ENABLED */ + +#ifdef HAL_PCD_MODULE_ENABLED +#include "stm32f3xx_hal_pcd.h" +#endif /* HAL_PCD_MODULE_ENABLED */ + +#ifdef HAL_PWR_MODULE_ENABLED +#include "stm32f3xx_hal_pwr.h" +#endif /* HAL_PWR_MODULE_ENABLED */ + +#ifdef HAL_RTC_MODULE_ENABLED +#include "stm32f3xx_hal_rtc.h" +#endif /* HAL_RTC_MODULE_ENABLED */ + +#ifdef HAL_SDADC_MODULE_ENABLED +#include "stm32f3xx_hal_sdadc.h" +#endif /* HAL_SDADC_MODULE_ENABLED */ + +#ifdef HAL_SMARTCARD_MODULE_ENABLED +#include "stm32f3xx_hal_smartcard.h" +#endif /* HAL_SMARTCARD_MODULE_ENABLED */ + +#ifdef HAL_SMBUS_MODULE_ENABLED +#include "stm32f3xx_hal_smbus.h" +#endif /* HAL_SMBUS_MODULE_ENABLED */ + +#ifdef HAL_SPI_MODULE_ENABLED +#include "stm32f3xx_hal_spi.h" +#endif /* HAL_SPI_MODULE_ENABLED */ + +#ifdef HAL_TIM_MODULE_ENABLED +#include "stm32f3xx_hal_tim.h" +#endif /* HAL_TIM_MODULE_ENABLED */ + +#ifdef HAL_TSC_MODULE_ENABLED +#include "stm32f3xx_hal_tsc.h" +#endif /* HAL_TSC_MODULE_ENABLED */ + +#ifdef HAL_UART_MODULE_ENABLED +#include "stm32f3xx_hal_uart.h" +#endif /* HAL_UART_MODULE_ENABLED */ + +#ifdef HAL_USART_MODULE_ENABLED +#include "stm32f3xx_hal_usart.h" +#endif /* HAL_USART_MODULE_ENABLED */ + +#ifdef HAL_WWDG_MODULE_ENABLED +#include "stm32f3xx_hal_wwdg.h" +#endif /* HAL_WWDG_MODULE_ENABLED */ + +/* Exported macro ------------------------------------------------------------*/ +#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)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) +/* Exported functions ------------------------------------------------------- */ +void assert_failed(uint8_t *file, uint32_t line); +#else +#define assert_param(expr) ((void)0) +#endif /* USE_FULL_ASSERT */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F3xx_HAL_CONF_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/inc/stm32f3xx_it.h b/stmsp/stm32f303/inc/stm32f3xx_it.h new file mode 100644 index 00000000..9ff9da4c --- /dev/null +++ b/stmsp/stm32f303/inc/stm32f3xx_it.h @@ -0,0 +1,63 @@ +/** + ****************************************************************************** + * @file stm32f3xx_it.h + * @brief This file contains the headers of the interrupt handlers. + ****************************************************************************** + * + * COPYRIGHT(c) 2016 STMicroelectronics + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F3xx_IT_H +#define __STM32F3xx_IT_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ + +void NMI_Handler(void); +void HardFault_Handler(void); +void MemManage_Handler(void); +void BusFault_Handler(void); +void UsageFault_Handler(void); +void DebugMon_Handler(void); +void SysTick_Handler(void); +void USB_LP_CAN_RX0_IRQHandler(void); + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32F3xx_IT_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/inc/stmsp_f3hw.h b/stmsp/stm32f303/inc/stmsp_f3hw.h new file mode 100644 index 00000000..1b827205 --- /dev/null +++ b/stmsp/stm32f303/inc/stmsp_f3hw.h @@ -0,0 +1,49 @@ +//stmbl +#define AREF 3.3 // analog reference voltage + +#define VDIVUP 20000.0 * 2.0 //HV div pullup R1,R12 +#define VDIVDOWN 1000.0 //HV div pulldown R2,R9 +#define SHUNT 0.013 //shunt +#define SHUNT_PULLUP 3900.0 +#define SHUNT_SERIE 120.0 + +#define LED_Pin GPIO_PIN_13 +#define LED_GPIO_Port GPIOC + +#define PWM_DEADTIME 0 +#define PWM_FREQ 100000.0 +#define PWM_RES ((int32_t)(72000000.0 * 2.0 / PWM_FREQ / 2.0)) +#define RT_FREQ 5000.0 +#define ADC_OVER (PWM_FREQ * 2 / RT_FREQ) +#define ADC_COUNT (ADC_OVER * 4) + +#define USB_CONNECT_PIN GPIO_PIN_2 +#define USB_CONNECT_PORT GPIOB + +#define USB_TERM 1 + +/* +//otter +//TODO: swap v,w cur feedback +#define PWM_INVERT +#define AREF 3.3// analog reference voltage + +#define VDIVUP 56000.0//HV div pullup R1,R12 +#define VDIVDOWN 2000.0//HV div pulldown R2,R9 +#define SHUNT 0.003//shunt +#define SHUNT_PULLUP 5100.0 +#define SHUNT_SERIE 100.0 + +#define LED_Pin GPIO_PIN_0 +#define LED_GPIO_Port GPIOA + +#define PWM_U TIM8->CCR1 +#define PWM_V TIM8->CCR2 +#define PWM_W TIM8->CCR3 + +//ottercontrol +#define USB_DISCONNECT_PIN GPIO_PIN_13 +#define USB_DISCONNECT_PORT GPIOC + +#define PWM_DEADTIME 50 +*/ diff --git a/stmsp/stm32f303/inc/usb_device.h b/stmsp/stm32f303/inc/usb_device.h new file mode 100644 index 00000000..4382b371 --- /dev/null +++ b/stmsp/stm32f303/inc/usb_device.h @@ -0,0 +1,74 @@ +/** + ****************************************************************************** + * @file : USB_DEVICE + * @version : v1.0_Cube + * @brief : Header for usb_device file. + ****************************************************************************** + * + * Copyright (c) 2016 STMicroelectronics International N.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted, provided that the following conditions are met: + * + * 1. Redistribution of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific written permission. + * 4. This software, including modifications and/or derivative works of this + * software, must execute solely and exclusively on microcontroller or + * microprocessor devices manufactured by or for STMicroelectronics. + * 5. Redistribution and use of this software other than as permitted under + * this license is void and will automatically terminate your rights under + * this license. + * + * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY + * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT + * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** +*/ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __usb_device_H +#define __usb_device_H +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f3xx.h" +#include "stm32f3xx_hal.h" +#include "usbd_def.h" + +extern USBD_HandleTypeDef hUsbDeviceFS; + +/* USB_Device init function */ +void MX_USB_DEVICE_Init(void); + +#ifdef __cplusplus +} +#endif +#endif /*__usb_device_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/inc/usbd_cdc_if.h b/stmsp/stm32f303/inc/usbd_cdc_if.h new file mode 100644 index 00000000..d268bba7 --- /dev/null +++ b/stmsp/stm32f303/inc/usbd_cdc_if.h @@ -0,0 +1,13 @@ +#pragma once +#include +#include "usbd_cdc.h" + +extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS; +uint8_t CDC_Transmit_FS(uint8_t *Buf, uint16_t Len); + + +//void cdc_init(void); +int cdc_tx(void *data, uint32_t len); +int cdc_getline(char *ptr, int len); +int cdc_is_connected(); +void cdc_poll(); diff --git a/stmsp/stm32f303/inc/usbd_conf.h b/stmsp/stm32f303/inc/usbd_conf.h new file mode 100644 index 00000000..d0ed7a16 --- /dev/null +++ b/stmsp/stm32f303/inc/usbd_conf.h @@ -0,0 +1,185 @@ +/** + ****************************************************************************** + * @file : usbd_conf.h + * @version : v1.0_Cube + * @brief : Header for usbd_conf file. + ****************************************************************************** + * + * Copyright (c) 2016 STMicroelectronics International N.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted, provided that the following conditions are met: + * + * 1. Redistribution of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific written permission. + * 4. This software, including modifications and/or derivative works of this + * software, must execute solely and exclusively on microcontroller or + * microprocessor devices manufactured by or for STMicroelectronics. + * 5. Redistribution and use of this software other than as permitted under + * this license is void and will automatically terminate your rights under + * this license. + * + * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY + * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT + * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** +*/ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_CONF__H__ +#define __USBD_CONF__H__ +#ifdef __cplusplus +extern "C" { +#endif +/* Includes ------------------------------------------------------------------*/ +#include +#include +#include +#include "stm32f3xx.h" +#include "stm32f3xx_hal.h" +#include "usbd_def.h" + +/** @addtogroup USBD_OTG_DRIVER + * @{ + */ + +/** @defgroup USBD_CONF + * @brief usb otg low level driver configuration file + * @{ + */ + +/** @defgroup USBD_CONF_Exported_Defines + * @{ + */ + +/*---------- -----------*/ +#define USBD_MAX_NUM_INTERFACES 1 +/*---------- -----------*/ +#define USBD_MAX_NUM_CONFIGURATION 1 +/*---------- -----------*/ +#define USBD_MAX_STR_DESC_SIZ 512 +/*---------- -----------*/ +#define USBD_SUPPORT_USER_STRING 0 +/*---------- -----------*/ +#define USBD_DEBUG_LEVEL 0 +/*---------- -----------*/ +#define USBD_SELF_POWERED 1 +/*---------- -----------*/ +#define USBD_CDC_INTERVAL 1000 +/****************************************/ +/* #define for FS and HS identification */ +#define DEVICE_FS 0 + +/** @defgroup USBD_Exported_Macros + * @{ + */ + +/* Memory management macros */ +#define USBD_malloc (uint32_t *)USBD_static_malloc +#define USBD_free USBD_static_free +#define USBD_memset /* Not used */ +#define USBD_memcpy /* Not used */ + +#define USBD_Delay HAL_Delay + +/* For footprint reasons and since only one allocation is handled in the HID class + driver, the malloc/free is changed into a static allocation method */ +void *USBD_static_malloc(uint32_t size); +void USBD_static_free(void *p); + +/* DEBUG macros */ +#if(USBD_DEBUG_LEVEL > 0) +#define USBD_UsrLog(...) \ + printf(__VA_ARGS__); \ + printf("\n"); +#else +#define USBD_UsrLog(...) +#endif + + +#if(USBD_DEBUG_LEVEL > 1) + +#define USBD_ErrLog(...) \ + printf("ERROR: "); \ + printf(__VA_ARGS__); \ + printf("\n"); +#else +#define USBD_ErrLog(...) +#endif + + +#if(USBD_DEBUG_LEVEL > 2) +#define USBD_DbgLog(...) \ + printf("DEBUG : "); \ + printf(__VA_ARGS__); \ + printf("\n"); +#else +#define USBD_DbgLog(...) +#endif + +/** + * @} + */ + + +/** + * @} + */ + +/** @defgroup USBD_CONF_Exported_Types + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_CONF_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_CONF_Exported_Variables + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_CONF_Exported_FunctionsPrototype + * @{ + */ +/** + * @} + */ +#ifdef __cplusplus +} +#endif + +#endif /*__USBD_CONF__H__*/ + +/** + * @} + */ + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/inc/usbd_desc.h b/stmsp/stm32f303/inc/usbd_desc.h new file mode 100644 index 00000000..21b4872f --- /dev/null +++ b/stmsp/stm32f303/inc/usbd_desc.h @@ -0,0 +1,114 @@ +/** + ****************************************************************************** + * @file : usbd_desc.h + * @version : v1.0_Cube + * @brief : Header for usbd_desc file. + ****************************************************************************** + * + * Copyright (c) 2016 STMicroelectronics International N.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted, provided that the following conditions are met: + * + * 1. Redistribution of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific written permission. + * 4. This software, including modifications and/or derivative works of this + * software, must execute solely and exclusively on microcontroller or + * microprocessor devices manufactured by or for STMicroelectronics. + * 5. Redistribution and use of this software other than as permitted under + * this license is void and will automatically terminate your rights under + * this license. + * + * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY + * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT + * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** +*/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_DESC__H__ +#define __USBD_DESC__H__ + +#ifdef __cplusplus +extern "C" { +#endif +/* Includes ------------------------------------------------------------------*/ +#include "usbd_def.h" + +/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USB_DESC + * @brief general defines for the usb device library file + * @{ + */ + +/** @defgroup USB_DESC_Exported_Defines + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_DESC_Exported_TypesDefinitions + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_DESC_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_DESC_Exported_Variables + * @{ + */ +extern USBD_DescriptorsTypeDef FS_Desc; +/** + * @} + */ + +/** @defgroup USBD_DESC_Exported_FunctionsPrototype + * @{ + */ + +/** + * @} + */ +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_DESC_H */ + +/** + * @} + */ + +/** +* @} +*/ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/src/adc.c b/stmsp/stm32f303/src/adc.c new file mode 100644 index 00000000..073db0c0 --- /dev/null +++ b/stmsp/stm32f303/src/adc.c @@ -0,0 +1,417 @@ +/* +* This file is part of the stmbl project. +* +* Copyright (C) 2013-2017 Rene Hopf +* Copyright (C) 2013-2017 Nico Stute +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +/* Includes ------------------------------------------------------------------*/ +#include "adc.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +ADC_HandleTypeDef hadc1; +ADC_HandleTypeDef hadc2; +ADC_HandleTypeDef hadc3; +ADC_HandleTypeDef hadc4; + +/* ADC1 init function */ +void MX_ADC1_Init(void) { + ADC_ChannelConfTypeDef sConfig; + + /**Common config + */ + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc1.Init.Resolution = ADC_RESOLUTION_12B; + hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; + hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.NbrOfDiscConversion = 1; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; + hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_TRGO2; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.NbrOfConversion = 4; + hadc1.Init.DMAContinuousRequests = DISABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc1.Init.LowPowerAutoWait = DISABLE; + hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; + if(HAL_ADC_Init(&hadc1) != HAL_OK) { + Error_Handler(); + } + + sConfig.Channel = ADC_CHANNEL_1; // pa0 = dclink + sConfig.Rank = 1; + sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.SamplingTime = ADC_SAMPLETIME_61CYCLES_5; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 2; + sConfig.Channel = ADC_CHANNEL_4; // pa3 = bemf0 + if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 3; + sConfig.Channel = ADC_CHANNEL_3; // pa2 = in0 + if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 4; + sConfig.Channel = ADC_CHANNEL_2; // pa1 = temp + if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { + Error_Handler(); + } +} +/* ADC2 init function */ +void MX_ADC2_Init(void) { + ADC_ChannelConfTypeDef sConfig; + // ADC_MultiModeTypeDef multimode; + + /**Common config + */ + hadc2.Instance = ADC2; + hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc2.Init.Resolution = ADC_RESOLUTION_12B; + hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE; + hadc2.Init.ContinuousConvMode = DISABLE; + hadc2.Init.DiscontinuousConvMode = DISABLE; + hadc2.Init.NbrOfDiscConversion = 1; + hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; + hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_TRGO2; + hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc2.Init.NbrOfConversion = 4; + hadc2.Init.DMAContinuousRequests = DISABLE; + hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc2.Init.LowPowerAutoWait = DISABLE; + hadc2.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; + if(HAL_ADC_Init(&hadc2) != HAL_OK) { + Error_Handler(); + } + + sConfig.Channel = ADC_CHANNEL_3; // pa6 = opamp2_out = shunt low + sConfig.Rank = 1; + sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.SamplingTime = ADC_SAMPLETIME_61CYCLES_5; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if(HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 2; + sConfig.Channel = ADC_CHANNEL_1; // pa4 = bemf1 + if(HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 3; + sConfig.Channel = ADC_CHANNEL_2; // pa5 = in1 + if(HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 4; + sConfig.Channel = ADC_CHANNEL_3; // pa6 = opamp2_out = shunt low + if(HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) { + Error_Handler(); + } +} +/* ADC3 init function */ +void MX_ADC3_Init(void) { + ADC_ChannelConfTypeDef sConfig; + + /**Common config + */ + hadc3.Instance = ADC3; + hadc3.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc3.Init.Resolution = ADC_RESOLUTION_12B; + hadc3.Init.ScanConvMode = ADC_SCAN_ENABLE; + hadc3.Init.ContinuousConvMode = DISABLE; + hadc3.Init.DiscontinuousConvMode = DISABLE; + hadc3.Init.NbrOfDiscConversion = 1; + hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; + hadc3.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_TRGO2; + hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc3.Init.NbrOfConversion = 4; + hadc3.Init.DMAContinuousRequests = DISABLE; + hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc3.Init.LowPowerAutoWait = DISABLE; + hadc3.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; + if(HAL_ADC_Init(&hadc3) != HAL_OK) { + Error_Handler(); + } + + /**Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_1; // pb1 = opamp3_out = shunt a + sConfig.Rank = 1; + sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.SamplingTime = ADC_SAMPLETIME_61CYCLES_5; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if(HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 2; + if(HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 3; + if(HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 4; + if(HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK) { + Error_Handler(); + } +} +/* ADC4 init function */ +void MX_ADC4_Init(void) { + ADC_ChannelConfTypeDef sConfig; + // ADC_MultiModeTypeDef multimode; + + /**Common config + */ + hadc4.Instance = ADC4; + hadc4.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc4.Init.Resolution = ADC_RESOLUTION_12B; + hadc4.Init.ScanConvMode = ADC_SCAN_ENABLE; + hadc4.Init.ContinuousConvMode = DISABLE; + hadc4.Init.DiscontinuousConvMode = DISABLE; + hadc4.Init.NbrOfDiscConversion = 1; + hadc4.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; + hadc4.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_TRGO2; + hadc4.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc4.Init.NbrOfConversion = 4; + hadc4.Init.DMAContinuousRequests = DISABLE; + hadc4.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc4.Init.LowPowerAutoWait = DISABLE; + hadc4.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; + if(HAL_ADC_Init(&hadc4) != HAL_OK) { + Error_Handler(); + } + + sConfig.Channel = ADC_CHANNEL_3; // pb12 = shunt b + sConfig.Rank = 1; + sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.SamplingTime = ADC_SAMPLETIME_61CYCLES_5; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if(HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 2; + if(HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 3; + if(HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) { + Error_Handler(); + } + + sConfig.Rank = 4; + if(HAL_ADC_ConfigChannel(&hadc4, &sConfig) != HAL_OK) { + Error_Handler(); + } +} + +static uint32_t HAL_RCC_ADC12_CLK_ENABLED = 0; +static uint32_t HAL_RCC_ADC34_CLK_ENABLED = 0; + +void HAL_ADC_MspInit(ADC_HandleTypeDef *adcHandle) { + GPIO_InitTypeDef GPIO_InitStruct; + if(adcHandle->Instance == ADC1) { + /* USER CODE BEGIN ADC1_MspInit 0 */ + + /* USER CODE END ADC1_MspInit 0 */ + /* Peripheral clock enable */ + HAL_RCC_ADC12_CLK_ENABLED++; + if(HAL_RCC_ADC12_CLK_ENABLED == 1) { + __HAL_RCC_ADC12_CLK_ENABLE(); + } + + /**ADC1 GPIO Configuration + PA0 ------> ADC1_IN1 + PA1 ------> ADC1_IN2 + PA2 ------> ADC1_IN3 + PA3 ------> ADC1_IN4 + */ + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN ADC1_MspInit 1 */ + + /* USER CODE END ADC1_MspInit 1 */ + } else if(adcHandle->Instance == ADC2) { + /* USER CODE BEGIN ADC2_MspInit 0 */ + + /* USER CODE END ADC2_MspInit 0 */ + /* Peripheral clock enable */ + HAL_RCC_ADC12_CLK_ENABLED++; + if(HAL_RCC_ADC12_CLK_ENABLED == 1) { + __HAL_RCC_ADC12_CLK_ENABLE(); + } + + /**ADC2 GPIO Configuration + PA4 ------> ADC2_IN1 + PA5 ------> ADC2_IN2 + PA6 ------> ADC2_IN3 + */ + GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN ADC2_MspInit 1 */ + + /* USER CODE END ADC2_MspInit 1 */ + } else if(adcHandle->Instance == ADC3) { + /* USER CODE BEGIN ADC3_MspInit 0 */ + + /* USER CODE END ADC3_MspInit 0 */ + /* Peripheral clock enable */ + HAL_RCC_ADC34_CLK_ENABLED++; + if(HAL_RCC_ADC34_CLK_ENABLED == 1) { + __HAL_RCC_ADC34_CLK_ENABLE(); + } + + /**ADC3 GPIO Configuration + PB1 ------> ADC3_IN1 + */ + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN ADC3_MspInit 1 */ + + /* USER CODE END ADC3_MspInit 1 */ + } else if(adcHandle->Instance == ADC4) { + /* USER CODE BEGIN ADC4_MspInit 0 */ + + /* USER CODE END ADC4_MspInit 0 */ + /* Peripheral clock enable */ + HAL_RCC_ADC34_CLK_ENABLED++; + if(HAL_RCC_ADC34_CLK_ENABLED == 1) { + __HAL_RCC_ADC34_CLK_ENABLE(); + } + + /**ADC4 GPIO Configuration + PB12 ------> ADC4_IN3 + */ + GPIO_InitStruct.Pin = GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN ADC4_MspInit 1 */ + + /* USER CODE END ADC4_MspInit 1 */ + } +} + +void HAL_ADC_MspDeInit(ADC_HandleTypeDef *adcHandle) { + if(adcHandle->Instance == ADC1) { + /* USER CODE BEGIN ADC1_MspDeInit 0 */ + + /* USER CODE END ADC1_MspDeInit 0 */ + /* Peripheral clock disable */ + HAL_RCC_ADC12_CLK_ENABLED--; + if(HAL_RCC_ADC12_CLK_ENABLED == 0) { + __HAL_RCC_ADC12_CLK_DISABLE(); + } + + /**ADC1 GPIO Configuration + PA3 ------> ADC1_IN4 + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3); + + /* USER CODE BEGIN ADC1_MspDeInit 1 */ + + /* USER CODE END ADC1_MspDeInit 1 */ + } else if(adcHandle->Instance == ADC2) { + /* USER CODE BEGIN ADC2_MspDeInit 0 */ + + /* USER CODE END ADC2_MspDeInit 0 */ + /* Peripheral clock disable */ + HAL_RCC_ADC12_CLK_ENABLED--; + if(HAL_RCC_ADC12_CLK_ENABLED == 0) { + __HAL_RCC_ADC12_CLK_DISABLE(); + } + + /**ADC2 GPIO Configuration + PA4 ------> ADC2_IN1 + PA5 ------> ADC2_IN2 + PA6 ------> ADC2_IN3 + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6); + + /* USER CODE BEGIN ADC2_MspDeInit 1 */ + + /* USER CODE END ADC2_MspDeInit 1 */ + } else if(adcHandle->Instance == ADC3) { + /* USER CODE BEGIN ADC3_MspDeInit 0 */ + + /* USER CODE END ADC3_MspDeInit 0 */ + /* Peripheral clock disable */ + HAL_RCC_ADC34_CLK_ENABLED--; + if(HAL_RCC_ADC34_CLK_ENABLED == 0) { + __HAL_RCC_ADC34_CLK_DISABLE(); + } + + /**ADC3 GPIO Configuration + PB1 ------> ADC3_IN1 + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_1); + + /* USER CODE BEGIN ADC3_MspDeInit 1 */ + + /* USER CODE END ADC3_MspDeInit 1 */ + } else if(adcHandle->Instance == ADC4) { + /* USER CODE BEGIN ADC4_MspDeInit 0 */ + + /* USER CODE END ADC4_MspDeInit 0 */ + /* Peripheral clock disable */ + HAL_RCC_ADC34_CLK_ENABLED--; + if(HAL_RCC_ADC34_CLK_ENABLED == 0) { + __HAL_RCC_ADC34_CLK_DISABLE(); + } + + /**ADC4 GPIO Configuration + PB12 ------> ADC4_IN3 + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12); + + /* USER CODE BEGIN ADC4_MspDeInit 1 */ + + /* USER CODE END ADC4_MspDeInit 1 */ + } +} \ No newline at end of file diff --git a/stmsp/stm32f303/src/comps/hv.c b/stmsp/stm32f303/src/comps/hv.c new file mode 100644 index 00000000..630dde54 --- /dev/null +++ b/stmsp/stm32f303/src/comps/hv.c @@ -0,0 +1,102 @@ +#include "commands.h" +#include "hal.h" +#include "math.h" +#include "defines.h" +#include "angle.h" +#include "stmsp_f3hw.h" +#include "stm32f3xx_hal.h" + +HAL_COMP(hv); + +//U V W input +HAL_PIN(a); +HAL_PIN(b); + +HAL_PIN(a_fb); +HAL_PIN(b_fb); + +//dclink input +HAL_PIN(udc); + +HAL_PIN(hv_temp); + +//enable in +HAL_PIN(en); + +//TODO: half bridge enable in +HAL_PIN(ena); +HAL_PIN(enb); + +//fault output +HAL_PIN(fault); + +HAL_PIN(min_on); // min on time [s] +HAL_PIN(min_off); // min off time [s] + +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct hv_ctx_t * ctx = (struct hv_ctx_t *)ctx_ptr; + struct hv_pin_ctx_t *pins = (struct hv_pin_ctx_t *)pin_ptr; + PIN(ena) = 1.0; + PIN(enb) = 1.0; + PIN(min_on) = 0.000001; + PIN(min_off) = 0.000001; + + GPIO_InitTypeDef GPIO_InitStruct; + //enable + GPIO_InitStruct.Pin = GPIO_PIN_15; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + GPIO_InitStruct.Pin = GPIO_PIN_10; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); +} + +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct hv_ctx_t * ctx = (struct hv_ctx_t *)ctx_ptr; + struct hv_pin_ctx_t *pins = (struct hv_pin_ctx_t *)pin_ptr; + + float udc = MAX(PIN(udc), 0.1); + //convert voltages to PWM output compare values + int32_t a = (int32_t)(PIN(a) / udc * PWM_RES / 2.0) + PWM_RES / 2; + int32_t b = (int32_t)(PIN(b) / udc * PWM_RES / 2.0) + PWM_RES / 2; + + //convert on and off times to PWM output compare values + int32_t min = (int32_t)(PWM_RES * RT_FREQ * PIN(min_on) + 0.5); + int32_t min_off = (int32_t)(PWM_RES * RT_FREQ * PIN(min_off) + 0.5); + + a = CLAMP(a, min, PWM_RES - min_off); + b = CLAMP(b, min, PWM_RES - min_off); + + TIM1->CCR1 = a; + TIM1->CCR2 = b; + // GPIOC->BSRR |= GPIO_PIN_15 << 16; + + PIN(a_fb) = (a - PWM_RES / 2.0) * 2.0 * udc / PWM_RES; + PIN(b_fb) = (b - PWM_RES / 2.0) * 2.0 * udc / PWM_RES; + + if(PIN(udc) < 52.0 && PIN(hv_temp) < 100.0 && PIN(en) > 0.0) { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, PIN(enb) > 0.0 ? GPIO_PIN_SET : GPIO_PIN_RESET); + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, PIN(ena) > 0.0 ? GPIO_PIN_SET : GPIO_PIN_RESET); + } else { + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET); + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET); + } +} + +hal_comp_t hv_comp_struct = { + .name = "hv", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct hv_pin_ctx_t) / sizeof(struct hal_pin_inst_t), +}; diff --git a/stmsp/stm32f303/src/comps/io.c b/stmsp/stm32f303/src/comps/io.c new file mode 100644 index 00000000..c9ccdf93 --- /dev/null +++ b/stmsp/stm32f303/src/comps/io.c @@ -0,0 +1,207 @@ +#include "commands.h" +#include "hal.h" +#include "math.h" +#include "defines.h" +#include "angle.h" +#include "stm32f3xx_hal.h" +#include "stmsp_f3hw.h" + +HAL_COMP(io); + +HAL_PIN(led); + +HAL_PIN(oc1); +HAL_PIN(oc2); +HAL_PIN(ena); +HAL_PIN(enb); + +HAL_PIN(hv_temp); +HAL_PIN(dc_link); +HAL_PIN(bemf0); +HAL_PIN(bemf1); +HAL_PIN(in0); +HAL_PIN(in1); +HAL_PIN(iap); +HAL_PIN(ian); +HAL_PIN(ibp); +HAL_PIN(ibn); +HAL_PIN(ip); +HAL_PIN(in); +HAL_PIN(ia); +HAL_PIN(ib); + +// uint32_t adc_12_buf[80]; +// uint32_t adc_34_buf[80]; + +#pragma pack(1) +struct adc_12_t { + uint16_t dc_link; + uint16_t shunt_low0; + uint16_t bemf0; + uint16_t bemf1; + uint16_t in0; + uint16_t in1; + uint16_t hv_temp; + uint16_t shunt_low1; +}; + +#pragma pack(1) +struct adc_34_t { + uint16_t shunt_a0; + uint16_t shunt_b0; + uint16_t shunt_a1; + uint16_t shunt_b1; + uint16_t shunt_a2; + uint16_t shunt_b2; + uint16_t shunt_a3; + uint16_t shunt_b3; +}; + +volatile struct adc_12_t adc_12_buf[(uint32_t)ADC_OVER]; +volatile struct adc_34_t adc_34_buf[(uint32_t)ADC_OVER]; + +struct io_ctx_t { + float u_offset; + float v_offset; + float w_offset; +}; + +#define ARES 4096.0 // analog resolution, 12 bit +#define ADC(a) ((a) / ARES * AREF) + +#define HV_TEMP_PULLUP 1000 +#define HV_R(a) (HV_TEMP_PULLUP / (AREF / (a)-1)) + +#define MOT_TEMP_PULLUP 10000 +#define MOT_TEMP_PULLMID 51000 +#define MOT_TEMP_PULLDOWN 10000 +#define MOT_TEMP_REF 15.26 +#define MOT_REF(a) ((a) * (MOT_TEMP_PULLMID + MOT_TEMP_PULLDOWN) / MOT_TEMP_PULLDOWN) +#define MOT_R(a) (MOT_TEMP_PULLUP / (MOT_TEMP_REF / (a)-1)) + +#define ARES 4096.0 // analog resolution, 12 bit + +#define VOLT(a) ((a) / (ARES) * (AREF) / (VDIVDOWN) * ((VDIVUP) + (VDIVDOWN))) +//#define TEMP(a) (log10f((a) * (AREF) / (ARES) * (TPULLUP) / ((AREF) - (a) * (AREF) / (ARES))) * (-53.0) + 290.0) + +#define SHUNT_GAIN 8.0 + +#define AMP(a, gain) (((a)*AREF / ARES / (gain)-AREF / (SHUNT_PULLUP + SHUNT_SERIE) * SHUNT_SERIE) / (SHUNT * SHUNT_PULLUP) * (SHUNT_PULLUP + SHUNT_SERIE)) + + +float r2temp(float r) { + r = r / 1000; + const int step = 5; + const int start = -10; + //-10..100 + const float temp[] = {21.377,16.869,13.411,10.735,8.653,7.018,5.726,4.700,3.879,3.219,2.685,2.250,1.895,1.604,1.363,1.163,0.996,0.857,0.740,0.641,0.558,0.487,0.426,0.375,0.330,0.292,0.259,0.230};//{271.7, 158.2, 95.23, 59.07, 37.64, 24.59, 16.43, 11.21, 7.798, 5.518, 3.972, 2.902}; + for(int i = 1; i < ARRAY_SIZE(temp); i++) { + if(temp[i] < r) { + float a = temp[i - 1]; + float b = temp[i]; + return (-(r - b) / (a - b) * step + i * step + start); + } + } + return (temp[ARRAY_SIZE(temp) - 1] + step); +} + +static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct io_ctx_t * ctx = (struct io_ctx_t *)ctx_ptr; + // struct io_pin_ctx_t * pins = (struct io_pin_ctx_t *)pin_ptr; + GPIO_InitTypeDef GPIO_InitStruct; + + // GPIO_InitStruct.Pin = LED_Pin | GPIO_PIN_14 | GPIO_PIN_15; + // GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + // GPIO_InitStruct.Pull = GPIO_NOPULL; + // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + // HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct); + + DMA1_Channel1->CCR &= (uint16_t)(~DMA_CCR_EN); + DMA1_Channel1->CPAR = (uint32_t) & (ADC12_COMMON->CDR); + DMA1_Channel1->CMAR = (uint32_t)adc_12_buf; + DMA1_Channel1->CNDTR = ADC_COUNT; + DMA1_Channel1->CCR = DMA_CCR_MINC | DMA_CCR_PL_0 | DMA_CCR_MSIZE_1 | DMA_CCR_PSIZE_1 | DMA_CCR_CIRC; + ADC1->CFGR |= ADC_CFGR_DMAEN | ADC_CFGR_DMACFG; + DMA1_Channel1->CCR |= DMA_CCR_TCIE; + DMA1_Channel1->CCR |= DMA_CCR_EN; + + // ADC12_COMMON->CCR |= ADC12_CCR_MDMA_1; + + DMA2_Channel5->CCR &= (uint16_t)(~DMA_CCR_EN); + DMA2_Channel5->CPAR = (uint32_t) & (ADC34_COMMON->CDR); + DMA2_Channel5->CMAR = (uint32_t)adc_34_buf; + DMA2_Channel5->CNDTR = ADC_COUNT; + DMA2_Channel5->CCR = DMA_CCR_MINC | DMA_CCR_PL_0 | DMA_CCR_MSIZE_1 | DMA_CCR_PSIZE_1 | DMA_CCR_CIRC; + ADC3->CFGR |= ADC_CFGR_DMAEN | ADC_CFGR_DMACFG; + DMA2_Channel5->CCR |= DMA_CCR_EN; + + // ADC34_COMMON->CCR |= ADC34_CCR_MDMA_1; +} + +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct io_ctx_t *ctx = (struct io_ctx_t *)ctx_ptr; + struct io_pin_ctx_t *pins = (struct io_pin_ctx_t *)pin_ptr; + + uint32_t dc_link = 0; + uint32_t hv_temp = 0; + uint32_t bemf0 = 0; + uint32_t bemf1 = 0; + uint32_t in0 = 0; + uint32_t in1 = 0; + uint32_t iap = 0; + uint32_t ian = 0; + uint32_t ibp = 0; + uint32_t ibn = 0; + // uint32_t ip = 0; + // uint32_t in = 0; + + for(int i = 0; i < ADC_OVER / 2; i++) { + dc_link += adc_12_buf[2 * i].dc_link + adc_12_buf[2 * i + 1].dc_link; + hv_temp += adc_12_buf[2 * i].hv_temp + adc_12_buf[2 * i + 1].hv_temp; + in0 += adc_12_buf[2 * i].in0 + adc_12_buf[2 * i + 1].in0; + in1 += adc_12_buf[2 * i].in1 + adc_12_buf[2 * i + 1].in1; + bemf0 += adc_12_buf[2 * i].bemf0 + adc_12_buf[2 * i + 1].bemf0; + bemf1 += adc_12_buf[2 * i].bemf1 + adc_12_buf[2 * i + 1].bemf1; + ian += adc_34_buf[2 * i].shunt_a0;// + adc_34_buf[2 * i].shunt_a1 + adc_34_buf[2 * i].shunt_a2 + adc_34_buf[2 * i].shunt_a3; + iap += adc_34_buf[2 * i + 1].shunt_a0;// + adc_34_buf[2 * i + 1].shunt_a1 + adc_34_buf[2 * i + 1].shunt_a2 + adc_34_buf[2 * i + 1].shunt_a3; + ibn += adc_34_buf[2 * i].shunt_b0;// + adc_34_buf[2 * i].shunt_b1 + adc_34_buf[2 * i].shunt_b2 + adc_34_buf[2 * i].shunt_b3; + ibp += adc_34_buf[2 * i + 1].shunt_b0;// + adc_34_buf[2 * i + 1].shunt_b1 + adc_34_buf[2 * i + 1].shunt_b2 + adc_34_buf[2 * i + 1].shunt_b3; + // in += adc_12_buf[2 * i].shunt_low0;// + adc_12_buf[2 * i].shunt_low1; + // ip += adc_12_buf[2 * i + 1].shunt_low0;// + adc_12_buf[2 * i + 1].shunt_low1; + } + + PIN(hv_temp) = r2temp(HV_R(ADC(hv_temp / ADC_OVER))); + PIN(dc_link) = ADC(dc_link / ADC_OVER) * (20.0 + 1.0) / 1.0; + PIN(bemf0) = ADC(bemf0 / ADC_OVER) * (20.0 + 1.0) / 1.0; + PIN(bemf1) = ADC(bemf1 / ADC_OVER) * (20.0 + 1.0) / 1.0; + PIN(in0) = ADC(in0 / ADC_OVER) * (10.0 + 1.5) / 1.5; + PIN(in1) = ADC(in1 / ADC_OVER) * (10.0 + 1.5) / 1.5; + + PIN(iap) = AMP(iap * 2.0 / ADC_OVER, 8.0); + PIN(ian) = AMP(ian * 2.0 / ADC_OVER, 8.0); + PIN(ibp) = AMP(ibp * 2.0 / ADC_OVER, 8.0); + PIN(ibn) = AMP(ibn * 2.0 / ADC_OVER, 8.0); + // PIN(ip) = ADC(ip / ADC_OVER); + // PIN(in) = ADC(in / ADC_OVER);; + PIN(ia) = (PIN(iap) - PIN(ian)) / 2.0; + PIN(ib) = (PIN(ibp) - PIN(ibn)) / 2.0; + + HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, PIN(led) > 0.0 ? GPIO_PIN_SET : GPIO_PIN_RESET); // 0.1u +} + +static void nrt_func(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { +} + +hal_comp_t io_comp_struct = { + .name = "io", + .nrt = nrt_func, + .rt = rt_func, + .frt = 0, + .nrt_init = nrt_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = sizeof(struct io_ctx_t), + .pin_count = sizeof(struct io_pin_ctx_t) / sizeof(struct hal_pin_inst_t), +}; diff --git a/stmsp/stm32f303/src/comps/tle.c b/stmsp/stm32f303/src/comps/tle.c new file mode 100644 index 00000000..f9783d4c --- /dev/null +++ b/stmsp/stm32f303/src/comps/tle.c @@ -0,0 +1,184 @@ +//driver for TLE5012 GMR-Based Angular Sensor +#include "commands.h" +#include "hal.h" +#include "math.h" +#include "defines.h" +#include "angle.h" +#include "stm32f3xx_hal.h" + +HAL_COMP(tle); + +HAL_PIN(pos); +HAL_PIN(error); + +SPI_HandleTypeDef hspi1; + +#pragma pack(1) +typedef union { + struct { + uint16_t nd : 4; //4-bit Number of data words + uint16_t addr : 6; //6-bit Address + uint16_t upd : 1; //Update register access 0: Access to current values 1: Access to updated values + uint16_t lock : 4; //4-bit lock value 0000B: Default operating access for addresses 0x00:0x04, 1010B: Config-access for addresses 0x05:0x11 + uint16_t rw : 1; //Read-write 0:Write 1:Read + }; + uint16_t word; +} tle_cmd_t; + +typedef union { + struct { + uint8_t crc : 8; //Cyclic Redundancy Check + uint8_t resp : 4; //Sensor number response indicator + uint8_t angle_valid : 1; //Valid angle value 1: Angle value valid + uint8_t if_err : 1; //interface error 1: No error + uint8_t sys_err : 1; //system error 1: No error + uint8_t rst : 1; //Indication of chip reset 0: Reset occurred 1: No reset + }; + uint16_t word; +} tle_safety_t; +//TODO: alles hier drunter drehen +typedef union { + struct { + uint8_t rd_st : 1; //Read Status + uint8_t s_nr : 2; //Slave Number + uint8_t no_gmr_a : 1; //No valid GMR Angle Value + uint8_t no_gmr_xy : 1; //No valid GMR XY Values + uint8_t s_rom : 1; //Status ROM + uint8_t s_adct : 1; //Status ADC-Test + uint8_t res : 1; //reserved + uint8_t s_magol : 1; //Status Magnitude Out of Limit + uint8_t s_xyol : 1; //Status X,Y Data Out of Limit + uint8_t s_ov : 1; //Status Overflow + uint8_t s_dspu : 1; //Status Digital Signal Processing Unit + uint8_t s_fuse : 1; //Status Fuse CRC + uint8_t s_vr : 1; //Status Voltage Regulator + uint8_t s_wd : 1; //Status Watchdog + uint8_t s_rst : 1; //Status Reset + }; + uint16_t word; +} tle_reg_stat_t; + +typedef union { + struct { + uint8_t res1 : 6; //Reserved + uint8_t as_adct : 1; //Enable ADC Testvector Check + uint8_t res2 : 1; //Reserved + uint8_t as_vec_mag : 1; //Activation of Magnitude Check + uint8_t as_vec_xy : 1; //Activation of X,Y out of limit Check + uint8_t as_ov : 1; //Enable of DSPU Overflow Check + uint8_t as_dspu : 1; //Activation DSPU BIST + uint8_t as_fuse : 1; //Activation Fuse CRC + uint8_t as_vr : 1; //Enable Voltage Regulator Check + uint8_t as_wd : 1; //Enable DSPU Watchdog-HW-Reset + uint8_t as_rst : 1; //Activation of Hardware Reset + }; + uint16_t word; +} tle_reg_acstat_t; + +typedef union { + struct { + uint8_t fir_md : 2; //Filter Decimation Setting (Update Rate Setting) + uint16_t res : 9; //Reserved + uint8_t clk_sel : 1; //Clock Source Select + uint8_t ssc_od : 1; //SSC Interface 0: Push-pull 1: Open drain + uint8_t dsp_hold : 1; //Hold DSPU Operation + uint8_t iif_mod : 2; //Incremental Interface Mode + }; + uint16_t word; +} tle_reg_mod1_t; + +#define TLE_REG_STAT 0x00 //Status Register +#define TLE_REG_ACSTAT 0x01 //Activation Status Register +#define TLE_REG_AVAL 0x02 //Angle Value Register +#define TLE_REG_ASPD 0x03 //Angle Speed Register +#define TLE_REG_AREV 0x04 //Angle Revolution Register +#define TLE_REG_FSYNC 0x05 //Frame Synchronization Register +#define TLE_REG_MOD_1 0x06 //Interface Mode1 Register +#define TLE_REG_SIL 0x07 //SIL Register +#define TLE_REG_MOD_2 0x08 //Interface Mode2 Register +#define TLE_REG_MOD_3 0x09 //Interface Mode3 Register +#define TLE_REG_OFFX 0x0A //Offset X +#define TLE_REG_OFFY 0x0B //Offset Y +#define TLE_REG_SYNCH 0x0C //Synchronicity +#define TLE_REG_IFAB 0x0D //IFAB Register +#define TLE_REG_MOD_4 0x0E //Interface Mode4 Register +#define TLE_REG_TCO_Y 0x0F //Temperature Coefficient Register +#define TLE_REG_ADC_X 0x10 //X-raw value +#define TLE_REG_ADC_Y 0x11 //Y-raw value +#define TLE_REG_CNT 0x20 //IIF Counter value + +static void hw_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct tle_ctx_t * ctx = (struct tle_ctx_t *)ctx_ptr; + // struct tle_pin_ctx_t *pins = (struct tle_pin_ctx_t *)pin_ptr; + GPIO_InitTypeDef GPIO_InitStruct; + /* Peripheral clock enable */ + __HAL_RCC_SPI1_CLK_ENABLE(); + + /**SPI1 GPIO Configuration + PB3 ------> SPI1_SCK + PB5 ------> SPI1_MOSI + */ + GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_5; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF5_SPI1; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /*Configure GPIO pin : PA15 */ + GPIO_InitStruct.Pin = GPIO_PIN_15; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET); + + hspi1.Instance = SPI1; + hspi1.Init.Mode = SPI_MODE_MASTER; + hspi1.Init.Direction = SPI_DIRECTION_1LINE; + hspi1.Init.DataSize = SPI_DATASIZE_16BIT; + hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; + hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; + hspi1.Init.NSS = SPI_NSS_SOFT; + hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; //SPI_BAUDRATEPRESCALER_64;//1.125 Mhz + hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; + hspi1.Init.TIMode = SPI_TIMODE_DISABLE; + hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; + hspi1.Init.CRCPolynomial = 7; + hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; + hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE; + HAL_SPI_Init(&hspi1); +} + +static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) { + // struct tle_ctx_t * ctx = (struct tle_ctx_t *)ctx_ptr; + struct tle_pin_ctx_t *pins = (struct tle_pin_ctx_t *)pin_ptr; + uint16_t buf[10]; + tle_cmd_t tle_cmd; + tle_safety_t tle_safety; + tle_cmd.word = 0x0000; + tle_cmd.rw = 1; + tle_cmd.addr = 2; + tle_cmd.nd = 1; + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET); + HAL_SPI_Transmit(&hspi1, (uint8_t *)&tle_cmd.word, 1, 100); + HAL_SPI_Receive(&hspi1, (uint8_t *)buf, tle_cmd.nd + 1, 100); + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET); + PIN(pos) = (float)((buf[0] & 0x7fff) - 0x4000) / 16384.0 * 2 * M_PI - M_PI; + tle_safety.word = buf[1]; + PIN(error) = !tle_safety.angle_valid; +} + +hal_comp_t tle_comp_struct = { + .name = "tle", + .nrt = 0, + .rt = rt_func, + .frt = 0, + .hw_init = hw_init, + .rt_start = 0, + .frt_start = 0, + .rt_stop = 0, + .frt_stop = 0, + .ctx_size = 0, + .pin_count = sizeof(struct tle_pin_ctx_t) / sizeof(struct hal_pin_inst_t), +}; diff --git a/stmsp/stm32f303/src/hal_tbl.c b/stmsp/stm32f303/src/hal_tbl.c new file mode 100644 index 00000000..c120d2e6 --- /dev/null +++ b/stmsp/stm32f303/src/hal_tbl.c @@ -0,0 +1,312 @@ +#include "hal.h" +//generated by tools/create_hal_tbl.py DO NOT EDIT + +const hal_comp_t * comps[] = { + &hv_comp_struct, // found in stmsp/stm32f303/src/comps/hv.c + &io_comp_struct, // found in stmsp/stm32f303/src/comps/io.c + &tle_comp_struct, // found in stmsp/stm32f303/src/comps/tle.c + &sim_comp_struct, // found in shared/comps/sim.c + &term_comp_struct, // found in shared/comps/term.c + &curpid_comp_struct, // found in shared/comps/curpid.c + &svm_comp_struct, // found in shared/comps/svm.c + &dq_comp_struct, // found in shared/comps/dq.c + &idq_comp_struct, // found in shared/comps/idq.c + &pole_comp_struct, // found in shared/comps/pole.c + &map_comp_struct, // found in shared/comps/map.c + &vel_comp_struct, // found in shared/comps/vel.c + &rl_comp_struct, // found in shared/comps/rl.c + &ypid_comp_struct, // found in shared/comps/ypid.c +}; + +const uint32_t comp_count = sizeof(comps) / sizeof(comps[0]); + +const pin_t pins[] = { + // pins for comp hv found in stmsp/stm32f303/src/comps/hv.c + "rt_prio", + "frt_prio", + "a", + "b", + "a_fb", + "b_fb", + "udc", + "hv_temp", + "en", + "ena", + "enb", + "fault", + "min_on", + "min_off", + // pins for comp io found in stmsp/stm32f303/src/comps/io.c + "rt_prio", + "frt_prio", + "led", + "oc1", + "oc2", + "ena", + "enb", + "hv_temp", + "dc_link", + "bemf0", + "bemf1", + "in0", + "in1", + "iap", + "ian", + "ibp", + "ibn", + "ip", + "in", + "ia", + "ib", + // pins for comp tle found in stmsp/stm32f303/src/comps/tle.c + "rt_prio", + "frt_prio", + "pos", + "error", + // pins for comp sim found in shared/comps/sim.c + "rt_prio", + "frt_prio", + "amp", + "freq", + "sin", + "msin", + "sin2", + "msin2", + "sin3", + "msin3", + "square", + "vel", + "res", + "offset", + // pins for comp term found in shared/comps/term.c + "rt_prio", + "frt_prio", + "wave0", + "wave1", + "wave2", + "wave3", + "wave4", + "wave5", + "wave6", + "wave7", + "offset0", + "offset1", + "offset2", + "offset3", + "offset4", + "offset5", + "offset6", + "offset7", + "gain0", + "gain1", + "gain2", + "gain3", + "gain4", + "gain5", + "gain6", + "gain7", + "send_step", + "con", + // pins for comp curpid found in shared/comps/curpid.c + "rt_prio", + "frt_prio", + "en", + "cmd_mode", + "id_cmd", + "iq_cmd", + "id_fb", + "iq_fb", + "ac_current", + "ud", + "uq", + "max_cur", + "pwm_volt", + "rd", + "rq", + "ld", + "lq", + "psi", + "ff", + "kp", + "ki", + "kind", + "vel", + "id_error", + "iq_error", + // pins for comp svm found in shared/comps/svm.c + "rt_prio", + "frt_prio", + "u", + "v", + "w", + "udc", + "su", + "sv", + "sw", + "cmode", + "mode", + "enu", + "env", + "enw", + // pins for comp dq found in shared/comps/dq.c + "rt_prio", + "frt_prio", + "mode", + "u", + "v", + "w", + "pos", + "polecount", + "a", + "b", + "y", + "d", + "q", + // pins for comp idq found in shared/comps/idq.c + "rt_prio", + "frt_prio", + "mode", + "d", + "q", + "pos", + "polecount", + "a", + "b", + "u", + "v", + "w", + // pins for comp pole found in shared/comps/pole.c + "rt_prio", + "frt_prio", + "pos", + "cpos", + "p", + // pins for comp map found in shared/comps/map.c + "rt_prio", + "frt_prio", + "pos_in", + "pos_out", + "pos_out2", + "start", + "freq", + "over", + "print", + "state", + "counter", + "index", + "m0", + "m1", + "m2", + "m3", + "m4", + "m5", + "m6", + "m7", + "m8", + "m9", + "m10", + "m11", + "m12", + "m13", + "m14", + "m15", + "m16", + "m17", + "m18", + "m19", + "m20", + "m21", + "m22", + "m23", + "m24", + "m25", + "m26", + "m27", + "m28", + "m29", + "m30", + "m31", + "m32", + "m33", + "m34", + "m35", + "m36", + "m37", + "m38", + "m39", + "m40", + "m41", + "m42", + "m43", + "m44", + "m45", + "m46", + "m47", + "m48", + "m49", + // pins for comp vel found in shared/comps/vel.c + "rt_prio", + "frt_prio", + "pos_in", + "pos_out", + "vel", + "acc", + "w", + "d", + "g", + "h", + "j", + "lp", + "torque", + "vel_ff", + "en", + "pos_error", + // pins for comp rl found in shared/comps/rl.c + "rt_prio", + "frt_prio", + "ra", + "rb", + "ld", + "lq", + "la", + "lb", + "max_cur", + "udc", + "start", + "state", + "ki", + "time", + "t", + "ia_fb", + "ib_fb", + "ua_fb", + "ub_fb", + "ua", + "ub", + // pins for comp ypid found in shared/comps/ypid.c + "rt_prio", + "frt_prio", + "pos_ext_cmd", + "pos_fb", + "pos_error", + "vel_ext_cmd", + "vel_fb", + "vel_cmd", + "vel_error", + "vel_min", + "enable", + "out", + "pos_p", + "vel_p", + "vel_i", + "vel_ff", + "max_vel", + "max_acc", + "max_out", + "vel_sat", + "out_sat", + "saturated", +}; + +const uint32_t pin_count = sizeof(pins) / sizeof(pins[0]); + diff --git a/stmsp/stm32f303/src/main.c b/stmsp/stm32f303/src/main.c new file mode 100644 index 00000000..e9bddf21 --- /dev/null +++ b/stmsp/stm32f303/src/main.c @@ -0,0 +1,560 @@ +/* +* This file is part of the stmbl project. +* +* Copyright (C) 2013-2017 Rene Hopf +* Copyright (C) 2013-2017 Nico Stute +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +#include "main.h" +#include "stm32f3xx_hal.h" +#include "adc.h" +#include "opamp.h" + +#include +#include "defines.h" +#include "hal.h" +#include "angle.h" + +#include "version.h" +#include "common.h" +#include "commands.h" +#include "stmsp_f3hw.h" + +#include "usbd_cdc_if.h" +#ifdef USB_TERM +#include "usb_device.h" +#endif + +volatile uint64_t systime = 0; + +void SysTick_Handler(void) { + /* USER CODE BEGIN SysTick_IRQn 0 */ + systime++; + /* USER CODE END SysTick_IRQn 0 */ + HAL_IncTick(); + HAL_SYSTICK_IRQHandler(); + /* USER CODE BEGIN SysTick_IRQn 1 */ + + /* USER CODE END SysTick_IRQn 1 */ +} + +uint32_t systick_freq; +CRC_HandleTypeDef hcrc; + +uint32_t hal_get_systick_value() { + return (SysTick->VAL); +} + +uint32_t hal_get_systick_reload() { + return (SysTick->LOAD); +} + +uint32_t hal_get_systick_freq() { + return (systick_freq); +} + +void SystemClock_Config(void); +void Error_Handler(void); + + + +uint32_t systick_freq; +CRC_HandleTypeDef hcrc; +TIM_HandleTypeDef htim1; + +static void MX_TIM1_Init(void) { + __HAL_RCC_TIM1_CLK_ENABLE(); + TIM_MasterConfigTypeDef sMasterConfig; + TIM_OC_InitTypeDef sConfigOC; + TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig; + + htim1.Instance = TIM1; + htim1.Init.Prescaler = 0; + htim1.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED3; + htim1.Init.Period = PWM_RES; + htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim1.Init.RepetitionCounter = 0; + // htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + // htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + HAL_TIM_OC_Init(&htim1); + + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_UPDATE; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig); + + sConfigOC.OCMode = TIM_OCMODE_PWM1; + sConfigOC.Pulse = PWM_RES / 2; + sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; + sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; + sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; + sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; + sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; + HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1); + + HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2); + + sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; + sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; + sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; + sBreakDeadTimeConfig.DeadTime = 0; + sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; + sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; + sBreakDeadTimeConfig.BreakFilter = 0; + sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE; + sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH; + sBreakDeadTimeConfig.Break2Filter = 0; + sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; + HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig); + + GPIO_InitTypeDef GPIO_InitStruct; + /**TIM1 GPIO Configuration + PB13 ------> TIM1_CH1N + PB14 ------> TIM1_CH2N + PA8 ------> TIM1_CH1 + PA9 ------> TIM1_CH2 + */ + GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF6_TIM1; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + GPIO_InitStruct.Alternate = GPIO_AF6_TIM1; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); +} + +void DMA1_Channel1_IRQHandler() { + DMA1->IFCR = DMA_IFCR_CTCIF1; + DMA2->IFCR = DMA_IFCR_CTCIF5; + + hal_run_rt(); + // if(__HAL_DMA_GET_FLAG(&hdma1, DMA_FLAG_TC1) == SET){ + // hal_stop(); + // hal.hal_state = RT_TOO_LONG; + // } + // if(__HAL_TIM_GET_FLAG(&hdma1, DMA_FLAG_TC1) == SET) { + // hal_stop(); + // hal.hal_state = RT_TOO_LONG; + // } + // reset + IWDG->KR = 0xAAAA; +} + + +void about(char *ptr) { + printf("######## software info ########\n"); + printf( + "%s v%i.%i.%i %s\n", + version_info.product_name, + version_info.major, + version_info.minor, + version_info.patch, + version_info.git_version); + printf("Branch %s\n", version_info.git_branch); + printf("Compiled %s %s ", version_info.build_date, version_info.build_time); + printf("by %s on %s\n", version_info.build_user, version_info.build_host); + printf("GCC %s\n", __VERSION__); + printf("newlib %s\n", _NEWLIB_VERSION); +#ifdef __CM4_CMSIS_VERSION + printf("CMSIS %i.%i\n", __CM4_CMSIS_VERSION_MAIN, __CM4_CMSIS_VERSION_SUB); +#endif +#ifdef __STM32F4XX_STDPERIPH_VERSION + printf("StdPeriph %i.%i.%i\n", __STM32F4XX_STDPERIPH_VERSION_MAIN, __STM32F4XX_STDPERIPH_VERSION_SUB1, __STM32F4XX_STDPERIPH_VERSION_SUB2); +#endif +#ifdef __STM32F3xx_HAL_VERSION + printf("HAL lib... TODO: print version\n"); +#endif +} + +COMMAND("about", about, "show system infos"); + +void bootloader(char *ptr) { +#ifdef USB_DISCONNECT_PIN + HAL_GPIO_WritePin(USB_DISCONNECT_PORT, USB_DISCONNECT_PIN, GPIO_PIN_SET); + HAL_Delay(100); +#endif +#ifdef USB_CONNECT_PIN + HAL_GPIO_WritePin(USB_CONNECT_PORT, USB_CONNECT_PIN, GPIO_PIN_RESET); + HAL_Delay(100); +#endif + RTC->BKP0R = 0xDEADBEEF; + NVIC_SystemReset(); +} + +COMMAND("bootloader", bootloader, "enter bootloader"); + +void reset(char *ptr) { + HAL_NVIC_SystemReset(); +} +COMMAND("reset", reset, "reset STMBL"); + +int main(void) { + // Relocate interrupt vectors + extern void *g_pfnVectors; + SCB->VTOR = (uint32_t)&g_pfnVectors; + + // reset + // IWDG->KR = 0xAAAA; + + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + HAL_Init(); + + /* Configure the system clock */ + SystemClock_Config(); + systick_freq = HAL_RCC_GetHCLKFreq(); + /* Initialize all configured peripherals */ + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOF_CLK_ENABLE(); + + GPIO_InitTypeDef GPIO_InitStruct; + + GPIO_InitStruct.Pin = LED_Pin | GPIO_PIN_14 | GPIO_PIN_15; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct); + HAL_GPIO_WritePin(LED_GPIO_Port, GPIO_PIN_15, GPIO_PIN_RESET); + HAL_GPIO_WritePin(LED_GPIO_Port, GPIO_PIN_14, GPIO_PIN_SET); + +#ifdef USB_DISCONNECT_PIN + GPIO_InitStruct.Pin = USB_DISCONNECT_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(USB_DISCONNECT_PORT, &GPIO_InitStruct); + HAL_GPIO_WritePin(USB_DISCONNECT_PORT, USB_DISCONNECT_PIN, GPIO_PIN_RESET); +#endif + + MX_TIM1_Init(); + MX_ADC1_Init(); + MX_ADC2_Init(); + MX_ADC3_Init(); + MX_ADC4_Init(); + + HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); + + MX_OPAMP2_Init(); + MX_OPAMP3_Init(); + MX_OPAMP4_Init(); + +#ifdef USB_TERM + MX_USB_DEVICE_Init(); +#endif + +#ifdef USB_CONNECT_PIN + GPIO_InitStruct.Pin = USB_CONNECT_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; + HAL_GPIO_Init(USB_CONNECT_PORT, &GPIO_InitStruct); + HAL_GPIO_WritePin(USB_CONNECT_PORT, USB_CONNECT_PIN, GPIO_PIN_SET); +#endif + + __HAL_RCC_DMA1_CLK_ENABLE(); + __HAL_RCC_DMA2_CLK_ENABLE(); + __HAL_RCC_RTC_ENABLE(); + + /* USER CODE BEGIN 2 */ + HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED); + HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED); + HAL_ADCEx_Calibration_Start(&hadc3, ADC_SINGLE_ENDED); + HAL_ADCEx_Calibration_Start(&hadc4, ADC_SINGLE_ENDED); + + HAL_OPAMP_SelfCalibrate(&hopamp2); + HAL_OPAMP_SelfCalibrate(&hopamp3); + HAL_OPAMP_SelfCalibrate(&hopamp4); + + hcrc.Instance = CRC; + hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE; + hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE; + hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE; + hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE; + hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_WORDS; + + __HAL_RCC_CRC_CLK_ENABLE(); + + if(HAL_CRC_Init(&hcrc) != HAL_OK) { + Error_Handler(); + } + + if(HAL_OPAMP_Start(&hopamp2) != HAL_OK) { + Error_Handler(); + } + if(HAL_OPAMP_Start(&hopamp3) != HAL_OK) { + Error_Handler(); + } + if(HAL_OPAMP_Start(&hopamp4) != HAL_OK) { + Error_Handler(); + } + + HAL_ADC_Start(&hadc1); + HAL_ADC_Start(&hadc2); + HAL_ADC_Start(&hadc3); + HAL_ADC_Start(&hadc4); + + + + hal_init(1.0 / RT_FREQ, 0.0); + // hal load comps + hal_parse("debug_level 1"); + + hal_parse("load term"); + // hal_parse("load sim"); + // hal_parse("load io"); + // hal_parse("load dq_sp"); + // hal_parse("load idq"); + // hal_parse("load tle"); + // hal_parse("load pole"); + // hal_parse("load map"); + // hal_parse("load vel"); + // hal_parse("load vel"); + // hal_parse("load ypid"); + // // hal_parse("load rl")); + // hal_parse("load hv"); + // hal_parse("load curpid"); + + hal_parse("io0.rt_prio = 1.0"); + hal_parse("tle0.rt_prio = 2.0"); + hal_parse("map0.rt_prio = 3.0"); + hal_parse("vel0.rt_prio = 4.0"); + hal_parse("vel1.rt_prio = 4.0"); + hal_parse("pole0.rt_prio = 4.0"); + hal_parse("dq_sp0.rt_prio = 5.0"); + hal_parse("ypid0.rt_prio = 6.0"); + hal_parse("trc0.rt_prio = 6.5"); + hal_parse("curpid0.rt_prio = 7.0"); + hal_parse("idq0.rt_prio = 8.0"); + hal_parse("rl0.rt_prio = 8.0"); + hal_parse("hv0.rt_prio = 9.0"); + + hal_parse("term0.rt_prio = 10"); + // hal_parse("sim0.rt_prio = 10.0"); + + hal_parse("term0.send_step = 10.0"); + hal_parse("term0.gain0 = 20.0"); + hal_parse("term0.gain1 = 20.0"); + hal_parse("term0.gain2 = 1.0"); + hal_parse("term0.gain3 = 1.0"); + hal_parse("term0.gain4 = 10.0"); + hal_parse("term0.gain5 = 10.0"); + hal_parse("term0.gain6 = 10.0"); + hal_parse("term0.gain7 = 10.0"); + + hal_parse("hv0.a = idq0.a"); + hal_parse("hv0.b = idq0.b"); + hal_parse("hv0.udc = io0.dc_link"); + hal_parse("hv0.hv_temp = io0.hv_temp"); + + hal_parse("curpid0.id_fb = dq_sp0.d"); + hal_parse("curpid0.iq_fb = dq_sp0.q"); + hal_parse("idq0.d = curpid0.ud"); + hal_parse("idq0.q = curpid0.uq"); + hal_parse("dq_sp0.a = io0.ia"); + hal_parse("dq_sp0.b = io0.ib"); + + hal_parse("curpid0.rd = 1.6"); + hal_parse("curpid0.rq = 1.6"); + hal_parse("curpid0.ld = 0.0038"); + hal_parse("curpid0.lq = 0.0038"); + hal_parse("curpid0.psi = 0.005"); + hal_parse("curpid0.kp = 1.0"); + hal_parse("curpid0.ki = 0.0005"); + hal_parse("curpid0.ff = 1"); + hal_parse("curpid0.kind = 0"); + hal_parse("curpid0.max_cur = 3.1"); + hal_parse("curpid0.pwm_volt = io0.dc_link"); + + hal_parse("pole0.p = 50.0"); + // hal_parse("io0.led = term0.con"); + + hal_parse("dq_sp0.pos = pole0.cpos"); + hal_parse("idq0.pos = pole0.cpos"); + hal_parse("pole0.pos = map0.pos_out2"); + hal_parse("map0.pos_in = tle0.pos"); + hal_parse("map0.freq = 0.1"); + hal_parse("curpid0.cmd_mode = 1"); + hal_parse("vel1.pos_in = map0.pos_out2"); + hal_parse("ypid0.pos_ext_cmd = vel0.pos_out"); + hal_parse("ypid0.vel_ext_cmd = vel0.vel"); + hal_parse("ypid0.pos_fb = map0.pos_out2"); + hal_parse("ypid0.vel_fb = vel1.vel"); + hal_parse("curpid0.iq_cmd = ypid0.out"); + // hal_parse("trc0.in = ypid0.out"); + // hal_parse("curpid0.iq_cmd = trc0.out"); + // hal_parse("trc0.pos = map0.pos_out2"); + // hal_parse("trc0.mult = 200"); + + hal_parse("ypid0.max_vel = 50"); + hal_parse("ypid0.max_acc = 10000"); + hal_parse("ypid0.max_out = 3"); + hal_parse("ypid0.pos_p = 100"); + hal_parse("ypid0.vel_p = 0.05"); + hal_parse("ypid0.vel_i = 0.05"); + hal_parse("ypid0.vel_ff = 1.0"); + + hal_parse("term0.wave0 = vel0.pos_out"); + hal_parse("term0.wave1 = map0.pos_out2"); + hal_parse("term0.wave2 = vel0.vel"); + hal_parse("term0.wave3 = vel1.vel"); + + hal_parse("vel0.w = 3000"); + hal_parse("vel1.w = 3000"); + + hal_parse("vel0.en = 1"); + hal_parse("vel1.en = 1"); + hal_parse("ypid0.enable = 1"); + hal_parse("curpid0.en = 1"); + hal_parse("hv0.en = 1"); + + hal_parse("rl0.ua_fb = hv0.a_fb"); + hal_parse("rl0.ub_fb = hv0.b_fb"); + hal_parse("rl0.ia_fb = io0.ia"); + hal_parse("rl0.ib_fb = io0.ib"); + hal_parse("term0.wave4 = rl0.ra"); + hal_parse("term0.wave5 = rl0.rb"); + hal_parse("term0.wave0 = rl0.t"); + hal_parse("term0.wave1 = rl0.state"); + + // hal_parse("hv0.a = rl0.ua"); + // hal_parse("hv0.b = rl0.ub"); + + hal_parse("flashloadconf"); + hal_parse("loadconf"); + + hal_parse("debug_level 0"); + + // reset + IWDG->KR = 0xAAAA; + + hal_parse("start"); + + // reset + IWDG->KR = 0xAAAA; + + // enable access + IWDG->KR = 0x5555; + + // set reaload 0.002s + IWDG->RLR = 0.002 * 40000 / 4; + + HAL_TIM_Base_Start(&htim1); + HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); + HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); + HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1); + HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2); + + while(1) { + hal_run_nrt(); + cdc_poll(); + HAL_Delay(1); + } +} + +/** System Clock Configuration +*/ +void SystemClock_Config(void) { + RCC_OscInitTypeDef RCC_OscInitStruct; + RCC_ClkInitTypeDef RCC_ClkInitStruct; + RCC_PeriphCLKInitTypeDef PeriphClkInit; + + /**Initializes the CPU, AHB and APB busses clocks + */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; + RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.LSIState = RCC_LSI_ON; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; + if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { + Error_Handler(); + } + + /**Initializes the CPU, AHB and APB busses clocks + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; + + if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { + Error_Handler(); + } + + //TODO: usb optional + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB | RCC_PERIPHCLK_USART1 | RCC_PERIPHCLK_USART3 | RCC_PERIPHCLK_TIM8 | RCC_PERIPHCLK_ADC12 | RCC_PERIPHCLK_ADC34 | RCC_PERIPHCLK_RTC; + PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2; + PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_SYSCLK; + PeriphClkInit.Adc12ClockSelection = RCC_ADC12PLLCLK_DIV1; + PeriphClkInit.Adc34ClockSelection = RCC_ADC34PLLCLK_DIV1; + PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5; + PeriphClkInit.Tim8ClockSelection = RCC_TIM8CLK_PLLCLK; + PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; + if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { + Error_Handler(); + } + + /**Configure the Systick interrupt time + */ + HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000); + + /**Configure the Systick + */ + HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); + + /* SysTick_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); +} + +/** + * @brief This function is executed in case of error occurrence. + * @param None + * @retval None + */ +void Error_Handler(void) { + /* User can add his own implementation to report the HAL error return state */ + while(1) { + } +} + +#ifdef USE_FULL_ASSERT + +/** + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +void assert_failed(uint8_t *file, uint32_t line) { + /* USER CODE BEGIN 6 */ + /* User can add his own implementation to report the file name and line number, + ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* USER CODE END 6 */ +} + +#endif diff --git a/stmsp/stm32f303/src/opamp.c b/stmsp/stm32f303/src/opamp.c new file mode 100644 index 00000000..1b2c18de --- /dev/null +++ b/stmsp/stm32f303/src/opamp.c @@ -0,0 +1,186 @@ +/* +* This file is part of the stmbl project. +* +* Copyright (C) 2013-2017 Rene Hopf +* Copyright (C) 2013-2017 Nico Stute +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +/* Includes ------------------------------------------------------------------*/ +#include "opamp.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +OPAMP_HandleTypeDef hopamp2; +OPAMP_HandleTypeDef hopamp3; +OPAMP_HandleTypeDef hopamp4; + +/* OPAMP2 init function */ +void MX_OPAMP2_Init(void) { + hopamp2.Instance = OPAMP2; + hopamp2.Init.Mode = OPAMP_PGA_MODE; + hopamp2.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0; + hopamp2.Init.TimerControlledMuxmode = OPAMP_TIMERCONTROLLEDMUXMODE_DISABLE; + hopamp2.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO; + hopamp2.Init.PgaGain = OPAMP_PGA_GAIN_16; + hopamp2.Init.UserTrimming = OPAMP_TRIMMING_FACTORY; + if(HAL_OPAMP_Init(&hopamp2) != HAL_OK) { + Error_Handler(); + } +} +/* OPAMP3 init function */ +void MX_OPAMP3_Init(void) { + hopamp3.Instance = OPAMP3; + hopamp3.Init.Mode = OPAMP_PGA_MODE; + hopamp3.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO0; + hopamp3.Init.TimerControlledMuxmode = OPAMP_TIMERCONTROLLEDMUXMODE_DISABLE; + hopamp3.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO; + hopamp3.Init.PgaGain = OPAMP_PGA_GAIN_16; + hopamp3.Init.UserTrimming = OPAMP_TRIMMING_FACTORY; + if(HAL_OPAMP_Init(&hopamp3) != HAL_OK) { + Error_Handler(); + } +} + +void MX_OPAMP4_Init(void) { + hopamp4.Instance = OPAMP4; + hopamp4.Init.Mode = OPAMP_PGA_MODE; + hopamp4.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_IO3; + hopamp4.Init.TimerControlledMuxmode = OPAMP_TIMERCONTROLLEDMUXMODE_DISABLE; + hopamp4.Init.PgaConnect = OPAMP_PGA_CONNECT_INVERTINGINPUT_NO; + hopamp4.Init.PgaGain = OPAMP_PGA_GAIN_8; + hopamp4.Init.UserTrimming = OPAMP_TRIMMING_FACTORY; + if(HAL_OPAMP_Init(&hopamp4) != HAL_OK) { + Error_Handler(); + } +} + +void HAL_OPAMP_MspInit(OPAMP_HandleTypeDef *opampHandle) { + GPIO_InitTypeDef GPIO_InitStruct; + if(opampHandle->Instance == OPAMP2) { + /* USER CODE BEGIN OPAMP2_MspInit 0 */ + + /* USER CODE END OPAMP2_MspInit 0 */ + + /**OPAMP2 GPIO Configuration + PA6 ------> OPAMP2_VOUT + PA7 ------> OPAMP2_VINP + */ + GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN OPAMP2_MspInit 1 */ + + /* USER CODE END OPAMP2_MspInit 1 */ + } else if(opampHandle->Instance == OPAMP3) { + /* USER CODE BEGIN OPAMP3_MspInit 0 */ + + /* USER CODE END OPAMP3_MspInit 0 */ + + /**OPAMP3 GPIO Configuration + PB0 ------> OPAMP3_VINP + PB1 ------> OPAMP3_VOUT + */ + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN OPAMP3_MspInit 1 */ + + /* USER CODE END OPAMP3_MspInit 1 */ + } else if(opampHandle->Instance == OPAMP4) { + /* USER CODE BEGIN OPAMP4_MspInit 0 */ + + /* USER CODE END OPAMP4_MspInit 0 */ + + /**OPAMP4 GPIO Configuration + PB11 ------> OPAMP4_VINP + PB12 ------> OPAMP4_VOUT + */ + GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN OPAMP4_MspInit 1 */ + + /* USER CODE END OPAMP4_MspInit 1 */ + } +} + +void HAL_OPAMP_MspDeInit(OPAMP_HandleTypeDef *opampHandle) { +if(opampHandle->Instance == OPAMP2) { + /* USER CODE BEGIN OPAMP2_MspDeInit 0 */ + + /* USER CODE END OPAMP2_MspDeInit 0 */ + + /**OPAMP2 GPIO Configuration + PA6 ------> OPAMP2_VOUT + PA7 ------> OPAMP2_VINP + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_6 | GPIO_PIN_7); + + /* USER CODE BEGIN OPAMP2_MspDeInit 1 */ + + /* USER CODE END OPAMP2_MspDeInit 1 */ + } else if(opampHandle->Instance == OPAMP3) { + /* USER CODE BEGIN OPAMP3_MspDeInit 0 */ + + /* USER CODE END OPAMP3_MspDeInit 0 */ + + /**OPAMP3 GPIO Configuration + PB0 ------> OPAMP3_VINP + PB1 ------> OPAMP3_VOUT + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0 | GPIO_PIN_1); + + /* USER CODE BEGIN OPAMP3_MspDeInit 1 */ + + /* USER CODE END OPAMP3_MspDeInit 1 */ + } else if(opampHandle->Instance == OPAMP4) { + /* USER CODE BEGIN OPAMP3_MspDeInit 0 */ + + /* USER CODE END OPAMP3_MspDeInit 0 */ + + /**OPAMP3 GPIO Configuration + PB0 ------> OPAMP3_VINP + PB1 ------> OPAMP3_VOUT + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_11 | GPIO_PIN_12); + + /* USER CODE BEGIN OPAMP3_MspDeInit 1 */ + + /* USER CODE END OPAMP3_MspDeInit 1 */ + } +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/src/stm32f3xx_hal_msp.c b/stmsp/stm32f303/src/stm32f3xx_hal_msp.c new file mode 100644 index 00000000..857c7192 --- /dev/null +++ b/stmsp/stm32f303/src/stm32f3xx_hal_msp.c @@ -0,0 +1,96 @@ +/** + ****************************************************************************** + * File Name : stm32f3xx_hal_msp.c + * Description : This file provides code for the MSP Initialization + * and de-Initialization codes. + ****************************************************************************** + * + * Copyright (c) 2016 STMicroelectronics International N.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted, provided that the following conditions are met: + * + * 1. Redistribution of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific written permission. + * 4. This software, including modifications and/or derivative works of this + * software, must execute solely and exclusively on microcontroller or + * microprocessor devices manufactured by or for STMicroelectronics. + * 5. Redistribution and use of this software other than as permitted under + * this license is void and will automatically terminate your rights under + * this license. + * + * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY + * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT + * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +/* Includes ------------------------------------------------------------------*/ +#include "stm32f3xx_hal.h" + +extern void Error_Handler(void); +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ +/** + * Initializes the Global MSP. + */ +void HAL_MspInit(void) { + /* USER CODE BEGIN MspInit 0 */ + + /* USER CODE END MspInit 0 */ + + __HAL_RCC_SYSCFG_CLK_ENABLE(); + + HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); + + /* System interrupt init*/ + /* MemoryManagement_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(MemoryManagement_IRQn, 0, 0); + /* BusFault_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(BusFault_IRQn, 0, 0); + /* UsageFault_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(UsageFault_IRQn, 0, 0); + /* SVCall_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(SVCall_IRQn, 0, 0); + /* DebugMonitor_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DebugMonitor_IRQn, 0, 0); + /* PendSV_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(PendSV_IRQn, 0, 0); + /* SysTick_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(SysTick_IRQn, 14, 0); + + /* USER CODE BEGIN MspInit 1 */ + + /* USER CODE END MspInit 1 */ +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/src/stm32f3xx_it.c b/stmsp/stm32f303/src/stm32f3xx_it.c new file mode 100644 index 00000000..7c36e536 --- /dev/null +++ b/stmsp/stm32f303/src/stm32f3xx_it.c @@ -0,0 +1,177 @@ +/** + ****************************************************************************** + * @file stm32f3xx_it.c + * @brief Interrupt Service Routines. + ****************************************************************************** + * + * COPYRIGHT(c) 2016 STMicroelectronics + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +/* Includes ------------------------------------------------------------------*/ +#include "stm32f3xx_hal.h" +#include "stm32f3xx.h" +#include "stm32f3xx_it.h" +#include "hal.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/* External variables --------------------------------------------------------*/ +#ifdef USB_TERM +extern PCD_HandleTypeDef hpcd_USB_FS; +#endif +/******************************************************************************/ +/* Cortex-M4 Processor Interruption and Exception Handlers */ +/******************************************************************************/ + +/** +* @brief This function handles Non maskable interrupt. +*/ +void NMI_Handler(void) { + /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ + hal_error(NMI); + HAL_NVIC_DisableIRQ(DMA1_Channel1_IRQn); + while(1){} + /* USER CODE END NonMaskableInt_IRQn 0 */ + /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ + + /* USER CODE END NonMaskableInt_IRQn 1 */ +} + +/** +* @brief This function handles Hard fault interrupt. +*/ +void HardFault_Handler(void) { + /* USER CODE BEGIN HardFault_IRQn 0 */ + + /* USER CODE END HardFault_IRQn 0 */ + hal_error(HardFault); + HAL_NVIC_DisableIRQ(DMA1_Channel1_IRQn); + while(1){} + /* USER CODE BEGIN HardFault_IRQn 1 */ + + /* USER CODE END HardFault_IRQn 1 */ +} + +/** +* @brief This function handles Memory management fault. +*/ +void MemManage_Handler(void) { + /* USER CODE BEGIN MemoryManagement_IRQn 0 */ + + /* USER CODE END MemoryManagement_IRQn 0 */ + hal_error(MemManage); + HAL_NVIC_DisableIRQ(DMA1_Channel1_IRQn); + while(1){} + /* USER CODE BEGIN MemoryManagement_IRQn 1 */ + + /* USER CODE END MemoryManagement_IRQn 1 */ +} + +/** +* @brief This function handles Pre-fetch fault, memory access fault. +*/ +void BusFault_Handler(void) { + /* USER CODE BEGIN BusFault_IRQn 0 */ + + /* USER CODE END BusFault_IRQn 0 */ + hal_error(BusFault); + HAL_NVIC_DisableIRQ(DMA1_Channel1_IRQn); + while(1){} + /* USER CODE BEGIN BusFault_IRQn 1 */ + + /* USER CODE END BusFault_IRQn 1 */ +} + +/** +* @brief This function handles Undefined instruction or illegal state. +*/ +void UsageFault_Handler(void) { + /* USER CODE BEGIN UsageFault_IRQn 0 */ + + /* USER CODE END UsageFault_IRQn 0 */ + hal_error(UsageFault); + HAL_NVIC_DisableIRQ(DMA1_Channel1_IRQn); + while(1){} + /* USER CODE BEGIN UsageFault_IRQn 1 */ + + /* USER CODE END UsageFault_IRQn 1 */ +} + +/** +* @brief This function handles Debug monitor. +*/ +void DebugMon_Handler(void) { + /* USER CODE BEGIN DebugMonitor_IRQn 0 */ + + /* USER CODE END DebugMonitor_IRQn 0 */ + /* USER CODE BEGIN DebugMonitor_IRQn 1 */ + + /* USER CODE END DebugMonitor_IRQn 1 */ +} + +/** +* @brief This function handles System tick timer. +*/ +// void SysTick_Handler(void) { +// /* USER CODE BEGIN SysTick_IRQn 0 */ + +// /* USER CODE END SysTick_IRQn 0 */ +// HAL_IncTick(); +// HAL_SYSTICK_IRQHandler(); +// /* USER CODE BEGIN SysTick_IRQn 1 */ + +// /* USER CODE END SysTick_IRQn 1 */ +// } + +/******************************************************************************/ +/* STM32F3xx Peripheral Interrupt Handlers */ +/* Add here the Interrupt Handlers for the used peripherals. */ +/* For the available peripheral interrupt handler names, */ +/* please refer to the startup file (startup_stm32f3xx.s). */ +/******************************************************************************/ + +/** +* @brief This function handles USB low priority or CAN_RX0 interrupts. +*/ +void USB_LP_CAN_RX0_IRQHandler(void) { +/* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 0 */ +//GPIOA->BSRR |= GPIO_PIN_10; + +/* USER CODE END USB_LP_CAN_RX0_IRQn 0 */ +#ifdef USB_TERM + HAL_PCD_IRQHandler(&hpcd_USB_FS); +#endif + /* USER CODE BEGIN USB_LP_CAN_RX0_IRQn 1 */ + //GPIOA->BSRR |= GPIO_PIN_10 << 16; + /* USER CODE END USB_LP_CAN_RX0_IRQn 1 */ +} + +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/src/usb_device.c b/stmsp/stm32f303/src/usb_device.c new file mode 100644 index 00000000..d132e2d8 --- /dev/null +++ b/stmsp/stm32f303/src/usb_device.c @@ -0,0 +1,75 @@ +/** + ****************************************************************************** + * @file : USB_DEVICE + * @version : v1.0_Cube + * @brief : This file implements the USB Device + ****************************************************************************** + * + * Copyright (c) 2016 STMicroelectronics International N.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted, provided that the following conditions are met: + * + * 1. Redistribution of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific written permission. + * 4. This software, including modifications and/or derivative works of this + * software, must execute solely and exclusively on microcontroller or + * microprocessor devices manufactured by or for STMicroelectronics. + * 5. Redistribution and use of this software other than as permitted under + * this license is void and will automatically terminate your rights under + * this license. + * + * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY + * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT + * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** +*/ + +/* Includes ------------------------------------------------------------------*/ + +#include "usb_device.h" +#include "usbd_core.h" +#include "usbd_desc.h" +#include "usbd_cdc.h" +#include "usbd_cdc_if.h" + +/* USB Device Core handle declaration */ +USBD_HandleTypeDef hUsbDeviceFS; + +/* init function */ +void MX_USB_DEVICE_Init(void) { + /* Init Device Library,Add Supported Class and Start the library*/ + USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS); + + USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC); + + USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS); + + USBD_Start(&hUsbDeviceFS); +} +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/src/usbd_cdc_if.c b/stmsp/stm32f303/src/usbd_cdc_if.c new file mode 100644 index 00000000..6d2368fe --- /dev/null +++ b/stmsp/stm32f303/src/usbd_cdc_if.c @@ -0,0 +1,170 @@ +#include "usbd_cdc_if.h" +#include "ringbuf.h" +#define RX_QUEUE_SIZE 1024 +#define TX_QUEUE_SIZE 4096 + +struct ringbuf rx_buf = RINGBUF(RX_QUEUE_SIZE); +struct ringbuf tx_buf = RINGBUF(TX_QUEUE_SIZE); + +#define APP_RX_DATA_SIZE 64 +#define APP_TX_DATA_SIZE 64 +uint8_t UserRxBufferFS[APP_RX_DATA_SIZE]; +uint8_t UserTxBufferFS[APP_TX_DATA_SIZE]; +extern USBD_HandleTypeDef hUsbDeviceFS; + +static int8_t CDC_Init_FS(void); +static int8_t CDC_DeInit_FS(void); +static int8_t CDC_Control_FS(uint8_t cmd, uint8_t *pbuf, uint16_t length); +static int8_t CDC_Receive_FS(uint8_t *pbuf, uint32_t *Len); + +USBD_CDC_ItfTypeDef USBD_Interface_fops_FS = + { + CDC_Init_FS, + CDC_DeInit_FS, + CDC_Control_FS, + CDC_Receive_FS}; + +static int8_t CDC_Init_FS(void) { + /* USER CODE BEGIN 3 */ + /* Set Application Buffers */ + USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0); + USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS); + return (USBD_OK); + /* USER CODE END 3 */ +} + +static int8_t CDC_DeInit_FS(void) { + /* USER CODE BEGIN 4 */ + return (USBD_OK); + /* USER CODE END 4 */ +} + +static int8_t CDC_Control_FS(uint8_t cmd, uint8_t *pbuf, uint16_t length) { + /* USER CODE BEGIN 5 */ + switch(cmd) { + case CDC_SEND_ENCAPSULATED_COMMAND: + + break; + + case CDC_GET_ENCAPSULATED_RESPONSE: + + break; + + case CDC_SET_COMM_FEATURE: + + break; + + case CDC_GET_COMM_FEATURE: + + break; + + case CDC_CLEAR_COMM_FEATURE: + + break; + + /*******************************************************************************/ + /* Line Coding Structure */ + /*-----------------------------------------------------------------------------*/ + /* Offset | Field | Size | Value | Description */ + /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/ + /* 4 | bCharFormat | 1 | Number | Stop bits */ + /* 0 - 1 Stop bit */ + /* 1 - 1.5 Stop bits */ + /* 2 - 2 Stop bits */ + /* 5 | bParityType | 1 | Number | Parity */ + /* 0 - None */ + /* 1 - Odd */ + /* 2 - Even */ + /* 3 - Mark */ + /* 4 - Space */ + /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */ + /*******************************************************************************/ + case CDC_SET_LINE_CODING: + + break; + + case CDC_GET_LINE_CODING: + + break; + + case CDC_SET_CONTROL_LINE_STATE: + + break; + + case CDC_SEND_BREAK: + + break; + + default: + break; + } + + return (USBD_OK); + /* USER CODE END 5 */ +} + +static int8_t CDC_Receive_FS(uint8_t *Buf, uint32_t *Len) { + /* USER CODE BEGIN 6 */ + rb_write(&rx_buf, Buf, *Len); + USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]); + USBD_CDC_ReceivePacket(&hUsbDeviceFS); + return (USBD_OK); + /* USER CODE END 6 */ +} + +uint8_t CDC_Transmit_FS(uint8_t *Buf, uint16_t Len) { + uint8_t result = USBD_OK; + /* USER CODE BEGIN 7 */ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)hUsbDeviceFS.pClassData; + if(hcdc->TxState != 0) { + return USBD_BUSY; + } + USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len); + result = USBD_CDC_TransmitPacket(&hUsbDeviceFS); + /* USER CODE END 7 */ + return result; +} + +/* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */ +void cdc_usbtx() { + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)hUsbDeviceFS.pClassData; + if(hcdc->TxState != 0) { + return; //busy + } + int len = rb_read(&tx_buf, UserTxBufferFS, sizeof(UserTxBufferFS)); + USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, len); + USBD_CDC_TransmitPacket(&hUsbDeviceFS); +} + +void cdc_poll() { + if(cdc_is_connected()) { + cdc_usbtx(); + } +} + +int cdc_is_connected() { + if(hUsbDeviceFS.dev_state != USBD_STATE_CONFIGURED) { + return 0; + } else { + return 1; + } +} + +int cdc_tx(void *data, uint32_t len) { + if(cdc_is_connected()) { + int ret; + ret = rb_write(&tx_buf, data, len); + cdc_usbtx(); + return ret; + } else { + return 0; + } + } + +int cdc_getline(char *ptr, int len) { + return rb_getline(&rx_buf, ptr, len); +} + +int _write(int file, char *ptr, int len) { + return cdc_tx(ptr, len); +} diff --git a/stmsp/stm32f303/src/usbd_conf.c b/stmsp/stm32f303/src/usbd_conf.c new file mode 100644 index 00000000..c90da411 --- /dev/null +++ b/stmsp/stm32f303/src/usbd_conf.c @@ -0,0 +1,494 @@ +/** + ****************************************************************************** + * @file : usbd_conf.c + * @version : v1.0_Cube + * @brief : This file implements the board support package for the USB device library + ****************************************************************************** + * + * Copyright (c) 2016 STMicroelectronics International N.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted, provided that the following conditions are met: + * + * 1. Redistribution of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific written permission. + * 4. This software, including modifications and/or derivative works of this + * software, must execute solely and exclusively on microcontroller or + * microprocessor devices manufactured by or for STMicroelectronics. + * 5. Redistribution and use of this software other than as permitted under + * this license is void and will automatically terminate your rights under + * this license. + * + * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY + * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT + * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** +*/ +/* Includes ------------------------------------------------------------------*/ +#include "stm32f3xx.h" +#include "stm32f3xx_hal.h" +#include "usbd_def.h" +#include "usbd_core.h" +#include "usbd_cdc.h" +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +PCD_HandleTypeDef hpcd_USB_FS; +void Error_Handler(void); + +/* USER CODE BEGIN 0 */ +/* USER CODE END 0 */ + +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ +/* USER CODE BEGIN 1 */ +/* USER CODE END 1 */ +void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state); + +/******************************************************************************* + LL Driver Callbacks (PCD -> USB Device Library) +*******************************************************************************/ +/* MSP Init */ + +void HAL_PCD_MspInit(PCD_HandleTypeDef *pcdHandle) { + GPIO_InitTypeDef GPIO_InitStruct; + if(pcdHandle->Instance == USB) { + /* USER CODE BEGIN USB_MspInit 0 */ + + /* USER CODE END USB_MspInit 0 */ + + /**USB GPIO Configuration + PA11 ------> USB_DM + PA12 ------> USB_DP + */ + GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF14_USB; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* Peripheral clock enable */ + __HAL_RCC_USB_CLK_ENABLE(); + + /* Peripheral interrupt init */ + HAL_NVIC_SetPriority(USB_LP_CAN_RX0_IRQn, 10, 0); + HAL_NVIC_EnableIRQ(USB_LP_CAN_RX0_IRQn); + /* USER CODE BEGIN USB_MspInit 1 */ + + /* USER CODE END USB_MspInit 1 */ + } +} + +void HAL_PCD_MspDeInit(PCD_HandleTypeDef *pcdHandle) { + if(pcdHandle->Instance == USB) { + /* USER CODE BEGIN USB_MspDeInit 0 */ + + /* USER CODE END USB_MspDeInit 0 */ + /* Peripheral clock disable */ + __HAL_RCC_USB_CLK_DISABLE(); + + /**USB GPIO Configuration + PA11 ------> USB_DM + PA12 ------> USB_DP + */ + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11 | GPIO_PIN_12); + + /* Peripheral interrupt Deinit*/ + HAL_NVIC_DisableIRQ(USB_LP_CAN_RX0_IRQn); + + /* USER CODE BEGIN USB_MspDeInit 1 */ + + /* USER CODE END USB_MspDeInit 1 */ + } +} + +/** + * @brief Setup Stage callback + * @param hpcd: PCD handle + * @retval None + */ +void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd) { + USBD_LL_SetupStage((USBD_HandleTypeDef *)hpcd->pData, (uint8_t *)hpcd->Setup); +} + +/** + * @brief Data Out Stage callback. + * @param hpcd: PCD handle + * @param epnum: Endpoint Number + * @retval None + */ +void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { + USBD_LL_DataOutStage((USBD_HandleTypeDef *)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff); +} + +/** + * @brief Data In Stage callback.. + * @param hpcd: PCD handle + * @param epnum: Endpoint Number + * @retval None + */ +void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { + USBD_LL_DataInStage((USBD_HandleTypeDef *)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff); +} + +/** + * @brief SOF callback. + * @param hpcd: PCD handle + * @retval None + */ +void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { + USBD_LL_SOF((USBD_HandleTypeDef *)hpcd->pData); +} + +/** + * @brief Reset callback. + * @param hpcd: PCD handle + * @retval None + */ +void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd) { + USBD_SpeedTypeDef speed = USBD_SPEED_FULL; + + /*Set USB Current Speed*/ + switch(hpcd->Init.speed) { + case PCD_SPEED_FULL: + speed = USBD_SPEED_FULL; + break; + + default: + speed = USBD_SPEED_FULL; + break; + } + USBD_LL_SetSpeed((USBD_HandleTypeDef *)hpcd->pData, speed); + + /*Reset Device*/ + USBD_LL_Reset((USBD_HandleTypeDef *)hpcd->pData); +} + +/** + * @brief Suspend callback. + * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it) + * @param hpcd: PCD handle + * @retval None + */ +void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd) { + /* Inform USB library that core enters in suspend Mode */ + USBD_LL_Suspend((USBD_HandleTypeDef *)hpcd->pData); + /*Enter in STOP mode */ + /* USER CODE BEGIN 2 */ + if(hpcd->Init.low_power_enable) { + /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register */ + SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk)); + } + /* USER CODE END 2 */ +} + +/** + * @brief Resume callback. + * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it) + * @param hpcd: PCD handle + * @retval None + */ +void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd) { + /* USER CODE BEGIN 3 */ + + /* USER CODE END 3 */ + USBD_LL_Resume((USBD_HandleTypeDef *)hpcd->pData); +} + +/** + * @brief ISOOUTIncomplete callback. + * @param hpcd: PCD handle + * @param epnum: Endpoint Number + * @retval None + */ +void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { + USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef *)hpcd->pData, epnum); +} + +/** + * @brief ISOINIncomplete callback. + * @param hpcd: PCD handle + * @param epnum: Endpoint Number + * @retval None + */ +void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) { + USBD_LL_IsoINIncomplete((USBD_HandleTypeDef *)hpcd->pData, epnum); +} + +/** + * @brief ConnectCallback callback. + * @param hpcd: PCD handle + * @retval None + */ +void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd) { + USBD_LL_DevConnected((USBD_HandleTypeDef *)hpcd->pData); +} + +/** + * @brief Disconnect callback. + * @param hpcd: PCD handle + * @retval None + */ +void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd) { + USBD_LL_DevDisconnected((USBD_HandleTypeDef *)hpcd->pData); +} + +/******************************************************************************* + LL Driver Interface (USB Device Library --> PCD) +*******************************************************************************/ +/** + * @brief Initializes the Low Level portion of the Device driver. + * @param pdev: Device handle + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) { + /* Init USB_IP */ + /* Link The driver to the stack */ + hpcd_USB_FS.pData = pdev; + pdev->pData = &hpcd_USB_FS; + + hpcd_USB_FS.Instance = USB; + hpcd_USB_FS.Init.dev_endpoints = 8; + hpcd_USB_FS.Init.speed = PCD_SPEED_FULL; + hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64; + hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED; + hpcd_USB_FS.Init.low_power_enable = DISABLE; + hpcd_USB_FS.Init.battery_charging_enable = DISABLE; + if(HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK) { + Error_Handler(); + } + + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x00, PCD_SNG_BUF, 0x18); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x80, PCD_SNG_BUF, 0x58); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x81, PCD_SNG_BUF, 0xC0); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x01, PCD_SNG_BUF, 0x110); + HAL_PCDEx_PMAConfig((PCD_HandleTypeDef *)pdev->pData, 0x82, PCD_SNG_BUF, 0x100); + return USBD_OK; +} + +/** + * @brief De-Initializes the Low Level portion of the Device driver. + * @param pdev: Device handle + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev) { + HAL_PCD_DeInit((PCD_HandleTypeDef *)pdev->pData); + return USBD_OK; +} + +/** + * @brief Starts the Low Level portion of the Device driver. + * @param pdev: Device handle + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev) { + HAL_PCD_Start((PCD_HandleTypeDef *)pdev->pData); + return USBD_OK; +} + +/** + * @brief Stops the Low Level portion of the Device driver. + * @param pdev: Device handle + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev) { + HAL_PCD_Stop((PCD_HandleTypeDef *)pdev->pData); + return USBD_OK; +} + +/** + * @brief Opens an endpoint of the Low Level Driver. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @param ep_type: Endpoint Type + * @param ep_mps: Endpoint Max Packet Size + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, + uint8_t ep_type, + uint16_t ep_mps) { + HAL_PCD_EP_Open((PCD_HandleTypeDef *)pdev->pData, + ep_addr, + ep_mps, + ep_type); + + return USBD_OK; +} + +/** + * @brief Closes an endpoint of the Low Level Driver. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + HAL_PCD_EP_Close((PCD_HandleTypeDef *)pdev->pData, ep_addr); + return USBD_OK; +} + +/** + * @brief Flushes an endpoint of the Low Level Driver. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + HAL_PCD_EP_Flush((PCD_HandleTypeDef *)pdev->pData, ep_addr); + return USBD_OK; +} + +/** + * @brief Sets a Stall condition on an endpoint of the Low Level Driver. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + HAL_PCD_EP_SetStall((PCD_HandleTypeDef *)pdev->pData, ep_addr); + return USBD_OK; +} + +/** + * @brief Clears a Stall condition on an endpoint of the Low Level Driver. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + HAL_PCD_EP_ClrStall((PCD_HandleTypeDef *)pdev->pData, ep_addr); + return USBD_OK; +} + +/** + * @brief Returns Stall condition. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval Stall (1: Yes, 0: No) + */ +uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef *)pdev->pData; + + if((ep_addr & 0x80) == 0x80) { + return hpcd->IN_ep[ep_addr & 0x7F].is_stall; + } else { + return hpcd->OUT_ep[ep_addr & 0x7F].is_stall; + } +} +/** + * @brief Assigns a USB address to the device. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr) { + HAL_PCD_SetAddress((PCD_HandleTypeDef *)pdev->pData, dev_addr); + return USBD_OK; +} + +/** + * @brief Transmits data over an endpoint. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @param pbuf: Pointer to data to be sent + * @param size: Data size + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, + uint8_t *pbuf, + uint16_t size) { + HAL_PCD_EP_Transmit((PCD_HandleTypeDef *)pdev->pData, ep_addr, pbuf, size); + return USBD_OK; +} + +/** + * @brief Prepares an endpoint for reception. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @param pbuf: Pointer to data to be received + * @param size: Data size + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, + uint8_t *pbuf, + uint16_t size) { + HAL_PCD_EP_Receive((PCD_HandleTypeDef *)pdev->pData, ep_addr, pbuf, size); + return USBD_OK; +} + +/** + * @brief Returns the last transfered packet size. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval Recived Data Size + */ +uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr) { + return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef *)pdev->pData, ep_addr); +} + +/** + * @brief Delays routine for the USB Device Library. + * @param Delay: Delay in ms + * @retval None + */ +void USBD_LL_Delay(uint32_t Delay) { + HAL_Delay(Delay); +} + +/** + * @brief static single allocation. + * @param size: size of allocated memory + * @retval None + */ +void *USBD_static_malloc(uint32_t size) { + static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef) / 4) + 1]; /* On 32-bit boundary */ + return mem; +} + +/** + * @brief Dummy memory free + * @param *p pointer to allocated memory address + * @retval None + */ +void USBD_static_free(void *p) { +} + +/** +* @brief Software Device Connection +* @param hpcd: PCD handle +* @param state: connection state (0 : disconnected / 1: connected) +* @retval None +*/ +void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) { + /* USER CODE BEGIN 4 */ + if(state == 1) { + /* Configure Low Connection State */ + + } else { + /* Configure High Connection State */ + } + /* USER CODE END 4 */ +} + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/src/usbd_desc.c b/stmsp/stm32f303/src/usbd_desc.c new file mode 100644 index 00000000..256897ff --- /dev/null +++ b/stmsp/stm32f303/src/usbd_desc.c @@ -0,0 +1,288 @@ +/** + ****************************************************************************** + * @file : usbd_desc.c + * @version : v1.0_Cube + * @brief : This file implements the USB Device descriptors + ****************************************************************************** + * + * Copyright (c) 2016 STMicroelectronics International N.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted, provided that the following conditions are met: + * + * 1. Redistribution of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of other + * contributors to this software may be used to endorse or promote products + * derived from this software without specific written permission. + * 4. This software, including modifications and/or derivative works of this + * software, must execute solely and exclusively on microcontroller or + * microprocessor devices manufactured by or for STMicroelectronics. + * 5. Redistribution and use of this software other than as permitted under + * this license is void and will automatically terminate your rights under + * this license. + * + * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY + * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT + * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** +*/ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_core.h" +#include "usbd_desc.h" +#include "usbd_conf.h" + +/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_DESC + * @brief USBD descriptors module + * @{ + */ + +/** @defgroup USBD_DESC_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_DESC_Private_Defines + * @{ + */ +#define USBD_VID 1155 +#define USBD_LANGID_STRING 1033 +#define USBD_MANUFACTURER_STRING "STMicroelectronics" +#define USBD_PID_FS 22336 +#define USBD_PRODUCT_STRING_FS "STM32 Virtual ComPort" +#define USBD_SERIALNUMBER_STRING_FS "00000000001A" +#define USBD_CONFIGURATION_STRING_FS "CDC Config" +#define USBD_INTERFACE_STRING_FS "CDC Interface" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0*/ +/** + * @} + */ + +/** @defgroup USBD_DESC_Private_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_DESC_Private_Variables + * @{ + */ +uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); +uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); + +#ifdef USB_SUPPORT_USER_STRING_DESC +uint8_t *USBD_FS_USRStringDesc(USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length); +#endif /* USB_SUPPORT_USER_STRING_DESC */ + +USBD_DescriptorsTypeDef FS_Desc = + { + USBD_FS_DeviceDescriptor, + USBD_FS_LangIDStrDescriptor, + USBD_FS_ManufacturerStrDescriptor, + USBD_FS_ProductStrDescriptor, + USBD_FS_SerialStrDescriptor, + USBD_FS_ConfigStrDescriptor, + USBD_FS_InterfaceStrDescriptor, +}; + +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 +#endif +/* USB Standard Device Descriptor */ +__ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = + { + 0x12, /*bLength */ + USB_DESC_TYPE_DEVICE, /*bDescriptorType*/ + 0x00, /* bcdUSB */ + 0x02, + 0x02, /*bDeviceClass*/ + 0x02, /*bDeviceSubClass*/ + 0x00, /*bDeviceProtocol*/ + USB_MAX_EP0_SIZE, /*bMaxPacketSize*/ + LOBYTE(USBD_VID), /*idVendor*/ + HIBYTE(USBD_VID), /*idVendor*/ + LOBYTE(USBD_PID_FS), /*idVendor*/ + HIBYTE(USBD_PID_FS), /*idVendor*/ + 0x00, /*bcdDevice rel. 2.00*/ + 0x02, + USBD_IDX_MFC_STR, /*Index of manufacturer string*/ + USBD_IDX_PRODUCT_STR, /*Index of product string*/ + USBD_IDX_SERIAL_STR, /*Index of serial number string*/ + USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/ +}; +/* USB_DeviceDescriptor */ + +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 +#endif + +/* USB Standard Device Descriptor */ +__ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = + { + USB_LEN_LANGID_STR_DESC, + USB_DESC_TYPE_STRING, + LOBYTE(USBD_LANGID_STRING), + HIBYTE(USBD_LANGID_STRING), +}; + +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 +#endif +__ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END; +/** + * @} + */ + +/** @defgroup USBD_DESC_Private_FunctionPrototypes + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_DESC_Private_Functions + * @{ + */ + +/** +* @brief USBD_FS_DeviceDescriptor +* return the device descriptor +* @param speed : current device speed +* @param length : pointer to data length variable +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + *length = sizeof(USBD_FS_DeviceDesc); + return USBD_FS_DeviceDesc; +} + +/** +* @brief USBD_FS_LangIDStrDescriptor +* return the LangID string descriptor +* @param speed : current device speed +* @param length : pointer to data length variable +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + *length = sizeof(USBD_LangIDDesc); + return USBD_LangIDDesc; +} + +/** +* @brief USBD_FS_ProductStrDescriptor +* return the product string descriptor +* @param speed : current device speed +* @param length : pointer to data length variable +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + if(speed == 0) { + USBD_GetString(USBD_PRODUCT_STRING_FS, USBD_StrDesc, length); + } else { + USBD_GetString(USBD_PRODUCT_STRING_FS, USBD_StrDesc, length); + } + return USBD_StrDesc; +} + +/** +* @brief USBD_FS_ManufacturerStrDescriptor +* return the manufacturer string descriptor +* @param speed : current device speed +* @param length : pointer to data length variable +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + USBD_GetString(USBD_MANUFACTURER_STRING, USBD_StrDesc, length); + return USBD_StrDesc; +} + +/** +* @brief USBD_FS_SerialStrDescriptor +* return the serial number string descriptor +* @param speed : current device speed +* @param length : pointer to data length variable +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + if(speed == USBD_SPEED_HIGH) { + USBD_GetString(USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length); + } else { + USBD_GetString(USBD_SERIALNUMBER_STRING_FS, USBD_StrDesc, length); + } + return USBD_StrDesc; +} + +/** +* @brief USBD_FS_ConfigStrDescriptor +* return the configuration string descriptor +* @param speed : current device speed +* @param length : pointer to data length variable +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + if(speed == USBD_SPEED_HIGH) { + USBD_GetString(USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length); + } else { + USBD_GetString(USBD_CONFIGURATION_STRING_FS, USBD_StrDesc, length); + } + return USBD_StrDesc; +} + +/** +* @brief USBD_HS_InterfaceStrDescriptor +* return the interface string descriptor +* @param speed : current device speed +* @param length : pointer to data length variable +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { + if(speed == 0) { + USBD_GetString(USBD_INTERFACE_STRING_FS, USBD_StrDesc, length); + } else { + USBD_GetString(USBD_INTERFACE_STRING_FS, USBD_StrDesc, length); + } + return USBD_StrDesc; +} +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/stmsp/stm32f303/src/version.c b/stmsp/stm32f303/src/version.c new file mode 100644 index 00000000..7855ff1c --- /dev/null +++ b/stmsp/stm32f303/src/version.c @@ -0,0 +1,8 @@ +#include "version.h" +#include "version_number.h" + +volatile const version_info_t version_info __attribute__((section(".version_info"))) = { + .product_name = "STMSP-f303", + .major = VERSION_MAJOR, + .minor = VERSION_MINOR, + .patch = VERSION_PATCH};