2010-10-16 19:54:00 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file ahrs_spi_program_master.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief AHRS programming over SPI link - master(OpenPilot) end.
|
|
|
|
*
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "openpilot.h"
|
|
|
|
#include "ahrs_spi_program_master.h"
|
|
|
|
#include "ahrs_spi_program.h"
|
|
|
|
#include "pios_spi.h"
|
|
|
|
|
2011-04-29 21:48:13 +02:00
|
|
|
PROGERR TransferPacket(uint32_t spi_id, AhrsProgramPacket *txBuf,
|
|
|
|
AhrsProgramPacket *rxBuf);
|
2010-10-16 19:54:00 +02:00
|
|
|
|
|
|
|
#define MAX_CONNECT_TRIES 500 //half a second
|
2011-04-29 21:48:13 +02:00
|
|
|
bool AhrsProgramConnect(uint32_t spi_id) {
|
2010-10-16 19:54:00 +02:00
|
|
|
AhrsProgramPacket rxBuf;
|
|
|
|
AhrsProgramPacket txBuf;
|
|
|
|
memset(&rxBuf, 0, sizeof(AhrsProgramPacket));
|
2011-04-29 21:48:13 +02:00
|
|
|
memcpy(&txBuf, SPI_PROGRAM_REQUEST, SPI_PROGRAM_REQUEST_LENGTH);
|
|
|
|
for (int ct = 0; ct < MAX_CONNECT_TRIES; ct++) {
|
2011-02-12 23:19:50 +01:00
|
|
|
PIOS_SPI_RC_PinSet(spi_id, 0);
|
|
|
|
uint32_t res = PIOS_SPI_TransferBlock(spi_id, (uint8_t *) &txBuf,
|
2011-04-29 21:48:13 +02:00
|
|
|
(uint8_t *) &rxBuf, SPI_PROGRAM_REQUEST_LENGTH + 1, NULL);
|
2011-02-12 23:19:50 +01:00
|
|
|
PIOS_SPI_RC_PinSet(spi_id, 1);
|
2011-04-29 21:48:13 +02:00
|
|
|
if (res == 0 && memcmp(&rxBuf, SPI_PROGRAM_ACK,
|
|
|
|
SPI_PROGRAM_REQUEST_LENGTH) == 0) {
|
2010-10-16 19:54:00 +02:00
|
|
|
return (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelay(1 / portTICK_RATE_MS);
|
|
|
|
}
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
2011-04-29 21:48:13 +02:00
|
|
|
PROGERR AhrsProgramWrite(uint32_t spi_id, uint32_t address, void * data,
|
|
|
|
uint32_t size) {
|
2010-10-16 19:54:00 +02:00
|
|
|
AhrsProgramPacket rxBuf;
|
|
|
|
AhrsProgramPacket txBuf;
|
|
|
|
memset(&rxBuf, 0, sizeof(AhrsProgramPacket));
|
2011-04-29 21:48:13 +02:00
|
|
|
memcpy(txBuf.data, data, size);
|
2010-10-16 19:54:00 +02:00
|
|
|
txBuf.size = size;
|
|
|
|
txBuf.type = PROGRAM_WRITE;
|
|
|
|
txBuf.address = address;
|
2011-02-12 23:19:50 +01:00
|
|
|
PROGERR ret = TransferPacket(spi_id, &txBuf, &rxBuf);
|
2011-04-29 21:48:13 +02:00
|
|
|
if (ret != PROGRAM_ERR_OK) {
|
|
|
|
return (ret);
|
2010-10-16 19:54:00 +02:00
|
|
|
}
|
2011-04-29 21:48:13 +02:00
|
|
|
return (PROGRAM_ERR_OK);
|
2010-10-16 19:54:00 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 21:48:13 +02:00
|
|
|
PROGERR AhrsProgramRead(uint32_t spi_id, uint32_t address, void * data,
|
|
|
|
uint32_t size) {
|
2010-10-16 19:54:00 +02:00
|
|
|
AhrsProgramPacket rxBuf;
|
|
|
|
AhrsProgramPacket txBuf;
|
|
|
|
memset(&rxBuf, 0, sizeof(AhrsProgramPacket));
|
|
|
|
txBuf.size = size;
|
|
|
|
txBuf.type = PROGRAM_READ;
|
|
|
|
txBuf.address = address;
|
2011-02-12 23:19:50 +01:00
|
|
|
PROGERR ret = TransferPacket(spi_id, &txBuf, &rxBuf);
|
2011-04-29 21:48:13 +02:00
|
|
|
if (ret != PROGRAM_ERR_OK) {
|
|
|
|
return (ret);
|
2010-10-16 19:54:00 +02:00
|
|
|
}
|
|
|
|
memcpy(data, rxBuf.data, size);
|
2011-04-29 21:48:13 +02:00
|
|
|
return (PROGRAM_ERR_OK);
|
2010-10-16 19:54:00 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 21:48:13 +02:00
|
|
|
PROGERR AhrsProgramReboot(uint32_t spi_id) {
|
2010-10-16 19:54:00 +02:00
|
|
|
AhrsProgramPacket rxBuf;
|
|
|
|
AhrsProgramPacket txBuf;
|
|
|
|
memset(&rxBuf, 0, sizeof(AhrsProgramPacket));
|
|
|
|
txBuf.type = PROGRAM_REBOOT;
|
2011-04-29 21:48:13 +02:00
|
|
|
memcpy(txBuf.data, REBOOT_CONFIRMATION, REBOOT_CONFIRMATION_LENGTH);
|
2011-02-12 23:19:50 +01:00
|
|
|
PROGERR ret = TransferPacket(spi_id, &txBuf, &rxBuf);
|
2010-10-16 19:54:00 +02:00
|
|
|
//If AHRS has rebooted we will get comms errors
|
2011-04-29 21:48:13 +02:00
|
|
|
if (ret == PROGRAM_ERR_LINK) {
|
|
|
|
return (PROGRAM_ERR_OK);
|
2010-10-16 19:54:00 +02:00
|
|
|
}
|
2011-04-29 21:48:13 +02:00
|
|
|
return (PROGRAM_ERR_FUNCTION);
|
2010-10-16 19:54:00 +02:00
|
|
|
}
|
|
|
|
|
2011-04-29 21:48:13 +02:00
|
|
|
PROGERR TransferPacket(uint32_t spi_id, AhrsProgramPacket *txBuf,
|
|
|
|
AhrsProgramPacket *rxBuf) {
|
2010-10-16 19:54:00 +02:00
|
|
|
static uint32_t pktId = 0;
|
|
|
|
pktId++;
|
|
|
|
txBuf->packetId = pktId;
|
|
|
|
txBuf->crc = GenerateCRC(txBuf);
|
|
|
|
int ct = 0;
|
2011-04-29 21:48:13 +02:00
|
|
|
for (; ct < MAX_CONNECT_TRIES; ct++) {
|
2011-02-12 23:19:50 +01:00
|
|
|
PIOS_SPI_RC_PinSet(spi_id, 0);
|
|
|
|
uint32_t res = PIOS_SPI_TransferBlock(spi_id, (uint8_t *) txBuf,
|
2011-04-29 21:48:13 +02:00
|
|
|
(uint8_t *) rxBuf, sizeof(AhrsProgramPacket), NULL);
|
2011-02-12 23:19:50 +01:00
|
|
|
PIOS_SPI_RC_PinSet(spi_id, 1);
|
2011-04-29 21:48:13 +02:00
|
|
|
if (res == 0) {
|
|
|
|
if (rxBuf->type != PROGRAM_NULL && rxBuf->crc == GenerateCRC(rxBuf)
|
|
|
|
&& rxBuf->packetId == pktId) {
|
2010-10-16 19:54:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelay(1 / portTICK_RATE_MS);
|
|
|
|
}
|
2011-04-29 21:48:13 +02:00
|
|
|
if (ct == MAX_CONNECT_TRIES) {
|
2010-10-16 19:54:00 +02:00
|
|
|
return (PROGRAM_ERR_LINK);
|
|
|
|
}
|
2011-04-29 21:48:13 +02:00
|
|
|
if (rxBuf->type != PROGRAM_ACK) {
|
|
|
|
return (PROGRAM_ERR_FUNCTION);
|
2010-10-16 19:54:00 +02:00
|
|
|
}
|
2011-04-29 21:48:13 +02:00
|
|
|
return (PROGRAM_ERR_OK);
|
2010-10-16 19:54:00 +02:00
|
|
|
}
|
|
|
|
|