From 6059c3a798a2b8ef40271585ac95eb7773350630 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Thu, 2 Oct 2025 21:42:37 +0300 Subject: [PATCH] start --- F3:F303/BME280/BMP280.c | 399 +++++++++++++++++++++++++++++ F3:F303/BME280/BMP280.h | 96 +++++++ F3:F303/BME280/Makefile | 10 + F3:F303/BME280/Readme | 1 + F3:F303/BME280/bme280.bin | Bin 0 -> 16920 bytes F3:F303/BME280/bme280.cflags | 1 + F3:F303/BME280/bme280.config | 7 + F3:F303/BME280/bme280.creator | 1 + F3:F303/BME280/bme280.creator.user | 215 ++++++++++++++++ F3:F303/BME280/bme280.cxxflags | 1 + F3:F303/BME280/bme280.files | 24 ++ F3:F303/BME280/bme280.includes | 6 + F3:F303/BME280/hardware.c | 34 +++ F3:F303/BME280/hardware.h | 31 +++ F3:F303/BME280/i2c.c | 222 ++++++++++++++++ F3:F303/BME280/i2c.h | 46 ++++ F3:F303/BME280/main.c | 77 ++++++ F3:F303/BME280/openocd.cfg | 119 +++++++++ F3:F303/BME280/proto.c | 94 +++++++ F3:F303/BME280/proto.h | 22 ++ F3:F303/BME280/ringbuffer.c | 184 +++++++++++++ F3:F303/BME280/ringbuffer.h | 59 +++++ F3:F303/BME280/spi.c | 113 ++++++++ F3:F303/BME280/spi.h | 35 +++ F3:F303/BME280/strfunc.c | 377 +++++++++++++++++++++++++++ F3:F303/BME280/strfunc.h | 40 +++ F3:F303/BME280/usb_descr.c | 210 +++++++++++++++ F3:F303/BME280/usb_descr.h | 62 +++++ F3:F303/BME280/usb_dev.c | 240 +++++++++++++++++ F3:F303/BME280/usb_dev.h | 63 +++++ F3:F303/BME280/usb_lib.c | 368 ++++++++++++++++++++++++++ F3:F303/BME280/usb_lib.h | 328 ++++++++++++++++++++++++ F3:F303/BME280/version.inc | 2 + 33 files changed, 3487 insertions(+) create mode 100644 F3:F303/BME280/BMP280.c create mode 100644 F3:F303/BME280/BMP280.h create mode 100644 F3:F303/BME280/Makefile create mode 100644 F3:F303/BME280/Readme create mode 100755 F3:F303/BME280/bme280.bin create mode 100644 F3:F303/BME280/bme280.cflags create mode 100644 F3:F303/BME280/bme280.config create mode 100644 F3:F303/BME280/bme280.creator create mode 100644 F3:F303/BME280/bme280.creator.user create mode 100644 F3:F303/BME280/bme280.cxxflags create mode 100644 F3:F303/BME280/bme280.files create mode 100644 F3:F303/BME280/bme280.includes create mode 100644 F3:F303/BME280/hardware.c create mode 100644 F3:F303/BME280/hardware.h create mode 100644 F3:F303/BME280/i2c.c create mode 100644 F3:F303/BME280/i2c.h create mode 100644 F3:F303/BME280/main.c create mode 100644 F3:F303/BME280/openocd.cfg create mode 100644 F3:F303/BME280/proto.c create mode 100644 F3:F303/BME280/proto.h create mode 100644 F3:F303/BME280/ringbuffer.c create mode 100644 F3:F303/BME280/ringbuffer.h create mode 100644 F3:F303/BME280/spi.c create mode 100644 F3:F303/BME280/spi.h create mode 100644 F3:F303/BME280/strfunc.c create mode 100644 F3:F303/BME280/strfunc.h create mode 100644 F3:F303/BME280/usb_descr.c create mode 100644 F3:F303/BME280/usb_descr.h create mode 100644 F3:F303/BME280/usb_dev.c create mode 100644 F3:F303/BME280/usb_dev.h create mode 100644 F3:F303/BME280/usb_lib.c create mode 100644 F3:F303/BME280/usb_lib.h create mode 100644 F3:F303/BME280/version.inc diff --git a/F3:F303/BME280/BMP280.c b/F3:F303/BME280/BMP280.c new file mode 100644 index 0000000..a1bd6ba --- /dev/null +++ b/F3:F303/BME280/BMP280.c @@ -0,0 +1,399 @@ +/** + * Ciastkolog.pl (https://github.com/ciastkolog) + * +*/ +/** + * The MIT License (MIT) + * + * Copyright (c) 2016 sheinz (https://github.com/sheinz) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +/* + * Copyright 2023 Edward V. Emelianov . + * + * 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 "i2c.h" +#include "spi.h" +#include "BMP280.h" + +#ifdef EBUG +#include "strfunc.h" +#include "usb_dev.h" +extern volatile uint32_t Tms; +#endif + +#include + +#define BMP280_I2C_ADDRESS_MASK (0x76) +#define BMP280_I2C_ADDRESS_0 (0x76) +#define BMP280_I2C_ADDRESS_1 (0x77) +/** + * BMP280 registers + */ +#define BMP280_REG_HUM_LSB 0xFE +#define BMP280_REG_HUM_MSB 0xFD +#define BMP280_REG_HUM (BMP280_REG_HUM_MSB) +#define BMP280_REG_TEMP_XLSB 0xFC /* bits: 7-4 */ +#define BMP280_REG_TEMP_LSB 0xFB +#define BMP280_REG_TEMP_MSB 0xFA +#define BMP280_REG_TEMP (BMP280_REG_TEMP_MSB) +#define BMP280_REG_PRESS_XLSB 0xF9 /* bits: 7-4 */ +#define BMP280_REG_PRESS_LSB 0xF8 +#define BMP280_REG_PRESS_MSB 0xF7 +#define BMP280_REG_PRESSURE (BMP280_REG_PRESS_MSB) +#define BMP280_REG_ALLDATA (BMP280_REG_PRESS_MSB) // all data: P, T & H +#define BMP280_REG_CONFIG 0xF5 /* bits: 7-5 t_sb; 4-2 filter; 0 spi3w_en */ +#define BMP280_REG_CTRL 0xF4 /* bits: 7-5 osrs_t; 4-2 osrs_p; 1-0 mode */ +#define BMP280_REG_STATUS 0xF3 /* bits: 3 measuring; 0 im_update */ +#define BMP280_REG_CTRL_HUM 0xF2 /* bits: 2-0 osrs_h; */ +#define BMP280_REG_RESET 0xE0 +#define BMP280_RESET_VALUE 0xB6 +#define BMP280_REG_ID 0xD0 + +#define BMP280_REG_CALIBA 0x88 +#define BMP280_CALIBA_SIZE (26) // 26 bytes of calibration registers sequence from 0x88 to 0xa1 +#define BMP280_CALIBB_SIZE (7) // 7 bytes of calibration registers sequence from 0xe1 to 0xe7 +#define BMP280_REG_CALIBB 0xE1 + +#define BMP280_MODE_FORSED (1) // force single measurement +#define BMP280_MODE_NORMAL (3) // run continuosly +#define BMP280_STATUS_MSRNG (1<<3) // measuring in process + +static uint8_t curaddress = BMP280_I2C_ADDRESS_0<<1; + +// function to read N registers or only one +static uint8_t* (*read_regs)(uint8_t addr, uint8_t reg, uint8_t nbytes) = NULL; +// write data to register +static uint8_t (*write_data)(uint8_t addr, uint8_t *data, uint8_t nbytes) = NULL; + +static int read_reg(uint8_t reg, uint8_t *val){ + uint8_t *got = read_regs(curaddress, reg, 1); + if(!got) return 0; + if(val) *val = *got; + return 1; +} +static int write_reg(uint8_t reg, uint8_t val){ + uint8_t d[2]; + d[0] = reg; d[1] = val; + if(!write_data(curaddress, d, 2)) return 0; + return 1; +} + +static struct { + // temperature + uint16_t dig_T1; // 0x88 (LSB), 0x98 (MSB) + int16_t dig_T2; // ... + int16_t dig_T3; + // pressure + uint16_t dig_P1; + int16_t dig_P2; + int16_t dig_P3; + int16_t dig_P4; + int16_t dig_P5; + int16_t dig_P6; + int16_t dig_P7; + int16_t dig_P8; + int16_t dig_P9; // 0x9e, 0x9f + // humidity (partially calculated from EEE struct) + uint8_t unused; // 0xA0 + uint8_t dig_H1; // 0xA1 + int16_t dig_H2; // -------------------- + uint8_t dig_H3; // only from EEE + uint16_t dig_H4; + uint16_t dig_H5; + int8_t dig_H6; + // data is ready + uint8_t rdy; +} __attribute__ ((packed)) CaliData = {0}; + +//T: 28222 26310 50 +//P: 37780 -10748 3024 7965 -43 -7 9900 -10230 4285 +//H: 75 25601 0 334 50 30 + +static struct{ + BMP280_Filter filter; // filtering + BMP280_Oversampling p_os; // oversampling for pressure + BMP280_Oversampling t_os; // -//- temperature + BMP280_Oversampling h_os; // -//- humidity + uint8_t ID; // identificator + uint8_t regctl; // control register base value [(params.t_os << 5) | (params.p_os << 2)] +} params = { + .filter = BMP280_FILTER_OFF, + .p_os = BMP280_OVERS16, + .t_os = BMP280_OVERS16, + .h_os = BMP280_OVERS16, + .ID = 0 +}; + +static BMP280_status bmpstatus = BMP280_NOTINIT; + +BMP280_status BMP280_get_status(){ + return bmpstatus; +} + +#define SPI_BUFSIZE (256) +static uint8_t SPIbuf[SPI_BUFSIZE]; + +// these functions for +static uint8_t *spi_readregs(uint8_t address, uint8_t reg, uint8_t len){ + if(len > SPI_BUFSIZE-2) return NULL; + SPIbuf[0] = address | 0x80; + SPIbuf[1] = reg; + if(!spi_writeread(SPIbuf, len+2)) return NULL; + return SPIbuf + 2; +} +static uint8_t spi_write(uint8_t address, uint8_t *data, uint8_t n){ + SPIbuf[0] = address; + memcpy(SPIbuf + 1, data, n); + return spi_writeread(SPIbuf, n+1); +} + +// address: 0 or 1 +void BMP280_setup(uint8_t address, uint8_t isI2C){ + bmpstatus = BMP280_NOTINIT; + if(isI2C){ + curaddress = (BMP280_I2C_ADDRESS_MASK | (address & 1))<<1; + read_regs = i2c_read_regs; + write_data = i2c_write; + i2c_setup(I2C_SPEED_10K); + }else{ + curaddress = BMP280_I2C_ADDRESS_MASK | (address & 1); + read_regs = spi_readregs; + write_data = spi_write; + spi_setup(); + } + BMP280_init(); +} + +// setters for `params` +void BMP280_setfilter(BMP280_Filter f){ + params.filter = f; +} +BMP280_Filter BMP280_getfilter(){ + return params.filter; +} +void BMP280_setOSt(BMP280_Oversampling os){ + params.t_os = os; +} +void BMP280_setOSp(BMP280_Oversampling os){ + params.p_os = os; +} +void BMP280_setOSh(BMP280_Oversampling os){ + params.h_os = os; +} +// get compensation data, return 1 if OK +static int readcompdata(){ + uint8_t *got = read_regs(curaddress, BMP280_REG_CALIBA, BMP280_CALIBA_SIZE); + if(!got) return 0; + memcpy(&CaliData, got, BMP280_CALIBA_SIZE); + CaliData.rdy = 1; + if(params.ID == BME280_CHIP_ID){ + got = read_regs(curaddress, BMP280_REG_CALIBB, BMP280_CALIBB_SIZE); + if(got){ + CaliData.dig_H2 = (got[1] << 8) | got[0]; + CaliData.dig_H3 = got[2]; + CaliData.dig_H4 = (got[3] << 4) | (got[4] & 0x0f); + CaliData.dig_H5 = (got[5] << 4) | (got[4] >> 4); + CaliData.dig_H6 = got[6]; + } + } + return 1; +} + +// read compensation data & write registers +int BMP280_init(){ + IWDG->KR = IWDG_REFRESH; + DBG("INI:\n"); + if(!read_reg(BMP280_REG_ID, ¶ms.ID)){ + DBG("Can't get ID\n"); + return 0; + } + if(params.ID != BMP280_CHIP_ID && params.ID != BME280_CHIP_ID){ + DBG("Not BMP/BME\n"); + return 0; + } + if(!write_reg(BMP280_REG_RESET, BMP280_RESET_VALUE)){ + DBG("Can't reset\n"); + return 0; + } + uint8_t reg = 1; + while(reg & 1){ + if(!read_reg(BMP280_REG_STATUS, ®)){ + DBG("can't get status\n"); + return 0; + } + } + if(!readcompdata()){ + DBG("Can't read calibration data\n"); + return 0; + } + // write filter configuration + reg = params.filter << 2; + if(!write_reg(BMP280_REG_CONFIG, reg)){DBG("Can't save filter settings\n");} + reg = (params.t_os << 5) | (params.p_os << 2); // oversampling for P/T, sleep mode + if(!write_reg(BMP280_REG_CTRL, reg)){ + DBG("Can't write settings for P/T\n"); + return 0; + } + params.regctl = reg; + if(params.ID == BME280_CHIP_ID){ // write CTRL_HUM only AFTER CTRL! + reg = params.h_os; + if(!write_reg(BMP280_REG_CTRL_HUM, reg)){ + DBG("Can't write settings for H\n"); + return 0; + } + } + bmpstatus = BMP280_RELAX; + return 1; +} + +// @return 1 if OK, *devid -> BMP/BME +int BMP280_read_ID(uint8_t *devid){ + if(params.ID != BMP280_CHIP_ID && params.ID != BME280_CHIP_ID) return 0; + *devid = params.ID; + return 1; +} + +// start measurement, @return 1 if all OK +int BMP280_start(){ + if(!CaliData.rdy || bmpstatus == BMP280_BUSY){ +#ifdef EBUG + USB_sendstr("rdy="); USB_sendstr(u2str(CaliData.rdy)); + USB_sendstr("\nbmpstatus="); USB_sendstr(u2str(bmpstatus)); + newline(); +#endif + return 0; + } + uint8_t reg = params.regctl | BMP280_MODE_FORSED; + if(!write_reg(BMP280_REG_CTRL, reg)){ + DBG("Can't write CTRL reg\n"); + return 0; + } + bmpstatus = BMP280_BUSY; + return 1; +} + +void BMP280_process(){ + if(bmpstatus == BMP280_NOTINIT){ + BMP280_init(); return; + } + if(bmpstatus != BMP280_BUSY) return; + // BUSY state: poll data ready + uint8_t reg; + if(!read_reg(BMP280_REG_STATUS, ®)) return; + if(reg & BMP280_STATUS_MSRNG) return; // still busy + bmpstatus = BMP280_RDY; // data ready +} + +// return T*100 degC +static inline int32_t compTemp(int32_t adc_temp, int32_t *t_fine){ + int32_t var1, var2; + var1 = ((((adc_temp >> 3) - ((int32_t) CaliData.dig_T1 << 1))) + * (int32_t) CaliData.dig_T2) >> 11; + var2 = (((((adc_temp >> 4) - (int32_t) CaliData.dig_T1) + * ((adc_temp >> 4) - (int32_t) CaliData.dig_T1)) >> 12) + * (int32_t) CaliData.dig_T3) >> 14; + *t_fine = var1 + var2; + return (*t_fine * 5 + 128) >> 8; +} + +// return p*256 hPa +static inline uint32_t compPres(int32_t adc_press, int32_t fine_temp) { + int64_t var1, var2, p; + var1 = (int64_t) fine_temp - 128000; + var2 = var1 * var1 * (int64_t) CaliData.dig_P6; + var2 = var2 + ((var1 * (int64_t) CaliData.dig_P5) << 17); + var2 = var2 + (((int64_t) CaliData.dig_P4) << 35); + var1 = ((var1 * var1 * (int64_t) CaliData.dig_P3) >> 8) + + ((var1 * (int64_t) CaliData.dig_P2) << 12); + var1 = (((int64_t) 1 << 47) + var1) * ((int64_t) CaliData.dig_P1) >> 33; + if (var1 == 0){ + return 0; // avoid exception caused by division by zero + } + p = 1048576 - adc_press; + p = (((p << 31) - var2) * 3125) / var1; + var1 = ((int64_t) CaliData.dig_P9 * (p >> 13) * (p >> 13)) >> 25; + var2 = ((int64_t) CaliData.dig_P8 * p) >> 19; + p = ((p + var1 + var2) >> 8) + ((int64_t) CaliData.dig_P7 << 4); + return p; +} + +// return H*1024 % +static inline uint32_t compHum(int32_t adc_hum, int32_t fine_temp){ + int32_t v_x1_u32r; + v_x1_u32r = fine_temp - (int32_t) 76800; + v_x1_u32r = ((((adc_hum << 14) - (((int32_t)CaliData.dig_H4) << 20) + - (((int32_t)CaliData.dig_H5) * v_x1_u32r)) + (int32_t)16384) >> 15) + * (((((((v_x1_u32r * ((int32_t)CaliData.dig_H6)) >> 10) + * (((v_x1_u32r * ((int32_t)CaliData.dig_H3)) >> 11) + + (int32_t)32768)) >> 10) + (int32_t)2097152) + * ((int32_t)CaliData.dig_H2) + 8192) >> 14); + v_x1_u32r = v_x1_u32r + - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) + * ((int32_t)CaliData.dig_H1)) >> 4); + v_x1_u32r = v_x1_u32r < 0 ? 0 : v_x1_u32r; + v_x1_u32r = v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r; + return v_x1_u32r >> 12; +} + + +// read data & convert it +int BMP280_getdata(float *T, float *P, float *H){ + if(bmpstatus != BMP280_RDY) return 0; + bmpstatus = BMP280_RELAX; + uint8_t datasz = 8; // amount of bytes to read + if(params.ID != BME280_CHIP_ID){ + if(H) *H = 0; + H = NULL; + datasz = 6; + } + uint8_t *data = read_regs(curaddress, BMP280_REG_ALLDATA, datasz); + if(!data) return 0; + int32_t p = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4); + int32_t t = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4); + int32_t t_fine; + int32_t Temp = compTemp(t, &t_fine); + if(T) *T = ((float)Temp)/100.f; + if(P) *P = ((float)compPres(p, t_fine)) / 256.f; + if(H){ + int32_t h = (data[6] << 8) | data[7]; + *H = ((float)compHum(h, t_fine))/1024.; + } + return 1; +} + +// dewpoint calculation (T in degrC, H in percents) +float Tdew(float T, float H){ + float gamma = 17.27f * T / (237.7f + T) + logf(H/100.f); + return (237.7f * gamma)/(17.27 - gamma); +} diff --git a/F3:F303/BME280/BMP280.h b/F3:F303/BME280/BMP280.h new file mode 100644 index 0000000..471396d --- /dev/null +++ b/F3:F303/BME280/BMP280.h @@ -0,0 +1,96 @@ +/** + * Ciastkolog.pl (https://github.com/ciastkolog) + * +*/ +/** + * The MIT License (MIT) + * + * Copyright (c) 2016 sheinz (https://github.com/sheinz) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +/* + * Copyright 2023 Edward V. Emelianov . + * + * 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 . + */ + +#pragma once +#ifndef BMP280_H__ +#define BMP280_H__ + +#include + +#define BMP280_CHIP_ID 0x58 +#define BME280_CHIP_ID 0x60 + +typedef enum{ // K for filtering: next = [prev*(k-1) + data_ADC]/k + BMP280_FILTER_OFF = 0, // k=1, no filtering + BMP280_FILTER_2 = 1, // k=2, 2 samples to reach >75% of data_ADC + BMP280_FILTER_4 = 2, // k=4, 5 samples + BMP280_FILTER_8 = 3, // k=8, 11 samples + BMP280_FILTER_16 = 4, // k=16, 22 samples + BMP280_FILTERMAX +} BMP280_Filter; + +typedef enum{ // Number of oversampling + BMP280_NOMEASUR = 0, + BMP280_OVERS1 = 1, + BMP280_OVERS2 = 2, + BMP280_OVERS4 = 3, + BMP280_OVERS8 = 4, + BMP280_OVERS16 = 5, + BMP280_OVERSMAX +} BMP280_Oversampling; + +typedef enum{ + BMP280_NOTINIT, // wasn't inited + BMP280_BUSY, // measurement in progress + BMP280_ERR, // error in I2C + BMP280_RELAX, // relaxed state + BMP280_RDY, // data ready - can get it +} BMP280_status; + + +void BMP280_setup(uint8_t address, uint8_t isI2C); +int BMP280_init(); +void BMP280_setfilter(BMP280_Filter f); +BMP280_Filter BMP280_getfilter(); +void BMP280_setOSt(BMP280_Oversampling os); +void BMP280_setOSp(BMP280_Oversampling os); +void BMP280_setOSh(BMP280_Oversampling os); +int BMP280_read_ID(uint8_t *devid); +BMP280_status BMP280_get_status(); +int BMP280_start(); +void BMP280_process(); +int BMP280_getdata(float *T, float *P, float *H); +float Tdew(float T, float H); + +#endif // BMP280_H__ diff --git a/F3:F303/BME280/Makefile b/F3:F303/BME280/Makefile new file mode 100644 index 0000000..2563d30 --- /dev/null +++ b/F3:F303/BME280/Makefile @@ -0,0 +1,10 @@ +BINARY := bme280 +# MCU code +MCU := F303xb +# change this linking script depending on particular MCU model, +LDSCRIPT := stm32f303xB.ld +DEFINES := -DUSB1_16 +LDADD := -lm + +include ../makefile.f3 +include ../../makefile.stm32 diff --git a/F3:F303/BME280/Readme b/F3:F303/BME280/Readme new file mode 100644 index 0000000..8457273 --- /dev/null +++ b/F3:F303/BME280/Readme @@ -0,0 +1 @@ +Work with BMP280/BME280 over SPI or I2C diff --git a/F3:F303/BME280/bme280.bin b/F3:F303/BME280/bme280.bin new file mode 100755 index 0000000000000000000000000000000000000000..20a2ef03e5564574707bfb6039263a3ddbfce496 GIT binary patch literal 16920 zcmeHue|%HNmG4|#*^*`BUos#|Cdd{E5J3=_25hB?u*}7_NX&!mCb6?y$=Dz{7>sQa zNtYB6XfdSSrI1$wWFP6Tq}%Oo69JoGm37+eF70mKdopwvu#=72Zc8z>EAu0kbqU(< zxmU6cX?Ne}eLnBM2cPksA7{>-IdkUBIcG-fb!I;MFwjVM<+P8OTO^7k1Wr$C9z62Sm!c!<}^eC8#={+s&Xh?DrDDjarK?4@pzX((8^T*7C1+z;ib6~&AJ9DpzqYx zMFK{l%FD-BCnEt)#-#?XfBTddhQiZvj7-CknYYo!DvX_x0JF}FN`Yq99OmOHzB#-x zuE*cOupV^kT_p`hcZvKL+>6itv^aKTLSy6O9AKRB=Vizonvp9Hkjh^!zVK5P|5AeW zE3;5|L%gRo=qO@Z;^UVJnRc;?X}^RNI!%0Ah80+b6);xxmcP2j?-tXmx;|#C2zVFO z@$rEPtBb4Hlc+F1=rRS|*4lyl(dT|zwThol_Nj`BUar#QH>r$a(Ti_b?=*(XTupmK z?=ip8W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cflags new file mode 100644 index 0000000..68d5165 --- /dev/null +++ b/F3:F303/BME280/bme280.cflags @@ -0,0 +1 @@ +-std=c17 \ No newline at end of file diff --git a/F3:F303/BME280/bme280.config b/F3:F303/BME280/bme280.config new file mode 100644 index 0000000..1cf1964 --- /dev/null +++ b/F3:F303/BME280/bme280.config @@ -0,0 +1,7 @@ +// Add predefined macros for your project here. For example: +// #define THE_ANSWER 42 +#define EBUG +#define STM32F3 +#define STM32F303xb +#define __thumb2__ 1 +#define __ARM_ARCH_7M__ diff --git a/F3:F303/BME280/bme280.creator b/F3:F303/BME280/bme280.creator new file mode 100644 index 0000000..e94cbbd --- /dev/null +++ b/F3:F303/BME280/bme280.creator @@ -0,0 +1 @@ +[General] diff --git a/F3:F303/BME280/bme280.creator.user b/F3:F303/BME280/bme280.creator.user new file mode 100644 index 0000000..9a72567 --- /dev/null +++ b/F3:F303/BME280/bme280.creator.user @@ -0,0 +1,215 @@ + + + + + + EnvironmentId + {7bd84e39-ca37-46d3-be9d-99ebea85bc0d} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + true + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + KOI8-R + false + 4 + false + 0 + 80 + true + true + 1 + 0 + false + true + false + 0 + true + true + 0 + 8 + true + false + 1 + true + false + true + *.md, *.MD, Makefile + false + true + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + false + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 2 + false + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + true + Desktop + Desktop + {65a14f9e-e008-4c1b-89df-4eaa4774b6e3} + 0 + 0 + 0 + + /Big/Data/00__Electronics/STM32/F303-nolib/blink + + + + all + + true + GenericProjectManager.GenericMakeStep + + 1 + Build + Build + ProjectExplorer.BuildSteps.Build + + + + + clean + + true + GenericProjectManager.GenericMakeStep + + 1 + Clean + Clean + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Default + GenericProjectManager.GenericBuildConfiguration + 0 + 0 + + + 0 + Deploy + Deploy + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + true + true + + 2 + + false + -e cpu-cycles --call-graph dwarf,4096 -F 250 + + ProjectExplorer.CustomExecutableRunConfiguration + + false + true + true + + 1 + + 1 + + + 0 + Deploy + Deploy + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + true + true + true + + 2 + + false + -e cpu-cycles --call-graph dwarf,4096 -F 250 + + ProjectExplorer.CustomExecutableRunConfiguration + + false + true + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/F3:F303/BME280/bme280.cxxflags b/F3:F303/BME280/bme280.cxxflags new file mode 100644 index 0000000..6435dfc --- /dev/null +++ b/F3:F303/BME280/bme280.cxxflags @@ -0,0 +1 @@ +-std=c++17 \ No newline at end of file diff --git a/F3:F303/BME280/bme280.files b/F3:F303/BME280/bme280.files new file mode 100644 index 0000000..dbfd92b --- /dev/null +++ b/F3:F303/BME280/bme280.files @@ -0,0 +1,24 @@ +BMP280.c +BMP280.h +hardware.c +hardware.h +i2c.c +i2c.h +main.c +proto.c +proto.h +ringbuffer.c +ringbuffer.h +spi.c +spi.h +strfunc.c +strfunc.h +usb.c +usb.h +usb_descr.c +usb_descr.h +usb_dev.c +usb_dev.h +usb_lib.c +usb_lib.h +usbhw.h diff --git a/F3:F303/BME280/bme280.includes b/F3:F303/BME280/bme280.includes new file mode 100644 index 0000000..06d1130 --- /dev/null +++ b/F3:F303/BME280/bme280.includes @@ -0,0 +1,6 @@ +. +../inc +../inc/Fx +../inc/cm +../inc/ld +../inc/startup diff --git a/F3:F303/BME280/hardware.c b/F3:F303/BME280/hardware.c new file mode 100644 index 0000000..3579757 --- /dev/null +++ b/F3:F303/BME280/hardware.c @@ -0,0 +1,34 @@ +/* + * This file is part of the bme280 project. + * Copyright 2025 Edward V. Emelianov . + * + * 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 "hardware.h" + +static inline void gpio_setup(){ + RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN; // for USART and LEDs + for(int i = 0; i < 10000; ++i) nop(); + // USB - alternate function 14 @ pins PA11/PA12; SWD - AF0 @PA13/14 + GPIOA->AFR[1] = AFRf(14, 11) | AFRf(14, 12); + GPIOA->MODER = MODER_AF(11) | MODER_AF(12) | MODER_AF(13) | MODER_AF(14) | MODER_O(15); + GPIOB->MODER = MODER_O(0) | MODER_O(1); + GPIOB->ODR = 1; +} + +void hw_setup(){ + gpio_setup(); +} + diff --git a/F3:F303/BME280/hardware.h b/F3:F303/BME280/hardware.h new file mode 100644 index 0000000..fc241df --- /dev/null +++ b/F3:F303/BME280/hardware.h @@ -0,0 +1,31 @@ +/* + * This file is part of the bme280 project. + * Copyright 2025 Edward V. Emelianov . + * + * 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 . + */ + +#pragma once + +#include + +#define USBPU_port GPIOA +#define USBPU_pin (1<<15) +#define USBPU_ON() pin_clear(USBPU_port, USBPU_pin) +#define USBPU_OFF() pin_set(USBPU_port, USBPU_pin) + +extern volatile uint32_t Tms; + +void hw_setup(); + diff --git a/F3:F303/BME280/i2c.c b/F3:F303/BME280/i2c.c new file mode 100644 index 0000000..e196054 --- /dev/null +++ b/F3:F303/BME280/i2c.c @@ -0,0 +1,222 @@ +/* + * Copyright 2023 Edward V. Emelianov . + * + * 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 +#include + +#include "i2c.h" +#include "strfunc.h" // hexdump +#include "usb_dev.h" + +i2c_speed_t i2c_curspeed = I2C_SPEED_AMOUNT; +extern volatile uint32_t Tms; +static uint32_t cntr; +volatile uint8_t i2c_scanmode = 0; // == 1 when I2C is in scan mode +static uint8_t i2caddr = I2C_ADDREND; // current address in scan mode +static uint8_t I2Cbuf[I2C_BUFSIZE]; + +// GPIO Resources: I2C1_SCL - PB6 (AF4), I2C1_SDA - PB7 (AF4) +void i2c_setup(i2c_speed_t speed){ + uint8_t PRESC, SCLDEL = 0x04, SDADEL = 0x03, SCLH, SCLL; // I2C1->TIMINGR fields + switch(speed){ + case I2C_SPEED_10K: + PRESC = 0x0F; + SCLH = 0xDA; + SCLL = 0xE0; + break; + case I2C_SPEED_100K: + PRESC = 0x0F; + SCLH = 0x13; + SCLL = 0x16; + break; + case I2C_SPEED_400K: + PRESC = 0x07; + SCLH = 0x08; + SCLL = 0x09; + break; + case I2C_SPEED_1M: + SDADEL = 1; + SCLDEL = 2; + PRESC = 0x3; + SCLH = 0x4; + SCLL = 0x6; + break; + /*case I2C_SPEED_2M: + SDADEL = 0; + SCLDEL = 1; + PRESC = 0x0; + SCLH = 0x1; + SCLL = 0x2; + break;*/ + default: + USND("Wrong I2C speed!"); + return; // wrong speed + } + RCC->AHBENR |= RCC_AHBENR_GPIOBEN; + I2C1->CR1 = 0; // disable I2C for setup + I2C1->ICR = 0x3f38; // clear all errors + GPIOB->AFR[0] = (GPIOB->AFR[0] & ~(GPIO_AFRL_AFRL6 | GPIO_AFRL_AFRL7)) | + AFRf(4, 6) | AFRf(4, 7); + GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER6 | GPIO_MODER_MODER7)) | + GPIO_MODER_MODER6_AF | GPIO_MODER_MODER7_AF; + GPIOB->PUPDR = (GPIOB->PUPDR & !(GPIO_PUPDR_PUPDR6 | GPIO_PUPDR_PUPDR7)) | + GPIO_PUPDR6_PU | GPIO_PUPDR7_PU; // pullup (what if there's no external pullup?) + GPIOB->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7; // both open-drain outputs + // I2C (default timing from sys clock - 72MHz) + RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; // clocking + if(speed < I2C_SPEED_400K){ // slow cpeed - common mode + SYSCFG->CFGR1 &= ~(SYSCFG_CFGR1_I2C1_FMP | SYSCFG_CFGR1_I2C_PB6_FMP | SYSCFG_CFGR1_I2C_PB7_FMP); + }else{ // activate "fast mode plus" + SYSCFG->CFGR1 |= SYSCFG_CFGR1_I2C1_FMP | SYSCFG_CFGR1_I2C_PB6_FMP | SYSCFG_CFGR1_I2C_PB7_FMP; + } + I2C1->TIMINGR = (PRESC<CR1 = I2C_CR1_PE; + i2c_curspeed = speed; +} + +// wait until bit set or clear; return 1 if OK, 0 in case of timeout +static uint8_t waitISRbit(uint32_t bit, uint8_t isset){ + uint32_t waitwhile = (isset) ? 0 : bit; // wait until != + //const char *errmsg = NULL; + cntr = Tms; + //if(bit != I2C_ISR_RXNE){ U("ISR wait "); U(uhex2str(bit)); USND(isset ? "set" : "reset"); } + while((I2C1->ISR & bit) == waitwhile){ + IWDG->KR = IWDG_REFRESH; + if(I2C1->ISR & I2C_ISR_NACKF){ + //errmsg = "NAK"; + goto goterr; + } + if(Tms - cntr > I2C_TIMEOUT){ + //errmsg = "timeout"; + goto goterr; + } + } + return 1; +goterr: + /*U("wait ISR bit: "); USND(errmsg); + U("I2c->ISR = "); USND(uhex2str(I2C1->ISR));*/ + I2C1->ICR = 0xff; + return 0; +} + +// start writing +static uint8_t i2c_startw(uint8_t addr, uint8_t nbytes, uint8_t stop){ + if(!waitISRbit(I2C_ISR_BUSY, 0)) return 0; + uint32_t cr2 = nbytes << 16 | addr | I2C_CR2_START; + if(stop) cr2 |= I2C_CR2_AUTOEND; + // now start transfer + I2C1->CR2 = cr2; + return 1; +} + +/** + * write command byte to I2C + * @param addr - device address + * @param data - bytes to write + * @param nbytes - amount of bytes to write + * @param stop - to set STOP + * @return 0 if error + */ +static uint8_t i2c_writes(uint8_t addr, uint8_t *data, uint8_t nbytes, uint8_t stop){ + if(!i2c_startw(addr, nbytes, stop)) return 0; + for(int i = 0; i < nbytes; ++i){ + cntr = Tms; + while(!(I2C1->ISR & I2C_ISR_TXIS)){ // ready to transmit + IWDG->KR = IWDG_REFRESH; + if(I2C1->ISR & I2C_ISR_NACKF){ + I2C1->ICR |= I2C_ICR_NACKCF; + //USND("i2c_writes: NAK"); + return 0; + } + if(Tms - cntr > I2C_TIMEOUT){ + //USND("i2c_writes: Timeout"); + return 0; + } + } + I2C1->TXDR = data[i]; // send data + //U("i2c_writes: "); USND(uhex2str(data[i])); + } + cntr = Tms; + if(stop){ + if(!waitISRbit(I2C_ISR_BUSY, 0)) return 0; + }else{ // repeated start + if(!waitISRbit(I2C_ISR_TC, 1)) return 0; + } + return 1; +} + +uint8_t i2c_write(uint8_t addr, uint8_t *data, uint8_t nbytes){ + return i2c_writes(addr, data, nbytes, 1); +} + +// start reading of `nbytes` from `addr`; if `start`==`, set START +static uint8_t i2c_startr(uint8_t addr, uint8_t nbytes, uint8_t start){ + uint32_t cr2 = addr | I2C_CR2_RD_WRN | I2C_CR2_AUTOEND | (nbytes << 16); + I2C1->CR2 = (start) ? cr2 | I2C_CR2_START : cr2; + return 1; +} + +/** + * read nbytes of data from I2C line + * all functions with `addr` should have addr = address << 1 + * `data` should be an array with at least `nbytes` length + * @return 1 if all OK, 0 if NACK or no device found + */ +static uint8_t *i2c_readb(uint8_t addr, uint8_t nbytes){ + uint8_t *bptr = I2Cbuf; + if(!i2c_startr(addr, nbytes, 1)) return NULL; + for(int i = 0; i < nbytes; ++i){ + if(!waitISRbit(I2C_ISR_RXNE, 1)) goto tmout; + *bptr++ = I2C1->RXDR; + } + return I2Cbuf; +tmout: + //USND("read I2C: Timeout"); + return NULL; +} + +uint8_t *i2c_read(uint8_t addr, uint8_t nbytes){ + if(!waitISRbit(I2C_ISR_BUSY, 0)) return 0; + return i2c_readb(addr, nbytes); +} + +// read register reg +uint8_t *i2c_read_regs(uint8_t addr, uint8_t reg, uint8_t nbytes){ + if(!waitISRbit(I2C_ISR_BUSY, 0)) return NULL; + if(!i2c_writes(addr, ®, 1, 0)) return NULL; + return i2c_readb(addr, nbytes); +} + +void i2c_init_scan_mode(){ + i2caddr = 1; // start from 1 as 0 is a broadcast address + i2c_scanmode = 1; +} + +// return 1 if next addr is active & return in as `addr` +// if addresses are over, return 1 and set addr to I2C_NOADDR +// if scan mode inactive, return 0 and set addr to I2C_NOADDR +int i2c_scan_next_addr(uint8_t *addr){ + *addr = i2caddr; + if(i2caddr == I2C_ADDREND){ + *addr = I2C_ADDREND; + i2c_scanmode = 0; + return 0; + } + if(!i2c_read((i2caddr++)<<1, 1)) return 0; + return 1; +} diff --git a/F3:F303/BME280/i2c.h b/F3:F303/BME280/i2c.h new file mode 100644 index 0000000..80bc90a --- /dev/null +++ b/F3:F303/BME280/i2c.h @@ -0,0 +1,46 @@ +/* + * Copyright 2023 Edward V. Emelianov . + * + * 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 . + */ + +#pragma once +#include + +#define I2C_ADDREND (0x80) +#define I2C_BUFSIZE (256) + +typedef enum{ + I2C_SPEED_10K, + I2C_SPEED_100K, + I2C_SPEED_400K, + I2C_SPEED_1M, + I2C_SPEED_AMOUNT +} i2c_speed_t; + +extern i2c_speed_t i2c_curspeed; +extern volatile uint8_t i2c_scanmode; + +// timeout of I2C bus in ms +#define I2C_TIMEOUT (5) + +void i2c_setup(i2c_speed_t speed); + +uint8_t *i2c_read(uint8_t addr, uint8_t nbytes); +uint8_t *i2c_read_regs(uint8_t addr, uint8_t reg, uint8_t nbytes); + +uint8_t i2c_write(uint8_t addr, uint8_t *data, uint8_t nbytes); + +void i2c_init_scan_mode(); +int i2c_scan_next_addr(uint8_t *addr); diff --git a/F3:F303/BME280/main.c b/F3:F303/BME280/main.c new file mode 100644 index 0000000..82b790e --- /dev/null +++ b/F3:F303/BME280/main.c @@ -0,0 +1,77 @@ +/* + * This file is part of the bme280 project. + * Copyright 2025 Edward V. Emelianov . + * + * 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 "hardware.h" +#include "i2c.h" +#include "proto.h" +#include "spi.h" +#include "strfunc.h" +#include "usb_dev.h" +#include "BMP280.h" + +#define MAXSTRLEN RBINSZ + +volatile uint32_t Tms = 0; + +void sys_tick_handler(void){ + ++Tms; +} + +int main(void){ + char inbuff[MAXSTRLEN+1]; + if(StartHSE()){ + SysTick_Config((uint32_t)72000); // 1ms + }else{ + StartHSI(); + SysTick_Config((uint32_t)48000); // 1ms + } + hw_setup(); + USBPU_OFF(); + USB_setup(); + BMP280_setup(0, 1); + uint32_t ctr = Tms, Tmeas = 0; + USBPU_ON(); + while(1){ + if(Tms - ctr > 499){ + ctr = Tms; + pin_toggle(GPIOB, 1 << 1 | 1 << 0); // toggle LED @ PB0 + } + int l = USB_receivestr(inbuff, MAXSTRLEN); + if(l < 0) USB_sendstr("ERROR: USB buffer overflow or string was too long\n"); + else if(l){ + const char *ans = parse_cmd(inbuff); + if(ans) USB_sendstr(ans); + } + BMP280_process(); + if(Tms - Tmeas > 9999){ + BMP280_status s = BMP280_get_status(); + float Temperature, Pressure, Humidity, Dewpoint; + if(s == BMP280_NOTINIT || s == BMP280_ERR) BMP280_init(); + else if(s == BMP280_RDY && BMP280_getdata(&Temperature, &Pressure, &Humidity)){ + Dewpoint = Tdew(Temperature, Humidity); + USB_sendstr("Tdeg="); USB_sendstr(float2str(Temperature, 2)); USB_sendstr("\nPpa="); + USB_sendstr(float2str(Pressure, 1)); + USB_sendstr("\nPmm="); USB_sendstr(float2str(Pressure * 0.00750062f, 1)); + USB_sendstr("\nH="); USB_sendstr(float2str(Humidity, 1)); + USB_sendstr("\nTdew="); USB_sendstr(float2str(Dewpoint, 1)); + newline(); + Tmeas += 10000; + } + } + } +} diff --git a/F3:F303/BME280/openocd.cfg b/F3:F303/BME280/openocd.cfg new file mode 100644 index 0000000..56ccc2e --- /dev/null +++ b/F3:F303/BME280/openocd.cfg @@ -0,0 +1,119 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +# script for stm32f3x family + +# +# stm32 devices support both JTAG and SWD transports. +# +source [find interface/stlink-v2-1.cfg] +source [find target/swj-dp.tcl] +source [find mem_helper.tcl] + +if { [info exists CHIPNAME] } { + set _CHIPNAME $CHIPNAME +} else { + set _CHIPNAME stm32f3x +} + +set _ENDIAN little + +# Work-area is a space in RAM used for flash programming +# By default use 16kB +if { [info exists WORKAREASIZE] } { + set _WORKAREASIZE $WORKAREASIZE +} else { + set _WORKAREASIZE 0x4000 +} + +# JTAG speed should be <= F_CPU/6. F_CPU after reset is 8MHz, so use F_JTAG = 1MHz +# +# Since we may be running of an RC oscilator, we crank down the speed a +# bit more to be on the safe side. Perhaps superstition, but if are +# running off a crystal, we can run closer to the limit. Note +# that there can be a pretty wide band where things are more or less stable. +adapter speed 1000 + +adapter srst delay 100 +if {[using_jtag]} { + jtag_ntrst_delay 100 +} + +#jtag scan chain +if { [info exists CPUTAPID] } { + set _CPUTAPID $CPUTAPID +} else { + if { [using_jtag] } { + # See STM Document RM0316 + # Section 29.6.3 - corresponds to Cortex-M4 r0p1 + set _CPUTAPID 0x4ba00477 + } { + set _CPUTAPID 0x2ba01477 + } +} + +swj_newdap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID +dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu + +if {[using_jtag]} { + jtag newtap $_CHIPNAME bs -irlen 5 +} + +set _TARGETNAME $_CHIPNAME.cpu +target create $_TARGETNAME cortex_m -endian $_ENDIAN -dap $_CHIPNAME.dap + +$_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0 + +set _FLASHNAME $_CHIPNAME.flash +flash bank $_FLASHNAME stm32f1x 0 0 0 0 $_TARGETNAME + +reset_config srst_nogate + +if {![using_hla]} { + # if srst is not fitted use SYSRESETREQ to + # perform a soft reset + cortex_m reset_config sysresetreq +} + +proc stm32f3x_default_reset_start {} { + # Reset clock is HSI (8 MHz) + adapter speed 1000 +} + +proc stm32f3x_default_examine_end {} { + # Enable debug during low power modes (uses more power) + mmw 0xe0042004 0x00000007 0 ;# DBGMCU_CR |= DBG_STANDBY | DBG_STOP | DBG_SLEEP + + # Stop watchdog counters during halt + mmw 0xe0042008 0x00001800 0 ;# DBGMCU_APB1_FZ |= DBG_IWDG_STOP | DBG_WWDG_STOP +} + +proc stm32f3x_default_reset_init {} { + # Configure PLL to boost clock to HSI x 8 (64 MHz) + mww 0x40021004 0x00380400 ;# RCC_CFGR = PLLMUL[3:1] | PPRE1[2] + mmw 0x40021000 0x01000000 0 ;# RCC_CR |= PLLON + mww 0x40022000 0x00000012 ;# FLASH_ACR = PRFTBE | LATENCY[1] + sleep 10 ;# Wait for PLL to lock + mmw 0x40021004 0x00000002 0 ;# RCC_CFGR |= SW[1] + + # Boost JTAG frequency + adapter speed 8000 +} + +# Default hooks +$_TARGETNAME configure -event examine-end { stm32f3x_default_examine_end } +$_TARGETNAME configure -event reset-start { stm32f3x_default_reset_start } +$_TARGETNAME configure -event reset-init { stm32f3x_default_reset_init } + +tpiu create $_CHIPNAME.tpiu -dap $_CHIPNAME.dap -ap-num 0 -baseaddr 0xE0040000 + +lappend _telnet_autocomplete_skip _proc_pre_enable_$_CHIPNAME.tpiu +proc _proc_pre_enable_$_CHIPNAME.tpiu {_targetname} { + targets $_targetname + + # Set TRACE_IOEN; TRACE_MODE is set to async; when using sync + # change this value accordingly to configure trace pins + # assignment + mmw 0xe0042004 0x00000020 0 +} + +$_CHIPNAME.tpiu configure -event pre-enable "_proc_pre_enable_$_CHIPNAME.tpiu $_TARGETNAME" diff --git a/F3:F303/BME280/proto.c b/F3:F303/BME280/proto.c new file mode 100644 index 0000000..bc4207a --- /dev/null +++ b/F3:F303/BME280/proto.c @@ -0,0 +1,94 @@ +/* + * This file is part of the bme280 project. + * Copyright 2025 Edward V. Emelianov . + * + * 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 +#include + +#include "spi.h" +#include "strfunc.h" +#include "usb_dev.h" +#include "version.inc" + +#define LOCBUFFSZ (32) +// local buffer for SPI data to send +static uint8_t locBuffer[LOCBUFFSZ]; +static const char *ERR = "ERR\n", *OK = "OK\n"; +extern volatile uint32_t Tms; + +const char *helpstring = + "https://github.com/eddyem/stm32samples/tree/master/F3:F303/xxx build#" BUILD_NUMBER " @ " BUILD_DATE "\n" + "i - [re]init SPI1\n" + "s - send up to 32 bytes of data and read answer\n" + "T - print current Tms\n" +; + +// read N numbers from buf, @return 0 if wrong or none +TRUE_INLINE uint16_t readNnumbers(const char *buf){ + uint32_t D; + const char *nxt; + uint16_t N = 0; + while((nxt = getnum(buf, &D)) && nxt != buf && N < LOCBUFFSZ){ + if(D > 0xff){ USND("Each number should be uint8_t"); return 0; } + buf = nxt; + locBuffer[N++] = (uint8_t) D&0xff; + USND("add byte: "); USND(uhex2str(D&0xff)); USND("\n"); + } + USND("Send "); USND(u2str(N)); USND(" bytes\n"); + return N; +} + +TRUE_INLINE const char* spirdwr(const char *buf){ + if(spi_status != SPI_READY){ USND("SPI not ready!"); return NULL; } + uint16_t got = readNnumbers(buf); + if(got < 1){ USND("Enter at least one uint8_t"); return NULL; } + if(!spi_writeread(locBuffer, got)) return ERR; + USND("Read data:"); + hexdump(USB_sendstr, locBuffer, got); + return NULL; +} + +const char *parse_cmd(const char *buf){ + // "long" commands + if(buf[1]){ + char c = *buf++; + switch(c){ + case 's': + return spirdwr(buf); + default: return buf; // echo input + } + } + /* switch(*buf){ + case '': + break; + }*/ + // "short" commands + switch(*buf){ + case 'i': + spi_setup(); + return OK; + case 'T': + USB_sendstr("T="); + USB_sendstr(u2str(Tms)); + newline(); + break; + default: // help + USB_sendstr(helpstring); + break; + } + return NULL; +} diff --git a/F3:F303/BME280/proto.h b/F3:F303/BME280/proto.h new file mode 100644 index 0000000..3ee30ab --- /dev/null +++ b/F3:F303/BME280/proto.h @@ -0,0 +1,22 @@ +/* + * This file is part of the bme280 project. + * Copyright 2025 Edward V. Emelianov . + * + * 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 . + */ + +#pragma once + +char *parse_cmd(char *buf); + diff --git a/F3:F303/BME280/ringbuffer.c b/F3:F303/BME280/ringbuffer.c new file mode 100644 index 0000000..8d0a577 --- /dev/null +++ b/F3:F303/BME280/ringbuffer.c @@ -0,0 +1,184 @@ +/* + * This file is part of the bme280 project. + * Copyright 2025 Edward V. Emelianov . + * + * 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 . + */ + +/* + * Copyright 2023 Edward V. Emelianov . + * + * 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 "ringbuffer.h" + +static int datalen(ringbuffer *b){ + if(b->tail >= b->head) return (b->tail - b->head); + else return (b->length - b->head + b->tail); +} + +// stored data length +int RB_datalen(ringbuffer *b){ + if(b->busy) return -1; + b->busy = 1; + int l = datalen(b); + b->busy = 0; + return l; +} + +static int hasbyte(ringbuffer *b, uint8_t byte){ + if(b->head == b->tail) return -1; // no data in buffer + int startidx = b->head; + if(b->head > b->tail){ // + for(int found = b->head; found < b->length; ++found) + if(b->data[found] == byte) return found; + startidx = 0; + } + for(int found = startidx; found < b->tail; ++found) + if(b->data[found] == byte) return found; + return -1; +} + +/** + * @brief RB_hasbyte - check if buffer has given byte stored + * @param b - buffer + * @param byte - byte to find + * @return index if found, -1 if none or busy + */ +int RB_hasbyte(ringbuffer *b, uint8_t byte){ + if(b->busy) return -1; + b->busy = 1; + int ret = hasbyte(b, byte); + b->busy = 0; + return ret; +} + +// poor memcpy +static void mcpy(uint8_t *targ, const uint8_t *src, int l){ + while(l--) *targ++ = *src++; +} + +// increment head or tail +TRUE_INLINE void incr(ringbuffer *b, volatile int *what, int n){ + *what += n; + if(*what >= b->length) *what -= b->length; +} + +static int read(ringbuffer *b, uint8_t *s, int len){ + int l = datalen(b); + if(!l) return 0; + if(l > len) l = len; + int _1st = b->length - b->head; + if(_1st > l) _1st = l; + if(_1st > len) _1st = len; + mcpy(s, b->data + b->head, _1st); + if(_1st < len && l > _1st){ + mcpy(s+_1st, b->data, l - _1st); + incr(b, &b->head, l); + return l; + } + incr(b, &b->head, _1st); + return _1st; +} + +/** + * @brief RB_read - read data from ringbuffer + * @param b - buffer + * @param s - array to write data + * @param len - max len of `s` + * @return bytes read or -1 if busy + */ +int RB_read(ringbuffer *b, uint8_t *s, int len){ + if(b->busy) return -1; + b->busy = 1; + int r = read(b, s, len); + b->busy = 0; + return r; +} + +static int readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len){ + int idx = hasbyte(b, byte); + if(idx < 0) return 0; + int partlen = idx + 1 - b->head; + // now calculate length of new data portion + if(idx < b->head) partlen += b->length; + if(partlen > len) return -read(b, s, len); + return read(b, s, partlen); +} + +/** + * @brief RB_readto fill array `s` with data until byte `byte` (with it) + * @param b - ringbuffer + * @param byte - check byte + * @param s - buffer to write data + * @param len - length of `s` + * @return amount of bytes written (negative, if lenbusy) return -1; + b->busy = 1; + int n = readto(b, byte, s, len); + b->busy = 0; + return n; +} + +static int write(ringbuffer *b, const uint8_t *str, int l){ + int r = b->length - 1 - datalen(b); // rest length + if(l > r || !l) return 0; + int _1st = b->length - b->tail; + if(_1st > l) _1st = l; + mcpy(b->data + b->tail, str, _1st); + if(_1st < l){ // add another piece from start + mcpy(b->data, str+_1st, l-_1st); + } + incr(b, &b->tail, l); + return l; +} + +/** + * @brief RB_write - write some data to ringbuffer + * @param b - buffer + * @param str - data + * @param l - length + * @return amount of bytes written or -1 if busy + */ +int RB_write(ringbuffer *b, const uint8_t *str, int l){ + if(b->busy) return -1; + b->busy = 1; + int w = write(b, str, l); + b->busy = 0; + return w; +} + +// just delete all information in buffer `b` +int RB_clearbuf(ringbuffer *b){ + if(b->busy) return -1; + b->busy = 1; + b->head = 0; + b->tail = 0; + b->busy = 0; + return 1; +} diff --git a/F3:F303/BME280/ringbuffer.h b/F3:F303/BME280/ringbuffer.h new file mode 100644 index 0000000..1ee44fb --- /dev/null +++ b/F3:F303/BME280/ringbuffer.h @@ -0,0 +1,59 @@ +/* + * This file is part of the bme280 project. + * Copyright 2025 Edward V. Emelianov . + * + * 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 . + */ + +/* + * Copyright 2023 Edward V. Emelianov . + * + * 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 . + */ + +#pragma once + +#if defined STM32F0 +#include +#elif defined STM32F1 +#include +#elif defined STM32F3 +#include +#endif + +typedef struct{ + uint8_t *data; // data buffer + const int length; // its length + int head; // head index + int tail; // tail index + volatile int busy; // == TRUE if buffer is busy now +} ringbuffer; + +int RB_read(ringbuffer *b, uint8_t *s, int len); +int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len); +int RB_hasbyte(ringbuffer *b, uint8_t byte); +int RB_write(ringbuffer *b, const uint8_t *str, int l); +int RB_datalen(ringbuffer *b); +int RB_clearbuf(ringbuffer *b); diff --git a/F3:F303/BME280/spi.c b/F3:F303/BME280/spi.c new file mode 100644 index 0000000..56fa8f8 --- /dev/null +++ b/F3:F303/BME280/spi.c @@ -0,0 +1,113 @@ +/* + * This file is part of the canbus4bta project. + * Copyright 2024 Edward V. Emelianov . + * + * 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 "hardware.h" +#include "spi.h" +#include // memcpy + +#ifdef EBUG +#include "usb_dev.h" +#include "strfunc.h" +#endif + +spiStatus spi_status = SPI_NOTREADY; +#define WAITX(x) do{volatile uint32_t wctr = 0; while((x) && (++wctr < 360000)) IWDG->KR = IWDG_REFRESH; if(wctr==360000){ DBG("timeout"); return 0;}}while(0) + +// init SPI +void spi_setup(){ + SPI1->CR1 = 0; // clear EN + SPI1->CR2 = 0; + // PB3 - SCK, BP4 - MISO, PB5 - MOSI; AF5 @PB3-5 + GPIOB->AFR[0] = (GPIOB->AFR[0] & ~(GPIO_AFRL_AFRL3 | GPIO_AFRL_AFRL4 | GPIO_AFRL_AFRL5)) | + AFRf(5, 3) | AFRf(5, 4) | AFRf(5, 5); + GPIOB->MODER = (GPIOB->MODER & (MODER_CLR(3) & MODER_CLR(4) & MODER_CLR(5))) | + MODER_AF(3) | MODER_AF(4) | MODER_AF(5); + RCC->APB1RSTR = RCC_APB2RSTR_SPI1RST; + RCC->APB1RSTR = 0; // clear reset + RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; + // software slave management (without hardware NSS pin); RX only; Baudrate = 0b110 - fpclk/128 + // CPOL=1, CPHA=1: + SPI1->CR1 = SPI_CR1_CPOL | SPI_CR1_CPHA | SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_MSTR | SPI_CR1_BR_2 | SPI_CR1_BR_1; + // hardware NSS management, RXNE after 8bit; 8bit transfer (default) + // DS=8bit; RXNE generates after 8bit of data in FIFO; + SPI1->CR2 = SPI_CR2_SSOE | SPI_CR2_FRXTH | SPI_CR2_DS_2|SPI_CR2_DS_1|SPI_CR2_DS_0; + spi_status = SPI_READY; + DBG("SPI works"); +} + +void spi_onoff(uint8_t on){ + if(on) SPI1->CR1 |= SPI_CR1_SPE; + else SPI1->CR1 &= ~SPI_CR1_SPE; +} + +// turn off given SPI channel and release GPIO +void spi_deinit(){ + SPI1->CR1 = 0; + SPI1->CR2 = 0; + RCC->APB2ENR &= ~RCC_APB2ENR_SPI1EN; + GPIOB->AFR[0] = GPIOB->AFR[0] & ~(GPIO_AFRL_AFRL3 | GPIO_AFRL_AFRL4 | GPIO_AFRL_AFRL5); + GPIOB->MODER = GPIOB->MODER & (MODER_CLR(3) & MODER_CLR(4) & MODER_CLR(5)); + spi_status = SPI_NOTREADY; +} + +int spi_waitbsy(){ + WAITX(SPI1->SR & SPI_SR_BSY); + return 1; +} + +/** + * @brief spi_writeread - send data over SPI (change data array with received bytes) + * @param data - data to write/read + * @param n - length of data + * @return 0 if failed + */ +int spi_writeread(uint8_t *data, uint8_t n){ + if(spi_status != SPI_READY || !data || !n){ + DBG("not ready"); + return 0; + } + // clear SPI Rx FIFO + for(int i = 0; i < 4; ++i) (void) SPI1->DR; + for(int x = 0; x < n; ++x){ + WAITX(!(SPI1->SR & SPI_SR_TXE)); + *((volatile uint8_t*)&SPI1->DR) = data[x]; + WAITX(!(SPI1->SR & SPI_SR_RXNE)); + data[x] = *((volatile uint8_t*)&SPI1->DR); + } + return 1; +} + +// read data through SPI in read-only mode +int spi_read(uint8_t *data, uint8_t n){ + if(spi_status != SPI_READY || !data || !n){ + DBG("not ready"); + return 0; + } + // clear SPI Rx FIFO + for(int i = 0; i < 4; ++i) (void) SPI1->DR; + spi_onoff(TRUE); + for(int x = 0; x < n; ++x){ + if(x == n - 1) SPI1->CR1 &= ~SPI_CR1_RXONLY; // clear RXonly bit to stop CLK generation after next byte + WAITX(!(SPI1->SR & SPI_SR_RXNE)); + data[x] = *((volatile uint8_t*)&SPI1->DR); + } + spi_onoff(FALSE); // turn off SPI + SPI1->CR1 |= SPI_CR1_RXONLY; // and return RXonly bit + return 1; +} + diff --git a/F3:F303/BME280/spi.h b/F3:F303/BME280/spi.h new file mode 100644 index 0000000..52e9f17 --- /dev/null +++ b/F3:F303/BME280/spi.h @@ -0,0 +1,35 @@ +/* + * This file is part of the canbus4bta project. + * Copyright 2024 Edward V. Emelianov . + * + * 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 . + */ + +#pragma once +#include + +typedef enum{ + SPI_NOTREADY, + SPI_READY, + SPI_BUSY +} spiStatus; + +extern spiStatus spi_status; + +void spi_onoff(uint8_t on); +void spi_deinit(); +void spi_setup(); +int spi_waitbsy(); +int spi_writeread(uint8_t *data, uint8_t n); +int spi_read(uint8_t *data, uint8_t n); diff --git a/F3:F303/BME280/strfunc.c b/F3:F303/BME280/strfunc.c new file mode 100644 index 0000000..51b14fb --- /dev/null +++ b/F3:F303/BME280/strfunc.c @@ -0,0 +1,377 @@ +/* + * This file is part of the mlx90640 project. + * Copyright 2025 Edward V. Emelianov . + * + * 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 // isnan / isinf + +#include "strfunc.h" + +// hex line number for hexdumps +void u16s(uint16_t n, char *buf){ + for(int j = 3; j > -1; --j){ + register uint8_t q = n & 0xf; + n >>= 4; + if(q < 10) buf[j] = q + '0'; + else buf[j] = q - 10 + 'a'; + } +} + +/** + * @brief hexdump - dump hex array by 16 bytes in string + * @param sendfun - function to send data + * @param arr - array to dump + * @param len - length of `arr` + */ +void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len){ + char buf[64] = "0000 ", *bptr = &buf[6]; + for(uint16_t l = 0; l < len; ++l, ++arr){ + for(int16_t j = 1; j > -1; --j){ + register uint8_t half = (*arr >> (4*j)) & 0x0f; + if(half < 10) *bptr++ = half + '0'; + else *bptr++ = half - 10 + 'a'; + } + if((l & 0xf) == 0xf){ + *bptr++ = '\n'; + *bptr = 0; + sendfun(buf); + u16s(l + 1, buf); + bptr = &buf[6]; + }else *bptr++ = ' '; + } + if(bptr != &buf[6]){ + *bptr++ = '\n'; + *bptr = 0; + sendfun(buf); + } +} + +// dump uint16_t by 8 values in string +void hexdump16(int (*sendfun)(const char *s), uint16_t *arr, uint16_t len){ + char buf[64] = "0000 ", *bptr = &buf[6]; + for(uint16_t l = 0; l < len; ++l, ++arr){ + //uint16_t val = *arr; + u16s(*arr, bptr); + /*for(int16_t j = 3; j > -1; --j){ + register uint8_t q = val & 0xf; + val >>= 4; + if(q < 10) bptr[j] = q + '0'; + else bptr[j] = q - 10 + 'a'; + }*/ + bptr += 4; + if((l & 7) == 7){ + *bptr++ = '\n'; + *bptr = 0; + sendfun(buf); + u16s((l + 1)*2, buf); // number of byte, not word! + bptr = &buf[6]; + }else *bptr++ = ' '; + } + if(bptr != &buf[6]){ + *bptr++ = '\n'; + *bptr = 0; + sendfun(buf); + } +} + +/** + * @brief _2str - convert value into string buffer + * @param val - |value| + * @param minus - ==0 if value > 0 + * @return buffer with number + */ +static const char *_2str(uint32_t val, uint8_t minus){ + static char strbuf[12]; + char *bufptr = &strbuf[11]; + *bufptr = 0; + if(!val){ + *(--bufptr) = '0'; + }else{ + while(val){ + uint32_t x = val / 10; + *(--bufptr) = (val - 10*x) + '0'; + val = x; + //*(--bufptr) = val % 10 + '0'; + //val /= 10; + } + } + if(minus) *(--bufptr) = '-'; + return bufptr; +} + +// return string with number `val` +const char *u2str(uint32_t val){ + return _2str(val, 0); +} +const char *i2str(int32_t i){ + uint8_t minus = 0; + uint32_t val; + if(i < 0){ + minus = 1; + val = -i; + }else val = i; + return _2str(val, minus); +} + +/** + * @brief uhex2str - print 32bit unsigned int as hex + * @param val - value + * @return string with number + */ +const char *uhex2str(uint32_t val){ + static char buf[12] = "0x"; + int npos = 2; + uint8_t *ptr = (uint8_t*)&val + 3; + int8_t i, j, z=1; + for(i = 0; i < 4; ++i, --ptr){ + if(*ptr == 0){ // omit leading zeros + if(i == 3) z = 0; + if(z) continue; + } + else z = 0; + for(j = 1; j > -1; --j){ + uint8_t half = (*ptr >> (4*j)) & 0x0f; + if(half < 10) buf[npos++] = half + '0'; + else buf[npos++] = half - 10 + 'a'; + } + } + buf[npos] = 0; + return buf; +} + +/** + * @brief omit_spaces - eliminate leading spaces and other trash in string + * @param buf - string + * @return - pointer to first character in `buf` > ' ' + */ +char *omit_spaces(const char *buf){ + while(*buf){ + if(*buf > ' ') break; + ++buf; + } + return (char*)buf; +} + +/** + * @brief getdec - read decimal number & return pointer to next non-number symbol + * @param buf - string + * @param N - number read + * @return Next non-number symbol. In case of overflow return `buf` and N==0xffffffff + */ +static const char *getdec(const char *buf, uint32_t *N){ + char *start = (char*)buf; + uint32_t num = 0; + while(*buf){ + char c = *buf; + if(c < '0' || c > '9'){ + break; + } + if(num > 429496729 || (num == 429496729 && c > '5')){ // overflow + *N = 0xffffff; + return start; + } + num *= 10; + num += c - '0'; + ++buf; + } + *N = num; + return buf; +} +// read hexadecimal number (without 0x prefix!) +static const char *gethex(const char *buf, uint32_t *N){ + const char *start = buf; + uint32_t num = 0; + while(*buf){ + char c = *buf; + uint8_t M = 0; + if(c >= '0' && c <= '9'){ + M = '0'; + }else if(c >= 'A' && c <= 'F'){ + M = 'A' - 10; + }else if(c >= 'a' && c <= 'f'){ + M = 'a' - 10; + } + if(M){ + if(num & 0xf0000000){ // overflow + *N = 0xffffff; + return start; + } + num <<= 4; + num += c - M; + }else{ + break; + } + ++buf; + } + *N = num; + return buf; +} +// read octal number (without 0 prefix!) +static const char *getoct(const char *buf, uint32_t *N){ + const char *start = (char*)buf; + uint32_t num = 0; + while(*buf){ + char c = *buf; + if(c < '0' || c > '7'){ + break; + } + if(num & 0xe0000000){ // overflow + *N = 0xffffff; + return start; + } + num <<= 3; + num += c - '0'; + ++buf; + } + *N = num; + return buf; +} +// read binary number (without b prefix!) +static const char *getbin(const char *buf, uint32_t *N){ + const char *start = (char*)buf; + uint32_t num = 0; + while(*buf){ + char c = *buf; + if(c < '0' || c > '1'){ + break; + } + if(num & 0x80000000){ // overflow + *N = 0xffffff; + return start; + } + num <<= 1; + if(c == '1') num |= 1; + ++buf; + } + *N = num; + return buf; +} + +/** + * @brief getnum - read uint32_t from string (dec, hex or bin: 127, 0x7f, 0b1111111) + * @param buf - buffer with number and so on + * @param N - the number read + * @return pointer to first non-number symbol in buf + * (if it is == buf, there's no number or if *N==0xffffffff there was overflow) + */ +const char *getnum(const char *txt, uint32_t *N){ + const char *nxt = NULL; + const char *s = omit_spaces(txt); + if(*s == '0'){ // hex, oct or 0 + if(s[1] == 'x' || s[1] == 'X'){ // hex + nxt = gethex(s+2, N); + if(nxt == s+2) nxt = (char*)txt; + }else if(s[1] > '0'-1 && s[1] < '8'){ // oct + nxt = getoct(s+1, N); + if(nxt == s+1) nxt = (char*)txt; + }else{ // 0 + nxt = s+1; + *N = 0; + } + }else if(*s == 'b' || *s == 'B'){ + nxt = getbin(s+1, N); + if(nxt == s+1) nxt = (char*)txt; + }else{ + nxt = getdec(s, N); + if(nxt == s) nxt = (char*)txt; + } + return nxt; +} + +// get signed integer +const char *getint(const char *txt, int32_t *I){ + const char *s = omit_spaces(txt); + int32_t sign = 1; + uint32_t U; + if(*s == '-'){ + sign = -1; + ++s; + } + const char *nxt = getnum(s, &U); + if(nxt == s) return txt; + if(U & 0x80000000) return txt; // overfull + *I = sign * (int32_t)U; + return nxt; +} + + +// be careful: if pow10 would be bigger you should change str[] size! +static const float pwr10[] = {1.f, 10.f, 100.f, 1000.f, 10000.f}; +static const float rounds[] = {0.5f, 0.05f, 0.005f, 0.0005f, 0.00005f}; +#define P10L (sizeof(pwr10)/sizeof(uint32_t) - 1) +char *float2str(float x, uint8_t prec){ + static char str[16] = {0}; // -117.5494E-36\0 - 14 symbols max! + if(prec > P10L) prec = P10L; + if(isnan(x)){ memcpy(str, "NAN", 4); return str;} + else{ + int i = isinf(x); + if(i){memcpy(str, "-INF", 5); if(i == 1) return str+1; else return str;} + } + char *s = str + 14; // go to end of buffer + uint8_t minus = 0; + if(x < 0){ + x = -x; + minus = 1; + } + int pow = 0; // xxxEpow + // now convert float to 1.xxxE3y + while(x > 1000.f){ + x /= 1000.f; + pow += 3; + } + if(x > 0.) while(x < 1.){ + x *= 1000.f; + pow -= 3; + } + // print Eyy + if(pow){ + uint8_t m = 0; + if(pow < 0){pow = -pow; m = 1;} + while(pow){ + register int p10 = pow/10; + *s-- = '0' + (pow - 10*p10); + pow = p10; + } + if(m) *s-- = '-'; + *s-- = 'E'; + } + // now our number is in [1, 1000] + uint32_t units; + if(prec){ + units = (uint32_t) x; + uint32_t decimals = (uint32_t)((x-units+rounds[prec])*pwr10[prec]); + // print decimals + while(prec){ + register int d10 = decimals / 10; + *s-- = '0' + (decimals - 10*d10); + decimals = d10; + --prec; + } + // decimal point + *s-- = '.'; + }else{ // without decimal part + units = (uint32_t) (x + 0.5); + } + // print main units + if(units == 0) *s-- = '0'; + else while(units){ + register uint32_t u10 = units / 10; + *s-- = '0' + (units - 10*u10); + units = u10; + } + if(minus) *s-- = '-'; + return s+1; +} diff --git a/F3:F303/BME280/strfunc.h b/F3:F303/BME280/strfunc.h new file mode 100644 index 0000000..fb7d830 --- /dev/null +++ b/F3:F303/BME280/strfunc.h @@ -0,0 +1,40 @@ +/* + * This file is part of the mlx90640 project. + * Copyright 2025 Edward V. Emelianov . + * + * 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 . + */ + +#pragma once + +#include +#include + +#include "usb_dev.h" + +#define printu(x) do{USB_sendstr(u2str(x));}while(0) +#define printi(x) do{USB_sendstr(i2str(x));}while(0) +#define printuhex(x) do{USB_sendstr(uhex2str(x));}while(0) +#define printfl(x,n) do{USB_sendstr(float2str(x, n));}while(0) + +void u16s(uint16_t n, char *buf); +void hexdump16(int (*sendfun)(const char *s), uint16_t *arr, uint16_t len); +void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len); +const char *u2str(uint32_t val); +const char *i2str(int32_t i); +const char *uhex2str(uint32_t val); +const char *getnum(const char *txt, uint32_t *N); +char *omit_spaces(const char *buf); +const char *getint(const char *txt, int32_t *I); +char *float2str(float x, uint8_t prec); diff --git a/F3:F303/BME280/usb_descr.c b/F3:F303/BME280/usb_descr.c new file mode 100644 index 0000000..2995c63 --- /dev/null +++ b/F3:F303/BME280/usb_descr.c @@ -0,0 +1,210 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * 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 "usb_descr.h" + +// low/high for uint16_t +#define L16(x) (x & 0xff) +#define H16(x) (x >> 8) + +static const uint8_t USB_DeviceDescriptor[] = { + USB_DT_DEVICE_SIZE, // bLength + USB_DT_DEVICE, // bDescriptorType + L16(bcdUSB), // bcdUSB_L + H16(bcdUSB), // bcdUSB_H + USB_CLASS_MISC, // bDeviceClass + bDeviceSubClass, // bDeviceSubClass + bDeviceProtocol, // bDeviceProtocol + USB_EP0BUFSZ, // bMaxPacketSize + L16(idVendor), // idVendor_L + H16(idVendor), // idVendor_H + L16(idProduct), // idProduct_L + H16(idProduct), // idProduct_H + L16(bcdDevice_Ver), // bcdDevice_Ver_L + H16(bcdDevice_Ver), // bcdDevice_Ver_H + iMANUFACTURER_DESCR, // iManufacturer - indexes of string descriptors in array + iPRODUCT_DESCR, // iProduct + iSERIAL_DESCR, // iSerial + bNumConfigurations // bNumConfigurations +}; + +static const uint8_t USB_DeviceQualifierDescriptor[] = { + USB_DT_QUALIFIER_SIZE, //bLength + USB_DT_QUALIFIER, // bDescriptorType + L16(bcdUSB), // bcdUSB_L + H16(bcdUSB), // bcdUSB_H + USB_CLASS_PER_INTERFACE, // bDeviceClass + bDeviceSubClass, // bDeviceSubClass + bDeviceProtocol, // bDeviceProtocol + USB_EP0BUFSZ, // bMaxPacketSize0 + bNumConfigurations, // bNumConfigurations + 0 // Reserved +}; + +#define wTotalLength (USB_DT_CONFIG_SIZE + (bNumInterfaces * USB_DT_INTERFACE_SIZE) + (bTotNumEndpoints * USB_DT_ENDPOINT_SIZE) + (bNumCsInterfaces * USB_DT_CS_INTERFACE_SIZE) - 1) + +static const uint8_t USB_ConfigDescriptor[] = { + // Configuration Descriptor + USB_DT_CONFIG_SIZE, // bLength: Configuration Descriptor size + USB_DT_CONFIG, // bDescriptorType: Configuration + L16(wTotalLength), // wTotalLength.L :no of returned bytes + H16(wTotalLength), // wTotalLength.H + bNumInterfaces, // bNumInterfaces + 1, // bConfigurationValue: Current configuration value + 0, // iConfiguration: Index of string descriptor describing the configuration or 0 + BusPowered, // bmAttributes - Bus powered + 50, // MaxPower in 2mA units + //--------------------------------------------------------------------------- + // Virtual command Interface Descriptor + USB_DT_INTERFACE_SIZE, // bLength: Interface Descriptor size + USB_DT_INTERFACE, // bDescriptorType: Interface + 0, // bInterfaceNumber: Number of Interface + 0, // bAlternateSetting: Alternate setting + 1, // bNumEndpoints: one for this + USB_CLASS_COMM, // bInterfaceClass + 2, // bInterfaceSubClass: ACM + 1, // bInterfaceProtocol: Common AT commands + iINTERFACE_DESCR1, // iInterface + // ---- CS Interfaces + USB_DT_CS_INTERFACE_SIZE, // bLength + USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE + 0, // bDescriptorSubtype: Header Func Desc + 0x10, // bcdCDC: spec release number + 1, // bDataInterface + USB_DT_CS_INTERFACE_SIZE, // bLength + USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE + 1, // bDescriptorSubtype: Call Management Func Desc + 0, // bmCapabilities: D0+D1 + 1, // bDataInterface + USB_DT_CS_INTERFACE_SIZE-1, // bLength + USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE + 2, // bDescriptorSubtype: Abstract Control Management desc + 2, // bmCapabilities + USB_DT_CS_INTERFACE_SIZE, // bLength + USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE + 6, // bDescriptorSubtype: Union func desc + 0, // bMasterInterface: Communication class interface + 1, // bSlaveInterface0: Data Class Interface + // Virtual endpoint 1 Descriptor + USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size + USB_DT_ENDPOINT, // bDescriptorType: Endpoint + 0x8A, // bEndpointAddress IN10 + USB_BM_ATTR_INTERRUPT, // bmAttributes: Interrupt + L16(USB_EP1BUFSZ), // wMaxPacketSize LO + H16(USB_EP1BUFSZ), // wMaxPacketSize HI + 0x10, // bInterval: 16ms + //--------------------------------------------------------------------------- + // Data interface + USB_DT_INTERFACE_SIZE, // bLength: Interface Descriptor size + USB_DT_INTERFACE, // bDescriptorType: Interface + 1, // bInterfaceNumber: Number of Interface + 0, // bAlternateSetting: Alternate setting + 2, // bNumEndpoints: in and out + USB_CLASS_DATA, // bInterfaceClass + 2, // bInterfaceSubClass: ACM + 0, // bInterfaceProtocol + 0, // iInterface + //Endpoint IN1 Descriptor + USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size + USB_DT_ENDPOINT, // bDescriptorType: Endpoint + 0x81, // bEndpointAddress: IN1 + USB_BM_ATTR_BULK, // bmAttributes: Bulk + L16(USB_TXBUFSZ), // wMaxPacketSize LO + H16(USB_TXBUFSZ), // wMaxPacketSize HI + 0, // bInterval: ignore for Bulk transfer + // Endpoint OUT1 Descriptor + USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size + USB_DT_ENDPOINT, // bDescriptorType: Endpoint + 0x01, // bEndpointAddress: OUT1 + USB_BM_ATTR_BULK, // bmAttributes: Bulk + L16(USB_RXBUFSZ), // wMaxPacketSize LO + H16(USB_RXBUFSZ), // wMaxPacketSize HI + 0, // bInterval: ignore for Bulk transfer + +}; + +//const uint8_t HID_ReportDescriptor[]; + +_USB_LANG_ID_(LD, LANG_US); +_USB_STRING_(SD, u"0.0.1"); +_USB_STRING_(MD, u"eddy@sao.ru"); +_USB_STRING_(PD, u"BME280 SPI management"); +_USB_STRING_(ID, u"bme280"); + +static const void* const StringDescriptor[iDESCR_AMOUNT] = { + [iLANGUAGE_DESCR] = &LD, + [iMANUFACTURER_DESCR] = &MD, + [iPRODUCT_DESCR] = &PD, + [iSERIAL_DESCR] = &SD, + [iINTERFACE_DESCR1] = &ID +}; + +static void wr0(const uint8_t *buf, uint16_t size, uint16_t askedsize){ + if(askedsize < size) size = askedsize; // shortened request + if(size < USB_EP0BUFSZ){ + EP_WriteIRQ(0, buf, size); + return; + } + while(size){ + uint16_t l = size; + if(l > USB_EP0BUFSZ) l = USB_EP0BUFSZ; + EP_WriteIRQ(0, buf, l); + buf += l; + size -= l; + uint8_t needzlp = (l == USB_EP0BUFSZ) ? 1 : 0; + if(size || needzlp){ // send last data buffer + uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]); + // keep DTOGs, clear CTR_RX,TX, set TX VALID, leave stat_Rx + USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX|USB_EPnR_STAT_RX)) + ^ USB_EPnR_STAT_TX; + uint32_t ctr = 1000000; + while(--ctr && (USB->ISTR & USB_ISTR_CTR) == 0){IWDG->KR = IWDG_REFRESH;}; + if((USB->ISTR & USB_ISTR_CTR) == 0){ + return; + } + if(needzlp) EP_WriteIRQ(0, NULL, 0); + } + } +} + +void get_descriptor(config_pack_t *pack){ + uint8_t descrtype = pack->wValue >> 8, + descridx = pack->wValue & 0xff; + switch(descrtype){ + case DEVICE_DESCRIPTOR: + wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor), pack->wLength); + break; + case CONFIGURATION_DESCRIPTOR: + wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor), pack->wLength); + break; + case STRING_DESCRIPTOR: + if(descridx < iDESCR_AMOUNT){ + wr0((const uint8_t *)StringDescriptor[descridx], *((uint8_t*)StringDescriptor[descridx]), pack->wLength); + }else{ + EP_WriteIRQ(0, NULL, 0); + } + break; + case DEVICE_QUALIFIER_DESCRIPTOR: + wr0(USB_DeviceQualifierDescriptor, sizeof(USB_DeviceQualifierDescriptor), pack->wLength); + break; + /* case HID_REPORT_DESCRIPTOR: + wr0(HID_ReportDescriptor, sizeof(HID_ReportDescriptor), pack->wLength); + break;*/ + default: + break; + } +} diff --git a/F3:F303/BME280/usb_descr.h b/F3:F303/BME280/usb_descr.h new file mode 100644 index 0000000..d260af2 --- /dev/null +++ b/F3:F303/BME280/usb_descr.h @@ -0,0 +1,62 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * 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 . + */ +#pragma once + +#include + +#include "usb_lib.h" + +// definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor +// bcdUSB: 1.10 +#define bcdUSB 0x0110 +// Class - Misc (EF), subclass - common (2), protocol - interface association descr (1) +#define bDeviceSubClass 0x02 +#define bDeviceProtocol 0x01 +#define idVendor 0x0483 +#define idProduct 0x5740 +#define bcdDevice_Ver 0x0200 +#define bNumConfigurations 1 + +// amount of interfaces and endpoints (except 0) used +#define bNumInterfaces 2 +#define bTotNumEndpoints 3 +#define bNumCsInterfaces 4 + +// powered +#define BusPowered (1<<7) +#define SelfPowered (1<<6) +#define RemoteWakeup (1<<5) + +// buffer sizes +// for USB FS EP0 buffers are from 8 to 64 bytes long +#define USB_EP0BUFSZ 64 +#define USB_EP1BUFSZ 10 +// Rx/Tx EPs +#define USB_RXBUFSZ 64 +#define USB_TXBUFSZ 64 + +// string descriptors +enum{ + iLANGUAGE_DESCR, + iMANUFACTURER_DESCR, + iPRODUCT_DESCR, + iSERIAL_DESCR, + iINTERFACE_DESCR1, + iDESCR_AMOUNT +}; + +void get_descriptor(config_pack_t *pack); diff --git a/F3:F303/BME280/usb_dev.c b/F3:F303/BME280/usb_dev.c new file mode 100644 index 0000000..8e519e4 --- /dev/null +++ b/F3:F303/BME280/usb_dev.c @@ -0,0 +1,240 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * 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 + +#include "ringbuffer.h" +#include "usb_descr.h" +#include "usb_dev.h" + +// Class-Specific Control Requests +#define SEND_ENCAPSULATED_COMMAND 0x00 // unused +#define GET_ENCAPSULATED_RESPONSE 0x01 // unused +#define SET_COMM_FEATURE 0x02 // unused +#define GET_COMM_FEATURE 0x03 // unused +#define CLEAR_COMM_FEATURE 0x04 // unused +#define SET_LINE_CODING 0x20 +#define GET_LINE_CODING 0x21 +#define SET_CONTROL_LINE_STATE 0x22 +#define SEND_BREAK 0x23 + +// control line states +#define CONTROL_DTR 0x01 +#define CONTROL_RTS 0x02 + +// inbuf overflow when receiving +static volatile uint8_t bufovrfl = 0; + +// receive buffer: hold data until chkin() call +static uint8_t volatile rcvbuf[USB_RXBUFSZ]; +static uint8_t volatile rcvbuflen = 0; +// line coding +usb_LineCoding WEAK lineCoding = {115200, 0, 0, 8}; +// CDC configured and ready to use +volatile uint8_t CDCready = 0; + +// ring buffers for incoming and outgoing data +static uint8_t obuf[RBOUTSZ], ibuf[RBINSZ]; +static volatile ringbuffer rbout = {.data = obuf, .length = RBOUTSZ, .head = 0, .tail = 0}; +static volatile ringbuffer rbin = {.data = ibuf, .length = RBINSZ, .head = 0, .tail = 0}; +// last send data size +static volatile int lastdsz = 0; + +static void chkin(){ + if(bufovrfl) return; // allow user to know that previous buffer was overflowed and cleared + if(!rcvbuflen) return; + int w = RB_write((ringbuffer*)&rbin, (uint8_t*)rcvbuf, rcvbuflen); + if(w < 0){ + return; + } + if(w != rcvbuflen) bufovrfl = 1; + rcvbuflen = 0; + uint16_t status = KEEP_DTOG(USB->EPnR[1]); // don't change DTOG + USB->EPnR[1] = (status & ~(USB_EPnR_STAT_TX|USB_EPnR_CTR_RX)) ^ USB_EPnR_STAT_RX; // prepare to get next data portion +} + +// called from transmit EP to send next data portion or by user - when new transmission starts +static void send_next(){ + uint8_t usbbuff[USB_TXBUFSZ]; + int buflen = RB_read((ringbuffer*)&rbout, (uint8_t*)usbbuff, USB_TXBUFSZ); + if(buflen == 0){ + if(lastdsz == 64) EP_Write(1, NULL, 0); // send ZLP after 64 bits packet when nothing more to send + lastdsz = 0; + return; + }else if(buflen < 0){ + lastdsz = 0; + return; + } + EP_Write(1, (uint8_t*)usbbuff, buflen); + lastdsz = buflen; +} + +// data IN/OUT handler +static void rxtx_handler(){ + uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]); + if(RX_FLAG(epstatus)){ // receive data + if(rcvbuflen){ + bufovrfl = 1; // lost last data + rcvbuflen = 0; + } + rcvbuflen = EP_Read(1, (uint8_t*)rcvbuf); + USB->EPnR[1] = epstatus & ~(USB_EPnR_CTR_RX | USB_EPnR_STAT_RX | USB_EPnR_STAT_TX); // keep RX in STALL state until read data + chkin(); // try to write current data into RXbuf if it's not busy + }else{ // tx successfull + USB->EPnR[1] = (epstatus & ~(USB_EPnR_CTR_TX | USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX; + send_next(); + } +} + +// weak handlers: change them somewhere else if you want to setup USART +// SET_LINE_CODING +void WEAK linecoding_handler(usb_LineCoding *lc){ + lineCoding = *lc; +} + +// SET_CONTROL_LINE_STATE +void WEAK clstate_handler(uint16_t val){ + CDCready = val; // CONTROL_DTR | CONTROL_RTS -> interface connected; 0 -> disconnected +} + +// SEND_BREAK +void WEAK break_handler(){ + CDCready = 0; +} + + +// USB is configured: setup endpoints +void set_configuration(){ + EP_Init(1, EP_TYPE_BULK, USB_TXBUFSZ, USB_RXBUFSZ, rxtx_handler); // IN1 and OUT1 +} + +// PL2303 CLASS request +void usb_class_request(config_pack_t *req, uint8_t *data, uint16_t datalen){ + uint8_t recipient = REQUEST_RECIPIENT(req->bmRequestType); + uint8_t dev2host = (req->bmRequestType & 0x80) ? 1 : 0; + switch(recipient){ + case REQ_RECIPIENT_INTERFACE: + switch(req->bRequest){ + case SET_LINE_CODING: + if(!data || !datalen) break; // wait for data + if(datalen == sizeof(usb_LineCoding)) + linecoding_handler((usb_LineCoding*)data); + break; + case GET_LINE_CODING: + EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding)); + break; + case SET_CONTROL_LINE_STATE: + clstate_handler(req->wValue); + break; + case SEND_BREAK: + break_handler(); + break; + default: + break; + } + break; + default: + if(dev2host) EP_WriteIRQ(0, NULL, 0); + } + if(!dev2host) EP_WriteIRQ(0, NULL, 0); +} + +// blocking send full content of ring buffer +int USB_sendall(){ + while(lastdsz > 0){ + if(!CDCready) return FALSE; + } + return TRUE; +} + +// put `buf` into queue to send +int USB_send(const uint8_t *buf, int len){ + if(!buf || !CDCready || !len) return FALSE; + while(len){ + int a = RB_write((ringbuffer*)&rbout, buf, len); + if(a > 0){ + len -= a; + buf += a; + } else if (a < 0) continue; // do nothing if buffer is in reading state + if(lastdsz == 0) send_next(); // need to run manually - all data sent, so no IRQ on IN + } + return TRUE; +} + +int USB_putbyte(uint8_t byte){ + if(!CDCready) return FALSE; + int l = 0; + while((l = RB_write((ringbuffer*)&rbout, &byte, 1)) != 1){ + if(l < 0) continue; + } + if(lastdsz == 0) send_next(); // need to run manually - all data sent, so no IRQ on IN + return TRUE; +} + +int USB_sendstr(const char *string){ + if(!string || !CDCready) return FALSE; + int len = 0; + const char *b = string; + while(*b++) ++len; + if(!len) return FALSE; + return USB_send((const uint8_t*)string, len); +} + +/** + * @brief USB_receive - get binary data from receiving ring-buffer + * @param buf (i) - buffer for received data + * @param len - length of `buf` + * @return amount of received bytes (negative, if overfull happened) + */ +int USB_receive(uint8_t *buf, int len){ + chkin(); + if(bufovrfl){ + while(1 != RB_clearbuf((ringbuffer*)&rbin)); + bufovrfl = 0; + return -1; + } + int sz = RB_read((ringbuffer*)&rbin, buf, len); + if(sz < 0) return 0; // buffer in writting state + return sz; +} + +/** + * @brief USB_receivestr - get string up to '\n' and replace '\n' with 0 + * @param buf - receiving buffer + * @param len - its length + * @return strlen or negative value indicating overflow (if so, string won't be ends with 0 and buffer should be cleared) + */ +int USB_receivestr(char *buf, int len){ + chkin(); + if(bufovrfl){ + while(1 != RB_clearbuf((ringbuffer*)&rbin)); + bufovrfl = 0; + return -1; + } + int l = RB_readto((ringbuffer*)&rbin, '\n', (uint8_t*)buf, len); + if(l < 1){ + if(rbin.length == RB_datalen((ringbuffer*)&rbin)){ // buffer is full but no '\n' found + while(1 != RB_clearbuf((ringbuffer*)&rbin)); + return -1; + } + return 0; + } + if(l == 0) return 0; + buf[l-1] = 0; // replace '\n' with strend + return l; +} + diff --git a/F3:F303/BME280/usb_dev.h b/F3:F303/BME280/usb_dev.h new file mode 100644 index 0000000..b7bd929 --- /dev/null +++ b/F3:F303/BME280/usb_dev.h @@ -0,0 +1,63 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * 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 . + */ +#pragma once + +#include +#include "usb_lib.h" + +typedef struct { + uint32_t dwDTERate; + uint8_t bCharFormat; +#define USB_CDC_1_STOP_BITS 0 +#define USB_CDC_1_5_STOP_BITS 1 +#define USB_CDC_2_STOP_BITS 2 + uint8_t bParityType; +#define USB_CDC_NO_PARITY 0 +#define USB_CDC_ODD_PARITY 1 +#define USB_CDC_EVEN_PARITY 2 +#define USB_CDC_MARK_PARITY 3 +#define USB_CDC_SPACE_PARITY 4 + uint8_t bDataBits; +} __attribute__ ((packed)) usb_LineCoding; + +extern usb_LineCoding lineCoding; +extern volatile uint8_t CDCready; + +void break_handler(); +void clstate_handler(uint16_t val); +void linecoding_handler(usb_LineCoding *lc); + + +// sizes of ringbuffers for outgoing and incoming data +#define RBOUTSZ (1024) +#define RBINSZ (1024) + +#ifdef EBUG +#define DBG(x) USB_sendstr(x) +#else +#define DBG() +#endif + +#define newline() USB_putbyte('\n') +#define USND(s) do{USB_sendstr(s); USB_putbyte('\n');}while(0) + +int USB_sendall(); +int USB_send(const uint8_t *buf, int len); +int USB_putbyte(uint8_t byte); +int USB_sendstr(const char *string); +int USB_receive(uint8_t *buf, int len); +int USB_receivestr(char *buf, int len); diff --git a/F3:F303/BME280/usb_lib.c b/F3:F303/BME280/usb_lib.c new file mode 100644 index 0000000..9f01a8f --- /dev/null +++ b/F3:F303/BME280/usb_lib.c @@ -0,0 +1,368 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * 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 + +#include "usb_lib.h" +#include "usb_descr.h" +#include "usb_dev.h" + +static ep_t endpoints[STM32ENDPOINTS]; + +static uint16_t USB_Addr = 0; +static uint8_t setupdatabuf[EP0DATABUF_SIZE]; +static config_pack_t *setup_packet = (config_pack_t*) setupdatabuf; +volatile uint8_t usbON = 0; // device is configured and active + +static uint16_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured) +static inline void std_d2h_req(){ + uint16_t st = 0; + switch(setup_packet->bRequest){ + case GET_DESCRIPTOR: + get_descriptor(setup_packet); + break; + case GET_STATUS: + EP_WriteIRQ(0, (uint8_t *)&st, 2); // send status: Bus Powered + break; + case GET_CONFIGURATION: + EP_WriteIRQ(0, (uint8_t*)&configuration, 1); + break; + default: + EP_WriteIRQ(0, NULL, 0); + break; + } +} + +static inline void std_h2d_req(){ + switch(setup_packet->bRequest){ + case SET_ADDRESS: + // new address will be assigned later - after acknowlegement or request to host + USB_Addr = setup_packet->wValue; + break; + case SET_CONFIGURATION: + // Now device configured + configuration = setup_packet->wValue; + set_configuration(); + usbON = 1; + break; + default: + break; + } +} + +void WEAK usb_standard_request(){ + uint8_t recipient = REQUEST_RECIPIENT(setup_packet->bmRequestType); + uint8_t dev2host = (setup_packet->bmRequestType & 0x80) ? 1 : 0; + switch(recipient){ + case REQ_RECIPIENT_DEVICE: + if(dev2host){ + std_d2h_req(); + }else{ + std_h2d_req(); + } + break; + case REQ_RECIPIENT_INTERFACE: + if(dev2host && setup_packet->bRequest == GET_DESCRIPTOR){ + get_descriptor(setup_packet); + } + break; + case REQ_RECIPIENT_ENDPOINT: + if(setup_packet->bRequest == CLEAR_FEATURE){ + }else{ /* wrong */ } + break; + default: + break; + } + if(!dev2host) EP_WriteIRQ(0, NULL, 0); +} + +void WEAK usb_class_request(config_pack_t *req, uint8_t _U_ *data, uint16_t _U_ datalen){ + switch(req->bRequest){ + case GET_INTERFACE: + break; + case SET_CONFIGURATION: // set featuring by req->wValue + break; + default: + break; + } + if(0 == (setup_packet->bmRequestType & 0x80)) // host2dev + EP_WriteIRQ(0, NULL, 0); +} + +void WEAK usb_vendor_request(config_pack_t _U_ *packet, uint8_t _U_ *data, uint16_t _U_ datalen){ + if(0 == (setup_packet->bmRequestType & 0x80)) // host2dev + EP_WriteIRQ(0, NULL, 0); +} + +/* +bmRequestType: 76543210 +7 direction: 0 - host->device, 1 - device->host +65 type: 0 - standard, 1 - class, 2 - vendor +4..0 getter: 0 - device, 1 - interface, 2 - endpoint, 3 - other +*/ +/** + * Endpoint0 (control) handler + */ +static void EP0_Handler(){ + uint8_t ep0dbuflen = 0; + uint8_t ep0databuf[EP0DATABUF_SIZE]; + uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]); // EP0R on input -> return this value after modifications + int rxflag = RX_FLAG(epstatus); + //if(rxflag){ } + // check direction + if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit) + if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack + EP_Read(0, setupdatabuf); + // interrupt handler will be called later + }else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf + //if(endpoints[0].rx_cnt){ } + ep0dbuflen = EP_Read(0, ep0databuf); + } + } + if(rxflag){ + uint8_t reqtype = REQUEST_TYPE(setup_packet->bmRequestType); + switch(reqtype){ + case REQ_TYPE_STANDARD: + if(SETUP_FLAG(epstatus)){ + usb_standard_request(); + }else{ } + break; + case REQ_TYPE_CLASS: + usb_class_request(setup_packet, ep0databuf, ep0dbuflen); + break; + case REQ_TYPE_VENDOR: + usb_vendor_request(setup_packet, ep0databuf, ep0dbuflen); + break; + default: + EP_WriteIRQ(0, NULL, 0); + break; + } + } + if(TX_FLAG(epstatus)){ + // now we can change address after enumeration + if ((USB->DADDR & USB_DADDR_ADD) != USB_Addr){ + USB->DADDR = USB_DADDR_EF | USB_Addr; + usbON = 0; + } + } + //epstatus = KEEP_DTOG(USB->EPnR[0]); + if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP or data transmission + else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged + // keep DTOGs, clear CTR_RX,TX, set RX VALID + USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_RX; +} + +/** + * Write data to EP buffer (called from IRQ handler) + * @param number - EP number + * @param *buf - array with data + * @param size - its size + */ +void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size){ + if(size > endpoints[number].txbufsz) size = endpoints[number].txbufsz; + uint16_t N2 = (size + 1) >> 1; + // the buffer is 16-bit, so we should copy data as it would be uint16_t + uint16_t *buf16 = (uint16_t *)buf; +#if defined USB1_16 + // very bad: what if `size` is odd? + uint32_t *out = (uint32_t *)endpoints[number].tx_buf; + for(int i = 0; i < N2; ++i, ++out){ + *out = buf16[i]; + } +#elif defined USB2_16 + // use memcpy instead? + for(int i = 0; i < N2; i++){ + endpoints[number].tx_buf[i] = buf16[i]; + } +#else +#error "Define USB1_16 or USB2_16" +#endif + USB_BTABLE->EP[number].USB_COUNT_TX = size; +} + +/** + * Write data to EP buffer (called outside IRQ handler) + * @param number - EP number + * @param *buf - array with data + * @param size - its size + */ +void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){ + EP_WriteIRQ(number, buf, size); + uint16_t epstatus = KEEP_DTOG(USB->EPnR[number]); + // keep DTOGs and RX stat, clear CTR_TX & set TX VALID to start transmission + USB->EPnR[number] = (epstatus & ~(USB_EPnR_CTR_TX | USB_EPnR_STAT_RX)) ^ USB_EPnR_STAT_TX; +} + +/* + * Copy data from EP buffer into user buffer area + * @param *buf - user array for data + * @return amount of data read + */ +int EP_Read(uint8_t number, uint8_t *buf){ + int sz = endpoints[number].rx_cnt; + if(!sz) return 0; + endpoints[number].rx_cnt = 0; +#if defined USB1_16 + int n = (sz + 1) >> 1; + uint32_t *in = (uint32_t*)endpoints[number].rx_buf; + uint16_t *out = (uint16_t*)buf; + for(int i = 0; i < n; ++i, ++in) + out[i] = *(uint16_t*)in; +#elif defined USB2_16 + // use memcpy instead? + for(int i = 0; i < sz; ++i) + buf[i] = endpoints[number].rx_buf[i]; +#else +#error "Define USB1_16 or USB2_16" +#endif + return sz; +} + + +static uint16_t lastaddr = LASTADDR_DEFAULT; +/** + * Endpoint initialisation + * @param number - EP num (0...7) + * @param type - EP type (EP_TYPE_BULK, EP_TYPE_CONTROL, EP_TYPE_ISO, EP_TYPE_INTERRUPT) + * @param txsz - transmission buffer size @ USB/CAN buffer + * @param rxsz - reception buffer size @ USB/CAN buffer + * @param uint16_t (*func)(ep_t *ep) - EP handler function + * @return 0 if all OK + */ +int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){ + if(number >= STM32ENDPOINTS) return 4; // out of configured amount + if(txsz > USB_BTABLE_SIZE/ACCESSZ || rxsz > USB_BTABLE_SIZE/ACCESSZ) return 1; // buffer too large + if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE/ACCESSZ) return 2; // out of btable + USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA); + USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1; + if(rxsz & 1) return 3; // wrong rx buffer size + uint16_t countrx = 0; + if(rxsz < 64) countrx = rxsz / 2; + else{ + if(rxsz & 0x1f) return 3; // should be multiple of 32 + countrx = 31 + rxsz / 32; + } + USB_BTABLE->EP[number].USB_ADDR_TX = lastaddr; + endpoints[number].tx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ); + endpoints[number].txbufsz = txsz; + lastaddr += txsz; + USB_BTABLE->EP[number].USB_COUNT_TX = 0; + USB_BTABLE->EP[number].USB_ADDR_RX = lastaddr; + endpoints[number].rx_buf = (uint8_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ); + lastaddr += rxsz; + USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10; + endpoints[number].func = func; + return 0; +} + +// standard IRQ handler +void USB_IRQ(){ + uint32_t CNTR = USB->CNTR; + USB->CNTR = 0; + if(USB->ISTR & USB_ISTR_RESET){ + usbON = 0; + // Reinit registers + CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM; + // Endpoint 0 - CONTROL + // ON USB LS size of EP0 may be 8 bytes, but on FS it should be 64 bytes! + lastaddr = LASTADDR_DEFAULT; + // clear address, leave only enable bit + USB->DADDR = USB_DADDR_EF; + USB->ISTR = ~USB_ISTR_RESET; + if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0BUFSZ, USB_EP0BUFSZ, EP0_Handler)){ + return; + }; + } + if(USB->ISTR & USB_ISTR_CTR){ + // EP number + uint8_t n = USB->ISTR & USB_ISTR_EPID; + // copy received bytes amount + endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter + // call EP handler + if(endpoints[n].func) endpoints[n].func(); + } + if(USB->ISTR & USB_ISTR_WKUP){ // wakeup +#ifndef STM32F0 + CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LP_MODE | USB_CNTR_WKUPM); // clear suspend flags +#else + CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LPMODE | USB_CNTR_WKUPM); +#endif + USB->ISTR = ~USB_ISTR_WKUP; + } + if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep + usbON = 0; +#ifndef STM32F0 + CNTR |= USB_CNTR_FSUSP | USB_CNTR_LP_MODE | USB_CNTR_WKUPM; +#else + CNTR |= USB_CNTR_FSUSP | USB_CNTR_LPMODE | USB_CNTR_WKUPM; +#endif + CNTR &= ~(USB_CNTR_SUSPM); + USB->ISTR = ~USB_ISTR_SUSP; + } + USB->CNTR = CNTR; // rewoke interrupts +} + +// here we suppose that all PIN settings done in hw_setup earlier +void USB_setup(){ +#if defined STM32F3 + NVIC_DisableIRQ(USB_LP_IRQn); + // remap USB LP & Wakeup interrupts to 75 and 76 - works only on pure F303 + RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // enable tacting of SYSCFG + SYSCFG->CFGR1 |= SYSCFG_CFGR1_USB_IT_RMP; +#elif defined STM32F1 + NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn); + NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn); +#elif defined STM32F0 + NVIC_DisableIRQ(USB_IRQn); + RCC->APB1ENR |= RCC_APB1ENR_CRSEN; + RCC->CFGR3 &= ~RCC_CFGR3_USBSW; // reset USB + RCC->CR2 |= RCC_CR2_HSI48ON; // turn ON HSI48 + uint32_t tmout = 16000000; + while(!(RCC->CR2 & RCC_CR2_HSI48RDY)){if(--tmout == 0) break;} + FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY; + CRS->CFGR &= ~CRS_CFGR_SYNCSRC; + CRS->CFGR |= CRS_CFGR_SYNCSRC_1; // USB SOF selected as sync source + CRS->CR |= CRS_CR_AUTOTRIMEN; // enable auto trim + CRS->CR |= CRS_CR_CEN; // enable freq counter & block CRS->CFGR as read-only + RCC->CFGR |= RCC_CFGR_SW; +#endif + RCC->APB1ENR |= RCC_APB1ENR_USBEN; + //?? + USB->CNTR = USB_CNTR_FRES; // Force USB Reset + for(uint32_t ctr = 0; ctr < 72000; ++ctr) nop(); // wait >1ms + USB->CNTR = 0; + USB->BTABLE = 0; + USB->DADDR = 0; + USB->ISTR = 0; + USB->CNTR = USB_CNTR_RESETM; // allow only reset interrupts +#if defined STM32F3 + NVIC_EnableIRQ(USB_LP_IRQn); +#elif defined STM32F1 + NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); +#elif defined STM32F0 + USB->BCDR |= USB_BCDR_DPPU; + NVIC_EnableIRQ(USB_IRQn); +#endif +} + + +#if defined STM32F3 +void usb_lp_isr() __attribute__ ((alias ("USB_IRQ"))); +#elif defined STM32F1 +void usb_lp_can_rx0_isr() __attribute__ ((alias ("USB_IRQ"))); +#elif defined STM32F0 +void usb_isr() __attribute__ ((alias ("USB_IRQ"))); +#endif diff --git a/F3:F303/BME280/usb_lib.h b/F3:F303/BME280/usb_lib.h new file mode 100644 index 0000000..d55c83f --- /dev/null +++ b/F3:F303/BME280/usb_lib.h @@ -0,0 +1,328 @@ +/* + * Copyright 2024 Edward V. Emelianov . + * + * 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 . + */ +#pragma once + +#include +#include + +#ifndef _U_ +#define _U_ __attribute__((unused)) +#endif + +/****************************************************************** + * Hardware registers etc * + *****************************************************************/ +#if defined STM32F0 +#include +#elif defined STM32F1 +#include +// there's no this define in standard header +#define USB_BASE ((uint32_t)0x40005C00) +#elif defined STM32F3 +#include +#endif + +// max endpoints number +#define STM32ENDPOINTS 8 +/** + * Buffers size definition + **/ + +// F0 - USB2_16; F1 - USB1_16; F3 - 1/2 depending on series +#if !defined USB1_16 && !defined USB2_16 +#if defined STM32F0 +#define USB2_16 +#elif defined STM32F1 +#define USB1_16 +#else +#error "Can't determine USB1_16 or USB2_16, define by hands" +#endif +#endif + +// BTABLE_SIZE FOR STM32F3: +// In STM32F303/302xB/C, 512 bytes SRAM is not shared with CAN. +// In STM32F302x6/x8 and STM32F30xxD/E, 726 bytes dedicated SRAM and 256 bytes shared SRAM with CAN i.e. +// 1Kbytes dedicated SRAM in case CAN is disabled. +// remember, that USB_BTABLE_SIZE will be divided by ACCESSZ, so don't divide it twice for 32-bit addressing + +#ifdef NOCAN +#if defined STM32F0 +#define USB_BTABLE_SIZE 1024 +#elif defined STM32F3 +#define USB_BTABLE_SIZE 726 +//#warning "Please, check real buffer size due to docs" +#else +#error "define STM32F0 or STM32F3" +#endif +#else // !NOCAN: F0/F3 with CAN or F1 (can't simultaneously run CAN and USB) +#if defined STM32F0 +#define USB_BTABLE_SIZE 768 +#elif defined STM32F3 +#define USB_BTABLE_SIZE 726 +//#warning "Please, check real buffer size due to docs" +#else // STM32F103: 1024 bytes but with 32-bit addressing +#define USB_BTABLE_SIZE 1024 +#endif +#endif // NOCAN + +// first 64 bytes of USB_BTABLE are registers! + +#define USB_BTABLE_BASE 0x40006000 +#define USB ((USB_TypeDef *) USB_BASE) + +#ifdef USB_BTABLE +#undef USB_BTABLE +#endif +#define USB_BTABLE ((USB_BtableDef *)(USB_BTABLE_BASE)) +#define USB_ISTR_EPID 0x0000000F +#define USB_FNR_LSOF_0 0x00000800 +#define USB_FNR_lSOF_1 0x00001000 +#define USB_LPMCSR_BESL_0 0x00000010 +#define USB_LPMCSR_BESL_1 0x00000020 +#define USB_LPMCSR_BESL_2 0x00000040 +#define USB_LPMCSR_BESL_3 0x00000080 +#define USB_EPnR_CTR_RX 0x00008000 +#define USB_EPnR_DTOG_RX 0x00004000 +#define USB_EPnR_STAT_RX 0x00003000 +#define USB_EPnR_STAT_RX_0 0x00001000 +#define USB_EPnR_STAT_RX_1 0x00002000 +#define USB_EPnR_SETUP 0x00000800 +#define USB_EPnR_EP_TYPE 0x00000600 +#define USB_EPnR_EP_TYPE_0 0x00000200 +#define USB_EPnR_EP_TYPE_1 0x00000400 +#define USB_EPnR_EP_KIND 0x00000100 +#define USB_EPnR_CTR_TX 0x00000080 +#define USB_EPnR_DTOG_TX 0x00000040 +#define USB_EPnR_STAT_TX 0x00000030 +#define USB_EPnR_STAT_TX_0 0x00000010 +#define USB_EPnR_STAT_TX_1 0x00000020 +#define USB_EPnR_EA 0x0000000F +#define USB_COUNTn_RX_BLSIZE 0x00008000 +#define USB_COUNTn_NUM_BLOCK 0x00007C00 +#define USB_COUNTn_RX 0x0000003F + +#define USB_TypeDef USB_TypeDef_custom + +typedef struct { + __IO uint32_t EPnR[STM32ENDPOINTS]; + __IO uint32_t RESERVED[STM32ENDPOINTS]; + __IO uint32_t CNTR; + __IO uint32_t ISTR; + __IO uint32_t FNR; + __IO uint32_t DADDR; + __IO uint32_t BTABLE; +#ifdef STM32F0 + __IO uint32_t LPMCSR; + __IO uint32_t BCDR; +#endif +} USB_TypeDef; + +// F303 D/E have 2x16 access scheme +typedef struct{ +#if defined USB2_16 + __IO uint16_t USB_ADDR_TX; + __IO uint16_t USB_COUNT_TX; + __IO uint16_t USB_ADDR_RX; + __IO uint16_t USB_COUNT_RX; +#define ACCESSZ (1) +#define BUFTYPE uint8_t +#elif defined USB1_16 + __IO uint32_t USB_ADDR_TX; + __IO uint32_t USB_COUNT_TX; + __IO uint32_t USB_ADDR_RX; + __IO uint32_t USB_COUNT_RX; +#define ACCESSZ (2) +#define BUFTYPE uint16_t +#else +#error "Define USB1_16 or USB2_16" +#endif +} USB_EPDATA_TypeDef; + + +typedef struct{ + __IO USB_EPDATA_TypeDef EP[STM32ENDPOINTS]; +} USB_BtableDef; + +#define EP0DATABUF_SIZE (64) +#define LASTADDR_DEFAULT (STM32ENDPOINTS * 8) + +/****************************************************************** + * Defines from usb.h * + *****************************************************************/ + +/* + * Device and/or Interface Class codes + */ +#define USB_CLASS_PER_INTERFACE 0 +#define USB_CLASS_AUDIO 1 +#define USB_CLASS_COMM 2 +#define USB_CLASS_HID 3 +#define USB_CLASS_PRINTER 7 +#define USB_CLASS_PTP 6 +#define USB_CLASS_MASS_STORAGE 8 +#define USB_CLASS_HUB 9 +#define USB_CLASS_DATA 10 +#define USB_CLASS_MISC 0xef +#define USB_CLASS_VENDOR_SPEC 0xff + +/* + * Descriptor types + */ +#define USB_DT_DEVICE 0x01 +#define USB_DT_CONFIG 0x02 +#define USB_DT_STRING 0x03 +#define USB_DT_INTERFACE 0x04 +#define USB_DT_ENDPOINT 0x05 +#define USB_DT_QUALIFIER 0x06 +#define USB_DT_IAD 0x0B + +#define USB_DT_HID 0x21 +#define USB_DT_REPORT 0x22 +#define USB_DT_PHYSICAL 0x23 +#define USB_DT_CS_INTERFACE 0x24 +#define USB_DT_HUB 0x29 + +/* + * Descriptor sizes per descriptor type + */ +#define USB_DT_DEVICE_SIZE 18 +#define USB_DT_CONFIG_SIZE 9 +#define USB_DT_INTERFACE_SIZE 9 +#define USB_DT_HID_SIZE 9 +#define USB_DT_ENDPOINT_SIZE 7 +#define USB_DT_QUALIFIER_SIZE 10 +#define USB_DT_CS_INTERFACE_SIZE 5 +#define USB_DT_IAD_SIZE 8 + + +// bmRequestType & 0x80 == dev2host (1) or host2dev (0) +// recipient: bmRequestType & 0x1f +#define REQUEST_RECIPIENT(b) (b & 0x1f) +#define REQ_RECIPIENT_DEVICE 0 +#define REQ_RECIPIENT_INTERFACE 1 +#define REQ_RECIPIENT_ENDPOINT 2 +#define REQ_RECIPIENT_OTHER 3 +// type: [bmRequestType & 0x60 >> 5] +#define REQUEST_TYPE(b) ((b&0x60)>>5) +#define REQ_TYPE_STANDARD 0 +#define REQ_TYPE_CLASS 1 +#define REQ_TYPE_VENDOR 2 +#define REQ_TYPE_RESERVED 3 + + +//#define VENDOR_REQUEST 0x01 + +// standard device requests +#define GET_STATUS 0x00 +#define CLEAR_FEATURE 0x01 +#define SET_FEATURE 0x03 +#define SET_ADDRESS 0x05 +#define GET_DESCRIPTOR 0x06 +#define SET_DESCRIPTOR 0x07 +#define GET_CONFIGURATION 0x08 +#define SET_CONFIGURATION 0x09 +// and some standard interface requests +#define GET_INTERFACE 0x0A +#define SET_INTERFACE 0x0B +// and some standard endpoint requests +#define SYNC_FRAME 0x0C + +// Types of descriptors +#define DEVICE_DESCRIPTOR 0x01 +#define CONFIGURATION_DESCRIPTOR 0x02 +#define STRING_DESCRIPTOR 0x03 +#define DEVICE_QUALIFIER_DESCRIPTOR 0x06 +#define DEBUG_DESCRIPTOR 0x0a +#define HID_REPORT_DESCRIPTOR 0x22 + +// EP types for EP_init +#define EP_TYPE_BULK 0x00 +#define EP_TYPE_CONTROL 0x01 +#define EP_TYPE_ISO 0x02 +#define EP_TYPE_INTERRUPT 0x03 + +// EP types for descriptors +#define USB_BM_ATTR_CONTROL 0x00 +#define USB_BM_ATTR_ISO 0x01 +#define USB_BM_ATTR_BULK 0x02 +#define USB_BM_ATTR_INTERRUPT 0x03 + + +/****************************************************************** + * Other stuff * + *****************************************************************/ + +#define RX_FLAG(epstat) (epstat & USB_EPnR_CTR_RX) +#define TX_FLAG(epstat) (epstat & USB_EPnR_CTR_TX) +#define SETUP_FLAG(epstat) (epstat & USB_EPnR_SETUP) + +// EPnR bits manipulation +#define KEEP_DTOG_STAT(EPnR) (EPnR & ~(USB_EPnR_STAT_RX|USB_EPnR_STAT_TX|USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX)) +#define KEEP_DTOG(EPnR) (EPnR & ~(USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX)) + +#define LANG_US (uint16_t)0x0409 + +#define _USB_STRING_(name, str) \ +static const struct name \ +{ \ + uint8_t bLength; \ + uint8_t bDescriptorType; \ + uint16_t bString[(sizeof(str) - 2) / 2]; \ + \ +} \ +name = {sizeof(name), 0x03, str} + +#define _USB_LANG_ID_(name, lng_id) \ +static const struct name \ +{ \ + uint8_t bLength; \ + uint8_t bDescriptorType; \ + uint16_t bString; \ + \ +} \ +name = {0x04, 0x03, lng_id} + +// EP0 configuration packet +typedef struct { + uint8_t bmRequestType; + uint8_t bRequest; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; +} config_pack_t; + +// endpoints state +typedef struct{ + uint16_t *tx_buf; // transmission buffer address + uint16_t txbufsz; // transmission buffer size + uint8_t *rx_buf; // reception buffer address + void (*func)(); // endpoint action function + unsigned rx_cnt : 10; // received data counter +} ep_t; + +extern volatile uint8_t usbON; + +void USB_setup(); +int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)()); +void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size); +void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size); +int EP_Read(uint8_t number, uint8_t *buf); + +// could be [re]defined in usb_dev.c +extern void usb_class_request(config_pack_t *packet, uint8_t *data, uint16_t datalen); +extern void usb_vendor_request(config_pack_t *packet, uint8_t *data, uint16_t datalen); +extern void set_configuration(); diff --git a/F3:F303/BME280/version.inc b/F3:F303/BME280/version.inc new file mode 100644 index 0000000..691b49d --- /dev/null +++ b/F3:F303/BME280/version.inc @@ -0,0 +1,2 @@ +#define BUILD_NUMBER "13" +#define BUILD_DATE "2025-10-02"