Lazarus

Programming => Embedded => Operating Systems => Embedded - ARM => Topic started by: diego bertotti on August 17, 2020, 10:58:13 pm

Title: blue pill usb hid etc etc [SOLVED]
Post by: diego bertotti on August 17, 2020, 10:58:13 pm
hi

i try to reuse the serrasidis usb hid bootloader in my project.

https://github.com/Serasidis/STM32_HID_Bootloader

bootloader works perfectly!!

i do a little mod in a led unit and works perfect

now i try to reuse the C object files compiled from the hid bootloader, to have usb communication between uC and windows PC .

now uC don't hang but usb host don't recognise it...remember the same code works perfect in the bootloader. actually i burn the flash using it (and compiling using $800 offset)

i dont found why

now a piece of code

Code: Pascal  [Select][+][-]
  1.  
  2. program something;
  3.  
  4. uses cortexm3, stm32f103fw;
  5.  
  6.  
  7. Procedure USB_Init; cdecl; external name'USB_Init';
  8. Procedure USB_Shutdown; cdecl; external name'USB_Shutdown';
  9. Procedure USB_SendData(EndPoint: byte; var data: word; Lenght: word); cdecl; external name'USB_SendData';
  10. Procedure USB_PMA2Buffer(endpoint: byte); cdecl; external name'USB_PMA2Buffer';
  11. Procedure USB_Buffer2PMA(endpoint: byte); cdecl; external name'USB_Buffer2PMA';
  12. Procedure USB_LP_CAN1_RX0_IRQHandler; cdecl; external name 'USB_LP_CAN1_RX0_IRQHandler';
  13. Procedure Pins_Init; cdecl; external name'pins_init';
  14. Procedure Pin13_One; cdecl; external name'pin13_one';
  15. Procedure Pin13_Cero; cdecl; external name'pin13_cero';  
  16. Procedure Pin13(status: byte); cdecl; external name'pin13';
  17. Procedure USB_EPHandler(status: word);cdecl; external name'USB_EPHandler';
  18. Procedure USB_Reset; cdecl; external name'USB_Reset';
  19.  
  20. {$L c:\hidboot\f1\build\usb.o}
  21. {$L c:\hidboot\f1\build\hid.o}
  22. {$L c:\hidboot\f1\build\flash.o}
  23. {$L c:\hidboot\f1\build\led.o}
  24.  
  25. const
  26.   MAX_BUFFER_SIZE = 64;
  27.   MAX_EP_NUM = 2;
  28.  
  29.  
  30.  
  31. {$O-}
  32.  
  33. Type
  34.  
  35.   TLed = bitpacked array[0..15] of boolean;
  36.  
  37.  USB_RxTxBuf_t = record
  38.         RXB: array[0..MAX_BUFFER_SIZE div 2] of word;
  39.         PTXB: word;
  40.         RXL: byte;
  41.         TXL: byte;
  42.         MaxPacketSize: byte;
  43.   end;  
  44.  
  45. var
  46.  
  47. DeviceConfigured: word; external name 'DeviceConfigured';
  48. DeviceAddress: byte; external name 'DeviceAddress';
  49. DeviceStatus: word; external name 'DeviceStatus';
  50. UploadStarted: boolean; external name 'UploadStarted';
  51. UploadFinished: boolean; external name 'UploadFinished';
  52. RxTxBuffer: array[0..MAX_EP_NUM] of USB_RxTxBuf_t; external;
  53.  
  54. PuertoA : TLed absolute PortA.ODR;
  55. PuertoB : TLed absolute PortB.ODR;
  56. PuertoC : TLed absolute PortC.ODR;
  57. PuertoD : TLed absolute PortD.ODR;
  58. LedB : TLed absolute PortB.ODR;
  59. LedC : TLed absolute PortC.ODR;
  60. contador: word;
  61. contador2,
  62. contador3: word;
  63. cuenta_max: word = 20;
  64. subiendo: boolean = true;
  65.  
  66.  
  67. procedure USB_Low_Priority_or_CAN_RX0_interrupts; public name 'USB_Low_Priority_or_CAN_RX0_interrupts';  interrupt;
  68.     begin
  69.     USB_LP_CAN1_RX0_IRQHandler;
  70.     USB.ISTR:= 0;
  71.     end;
  72.  
  73. begin
  74.  
  75.  //************************************************
  76.  //**************** RCC  *************************
  77.  RCC_HSICmd(Enabled);
  78.  RCC_DeInit;
  79.  RCC_HSICmd(Enabled);
  80.  RCC_HSEConfig(RCC_HSE_On);
  81.  RCC_WaitForHSEStartUp;
  82.  RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6); //48MHz
  83.  RCC_PLLCmd(Enabled);
  84.    while rcc.cr and  RCC_CR_PLLRDY = 0 do
  85.     begin
  86.     //wait pll ready
  87.     end;
  88.  RCC_HCLKConfig(RCC_SYSCLK_Div1);
  89.  RCC_PCLK1Config(RCC_HCLK_Div2); //24MHz
  90.  RCC_PCLK2Config(RCC_HCLK_Div1); //48MHz
  91.  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  92.  while RCC_GetSYSCLKSource <> %1000 do //(RCC_SYSCLKSource_PLLCLK shl 2)
  93.    begin
  94.    //wait pll source
  95.    end;
  96.  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_DMA1 or RCC_AHBPeriph_DMA2 or RCC_AHBPeriph_SRAM or RCC_AHBPeriph_FLITF, Enabled);
  97.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL, Enabled);
  98.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, Disabled);
  99.  RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL, Enabled);
  100.  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 or RCC_APB1Periph_I2C2 or RCC_APB1Periph_CAN, Disabled);
  101.  RCC_ADCCLKConfig(RCC_PCLK2_Div8);  //6 MHz
  102.  RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_Div1); //48MHz
  103.  RCC_ClockSecuritySystemCmd(Enabled);
  104.  RCC_GetClocksFreq(RCC_Clocks);  
  105.  
  106. //***************************************************
  107.   //******************** Systick **********************
  108.   Systick.LOAD:= 6000;   // 1ms main loop
  109.   SysTick.CTRL := SysTick.CTRL or CTRL_TICKINT_Set AND (not 4);// interrupt, AHB/8
  110.   SysTick.CTRL := SysTick.CTRL or systick_counter_enable ; //enabled
  111.  
  112.   contador:= cuenta_max - 2;
  113.  
  114.   USB_Init;
  115.   while true do
  116.     begin
  117.     If marca then
  118.       begin
  119.       marca:= False;
  120.       Inc(Contador);
  121.       Inc(Contador_tick_Segundos);
  122.  
  123.     If Contador_tick_segundos >= 10 then  //1seg
  124.         begin
  125.         Contador_tick_segundos:= 0;
  126.         end;
  127.     if contador <= contador2 then LedC[13] := not LedC[13] else LedC[13] := true;
  128.     if contador >= cuenta_Max then
  129.       begin
  130.       contador := 0;
  131.       if subiendo then contador2 += 2 else contador2 -= 2;
  132.       if contador2 > Cuenta_max then subiendo:= false;
  133.       if contador2 = 0 then subiendo:= true;
  134.       end;
  135.     end;
  136.     end;
  137.  end.      
  138.  
  139.  

