mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-02-28 03:44:30 +03:00
hide deprecated code; make USB snippet common for F0/F1/F3
This commit is contained in:
@@ -1 +1,9 @@
|
||||
Actual snipper for all MCUs (just fix usbhw.c and usbhw.h)
|
||||
Actual snipper for all MCUs (just fix usbhw.c and usbhw.h)
|
||||
|
||||
If USB DP/DM pins need to be setting up, do it before calling USB_setup().
|
||||
For F1/F3 your should manulally turn off DP pullup before calling USB_setup() and turn it on after this.
|
||||
F0 do it itself (excluding need of external pullup, e.g for halvanic isolator).
|
||||
|
||||
defines:
|
||||
NOCAN - no common work with CAN bus (for F0/F3) - buffer will be 256 bytes larger
|
||||
USB1_16 (32-bit addressing) / USB2_16 (16-bit addressing) - addressing method of USB registers (manually define only for F3, where < D have 1_16, >= D have 2_16)
|
||||
|
||||
167
snippets/usb_pl2303/ringbuffer.c
Normal file
167
snippets/usb_pl2303/ringbuffer.c
Normal file
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "ringbuffer.h"
|
||||
|
||||
static int datalen(ringbuffer *b){
|
||||
if(b->tail >= b->head) return (b->tail - b->head);
|
||||
else return (b->length - b->head + b->tail);
|
||||
}
|
||||
|
||||
// stored data length
|
||||
int RB_datalen(ringbuffer *b){
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
int l = datalen(b);
|
||||
b->busy = 0;
|
||||
return l;
|
||||
}
|
||||
|
||||
static int hasbyte(ringbuffer *b, uint8_t byte){
|
||||
if(b->head == b->tail) return -1; // no data in buffer
|
||||
int startidx = b->head;
|
||||
if(b->head > b->tail){ //
|
||||
for(int found = b->head; found < b->length; ++found)
|
||||
if(b->data[found] == byte) return found;
|
||||
startidx = 0;
|
||||
}
|
||||
for(int found = startidx; found < b->tail; ++found)
|
||||
if(b->data[found] == byte) return found;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief RB_hasbyte - check if buffer has given byte stored
|
||||
* @param b - buffer
|
||||
* @param byte - byte to find
|
||||
* @return index if found, -1 if none or busy
|
||||
*/
|
||||
int RB_hasbyte(ringbuffer *b, uint8_t byte){
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
int ret = hasbyte(b, byte);
|
||||
b->busy = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// poor memcpy
|
||||
static void mcpy(uint8_t *targ, const uint8_t *src, int l){
|
||||
while(l--) *targ++ = *src++;
|
||||
}
|
||||
|
||||
// increment head or tail
|
||||
TRUE_INLINE void incr(ringbuffer *b, volatile int *what, int n){
|
||||
*what += n;
|
||||
if(*what >= b->length) *what -= b->length;
|
||||
}
|
||||
|
||||
static int read(ringbuffer *b, uint8_t *s, int len){
|
||||
int l = datalen(b);
|
||||
if(!l) return 0;
|
||||
if(l > len) l = len;
|
||||
int _1st = b->length - b->head;
|
||||
if(_1st > l) _1st = l;
|
||||
if(_1st > len) _1st = len;
|
||||
mcpy(s, b->data + b->head, _1st);
|
||||
if(_1st < len && l > _1st){
|
||||
mcpy(s+_1st, b->data, l - _1st);
|
||||
incr(b, &b->head, l);
|
||||
return l;
|
||||
}
|
||||
incr(b, &b->head, _1st);
|
||||
return _1st;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief RB_read - read data from ringbuffer
|
||||
* @param b - buffer
|
||||
* @param s - array to write data
|
||||
* @param len - max len of `s`
|
||||
* @return bytes read or -1 if busy
|
||||
*/
|
||||
int RB_read(ringbuffer *b, uint8_t *s, int len){
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
int r = read(b, s, len);
|
||||
b->busy = 0;
|
||||
return r;
|
||||
}
|
||||
|
||||
static int readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len){
|
||||
int idx = hasbyte(b, byte);
|
||||
if(idx < 0) return 0;
|
||||
int partlen = idx + 1 - b->head;
|
||||
// now calculate length of new data portion
|
||||
if(idx < b->head) partlen += b->length;
|
||||
if(partlen > len) return -read(b, s, len);
|
||||
return read(b, s, partlen);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief RB_readto fill array `s` with data until byte `byte` (with it)
|
||||
* @param b - ringbuffer
|
||||
* @param byte - check byte
|
||||
* @param s - buffer to write data
|
||||
* @param len - length of `s`
|
||||
* @return amount of bytes written (negative, if len<data in buffer or buffer is busy)
|
||||
*/
|
||||
int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len){
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
int n = readto(b, byte, s, len);
|
||||
b->busy = 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
static int write(ringbuffer *b, const uint8_t *str, int l){
|
||||
int r = b->length - 1 - datalen(b); // rest length
|
||||
if(l > r) l = 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief RB_write - write some data to ringbuffer
|
||||
* @param b - buffer
|
||||
* @param str - data
|
||||
* @param l - length
|
||||
* @return amount of bytes written or -1 if busy
|
||||
*/
|
||||
int RB_write(ringbuffer *b, const uint8_t *str, int l){
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
int w = write(b, str, l);
|
||||
b->busy = 0;
|
||||
return w;
|
||||
}
|
||||
|
||||
// just delete all information in buffer `b`
|
||||
int RB_clearbuf(ringbuffer *b){
|
||||
if(b->busy) return -1;
|
||||
b->busy = 1;
|
||||
b->head = 0;
|
||||
b->tail = 0;
|
||||
b->busy = 0;
|
||||
return 1;
|
||||
}
|
||||
41
snippets/usb_pl2303/ringbuffer.h
Normal file
41
snippets/usb_pl2303/ringbuffer.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined STM32F0
|
||||
#include <stm32f0.h>
|
||||
#elif defined STM32F1
|
||||
#include <stm32f1.h>
|
||||
#elif defined STM32F3
|
||||
#include <stm32f3.h>
|
||||
#endif
|
||||
|
||||
typedef struct{
|
||||
uint8_t *data; // data buffer
|
||||
const int length; // its length
|
||||
int head; // head index
|
||||
int tail; // tail index
|
||||
volatile int busy; // == TRUE if buffer is busy now
|
||||
} ringbuffer;
|
||||
|
||||
int RB_read(ringbuffer *b, uint8_t *s, int len);
|
||||
int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len);
|
||||
int RB_hasbyte(ringbuffer *b, uint8_t byte);
|
||||
int RB_write(ringbuffer *b, const uint8_t *str, int l);
|
||||
int RB_datalen(ringbuffer *b);
|
||||
int RB_clearbuf(ringbuffer *b);
|
||||
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -27,18 +26,22 @@ static volatile uint8_t usbbuff[USB_TXBUFSZ]; // temporary buffer for sending da
|
||||
static uint8_t obuf[RBOUTSZ], ibuf[RBINSZ];
|
||||
volatile ringbuffer rbout = {.data = obuf, .length = RBOUTSZ, .head = 0, .tail = 0};
|
||||
volatile ringbuffer rbin = {.data = ibuf, .length = RBINSZ, .head = 0, .tail = 0};
|
||||
// transmission is succesfull
|
||||
volatile uint8_t bufisempty = 1;
|
||||
// inbuf overflow when receiving
|
||||
volatile uint8_t bufovrfl = 0;
|
||||
// last send data size
|
||||
static volatile int lastdsz = 0;
|
||||
|
||||
// called from transmit EP
|
||||
void send_next(){
|
||||
if(bufisempty) return;
|
||||
static int lastdsz = 0;
|
||||
int buflen = RB_read((ringbuffer*)&rbout, (uint8_t*)usbbuff, USB_TXBUFSZ);
|
||||
if(!buflen){
|
||||
if(buflen == 0){
|
||||
if(lastdsz == 64) EP_Write(3, NULL, 0); // send ZLP after 64 bits packet when nothing more to send
|
||||
lastdsz = 0;
|
||||
bufisempty = 1;
|
||||
return;
|
||||
}else if(buflen < 0){
|
||||
lastdsz = 0;
|
||||
// Uncomment next line if you want 4Mbit/s instead of 6Mbit/s
|
||||
//EP_Write(3, NULL, 0); // send ZLP if buffer is in writting state now
|
||||
return;
|
||||
}
|
||||
EP_Write(3, (uint8_t*)usbbuff, buflen);
|
||||
@@ -47,44 +50,42 @@ void send_next(){
|
||||
|
||||
// blocking send full content of ring buffer
|
||||
int USB_sendall(){
|
||||
while(!bufisempty){
|
||||
if(!usbON) return 0;
|
||||
while(lastdsz > 0){
|
||||
if(!usbON) return FALSE;
|
||||
}
|
||||
return 1;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// put `buf` into queue to send
|
||||
int USB_send(const uint8_t *buf, int len){
|
||||
if(!buf || !usbON || !len) return 0;
|
||||
if(!buf || !usbON || !len) return FALSE;
|
||||
while(len){
|
||||
int a = RB_write((ringbuffer*)&rbout, buf, len);
|
||||
len -= a;
|
||||
buf += a;
|
||||
if(bufisempty){
|
||||
bufisempty = 0;
|
||||
send_next();
|
||||
}
|
||||
if(a > 0){
|
||||
len -= a;
|
||||
buf += a;
|
||||
} else if (a < 0) continue; // do nothing if buffer is in reading state
|
||||
if(lastdsz == 0) send_next(); // need to run manually - all data sent, so no IRQ on IN
|
||||
}
|
||||
return 1;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int USB_putbyte(uint8_t byte){
|
||||
if(!usbON) return 0;
|
||||
while(0 == RB_write((ringbuffer*)&rbout, &byte, 1)){
|
||||
if(bufisempty){
|
||||
bufisempty = 0;
|
||||
send_next();
|
||||
}
|
||||
if(!usbON) return FALSE;
|
||||
int l = 0;
|
||||
while((l = RB_write((ringbuffer*)&rbout, &byte, 1)) != 1){
|
||||
if(l < 0) continue;
|
||||
}
|
||||
return 1;
|
||||
if(lastdsz == 0) send_next(); // need to run manually - all data sent, so no IRQ on IN
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int USB_sendstr(const char *string){
|
||||
if(!string || !usbON) return 0;
|
||||
if(!string || !usbON) return FALSE;
|
||||
int len = 0;
|
||||
const char *b = string;
|
||||
while(*b++) ++len;
|
||||
if(!len) return 0;
|
||||
if(!len) return FALSE;
|
||||
return USB_send((const uint8_t*)string, len);
|
||||
}
|
||||
|
||||
@@ -95,13 +96,14 @@ 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((ringbuffer*)&rbin, buf, len);
|
||||
chkin();
|
||||
if(bufovrfl){
|
||||
RB_clearbuf((ringbuffer*)&rbin);
|
||||
if(!sz) sz = -1;
|
||||
else sz = -sz;
|
||||
while(1 != RB_clearbuf((ringbuffer*)&rbin));
|
||||
bufovrfl = 0;
|
||||
return -1;
|
||||
}
|
||||
int sz = RB_read((ringbuffer*)&rbin, buf, len);
|
||||
if(sz < 0) return 0; // buffer in writting state
|
||||
return sz;
|
||||
}
|
||||
|
||||
@@ -112,15 +114,22 @@ 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((ringbuffer*)&rbin, '\n', (uint8_t*)buf, len);
|
||||
if(l == 0) return 0;
|
||||
if(--l < 0 || bufovrfl) RB_clearbuf((ringbuffer*)&rbin);
|
||||
else buf[l] = 0; // replace '\n' with strend
|
||||
chkin();
|
||||
if(bufovrfl){
|
||||
if(l > 0) l = -l;
|
||||
else l = -1;
|
||||
while(1 != RB_clearbuf((ringbuffer*)&rbin));
|
||||
bufovrfl = 0;
|
||||
return -1;
|
||||
}
|
||||
int l = RB_readto((ringbuffer*)&rbin, '\n', (uint8_t*)buf, len);
|
||||
if(l < 1){
|
||||
if(rbin.length == RB_datalen((ringbuffer*)&rbin)){ // buffer is full but no '\n' found
|
||||
while(1 != RB_clearbuf((ringbuffer*)&rbin));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if(l == 0) return 0;
|
||||
buf[l-1] = 0; // replace '\n' with strend
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -23,7 +22,7 @@
|
||||
|
||||
// sizes of ringbuffers for outgoing and incoming data
|
||||
#define RBOUTSZ (512)
|
||||
#define RBINSZ (512)
|
||||
#define RBINSZ (256)
|
||||
|
||||
#define newline() USB_putbyte('\n')
|
||||
#define USND(s) do{USB_sendstr(s); USB_putbyte('\n');}while(0)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -15,7 +14,6 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "usb.h"
|
||||
#include "usb_lib.h"
|
||||
@@ -26,7 +24,7 @@ ep_t endpoints[STM32ENDPOINTS];
|
||||
static uint16_t USB_Addr = 0;
|
||||
static usb_LineCoding lineCoding = {115200, 0, 0, 8};
|
||||
uint8_t ep0databuf[EP0DATABUF_SIZE], setupdatabuf[EP0DATABUF_SIZE];
|
||||
static config_pack_t *setup_packet = (config_pack_t*) setupdatabuf;
|
||||
config_pack_t *setup_packet = (config_pack_t*) setupdatabuf;
|
||||
|
||||
usb_LineCoding getLineCoding(){return lineCoding;}
|
||||
|
||||
@@ -131,7 +129,7 @@ _USB_LANG_ID_(LD, LANG_US);
|
||||
_USB_STRING_(SD, u"0.0.1");
|
||||
_USB_STRING_(MD, u"Prolific Technology Inc.");
|
||||
_USB_STRING_(PD, u"USB-Serial Controller");
|
||||
_USB_STRING_(ID, u"pl2303_emulator");
|
||||
_USB_STRING_(ID, u"USB-STM32");
|
||||
static void const *StringDescriptor[iDESCR_AMOUNT] = {
|
||||
[iLANGUAGE_DESCR] = &LD,
|
||||
[iMANUFACTURER_DESCR] = &MD,
|
||||
@@ -266,15 +264,29 @@ static void transmit_Handler(){ // EP3IN
|
||||
send_next();
|
||||
}
|
||||
|
||||
static uint8_t volatile rcvbuf[USB_RXBUFSZ];
|
||||
static uint8_t volatile rcvbuflen = 0;
|
||||
|
||||
void chkin(){
|
||||
if(bufovrfl) return;
|
||||
if(!rcvbuflen) return;
|
||||
int w = RB_write((ringbuffer*)&rbin, (uint8_t*)rcvbuf, rcvbuflen);
|
||||
if(w < 0) return;
|
||||
if(w != rcvbuflen) bufovrfl = 1;
|
||||
rcvbuflen = 0;
|
||||
uint16_t status = KEEP_DTOG(USB->EPnR[2]); // don't change DTOG
|
||||
USB->EPnR[2] = status ^ USB_EPnR_STAT_RX;
|
||||
}
|
||||
|
||||
// receiver reads data from local buffer and only then ACK'ed
|
||||
static void receive_Handler(){ // EP2OUT
|
||||
uint8_t buf[USB_RXBUFSZ];
|
||||
uint16_t epstatus = KEEP_DTOG(USB->EPnR[2]);
|
||||
uint8_t sz = EP_Read(2, (uint8_t*)buf);
|
||||
if(sz){
|
||||
if(RB_write((ringbuffer*)&rbin, buf, sz) != sz) bufovrfl = 1;
|
||||
uint16_t status = KEEP_DTOG_STAT(USB->EPnR[2]); // don't change DTOG and NACK
|
||||
if(rcvbuflen){
|
||||
bufovrfl = 1; // lost last data
|
||||
rcvbuflen = 0;
|
||||
}
|
||||
// keep stat_tx & set ACK rx, clear RX ctr
|
||||
USB->EPnR[2] = (epstatus & ~USB_EPnR_CTR_RX) ^ USB_EPnR_STAT_RX;
|
||||
rcvbuflen = EP_Read(2, (uint8_t*)rcvbuf);
|
||||
USB->EPnR[2] = status & ~USB_EPnR_CTR_RX;
|
||||
}
|
||||
|
||||
static inline void std_h2d_req(){
|
||||
@@ -436,3 +448,103 @@ int EP_Read(uint8_t number, uint8_t *buf){
|
||||
return sz;
|
||||
}
|
||||
|
||||
|
||||
static uint16_t lastaddr = LASTADDR_DEFAULT;
|
||||
/**
|
||||
* Endpoint initialisation
|
||||
* @param number - EP num (0...7)
|
||||
* @param type - EP type (EP_TYPE_BULK, EP_TYPE_CONTROL, EP_TYPE_ISO, EP_TYPE_INTERRUPT)
|
||||
* @param txsz - transmission buffer size @ USB/CAN buffer
|
||||
* @param rxsz - reception buffer size @ USB/CAN buffer
|
||||
* @param uint16_t (*func)(ep_t *ep) - EP handler function
|
||||
* @return 0 if all OK
|
||||
*/
|
||||
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){
|
||||
if(number >= STM32ENDPOINTS) return 4; // out of configured amount
|
||||
if(txsz > USB_BTABLE_SIZE || rxsz > USB_BTABLE_SIZE) return 1; // buffer too large
|
||||
if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE/ACCESSZ) return 2; // out of btable
|
||||
USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA);
|
||||
USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1;
|
||||
if(rxsz & 1 || rxsz > USB_BTABLE_SIZE) return 3; // wrong rx buffer size
|
||||
uint16_t countrx = 0;
|
||||
if(rxsz < 64) countrx = rxsz / 2;
|
||||
else{
|
||||
if(rxsz & 0x1f) return 3; // should be multiple of 32
|
||||
countrx = 31 + rxsz / 32;
|
||||
}
|
||||
USB_BTABLE->EP[number].USB_ADDR_TX = lastaddr;
|
||||
endpoints[number].tx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ);
|
||||
endpoints[number].txbufsz = txsz;
|
||||
lastaddr += txsz;
|
||||
USB_BTABLE->EP[number].USB_COUNT_TX = 0;
|
||||
USB_BTABLE->EP[number].USB_ADDR_RX = lastaddr;
|
||||
endpoints[number].rx_buf = (uint8_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ);
|
||||
lastaddr += rxsz;
|
||||
USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10;
|
||||
endpoints[number].func = func;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// standard IRQ handler
|
||||
void USB_IRQ(){
|
||||
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;
|
||||
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, setupdatabuf);
|
||||
// interrupt handler will be called later
|
||||
}else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf
|
||||
EP_Read(0, 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;
|
||||
#ifndef STM32F0
|
||||
USB->CNTR |= USB_CNTR_FSUSP | USB_CNTR_LP_MODE;
|
||||
#else
|
||||
USB->CNTR |= USB_CNTR_FSUSP | USB_CNTR_LPMODE;
|
||||
#endif
|
||||
USB->ISTR = ~USB_ISTR_SUSP;
|
||||
}
|
||||
if(USB->ISTR & USB_ISTR_WKUP){ // wakeup
|
||||
#ifndef STM32F0
|
||||
USB->CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LP_MODE); // clear suspend flags
|
||||
#else
|
||||
USB->CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LPMODE);
|
||||
#endif
|
||||
USB->ISTR = ~USB_ISTR_WKUP;
|
||||
}
|
||||
}
|
||||
|
||||
#if defined STM32F3
|
||||
void usb_lp_isr() __attribute__ ((alias ("USB_IRQ")));
|
||||
#elif defined STM32F1
|
||||
void usb_lp_can_rx0_isr() __attribute__ ((alias ("USB_IRQ")));
|
||||
#elif defined STM32F0
|
||||
void usb_isr() __attribute__ ((alias ("USB_IRQ")));
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -15,7 +14,6 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <wchar.h>
|
||||
@@ -156,6 +154,7 @@ typedef struct {
|
||||
|
||||
extern ep_t endpoints[];
|
||||
extern volatile uint8_t usbON;
|
||||
extern config_pack_t *setup_packet;
|
||||
extern uint8_t ep0databuf[], setupdatabuf[];
|
||||
|
||||
void EP0_Handler();
|
||||
@@ -169,3 +168,5 @@ void linecoding_handler(usb_LineCoding *lc);
|
||||
void clstate_handler(uint16_t val);
|
||||
void break_handler();
|
||||
void vendor_handler(config_pack_t *packet);
|
||||
void chkin();
|
||||
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)());
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -15,109 +14,50 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "usb.h"
|
||||
#include "usb_lib.h"
|
||||
|
||||
// here we suppose that all PIN settings done in hw_setup earlier
|
||||
void USB_setup(){
|
||||
#if defined STM32F3
|
||||
NVIC_DisableIRQ(USB_LP_IRQn);
|
||||
// remap USB LP & Wakeup interrupts to 75 and 76 - works only on pure F303
|
||||
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // enable tacting of SYSCFG
|
||||
SYSCFG->CFGR1 |= SYSCFG_CFGR1_USB_IT_RMP;
|
||||
#elif defined STM32F1
|
||||
NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
|
||||
NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn);
|
||||
#elif defined STM32F0
|
||||
NVIC_DisableIRQ(USB_IRQn);
|
||||
RCC->APB1ENR |= RCC_APB1ENR_CRSEN;
|
||||
RCC->CFGR3 &= ~RCC_CFGR3_USBSW; // reset USB
|
||||
RCC->CR2 |= RCC_CR2_HSI48ON; // turn ON HSI48
|
||||
uint32_t tmout = 16000000;
|
||||
while(!(RCC->CR2 & RCC_CR2_HSI48RDY)){if(--tmout == 0) break;}
|
||||
FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;
|
||||
CRS->CFGR &= ~CRS_CFGR_SYNCSRC;
|
||||
CRS->CFGR |= CRS_CFGR_SYNCSRC_1; // USB SOF selected as sync source
|
||||
CRS->CR |= CRS_CR_AUTOTRIMEN; // enable auto trim
|
||||
CRS->CR |= CRS_CR_CEN; // enable freq counter & block CRS->CFGR as read-only
|
||||
RCC->CFGR |= RCC_CFGR_SW;
|
||||
#endif
|
||||
RCC->APB1ENR |= RCC_APB1ENR_USBEN;
|
||||
//??
|
||||
USB->CNTR = USB_CNTR_FRES; // Force USB Reset
|
||||
for(uint32_t ctr = 0; ctr < 72000; ++ctr) nop(); // wait >1ms
|
||||
//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
|
||||
#if defined STM32F3
|
||||
NVIC_EnableIRQ(USB_LP_IRQn);
|
||||
#elif defined STM32F1
|
||||
NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
|
||||
#elif defined STM32F0
|
||||
USB->BCDR |= USB_BCDR_DPPU;
|
||||
NVIC_EnableIRQ(USB_IRQn);
|
||||
#endif
|
||||
}
|
||||
|
||||
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 * ACCESSZ);
|
||||
endpoints[number].txbufsz = txsz;
|
||||
lastaddr += txsz;
|
||||
USB_BTABLE->EP[number].USB_COUNT_TX = 0;
|
||||
USB_BTABLE->EP[number].USB_ADDR_RX = lastaddr;
|
||||
endpoints[number].rx_buf = (uint8_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ);
|
||||
lastaddr += rxsz;
|
||||
USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10;
|
||||
endpoints[number].func = func;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// standard IRQ handler
|
||||
void usb_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;
|
||||
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, setupdatabuf);
|
||||
// interrupt handler will be called later
|
||||
}else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf
|
||||
EP_Read(0, 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* This file is part of the pl2303 project.
|
||||
* Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@@ -15,17 +14,61 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined STM32F0
|
||||
#include <stm32f0.h>
|
||||
#elif defined STM32F1
|
||||
#include <stm32f1.h>
|
||||
// there's no this define in standard header
|
||||
#define USB_BASE ((uint32_t)0x40005C00)
|
||||
#elif defined STM32F3
|
||||
#include <stm32f3.h>
|
||||
#endif
|
||||
|
||||
// max endpoints number
|
||||
#define STM32ENDPOINTS 8
|
||||
/**
|
||||
* Buffers size definition
|
||||
**/
|
||||
|
||||
// F0 - USB2_16; F1 - USB1_16; F3 - 1/2 depending on series
|
||||
#if !defined USB1_16 && !defined USB2_16
|
||||
#if defined STM32F0
|
||||
#define USB2_16
|
||||
#elif defined STM32F1
|
||||
#define USB1_16
|
||||
#else
|
||||
#error "Can't determine USB1_16 or USB2_16, define by hands"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// BTABLE_SIZE FOR STM32F3:
|
||||
// In STM32F303/302xB/C, 512 bytes SRAM is not shared with CAN.
|
||||
// In STM32F302x6/x8 and STM32F30xxD/E, 726 bytes dedicated SRAM and 256 bytes shared SRAM with CAN i.e.
|
||||
// 1Kbytes dedicated SRAM in case CAN is disabled.
|
||||
// remember, that USB_BTABLE_SIZE will be divided by ACCESSZ, so don't divide it twice for 32-bit addressing
|
||||
|
||||
#ifdef NOCAN
|
||||
#if defined STM32F0
|
||||
#define USB_BTABLE_SIZE 1024
|
||||
#elif defined STM32F3
|
||||
#define USB_BTABLE_SIZE 512
|
||||
#warning "Please, check real buffer size due to docs"
|
||||
#else
|
||||
#error "define STM32F0 or STM32F3"
|
||||
#endif
|
||||
#else // !NOCAN: F0/F3 with CAN or F1 (can't simultaneously run CAN and USB)
|
||||
#if defined STM32F0
|
||||
#define USB_BTABLE_SIZE 768
|
||||
#elif defined STM32F3
|
||||
#define USB_BTABLE_SIZE 512
|
||||
#warning "Please, check real buffer size due to docs"
|
||||
#else // STM32F103: 1024 bytes but with 32-bit addressing
|
||||
#define USB_BTABLE_SIZE 1024
|
||||
#endif
|
||||
#endif // NOCAN
|
||||
|
||||
// first 64 bytes of USB_BTABLE are registers!
|
||||
//#define USB_EP0_BASEADDR 64
|
||||
// for USB FS EP0 buffers are from 8 to 64 bytes long (64 for PL2303)
|
||||
@@ -110,4 +153,3 @@ typedef struct{
|
||||
} USB_BtableDef;
|
||||
|
||||
void USB_setup();
|
||||
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)());
|
||||
|
||||
Reference in New Issue
Block a user