necroware / ps2-serial-mouse-adapter

PS/2 to serial port mouse adapter
GNU General Public License v3.0
72 stars 15 forks source link

Use software serial instead #1

Open cocus opened 3 years ago

cocus commented 3 years ago

Hi! I found your project thru your Youtube channel. I figured that you want to improve it further, and I think you can use the SoftwareSerial provided by the Arduino framework so you don't have to use a hardcoded way to send bytes; and you should also gain being "agnostic" of the CPU clock, since that should be handled by that class. I could try to make the changes, but I don't have the appropriate hardware to even test this.

necroware commented 3 years ago

Hi! My first implementation was around SoftwareSerial, but for some reason, it didn't work back then. I don't quite remember why, because it was more, then 3 years ago. I think it was something with format, SoftwareSerial sent using 8N1, but the old controllers need 8N2. You can configure it in normal Serial, but not in SoftwareSerial.... May be I have to give it another try, because I'm not 100% sure if it was it...

cocus commented 3 years ago

Oh I see. I checked again, and seems there's no support for the 8N2 on the software serial :( there are other libraries that should support it, but since you already have implemented and adding a third party lib is usually a pain, I think this soulution should suffice. Probably a small change on the baud rate calculation should be added so the F_CPU is considered; but that's entirely optional. Thanks!

necroware commented 3 years ago

Thank you very much for your suggestion. My code at that point is really very simple, may be too simple :D I already refactored it on a local branch, and moved serial code from the main file, but I still have to merge it into the master. I will consider to use F_CPU for the baud rate calculation. If you have an example, how you'd like to see it, just post a comment here and I'll test it and integrate it into the code, if it works.

cocus commented 3 years ago

So, having a look at the original SoftwareSerial.cpp from Arduino, I can see that they calculate the bit delay with this code:

  // Precalculate the various delays, in number of 4-cycle delays
  uint16_t bit_delay = (F_CPU / speed) / 4;

  // 12 (gcc 4.8.2) or 13 (gcc 4.3.2) cycles from start bit to first bit,
  // 15 (gcc 4.8.2) or 16 (gcc 4.3.2) cycles between bits,
  // 12 (gcc 4.8.2) or 14 (gcc 4.3.2) cycles from last bit to stop bit
  // These are all close enough to just use 15 cycles, since the inter-bit
  // timings are the most critical (deviations stack 8 times)
  _tx_delay = subtract_cap(bit_delay, 15 / 4);

subtract_cap is defined at https://github.com/arduino/ArduinoCore-avr/blob/9f8d27f09f3bbd1da1374b5549a82bda55d45d44/libraries/SoftwareSerial/src/SoftwareSerial.cpp#L293

Then, in the bit send loop, they call _delay_loop_2 which I can only assume it is defined on #include <util/delay_basic.h>.

necroware commented 3 years ago

Thank you, I'll take a closer look at it in the next days and test it.