add I2C for F303, fix bug in USB (some variables wasn't volatile)

This commit is contained in:
eddyem
2023-01-22 21:55:49 +03:00
parent c6e3acc460
commit b415b94b55
35 changed files with 2605 additions and 27 deletions

Binary file not shown.

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -1,2 +1,2 @@
#define BUILD_NUMBER "14"
#define BUILD_DATE "2023-01-18"
#define BUILD_NUMBER "35"
#define BUILD_DATE "2023-01-22"