Skip to content

Commit

Permalink
uart module works
Browse files Browse the repository at this point in the history
  • Loading branch information
oblaser committed May 12, 2022
1 parent dcf0ed8 commit de346ad
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 45 deletions.
20 changes: 10 additions & 10 deletions build/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# author Oliver Blaser
# date 04.05.2022
# date 12.05.2022
# copyright MIT - Copyright (c) 2022 Oliver Blaser

cmake_minimum_required(VERSION 3.13)
Expand All @@ -18,12 +18,12 @@ include_directories(../../include/)



add_executable(rpihal-system-test-gpio
../../test/system/gpio.cpp
../../src/gpio.c
)

add_executable(rpihal-system-test-uart
../../test/system/uart.cpp
../../src/uart.c
)
#add_executable(rpihal-system-test-gpio
#../../test/system/gpio.cpp
#../../src/gpio.c
#)
#
#add_executable(rpihal-system-test-uart
#../../test/system/uart.cpp
#../../src/uart.c
#)
18 changes: 13 additions & 5 deletions build/make/makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# author Oliver Blaser
# date 04.05.2022
# date 12.05.2022
# copyright MIT - Copyright (c) 2022 Oliver Blaser


Expand All @@ -10,8 +10,8 @@ LINK = g++
CFLAGS = -c -I../../include --std=c++17 -O3 -Wall -pedantic
LFLAGS = -O3 -Wall -pedantic

OBJS = main.o gpio.o
EXE = rpihal-system-test
OBJS = main-uart.o gpio.o uart.o
EXE = rpihal-system-test-uart

BUILDDATE = $(shell date +"%Y-%m-%d-%H-%M")

Expand All @@ -21,15 +21,23 @@ BUILDDATE = $(shell date +"%Y-%m-%d-%H-%M")
$(EXE): $(OBJS)
$(LINK) $(LFLAGS) -o $(EXE) $(OBJS)

main.o: ../../test/system/main.cpp ../../include/rpihal/gpio.h
$(CC) $(CFLAGS) ../../test/system/main.cpp
main-uart.o: ../../test/system/uart.cpp ../../include/rpihal/gpio.h ../../include/rpihal/uart.h
$(CC) $(CFLAGS) ../../test/system/uart.cpp -o main-uart.o

gpio.o: ../../src/gpio.c ../../include/rpihal/gpio.h
$(CC) $(CFLAGS) ../../src/gpio.c

uart.o: ../../src/uart.c ../../include/rpihal/uart.h
$(CC) $(CFLAGS) ../../src/uart.c

all: $(EXE)


run: $(EXE)
@echo ""
@echo "\033[38;5;27m--================# run #================--\033[39m"
./$(EXE)

clean:
rm $(OBJS)
rm $(EXE)
18 changes: 6 additions & 12 deletions include/rpihal/uart.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
author Oliver Blaser
date 11.05.2022
date 12.05.2022
copyright MIT - Copyright (c) 2022 Oliver Blaser
*/

Expand Down Expand Up @@ -71,25 +71,19 @@ int UART_open(UART_port_t* port, const char* name, int baud/*, parity, nStop*/);

int UART_close(UART_port_t* port);

int UART_read2(const UART_port_t* port, uint8_t* buffer, size_t bufferSize, size_t* nBytesRead);
int UART_read(const UART_port_t* port, uint8_t* buffer, size_t bufferSize, size_t* nBytesRead);

int UART_read(const UART_port_t* port, uint8_t* buffer, size_t bufferSize)
{ return UART_read2(port, buffer, bufferSize, NULL); }

int UART_readByte2(const UART_port_t* port, uint8_t* byte, size_t* nBytesRead)
{ return UART_read2(port, byte, 1, nBytesRead); }

int UART_readByte(const UART_port_t* port, uint8_t* byte)
{ return UART_readByte2(port, byte, NULL); }
inline int UART_readByte(const UART_port_t* port, uint8_t* byte, size_t* nBytesRead)
{ return UART_read(port, byte, 1, nBytesRead); }

int UART_write2(const UART_port_t* port, const uint8_t* data, size_t count, size_t* nBytesWritten);

int UART_write(const UART_port_t* port, const uint8_t* data, size_t count)
inline int UART_write(const UART_port_t* port, const uint8_t* data, size_t count)
{ return UART_write2(port, data, count, NULL); }

int UART_print2(const UART_port_t* port, const char* str, size_t* nBytesWritten);

int UART_print(const UART_port_t* port, const char* str)
inline int UART_print(const UART_port_t* port, const char* str)
{ return UART_print2(port, str, NULL); }

//! @return TRUE (`1`), FALSE (`0`) or error (`-1`) if __port__ is `NULL`
Expand Down
5 changes: 3 additions & 2 deletions src/gpio.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
author Oliver Blaser
date 11.05.2022
date 12.05.2022
copyright MIT - Copyright (c) 2022 Oliver Blaser
*/

Expand All @@ -12,6 +12,7 @@ copyright MIT - Copyright (c) 2022 Oliver Blaser
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h> // usleep()



Expand Down Expand Up @@ -167,7 +168,7 @@ int GPIO_initPin(int pin, const GPIO_init_t* initStruct)
}

//! @param pin BCM GPIO pin number
//! @return __0__ on success
//! @return __0__ LOW / __1__ HIGH / __-1__ error
int GPIO_readPin(int pin)
{
int r = 0;
Expand Down
16 changes: 5 additions & 11 deletions src/uart.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
author Oliver Blaser
date 11.05.2022
date 12.05.2022
copyright MIT - Copyright (c) 2022 Oliver Blaser
*/

Expand All @@ -21,17 +21,9 @@ Since the OS uses the BCM registers, we use the OS' library instead of registers
#include <termios.h>
#include <unistd.h>



// hack, if using VS or VS Code during development
#warning "TEST" // test
#ifdef _MSC_VER
#warning "_MSC_VER defined" // test
#ifndef RSIZE_MAX
#warning "RSIZE_MAX defined by rpihal"
#ifndef RSIZE_MAX // https://stackoverflow.com/a/33604977/16658255
#define RSIZE_MAX (SIZE_MAX >> 1)
#endif
#endif



Expand Down Expand Up @@ -59,6 +51,8 @@ int UART_open(UART_port_t* port, const char* name, int baud)
{
r = OPEN_E_OK;

port->name[0] = 0;

if(name)
{
const size_t nameLen = strlen(name);
Expand Down Expand Up @@ -166,7 +160,7 @@ int UART_close(UART_port_t* port)
return r;
}

int UART_read2(const UART_port_t* port, uint8_t* buffer, size_t bufferSize, size_t* nBytesRead)
int UART_read(const UART_port_t* port, uint8_t* buffer, size_t bufferSize, size_t* nBytesRead)
{
int r = -1;

Expand Down
Loading

0 comments on commit de346ad

Please sign in to comment.