moved some files

This commit is contained in:
2021-03-04 18:29:28 +01:00
parent 189c35eb8d
commit 93636426b6
219 changed files with 18872 additions and 874 deletions

View File

@ -0,0 +1,254 @@
/*
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
Copyright (C) 2012 GomSpace ApS (http://www.gomspace.com)
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <csp/drivers/usart.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <termios.h>
#include <fcntl.h>
#include <csp/csp.h>
#include <sys/time.h>
int fd;
usart_callback_t usart_callback = NULL;
static void *serial_rx_thread(void *vptr_args);
int getbaud(int ifd) {
struct termios termAttr;
int inputSpeed = -1;
speed_t baudRate;
tcgetattr(ifd, &termAttr);
/* Get the input speed. */
baudRate = cfgetispeed(&termAttr);
switch (baudRate) {
case B0:
inputSpeed = 0;
break;
case B50:
inputSpeed = 50;
break;
case B110:
inputSpeed = 110;
break;
case B134:
inputSpeed = 134;
break;
case B150:
inputSpeed = 150;
break;
case B200:
inputSpeed = 200;
break;
case B300:
inputSpeed = 300;
break;
case B600:
inputSpeed = 600;
break;
case B1200:
inputSpeed = 1200;
break;
case B1800:
inputSpeed = 1800;
break;
case B2400:
inputSpeed = 2400;
break;
case B4800:
inputSpeed = 4800;
break;
case B9600:
inputSpeed = 9600;
break;
case B19200:
inputSpeed = 19200;
break;
case B38400:
inputSpeed = 38400;
break;
case B57600:
inputSpeed = 57600;
break;
case B115200:
inputSpeed = 115200;
break;
case B230400:
inputSpeed = 230400;
break;
#ifndef CSP_MACOSX
case B460800:
inputSpeed = 460800;
break;
case B500000:
inputSpeed = 500000;
break;
case B576000:
inputSpeed = 576000;
break;
case B921600:
inputSpeed = 921600;
break;
case B1000000:
inputSpeed = 1000000;
break;
case B1152000:
inputSpeed = 1152000;
break;
case B1500000:
inputSpeed = 1500000;
break;
case B2000000:
inputSpeed = 2000000;
break;
case B2500000:
inputSpeed = 2500000;
break;
case B3000000:
inputSpeed = 3000000;
break;
case B3500000:
inputSpeed = 3500000;
break;
case B4000000:
inputSpeed = 4000000;
break;
#endif
}
return inputSpeed;
}
void usart_init(struct usart_conf * conf) {
struct termios options;
pthread_t rx_thread;
fd = open(conf->device, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0) {
printf("Failed to open %s: %s\r\n", conf->device, strerror(errno));
return;
}
int brate = 0;
switch(conf->baudrate) {
case 4800: brate=B4800; break;
case 9600: brate=B9600; break;
case 19200: brate=B19200; break;
case 38400: brate=B38400; break;
case 57600: brate=B57600; break;
case 115200: brate=B115200; break;
case 230400: brate=B230400; break;
#ifndef CSP_MACOSX
case 460800: brate=B460800; break;
case 500000: brate=B500000; break;
case 576000: brate=B576000; break;
case 921600: brate=B921600; break;
case 1000000: brate=B1000000; break;
case 1152000: brate=B1152000; break;
case 1500000: brate=B1500000; break;
case 2000000: brate=B2000000; break;
case 2500000: brate=B2500000; break;
case 3000000: brate=B3000000; break;
case 3500000: brate=B3500000; break;
case 4000000: brate=B4000000; break;
#endif
default:
printf("Unsupported baudrate requested, defaulting to 500000, requested baudrate=%u\n", conf->baudrate);
brate=B500000;
break;
}
tcgetattr(fd, &options);
cfsetispeed(&options, brate);
cfsetospeed(&options, brate);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
options.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | PARMRK | INPCK | ISTRIP | IXON);
options.c_oflag &= ~(OCRNL | ONLCR | ONLRET | ONOCR | OFILL | OPOST);
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 1;
tcsetattr(fd, TCSANOW, &options);
if (tcgetattr(fd, &options) == -1)
perror("error setting options");
fcntl(fd, F_SETFL, 0);
/* Flush old transmissions */
if (tcflush(fd, TCIOFLUSH) == -1)
printf("Error flushing serial port - %s(%d).\n", strerror(errno), errno);
if (pthread_create(&rx_thread, NULL, serial_rx_thread, NULL) != 0)
return;
}
void usart_set_callback(usart_callback_t callback) {
usart_callback = callback;
}
void usart_insert(char c, void * pxTaskWoken) {
printf("%c", c);
}
void usart_putstr(char * buf, int len) {
if (write(fd, buf, len) != len)
return;
}
void usart_putc(char c) {
if (write(fd, &c, 1) != 1)
return;
}
char usart_getc(void) {
char c;
if (read(fd, &c, 1) != 1) return 0;
return c;
}
static void *serial_rx_thread(void *vptr_args) {
unsigned int length;
uint8_t * cbuf = malloc(100000);
// Receive loop
while (1) {
length = read(fd, cbuf, 300);
if (length <= 0) {
perror("Error: ");
exit(1);
}
if (usart_callback)
usart_callback(cbuf, length, NULL);
}
return NULL;
}

