commit 2d5fb588a3c351ce880225770b621bdad2453e55
parent 1aed5d372100994132866975218d5dd26e6c86df
Author: Tellenbach Reto <tellr1@bfh.ch>
Date: Mon, 8 Jun 2026 23:31:58 +0200
[ref] UART: recovered main from dangling branch
Diffstat:
1 file changed, 49 insertions(+), 7 deletions(-)
diff --git a/thinker/CoinAcceptor/src/main.c b/thinker/CoinAcceptor/src/main.c
@@ -1,15 +1,16 @@
-/* Thinker CoinAcceptor */
-/* Use GPIO to enable coin insert*/
+#ifndef _GNU_SOURCE
#define _GNU_SOURCE
+#endif
#include <gpiod.h>
-#include <stdio.h>
-#include <unistd.h>
+#include <termios.h>
+#include <fcntl.h>
#include <stdlib.h>
-#define CHIPPATH "/dev/gpiochip0"
+const char* CHIPPATH = "/dev/gpiochip0";
const unsigned int GPIO16_OFFSET = 16;
+const char* UART0 = "/dev/ttyS0";
//CLI Controlls
enum command{
@@ -28,7 +29,35 @@ static char keyInput = down;
struct gpiod_line_settings* settings;
int ret;
- //Config
+ int uart0_filestream = -1;
+ int rx_length = -1;
+ int lword = 0;
+
+ // Config UART0
+ uart0_filestream = open(UART0, O_RDONLY | O_NOCTTY | O_NDELAY);
+ // O_RDONLY: read only
+ // O_NOCTTY: The port never becomes the controlling terminal of the process
+ // O_NDELAY: Use non-blocking I/O
+ if (uart0_filestream == -1)
+ {
+ perror("Fehler - UART konnte nicht geƶffnet werden\n");
+ }
+
+
+ struct termios options;
+ tcgetattr(uart0_filestream, &options);
+ options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // RAW input
+ options.c_cflag |= PARENB; // Parity bit enabled
+ options.c_iflag |= (INPCK | ISTRIP); // check and strip paryty bit
+ options.c_cflag |= B9600; // Baud rate 9600
+ options.c_cflag |= CS8; // Size: 8 Bits
+ options.c_cflag &= (CREAD | CLOCAL); // These will ensure that your program does not become the 'owner' of the port subject to sporatic job control and hangup signals, and also that the serial interface driver will read incoming data bytes.
+ options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow controll
+
+ tcsetattr(uart0_filestream, TCSANOW, &options); //Flush buffers and apply settings
+
+
+ //Config GPIO
settings = gpiod_make_settings(GPIOD_LINE_DIRECTION_OUTPUT,GPIOD_LINE_BIAS_PULL_UP,GPIOD_LINE_DRIVE_OPEN_DRAIN,1);
if (!settings)
{
@@ -49,6 +78,18 @@ static char keyInput = down;
if(keyInput == up)
{
printf("ACTIVE\n");
+
+ //Byte lesen
+ if(uart0_filestream != -1)
+ {
+ char * byte;
+ rx_length = read(uart0_filestream,byte,1);
+ if(rx_length>0)
+ {
+ print("recived byte: %c \n", byte);
+ }
+ };
+
ret = gpiod_line_request_set_value(request_line16,GPIO16_OFFSET, GPIOD_LINE_VALUE_ACTIVE);
}
@@ -65,5 +106,6 @@ static char keyInput = down;
}
gpiod_line_request_release(request_line16);
+ close(uart0_filestream);
return 0;
- }
+}