somebody can help me?

thanks
Title: Re: blue pill usb hid etc etc
Post by: MiR on August 17, 2020, 11:23:09 pm
I have close to no experince with USB, so sorry, cannot help much...

Nevertheless two hints:

https://github.com/hathach/tinyusb

looks very portable and is used by big Projects like MicroPython and Circuitpython, perhaps a good choice for using a C-Stack for USB.

There is however a properly more simple solution, have a look at

https://libopencm3.org/

and

https://github.com/libopencm3/libopencm3-examples/tree/master/examples/stm32/f1/stm32-maple/usb_cdcacm

for an example

they have low level examples for USB and code is usually not that complicated.

From what I know (which is very,very little) you must call some usb specific callbacks from time to time to be able to properly communicate. The example above does this in main loop
Title: Re: blue pill usb hid etc etc
Post by: diego bertotti on August 17, 2020, 11:35:29 pm
hi MiR

thanks for reply

Quote
From what I know (which is very,very little) you must call some usb specific callbacks from time to time to be able to properly communicate. The example above does this in main loop

this is why i want to "reuse" this code. i know its working!
Title: Re: blue pill usb hid etc etc
Post by: Laksen on August 18, 2020, 12:05:48 am
Shameless plug: https://github.com/laksen/fp-usb ;)

