How to interface Xbee with FPGA using SPI?

I am trying to connect an Xbee S6 to a FPGA using SPI. I found some code online to read in the MISO and convert to an 8bit output register. I have it working in the simulation but I cannot get the correct output when I try to implement it. I connected dout from the Xbee to ground to initiate SPI mode, but i’m not sure how to confirm this. I have my clock set to 20 Mhz and the clock divider sets the SPI clock to under 5Mhz. Also, if I am just using one slave, can my select (ss) be held to zero?

I have a remote PC with HyperTerminal to send keyboard characters to the FPGA. I initially want to relay these hex keyboard codes to the LED’s on the FPGA and later use these codes as input for motor control. The keyboard will be used as input for all of the motors used on a rover.

Here is the code I’ve been working on. I appreciate any assistance.

*I added MOSI section in the code and set tdat to FF
**LED outputs for rdata just switch between FF to 00 depending on the keyboard key pressed.

module SPI_test(
    input clk,
    input attn,
    input miso,
    input [7:0] tdat,
    output reg ss,
    output reg sck,
    output reg mosi,
    output reg [7:0] rdata,
    output ledtest, 
    output reg ledtest2
);

assign ledtest = ~attn;
//assign ledtest2 = ~ss;

parameter idle = 2'b00;
parameter send = 2'b10;
parameter finish = 2'b11;

reg [1:0] cur;
reg [1:0] nxt;

reg [7:0] rreg, treg;
reg [3:0] nbit;
reg [4:0] mid;
reg [4:0] cnt;
reg shift;
reg clr;

reg [1:0] cdiv = 2'b00;

//FSM i/o
always @ (attn or cur or nbit) begin
    nxt <= cur;
    clr <= 0;
    shift <= 0;
    //ss <= 0;
    case (cur)
        idle: begin
            if (attn==0) begin
                 case (cdiv)
                    2'b00: mid <= 2;
                    2'b01: mid <= 4;
                    2'b10: mid <= 8;
                    2'b11: mid <= 16;
                 endcase
                shift <= 1;
                nxt <= send;                
            end    
        end //idle
        send: begin
            ss <= 0;
            if (nbit != 9) 
                shift <= 1;
            else begin
                rdata <= rreg;
                nxt <= finish;
            end
        end //send
        finish: begin
            shift <= 0;
            ss <= 1;
            clr <= 1;
            nxt <= idle;
        end
        default: nxt <= finish;
    endcase
end //always

//state transitions
always @ (negedge clk) begin
    cur <= nxt;
end

//Setup clk for read miso
always @ (negedge clk or posedge clr) begin
    if (clr == 1) begin
        cnt <= 0;
        sck <= 1;
    end
    else begin
        if (shift == 1) begin
            cnt <= cnt + 1;
            if (cnt == mid) begin
                sck <= ~sck;
                cnt <= 0;
            end //mid
        end //shift
    end //else
end //always

//read miso
always @ (posedge sck or posedge clr) begin
   if (clr == 1) begin
        nbit <= 0;
        rreg <= 8'hFF;
    end
    else begin
        rreg <= {rreg[6:0],miso};
        nbit <= nbit + 1;
    end
end //always    

always@(posedge sck or posedge clr) begin
 if(clr==1) begin
	  treg <= 8'hFF;  
      mosi <= 1;  
  end  
 else begin
		if(nbit==0) begin //load data into TREG
			treg <= tdat; 
            mosi <= treg[7];
		end //nbit_if
		else begin
            treg <= {treg[6:0],1'b1}; 
            mosi <= treg[7];
		end //else
 end //rst
end //always
endmodule


No you do need all of the lines. You also need to trasnfer FF’s in order to receve data. No you als oneed to have your Slave select set so that your device is the Master.

I added the MOSI line to send FF but still no luck. The FPGA is the master. Can I just keep SS low at all times if I’m just using one slave (xbee)? I have the rdata assigned to the led’s on the board, but it just changes from FF to 00 when something is sent to the Xbee

No you do need to use slave select. You also need to configure the XBee for SPI via its AT commands.

How do I set it up using the AT commands? I am using the Xbee S6B. Do I have to do something in addition to holding Dout low at reset?

http://ftp1.digi.com/support/documentation/90002180_K.pdf

Page 29 details the SPI

Try pages 28 - 30,

Can you explain how the AT commands are used? Is this done in the code or the hyperterminal?

You can use either XCTU or Hyper terminal to issue the AT commands needed to disable the UART and enable the SPI port.

Ok, so to enable the SPI ports, do I just send +++D0 D1 D2…ect for each of the pins?

May I suggest reading over Enabling SPI port section within the manual? It walks you through the process.

Yes, I have read that. I am asking for help understanding it

If you use the Modem configuration tab of XCTU, you would set
DIO7/nCTS 0
DIO6/nRTS 0
P3 0
P4 0
P2 1
P5 1
P6 1
P7 1
P8 1
P9 1
D1 1
D2 1
D3 1
D4 1

At the end of all of this you would use the Write function or the pencil on each command.

Ok, I got that, except I didnt see P5-P9. The LED’s only switch between FF, 01011100 (5C), and 01010100 (54) on random keys. It doesnt seem to be picking out the hex codes for the keys