1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-10 18:24:11 +01:00

More cleaning, moved stopwatch files to library folder, code formatting.

This commit is contained in:
zedamota 2011-04-29 20:48:13 +01:00
parent 3132de4e48
commit 1cc152b4dd
46 changed files with 1641 additions and 2141 deletions

View File

@ -3,36 +3,32 @@
uint8_t buf[256]; uint8_t buf[256];
bool StartProgramming(void) bool StartProgramming(void) {
{ PIOS_COM_SendFormattedString(PIOS_COM_AUX, "Started programming\r\n");
PIOS_COM_SendFormattedString(PIOS_COM_AUX,"Started programming\r\n"); return (true);
return(true);
} }
bool WriteData(uint32_t offset, uint8_t *buffer, uint32_t size) bool WriteData(uint32_t offset, uint8_t *buffer, uint32_t size) {
{ if (size > SPI_MAX_PROGRAM_DATA_SIZE) {
if(size > SPI_MAX_PROGRAM_DATA_SIZE) PIOS_COM_SendFormattedString(PIOS_COM_AUX, "oversize: %d\r\n", size);
{ return (false);
PIOS_COM_SendFormattedString(PIOS_COM_AUX,"oversize: %d\r\n", size);
return(false);
} }
PIOS_COM_SendFormattedString(PIOS_COM_AUX,"Wrote %d bytes to %d\r\n", size, offset); PIOS_COM_SendFormattedString(PIOS_COM_AUX, "Wrote %d bytes to %d\r\n",
memcpy(buf,buffer,size); size, offset);
memcpy(buf, buffer, size);
PIOS_LED_Toggle(LED1); PIOS_LED_Toggle(LED1);
return(true); return (true);
} }
bool ReadData(uint32_t offset, uint8_t *buffer, uint32_t size) bool ReadData(uint32_t offset, uint8_t *buffer, uint32_t size) {
{ if (size > SPI_MAX_PROGRAM_DATA_SIZE) {
if(size > SPI_MAX_PROGRAM_DATA_SIZE) PIOS_COM_SendFormattedString(PIOS_COM_AUX, "oversize: %d\r\n", size);
{ return (false);
PIOS_COM_SendFormattedString(PIOS_COM_AUX,"oversize: %d\r\n", size);
return(false);
} }
PIOS_COM_SendFormattedString(PIOS_COM_AUX,"Read %d bytes from %d\r\n", size, offset); PIOS_COM_SendFormattedString(PIOS_COM_AUX, "Read %d bytes from %d\r\n",
memcpy(buffer,buf,size); size, offset);
memcpy(buffer, buf, size);
PIOS_LED_Toggle(LED1); PIOS_LED_Toggle(LED1);
return(true); return (true);
} }

View File

@ -2,88 +2,64 @@
#include "ahrs_spi_program.h" #include "ahrs_spi_program.h"
// Static CRC polynomial table // Static CRC polynomial table
static uint32_t crcTable[256] = static uint32_t crcTable[256] = { 0x00000000, 0x77073096, 0xEE0E612C,
{ 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832,
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07,
0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D,
0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A,
0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD,
0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A, 0xC8D75180,
0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 0x2802B89E,
0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB,
0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0xB6662D3D,
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,
0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162,
0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49,
0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC,
0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3,
0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0xB966D409, 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF,
0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2,
0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9,
0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C,
0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703,
0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D, };
0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,
};
/**generate CRC32 from a program packet /**generate CRC32 from a program packet
this is slightly overkill but we want to be sure this is slightly overkill but we want to be sure
*/ */
uint32_t GenerateCRC (AhrsProgramPacket * packet) uint32_t GenerateCRC(AhrsProgramPacket * packet) {
{ uint8_t * ptr = (uint8_t *) packet;
uint8_t * ptr = (uint8_t *)packet; int size = ((int) &packet->crc) - (int) packet;
int size = ((int)&packet->crc) - (int)packet;
uint32_t crc = 0xFFFFFFFF; uint32_t crc = 0xFFFFFFFF;
for(int ct=0; ct< size; ct++) for (int ct = 0; ct < size; ct++) {
{
crc = ((crc) >> 8) ^ crcTable[(*ptr++) ^ ((crc) & 0x000000FF)]; crc = ((crc) >> 8) ^ crcTable[(*ptr++) ^ ((crc) & 0x000000FF)];
} }
return(~crc); return (~crc);
} }

View File

