Skip to content

Commit

Permalink
[mcu_spi] mcu_spi link for classix using new slave mode
Browse files Browse the repository at this point in the history
It should compile, not sure it actually works...
  • Loading branch information
gautierhattenberger committed Nov 9, 2012
1 parent 3c97058 commit 3dd4353
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 35 deletions.
19 changes: 13 additions & 6 deletions conf/firmwares/subsystems/fixedwing/intermcu_spi.makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

# InterMCU type SPI

ifeq ($(INTER_MCU_SPI),)
INTER_MCU_SPI = SPI1
endif

fbw.CFLAGS += -DMCU_SPI_LINK -DUSE_SPI -DSPI_SLAVE
fbw.srcs += $(SRC_FIXEDWING)/link_mcu_spi.c mcu_periph/spi.c $(SRC_ARCH)/mcu_periph/spi_arch.c
ap_srcs += $(SRC_FIRMWARE)/fbw_downlink.c
ap.CFLAGS += -DMCU_SPI_LINK -DUSE_SPI -DSPI_MASTER -DUSE_SPI_SLAVE0
ap.srcs += $(SRC_FIXEDWING)/link_mcu_spi.c mcu_periph/spi.c $(SRC_ARCH)/mcu_periph/spi_arch.c
SEPARATE_FBW = 1
ifeq ($(INTER_MCU_SLAVE),)
INTER_MCU_SLAVE = SLAVE0
endif

fbw.CFLAGS += -DMCU_SPI_LINK -DUSE_$(INTER_MCU_SPI)_SLAVE -DSPI_SLAVE
fbw.srcs += $(SRC_FIXEDWING)/link_mcu_spi.c mcu_periph/spi.c $(SRC_ARCH)/mcu_periph/spi_arch.c
ap_srcs += $(SRC_FIRMWARE)/fbw_downlink.c
ap.CFLAGS += -DMCU_SPI_LINK -DUSE_$(INTER_MCU_SPI) -DSPI_MASTER -DUSE_SPI_$(INTER_MCU_SLAVE)
ap.srcs += $(SRC_FIXEDWING)/link_mcu_spi.c mcu_periph/spi.c $(SRC_ARCH)/mcu_periph/spi_arch.c
SEPARATE_FBW = 1
4 changes: 2 additions & 2 deletions conf/tests_conf.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
settings=" settings/fixedwing_basic.xml settings/control/ctl_basic.xml settings/estimation/ins_neutrals.xml settings/control/tune_agr_climb.xml settings/modules/dc.xml"
gui_color="blue"
/>
<!--aircraft
<aircraft
name="Classix_XSens"
ac_id="33"
airframe="airframes/CDW/classix.xml"
Expand All @@ -195,7 +195,7 @@
flight_plan="flight_plans/versatile.xml"
settings=" settings/fixedwing_basic.xml settings/control/ctl_basic.xml settings/modules/photogrammetry_calculator.xml settings/modules/dc.xml"
gui_color="blue"
/-->
/>
<aircraft
name="tiny2_chimu_spi"
ac_id="22"
Expand Down
3 changes: 3 additions & 0 deletions sw/airborne/boards/classix_1.0.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@

#define SPI_SELECT_SLAVE0_PORT 0
#define SPI_SELECT_SLAVE0_PIN 20
#define SPI_SELECT_SLAVE0_PINSEL PINSEL1
#define SPI_SELECT_SLAVE0_PINSEL_BIT 8
#define SPI_SELECT_SLAVE0_PINSEL_VAL 0

#ifndef SITL
/* sitl handles this board as a mono-processor one: unfortunately these
Expand Down
3 changes: 3 additions & 0 deletions sw/airborne/boards/tiny_2.0.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,8 @@

#define SPI_SELECT_SLAVE0_PORT 0
#define SPI_SELECT_SLAVE0_PIN 20
#define SPI_SELECT_SLAVE0_PINSEL PINSEL1
#define SPI_SELECT_SLAVE0_PINSEL_BIT 8
#define SPI_SELECT_SLAVE0_PINSEL_VAL 0

#endif /* CONFIG_TINY_H */
3 changes: 3 additions & 0 deletions sw/airborne/boards/tiny_2.1.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@

