paparazzi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[paparazzi-commits] [4246] Micromag3 magnetic sensor for fixed wing.


From: Martin Mueller
Subject: [paparazzi-commits] [4246] Micromag3 magnetic sensor for fixed wing.
Date: Mon, 12 Oct 2009 12:00:27 +0000

Revision: 4246
          http://svn.sv.gnu.org/viewvc/?view=rev&root=paparazzi&revision=4246
Author:   mmm
Date:     2009-10-12 12:00:26 +0000 (Mon, 12 Oct 2009)
Log Message:
-----------
Micromag3 magnetic sensor for fixed wing.

Modified Paths:
--------------
    paparazzi3/trunk/conf/airframes/funjetmm.xml

Added Paths:
-----------
    paparazzi3/trunk/sw/airborne/arm7/micromag_fw_hw.c
    paparazzi3/trunk/sw/airborne/arm7/micromag_fw_hw.h
    paparazzi3/trunk/sw/airborne/micromag_fw.c
    paparazzi3/trunk/sw/airborne/micromag_fw.h

Modified: paparazzi3/trunk/conf/airframes/funjetmm.xml
===================================================================
--- paparazzi3/trunk/conf/airframes/funjetmm.xml        2009-10-12 09:02:46 UTC 
(rev 4245)
+++ paparazzi3/trunk/conf/airframes/funjetmm.xml        2009-10-12 12:00:26 UTC 
(rev 4246)
@@ -257,8 +257,8 @@
 #ap.srcs += baro_scp.c
 #ap.CFLAGS += -DUSE_BARO_SCP
 
-#ap.CFLAGS += -DMICROMAG_DRDY_VIC_SLOT=9 -DSSP_VIC_SLOT=11 -DUSE_MICROMAG
-#ap.srcs += micromag.c $(SRC_ARCH)/micromag_hw.c
+ap.CFLAGS += -DMICROMAG_DRDY_VIC_SLOT=9 -DSSP_VIC_SLOT=11 -DUSE_MICROMAG_FW
+ap.srcs += micromag_fw.c $(SRC_ARCH)/micromag_fw_hw.c
 
 ap.srcs += joystick.c
 ap.CFLAGS += -DUSE_JOYSTICK

Added: paparazzi3/trunk/sw/airborne/arm7/micromag_fw_hw.c
===================================================================
--- paparazzi3/trunk/sw/airborne/arm7/micromag_fw_hw.c                          
(rev 0)
+++ paparazzi3/trunk/sw/airborne/arm7/micromag_fw_hw.c  2009-10-12 12:00:26 UTC 
(rev 4246)
@@ -0,0 +1,52 @@
+/* PNI micromag3 connected on SPI1 */
+/* 
+   Tiny2 (fixed wing)
+   SS    on P0.20 (SSEL)
+   RESET on P0.29 (ADC5)
+   DRDY  on P0.16 ( EINT0 )
+*/
+
+#include "led.h"
+#include "micromag_fw.h"
+
+volatile uint8_t micromag_cur_axe;
+
+static void EXTINT_ISR(void) __attribute__((naked));
+
+void micromag_hw_init( void ) {
+
+  MmUnselect();                   /* pin idles high */
+  /* configure SS pin */
+  SetBit(MM_SS_IODIR, MM_SS_PIN); /* pin is output  */
+
+  /* configure RESET pin */
+  SetBit(MM_RESET_IODIR, MM_RESET_PIN); /* pin is output  */
+  MmReset();                            /* pin idles low  */
+
+  /* configure DRDY pin */
+  /* connected pin to EXINT */ 
+  MM_DRDY_PINSEL |= MM_DRDY_PINSEL_VAL << MM_DRDY_PINSEL_BIT;
+  SetBit(EXTMODE, MM_DRDY_EINT); /* EINT is edge trigered */
+  SetBit(EXTPOLAR,MM_DRDY_EINT); /* EINT is trigered on rising edge */
+  SetBit(EXTINT,MM_DRDY_EINT);   /* clear pending EINT */
+  
+  /* initialize interrupt vector */
+  VICIntSelect &= ~VIC_BIT( MM_DRDY_VIC_IT );                       /* select 
EINT as IRQ source */
+  VICIntEnable = VIC_BIT( MM_DRDY_VIC_IT );                         /* enable 
it                 */
+  _VIC_CNTL(MICROMAG_DRDY_VIC_SLOT) = VIC_ENABLE | MM_DRDY_VIC_IT;
+  _VIC_ADDR(MICROMAG_DRDY_VIC_SLOT) = (uint32_t)EXTINT_ISR;         // address 
of the ISR 
+}
+
+void EXTINT_ISR(void) {
+  ISR_ENTRY();
+//LED_TOGGLE(3); 
+
+  /* no, we won't do anything asynchronously, so just notify */
+  micromag_status = MM_GOT_EOC;
+  /* clear EINT */
+  SetBit(EXTINT,MM_DRDY_EINT);
+//  EXTINT = (1<<MM_DRDY_EINT);
+  VICVectAddr = 0x00000000;    /* clear this interrupt from the VIC */
+  ISR_EXIT();
+}
+

