Welcome Guest,Register Now
Log In

ANT Forum

Welcome guest, please Login or Register

   

Interfacing ANT with PIC24F via SPI

Rank

Total Posts: 1

Joined 2017-09-02

PM

Hello.

I've a custom built board with PIC24F microcontroller and Ant AP2 module. I'm new to both PIC programming and interfacing Ant protocol, and right now I'm trying to establish a communication between the microcontroller and the Ant module.

According to the available documentation, the Ant module should send a Startup message to the host after it is reset. On my board there's a dedicated reset line between the microcontroller and the Ant module and that's what I'm using for the reset (the function Ant_Reset_ResetPin()). After reset I successfully read first message sent from the Ant module. The message has 5 bytes but its content is invalid. First of all, I'll describe my setup.

I've PIC24FJ256GA410 microcontroller which runs from 3.6864 MHz external crystal oscillator. The Ant module is AP2M_Ver5 SMT. The module uses nRF24AP2-8CHQ32 Ant chip which should drive the SPI clock at 500 kHz according to its datasheet. I'm using reset pin, byte flow control and I make pulses on SEN line with software delays.

The code below initializes peripherals (SPI slave for Ant and UART for debugging) and Ant pins, then resets the Ant module via reset pin, then reads bytes from the Ant module and logs them to the UART. The PIC SPI module is initialized with CKP = 1, meaning "Idle state for clock is a high level; active state is a low level", and CKE = 0, meaning "Transmit happens on transition from Idle clock state to active clock state". On board reset, I receive the following from the Ant module:

ANT reset
ANT
->HOST12
ANT
->HOSTc0
ANT
->HOST7b
ANT
->HOST40
ANT
->HOST69 


I wasn't so sure about the CKE SPI setting so I tried with 1 too with the following result:

ANT->HOST49
ANT
->HOST20
ANT
->HOST3d
ANT
->HOSTa0
ANT
->HOST74 


It's garbage. The sync byte isn't 0xA4, the length isn't 1 and the checksum is invalid. What could be my problem?

#include <xc.h>
#include <stdint.h>
#include <stdio.h>

#define FOSC 3686400
#define FCY (FOSC/2)

#include <libpic30.h>

#pragma config JTAGEN = OFF
#pragma config GWRP = OFF
#pragma config FWDTEN = OFF
#pragma config ICS = PGx3
#pragma config FWPSA = PR128
#pragma config WDTPS = PS2048
#pragma config FCKSM = CSECMD
#pragma config OSCIOFCN = OFF
#pragma config POSCMOD = XT
#pragma config FNOSC = PRI

#define IO_TRIS_SPI_SDO                _TRISG7
#define IO_PORT_SPI_SDO                _RG7
#define IO_LAT_SPI_SDO                 _LATG7
#define IO_TRIS_SPI_SCK                _TRISG8
#define IO_PORT_SPI_SCK                _RG8
#define IO_LAT_SPI_SCK                 _LATG8
#define IO_TRIS_SPI_SDI                _TRISB4
#define IO_PORT_SPI_SDI                _RB4
#define IO_LAT_SPI_SDI                 _LATB4
#define IO_TRIS_ANTRFID_nRESET         _TRISA4
#define IO_PORT_ANTRFID_nRESET         _RA4
#define IO_LAT_ANTRFID_nRESET          _LATA4
#define IO_TRIS_ANT_nSRDY              _TRISF4
#define IO_PORT_ANT_nSRDY              _RF4
#define IO_LAT_ANT_nSRDY               _LATF4
#define IO_TRIS_ANT_nMRDY              _TRISF5
#define IO_PORT_ANT_nMRDY              _RF5
#define IO_LAT_ANT_nMRDY               _LATF5

#define IO_TRIS_ANT_SFLOW              _TRISE0
#define IO_PORT_ANT_SFLOW              _RE0
#define IO_LAT_ANT_SFLOW               _LATE0

#define IO_TRIS_ANT_SEN                _TRISE1
#define IO_PORT_ANT_SEN                _RE1
#define IO_LAT_ANT_SEN                 _LATE1