It needs a simpler example than the ACM class device. Maybe USB-DFU would make sense. It's pretty widely supported and a simple HID like EP0 interface
Title: Re: blue pill usb hid etc etc
Post by: diego bertotti on August 18, 2020, 01:36:05 pm
thanks laksen ....ill try it
Title: Re: blue pill usb hid etc etc
Post by: Laksen on August 18, 2020, 03:17:27 pm
I added the slightly simpler DFU example
Title: Re: blue pill usb hid etc etc
Post by: diego bertotti on August 20, 2020, 02:44:48 pm
bingooooooo!!

now is working!!

i must set clock to 72 MHz and usb divisor to 1.5.....actually i dont know why, my original code, generates 48 MHz too to the usb macrocell, but dont work.

now with 72 MHz run perfect!!!!!

somebody knows why?

maybe undocumented need of this speed in another uC section to use usb macrocell
Title: Re: blue pill usb hid etc etc [SOLVED]
Post by: Laksen on August 20, 2020, 03:53:35 pm
Were you using the HSE before? The HSI is not precise enough for USB
Title: Re: blue pill usb hid etc etc [SOLVED]
Post by: diego bertotti on August 20, 2020, 04:20:32 pm
Quote
Were you using the HSE before? The HSI is not precise enough for USB
always HSE. look code in the first post
Title: Re: blue pill usb hid etc etc [SOLVED]
Post by: diego bertotti on August 24, 2020, 09:24:50 pm
hi

laksen

Quote
Shameless plug: https://github.com/laksen/fp-usb ;)

It needs a simpler example than the ACM class device. Maybe USB-DFU would make sense. It's pretty widely supported and a simple HID like EP0 interface

i try your code and works fine

then, i change pll to 48 MHz (mult by 6) and usb div to 1 and it fails!!!!!!!


anybody knows why?
Title: Re: blue pill usb hid etc etc
Post by: Blade on August 24, 2020, 10:25:58 pm
bingooooooo!!

now is working!!

Could you show examples of code that was working?
Title: Re: blue pill usb hid etc etc [SOLVED]
Post by: diego bertotti on August 25, 2020, 03:20:18 pm
hi

look the code in the first post and make corrections indicated on reply#6
Title: Re: blue pill usb hid etc etc [SOLVED]
Post by: MarkMLl on August 25, 2020, 04:21:57 pm
anybody knows why?

Do you appreciate that those "Blue Pills" are notorious for having very flaky USB interface circuitry due to the manufracturers making random component substitutions?

MarkMLl
Title: Re: blue pill usb hid etc etc [SOLVED]
Post by: diego bertotti on August 25, 2020, 10:56:08 pm
Quote
Do you appreciate that those "Blue Pills" are notorious for having very flaky USB interface circuitry due to the manufracturers making random component substitutions?

i'm not sure to understand you, but the same board works fine at 72 MHz and don't work at 48 MHz!!!

maybe could be a bad quality reason if things happened in the inverse order (72 fail 48 ok).

In my opinion, isn't the case, but this boards are very cheap... may be, maybe not
Title: Re: blue pill usb hid etc etc [SOLVED]
Post by: avra on August 25, 2020, 11:29:46 pm
Quote
Do you appreciate that those "Blue Pills" are notorious for having very flaky USB interface circuitry due to the manufracturers making random component substitutions?

i'm not sure to understand you
In the past there have been many cheap blue pill boards with this flaw:
https://amitesh-singh.github.io/stm32/2017/05/27/Overcoming-wrong-pullup-in-blue-pill.html

There is a hardware, or a software workaround. It is best to order from the seller who already claims to have that problem solved.

There are also STM32F103 clones:
https://www.cnx-software.com/2020/03/22/how-to-detect-stm32-fakes/
https://www.cnx-software.com/2019/02/10/cs32-mcu-stm32-clone-bluepill-board/
Title: Re: blue pill usb hid etc etc [SOLVED]
Post by: diego bertotti on August 31, 2020, 01:20:40 am
hi

i'm very happy

finally i was do it!!

now i can put the stm32f103 bluepill board in a hid_bootloader state from lazarus menu  and start to write flash memory without move any jumper or unplug and replug the usb cable.

i modified the hid.c unit and made a new windows executable file
TinyPortal © 2005-2018