@ -29,22 +29,22 @@
#include "ahrs_spi_program.h" #include "ahrs_spi_program.h"
#include "pios_spi.h" #include "pios_spi.h"
PROGERR TransferPacket(uint32_t spi_id, AhrsProgramPacket *txBuf, AhrsProgramPacket *rxBuf); PROGERR TransferPacket(uint32_t spi_id, AhrsProgramPacket *txBuf,
AhrsProgramPacket *rxBuf);
#define MAX_CONNECT_TRIES 500 //half a second #define MAX_CONNECT_TRIES 500 //half a second
bool AhrsProgramConnect(uint32_t spi_id) {
bool AhrsProgramConnect(uint32_t spi_id)
{
AhrsProgramPacket rxBuf; AhrsProgramPacket rxBuf;
AhrsProgramPacket txBuf; AhrsProgramPacket txBuf;
memset(&rxBuf, 0, sizeof(AhrsProgramPacket)); memset(&rxBuf, 0, sizeof(AhrsProgramPacket));
memcpy(&txBuf,SPI_PROGRAM_REQUEST,SPI_PROGRAM_REQUEST_LENGTH); memcpy(&txBuf, SPI_PROGRAM_REQUEST, SPI_PROGRAM_REQUEST_LENGTH);
for(int ct = 0; ct < MAX_CONNECT_TRIES; ct++) { for (int ct = 0; ct < MAX_CONNECT_TRIES; ct++) {
PIOS_SPI_RC_PinSet(spi_id, 0); PIOS_SPI_RC_PinSet(spi_id, 0);
uint32_t res = PIOS_SPI_TransferBlock(spi_id, (uint8_t *) &txBuf, uint32_t res = PIOS_SPI_TransferBlock(spi_id, (uint8_t *) &txBuf,
(uint8_t *) & rxBuf, SPI_PROGRAM_REQUEST_LENGTH +1, NULL); (uint8_t *) &rxBuf, SPI_PROGRAM_REQUEST_LENGTH + 1, NULL);
PIOS_SPI_RC_PinSet(spi_id, 1); PIOS_SPI_RC_PinSet(spi_id, 1);
if(res == 0 && memcmp(&rxBuf, SPI_PROGRAM_ACK, SPI_PROGRAM_REQUEST_LENGTH) == 0) { if (res == 0 && memcmp(&rxBuf, SPI_PROGRAM_ACK,
SPI_PROGRAM_REQUEST_LENGTH) == 0) {
return (true); return (true);
} }
@ -53,25 +53,24 @@ bool AhrsProgramConnect(uint32_t spi_id)
return (false); return (false);
} }
PROGERR AhrsProgramWrite(uint32_t spi_id, uint32_t address, void * data,
PROGERR AhrsProgramWrite(uint32_t spi_id, uint32_t address, void * data, uint32_t size) uint32_t size) {
{
AhrsProgramPacket rxBuf; AhrsProgramPacket rxBuf;
AhrsProgramPacket txBuf; AhrsProgramPacket txBuf;
memset(&rxBuf, 0, sizeof(AhrsProgramPacket)); memset(&rxBuf, 0, sizeof(AhrsProgramPacket));
memcpy(txBuf.data,data,size); memcpy(txBuf.data, data, size);
txBuf.size = size; txBuf.size = size;
txBuf.type = PROGRAM_WRITE; txBuf.type = PROGRAM_WRITE;
txBuf.address = address; txBuf.address = address;
PROGERR ret = TransferPacket(spi_id, &txBuf, &rxBuf); PROGERR ret = TransferPacket(spi_id, &txBuf, &rxBuf);
if(ret != PROGRAM_ERR_OK) { if (ret != PROGRAM_ERR_OK) {
return(ret); return (ret);
} }
return(PROGRAM_ERR_OK); return (PROGRAM_ERR_OK);
} }
PROGERR AhrsProgramRead(uint32_t spi_id, uint32_t address, void * data, uint32_t size) PROGERR AhrsProgramRead(uint32_t spi_id, uint32_t address, void * data,
{ uint32_t size) {
AhrsProgramPacket rxBuf; AhrsProgramPacket rxBuf;
AhrsProgramPacket txBuf; AhrsProgramPacket txBuf;
memset(&rxBuf, 0, sizeof(AhrsProgramPacket)); memset(&rxBuf, 0, sizeof(AhrsProgramPacket));
@ -79,59 +78,54 @@ PROGERR AhrsProgramRead(uint32_t spi_id, uint32_t address, void * data, uint32_t
txBuf.type = PROGRAM_READ; txBuf.type = PROGRAM_READ;
txBuf.address = address; txBuf.address = address;
PROGERR ret = TransferPacket(spi_id, &txBuf, &rxBuf); PROGERR ret = TransferPacket(spi_id, &txBuf, &rxBuf);
if(ret != PROGRAM_ERR_OK) { if (ret != PROGRAM_ERR_OK) {
return(ret); return (ret);
} }
memcpy(data, rxBuf.data, size); memcpy(data, rxBuf.data, size);
return(PROGRAM_ERR_OK); return (PROGRAM_ERR_OK);
} }
PROGERR AhrsProgramReboot(uint32_t spi_id) PROGERR AhrsProgramReboot(uint32_t spi_id) {
{
AhrsProgramPacket rxBuf; AhrsProgramPacket rxBuf;
AhrsProgramPacket txBuf; AhrsProgramPacket txBuf;
memset(&rxBuf, 0, sizeof(AhrsProgramPacket)); memset(&rxBuf, 0, sizeof(AhrsProgramPacket));
txBuf.type = PROGRAM_REBOOT; txBuf.type = PROGRAM_REBOOT;
memcpy(txBuf.data,REBOOT_CONFIRMATION,REBOOT_CONFIRMATION_LENGTH); memcpy(txBuf.data, REBOOT_CONFIRMATION, REBOOT_CONFIRMATION_LENGTH);
PROGERR ret = TransferPacket(spi_id, &txBuf, &rxBuf); PROGERR ret = TransferPacket(spi_id, &txBuf, &rxBuf);
//If AHRS has rebooted we will get comms errors //If AHRS has rebooted we will get comms errors
if(ret == PROGRAM_ERR_LINK) { if (ret == PROGRAM_ERR_LINK) {
return(PROGRAM_ERR_OK); return (PROGRAM_ERR_OK);
} }
return(PROGRAM_ERR_FUNCTION); return (PROGRAM_ERR_FUNCTION);
} }
PROGERR TransferPacket(uint32_t spi_id, AhrsProgramPacket *txBuf, AhrsProgramPacket *rxBuf) PROGERR TransferPacket(uint32_t spi_id, AhrsProgramPacket *txBuf,
{ AhrsProgramPacket *rxBuf) {
static uint32_t pktId = 0; static uint32_t pktId = 0;
pktId++; pktId++;
txBuf->packetId = pktId; txBuf->packetId = pktId;
txBuf->crc = GenerateCRC(txBuf); txBuf->crc = GenerateCRC(txBuf);
int ct = 0; int ct = 0;
for(; ct < MAX_CONNECT_TRIES; ct++) { for (; ct < MAX_CONNECT_TRIES; ct++) {
PIOS_SPI_RC_PinSet(spi_id, 0); PIOS_SPI_RC_PinSet(spi_id, 0);
uint32_t res = PIOS_SPI_TransferBlock(spi_id, (uint8_t *) txBuf, uint32_t res = PIOS_SPI_TransferBlock(spi_id, (uint8_t *) txBuf,
(uint8_t *) rxBuf, sizeof(AhrsProgramPacket), NULL); (uint8_t *) rxBuf, sizeof(AhrsProgramPacket), NULL);
PIOS_SPI_RC_PinSet(spi_id, 1); PIOS_SPI_RC_PinSet(spi_id, 1);
if(res == 0) { if (res == 0) {
if(rxBuf->type != PROGRAM_NULL && if (rxBuf->type != PROGRAM_NULL && rxBuf->crc == GenerateCRC(rxBuf)
rxBuf->crc == GenerateCRC(rxBuf) && && rxBuf->packetId == pktId) {
rxBuf->packetId == pktId) {
break; break;
} }
} }
vTaskDelay(1 / portTICK_RATE_MS); vTaskDelay(1 / portTICK_RATE_MS);
} }
if(ct == MAX_CONNECT_TRIES) { if (ct == MAX_CONNECT_TRIES) {
return (PROGRAM_ERR_LINK); return (PROGRAM_ERR_LINK);
} }
if(rxBuf->type != PROGRAM_ACK) { if (rxBuf->type != PROGRAM_ACK) {
return(PROGRAM_ERR_FUNCTION); return (PROGRAM_ERR_FUNCTION);
} }
return(PROGRAM_ERR_OK); return (PROGRAM_ERR_OK);
} }

View File

@ -24,7 +24,6 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
@ -43,53 +42,52 @@ static bool done = false;
static void ProcessPacket(); static void ProcessPacket();
#define WAIT_IF_RECEIVING() while(!(GPIOB->IDR & GPIO_Pin_12)){}; //NSS must be high #define WAIT_IF_RECEIVING() while(!(GPIOB->IDR & GPIO_Pin_12)){}; //NSS must be high
//Number of crc failures to allow before giving up //Number of crc failures to allow before giving up
#define PROGRAM_PACKET_TRIES 4 #define PROGRAM_PACKET_TRIES 4
void AhrsProgramReceive(uint32_t spi_id) void AhrsProgramReceive(uint32_t spi_id) {
{
done = false; done = false;
memset(&txBuf,0,sizeof(AhrsProgramPacket)); memset(&txBuf, 0, sizeof(AhrsProgramPacket));
//wait for a program request //wait for a program request
int count = PROGRAM_PACKET_TRIES; int count = PROGRAM_PACKET_TRIES;
while(1) { while (1) {
WAIT_IF_RECEIVING(); WAIT_IF_RECEIVING();
while((PIOS_SPI_Busy(spi_id) != 0)){}; while ((PIOS_SPI_Busy(spi_id) != 0)) {
memset(&rxBuf,'a',sizeof(AhrsProgramPacket)); };
memset(&rxBuf, 'a', sizeof(AhrsProgramPacket));
int32_t res = PIOS_SPI_TransferBlock(spi_id, NULL, (uint8_t*) &rxBuf, int32_t res = PIOS_SPI_TransferBlock(spi_id, NULL, (uint8_t*) &rxBuf,
SPI_PROGRAM_REQUEST_LENGTH + 1, NULL); SPI_PROGRAM_REQUEST_LENGTH + 1, NULL);
if(res == 0 && if (res == 0 && memcmp(&rxBuf, SPI_PROGRAM_REQUEST,
memcmp(&rxBuf, SPI_PROGRAM_REQUEST, SPI_PROGRAM_REQUEST_LENGTH) == 0) { SPI_PROGRAM_REQUEST_LENGTH) == 0) {
break; break;
} }
if(count-- == 0) { if (count-- == 0) {
return; return;
} }
} }
if(!StartProgramming()) { if (!StartProgramming()) {
//Couldn't erase FLASH. Nothing we can do. //Couldn't erase FLASH. Nothing we can do.
return; return;
} }
//send ack //send ack
memcpy(&txBuf,SPI_PROGRAM_ACK,SPI_PROGRAM_REQUEST_LENGTH); memcpy(&txBuf, SPI_PROGRAM_ACK, SPI_PROGRAM_REQUEST_LENGTH);
WAIT_IF_RECEIVING(); WAIT_IF_RECEIVING();
while(0 != PIOS_SPI_TransferBlock(spi_id,(uint8_t*) &txBuf, NULL, while (0 != PIOS_SPI_TransferBlock(spi_id, (uint8_t*) &txBuf, NULL,
SPI_PROGRAM_REQUEST_LENGTH + 1, NULL)) {}; SPI_PROGRAM_REQUEST_LENGTH + 1, NULL)) {
};
txBuf.type = PROGRAM_NULL; txBuf.type = PROGRAM_NULL;
while(!done) { while (!done) {
WAIT_IF_RECEIVING(); WAIT_IF_RECEIVING();
if(0 == PIOS_SPI_TransferBlock(spi_id,(uint8_t*) &txBuf, if (0 == PIOS_SPI_TransferBlock(spi_id, (uint8_t*) &txBuf,
(uint8_t*) &rxBuf, sizeof(AhrsProgramPacket), NULL)) { (uint8_t*) &rxBuf, sizeof(AhrsProgramPacket), NULL)) {
uint32_t crc = GenerateCRC(&rxBuf); uint32_t crc = GenerateCRC(&rxBuf);
if(crc != rxBuf.crc || if (crc != rxBuf.crc || txBuf.packetId == rxBuf.packetId) {
txBuf.packetId == rxBuf.packetId) {
continue; continue;
} }
ProcessPacket(); ProcessPacket();
@ -99,18 +97,14 @@ void AhrsProgramReceive(uint32_t spi_id)
} }
} }
void ProcessPacket() {
switch (rxBuf.type) {
void ProcessPacket()
{
switch(rxBuf.type) {
case PROGRAM_NULL: case PROGRAM_NULL:
txBuf.type = PROGRAM_NULL; txBuf.type = PROGRAM_NULL;
break; break;
case PROGRAM_WRITE: case PROGRAM_WRITE:
if(WriteData(rxBuf.address, rxBuf.data, rxBuf.size)) { if (WriteData(rxBuf.address, rxBuf.data, rxBuf.size)) {
txBuf.type = PROGRAM_ACK; txBuf.type = PROGRAM_ACK;
txBuf.size = rxBuf.size; txBuf.size = rxBuf.size;
} else { } else {
@ -119,7 +113,7 @@ void ProcessPacket()
break; break;
case PROGRAM_READ: case PROGRAM_READ:
if(ReadData(rxBuf.address, txBuf.data, rxBuf.size)) { if (ReadData(rxBuf.address, txBuf.data, rxBuf.size)) {
txBuf.type = PROGRAM_ACK; txBuf.type = PROGRAM_ACK;
txBuf.size = rxBuf.size; txBuf.size = rxBuf.size;
} else { } else {
@ -128,10 +122,11 @@ void ProcessPacket()
break; break;
case PROGRAM_REBOOT: case PROGRAM_REBOOT:
if(0 == memcmp(rxBuf.data,REBOOT_CONFIRMATION,REBOOT_CONFIRMATION_LENGTH)) { if (0 == memcmp(rxBuf.data, REBOOT_CONFIRMATION,
REBOOT_CONFIRMATION_LENGTH)) {
done = true; done = true;
txBuf.type = PROGRAM_ACK; txBuf.type = PROGRAM_ACK;
}else{ } else {
txBuf.type = PROGRAM_ERR; txBuf.type = PROGRAM_ERR;
} }
break; break;

View File

@ -29,12 +29,11 @@ struct lfsm_context {
static struct lfsm_context context = { 0 }; static struct lfsm_context context = { 0 };
static void lfsm_update_link_tx (struct lfsm_context * context); static void lfsm_update_link_tx(struct lfsm_context * context);
static void lfsm_init_rx (struct lfsm_context * context); static void lfsm_init_rx(struct lfsm_context * context);
static uint32_t PIOS_SPI_OP; static uint32_t PIOS_SPI_OP;
void lfsm_attach(uint32_t spi_id) void lfsm_attach(uint32_t spi_id) {
{
PIOS_SPI_OP = spi_id; PIOS_SPI_OP = spi_id;
} }
@ -67,23 +66,20 @@ static void go_user_rxtx_active(struct lfsm_context * context);
const static struct lfsm_transition lfsm_transitions[LFSM_STATE_NUM_STATES] = { const static struct lfsm_transition lfsm_transitions[LFSM_STATE_NUM_STATES] = {
[LFSM_STATE_FAULTED] = { [LFSM_STATE_FAULTED] = {
.entry_fn = go_faulted, .entry_fn = go_faulted,
}, }, [LFSM_STATE_STOPPED] = {
[LFSM_STATE_STOPPED] = {
.entry_fn = go_stopped, .entry_fn = go_stopped,
.next_state = { .next_state = {
[LFSM_EVENT_INIT_LINK] = LFSM_STATE_INACTIVE, [LFSM_EVENT_INIT_LINK] = LFSM_STATE_INACTIVE,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_STOPPED, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_STOPPED,
}, },
}, }, [LFSM_STATE_STOPPING] = {
[LFSM_STATE_STOPPING] = {
.entry_fn = go_stopping, .entry_fn = go_stopping,
.next_state = { .next_state = {
[LFSM_EVENT_RX_LINK] = LFSM_STATE_STOPPED, [LFSM_EVENT_RX_LINK] = LFSM_STATE_STOPPED,
[LFSM_EVENT_RX_USER] = LFSM_STATE_STOPPED, [LFSM_EVENT_RX_USER] = LFSM_STATE_STOPPED,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_STOPPED, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_STOPPED,
}, },
}, }, [LFSM_STATE_INACTIVE] = {
[LFSM_STATE_INACTIVE] = {
.entry_fn = go_inactive, .entry_fn = go_inactive,
.next_state = { .next_state = {
[LFSM_EVENT_STOP] = LFSM_STATE_STOPPING, [LFSM_EVENT_STOP] = LFSM_STATE_STOPPING,
@ -93,8 +89,7 @@ const static struct lfsm_transition lfsm_transitions[LFSM_STATE_NUM_STATES] = {
[LFSM_EVENT_RX_USER] = LFSM_STATE_INACTIVE, [LFSM_EVENT_RX_USER] = LFSM_STATE_INACTIVE,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_INACTIVE, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_INACTIVE,
}, },
}, }, [LFSM_STATE_USER_BUSY] = {
[LFSM_STATE_USER_BUSY] = {
.entry_fn = go_user_busy, .entry_fn = go_user_busy,
.next_state = { .next_state = {
[LFSM_EVENT_STOP] = LFSM_STATE_STOPPING, [LFSM_EVENT_STOP] = LFSM_STATE_STOPPING,
@ -105,8 +100,7 @@ const static struct lfsm_transition lfsm_transitions[LFSM_STATE_NUM_STATES] = {
[LFSM_EVENT_RX_USER] = LFSM_STATE_USER_BUSY, [LFSM_EVENT_RX_USER] = LFSM_STATE_USER_BUSY,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_BUSY, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_BUSY,
}, },
}, }, [LFSM_STATE_USER_BUSY_RX_PENDING] = {
[LFSM_STATE_USER_BUSY_RX_PENDING] = {
.entry_fn = go_user_busy_rx_pending, .entry_fn = go_user_busy_rx_pending,
.next_state = { .next_state = {
[LFSM_EVENT_USER_SET_TX] = LFSM_STATE_USER_BUSY_RXTX_PENDING, [LFSM_EVENT_USER_SET_TX] = LFSM_STATE_USER_BUSY_RXTX_PENDING,
@ -115,8 +109,7 @@ const static struct lfsm_transition lfsm_transitions[LFSM_STATE_NUM_STATES] = {
[LFSM_EVENT_RX_USER] = LFSM_STATE_USER_BUSY_RX_PENDING, [LFSM_EVENT_RX_USER] = LFSM_STATE_USER_BUSY_RX_PENDING,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_BUSY_RX_PENDING, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_BUSY_RX_PENDING,
}, },
}, }, [LFSM_STATE_USER_BUSY_TX_PENDING] = {
[LFSM_STATE_USER_BUSY_TX_PENDING] = {
.entry_fn = go_user_busy_tx_pending, .entry_fn = go_user_busy_tx_pending,
.next_state = { .next_state = {
[LFSM_EVENT_USER_SET_RX] = LFSM_STATE_USER_BUSY_RXTX_PENDING, [LFSM_EVENT_USER_SET_RX] = LFSM_STATE_USER_BUSY_RXTX_PENDING,
@ -125,8 +118,7 @@ const static struct lfsm_transition lfsm_transitions[LFSM_STATE_NUM_STATES] = {
[LFSM_EVENT_RX_USER] = LFSM_STATE_USER_BUSY_TX_PENDING, [LFSM_EVENT_RX_USER] = LFSM_STATE_USER_BUSY_TX_PENDING,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_BUSY_TX_PENDING, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_BUSY_TX_PENDING,
}, },
}, }, [LFSM_STATE_USER_BUSY_RXTX_PENDING] = {
[LFSM_STATE_USER_BUSY_RXTX_PENDING] = {
.entry_fn = go_user_busy_rxtx_pending, .entry_fn = go_user_busy_rxtx_pending,
.next_state = { .next_state = {
[LFSM_EVENT_USER_DONE] = LFSM_STATE_USER_RXTX_PENDING, [LFSM_EVENT_USER_DONE] = LFSM_STATE_USER_RXTX_PENDING,
@ -134,81 +126,70 @@ const static struct lfsm_transition lfsm_transitions[LFSM_STATE_NUM_STATES] = {
[LFSM_EVENT_RX_USER] = LFSM_STATE_USER_BUSY_RXTX_PENDING, [LFSM_EVENT_RX_USER] = LFSM_STATE_USER_BUSY_RXTX_PENDING,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_BUSY_RXTX_PENDING, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_BUSY_RXTX_PENDING,
}, },
}, }, [LFSM_STATE_USER_RX_PENDING] = {
[LFSM_STATE_USER_RX_PENDING] = {
.entry_fn = go_user_rx_pending, .entry_fn = go_user_rx_pending,
.next_state = { .next_state = {
[LFSM_EVENT_RX_LINK] = LFSM_STATE_USER_RX_ACTIVE, [LFSM_EVENT_RX_LINK] = LFSM_STATE_USER_RX_ACTIVE,
[LFSM_EVENT_RX_USER] = LFSM_STATE_USER_RX_ACTIVE, [LFSM_EVENT_RX_USER] = LFSM_STATE_USER_RX_ACTIVE,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_RX_ACTIVE, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_RX_ACTIVE,
}, },
}, }, [LFSM_STATE_USER_TX_PENDING] = {
[LFSM_STATE_USER_TX_PENDING] = {
.entry_fn = go_user_tx_pending, .entry_fn = go_user_tx_pending,
.next_state = { .next_state = {
[LFSM_EVENT_RX_LINK] = LFSM_STATE_USER_TX_ACTIVE, [LFSM_EVENT_RX_LINK] = LFSM_STATE_USER_TX_ACTIVE,
[LFSM_EVENT_RX_USER] = LFSM_STATE_USER_TX_ACTIVE, [LFSM_EVENT_RX_USER] = LFSM_STATE_USER_TX_ACTIVE,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_TX_ACTIVE, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_TX_ACTIVE,
}, },
}, }, [LFSM_STATE_USER_RXTX_PENDING] = {
[LFSM_STATE_USER_RXTX_PENDING] = {
.entry_fn = go_user_rxtx_pending, .entry_fn = go_user_rxtx_pending,
.next_state = { .next_state = {
[LFSM_EVENT_RX_LINK] = LFSM_STATE_USER_RXTX_ACTIVE, [LFSM_EVENT_RX_LINK] = LFSM_STATE_USER_RXTX_ACTIVE,
[LFSM_EVENT_RX_USER] = LFSM_STATE_USER_RXTX_ACTIVE, [LFSM_EVENT_RX_USER] = LFSM_STATE_USER_RXTX_ACTIVE,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_RXTX_ACTIVE, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_RXTX_ACTIVE,
}, },
}, }, [LFSM_STATE_USER_RX_ACTIVE] = {
[LFSM_STATE_USER_RX_ACTIVE] = {
.entry_fn = go_user_rx_active, .entry_fn = go_user_rx_active,
.next_state = { .next_state = {
[LFSM_EVENT_RX_LINK] = LFSM_STATE_USER_RX_ACTIVE, [LFSM_EVENT_RX_LINK] = LFSM_STATE_USER_RX_ACTIVE,
[LFSM_EVENT_RX_USER] = LFSM_STATE_USER_BUSY, [LFSM_EVENT_RX_USER] = LFSM_STATE_USER_BUSY,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_RX_ACTIVE, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_RX_ACTIVE,
}, },
}, }, [LFSM_STATE_USER_TX_ACTIVE] = {
[LFSM_STATE_USER_TX_ACTIVE] = {
.entry_fn = go_user_tx_active, .entry_fn = go_user_tx_active,
.next_state = { .next_state = {
[LFSM_EVENT_RX_LINK] = LFSM_STATE_INACTIVE, [LFSM_EVENT_RX_LINK] = LFSM_STATE_INACTIVE,
[LFSM_EVENT_RX_USER] = LFSM_STATE_INACTIVE, [LFSM_EVENT_RX_USER] = LFSM_STATE_INACTIVE,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_INACTIVE, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_INACTIVE,
}, },
}, }, [LFSM_STATE_USER_RXTX_ACTIVE] = {
[LFSM_STATE_USER_RXTX_ACTIVE] = {
.entry_fn = go_user_rxtx_active, .entry_fn = go_user_rxtx_active,
.next_state = { .next_state = {
[LFSM_EVENT_RX_LINK] = LFSM_STATE_USER_RX_ACTIVE, [LFSM_EVENT_RX_LINK] = LFSM_STATE_USER_RX_ACTIVE,
[LFSM_EVENT_RX_USER] = LFSM_STATE_USER_BUSY, [LFSM_EVENT_RX_USER] = LFSM_STATE_USER_BUSY,
[LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_RX_ACTIVE, [LFSM_EVENT_RX_UNKNOWN] = LFSM_STATE_USER_RX_ACTIVE,
}, },
}, }, };
};
/* /*
* FSM State Entry Functions * FSM State Entry Functions
*/ */
static void go_faulted(struct lfsm_context * context) static void go_faulted(struct lfsm_context * context) {
{
PIOS_DEBUG_Assert(0); PIOS_DEBUG_Assert(0);
} }
static void go_stopped(struct lfsm_context * context) static void go_stopped(struct lfsm_context * context) {
{
#if 0 #if 0
PIOS_SPI_Stop(PIOS_SPI_OP); PIOS_SPI_Stop(PIOS_SPI_OP);
#endif #endif
} }
static void go_stopping(struct lfsm_context * context) static void go_stopping(struct lfsm_context * context) {
{
context->link_tx = NULL; context->link_tx = NULL;
context->tx = NULL; context->tx = NULL;
} }
static void go_inactive(struct lfsm_context * context) static void go_inactive(struct lfsm_context * context) {
{
context->link_state = OPAHRS_MSG_LINK_STATE_INACTIVE; context->link_state = OPAHRS_MSG_LINK_STATE_INACTIVE;
lfsm_update_link_tx(context); lfsm_update_link_tx(context);
@ -219,11 +200,11 @@ static void go_inactive(struct lfsm_context * context)
context->tx = context->link_tx; context->tx = context->link_tx;
lfsm_init_rx(context); lfsm_init_rx(context);
PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx, context->user_payload_len, lfsm_irq_callback); PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx,
context->user_payload_len, lfsm_irq_callback);
} }
static void go_user_busy(struct lfsm_context * context) static void go_user_busy(struct lfsm_context * context) {
{
/* Sanity checks */ /* Sanity checks */
PIOS_DEBUG_Assert(context->user_rx); PIOS_DEBUG_Assert(context->user_rx);
@ -237,11 +218,11 @@ static void go_user_busy(struct lfsm_context * context)
context->tx = context->link_tx; context->tx = context->link_tx;
lfsm_init_rx(context); lfsm_init_rx(context);
PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx, context->user_payload_len, lfsm_irq_callback); PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx,
context->user_payload_len, lfsm_irq_callback);
} }
static void go_user_busy_rx_pending(struct lfsm_context * context) static void go_user_busy_rx_pending(struct lfsm_context * context) {
{
/* Sanity checks */ /* Sanity checks */
PIOS_DEBUG_Assert(context->user_rx); PIOS_DEBUG_Assert(context->user_rx);
@ -252,11 +233,11 @@ static void go_user_busy_rx_pending(struct lfsm_context * context)
context->tx = context->link_tx; context->tx = context->link_tx;
lfsm_init_rx(context); lfsm_init_rx(context);
PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx, context->user_payload_len, lfsm_irq_callback); PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx,
context->user_payload_len, lfsm_irq_callback);
} }
static void go_user_busy_tx_pending(struct lfsm_context * context) static void go_user_busy_tx_pending(struct lfsm_context * context) {
{
/* Sanity checks */ /* Sanity checks */
PIOS_DEBUG_Assert(context->user_tx); PIOS_DEBUG_Assert(context->user_tx);
@ -267,14 +248,13 @@ static void go_user_busy_tx_pending(struct lfsm_context * context)
context->tx = context->link_tx; context->tx = context->link_tx;
lfsm_init_rx(context); lfsm_init_rx(context);
PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx, context->user_payload_len, lfsm_irq_callback); PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx,
context->user_payload_len, lfsm_irq_callback);
} }
static void go_user_busy_rxtx_pending(struct lfsm_context * context) static void go_user_busy_rxtx_pending(struct lfsm_context * context) {
{
/* Sanity checks */ /* Sanity checks */
PIOS_DEBUG_Assert(context->user_rx); PIOS_DEBUG_Assert(context->user_rx); PIOS_DEBUG_Assert(context->user_tx);
PIOS_DEBUG_Assert(context->user_tx);
context->link_state = OPAHRS_MSG_LINK_STATE_BUSY; context->link_state = OPAHRS_MSG_LINK_STATE_BUSY;
lfsm_update_link_tx(context); lfsm_update_link_tx(context);
@ -283,11 +263,11 @@ static void go_user_busy_rxtx_pending(struct lfsm_context * context)
context->tx = context->link_tx; context->tx = context->link_tx;
lfsm_init_rx(context); lfsm_init_rx(context);
PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx, context->user_payload_len, lfsm_irq_callback); PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx,
context->user_payload_len, lfsm_irq_callback);
} }
static void go_user_rx_pending(struct lfsm_context * context) static void go_user_rx_pending(struct lfsm_context * context) {
{
/* Sanity checks */ /* Sanity checks */
PIOS_DEBUG_Assert(context->user_rx); PIOS_DEBUG_Assert(context->user_rx);
@ -298,11 +278,11 @@ static void go_user_rx_pending(struct lfsm_context * context)
context->tx = context->link_tx; context->tx = context->link_tx;
lfsm_init_rx(context); lfsm_init_rx(context);
PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx, context->user_payload_len, lfsm_irq_callback); PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx,
context->user_payload_len, lfsm_irq_callback);
} }
static void go_user_tx_pending(struct lfsm_context * context) static void go_user_tx_pending(struct lfsm_context * context) {
{
/* Sanity checks */ /* Sanity checks */
PIOS_DEBUG_Assert(context->user_tx); PIOS_DEBUG_Assert(context->user_tx);
@ -313,14 +293,13 @@ static void go_user_tx_pending(struct lfsm_context * context)
context->tx = context->link_tx; context->tx = context->link_tx;
lfsm_init_rx(context); lfsm_init_rx(context);
PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx, context->user_payload_len, lfsm_irq_callback); PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx,
context->user_payload_len, lfsm_irq_callback);
} }
static void go_user_rxtx_pending(struct lfsm_context * context) static void go_user_rxtx_pending(struct lfsm_context * context) {
{
/* Sanity checks */ /* Sanity checks */
PIOS_DEBUG_Assert(context->user_rx); PIOS_DEBUG_Assert(context->user_rx); PIOS_DEBUG_Assert(context->user_tx);
PIOS_DEBUG_Assert(context->user_tx);
context->link_state = OPAHRS_MSG_LINK_STATE_BUSY; context->link_state = OPAHRS_MSG_LINK_STATE_BUSY;
lfsm_update_link_tx(context); lfsm_update_link_tx(context);
@ -329,11 +308,11 @@ static void go_user_rxtx_pending(struct lfsm_context * context)
context->tx = context->link_tx; context->tx = context->link_tx;
lfsm_init_rx(context); lfsm_init_rx(context);
PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx, context->user_payload_len, lfsm_irq_callback); PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx,
context->user_payload_len, lfsm_irq_callback);
} }
static void go_user_rx_active(struct lfsm_context * context) static void go_user_rx_active(struct lfsm_context * context) {
{
/* Sanity checks */ /* Sanity checks */
PIOS_DEBUG_Assert(context->user_rx); PIOS_DEBUG_Assert(context->user_rx);
@ -343,11 +322,11 @@ static void go_user_rx_active(struct lfsm_context * context)
lfsm_update_link_tx(context); lfsm_update_link_tx(context);
lfsm_init_rx(context); lfsm_init_rx(context);
PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx, context->user_payload_len, lfsm_irq_callback); PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx,
context->user_payload_len, lfsm_irq_callback);
} }
static void go_user_tx_active(struct lfsm_context * context) static void go_user_tx_active(struct lfsm_context * context) {
{
/* Sanity checks */ /* Sanity checks */
PIOS_DEBUG_Assert(context->user_tx); PIOS_DEBUG_Assert(context->user_tx);
@ -356,21 +335,21 @@ static void go_user_tx_active(struct lfsm_context * context)
context->tx = context->user_tx; context->tx = context->user_tx;
lfsm_init_rx(context); lfsm_init_rx(context);
PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx, context->user_payload_len, lfsm_irq_callback); PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx,
context->user_payload_len, lfsm_irq_callback);
} }
static void go_user_rxtx_active(struct lfsm_context * context) static void go_user_rxtx_active(struct lfsm_context * context) {
{
/* Sanity checks */ /* Sanity checks */
PIOS_DEBUG_Assert(context->user_rx); PIOS_DEBUG_Assert(context->user_rx); PIOS_DEBUG_Assert(context->user_tx);
PIOS_DEBUG_Assert(context->user_tx);
context->link_state = OPAHRS_MSG_LINK_STATE_READY; context->link_state = OPAHRS_MSG_LINK_STATE_READY;
context->rx = context->user_rx; context->rx = context->user_rx;
context->tx = context->user_tx; context->tx = context->user_tx;
lfsm_init_rx(context); lfsm_init_rx(context);
PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx, context->user_payload_len, lfsm_irq_callback); PIOS_SPI_TransferBlock(PIOS_SPI_OP, context->tx, context->rx,
context->user_payload_len, lfsm_irq_callback);
} }
/* /*
@ -379,48 +358,48 @@ static void go_user_rxtx_active(struct lfsm_context * context)
* *
*/ */
static void lfsm_update_link_tx_v0 (struct opahrs_msg_v0 * msg, enum opahrs_msg_link_state state, uint16_t errors) static void lfsm_update_link_tx_v0(struct opahrs_msg_v0 * msg,
{ enum opahrs_msg_link_state state, uint16_t errors) {
opahrs_msg_v0_init_link_tx(msg, OPAHRS_MSG_LINK_TAG_NOP); opahrs_msg_v0_init_link_tx(msg, OPAHRS_MSG_LINK_TAG_NOP);
msg->payload.link.state = state; msg->payload.link.state = state;
msg->payload.link.errors = errors; msg->payload.link.errors = errors;
} }
static void lfsm_update_link_tx_v1 (struct opahrs_msg_v1 * msg, enum opahrs_msg_link_state state, uint16_t errors) static void lfsm_update_link_tx_v1(struct opahrs_msg_v1 * msg,
{ enum opahrs_msg_link_state state, uint16_t errors) {
opahrs_msg_v1_init_link_tx(msg, OPAHRS_MSG_LINK_TAG_NOP); opahrs_msg_v1_init_link_tx(msg, OPAHRS_MSG_LINK_TAG_NOP);
msg->payload.link.state = state; msg->payload.link.state = state;
msg->payload.link.errors = errors; msg->payload.link.errors = errors;
} }
static void lfsm_update_link_tx (struct lfsm_context * context) static void lfsm_update_link_tx(struct lfsm_context * context) {
{
PIOS_DEBUG_Assert(context->link_tx); PIOS_DEBUG_Assert(context->link_tx);
switch (context->user_payload_type) { switch (context->user_payload_type) {
case OPAHRS_MSG_TYPE_USER_V0: case OPAHRS_MSG_TYPE_USER_V0:
lfsm_update_link_tx_v0 ((struct opahrs_msg_v0 *)context->link_tx, context->link_state, context->errors); lfsm_update_link_tx_v0((struct opahrs_msg_v0 *) context->link_tx,
context->link_state, context->errors);
break; break;
case OPAHRS_MSG_TYPE_USER_V1: case OPAHRS_MSG_TYPE_USER_V1:
lfsm_update_link_tx_v1 ((struct opahrs_msg_v1 *)context->link_tx, context->link_state, context->errors); lfsm_update_link_tx_v1((struct opahrs_msg_v1 *) context->link_tx,
context->link_state, context->errors);
break; break;
case OPAHRS_MSG_TYPE_LINK: case OPAHRS_MSG_TYPE_LINK:
PIOS_DEBUG_Assert(0); PIOS_DEBUG_Assert(0);
} }
} }
static void lfsm_init_rx (struct lfsm_context * context) static void lfsm_init_rx(struct lfsm_context * context) {
{
PIOS_DEBUG_Assert(context->rx); PIOS_DEBUG_Assert(context->rx);
switch (context->user_payload_type) { switch (context->user_payload_type) {
case OPAHRS_MSG_TYPE_USER_V0: case OPAHRS_MSG_TYPE_USER_V0:
opahrs_msg_v0_init_rx ((struct opahrs_msg_v0 *)context->rx); opahrs_msg_v0_init_rx((struct opahrs_msg_v0 *) context->rx);
break; break;
case OPAHRS_MSG_TYPE_USER_V1: case OPAHRS_MSG_TYPE_USER_V1:
opahrs_msg_v1_init_rx ((struct opahrs_msg_v1 *)context->rx); opahrs_msg_v1_init_rx((struct opahrs_msg_v1 *) context->rx);
break; break;
case OPAHRS_MSG_TYPE_LINK: case OPAHRS_MSG_TYPE_LINK:
PIOS_DEBUG_Assert(0); PIOS_DEBUG_Assert(0);
@ -433,8 +412,7 @@ static void lfsm_init_rx (struct lfsm_context * context)
* *
*/ */
void lfsm_inject_event(enum lfsm_event event) void lfsm_inject_event(enum lfsm_event event) {
{
PIOS_IRQ_Disable(); PIOS_IRQ_Disable();
/* /*
@ -453,18 +431,17 @@ void lfsm_inject_event(enum lfsm_event event)
PIOS_IRQ_Enable(); PIOS_IRQ_Enable();
} }
void lfsm_init(void) void lfsm_init(void) {
{
context.curr_state = LFSM_STATE_STOPPED; context.curr_state = LFSM_STATE_STOPPED;
go_stopped(&context); go_stopped(&context);
} }
void lfsm_set_link_proto_v0 (struct opahrs_msg_v0 * link_tx, struct opahrs_msg_v0 * link_rx) void lfsm_set_link_proto_v0(struct opahrs_msg_v0 * link_tx,
{ struct opahrs_msg_v0 * link_rx) {
PIOS_DEBUG_Assert(link_tx); PIOS_DEBUG_Assert(link_tx);
context.link_tx = (uint8_t *)link_tx; context.link_tx = (uint8_t *) link_tx;
context.link_rx = (uint8_t *)link_rx; context.link_rx = (uint8_t *) link_rx;
context.user_payload_type = OPAHRS_MSG_TYPE_USER_V0; context.user_payload_type = OPAHRS_MSG_TYPE_USER_V0;
context.user_payload_len = sizeof(*link_tx); context.user_payload_len = sizeof(*link_tx);
@ -473,12 +450,12 @@ void lfsm_set_link_proto_v0 (struct opahrs_msg_v0 * link_tx, struct opahrs_msg_v
lfsm_inject_event(LFSM_EVENT_INIT_LINK); lfsm_inject_event(LFSM_EVENT_INIT_LINK);
} }
void lfsm_set_link_proto_v1 (struct opahrs_msg_v1 * link_tx, struct opahrs_msg_v1 * link_rx) void lfsm_set_link_proto_v1(struct opahrs_msg_v1 * link_tx,
{ struct opahrs_msg_v1 * link_rx) {
PIOS_DEBUG_Assert(link_tx); PIOS_DEBUG_Assert(link_tx);
context.link_tx = (uint8_t *)link_tx; context.link_tx = (uint8_t *) link_tx;
context.link_rx = (uint8_t *)link_rx; context.link_rx = (uint8_t *) link_rx;
context.user_payload_type = OPAHRS_MSG_TYPE_USER_V1; context.user_payload_type = OPAHRS_MSG_TYPE_USER_V1;
context.user_payload_len = sizeof(*link_tx); context.user_payload_len = sizeof(*link_tx);
@ -487,65 +464,54 @@ void lfsm_set_link_proto_v1 (struct opahrs_msg_v1 * link_tx, struct opahrs_msg_v
lfsm_inject_event(LFSM_EVENT_INIT_LINK); lfsm_inject_event(LFSM_EVENT_INIT_LINK);
} }
void lfsm_user_set_tx_v0 (struct opahrs_msg_v0 * user_tx) void lfsm_user_set_tx_v0(struct opahrs_msg_v0 * user_tx) {
{
PIOS_DEBUG_Assert(user_tx); PIOS_DEBUG_Assert(user_tx);
PIOS_DEBUG_Assert(context.user_payload_type == OPAHRS_MSG_TYPE_USER_V0); PIOS_DEBUG_Assert(context.user_payload_type == OPAHRS_MSG_TYPE_USER_V0);
context.user_tx = (uint8_t *)user_tx; context.user_tx = (uint8_t *) user_tx;
lfsm_inject_event(LFSM_EVENT_USER_SET_TX); lfsm_inject_event(LFSM_EVENT_USER_SET_TX);
} }
void lfsm_user_set_rx_v0 (struct opahrs_msg_v0 * user_rx) void lfsm_user_set_rx_v0(struct opahrs_msg_v0 * user_rx) {
{ PIOS_DEBUG_Assert(user_rx); PIOS_DEBUG_Assert(context.user_payload_type == OPAHRS_MSG_TYPE_USER_V0);
PIOS_DEBUG_Assert(user_rx);
PIOS_DEBUG_Assert(context.user_payload_type == OPAHRS_MSG_TYPE_USER_V0);
context.user_rx = (uint8_t *)user_rx; context.user_rx = (uint8_t *) user_rx;
lfsm_inject_event(LFSM_EVENT_USER_SET_RX); lfsm_inject_event(LFSM_EVENT_USER_SET_RX);
} }
void lfsm_user_set_tx_v1 (struct opahrs_msg_v1 * user_tx) void lfsm_user_set_tx_v1(struct opahrs_msg_v1 * user_tx) {
{ PIOS_DEBUG_Assert(user_tx); PIOS_DEBUG_Assert(context.user_payload_type == OPAHRS_MSG_TYPE_USER_V1);
PIOS_DEBUG_Assert(user_tx);
PIOS_DEBUG_Assert(context.user_payload_type == OPAHRS_MSG_TYPE_USER_V1);
context.user_tx = (uint8_t *)user_tx; context.user_tx = (uint8_t *) user_tx;
lfsm_inject_event(LFSM_EVENT_USER_SET_TX); lfsm_inject_event(LFSM_EVENT_USER_SET_TX);
} }
void lfsm_user_set_rx_v1 (struct opahrs_msg_v1 * user_rx) void lfsm_user_set_rx_v1(struct opahrs_msg_v1 * user_rx) {
{ PIOS_DEBUG_Assert(user_rx); PIOS_DEBUG_Assert(context.user_payload_type == OPAHRS_MSG_TYPE_USER_V1);
PIOS_DEBUG_Assert(user_rx);
PIOS_DEBUG_Assert(context.user_payload_type == OPAHRS_MSG_TYPE_USER_V1);
context.user_rx = (uint8_t *)user_rx; context.user_rx = (uint8_t *) user_rx;
lfsm_inject_event(LFSM_EVENT_USER_SET_RX); lfsm_inject_event(LFSM_EVENT_USER_SET_RX);
} }
void lfsm_user_done (void) void lfsm_user_done(void) {
{
lfsm_inject_event(LFSM_EVENT_USER_DONE); lfsm_inject_event(LFSM_EVENT_USER_DONE);
} }
void lfsm_stop (void) void lfsm_stop(void) {
{
lfsm_inject_event(LFSM_EVENT_STOP); lfsm_inject_event(LFSM_EVENT_STOP);
} }
void lfsm_get_link_stats (struct lfsm_link_stats * stats) void lfsm_get_link_stats(struct lfsm_link_stats * stats) {
{
PIOS_DEBUG_Assert(stats); PIOS_DEBUG_Assert(stats);
*stats = context.stats; *stats = context.stats;
} }
enum lfsm_state lfsm_get_state (void) enum lfsm_state lfsm_get_state(void) {
{
return context.curr_state; return context.curr_state;
} }
@ -555,32 +521,31 @@ enum lfsm_state lfsm_get_state (void)
* *
*/ */
void lfsm_irq_callback(uint8_t crc_ok, uint8_t crc_val) void lfsm_irq_callback(uint8_t crc_ok, uint8_t crc_val) {
{
if (!crc_ok) { if (!crc_ok) {
context.stats.rx_badcrc++; context.stats.rx_badcrc++;
lfsm_inject_event (LFSM_EVENT_RX_UNKNOWN); lfsm_inject_event(LFSM_EVENT_RX_UNKNOWN);
return; return;
} }
if (!context.rx) { if (!context.rx) {
/* No way to know what we just received, assume invalid */ /* No way to know what we just received, assume invalid */
lfsm_inject_event (LFSM_EVENT_RX_UNKNOWN); lfsm_inject_event(LFSM_EVENT_RX_UNKNOWN);
return; return;
} }
/* Recover the head and tail pointers from the message */ /* Recover the head and tail pointers from the message */
struct opahrs_msg_link_head * head=0; struct opahrs_msg_link_head * head = 0;
struct opahrs_msg_link_tail * tail=0; struct opahrs_msg_link_tail * tail = 0;
switch (context.user_payload_type) { switch (context.user_payload_type) {
case OPAHRS_MSG_TYPE_USER_V0: case OPAHRS_MSG_TYPE_USER_V0:
head = &((struct opahrs_msg_v0 *)context.rx)->head; head = &((struct opahrs_msg_v0 *) context.rx)->head;
tail = &((struct opahrs_msg_v0 *)context.rx)->tail; tail = &((struct opahrs_msg_v0 *) context.rx)->tail;
break; break;
case OPAHRS_MSG_TYPE_USER_V1: case OPAHRS_MSG_TYPE_USER_V1:
head = &((struct opahrs_msg_v1 *)context.rx)->head; head = &((struct opahrs_msg_v1 *) context.rx)->head;
tail = &((struct opahrs_msg_v1 *)context.rx)->tail; tail = &((struct opahrs_msg_v1 *) context.rx)->tail;
break; break;
case OPAHRS_MSG_TYPE_LINK: case OPAHRS_MSG_TYPE_LINK:
/* Should never be rx'ing before the link protocol version is known */ /* Should never be rx'ing before the link protocol version is known */
@ -589,15 +554,15 @@ void lfsm_irq_callback(uint8_t crc_ok, uint8_t crc_val)
} }
/* Check for bad magic */ /* Check for bad magic */
if ((head->magic != OPAHRS_MSG_MAGIC_HEAD) || if ((head->magic != OPAHRS_MSG_MAGIC_HEAD) || (tail->magic
(tail->magic != OPAHRS_MSG_MAGIC_TAIL)) { != OPAHRS_MSG_MAGIC_TAIL)) {
if (head->magic != OPAHRS_MSG_MAGIC_HEAD) { if (head->magic != OPAHRS_MSG_MAGIC_HEAD) {
context.stats.rx_badmagic_head++; context.stats.rx_badmagic_head++;
} }
if (tail->magic != OPAHRS_MSG_MAGIC_TAIL) { if (tail->magic != OPAHRS_MSG_MAGIC_TAIL) {
context.stats.rx_badmagic_tail++; context.stats.rx_badmagic_tail++;
} }
lfsm_inject_event (LFSM_EVENT_RX_UNKNOWN); lfsm_inject_event(LFSM_EVENT_RX_UNKNOWN);
return; return;
} }
@ -605,22 +570,22 @@ void lfsm_irq_callback(uint8_t crc_ok, uint8_t crc_val)
switch (head->type) { switch (head->type) {
case OPAHRS_MSG_TYPE_LINK: case OPAHRS_MSG_TYPE_LINK:
context.stats.rx_link++; context.stats.rx_link++;
lfsm_inject_event (LFSM_EVENT_RX_LINK); lfsm_inject_event(LFSM_EVENT_RX_LINK);
break; break;
case OPAHRS_MSG_TYPE_USER_V0: case OPAHRS_MSG_TYPE_USER_V0:
case OPAHRS_MSG_TYPE_USER_V1: case OPAHRS_MSG_TYPE_USER_V1:
if (head->type == context.user_payload_type) { if (head->type == context.user_payload_type) {
context.stats.rx_user++; context.stats.rx_user++;
lfsm_inject_event (LFSM_EVENT_RX_USER); lfsm_inject_event(LFSM_EVENT_RX_USER);
} else { } else {
/* Mismatched user payload type */ /* Mismatched user payload type */
context.stats.rx_badver++; context.stats.rx_badver++;
lfsm_inject_event (LFSM_EVENT_RX_UNKNOWN); lfsm_inject_event(LFSM_EVENT_RX_UNKNOWN);
} }
break; break;
default: default:
/* Unidentifiable payload type */ /* Unidentifiable payload type */
context.stats.rx_badtype++; context.stats.rx_badtype++;
lfsm_inject_event (LFSM_EVENT_RX_UNKNOWN); lfsm_inject_event(LFSM_EVENT_RX_UNKNOWN);
} }
} }

View File

@ -23,31 +23,30 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#ifndef AHRS_BL_H #ifndef AHRS_BL_H
#define AHRS_BL_H #define AHRS_BL_H
/* PIOS Includes */ /* PIOS Includes */
#include <pios.h> #include <pios.h>
/** Start programming /** Start programming
returns: true if FLASH erased and ready to program returns: true if FLASH erased and ready to program
*/ */
bool StartProgramming(void); bool
StartProgramming(void);
/** Write a block to FLASH /** Write a block to FLASH
buffer contains the data to be written buffer contains the data to be written
returns: true if FLASH programmed correctly returns: true if FLASH programmed correctly
*/ */
bool WriteData(uint32_t offset, uint8_t *buffer, uint32_t size); bool
WriteData(uint32_t offset, uint8_t *buffer, uint32_t size);
/** Read a block from FLASH /** Read a block from FLASH
returns: true if FLASH read correctly. returns: true if FLASH read correctly.
Buffer is set to the read data Buffer is set to the read data
*/ */
bool ReadData(uint32_t offset, uint8_t *buffer, uint32_t size); bool
ReadData(uint32_t offset, uint8_t *buffer, uint32_t size);
#endif /* AHRS_BL_H */ #endif /* AHRS_BL_H */

View File

@ -2,47 +2,53 @@
#define AHRS_SPI_PROGRAM_H #define AHRS_SPI_PROGRAM_H
/* Special packets to enter programming mode. /* Special packets to enter programming mode.
Note: these must both be SPI_PROGRAM_REQUEST_LENGTH long. Note: these must both be SPI_PROGRAM_REQUEST_LENGTH long.
Pad with spaces if needed. Pad with spaces if needed.
*/ */
#define SPI_PROGRAM_REQUEST "AHRS START PROGRAMMING " #define SPI_PROGRAM_REQUEST "AHRS START PROGRAMMING "
#define SPI_PROGRAM_ACK "AHRS PROGRAMMING STARTED" #define SPI_PROGRAM_ACK "AHRS PROGRAMMING STARTED"
#define SPI_PROGRAM_REQUEST_LENGTH 24 #define SPI_PROGRAM_REQUEST_LENGTH 24
/**Proposed programming protocol: /**Proposed programming protocol:
In the master: In the master:
1) Send a AhrsProgramPacket containing the relevant data. 1) Send a AhrsProgramPacket containing the relevant data.
Note crc is a CRC32 as the CRC8 used in hardware can be fooled. Note crc is a CRC32 as the CRC8 used in hardware can be fooled.
2) Keep sending PROGRAM_NULL packets and wait for an ack. 2) Keep sending PROGRAM_NULL packets and wait for an ack.
Time out if we waited too long. Time out if we waited too long.
3) Compare ack packet with transmitted packet. The data 3) Compare ack packet with transmitted packet. The data
should be the bitwise inverse of the data transmitted. should be the bitwise inverse of the data transmitted.
packetId should correspond to the transmitted packet. packetId should correspond to the transmitted packet.
4) repeat for next packet until finished 4) repeat for next packet until finished
5) Repeat using verify packets 5) Repeat using verify packets
Returned data should be exactly as read from memory Returned data should be exactly as read from memory
In the slave: In the slave:
1) Wait for an AhrsProgramPacket 1) Wait for an AhrsProgramPacket
2) Check CRC then write to memory 2) Check CRC then write to memory
3) Bitwise invert data and add it to the return packet 3) Bitwise invert data and add it to the return packet
4) Copy packetId from received packet 4) Copy packetId from received packet
5) Transmit packet. 5) Transmit packet.
6) repeat until we receive a read packet 6) repeat until we receive a read packet
7) read memory as requested until we receive a reboot packet 7) read memory as requested until we receive a reboot packet
Reboot packets had better have some sort of magic number in the data, Reboot packets had better have some sort of magic number in the data,
just to be absolutely sure just to be absolutely sure
*/ */
typedef enum { PROGRAM_NULL, PROGRAM_WRITE, PROGRAM_READ, PROGRAM_ACK, PROGRAM_REBOOT, PROGRAM_ERR} ProgramType; typedef enum {
PROGRAM_NULL,
PROGRAM_WRITE,
PROGRAM_READ,
PROGRAM_ACK,
PROGRAM_REBOOT,
PROGRAM_ERR
} ProgramType;
#define SPI_MAX_PROGRAM_DATA_SIZE (14 * 4) //USB comms uses 14x 32 bit words #define SPI_MAX_PROGRAM_DATA_SIZE (14 * 4) //USB comms uses 14x 32 bit words
#define REBOOT_CONFIRMATION "AHRS REBOOT" #define REBOOT_CONFIRMATION "AHRS REBOOT"
#define REBOOT_CONFIRMATION_LENGTH 11 #define REBOOT_CONFIRMATION_LENGTH 11
/** Proposed program packet defintion /** Proposed program packet defintion
*/ */
typedef struct { typedef struct {
ProgramType type; ProgramType type;
@ -54,6 +60,6 @@ typedef struct {
uint8_t dummy; //for some reason comms trashes the last byte sent uint8_t dummy; //for some reason comms trashes the last byte sent
} AhrsProgramPacket; } AhrsProgramPacket;
uint32_t GenerateCRC (AhrsProgramPacket * packet); uint32_t GenerateCRC(AhrsProgramPacket * packet);
#endif #endif

View File

@ -27,34 +27,37 @@
#ifndef AHRS_PROGRAM_MASTER_H #ifndef AHRS_PROGRAM_MASTER_H
#define AHRS_PROGRAM_MASTER_H #define AHRS_PROGRAM_MASTER_H
typedef enum {PROGRAM_ERR_OK, //OK typedef enum {
PROGRAM_ERR_OK, //OK
PROGRAM_ERR_LINK, //comms error PROGRAM_ERR_LINK, //comms error
PROGRAM_ERR_FUNCTION, //function failed PROGRAM_ERR_FUNCTION,
//function failed
} PROGERR; } PROGERR;
/** Connect to AHRS and request programming mode /** Connect to AHRS and request programming mode
* returns: false if failed. * returns: false if failed.
*/ */
bool AhrsProgramConnect(uint32_t spi_id); bool AhrsProgramConnect(uint32_t spi_id);
/** Write data to AHRS /** Write data to AHRS
* size must be between 1 and SPI_MAX_PROGRAM_DATA_SIZE * size must be between 1 and SPI_MAX_PROGRAM_DATA_SIZE
* returns: error status * returns: error status
*/ */
PROGERR AhrsProgramWrite(uint32_t spi_id, uint32_t address, void * data, uint32_t size); PROGERR AhrsProgramWrite(uint32_t spi_id, uint32_t address, void * data,
uint32_t size);
/** Read data from AHRS /** Read data from AHRS
* size must be between 1 and SPI_MAX_PROGRAM_DATA_SIZE * size must be between 1 and SPI_MAX_PROGRAM_DATA_SIZE
* returns: error status * returns: error status
*/ */
PROGERR AhrsProgramRead(uint32_t spi_id, uint32_t address, void * data, uint32_t size); PROGERR AhrsProgramRead(uint32_t spi_id, uint32_t address, void * data,
uint32_t size);
/** reboot AHRS /** reboot AHRS
* returns: error status * returns: error status
*/ */
PROGERR AhrsProgramReboot(uint32_t spi_id); PROGERR AhrsProgramReboot(uint32_t spi_id);

View File

@ -24,12 +24,12 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#ifndef AHRS_SPI_PROGRAM_SLAVE_H #ifndef AHRS_SPI_PROGRAM_SLAVE_H
#define AHRS_SPI_PROGRAM_SLAVE_H #define AHRS_SPI_PROGRAM_SLAVE_H
/** Check if OpenPilot is trying to program AHRS /** Check if OpenPilot is trying to program AHRS
* If so, it will program the FLASH then return * If so, it will program the FLASH then return
* If not it just returns. * If not it just returns.
*/ */
void AhrsProgramReceive(uint32_t spi_id); void AhrsProgramReceive(uint32_t spi_id);
#endif //AHRS_PROGRAM_SLAVE_H #endif //AHRS_PROGRAM_SLAVE_H

View File

@ -1,4 +1,4 @@
/** /**
****************************************************************************** ******************************************************************************
* *
* @file ahrs_fsm.h * @file ahrs_fsm.h
@ -23,7 +23,6 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#ifndef BL_FSM_H #ifndef BL_FSM_H
#define BL_FSM_H #define BL_FSM_H
@ -45,7 +44,8 @@ enum lfsm_state {
LFSM_STATE_USER_TX_ACTIVE, LFSM_STATE_USER_TX_ACTIVE,
LFSM_STATE_USER_RXTX_ACTIVE, LFSM_STATE_USER_RXTX_ACTIVE,
LFSM_STATE_NUM_STATES /* Must be last */ LFSM_STATE_NUM_STATES
/* Must be last */
}; };
enum lfsm_event { enum lfsm_event {
@ -58,7 +58,8 @@ enum lfsm_event {
LFSM_EVENT_RX_USER, LFSM_EVENT_RX_USER,
LFSM_EVENT_RX_UNKNOWN, LFSM_EVENT_RX_UNKNOWN,
LFSM_EVENT_NUM_EVENTS /* Must be last */ LFSM_EVENT_NUM_EVENTS
/* Must be last */
}; };
struct lfsm_link_stats { struct lfsm_link_stats {
@ -78,17 +79,19 @@ extern void lfsm_inject_event(enum lfsm_event event);
extern void lfsm_irq_callback(uint8_t crc_ok, uint8_t crc_val); extern void lfsm_irq_callback(uint8_t crc_ok, uint8_t crc_val);
extern void lfsm_get_link_stats (struct lfsm_link_stats * stats); extern void lfsm_get_link_stats(struct lfsm_link_stats * stats);
extern enum lfsm_state lfsm_get_state (void); extern enum lfsm_state lfsm_get_state(void);
extern void lfsm_set_link_proto_v0 (struct opahrs_msg_v0 * link_tx, struct opahrs_msg_v0 * link_rx); extern void lfsm_set_link_proto_v0(struct opahrs_msg_v0 * link_tx,
extern void lfsm_user_set_rx_v0 (struct opahrs_msg_v0 * user_rx); struct opahrs_msg_v0 * link_rx);
extern void lfsm_user_set_tx_v0 (struct opahrs_msg_v0 * user_tx); extern void lfsm_user_set_rx_v0(struct opahrs_msg_v0 * user_rx);
extern void lfsm_user_set_tx_v0(struct opahrs_msg_v0 * user_tx);
extern void lfsm_set_link_proto_v1 (struct opahrs_msg_v1 * link_tx, struct opahrs_msg_v1 * link_rx); extern void lfsm_set_link_proto_v1(struct opahrs_msg_v1 * link_tx,
extern void lfsm_user_set_rx_v1 (struct opahrs_msg_v1 * user_rx); struct opahrs_msg_v1 * link_rx);
extern void lfsm_user_set_tx_v1 (struct opahrs_msg_v1 * user_tx); extern void lfsm_user_set_rx_v1(struct opahrs_msg_v1 * user_rx);
extern void lfsm_user_set_tx_v1(struct opahrs_msg_v1 * user_tx);
extern void lfsm_user_done (void); extern void lfsm_user_done(void);
#endif /* BL_FSM_H */ #endif /* BL_FSM_H */

View File

@ -24,7 +24,6 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#ifndef PIOS_CONFIG_H #ifndef PIOS_CONFIG_H
#define PIOS_CONFIG_H #define PIOS_CONFIG_H

View File

@ -89,8 +89,7 @@ int main() {
if (GO_dfu == FALSE) { if (GO_dfu == FALSE) {
jump_to_app(); jump_to_app();
} }
if(PIOS_IAP_CheckRequest()) if (PIOS_IAP_CheckRequest()) {
{
PIOS_DELAY_WaitmS(1000); PIOS_DELAY_WaitmS(1000);
PIOS_IAP_ClearRequest(); PIOS_IAP_ClearRequest();
} }
@ -160,7 +159,8 @@ void process_spi_request(void) {
//PIOS_LED_On(LED1); //PIOS_LED_On(LED1);
opahrs_msg_v0_init_user_tx(&user_tx_v0, OPAHRS_MSG_V0_RSP_VERSIONS); opahrs_msg_v0_init_user_tx(&user_tx_v0, OPAHRS_MSG_V0_RSP_VERSIONS);
user_tx_v0.payload.user.v.rsp.versions.bl_version = BOOTLOADER_VERSION; user_tx_v0.payload.user.v.rsp.versions.bl_version = BOOTLOADER_VERSION;
user_tx_v0.payload.user.v.rsp.versions.hw_version = (BOARD_TYPE << 8) | BOARD_REVISION; user_tx_v0.payload.user.v.rsp.versions.hw_version = (BOARD_TYPE << 8)
| BOARD_REVISION;
user_tx_v0.payload.user.v.rsp.versions.fw_crc = Fw_crc; user_tx_v0.payload.user.v.rsp.versions.fw_crc = Fw_crc;
lfsm_user_set_tx_v0(&user_tx_v0); lfsm_user_set_tx_v0(&user_tx_v0);
break; break;
@ -213,10 +213,10 @@ void process_spi_request(void) {
break; break;
case OPAHRS_MSG_V0_REQ_FWDN_DATA: case OPAHRS_MSG_V0_REQ_FWDN_DATA:
opahrs_msg_v0_init_user_tx(&user_tx_v0, OPAHRS_MSG_V0_RSP_FWDN_DATA); opahrs_msg_v0_init_user_tx(&user_tx_v0, OPAHRS_MSG_V0_RSP_FWDN_DATA);
uint32_t adr=user_rx_v0.payload.user.v.req.fwdn_data.adress; uint32_t adr = user_rx_v0.payload.user.v.req.fwdn_data.adress;
for(uint8_t x=0;x<4;++x) for (uint8_t x = 0; x < 4; ++x) {
{ user_tx_v0.payload.user.v.rsp.fw_dn.data[x]
user_tx_v0.payload.user.v.rsp.fw_dn.data[x]=*PIOS_BL_HELPER_FLASH_If_Read(adr+x); = *PIOS_BL_HELPER_FLASH_If_Read(adr + x);
} }
lfsm_user_set_tx_v0(&user_tx_v0); lfsm_user_set_tx_v0(&user_tx_v0);
break; break;

View File

@ -29,7 +29,6 @@
#include <pios_spi_priv.h> #include <pios_spi_priv.h>
/* OP Interface /* OP Interface
* *
* NOTE: Leave this declared as const data so that it ends up in the * NOTE: Leave this declared as const data so that it ends up in the
@ -38,7 +37,8 @@
void PIOS_SPI_op_irq_handler(void); void PIOS_SPI_op_irq_handler(void);
void DMA1_Channel5_IRQHandler() __attribute__ ((alias ("PIOS_SPI_op_irq_handler"))); void DMA1_Channel5_IRQHandler() __attribute__ ((alias ("PIOS_SPI_op_irq_handler")));
void DMA1_Channel4_IRQHandler() __attribute__ ((alias ("PIOS_SPI_op_irq_handler"))); void DMA1_Channel4_IRQHandler() __attribute__ ((alias ("PIOS_SPI_op_irq_handler")));
static const struct pios_spi_cfg pios_spi_op_cfg = { static const struct pios_spi_cfg
pios_spi_op_cfg = {
.regs = SPI2, .regs = SPI2,
.init = { .init = {
.SPI_Mode = SPI_Mode_Slave, .SPI_Mode = SPI_Mode_Slave,
@ -93,44 +93,38 @@ static const struct pios_spi_cfg pios_spi_op_cfg = {
.DMA_M2M = DMA_M2M_Disable, .DMA_M2M = DMA_M2M_Disable,
}, },
}, },
}, }, .ssel = {
.ssel = {
.gpio = GPIOB, .gpio = GPIOB,
.init = { .init = {
.GPIO_Pin = GPIO_Pin_12, .GPIO_Pin = GPIO_Pin_12,
.GPIO_Speed = GPIO_Speed_50MHz, .GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_IN_FLOATING, .GPIO_Mode = GPIO_Mode_IN_FLOATING,
}, },
}, }, .sclk = {
.sclk = {
.gpio = GPIOB, .gpio = GPIOB,
.init = { .init = {
.GPIO_Pin = GPIO_Pin_13, .GPIO_Pin = GPIO_Pin_13,
.GPIO_Speed = GPIO_Speed_50MHz, .GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_IN_FLOATING, .GPIO_Mode = GPIO_Mode_IN_FLOATING,
}, },
}, }, .miso = {
.miso = {
.gpio = GPIOB, .gpio = GPIOB,
.init = { .init = {
.GPIO_Pin = GPIO_Pin_14, .GPIO_Pin = GPIO_Pin_14,
.GPIO_Speed = GPIO_Speed_50MHz, .GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_AF_PP, .GPIO_Mode = GPIO_Mode_AF_PP,
}, },
}, }, .mosi = {
.mosi = {
.gpio = GPIOB, .gpio = GPIOB,
.init = { .init = {
.GPIO_Pin = GPIO_Pin_15, .GPIO_Pin = GPIO_Pin_15,
.GPIO_Speed = GPIO_Speed_50MHz, .GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_IN_FLOATING, .GPIO_Mode = GPIO_Mode_IN_FLOATING,
}, },
}, }, };
};
uint32_t pios_spi_op_id; uint32_t pios_spi_op_id;
void PIOS_SPI_op_irq_handler(void) void PIOS_SPI_op_irq_handler(void) {
{
/* Call into the generic code to handle the IRQ for this specific device */ /* Call into the generic code to handle the IRQ for this specific device */
PIOS_SPI_IRQ_Handler(pios_spi_op_id); PIOS_SPI_IRQ_Handler(pios_spi_op_id);
} }
@ -139,8 +133,7 @@ void PIOS_SPI_op_irq_handler(void)
#include "bl_fsm.h" /* lfsm_* */ #include "bl_fsm.h" /* lfsm_* */
void PIOS_Board_Init() void PIOS_Board_Init() {
{
/* Set up the SPI interface to the OP board */ /* Set up the SPI interface to the OP board */
if (PIOS_SPI_Init(&pios_spi_op_id, &pios_spi_op_cfg)) { if (PIOS_SPI_Init(&pios_spi_op_id, &pios_spi_op_cfg)) {
PIOS_DEBUG_Assert(0); PIOS_DEBUG_Assert(0);

View File

@ -112,7 +112,7 @@ DOXYGENDIR = ../Doc/Doxygen
SRC += $(OPSYSTEM)/main.c SRC += $(OPSYSTEM)/main.c
SRC += $(OPSYSTEM)/pios_board.c SRC += $(OPSYSTEM)/pios_board.c
SRC += $(OPSYSTEM)/op_dfu.c SRC += $(OPSYSTEM)/op_dfu.c
SRC += $(OPSYSTEM)/stopwatch.c SRC += $(FLIGHTLIB)/stopwatch.c
## PIOS Hardware (STM32F10x) ## PIOS Hardware (STM32F10x)
@ -121,35 +121,21 @@ SRC += $(PIOSSTM32F10X)/pios_led.c
SRC += $(PIOSSTM32F10X)/pios_delay.c SRC += $(PIOSSTM32F10X)/pios_delay.c
SRC += $(PIOSSTM32F10X)/pios_usart.c SRC += $(PIOSSTM32F10X)/pios_usart.c
SRC += $(PIOSSTM32F10X)/pios_irq.c SRC += $(PIOSSTM32F10X)/pios_irq.c
#SRC += $(PIOSSTM32F10X)/pios_adc.c
#SRC += $(PIOSSTM32F10X)/pios_servo.c
#SRC += $(PIOSSTM32F10X)/pios_i2c.c
#SRC += $(PIOSSTM32F10X)/pios_spi.c
#SRC += $(PIOSSTM32F10X)/pios_ppm.c
#SRC += $(PIOSSTM32F10X)/pios_pwm.c
#SRC += $(PIOSSTM32F10X)/pios_spektrum.c
SRC += $(PIOSSTM32F10X)/pios_debug.c SRC += $(PIOSSTM32F10X)/pios_debug.c
SRC += $(PIOSSTM32F10X)/pios_gpio.c SRC += $(PIOSSTM32F10X)/pios_gpio.c
#SRC += $(PIOSSTM32F10X)/pios_exti.c
#SRC += $(PIOSSTM32F10X)/pios_wdg.c
# PIOS USB related files (seperated to make code maintenance more easy) # PIOS USB related files (seperated to make code maintenance more easy)
SRC += $(PIOSSTM32F10X)/pios_usb_hid.c SRC += $(PIOSSTM32F10X)/pios_usb_hid.c
SRC += $(PIOSSTM32F10X)/pios_usb_hid_desc.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_desc.c
#SRC += $(PIOSSTM32F10X)/pios_usb_hid_endp.c
SRC += $(PIOSSTM32F10X)/pios_usb_hid_istr.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_istr.c
SRC += $(PIOSSTM32F10X)/pios_usb_hid_prop.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_prop.c
SRC += $(PIOSSTM32F10X)/pios_usb_hid_pwr.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_pwr.c
## PIOS Hardware (Common) ## PIOS Hardware (Common)
#SRC += $(PIOSCOMMON)/pios_sdcard.c
SRC += $(PIOSCOMMON)/pios_com.c SRC += $(PIOSCOMMON)/pios_com.c
#SRC += $(PIOSCOMMON)/pios_bmp085.c
#SRC += $(PIOSCOMMON)/pios_opahrs_v0.c
SRC += $(PIOSCOMMON)/pios_bl_helper.c SRC += $(PIOSCOMMON)/pios_bl_helper.c
SRC += $(PIOSCOMMON)/pios_iap.c SRC += $(PIOSCOMMON)/pios_iap.c
#SRC += $(PIOSCOMMON)/pios_opahrs_proto.c
SRC += $(PIOSCOMMON)/printf-stdarg.c SRC += $(PIOSCOMMON)/printf-stdarg.c
## Libraries for flight calculations ## Libraries for flight calculations
@ -160,22 +146,18 @@ SRC += $(CMSISDIR)/core_cm3.c
SRC += $(CMSISDIR)/system_stm32f10x.c SRC += $(CMSISDIR)/system_stm32f10x.c
## Used parts of the STM-Library ## Used parts of the STM-Library
#SRC += $(STMSPDSRCDIR)/stm32f10x_adc.c
SRC += $(STMSPDSRCDIR)/stm32f10x_bkp.c SRC += $(STMSPDSRCDIR)/stm32f10x_bkp.c
SRC += $(STMSPDSRCDIR)/stm32f10x_crc.c SRC += $(STMSPDSRCDIR)/stm32f10x_crc.c
#SRC += $(STMSPDSRCDIR)/stm32f10x_dac.c
SRC += $(STMSPDSRCDIR)/stm32f10x_dma.c SRC += $(STMSPDSRCDIR)/stm32f10x_dma.c
SRC += $(STMSPDSRCDIR)/stm32f10x_exti.c SRC += $(STMSPDSRCDIR)/stm32f10x_exti.c
SRC += $(STMSPDSRCDIR)/stm32f10x_flash.c SRC += $(STMSPDSRCDIR)/stm32f10x_flash.c
SRC += $(STMSPDSRCDIR)/stm32f10x_gpio.c SRC += $(STMSPDSRCDIR)/stm32f10x_gpio.c
#SRC += $(STMSPDSRCDIR)/stm32f10x_i2c.c
SRC += $(STMSPDSRCDIR)/stm32f10x_pwr.c SRC += $(STMSPDSRCDIR)/stm32f10x_pwr.c
SRC += $(STMSPDSRCDIR)/stm32f10x_rcc.c SRC += $(STMSPDSRCDIR)/stm32f10x_rcc.c
SRC += $(STMSPDSRCDIR)/stm32f10x_rtc.c SRC += $(STMSPDSRCDIR)/stm32f10x_rtc.c
SRC += $(STMSPDSRCDIR)/stm32f10x_spi.c SRC += $(STMSPDSRCDIR)/stm32f10x_spi.c
SRC += $(STMSPDSRCDIR)/stm32f10x_tim.c SRC += $(STMSPDSRCDIR)/stm32f10x_tim.c
SRC += $(STMSPDSRCDIR)/stm32f10x_usart.c SRC += $(STMSPDSRCDIR)/stm32f10x_usart.c
#SRC += $(STMSPDSRCDIR)/stm32f10x_iwdg.c
SRC += $(STMSPDSRCDIR)/stm32f10x_dbgmcu.c SRC += $(STMSPDSRCDIR)/stm32f10x_dbgmcu.c
SRC += $(STMSPDSRCDIR)/misc.c SRC += $(STMSPDSRCDIR)/misc.c

View File

@ -51,7 +51,8 @@ typedef enum {
uploadingStarting, //9 uploadingStarting, //9
outsideDevCapabilities, //10 outsideDevCapabilities, //10
CRC_Fail,//11 CRC_Fail,//11
failed_jump,//12 failed_jump,
//12
} DFUStates; } DFUStates;
/**************************************************/ /**************************************************/
/* OP_DFU commands */ /* OP_DFU commands */
@ -74,9 +75,8 @@ typedef enum {
} DFUCommands; } DFUCommands;
typedef enum { typedef enum {
High_Density, High_Density, Medium_Density
Medium_Density } DeviceType;
}DeviceType;
/**************************************************/ /**************************************************/
/* OP_DFU transfer types */ /* OP_DFU transfer types */
/**************************************************/ /**************************************************/

View File

@ -1,4 +1,4 @@
/** /**
****************************************************************************** ******************************************************************************
* *
* @file op_dfu.h * @file op_dfu.h
@ -29,8 +29,7 @@
#include "common.h" #include "common.h"
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/ /* Exported types ------------------------------------------------------------*/
typedef struct typedef struct {
{
uint8_t programmingType; uint8_t programmingType;
uint8_t readWriteFlags; uint8_t readWriteFlags;
uint32_t startOfUserCode; uint32_t startOfUserCode;
@ -40,7 +39,7 @@ typedef struct
uint16_t devID; uint16_t devID;
DeviceType devType; DeviceType devType;
uint32_t FW_Crc; uint32_t FW_Crc;
}Device; } Device;
/* Exported constants --------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/

View File

@ -28,7 +28,6 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#ifndef PIOS_CONFIG_H #ifndef PIOS_CONFIG_H
#define PIOS_CONFIG_H #define PIOS_CONFIG_H
#define PIOS_INCLUDE_BL_HELPER #define PIOS_INCLUDE_BL_HELPER

View File

@ -69,8 +69,8 @@
#define PIOS_USB_ENDP1_RXADDR 0x100 #define PIOS_USB_ENDP1_RXADDR 0x100
/* Global Variables */ /* Global Variables */
extern void (*pEpInt_IN[7]) (void); extern void (*pEpInt_IN[7])(void);
extern void (*pEpInt_OUT[7]) (void); extern void (*pEpInt_OUT[7])(void);
/* Public Functions */ /* Public Functions */
extern int32_t PIOS_USB_Init(uint32_t mode); extern int32_t PIOS_USB_Init(uint32_t mode);

View File

@ -53,12 +53,11 @@ uint32_t sweep_steps2 = 100; // * 5 mS -> 500 mS
//////////////////////////////////////// ////////////////////////////////////////
uint8_t tempcount=0; uint8_t tempcount = 0;
/* Extern variables ----------------------------------------------------------*/ /* Extern variables ----------------------------------------------------------*/
DFUStates DeviceState; DFUStates DeviceState;
int16_t status=0; int16_t status = 0;
uint8_t JumpToApp = FALSE; uint8_t JumpToApp = FALSE;
uint8_t GO_dfu = FALSE; uint8_t GO_dfu = FALSE;
uint8_t USB_connected = FALSE; uint8_t USB_connected = FALSE;
@ -70,13 +69,8 @@ uint8_t processRX();
void jump_to_app(); void jump_to_app();
#define BLUE LED1 #define BLUE LED1
// #define RED LED4
#define LED_PWM_TIMER TIM1 #define LED_PWM_TIMER TIM1
int main() { int main() {
/* NOTE: Do NOT modify the following start-up sequence */
/* Any new initialization functions should be added in OpenPilotInit() */
/* Brings up System using CMSIS functions, enables the LEDs. */
PIOS_SYS_Init(); PIOS_SYS_Init();
if (BSL_HOLD_STATE == 0) if (BSL_HOLD_STATE == 0)
USB_connected = TRUE; USB_connected = TRUE;
@ -94,11 +88,11 @@ int main() {
if (GO_dfu == TRUE) { if (GO_dfu == TRUE) {
PIOS_Board_Init(); PIOS_Board_Init();
if(User_DFU_request == TRUE) if (User_DFU_request == TRUE)
DeviceState = DFUidle; DeviceState = DFUidle;
else else
DeviceState = BLidle; DeviceState = BLidle;
STOPWATCH_Init(100,LED_PWM_TIMER); STOPWATCH_Init(100, LED_PWM_TIMER);
} else } else
JumpToApp = TRUE; JumpToApp = TRUE;
@ -107,8 +101,6 @@ int main() {
while (TRUE) { while (TRUE) {
if (JumpToApp == TRUE) if (JumpToApp == TRUE)
jump_to_app(); jump_to_app();
//pwm_period = 50; // *100 uS -> 5 mS
//pwm_sweep_steps =100; // * 5 mS -> 500 mS
switch (DeviceState) { switch (DeviceState) {
case Last_operation_Success: case Last_operation_Success:
@ -161,7 +153,8 @@ int main() {
if (STOPWATCH_ValueGet(LED_PWM_TIMER) > 100 * 50 * 100) if (STOPWATCH_ValueGet(LED_PWM_TIMER) > 100 * 50 * 100)
STOPWATCH_Reset(LED_PWM_TIMER); STOPWATCH_Reset(LED_PWM_TIMER);
if ((STOPWATCH_ValueGet(LED_PWM_TIMER) > 60000) && (DeviceState == BLidle)) if ((STOPWATCH_ValueGet(LED_PWM_TIMER) > 60000) && (DeviceState
== BLidle))
JumpToApp = TRUE; JumpToApp = TRUE;
processRX(); processRX();
@ -178,7 +171,6 @@ void jump_to_app() {
RCC_APB1PeriphResetCmd(0xffffffff, DISABLE); RCC_APB1PeriphResetCmd(0xffffffff, DISABLE);
_SetCNTR(0); // clear interrupt mask _SetCNTR(0); // clear interrupt mask
_SetISTR(0); // clear all requests _SetISTR(0); // clear all requests
JumpAddress = *(__IO uint32_t*) (START_OF_USER_CODE + 4); JumpAddress = *(__IO uint32_t*) (START_OF_USER_CODE + 4);
Jump_To_Application = (pFunction) JumpAddress; Jump_To_Application = (pFunction) JumpAddress;
/* Initialize user application's Stack Pointer */ /* Initialize user application's Stack Pointer */

View File

@ -1,6 +1,6 @@
/** /**
****************************************************************************** ******************************************************************************
* @addtogroup CopterControlBL CopterControl BootLoader * @addtogroup CopterControlBL CopterControl BootLoader
* @brief These files contain the code to the CopterControl Bootloader. * @brief These files contain the code to the CopterControl Bootloader.
* *
* @{ * @{
@ -70,7 +70,7 @@ extern DFUStates DeviceState;
extern uint8_t JumpToApp; extern uint8_t JumpToApp;
/* Private function prototypes -----------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/ /* Private functions ---------------------------------------------------------*/
void sendData(uint8_t * buf,uint16_t size); void sendData(uint8_t * buf, uint16_t size);
uint32_t CalcFirmCRC(void); uint32_t CalcFirmCRC(void);
void DataDownload(DownloadAction action) { void DataDownload(DownloadAction action) {
@ -93,8 +93,8 @@ void DataDownload(DownloadAction action) {
for (uint8_t x = 0; x < packetSize; ++x) { for (uint8_t x = 0; x < packetSize; ++x) {
partoffset = (downPacketCurrent * 14 * 4) + (x * 4); partoffset = (downPacketCurrent * 14 * 4) + (x * 4);
offset = baseOfAdressType(downType) + partoffset; offset = baseOfAdressType(downType) + partoffset;
if(!flash_read(SendBuffer+(6+x*4),offset,currentProgrammingDestination)) if (!flash_read(SendBuffer + (6 + x * 4), offset,
{ currentProgrammingDestination)) {
DeviceState = Last_operation_failed; DeviceState = Last_operation_failed;
} }
} }
@ -103,14 +103,14 @@ void DataDownload(DownloadAction action) {
DeviceState = Last_operation_Success; DeviceState = Last_operation_Success;
Aditionals = (uint32_t) Download; Aditionals = (uint32_t) Download;
} }
sendData(SendBuffer+1,63); sendData(SendBuffer + 1, 63);
} }
} }
void processComand(uint8_t *xReceive_Buffer) { void processComand(uint8_t *xReceive_Buffer) {
Command = xReceive_Buffer[COMMAND]; Command = xReceive_Buffer[COMMAND];
#ifdef DEBUG_SSP #ifdef DEBUG_SSP
char str[63]={0}; char str[63]= {0};
sprintf(str,"Received COMMAND:%d|",Command); sprintf(str,"Received COMMAND:%d|",Command);
PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); PIOS_COM_SendString(PIOS_COM_TELEM_USB,str);
#endif #endif
@ -139,7 +139,7 @@ void processComand(uint8_t *xReceive_Buffer) {
case EnterDFU: case EnterDFU:
if (((DeviceState == BLidle) && (Data0 < numberOfDevices)) if (((DeviceState == BLidle) && (Data0 < numberOfDevices))
|| (DeviceState == DFUidle)) { || (DeviceState == DFUidle)) {
if (Data0 > 0)//PORQUE??? if (Data0 > 0)
OPDfuIni(TRUE); OPDfuIni(TRUE);
DeviceState = DFUidle; DeviceState = DFUidle;
currentProgrammingDestination = devicesTable[Data0].programmingType; currentProgrammingDestination = devicesTable[Data0].programmingType;
@ -196,7 +196,7 @@ void processComand(uint8_t *xReceive_Buffer) {
} }
} }
if (result != 1) { if (result != 1) {
DeviceState = Last_operation_failed;//ok DeviceState = Last_operation_failed;
Aditionals = (uint32_t) Command; Aditionals = (uint32_t) Command;
} else { } else {
@ -287,7 +287,7 @@ void processComand(uint8_t *xReceive_Buffer) {
Buffer[11] = devicesTable[Data0 - 1].FW_Crc >> 16; Buffer[11] = devicesTable[Data0 - 1].FW_Crc >> 16;
Buffer[12] = devicesTable[Data0 - 1].FW_Crc >> 8; Buffer[12] = devicesTable[Data0 - 1].FW_Crc >> 8;
Buffer[13] = devicesTable[Data0 - 1].FW_Crc; Buffer[13] = devicesTable[Data0 - 1].FW_Crc;
Buffer[14] = devicesTable[Data0 - 1].devID>>8; Buffer[14] = devicesTable[Data0 - 1].devID >> 8;
Buffer[15] = devicesTable[Data0 - 1].devID; Buffer[15] = devicesTable[Data0 - 1].devID;
} }
sendData(Buffer + 1, 63); sendData(Buffer + 1, 63);
@ -438,10 +438,9 @@ uint32_t CalcFirmCRC() {
} }
} }
void sendData(uint8_t * buf,uint16_t size) void sendData(uint8_t * buf, uint16_t size) {
{
PIOS_COM_SendBuffer(PIOS_COM_TELEM_USB, buf, size); PIOS_COM_SendBuffer(PIOS_COM_TELEM_USB, buf, size);
if(DeviceState == downloading) if (DeviceState == downloading)
PIOS_DELAY_WaitmS(20);//this is an hack, we should check wtf is wrong with hid PIOS_DELAY_WaitmS(20);//this is an hack, we should check wtf is wrong with hid
} }

View File

@ -112,7 +112,7 @@ DOXYGENDIR = ../Doc/Doxygen
SRC += $(OPSYSTEM)/main.c SRC += $(OPSYSTEM)/main.c
SRC += $(OPSYSTEM)/pios_board.c SRC += $(OPSYSTEM)/pios_board.c
SRC += $(OPSYSTEM)/op_dfu.c SRC += $(OPSYSTEM)/op_dfu.c
SRC += $(OPSYSTEM)/stopwatch.c SRC += $(FLIGHTLIB)/stopwatch.c
SRC += $(OPSYSTEM)/ssp.c SRC += $(OPSYSTEM)/ssp.c
@ -122,31 +122,19 @@ SRC += $(PIOSSTM32F10X)/pios_led.c
SRC += $(PIOSSTM32F10X)/pios_delay.c SRC += $(PIOSSTM32F10X)/pios_delay.c
SRC += $(PIOSSTM32F10X)/pios_usart.c SRC += $(PIOSSTM32F10X)/pios_usart.c
SRC += $(PIOSSTM32F10X)/pios_irq.c SRC += $(PIOSSTM32F10X)/pios_irq.c
#SRC += $(PIOSSTM32F10X)/pios_adc.c
#SRC += $(PIOSSTM32F10X)/pios_servo.c
#SRC += $(PIOSSTM32F10X)/pios_i2c.c
SRC += $(PIOSSTM32F10X)/pios_spi.c SRC += $(PIOSSTM32F10X)/pios_spi.c
#SRC += $(PIOSSTM32F10X)/pios_ppm.c
#SRC += $(PIOSSTM32F10X)/pios_pwm.c
#SRC += $(PIOSSTM32F10X)/pios_spektrum.c
SRC += $(PIOSSTM32F10X)/pios_debug.c SRC += $(PIOSSTM32F10X)/pios_debug.c
SRC += $(PIOSSTM32F10X)/pios_gpio.c SRC += $(PIOSSTM32F10X)/pios_gpio.c
#SRC += $(PIOSSTM32F10X)/pios_exti.c
#SRC += $(PIOSSTM32F10X)/pios_wdg.c
# PIOS USB related files (seperated to make code maintenance more easy) # PIOS USB related files (seperated to make code maintenance more easy)
SRC += $(PIOSSTM32F10X)/pios_usb_hid.c SRC += $(PIOSSTM32F10X)/pios_usb_hid.c
SRC += $(PIOSSTM32F10X)/pios_usb_hid_desc.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_desc.c
#SRC += $(PIOSSTM32F10X)/pios_usb_hid_endp.c
SRC += $(PIOSSTM32F10X)/pios_usb_hid_istr.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_istr.c
SRC += $(PIOSSTM32F10X)/pios_usb_hid_prop.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_prop.c
SRC += $(PIOSSTM32F10X)/pios_usb_hid_pwr.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_pwr.c
## PIOS Hardware (Common) ## PIOS Hardware (Common)
#SRC += $(PIOSCOMMON)/pios_sdcard.c
SRC += $(PIOSCOMMON)/pios_com.c SRC += $(PIOSCOMMON)/pios_com.c
#SRC += $(PIOSCOMMON)/pios_bmp085.c
SRC += $(PIOSCOMMON)/pios_opahrs_v0.c SRC += $(PIOSCOMMON)/pios_opahrs_v0.c
SRC += $(PIOSCOMMON)/pios_bl_helper.c SRC += $(PIOSCOMMON)/pios_bl_helper.c
SRC += $(PIOSCOMMON)/pios_iap.c SRC += $(PIOSCOMMON)/pios_iap.c
@ -161,22 +149,18 @@ SRC += $(CMSISDIR)/core_cm3.c
SRC += $(CMSISDIR)/system_stm32f10x.c SRC += $(CMSISDIR)/system_stm32f10x.c
## Used parts of the STM-Library ## Used parts of the STM-Library
#SRC += $(STMSPDSRCDIR)/stm32f10x_adc.c
SRC += $(STMSPDSRCDIR)/stm32f10x_bkp.c SRC += $(STMSPDSRCDIR)/stm32f10x_bkp.c
SRC += $(STMSPDSRCDIR)/stm32f10x_crc.c SRC += $(STMSPDSRCDIR)/stm32f10x_crc.c
#SRC += $(STMSPDSRCDIR)/stm32f10x_dac.c
SRC += $(STMSPDSRCDIR)/stm32f10x_dma.c SRC += $(STMSPDSRCDIR)/stm32f10x_dma.c
SRC += $(STMSPDSRCDIR)/stm32f10x_exti.c SRC += $(STMSPDSRCDIR)/stm32f10x_exti.c
SRC += $(STMSPDSRCDIR)/stm32f10x_flash.c SRC += $(STMSPDSRCDIR)/stm32f10x_flash.c
SRC += $(STMSPDSRCDIR)/stm32f10x_gpio.c SRC += $(STMSPDSRCDIR)/stm32f10x_gpio.c
#SRC += $(STMSPDSRCDIR)/stm32f10x_i2c.c
SRC += $(STMSPDSRCDIR)/stm32f10x_pwr.c SRC += $(STMSPDSRCDIR)/stm32f10x_pwr.c
SRC += $(STMSPDSRCDIR)/stm32f10x_rcc.c SRC += $(STMSPDSRCDIR)/stm32f10x_rcc.c
SRC += $(STMSPDSRCDIR)/stm32f10x_rtc.c SRC += $(STMSPDSRCDIR)/stm32f10x_rtc.c
SRC += $(STMSPDSRCDIR)/stm32f10x_spi.c SRC += $(STMSPDSRCDIR)/stm32f10x_spi.c
SRC += $(STMSPDSRCDIR)/stm32f10x_tim.c SRC += $(STMSPDSRCDIR)/stm32f10x_tim.c
SRC += $(STMSPDSRCDIR)/stm32f10x_usart.c SRC += $(STMSPDSRCDIR)/stm32f10x_usart.c
#SRC += $(STMSPDSRCDIR)/stm32f10x_iwdg.c
SRC += $(STMSPDSRCDIR)/stm32f10x_dbgmcu.c SRC += $(STMSPDSRCDIR)/stm32f10x_dbgmcu.c
SRC += $(STMSPDSRCDIR)/misc.c SRC += $(STMSPDSRCDIR)/misc.c

View File

@ -51,7 +51,8 @@ typedef enum {
uploadingStarting, //9 uploadingStarting, //9
outsideDevCapabilities, //10 outsideDevCapabilities, //10
CRC_Fail,//11 CRC_Fail,//11
failed_jump,//12 failed_jump,
//12
} DFUStates; } DFUStates;
/**************************************************/ /**************************************************/
/* OP_DFU commands */ /* OP_DFU commands */
@ -74,9 +75,8 @@ typedef enum {
} DFUCommands; } DFUCommands;
typedef enum { typedef enum {
High_Density, High_Density, Medium_Density
Medium_Density } DeviceType;
}DeviceType;
/**************************************************/ /**************************************************/
/* OP_DFU transfer types */ /* OP_DFU transfer types */
/**************************************************/ /**************************************************/

View File

@ -1,4 +1,4 @@
/** /**
****************************************************************************** ******************************************************************************
* *
* @file op_dfu.h * @file op_dfu.h
@ -29,8 +29,7 @@
#include "common.h" #include "common.h"
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/ /* Exported types ------------------------------------------------------------*/
typedef struct typedef struct {
{
uint8_t programmingType; uint8_t programmingType;
uint8_t readWriteFlags; uint8_t readWriteFlags;
uint32_t startOfUserCode; uint32_t startOfUserCode;
@ -40,7 +39,7 @@ typedef struct
uint16_t devID; uint16_t devID;
DeviceType devType; DeviceType devType;
uint32_t FW_Crc; uint32_t FW_Crc;
}Device; } Device;
/* Exported constants --------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/

View File

@ -27,7 +27,6 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#ifndef PIOS_CONFIG_H #ifndef PIOS_CONFIG_H
#define PIOS_CONFIG_H #define PIOS_CONFIG_H
#define PIOS_INCLUDE_BL_HELPER #define PIOS_INCLUDE_BL_HELPER

View File

@ -69,8 +69,8 @@
#define PIOS_USB_ENDP1_RXADDR 0x100 #define PIOS_USB_ENDP1_RXADDR 0x100
/* Global Variables */ /* Global Variables */
extern void (*pEpInt_IN[7]) (void); extern void (*pEpInt_IN[7])(void);
extern void (*pEpInt_OUT[7]) (void); extern void (*pEpInt_OUT[7])(void);
/* Public Functions */ /* Public Functions */
extern int32_t PIOS_USB_Init(uint32_t mode); extern int32_t PIOS_USB_Init(uint32_t mode);

View File

@ -45,12 +45,10 @@ typedef enum decodeState_ {
} DecodeState_t; } DecodeState_t;
typedef enum ReceiveState { typedef enum ReceiveState {
state_escaped_e = 0, state_escaped_e = 0, state_unescaped_e
state_unescaped_e
} ReceiveState_t; } ReceiveState_t;
typedef struct typedef struct {
{
uint8_t *pbuff; uint8_t *pbuff;
uint16_t length; uint16_t length;
uint16_t crc; uint16_t crc;
@ -65,16 +63,16 @@ typedef struct {
uint16_t txBufSize; // CRC for data in Packet buff uint16_t txBufSize; // CRC for data in Packet buff
uint16_t max_retry; // Maximum number of retrys for a single transmit. uint16_t max_retry; // Maximum number of retrys for a single transmit.
int32_t timeoutLen; // how long to wait for each retry to succeed int32_t timeoutLen; // how long to wait for each retry to succeed
void (*pfCallBack)( uint8_t *, uint16_t); // call back function that is called when a full packet has been received void (*pfCallBack)(uint8_t *, uint16_t); // call back function that is called when a full packet has been received
int16_t (*pfSerialRead)(void); // function to call to read a byte from serial hardware int16_t (*pfSerialRead)(void); // function to call to read a byte from serial hardware
void (*pfSerialWrite)( uint8_t ); // function used to write a byte to serial hardware for transmission void (*pfSerialWrite)( uint8_t); // function used to write a byte to serial hardware for transmission
uint32_t (*pfGetTime)(void); // function returns time in number of seconds that has elapsed from a given reference point uint32_t (*pfGetTime)(void); // function returns time in number of seconds that has elapsed from a given reference point
} PortConfig_t; } PortConfig_t;
typedef struct Port_tag { typedef struct Port_tag {
void (*pfCallBack)( uint8_t *, uint16_t); // call back function that is called when a full packet has been received void (*pfCallBack)(uint8_t *, uint16_t); // call back function that is called when a full packet has been received
int16_t (*pfSerialRead)(void); // function to read a character from the serial input stream int16_t (*pfSerialRead)(void); // function to read a character from the serial input stream
void (*pfSerialWrite)( uint8_t ); // function to write a byte to be sent out the serial port void (*pfSerialWrite)( uint8_t); // function to write a byte to be sent out the serial port
uint32_t (*pfGetTime)(void); // function returns time in number of seconds that has elapsed from a given reference point uint32_t (*pfGetTime)(void); // function returns time in number of seconds that has elapsed from a given reference point
uint8_t retryCount; // how many times have we tried to transmit the 'send' packet uint8_t retryCount; // how many times have we tried to transmit the 'send' packet
uint8_t maxRetryCount; // max. times to try to transmit the 'send' packet uint8_t maxRetryCount; // max. times to try to transmit the 'send' packet
@ -102,19 +100,17 @@ typedef struct Port_tag {
uint16_t flags; uint16_t flags;
} Port_t; } Port_t;
/** Public Data **/ /** Public Data **/
/** PUBLIC FUNCTIONS **/ /** PUBLIC FUNCTIONS **/
int16_t ssp_ReceiveProcess( Port_t *thisport ); int16_t ssp_ReceiveProcess(Port_t *thisport);
int16_t ssp_SendProcess( Port_t *thisport ); int16_t ssp_SendProcess(Port_t *thisport);
uint16_t ssp_SendString( Port_t *thisport, char *str ); uint16_t ssp_SendString(Port_t *thisport, char *str);
int16_t ssp_SendData(Port_t *thisport, const uint8_t * data,const uint16_t length ); int16_t ssp_SendData(Port_t *thisport, const uint8_t * data,
void ssp_Init( Port_t *thisport, const PortConfig_t* const info); const uint16_t length);
int16_t ssp_ReceiveByte(Port_t *thisport ); void ssp_Init(Port_t *thisport, const PortConfig_t* const info);
uint16_t ssp_Synchronise( Port_t *thisport ); int16_t ssp_ReceiveByte(Port_t *thisport);
uint16_t ssp_Synchronise(Port_t *thisport);
/** EXTERNAL FUNCTIONS **/ /** EXTERNAL FUNCTIONS **/

View File

@ -1,55 +0,0 @@
/**
******************************************************************************
* @addtogroup OpenPilotBL OpenPilot BootLoader
* @{
*
* @file stopwatch.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief Timer functions for the LED PWM.
* @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
*/
#ifndef _STOPWATCH_H
#define _STOPWATCH_H
/////////////////////////////////////////////////////////////////////////////
// Global definitions
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Global Types
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Prototypes
/////////////////////////////////////////////////////////////////////////////
extern s32 STOPWATCH_Init(u32 resolution, TIM_TypeDef* TIM);
extern s32 STOPWATCH_Reset(TIM_TypeDef* TIM);
extern u32 STOPWATCH_ValueGet(TIM_TypeDef* TIM);
/////////////////////////////////////////////////////////////////////////////
// Export global variables
/////////////////////////////////////////////////////////////////////////////
#endif /* _STOPWATCH_H */

View File

@ -55,17 +55,17 @@ uint32_t sweep_steps2 = 100; // * 5 mS -> 500 mS
//////////////////////////////////////// ////////////////////////////////////////
uint8_t tempcount=0; uint8_t tempcount = 0;
/// SSP SECTION /// SSP SECTION
/// SSP TIME SOURCE /// SSP TIME SOURCE
#define SSP_TIMER TIM7 #define SSP_TIMER TIM7
uint32_t ssp_time=0; uint32_t ssp_time = 0;
#define MAX_PACKET_DATA_LEN 255 #define MAX_PACKET_DATA_LEN 255
#define MAX_PACKET_BUF_SIZE (1+1+MAX_PACKET_DATA_LEN+2) #define MAX_PACKET_BUF_SIZE (1+1+MAX_PACKET_DATA_LEN+2)
#define UART_BUFFER_SIZE 1024 #define UART_BUFFER_SIZE 1024
uint8_t rx_buffer[UART_BUFFER_SIZE] __attribute__ ((aligned(4))); // align to 32-bit to try and provide speed improvement; uint8_t rx_buffer[UART_BUFFER_SIZE] __attribute__ ((aligned(4)));
// align to 32-bit to try and provide speed improvement;
// master buffers... // master buffers...
uint8_t SSP_TxBuf[MAX_PACKET_BUF_SIZE]; uint8_t SSP_TxBuf[MAX_PACKET_BUF_SIZE];
uint8_t SSP_RxBuf[MAX_PACKET_BUF_SIZE]; uint8_t SSP_RxBuf[MAX_PACKET_BUF_SIZE];
@ -84,7 +84,7 @@ t_fifo_buffer ssp_buffer;
/* Extern variables ----------------------------------------------------------*/ /* Extern variables ----------------------------------------------------------*/
DFUStates DeviceState; DFUStates DeviceState;
DFUPort ProgPort; DFUPort ProgPort;
int16_t status=0; int16_t status = 0;
uint8_t JumpToApp = FALSE; uint8_t JumpToApp = FALSE;
uint8_t GO_dfu = FALSE; uint8_t GO_dfu = FALSE;
uint8_t USB_connected = FALSE; uint8_t USB_connected = FALSE;
@ -125,14 +125,14 @@ int main() {
else else
ProgPort = Serial; ProgPort = Serial;
PIOS_Board_Init(); PIOS_Board_Init();
if(User_DFU_request == TRUE) if (User_DFU_request == TRUE)
DeviceState = DFUidle; DeviceState = DFUidle;
else else
DeviceState = BLidle; DeviceState = BLidle;
STOPWATCH_Init(100,LED_PWM_TIMER); STOPWATCH_Init(100, LED_PWM_TIMER);
if (ProgPort == Serial) { if (ProgPort == Serial) {
fifoBuf_init(&ssp_buffer,rx_buffer,UART_BUFFER_SIZE); fifoBuf_init(&ssp_buffer, rx_buffer, UART_BUFFER_SIZE);
STOPWATCH_Init(100,SSP_TIMER);//nao devia ser 1000? STOPWATCH_Init(100, SSP_TIMER);//nao devia ser 1000?
STOPWATCH_Reset(SSP_TIMER); STOPWATCH_Reset(SSP_TIMER);
ssp_Init(&ssp_port, &SSP_PortConfig); ssp_Init(&ssp_port, &SSP_PortConfig);
} }
@ -144,10 +144,10 @@ int main() {
while (TRUE) { while (TRUE) {
if (ProgPort == Serial) { if (ProgPort == Serial) {
ssp_ReceiveProcess(&ssp_port); ssp_ReceiveProcess(&ssp_port);
status=ssp_SendProcess(&ssp_port); status = ssp_SendProcess(&ssp_port);
while((status!=SSP_TX_IDLE) && (status!=SSP_TX_ACKED)){ while ((status != SSP_TX_IDLE) && (status != SSP_TX_ACKED)) {
ssp_ReceiveProcess(&ssp_port); ssp_ReceiveProcess(&ssp_port);
status=ssp_SendProcess(&ssp_port); status = ssp_SendProcess(&ssp_port);
} }
} }
if (JumpToApp == TRUE) if (JumpToApp == TRUE)
@ -206,7 +206,8 @@ int main() {
if (STOPWATCH_ValueGet(LED_PWM_TIMER) > 100 * 50 * 100) if (STOPWATCH_ValueGet(LED_PWM_TIMER) > 100 * 50 * 100)
STOPWATCH_Reset(LED_PWM_TIMER); STOPWATCH_Reset(LED_PWM_TIMER);
if ((STOPWATCH_ValueGet(LED_PWM_TIMER) > 60000) && (DeviceState == BLidle)) if ((STOPWATCH_ValueGet(LED_PWM_TIMER) > 60000) && (DeviceState
== BLidle))
JumpToApp = TRUE; JumpToApp = TRUE;
processRX(); processRX();
@ -272,17 +273,15 @@ uint32_t sspTimeSource() {
} }
void SSP_CallBack(uint8_t *buf, uint16_t len) { void SSP_CallBack(uint8_t *buf, uint16_t len) {
fifoBuf_putData(&ssp_buffer, buf, len); fifoBuf_putData(&ssp_buffer, buf, len);
} }
int16_t SSP_SerialRead(void) { int16_t SSP_SerialRead(void) {
if(PIOS_COM_ReceiveBufferUsed(PIOS_COM_TELEM_RF)>0) if (PIOS_COM_ReceiveBufferUsed(PIOS_COM_TELEM_RF) > 0) {
{
return PIOS_COM_ReceiveBuffer(PIOS_COM_TELEM_RF); return PIOS_COM_ReceiveBuffer(PIOS_COM_TELEM_RF);
} } else
else
return -1; return -1;
} }
void SSP_SerialWrite(uint8_t value) { void SSP_SerialWrite(uint8_t value) {
PIOS_COM_SendChar(PIOS_COM_TELEM_RF,value); PIOS_COM_SendChar(PIOS_COM_TELEM_RF, value);
} }
uint32_t SSP_GetTime(void) { uint32_t SSP_GetTime(void) {
return sspTimeSource(); return sspTimeSource();

View File

@ -78,7 +78,7 @@ extern Port_t ssp_port;
extern DFUPort ProgPort; extern DFUPort ProgPort;
/* Private function prototypes -----------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/ /* Private functions ---------------------------------------------------------*/
void sendData(uint8_t * buf,uint16_t size); void sendData(uint8_t * buf, uint16_t size);
uint32_t CalcFirmCRC(void); uint32_t CalcFirmCRC(void);
void DataDownload(DownloadAction action) { void DataDownload(DownloadAction action) {
@ -101,8 +101,8 @@ void DataDownload(DownloadAction action) {
for (uint8_t x = 0; x < packetSize; ++x) { for (uint8_t x = 0; x < packetSize; ++x) {
partoffset = (downPacketCurrent * 14 * 4) + (x * 4); partoffset = (downPacketCurrent * 14 * 4) + (x * 4);
offset = baseOfAdressType(downType) + partoffset; offset = baseOfAdressType(downType) + partoffset;
if(!flash_read(SendBuffer+(6+x*4),offset,currentProgrammingDestination)) if (!flash_read(SendBuffer + (6 + x * 4), offset,
{ currentProgrammingDestination)) {
DeviceState = Last_operation_failed; DeviceState = Last_operation_failed;
} }
} }
@ -111,14 +111,14 @@ void DataDownload(DownloadAction action) {
DeviceState = Last_operation_Success; DeviceState = Last_operation_Success;
Aditionals = (uint32_t) Download; Aditionals = (uint32_t) Download;
} }
sendData(SendBuffer+1,63); sendData(SendBuffer + 1, 63);
} }
} }
void processComand(uint8_t *xReceive_Buffer) { void processComand(uint8_t *xReceive_Buffer) {
Command = xReceive_Buffer[COMMAND]; Command = xReceive_Buffer[COMMAND];
#ifdef DEBUG_SSP #ifdef DEBUG_SSP
char str[63]={0}; char str[63]= {0};
sprintf(str,"Received COMMAND:%d|",Command); sprintf(str,"Received COMMAND:%d|",Command);
PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); PIOS_COM_SendString(PIOS_COM_TELEM_USB,str);
#endif #endif
@ -339,7 +339,7 @@ void processComand(uint8_t *xReceive_Buffer) {
Buffer[11] = devicesTable[Data0 - 1].FW_Crc >> 16; Buffer[11] = devicesTable[Data0 - 1].FW_Crc >> 16;
Buffer[12] = devicesTable[Data0 - 1].FW_Crc >> 8; Buffer[12] = devicesTable[Data0 - 1].FW_Crc >> 8;
Buffer[13] = devicesTable[Data0 - 1].FW_Crc; Buffer[13] = devicesTable[Data0 - 1].FW_Crc;
Buffer[14] = devicesTable[Data0 - 1].devID>>8; Buffer[14] = devicesTable[Data0 - 1].devID >> 8;
Buffer[15] = devicesTable[Data0 - 1].devID; Buffer[15] = devicesTable[Data0 - 1].devID;
} }
sendData(Buffer + 1, 63); sendData(Buffer + 1, 63);
@ -545,12 +545,11 @@ uint32_t CalcFirmCRC() {
} }
} }
void sendData(uint8_t * buf,uint16_t size) void sendData(uint8_t * buf, uint16_t size) {
{ if (ProgPort == Usb) {
if (ProgPort == Usb) {
PIOS_COM_SendBuffer(PIOS_COM_TELEM_USB, buf, size); PIOS_COM_SendBuffer(PIOS_COM_TELEM_USB, buf, size);
if(DeviceState == downloading) if (DeviceState == downloading)
PIOS_DELAY_WaitmS(10); PIOS_DELAY_WaitmS(10);
} else if (ProgPort == Serial) { } else if (ProgPort == Serial) {

View File

@ -41,7 +41,8 @@
void PIOS_SPI_ahrs_irq_handler(void); void PIOS_SPI_ahrs_irq_handler(void);
void DMA1_Channel4_IRQHandler() __attribute__ ((alias ("PIOS_SPI_ahrs_irq_handler"))); void DMA1_Channel4_IRQHandler() __attribute__ ((alias ("PIOS_SPI_ahrs_irq_handler")));
void DMA1_Channel5_IRQHandler() __attribute__ ((alias ("PIOS_SPI_ahrs_irq_handler"))); void DMA1_Channel5_IRQHandler() __attribute__ ((alias ("PIOS_SPI_ahrs_irq_handler")));
const struct pios_spi_cfg pios_spi_ahrs_cfg = { const struct pios_spi_cfg
pios_spi_ahrs_cfg = {
.regs = SPI2, .regs = SPI2,
.init = { .init = {
.SPI_Mode = SPI_Mode_Master, .SPI_Mode = SPI_Mode_Master,
@ -97,51 +98,44 @@ const struct pios_spi_cfg pios_spi_ahrs_cfg = {
.DMA_M2M = DMA_M2M_Disable, .DMA_M2M = DMA_M2M_Disable,
}, },
}, },
}, }, .ssel = {
.ssel = {
.gpio = GPIOB, .gpio = GPIOB,
.init = { .init = {
.GPIO_Pin = GPIO_Pin_12, .GPIO_Pin = GPIO_Pin_12,
.GPIO_Speed = GPIO_Speed_50MHz, .GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_Out_PP, .GPIO_Mode = GPIO_Mode_Out_PP,
}, },
}, }, .sclk = {
.sclk = {
.gpio = GPIOB, .gpio = GPIOB,
.init = { .init = {
.GPIO_Pin = GPIO_Pin_13, .GPIO_Pin = GPIO_Pin_13,
.GPIO_Speed = GPIO_Speed_50MHz, .GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_AF_PP, .GPIO_Mode = GPIO_Mode_AF_PP,
}, },
}, }, .miso = {
.miso = {
.gpio = GPIOB, .gpio = GPIOB,
.init = { .init = {
.GPIO_Pin = GPIO_Pin_14, .GPIO_Pin = GPIO_Pin_14,
.GPIO_Speed = GPIO_Speed_50MHz, .GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_IN_FLOATING, .GPIO_Mode = GPIO_Mode_IN_FLOATING,
}, },
}, }, .mosi = {
.mosi = {
.gpio = GPIOB, .gpio = GPIOB,
.init = { .init = {
.GPIO_Pin = GPIO_Pin_15, .GPIO_Pin = GPIO_Pin_15,
.GPIO_Speed = GPIO_Speed_50MHz, .GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_AF_PP, .GPIO_Mode = GPIO_Mode_AF_PP,
}, },
}, }, };
};
uint32_t pios_spi_ahrs_id; uint32_t pios_spi_ahrs_id;
void PIOS_SPI_ahrs_irq_handler(void) void PIOS_SPI_ahrs_irq_handler(void) {
{
/* Call into the generic code to handle the IRQ for this specific device */ /* Call into the generic code to handle the IRQ for this specific device */
PIOS_SPI_IRQ_Handler(pios_spi_ahrs_id); PIOS_SPI_IRQ_Handler(pios_spi_ahrs_id);
} }
#endif /* PIOS_INCLUDE_SPI */ #endif /* PIOS_INCLUDE_SPI */
#if defined(PIOS_INCLUDE_USART) #if defined(PIOS_INCLUDE_USART)
#include "pios_usart_priv.h" #include "pios_usart_priv.h"
@ -151,21 +145,18 @@ void PIOS_SPI_ahrs_irq_handler(void)
*/ */
void PIOS_USART_telem_irq_handler(void); void PIOS_USART_telem_irq_handler(void);
void USART2_IRQHandler() __attribute__ ((alias ("PIOS_USART_telem_irq_handler"))); void USART2_IRQHandler() __attribute__ ((alias ("PIOS_USART_telem_irq_handler")));
const struct pios_usart_cfg pios_usart_telem_cfg = { const struct pios_usart_cfg pios_usart_telem_cfg = { .regs = USART2, .init = {
.regs = USART2, #if defined (PIOS_COM_TELEM_BAUDRATE)
.init = {
#if defined (PIOS_COM_TELEM_BAUDRATE)
.USART_BaudRate = PIOS_COM_TELEM_BAUDRATE, .USART_BaudRate = PIOS_COM_TELEM_BAUDRATE,
#else #else
.USART_BaudRate = 57600, .USART_BaudRate = 57600,
#endif #endif
.USART_WordLength = USART_WordLength_8b, .USART_WordLength = USART_WordLength_8b,
.USART_Parity = USART_Parity_No, .USART_Parity = USART_Parity_No,
.USART_StopBits = USART_StopBits_1, .USART_StopBits = USART_StopBits_1,
.USART_HardwareFlowControl = USART_HardwareFlowControl_None, .USART_HardwareFlowControl = USART_HardwareFlowControl_None,
.USART_Mode = USART_Mode_Rx | USART_Mode_Tx, .USART_Mode = USART_Mode_Rx | USART_Mode_Tx,
}, }, .irq = {
.irq = {
.handler = PIOS_USART_telem_irq_handler, .handler = PIOS_USART_telem_irq_handler,
.init = { .init = {
.NVIC_IRQChannel = USART2_IRQn, .NVIC_IRQChannel = USART2_IRQn,
@ -173,28 +164,24 @@ const struct pios_usart_cfg pios_usart_telem_cfg = {
.NVIC_IRQChannelSubPriority = 0, .NVIC_IRQChannelSubPriority = 0,
.NVIC_IRQChannelCmd = ENABLE, .NVIC_IRQChannelCmd = ENABLE,
}, },
}, }, .rx = {
.rx = {
.gpio = GPIOA, .gpio = GPIOA,
.init = { .init = {
.GPIO_Pin = GPIO_Pin_3, .GPIO_Pin = GPIO_Pin_3,
.GPIO_Speed = GPIO_Speed_2MHz, .GPIO_Speed = GPIO_Speed_2MHz,
.GPIO_Mode = GPIO_Mode_IPU, .GPIO_Mode = GPIO_Mode_IPU,
}, },
}, }, .tx = {
.tx = {
.gpio = GPIOA, .gpio = GPIOA,
.init = { .init = {
.GPIO_Pin = GPIO_Pin_2, .GPIO_Pin = GPIO_Pin_2,
.GPIO_Speed = GPIO_Speed_2MHz, .GPIO_Speed = GPIO_Speed_2MHz,
.GPIO_Mode = GPIO_Mode_AF_PP, .GPIO_Mode = GPIO_Mode_AF_PP,
}, },
}, }, };
};
static uint32_t pios_usart_telem_rf_id; static uint32_t pios_usart_telem_rf_id;
void PIOS_USART_telem_irq_handler(void) void PIOS_USART_telem_irq_handler(void) {
{
PIOS_USART_IRQ_Handler(pios_usart_telem_rf_id); PIOS_USART_IRQ_Handler(pios_usart_telem_rf_id);
} }
@ -220,7 +207,6 @@ uint32_t pios_com_telem_usb_id;
*/ */
void PIOS_Board_Init(void) { void PIOS_Board_Init(void) {
/* Enable Prefetch Buffer */ /* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
@ -235,7 +221,8 @@ void PIOS_Board_Init(void) {
if (PIOS_USART_Init(&pios_usart_telem_rf_id, &pios_usart_telem_cfg)) { if (PIOS_USART_Init(&pios_usart_telem_rf_id, &pios_usart_telem_cfg)) {
PIOS_DEBUG_Assert(0); PIOS_DEBUG_Assert(0);
} }
if (PIOS_COM_Init(&pios_com_telem_rf_id, &pios_usart_com_driver, pios_usart_telem_rf_id)) { if (PIOS_COM_Init(&pios_com_telem_rf_id, &pios_usart_com_driver,
pios_usart_telem_rf_id)) {
PIOS_DEBUG_Assert(0); PIOS_DEBUG_Assert(0);
} }
#endif /* PIOS_INCLUDE_COM */ #endif /* PIOS_INCLUDE_COM */

View File

@ -1,71 +1,71 @@
/*********************************************************************************************************** /***********************************************************************************************************
* *
* NAME: ssp.c * NAME: ssp.c
* DESCRIPTION: simple serial protocol - packet based serial transport layer. * DESCRIPTION: simple serial protocol - packet based serial transport layer.
* AUTHOR: Joe Hlebasko * AUTHOR: Joe Hlebasko
* HISTORY: Created 1/1/2010 * HISTORY: Created 1/1/2010
* *
* Packet Formats * Packet Formats
* Format: * Format:
* +------+----+------+---------------------------+--------+ * +------+----+------+---------------------------+--------+
* | 225 | L1 | S# | App Data (0-254 bytes) | CRC 16 | * | 225 | L1 | S# | App Data (0-254 bytes) | CRC 16 |
* +------+----+------+---------------------------+--------+ * +------+----+------+---------------------------+--------+
* *
* 225 = sync byte, indicates start of a packet * 225 = sync byte, indicates start of a packet
* L1 = 1 byte for size of data payload. (sequence number is part of data payload.) * L1 = 1 byte for size of data payload. (sequence number is part of data payload.)
* S# = 1 byte for sequence number. * S# = 1 byte for sequence number.
* Seq of 0 = seq # synchronise request, forces other end to reset receive sequence number to 1. * Seq of 0 = seq # synchronise request, forces other end to reset receive sequence number to 1.
* sender of synchronise request will reset the tx seq number to 1 * sender of synchronise request will reset the tx seq number to 1
* Seq # of 1..127 = normal data packets. Sequence number is incremented by for each transmitted * Seq # of 1..127 = normal data packets. Sequence number is incremented by for each transmitted
* packet. Rolls over from 127 to 1. * packet. Rolls over from 127 to 1.
* if most sig. bit is set then the packet is an ACK packet of data packet sequence number of the * if most sig. bit is set then the packet is an ACK packet of data packet sequence number of the
* lower 7 bits (1..127) * lower 7 bits (1..127)
* App Data may contain 0..254 bytes. The sequence number is consider part of the payload. * App Data may contain 0..254 bytes. The sequence number is consider part of the payload.
* CRC 16 - 16 bits of CRC values of Sequence # and data bytes. * CRC 16 - 16 bits of CRC values of Sequence # and data bytes.
* *
* Protocol has two types of packets: data and ack packets. ACK packets have the most sig. bit set in the * Protocol has two types of packets: data and ack packets. ACK packets have the most sig. bit set in the
* sequence number, this implies that valid sequence numbers are 1..127 * sequence number, this implies that valid sequence numbers are 1..127
* *
* This protocol uses the concept of sequences numbers to determine if a given packet has been received. This * This protocol uses the concept of sequences numbers to determine if a given packet has been received. This
* requires both devices to be able to synchronize sequence numbers. This is accomplished by sending a packet * requires both devices to be able to synchronize sequence numbers. This is accomplished by sending a packet
* length 1 and sequence number = 0. The receive then resets it's transmit sequence number to 1. * length 1 and sequence number = 0. The receive then resets it's transmit sequence number to 1.
* *
* ACTIVE_SYNCH is a version that will automatically send a synch request if it receives a synch packet. Only * ACTIVE_SYNCH is a version that will automatically send a synch request if it receives a synch packet. Only
* one device in the communication should do otherwise you end up with an endless loops of synchronization. * one device in the communication should do otherwise you end up with an endless loops of synchronization.
* Right now each side needs to manually issues a synch request. * Right now each side needs to manually issues a synch request.
* *
* This protocol is best used in cases where one device is the master and the other is the slave, or a don't * This protocol is best used in cases where one device is the master and the other is the slave, or a don't
* speak unless spoken to type of approach. * speak unless spoken to type of approach.
* *
* The following are items are required to initialize a port for communications: * The following are items are required to initialize a port for communications:
* 1. The number attempts for each packet * 1. The number attempts for each packet
* 2. time to wait for an ack. * 2. time to wait for an ack.
* 3. pointer to buffer to be used for receiving. * 3. pointer to buffer to be used for receiving.
* 4. pointer to a buffer to be used for transmission * 4. pointer to a buffer to be used for transmission
* 5. length of each buffer (rx and tx) * 5. length of each buffer (rx and tx)
* 6. Four functions: * 6. Four functions:
* 1. write byte = writes a byte out the serial port (or other comm device) * 1. write byte = writes a byte out the serial port (or other comm device)
* 2. read byte = retrieves a byte from the serial port. Returns -1 if a byte is not available * 2. read byte = retrieves a byte from the serial port. Returns -1 if a byte is not available
* 3. callback = function to call when a valid data packet has been received. This function is responsible * 3. callback = function to call when a valid data packet has been received. This function is responsible
* to do what needs to be done with the data when it is received. The primary mission of this function * to do what needs to be done with the data when it is received. The primary mission of this function
* should be to copy the data to a private buffer out of the working receive buffer to prevent overrun. * should be to copy the data to a private buffer out of the working receive buffer to prevent overrun.
* processing should be kept to a minimum. * processing should be kept to a minimum.
* 4. get time = function should return the current time. Note that time units are not specified it just * 4. get time = function should return the current time. Note that time units are not specified it just
* needs to be some measure of time that increments as time passes by. The timeout values for a given * needs to be some measure of time that increments as time passes by. The timeout values for a given
* port should the units used/returned by the get time function. * port should the units used/returned by the get time function.
* *
* All of the state information of a communication port is contained in a Port_t structure. This allows this * All of the state information of a communication port is contained in a Port_t structure. This allows this
* module to operature on multiple communication ports with a single code base. * module to operature on multiple communication ports with a single code base.
* *
* The ssp_ReceiveProcess and ssp_SendProcess functions need to be called to process data through the * The ssp_ReceiveProcess and ssp_SendProcess functions need to be called to process data through the
* respective state machines. Typical implementation would have a serial ISR to pull bytes out of the UART * respective state machines. Typical implementation would have a serial ISR to pull bytes out of the UART
* and place into a circular buffer. The serial read function would then pull bytes out this buffer * and place into a circular buffer. The serial read function would then pull bytes out this buffer
* processing. The TX side has the write function placing bytes into a circular buffer with the TX ISR * processing. The TX side has the write function placing bytes into a circular buffer with the TX ISR
* pulling bytes out of the buffer and putting into the UART. It is possible to run the receive process from * pulling bytes out of the buffer and putting into the UART. It is possible to run the receive process from
* the receive ISR but care must be taken on processing data when it is received to avoid holding up the ISR * the receive ISR but care must be taken on processing data when it is received to avoid holding up the ISR
* and sending ACK packets from the receive ISR. * and sending ACK packets from the receive ISR.
* *
***********************************************************************************************************/ ***********************************************************************************************************/
/** INCLUDE FILES **/ /** INCLUDE FILES **/
@ -79,19 +79,17 @@
#define ESC 224 // ESC character used in Serial Protocol #define ESC 224 // ESC character used in Serial Protocol
#define ESC_SYNC 1 // ESC_SYNC character used in Serial Protocol #define ESC_SYNC 1 // ESC_SYNC character used in Serial Protocol
#define ACK_BIT 0x80 // Ack bit, bit 7 of sequence number, 1 = Acknowledge, 0 = #define ACK_BIT 0x80 // Ack bit, bit 7 of sequence number, 1 = Acknowledge, 0 =
// new packet // new packet
// packet location definitions. // packet location definitions.
#define LENGTH 0 #define LENGTH 0
#define SEQNUM 1 #define SEQNUM 1
#define DATA 2 #define DATA 2
// Make larger sized integers from smaller sized integers // Make larger sized integers from smaller sized integers
#define MAKEWORD16( ub, lb ) ((uint16_t)0x0000 | ((uint16_t)(ub) << 8) | (uint16_t)(lb) ) #define MAKEWORD16( ub, lb ) ((uint16_t)0x0000 | ((uint16_t)(ub) << 8) | (uint16_t)(lb) )
#define MAKEWORD32( uw, lw ) ((uint32_t)(0x0UL | ((uint32_t)(uw) << 16) | (uint32_t)(lw)) ) #define MAKEWORD32( uw, lw ) ((uint32_t)(0x0UL | ((uint32_t)(uw) << 16) | (uint32_t)(lw)) )
#define MAKEWORD32B( b3, b2, b1, b0 ) ((uint32_t)((uint32_t)(b3)<< 24) | ((uint32_t)(b2)<<16) | ((uint32_t)(b1)<<8) | ((uint32_t)(b0) ) #define MAKEWORD32B( b3, b2, b1, b0 ) ((uint32_t)((uint32_t)(b3)<< 24) | ((uint32_t)(b2)<<16) | ((uint32_t)(b1)<<8) | ((uint32_t)(b0) )
// Used to extract smaller integers from larger sized intergers // Used to extract smaller integers from larger sized intergers
#define LOWERBYTE( w ) (uint8_t)((w) & 0x00ff ) #define LOWERBYTE( w ) (uint8_t)((w) & 0x00ff )
#define UPPERBYTE( w ) (uint8_t)(((w) & 0xff00) >> 8 ) #define UPPERBYTE( w ) (uint8_t)(((w) & 0xff00) >> 8 )
@ -109,16 +107,17 @@
/** PRIVATE FUNCTIONS **/ /** PRIVATE FUNCTIONS **/
//static void sf_SendSynchPacket( Port_t *thisport ); //static void sf_SendSynchPacket( Port_t *thisport );
static uint16_t sf_crc16( uint16_t crc, uint8_t data ); static uint16_t sf_crc16(uint16_t crc, uint8_t data);
static void sf_write_byte( Port_t *thisport, uint8_t c ); static void sf_write_byte(Port_t *thisport, uint8_t c);
static void sf_SetSendTimeout( Port_t *thisport ); static void sf_SetSendTimeout(Port_t *thisport);
static uint16_t sf_CheckTimeout( Port_t *thisport ); static uint16_t sf_CheckTimeout(Port_t *thisport);
static int16_t sf_DecodeState( Port_t *thisport, uint8_t c ); static int16_t sf_DecodeState(Port_t *thisport, uint8_t c);
static int16_t sf_ReceiveState( Port_t *thisport, uint8_t c ); static int16_t sf_ReceiveState(Port_t *thisport, uint8_t c);
static void sf_SendPacket( Port_t *thisport ); static void sf_SendPacket(Port_t *thisport);
static void sf_SendAckPacket( Port_t *thisport, uint8_t seqNumber); static void sf_SendAckPacket(Port_t *thisport, uint8_t seqNumber);
static void sf_MakePacket( uint8_t *buf, const uint8_t * pdata, uint16_t length, uint8_t seqNo ); static void sf_MakePacket(uint8_t *buf, const uint8_t * pdata, uint16_t length,
uint8_t seqNo);
static int16_t sf_ReceivePacket(Port_t *thisport); static int16_t sf_ReceivePacket(Port_t *thisport);
/* Flag bit masks...*/ /* Flag bit masks...*/
@ -131,40 +130,35 @@ static int16_t sf_ReceivePacket(Port_t *thisport);
#define SSP_IDLE 2 #define SSP_IDLE 2
/** PRIVATE DATA **/ /** PRIVATE DATA **/
static const uint16_t CRC_TABLE[] = { static const uint16_t CRC_TABLE[] = { 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301,
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, 0x03C0, 0x0280, 0xC241, 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, 0xC481, 0x0440, 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81,
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0E40, 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00,
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, 0x1400, 0xD4C1,
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, 0xD201, 0x12C0, 0x1380,
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040, 0xF001, 0x30C0, 0x3180, 0xF141,
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040, 0x3300, 0xF3C1, 0xF281, 0x3240, 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501,
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, 0x35C0, 0x3480, 0xF441, 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0,
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, 0x3E80, 0xFE41, 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881,
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0x3840, 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40, 0xE401,
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, 0x2200, 0xE2C1,
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041, 0xA001, 0x60C0, 0x6180,
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, 0x6600, 0xA6C1, 0xA781, 0x6740,
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041, 0xA501, 0x65C0, 0x6480, 0xA441, 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01,
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, 0x6FC0, 0x6E80, 0xAE41, 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1,
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, 0xA881, 0x6840, 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80,
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, 0xBA41, 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200,
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, 0x5000, 0x90C1,
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, 0x9601, 0x56C0, 0x5780,
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440, 0x9C01, 0x5CC0, 0x5D80, 0x9D41,
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901,
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, 0x59C0, 0x5880, 0x9841, 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1,
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440, 0x8A81, 0x4A40, 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80,
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, 0x8C41, 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040 };
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
};
/** EXTERNAL DATA **/ /** EXTERNAL DATA **/
@ -172,7 +166,6 @@ static const uint16_t CRC_TABLE[] = {
/** VERIFICATION FUNCTIONS **/ /** VERIFICATION FUNCTIONS **/
/***********************************************************************************************************/ /***********************************************************************************************************/
/*! /*!
@ -185,8 +178,7 @@ static const uint16_t CRC_TABLE[] = {
* Must be called before calling the Send or REceive process functions. * Must be called before calling the Send or REceive process functions.
*/ */
void ssp_Init( Port_t *thisport, const PortConfig_t* const info) void ssp_Init(Port_t *thisport, const PortConfig_t* const info) {
{
thisport->pfCallBack = info->pfCallBack; thisport->pfCallBack = info->pfCallBack;
thisport->pfSerialRead = info->pfSerialRead; thisport->pfSerialRead = info->pfSerialRead;
thisport->pfSerialWrite = info->pfSerialWrite; thisport->pfSerialWrite = info->pfSerialWrite;
@ -216,11 +208,10 @@ void ssp_Init( Port_t *thisport, const PortConfig_t* const info)
* \note * \note
* *
*/ */
int16_t ssp_SendProcess( Port_t *thisport ) int16_t ssp_SendProcess(Port_t *thisport) {
{
int16_t value = SSP_TX_WAITING; int16_t value = SSP_TX_WAITING;
if (thisport->SendState == SSP_AWAITING_ACK ) { if (thisport->SendState == SSP_AWAITING_ACK) {
if (sf_CheckTimeout(thisport) == TRUE) { if (sf_CheckTimeout(thisport) == TRUE) {
if (thisport->retryCount < thisport->maxRetryCount) { if (thisport->retryCount < thisport->maxRetryCount) {
// Try again // Try again
@ -230,7 +221,7 @@ int16_t ssp_SendProcess( Port_t *thisport )
} else { } else {
// Give up, # of trys has exceded the limit // Give up, # of trys has exceded the limit
#ifdef DEBUG_SSP #ifdef DEBUG_SSP
char str[63]={0}; char str[63]= {0};
sprintf(str,"Send Timeout|"); sprintf(str,"Send Timeout|");
PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); PIOS_COM_SendString(PIOS_COM_TELEM_USB,str);
#endif #endif
@ -241,7 +232,7 @@ int16_t ssp_SendProcess( Port_t *thisport )
} else { } else {
value = SSP_TX_WAITING; value = SSP_TX_WAITING;
} }
} else if( thisport->SendState == SSP_ACKED ) { } else if (thisport->SendState == SSP_ACKED) {
SETBIT( thisport->flags, ACK_RECEIVED); SETBIT( thisport->flags, ACK_RECEIVED);
value = SSP_TX_ACKED; value = SSP_TX_ACKED;
thisport->SendState = SSP_IDLE; thisport->SendState = SSP_IDLE;
@ -260,8 +251,7 @@ int16_t ssp_SendProcess( Port_t *thisport )
* \note * \note
* *
*/ */
int16_t ssp_ReceiveProcess(Port_t *thisport) int16_t ssp_ReceiveProcess(Port_t *thisport) {
{
int16_t b; int16_t b;
int16_t packet_status = SSP_RX_IDLE; int16_t packet_status = SSP_RX_IDLE;
@ -284,13 +274,12 @@ int16_t ssp_ReceiveProcess(Port_t *thisport)
* *
*/ */
int16_t ssp_ReceiveByte(Port_t *thisport ) int16_t ssp_ReceiveByte(Port_t *thisport) {
{
int16_t b; int16_t b;
int16_t packet_status = SSP_RX_IDLE; int16_t packet_status = SSP_RX_IDLE;
b = thisport->pfSerialRead(); b = thisport->pfSerialRead();
if( b != -1 ) { if (b != -1) {
packet_status = sf_ReceiveState(thisport, b); packet_status = sf_ReceiveState(thisport, b);
} }
return packet_status; return packet_status;
@ -307,17 +296,16 @@ int16_t ssp_ReceiveByte(Port_t *thisport )
* \note * \note
* *
*/ */
uint16_t ssp_SendDataBlock( Port_t *thisport, uint8_t *data, uint16_t length ) uint16_t ssp_SendDataBlock(Port_t *thisport, uint8_t *data, uint16_t length) {
{
int16_t packet_status = SSP_TX_WAITING; int16_t packet_status = SSP_TX_WAITING;
uint16_t retval = FALSE; uint16_t retval = FALSE;
packet_status = ssp_SendData( thisport, data, length ); // send the data packet_status = ssp_SendData(thisport, data, length); // send the data
while( packet_status == SSP_TX_WAITING ) { // check the status while (packet_status == SSP_TX_WAITING) { // check the status
(void)ssp_ReceiveProcess( thisport ); // process any bytes received. (void) ssp_ReceiveProcess(thisport); // process any bytes received.
packet_status = ssp_SendProcess( thisport ); // check the send status packet_status = ssp_SendProcess(thisport); // check the send status
} }
if( packet_status == SSP_TX_ACKED ) { // figure out what happened to the packet if (packet_status == SSP_TX_ACKED) { // figure out what happened to the packet
retval = TRUE; retval = TRUE;
} else { } else {
retval = FALSE; retval = FALSE;
@ -337,15 +325,15 @@ uint16_t ssp_SendDataBlock( Port_t *thisport, uint8_t *data, uint16_t length )
* \note * \note
* *
*/ */
int16_t ssp_SendData( Port_t *thisport, const uint8_t *data, const uint16_t length ) int16_t ssp_SendData(Port_t *thisport, const uint8_t *data,
{ const uint16_t length) {
int16_t value = SSP_TX_WAITING; int16_t value = SSP_TX_WAITING;
if( (length + 2) > thisport->txBufSize ) { if ((length + 2) > thisport->txBufSize) {
// TRYING to send too much data. // TRYING to send too much data.
value = SSP_TX_BUFOVERRUN; value = SSP_TX_BUFOVERRUN;
} else if( thisport->SendState == SSP_IDLE ) { } else if (thisport->SendState == SSP_IDLE) {
#ifdef ACTIVE_SYNCH #ifdef ACTIVE_SYNCH
if( thisport->sendSynch == TRUE ) { if( thisport->sendSynch == TRUE ) {
sf_SendSynchPacket(thisport); sf_SendSynchPacket(thisport);
@ -373,7 +361,7 @@ int16_t ssp_SendData( Port_t *thisport, const uint8_t *data, const uint16_t leng
#else #else
CLEARBIT( thisport->txSeqNo, ACK_BIT ); // make sure we are not sending a ACK packet CLEARBIT( thisport->txSeqNo, ACK_BIT ); // make sure we are not sending a ACK packet
thisport->txSeqNo++; // update the sequence number. thisport->txSeqNo++; // update the sequence number.
if( thisport->txSeqNo > 0x7F) { // check for sequence number rollover if (thisport->txSeqNo > 0x7F) { // check for sequence number rollover
thisport->txSeqNo = 1; // if we do have rollover then reset to 1 not zero, thisport->txSeqNo = 1; // if we do have rollover then reset to 1 not zero,
// zero is reserved for synchronization requests // zero is reserved for synchronization requests
} }
@ -382,18 +370,18 @@ int16_t ssp_SendData( Port_t *thisport, const uint8_t *data, const uint16_t leng
thisport->SendState = SSP_AWAITING_ACK; thisport->SendState = SSP_AWAITING_ACK;
value = SSP_TX_WAITING; value = SSP_TX_WAITING;
thisport->retryCount = 0; // zero out the retry counter for this transmission thisport->retryCount = 0; // zero out the retry counter for this transmission
sf_MakePacket( thisport->txBuf, data, length, thisport->txSeqNo ); sf_MakePacket(thisport->txBuf, data, length, thisport->txSeqNo);
sf_SendPacket( thisport ); // punch out the packet to the serial port sf_SendPacket(thisport); // punch out the packet to the serial port
sf_SetSendTimeout( thisport ); // do the timeout values sf_SetSendTimeout(thisport); // do the timeout values
#ifdef DEBUG_SSP #ifdef DEBUG_SSP
char str[63]={0}; char str[63]= {0};
sprintf(str,"Sent DATA PACKET:%d|",thisport->txSeqNo); sprintf(str,"Sent DATA PACKET:%d|",thisport->txSeqNo);
PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); PIOS_COM_SendString(PIOS_COM_TELEM_USB,str);
#endif #endif
} else { } else {
// error we are already sending a packet. Need to wait for the current packet to be acked or timeout. // error we are already sending a packet. Need to wait for the current packet to be acked or timeout.
#ifdef DEBUG_SSP #ifdef DEBUG_SSP
char str[63]={0}; char str[63]= {0};
sprintf(str,"Error sending TX was busy|"); sprintf(str,"Error sending TX was busy|");
PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); PIOS_COM_SendString(PIOS_COM_TELEM_USB,str);
#endif #endif
@ -416,8 +404,7 @@ int16_t ssp_SendData( Port_t *thisport, const uint8_t *data, const uint16_t leng
* if number of tries exceed maximum try limit then exit * if number of tries exceed maximum try limit then exit
* C. goto A * C. goto A
*/ */
uint16_t ssp_Synchronise( Port_t *thisport ) uint16_t ssp_Synchronise(Port_t *thisport) {
{
int16_t packet_status; int16_t packet_status;
uint16_t retval = FALSE; uint16_t retval = FALSE;
@ -425,20 +412,20 @@ uint16_t ssp_Synchronise( Port_t *thisport )
thisport->txSeqNo = 0; // make this zero to cause the other end to re-synch with us thisport->txSeqNo = 0; // make this zero to cause the other end to re-synch with us
SETBIT(thisport->flags, SENT_SYNCH); SETBIT(thisport->flags, SENT_SYNCH);
// TODO - should this be using ssp_SendPacketData()?? // TODO - should this be using ssp_SendPacketData()??
sf_MakePacket( thisport->txBuf, NULL, 0, thisport->txSeqNo ); // construct the packet sf_MakePacket(thisport->txBuf, NULL, 0, thisport->txSeqNo); // construct the packet
sf_SendPacket( thisport ); sf_SendPacket(thisport);
sf_SetSendTimeout( thisport ); sf_SetSendTimeout(thisport);
thisport->SendState = SSP_AWAITING_ACK; thisport->SendState = SSP_AWAITING_ACK;
packet_status = SSP_TX_WAITING; packet_status = SSP_TX_WAITING;
#else #else
packet_status = ssp_SendData( thisport, NULL, 0 ); packet_status = ssp_SendData( thisport, NULL, 0 );
#endif #endif
while( packet_status == SSP_TX_WAITING ) { // we loop until we time out. while (packet_status == SSP_TX_WAITING) { // we loop until we time out.
(void)ssp_ReceiveProcess( thisport ); // do the receive process (void) ssp_ReceiveProcess(thisport); // do the receive process
packet_status = ssp_SendProcess( thisport ); // do the send process packet_status = ssp_SendProcess(thisport); // do the send process
} }
thisport->sendSynch = FALSE; thisport->sendSynch = FALSE;
switch( packet_status ) { switch (packet_status) {
case SSP_TX_ACKED: case SSP_TX_ACKED:
retval = TRUE; retval = TRUE;
break; break;
@ -454,7 +441,6 @@ uint16_t ssp_Synchronise( Port_t *thisport )
return retval; return retval;
} }
/*! /*!
* \brief sends out a preformatted packet for a give port * \brief sends out a preformatted packet for a give port
* \param thisport = which port to use. * \param thisport = which port to use.
@ -463,20 +449,18 @@ uint16_t ssp_Synchronise( Port_t *thisport )
* \note * \note
* Packet should be formed through the use of sf_MakePacket before calling this function. * Packet should be formed through the use of sf_MakePacket before calling this function.
*/ */
static void sf_SendPacket(Port_t *thisport) static void sf_SendPacket(Port_t *thisport) {
{
// add 3 to packet data length for: 1 length + 2 CRC (packet overhead) // add 3 to packet data length for: 1 length + 2 CRC (packet overhead)
uint8_t packetLen = thisport->txBuf[LENGTH] + 3; uint8_t packetLen = thisport->txBuf[LENGTH] + 3;
// use the raw serial write function so the SYNC byte does not get 'escaped' // use the raw serial write function so the SYNC byte does not get 'escaped'
thisport->pfSerialWrite(SYNC); thisport->pfSerialWrite(SYNC);
for( uint8_t x = 0; x < packetLen; x++ ) { for (uint8_t x = 0; x < packetLen; x++) {
sf_write_byte(thisport, thisport->txBuf[x] ); sf_write_byte(thisport, thisport->txBuf[x]);
} }
thisport->retryCount++; thisport->retryCount++;
} }
/*! /*!
* \brief converts data to transport layer protocol packet format. * \brief converts data to transport layer protocol packet format.
* \param txbuf = buffer to use when forming the packet * \param txbuf = buffer to use when forming the packet
@ -493,8 +477,8 @@ static void sf_SendPacket(Port_t *thisport)
* 3. TODO: Should this function return an error if data length to be sent is greater th tx buffer size? * 3. TODO: Should this function return an error if data length to be sent is greater th tx buffer size?
* *
*/ */
void sf_MakePacket( uint8_t *txBuf, const uint8_t * pdata, uint16_t length, uint8_t seqNo ) void sf_MakePacket(uint8_t *txBuf, const uint8_t * pdata, uint16_t length,
{ uint8_t seqNo) {
uint16_t crc = 0xffff; uint16_t crc = 0xffff;
uint16_t bufPos = 0; uint16_t bufPos = 0;
uint8_t b; uint8_t b;
@ -502,13 +486,13 @@ void sf_MakePacket( uint8_t *txBuf, const uint8_t * pdata, uint16_t length, uint
// add 1 for the seq. number // add 1 for the seq. number
txBuf[LENGTH] = length + 1; txBuf[LENGTH] = length + 1;
txBuf[SEQNUM] = seqNo; txBuf[SEQNUM] = seqNo;
crc = sf_crc16( crc, seqNo ); crc = sf_crc16(crc, seqNo);
length = length + 2; // add two for the length and seqno bytes which are added before the loop. length = length + 2; // add two for the length and seqno bytes which are added before the loop.
for( bufPos = 2; bufPos < length; bufPos++ ) { for (bufPos = 2; bufPos < length; bufPos++) {
b = *pdata++; b = *pdata++;
txBuf[bufPos] = b; txBuf[bufPos] = b;
crc = sf_crc16( crc, b ); // update CRC value crc = sf_crc16(crc, b); // update CRC value
} }
txBuf[bufPos++] = LOWERBYTE(crc); txBuf[bufPos++] = LOWERBYTE(crc);
txBuf[bufPos] = UPPERBYTE(crc); txBuf[bufPos] = UPPERBYTE(crc);
@ -525,18 +509,17 @@ void sf_MakePacket( uint8_t *txBuf, const uint8_t * pdata, uint16_t length, uint
* *
*/ */
static void sf_SendAckPacket( Port_t *thisport, uint8_t seqNumber) static void sf_SendAckPacket(Port_t *thisport, uint8_t seqNumber) {
{
#ifdef DEBUG_SSP #ifdef DEBUG_SSP
char str[63]={0}; char str[63]= {0};
sprintf(str,"Sent ACK PACKET:%d|",seqNumber); sprintf(str,"Sent ACK PACKET:%d|",seqNumber);
PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); PIOS_COM_SendString(PIOS_COM_TELEM_USB,str);
#endif #endif
uint8_t AckSeqNumber = SETBIT( seqNumber, ACK_BIT ); uint8_t AckSeqNumber = SETBIT( seqNumber, ACK_BIT );
// create the packet, note we pass AckSequenceNumber directly // create the packet, note we pass AckSequenceNumber directly
sf_MakePacket( thisport->txBuf, NULL, 0, AckSeqNumber ); sf_MakePacket(thisport->txBuf, NULL, 0, AckSeqNumber);
sf_SendPacket( thisport ); sf_SendPacket(thisport);
// we don't set the timeout for an ACK because we don't ACK our ACKs in this protocol // we don't set the timeout for an ACK because we don't ACK our ACKs in this protocol
} }
@ -549,30 +532,29 @@ static void sf_SendAckPacket( Port_t *thisport, uint8_t seqNumber)
* \note * \note
* *
*/ */
static void sf_write_byte( Port_t *thisport, uint8_t c ) static void sf_write_byte(Port_t *thisport, uint8_t c) {
{ if (c == SYNC) { // check for SYNC byte
if( c == SYNC ) { // check for SYNC byte thisport->pfSerialWrite(ESC); // since we are not starting a packet we must ESCAPE the SYNCH byte
thisport->pfSerialWrite( ESC ); // since we are not starting a packet we must ESCAPE the SYNCH byte thisport->pfSerialWrite(ESC_SYNC); // now send the escaped synch char
thisport->pfSerialWrite( ESC_SYNC ); // now send the escaped synch char } else if (c == ESC) { // Check for ESC character
} else if( c == ESC ) { // Check for ESC character thisport->pfSerialWrite(ESC); // if it is, we need to send it twice
thisport->pfSerialWrite( ESC ); // if it is, we need to send it twice thisport->pfSerialWrite(ESC);
thisport->pfSerialWrite( ESC );
} else { } else {
thisport->pfSerialWrite( c ); // otherwise write the byte to serial port thisport->pfSerialWrite(c); // otherwise write the byte to serial port
} }
} }
/************************************************************************************************************ /************************************************************************************************************
* *
* NAME: uint16_t ssp_crc16( uint16_t crc, uint16_t data ) * NAME: uint16_t ssp_crc16( uint16_t crc, uint16_t data )
* DESCRIPTION: Uses crc_table to calculate new crc * DESCRIPTION: Uses crc_table to calculate new crc
* ARGUMENTS: * ARGUMENTS:
* arg1: crc * arg1: crc
* arg2: data - byte to calculate into CRC * arg2: data - byte to calculate into CRC
* RETURN: New crc * RETURN: New crc
* CREATED: 5/8/02 * CREATED: 5/8/02
* *
*************************************************************************************************************/ *************************************************************************************************************/
/*! /*!
* \brief calculates the new CRC value for 'data' * \brief calculates the new CRC value for 'data'
* \param crc = current CRC value * \param crc = current CRC value
@ -583,12 +565,10 @@ static void sf_write_byte( Port_t *thisport, uint8_t c )
* *
*/ */
static uint16_t sf_crc16( uint16_t crc, uint8_t data ) static uint16_t sf_crc16(uint16_t crc, uint8_t data) {
{ return (crc >> 8) ^ CRC_TABLE[(crc ^ data) & 0x00FF];
return (crc >> 8) ^ CRC_TABLE[( crc ^ data ) & 0x00FF ];
} }
/*! /*!
* \brief sets the timeout for the given packet * \brief sets the timeout for the given packet
* \param thisport = which port to use * \param thisport = which port to use
@ -598,8 +578,7 @@ static uint16_t sf_crc16( uint16_t crc, uint8_t data )
* *
*/ */
static void sf_SetSendTimeout( Port_t *thisport ) static void sf_SetSendTimeout(Port_t *thisport) {
{
uint32_t timeout; uint32_t timeout;
timeout = thisport->pfGetTime() + thisport->timeoutLen; timeout = thisport->pfGetTime() + thisport->timeoutLen;
thisport->timeout = timeout; thisport->timeout = timeout;
@ -614,19 +593,17 @@ static void sf_SetSendTimeout( Port_t *thisport )
* \note * \note
* *
*/ */
static uint16_t sf_CheckTimeout( Port_t *thisport ) static uint16_t sf_CheckTimeout(Port_t *thisport) {
{
uint16_t retval = FALSE; uint16_t retval = FALSE;
uint32_t current_time; uint32_t current_time;
current_time = thisport->pfGetTime(); current_time = thisport->pfGetTime();
if( current_time > thisport->timeout ) { if (current_time > thisport->timeout) {
retval = TRUE; retval = TRUE;
} }
return retval; return retval;
} }
/**************************************************************************** /****************************************************************************
* NAME: sf_ReceiveState * NAME: sf_ReceiveState
* DESC: Implements the receive state handling code for escaped and unescaped data * DESC: Implements the receive state handling code for escaped and unescaped data
@ -646,28 +623,27 @@ static uint16_t sf_CheckTimeout( Port_t *thisport )
* \note * \note
* *
*/ */
static int16_t sf_ReceiveState( Port_t *thisport, uint8_t c ) static int16_t sf_ReceiveState(Port_t *thisport, uint8_t c) {
{
int16_t retval = SSP_RX_RECEIVING; int16_t retval = SSP_RX_RECEIVING;
switch( thisport->InputState ) { switch (thisport->InputState) {
case state_unescaped_e: case state_unescaped_e:
if( c == SYNC ) { if (c == SYNC) {
thisport->DecodeState = decode_len1_e; thisport->DecodeState = decode_len1_e;
} else if ( c == ESC ) { } else if (c == ESC) {
thisport->InputState = state_escaped_e; thisport->InputState = state_escaped_e;
} else { } else {
retval = sf_DecodeState( thisport, c); retval = sf_DecodeState(thisport, c);
} }
break; // end of unescaped state. break; // end of unescaped state.
case state_escaped_e: case state_escaped_e:
thisport->InputState = state_unescaped_e; thisport->InputState = state_unescaped_e;
if( c == SYNC ) { if (c == SYNC) {
thisport->DecodeState = decode_len1_e; thisport->DecodeState = decode_len1_e;
} else if (c == ESC_SYNC ) { } else if (c == ESC_SYNC) {
retval = sf_DecodeState( thisport, SYNC); retval = sf_DecodeState(thisport, SYNC);
} else { } else {
retval = sf_DecodeState( thisport, c); retval = sf_DecodeState(thisport, c);
} }
break; // end of the escaped state. break; // end of the escaped state.
default: default:
@ -696,19 +672,18 @@ static int16_t sf_ReceiveState( Port_t *thisport, uint8_t c )
* \note * \note
* *
*/ */
static int16_t sf_DecodeState( Port_t *thisport, uint8_t c ) static int16_t sf_DecodeState(Port_t *thisport, uint8_t c) {
{
int16_t retval; int16_t retval;
switch( thisport->DecodeState ) { switch (thisport->DecodeState) {
case decode_idle_e: case decode_idle_e:
// 'c' is ignored in this state as the only way to leave the idle state is // 'c' is ignored in this state as the only way to leave the idle state is
// recognition of the SYNC byte in the sf_ReceiveState function. // recognition of the SYNC byte in the sf_ReceiveState function.
retval = SSP_RX_IDLE; retval = SSP_RX_IDLE;
break; break;
case decode_len1_e: case decode_len1_e:
thisport->rxBuf[LENGTH]= c; thisport->rxBuf[LENGTH] = c;
thisport->rxBufLen = c; thisport->rxBufLen = c;
if( thisport->rxBufLen <= thisport->rxBufSize ) { if (thisport->rxBufLen <= thisport->rxBufSize) {
thisport->DecodeState = decode_seqNo_e; thisport->DecodeState = decode_seqNo_e;
retval = SSP_RX_RECEIVING; retval = SSP_RX_RECEIVING;
} else { } else {
@ -722,8 +697,8 @@ static int16_t sf_DecodeState( Port_t *thisport, uint8_t c )
thisport->rxBufLen--; // subtract 1 for the seq. no. thisport->rxBufLen--; // subtract 1 for the seq. no.
thisport->rxBufPos = 2; thisport->rxBufPos = 2;
thisport->crc = sf_crc16( thisport->crc, c ); thisport->crc = sf_crc16(thisport->crc, c);
if( thisport->rxBufLen > 0 ) { if (thisport->rxBufLen > 0) {
thisport->DecodeState = decode_data_e; thisport->DecodeState = decode_data_e;
} else { } else {
thisport->DecodeState = decode_crc1_e; thisport->DecodeState = decode_crc1_e;
@ -731,24 +706,24 @@ static int16_t sf_DecodeState( Port_t *thisport, uint8_t c )
retval = SSP_RX_RECEIVING; retval = SSP_RX_RECEIVING;
break; break;
case decode_data_e: case decode_data_e:
thisport->rxBuf[ (thisport->rxBufPos)++] = c; thisport->rxBuf[(thisport->rxBufPos)++] = c;
thisport->crc = sf_crc16( thisport->crc, c ); thisport->crc = sf_crc16(thisport->crc, c);
if( thisport->rxBufPos == (thisport->rxBufLen+2) ) { if (thisport->rxBufPos == (thisport->rxBufLen + 2)) {
thisport->DecodeState = decode_crc1_e; thisport->DecodeState = decode_crc1_e;
} }
retval = SSP_RX_RECEIVING; retval = SSP_RX_RECEIVING;
break; break;
case decode_crc1_e: case decode_crc1_e:
thisport->crc = sf_crc16( thisport->crc, c ); thisport->crc = sf_crc16(thisport->crc, c);
thisport->DecodeState = decode_crc2_e; thisport->DecodeState = decode_crc2_e;
retval = SSP_RX_RECEIVING; retval = SSP_RX_RECEIVING;
break; break;
case decode_crc2_e: case decode_crc2_e:
thisport->DecodeState = decode_idle_e; thisport->DecodeState = decode_idle_e;
// verify the CRC value for the packet // verify the CRC value for the packet
if( sf_crc16( thisport->crc, c) == 0) { if (sf_crc16(thisport->crc, c) == 0) {
// TODO shouldn't the return value of sf_ReceivePacket() be checked? // TODO shouldn't the return value of sf_ReceivePacket() be checked?
sf_ReceivePacket( thisport ); sf_ReceivePacket(thisport);
retval = SSP_RX_COMPLETE; retval = SSP_RX_COMPLETE;
} else { } else {
thisport->RxError++; thisport->RxError++;
@ -764,18 +739,18 @@ static int16_t sf_DecodeState( Port_t *thisport, uint8_t c )
} }
/************************************************************************************************************ /************************************************************************************************************
* *
* NAME: int16_t sf_ReceivePacket( ) * NAME: int16_t sf_ReceivePacket( )
* DESCRIPTION: Receive one packet, assumed that data is in rec.buff[] * DESCRIPTION: Receive one packet, assumed that data is in rec.buff[]
* ARGUMENTS: * ARGUMENTS:
* RETURN: 0 . no new packet was received, could be ack or same packet * RETURN: 0 . no new packet was received, could be ack or same packet
* 1 . new packet received * 1 . new packet received
* SSP_PACKET_? * SSP_PACKET_?
* SSP_PACKET_COMPLETE * SSP_PACKET_COMPLETE
* SSP_PACKET_ACK * SSP_PACKET_ACK
* CREATED: 5/8/02 * CREATED: 5/8/02
* *
*************************************************************************************************************/ *************************************************************************************************************/
/*! /*!
* \brief receive one packet. calls the callback function if needed. * \brief receive one packet. calls the callback function if needed.
* \param thisport = which port to use * \param thisport = which port to use
@ -787,18 +762,17 @@ static int16_t sf_DecodeState( Port_t *thisport, uint8_t c )
* Created: Oct 7, 2010 12:07:22 AM by joe * Created: Oct 7, 2010 12:07:22 AM by joe
*/ */
static int16_t sf_ReceivePacket(Port_t *thisport) static int16_t sf_ReceivePacket(Port_t *thisport) {
{
int16_t value = FALSE; int16_t value = FALSE;
if( ISBITSET(thisport->rxBuf[SEQNUM], ACK_BIT ) ) { if (ISBITSET(thisport->rxBuf[SEQNUM], ACK_BIT )) {
// Received an ACK packet, need to check if it matches the previous sent packet // Received an ACK packet, need to check if it matches the previous sent packet
if( ( thisport->rxBuf[SEQNUM] & 0x7F) == (thisport->txSeqNo & 0x7f)) { if ((thisport->rxBuf[SEQNUM] & 0x7F) == (thisport->txSeqNo & 0x7f)) {
// It matches the last packet sent by us // It matches the last packet sent by us
SETBIT( thisport->txSeqNo, ACK_BIT ); SETBIT( thisport->txSeqNo, ACK_BIT );
thisport->SendState = SSP_ACKED; thisport->SendState = SSP_ACKED;
#ifdef DEBUG_SSP #ifdef DEBUG_SSP
char str[63]={0}; char str[63]= {0};
sprintf(str,"Received ACK:%d|",(thisport->txSeqNo & 0x7F)); sprintf(str,"Received ACK:%d|",(thisport->txSeqNo & 0x7F));
PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); PIOS_COM_SendString(PIOS_COM_TELEM_USB,str);
#endif #endif
@ -807,7 +781,7 @@ static int16_t sf_ReceivePacket(Port_t *thisport)
// else ignore the ACK packet // else ignore the ACK packet
} else { } else {
// Received a 'data' packet, figure out what type of packet we received... // Received a 'data' packet, figure out what type of packet we received...
if( thisport->rxBuf[SEQNUM] == 0 ) { if (thisport->rxBuf[SEQNUM] == 0) {
#ifdef DEBUG_SSP #ifdef DEBUG_SSP
PIOS_COM_SendString(PIOS_COM_TELEM_USB,"Received SYNC Request|"); PIOS_COM_SendString(PIOS_COM_TELEM_USB,"Received SYNC Request|");
#endif #endif
@ -815,30 +789,30 @@ static int16_t sf_ReceivePacket(Port_t *thisport)
#ifdef ACTIVE_SYNCH #ifdef ACTIVE_SYNCH
thisport->sendSynch = TRUE; thisport->sendSynch = TRUE;
#endif #endif
sf_SendAckPacket( thisport, thisport->rxBuf[SEQNUM] ); sf_SendAckPacket(thisport, thisport->rxBuf[SEQNUM]);
thisport->rxSeqNo = 0; thisport->rxSeqNo = 0;
value = FALSE; value = FALSE;
} else if( thisport->rxBuf[SEQNUM] == thisport->rxSeqNo ) { } else if (thisport->rxBuf[SEQNUM] == thisport->rxSeqNo) {
// Already seen this packet, just ack it, don't act on the packet. // Already seen this packet, just ack it, don't act on the packet.
sf_SendAckPacket( thisport, thisport->rxBuf[SEQNUM] ); sf_SendAckPacket(thisport, thisport->rxBuf[SEQNUM]);
value = FALSE; value = FALSE;
} else { } else {
//New Packet //New Packet
thisport->rxSeqNo = thisport->rxBuf[SEQNUM]; thisport->rxSeqNo = thisport->rxBuf[SEQNUM];
// Let the application do something with the data/packet. // Let the application do something with the data/packet.
if( thisport->pfCallBack != NULL ) { if (thisport->pfCallBack != NULL) {
#ifdef DEBUG_SSP #ifdef DEBUG_SSP
char str[63]={0}; char str[63]= {0};
sprintf(str,"Received DATA PACKET:%d [0]=%d %d %d|",thisport->rxSeqNo,(uint8_t)thisport->rxBuf[2],(uint8_t)thisport->rxBuf[3],(uint8_t)thisport->rxBuf[4]); sprintf(str,"Received DATA PACKET:%d [0]=%d %d %d|",thisport->rxSeqNo,(uint8_t)thisport->rxBuf[2],(uint8_t)thisport->rxBuf[3],(uint8_t)thisport->rxBuf[4]);
PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); PIOS_COM_SendString(PIOS_COM_TELEM_USB,str);
#endif #endif
// skip the first two bytes (length and seq. no.) in the buffer. // skip the first two bytes (length and seq. no.) in the buffer.
thisport->pfCallBack( &(thisport->rxBuf[2]), thisport->rxBufLen); thisport->pfCallBack(&(thisport->rxBuf[2]), thisport->rxBufLen);
} }
// after we send the ACK, it is possible for the host to send a new packet. // after we send the ACK, it is possible for the host to send a new packet.
// Thus the application needs to copy the data and reset the receive buffer // Thus the application needs to copy the data and reset the receive buffer
// inside of thisport->pfCallBack() // inside of thisport->pfCallBack()
sf_SendAckPacket( thisport, thisport->rxBuf[SEQNUM] ); sf_SendAckPacket(thisport, thisport->rxBuf[SEQNUM]);
value = TRUE; value = TRUE;
} }
} }

View File

@ -25,17 +25,12 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Include files // Include files
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
#include "stm32f10x_tim.h" #include "stm32f10x_tim.h"
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Local definitions // Local definitions
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
@ -43,10 +38,10 @@
#define SSP_TIMER_TIMER_BASE TIM7 #define SSP_TIMER_TIMER_BASE TIM7
#define SSP_TIMER_TIMER_RCC RCC_APB1Periph_TIM7 #define SSP_TIMER_TIMER_RCC RCC_APB1Periph_TIM7
uint32_t SSP_TIMER_Init(u32 resolution) uint32_t SSP_TIMER_Init(u32 resolution) {
{
// enable timer clock // enable timer clock
if( SSP_TIMER_TIMER_RCC == RCC_APB2Periph_TIM1 || SSP_TIMER_TIMER_RCC == RCC_APB2Periph_TIM8 ) if (SSP_TIMER_TIMER_RCC == RCC_APB2Periph_TIM1 || SSP_TIMER_TIMER_RCC
== RCC_APB2Periph_TIM8)
RCC_APB2PeriphClockCmd(SSP_TIMER_TIMER_RCC, ENABLE); RCC_APB2PeriphClockCmd(SSP_TIMER_TIMER_RCC, ENABLE);
else else
RCC_APB1PeriphClockCmd(SSP_TIMER_TIMER_RCC, ENABLE); RCC_APB1PeriphClockCmd(SSP_TIMER_TIMER_RCC, ENABLE);
@ -54,7 +49,7 @@ uint32_t SSP_TIMER_Init(u32 resolution)
// time base configuration // time base configuration
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 0xffff; // max period TIM_TimeBaseStructure.TIM_Period = 0xffff; // max period
TIM_TimeBaseStructure.TIM_Prescaler = (72 * resolution)-1; // <resolution> uS accuracy @ 72 MHz TIM_TimeBaseStructure.TIM_Prescaler = (72 * resolution) - 1; // <resolution> uS accuracy @ 72 MHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(SSP_TIMER_TIMER_BASE, &TIM_TimeBaseStructure); TIM_TimeBaseInit(SSP_TIMER_TIMER_BASE, &TIM_TimeBaseStructure);
@ -68,13 +63,11 @@ uint32_t SSP_TIMER_Init(u32 resolution)
return 0; // no error return 0; // no error
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
//! Resets the SSP_TIMER //! Resets the SSP_TIMER
//! \return < 0 on errors //! \return < 0 on errors
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
uint32_t SSP_TIMER_Reset(void) uint32_t SSP_TIMER_Reset(void) {
{
// reset counter // reset counter
SSP_TIMER_TIMER_BASE->CNT = 1; // set to 1 instead of 0 to avoid new IRQ request SSP_TIMER_TIMER_BASE->CNT = 1; // set to 1 instead of 0 to avoid new IRQ request
TIM_ClearITPendingBit(SSP_TIMER_TIMER_BASE, TIM_IT_Update); TIM_ClearITPendingBit(SSP_TIMER_TIMER_BASE, TIM_IT_Update);
@ -82,17 +75,15 @@ uint32_t SSP_TIMER_Reset(void)
return 0; // no error return 0; // no error
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
//! Returns current value of SSP_TIMER //! Returns current value of SSP_TIMER
//! \return 1..65535: valid SSP_TIMER value //! \return 1..65535: valid SSP_TIMER value
//! \return 0xffffffff: counter overrun //! \return 0xffffffff: counter overrun
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
uint32_t SSP_TIMER_ValueGet(void) uint32_t SSP_TIMER_ValueGet(void) {
{
uint32_t value = SSP_TIMER_TIMER_BASE->CNT; uint32_t value = SSP_TIMER_TIMER_BASE->CNT;
if( TIM_GetITStatus(SSP_TIMER_TIMER_BASE, TIM_IT_Update) != RESET ) if (TIM_GetITStatus(SSP_TIMER_TIMER_BASE, TIM_IT_Update) != RESET)
SSP_TIMER_Reset(); SSP_TIMER_Reset();
return value; return value;

View File

@ -1,103 +0,0 @@
/**
******************************************************************************
* @addtogroup OpenPilotBL OpenPilot BootLoader
* @{
*
* @file stopwatch.c
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief Timer functions for the LED PWM.
* @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 files
/////////////////////////////////////////////////////////////////////////////
#include "stm32f10x_tim.h"
/////////////////////////////////////////////////////////////////////////////
// Local definitions
/////////////////////////////////////////////////////////////////////////////
//#define STOPWATCH_TIMER_BASE TIM6
//#define STOPWATCH_TIMER_RCC RCC_APB1Periph_TIM6
uint32_t STOPWATCH_Init(u32 resolution, TIM_TypeDef* TIM) {
uint32_t STOPWATCH_TIMER_RCC;
switch ((uint32_t)TIM) {
case (uint32_t)TIM6:
STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM6;
break;
case (uint32_t)TIM7:
STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM7;
break;
default:
STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM6;
}
// enable timer clock
if (STOPWATCH_TIMER_RCC == RCC_APB2Periph_TIM1 || STOPWATCH_TIMER_RCC
== RCC_APB2Periph_TIM8)
RCC_APB2PeriphClockCmd(STOPWATCH_TIMER_RCC, ENABLE);
else
RCC_APB1PeriphClockCmd(STOPWATCH_TIMER_RCC, ENABLE);
// time base configuration
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 0xffff; // max period
TIM_TimeBaseStructure.TIM_Prescaler = (72 * resolution) - 1; // <resolution> uS accuracy @ 72 MHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM, &TIM_TimeBaseStructure);
// enable interrupt request
TIM_ITConfig(TIM, TIM_IT_Update, ENABLE);
// start counter
TIM_Cmd(TIM, ENABLE);
return 0; // no error
}
/////////////////////////////////////////////////////////////////////////////
//! Resets the stopwatch
//! \return < 0 on errors
/////////////////////////////////////////////////////////////////////////////
uint32_t STOPWATCH_Reset(TIM_TypeDef* TIM) {
// reset counter
TIM->CNT = 1; // set to 1 instead of 0 to avoid new IRQ request
TIM_ClearITPendingBit(TIM, TIM_IT_Update);
return 0; // no error
}
/////////////////////////////////////////////////////////////////////////////
//! Returns current value of stopwatch
//! \return 1..65535: valid stopwatch value
//! \return 0xffffffff: counter overrun
/////////////////////////////////////////////////////////////////////////////
uint32_t STOPWATCH_ValueGet(TIM_TypeDef* TIM) {
uint32_t value = TIM->CNT;
if (TIM_GetITStatus(TIM, TIM_IT_Update) != RESET)
value = 0xffffffff;
return value;
}

View File

@ -112,7 +112,7 @@ DOXYGENDIR = ../Doc/Doxygen
SRC += $(OPSYSTEM)/main.c SRC += $(OPSYSTEM)/main.c
SRC += $(OPSYSTEM)/pios_board.c SRC += $(OPSYSTEM)/pios_board.c
SRC += $(OPSYSTEM)/op_dfu.c SRC += $(OPSYSTEM)/op_dfu.c
SRC += $(OPSYSTEM)/stopwatch.c SRC += $(FLIGHTLIB)/stopwatch.c
## PIOS Hardware (STM32F10x) ## PIOS Hardware (STM32F10x)
@ -121,35 +121,20 @@ SRC += $(PIOSSTM32F10X)/pios_led.c
SRC += $(PIOSSTM32F10X)/pios_delay.c SRC += $(PIOSSTM32F10X)/pios_delay.c
SRC += $(PIOSSTM32F10X)/pios_usart.c SRC += $(PIOSSTM32F10X)/pios_usart.c
SRC += $(PIOSSTM32F10X)/pios_irq.c SRC += $(PIOSSTM32F10X)/pios_irq.c
#SRC += $(PIOSSTM32F10X)/pios_adc.c
#SRC += $(PIOSSTM32F10X)/pios_servo.c
#SRC += $(PIOSSTM32F10X)/pios_i2c.c
#SRC += $(PIOSSTM32F10X)/pios_spi.c
#SRC += $(PIOSSTM32F10X)/pios_ppm.c
#SRC += $(PIOSSTM32F10X)/pios_pwm.c
#SRC += $(PIOSSTM32F10X)/pios_spektrum.c
SRC += $(PIOSSTM32F10X)/pios_debug.c SRC += $(PIOSSTM32F10X)/pios_debug.c
SRC += $(PIOSSTM32F10X)/pios_gpio.c SRC += $(PIOSSTM32F10X)/pios_gpio.c
#SRC += $(PIOSSTM32F10X)/pios_exti.c
#SRC += $(PIOSSTM32F10X)/pios_wdg.c
# PIOS USB related files (seperated to make code maintenance more easy) # PIOS USB related files (seperated to make code maintenance more easy)
SRC += $(PIOSSTM32F10X)/pios_usb_hid.c SRC += $(PIOSSTM32F10X)/pios_usb_hid.c
SRC += $(PIOSSTM32F10X)/pios_usb_hid_desc.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_desc.c
#SRC += $(PIOSSTM32F10X)/pios_usb_hid_endp.c
SRC += $(PIOSSTM32F10X)/pios_usb_hid_istr.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_istr.c
SRC += $(PIOSSTM32F10X)/pios_usb_hid_prop.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_prop.c
SRC += $(PIOSSTM32F10X)/pios_usb_hid_pwr.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_pwr.c
## PIOS Hardware (Common) ## PIOS Hardware (Common)
#SRC += $(PIOSCOMMON)/pios_sdcard.c
SRC += $(PIOSCOMMON)/pios_com.c SRC += $(PIOSCOMMON)/pios_com.c
#SRC += $(PIOSCOMMON)/pios_bmp085.c
#SRC += $(PIOSCOMMON)/pios_opahrs_v0.c
SRC += $(PIOSCOMMON)/pios_bl_helper.c SRC += $(PIOSCOMMON)/pios_bl_helper.c
SRC += $(PIOSCOMMON)/pios_iap.c SRC += $(PIOSCOMMON)/pios_iap.c
#SRC += $(PIOSCOMMON)/pios_opahrs_proto.c
SRC += $(PIOSCOMMON)/printf-stdarg.c SRC += $(PIOSCOMMON)/printf-stdarg.c
## Libraries for flight calculations ## Libraries for flight calculations
@ -160,22 +145,18 @@ SRC += $(CMSISDIR)/core_cm3.c
SRC += $(CMSISDIR)/system_stm32f10x.c SRC += $(CMSISDIR)/system_stm32f10x.c
## Used parts of the STM-Library ## Used parts of the STM-Library
#SRC += $(STMSPDSRCDIR)/stm32f10x_adc.c
SRC += $(STMSPDSRCDIR)/stm32f10x_bkp.c SRC += $(STMSPDSRCDIR)/stm32f10x_bkp.c
SRC += $(STMSPDSRCDIR)/stm32f10x_crc.c SRC += $(STMSPDSRCDIR)/stm32f10x_crc.c
#SRC += $(STMSPDSRCDIR)/stm32f10x_dac.c
SRC += $(STMSPDSRCDIR)/stm32f10x_dma.c SRC += $(STMSPDSRCDIR)/stm32f10x_dma.c
SRC += $(STMSPDSRCDIR)/stm32f10x_exti.c SRC += $(STMSPDSRCDIR)/stm32f10x_exti.c
SRC += $(STMSPDSRCDIR)/stm32f10x_flash.c SRC += $(STMSPDSRCDIR)/stm32f10x_flash.c
SRC += $(STMSPDSRCDIR)/stm32f10x_gpio.c SRC += $(STMSPDSRCDIR)/stm32f10x_gpio.c
#SRC += $(STMSPDSRCDIR)/stm32f10x_i2c.c
SRC += $(STMSPDSRCDIR)/stm32f10x_pwr.c SRC += $(STMSPDSRCDIR)/stm32f10x_pwr.c
SRC += $(STMSPDSRCDIR)/stm32f10x_rcc.c SRC += $(STMSPDSRCDIR)/stm32f10x_rcc.c
SRC += $(STMSPDSRCDIR)/stm32f10x_rtc.c SRC += $(STMSPDSRCDIR)/stm32f10x_rtc.c
SRC += $(STMSPDSRCDIR)/stm32f10x_spi.c SRC += $(STMSPDSRCDIR)/stm32f10x_spi.c
SRC += $(STMSPDSRCDIR)/stm32f10x_tim.c SRC += $(STMSPDSRCDIR)/stm32f10x_tim.c
SRC += $(STMSPDSRCDIR)/stm32f10x_usart.c SRC += $(STMSPDSRCDIR)/stm32f10x_usart.c
#SRC += $(STMSPDSRCDIR)/stm32f10x_iwdg.c
SRC += $(STMSPDSRCDIR)/stm32f10x_dbgmcu.c SRC += $(STMSPDSRCDIR)/stm32f10x_dbgmcu.c
SRC += $(STMSPDSRCDIR)/misc.c SRC += $(STMSPDSRCDIR)/misc.c

View File

@ -51,7 +51,8 @@ typedef enum {
uploadingStarting, //9 uploadingStarting, //9
outsideDevCapabilities, //10 outsideDevCapabilities, //10
CRC_Fail,//11 CRC_Fail,//11
failed_jump,//12 failed_jump,
//12
} DFUStates; } DFUStates;
/**************************************************/ /**************************************************/
/* OP_DFU commands */ /* OP_DFU commands */
@ -74,9 +75,8 @@ typedef enum {
} DFUCommands; } DFUCommands;
typedef enum { typedef enum {
High_Density, High_Density, Medium_Density
Medium_Density } DeviceType;
}DeviceType;
/**************************************************/ /**************************************************/
/* OP_DFU transfer types */ /* OP_DFU transfer types */
/**************************************************/ /**************************************************/

View File

@ -1,4 +1,4 @@
/** /**
****************************************************************************** ******************************************************************************
* *
* @file op_dfu.h * @file op_dfu.h
@ -29,8 +29,7 @@
#include "common.h" #include "common.h"
/* Includes ------------------------------------------------------------------*/ /* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/ /* Exported types ------------------------------------------------------------*/
typedef struct typedef struct {
{
uint8_t programmingType; uint8_t programmingType;
uint8_t readWriteFlags; uint8_t readWriteFlags;
uint32_t startOfUserCode; uint32_t startOfUserCode;
@ -40,7 +39,7 @@ typedef struct
uint16_t devID; uint16_t devID;
DeviceType devType; DeviceType devType;
uint32_t FW_Crc; uint32_t FW_Crc;
}Device; } Device;
/* Exported constants --------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/

View File

@ -27,7 +27,6 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#ifndef PIOS_CONFIG_H #ifndef PIOS_CONFIG_H
#define PIOS_CONFIG_H #define PIOS_CONFIG_H
#define PIOS_INCLUDE_BL_HELPER #define PIOS_INCLUDE_BL_HELPER

View File

@ -69,8 +69,8 @@
#define PIOS_USB_ENDP1_RXADDR 0x100 #define PIOS_USB_ENDP1_RXADDR 0x100
/* Global Variables */ /* Global Variables */
extern void (*pEpInt_IN[7]) (void); extern void (*pEpInt_IN[7])(void);
extern void (*pEpInt_OUT[7]) (void); extern void (*pEpInt_OUT[7])(void);
/* Public Functions */ /* Public Functions */
extern int32_t PIOS_USB_Init(uint32_t mode); extern int32_t PIOS_USB_Init(uint32_t mode);

View File

@ -1,55 +0,0 @@
/**
******************************************************************************
* @addtogroup OpenPilotBL OpenPilot BootLoader
* @{
*
* @file stopwatch.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief Timer functions for the LED PWM.
* @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
*/
#ifndef _STOPWATCH_H
#define _STOPWATCH_H
/////////////////////////////////////////////////////////////////////////////
// Global definitions
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Global Types
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Prototypes
/////////////////////////////////////////////////////////////////////////////
extern s32 STOPWATCH_Init(u32 resolution, TIM_TypeDef* TIM);
extern s32 STOPWATCH_Reset(TIM_TypeDef* TIM);
extern u32 STOPWATCH_ValueGet(TIM_TypeDef* TIM);
/////////////////////////////////////////////////////////////////////////////
// Export global variables
/////////////////////////////////////////////////////////////////////////////
#endif /* _STOPWATCH_H */

View File

@ -53,12 +53,11 @@ uint32_t sweep_steps2 = 100; // * 5 mS -> 500 mS
//////////////////////////////////////// ////////////////////////////////////////
uint8_t tempcount=0; uint8_t tempcount = 0;
/* Extern variables ----------------------------------------------------------*/ /* Extern variables ----------------------------------------------------------*/
DFUStates DeviceState; DFUStates DeviceState;
int16_t status=0; int16_t status = 0;
uint8_t JumpToApp = FALSE; uint8_t JumpToApp = FALSE;
uint8_t GO_dfu = FALSE; uint8_t GO_dfu = FALSE;
uint8_t USB_connected = FALSE; uint8_t USB_connected = FALSE;
@ -94,11 +93,11 @@ int main() {
if (GO_dfu == TRUE) { if (GO_dfu == TRUE) {
PIOS_Board_Init(); PIOS_Board_Init();
if(User_DFU_request == TRUE) if (User_DFU_request == TRUE)
DeviceState = DFUidle; DeviceState = DFUidle;
else else
DeviceState = BLidle; DeviceState = BLidle;
STOPWATCH_Init(100,LED_PWM_TIMER); STOPWATCH_Init(100, LED_PWM_TIMER);
} else } else
JumpToApp = TRUE; JumpToApp = TRUE;
@ -161,7 +160,8 @@ int main() {
if (STOPWATCH_ValueGet(LED_PWM_TIMER) > 100 * 50 * 100) if (STOPWATCH_ValueGet(LED_PWM_TIMER) > 100 * 50 * 100)
STOPWATCH_Reset(LED_PWM_TIMER); STOPWATCH_Reset(LED_PWM_TIMER);
if ((STOPWATCH_ValueGet(LED_PWM_TIMER) > 60000) && (DeviceState == BLidle)) if ((STOPWATCH_ValueGet(LED_PWM_TIMER) > 60000) && (DeviceState
== BLidle))
JumpToApp = TRUE; JumpToApp = TRUE;
processRX(); processRX();

View File

@ -74,7 +74,7 @@ extern DFUStates DeviceState;
extern uint8_t JumpToApp; extern uint8_t JumpToApp;
/* Private function prototypes -----------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/ /* Private functions ---------------------------------------------------------*/
void sendData(uint8_t * buf,uint16_t size); void sendData(uint8_t * buf, uint16_t size);
uint32_t CalcFirmCRC(void); uint32_t CalcFirmCRC(void);
void DataDownload(DownloadAction action) { void DataDownload(DownloadAction action) {
@ -97,8 +97,8 @@ void DataDownload(DownloadAction action) {
for (uint8_t x = 0; x < packetSize; ++x) { for (uint8_t x = 0; x < packetSize; ++x) {
partoffset = (downPacketCurrent * 14 * 4) + (x * 4); partoffset = (downPacketCurrent * 14 * 4) + (x * 4);
offset = baseOfAdressType(downType) + partoffset; offset = baseOfAdressType(downType) + partoffset;
if(!flash_read(SendBuffer+(6+x*4),offset,currentProgrammingDestination)) if (!flash_read(SendBuffer + (6 + x * 4), offset,
{ currentProgrammingDestination)) {
DeviceState = Last_operation_failed; DeviceState = Last_operation_failed;
} }
/* /*
@ -122,7 +122,7 @@ void DataDownload(DownloadAction action) {
SendBuffer[9 + (x * 4)] = *PIOS_BL_HELPER_FLASH_If_Read(offset + 3); SendBuffer[9 + (x * 4)] = *PIOS_BL_HELPER_FLASH_If_Read(offset + 3);
break; break;
} }
*/ */
} }
//PIOS USB_SIL_Write(EP1_IN, (uint8_t*) SendBuffer, 64); //PIOS USB_SIL_Write(EP1_IN, (uint8_t*) SendBuffer, 64);
downPacketCurrent = downPacketCurrent + 1; downPacketCurrent = downPacketCurrent + 1;
@ -131,14 +131,14 @@ void DataDownload(DownloadAction action) {
DeviceState = Last_operation_Success; DeviceState = Last_operation_Success;
Aditionals = (uint32_t) Download; Aditionals = (uint32_t) Download;
} }
sendData(SendBuffer+1,63); sendData(SendBuffer + 1, 63);
} }
} }
void processComand(uint8_t *xReceive_Buffer) { void processComand(uint8_t *xReceive_Buffer) {
Command = xReceive_Buffer[COMMAND]; Command = xReceive_Buffer[COMMAND];
#ifdef DEBUG_SSP #ifdef DEBUG_SSP
char str[63]={0}; char str[63]= {0};
sprintf(str,"Received COMMAND:%d|",Command); sprintf(str,"Received COMMAND:%d|",Command);
PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); PIOS_COM_SendString(PIOS_COM_TELEM_USB,str);
#endif #endif
@ -317,7 +317,7 @@ void processComand(uint8_t *xReceive_Buffer) {
Buffer[11] = devicesTable[Data0 - 1].FW_Crc >> 16; Buffer[11] = devicesTable[Data0 - 1].FW_Crc >> 16;
Buffer[12] = devicesTable[Data0 - 1].FW_Crc >> 8; Buffer[12] = devicesTable[Data0 - 1].FW_Crc >> 8;
Buffer[13] = devicesTable[Data0 - 1].FW_Crc; Buffer[13] = devicesTable[Data0 - 1].FW_Crc;
Buffer[14] = devicesTable[Data0 - 1].devID>>8; Buffer[14] = devicesTable[Data0 - 1].devID >> 8;
Buffer[15] = devicesTable[Data0 - 1].devID; Buffer[15] = devicesTable[Data0 - 1].devID;
} }
sendData(Buffer + 1, 63); sendData(Buffer + 1, 63);
@ -470,10 +470,9 @@ uint32_t CalcFirmCRC() {
} }
} }
void sendData(uint8_t * buf,uint16_t size) void sendData(uint8_t * buf, uint16_t size) {
{
PIOS_COM_SendBuffer(PIOS_COM_TELEM_USB, buf, size); PIOS_COM_SendBuffer(PIOS_COM_TELEM_USB, buf, size);
if(DeviceState == downloading) if (DeviceState == downloading)
PIOS_DELAY_WaitmS(10); PIOS_DELAY_WaitmS(10);
} }

View File

@ -1,101 +0,0 @@
/**
******************************************************************************
* @addtogroup OpenPilotBL OpenPilot BootLoader
* @{
*
* @file stopwatch.c
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief Timer functions for the LED PWM.
* @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 files
/////////////////////////////////////////////////////////////////////////////
#include "stm32f10x_tim.h"
/////////////////////////////////////////////////////////////////////////////
// Local definitions
/////////////////////////////////////////////////////////////////////////////
//#define STOPWATCH_TIMER_BASE TIM6
//#define STOPWATCH_TIMER_RCC RCC_APB1Periph_TIM6
uint32_t STOPWATCH_Init(u32 resolution, TIM_TypeDef* TIM) {
uint32_t STOPWATCH_TIMER_RCC;
switch ((uint32_t)TIM) {
case (uint32_t)TIM1: STOPWATCH_TIMER_RCC = RCC_APB2Periph_TIM1;break;
case (uint32_t)TIM2: STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM2; break;
case (uint32_t)TIM3: STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM3; break;
case (uint32_t)TIM4: STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM4; break;
default:
STOPWATCH_TIMER_RCC = RCC_APB2Periph_TIM1;
}
// enable timer clock
if (STOPWATCH_TIMER_RCC == RCC_APB2Periph_TIM1 || STOPWATCH_TIMER_RCC
== RCC_APB2Periph_TIM8)
RCC_APB2PeriphClockCmd(STOPWATCH_TIMER_RCC, ENABLE);
else
RCC_APB1PeriphClockCmd(STOPWATCH_TIMER_RCC, ENABLE);
// time base configuration
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 0xffff; // max period
TIM_TimeBaseStructure.TIM_Prescaler = (72 * resolution) - 1; // <resolution> uS accuracy @ 72 MHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM, &TIM_TimeBaseStructure);
// enable interrupt request
TIM_ITConfig(TIM, TIM_IT_Update, ENABLE);
// start counter
TIM_Cmd(TIM, ENABLE);
return 0; // no error
}
/////////////////////////////////////////////////////////////////////////////
//! Resets the stopwatch
//! \return < 0 on errors
/////////////////////////////////////////////////////////////////////////////
uint32_t STOPWATCH_Reset(TIM_TypeDef* TIM) {
// reset counter
TIM->CNT = 1; // set to 1 instead of 0 to avoid new IRQ request
TIM_ClearITPendingBit(TIM, TIM_IT_Update);
return 0; // no error
}
/////////////////////////////////////////////////////////////////////////////
//! Returns current value of stopwatch
//! \return 1..65535: valid stopwatch value
//! \return 0xffffffff: counter overrun
/////////////////////////////////////////////////////////////////////////////
uint32_t STOPWATCH_ValueGet(TIM_TypeDef* TIM) {
uint32_t value = TIM->CNT;
if (TIM_GetITStatus(TIM, TIM_IT_Update) != RESET)
value = 0xffffffff;
return value;
}

View File

@ -37,11 +37,19 @@
uint32_t STOPWATCH_Init(u32 resolution, TIM_TypeDef* TIM) { uint32_t STOPWATCH_Init(u32 resolution, TIM_TypeDef* TIM) {
uint32_t STOPWATCH_TIMER_RCC; uint32_t STOPWATCH_TIMER_RCC;
switch ((uint32_t)TIM) { switch ((uint32_t) TIM) {
case (uint32_t)TIM1: STOPWATCH_TIMER_RCC = RCC_APB2Periph_TIM1;break; case (uint32_t)TIM1:
case (uint32_t)TIM2: STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM2; break; STOPWATCH_TIMER_RCC = RCC_APB2Periph_TIM1;
case (uint32_t)TIM3: STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM3; break; break;
case (uint32_t)TIM4: STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM4; break; case (uint32_t)TIM2:
STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM2;
break;
case (uint32_t)TIM3:
STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM3;
break;
case (uint32_t)TIM4:
STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM4;
break;
default: default:
STOPWATCH_TIMER_RCC = RCC_APB2Periph_TIM1; STOPWATCH_TIMER_RCC = RCC_APB2Periph_TIM1;
} }