static void SPI_Init(void)
{
    _SPI1TXIE 
0;                  // disable receive interrupts
    
SPI1CON1Lbits.SPIEN 0;        // stop and reset SPI

    /*
     * pin 11 ~ RP26 ~ RG7 ~ SDO
     * pin 21 ~ RP21 ~ RB4 ~ SDI
     * pin 12 ~ RP19 ~ RG8 ~ SCK
     */

    
IO_TRIS_SPI_SDO 0;
    
IO_TRIS_SPI_SDI 1;
    
IO_TRIS_SPI_SCK 1;

    
_RP26R _RPOUT_SDO1;
    
_SDI1R 21;
    
_SCK1R 19;

    
SPI1CON1Lbits.MSTEN 0;        // slave mode
    
SPI1CON1Lbits.ENHBUF 0;       // single byte buffer
    
SPI1CON1Lbits.CKP 1;          // clock polarity (idle is 1, active is 0)
    
SPI1CON1Lbits.CKE 0;          // clock edge (tx happens on falling edge, probably...)

    
while (SPI1STATLbits.SPIRBF{  // clear the receive buffer
        
(voidSPI1BUFL;
        (
voidSPI1BUFH;
    
}
    
(voidSPI1STATLbits.SPIROV;    // clear the RX overflow flag

    
SPI1CON1Lbits.SPIEN 1;        // enable the SPI module
}

uint8_t SPI_TransferBlocking
(uint8_t b)
{
    SPI1BUFL 
b;
    while (!
SPI1STATLbits.SPIRBF);
    return 
SPI1BUFL;
}

void ANT_InitPins
(void)
{
    IO_TRIS_ANTRFID_nRESET 
0;
    
IO_TRIS_ANT_nMRDY 0;
    
IO_TRIS_ANT_nSRDY 0;
    
IO_TRIS_ANT_SFLOW 0;
    
IO_TRIS_ANT_SEN 1;

    
IO_LAT_ANTRFID_nRESET 1;
    
IO_LAT_ANT_SFLOW 0;
    
IO_LAT_ANT_nMRDY 1;
    
IO_LAT_ANT_nSRDY 1;
}

void ANT_Reset_ResetPin
(void)
{
    IO_LAT_ANTRFID_nRESET 
0;
    
__delay_ms(1);
    
IO_LAT_ANTRFID_nRESET 1;
    
__delay_ms(1);
}

void InitUART
(void);

int main(void)
{
    
// make all pins digital
    
ANSA ANSB ANSC ANSD ANSE ANSF ANSG ANSH 0;

    
InitUART();
    
SPI_Init();
    
ANT_InitPins();

    
ANT_Reset_ResetPin();
    
printf("ANT reset\r\n");
    
__delay_ms(1000);

    while (
1{
        
if (!IO_PORT_ANT_SEN{
            IO_LAT_ANT_nSRDY 
0;
            
__delay_us(50);
            
IO_LAT_ANT_nSRDY 1;
            
uint8_t b SPI_TransferBlocking(0);
            
printf("ANT->HOST: %x\r\n"b);
        
}
    }

    
return 0;
}

#define UART_BAUDRATE 115200

void InitUART(void)
{
    _U1TXIE 
0;
    
_U1RXIE 0;

    
_RP11R _RPOUT_U1TX;
    
_U1RXR 12;

    
U1MODEbits.STSEL 0;
    
U1MODEbits.PDSEL 0;
    
U1MODEbits.ABAUD 0;
    
U1MODEbits.BRGH 0;

    
U1BRG = (int) (FCY/(16 UART_BAUDRATE) - 1);

    
U1MODEbits.UARTEN 1;
    
U1STAbits.UTXEN 1;

    
__delay_ms(1000000/UART_BAUDRATE);
}

void putch
(unsigned char ch)
{
    
while (U1STAbits.UTXBF);
    
U1TXREG ch;
    
__delay_ms(1); // FIXME
     
RankRankRank

Total Posts: 54

Joined 2013-05-15

PM

Hi,
I'm trying the same communication via SPI with

* an nRF24AP2 and an Arduino Nano

Arduino has a MISO/MOSI - SCKL/SS lines to drive the SPI bus.
So, which kind of hardware connection have you been done?
Could you write down here the hardware connections?
Thanks.      
Avatar
RankRankRankRank

Total Posts: 745

Joined 2012-09-14

PM

Framing between UART and SPI is different, the sync byte can be 0xA4 or 0xA5 depending on the direction of the message. The Interfacing with ANT General Purpose Chipsets and Modules document details the difference between the two protocols, as well as the required hardware connections. The ANT+ Embedded Reference Designs also have fairly useful example C code, even if it is written for the MSP430 instead of the PIC.