[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[paparazzi-commits] [5304] spi link now has crc by default.
From: |
antoine drouin |
Subject: |
[paparazzi-commits] [5304] spi link now has crc by default. |
Date: |
Wed, 11 Aug 2010 01:05:53 +0000 |
Revision: 5304
http://svn.sv.gnu.org/viewvc/?view=rev&root=paparazzi&revision=5304
Author: poine
Date: 2010-08-11 01:05:52 +0000 (Wed, 11 Aug 2010)
Log Message:
-----------
spi link now has crc by default. moved crc computation to fms_spi_link.c
Modified Paths:
--------------
paparazzi3/trunk/sw/airborne/fms/fms_spi_link.c
paparazzi3/trunk/sw/airborne/fms/fms_spi_link.h
Removed Paths:
-------------
paparazzi3/trunk/sw/airborne/fms/fms_crc.c
paparazzi3/trunk/sw/airborne/fms/fms_crc.h
paparazzi3/trunk/sw/airborne/fms/fms_spi_link_crc.c
paparazzi3/trunk/sw/airborne/fms/fms_spi_link_crc.h
paparazzi3/trunk/sw/airborne/fms/overo_test_spi_link_crc.c
Deleted: paparazzi3/trunk/sw/airborne/fms/fms_crc.c
===================================================================
--- paparazzi3/trunk/sw/airborne/fms/fms_crc.c 2010-08-10 22:48:21 UTC (rev
5303)
+++ paparazzi3/trunk/sw/airborne/fms/fms_crc.c 2010-08-11 01:05:52 UTC (rev
5304)
@@ -1,74 +0,0 @@
-
-#include "fms/fms_crc.h"
-
-crc_t crc__table[256];
-
-void crc__init(uint32_t polynomial) {
- crc_t crc_remainder;
- uint32_t crc_dividend;
- crc_t top_bit = (1 << (CRC__WIDTH - 1));
- uint8_t bit;
- for(crc_dividend = 0; crc_dividend < 256; crc_dividend++) {
- crc_remainder = crc_dividend << (CRC__WIDTH - 8);
- for(bit = 8; bit > 0; bit--) {
- if(crc_remainder & top_bit) {
- crc_remainder = (crc_remainder << 1) ^ polynomial;
- }
- else {
- crc_remainder = (crc_remainder << 1);
- }
- }
- crc__table[crc_dividend] = crc_remainder;
- }
-
-#if 0
- int i=0;
- while (i<256) {
- printf("%03d ",crc__table[i]);
- if ((i%8)==7) printf("\n");
- i++;
- }
-#endif
-
-}
-
-#define POLYNOMIAL 0x31
-#define WIDTH (8 * sizeof(uint8_t))
-#define TOPBIT (1 << (WIDTH - 1))
-uint8_t crc__calc_block_crc8(const uint8_t buf[], uint32_t len) {
- crc_t _remainder = 0;
- for (int byte = 0; byte < len; ++byte) {
- _remainder ^= (buf[byte] << (WIDTH - 8));
- for (uint8_t bit = 8; bit > 0; --bit) {
- if (_remainder & TOPBIT)
- _remainder = (_remainder << 1) ^ POLYNOMIAL;
- else
- _remainder = (_remainder << 1);
- }
- }
- return (_remainder);
-}
-
-#if 0
-uint8_t crc__calc_block_crc8(const uint8_t buffer[], uint32_t buffer_length) {
- int counter;
- uint16_t crc = 0;
- for(counter = 0; counter < buffer_length; counter++) {
- crc = crc ^ crc__table[ ( crc ^ *(char *)(buffer)++ ) & 0x00FF ];
- }
- return crc;
-}
-#endif
-
-
-uint16_t crc__calc_block_crc16(const void * buffer, uint32_t buffer_length) {
- int counter;
- crc_t crc = 0;
- for(counter = 0; counter < buffer_length; counter++) {
- crc = (crc << 8) ^ crc__table[ ( (crc >> 8) ^ *(char *)(buffer)++ ) &
0x00FF ];
- }
- return crc;
-}
-
-
-
Deleted: paparazzi3/trunk/sw/airborne/fms/fms_crc.h
===================================================================
--- paparazzi3/trunk/sw/airborne/fms/fms_crc.h 2010-08-10 22:48:21 UTC (rev
5303)
+++ paparazzi3/trunk/sw/airborne/fms/fms_crc.h 2010-08-11 01:05:52 UTC (rev
5304)
@@ -1,21 +0,0 @@
-#ifndef __CRC_H__
-#define __CRC_H__
-
-#include "std.h"
-
-typedef uint8_t crc_t;
-
-#define CRC__WIDTH (8 * sizeof(crc_t))
-
-void crc__init(uint32_t polynomial);
-
-extern uint8_t crc__calc_block_crc8(const uint8_t buffer[], uint32_t
buffer_length);
-uint16_t crc__calc_block_crc16(const void * buffer,
- uint32_t buffer_length);
-
-#define crc8__check(buffer, length, crc) \
- (crc == crc__calc_block_crc8((buffer), (length)));
-#define crc16__check(buffer, length, crc) \
- (crc == crc__calc_block_crc16((buffer), (length)));
-
-#endif /* __CRC_H__ */
Modified: paparazzi3/trunk/sw/airborne/fms/fms_spi_link.c
===================================================================
--- paparazzi3/trunk/sw/airborne/fms/fms_spi_link.c 2010-08-10 22:48:21 UTC
(rev 5303)
+++ paparazzi3/trunk/sw/airborne/fms/fms_spi_link.c 2010-08-11 01:05:52 UTC
(rev 5304)
@@ -1,5 +1,6 @@
#include "fms_spi_link.h"
+#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@@ -8,17 +9,18 @@
#include <linux/types.h>
#include <linux/spi/spidev.h>
+
int spi_link_init(void) {
spi_link.device = "/dev/spidev1.1";
- // spi_link.mode = 0;
- // spi_link.mode = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST;
spi_link.mode = SPI_CPHA;
- // spi_link.mode = SPI_LSB_FIRST;
spi_link.bits = 8;
- spi_link.speed = 3000000;
+ spi_link.speed = 12000000;
spi_link.delay = 1;
+ spi_link.msg_cnt = 0;
+ spi_link.crc_err_cnt = 0;
+
spi_link.fd = open(spi_link.device, O_RDWR);
if (spi_link.fd < 0)
return -1;
@@ -39,20 +41,95 @@
return 0;
}
-int spi_link_send(const void *buf_out, size_t count, void *buf_in) {
+int spi_link_send(void *buf_out, size_t count, void *buf_in, uint8_t*
crc_valid) {
int ret;
struct spi_ioc_transfer tr = {
- .tx_buf = (unsigned long)buf_out,
- .rx_buf = (unsigned long)buf_in,
- .len = count,
- .delay_usecs = spi_link.delay,
- .speed_hz = spi_link.speed,
+ .tx_buf = (unsigned long)buf_out,
+ .rx_buf = (unsigned long)buf_in,
+ .len = count,
+ .delay_usecs = spi_link.delay,
+ .speed_hz = spi_link.speed,
.bits_per_word = spi_link.bits,
};
+
+ ((uint8_t*)buf_out)[count-1] = crc_calc_block_crc8(buf_out, count-1);
ret = ioctl(spi_link.fd, SPI_IOC_MESSAGE(1), &tr);
+ spi_link.msg_cnt++;
+ uint8_t computed_crc = crc_calc_block_crc8(buf_in, count-1);
+ if (computed_crc == ((uint8_t*)buf_in)[count-1])
+ *crc_valid = 1;
+ else {
+ *crc_valid = 0;
+ spi_link.crc_err_cnt++;
+ }
+
return ret;
}
+
+
+#define POLYNOMIAL 0x31
+#define WIDTH (8 * sizeof(uint8_t))
+#define TOPBIT (1 << (WIDTH - 1))
+uint8_t crc_calc_block_crc8(const uint8_t buf[], uint32_t len) {
+ uint8_t _remainder = 0;
+ for (int byte = 0; byte < len; ++byte) {
+ _remainder ^= (buf[byte] << (WIDTH - 8));
+ for (uint8_t bit = 8; bit > 0; --bit) {
+ if (_remainder & TOPBIT)
+ _remainder = (_remainder << 1) ^ POLYNOMIAL;
+ else
+ _remainder = (_remainder << 1);
+ }
+ }
+ return (_remainder);
+}
+
+
+#if 0
+/* for reference: need to write a more efficient crc computation */
+crc_t crc__table[256];
+
+void crc__init(uint32_t polynomial) {
+ crc_t crc_remainder;
+ uint32_t crc_dividend;
+ crc_t top_bit = (1 << (CRC__WIDTH - 1));
+ uint8_t bit;
+ for(crc_dividend = 0; crc_dividend < 256; crc_dividend++) {
+ crc_remainder = crc_dividend << (CRC__WIDTH - 8);
+ for(bit = 8; bit > 0; bit--) {
+ if(crc_remainder & top_bit) {
+ crc_remainder = (crc_remainder << 1) ^ polynomial;
+ }
+ else {
+ crc_remainder = (crc_remainder << 1);
+ }
+ }
+ crc__table[crc_dividend] = crc_remainder;
+ }
+
+#if 0
+ int i=0;
+ while (i<256) {
+ printf("%03d ",crc__table[i]);
+ if ((i%8)==7) printf("\n");
+ i++;
+ }
+#endif
+
+}
+
+uint8_t crc__calc_block_crc8(const uint8_t buffer[], uint32_t buffer_length) {
+ int counter;
+ uint16_t crc = 0;
+ for(counter = 0; counter < buffer_length; counter++) {
+ crc = crc ^ crc__table[ ( crc ^ *(char *)(buffer)++ ) & 0x00FF ];
+ }
+ return crc;
+}
+
+
+#endif
Modified: paparazzi3/trunk/sw/airborne/fms/fms_spi_link.h
===================================================================
--- paparazzi3/trunk/sw/airborne/fms/fms_spi_link.h 2010-08-10 22:48:21 UTC
(rev 5303)
+++ paparazzi3/trunk/sw/airborne/fms/fms_spi_link.h 2010-08-11 01:05:52 UTC
(rev 5304)
@@ -11,11 +11,32 @@
uint8_t bits;
uint32_t speed;
uint16_t delay;
+ /* number of message exchanged since initialization */
+ uint32_t msg_cnt;
+ /* number of crc errors on received messages */
+ uint32_t crc_err_cnt;
};
struct SpiLink spi_link;
+/*
+ * initialize peripheral
+ */
extern int spi_link_init(void);
-extern int spi_link_send(const void *buf_out, size_t count, void* buf_in);
+/*
+ * exchange a data buffer
+ * the last byte of buf_out will be overrwiten with a crc
+ * the last byte of buf_in will contain the received crc
+ * count is the size of buf_out and buf_in, that is
+ * the count of data to exchange+1
+ */
+extern int spi_link_send(void *buf_out, size_t count, void* buf_in, uint8_t*
crc_valid);
+
+/*
+ * just for debuging purposes
+ */
+extern uint8_t crc_calc_block_crc8(const uint8_t buf[], uint32_t len);
+
+
#endif /* FMS_SPI_LINK_H */
Deleted: paparazzi3/trunk/sw/airborne/fms/fms_spi_link_crc.c
===================================================================
--- paparazzi3/trunk/sw/airborne/fms/fms_spi_link_crc.c 2010-08-10 22:48:21 UTC
(rev 5303)
+++ paparazzi3/trunk/sw/airborne/fms/fms_spi_link_crc.c 2010-08-11 01:05:52 UTC
(rev 5304)
@@ -1,59 +0,0 @@
-#include "fms_spi_link.h"
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <stdint.h>
-#include <sys/ioctl.h>
-#include <linux/types.h>
-#include <linux/spi/spidev.h>
-
-int spi_link_init(void) {
-
- spi_link.device = "/dev/spidev1.1";
- // spi_link.mode = 0;
- // spi_link.mode = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST;
- spi_link.mode = SPI_CPHA;
- // spi_link.mode = SPI_LSB_FIRST;
- spi_link.bits = 8;
- //spi_link.speed = 3000000;
- spi_link.speed = 18000000;
- spi_link.delay = 1;
-
- spi_link.fd = open(spi_link.device, O_RDWR);
- if (spi_link.fd < 0)
- return -1;
-
- int ret = 0;
- ret = ioctl(spi_link.fd, SPI_IOC_WR_MODE, &spi_link.mode);
- if (ret == -1)
- return -2;
-
- ret = ioctl(spi_link.fd, SPI_IOC_WR_BITS_PER_WORD, &spi_link.bits);
- if (ret == -1)
- return -3;
-
- ret = ioctl(spi_link.fd, SPI_IOC_WR_MAX_SPEED_HZ, &spi_link.speed);
- if (ret == -1)
- return -4;
-
- return 0;
-}
-
-int spi_link_send(const void *buf_out, size_t count, void *buf_in) {
-
- int ret;
-
- struct spi_ioc_transfer tr = {
- .tx_buf = (unsigned long)buf_out,
- .rx_buf = (unsigned long)buf_in,
- .len = count,
- .delay_usecs = spi_link.delay,
- .speed_hz = spi_link.speed,
- .bits_per_word = spi_link.bits,
- };
- ret = ioctl(spi_link.fd, SPI_IOC_MESSAGE(1), &tr);
-
- return ret;
-
-}
Deleted: paparazzi3/trunk/sw/airborne/fms/fms_spi_link_crc.h
===================================================================
--- paparazzi3/trunk/sw/airborne/fms/fms_spi_link_crc.h 2010-08-10 22:48:21 UTC
(rev 5303)
+++ paparazzi3/trunk/sw/airborne/fms/fms_spi_link_crc.h 2010-08-11 01:05:52 UTC
(rev 5304)
@@ -1,21 +0,0 @@
-#ifndef FMS_SPI_LINK_H
-#define FMS_SPI_LINK_H
-
-#include <inttypes.h>
-#include <unistd.h>
-
-struct SpiLink {
- int fd;
- char* device;
- uint8_t mode;
- uint8_t bits;
- uint32_t speed;
- uint16_t delay;
-};
-
-struct SpiLink spi_link;
-
-extern int spi_link_init(void);
-extern int spi_link_send(const void *buf_out, size_t count, void* buf_in);
-
-#endif /* FMS_SPI_LINK_H */
Deleted: paparazzi3/trunk/sw/airborne/fms/overo_test_spi_link_crc.c
===================================================================
--- paparazzi3/trunk/sw/airborne/fms/overo_test_spi_link_crc.c 2010-08-10
22:48:21 UTC (rev 5303)
+++ paparazzi3/trunk/sw/airborne/fms/overo_test_spi_link_crc.c 2010-08-11
01:05:52 UTC (rev 5304)
@@ -1,166 +0,0 @@
-/*
- * $Id$
- *
- * Copyright (C) 2008-2009 Antoine Drouin <address@hidden>
- *
- * This file is part of paparazzi.
- *
- * paparazzi 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 2, or (at your option)
- * any later version.
- *
- * paparazzi 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 paparazzi; see the file COPYING. If not, write to
- * the Free Software Foundation, 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- */
-
-#include <stdint.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-
-#include "fms/fms_debug.h"
-#include "fms/fms_spi_link.h"
-#include "fms/fms_autopilot_msg.h"
-
-#include "fms/fms_crc.h"
-
-static void fill_msg(struct AutopilotMessageCRCFrame * msg);
-static void print_up_msg(struct AutopilotMessageCRCFrame * msg);
-static void print_down_msg(struct AutopilotMessageCRCFrame * msg);
-
-static uint32_t passed_cnt = 0;
-static uint32_t crc_errors = 0;
-
-int main(int argc, char *argv[]) {
-
- unsigned char skip_test;
- uint32_t us_delay;
-
- if(argc > 1) {
- us_delay = atoi(argv[1]);
- }
- else {
- us_delay = 1953;
- }
-
- printf("Delay: %dus\n", us_delay);
-
- // crc__init(0x31);
- crc__init(0x07);
- printf("CRC initialized\n");
-
- if (spi_link_init()) {
- TRACE(TRACE_ERROR, "%s", "failed to open SPI link \n");
- return -1;
- }
-
- skip_test = 1;
- while (1) {
- struct AutopilotMessageCRCFrame crc_msg_out;
- struct AutopilotMessageCRCFrame msg_out_prev;
- struct AutopilotMessageCRCFrame crc_msg_in;
- crc_t crc_in;
-
- memcpy(&msg_out_prev, &crc_msg_out, sizeof(struct
AutopilotMessageCRCFrame));
-
- fill_msg(&crc_msg_out);
-
- crc_msg_out.crc = crc__calc_block_crc8(&crc_msg_out, sizeof(struct
OVERO_LINK_MSG_DOWN));
- spi_link_send(&crc_msg_out, sizeof(struct AutopilotMessageCRCFrame),
&crc_msg_in);
- crc_in = crc__calc_block_crc8(&crc_msg_in, sizeof(struct
OVERO_LINK_MSG_DOWN));
-
- if(crc_msg_in.crc != crc_in) {
- crc_errors++;
- printf("CRC checksum failed: received %04X != computed %04X (out:
%04X)\n",
- crc_msg_in.crc, crc_in, crc_msg_out.crc);
- }
-
- if (!skip_test && memcmp(&crc_msg_in.payload, &msg_out_prev.payload,
sizeof(struct OVERO_LINK_MSG_DOWN))) {
- printf("Compare failed: (received != expected): \n");
- print_up_msg(&crc_msg_in);
- print_down_msg(&msg_out_prev);
-// printf("! expected %d %d %d %d\n", msg_out_prev.payload.msg_down.foo,
msg_out_prev.payload.msg_down.bar, msg_out_prev.payload.msg_down.bla,
msg_out_prev.payload.msg_down.ble);
-// printf("! got %d %d %d %d\n\n", crc_msg_in.payload.msg_up.foo,
crc_msg_in.payload.msg_up.bar, crc_msg_in.payload.msg_up.bla,
crc_msg_in.payload.msg_up.ble);
- }
- else {
- if(crc_msg_in.crc != crc_in) {
- printf("False CRC error\n");
- }
- passed_cnt++;
- skip_test = 0;
- if (!(passed_cnt % 1000)) {
- printf("passed %d, CRC errors: %d\n", passed_cnt, crc_errors);
- }
- }
- if(us_delay > 0) {
- usleep(us_delay);
- }
- }
-
- return 0;
-}
-
-
-static void print_up_msg(struct AutopilotMessageCRCFrame * msg) {
- printf("UP: %08X %08X %08X %08X %08X %08X %08X %08X CRC: %08X\n",
msg->payload.msg_up.foo,
- msg->payload.msg_up.bar,
- msg->payload.msg_up.bla,
- msg->payload.msg_up.ble,
- msg->payload.msg_up.bli,
- msg->payload.msg_up.blo,
- msg->payload.msg_up.blu,
- msg->payload.msg_up.bly,
- msg->crc);
-}
-static void print_down_msg(struct AutopilotMessageCRCFrame * msg) {
- printf("DW: %08X %08X %08X %08X %08X %08X %08X %08X CRC: %08X\n",
msg->payload.msg_down.foo,
- msg->payload.msg_down.bar,
- msg->payload.msg_down.bla,
- msg->payload.msg_down.ble,
- msg->payload.msg_down.bli,
- msg->payload.msg_down.blo,
- msg->payload.msg_up.blu,
- msg->payload.msg_up.bly,
- msg->crc);
-}
-
-
-#if 1
-static void fill_msg(struct AutopilotMessageCRCFrame * msg) {
- static uint32_t foo = 5000;
- msg->payload.msg_up.foo = 0x55;
- msg->payload.msg_up.bar = 1;
- msg->payload.msg_up.bla = 0xff;
- msg->payload.msg_up.ble = foo % 255;
- msg->payload.msg_up.bli = 1;
- msg->payload.msg_up.blo = 0xff;
- msg->payload.msg_up.blu = 0;
- msg->payload.msg_up.bly = 0;
-
- foo--;
- if(foo == 0) {
- foo = 5000;
- }
-}
-#else
-static void fill_msg(struct AutopilotMessageCRCFrame * msg) {
- msg->payload.msg_up.foo = 0;
- msg->payload.msg_up.bar = 0;
- msg->payload.msg_up.bla = 0;
- msg->payload.msg_up.ble = 0;
- msg->payload.msg_up.bli = 0;
- msg->payload.msg_up.blo = 0;
- msg->payload.msg_up.blu = 0;
- msg->payload.msg_up.bly = 0x01;
-}
-#endif
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [paparazzi-commits] [5304] spi link now has crc by default.,
antoine drouin <=