USB Host, "Blue Pill", the method of dividing the segment in half and the price of vodka in the USSR

Wrote recently a software USB-HOST on esp32 to work with keyboard / mouse / joystick. The processor is fast, but delicate, 5 volts on the legs can't stand it. Therefore, I decided to rewrite it to stm32f103c8t6, ​​widely known in the version of the "Blue Pill" debug board .





Unfortunately, this is a very leisurely processor by today's standards (72 MHz vs 240 for esp32), so there were doubts whether I could provide the necessary accuracy of the time interval between bits during transmission ( 1.5 Mbps +/- 1.5% ), which corresponds to +/- 0.01uS, which is approximately one cycle of the processor. That is, a delay procedure like:





static inline  void cpuDelay(uint32_t ticks)
{
	uint32_t stop =_getCycleCount32() + ticks;
	while((_getCycleCount32() - stop)&0x80000000u); // ntinue while overflow
}

      
      



I stopped providing the necessary accuracy, so I decided to go to a procedure of the form:





void cpuDelay()
{
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
__asm__ __volatile__("nop");
}
  
      
      



with the required number of "nop" - s. When I got tired of adjusting the latency manually and recompiling the code, I decided to write a procedure that selects the necessary latency, working in a wide range of processor frequencies. The idea is as follows: instead of entering the procedure with the cpuDelay address, enter the procedure in the middle, at the pointer:





#define TNOP1  { __asm__ __volatile__("   nop"); }
#define TNOP2  {TNOP1 TNOP1}
#define TNOP4  {TNOP2 TNOP2}
#define TNOP8  {TNOP4 TNOP4}
#define TNOP16  {TNOP8 TNOP8}
#define TNOP32  {TNOP16 TNOP16}
#define TNOP64  {TNOP32 TNOP32}

__volatile__ void cpuDelayBase()
{
	TNOP64;
	TNOP64;
	TNOP64;
	TNOP64;
END_BASE:;
}

void (*delay_pntA)() = &cpuDelayBase;

#define cpuDelay(x) {(*delay_pntA)();}

#define SIZEOF_NOP 2

void setDelay(uint8_t ticks)
{
	delay_pntA = (&cpuDelayBase)+((256-ticks)*SIZEOF_NOP);
}

      
      



200,, delay_pntA.





6 4.0uS. 6 . 6 , :





, , 0.12uS 4.12 uS.





!!!???





, , CCCP , , , . , , , , .





. , , 2-87, - 3-62, 4-12. . 2.87 3.62 ( ) 4.12 () - . 12 , .





Russian vodka, label:
Stolichnaya vodka, label:

These are pictures from here





So 4.0 - content and 0.12-capacity total 4.12 ---- This is the price of Stolichnaya vodka in 1981.





run result:





pins 8 9 1 1 is OK!
cpu freq = 72.000000 MHz
TIME_MULT = 43
120 bits in 57.333332 uSec 2.093023 MHz  6 ticks in 2.866667 uS
120 bits in 269.000000 uSec 0.446097 MHz  6 ticks in 13.450000 uS
120 bits in 162.333328 uSec 0.739220 MHz  6 ticks in 8.116667 uS
120 bits in 109.000000 uSec 1.100917 MHz  6 ticks in 5.450000 uS
120 bits in 82.916664 uSec 1.447236 MHz  6 ticks in 4.145833 uS
120 bits in 69.000000 uSec 1.739130 MHz  6 ticks in 3.450000 uS
120 bits in 75.666664 uSec 1.585903 MHz  6 ticks in 3.783333 uS
120 bits in 75.666664 uSec 1.585903 MHz  6 ticks in 3.783333 uS
120 bits in 77.333336 uSec 1.551724 MHz  6 ticks in 3.866667 uS
120 bits in 77.333336 uSec 1.551724 MHz  6 ticks in 3.866667 uS
TRANSMIT_TIME_DELAY = 15 time = 4.145833 error = 0.627029%
USB1: Ack = 0 Nack = 0 20 pcurrent->cb_Cmd = 2  state = 2 epCount = 0
USB2: Ack = 0 Nack = 0 00 pcurrent->cb_Cmd = 0  state = 0 epCount = 0
desc.bDeviceClass    = 00
desc.bNumConfigurations = 01
cfg.bLength         = 09
cfg.wLength         = 59
cfg.bNumIntf        = 02
cfg.bCV             = 01
cfg.bIndex          = 00
cfg.bAttr           = a0
cfg.bMaxPower       = 50
pcurrent->epCount = 1
pcurrent->epCount = 2
desc.bDeviceClass    = 00
desc.bNumConfigurations = 01
cfg.bLength         = 09
cfg.wLength         = 34
cfg.bNumIntf        = 01
cfg.bCV             = 01
cfg.bIndex          = 00
cfg.bAttr           = a0
cfg.bMaxPower       = 50
pcurrent->epCount = 1
USB0: Ack = 6 Nack = 0 80 pcurrent->cb_Cmd = 14  state = 100 epCount = 2
USB1: Ack = 6 Nack = 0 20 pcurrent->cb_Cmd = 14  state = 104 epCount = 1

      
      



PS Vodka vodka, we in Israel allowed hemp, I forgot to post the source, here .





PPS don't find fault with the code. This is just a proof of concept. Will be cleaned.








All Articles