bluepill detection - a hack

Post your cool example code here.
Post Reply
ag123
Posts: 243
Joined: Thu Mar 07, 2019 6:15 am
OS: linux
IDE: eclipse, arduino 1.8.5
Core: Roger's
Board: Maple mini, Bluepill

bluepill detection - a hack

Post by ag123 » Wed Mar 20, 2019 9:46 am

i tried to make a binary that works for both maple mini and blue pill, a trouble is that on blue pill the usb reset needs to be done a little differently
comparing the board differences i noted the led PC13 on blue pill is sinking currents from the led

hence i coded this way in the maple mini variant codes

Code: Select all

__weak void board_setup_usb(void) {
#ifdef SERIAL_USB
//       SerialUSB.begin();
         gpio_set_mode(GPIOC, 13, GPIO_INPUT_FLOATING);
         if(gpio_read_bit(GPIOC, 13)) {
         	//USB reset (single ended zero)
         	gpio_set_mode(GPIOA,12,GPIO_OUTPUT_PP);
         	gpio_write_bit(GPIOA,12,0);
         	uint32_t wait = systick_get_count() + 10;
         	while(systick_get_count()<wait);
         	gpio_write_bit(GPIOA,12,1);
        }
	Serial.begin();// Roger Clark. Changed SerialUSB to Serial for Arduino sketch compatibility
#endif
}
the hack by checking the led pin seem to work well enough so that the binary works on both boards
:mrgreen:

User avatar
Vassilis
Posts: 181
Joined: Wed Feb 27, 2019 5:09 pm
Answers: 2
Location: Thessaloniki, Greece
OS: Linux, Win10, MacOS
IDE: Arduino 1.8.9
Core: Roger, STM official
Board: Bluepill, Maple mini, STM32F4xx
Contact:

Re: bluepill detection - a hack

Post by Vassilis » Wed Mar 20, 2019 11:54 am

Nice hack !

Of course there are some differences between these two boards such as:
  • LED_BUILTIN (Bluepill LED > PC13, Maple mini LED > PB1)
  • Different pin order. For example:

    Code: Select all

    digitalWrite(0,HIGH)
    On Bluepill pin 0 = PA0 but on Maple mini pin 0 = PB11
and more...
-Vassilis Serasidis

User avatar
zoomx
Posts: 52
Joined: Tue Mar 05, 2019 8:10 am
Location: Mt Etna - Italy
OS: Windows 7 & 10
IDE: 1.8.8
Core: Roger & STM official
Board: Bluepill, Maple mini

Re: bluepill detection - a hack

Post by zoomx » Thu Mar 21, 2019 9:02 am

Maybe you can simply check LED_BUILTIN value, if it is equal to PB1 the board is a Maple mini

User avatar
Vassilis
Posts: 181
Joined: Wed Feb 27, 2019 5:09 pm
Answers: 2
Location: Thessaloniki, Greece
OS: Linux, Win10, MacOS
IDE: Arduino 1.8.9
Core: Roger, STM official
Board: Bluepill, Maple mini, STM32F4xx
Contact:

Re: bluepill detection - a hack

Post by Vassilis » Thu Mar 21, 2019 2:33 pm

You can do the same by the following hack (basically, the following could be named "Maple mini detection") :D .
First, get a backup of your file
  • Edit the file STM32F1\variants\generic_stm32f103c\wirish\boards_setup.cpp
  • Replace with the following code. I wrote some comments about how this method works .

    Code: Select all

    #ifdef SERIAL_USB
    
    #ifdef GENERIC_BOOTLOADER
          /* PB8 pin on Maple mini goes to GND through a 10k resistor
           * If you enable the internal 40k pull-up resistor on PB8
           * then the PB9 has almost 0.66V that  means Logic LOW.
           * If the PB9 is not connected to GND then the voltage = 3.3V = Logic High
           */
          gpio_set_mode(GPIOB, 8, GPIO_INPUT_PU); //Enable internal pull-up
          if(gpio_read_bit(GPIOB, 8) == false) { //If PB9 is LOW...
            gpio_set_mode(GPIOB,9,GPIO_OUTPUT_PP);
            gpio_write_bit(GPIOB,9,0);//Enable pull-up on PA12 (through the on board transistor)
          }
    
          //Reset the USB interface on generic boards - developed by Victor PV
          gpio_set_mode(PIN_MAP[PA12].gpio_device, PIN_MAP[PA12].gpio_bit, GPIO_OUTPUT_PP);
          gpio_write_bit(PIN_MAP[PA12].gpio_device, PIN_MAP[PA12].gpio_bit,0);
          
          for(volatile unsigned int i=0;i<512;i++);// Only small delay seems to be needed, and USB pins will get configured in Serial.begin
          gpio_set_mode(PIN_MAP[PA12].gpio_device, PIN_MAP[PA12].gpio_bit, GPIO_INPUT_FLOATING);
    #endif
          Serial.begin();// Roger Clark. Changed SerialUSB to Serial for Arduino sketch compatibility
    #endif
  • Select Generic STM32F103C series from Tools > Board menu
  • Upload method: STM32Duino bootloader
  • You can now compile and burn your sketch on both Maple mini and blue pill by selecting the Generic STM32F103C series variant
-Vassilis Serasidis

ag123
Posts: 243
Joined: Thu Mar 07, 2019 6:15 am
OS: linux
IDE: eclipse, arduino 1.8.5
Core: Roger's
Board: Maple mini, Bluepill

Re: bluepill detection - a hack

Post by ag123 » Thu Mar 21, 2019 5:11 pm

oops i realised my hack isn't foolproof
it works on a blue pill
but when i tried the same on maple mini it says it is a blue pill
for some reason pc13 (blue pills led pin) read as high even though on maple mini nothing is connected there (floating)

detecting a maple mini is 'harder', this in part because maple mini led is active high. so i'd guess it would read as 0
and it isn't easy to tell if the pin is grounded or left floating but zero.

the other hairy way is to play with the usb reset transistors that's there on maple mini. the trouble with that is that it would trigger a usb reset and cause the host to enumerate usb again. hence that's not a good way for detection.

one possibility is that pb8 is connected to boot0, at least it seemed on the schematic
https://github.com/leaflabs/maplemini
unfortunately, stm32 didn't offer a way to read boot0 a search in rm0008 draws a blank
:lol:

Post Reply