#define SPI_SELECT_SLAVE0_PORT 0
#define SPI_SELECT_SLAVE0_PIN 20
#define SPI_SELECT_SLAVE0_PINSEL PINSEL1
#define SPI_SELECT_SLAVE0_PINSEL_BIT 8
#define SPI_SELECT_SLAVE0_PINSEL_VAL 0

#define SPI1_DRDY_PINSEL PINSEL1
#define SPI1_DRDY_PINSEL_BIT 0
Expand Down
3 changes: 3 additions & 0 deletions sw/airborne/boards/umarim_1.0.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
/* SPI (SSP) */
#define SPI_SELECT_SLAVE0_PORT 0
#define SPI_SELECT_SLAVE0_PIN 20
#define SPI_SELECT_SLAVE0_PINSEL PINSEL1
#define SPI_SELECT_SLAVE0_PINSEL_BIT 8
#define SPI_SELECT_SLAVE0_PINSEL_VAL 0

#define SPI1_DRDY_PINSEL PINSEL1
#define SPI1_DRDY_PINSEL_BIT 0
Expand Down
3 changes: 3 additions & 0 deletions sw/airborne/boards/umarim_lite_2.0.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
/* SPI (SSP) */
#define SPI_SELECT_SLAVE0_PORT 0
#define SPI_SELECT_SLAVE0_PIN 20
#define SPI_SELECT_SLAVE0_PINSEL PINSEL1
#define SPI_SELECT_SLAVE0_PINSEL_BIT 8
#define SPI_SELECT_SLAVE0_PINSEL_VAL 0

