How to bypass Serial port libraries in Arduino - arduino

I want to write my own code for managing an Arduino serial port (9bit serial) so I need to bypass the preprogrammed arduino ISR vector for Serial1 TX. I can do that by modifying HardwareSerial1.cpp but I dislike the fact that my code becomes non portable and a potential victim of library updates.
Do you know how to program a different ISR vector without messing into library code? I tried this in my own code to no avail:
//bypass arduino library for serial 1 and use this ISR instead
ISR(UART1_UDRE_vect)
{
9bitSerial.interrupt();
}
The following error is reported, which seems to confirm the approach doesn't work:
ISR.cpp:11:25: error: expected unqualified-id before numeric constant
ISR.cpp:in expansion of macro 'UART1_UDRE_vect'
Any clues?

The safest way, IMHO, is to copy the entire HardwareSerial class to a differently-named class. Then proceed to make your modifications. This is exactly what I did for NeoHWSerial and NeoICSerial: they are copied and renamed from HardwareSerial and AltSoftSerial.
However, all uses of Serialx (instances of HardwareSerial) in a build must be replaced with instances of your class. You won't be able to mix instances of HardwareSerial with your class (duplicate ISR errors). But that's no big deal if your class only adds behavior. Other instances that don't use 9 bits should not be affected. If you don't mind, I'd be interested in folding those changes into NeoHWSerial. I've been occasionally looking at 9-bit serial for NeoSWSerial, too.

Related

What does this common SPI function do?

I see this function in Arduino scripts using SPI: ISR (SPI_STC_vect). I am familiar with C but this doesn't seem to follow - it looks like a function though there is no return type definition, and the argument doesn't make sense. I am new to SPI and cannot find much/any documentation online. Other functions which run during an interrupt are called using attachInterrupt, however this doesn't seem to be called anywhere.
ISR() is a macro that introduces an interrupt handler.
SPI_STC_vect is a Serial Transfer Complete interrupt vector.
See here for complete documentation, including the list of available interrupt vectors.

Why do we need to select a board to work with arduino IDE

I always wondered how this works . In arduino IDE we have option to select different boards because the code upload i guess is different ? Also there is possibility to even add board manager url to add other boards.
Why uploading the code is so different from board to board , and what exactly differs there ? The code is sent to via serial connection and i understand that a board might have different chip for handling USB to serial but ..
Can someone explain as clear as possible , how the code is uploaded to a board and why there is no generic way , why so many configurations.
Thanks and sorry if is a dumb question.
One of the important things that selecting a board does is, as you say, inform the IDE of how to load code onto the board. Perhaps more importantly, the board definitions give you the logical mapping of the board's hardware to the code constructs you are using to program against. For example, have you ever wondered what happens when you use a constant like PIN_13, or how that constant maps to a physical pin on your board, which has a trace to some contact on the microcontroller chip itself?
There is a lot of other code you don't see, much of it in the board definitions (and some in the Arduino core) which lets you work with relatively direct concepts (like pin numbers and modes) in the code that you write or edit.

A way to change mcu program from the outside

We need to change a controller code from the out side as they do with industrial MCU .
So that you have an mcu,with a program on it, and someone can program some "words" to it, that will determine how it works.
So for example you can program an mcu -not with a programer but with some inputs from serial, to do some simple things such as:
if input A==1
b=1
I wonder if there is a smart way to do that with simple software on the mcu, that it has many #defines for various commands, and it perform them according to values it gets from the outside (and saved for the rest of the program).
I wonder if the industrial programers are using that method, or that every programing of a user is actually load a code(.hex) to the chip(with internal programer ) .
I prefer the simplest way(i wonder if its by pre defined software)
A couple of options come to mind so hopefully this answers your question. It sounds like the simplest version of your question is "How do I change the behavior of the MCU without an actual MCU programmer?" A couple of options come to mind.
1) Depending on the MCU you can have a bootloader that is essentially a small piece of code programmed in the MCU by a programmer that has the ability to reprogram other parts of the MCU. This doesn't require a programmer but involves some other form of letting the bootloader know what the new code is (USB, Serial, SD Card, etc). This will only work if the MCU has the ability to self flash.
2) Again, depending on MCU and scenario you could program a generic set of rules that carry out functionality based on the inputs given to the MCU. This could be in the form of IO pins, EEPROM, or a domain-specific script on an SD card that the MCU can read and interpret at runtime.
Both options depend on the MCU you are using and what hardware capabilities you have at your disposal. But you certainly have options other than reprogramming the end hardware with an actual programmer every time you want to make a change. Hopefully that helps.

