From 5d3a0b17790327bd258257848ecd74894a041592 Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Fri, 26 Feb 2021 00:02:48 +0300 Subject: [PATCH] add DHT22/DHT11 --- F1-nolib/BMP180/BMP180.bin | Bin 9880 -> 9864 bytes F1-nolib/BMP180/BMP180.c | 10 +- F1-nolib/BMP180/BMP180.h | 4 +- F1-nolib/BMP180/Readme | 2 +- F1-nolib/BMP180/main.c | 2 +- F1-nolib/BMP180/proto.c | 2 +- F1-nolib/BMP280/Readme | 3 +- F1-nolib/DHT22_DHT11/DHT22.bin | Bin 0 -> 5272 bytes F1-nolib/DHT22_DHT11/Makefile | 139 ++++++++++ F1-nolib/DHT22_DHT11/Readme | 2 + F1-nolib/DHT22_DHT11/dht.c | 210 ++++++++++++++ F1-nolib/DHT22_DHT11/dht.h | 48 ++++ F1-nolib/DHT22_DHT11/hardware.c | 48 ++++ F1-nolib/DHT22_DHT11/hardware.h | 42 +++ F1-nolib/DHT22_DHT11/main.c | 104 +++++++ F1-nolib/DHT22_DHT11/proto.c | 95 +++++++ F1-nolib/DHT22_DHT11/proto.h | 28 ++ F1-nolib/DHT22_DHT11/usb.c | 162 +++++++++++ F1-nolib/DHT22_DHT11/usb.h | 33 +++ F1-nolib/DHT22_DHT11/usb_defs.h | 102 +++++++ F1-nolib/DHT22_DHT11/usb_lib.c | 476 ++++++++++++++++++++++++++++++++ F1-nolib/DHT22_DHT11/usb_lib.h | 184 ++++++++++++ 22 files changed, 1685 insertions(+), 11 deletions(-) create mode 100755 F1-nolib/DHT22_DHT11/DHT22.bin create mode 100644 F1-nolib/DHT22_DHT11/Makefile create mode 100644 F1-nolib/DHT22_DHT11/Readme create mode 100644 F1-nolib/DHT22_DHT11/dht.c create mode 100644 F1-nolib/DHT22_DHT11/dht.h create mode 100644 F1-nolib/DHT22_DHT11/hardware.c create mode 100644 F1-nolib/DHT22_DHT11/hardware.h create mode 100644 F1-nolib/DHT22_DHT11/main.c create mode 100644 F1-nolib/DHT22_DHT11/proto.c create mode 100644 F1-nolib/DHT22_DHT11/proto.h create mode 100644 F1-nolib/DHT22_DHT11/usb.c create mode 100644 F1-nolib/DHT22_DHT11/usb.h create mode 100644 F1-nolib/DHT22_DHT11/usb_defs.h create mode 100644 F1-nolib/DHT22_DHT11/usb_lib.c create mode 100644 F1-nolib/DHT22_DHT11/usb_lib.h diff --git a/F1-nolib/BMP180/BMP180.bin b/F1-nolib/BMP180/BMP180.bin index 25181ade8956389a6ec7069c70991baea13c43d3..8bd490264c18b9e28fad855443bbf961fa99bf64 100755 GIT binary patch delta 1117 zcmb`HUr19?9LIlWTRC;obQ=3tF1P8L36>O*g3IZcyUuI)hqSD#72TpYF?y*b8B{M) z6BNn_Do6{{29{7wL?qOQg(O9VO2I8GIFz-!J8S*cLl0W?PzOHuobUI1f9H44eYoA; zb6y`*fho@b%8iJ18L(``GjZbf&wTRAn&oHwMf+AX{i6NnLsy*n=a*prbF+Y#L=p|1 zOs7Z3pB>mq3DI%&e95OZtx1jsPS1Dvo05wYPcnM`lDzEMDSvNrBRS|#P;J?losb1q zmV%?6sb}jds>}Ct?V_3M;q)AHw9ezCr(2k2braADDHiTW(wEppaC*M>*(*J4C0j8o zx(8WyR@~i&T+}o@5#d?^f4f96UBK`0cd6F-w(>xo(O6i(>I2d$+vbqm)(|qGdm##0 zW?o+-F9q97NF$O>N%oY`4&KbE^0!eUrJ+w&s@l1Z*$zsb7QZ3 zpqdES?uX8>E_CAVVXGf``!8|Y(+bOQ}UfDTJpsD~sO>hq`vrA*ZQ zs0rOAs;*DJ2Q#7LBB3)9p_VAn4uPja~EDD1u;lh1b=r*zOKju$|?Yp zXa?mHqC%}jEP?ryR^oGqWCQ9)`Gq;Tj_0%Q@ z!h0hY#_9o^DyVg-T{A-bautytQDHK{b%X0 zpl7uFkWgwgxkzs;ygJriA`6jjxsANYaQ6u#_hR7*QRBWTXs~^4B>0)_NGnfcCV1K6 zT(ibG$;F-T0t7|8;S|Cl50Dz96S;$&K-v)%ew9QX7bD1rz#(;uJTW5`lt&P6U|#b^ zp?hlurHClAL8(9<;<^ZDKJZ$5n2_E;nLd}KF2u0qN0`HZfXRp)3$z&$lGQ2<6L0{G I65LV34=g!MQ~&?~ delta 1118 zcmb`HUr1AN6vxlcDPG-1lMUOZ(sFKHwnz&FOCp^%=I&$@vP`kA(w|FDg$M@4EDU^0 zTbNKuq=EKO*7{GPlz~yg%!CBPNb1%@@FS(&-I?p$9(vHChx) z>-L(2Gec#R5EH*waD83Vl3rTJn}x1Wqk2bLkTVPCR6D2JLWAlCawwFl*|N`;nhSP6 zh0=Pi-e2phsoc-EOV&UyZ|1opwcc{t*332O8iBr&6VV%pVadM;<=$`C9RY83Rkd$U z3f!vp&q@3#q)Xp8F2NP25OyU`IfaX%3!0qZCIKiHXN5%cBi1*`DeUHmimi)SQ16OE zu2nGaQdIyu9nni5z2TId;*V5o!#`d)r22CXGLPPZyrz6Ylj z0L_d6doQALW4#j`@kyW)@(nR*kPIZErw_*M9-z-;(gXBS4CttA#e7&!!n_mn2eJk8 zyOBQgB*Mte1!xO(Y70!h zJYWI<76zjR$wpYDFg&AeAwh2>X-GF*P_n`4dNcP)MT_&xet+^%HYu-al!t-Ei9InG z`7YWyBYwu+F&k(10;?G@x^?c?qOzttc3DcctoP({H4Y={&&5P^Sz5NI&uz=|IjS8uYYcj37(l8eOw0&6WYoaik$Ush>|a zEn+9e4vY~oATO297+b>=#>0d>3>TT5Bx63YEW#Ifhd%*JHgY0-EGt#5%SJPSQh+A} H-!Sk45ob#H diff --git a/F1-nolib/BMP180/BMP180.c b/F1-nolib/BMP180/BMP180.c index c614ab7..3914673 100644 --- a/F1-nolib/BMP180/BMP180.c +++ b/F1-nolib/BMP180/BMP180.c @@ -73,7 +73,7 @@ static struct { static BMP180_status bmpstatus = BMP180_NOTINIT; static uint8_t calidata_rdy = 0; -static uint32_t milliseconds_start = 0; // time of measurement start +//static uint32_t milliseconds_start = 0; // time of measurement start //static uint32_t p_delay = 8; // delay for P measurement static uint8_t uncomp_data[3]; // raw uncompensated data static int32_t Tval; // uncompensated T value @@ -173,7 +173,7 @@ void BMP180_read_ID(uint8_t *devid){ } // start measurement, @return 1 if all OK -int BMP180_start(uint32_t curr_milliseconds){ +int BMP180_start(/*uint32_t curr_milliseconds*/){ if(!calidata_rdy || bmpstatus == BMP180_BUSYT || bmpstatus == BMP180_BUSYP) return 0; uint8_t reg = BMP180_READ_T | BMP180_CTRLM_SCO; if(!write_reg8(BMP180_REG_CTRLMEAS, reg)){ @@ -181,7 +181,7 @@ int BMP180_start(uint32_t curr_milliseconds){ return 0; } bmpstatus = BMP180_BUSYT; - milliseconds_start = curr_milliseconds; + //milliseconds_start = curr_milliseconds; return 1; } @@ -226,7 +226,7 @@ static int still_measuring(){ return 0; } -void BMP180_process(uint32_t curr_milliseconds){ +void BMP180_process(/*uint32_t curr_milliseconds*/){ uint8_t reg; if(bmpstatus != BMP180_BUSYT && bmpstatus != BMP180_BUSYP) return; if(bmpstatus == BMP180_BUSYT){ // wait for temperature @@ -249,7 +249,7 @@ void BMP180_process(uint32_t curr_milliseconds){ bmpstatus = BMP180_ERR; return; } - milliseconds_start = curr_milliseconds; + //milliseconds_start = curr_milliseconds; bmpstatus = BMP180_BUSYP; }else{ // wait for pressure //if(curr_milliseconds - milliseconds_start < p_delay) return; diff --git a/F1-nolib/BMP180/BMP180.h b/F1-nolib/BMP180/BMP180.h index 08ac81c..63ea6c8 100644 --- a/F1-nolib/BMP180/BMP180.h +++ b/F1-nolib/BMP180/BMP180.h @@ -47,8 +47,8 @@ int BMP180_init(); void BMP180_read_ID(uint8_t *devid); void BMP180_setOS(BMP180_oversampling os); BMP180_status BMP180_get_status(); -int BMP180_start(uint32_t curr_milliseconds); -void BMP180_process(uint32_t curr_milliseconds); +int BMP180_start(/*uint32_t curr_milliseconds*/); +void BMP180_process(/*uint32_t curr_milliseconds*/); void BMP180_getdata(int32_t *T, uint32_t *P); #endif // BMP180_H__ diff --git a/F1-nolib/BMP180/Readme b/F1-nolib/BMP180/Readme index 74e8004..184acf0 100644 --- a/F1-nolib/BMP180/Readme +++ b/F1-nolib/BMP180/Readme @@ -1 +1 @@ -Work with I2C humidity/temperature sensors SI7005 & HTU21D +Work with I2C temperature/pressure sensor BMP180 \ No newline at end of file diff --git a/F1-nolib/BMP180/main.c b/F1-nolib/BMP180/main.c index e9cd46b..64c7506 100644 --- a/F1-nolib/BMP180/main.c +++ b/F1-nolib/BMP180/main.c @@ -74,7 +74,7 @@ int main(void){ lastT = Tms; } usb_proc(); - BMP180_process(Tms); + BMP180_process(/*Tms*/); BMP180_status s = BMP180_get_status(); if(s == BMP180_RDY){ // data ready - get it int32_t T; diff --git a/F1-nolib/BMP180/proto.c b/F1-nolib/BMP180/proto.c index c020e64..197c800 100644 --- a/F1-nolib/BMP180/proto.c +++ b/F1-nolib/BMP180/proto.c @@ -180,7 +180,7 @@ const char *parse_cmd(const char *buf){ else return "ID unknown\n"; break; case 'M': - if(!BMP180_start(Tms)) return "BMP180 not found or not inited\n"; + if(!BMP180_start(/*Tms*/)) return "BMP180 not found or not inited\n"; else return "Start\n"; break; case 'R': diff --git a/F1-nolib/BMP280/Readme b/F1-nolib/BMP280/Readme index d2b3d08..fc8f806 100644 --- a/F1-nolib/BMP280/Readme +++ b/F1-nolib/BMP280/Readme @@ -1 +1,2 @@ -BME/BMP280 humidity-pressure-temperature sensor +Work with I2C temperature/pressure sensor BMP280 and temp/pres/humidity sensor BME280 + diff --git a/F1-nolib/DHT22_DHT11/DHT22.bin b/F1-nolib/DHT22_DHT11/DHT22.bin new file mode 100755 index 0000000000000000000000000000000000000000..46fca13986c2ac77fb59786d1e2640afc9e00220 GIT binary patch literal 5272 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 F`cE`o;%)!{ literal 0 HcmV?d00001 diff --git a/F1-nolib/DHT22_DHT11/Makefile b/F1-nolib/DHT22_DHT11/Makefile new file mode 100644 index 0000000..585c56c --- /dev/null +++ b/F1-nolib/DHT22_DHT11/Makefile @@ -0,0 +1,139 @@ +BINARY = DHT22 +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 diff --git a/F1-nolib/DHT22_DHT11/Readme b/F1-nolib/DHT22_DHT11/Readme new file mode 100644 index 0000000..d460385 --- /dev/null +++ b/F1-nolib/DHT22_DHT11/Readme @@ -0,0 +1,2 @@ +Timer1 + DMA to work with DHT22/DHT11 sensors +#define DHT11 in dht.h to change data format to DHT11 diff --git a/F1-nolib/DHT22_DHT11/dht.c b/F1-nolib/DHT22_DHT11/dht.c new file mode 100644 index 0000000..d19efc5 --- /dev/null +++ b/F1-nolib/DHT22_DHT11/dht.c @@ -0,0 +1,210 @@ +/* + * This file is part of the DHT22 project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "dht.h" + +// max amount of bits (including starting) +#define NmeasurementMax (45) +// min length of start pulse +#define STARTLEN (150) +// start pulse max position +#define STARTMAXPOS (4) +// border between zero and one: border is 1 +#define ZEROBORDER (100) + +//#define EBUG + +#ifdef EBUG +#include "usb.h" +#include "proto.h" +#define DBG(x) do{USB_send(x);}while(0) +#else +#define DBG(x) +#endif + +static DHT_state dhtstate = DHT_SLEEP; +static uint32_t Tstart = 0; // time of start pulse +uint8_t DHT_tim_overflow = 0; +static uint8_t CC1array[NmeasurementMax]; +static uint16_t Humidity; +static int16_t Temperature; + +DHT_state DHT_getstate(){return dhtstate;} + +void DHT_getdata(uint16_t *Hum, int16_t *T){ + *Hum = Humidity; + *T = Temperature; + dhtstate = DHT_SLEEP; +} + +// TIM1_CH1 & TIM1_CH2 are used for total len & zero len pulse measurement +// T1ch1 - DMA1Ch2 +void DHT_pinsetup(){ + TIM1->CR1 = 0; + RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_TIM1EN | RCC_APB2ENR_AFIOEN; + RCC->AHBENR |= RCC_AHBENR_DMA1EN; + pin_set(GPIOA, 1<<8); + // PA8 as opendrain output + GPIOA->CRH = (GPIOA->CRH & ~(GPIO_CRH_MODE8 | GPIO_CRH_CNF8)) | + CRH(8, CNF_ODOUTPUT | MODE_FAST); + TIM1->PSC = 71; // 1MHz + TIM1->ARR = 200; // 200mks - max time for measurement + // dma->mem, 16bit->8bit, + DMA1_Channel2->CCR = DMA_CCR_MINC | DMA_CCR_PSIZE_0 | DMA_CCR_TCIE; + DMA1_Channel2->CPAR = (uint32_t)&TIM1->CCR1; + DMA1_Channel2->CMAR = (uint32_t)CC1array; + NVIC_EnableIRQ(TIM1_UP_IRQn); + NVIC_EnableIRQ(DMA1_Channel2_IRQn); + dhtstate = DHT_SLEEP; +} + +// start measurement +static void DHT_startmeas(uint32_t Tms){ + DBG("DHT_startmeas()\n"); + pin_set(GPIOA, 1<<8); + dhtstate = DHT_MEASURING; + Tstart = Tms; + // CC1 is input, IC1@TI1, IC2@TI1 + TIM1->CCMR1 = TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_1; + // CC1 active on falling edge, CC2 - on rising, enable capture on both channels + TIM1->CCER = TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC1P; + // TS=0b101 (trigger input TI1FP1, falling edge), SMS=0b100 (slave mode in reset mode) + TIM1->SMCR = TIM_SMCR_TS_2 | TIM_SMCR_TS_0 | TIM_SMCR_SMS_2; + // enable update interrupt & DMA events generation + TIM1->DIER = TIM_DIER_UIE | TIM_DIER_CC1DE; + // prepare DMA + DMA1->IFCR = DMA_IFCR_CGIF2; // clear all flags + DMA1_Channel2->CNDTR = NmeasurementMax; + // enable DMA channel + DMA1_Channel2->CCR |= DMA_CCR_EN; + // turn it on in, update generation only by overflow + TIM1->CR1 = TIM_CR1_CEN | TIM_CR1_URS; +} + +static uint8_t getnext8(int startidx){ + uint8_t *p = CC1array + startidx; + uint8_t x = 0; + for(int i = 0; i < 8; ++i){ + x <<= 1; + if(*p++ > ZEROBORDER) x |= 1; + } + return x; +} + +static void DHT_stopmeas(){ + DBG("DHT_stopmeas()\n"); + if(DHT_tim_overflow){ + DBG("Overflow="); DBG(u2str(DHT_tim_overflow)); DBG("\n"); + } + TIM1->CR1 = 0; + TIM1->SMCR = 0; + // disnable DMA channel + DMA1_Channel2->CCR &= ~DMA_CCR_EN; + DHT_tim_overflow = 0; + DBG("ctr="); DBG(u2str(DMA1_Channel2->CNDTR)); DBG("\nn\tCC1\n"); + int nmax = NmeasurementMax - DMA1_Channel2->CNDTR; + for(int i = 0; i < nmax; ++i){ + DBG(u2str(i)); DBG("\t"); DBG(u2str(CC1array[i])); DBG("\n"); + } + dhtstate = DHT_ERROR; + // find start pulse + int start = -1; + for(int i = 0; i < STARTMAXPOS; ++i){ + if(CC1array[i] > STARTLEN){ + start = i + 1; + break; + } + } + if(start == -1){ + DBG("No start pulse\n"); + return; + } + if(nmax - start < 40){ // no data & start pulse + DBG("Insufficient length\n"); + return; + } + uint8_t x[5]; + for(int i = 0; i < 5; ++i, start += 8){ + x[i] = getnext8(start); + DBG(u2str(getnext8(start))); DBG(" "); + } + uint8_t sum = x[0]; + for(int i = 1; i < 4; ++i) sum += x[i]; + if(sum != x[4]){ + DBG("Checksum failed\n"); + DBG("sum="); DBG(u2str(sum)); + DBG(", ch="); DBG(u2str(x[4])); DBG("\n"); + return; + } +#ifndef DHT11 + Humidity = (x[0] << 8) | x[1]; + Temperature = ((x[2]&0x7f) << 8) | x[3]; +#else + Humidity = x[0]; + Temperature = x[2]&0x7f; +#endif + if(x[2] & 0x80) Temperature = -Temperature; + dhtstate = DHT_GOTRESULT; +} + +// processing, Tms - current time in milliseconds +void DHT_process(uint32_t Tms){ + switch(dhtstate){ + case DHT_RESETTING: + if(Tms - Tstart > DHT_RESETPULSE_LEN) DHT_startmeas(Tms); + break; + case DHT_MEASURING: + if(Tms - Tstart > DHT_MEASUR_LEN || DHT_tim_overflow) + DHT_stopmeas(); + break; + default: + return; + } +} + +/** + * @brief DHT_start - start measurement process + */ +int DHT_start(uint32_t Tms){ + if(dhtstate != DHT_SLEEP && dhtstate != DHT_ERROR) return 0; + DBG("DHT_start()\n"); + DHT_tim_overflow = 0; + // setup pin into opendrain output mode +// GPIOA->CRH = (GPIOA->CRH & ~(GPIO_CRH_MODE8 | GPIO_CRH_CNF8)) | +// CRH(8, CNF_ODOUTPUT | MODE_NORMAL); + pin_clear(GPIOA, 1<<8); + Tstart = Tms; + dhtstate = DHT_RESETTING; + return 1; +} + +void dma1_channel2_isr(){ + DMA1->IFCR = DMA_IFCR_CGIF2; + TIM1->CR1 = 0; + DHT_tim_overflow = 1; +} + +// update IRQ: no data on input +void tim1_up_isr(){ + TIM1->SR = 0; + TIM1->CR1 = 0; + TIM1->DIER = 0; + TIM1->SMCR = 0; + DHT_tim_overflow = 2; +} + diff --git a/F1-nolib/DHT22_DHT11/dht.h b/F1-nolib/DHT22_DHT11/dht.h new file mode 100644 index 0000000..11d4b47 --- /dev/null +++ b/F1-nolib/DHT22_DHT11/dht.h @@ -0,0 +1,48 @@ +/* + * This file is part of the DHT22 project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#ifndef DHT_H__ +#define DHT_H__ + +#include + +// define it to work with DHT11 +//#define DHT11 + +// reset pulse length in ms +#define DHT_RESETPULSE_LEN (20) +// max measurement length +#define DHT_MEASUR_LEN (50) + +// state of device +typedef enum{ + DHT_SLEEP, + DHT_RESETTING, + DHT_MEASURING, + DHT_GOTRESULT, + DHT_ERROR +} DHT_state; + +void DHT_pinsetup(); +int DHT_start(uint32_t Tms); +DHT_state DHT_getstate(); +void DHT_process(uint32_t Tms); +void DHT_getdata(uint16_t *Hum, int16_t *T); + +#endif // DHT_H__ + diff --git a/F1-nolib/DHT22_DHT11/hardware.c b/F1-nolib/DHT22_DHT11/hardware.c new file mode 100644 index 0000000..58b9a77 --- /dev/null +++ b/F1-nolib/DHT22_DHT11/hardware.c @@ -0,0 +1,48 @@ +/* + * This file is part of the DHT22 project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "hardware.h" + +static inline void gpio_setup(){ + // 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; +} diff --git a/F1-nolib/DHT22_DHT11/hardware.h b/F1-nolib/DHT22_DHT11/hardware.h new file mode 100644 index 0000000..6c842eb --- /dev/null +++ b/F1-nolib/DHT22_DHT11/hardware.h @@ -0,0 +1,42 @@ +/* + * This file is part of the DHT22 project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#ifndef __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__ diff --git a/F1-nolib/DHT22_DHT11/main.c b/F1-nolib/DHT22_DHT11/main.c new file mode 100644 index 0000000..032870a --- /dev/null +++ b/F1-nolib/DHT22_DHT11/main.c @@ -0,0 +1,104 @@ +/* + * This file is part of the DHT22 project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "dht.h" +#include "hardware.h" +#include "proto.h" +#include "usb.h" +#include "usb_lib.h" + +volatile uint32_t Tms = 0; + +/* Called when systick fires */ +void sys_tick_handler(void){ + ++Tms; +} + +// usb getline +char *get_USB(){ + static char tmpbuf[512], *curptr = tmpbuf; + static int rest = 511; + uint8_t x = USB_receive(curptr); + curptr[x] = 0; + if(!x) return NULL; + if(curptr[x-1] == '\n'){ + curptr = tmpbuf; + rest = 511; + return tmpbuf; + } + curptr += x; rest -= x; + if(rest <= 0){ // buffer overflow + curptr = tmpbuf; + rest = 511; + } + return NULL; +} + +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(); + DHT_pinsetup(); + iwdg_setup(); + USBPU_ON(); + + uint16_t H; + int16_t T; + while (1){ + IWDG->KR = IWDG_REFRESH; // refresh watchdog + if(lastT > Tms || Tms - lastT > 499){ + LED_blink(LED0); + lastT = Tms; + } + DHT_process(Tms); + if(DHT_GOTRESULT == DHT_getstate()){ + DHT_getdata(&H, &T); + USB_send("Temperature="); USB_send(i2str(T)); +#ifdef DHT11 + USB_send(" (degC)\nHumidity="); +#else + USB_send(" (/10degC)\nHumidity="); +#endif + USB_send(u2str(H)); +#ifdef DHT11 + USB_send(" (%)\n"); +#else + USB_send(" (/10%)\n"); +#endif + } + usb_proc(); + 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; +} diff --git a/F1-nolib/DHT22_DHT11/proto.c b/F1-nolib/DHT22_DHT11/proto.c new file mode 100644 index 0000000..5e87f01 --- /dev/null +++ b/F1-nolib/DHT22_DHT11/proto.c @@ -0,0 +1,95 @@ +/* + * This file is part of the DHT22 project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "dht.h" +#include "proto.h" +#include "usb.h" + +extern volatile uint32_t Tms; + +static const char *helpmesg = + "'M' - start measurement\n" + "'S' - get DHT state\n" + "'R' - software reset\n" + "'W' - test watchdog\n" +; + +const char *parse_cmd(const char *buf){ + if(buf[1] != '\n') return buf; + switch(*buf){ + case 'M': + if(DHT_start(Tms)) return "Started\n"; + else return "Wait a little\n"; + break; + case 'R': + USB_send("Soft reset\n"); + NVIC_SystemReset(); + break; + case 'S': + switch(DHT_getstate()){ + case DHT_SLEEP: + return "Sleeping\n"; + case DHT_RESETTING: + return "Resetting\n"; + case DHT_MEASURING: + return "Measuring\n"; + case DHT_GOTRESULT: + return "Results are ready\n"; + default: + return "Not found\n"; + } + case 'W': + USB_send("Wait for reboot\n"); + while(1){nop();}; + break; + default: // help + return helpmesg; + break; + } + 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); +} diff --git a/F1-nolib/DHT22_DHT11/proto.h b/F1-nolib/DHT22_DHT11/proto.h new file mode 100644 index 0000000..4c2773c --- /dev/null +++ b/F1-nolib/DHT22_DHT11/proto.h @@ -0,0 +1,28 @@ +/* + * This file is part of the DHT22 project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#ifndef PROTO_H__ +#define PROTO_H__ + +#include + +const char *parse_cmd(const char *buf); +char *u2str(uint32_t val); +char *i2str(int32_t i); + +#endif // PROTO_H__ diff --git a/F1-nolib/DHT22_DHT11/usb.c b/F1-nolib/DHT22_DHT11/usb.c new file mode 100644 index 0000000..4df646e --- /dev/null +++ b/F1-nolib/DHT22_DHT11/usb.c @@ -0,0 +1,162 @@ +/* + * This file is part of the DHT22 project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "usb.h" +#include "usb_lib.h" + +static volatile uint8_t tx_succesfull = 1; +static volatile uint8_t rxNE = 0; + +// 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 uint8_t *buf, uint16_t l){ + uint32_t ctra = 1000000; + while(--ctra && tx_succesfull == 0){ + IWDG->KR = IWDG_REFRESH; + } + tx_succesfull = 0; + EP_Write(3, 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 uint8_t 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){ + if(!usbON) return; + uint16_t len = 0; + const char *b = buf; + while(*b++) ++len; + if(len > USB_TXBUFSZ-1 - buflen){ + usbwr((uint8_t*)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, uint16_t len){ + if(!usbON || !len) return; // USB disconnected + if(buflen){ + usbwr((uint8_t*)usbbuff, buflen); + buflen = 0; + } + int needzlp = 0; + while(len){ + if(len == USB_TXBUFSZ) needzlp = 1; + uint16_t s = (len > USB_TXBUFSZ) ? USB_TXBUFSZ : len; + if(usbwr((uint8_t*)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 + */ +uint8_t USB_receive(char *buf){ + if(!usbON || !rxNE) return 0; + uint8_t 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; +} diff --git a/F1-nolib/DHT22_DHT11/usb.h b/F1-nolib/DHT22_DHT11/usb.h new file mode 100644 index 0000000..9be2fb1 --- /dev/null +++ b/F1-nolib/DHT22_DHT11/usb.h @@ -0,0 +1,33 @@ +/* + * This file is part of the DHT22 project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#ifndef __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, uint16_t len); +uint8_t USB_receive(char *buf); + +#endif // __USB_H__ diff --git a/F1-nolib/DHT22_DHT11/usb_defs.h b/F1-nolib/DHT22_DHT11/usb_defs.h new file mode 100644 index 0000000..b8d1b2e --- /dev/null +++ b/F1-nolib/DHT22_DHT11/usb_defs.h @@ -0,0 +1,102 @@ +/* + * This file is part of the DHT22 project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#ifndef __USB_DEFS_H__ +#define __USB_DEFS_H__ + +#include + +// 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__ diff --git a/F1-nolib/DHT22_DHT11/usb_lib.c b/F1-nolib/DHT22_DHT11/usb_lib.c new file mode 100644 index 0000000..0d86fcb --- /dev/null +++ b/F1-nolib/DHT22_DHT11/usb_lib.c @@ -0,0 +1,476 @@ +/* + * This file is part of the DHT22 project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include "usb_lib.h" + +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; +} + diff --git a/F1-nolib/DHT22_DHT11/usb_lib.h b/F1-nolib/DHT22_DHT11/usb_lib.h new file mode 100644 index 0000000..b23efe0 --- /dev/null +++ b/F1-nolib/DHT22_DHT11/usb_lib.h @@ -0,0 +1,184 @@ +/* + * This file is part of the DHT22 project. + * Copyright 2021 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#ifndef __USB_LIB_H__ +#define __USB_LIB_H__ + +#include +#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__