How to Usart Hoverboard hack

xplosion
Thu Jun 08, 2017 10:05 pm
Hi
I got this code from another post on this forum. Im having trouble make it work. I dont know which pin to use. I use Uart pin PA2 & PA3, PA6 (miso1) & PA7(mosi1), PA14 (miso1) & PA15(mosi1) and i can’t get it to working. i will like to use 2 Usart port to control each motor with 1 Usart port its 2 motors in total. I got it working on arduino pro mini but pro mini only have 1 usart port and i get problem when using 2 pro mini on ic2 to control each motor. im using the mini STM32. thank you for any help

i want to do the hoverboard hack.
http://drewspewsmuse.blogspot.co.uk/201 … ooter.html

#include <libmaple/usart.h>
#include "HardwareSerial.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial1.begin(26300);
Serial2.begin(26300);
USART1->regs->CR1 |= USART_CR1_M_9N1 ;
USART2->regs->CR1 |= USART_CR1_M_9N1 ;
GPIOA->regs->BSRR = 1;
}

char c = ' ';
signed int sp = 0;

void loop() {
Serial.println(c);
if (c == ' ') {
sp = 0;
} else if (c == 'q') {
sp -= 10;
} else if (c == 'w') {
sp += 10;
} else if (c == '2') {
sp += 100;
} else if (c == '1') {
sp -= 100;
}
Serial.print("speed ");
Serial.println(sp);
Serial.print(" low byte ");
Serial.print((sp & 0xFF), HEX);
Serial.print(" high byte ");
Serial.println((sp >> 8) & 0xFF, HEX);
do {
Serial1.write(256);
Serial1.write(sp & 0xFF);
Serial1.write((sp >> 8) & 0xFF);
Serial1.write(sp & 0xFF);
Serial1.write((sp >> 8) & 0xFF);
Serial1.write(85);
delayMicroseconds(300);
} while (!Serial.available());
c = Serial.read();
}


RogerClark
Thu Jun 08, 2017 10:33 pm
can you clarify things

You mentioned 3 sets of pins, and also used the SPI terms mosi and miso

Are you using UARTS and SPI?

Assuming you are using the bootloader, Serial is USB-Serial.

Serial1 is UART1 on PA9 and PA10
Serial2 is on UART2 ( Im not sure but this could be PA2 and PA3 – but you should double check)

If you need serial on other pins, all of the UARTs can be remapped to Alternative pins, but its not totally flexible. its just a choice of 2 sets of pins for each UART


xplosion
Fri Jun 09, 2017 12:21 am
thank you for the reply Roger

i tested all 3 set of pins to see if i get it to work and i didnt get no responds. this is the code i used on pro mini and it uses the spi pins. do i have to use the spi pins in mini STM32? im using PA9 & PA10 for USB

#include <SoftwareSerial9.h>

#define MOSI 11
#define MISO 12
#define TX MOSI
#define RX MISO
#define LEDPIN 13

SoftwareSerial9 mySerial(RX,TX);

void setup() {
mySerial.begin(26315);
Serial.begin(115200);
}

char c = ' ';
signed int sp=0;

void loop() {
Serial.println(c);
if(c == ' ') {
sp=0;
} else if(c == '-') {
sp -= 10;
} else if(c == '+') {
sp += 10;
} else if(c == '2') {
sp += 100;
} else if(c == '1') {
sp -= 100;
}
Serial.print("speed ");
Serial.println(sp);
Serial.print(" low byte ");
Serial.print((sp & 0xFF), HEX);
Serial.print(" high byte ");
Serial.println((sp >> 8) & 0xFF, HEX);
do {
mySerial.write9(256);
mySerial.write9(sp & 0xFF);
mySerial.write9((sp >> 8) & 0xFF);
mySerial.write9(sp & 0xFF);
mySerial.write9((sp >> 8) & 0xFF);
mySerial.write9(85);
delayMicroseconds(300);
} while(!Serial.available());
c=Serial.read();
}


RogerClark
Fri Jun 09, 2017 1:21 am
Back to basics

Precisely which board are you using (link please)
Do know if you are using the bootloader and USB Serial ?


xplosion
Fri Jun 09, 2017 1:53 am
Image

yes im using the bootloader and USB serial.


RogerClark
Fri Jun 09, 2017 2:07 am
OK. That board is known here as Blue Pill

So.. What do you have connected to what (STM32 <–> Hover board)

BTW.
I’m not entirely sure why you are using software serial, as there are 3 hardware serial channels available


xplosion
Mon Jun 12, 2017 4:01 am
Hi Roger

I connected PA2 & PA3 to hoverboard.

this the code
#include <libmaple/usart.h>
#include <libmaple/gpio.h>
#include "HardwareSerial.h"

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial1.begin(26300);
Serial2.begin(26300);
//Serial3.begin(26300);