Joystick USB definition for use with Arduino

I'm trying to write the control code for a custom joystick, and I cannot find any reference to what needs to be implemented to be recognized by a computer as a joystick.
I can see what things I need to have by referencing various game library APIs for using a joystick.
For example, pygame has the following methods,
init
quit
get_init
get_name
get_id
get_numaxes
get_numbuttons
etc.
My next stop is to read through the kernel for Linux's HID and joystick controlers... But this seems like a really round about way of finding this information.
I haven't worked with it, but it looks like there is some interesting information in Arduino Uno Joystick HID firmware and Arduino Uno Big Joystick HID firmware. There is also UnoJoy - I'd try that first (assuming you have an Arduino Uno or Leonardo).

Communicating with the Pic 16F913

Here is my issue, it appears that all the communication lines for the PIC 16F913 reside on the same set of pins, this is convenient in that I don't have to sacrifice GPIO pins just to do comms, however the problem I'm having now is if I'm using the SPI on the chip, how can I send information to the RS232?
The reason this issue came up, is that I just bought a CAN bus chip that communicates over SPI, and I would really like to see the data on RS232, so I can see messages. (I really don't know much about CAN yet, so who knows if this even makes sense yet).
Here are the options I see, and maybe someone else has better ideas that I'm just simply missing.
Somehow setup a time scheme that will switch between SPI and RS232 every time I get data,
-- This doesn't seem hard and should work, but supposing I don't want to miss a message, what if a message is written while I'm writing to RS232, is it possible I'll miss it?
2.. I can always use SPI, but then build my own comm bus over 8 of the GPIO lines, to another PIC 16F913, using only the GPIO lines and then since the RS232 lines are free on the second PIC I can simply read the data and spit it out.
-- This one is doable but now we're wasting 2 chips, AND all the GPIO lines,
There has to be a better way. Or is there?
Any help is greatly appreciated.
Update: I would like to clarify, obviously one solution is using a completely different chip (which may in fact be what I end up doing, if I can get the 18F programmed), however, I'm interested in worst case scenario, in which I am limited in resources and only have some 913's, is the way described above the only way to do it with this chip, or is there a better way?
You could do a software implementation of the SPI bus - it's easier to do than the UART because the timing isn't critical and you are in control of it.
Most CAN chips have a few receive buffers so if you're busy doing something with the UART then the messages will be buffered inside the CAN chip. But... you will need to make sure that you can get the messages out of the CAN chip fast/often enough so you don't lose some.
You would probably have to either use an interrupt for the UART Tx process - so that you can be receiving CAN messages while you're sending data on the UART.
If you're only interested in certain messages most CAN chips have filters - this makes it easy to only receive the messages you're interested in, usually dropping the number of packets/second dramatically.
You can use a software implementation of a RS232 port. For example the c compiler from http://mikroe.com comes with such a library. But it shouldn't be too hard to shift bits to an output pin, rs232 is a fairly simple protocol.
I strongly recommend that you change the MCU with PIC16F1933.
It is newer
It is cheaper
With EUART and MSSP moduls.
The MCU arhitecture is impruved.
The PIC16F913 MCU will become obsolete in next few years!

Resources