View File

@ -0,0 +1,230 @@
#include <stdio.h>
#include <Windows.h>
#include <process.h>
#include <csp/csp.h>
#include <csp/drivers/usart.h>
static HANDLE portHandle = INVALID_HANDLE_VALUE;
static HANDLE rxThread = INVALID_HANDLE_VALUE;
static CRITICAL_SECTION txSection;
static LONG isListening = 0;
static usart_callback_t usart_callback = NULL;
static void prvSendData(char *buf, int bufsz);
static int prvTryOpenPort(const char* intf);
static int prvTryConfigurePort(const struct usart_conf*);
static int prvTrySetPortTimeouts(void);
static const char* prvParityToStr(BYTE paritySetting);
#ifdef CSP_DEBUG
static void prvPrintError(void) {
char *messageBuffer = NULL;
DWORD errorCode = GetLastError();
DWORD formatMessageRet;
formatMessageRet = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
errorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(char*)&messageBuffer,
0,
NULL);
if( !formatMessageRet ) {
csp_log_error("FormatMessage error, code: %lu", GetLastError());
return;
}
csp_log_error("%s", messageBuffer);
LocalFree(messageBuffer);
}
#endif
#ifdef CSP_DEBUG
#define printError() prvPrintError()
#else
#define printError() do {} while(0)
#endif
static int prvTryOpenPort(const char *intf) {
portHandle = CreateFileA(
intf,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if( portHandle == INVALID_HANDLE_VALUE ) {
DWORD errorCode = GetLastError();
if( errorCode == ERROR_FILE_NOT_FOUND ) {
csp_log_error("Could not open serial port, because it didn't exist!");
}
else
csp_log_error("Failure opening serial port! Code: %lu", errorCode);
return 1;
}
return 0;
}
static int prvTryConfigurePort(const struct usart_conf * conf) {
DCB portSettings = {0};
portSettings.DCBlength = sizeof(DCB);
if(!GetCommState(portHandle, &portSettings) ) {
csp_log_error("Could not get default settings for open COM port! Code: %lu", GetLastError());
return -1;
}
portSettings.BaudRate = conf->baudrate;
portSettings.Parity = conf->paritysetting;
portSettings.StopBits = conf->stopbits;
portSettings.fParity = conf->checkparity;
portSettings.fBinary = TRUE;
portSettings.ByteSize = conf->databits;
if( !SetCommState(portHandle, &portSettings) ) {
csp_log_error("Error when setting COM port settings! Code:%lu", GetLastError());
return 1;
}
GetCommState(portHandle, &portSettings);
csp_log_info("Port: %s, Baudrate: %lu, Data bits: %d, Stop bits: %d, Parity: %s",
conf->device, conf->baudrate, conf->databits, conf->stopbits, prvParityToStr(conf->paritysetting));
return 0;
}
static const char* prvParityToStr(BYTE paritySetting) {
static const char *parityStr[] = {
"None",
"Odd",
"Even",
"N/A"
};
char const *resultStr = NULL;
switch(paritySetting) {
case NOPARITY:
resultStr = parityStr[0];
break;
case ODDPARITY:
resultStr = parityStr[1];
break;
case EVENPARITY:
resultStr = parityStr[2];
break;
default:
resultStr = parityStr[3];
};
return resultStr;
}
static int prvTrySetPortTimeouts(void) {
COMMTIMEOUTS timeouts = {0};
if( !GetCommTimeouts(portHandle, &timeouts) ) {
csp_log_error("Error gettings current timeout settings");
return 1;
}
timeouts.ReadIntervalTimeout = 5;
timeouts.ReadTotalTimeoutMultiplier = 1;
timeouts.ReadTotalTimeoutConstant = 5;
timeouts.WriteTotalTimeoutMultiplier = 1;
timeouts.WriteTotalTimeoutConstant = 5;
if(!SetCommTimeouts(portHandle, &timeouts)) {
csp_log_error("Error setting timeouts!");
return 1;
}
return 0;
}
unsigned WINAPI prvRxTask(void* params) {
DWORD bytesRead;
DWORD eventStatus;
uint8_t recvBuffer[24];
SetCommMask(portHandle, EV_RXCHAR);
while(isListening) {
WaitCommEvent(portHandle, &eventStatus, NULL);
if( !(eventStatus & EV_RXCHAR) ) {
continue;
}
if( !ReadFile(portHandle, recvBuffer, 24, &bytesRead, NULL)) {
csp_log_warn("Error receiving data! Code: %lu", GetLastError());
continue;
}
if( usart_callback != NULL )
usart_callback(recvBuffer, (size_t)bytesRead, NULL);
}
return 0;
}
static void prvSendData(char *buf, int bufsz) {
DWORD bytesTotal = 0;
DWORD bytesActual;
if( !WriteFile(portHandle, buf, bufsz-bytesTotal, &bytesActual, NULL) ) {
csp_log_error("Could not write data. Code: %lu", GetLastError());
return;
}
if( !FlushFileBuffers(portHandle) ) {
csp_log_warn("Could not flush write buffer. Code: %lu", GetLastError());
}
}
void usart_shutdown(void) {
InterlockedExchange(&isListening, 0);
CloseHandle(portHandle);
portHandle = INVALID_HANDLE_VALUE;
if( rxThread != INVALID_HANDLE_VALUE ) {
WaitForSingleObject(rxThread, INFINITE);
rxThread = INVALID_HANDLE_VALUE;
}
DeleteCriticalSection(&txSection);
}
void usart_listen(void) {
InterlockedExchange(&isListening, 1);
rxThread = (HANDLE)_beginthreadex(NULL, 0, &prvRxTask, NULL, 0, NULL);
}
void usart_putstr(char* buf, int bufsz) {
EnterCriticalSection(&txSection);
prvSendData(buf, bufsz);
LeaveCriticalSection(&txSection);
}
void usart_insert(char c, void *pxTaskWoken) {
/* redirect debug output to stdout */
printf("%c", c);
}
void usart_set_callback(usart_callback_t callback) {
usart_callback = callback;
}
void usart_init(struct usart_conf * conf) {
if( prvTryOpenPort(conf->device) ) {
printError();
return;
}
if( prvTryConfigurePort(conf) ) {
printError();
return;
}
if( prvTrySetPortTimeouts() ) {
printError();
return;
}
InitializeCriticalSection(&txSection);
/* Start receiver thread */
usart_listen();
}