USART1->regs->CR1 |= USART_CR1_M_9N1 ;
USART2->regs->CR1 |= USART_CR1_M_9N1 ;
USART3->regs->CR1 |= USART_CR1_M_9N1 ;
GPIOA->regs->BSRR = 1;
}

char c = ' ';
signed int sp = 0;

void loop() {
Serial.println(c);
if (c == ' ') {
sp = 0;
} else if (c == 'q') {
sp -= 10;
} else if (c == 'w') {
sp += 10;
} else if (c == '2') {
sp += 100;
} else if (c == '1') {
sp -= 100;
}
Serial.print("speed ");
Serial.println(sp);
Serial.print(" low byte ");
Serial.print((sp & 0xFF), HEX);
Serial.print(" high byte ");
Serial.println((sp >> 8) & 0xFF, HEX);
do {
Serial1.write(256);
Serial1.write(sp & 0xFF);
Serial1.write((sp >> 8) & 0xFF);
Serial1.write(sp & 0xFF);
Serial1.write((sp >> 8) & 0xFF);
Serial1.write(85);
//
Serial2.write(256);
Serial2.write(sp & 0xFF);
Serial2.write((sp >> 8) & 0xFF);
Serial2.write(sp & 0xFF);
Serial2.write((sp >> 8) & 0xFF);
Serial2.write(85);

delayMicroseconds(300);
} while (!Serial.available());
c = Serial.read();
}


RogerClark
Mon Jun 12, 2017 6:07 am
Serial2 should output to PA2 / PA3

I suggest you hook up a logic analyser to those pins and check the data being sent is what is required.


stevestrong
Mon Jun 12, 2017 8:59 am
You could use in your code a simpler way to pass parameters to serial:

Serial1.begin(26300, SERIAL_9N1);


xplosion
Mon Jun 12, 2017 3:36 pm
Roger i dont have a logic analyser is there another way to check it?

steve this is the right way to simplify the code?
#include <libmaple/usart.h>
#include <libmaple/gpio.h>
#include "HardwareSerial.h"
//
//disableDebugPorts();
//enableDebugPorts();

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial1.begin(26300, SERIAL_9N1);
Serial2.begin(26300, SERIAL_9N1);
}

char c = ' ';
signed int sp = 0;

void loop() {
Serial.println(c);
if (c == ' ') {
sp = 0;
} else if (c == 'q') {
sp -= 10;
} else if (c == 'w') {
sp += 10;
} else if (c == '2') {
sp += 100;
} else if (c == '1') {
sp -= 100;
}
Serial.print("speed ");
Serial.println(sp);
Serial.print(" low byte ");
Serial.print((sp & 0xFF), HEX);
Serial.print(" high byte ");
Serial.println((sp >> 8) & 0xFF, HEX);
do {
Serial1.write(256);
Serial1.write(sp & 0xFF);
Serial1.write((sp >> 8) & 0xFF);
Serial1.write(sp & 0xFF);
Serial1.write((sp >> 8) & 0xFF);
Serial1.write(85);
//
Serial2.write(256);
Serial2.write(sp & 0xFF);
Serial2.write((sp >> 8) & 0xFF);
Serial2.write(sp & 0xFF);
Serial2.write((sp >> 8) & 0xFF);
Serial2.write(85);

delayMicroseconds(300);
} while (!Serial.available());
c = Serial.read();
}


stevestrong
Mon Jun 12, 2017 7:37 pm
It seems so.

But I think you have an unexpected behavior when you write
Serial1.write(256);


RogerClark
Mon Jun 12, 2017 9:41 pm
If you have a USB to serial adaptor, you could just connect its Rx pin to the TX data from the STM32.

In the longer term, if you are interested in doing other microcontroller projects, It would be worth spending $10 on one of those USB logic analysers that can be found on eBay and AliExpress etc.

e.g something like this

http://www.ebay.com.au/itm/USB-24MHz-8C … Sw3YJZOjoW

Or if you have more money to spend, you can get a 16 Channel ( and more importantly, 100Mhz) usb logic analyser for around $50.

I also have a digital storage scope (100MHz) which has a 16 channel logic analyser, but I now never use the logic analyser part of the scope as the USB analysers are much better as they are much easier to use.


xplosion
Mon Jun 12, 2017 10:57 pm
thanks Roger. im going to order a USB logic analyser.

I connect a serial adaptor to RX & TX pin and the Serial.begin(115200) working and respond when i Println but the Serial1.begin(26300) or Serial2.begin(26300) is not respond or working


RogerClark
Tue Jun 13, 2017 12:15 am
xplosion wrote:thanks Roger. im going to order a USB logic analyser.

I connect a serial adaptor to RX & TX pin and the Serial.begin(115200) working and respond when i Println but the Serial1.begin(26300) or Serial2.begin(26300) is not respond or working


xplosion
Tue Jun 13, 2017 12:47 am
thank you very much for your help. im going to try doing what you said and i will get back to you.

Leave a Reply

Your email address will not be published. Required fields are marked *