#define Taster 25
#define RXD2 16
#define TXD2 17
TaskHandle_t xBlinker = NULL;
TaskHandle_t xDebounce= NULL;
TaskHandle_t xButton= NULL;
TaskHandle_t xUART2= NULL;
void Blinker(void * args);
void Debounce(void * args);
void Button(void * args);
void UART2(void * args);
hw_timer_t *timerDebounce = NULL;
hw_timer_t *timerUARTtimeout = NULL;
char Inbyte;
int i =0;
void ARDUINO_ISR_ATTR UART_RX_IRQ()
{
uint16_t size = Serial2.available();
Serial.printf("Got %d bytes on Serial to read\n", size);
while(Serial2.available())
{
Inbyte = Serial2.read();
Serial.write(Inbyte);
Serial2.write(Inbyte);
}
Serial.printf("\nSerial data processed!\n");
}
void ARDUINO_ISR_ATTR onBlinker()
{
vTaskNotifyGiveFromISR(xBlinker,0);
}
void ARDUINO_ISR_ATTR onTimerDebounce()
{
vTaskNotifyGiveFromISR(xDebounce,0);
}
void ARDUINO_ISR_ATTR onButton()
{
vTaskNotifyGiveFromISR(xButton,0);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
Serial2.onReceive(UART_RX_IRQ);
Serial.println("Send data to UART2 in order to activate the RX callback");
hw_timer_t *timer = NULL;
// Set timer frequency to 1Mhz
timer = timerBegin(1000000);
timerDebounce = timerBegin(1000000);
// Attach onTimer function to our timer.
timerAttachInterrupt(timer, &onBlinker);
timerAttachInterrupt(timerDebounce, &onTimerDebounce);
// Set alarm to call onTimer function every second (value in microseconds).
// Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
timerAlarm(timer, 300000, true, 0);
timerAlarm(timerDebounce,10000, false, 0);//1 sekunde, oneshot
pinMode(2,OUTPUT);
pinMode(Taster, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(25), onButton, FALLING);
xTaskCreate(Blinker,"Blinker",1024,NULL,2,&xBlinker);
xTaskCreate(Debounce,"Debounce",1024,NULL,2,&xDebounce);
xTaskCreate(Button,"Button",1024,NULL,2,&xButton);
xTaskCreate(UART2,"UART2",1024,NULL,2,&xUART2);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
void Blinker(void * args)
{
while(1)
{
ulTaskNotifyTake(pdTRUE,portMAX_DELAY );
digitalWrite(2,!digitalRead(2));
}
}
void Debounce(void * args)
{int i;
while(1)
{
ulTaskNotifyTake(pdTRUE,portMAX_DELAY );
Serial.print(i);
Serial.print(" Status Taste: ");
Serial.println(digitalRead(Taster));
i++;
}
}
void Button(void * args)
{
while(1)
{ timerAlarm(timerDebounce,40000, false, 0);//40ms, oneshot
ulTaskNotifyTake(pdTRUE,portMAX_DELAY );
timerRestart(timerDebounce);
}
}
void UART2(void * args)
{
while(1)
{
vTaskDelay(1000);
}
}