#include #include #include #include #include #include #define REG_FREQ_7BIT 0x91A0 #define REG_BUSY_1BIT 0x91A4 #pragma inline void write_reg_32(volatile void *bar, int addr, uint32_t value) { *((volatile uint32_t *) (((char*)bar) + addr)) = value; } #pragma inline uint32_t read_reg_32(volatile void *bar, int addr) { return *((volatile uint32_t *) (((char*)bar) + addr)); } int main(int argc, char *argv[]) { pcilib_t *pci; pci = pcilib_open("/dev/fpga0", PCILIB_MODEL_DETECT); if (!pci) { printf("pcilib_open failed\n"); exit(-1); } volatile void *bar = pcilib_resolve_bar_address(pci, PCILIB_BAR0, 0); if (!bar) { printf("Failed to map PCI BAR for access\n"); pcilib_close(pci); exit(-1); } int socket; int ret; socket = create_inet_server_socket("0.0.0.0", "56000", LIBSOCKET_TCP, LIBSOCKET_IPv4, 0); if (socket < 0) { printf("Failed to create server listening socket\n"); exit(-1); } printf("Ethernet socket created on port 56000.\n"); while (1) { int peer_socket; char *peer_addr = calloc(16,1); printf("Waiting for incoming connection...\n"); peer_socket = accept_inet_stream_socket(socket, peer_addr, 16, 0, 0, 0, 0); if (peer_socket < 0) { printf("Failed to accpet client connection\n"); destroy_inet_socket(socket); exit(-1); } printf("Got connection from: %s\n", peer_addr); unsigned char *buf = calloc(1, 1); uint32_t val = 0; //We use 7-bit encoding. So any value larger than 127 is "invalid" //and can be used it for signalling. //We use 0xFF as the disconnect signal while (val != 0xFF) { //read() returns 0 in case of error ret = read (peer_socket, buf, 1); val = (*buf)&0xFF; if ((val == 0xFF) || (ret == 0)) { break; } printf("Received: %u\n", val); uint32_t busy = read_reg_32(bar, REG_BUSY_1BIT); if ((busy&0x1) == 0) { //Value needs to be stored in bits 1 - 7, because bit 0 is //used as a busy-bit uint32_t write_val = val << 1; write_val = write_val & 0xFF; write_reg_32(bar, REG_FREQ_7BIT, val); } } printf("Client disconnected.\n"); destroy_inet_socket(peer_socket); free(buf); free(peer_addr); } destroy_inet_socket(socket); printf("All went well\n"); pcilib_close(pci); return 0; }