Added: paparazzi3/trunk/sw/airborne/arm7/micromag_fw_hw.h
===================================================================
--- paparazzi3/trunk/sw/airborne/arm7/micromag_fw_hw.h                          
(rev 0)
+++ paparazzi3/trunk/sw/airborne/arm7/micromag_fw_hw.h  2009-10-12 12:00:26 UTC 
(rev 4246)
@@ -0,0 +1,93 @@
+#ifndef MICROMAG_FW_HW_H
+#define MICROMAG_FW_HW_H
+
+#include <stdlib.h>  // for abs
+
+#include "std.h"
+#include "LPC21xx.h"
+#include "interrupt_hw.h" 
+
+#include "spi_hw.h"
+#include BOARD_CONFIG
+
+#include "airframe.h"
+
+#define MM_DIVISOR_128  2
+#define MM_DIVISOR_256  3
+#define MM_DIVISOR_512  4
+#define MM_DIVISOR_1024 5
+
+#define MM_DIVISOR MM_DIVISOR_512
+
+
+extern volatile uint8_t micromag_cur_axe;
+
+#define MmSelect() SetBit(MM_SS_IOCLR,MM_SS_PIN)
+#define MmUnselect() SetBit(MM_SS_IOSET,MM_SS_PIN)
+
+#define MmReset() SetBit(MM_RESET_IOCLR,MM_RESET_PIN)
+#define MmSet() SetBit(MM_RESET_IOSET,MM_RESET_PIN)
+
+#define MmOnSpiIt() {                                                  \
+    switch (micromag_status) {                                         \
+    case MM_SENDING_REQ:                                               \
+      {                                                                        
\
+       /* read dummy control byte reply */                             \
+       uint8_t foo __attribute__ ((unused)) = SSPDR;                   \
+       micromag_status = MM_WAITING_EOC;                               \
+       MmUnselect();                                                   \
+       SpiClearRti();                                                  \
+       SpiDisableRti();                                                \
+       SpiDisable();                                                   \
+      }                                                                        
\
+      break;                                                           \
+    case MM_READING_RES:                                               \
+      {                                                                        
\
+       int16_t new_val;                                                \
+       new_val = SSPDR << 8;                                           \
+       new_val += SSPDR;                                               \
+       if (abs(new_val) < 2000)                                        \
+         micromag_values[micromag_cur_axe] = new_val;                  \
+       MmUnselect();                                                   \
+       SpiClearRti();                                                  \
+       SpiDisableRti();                                                \
+       SpiDisable();                                                   \
+       micromag_cur_axe++;                                             \
+       if (micromag_cur_axe > 2) {                                     \
+         micromag_cur_axe = 0;                                         \
+         micromag_status = MM_DATA_AVAILABLE;                          \
+       }                                                               \
+       else                                                            \
+         micromag_status = MM_IDLE;                                    \
+      }                                                                        
\
+      break;                                                           \
+    }                                                                  \
+  }
+
+
+#define MmSendReq() {                                                  \
+    MmSelect();                                                                
\
+    micromag_status = MM_SENDING_REQ;                                  \
+    MmSet();                                                           \
+    SpiClearRti();                                                     \
+    SpiEnableRti();                                                    \
+    MmReset();                                                         \
+    uint8_t control_byte = (micromag_cur_axe+1) << 0 | MM_DIVISOR_1024 << 4; \
+    SSPDR = control_byte;                                              \
+    SpiEnable();                                                       \
+  }
+
+#define MmReadRes() {                                                  \
+    micromag_status = MM_READING_RES;                                  \
+    MmSelect();                                                                
\
+    /* trigger 2 bytes read */                                         \
+    SSPDR = 0;                                                 \
+    SSPDR = 0;                                                 \
+    SpiEnable();                                                       \
+    SpiClearRti();                                                     \
+    SpiEnableRti();                                                    \
+  }
+
+
+
+#endif /* MICROMAG_HW_H */

