mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-02-28 11:54:30 +03:00
try to make a-la usb-i2c-tiny over F103
This commit is contained in:
243
F1:F103/I2C_tiny/usbdev.c
Normal file
243
F1:F103/I2C_tiny/usbdev.c
Normal file
@@ -0,0 +1,243 @@
|
||||
/*
|
||||
* This file is part of the I2Ctiny project.
|
||||
* 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
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stm32f1.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "i2c.h"
|
||||
#include "i2ctiny.h"
|
||||
#include "usbdev.h"
|
||||
|
||||
static const uint32_t func = I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART;
|
||||
static uint8_t status = STATUS_IDLE;
|
||||
|
||||
// interrupt IN handler - never used
|
||||
static void EP1_Handler(){
|
||||
uint16_t epstatus = KEEP_DTOG_STAT(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;
|
||||
}
|
||||
|
||||
void set_configuration(uint16_t _U_ configuration){
|
||||
EP_Init(1, EP_TYPE_INTERRUPT, USB_EP1BUFSZ, 0, EP1_Handler);
|
||||
}
|
||||
|
||||
static void usb_i2c_io(config_pack_t *req, uint8_t *buf, size_t *len){
|
||||
static uint8_t iobuf[256] = "1234567890abcdefghijclmnop";
|
||||
//uint8_t cmd = req->bRequest;
|
||||
uint8_t size = req->wLength;
|
||||
//i2c_set_addr7(req->wIndex);
|
||||
i2c_status stat = I2C_NACK;
|
||||
// ignore NOSTART and STOP!
|
||||
if(req->wValue & I2C_M_RD){ // read
|
||||
//stat = i2c_7bit_receive(buf, size);
|
||||
if(len && *len) memcpy(buf, iobuf, *len);
|
||||
stat = I2C_OK;
|
||||
*len = size;
|
||||
}else{ // write
|
||||
//stat = i2c_7bit_send(buf, size);
|
||||
if(len && *len) memcpy(iobuf, buf, *len);
|
||||
stat = I2C_OK;
|
||||
*len = 0;
|
||||
}
|
||||
if(stat == I2C_OK){
|
||||
status = STATUS_ADDRESS_ACK;
|
||||
}else{
|
||||
//i2c_setup();
|
||||
*len = 0;
|
||||
status = STATUS_ADDRESS_NACK;
|
||||
}
|
||||
}
|
||||
|
||||
void usb_class_request(config_pack_t *req, uint8_t *data, unsigned int datalen){
|
||||
uint8_t buf[USB_EP0BUFSZ];
|
||||
size_t len = 0;
|
||||
//uint8_t recipient = REQUEST_RECIPIENT(req->bmRequestType);
|
||||
if((req->bmRequestType & 0x80) == 0){ // OUT - setters
|
||||
switch(req->bRequest){
|
||||
case CMD_I2C_IO:
|
||||
case CMD_I2C_IO | CMD_I2C_BEGIN:
|
||||
case CMD_I2C_IO | CMD_I2C_END:
|
||||
case CMD_I2C_IO | CMD_I2C_BEGIN | CMD_I2C_END: // write
|
||||
if(req->wValue & I2C_M_RD) break;
|
||||
len = datalen;
|
||||
usb_i2c_io(req, data, &len);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
EP_WriteIRQ(0, 0, 0);
|
||||
return;
|
||||
}
|
||||
switch(req->bRequest){
|
||||
case CMD_ECHO:
|
||||
memcpy(buf, &req->wValue, sizeof(req->wValue));
|
||||
len = sizeof(req->wValue);
|
||||
break;
|
||||
case CMD_GET_FUNC:
|
||||
/* Report our capabilities */
|
||||
bzero(buf, req->wLength);
|
||||
memcpy(buf, &func, sizeof(func));
|
||||
len = req->wLength;
|
||||
break;
|
||||
case CMD_I2C_IO:
|
||||
case CMD_I2C_IO | CMD_I2C_BEGIN:
|
||||
case CMD_I2C_IO | CMD_I2C_END:
|
||||
case CMD_I2C_IO | CMD_I2C_BEGIN | CMD_I2C_END: // read
|
||||
if(req->wValue & I2C_M_RD){
|
||||
len = req->wLength;
|
||||
usb_i2c_io(req, buf, &len);
|
||||
}
|
||||
break;
|
||||
case CMD_GET_STATUS:
|
||||
memcpy(buf, &status, sizeof(status));
|
||||
len = sizeof(status);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
EP_WriteIRQ(0, buf, len); // write ZLP if nothing received
|
||||
}
|
||||
|
||||
void usb_vendor_request(config_pack_t *req, uint8_t *data, unsigned int datalen){
|
||||
uint8_t buf[USB_EP0BUFSZ];
|
||||
size_t len = 0;
|
||||
//uint8_t recipient = REQUEST_RECIPIENT(req->bmRequestType);
|
||||
if((req->bmRequestType & 0x80) == 0){ // OUT - setters
|
||||
switch(req->bRequest){
|
||||
case CMD_I2C_IO:
|
||||
case CMD_I2C_IO | CMD_I2C_BEGIN:
|
||||
case CMD_I2C_IO | CMD_I2C_END:
|
||||
case CMD_I2C_IO | CMD_I2C_BEGIN | CMD_I2C_END: // write
|
||||
if(req->wValue & I2C_M_RD) break;
|
||||
if(!data || !datalen) break; // omit for next stage - when data received
|
||||
len = datalen;
|
||||
usb_i2c_io(req, data, &len);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
EP_WriteIRQ(0, 0, 0);
|
||||
return;
|
||||
}
|
||||
switch(req->bRequest){
|
||||
case CMD_ECHO:
|
||||
memcpy(buf, &req->wValue, sizeof(req->wValue));
|
||||
len = sizeof(req->wValue);
|
||||
break;
|
||||
case CMD_GET_FUNC:
|
||||
/* Report our capabilities */
|
||||
bzero(buf, req->wLength);
|
||||
memcpy(buf, &func, sizeof(func));
|
||||
len = req->wLength;
|
||||
break;
|
||||
case CMD_I2C_IO:
|
||||
case CMD_I2C_IO | CMD_I2C_BEGIN:
|
||||
case CMD_I2C_IO | CMD_I2C_END:
|
||||
case CMD_I2C_IO | CMD_I2C_BEGIN | CMD_I2C_END: // read
|
||||
if(req->wValue & I2C_M_RD){
|
||||
len = req->wLength;
|
||||
usb_i2c_io(req, buf, &len);
|
||||
}
|
||||
break;
|
||||
case CMD_GET_STATUS:
|
||||
memcpy(buf, &status, sizeof(status));
|
||||
len = sizeof(status);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
EP_WriteIRQ(0, buf, len); // write ZLP if nothing received
|
||||
}
|
||||
#if 0
|
||||
// handler of vendor requests
|
||||
void usb_vendor_request(config_pack_t *req){
|
||||
uint8_t buf[USB_EP0BUFSZ];
|
||||
size_t len = 0;
|
||||
if((req->bmRequestType & 0x80) == 0){ // OUT
|
||||
EP_WriteIRQ(0, 0, 0);
|
||||
return;
|
||||
}
|
||||
switch(req->bRequest){
|
||||
case CMD_ECHO:
|
||||
memcpy(buf, &req->wValue, sizeof(req->wValue));
|
||||
len = sizeof(req->wValue);
|
||||
break;
|
||||
case CMD_GET_FUNC:
|
||||
/* Report our capabilities */
|
||||
memcpy(buf, &func, sizeof(func));
|
||||
len = sizeof(func);
|
||||
break;
|
||||
/* case CMD_I2C_IO:
|
||||
case CMD_I2C_IO | CMD_I2C_BEGIN:
|
||||
case CMD_I2C_IO | CMD_I2C_END:
|
||||
case CMD_I2C_IO | CMD_I2C_BEGIN | CMD_I2C_END:
|
||||
if(req->wValue & I2C_M_RD) bptr = buf;
|
||||
else{
|
||||
bptr = data;
|
||||
len = datalen;
|
||||
}
|
||||
usb_i2c_io(req, bptr, &len);
|
||||
break;*/
|
||||
case CMD_GET_STATUS:
|
||||
memcpy(buf, &status, sizeof(status));
|
||||
len = sizeof(status);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
EP_WriteIRQ(0, buf, len); // write ZLP if nothing received
|
||||
#if 0
|
||||
buf[0] = 'D';
|
||||
buf[1] = 'V';
|
||||
switch(recipient){
|
||||
case REQ_RECIPIENT_DEVICE:
|
||||
buf[2] = 'D';
|
||||
//return;
|
||||
break;
|
||||
case REQ_RECIPIENT_INTERFACE:
|
||||
buf[2] = 'I';
|
||||
break;
|
||||
case REQ_RECIPIENT_ENDPOINT:
|
||||
buf[2] = 'E';
|
||||
break;
|
||||
default:
|
||||
buf[2] = 'O';
|
||||
return;
|
||||
}
|
||||
EP_WriteIRQ(0, buf, 3);
|
||||
return;
|
||||
switch(recipient){
|
||||
case REQ_RECIPIENT_DEVICE:
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
/*
|
||||
case REQ_RECIPIENT_INTERFACE:
|
||||
break;
|
||||
case REQ_RECIPIENT_ENDPOINT:
|
||||
break;
|
||||
default:
|
||||
break;*/
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user