Hmm normally i manage the whole explanation in the topic name, anyway:
I have an ATtiny2313a that i want to use as part of an alarm clock. If i would have had a 4313 or some of the newer series versions the issue i am facing might not have come up, but when i bought the 2313 i had no idea what i wanted to use it for.
So just to make it clear; I want to use the UART to receive data from an ESP-01 with updates on the correct time. It is going to be a one way transmission.
Just to explain why not to use an ESP-01 (or a nodeMCU) for that matter to do the whole thing. The clock should have a backup battery that powers the clock and makes the alarm go off regardless of mains power, and should be fitted with a rechargeable battery (preferably LIPO)
ATtiny chips or low on power use and run on a wide voltage range (If i want to use a LIPO an ATtiny is the obvious choice)
Anyway, the disadvantage of an ATtiny (and this one in particular) is the small Flash and RAM size, and though the requirements are small, it is going to be a challenge.
Now the issue gets aggravated by the use of hwSerial
#define PIN 4
#define DEL 500
void setup() {
//Serial.begin(9600);
pinMode(PIN, OUTPUT);
}
void loop() {
digitalWrite(PIN, HIGH);
delay(DEL);
digitalWrite(PIN, LOW);
delay(DEL);
}
If i compile the using Spence Konde 's Library i get
Sketch uses 450 bytes (21%) of program storage space. Maximum is 2048 bytes.
Global variables use 9 bytes (7%) of dynamic memory, leaving 119 bytes for local variables. Maximum is 128 bytes.
but if i un-comment the Serial.begin()
Sketch uses 1256 bytes (61%) of program storage space. Maximum is 2048 bytes.
Global variables use 92 bytes (71%) of dynamic memory, leaving 36 bytes for local variables. Maximum is 128 bytes.
Now i am only planning to do RX, and normally speaking i just want to set the BAUD-rate once, and do a check on available() and read().
I really don't need all of the rest, i don't need the write, the TX buffer, peek etc. and looking through the core files related to it, i realized that since i am planning to receive messages only 4 bytes long, i might not need the RX buffer to be any longer than that. And rather than discarding the byte in case of a full buffer i would want to just overwrite the oldest byte. Anyway, rather than modifying the library i figure it might be better to just make the reading of the UART a part of the main sketch. But now i am not really sure what i do and what i don't need, and i am talking about the parts that i find in the HardwareSerial.h .cpp & private.h
For instance :
Do i need all these ?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <util/atomic.h>
#include "Arduino.h"
I figure i'll need these :
int HardwareSerial::available(void)
{
return ((unsigned int)(SERIAL_RX_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail)) % SERIAL_RX_BUFFER_SIZE;
}
int HardwareSerial::read(void)
{
// if the head isn't ahead of the tail, we don't have any characters
if (_rx_buffer_head == _rx_buffer_tail) {
return -1;
} else {
unsigned char c = _rx_buffer[_rx_buffer_tail];
_rx_buffer_tail = (rx_buffer_index_t)(_rx_buffer_tail + 1) % SERIAL_RX_BUFFER_SIZE;
return c;
}
}
and i need to set up the UART at a fixed BAUD-rate manually and i've never done that before.
Maybe i am overcomplicating things by using the UART, it's 4 bytes once a minute or so (once an hour would also be ok) But it reliability is a focus point in this project.