IQRF SPI library for master MCU

swThis article describes my new SPI library for master MCU. It is based on library published in startup package. I made some changes. At the first, I removed all PIC MCU related lines of code, because of universality of library. Next, I encapsulated all global variables related to SPI state machine to one structure. There are published two versions of library – for AVR-GCC and GCC compiler for ARM version 4.9.3.

All you need to do is definition of this two lines of code:

SPI_SS_LO;
SPI_SS_HI;

This macro is defined in another part of project. It manage slave select pin manipulation. Please make your own macro against target platform.

If you use different platform from AVR and AVR-GCC, you have to define _delay_us() function. In case of ARM STM32F1 you can use something like this:

TIM3->CNT = N-1;
while( TIM3->CNT );

Where timer 3 is clocked from 1MHz clock, and timer is set to downcounting

This is example, how to use library with ATMega8A:

_delay_us(150);
IQRF_SPI_Task(&stm);
 
// Send "iqrf" message every one SLED period
if (blink)
{            
    blink = 0;
    if ((stm.SPIState == TR_STAT_COM_MODE || stm.SPIState == TR_STAT_SLOW_MODE) && stm.busyState == IQRF_NOT_BUSY) 
    {
        IQRF_SPI_ClearTxBuffer(&stm);
        stm.DLEN = 4;                         // number of user bytes
        stm.PTYPE = (stm.DLEN | 0x80);         // PTYPE set bit7 - write to bufferCOM of TR module
        stm.pTxBuffer[0] = TR_CMD_WR_RD;
        stm.pTxBuffer[1] = stm.PTYPE;
        stm.pTxBuffer[2] = 'i';            // user data "iqrf"
        stm.pTxBuffer[3] = 'q';
        stm.pTxBuffer[4] = 'r';
        stm.pTxBuffer[5] = 'f';
        Start_IQRF_SPI(&stm);            
    }
}
 
if (stm.SPITaskMessage)                  // any message from IQRF_SPI_Task?
{
    switch (stm.SPITaskMessage)
    {
        case IQRF_SPI_WRITE_OK:             // last sending was successful
        break;
 
        case IQRF_SPI_WRITE_ERR:            // last sending was not successful
        break;
 
        case IQRF_SPI_DATA_READY:           // received packet is in IQRF_SPI_RxBuf                
        // All useful data are copied to USART
        temp = stm.pRxBuffer[1];
        if (temp == 0x40) temp = 64; else temp = temp & 0x3F;
        for (i = 0; i<temp; i++)
            UART_Transmit(stm.pRxBuffer[i+2]);                
        break;
    }
    stm.SPITaskMessage = 0;
}

This code should be in loop of course.

Library attached.

Enjoy.

Download Attachments

  • c main_iqrfspi.c
    IQRF SPI main file example
    File size: 3 KB Downloads: 239
  • c iqrf_spi.c
    IQRF SPI library
    File size: 8 KB Downloads: 355
  • h iqrf_spi.h
    IQRF SPI library
    File size: 2 KB Downloads: 237

One thought on “IQRF SPI library for master MCU

  1. Pingback: IQRF SPI library for master MCU – Ráj Bastlířů

Leave a Reply

Your email address will not be published. Required fields are marked *