#define SPI1_DRDY_PINSEL PINSEL1
#define SPI1_DRDY_PINSEL_BIT 0
Expand Down
1 change: 1 addition & 0 deletions sw/airborne/firmwares/fixedwing/main_fbw.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ void init_fbw( void ) {
inter_mcu_init();
#endif
#ifdef MCU_SPI_LINK
link_mcu_init();
link_mcu_restart();
#endif

Expand Down
68 changes: 49 additions & 19 deletions sw/airborne/link_mcu_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@
*/

#include "link_mcu_spi.h"
#include "mcu_periph/spi.h"

#ifndef LINK_MCU_SPI_DEV
#define LINK_MCU_SPI_DEV spi1
#endif

struct link_mcu_msg link_mcu_from_ap_msg;
struct link_mcu_msg link_mcu_from_fbw_msg;

struct spi_transaction link_mcu_trans;

bool_t link_mcu_received;

static uint16_t crc = 0;
Expand All @@ -46,20 +51,30 @@ static uint16_t crc = 0;

#ifdef FBW

void link_mcu_init(void) {

link_mcu_trans.cpol = SPICpolIdleLow;
link_mcu_trans.cpha = SPICphaEdge2;
link_mcu_trans.dss = SPIDss8bit;
link_mcu_trans.input_buf = (uint8_t*)&link_mcu_from_ap_msg;
link_mcu_trans.output_buf = (uint8_t*)&link_mcu_from_fbw_msg;
link_mcu_trans.input_length = LINK_MCU_FRAME_LENGTH;
link_mcu_trans.output_length = LINK_MCU_FRAME_LENGTH;
spi_slave_register(&(LINK_MCU_SPI_DEV), &link_mcu_trans);
}

void link_mcu_restart(void) {
ComputeChecksum(link_mcu_from_fbw_msg);
link_mcu_from_fbw_msg.checksum = crc;

spi_buffer_input = (uint8_t*)&link_mcu_from_ap_msg;
spi_buffer_output = (uint8_t*)&link_mcu_from_fbw_msg;
spi_buffer_length = LINK_MCU_FRAME_LENGTH;
SpiStart();
// wait for the next transaction
spi_slave_wait(&(LINK_MCU_SPI_DEV));
}

void link_mcu_event_task( void ) {
if (spi_message_received) {
if (link_mcu_trans.status == SPITransSuccess) {
/* Got a message on SPI. */
spi_message_received = FALSE;
link_mcu_trans.status = SPITransDone;

/* A message has been received */
ComputeChecksum(link_mcu_from_ap_msg);
Expand All @@ -69,6 +84,11 @@ void link_mcu_event_task( void ) {
else
fbw_state->nb_err++;
}
if (link_mcu_trans.status == SPITransFailed) {
link_mcu_trans.status = SPITransDone;
link_mcu_received = TRUE;
fbw_state->nb_err++;
}
}

#endif /* FBW */
Expand All @@ -78,40 +98,50 @@ void link_mcu_event_task( void ) {
/*****************************************************************************/
#ifdef AP

#ifndef LINK_MCU_SLAVE_IDX
#define LINK_MCU_SLAVE_IDX SPI_SLAVE0
#endif

uint8_t link_mcu_nb_err;
uint8_t link_mcu_fbw_nb_err;

void link_mcu_init(void) {
link_mcu_nb_err = 0;

link_mcu_trans.cpol = SPICpolIdleLow;
link_mcu_trans.cpha = SPICphaEdge2;
link_mcu_trans.dss = SPIDss8bit;
link_mcu_trans.select = SPISelectUnselect;
link_mcu_trans.slave_idx = LINK_MCU_SLAVE_IDX;
link_mcu_trans.input_buf = (uint8_t*)&link_mcu_from_fbw_msg;
link_mcu_trans.output_buf = (uint8_t*)&link_mcu_from_ap_msg;
link_mcu_trans.input_length = LINK_MCU_FRAME_LENGTH;
link_mcu_trans.output_length = LINK_MCU_FRAME_LENGTH;
}

void link_mcu_send(void) {

if (!SpiCheckAvailable()) {
SpiOverRun();
return;
}

ComputeChecksum(link_mcu_from_ap_msg);
link_mcu_from_ap_msg.checksum = crc;
spi_buffer_input = (uint8_t*)&link_mcu_from_fbw_msg;
spi_buffer_output = (uint8_t*)&link_mcu_from_ap_msg;
spi_buffer_length = LINK_MCU_FRAME_LENGTH;
SpiSelectSlave0();
SpiStart();
spi_submit(&(LINK_MCU_SPI_DEV), &link_mcu_trans);
}

void link_mcu_event_task( void ) {
if (spi_message_received) {
if (link_mcu_trans.status == SPITransSuccess) {
/* Got a message on SPI. */
spi_message_received = FALSE;
link_mcu_trans.status = SPITransDone;
/* A message has been received */
ComputeChecksum(link_mcu_from_fbw_msg);
if (link_mcu_from_fbw_msg.checksum == crc)
inter_mcu_received_fbw = TRUE;
else
link_mcu_nb_err++;
}
if (link_mcu_trans.status == SPITransFailed) {
link_mcu_trans.status = SPITransDone;
link_mcu_received = TRUE;
link_mcu_nb_err++;
}
}

#endif /* AP */
12 changes: 4 additions & 8 deletions sw/airborne/link_mcu_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <inttypes.h>
#include "inter_mcu.h"
#include "mcu_periph/spi.h"

#ifndef SITL
#include "link_mcu_hw.h"
Expand All @@ -47,26 +48,21 @@ struct link_mcu_msg {
extern struct link_mcu_msg link_mcu_from_ap_msg;
extern struct link_mcu_msg link_mcu_from_fbw_msg;

extern struct spi_transaction link_mcu_trans;

extern bool_t link_mcu_received;

extern void link_mcu_init(void);
extern void link_mcu_event_task( void );

#if defined MCU_SPI_LINK && !USE_SPI
#define USE_SPI 1
#endif


#ifdef FBW
#define SPI_SLAVE 1
extern void link_mcu_restart(void);
#endif /* FBW */

#ifdef AP
#define SPI_MASTER 1
extern uint8_t link_mcu_nb_err;
extern uint8_t link_mcu_fbw_nb_err;

extern void link_mcu_init(void);
extern void link_mcu_send(void);
#endif /* AP */

Expand Down

0 comments on commit 3dd4353

Please sign in to comment.