mirror of
https://github.com/eddyem/stm32samples.git
synced 2025-12-06 18:55:13 +03:00
add BMP280/BME280
This commit is contained in:
parent
5b9e4233e6
commit
c1ee273258
BIN
F1-nolib/BMP280/BMP280.bin
Executable file
BIN
F1-nolib/BMP280/BMP280.bin
Executable file
Binary file not shown.
472
F1-nolib/BMP280/BMP280.c
Normal file
472
F1-nolib/BMP280/BMP280.c
Normal file
@ -0,0 +1,472 @@
|
|||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2021 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "i2c.h"
|
||||||
|
#include "BMP280.h"
|
||||||
|
|
||||||
|
//#define EBUG
|
||||||
|
|
||||||
|
#ifdef EBUG
|
||||||
|
#include "usb.h"
|
||||||
|
#include "proto.h"
|
||||||
|
#define DBG(x) do{USB_send(x);}while(0)
|
||||||
|
#else
|
||||||
|
#define DBG(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#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;
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
// data for humidity calibration of BME280
|
||||||
|
static uint8_t EEE[BMP280_CALIBB_SIZE] = {0};
|
||||||
|
|
||||||
|
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_16,
|
||||||
|
.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// address: 0 or 1
|
||||||
|
void BMP280_setup(uint8_t address){
|
||||||
|
curaddress = BMP280_I2C_ADDRESS_MASK | (address & 1);
|
||||||
|
bmpstatus = BMP280_NOTINIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
// setters for `params`
|
||||||
|
void BMP280_setfilter(BMP280_Filter f){
|
||||||
|
params.filter = f;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
// read register, @return 1 if all OK
|
||||||
|
static int read_reg16(uint8_t reg, uint16_t *val){
|
||||||
|
if(I2C_OK != i2c_7bit_send_onebyte(reg, 0)) return 0;
|
||||||
|
if(I2C_OK != i2c_7bit_receive_twobytes((uint8_t*)val)) return 0;
|
||||||
|
return 1;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
static int read_reg8(uint8_t reg, uint8_t *val){
|
||||||
|
if(I2C_OK != i2c_7bit_send_onebyte(reg, 0)) return 0;
|
||||||
|
if(I2C_OK != i2c_7bit_receive_onebyte(val, 1)) return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int write_reg8(uint8_t reg, uint8_t val){
|
||||||
|
uint8_t d[2] = {reg, val};
|
||||||
|
if(I2C_OK != i2c_7bit_send(d, 2)) return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get compensation data, return 1 if OK
|
||||||
|
static int readcompdata(){
|
||||||
|
if(I2C_OK != i2c_7bit_send_onebyte(BMP280_REG_CALIBA, 0)) return 0;
|
||||||
|
if(I2C_OK != i2c_7bit_receive((uint8_t*)&CaliData, BMP280_CALIBA_SIZE)) return 0;
|
||||||
|
CaliData.rdy = 1;
|
||||||
|
if(params.ID == BME280_CHIP_ID){
|
||||||
|
if(I2C_OK == i2c_7bit_send_onebyte(BMP280_REG_CALIBB, 0) &&
|
||||||
|
I2C_OK == i2c_7bit_receive(EEE, BMP280_CALIBB_SIZE)){
|
||||||
|
CaliData.dig_H2 = (EEE[1] << 8) | EEE[0];
|
||||||
|
CaliData.dig_H3 = EEE[2];
|
||||||
|
CaliData.dig_H4 = (EEE[3] << 4) | (EEE[4] & 0x0f);
|
||||||
|
CaliData.dig_H5 = (EEE[5] << 4) | (EEE[4] >> 4);
|
||||||
|
CaliData.dig_H6 = EEE[6];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// read compensation data & write registers
|
||||||
|
int BMP280_init(){
|
||||||
|
i2c_setup();
|
||||||
|
i2c_set_addr7(curaddress);
|
||||||
|
if(!read_reg8(BMP280_REG_ID, ¶ms.ID)) return 0;
|
||||||
|
DBG("Got device ID: "); DBG(u2str(params.ID)); DBG("\n");
|
||||||
|
if(params.ID != BMP280_CHIP_ID && params.ID != BME280_CHIP_ID){
|
||||||
|
DBG("Not BMP/BME\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(!write_reg8(BMP280_REG_RESET, BMP280_RESET_VALUE)){
|
||||||
|
DBG("Can't reset\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t reg = 1;
|
||||||
|
while(reg & 1){
|
||||||
|
if(!read_reg8(BMP280_REG_STATUS, ®)) return 0;
|
||||||
|
}
|
||||||
|
if(!readcompdata()){
|
||||||
|
DBG("Can't read calibration data\n");
|
||||||
|
}else{
|
||||||
|
DBG("T: "); DBG(u2str(CaliData.dig_T1)); DBG(" "); DBG(i2str(CaliData.dig_T2));
|
||||||
|
DBG(" "); DBG(i2str(CaliData.dig_T3));
|
||||||
|
DBG("\nP: "); DBG(u2str(CaliData.dig_P1)); DBG(" ");
|
||||||
|
DBG(i2str(CaliData.dig_P2)); DBG(" "); DBG(i2str(CaliData.dig_P3)); DBG(" ");
|
||||||
|
DBG(i2str(CaliData.dig_P4)); DBG(" "); DBG(i2str(CaliData.dig_P5)); DBG(" ");
|
||||||
|
DBG(i2str(CaliData.dig_P6)); DBG(" "); DBG(i2str(CaliData.dig_P7)); DBG(" ");
|
||||||
|
DBG(i2str(CaliData.dig_P8)); DBG(" "); DBG(i2str(CaliData.dig_P9));
|
||||||
|
DBG("\nH: "); DBG(u2str(CaliData.dig_H1)); DBG(" ");
|
||||||
|
if(params.ID == BME280_CHIP_ID){ // read H compensation
|
||||||
|
DBG(i2str(CaliData.dig_H2)); DBG(" "); DBG(u2str(CaliData.dig_H3)); DBG(" ");
|
||||||
|
DBG(i2str(CaliData.dig_H4)); DBG(" "); DBG(i2str(CaliData.dig_H5)); DBG(" ");
|
||||||
|
DBG(i2str(CaliData.dig_H6));
|
||||||
|
}else{DBG("not BME!");}
|
||||||
|
DBG("\n");
|
||||||
|
}
|
||||||
|
// write filter configuration
|
||||||
|
reg = params.filter << 2;
|
||||||
|
if(!write_reg8(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_reg8(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_reg8(BMP280_REG_CTRL_HUM, reg)){
|
||||||
|
DBG("Can't write settings for H\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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) return 0;
|
||||||
|
uint8_t reg = params.regctl | BMP280_MODE_FORSED;
|
||||||
|
if(!write_reg8(BMP280_REG_CTRL, reg)){
|
||||||
|
DBG("Can't write CTRL reg\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
bmpstatus = BMP280_BUSY;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BMP280_process(){
|
||||||
|
if(bmpstatus != BMP280_BUSY) return;
|
||||||
|
// BUSY state: poll data ready
|
||||||
|
uint8_t reg;
|
||||||
|
if(!read_reg8(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);
|
||||||
|
DBG("Step1: "); DBG(i2str(v_x1_u32r)); DBG(".. ");
|
||||||
|
v_x1_u32r = v_x1_u32r
|
||||||
|
- (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7)
|
||||||
|
* ((int32_t)CaliData.dig_H1)) >> 4);
|
||||||
|
DBG("Step2: "); DBG(i2str(v_x1_u32r)); DBG(".. ");
|
||||||
|
v_x1_u32r = v_x1_u32r < 0 ? 0 : v_x1_u32r;
|
||||||
|
v_x1_u32r = v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r;
|
||||||
|
DBG("Step3: "); DBG(i2str(v_x1_u32r)); DBG("\n");
|
||||||
|
return v_x1_u32r >> 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// read data & convert it
|
||||||
|
int BMP280_getdata(int32_t *T, uint32_t *P, uint32_t *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){
|
||||||
|
DBG("Not BME!\n");
|
||||||
|
if(H) *H = 0;
|
||||||
|
H = NULL;
|
||||||
|
datasz = 6;
|
||||||
|
}
|
||||||
|
uint8_t data[8];
|
||||||
|
if(I2C_OK != i2c_7bit_send_onebyte(BMP280_REG_ALLDATA, 0)) return 0;
|
||||||
|
if(I2C_OK != i2c_7bit_receive(data, datasz)) return 0;
|
||||||
|
int32_t p = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4);
|
||||||
|
DBG("puncomp = "); DBG(i2str(p)); DBG("\n");
|
||||||
|
int32_t t = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4);
|
||||||
|
DBG("tuncomp = "); DBG(i2str(t)); DBG("\n");
|
||||||
|
int32_t t_fine;
|
||||||
|
int32_t Temp = compTemp(t, &t_fine);
|
||||||
|
DBG("tfine = "); DBG(i2str(t_fine)); DBG("\n");
|
||||||
|
if(T) *T = Temp;
|
||||||
|
if(P){
|
||||||
|
float fp = compPres(p, t_fine) / 256.;
|
||||||
|
*P = fp;// * 100.;
|
||||||
|
}
|
||||||
|
if(H){
|
||||||
|
int32_t h = (data[6] << 8) | data[7];
|
||||||
|
DBG("huncomp = "); DBG(i2str(h)); DBG("\n");
|
||||||
|
float fh = compHum(h, t_fine)/1024.;
|
||||||
|
*H = fh * 100.;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
/*
|
||||||
|
* start themperature reading @return 0 if all OK
|
||||||
|
*/
|
||||||
|
int BMP280_cmdT(){
|
||||||
|
const uint8_t cmd[2] = {0x03, 0x11};
|
||||||
|
if(state != RELAX){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
bmpstatus = BMP280_BUSY;
|
||||||
|
i2c_status st = i2c_7bit_send(cmd, 2);
|
||||||
|
if(st != I2C_OK){
|
||||||
|
bmpstatus = BMP280_ERR;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
DBG("Wait for T\n");
|
||||||
|
state = WAITFORT;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* start humidity reading @return 0 if all OK
|
||||||
|
*/
|
||||||
|
int BMP280_cmdH(){
|
||||||
|
const uint8_t cmd[2] = {0x03, 0x01};
|
||||||
|
if(state != RELAX){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
bmpstatus = BMP280_BUSY;
|
||||||
|
i2c_status st = i2c_7bit_send(cmd, 2);
|
||||||
|
if(st != I2C_OK){
|
||||||
|
bmpstatus = BMP280_ERR;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
state = WAITFORH;
|
||||||
|
DBG("Wait for H\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t BMP280_getT(){ // T*10
|
||||||
|
if(bmpstatus != BMP280_TRDY) return -5000;
|
||||||
|
DBG("TH="); DBG(u2str(TH)); DBG("\n");
|
||||||
|
TH >>= 2;
|
||||||
|
uint32_t d = (TH*10)/32 - 500;
|
||||||
|
bmpstatus = BMP280_RELAX;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
uint32_t BMP280_getH(){ // hum * 10
|
||||||
|
if(bmpstatus != BMP280_HRDY) return 5000;
|
||||||
|
TH >>= 4;
|
||||||
|
uint32_t d = (TH*10)/16 - 240;
|
||||||
|
bmpstatus = BMP280_RELAX;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* process state machine
|
||||||
|
*/
|
||||||
|
void BMP280_process(){
|
||||||
|
uint8_t b, d[2];
|
||||||
|
i2c_status st;
|
||||||
|
if(state == RELAX) return;
|
||||||
|
if(state == WAITFORH || state == WAITFORT){ // poll RDY
|
||||||
|
DBG("Poll\n");
|
||||||
|
if((st = i2c_7bit_send_onebyte(0, 0)) == I2C_OK){
|
||||||
|
DBG("0 sent\n");
|
||||||
|
if(i2c_7bit_receive_onebyte(&b, 1) == I2C_OK){
|
||||||
|
DBG("received: "); DBG(u2str(b)); DBG("\n");
|
||||||
|
if(b) return; // !RDY
|
||||||
|
if((st = i2c_7bit_send_onebyte(1, 0)) == I2C_OK){
|
||||||
|
DBG("sent 1\n");
|
||||||
|
if((st = i2c_7bit_receive_twobytes(d)) == I2C_OK){
|
||||||
|
DBG("got data: ");
|
||||||
|
DBG(u2str(d[0])); DBG(" "); DBG(u2str(d[1])); DBG("\n");
|
||||||
|
bmpstatus = (state == WAITFORH) ? BMP280_HRDY : BMP280_TRDY;
|
||||||
|
TH = (d[0]<<8) | d[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state = RELAX;
|
||||||
|
if(st != I2C_OK){
|
||||||
|
bmpstatus = BMP280_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
state = RELAX;
|
||||||
|
bmpstatus = BMP280_ERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
96
F1-nolib/BMP280/BMP280.h
Normal file
96
F1-nolib/BMP280/BMP280.h
Normal file
@ -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.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2021 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#ifndef BMP280_H__
|
||||||
|
#define BMP280_H__
|
||||||
|
|
||||||
|
#include <stm32f1.h>
|
||||||
|
|
||||||
|
#define BMP280_CHIP_ID 0x58
|
||||||
|
#define BME280_CHIP_ID 0x60
|
||||||
|
|
||||||
|
typedef enum{ // Nsamples for filtering
|
||||||
|
BMP280_FILTER_OFF = 0,
|
||||||
|
BMP280_FILTER_2 = 1,
|
||||||
|
BMP280_FILTER_4 = 2,
|
||||||
|
BMP280_FILTER_8 = 3,
|
||||||
|
BMP280_FILTER_16 = 4,
|
||||||
|
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);
|
||||||
|
int BMP280_init();
|
||||||
|
void BMP280_setfilter(BMP280_Filter f);
|
||||||
|
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(int32_t *T, uint32_t *P, uint32_t *H);
|
||||||
|
|
||||||
|
|
||||||
|
#endif // BMP280_H__
|
||||||
139
F1-nolib/BMP280/Makefile
Normal file
139
F1-nolib/BMP280/Makefile
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
BINARY = BMP280
|
||||||
|
BOOTPORT ?= /dev/ttyUSB0
|
||||||
|
BOOTSPEED ?= 115200
|
||||||
|
# MCU FAMILY
|
||||||
|
FAMILY ?= F1
|
||||||
|
# MCU code
|
||||||
|
MCU ?= F103x8
|
||||||
|
# density (stm32f10x.h, lines 70-84)
|
||||||
|
DENSITY ?= MD
|
||||||
|
# change this linking script depending on particular MCU model,
|
||||||
|
LDSCRIPT ?= stm32f103x8.ld
|
||||||
|
# debug
|
||||||
|
#DEFS = -DEBUG
|
||||||
|
|
||||||
|
INDEPENDENT_HEADERS=
|
||||||
|
|
||||||
|
FP_FLAGS ?= -msoft-float -mfloat-abi=soft
|
||||||
|
ASM_FLAGS ?= -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd
|
||||||
|
ARCH_FLAGS = $(ASM_FLAGS) $(FP_FLAGS)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# Executables
|
||||||
|
#PREFIX ?= arm-none-eabi
|
||||||
|
# gcc from arm web site
|
||||||
|
PREFIX ?= /opt/bin/arm-none-eabi
|
||||||
|
TOOLCHLIB ?= /opt/arm-none-eabi/lib
|
||||||
|
RM := rm -f
|
||||||
|
RMDIR := rmdir
|
||||||
|
CC := $(PREFIX)-gcc
|
||||||
|
# don't replace ld with gcc: the binary size would be much greater!!
|
||||||
|
LD := $(PREFIX)-ld
|
||||||
|
AR := $(PREFIX)-ar
|
||||||
|
AS := $(PREFIX)-as
|
||||||
|
SIZE := $(PREFIX)-size
|
||||||
|
OBJCOPY := $(PREFIX)-objcopy
|
||||||
|
OBJDUMP := $(PREFIX)-objdump
|
||||||
|
GDB := $(PREFIX)-gdb
|
||||||
|
STFLASH := $(shell which st-flash)
|
||||||
|
STBOOT := $(shell which stm32flash)
|
||||||
|
DFUUTIL := $(shell which dfu-util)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# Source files
|
||||||
|
OBJDIR = mk
|
||||||
|
SRC := $(wildcard *.c)
|
||||||
|
OBJS := $(addprefix $(OBJDIR)/, $(SRC:%.c=%.o))
|
||||||
|
STARTUP = $(OBJDIR)/startup.o
|
||||||
|
OBJS += $(STARTUP)
|
||||||
|
# dependencies: we need them to recompile files if their headers-dependencies changed
|
||||||
|
DEPS := $(OBJS:.o=.d)
|
||||||
|
|
||||||
|
INC_DIR ?= ../inc
|
||||||
|
|
||||||
|
INCLUDE := -I$(INC_DIR)/Fx -I$(INC_DIR)/cm
|
||||||
|
LIB_DIR := $(INC_DIR)/ld
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# C flags
|
||||||
|
CFLAGS += -O2 -g -D__thumb2__=1 -MD
|
||||||
|
CFLAGS += -Wall -Werror -Wextra -Wshadow
|
||||||
|
CFLAGS += -fno-common -ffunction-sections -fdata-sections -fno-stack-protector
|
||||||
|
CFLAGS += $(ARCH_FLAGS)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# Linker flags
|
||||||
|
LDFLAGS += -nostartfiles --static -nostdlibs
|
||||||
|
LDFLAGS += -L$(LIB_DIR) -L$(TOOLCHLIB)
|
||||||
|
LDFLAGS += -T$(LDSCRIPT)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# Used libraries
|
||||||
|
LDLIBS += -lc $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
|
||||||
|
|
||||||
|
DEFS += -DSTM32$(FAMILY) -DSTM32$(MCU) -DSTM32F10X_$(DENSITY)
|
||||||
|
|
||||||
|
ELF := $(OBJDIR)/$(BINARY).elf
|
||||||
|
LIST := $(OBJDIR)/$(BINARY).list
|
||||||
|
BIN := $(BINARY).bin
|
||||||
|
HEX := $(BINARY).hex
|
||||||
|
|
||||||
|
all: bin list size
|
||||||
|
|
||||||
|
elf: $(ELF)
|
||||||
|
bin: $(BIN)
|
||||||
|
hex: $(HEX)
|
||||||
|
list: $(LIST)
|
||||||
|
|
||||||
|
ifneq ($(MAKECMDGOALS),clean)
|
||||||
|
-include $(DEPS)
|
||||||
|
endif
|
||||||
|
|
||||||
|
$(OBJDIR):
|
||||||
|
mkdir $(OBJDIR)
|
||||||
|
|
||||||
|
$(STARTUP): $(INC_DIR)/startup/vector.c
|
||||||
|
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<
|
||||||
|
|
||||||
|
$(OBJDIR)/%.o: %.c
|
||||||
|
@echo " CC $<"
|
||||||
|
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<
|
||||||
|
|
||||||
|
$(BIN): $(ELF)
|
||||||
|
@echo " OBJCOPY $(BIN)"
|
||||||
|
$(OBJCOPY) -Obinary $(ELF) $(BIN)
|
||||||
|
|
||||||
|
$(HEX): $(ELF)
|
||||||
|
@echo " OBJCOPY $(HEX)"
|
||||||
|
$(OBJCOPY) -Oihex $(ELF) $(HEX)
|
||||||
|
|
||||||
|
$(LIST): $(ELF)
|
||||||
|
@echo " OBJDUMP $(LIST)"
|
||||||
|
$(OBJDUMP) -S $(ELF) > $(LIST)
|
||||||
|
|
||||||
|
$(ELF): $(OBJDIR) $(OBJS)
|
||||||
|
@echo " LD $(ELF)"
|
||||||
|
$(LD) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(ELF)
|
||||||
|
|
||||||
|
size: $(ELF)
|
||||||
|
$(SIZE) $(ELF)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@echo " CLEAN"
|
||||||
|
$(RM) $(OBJS) $(DEPS) $(ELF) $(HEX) $(LIST)
|
||||||
|
@rmdir $(OBJDIR) 2>/dev/null || true
|
||||||
|
|
||||||
|
|
||||||
|
flash: $(BIN)
|
||||||
|
@echo " FLASH $(BIN)"
|
||||||
|
$(STFLASH) write $(BIN) 0x8000000
|
||||||
|
|
||||||
|
boot: $(BIN)
|
||||||
|
@echo " LOAD $(BIN) through bootloader"
|
||||||
|
$(STBOOT) -b$(BOOTSPEED) $(BOOTPORT) -w $(BIN)
|
||||||
|
|
||||||
|
dfuboot: $(BIN)
|
||||||
|
@echo " LOAD $(BIN) THROUGH DFU"
|
||||||
|
$(DFUUTIL) -a0 -D $(BIN) -s 0x08000000
|
||||||
|
|
||||||
|
.PHONY: clean flash boot
|
||||||
1
F1-nolib/BMP280/Readme
Normal file
1
F1-nolib/BMP280/Readme
Normal file
@ -0,0 +1 @@
|
|||||||
|
Work with I2C humidity/temperature sensors SI7005 & HTU21D
|
||||||
74
F1-nolib/BMP280/dewpoint.c
Normal file
74
F1-nolib/BMP280/dewpoint.c
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2021 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "dewpoint.h"
|
||||||
|
#include "usb.h"
|
||||||
|
#include "proto.h"
|
||||||
|
|
||||||
|
#define NKNOTS (32)
|
||||||
|
const uint16_t X[NKNOTS] = {1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 19, 22, 29, 37, 47, 59, 72, 87, 104,
|
||||||
|
123, 145, 171, 200, 232, 267, 348, 443, 555, 684, 832};
|
||||||
|
const float Y[NKNOTS] = {-6.90776, -6.21461, -5.80914, -5.52146, -5.29832, -5.116, -4.96185, -4.82831,
|
||||||
|
-4.60517, -4.42285, -4.2687, -4.13517, -3.96332, -3.81671, -3.54046, -3.29684,
|
||||||
|
-3.05761, -2.83022, -2.63109, -2.44185, -2.26336, -2.09557, -1.93102, -1.76609,
|
||||||
|
-1.60944, -1.46102, -1.32051, -1.05555, -0.814186, -0.588787, -0.379797, -0.183923};
|
||||||
|
const float K[NKNOTS] = {0.693147, 0.405465, 0.287682, 0.223144, 0.182322, 0.154151, 0.133531, 0.111572,
|
||||||
|
0.0911608, 0.0770753, 0.0667657, 0.0572834, 0.0488678, 0.0394648, 0.0304528,
|
||||||
|
0.023923, 0.0189492, 0.0153176, 0.0126161, 0.010499, 0.00883123, 0.00747952,
|
||||||
|
0.00634345, 0.00540186, 0.00463813, 0.00401461, 0.00327103, 0.00254071,
|
||||||
|
0.00201249, 0.00162008, 0.00132348, 0.00109478};
|
||||||
|
|
||||||
|
float ln(uint16_t RH){
|
||||||
|
// find interval
|
||||||
|
int idx = (NKNOTS)/2, left = 0, right = NKNOTS-1; // left, right, middle
|
||||||
|
while(idx > left && idx < right){
|
||||||
|
uint16_t midval = X[idx];
|
||||||
|
if(RH < midval){
|
||||||
|
if(idx == 0) break;
|
||||||
|
if(RH > X[idx-1]){ // found
|
||||||
|
--idx;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
right = idx - 1;
|
||||||
|
idx = (left + right)/2;
|
||||||
|
}else{
|
||||||
|
if(RH < X[idx+1]) break; // found
|
||||||
|
left = idx + 1;
|
||||||
|
idx = (left + right)/2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(idx < 0) idx = 0;
|
||||||
|
else if(idx > NKNOTS-1) idx = NKNOTS - 1;
|
||||||
|
//USB_send("idx="); USB_send(u2str(idx)); USB_send("\n");
|
||||||
|
float l = Y[idx] + K[idx]*(RH - X[idx]);
|
||||||
|
#undef NKNOTS
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief dewpoint calculate dew point (*10degrC)
|
||||||
|
* @param T - temperature (*10degrC)
|
||||||
|
* @param H - rel. humidity (*10%)
|
||||||
|
* @return dewpoint
|
||||||
|
*/
|
||||||
|
int32_t dewpoint(int32_t T, uint32_t H){
|
||||||
|
float gamma = 17.27 * T / (2377 + T);
|
||||||
|
gamma += ln(H);
|
||||||
|
float d = 2377 * gamma / (17.27 - gamma);
|
||||||
|
return (int32_t)d;
|
||||||
|
}
|
||||||
22
F1-nolib/BMP280/dewpoint.h
Normal file
22
F1-nolib/BMP280/dewpoint.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2021 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stm32f1.h>
|
||||||
|
|
||||||
|
float ln(uint16_t RH);
|
||||||
|
int32_t dewpoint(int32_t T, uint32_t H);
|
||||||
48
F1-nolib/BMP280/hardware.c
Normal file
48
F1-nolib/BMP280/hardware.c
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "hardware.h"
|
||||||
|
|
||||||
|
static inline void gpio_setup(){
|
||||||
|
// Enable clocks to the GPIO subsystems (PB for ADC), turn on AFIO clocking to disable SWD/JTAG
|
||||||
|
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_AFIOEN;
|
||||||
|
// turn off SWJ/JTAG
|
||||||
|
// AFIO->MAPR = AFIO_MAPR_SWJ_CFG_DISABLE;
|
||||||
|
AFIO->MAPR = AFIO_MAPR_SWJ_CFG_JTAGDISABLE; // for PA15
|
||||||
|
// Set led as opendrain output
|
||||||
|
GPIOC->CRH |= CRH(13, CNF_ODOUTPUT|MODE_SLOW);
|
||||||
|
// USB pullup (PA15) - pushpull output
|
||||||
|
GPIOA->CRH = CRH(15, CNF_PPOUTPUT|MODE_SLOW);
|
||||||
|
}
|
||||||
|
|
||||||
|
void hw_setup(){
|
||||||
|
gpio_setup();
|
||||||
|
}
|
||||||
|
|
||||||
|
void iwdg_setup(){
|
||||||
|
uint32_t tmout = 16000000;
|
||||||
|
RCC->CSR |= RCC_CSR_LSION;
|
||||||
|
while((RCC->CSR & RCC_CSR_LSIRDY) != RCC_CSR_LSIRDY){if(--tmout == 0) break;} /* (2) */
|
||||||
|
IWDG->KR = IWDG_START;
|
||||||
|
IWDG->KR = IWDG_WRITE_ACCESS;
|
||||||
|
IWDG->PR = IWDG_PR_PR_1;
|
||||||
|
IWDG->RLR = 1250;
|
||||||
|
tmout = 16000000;
|
||||||
|
while(IWDG->SR){if(--tmout == 0) break;}
|
||||||
|
IWDG->KR = IWDG_REFRESH;
|
||||||
|
}
|
||||||
43
F1-nolib/BMP280/hardware.h
Normal file
43
F1-nolib/BMP280/hardware.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#ifndef __HARDWARE_H__
|
||||||
|
#define __HARDWARE_H__
|
||||||
|
|
||||||
|
#include "stm32f1.h"
|
||||||
|
|
||||||
|
// LED0 - PC13 (bluepill), blinking each second
|
||||||
|
#define LED0_port GPIOC
|
||||||
|
#define LED0_pin (1<<13)
|
||||||
|
|
||||||
|
// USB pullup (not present in bluepill, should be soldered) - PA15
|
||||||
|
#define USBPU_port GPIOA
|
||||||
|
#define USBPU_pin (1<<15)
|
||||||
|
#define USBPU_ON() pin_set(USBPU_port, USBPU_pin)
|
||||||
|
#define USBPU_OFF() pin_clear(USBPU_port, USBPU_pin)
|
||||||
|
|
||||||
|
#define LED_blink(x) pin_toggle(x ## _port, x ## _pin)
|
||||||
|
#define LED_on(x) pin_clear(x ## _port, x ## _pin)
|
||||||
|
#define LED_off(x) pin_set(x ## _port, x ## _pin)
|
||||||
|
|
||||||
|
void hw_setup();
|
||||||
|
void iwdg_setup();
|
||||||
|
|
||||||
|
|
||||||
|
#endif // __HARDWARE_H__
|
||||||
236
F1-nolib/BMP280/i2c.c
Normal file
236
F1-nolib/BMP280/i2c.c
Normal file
@ -0,0 +1,236 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2021 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "hardware.h"
|
||||||
|
#include "i2c.h"
|
||||||
|
|
||||||
|
#ifdef EBUG
|
||||||
|
#include "usb.h"
|
||||||
|
#include "proto.h"
|
||||||
|
#define DBG(x) do{USB_send(x);}while(0)
|
||||||
|
#else
|
||||||
|
#define DBG(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern volatile uint32_t Tms;
|
||||||
|
|
||||||
|
// current addresses for read/write (should be set with i2c_set_addr7)
|
||||||
|
static uint8_t addr7r = 0, addr7w = 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* PB10/PB6 - I2C_SCL, PB11/PB7 - I2C_SDA or remap @ PB8 & PB9
|
||||||
|
*/
|
||||||
|
void i2c_setup(){
|
||||||
|
I2C1->CR1 = 0;
|
||||||
|
I2C1->SR1 = 0;
|
||||||
|
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
|
||||||
|
GPIOB->CRL = (GPIOB->CRL & ~(GPIO_CRL_CNF6 | GPIO_CRL_CNF7)) |
|
||||||
|
CRL(6, CNF_AFOD | MODE_NORMAL) | CRL(7, CNF_AFOD | MODE_NORMAL);
|
||||||
|
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
|
||||||
|
I2C1->CR2 = 8; // FREQR=8MHz, T=125ns
|
||||||
|
I2C1->TRISE = 9; // (9-1)*125 = 1mks
|
||||||
|
I2C1->CCR = 40; // normal mode, 8MHz/2/40 = 100kHz
|
||||||
|
I2C1->CR1 |= I2C_CR1_PE; // enable periph
|
||||||
|
}
|
||||||
|
|
||||||
|
void i2c_set_addr7(uint8_t addr){
|
||||||
|
addr7w = addr << 1;
|
||||||
|
addr7r = addr7w | 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait for event evt no more than 2 ms
|
||||||
|
#define I2C_WAIT(evt) do{ register uint32_t wait4 = Tms + 2; \
|
||||||
|
while(Tms < wait4 && !(evt)) IWDG->KR = IWDG_REFRESH; \
|
||||||
|
if(!(evt)){ret = I2C_TMOUT; goto eotr;}}while(0)
|
||||||
|
// wait for !busy
|
||||||
|
#define I2C_LINEWAIT() do{ register uint32_t wait4 = Tms + 2; \
|
||||||
|
while(Tms < wait4 && (I2C1->SR2 & I2C_SR2_BUSY)) IWDG->KR = IWDG_REFRESH; \
|
||||||
|
if(I2C1->SR2 & I2C_SR2_BUSY){I2C1->CR1 |= I2C_CR1_SWRST; return I2C_LINEBUSY;}\
|
||||||
|
}while(0)
|
||||||
|
|
||||||
|
// start writing
|
||||||
|
static i2c_status i2c_7bit_startw(){
|
||||||
|
i2c_status ret = I2C_LINEBUSY;
|
||||||
|
DBG("SR1="); DBG(u2str(I2C1->SR1)); DBG("\n");
|
||||||
|
DBG("CR1="); DBG(u2str(I2C1->CR1)); DBG("\n");
|
||||||
|
DBG("CR2="); DBG(u2str(I2C1->CR2)); DBG("\n");
|
||||||
|
if(I2C1->CR1 != I2C_CR1_PE) i2c_setup();
|
||||||
|
if(I2C1->SR1) I2C1->SR1 = 0; // clear NACK and other problems
|
||||||
|
(void) I2C1->SR2;
|
||||||
|
I2C_LINEWAIT();
|
||||||
|
DBG("linew\n");
|
||||||
|
I2C1->CR1 |= I2C_CR1_START; // generate start sequence
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_SB); // wait for SB
|
||||||
|
DBG("SB\n");
|
||||||
|
(void) I2C1->SR1; // clear SB
|
||||||
|
I2C1->DR = addr7w; // set address
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_ADDR); // wait for ADDR flag (timeout @ NACK)
|
||||||
|
DBG("ADDR\n");
|
||||||
|
if(I2C1->SR1 & I2C_SR1_AF){ // NACK
|
||||||
|
return I2C_NACK;
|
||||||
|
}
|
||||||
|
DBG("ACK\n");
|
||||||
|
(void) I2C1->SR2; // clear ADDR
|
||||||
|
ret = I2C_OK;
|
||||||
|
eotr:
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* send one byte in 7bit address mode
|
||||||
|
* @param data - data to write
|
||||||
|
* @param stop - ==1 to send stop event
|
||||||
|
* @return status
|
||||||
|
*/
|
||||||
|
i2c_status i2c_7bit_send_onebyte(uint8_t data, uint8_t stop){
|
||||||
|
i2c_status ret = i2c_7bit_startw();
|
||||||
|
if(ret != I2C_OK){
|
||||||
|
I2C1->CR1 |= I2C_CR1_STOP;
|
||||||
|
goto eotr;
|
||||||
|
}
|
||||||
|
I2C1->DR = data; // init data send register
|
||||||
|
DBG("TxE\n");
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_TXE); // wait for TxE (timeout when NACK)
|
||||||
|
ret = I2C_OK;
|
||||||
|
DBG("OK\n");
|
||||||
|
if(stop){
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_BTF); // wait for BTF
|
||||||
|
DBG("BTF\n");
|
||||||
|
}
|
||||||
|
eotr:
|
||||||
|
if(stop){
|
||||||
|
I2C1->CR1 |= I2C_CR1_STOP; // generate stop event
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// send data array
|
||||||
|
i2c_status i2c_7bit_send(const uint8_t *data, int datalen){
|
||||||
|
i2c_status ret = i2c_7bit_startw();
|
||||||
|
if(ret != I2C_OK){
|
||||||
|
I2C1->CR1 |= I2C_CR1_STOP;
|
||||||
|
goto eotr;
|
||||||
|
}
|
||||||
|
for(int i = 0; i < datalen; ++i){
|
||||||
|
I2C1->DR = data[i];
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_TXE);
|
||||||
|
}
|
||||||
|
ret = I2C_OK;
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_BTF);
|
||||||
|
eotr:
|
||||||
|
I2C1->CR1 |= I2C_CR1_STOP;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_status i2c_7bit_receive_onebyte(uint8_t *data, uint8_t stop){
|
||||||
|
i2c_status ret = I2C_LINEBUSY;
|
||||||
|
//I2C_LINEWAIT();
|
||||||
|
I2C1->CR1 |= I2C_CR1_START; // generate start sequence
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_SB); // wait for SB
|
||||||
|
DBG("Rx SB\n");
|
||||||
|
(void) I2C1->SR1; // clear SB
|
||||||
|
I2C1->DR = addr7r; // set address
|
||||||
|
DBG("Rx addr\n");
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_ADDR); // wait for ADDR flag
|
||||||
|
DBG("Rx ack\n");
|
||||||
|
I2C1->CR1 &= ~I2C_CR1_ACK; // clear ACK
|
||||||
|
if(I2C1->SR1 & I2C_SR1_AF){ // NACK
|
||||||
|
DBG("Rx nak\n");
|
||||||
|
ret = I2C_NACK;
|
||||||
|
goto eotr;
|
||||||
|
}
|
||||||
|
(void) I2C1->SR2; // clear ADDR
|
||||||
|
DBG("Rx stop\n");
|
||||||
|
if(stop) I2C1->CR1 |= I2C_CR1_STOP; // program STOP
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_RXNE); // wait for RxNE
|
||||||
|
DBG("Rx OK\n");
|
||||||
|
*data = I2C1->DR; // read data & clear RxNE
|
||||||
|
ret = I2C_OK;
|
||||||
|
eotr:
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
i2c_status i2c_7bit_receive_twobytes(uint8_t *data){
|
||||||
|
i2c_status ret = I2C_LINEBUSY;
|
||||||
|
//I2C_LINEWAIT();
|
||||||
|
I2C1->CR1 |= I2C_CR1_START | I2C_CR1_POS | I2C_CR1_ACK; // generate start sequence, set pos & ack
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_SB); // wait for SB
|
||||||
|
DBG("2 Rx sb\n");
|
||||||
|
(void) I2C1->SR1; // clear SB
|
||||||
|
I2C1->DR = addr7r; // set address
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_ADDR); // wait for ADDR flag
|
||||||
|
DBG("2 ADDR\n");
|
||||||
|
if(I2C1->SR1 & I2C_SR1_AF){ // NACK
|
||||||
|
ret = I2C_NACK;
|
||||||
|
goto eotr;
|
||||||
|
}
|
||||||
|
DBG("2 ACK\n");
|
||||||
|
(void) I2C1->SR2; // clear ADDR
|
||||||
|
I2C1->CR1 &= ~I2C_CR1_ACK; // clear ACK
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_BTF); // wait for BTF
|
||||||
|
DBG("2 BTF\n");
|
||||||
|
I2C1->CR1 |= I2C_CR1_STOP; // program STOP
|
||||||
|
*data++ = I2C1->DR; *data = I2C1->DR; // read data & clear RxNE
|
||||||
|
ret = I2C_OK;
|
||||||
|
eotr:
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
// receive any amount of bytes
|
||||||
|
i2c_status i2c_7bit_receive(uint8_t *data, uint16_t nbytes){
|
||||||
|
if(nbytes == 0) return I2C_HWPROBLEM;
|
||||||
|
I2C1->SR1 = 0; // clear previous NACK flag & other error flags
|
||||||
|
if(nbytes == 1) return i2c_7bit_receive_onebyte(data, 1);
|
||||||
|
else if(nbytes == 2) return i2c_7bit_receive_twobytes(data);
|
||||||
|
i2c_status ret = I2C_LINEBUSY;
|
||||||
|
//I2C_LINEWAIT();
|
||||||
|
I2C1->CR1 |= I2C_CR1_START | I2C_CR1_ACK; // generate start sequence, set pos & ack
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_SB); // wait for SB
|
||||||
|
DBG("got SB\n");
|
||||||
|
(void) I2C1->SR1; // clear SB
|
||||||
|
I2C1->DR = addr7r; // set address
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_ADDR); // wait for ADDR flag
|
||||||
|
DBG("send addr\n");
|
||||||
|
if(I2C1->SR1 & I2C_SR1_AF){ // NACK
|
||||||
|
DBG("NACKed\n");
|
||||||
|
ret = I2C_NACK;
|
||||||
|
goto eotr;
|
||||||
|
}
|
||||||
|
DBG("ACKed\n");
|
||||||
|
(void) I2C1->SR2; // clear ADDR
|
||||||
|
for(uint16_t x = nbytes - 3; x > 0; --x){
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_RXNE); // wait next byte
|
||||||
|
*data++ = I2C1->DR; // get data
|
||||||
|
}
|
||||||
|
DBG("three left\n");
|
||||||
|
// three bytes remain to be read
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_RXNE); // wait dataN-2
|
||||||
|
DBG("dataN-2\n");
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_BTF); // wait for BTF
|
||||||
|
DBG("BTF\n");
|
||||||
|
I2C1->CR1 &= ~I2C_CR1_ACK; // clear ACK
|
||||||
|
*data++ = I2C1->DR; // read dataN-2
|
||||||
|
I2C1->CR1 |= I2C_CR1_STOP; // program STOP
|
||||||
|
*data++ = I2C1->DR; // read dataN-1
|
||||||
|
I2C_WAIT(I2C1->SR1 & I2C_SR1_RXNE); // wait next byte
|
||||||
|
*data = I2C1->DR; // read dataN
|
||||||
|
DBG("got it\n");
|
||||||
|
ret = I2C_OK;
|
||||||
|
eotr:
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
41
F1-nolib/BMP280/i2c.h
Normal file
41
F1-nolib/BMP280/i2c.h
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2021 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#ifndef I2C_H__
|
||||||
|
|
||||||
|
#include <stm32f1.h>
|
||||||
|
|
||||||
|
typedef enum{
|
||||||
|
I2C_OK = 0,
|
||||||
|
I2C_LINEBUSY,
|
||||||
|
I2C_TMOUT,
|
||||||
|
I2C_NOADDR,
|
||||||
|
I2C_NACK,
|
||||||
|
I2C_HWPROBLEM,
|
||||||
|
} i2c_status;
|
||||||
|
|
||||||
|
void i2c_setup();
|
||||||
|
void i2c_set_addr7(uint8_t addr);
|
||||||
|
i2c_status i2c_7bit_send_onebyte(uint8_t data, uint8_t stop);
|
||||||
|
i2c_status i2c_7bit_send(const uint8_t *data, int datalen);
|
||||||
|
i2c_status i2c_7bit_receive_onebyte(uint8_t *data, uint8_t stop);
|
||||||
|
i2c_status i2c_7bit_receive_twobytes(uint8_t *data);
|
||||||
|
i2c_status i2c_7bit_receive(uint8_t *data, uint16_t nbytes);
|
||||||
|
|
||||||
|
#define I2C_H__
|
||||||
|
#endif // I2C_H__
|
||||||
115
F1-nolib/BMP280/main.c
Normal file
115
F1-nolib/BMP280/main.c
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "BMP280.h"
|
||||||
|
#include "dewpoint.h"
|
||||||
|
#include "hardware.h"
|
||||||
|
#include "proto.h"
|
||||||
|
#include "usb.h"
|
||||||
|
#include "usb_lib.h"
|
||||||
|
|
||||||
|
#define USBBUFSZ 127
|
||||||
|
|
||||||
|
volatile uint32_t Tms = 0;
|
||||||
|
|
||||||
|
/* Called when systick fires */
|
||||||
|
void sys_tick_handler(void){
|
||||||
|
++Tms;
|
||||||
|
}
|
||||||
|
|
||||||
|
// usb getline
|
||||||
|
static char *get_USB(){
|
||||||
|
static char tmpbuf[USBBUFSZ+1], *curptr = tmpbuf;
|
||||||
|
static int rest = USBBUFSZ;
|
||||||
|
int x = USB_receive(curptr);
|
||||||
|
curptr[x] = 0;
|
||||||
|
if(!x) return NULL;
|
||||||
|
if(curptr[x-1] == '\n'){
|
||||||
|
curptr = tmpbuf;
|
||||||
|
rest = USBBUFSZ;
|
||||||
|
return tmpbuf;
|
||||||
|
}
|
||||||
|
curptr += x; rest -= x;
|
||||||
|
if(rest <= 0){ // buffer overflow
|
||||||
|
curptr = tmpbuf;
|
||||||
|
rest = USBBUFSZ;
|
||||||
|
USB_send("USB buffer overflow\n");
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void showd(int32_t mT, int32_t mH){
|
||||||
|
USB_send("Dewpoint=");
|
||||||
|
USB_send(u2str(dewpoint(mT, mH)));
|
||||||
|
USB_send("*10degrC\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
uint32_t lastT = 0;
|
||||||
|
sysreset();
|
||||||
|
StartHSE();
|
||||||
|
hw_setup();
|
||||||
|
SysTick_Config(72000);
|
||||||
|
|
||||||
|
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
|
||||||
|
|
||||||
|
USBPU_OFF();
|
||||||
|
USB_setup();
|
||||||
|
iwdg_setup();
|
||||||
|
USBPU_ON();
|
||||||
|
|
||||||
|
while (1){
|
||||||
|
IWDG->KR = IWDG_REFRESH; // refresh watchdog
|
||||||
|
if(lastT > Tms || Tms - lastT > 499){
|
||||||
|
LED_blink(LED0);
|
||||||
|
lastT = Tms;
|
||||||
|
}
|
||||||
|
usb_proc();
|
||||||
|
BMP280_process();
|
||||||
|
BMP280_status s = BMP280_get_status();
|
||||||
|
if(s == BMP280_RDY){ // data ready - get it
|
||||||
|
int32_t T;
|
||||||
|
uint32_t P, H;
|
||||||
|
if(BMP280_getdata(&T, &P, &H)){
|
||||||
|
USB_send("T="); USB_send(i2str(T)); USB_send(" (*100degC)\nP=");
|
||||||
|
USB_send(u2str(P)); USB_send(" (Pa) or ");
|
||||||
|
float mm = P*0.0750062;
|
||||||
|
USB_send(u2str(mm)); USB_send(" (*10mmHg)\n");
|
||||||
|
if(H){
|
||||||
|
USB_send("H="); USB_send(u2str(H)); USB_send(" (*100%)\n");
|
||||||
|
showd(T/10, H/10);
|
||||||
|
}
|
||||||
|
}else USB_send("Can't read data\n");
|
||||||
|
}else if(s == BMP280_ERR){
|
||||||
|
USB_send("Error in measurement\n");
|
||||||
|
BMP280_init();
|
||||||
|
}
|
||||||
|
|
||||||
|
char *txt, *ans;
|
||||||
|
if((txt = get_USB())){
|
||||||
|
IWDG->KR = IWDG_REFRESH;
|
||||||
|
ans = (char*)parse_cmd(txt);
|
||||||
|
if(ans){
|
||||||
|
uint16_t l = 0; char *p = ans;
|
||||||
|
while(*p++) l++;
|
||||||
|
USB_send(ans);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
283
F1-nolib/BMP280/proto.c
Normal file
283
F1-nolib/BMP280/proto.c
Normal file
@ -0,0 +1,283 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "BMP280.h"
|
||||||
|
#include "dewpoint.h"
|
||||||
|
#include "proto.h"
|
||||||
|
#include "usb.h"
|
||||||
|
|
||||||
|
extern volatile uint32_t Tms;
|
||||||
|
|
||||||
|
char *omit_spaces(const char *buf){
|
||||||
|
while(*buf){
|
||||||
|
if(*buf > ' ') break;
|
||||||
|
++buf;
|
||||||
|
}
|
||||||
|
return (char*)buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
// In case of overflow return `buf` and N==0xffffffff
|
||||||
|
// read decimal number & return pointer to next non-number symbol
|
||||||
|
static 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 (char*)buf;
|
||||||
|
}
|
||||||
|
// read hexadecimal number (without 0x prefix!)
|
||||||
|
static char *gethex(const char *buf, uint32_t *N){
|
||||||
|
char *start = (char*)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 (char*)buf;
|
||||||
|
}
|
||||||
|
// read octal number (without 0 prefix!)
|
||||||
|
static char *getoct(const char *buf, uint32_t *N){
|
||||||
|
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 (char*)buf;
|
||||||
|
}
|
||||||
|
// read binary number (without b prefix!)
|
||||||
|
static char *getbin(const char *buf, uint32_t *N){
|
||||||
|
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 (char*)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)
|
||||||
|
*/
|
||||||
|
char *getnum(const char *txt, uint32_t *N){
|
||||||
|
char *nxt = NULL;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* helpmsg =
|
||||||
|
"'0/1' - reset I2C with devADDR 0/1\n"
|
||||||
|
"'Fx'- set filter to x (0..4)\n"
|
||||||
|
"'I' - read chip ID (after init)\n"
|
||||||
|
"'M' - start measurements\n"
|
||||||
|
"'N' - read number (0..1000) and show its ln\n"
|
||||||
|
"'Ovx' - set oversampling of v(t,h or p) to x(0..5)\n"
|
||||||
|
"'R' - software reset\n"
|
||||||
|
"'W' - test watchdog\n"
|
||||||
|
;
|
||||||
|
|
||||||
|
const char *parse_cmd(const char *buf){
|
||||||
|
uint8_t u;
|
||||||
|
if(buf[1] == '\n'){ // one symbol commands
|
||||||
|
if(*buf == '0' || *buf == '1'){
|
||||||
|
BMP280_setup(*buf - '0');
|
||||||
|
if(!BMP280_init()) return "Error in init process\n";
|
||||||
|
return "OK, inited\n";
|
||||||
|
}
|
||||||
|
switch(*buf){
|
||||||
|
case 'I':
|
||||||
|
if(BMP280_read_ID(&u)){
|
||||||
|
if(u == BMP280_CHIP_ID) return "BMP280\n";
|
||||||
|
else return "BME280\n";
|
||||||
|
}else return "Failed\n";
|
||||||
|
break;
|
||||||
|
case 'M':
|
||||||
|
if(!BMP280_start()) return "BMP280 not found or not inited\n";
|
||||||
|
else return "Start\n";
|
||||||
|
break;
|
||||||
|
case 'R':
|
||||||
|
USB_send("Soft reset\n");
|
||||||
|
NVIC_SystemReset();
|
||||||
|
break;
|
||||||
|
case 'W':
|
||||||
|
USB_send("Wait for reboot\n");
|
||||||
|
while(1){nop();};
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return helpmsg;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
uint32_t Num = 0;
|
||||||
|
int32_t I;
|
||||||
|
char *nxt, *bufori = (char*)buf;
|
||||||
|
void (*osfunc)(BMP280_Oversampling);
|
||||||
|
switch(*buf){ // long messages
|
||||||
|
case 'F':
|
||||||
|
++buf;
|
||||||
|
if(buf != getnum(buf, &Num) && Num < BMP280_FILTERMAX){
|
||||||
|
BMP280_setfilter((BMP280_Filter)Num);
|
||||||
|
return "Changed\n";
|
||||||
|
}else return "Wrong filter number";
|
||||||
|
break;
|
||||||
|
case 'N':
|
||||||
|
++buf;
|
||||||
|
nxt = getnum(buf, &Num);
|
||||||
|
if(buf == nxt){
|
||||||
|
if(Num == 0) return "Wrong number\n";
|
||||||
|
return "Integer32 overflow\n";
|
||||||
|
}
|
||||||
|
USB_send("ln("); USB_send(u2str(Num)); USB_send(")=");
|
||||||
|
I = ln(Num);
|
||||||
|
USB_send(i2str(I));
|
||||||
|
if(*nxt && *nxt != '\n'){
|
||||||
|
USB_send(", the rest of string: ");
|
||||||
|
USB_send(nxt);
|
||||||
|
}else USB_send("\n");
|
||||||
|
break;
|
||||||
|
case 'O':
|
||||||
|
++buf;
|
||||||
|
osfunc = NULL;
|
||||||
|
switch(*buf){
|
||||||
|
case 'h': // h_os
|
||||||
|
osfunc = BMP280_setOSh;
|
||||||
|
break;
|
||||||
|
case 'p': // p_os
|
||||||
|
osfunc = BMP280_setOSp;
|
||||||
|
break;
|
||||||
|
case 't': // t_os
|
||||||
|
osfunc = BMP280_setOSt;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return bufori;
|
||||||
|
}
|
||||||
|
++buf;
|
||||||
|
if(osfunc && buf != getnum(buf, &Num) && Num < BMP280_OVERSMAX){
|
||||||
|
osfunc((BMP280_Oversampling)Num);
|
||||||
|
return "Changed\n";
|
||||||
|
}else return "Wrong OS number";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return bufori;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static 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){
|
||||||
|
*(--bufptr) = val % 10 + '0';
|
||||||
|
val /= 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(minus) *(--bufptr) = '-';
|
||||||
|
return bufptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return string with number `val`
|
||||||
|
char *u2str(uint32_t val){
|
||||||
|
return _2str(val, 0);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
30
F1-nolib/BMP280/proto.h
Normal file
30
F1-nolib/BMP280/proto.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#ifndef PROTO_H__
|
||||||
|
#define PROTO_H__
|
||||||
|
|
||||||
|
#include <stm32f1.h>
|
||||||
|
|
||||||
|
const char *parse_cmd(const char *buf);
|
||||||
|
char *omit_spaces(const char *buf);
|
||||||
|
char *getnum(const char *buf, uint32_t *N);
|
||||||
|
char *u2str(uint32_t val);
|
||||||
|
char *i2str(int32_t val);
|
||||||
|
|
||||||
|
#endif // PROTO_H__
|
||||||
167
F1-nolib/BMP280/usb.c
Normal file
167
F1-nolib/BMP280/usb.c
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "usb.h"
|
||||||
|
#include "usb_lib.h"
|
||||||
|
|
||||||
|
static volatile uint8_t tx_succesfull = 1;
|
||||||
|
static volatile uint8_t rxNE = 0;
|
||||||
|
|
||||||
|
static int sstrlen(const char *s){
|
||||||
|
if(!s) return 0;
|
||||||
|
int l = 0;
|
||||||
|
while(*s++) ++l;
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
// interrupt IN handler (never used?)
|
||||||
|
static void EP1_Handler(){
|
||||||
|
uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]);
|
||||||
|
if(RX_FLAG(epstatus)) epstatus = (epstatus & ~USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_RX; // set valid RX
|
||||||
|
else epstatus = epstatus & ~(USB_EPnR_STAT_TX|USB_EPnR_STAT_RX);
|
||||||
|
// clear CTR
|
||||||
|
epstatus = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX));
|
||||||
|
USB->EPnR[1] = epstatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
// data IN/OUT handlers
|
||||||
|
static void transmit_Handler(){ // EP3IN
|
||||||
|
tx_succesfull = 1;
|
||||||
|
uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[3]);
|
||||||
|
// clear CTR keep DTOGs & STATs
|
||||||
|
USB->EPnR[3] = (epstatus & ~(USB_EPnR_CTR_TX)); // clear TX ctr
|
||||||
|
}
|
||||||
|
|
||||||
|
static void receive_Handler(){ // EP2OUT
|
||||||
|
rxNE = 1;
|
||||||
|
uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[2]);
|
||||||
|
USB->EPnR[2] = (epstatus & ~(USB_EPnR_CTR_RX)); // clear RX ctr
|
||||||
|
}
|
||||||
|
|
||||||
|
void USB_setup(){
|
||||||
|
NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
|
||||||
|
NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn);
|
||||||
|
RCC->APB1ENR |= RCC_APB1ENR_USBEN;
|
||||||
|
USB->CNTR = USB_CNTR_FRES; // Force USB Reset
|
||||||
|
for(uint32_t ctr = 0; ctr < 72000; ++ctr) nop(); // wait >1ms
|
||||||
|
//uint32_t ctr = 0;
|
||||||
|
USB->CNTR = 0;
|
||||||
|
USB->BTABLE = 0;
|
||||||
|
USB->DADDR = 0;
|
||||||
|
USB->ISTR = 0;
|
||||||
|
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_WKUPM; // allow only wakeup & reset interrupts
|
||||||
|
NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int usbwr(const char *buf, int l){
|
||||||
|
uint32_t ctra = 1000000;
|
||||||
|
while(--ctra && tx_succesfull == 0){
|
||||||
|
IWDG->KR = IWDG_REFRESH;
|
||||||
|
}
|
||||||
|
tx_succesfull = 0;
|
||||||
|
EP_Write(3, (uint8_t*)buf, l);
|
||||||
|
ctra = 1000000;
|
||||||
|
while(--ctra && tx_succesfull == 0){
|
||||||
|
IWDG->KR = IWDG_REFRESH;
|
||||||
|
}
|
||||||
|
if(tx_succesfull == 0){usbON = 0; return 1;} // usb is OFF?
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char usbbuff[USB_TXBUFSZ-1]; // temporary buffer (63 - to prevent need of ZLP)
|
||||||
|
static int buflen = 0; // amount of symbols in usbbuff
|
||||||
|
|
||||||
|
// send next up to 63 bytes of data in usbbuff
|
||||||
|
static void send_next(){
|
||||||
|
if(!buflen || !tx_succesfull) return;
|
||||||
|
tx_succesfull = 0;
|
||||||
|
EP_Write(3, (uint8_t*)usbbuff, buflen);
|
||||||
|
buflen = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// unblocking sending - just fill a buffer
|
||||||
|
void USB_send(const char *buf){
|
||||||
|
int len = sstrlen(buf);
|
||||||
|
if(!usbON || !len) return;
|
||||||
|
if(len > USB_TXBUFSZ-1 - buflen){
|
||||||
|
usbwr(usbbuff, buflen);
|
||||||
|
buflen = 0;
|
||||||
|
}
|
||||||
|
if(len > USB_TXBUFSZ-1){
|
||||||
|
USB_send_blk(buf, len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while(len--) usbbuff[buflen++] = *buf++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// blocking sending
|
||||||
|
void USB_send_blk(const char *buf, int len){
|
||||||
|
if(!usbON || !len) return; // USB disconnected
|
||||||
|
if(buflen){
|
||||||
|
usbwr(usbbuff, buflen);
|
||||||
|
buflen = 0;
|
||||||
|
}
|
||||||
|
int needzlp = 0;
|
||||||
|
while(len){
|
||||||
|
if(len == USB_TXBUFSZ) needzlp = 1;
|
||||||
|
int s = (len > USB_TXBUFSZ) ? USB_TXBUFSZ : len;
|
||||||
|
if(usbwr(buf, s)) return;
|
||||||
|
len -= s;
|
||||||
|
buf += s;
|
||||||
|
}
|
||||||
|
if(needzlp){
|
||||||
|
usbwr(NULL, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void usb_proc(){
|
||||||
|
switch(USB_Dev.USB_Status){
|
||||||
|
case USB_STATE_CONFIGURED:
|
||||||
|
// make new BULK endpoint
|
||||||
|
// Buffer have 1024 bytes, but last 256 we use for CAN bus (30.2 of RM: USB main features)
|
||||||
|
EP_Init(1, EP_TYPE_INTERRUPT, USB_EP1BUFSZ, 0, EP1_Handler); // IN1 - transmit
|
||||||
|
EP_Init(2, EP_TYPE_BULK, 0, USB_RXBUFSZ, receive_Handler); // OUT2 - receive data
|
||||||
|
EP_Init(3, EP_TYPE_BULK, USB_TXBUFSZ, 0, transmit_Handler); // IN3 - transmit data
|
||||||
|
USB_Dev.USB_Status = USB_STATE_CONNECTED;
|
||||||
|
break;
|
||||||
|
case USB_STATE_DEFAULT:
|
||||||
|
case USB_STATE_ADDRESSED:
|
||||||
|
if(usbON){
|
||||||
|
usbON = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default: // USB_STATE_CONNECTED - send next data portion
|
||||||
|
if(!usbON) return;
|
||||||
|
send_next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief USB_receive
|
||||||
|
* @param buf (i) - buffer[64] for received data
|
||||||
|
* @return amount of received bytes
|
||||||
|
*/
|
||||||
|
int USB_receive(char *buf){
|
||||||
|
if(!usbON || !rxNE) return 0;
|
||||||
|
int sz = EP_Read(2, (uint16_t*)buf);
|
||||||
|
uint16_t epstatus = KEEP_DTOG(USB->EPnR[2]);
|
||||||
|
// keep stat_tx & set ACK rx
|
||||||
|
USB->EPnR[2] = (epstatus & ~(USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX;
|
||||||
|
rxNE = 0;
|
||||||
|
return sz;
|
||||||
|
}
|
||||||
33
F1-nolib/BMP280/usb.h
Normal file
33
F1-nolib/BMP280/usb.h
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#ifndef __USB_H__
|
||||||
|
#define __USB_H__
|
||||||
|
|
||||||
|
#include "hardware.h"
|
||||||
|
|
||||||
|
#define BUFFSIZE (64)
|
||||||
|
|
||||||
|
void USB_setup();
|
||||||
|
void usb_proc();
|
||||||
|
void USB_send(const char *buf);
|
||||||
|
void USB_send_blk(const char *buf, int len);
|
||||||
|
int USB_receive(char *buf);
|
||||||
|
|
||||||
|
#endif // __USB_H__
|
||||||
102
F1-nolib/BMP280/usb_defs.h
Normal file
102
F1-nolib/BMP280/usb_defs.h
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#ifndef __USB_DEFS_H__
|
||||||
|
#define __USB_DEFS_H__
|
||||||
|
|
||||||
|
#include <stm32f1.h>
|
||||||
|
|
||||||
|
// max endpoints number
|
||||||
|
#define STM32ENDPOINTS 8
|
||||||
|
/**
|
||||||
|
* Buffers size definition
|
||||||
|
**/
|
||||||
|
#define USB_BTABLE_SIZE 512
|
||||||
|
// first 64 bytes of USB_BTABLE are registers!
|
||||||
|
//#define USB_EP0_BASEADDR 64
|
||||||
|
// for USB FS EP0 buffers are from 8 to 64 bytes long (64 for PL2303)
|
||||||
|
#define USB_EP0_BUFSZ 64
|
||||||
|
// USB transmit buffer size (64 for PL2303)
|
||||||
|
#define USB_TXBUFSZ 64
|
||||||
|
// USB receive buffer size (64 for PL2303)
|
||||||
|
#define USB_RXBUFSZ 64
|
||||||
|
// EP1 - interrupt - buffer size
|
||||||
|
#define USB_EP1BUFSZ 8
|
||||||
|
|
||||||
|
#define USB_BTABLE_BASE 0x40006000
|
||||||
|
#define USB_BASE ((uint32_t)0x40005C00)
|
||||||
|
#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
|
||||||
|
|
||||||
|
#ifdef USB_TypeDef
|
||||||
|
#define USB_TypeDef USB_TypeDef_custom
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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;
|
||||||
|
} USB_TypeDef;
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
__IO uint32_t USB_ADDR_TX;
|
||||||
|
__IO uint32_t USB_COUNT_TX;
|
||||||
|
__IO uint32_t USB_ADDR_RX;
|
||||||
|
__IO uint32_t USB_COUNT_RX;
|
||||||
|
} USB_EPDATA_TypeDef;
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
__IO USB_EPDATA_TypeDef EP[STM32ENDPOINTS];
|
||||||
|
} USB_BtableDef;
|
||||||
|
|
||||||
|
#endif // __USB_DEFS_H__
|
||||||
476
F1-nolib/BMP280/usb_lib.c
Normal file
476
F1-nolib/BMP280/usb_lib.c
Normal file
@ -0,0 +1,476 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "usb_lib.h"
|
||||||
|
|
||||||
|
ep_t endpoints[STM32ENDPOINTS];
|
||||||
|
|
||||||
|
usb_dev_t USB_Dev;
|
||||||
|
static usb_LineCoding lineCoding = {115200, 0, 0, 8};
|
||||||
|
static config_pack_t setup_packet;
|
||||||
|
static uint8_t ep0databuf[EP0DATABUF_SIZE];
|
||||||
|
static uint8_t ep0dbuflen = 0;
|
||||||
|
|
||||||
|
usb_LineCoding getLineCoding(){return lineCoding;}
|
||||||
|
|
||||||
|
uint8_t usbON = 0; // device disconnected from terminal
|
||||||
|
|
||||||
|
// definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor
|
||||||
|
#define bcdUSB_L 0x10
|
||||||
|
#define bcdUSB_H 0x01
|
||||||
|
#define bDeviceClass 0
|
||||||
|
#define bDeviceSubClass 0
|
||||||
|
#define bDeviceProtocol 0
|
||||||
|
#define bNumConfigurations 1
|
||||||
|
|
||||||
|
static const uint8_t USB_DeviceDescriptor[] = {
|
||||||
|
18, // bLength
|
||||||
|
0x01, // bDescriptorType - Device descriptor
|
||||||
|
bcdUSB_L, // bcdUSB_L - 1.10
|
||||||
|
bcdUSB_H, // bcdUSB_H
|
||||||
|
bDeviceClass, // bDeviceClass - USB_COMM
|
||||||
|
bDeviceSubClass, // bDeviceSubClass
|
||||||
|
bDeviceProtocol, // bDeviceProtocol
|
||||||
|
USB_EP0_BUFSZ, // bMaxPacketSize
|
||||||
|
0x7b, // idVendor_L PL2303: VID=0x067b, PID=0x2303
|
||||||
|
0x06, // idVendor_H
|
||||||
|
0x03, // idProduct_L
|
||||||
|
0x23, // idProduct_H
|
||||||
|
0x00, // bcdDevice_Ver_L
|
||||||
|
0x03, // bcdDevice_Ver_H
|
||||||
|
0x01, // iManufacturer
|
||||||
|
0x02, // iProduct
|
||||||
|
0x00, // iSerialNumber
|
||||||
|
bNumConfigurations // bNumConfigurations
|
||||||
|
};
|
||||||
|
|
||||||
|
static const uint8_t USB_DeviceQualifierDescriptor[] = {
|
||||||
|
10, //bLength
|
||||||
|
0x06, // bDescriptorType - Device qualifier
|
||||||
|
bcdUSB_L, // bcdUSB_L
|
||||||
|
bcdUSB_H, // bcdUSB_H
|
||||||
|
bDeviceClass, // bDeviceClass
|
||||||
|
bDeviceSubClass, // bDeviceSubClass
|
||||||
|
bDeviceProtocol, // bDeviceProtocol
|
||||||
|
USB_EP0_BUFSZ, // bMaxPacketSize0
|
||||||
|
bNumConfigurations, // bNumConfigurations
|
||||||
|
0x00 // Reserved
|
||||||
|
};
|
||||||
|
|
||||||
|
static const uint8_t USB_ConfigDescriptor[] = {
|
||||||
|
/*Configuration Descriptor*/
|
||||||
|
0x09, /* bLength: Configuration Descriptor size */
|
||||||
|
0x02, /* bDescriptorType: Configuration */
|
||||||
|
39, /* wTotalLength:no of returned bytes */
|
||||||
|
0x00,
|
||||||
|
0x01, /* bNumInterfaces: 1 interface */
|
||||||
|
0x01, /* bConfigurationValue: Configuration value */
|
||||||
|
0x00, /* iConfiguration: Index of string descriptor describing the configuration */
|
||||||
|
0xa0, /* bmAttributes - Bus powered, Remote wakeup */
|
||||||
|
0x32, /* MaxPower 100 mA */
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/*Interface Descriptor */
|
||||||
|
0x09, /* bLength: Interface Descriptor size */
|
||||||
|
0x04, /* bDescriptorType: Interface */
|
||||||
|
0x00, /* bInterfaceNumber: Number of Interface */
|
||||||
|
0x00, /* bAlternateSetting: Alternate setting */
|
||||||
|
0x03, /* bNumEndpoints: 3 endpoints used */
|
||||||
|
0xff, /* bInterfaceClass */
|
||||||
|
0x00, /* bInterfaceSubClass */
|
||||||
|
0x00, /* bInterfaceProtocol */
|
||||||
|
0x00, /* iInterface: */
|
||||||
|
///////////////////////////////////////////////////
|
||||||
|
/*Endpoint 1 Descriptor*/
|
||||||
|
0x07, /* bLength: Endpoint Descriptor size */
|
||||||
|
0x05, /* bDescriptorType: Endpoint */
|
||||||
|
0x81, /* bEndpointAddress IN1 */
|
||||||
|
0x03, /* bmAttributes: Interrupt */
|
||||||
|
0x0a, /* wMaxPacketSize LO: */
|
||||||
|
0x00, /* wMaxPacketSize HI: */
|
||||||
|
0x01, /* bInterval: */
|
||||||
|
|
||||||
|
/*Endpoint OUT2 Descriptor*/
|
||||||
|
0x07, /* bLength: Endpoint Descriptor size */
|
||||||
|
0x05, /* bDescriptorType: Endpoint */
|
||||||
|
0x02, /* bEndpointAddress: OUT2 */
|
||||||
|
0x02, /* bmAttributes: Bulk */
|
||||||
|
(USB_RXBUFSZ & 0xff), /* wMaxPacketSize: 64 */
|
||||||
|
(USB_RXBUFSZ >> 8),
|
||||||
|
0x00, /* bInterval: ignore for Bulk transfer */
|
||||||
|
|
||||||
|
/*Endpoint IN3 Descriptor*/
|
||||||
|
0x07, /* bLength: Endpoint Descriptor size */
|
||||||
|
0x05, /* bDescriptorType: Endpoint */
|
||||||
|
0x83, /* bEndpointAddress IN3 */
|
||||||
|
0x02, /* bmAttributes: Bulk */
|
||||||
|
(USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */
|
||||||
|
(USB_TXBUFSZ >> 8),
|
||||||
|
0x00, /* bInterval: ignore for Bulk transfer */
|
||||||
|
};
|
||||||
|
|
||||||
|
_USB_LANG_ID_(USB_StringLangDescriptor, LANG_US);
|
||||||
|
// these descriptors are not used in PL2303 emulator!
|
||||||
|
_USB_STRING_(USB_StringSerialDescriptor, u"0");
|
||||||
|
_USB_STRING_(USB_StringManufacturingDescriptor, u"Prolific Technology Inc.");
|
||||||
|
_USB_STRING_(USB_StringProdDescriptor, u"USB-Serial Controller");
|
||||||
|
|
||||||
|
/*
|
||||||
|
* default handlers
|
||||||
|
*/
|
||||||
|
// SET_LINE_CODING
|
||||||
|
void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lc){
|
||||||
|
}
|
||||||
|
|
||||||
|
// SET_CONTROL_LINE_STATE
|
||||||
|
void WEAK clstate_handler(uint16_t __attribute__((unused)) val){
|
||||||
|
}
|
||||||
|
|
||||||
|
// SEND_BREAK
|
||||||
|
void WEAK break_handler(){
|
||||||
|
}
|
||||||
|
|
||||||
|
// handler of vendor requests
|
||||||
|
void WEAK vendor_handler(config_pack_t *packet){
|
||||||
|
if(packet->bmRequestType & 0x80){ // read
|
||||||
|
uint8_t c;
|
||||||
|
switch(packet->wValue){
|
||||||
|
case 0x8484:
|
||||||
|
c = 2;
|
||||||
|
break;
|
||||||
|
case 0x0080:
|
||||||
|
c = 1;
|
||||||
|
break;
|
||||||
|
case 0x8686:
|
||||||
|
c = 0xaa;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
c = 0;
|
||||||
|
}
|
||||||
|
EP_WriteIRQ(0, &c, 1);
|
||||||
|
}else{ // write ZLP
|
||||||
|
EP_WriteIRQ(0, (uint8_t *)0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void wr0(const uint8_t *buf, uint16_t size){
|
||||||
|
if(setup_packet.wLength < size) size = setup_packet.wLength; // shortened request
|
||||||
|
if(size < endpoints[0].txbufsz){
|
||||||
|
EP_WriteIRQ(0, buf, size);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while(size){
|
||||||
|
uint16_t l = size;
|
||||||
|
if(l > endpoints[0].txbufsz) l = endpoints[0].txbufsz;
|
||||||
|
EP_WriteIRQ(0, buf, l);
|
||||||
|
buf += l;
|
||||||
|
size -= l;
|
||||||
|
uint8_t needzlp = (l == endpoints[0].txbufsz) ? 1 : 0;
|
||||||
|
if(size || needzlp){ // send last data buffer
|
||||||
|
uint16_t status = KEEP_DTOG(USB->EPnR[0]);
|
||||||
|
// keep DTOGs, clear CTR_RX,TX, set TX VALID, leave stat_Rx
|
||||||
|
USB->EPnR[0] = (status & ~(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, (uint8_t*)0, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void get_descriptor(){
|
||||||
|
switch(setup_packet.wValue){
|
||||||
|
case DEVICE_DESCRIPTOR:
|
||||||
|
wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor));
|
||||||
|
break;
|
||||||
|
case CONFIGURATION_DESCRIPTOR:
|
||||||
|
wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor));
|
||||||
|
break;
|
||||||
|
case STRING_LANG_DESCRIPTOR:
|
||||||
|
wr0((const uint8_t *)&USB_StringLangDescriptor, STRING_LANG_DESCRIPTOR_SIZE_BYTE);
|
||||||
|
break;
|
||||||
|
case STRING_MAN_DESCRIPTOR:
|
||||||
|
wr0((const uint8_t *)&USB_StringManufacturingDescriptor, USB_StringManufacturingDescriptor.bLength);
|
||||||
|
break;
|
||||||
|
case STRING_PROD_DESCRIPTOR:
|
||||||
|
wr0((const uint8_t *)&USB_StringProdDescriptor, USB_StringProdDescriptor.bLength);
|
||||||
|
break;
|
||||||
|
case STRING_SN_DESCRIPTOR:
|
||||||
|
wr0((const uint8_t *)&USB_StringSerialDescriptor, USB_StringSerialDescriptor.bLength);
|
||||||
|
break;
|
||||||
|
case DEVICE_QUALIFIER_DESCRIPTOR:
|
||||||
|
wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured)
|
||||||
|
static inline void std_d2h_req(){
|
||||||
|
uint16_t status = 0; // bus powered
|
||||||
|
switch(setup_packet.bRequest){
|
||||||
|
case GET_DESCRIPTOR:
|
||||||
|
get_descriptor();
|
||||||
|
break;
|
||||||
|
case GET_STATUS:
|
||||||
|
EP_WriteIRQ(0, (uint8_t *)&status, 2); // send status: Bus Powered
|
||||||
|
break;
|
||||||
|
case GET_CONFIGURATION:
|
||||||
|
EP_WriteIRQ(0, &configuration, 1);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
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_Dev.USB_Addr = setup_packet.wValue;
|
||||||
|
break;
|
||||||
|
case SET_CONFIGURATION:
|
||||||
|
// Now device configured
|
||||||
|
USB_Dev.USB_Status = USB_STATE_CONFIGURED;
|
||||||
|
configuration = setup_packet.wValue;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
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(){
|
||||||
|
uint16_t epstatus = USB->EPnR[0]; // EP0R on input -> return this value after modifications
|
||||||
|
uint8_t reqtype = setup_packet.bmRequestType & 0x7f;
|
||||||
|
uint8_t dev2host = (setup_packet.bmRequestType & 0x80) ? 1 : 0;
|
||||||
|
int rxflag = RX_FLAG(epstatus);
|
||||||
|
if(rxflag && SETUP_FLAG(epstatus)){
|
||||||
|
switch(reqtype){
|
||||||
|
case STANDARD_DEVICE_REQUEST_TYPE: // standard device request
|
||||||
|
if(dev2host){
|
||||||
|
std_d2h_req();
|
||||||
|
}else{
|
||||||
|
std_h2d_req();
|
||||||
|
EP_WriteIRQ(0, (uint8_t *)0, 0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case STANDARD_ENDPOINT_REQUEST_TYPE: // standard endpoint request
|
||||||
|
if(setup_packet.bRequest == CLEAR_FEATURE){
|
||||||
|
EP_WriteIRQ(0, (uint8_t *)0, 0);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case VENDOR_REQUEST_TYPE:
|
||||||
|
vendor_handler(&setup_packet);
|
||||||
|
break;
|
||||||
|
case CONTROL_REQUEST_TYPE:
|
||||||
|
switch(setup_packet.bRequest){
|
||||||
|
case GET_LINE_CODING:
|
||||||
|
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
|
||||||
|
break;
|
||||||
|
case SET_LINE_CODING: // omit this for next stage, when data will come
|
||||||
|
break;
|
||||||
|
case SET_CONTROL_LINE_STATE:
|
||||||
|
usbON = 1;
|
||||||
|
clstate_handler(setup_packet.wValue);
|
||||||
|
break;
|
||||||
|
case SEND_BREAK:
|
||||||
|
usbON = 0;
|
||||||
|
break_handler();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(setup_packet.bRequest != GET_LINE_CODING) EP_WriteIRQ(0, (uint8_t *)0, 0); // write acknowledgement
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
EP_WriteIRQ(0, (uint8_t *)0, 0);
|
||||||
|
}
|
||||||
|
}else if(rxflag){ // got data over EP0 or host acknowlegement
|
||||||
|
if(endpoints[0].rx_cnt){
|
||||||
|
if(setup_packet.bRequest == SET_LINE_CODING){
|
||||||
|
linecoding_handler((usb_LineCoding*)ep0databuf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if(TX_FLAG(epstatus)){ // package transmitted
|
||||||
|
// now we can change address after enumeration
|
||||||
|
if ((USB->DADDR & USB_DADDR_ADD) != USB_Dev.USB_Addr){
|
||||||
|
USB->DADDR = USB_DADDR_EF | USB_Dev.USB_Addr;
|
||||||
|
// change state to ADRESSED
|
||||||
|
USB_Dev.USB_Status = USB_STATE_ADDRESSED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
epstatus = KEEP_DTOG(USB->EPnR[0]);
|
||||||
|
if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP/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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 || rxsz > USB_BTABLE_SIZE) return 1; // buffer too large
|
||||||
|
if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE) 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 || rxsz > 512) 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*2);
|
||||||
|
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 = (uint16_t *)(USB_BTABLE_BASE + lastaddr*2);
|
||||||
|
lastaddr += rxsz;
|
||||||
|
USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10;
|
||||||
|
endpoints[number].func = func;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// standard IRQ handler
|
||||||
|
void usb_lp_can_rx0_isr(){
|
||||||
|
if(USB->ISTR & USB_ISTR_RESET){
|
||||||
|
usbON = 0;
|
||||||
|
// Reinit registers
|
||||||
|
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM | USB_CNTR_WKUPM;
|
||||||
|
USB->ISTR = 0;
|
||||||
|
// 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_Dev.USB_Status = USB_STATE_DEFAULT;
|
||||||
|
USB->ISTR = ~USB_ISTR_RESET;
|
||||||
|
if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0_BUFSZ, USB_EP0_BUFSZ, EP0_Handler)){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(USB->ISTR & USB_ISTR_CTR){
|
||||||
|
// EP number
|
||||||
|
uint8_t n = USB->ISTR & USB_ISTR_EPID;
|
||||||
|
// copy status register
|
||||||
|
uint16_t epstatus = USB->EPnR[n];
|
||||||
|
// copy received bytes amount
|
||||||
|
endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter
|
||||||
|
// 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(n == 0){ // control endpoint
|
||||||
|
if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack
|
||||||
|
EP_Read(0, (uint16_t*)&setup_packet);
|
||||||
|
ep0dbuflen = 0;
|
||||||
|
// interrupt handler will be called later
|
||||||
|
}else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf
|
||||||
|
ep0dbuflen = endpoints[0].rx_cnt;
|
||||||
|
EP_Read(0, (uint16_t*)&ep0databuf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// call EP handler
|
||||||
|
if(endpoints[n].func) endpoints[n].func(endpoints[n]);
|
||||||
|
}
|
||||||
|
if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep
|
||||||
|
usbON = 0;
|
||||||
|
USB->CNTR |= USB_CNTR_FSUSP | USB_CNTR_LP_MODE;
|
||||||
|
USB->ISTR = ~USB_ISTR_SUSP;
|
||||||
|
}
|
||||||
|
if(USB->ISTR & USB_ISTR_WKUP){ // wakeup
|
||||||
|
USB->CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LP_MODE); // clear suspend flags
|
||||||
|
USB->ISTR = ~USB_ISTR_WKUP;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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){
|
||||||
|
uint8_t i;
|
||||||
|
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;
|
||||||
|
uint32_t *out = (uint32_t *)endpoints[number].tx_buf;
|
||||||
|
for(i = 0; i < N2; ++i, ++out){
|
||||||
|
*out = buf16[i];
|
||||||
|
}
|
||||||
|
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 status = KEEP_DTOG(USB->EPnR[number]);
|
||||||
|
// keep DTOGs, clear CTR_TX & set TX VALID to start transmission
|
||||||
|
USB->EPnR[number] = (status & ~(USB_EPnR_CTR_TX)) ^ 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, uint16_t *buf){
|
||||||
|
int sz = endpoints[number].rx_cnt;
|
||||||
|
if(!sz) return 0;
|
||||||
|
endpoints[number].rx_cnt = 0;
|
||||||
|
int n = (sz + 1) >> 1;
|
||||||
|
uint32_t *in = (uint32_t *)endpoints[number].rx_buf;
|
||||||
|
if(n){
|
||||||
|
for(int i = 0; i < n; ++i, ++in)
|
||||||
|
buf[i] = *(uint16_t*)in;
|
||||||
|
}
|
||||||
|
return sz;
|
||||||
|
}
|
||||||
|
|
||||||
184
F1-nolib/BMP280/usb_lib.h
Normal file
184
F1-nolib/BMP280/usb_lib.h
Normal file
@ -0,0 +1,184 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the BMP280 project.
|
||||||
|
* Copyright 2020 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#ifndef __USB_LIB_H__
|
||||||
|
#define __USB_LIB_H__
|
||||||
|
|
||||||
|
#include <wchar.h>
|
||||||
|
#include "usb_defs.h"
|
||||||
|
|
||||||
|
#define EP0DATABUF_SIZE (64)
|
||||||
|
#define LASTADDR_DEFAULT (STM32ENDPOINTS * 8)
|
||||||
|
|
||||||
|
// bmRequestType & 0x7f
|
||||||
|
#define STANDARD_DEVICE_REQUEST_TYPE 0
|
||||||
|
#define STANDARD_ENDPOINT_REQUEST_TYPE 2
|
||||||
|
#define VENDOR_REQUEST_TYPE 0x40
|
||||||
|
#define CONTROL_REQUEST_TYPE 0x21
|
||||||
|
// bRequest, standard; for bmRequestType == 0x80
|
||||||
|
#define GET_STATUS 0x00
|
||||||
|
#define GET_DESCRIPTOR 0x06
|
||||||
|
#define GET_CONFIGURATION 0x08
|
||||||
|
// for bmRequestType == 0
|
||||||
|
#define CLEAR_FEATURE 0x01
|
||||||
|
#define SET_FEATURE 0x03 // unused
|
||||||
|
#define SET_ADDRESS 0x05
|
||||||
|
#define SET_DESCRIPTOR 0x07 // unused
|
||||||
|
#define SET_CONFIGURATION 0x09
|
||||||
|
// for bmRequestType == 0x81, 1 or 0xB2
|
||||||
|
#define GET_INTERFACE 0x0A // unused
|
||||||
|
#define SET_INTERFACE 0x0B // unused
|
||||||
|
#define SYNC_FRAME 0x0C // unused
|
||||||
|
#define VENDOR_REQUEST 0x01 // unused
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
// wValue
|
||||||
|
#define DEVICE_DESCRIPTOR 0x100
|
||||||
|
#define CONFIGURATION_DESCRIPTOR 0x200
|
||||||
|
#define STRING_LANG_DESCRIPTOR 0x300
|
||||||
|
#define STRING_MAN_DESCRIPTOR 0x301
|
||||||
|
#define STRING_PROD_DESCRIPTOR 0x302
|
||||||
|
#define STRING_SN_DESCRIPTOR 0x303
|
||||||
|
#define DEVICE_QUALIFIER_DESCRIPTOR 0x600
|
||||||
|
|
||||||
|
#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))
|
||||||
|
|
||||||
|
// USB state: uninitialized, addressed, ready for use
|
||||||
|
typedef enum{
|
||||||
|
USB_STATE_DEFAULT,
|
||||||
|
USB_STATE_ADDRESSED,
|
||||||
|
USB_STATE_CONFIGURED,
|
||||||
|
USB_STATE_CONNECTED
|
||||||
|
} USB_state;
|
||||||
|
|
||||||
|
// EP types
|
||||||
|
#define EP_TYPE_BULK 0x00
|
||||||
|
#define EP_TYPE_CONTROL 0x01
|
||||||
|
#define EP_TYPE_ISO 0x02
|
||||||
|
#define EP_TYPE_INTERRUPT 0x03
|
||||||
|
|
||||||
|
#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}
|
||||||
|
#define STRING_LANG_DESCRIPTOR_SIZE_BYTE (4)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
uint16_t *rx_buf; // reception buffer address
|
||||||
|
void (*func)(); // endpoint action function
|
||||||
|
unsigned rx_cnt : 10; // received data counter
|
||||||
|
} ep_t;
|
||||||
|
|
||||||
|
// USB status & its address
|
||||||
|
typedef struct {
|
||||||
|
uint8_t USB_Status;
|
||||||
|
uint16_t USB_Addr;
|
||||||
|
}usb_dev_t;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t bmRequestType;
|
||||||
|
uint8_t bNotificationType;
|
||||||
|
uint16_t wValue;
|
||||||
|
uint16_t wIndex;
|
||||||
|
uint16_t wLength;
|
||||||
|
} __attribute__ ((packed)) usb_cdc_notification;
|
||||||
|
|
||||||
|
extern ep_t endpoints[];
|
||||||
|
extern usb_dev_t USB_Dev;
|
||||||
|
extern uint8_t usbON;
|
||||||
|
|
||||||
|
void USB_Init();
|
||||||
|
void USB_ResetState();
|
||||||
|
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, uint16_t *buf);
|
||||||
|
usb_LineCoding getLineCoding();
|
||||||
|
|
||||||
|
void linecoding_handler(usb_LineCoding *lc);
|
||||||
|
void clstate_handler(uint16_t val);
|
||||||
|
void break_handler();
|
||||||
|
void vendor_handler(config_pack_t *packet);
|
||||||
|
|
||||||
|
#endif // __USB_LIB_H__
|
||||||
Loading…
x
Reference in New Issue
Block a user