|
| 1 | +#include "xparameters.h" |
| 2 | +#include "xgpio.h" |
| 3 | + |
| 4 | + |
| 5 | +#define AUX_ADDR 0x41200000 |
| 6 | +#define CLEAR_ADDR 0x41210000 |
| 7 | +#define DATA_IN_ADDR 0x41220000 |
| 8 | +#define DATA_OUT_ADDR 0x41230000 |
| 9 | +#define EN_ADDR 0x41240000 |
| 10 | + |
| 11 | +#define AUX_ID 0 |
| 12 | +#define CLEAR_ID 1 |
| 13 | +#define DIN_ID 2 |
| 14 | +#define DOUT_ID 3 |
| 15 | +#define EN_ID 4 |
| 16 | + |
| 17 | +#define TO_PL 0 |
| 18 | +#define TO_PS 1 |
| 19 | + |
| 20 | +// Shiftting |
| 21 | +#define _write 0 |
| 22 | +#define _load 1 |
| 23 | +#define _idx 2 |
| 24 | +#define _reg_select 5 |
| 25 | + |
| 26 | + |
| 27 | +#define set_write 1 |
| 28 | +#define set_load 2 |
| 29 | +#define set_matmul 0 |
| 30 | +#define set_read 3 |
| 31 | + |
| 32 | +#define write(data) XGpio_DiscreteWrite(&xDIN,1, data) |
| 33 | +#define set(aux) XGpio_DiscreteWrite(&xAux,1, aux) |
| 34 | +#define en(x) XGpio_DiscreteWrite(&xEN, 1, x) |
| 35 | +#define read() XGpio_DiscreteRead(&xDOUT,1) |
| 36 | +#define clean(x) XGpio_DiscreteWrite(&xClear, 1, x) |
| 37 | + |
| 38 | +#define aux_bit(mode_, reg_select_, idx_) ((reg_select_ << _reg_select) | (idx_ << _idx) | (mode_)) |
| 39 | + |
| 40 | +#define shutdown() en(0) |
| 41 | + |
| 42 | +XGpio xAux, xClear, xDIN, xDOUT, xEN; |
| 43 | + |
| 44 | + |
| 45 | +void mem_write(int data, int reg_select, int idx){ |
| 46 | + en(0); |
| 47 | + write(data); |
| 48 | + set(aux_bit(set_write, reg_select, idx)); |
| 49 | + en(1); |
| 50 | +} |
| 51 | + |
| 52 | +void load_regs(){ |
| 53 | + en(0); |
| 54 | + set(set_load); |
| 55 | + en(1); |
| 56 | +} |
| 57 | + |
| 58 | +void do_matmul(){ |
| 59 | + en(0); |
| 60 | + set(set_matmul); |
| 61 | + en(1); |
| 62 | +} |
| 63 | + |
| 64 | +volatile u32 mem_read(int reg_select, int idx){ |
| 65 | + en(0); |
| 66 | + set(aux_bit(set_read, reg_select,idx)); |
| 67 | + en(1); |
| 68 | + return read(); |
| 69 | +} |
| 70 | + |
| 71 | +u32 show_aux(int mode_, int reg_select_, int idx_){ |
| 72 | + return ((reg_select_ << _reg_select) | (idx_ << _idx) | (mode_)); |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +void LED_TEST(){ |
| 77 | + en(0); |
| 78 | + usleep(500000); |
| 79 | + set(set_read); |
| 80 | + usleep(500000); |
| 81 | + set(set_write); |
| 82 | + usleep(500000); |
| 83 | + set(set_load); |
| 84 | + usleep(500000); |
| 85 | + set(set_matmul); |
| 86 | +} |
| 87 | + |
| 88 | +int connectFPGA(){ |
| 89 | + // AXI GPIO SETUP |
| 90 | + if(XGpio_Initialize(&xAux, AUX_ID) != XST_SUCCESS){ |
| 91 | + printf( "aux init fail\n"); |
| 92 | + return 1; |
| 93 | + } |
| 94 | + |
| 95 | + if(XGpio_Initialize(&xClear, CLEAR_ID) != XST_SUCCESS){ |
| 96 | + printf("clear init fail\n"); |
| 97 | + return 1; |
| 98 | + } |
| 99 | + |
| 100 | + if(XGpio_Initialize(&xDIN, DIN_ID) != XST_SUCCESS){ |
| 101 | + printf("din init fail\n"); |
| 102 | + return 1; |
| 103 | + } |
| 104 | + |
| 105 | + if(XGpio_Initialize(&xDOUT, DOUT_ID) != XST_SUCCESS){ |
| 106 | + printf("dout init fail\n"); |
| 107 | + return 1; |
| 108 | + } |
| 109 | + |
| 110 | + if(XGpio_Initialize(&xEN, EN_ID) != XST_SUCCESS){ |
| 111 | + printf("en init fail\n"); |
| 112 | + return 1; |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + XGpio_SetDataDirection(&xAux, 1, TO_PL); |
| 117 | + XGpio_SetDataDirection(&xClear, 1, TO_PL); |
| 118 | + XGpio_SetDataDirection(&xDIN, 1, TO_PL); |
| 119 | + XGpio_SetDataDirection(&xDOUT, 1, TO_PS); |
| 120 | + XGpio_SetDataDirection(&xEN, 1, TO_PL); |
| 121 | + |
| 122 | + return 0; |
| 123 | +} |
0 commit comments