# How to interface Xbee with FPGA using SPI?

**URL:** <https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605>\
**Category:** XBee Programamble (S2C & 900 HP)\
**Tags:** fpga, spi, xbee\
**Created:** [March 26, 2015, 10:40am UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605 "2015-03-26T10:40:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtourtelot](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@mtourtelot](https://forums.digi.com/u/mtourtelot)\
**Post date:** [March 26, 2015, 10:40am UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/1 "2015-03-26T10:40:41Z")

</div>

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.

```auto
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 &lt;= cur;
    clr &lt;= 0;
    shift &lt;= 0;
    //ss &lt;= 0;
    case (cur)
        idle: begin
            if (attn==0) begin
                 case (cdiv)
                    2'b00: mid &lt;= 2;
                    2'b01: mid &lt;= 4;
                    2'b10: mid &lt;= 8;
                    2'b11: mid &lt;= 16;
                 endcase
                shift &lt;= 1;
                nxt &lt;= send;                
            end    
        end //idle
        send: begin
            ss &lt;= 0;
            if (nbit != 9) 
                shift &lt;= 1;
            else begin
                rdata &lt;= rreg;
                nxt &lt;= finish;
            end
        end //send
        finish: begin
            shift &lt;= 0;
            ss &lt;= 1;
            clr &lt;= 1;
            nxt &lt;= idle;
        end
        default: nxt &lt;= finish;
    endcase
end //always

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

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

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

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

```

---

<div class="post-metadata">

**Author:** ![mvut](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/mvut/32/31_2.png) [@mvut](https://forums.digi.com/u/mvut)\
**Post date:** [March 26, 2015, 2:25pm UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/2 "2015-03-26T14:25:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mtourtelot](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@mtourtelot](https://forums.digi.com/u/mtourtelot)\
**Post date:** [March 26, 2015, 2:56pm UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/3 "2015-03-26T14:56:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mvut](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/mvut/32/31_2.png) [@mvut](https://forums.digi.com/u/mvut)\
**Post date:** [March 26, 2015, 3:26pm UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/4 "2015-03-26T15:26:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mtourtelot](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@mtourtelot](https://forums.digi.com/u/mtourtelot)\
**Post date:** [March 26, 2015, 4:10pm UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/5 "2015-03-26T16:10:13Z")

</div>

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](http://ftp1.digi.com/support/documentation/90002180_K.pdf)

Page 29 details the SPI

---

<div class="post-metadata">

**Author:** ![mvut](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/mvut/32/31_2.png) [@mvut](https://forums.digi.com/u/mvut)\
**Post date:** [March 26, 2015, 4:14pm UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/6 "2015-03-26T16:14:07Z")

</div>

Try pages 28 - 30,

---

<div class="post-metadata">

**Author:** ![mtourtelot](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@mtourtelot](https://forums.digi.com/u/mtourtelot)\
**Post date:** [March 31, 2015, 8:59am UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/7 "2015-03-31T08:59:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mvut](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/mvut/32/31_2.png) [@mvut](https://forums.digi.com/u/mvut)\
**Post date:** [March 31, 2015, 11:42am UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/8 "2015-03-31T11:42:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mtourtelot](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@mtourtelot](https://forums.digi.com/u/mtourtelot)\
**Post date:** [March 31, 2015, 1:07pm UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/9 "2015-03-31T13:07:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mvut](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/mvut/32/31_2.png) [@mvut](https://forums.digi.com/u/mvut)\
**Post date:** [March 31, 2015, 1:37pm UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/10 "2015-03-31T13:37:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mtourtelot](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@mtourtelot](https://forums.digi.com/u/mtourtelot)\
**Post date:** [March 31, 2015, 2:28pm UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/11 "2015-03-31T14:28:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mvut](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.digi.com/mvut/32/31_2.png) [@mvut](https://forums.digi.com/u/mvut)\
**Post date:** [March 31, 2015, 2:40pm UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/12 "2015-03-31T14:40:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mtourtelot](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@mtourtelot](https://forums.digi.com/u/mtourtelot)\
**Post date:** [March 31, 2015, 4:17pm UTC](https://forums.digi.com/t/how-to-interface-xbee-with-fpga-using-spi/14605/13 "2015-03-31T16:17:07Z")

</div>

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
