You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.5 KiB
85 lines
2.5 KiB
/*
|
|
* Created on: Aug 5, 2019
|
|
* Author: Cristian Fatu
|
|
* This project implements a basic UART communication over UART on
|
|
* Petalinux, unsing Uart Lite linux driver. The hardware platform implements an
|
|
* UART Lite IP core and the device tree (in pl.dtsi) sets the appropriate
|
|
* parameters. After booting linux, the "/dev/ttyUL1" must be present.
|
|
*/
|
|
|
|
#include <fcntl.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <termios.h>
|
|
#include <unistd.h>
|
|
//
|
|
#include <map>
|
|
#include <set>
|
|
#include <string>
|
|
|
|
#include "uart.h"
|
|
|
|
#define MAX_RECV_SIZE 235
|
|
using namespace std;
|
|
map<string, uint32_t> g_baundmap = {
|
|
{"0", 0000000}, {"50", 0000001}, {"75", 0000002}, {"110", 0000003}, //
|
|
{"134", 0000004}, {"150", 0000005}, {"200", 0000006}, {"300", 0000007}, //
|
|
{"600", 0000010}, {"1200", 0000011}, {"1800", 0000012}, {"2400", 0000013}, //
|
|
{"4800", 0000014}, {"9600", 0000015}, {"19200", 0000016}, {"38400", 0000017}, //
|
|
{"57600", 0010001}, {"115200", 0010002}, {"230400", 0010003}, {"460800", 0010004}, //
|
|
{"500000", 0010005}, {"576000", 0010006}, {"921600", 0010007}, {"1000000", 0010010}, //
|
|
{"1152000", 0010011}, {"1500000", 0010012}, {"2000000", 0010013}, {"2500000", 0010014}, //
|
|
{"3000000", 0010015}, {"3500000", 0010016}, {"4000000", 0010017},
|
|
};
|
|
|
|
void openuart(struct UartDevice *device, char *devname, int rate) {
|
|
device->name = devname;
|
|
device->rate = rate;
|
|
|
|
printf("UART open %s\n", device->name);
|
|
int rc = uartStart(device, 0);
|
|
if (rc) {
|
|
printf("uartStart %s fail\n", device->name);
|
|
exit(-1);
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
struct UartDevice dev = {0};
|
|
if (argc != 3) {
|
|
printf("Usage: %s /dev/ttyUSB0 115200\n", argv[0]);
|
|
return -1;
|
|
}
|
|
printf("device name:%s\n", argv[1]);
|
|
printf("baundrate :%s\n", argv[2]);
|
|
|
|
auto baundrate_find_result = g_baundmap.find(argv[2]);
|
|
if (baundrate_find_result == g_baundmap.end()) {
|
|
printf("unsupport baundrate\n");
|
|
return -1;
|
|
};
|
|
|
|
openuart(&dev, argv[1], baundrate_find_result->second);
|
|
char buf[1024] = {0};
|
|
memset(buf, 0x55, 1024);
|
|
for (size_t i = 0;;) {
|
|
int ret1, ret2;
|
|
ret1 = uartReceive(&dev, buf, 2 /*接收的字节数2-1*/); // send the received text over UART
|
|
if (ret1 != 0) {
|
|
printf("0x%02x,", buf[0]);
|
|
i++;
|
|
if (i % 20 == 0) {
|
|
printf("\n");
|
|
}
|
|
}
|
|
fflush(stdout);
|
|
}
|
|
|
|
while (1) {
|
|
sleep(1);
|
|
}
|
|
|
|
return 0;
|
|
}
|