Added: paparazzi3/trunk/sw/airborne/micromag_fw.c
===================================================================
--- paparazzi3/trunk/sw/airborne/micromag_fw.c                          (rev 0)
+++ paparazzi3/trunk/sw/airborne/micromag_fw.c  2009-10-12 12:00:26 UTC (rev 
4246)
@@ -0,0 +1,110 @@
+#include "micromag_fw.h"
+#include "led.h"
+
+
+/* SSPCR0 settings */
+#define SSP_DDS  0x07 << 0  /* data size         : 8 bits        */
+#define SSP_FRF  0x00 << 4  /* frame format      : SPI           */
+#define SSP_CPOL 0x00 << 6  /* clock polarity    : data captured on first 
clock transition */  
+#define SSP_CPHA 0x00 << 7  /* clock phase       : SCK idles low */
+#define SSP_SCR  0x0F << 8  /* serial clock rate : divide by 16  */
+
+/* SSPCR1 settings */
+#define SSP_LBM  0x00 << 0  /* loopback mode     : disabled                  */
+#define SSP_SSE  0x00 << 1  /* SSP enable        : disabled                  */
+#define SSP_MS   0x00 << 2  /* master slave mode : master                    */
+#define SSP_SOD  0x00 << 3  /* slave output disable : don't care when master */
+
+#define SSPCR0_VAL (SSP_DDS |  SSP_FRF | SSP_CPOL | SSP_CPHA | SSP_SCR )
+#define SSPCR1_VAL (SSP_LBM |  SSP_SSE | SSP_MS | SSP_SOD )
+
+#define SSP_PINSEL1_SCK  (2<<2)
+#define SSP_PINSEL1_MISO (2<<4)
+#define SSP_PINSEL1_MOSI (2<<6)
+
+#define SSP_Enable()     SetBit(SSPCR1, SSE);
+#define SSP_Disable()    ClearBit(SSPCR1, SSE);
+#define SSP_EnableRxi()  SetBit(SSPIMSC, RXIM)
+#define SSP_DisableRxi() ClearBit(SSPIMSC, RXIM)
+#define SSP_EnableTxi()  SetBit(SSPIMSC, TXIM)
+#define SSP_DisableTxi() ClearBit(SSPIMSC, TXIM)
+#define SSP_EnableRti()  SetBit(SSPIMSC, RTIM);
+#define SSP_DisableRti() ClearBit(SSPIMSC, RTIM);
+#define SSP_ClearRti()   SetBit(SSPICR, RTIC);
+
+volatile uint8_t micromag_status;
+volatile int16_t micromag_values[MM_NB_AXIS];
+
+static void SSP_ISR(void) __attribute__((naked));
+
+void micromag_periodic( void ) {
+
+  static uint8_t cnt = 0;
+
+  if (micromag_status == MM_IDLE) {
+    //    uint8_t * tab = &cnt;
+    //    DOWNLINK_SEND_DEBUG(1,tab);
+    cnt = 0;
+    MmSendReq();
+  }
+  else if (micromag_status ==  MM_GOT_EOC) {
+    MmReadRes();
+  }
+  else if (micromag_status == MM_WAITING_EOC) {
+    cnt++;
+    if (cnt > 50) {cnt = 0; micromag_status = MM_IDLE;}
+  }
+}
+
+static void SSP_ISR(void) {
+ ISR_ENTRY();
+
+ MmOnSpiIt();
+
+ VICVectAddr = 0x00000000; /* clear this interrupt from the VIC */
+ ISR_EXIT();
+}
+
+void micromag_init_ssp(void) {
+
+  /* setup pins for SSP (SCK, MISO, MOSI, SSEL) */
+  PINSEL1 |= SSP_PINSEL1_SCK  | SSP_PINSEL1_MISO | SSP_PINSEL1_MOSI;
+  
+  /* setup SSP */
+  SSPCR0 = SSPCR0_VAL;;
+  SSPCR1 = SSPCR1_VAL;
+  SSPCPSR = 0x02;
+  
+  /* initialize interrupt vector */
+  VICIntSelect &= ~VIC_BIT( VIC_SPI1 );  /* SPI1 selected as IRQ */
+  VICIntEnable = VIC_BIT( VIC_SPI1 );    /* enable it            */
+  _VIC_CNTL(SSP_VIC_SLOT) = VIC_ENABLE | VIC_SPI1;
+  _VIC_ADDR(SSP_VIC_SLOT) = (uint32_t)SSP_ISR;      /* address of the ISR   */
+}
+
+void micromag_init( void ) {
+
+  micromag_hw_init();
+  
+  uint8_t i;
+  for (i=0; i<MM_NB_AXIS; i++)
+    micromag_values[i] = 0;
+  micromag_status = MM_IDLE;
+}
+
+void micromag_reset() {
+  micromag_status = MM_IDLE;
+}
+
+void micromag_read() {
+  if (micromag_status == MM_IDLE) {
+    MmSendReq();
+  }
+  else if (micromag_status ==  MM_GOT_EOC) {
+    MmReadRes();
+  }
+  else if (micromag_status ==  MM_DATA_AVAILABLE) {
+    micromag_status == MM_IDLE;
+  }
+}
+

