Arduino-Pi USART interface

I need to interface an IDUINO MEGA to a Pi3 and also in a separate configuration a BLUE PILL to a Pi3. I shall refer to both the MEGA and the BLUE PILL as MICRO. I need to use serial UART on the Pi and UART on the MICRO. I need to be able to send raw binary data from the MICRO to the Pi. I understand that the Pi has two pins available for independent serial commas. I understand that the MICRO can be programmed to allocate UARTS to various pins. IT is completely unclear from my internet searches precisely how to do this??? That is determine which pins to choose and how to allocate them in the code and then set up UART and send raw data. I will need to be able to be doing other seriously high speed operations in the MICRO loop probably interrupt timed loop and so the serial port cannot sit there dedicated to the UART and nothing else.

This is not essential but very desirable to be able to send parity bits. I want to be able to send data with odd parity and depending on the data change over to even parity and back again without having a major code delay to do this. Similarly the Pi must be able to detect odd / even parity in its receive string and be able to recognize it in the received data.

I prefer to program in C not C++, but I will put up with what advise I am given.

What is the desirable method to interface Pi 3V3 tolerant logic to MICRO 5V0 logic?

Please help.

Regards

Hi Clem,

Lets break this problem down into parts. There is a bit too much going on here at once for me to get my head around. I’ll start with the parts that I know the answers to.

If you want to interface a 3V logic device to a 5V, you need a Logic Level Converter:
https://core-electronics.com.au/logic-level-converter-bi-directional.html
https://core-electronics.com.au/tutorials/how-to-use-logic-level-shifters-converters.html

To identify the UART pins you just need to use a combination of the digital output pins and RX/TX pins. You can find a pinout diagram of the Arduino Mega here:
https://www.element14.com/community/docs/DOC-87013/l/arduino-mega-2560-rev3-pinout-atmega2560-pin-mapping-eagle-files-schematics-and-more
Here is a great tutorial on setting up a UART connection with the Arduino MEGA that passes bytes like you want:

The UART pins on the Raspberry Pi are pins 8 and 10 as seen here:

I hope that gets you started! Let me know if you have further questions and we can work through them!

Thank you Stephen, you have answered the level conversion problem precisely.

I need to send 8 bit raw data - no ascii control character interpretation.

I want to find out if it is possible to use the parity bit such that the character is start bit, 8 data bits, parity bit, one stop bit.

I want to use the parity bit as a signal to indicate start of block of data.

That is first character would be even parity, then all other characters would be odd parity until start of another block.

Is this possible? I can arrange for an integer character to be set up so that the lower 8 bits b0 - b7 is the data and b8 is the parity bit. Can the MICRO code be arranged so that the parity is set according to this bit? I suspect that a UART register might have to be altered to change the parity setting??

Then in receive on the Pi. I would need the Pi to be able to read the parity bit as well as the data and the receive routine will need to be able to record the parity bit along with the data. I am sure that this is possibel.

I looked at the example of 4 port serial. This appears to use one serial port and multiplexes it over 4 possible ports.

Although there are 2 serial interrupt routines ???

In the MEGA I want to be able to use a different serial pins to those currently being used on the standard serial port which connects to my development system.

How do I tell the MEGA which pins to use as Tx, and Rx and how do I associate the pins with the second serial code??

Serial and Serial1 ?? I cannot understand where this is happening and how it is done or how it must be changed?

This is typical of C++, hide and obscure the details of the lower level and make it difficult to find for those not used to C++.

Regards’

Hi Clem,

USART is generally handled by hardware on the chip so customising the parity can be difficult, the best place to start for that is the datasheet for the chip itself. Though from memory USART has to communicate with chars and parity and lost packets are handled in hardware. The other thing with it being done in hardware is that it is generally not possible to change the USART pins.

You could implement a software USART (I found an example but not for the AVR) . In general for sending binary data across the USART I would encode the binary data as chars as the processor deals with memory a word at a time and the USART hardware deals with the overhead this will more than likely be the faster solution anyway.