From b415b94b55f89b8f11a266af3106cc9904f8daa0 Mon Sep 17 00:00:00 2001 From: eddyem Date: Sun, 22 Jan 2023 21:55:49 +0300 Subject: [PATCH] add I2C for F303, fix bug in USB (some variables wasn't volatile) --- F3:F303/I2C_scan/Makefile | 8 + F3:F303/I2C_scan/hardware.c | 31 ++ F3:F303/I2C_scan/hardware.h | 25 ++ F3:F303/I2C_scan/i2c.c | 313 ++++++++++++++ F3:F303/I2C_scan/i2c.h | 50 +++ F3:F303/I2C_scan/i2cscan.bin | Bin 0 -> 8124 bytes F3:F303/I2C_scan/i2cscan.cflags | 1 + F3:F303/I2C_scan/i2cscan.config | 10 + F3:F303/I2C_scan/i2cscan.creator | 1 + F3:F303/I2C_scan/i2cscan.creator.user | 174 ++++++++ F3:F303/I2C_scan/i2cscan.creator.user.7bd84e3 | 171 ++++++++ F3:F303/I2C_scan/i2cscan.cxxflags | 1 + F3:F303/I2C_scan/i2cscan.files | 17 + F3:F303/I2C_scan/i2cscan.includes | 6 + F3:F303/I2C_scan/main.c | 69 ++++ F3:F303/I2C_scan/proto.c | 173 ++++++++ F3:F303/I2C_scan/proto.h | 22 + F3:F303/I2C_scan/ringbuffer.c | 124 ++++++ F3:F303/I2C_scan/ringbuffer.h | 35 ++ F3:F303/I2C_scan/strfunc.c | 274 ++++++++++++ F3:F303/I2C_scan/strfunc.h | 31 ++ F3:F303/I2C_scan/usb.c | 176 ++++++++ F3:F303/I2C_scan/usb.h | 41 ++ F3:F303/I2C_scan/usb_lib.c | 389 ++++++++++++++++++ F3:F303/I2C_scan/usb_lib.h | 186 +++++++++ F3:F303/I2C_scan/usbhw.c | 137 ++++++ F3:F303/I2C_scan/usbhw.h | 107 +++++ F3:F303/I2C_scan/version.inc | 2 + F3:F303/PL2303/pl2303.bin | Bin 7432 -> 8412 bytes F3:F303/PL2303/usb.c | 27 +- F3:F303/PL2303/usb_lib.c | 13 +- F3:F303/PL2303/usb_lib.h | 2 +- F3:F303/PL2303/version.inc | 4 +- F3:F303/Readme.md | 8 +- Readme.md | 4 +- 35 files changed, 2605 insertions(+), 27 deletions(-) create mode 100644 F3:F303/I2C_scan/Makefile create mode 100644 F3:F303/I2C_scan/hardware.c create mode 100644 F3:F303/I2C_scan/hardware.h create mode 100644 F3:F303/I2C_scan/i2c.c create mode 100644 F3:F303/I2C_scan/i2c.h create mode 100755 F3:F303/I2C_scan/i2cscan.bin create mode 100644 F3:F303/I2C_scan/i2cscan.cflags create mode 100644 F3:F303/I2C_scan/i2cscan.config create mode 100644 F3:F303/I2C_scan/i2cscan.creator create mode 100644 F3:F303/I2C_scan/i2cscan.creator.user create mode 100644 F3:F303/I2C_scan/i2cscan.creator.user.7bd84e3 create mode 100644 F3:F303/I2C_scan/i2cscan.cxxflags create mode 100644 F3:F303/I2C_scan/i2cscan.files create mode 100644 F3:F303/I2C_scan/i2cscan.includes create mode 100644 F3:F303/I2C_scan/main.c create mode 100644 F3:F303/I2C_scan/proto.c create mode 100644 F3:F303/I2C_scan/proto.h create mode 100644 F3:F303/I2C_scan/ringbuffer.c create mode 100644 F3:F303/I2C_scan/ringbuffer.h create mode 100644 F3:F303/I2C_scan/strfunc.c create mode 100644 F3:F303/I2C_scan/strfunc.h create mode 100644 F3:F303/I2C_scan/usb.c create mode 100644 F3:F303/I2C_scan/usb.h create mode 100644 F3:F303/I2C_scan/usb_lib.c create mode 100644 F3:F303/I2C_scan/usb_lib.h create mode 100644 F3:F303/I2C_scan/usbhw.c create mode 100644 F3:F303/I2C_scan/usbhw.h create mode 100644 F3:F303/I2C_scan/version.inc diff --git a/F3:F303/I2C_scan/Makefile b/F3:F303/I2C_scan/Makefile new file mode 100644 index 0000000..cbee1bb --- /dev/null +++ b/F3:F303/I2C_scan/Makefile @@ -0,0 +1,8 @@ +BINARY := i2cscan +# MCU code +MCU := F303xb +# change this linking script depending on particular MCU model, +LDSCRIPT := stm32f303xB.ld + +include ../makefile.f3 +include ../../makefile.stm32 diff --git a/F3:F303/I2C_scan/hardware.c b/F3:F303/I2C_scan/hardware.c new file mode 100644 index 0000000..f07d821 --- /dev/null +++ b/F3:F303/I2C_scan/hardware.c @@ -0,0 +1,31 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "hardware.h" + +static inline void gpio_setup(){ + RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // for LEDs + for(int i = 0; i < 10000; ++i) nop(); + GPIOB->MODER = GPIO_MODER_MODER0_O | GPIO_MODER_MODER1_O; + GPIOB->ODR = 1; +} + +void hw_setup(){ + gpio_setup(); +} + diff --git a/F3:F303/I2C_scan/hardware.h b/F3:F303/I2C_scan/hardware.h new file mode 100644 index 0000000..0e9cc8b --- /dev/null +++ b/F3:F303/I2C_scan/hardware.h @@ -0,0 +1,25 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include + +extern volatile uint32_t Tms; + +void hw_setup(); diff --git a/F3:F303/I2C_scan/i2c.c b/F3:F303/I2C_scan/i2c.c new file mode 100644 index 0000000..27d8404 --- /dev/null +++ b/F3:F303/I2C_scan/i2c.c @@ -0,0 +1,313 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "i2c.h" +#include "strfunc.h" // hexdump +#include "usb.h" + +I2C_SPEED curI2Cspeed = LOW_SPEED; +extern volatile uint32_t Tms; +static uint32_t cntr; +static uint8_t i2c_got_DMA_Rx = 0; +volatile uint8_t I2C_scan_mode = 0; // == 1 when I2C is in scan mode +static uint8_t i2caddr = I2C_ADDREND; // current address in scan mode +static volatile int I2Cbusy = 0, goterr = 0; // busy==1 when DMA active, goterr==1 if 't was error @ last sent +static uint8_t I2Cbuf[256], i2cbuflen = 0; // buffer for DMA tx/rx and its len + +// macros for I2C rx/tx +#define DMARXCCR (DMA_CCR_MINC | DMA_CCR_TCIE | DMA_CCR_TEIE) +#define DMATXCCR (DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_TCIE | DMA_CCR_TEIE) +// macro for I2CCR1 +#define I2CCR1 (I2C_CR1_PE | I2C_CR1_RXDMAEN | I2C_CR1_TXDMAEN) + +// return 1 if I2Cbusy is set & timeout reached +static inline int isI2Cbusy(){ + cntr = Tms; + do{ + if(Tms - cntr > I2C_TIMEOUT){ USND("Timeout, DMA transfer in progress?\n"); return 1;} + }while(I2Cbusy); + return 0; +} + +// GPIO Resources: I2C1_SCL - PB6 (AF4), I2C1_SDA - PB7 (AF4) +void i2c_setup(I2C_SPEED speed){ + if(speed >= CURRENT_SPEED){ + speed = curI2Cspeed; + }else{ + curI2Cspeed = speed; + } + RCC->AHBENR |= RCC_AHBENR_GPIOBEN; + I2C1->CR1 = 0; + I2C1->ICR = 0x3f38; // clear all errors + GPIOB->AFR[0] = (GPIOB->AFR[0] & ~(GPIO_AFRL_AFRL6 | GPIO_AFRL_AFRL7)) | + AFRf(4, 6) | AFRf(4, 7); + GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER6 | GPIO_MODER_MODER7)) | + GPIO_MODER_MODER6_AF | GPIO_MODER_MODER7_AF; + GPIOB->PUPDR = (GPIOB->PUPDR & !(GPIO_PUPDR_PUPDR6 | GPIO_PUPDR_PUPDR7)) | + GPIO_PUPDR6_PU | GPIO_PUPDR7_PU; // pullup (what if there's no external pullup?) + GPIOB->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7; // both open-drain outputs + // I2C (default timing from PCLK - 64MHz) + RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; // clocking + if(speed == LOW_SPEED){ // 10kHz + // PRESC=F, SCLDEL=4, SDADEL=2, SCLH=0xC3, SCLL=0xC7 + I2C1->TIMINGR = (0xF<<28) | (4<<20) | (2<<16) | (0xC3<<8) | (0xC7); + }else if(speed == HIGH_SPEED){ // 100kHz + I2C1->TIMINGR = (0xF<<28) | (4<<20) | (2<<16) | (0xF<<8) | (0x13); + }else{ // VERYLOW_SPEED - the lowest speed by STM register: ~7.7kHz + I2C1->TIMINGR = (0xF<<28) | (4<<20) | (2<<16) | (0xff<<8) | (0xff); + } + I2C1->CR1 = I2CCR1; + RCC->AHBENR |= RCC_AHBENR_DMA1EN; + NVIC_EnableIRQ(DMA1_Channel6_IRQn); + NVIC_EnableIRQ(DMA1_Channel7_IRQn); + I2Cbusy = 0; +} + +// setup DMA for rx (tx==0) or tx (tx==1) +// DMA channels: 7 - I2C1_Rx, 6 - I2C1_Tx +static void i2cDMAsetup(int tx, uint8_t len){ + if(tx){ + DMA1_Channel6->CCR = DMATXCCR; + DMA1_Channel6->CPAR = (uint32_t) &I2C1->TXDR; + DMA1_Channel6->CMAR = (uint32_t) I2Cbuf; + DMA1_Channel6->CNDTR = i2cbuflen = len; + }else{ + DMA1_Channel7->CCR = DMARXCCR; + DMA1_Channel7->CPAR = (uint32_t) &I2C1->RXDR; + DMA1_Channel7->CMAR = (uint32_t) I2Cbuf; + DMA1_Channel7->CNDTR = i2cbuflen = len; + } +} + +static uint8_t i2c_start(uint8_t busychk){ + if(busychk){ + cntr = Tms; + while(I2C1->ISR & I2C_ISR_BUSY){ + IWDG->KR = IWDG_REFRESH; + if(Tms - cntr > I2C_TIMEOUT){ + USND("Line busy\n"); + return 0; // check busy + }} + } + cntr = Tms; + while(I2C1->CR2 & I2C_CR2_START){ + IWDG->KR = IWDG_REFRESH; + if(Tms - cntr > I2C_TIMEOUT){ + USND("No start\n"); + return 0; // check start + }} + return 1; +} + +// start writing +static uint8_t i2c_startw(uint8_t addr, uint8_t nbytes, uint8_t stop){ + if(!i2c_start(1)) return 0; + I2C1->CR2 = nbytes << 16 | addr; + if(stop) I2C1->CR2 |= I2C_CR2_AUTOEND; // autoend + // now start transfer + I2C1->CR2 |= I2C_CR2_START; + return 1; +} + +/** + * write command byte to I2C + * @param addr - device address (TSYS01_ADDR0 or TSYS01_ADDR1) + * @param data - bytes to write + * @param nbytes - amount of bytes to write + * @param stop - to set STOP + * @return 0 if error + */ +static uint8_t write_i2cs(uint8_t addr, uint8_t *data, uint8_t nbytes, uint8_t stop){ + if(!i2c_startw(addr, nbytes, stop)) return 0; + for(int i = 0; i < nbytes; ++i){ + cntr = Tms; + while(!(I2C1->ISR & I2C_ISR_TXIS)){ // ready to transmit + IWDG->KR = IWDG_REFRESH; + if(I2C1->ISR & I2C_ISR_NACKF){ + I2C1->ICR |= I2C_ICR_NACKCF; + //USND("NAK\n"); + return 0; + } + if(Tms - cntr > I2C_TIMEOUT){ + //USND("Timeout\n"); + return 0; + } + } + I2C1->TXDR = data[i]; // send data + } + cntr = Tms; + // wait for data gone + while(I2C1->ISR & I2C_ISR_BUSY){ + IWDG->KR = IWDG_REFRESH; + if(Tms - cntr > I2C_TIMEOUT){break;} + } + return 1; +} + +uint8_t write_i2c(uint8_t addr, uint8_t *data, uint8_t nbytes){ + if(isI2Cbusy()) return 0; + return write_i2cs(addr, data, nbytes, 1); +} + +uint8_t write_i2c_dma(uint8_t addr, uint8_t *data, uint8_t nbytes){ + if(!data || nbytes < 1) return 0; + if(isI2Cbusy()) return 0; + memcpy((char*)I2Cbuf, (char*)data, nbytes); + i2cDMAsetup(1, nbytes); + goterr = 0; + if(!i2c_startw(addr, nbytes, 1)) return 0; + I2Cbusy = 1; + DMA1_Channel6->CCR = DMATXCCR | DMA_CCR_EN; // start transfer + return 1; +} + +// start reading +static uint8_t i2c_startr(uint8_t addr, uint8_t nbytes, uint8_t busychk){ + if(!i2c_start(busychk)) return 0; + // read N bytes + I2C1->CR2 = (nbytes<<16) | addr | 1 | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN; + I2C1->CR2 |= I2C_CR2_START; + return 1; +} + +/** + * read nbytes of data from I2C line + * all functions with `addr` should have addr = address << 1 + * `data` should be an array with at least `nbytes` length + * @return 1 if all OK, 0 if NACK or no device found + */ +static uint8_t read_i2cb(uint8_t addr, uint8_t *data, uint8_t nbytes, uint8_t busychk){ + if(!i2c_startr(addr, nbytes, busychk)) return 0; + uint8_t i; + for(i = 0; i < nbytes; ++i){ + cntr = Tms; + while(!(I2C1->ISR & I2C_ISR_RXNE)){ // wait for data + IWDG->KR = IWDG_REFRESH; + if(I2C1->ISR & I2C_ISR_NACKF){ + I2C1->ICR |= I2C_ICR_NACKCF; + //USND("NAK\n"); + return 0; + } + if(Tms - cntr > I2C_TIMEOUT){ + //USND("Timeout\n"); + return 0; + } + } + *data++ = I2C1->RXDR; + } + return 1; + } + +uint8_t read_i2c(uint8_t addr, uint8_t *data, uint8_t nbytes){ + if(isI2Cbusy()) return 0; + return read_i2cb(addr, data, nbytes, 1); +} + +uint8_t read_i2c_dma(uint8_t addr, uint8_t nbytes){ + if(nbytes < 1) return 0; + if(isI2Cbusy()) return 0; + i2cDMAsetup(0, nbytes); + goterr = 0; + if(!i2c_startr(addr, nbytes, 1)) return 0; + I2Cbusy = 1; + DMA1_Channel7->CCR = DMARXCCR | DMA_CCR_EN; // start transfer + return 1; +} + + +// read register reg +uint8_t read_i2c_reg(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t nbytes){ + if(isI2Cbusy()) return 0; + if(!write_i2cs(addr, ®, 1, 0)) return 0; + return read_i2cb(addr, data, nbytes, 0); +} + +// read 16bit register reg +uint8_t read_i2c_reg16(uint8_t addr, uint16_t reg16, uint8_t *data, uint8_t nbytes){ + if(isI2Cbusy()) return 0; + if(!write_i2cs(addr, (uint8_t*)®16, 2, 0)) return 0; + return read_i2cb(addr, data, nbytes, 0); +} + +void i2c_init_scan_mode(){ + i2caddr = 1; // start from 1 as 0 is a broadcast address + I2C_scan_mode = 1; +} + +// return 1 if next addr is active & return in as `addr` +// if addresses are over, return 1 and set addr to I2C_NOADDR +// if scan mode inactive, return 0 and set addr to I2C_NOADDR +int i2c_scan_next_addr(uint8_t *addr){ + if(isI2Cbusy()) return 0; + *addr = i2caddr; + if(i2caddr == I2C_ADDREND){ + *addr = I2C_ADDREND; + I2C_scan_mode = 0; + return 0; + } + /*while(!u3txrdy); + USND("Addr: "); USND(uhex2str(i2caddr)); USND("\n"); + usart3_sendbuf();*/ + uint8_t byte; + if(!read_i2c((i2caddr++)<<1, &byte, 1)) return 0; + return 1; +} + +// dump I2Cbuf +void i2c_bufdudump(){ + if(goterr){ + USND("Last transfer ends with error!\n"); + goterr = 0; + } + USND("I2C buffer:\n"); + hexdump(USB_sendstr, I2Cbuf, i2cbuflen); +} + +void i2c_have_DMA_Rx(){ + if(!i2c_got_DMA_Rx) return; + i2c_got_DMA_Rx = 0; + i2c_bufdudump(); +} + +int i2cdma_haderr(){ + int r = goterr; + goterr = 0; + return r; +} + +// Rx (7) /Tx (6) interrupts +static void I2C_isr(int rx){ + uint32_t isr = DMA1->ISR; + DMA_Channel_TypeDef *ch = (rx) ? DMA1_Channel7 : DMA1_Channel6; + if(isr & (DMA_ISR_TEIF6 | DMA_ISR_TEIF6)) goterr = 1; + if(rx) i2c_got_DMA_Rx = 1; // last transfer was Rx + ch->CCR = 0; + I2Cbusy = 0; + DMA1->IFCR = 0x0ff00000; // clear all flags for channel6/7 +} + +void dma1_channel6_isr(){ + I2C_isr(0); +} + +void dma1_channel7_isr(){ + I2C_isr(1); +} diff --git a/F3:F303/I2C_scan/i2c.h b/F3:F303/I2C_scan/i2c.h new file mode 100644 index 0000000..fdb9358 --- /dev/null +++ b/F3:F303/I2C_scan/i2c.h @@ -0,0 +1,50 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include + +#define I2C_ADDREND (0x80) + +typedef enum{ + VERYLOW_SPEED, + LOW_SPEED, + HIGH_SPEED, + CURRENT_SPEED +} I2C_SPEED; + +extern I2C_SPEED curI2Cspeed; +extern volatile uint8_t I2C_scan_mode; + +// timeout of I2C bus in ms +#define I2C_TIMEOUT (100) + +void i2c_setup(I2C_SPEED speed); +uint8_t read_i2c(uint8_t addr, uint8_t *data, uint8_t nbytes); +uint8_t read_i2c_reg(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t nbytes); +uint8_t read_i2c_reg16(uint8_t addr, uint16_t reg16, uint8_t *data, uint8_t nbytes); +uint8_t write_i2c(uint8_t addr, uint8_t *data, uint8_t nbytes); +uint8_t write_i2c_dma(uint8_t addr, uint8_t *data, uint8_t nbytes); +uint8_t read_i2c_dma(uint8_t addr, uint8_t nbytes); + +void i2c_bufdudump(); +void i2c_have_DMA_Rx(); +int i2cdma_haderr(); + +void i2c_init_scan_mode(); +int i2c_scan_next_addr(uint8_t *addr); diff --git a/F3:F303/I2C_scan/i2cscan.bin b/F3:F303/I2C_scan/i2cscan.bin new file mode 100755 index 0000000000000000000000000000000000000000..886c4115d743f38a523dbc480f94ea28d6d8b428 GIT binary patch literal 8124 zcmd5=dw5e-w%_NGwn42R}@M3m(Sxm&E_%`Js$|*8e%k z@ctjj`Dg3=f0+Mu(m%FWqB+T!Eo76G1B5!+LYtAQHr#Fbc#X-j%Hmv#1`r~3I)oU7 zIa|nxqBOZVXMj*;SrSYrw>C_qVCo(*>EyejM|p>3`S*B8(;0!#o{`3|j3jfTu-BJP zC!S3-W$0w4rI)(bC+q5Ao`!x;R97DjqSZ*Zdv`3hdmt8b_RIK_!mGkT?)bnuAv+so zpPXILw})FN93D{e3H=oo4(#EGk9Tm2 zKOPa*3RLA0%MoEOM}BfdIFNP3;yj@dxH)Xr%*xr7z@s5VqO=EiFs=3nv@GvDfd(+9 z!M~8aUU-pvV<0E{=IrM)SIt3FNXq*TWcL6Tiq8R54PN3WR45@uk(ta-Biakn4|4K9 z6$#0i#6kkEY9>XZb{C9?B!nSLNJ1C_gdsqP2H5_dqq)_@9%3$e7^%rzrAU-;Z&I2( ze_J1J6WTj{sOXaanWEZ}^f_cWjodXbBeHVFj0ih}h!82b$ct!^7|uMmvWyl}n3M=X z5ZX_Cv?kFHymJSGrW{t?+;FIYoS$ew(4vmAC)m062F%M|8N06a5ZsU;#g&*-a z`$?W0P8W$KyS$8M;vB146R6)vh0mi^1wZj0im>#sFR?&%4yl*tB!e#3ZA8T`|Kb`Y zkLJ?DfV~!IWW{~Y15#ONJN0%%o<@iCRL)R8M-Kfpu%P&>a~+E0<~s`kg2CZY0Z)X0adh%p8`@I~1|fv(Cwi z|2niXuRr3s>rjZM{HLjqY!;S;G@nU$<;9006$!y1Ht(z8t$8Cs(1qeO&w*CA=k$3C zc%)JcBd{|^`aYSj!JJ70T$muUB@{$)_}q3*VR;GGM{u4j{GlMOol75o{P9d@zp6&N znesYMvO7=oi~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_scan/i2cscan.cflags b/F3:F303/I2C_scan/i2cscan.cflags new file mode 100644 index 0000000..68d5165 --- /dev/null +++ b/F3:F303/I2C_scan/i2cscan.cflags @@ -0,0 +1 @@ +-std=c17 \ No newline at end of file diff --git a/F3:F303/I2C_scan/i2cscan.config b/F3:F303/I2C_scan/i2cscan.config new file mode 100644 index 0000000..52b0751 --- /dev/null +++ b/F3:F303/I2C_scan/i2cscan.config @@ -0,0 +1,10 @@ +// Add predefined macros for your project here. For example: +// #define THE_ANSWER 42 +#define EBUG +#define STM32F3 +#define STM32F303xb +#define __thumb2__ 1 +#define __ARM_ARCH_7M__ + +#define BUILD_NUMBER "xx" +#define BUILD_DATE "yy" diff --git a/F3:F303/I2C_scan/i2cscan.creator b/F3:F303/I2C_scan/i2cscan.creator new file mode 100644 index 0000000..e94cbbd --- /dev/null +++ b/F3:F303/I2C_scan/i2cscan.creator @@ -0,0 +1 @@ +[General] diff --git a/F3:F303/I2C_scan/i2cscan.creator.user b/F3:F303/I2C_scan/i2cscan.creator.user new file mode 100644 index 0000000..19feb85 --- /dev/null +++ b/F3:F303/I2C_scan/i2cscan.creator.user @@ -0,0 +1,174 @@ + + + + + + EnvironmentId + {cf63021e-ef53-49b0-b03b-2f2570cdf3b6} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + KOI8-R + false + 4 + false + 80 + true + true + 1 + false + true + false + 1 + true + true + 0 + 8 + true + false + 2 + true + false + true + *.md, *.MD, Makefile + false + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + + 0 + true + + true + Builtin.BuildSystem + + true + true + Builtin.DefaultTidyAndClazy + 1 + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + Desktop + {91347f2c-5221-46a7-80b1-0a054ca02f79} + 0 + 0 + 0 + + /home/eddy/C-files/stm32samples/F3:F303/I2C_scan + + + + all + + true + GenericProjectManager.GenericMakeStep + + 1 + Сборка + Сборка + ProjectExplorer.BuildSteps.Build + + + + + clean + + true + GenericProjectManager.GenericMakeStep + + 1 + Очистка + Очистка + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + По умолчанию + GenericProjectManager.GenericBuildConfiguration + + 1 + + + 0 + Развёртывание + Развёртывание + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + 2 + + ProjectExplorer.CustomExecutableRunConfiguration + + false + true + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/F3:F303/I2C_scan/i2cscan.creator.user.7bd84e3 b/F3:F303/I2C_scan/i2cscan.creator.user.7bd84e3 new file mode 100644 index 0000000..aa79e75 --- /dev/null +++ b/F3:F303/I2C_scan/i2cscan.creator.user.7bd84e3 @@ -0,0 +1,171 @@ + + + + + + EnvironmentId + {7bd84e39-ca37-46d3-be9d-99ebea85bc0d} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + KOI8-R + false + 4 + false + 80 + true + true + 1 + false + true + false + 0 + true + true + 0 + 8 + true + false + 1 + true + false + true + *.md, *.MD, Makefile + false + true + + + + ProjectExplorer.Project.PluginSettings + + + true + false + true + true + true + true + + + 0 + true + + true + true + Builtin.DefaultTidyAndClazy + 2 + + + + true + + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + Desktop + {65a14f9e-e008-4c1b-89df-4eaa4774b6e3} + 0 + 0 + 0 + + /Big/Data/00__Electronics/STM32/F303-nolib/blink + + + + all + + true + GenericProjectManager.GenericMakeStep + + 1 + Сборка + Сборка + ProjectExplorer.BuildSteps.Build + + + + + clean + + true + GenericProjectManager.GenericMakeStep + + 1 + Очистка + Очистка + ProjectExplorer.BuildSteps.Clean + + 2 + false + + false + + Default + GenericProjectManager.GenericBuildConfiguration + + 1 + + + 0 + Развёртывание + Развёртывание + ProjectExplorer.BuildSteps.Deploy + + 1 + + false + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + 2 + + ProjectExplorer.CustomExecutableRunConfiguration + + false + true + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/F3:F303/I2C_scan/i2cscan.cxxflags b/F3:F303/I2C_scan/i2cscan.cxxflags new file mode 100644 index 0000000..6435dfc --- /dev/null +++ b/F3:F303/I2C_scan/i2cscan.cxxflags @@ -0,0 +1 @@ +-std=c++17 \ No newline at end of file diff --git a/F3:F303/I2C_scan/i2cscan.files b/F3:F303/I2C_scan/i2cscan.files new file mode 100644 index 0000000..faf2295 --- /dev/null +++ b/F3:F303/I2C_scan/i2cscan.files @@ -0,0 +1,17 @@ +hardware.c +hardware.h +i2c.c +i2c.h +main.c +proto.c +proto.h +ringbuffer.c +ringbuffer.h +strfunc.c +strfunc.h +usb.c +usb.h +usb_lib.c +usb_lib.h +usbhw.c +usbhw.h diff --git a/F3:F303/I2C_scan/i2cscan.includes b/F3:F303/I2C_scan/i2cscan.includes new file mode 100644 index 0000000..06d1130 --- /dev/null +++ b/F3:F303/I2C_scan/i2cscan.includes @@ -0,0 +1,6 @@ +. +../inc +../inc/Fx +../inc/cm +../inc/ld +../inc/startup diff --git a/F3:F303/I2C_scan/main.c b/F3:F303/I2C_scan/main.c new file mode 100644 index 0000000..0b93dad --- /dev/null +++ b/F3:F303/I2C_scan/main.c @@ -0,0 +1,69 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "i2c.h" +#include "hardware.h" +#include "proto.h" +#include "strfunc.h" +#include "usb.h" + +#define MAXSTRLEN RBINSZ + +volatile uint32_t Tms = 0; + +void sys_tick_handler(void){ + ++Tms; +} + +int main(void){ + char inbuff[MAXSTRLEN+1]; + if(StartHSE()){ + SysTick_Config((uint32_t)72000); // 1ms + }else{ + StartHSI(); + SysTick_Config((uint32_t)48000); // 1ms + } + hw_setup(); + USB_setup(); + uint32_t ctr = Tms; + while(1){ + if(Tms - ctr > 499){ + ctr = Tms; + pin_toggle(GPIOB, 1 << 1 | 1 << 0); // toggle LED @ PB0 + //USND("HALO\n"); + } + USB_proc(); + int l = USB_receivestr(inbuff, MAXSTRLEN); + if(l < 0) USB_sendstr("ERROR: USB buffer overflow or string was too long\n"); + else if(l){ + const char *ans = parse_cmd(inbuff); + if(ans) USB_sendstr(ans); + } + if(I2C_scan_mode){ + uint8_t addr; + int ok = i2c_scan_next_addr(&addr); + if(addr == I2C_ADDREND) USND("Scan ends\n"); + else if(ok){ + USND(uhex2str(addr)); + USND(" ("); USND(u2str(addr)); + USND(") - found device\n"); + } + } + i2c_have_DMA_Rx(); // check if there's DMA Rx complete + } +} diff --git a/F3:F303/I2C_scan/proto.c b/F3:F303/I2C_scan/proto.c new file mode 100644 index 0000000..c2e4452 --- /dev/null +++ b/F3:F303/I2C_scan/proto.c @@ -0,0 +1,173 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include "i2c.h" +#include "strfunc.h" +#include "usb.h" +#include "version.inc" + +#define LOCBUFFSZ (32) +// local buffer for I2C data to send +static uint8_t locBuffer[LOCBUFFSZ]; +extern volatile uint32_t Tms; + +const char *helpstring = + "https://github.com/eddyem/stm32samples/tree/master/F3:F303/I2C_scan build#" BUILD_NUMBER " @ " BUILD_DATE "\n" + "i0..2 - setup I2C with lowest..highest speed (7.7, 10 and 100kHz)\n" + "Ia addr - set I2C address\n" + "Ig - dump content of I2Cbuf\n" + "Iw bytes - send bytes (hex/dec/oct/bin) to I2C\n" + "IW bytes - the same over DMA\n" + "Ir reg n - read n bytes from I2C reg\n" + "I2 reg16 n - read n bytes from 16-bit register\n" + "In n - just read n bytes\n" + "IN n - the same but with DMA\n" + "Is - scan I2C bus\n" + "T - print current Tms\n" +; + +static uint8_t i2cinited = 0; +TRUE_INLINE const char *setupI2C(const char *buf){ + buf = omit_spaces(buf); + if(*buf < '0' || *buf > '2') return "Wrong speed"; + i2c_setup(*buf - '0'); + i2cinited = 1; + return "OK"; +} + +static uint8_t I2Caddress = 0; +TRUE_INLINE const char *saI2C(const char *buf){ + uint32_t addr; + if(!getnum(buf, &addr) || addr > 0x7f) return "Wrong address"; + I2Caddress = (uint8_t) addr << 1; + USND("I2Caddr="); USND(uhex2str(addr)); newline(); + return "OK"; +} +static void rdI2C(const char *buf, int is16){ + uint32_t N = 0; + int noreg = 0; // write register (==1 - just read, ==2 - -//- using DMA) + const char *nxt = NULL; + if(*buf == 'n'){ + ++buf; + noreg = 1; + }else if(*buf == 'N'){ + ++buf; + noreg = 2; + }else{ + nxt = getnum(buf, &N); + if(!nxt || buf == nxt || N > 0xffff || (!is16 && N > 0xff)){ + USND("Bad register number\n"); + return; + } + buf = nxt; + } + uint16_t reg = N; + nxt = getnum(buf, &N); + if(!nxt || buf == nxt || N > LOCBUFFSZ){ + USND("Bad length (<=32)\n"); + return; + } + const char *erd = "Error reading I2C\n"; + if(noreg){ // don't write register + if(noreg == 1){ + USND("Simple read:\n"); + if(!read_i2c(I2Caddress, locBuffer, N)){ + USND(erd); + return; + } + }else{ + USND("Try to read using DMA .. "); + if(!read_i2c_dma(I2Caddress, N)) USND(erd); + else USND("OK\n"); + return; + } + }else{ + if(is16){ + if(!read_i2c_reg16(I2Caddress, reg, locBuffer, N)){ + USND(erd); + return; + } + }else{ + if(!read_i2c_reg(I2Caddress, reg, locBuffer, N)){ + USND(erd); + return; + } + } + } + if(N == 0){ USND("OK"); return; } + if(!noreg){USND("Register "); USND(uhex2str(reg)); USND(":\n");} + hexdump(USB_sendstr, locBuffer, N); +} +// read N numbers from buf, @return 0 if wrong or none +TRUE_INLINE uint16_t readNnumbers(const char *buf){ + uint32_t D; + const char *nxt; + uint16_t N = 0; + while((nxt = getnum(buf, &D)) && nxt != buf && N < LOCBUFFSZ){ + buf = nxt; + locBuffer[N++] = (uint8_t) D&0xff; + USND("add byte: "); USND(uhex2str(D&0xff)); USND("\n"); + } + USND("Send "); USND(u2str(N)); USND(" bytes\n"); + return N; +} +static const char *wrI2C(const char *buf, int isdma){ + uint16_t N = readNnumbers(buf); + int result = isdma ? write_i2c_dma(I2Caddress, locBuffer, N) : + write_i2c(I2Caddress, locBuffer, N); + if(!result) return "Error writing I2C"; + return "OK"; +} + +const char *parse_cmd(const char *buf){ + // "long" commands + switch(*buf){ + case 'i': + return setupI2C(buf + 1); + break; + case 'I': + if(!i2cinited) return "Init I2C first"; + buf = omit_spaces(buf + 1); + if(*buf == 'a') return saI2C(buf + 1); + else if(*buf == 'r'){ rdI2C(buf + 1, 0); return NULL; } + else if(*buf == '2'){ rdI2C(buf + 1, 1); return NULL; } + else if(*buf == 'n'){ rdI2C(buf, 0); return NULL; } + else if(*buf == 'N'){ rdI2C(buf, 0); return NULL; } + else if(*buf == 'w') return wrI2C(buf + 1, 0); + else if(*buf == 'W') return wrI2C(buf + 1, 1); + else if(*buf == 's'){ i2c_init_scan_mode(); return "Start scan\n"; } + else if(*buf == 'g'){ i2c_bufdudump(); return NULL; } + else return "Command should be 'Ia', 'Iw', 'Ir' or 'Is'\n"; + break; + } + // "short" commands + if(buf[1]) return buf; // echo wrong data + switch(*buf){ + case 'T': + USND("T="); + USND(u2str(Tms)); + newline(); + break; + default: // help + USND(helpstring); + break; + } + return NULL; +} diff --git a/F3:F303/I2C_scan/proto.h b/F3:F303/I2C_scan/proto.h new file mode 100644 index 0000000..5c21204 --- /dev/null +++ b/F3:F303/I2C_scan/proto.h @@ -0,0 +1,22 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +char *parse_cmd(char *buf); + diff --git a/F3:F303/I2C_scan/ringbuffer.c b/F3:F303/I2C_scan/ringbuffer.c new file mode 100644 index 0000000..9d93936 --- /dev/null +++ b/F3:F303/I2C_scan/ringbuffer.c @@ -0,0 +1,124 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "ringbuffer.h" + +// stored data length +int RB_datalen(ringbuffer *b){ + if(b->tail >= b->head) return (b->tail - b->head); + else return (b->length - b->head + b->tail); +} + +/** + * @brief RB_hasbyte - check if buffer has given byte stored + * @param b - buffer + * @param byte - byte to find + * @return index if found, -1 if none + */ +int RB_hasbyte(ringbuffer *b, uint8_t byte){ + if(b->head == b->tail) return -1; // no data in buffer + int startidx = b->head; + if(b->head > b->tail){ // + for(int found = b->head; found < b->length; ++found) + if(b->data[found] == byte) return found; + startidx = 0; + } + for(int found = startidx; found < b->tail; ++found) + if(b->data[found] == byte) return found; + return -1; +} + +// poor memcpy +static void mcpy(uint8_t *targ, const uint8_t *src, int l){ + while(l--) *targ++ = *src++; +} + +// increment head or tail +TRUE_INLINE void incr(ringbuffer *b, volatile int *what, int n){ + *what += n; + if(*what >= b->length) *what -= b->length; +} + +/** + * @brief RB_read - read data from ringbuffer + * @param b - buffer + * @param s - array to write data + * @param len - max len of `s` + * @return bytes read + */ +int RB_read(ringbuffer *b, uint8_t *s, int len){ + int l = RB_datalen(b); + if(!l) return 0; + if(l > len) l = len; + int _1st = b->length - b->head; + if(_1st > l) _1st = l; + if(_1st > len) _1st = len; + mcpy(s, b->data + b->head, _1st); + if(_1st < len && l > _1st){ + mcpy(s+_1st, b->data, l - _1st); + incr(b, &b->head, l); + return l; + } + incr(b, &b->head, _1st); + return _1st; +} + +/** + * @brief RB_readto fill array `s` with data until byte `byte` (with it) + * @param b - ringbuffer + * @param byte - check byte + * @param s - buffer to write data + * @param len - length of `s` + * @return amount of bytes written (negative, if lenhead; + // now calculate length of new data portion + if(idx < b->head) partlen += b->length; + if(partlen > len) return -RB_read(b, s, len); + return RB_read(b, s, partlen); +} + +/** + * @brief RB_write - write some data to ringbuffer + * @param b - buffer + * @param str - data + * @param l - length + * @return amount of bytes written + */ +int RB_write(ringbuffer *b, const uint8_t *str, int l){ + int r = b->length - 1 - RB_datalen(b); // rest length + if(l > r) l = r; + if(!l) return 0; + int _1st = b->length - b->tail; + if(_1st > l) _1st = l; + mcpy(b->data + b->tail, str, _1st); + if(_1st < l){ // add another piece from start + mcpy(b->data, str+_1st, l-_1st); + } + incr(b, &b->tail, l); + return l; +} + +// just delete all information in buffer `b` +void RB_clearbuf(ringbuffer *b){ + b->head = 0; + b->tail = 0; +} diff --git a/F3:F303/I2C_scan/ringbuffer.h b/F3:F303/I2C_scan/ringbuffer.h new file mode 100644 index 0000000..7290c28 --- /dev/null +++ b/F3:F303/I2C_scan/ringbuffer.h @@ -0,0 +1,35 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include + +typedef struct{ + uint8_t *data; // data buffer + const int length; // its length + int head; // head index + int tail; // tail index +} ringbuffer; + +int RB_read(ringbuffer *b, uint8_t *s, int len); +int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len); +int RB_hasbyte(ringbuffer *b, uint8_t byte); +int RB_write(ringbuffer *b, const uint8_t *str, int l); +int RB_datalen(ringbuffer *b); +void RB_clearbuf(ringbuffer *b); diff --git a/F3:F303/I2C_scan/strfunc.c b/F3:F303/I2C_scan/strfunc.c new file mode 100644 index 0000000..8ea4906 --- /dev/null +++ b/F3:F303/I2C_scan/strfunc.c @@ -0,0 +1,274 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +/** + * @brief hexdump - dump hex array by 16 bytes in string + * @param sendfun - function to send data + * @param arr - array to dump + * @param len - length of `arr` + */ +void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len){ + char buf[52], *bptr = buf; + for(uint16_t l = 0; l < len; ++l, ++arr){ + for(int16_t j = 1; j > -1; --j){ + register uint8_t half = (*arr >> (4*j)) & 0x0f; + if(half < 10) *bptr++ = half + '0'; + else *bptr++ = half - 10 + 'a'; + } + if(l % 16 == 15){ + *bptr++ = '\n'; + *bptr = 0; + sendfun(buf); + bptr = buf; + }else *bptr++ = ' '; + } + if(bptr != buf){ + *bptr++ = '\n'; + *bptr = 0; + sendfun(buf); + } +} + +/** + * @brief _2str - convert value into string buffer + * @param val - |value| + * @param minus - ==0 if value > 0 + * @return buffer with number + */ +static char *_2str(uint32_t val, uint8_t minus){ + static char strbuf[12]; + char *bufptr = &strbuf[11]; + *bufptr = 0; + if(!val){ + *(--bufptr) = '0'; + }else{ + while(val){ + uint32_t x = val / 10; + *(--bufptr) = (val - 10*x) + '0'; + val = x; + //*(--bufptr) = val % 10 + '0'; + //val /= 10; + } + } + if(minus) *(--bufptr) = '-'; + return bufptr; +} + +// return string with number `val` +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); +} + +/** + * @brief uhex2str - print 32bit unsigned int as hex + * @param val - value + * @return string with number + */ +char *uhex2str(uint32_t val){ + static char buf[12] = "0x"; + int npos = 2; + uint8_t *ptr = (uint8_t*)&val + 3; + int8_t i, j, z=1; + for(i = 0; i < 4; ++i, --ptr){ + if(*ptr == 0){ // omit leading zeros + if(i == 3) z = 0; + if(z) continue; + } + else z = 0; + for(j = 1; j > -1; --j){ + uint8_t half = (*ptr >> (4*j)) & 0x0f; + if(half < 10) buf[npos++] = half + '0'; + else buf[npos++] = half - 10 + 'a'; + } + } + buf[npos] = 0; + return buf; +} + +/** + * @brief omit_spaces - eliminate leading spaces and other trash in string + * @param buf - string + * @return - pointer to first character in `buf` > ' ' + */ +const char *omit_spaces(const char *buf){ + while(*buf){ + if(*buf > ' ') break; + ++buf; + } + return buf; +} + +/** + * @brief getdec - read decimal number & return pointer to next non-number symbol + * @param buf - string + * @param N - number read + * @return Next non-number symbol. In case of overflow return `buf` and N==0xffffffff + */ +static const char *getdec(const char *buf, uint32_t *N){ + char *start = (char*)buf; + uint32_t num = 0; + while(*buf){ + char c = *buf; + if(c < '0' || c > '9'){ + break; + } + if(num > 429496729 || (num == 429496729 && c > '5')){ // overflow + *N = 0xffffff; + return start; + } + num *= 10; + num += c - '0'; + ++buf; + } + *N = num; + return buf; +} +// read hexadecimal number (without 0x prefix!) +static const char *gethex(const char *buf, uint32_t *N){ + const char *start = buf; + uint32_t num = 0; + while(*buf){ + char c = *buf; + uint8_t M = 0; + if(c >= '0' && c <= '9'){ + M = '0'; + }else if(c >= 'A' && c <= 'F'){ + M = 'A' - 10; + }else if(c >= 'a' && c <= 'f'){ + M = 'a' - 10; + } + if(M){ + if(num & 0xf0000000){ // overflow + *N = 0xffffff; + return start; + } + num <<= 4; + num += c - M; + }else{ + break; + } + ++buf; + } + *N = num; + return buf; +} +// read octal number (without 0 prefix!) +static const char *getoct(const char *buf, uint32_t *N){ + const char *start = (char*)buf; + uint32_t num = 0; + while(*buf){ + char c = *buf; + if(c < '0' || c > '7'){ + break; + } + if(num & 0xe0000000){ // overflow + *N = 0xffffff; + return start; + } + num <<= 3; + num += c - '0'; + ++buf; + } + *N = num; + return buf; +} +// read binary number (without b prefix!) +static const char *getbin(const char *buf, uint32_t *N){ + const char *start = (char*)buf; + uint32_t num = 0; + while(*buf){ + char c = *buf; + if(c < '0' || c > '1'){ + break; + } + if(num & 0x80000000){ // overflow + *N = 0xffffff; + return start; + } + num <<= 1; + if(c == '1') num |= 1; + ++buf; + } + *N = num; + return buf; +} + +/** + * @brief getnum - read uint32_t from string (dec, hex or bin: 127, 0x7f, 0b1111111) + * @param buf - buffer with number and so on + * @param N - the number read + * @return pointer to first non-number symbol in buf + * (if it is == buf, there's no number or if *N==0xffffffff there was overflow) + */ +const char *getnum(const char *txt, uint32_t *N){ + const char *nxt = NULL; + const char *s = omit_spaces(txt); + if(*s == '0'){ // hex, oct or 0 + if(s[1] == 'x' || s[1] == 'X'){ // hex + nxt = gethex(s+2, N); + if(nxt == s+2) nxt = (char*)txt; + }else if(s[1] > '0'-1 && s[1] < '8'){ // oct + nxt = getoct(s+1, N); + if(nxt == s+1) nxt = (char*)txt; + }else{ // 0 + nxt = s+1; + *N = 0; + } + }else if(*s == 'b' || *s == 'B'){ + nxt = getbin(s+1, N); + if(nxt == s+1) nxt = (char*)txt; + }else{ + nxt = getdec(s, N); + if(nxt == s) nxt = (char*)txt; + } + return nxt; +} + +// get signed integer +const char *getint(const char *txt, int32_t *I){ + const char *s = omit_spaces(txt); + int32_t sign = 1; + uint32_t U; + if(*s == '-'){ + sign = -1; + ++s; + } + const char *nxt = getnum(s, &U); + if(nxt == s) return txt; + if(U & 0x80000000) return txt; // overfull + *I = sign * (int32_t)U; + return nxt; +} + +/* +void mymemcpy(char *dest, const char *src, int len){ + if(len < 1) return; + while(len--) *dest++ = *src++; +} +*/ diff --git a/F3:F303/I2C_scan/strfunc.h b/F3:F303/I2C_scan/strfunc.h new file mode 100644 index 0000000..1b89d0a --- /dev/null +++ b/F3:F303/I2C_scan/strfunc.h @@ -0,0 +1,31 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include + +void hexdump(int (*sendfun)(const char *s), uint8_t *arr, uint16_t len); +const char *u2str(uint32_t val); +const char *i2str(int32_t i); +const char *uhex2str(uint32_t val); +const char *getnum(const char *txt, uint32_t *N); +const char *omit_spaces(const char *buf); +const char *getint(const char *txt, int32_t *I); +//void mymemcpy(char *dest, const char *src, int len); diff --git a/F3:F303/I2C_scan/usb.c b/F3:F303/I2C_scan/usb.c new file mode 100644 index 0000000..a4402fa --- /dev/null +++ b/F3:F303/I2C_scan/usb.c @@ -0,0 +1,176 @@ +/* + * This file is part of the pl2303 project. + * Copyright 2022 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 "hardware.h" +#include "ringbuffer.h" +#include "usb.h" +#include "usb_lib.h" + +static volatile uint8_t usbbuff[USB_TXBUFSZ]; // temporary buffer for sending data +// ring buffers for incoming and outgoing data +static uint8_t obuf[RBOUTSZ], ibuf[RBINSZ]; +static volatile ringbuffer out = {.data = obuf, .length = RBOUTSZ, .head = 0, .tail = 0}; +static volatile ringbuffer in = {.data = ibuf, .length = RBINSZ, .head = 0, .tail = 0}; +// transmission is succesfull +static volatile uint8_t bufisempty = 1; +static volatile uint8_t bufovrfl = 0; + +static void send_next(){ + if(bufisempty) return; + static int lastdsz = 0; + int buflen = RB_read((ringbuffer*)&out, (uint8_t*)usbbuff, USB_TXBUFSZ); + if(!buflen){ + if(lastdsz == 64) EP_Write(3, NULL, 0); // send ZLP after 64 bits packet when nothing more to send + lastdsz = 0; + bufisempty = 1; + return; + } + EP_Write(3, (uint8_t*)usbbuff, buflen); + lastdsz = buflen; +} + +// blocking send full content of ring buffer +int USB_sendall(){ + while(!bufisempty){ + if(!usbON) return 0; + } + return 1; +} + +// put `buf` into queue to send +int USB_send(const uint8_t *buf, int len){ + if(!buf || !usbON || !len) return 0; + while(len){ + int a = RB_write((ringbuffer*)&out, buf, len); + len -= a; + buf += a; + if(bufisempty){ + bufisempty = 0; + send_next(); + } + } + return 1; +} + +int USB_putbyte(uint8_t byte){ + if(!usbON) return 0; + while(0 == RB_write((ringbuffer*)&out, &byte, 1)){ + if(bufisempty){ + bufisempty = 0; + send_next(); + } + } + return 1; +} + +int USB_sendstr(const char *string){ + if(!string || !usbON) return 0; + int len = 0; + const char *b = string; + while(*b++) ++len; + if(!len) return 0; + return USB_send((const uint8_t*)string, len); +} + +/** + * @brief USB_receive - get binary data from receiving ring-buffer + * @param buf (i) - buffer for received data + * @param len - length of `buf` + * @return amount of received bytes (negative, if overfull happened) + */ +int USB_receive(uint8_t *buf, int len){ + int sz = RB_read((ringbuffer*)&in, buf, len); + if(bufovrfl){ + RB_clearbuf((ringbuffer*)&in); + if(!sz) sz = -1; + else sz = -sz; + bufovrfl = 0; + } + return sz; +} + +/** + * @brief USB_receivestr - get string up to '\n' and replace '\n' with 0 + * @param buf - receiving buffer + * @param len - its length + * @return strlen or negative value indicating overflow (if so, string won't be ends with 0 and buffer should be cleared) + */ +int USB_receivestr(char *buf, int len){ + int l = RB_readto((ringbuffer*)&in, '\n', (uint8_t*)buf, len); + if(l == 0) return 0; + if(l < 1 || bufovrfl) RB_clearbuf((ringbuffer*)&in); + else buf[l-1] = 0; // replace '\n' with strend + if(bufovrfl){ + if(l > 0) l = -l; + else l = -1; + bufovrfl = 0; + } + 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 + 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 + send_next(); +} + +static void receive_Handler(){ // EP2OUT + uint8_t buf[USB_RXBUFSZ]; + uint16_t epstatus = KEEP_DTOG(USB->EPnR[2]); + uint8_t sz = EP_Read(2, (uint16_t*)buf); + if(sz){ + if(RB_write((ringbuffer*)&in, buf, sz) != sz) bufovrfl = 1; + } + // keep stat_tx & set ACK rx, clear RX ctr + USB->EPnR[2] = (epstatus & ~USB_EPnR_CTR_RX) ^ USB_EPnR_STAT_RX; +} + +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; + } +} diff --git a/F3:F303/I2C_scan/usb.h b/F3:F303/I2C_scan/usb.h new file mode 100644 index 0000000..ba41064 --- /dev/null +++ b/F3:F303/I2C_scan/usb.h @@ -0,0 +1,41 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#ifndef __USB_H__ +#define __USB_H__ + +#include "usbhw.h" + +// sizes of ringbuffers for outgoing and incoming data +#define RBOUTSZ (256) +#define RBINSZ (128) + +#define USND(x) USB_sendstr(x) + +void USB_proc(); +int USB_sendall(); +int USB_send(const uint8_t *buf, int len); +int USB_putbyte(uint8_t byte); +int USB_sendstr(const char *string); +int USB_receive(uint8_t *buf, int len); +int USB_receivestr(char *buf, int len); + +#define newline() USB_putbyte('\n') + +#endif // __USB_H__ diff --git a/F3:F303/I2C_scan/usb_lib.c b/F3:F303/I2C_scan/usb_lib.c new file mode 100644 index 0000000..1df4915 --- /dev/null +++ b/F3:F303/I2C_scan/usb_lib.c @@ -0,0 +1,389 @@ +/* + * This file is part of the pl2303 project. + * Copyright 2022 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}; +config_pack_t setup_packet; +uint8_t ep0databuf[EP0DATABUF_SIZE]; +uint8_t ep0dbuflen = 0; + +usb_LineCoding getLineCoding(){return lineCoding;} + +volatile 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){ + uint16_t c; + if(packet->bmRequestType & 0x80){ // read + switch(packet->wValue){ + case 0x8484: + c = 2; + break; + case 0x0080: + c = 1; + break; + case 0x8686: + c = 0xaa; + break; + default: + c = 0; + } + EP_WriteIRQ(0, (uint8_t*)&c, 1); + }else{ // write ZLP + c = 0; + EP_WriteIRQ(0, (uint8_t *)&c, 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 uint16_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, (uint8_t*)&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 + */ +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; +} + +/** + * 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/F3:F303/I2C_scan/usb_lib.h b/F3:F303/I2C_scan/usb_lib.h new file mode 100644 index 0000000..05c41a9 --- /dev/null +++ b/F3:F303/I2C_scan/usb_lib.h @@ -0,0 +1,186 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#ifndef __USB_LIB_H__ +#define __USB_LIB_H__ + +#include +#include "usbhw.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 volatile uint8_t usbON; +extern config_pack_t setup_packet; +extern uint8_t ep0databuf[]; +extern uint8_t ep0dbuflen; + +void EP0_Handler(); + +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__ diff --git a/F3:F303/I2C_scan/usbhw.c b/F3:F303/I2C_scan/usbhw.c new file mode 100644 index 0000000..573ec1e --- /dev/null +++ b/F3:F303/I2C_scan/usbhw.c @@ -0,0 +1,137 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "usb.h" +#include "usb_lib.h" + +void USB_setup(){ + NVIC_DisableIRQ(USB_LP_IRQn); + // remap USB LP & Wakeup interrupts to 75 and 76 - works only on pure F303 + RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // enable tacting of SYSCFG + SYSCFG->CFGR1 |= SYSCFG_CFGR1_USB_IT_RMP; + // setup pullup + RCC->AHBENR |= RCC_AHBENR_GPIOAEN; + USBPU_OFF(); + //GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER15_Msk | GPIO_MODER_MODER11_Msk | GPIO_MODER_MODER12_Msk)) | + GPIOA->MODER = (GPIOA->MODER & (MODER_CLR(11) & MODER_CLR(12) & MODER_CLR(15))) | + (MODER_AF(11) | MODER_AF(12) | MODER_O(15)); + //(GPIO_MODER_MODER11_AF | GPIO_MODER_MODER12_AF | GPIO_MODER_MODER15_O); + // USB - alternate function 14 @ pins PA11/PA12 + GPIOA->AFR[1] = (GPIOA->AFR[1] & ~(GPIO_AFRH_AFRH3_Msk | GPIO_AFRH_AFRH4_Msk)) | + //AFR1(14, 11) | AFR1(14, 12); + AFRf(14, 11) | AFRf(14, 12); + 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_IRQn); + USBPU_ON(); +} + +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_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; + // 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; + if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0_BUFSZ, USB_EP0_BUFSZ, EP0_Handler)){ + return; + } + USB->ISTR = ~USB_ISTR_RESET; + } + 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; + } +} + diff --git a/F3:F303/I2C_scan/usbhw.h b/F3:F303/I2C_scan/usbhw.h new file mode 100644 index 0000000..7e7d404 --- /dev/null +++ b/F3:F303/I2C_scan/usbhw.h @@ -0,0 +1,107 @@ +/* + * This file is part of the i2cscan project. + * Copyright 2023 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#ifndef USBHW_H__ +#define USBHW_H__ + +#include + +#define USBPU_port GPIOA +#define USBPU_pin (1<<15) +#define USBPU_ON() pin_clear(USBPU_port, USBPU_pin) +#define USBPU_OFF() pin_set(USBPU_port, USBPU_pin) + +// 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 ((USB_TypeDef *) USB_BASE) + +#ifdef USB_BTABLE +#undef USB_BTABLE +#endif +#define USB_BTABLE ((USB_BtableDef *)(USB_BTABLE_BASE)) +#define USB_ISTR_EPID 0x0000000F +#define USB_FNR_LSOF_0 0x00000800 +#define USB_FNR_lSOF_1 0x00001000 +#define USB_LPMCSR_BESL_0 0x00000010 +#define USB_LPMCSR_BESL_1 0x00000020 +#define USB_LPMCSR_BESL_2 0x00000040 +#define USB_LPMCSR_BESL_3 0x00000080 +#define USB_EPnR_CTR_RX 0x00008000 +#define USB_EPnR_DTOG_RX 0x00004000 +#define USB_EPnR_STAT_RX 0x00003000 +#define USB_EPnR_STAT_RX_0 0x00001000 +#define USB_EPnR_STAT_RX_1 0x00002000 +#define USB_EPnR_SETUP 0x00000800 +#define USB_EPnR_EP_TYPE 0x00000600 +#define USB_EPnR_EP_TYPE_0 0x00000200 +#define USB_EPnR_EP_TYPE_1 0x00000400 +#define USB_EPnR_EP_KIND 0x00000100 +#define USB_EPnR_CTR_TX 0x00000080 +#define USB_EPnR_DTOG_TX 0x00000040 +#define USB_EPnR_STAT_TX 0x00000030 +#define USB_EPnR_STAT_TX_0 0x00000010 +#define USB_EPnR_STAT_TX_1 0x00000020 +#define USB_EPnR_EA 0x0000000F +#define USB_COUNTn_RX_BLSIZE 0x00008000 +#define USB_COUNTn_NUM_BLOCK 0x00007C00 +#define USB_COUNTn_RX 0x0000003F + +#define USB_TypeDef USB_TypeDef_custom + +typedef struct { + __IO uint32_t EPnR[STM32ENDPOINTS]; + __IO uint32_t RESERVED[STM32ENDPOINTS]; + __IO uint32_t CNTR; + __IO uint32_t ISTR; + __IO uint32_t FNR; + __IO uint32_t DADDR; + __IO uint32_t BTABLE; +} 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; + +void USB_setup(); +int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)()); + +#endif // USBHW_H__ diff --git a/F3:F303/I2C_scan/version.inc b/F3:F303/I2C_scan/version.inc new file mode 100644 index 0000000..cde0d53 --- /dev/null +++ b/F3:F303/I2C_scan/version.inc @@ -0,0 +1,2 @@ +#define BUILD_NUMBER "17" +#define BUILD_DATE "2023-01-22" diff --git a/F3:F303/PL2303/pl2303.bin b/F3:F303/PL2303/pl2303.bin index 4cb469a490fc195c7e6c0f65e91d78de19d567d3..ed169a437015fa135b16cddbdd991840b626765b 100755 GIT binary patch literal 8412 zcmd^EdstIfwqNI*BqSk95L@L@JRvGz1VYeCrPYwYF(e2II^tt$0z?ywk`kO^=RqRH z>1{jJmg#(2+WKs#QynExJGRw=R_D&`>kA_NbZoEm8zrr2r-=kPJ|K7PfM`22_x^eB z_g(#V&f5E|wf1j4_TKAkU=OhAlRBt=)Zj<|2oGswm^}ZD`bU``>-gX3pJe`T z;(9>zuW`}FI2)A{XLc-t&RhUiY+7Y&X`dvBIu~V((+Z7?t~Wj&tu4p*0_-=1wB;cm ztO4!z?cwnD&Tv?MRKUr3D$nVm0w-@r85LYCPkr2DPZZtmjZUQUnsod4w64%a`3dNR zTuLiHy0vXu5w(MwPQM9Cl+4h*%czvzH64Vea#pv)tn1$0vr4Z{N~Qxb^s}84gDWOZ z45}wmK}trNWR{zUz9|k@}lJQ&wcu9YDU4+c~J&XMmy*>`HAN^ zwL->Ii55;B;qUj+XeXu;>o_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)a zt77N}F;o{rt7E8M5`+K$R7a?CSp`Hp{Cz}j9x?o<3qo2ETvQZ*i9fQUrT=zLwd3(B GD*kWydZR1= diff --git a/F3:F303/PL2303/usb.c b/F3:F303/PL2303/usb.c index 68e7f71..a4402fa 100644 --- a/F3:F303/PL2303/usb.c +++ b/F3:F303/PL2303/usb.c @@ -23,11 +23,11 @@ #include "usb.h" #include "usb_lib.h" -static uint8_t usbbuff[USB_TXBUFSZ]; // temporary buffer for sending data +static volatile uint8_t usbbuff[USB_TXBUFSZ]; // temporary buffer for sending data // ring buffers for incoming and outgoing data static uint8_t obuf[RBOUTSZ], ibuf[RBINSZ]; -static ringbuffer out = {.data = obuf, .length = RBOUTSZ, .head = 0, .tail = 0}; -static ringbuffer in = {.data = ibuf, .length = RBINSZ, .head = 0, .tail = 0}; +static volatile ringbuffer out = {.data = obuf, .length = RBOUTSZ, .head = 0, .tail = 0}; +static volatile ringbuffer in = {.data = ibuf, .length = RBINSZ, .head = 0, .tail = 0}; // transmission is succesfull static volatile uint8_t bufisempty = 1; static volatile uint8_t bufovrfl = 0; @@ -35,14 +35,14 @@ static volatile uint8_t bufovrfl = 0; static void send_next(){ if(bufisempty) return; static int lastdsz = 0; - int buflen = RB_read(&out, usbbuff, USB_TXBUFSZ); + int buflen = RB_read((ringbuffer*)&out, (uint8_t*)usbbuff, USB_TXBUFSZ); if(!buflen){ if(lastdsz == 64) EP_Write(3, NULL, 0); // send ZLP after 64 bits packet when nothing more to send lastdsz = 0; bufisempty = 1; return; } - EP_Write(3, usbbuff, buflen); + EP_Write(3, (uint8_t*)usbbuff, buflen); lastdsz = buflen; } @@ -58,7 +58,7 @@ int USB_sendall(){ int USB_send(const uint8_t *buf, int len){ if(!buf || !usbON || !len) return 0; while(len){ - int a = RB_write(&out, buf, len); + int a = RB_write((ringbuffer*)&out, buf, len); len -= a; buf += a; if(bufisempty){ @@ -71,7 +71,7 @@ int USB_send(const uint8_t *buf, int len){ int USB_putbyte(uint8_t byte){ if(!usbON) return 0; - while(0 == RB_write(&out, &byte, 1)){ + while(0 == RB_write((ringbuffer*)&out, &byte, 1)){ if(bufisempty){ bufisempty = 0; send_next(); @@ -96,9 +96,9 @@ int USB_sendstr(const char *string){ * @return amount of received bytes (negative, if overfull happened) */ int USB_receive(uint8_t *buf, int len){ - int sz = RB_read(&in, buf, len); + int sz = RB_read((ringbuffer*)&in, buf, len); if(bufovrfl){ - RB_clearbuf(&in); + RB_clearbuf((ringbuffer*)&in); if(!sz) sz = -1; else sz = -sz; bufovrfl = 0; @@ -113,9 +113,10 @@ int USB_receive(uint8_t *buf, int len){ * @return strlen or negative value indicating overflow (if so, string won't be ends with 0 and buffer should be cleared) */ int USB_receivestr(char *buf, int len){ - int l = RB_readto(&in, '\n', (uint8_t*)buf, len); - if(l < 0 || bufovrfl) RB_clearbuf(&in); - else buf[l] = 0; // replace '\n' with strend + int l = RB_readto((ringbuffer*)&in, '\n', (uint8_t*)buf, len); + if(l == 0) return 0; + if(l < 1 || bufovrfl) RB_clearbuf((ringbuffer*)&in); + else buf[l-1] = 0; // replace '\n' with strend if(bufovrfl){ if(l > 0) l = -l; else l = -1; @@ -147,7 +148,7 @@ static void receive_Handler(){ // EP2OUT uint16_t epstatus = KEEP_DTOG(USB->EPnR[2]); uint8_t sz = EP_Read(2, (uint16_t*)buf); if(sz){ - if(RB_write(&in, buf, sz) != sz) bufovrfl = 1; + if(RB_write((ringbuffer*)&in, buf, sz) != sz) bufovrfl = 1; } // keep stat_tx & set ACK rx, clear RX ctr USB->EPnR[2] = (epstatus & ~USB_EPnR_CTR_RX) ^ USB_EPnR_STAT_RX; diff --git a/F3:F303/PL2303/usb_lib.c b/F3:F303/PL2303/usb_lib.c index ad49ea4..1df4915 100644 --- a/F3:F303/PL2303/usb_lib.c +++ b/F3:F303/PL2303/usb_lib.c @@ -29,7 +29,7 @@ uint8_t ep0dbuflen = 0; usb_LineCoding getLineCoding(){return lineCoding;} -uint8_t usbON = 0; // device disconnected from terminal +volatile uint8_t usbON = 0; // device disconnected from terminal // definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor #define bcdUSB_L 0x10 @@ -149,8 +149,8 @@ void WEAK break_handler(){ // handler of vendor requests void WEAK vendor_handler(config_pack_t *packet){ + uint16_t c; if(packet->bmRequestType & 0x80){ // read - uint8_t c; switch(packet->wValue){ case 0x8484: c = 2; @@ -164,9 +164,10 @@ void WEAK vendor_handler(config_pack_t *packet){ default: c = 0; } - EP_WriteIRQ(0, &c, 1); + EP_WriteIRQ(0, (uint8_t*)&c, 1); }else{ // write ZLP - EP_WriteIRQ(0, (uint8_t *)0, 0); + c = 0; + EP_WriteIRQ(0, (uint8_t *)&c, 0); } } @@ -226,7 +227,7 @@ static inline void get_descriptor(){ } } -static uint8_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured) +static uint16_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){ @@ -237,7 +238,7 @@ static inline void std_d2h_req(){ EP_WriteIRQ(0, (uint8_t *)&status, 2); // send status: Bus Powered break; case GET_CONFIGURATION: - EP_WriteIRQ(0, &configuration, 1); + EP_WriteIRQ(0, (uint8_t*)&configuration, 1); break; default: break; diff --git a/F3:F303/PL2303/usb_lib.h b/F3:F303/PL2303/usb_lib.h index 4b45040..b6777b0 100644 --- a/F3:F303/PL2303/usb_lib.h +++ b/F3:F303/PL2303/usb_lib.h @@ -166,7 +166,7 @@ typedef struct { extern ep_t endpoints[]; extern usb_dev_t USB_Dev; -extern uint8_t usbON; +extern volatile uint8_t usbON; extern config_pack_t setup_packet; extern uint8_t ep0databuf[]; extern uint8_t ep0dbuflen; diff --git a/F3:F303/PL2303/version.inc b/F3:F303/PL2303/version.inc index 44a91c7..b48b0fa 100644 --- a/F3:F303/PL2303/version.inc +++ b/F3:F303/PL2303/version.inc @@ -1,2 +1,2 @@ -#define BUILD_NUMBER "14" -#define BUILD_DATE "2023-01-18" +#define BUILD_NUMBER "35" +#define BUILD_DATE "2023-01-22" diff --git a/F3:F303/Readme.md b/F3:F303/Readme.md index 4c6e71f..23740e0 100644 --- a/F3:F303/Readme.md +++ b/F3:F303/Readme.md @@ -4,8 +4,10 @@ Samples for STM32F303 Most of these samples works with [my non-solder devboard](https://github.com/eddyem/stm32samples/tree/master/F0_F1_F3-LQFP48_testboard) - **blink** - simple LEDs blink on PB0 and PB1 -- **usart1** - read data from USART1 and send it back (blocking sending) -- **usarts** - work with all three USARTs with Tx DMA, send to USART1 data read from other, you can send "2\n" or "3\n" to USART1 for tests of USART2/USART3 - **floatPrintf** - work with floats, convert floating point number into a string -- **usart1fullDMA** - USART1 Rx and Tx DMA access (Rx terminated by character match of '\n' or buffer overflow). +- **Multistepper** - stepper motor controller (8 independend motors or up to 64 multiplexed) +- **NitrogenFlooding** - automatic nitrogen flooding - **PL2303** - emulates PL2303 +- **usart1** - read data from USART1 and send it back (blocking sending) +- **usart1fullDMA** - USART1 Rx and Tx DMA access (Rx terminated by character match of '\n' or buffer overflow). +- **usarts** - work with all three USARTs with Tx DMA, send to USART1 data read from other, you can send "2\n" or "3\n" to USART1 for tests of USART2/USART3 diff --git a/Readme.md b/Readme.md index 8a668af..8afd45b 100644 --- a/Readme.md +++ b/Readme.md @@ -3,8 +3,8 @@ Some working code examples for STM32 Built without any third-party library. -Supported F0, F1, F3 and F4 series. +Supported F0, F1, F3, F4 and G0 series. CMSIS-headers: -https://github.com/modm-io/cmsis-header-stm32 \ No newline at end of file +https://github.com/modm-io/cmsis-header-stm32