Added: paparazzi3/trunk/sw/airborne/micromag_fw.h
===================================================================
--- paparazzi3/trunk/sw/airborne/micromag_fw.h                          (rev 0)
+++ paparazzi3/trunk/sw/airborne/micromag_fw.h  2009-10-12 12:00:26 UTC (rev 
4246)
@@ -0,0 +1,53 @@
+#ifndef MICROMAG_FW_H
+#define MICROMAG_FW_H
+
+
+#include "std.h"
+#define MM_NB_AXIS 3
+
+extern void micromag_init_ssp(void);
+extern void micromag_init( void );
+extern void micromag_read( void );
+
+extern void micromag_reset( void);
+extern void micromag_periodic( void );
+
+#define MM_IDLE            0
+#define MM_BUSY            1
+#define MM_SENDING_REQ     2
+#define MM_WAITING_EOC     3
+#define MM_GOT_EOC         4
+#define MM_READING_RES     5
+#define MM_DATA_AVAILABLE  6
+
+/* ssp input clock 468.75kHz, clock that divided by SCR+1 */
+#define SSP_CLOCK 468750
+
+/* SSPCR0 settings */
+#define SSP_DDS  0x07 << 0  /* data size         : 8 bits        */
+#define SSP_FRF  0x00 << 4  /* frame format      : SPI           */
+#define SSP_CPOL 0x00 << 6  /* clock polarity    : data captured on first 
clock transition */  
+#define SSP_CPHA 0x00 << 7  /* clock phase       : SCK idles low */
+#define SSP_SCR  0x0F << 8  /* serial clock rate : divide by 16  */
+
+/* SSPCR1 settings */
+#define SSP_LBM  0x00 << 0  /* loopback mode     : disabled                  */
+#define SSP_SSE  0x00 << 1  /* SSP enable        : disabled                  */
+#define SSP_MS   0x00 << 2  /* master slave mode : master                    */
+#define SSP_SOD  0x00 << 3  /* slave output disable : don't care when master */
+
+#define SS_PIN   20
+#define SS_IODIR IO0DIR
+#define SS_IOSET IO0SET
+#define SS_IOCLR IO0CLR
+
+
+extern volatile uint8_t micromag_status;
+extern volatile int16_t micromag_values[MM_NB_AXIS];
+
+extern void micromag_hw_init( void );
+
+#include "micromag_fw_hw.h"
+
+
+#endif /* MICROMAG_H */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]