You are here: Forum Home → ANT Developers Forums → ANT General Questions → Thread
/**
*****************************************************************************
**
** File : main.c
**
** Abstract : main function.
**
** Functions : main
**
** Environment : Atollic TrueSTUDIO(R)
** STMicroelectronics STM32F4xx Standard Peripherals Library
**
** Distribution: The file is distributed “as is,” without any warranty
** of any kind.
**
** (c)Copyright Atollic AB.
** You may use this file as-is or modify it according to the needs of your
** project. Distribution of this file (unmodified or modified) is not
** permitted. Atollic AB permit registered Atollic TrueSTUDIO(R) users the
** rights to distribute the assembled, compiled & linked contents of this
** file as part of an application binary file, provided that it is built
** using the Atollic TrueSTUDIO(R) toolchain.
**
**
*****************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
#include "stm324xg_eval.h"
#include <stdio.h>
/* Private variables */
#define ANT_CH_ID 0x00
#define ANT_CH_TYPE 0x10
#define ANT_NET_ID 0x00
#define ANT_DEV_ID1 0x31
#define ANT_DEV_ID2 0x00
#define ANT_DEV_TYPE 0x01
#define ANT_TX_TYPE 0x01
#define ANT_CH_FREQ 0x0023
#define ANT_CH_PER 0x2000
typedef uint8_t uchar;
uchar txBuffer[32];
uint8_t txBufferSize;
uint8_t txBufferPos;
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructureA;
GPIO_InitTypeDef GPIO_InitStructureC;
static __IO uint32_t TimingDelay;
/* Private function prototypes -----------------------------------------------*/
void Delay(__IO uint32_t nTime);
void build_Message(uchar* message, uint8_t messageSize);
void Delay(__IO uint32_t nTime);
void TimingDelay_Decrement(void);
void opench();
void closech();
void reset();
void assignch();
void setchid();
void setrf();
void setchperiod();
void sendbroadcast();
void configureGPIO();
void configureUART();
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
if (SysTick_Config(SystemCoreClock / 1000))
{
/* Capture error */
while (1);
}
configureGPIO();
configureUART();
// //printf("\n\rherr3o\n\r");
Delay(300);
//configure the txrx board
reset();
Delay(150);
assignch();
Delay(100);
setrf();
Delay(100);
setchperiod();
Delay(100);
setchid();
Delay(100);
opench();
Delay(100);
while(1)
{
sendbroadcast();
Delay(300);
}
}
void build_Message(uchar* message, uint8_t messageSize)
{
uint8_t i;
int j;
// _BIC_SR(GIE); // disable interrupt
txBufferPos = 0; // set position to 0
txBufferSize = messageSize + 3; // message plus syc, size and checksum
txBuffer[0] = 0xa4; // sync byte
txBuffer[1] = (uchar) messageSize - 1; // message size - command size (1)
for(i=0; i<messageSize; i++)
txBuffer[2+i] = message[i];
// calculate the checksum
for(i=0; i<txBufferSize - 1; ++i)
txBuffer[txBufferSize - 1] = txBuffer[txBufferSize - 1] ^ txBuffer[i];
for (j=0; j<txBufferSize; j++)
{
//__io_putchar(*(ptr++));
//_BIS_SR(GIE); // enable interrupt
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(EVAL_COM1, (uint8_t) txBuffer[j]);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
{}
}
//printf("\n\rSent a message!\n\r");
}
/**
* @brief Inserts a delay time.
* @param nTime: specifies the delay time length, in milliseconds.
* @retval None
*/
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
/**
* @brief Decrements the TimingDelay variable.
* @param None
* @retval None
*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
/*
* Open channel 1 for communications
*/
void opench()
{
//0xA4 SYNC
//0x01 LENGTH
//0x4B ID - ANT_OpenChannel
//0x01 Data - Channel 1
//0xE8 CRC
uchar setup[2];
setup[0] = 0x4B;
setup[1] = ANT_CH_ID;
build_Message(setup, 2);
return;
}
/**
* Close channel 1 for communications
*/
void closech()
{
//0xA4 SYNC
//0x01 LENGTH
//0x4C ID - ANT_OpenChannel
//0x01 Data - Channel 1
//0xEF CRC
uchar setup[2];
setup[0] = 0x4C;
setup[1] = ANT_CH_ID;
build_Message(setup, 2);
return;
}
// Resets module
void reset()
{
uchar setup[2];
setup[0] = 0x4a; // ID Byte
setup[1] = 0x00; // Data Byte N (N=LENGTH)
build_Message(setup, 2);
}
// Assigns CH=0, CH Type=10(TX), Net#=0
void assignch()
{
uchar setup[4];
setup[0] = 0x42;
setup[1] = ANT_CH_ID; // Channel ID, 0x00 for HRM, 0x01 for custom
setup[2] = ANT_CH_TYPE; // CH Type
setup[3] = ANT_NET_ID; // Network ID
build_Message(setup, 4);
}
// Assigns CH#, Device#=0000, Device Type ID=00, Trans Type=00
void setchid()
{
uchar setup[6];
setup[0] = 0x51;
setup[1] = ANT_CH_ID;