A test program that writes green/red/blue to ws2812b at 4MHz

Former-commit-id: 2fa36165086f5bef99eaca387f6abeed8c4e683c
This commit is contained in:
T. van der Zwan 2013-12-23 11:55:38 +00:00
parent 3d3c8c93b8
commit 7f3fbae314
2 changed files with 241 additions and 0 deletions

View File

@ -54,3 +54,6 @@ add_executable(test_rs232highspeed
../libsrc/leddevice/LedRs232Device.cpp)
target_link_libraries(test_rs232highspeed
serialport)
add_executable(test_uartHighSpeed TestUartHighSpeed.cpp)
target_link_libraries(test_uartHighSpeed ${QT_LIBRARIES})

238
test/TestUartHighSpeed.cpp Normal file
View File

@ -0,0 +1,238 @@
#include <iostream>
#include <stdio.h>
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <termios.h> //Used for UART
#include <sys/ioctl.h>
#include </usr/include/linux/tty_flags.h>
#include <csignal>
#include <cstdint>
#include <bitset>
#include <QElapsedTimer>
struct serial_struct {
int type;
int line;
unsigned int port;
int irq;
int flags;
int xmit_fifo_size;
int custom_divisor;
int baud_base;
unsigned short close_delay;
char io_type;
char reserved_char[1];
int hub6;
unsigned short closing_wait; /* time to wait before closing */
unsigned short closing_wait2; /* no longer used... */
unsigned char *iomem_base;
unsigned short iomem_reg_shift;
unsigned int port_high;
unsigned long iomap_base; /* cookie passed into ioremap */
};
static volatile bool _running;
void signal_handler(int signum)
{
_running = false;
}
int main()
{
_running = true;
signal(SIGTERM, &signal_handler);
//-------------------------
//----- SETUP USART 0 -----
//-------------------------
//At bootup, pins 8 and 10 are already set to UART0_TXD, UART0_RXD (ie the alt0 function) respectively
int uart0_filestream = -1;
//OPEN THE UART
//The flags (defined in fcntl.h):
// Access modes (use 1 of these):
// O_RDONLY - Open for reading only.
// O_RDWR - Open for reading and writing.
// O_WRONLY - Open for writing only.
//
// O_NDELAY / O_NONBLOCK (same function) - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status
// if there is no input immediately available (instead of blocking). Likewise, write requests can also return
// immediately with a failure status if the output can't be written immediately.
//
// O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.
uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY); //Open in non blocking read/write mode
if (uart0_filestream == -1)
{
//ERROR - CAN'T OPEN SERIAL PORT
printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
}
//CONFIGURE THE UART
//The flags (defined in /usr/include/termios.h - see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html):
// Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000
// CSIZE:- CS5, CS6, CS7, CS8
// CLOCAL - Ignore modem status lines
// CREAD - Enable receiver
// IGNPAR = Ignore characters with parity errors
// ICRNL - Map CR to NL on input (Use for ASCII comms where you want to auto correct end of line characters - don't use for bianry comms!)
// PARENB - Parity enable
// PARODD - Odd parity (else even)
struct termios options;
tcgetattr(uart0_filestream, &options);
options.c_cflag = B4000000 | CS8 | CLOCAL | CREAD; //<Set baud rate
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(uart0_filestream, TCIFLUSH);
std::cout << "options.c_cflag = " << options.c_cflag << std::endl;
std::cout << "options.c_iflag = " << options.c_iflag << std::endl;
std::cout << "options.c_oflag = " << options.c_oflag << std::endl;
std::cout << "options.c_lflag = " << options.c_lflag << std::endl;
tcsetattr(uart0_filestream, TCSANOW, &options);
// Let's verify configured options
tcgetattr(uart0_filestream, &options);
std::cout << "options.c_cflag = " << options.c_cflag << std::endl;
std::cout << "options.c_iflag = " << options.c_iflag << std::endl;
std::cout << "options.c_oflag = " << options.c_oflag << std::endl;
std::cout << "options.c_lflag = " << options.c_lflag << std::endl;
// {
// struct serial_struct ser;
// if (-1 == ioctl(uart0_filestream, TIOCGSERIAL, &ser))
// {
// std::cerr << "Failed to obtian 'serial_struct' for setting custom baudrate" << std::endl;
// }
// std::cout << "Current divisor: " << ser.custom_divisor << " ( = " << ser.baud_base << " / 4000000" << std::endl;
// // set custom divisor
// ser.custom_divisor = ser.baud_base / 8000000;
// // update flags
// ser.flags &= ~ASYNC_SPD_MASK;
// ser.flags |= ASYNC_SPD_CUST;
// std::cout << "Current divisor: " << ser.custom_divisor << " ( = " << ser.baud_base << " / 8000000" << std::endl;
// if (-1 == ioctl(uart0_filestream, TIOCSSERIAL, &ser))
// {
// std::cerr << "Failed to configure 'serial_struct' for setting custom baudrate" << std::endl;
// }
// }
if (uart0_filestream < 0)
{
std::cerr << "Opening the device has failed" << std::endl;
return -1;
}
//----- TX BYTES -----
uint8_t tx_buffer[3*3*8*4];
uint8_t *p_tx_buffer;
p_tx_buffer = &tx_buffer[0];
// for (int i=0; i<3; ++i)
// {
// Writing 0xFF, 0x00, 0x00
// *p_tx_buffer++ = 0x8C;
// *p_tx_buffer++ = 0x8C;
// *p_tx_buffer++ = 0x8C;
// *p_tx_buffer++ = 0x8C;
*p_tx_buffer++ = 0x8C;
*p_tx_buffer++ = 0x8C;
*p_tx_buffer++ = 0x8C;
*p_tx_buffer++ = 0x8C;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0x8C;
*p_tx_buffer++ = 0x8C;
*p_tx_buffer++ = 0x8C;
*p_tx_buffer++ = 0x8C;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0xCE;
*p_tx_buffer++ = 0x8C;
*p_tx_buffer++ = 0x8C;
*p_tx_buffer++ = 0x8C;
*p_tx_buffer++ = 0x8C;
// }
std::cout << "Binary stream: [";
for (unsigned char* txIt=&(tx_buffer[0]); txIt!=p_tx_buffer; ++txIt)
{
std::cout << " 1 " << (std::bitset<8>) (*txIt) << " 0 ";
}
std::cout << "]" << std::endl;
std::cout << "Type 'c' to continue, 'q' or 'x' to quit: ";
while (_running)
{
char c = getchar();
if (c == 'q' || c == 'x')
{
break;
}
if (c != 'c')
{
continue;
}
int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0])); //Filestream, bytes to write, number of bytes to write
if (count < 0)
{
std::cerr << "UART TX error" << std::endl;
//----- CLOSE THE UART -----
close(uart0_filestream);
return -1;
}
std::cout << "Writing " << count << " bytes to uart" << std::endl;
}
//----- CLOSE THE UART -----
close(uart0_filestream);
std::cout << "Program finished" << std::endl;
return 0;
}