diff --git a/platform/mbxxx/Makefile.mbxxx b/platform/mbxxx/Makefile.mbxxx new file mode 100644 index 000000000..2403f2d70 --- /dev/null +++ b/platform/mbxxx/Makefile.mbxxx @@ -0,0 +1,30 @@ + + +ARCH= irq.c sensors.c acc-sensor.c button-sensor.c temperature-sensor.c mems.c + +CONTIKI_TARGET_DIRS = . dev +ifndef CONTIKI_TARGET_MAIN +CONTIKI_TARGET_MAIN = contiki-main.c led.c button.c board.c +endif + +ifdef UIP_CONF_IPV6 +CFLAGS += -DWITH_UIP6=1 +endif + +CONTIKI_TARGET_SOURCEFILES += $(ARCH) $(CONTIKI_TARGET_MAIN) + +MCU=STM32W108 +include $(CONTIKI)/cpu/stm32w108/Makefile.stm32w108 + +SERIALDUMP = $(CONTIKI)/tools/stm32w/serialdump-linux + +ifdef OS + ifneq (,$(findstring Windows,$(OS))) + SERIALDUMP = $(CONTIKI)/tools/stm32w/serialdump-windows + endif +endif + + + +login: + $(SERIALDUMP) -b115200 -d10000 $(PORT) diff --git a/platform/mbxxx/apps/shell-sensors.c b/platform/mbxxx/apps/shell-sensors.c new file mode 100644 index 000000000..9ff6b917f --- /dev/null +++ b/platform/mbxxx/apps/shell-sensors.c @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2010, STMicroelectronics. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-sensors.c,v 1.1 2010/10/25 09:03:39 salvopitru Exp $ + */ + + /** + * \file + * Shell function for temp and acc sensors. + * \author + * Salvatore Pitrulli + */ + +#include +#include + +#include "contiki.h" +#include "shell.h" +#include "contiki-net.h" +#include "dev/temperature-sensor.h" +#include "dev/acc-sensor.h" + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_sensors_process, "sensors"); +SHELL_COMMAND(sensors_command, + "sensors", + "sensors {temp|acc}: get sensor value", + &shell_sensors_process); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_sensors_process, ev, data) +{ + + char str_buf[22]; + + PROCESS_BEGIN(); + + if(data == NULL) { + shell_output_str(&sensors_command, + "sensors {temp|acc}: a sensor must be specified", ""); + PROCESS_EXIT(); + } + + if(strcmp(data,"temp")==0) { + + unsigned int temp = temperature_sensor.value(0); + + snprintf(str_buf,sizeof(str_buf),"%d.%d degC",temp/10,temp-(temp/10)*10); + + shell_output_str(&sensors_command, "Temp: ", str_buf); + + } + else if (strcmp(data,"acc")==0) { + + snprintf(str_buf,sizeof(str_buf),"%d,%d,%d) mg",acc_sensor.value(ACC_X_AXIS),acc_sensor.value(ACC_Y_AXIS),acc_sensor.value(ACC_Z_AXIS)); + + shell_output_str(&sensors_command, "(X,Y,Z): (", str_buf); + + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_sensors_init(void) +{ + SENSORS_ACTIVATE(acc_sensor); + + shell_register_command(&sensors_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/platform/mbxxx/apps/shell-sensors.h b/platform/mbxxx/apps/shell-sensors.h new file mode 100644 index 000000000..a61562ffa --- /dev/null +++ b/platform/mbxxx/apps/shell-sensors.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2010, STMicroelectronics. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-sensors.h,v 1.1 2010/10/25 09:03:39 salvopitru Exp $ + */ + +/** + * \file + * Header file for Contik shell sensors command. + * \author + * Salvatore Pitrulli + */ + +#ifndef __SHELL_SENSORS_H__ +#define __SHELL_SENSORS_H__ + +#include "shell.h" + +void shell_sensors_init(void); + +#endif /* __SHELL_SENSORS_H__ */ diff --git a/platform/mbxxx/clock.c b/platform/mbxxx/clock.c new file mode 100644 index 000000000..76cfa4bf6 --- /dev/null +++ b/platform/mbxxx/clock.c @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2010, STMicroelectronics. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This file is part of the Contiki OS + * + */ +/*---------------------------------------------------------------------------*/ +/** +* \file +* Clock for STM32W. +* \author +* Salvatore Pitrulli +*/ +/*---------------------------------------------------------------------------*/ + +/* + * File customized for mbxxx platform. It uses systick timer to control button + * status without interrupts, as well as for system clock. + */ + +#include PLATFORM_HEADER +#include "hal/error.h" +#include "hal/hal.h" +#include "dev/stm32w_systick.h" + +#include "sys/clock.h" +#include "sys/etimer.h" +#include "dev/button-sensor.h" + +// The value that will be load in the SysTick value register. +#define RELOAD_VALUE 24000-1 // 1 ms with a 24 MHz clock + +static volatile clock_time_t count; +static volatile unsigned long current_seconds = 0; +static unsigned int second_countdown = CLOCK_SECOND; + +/*---------------------------------------------------------------------------*/ +void SysTick_Handler(void) +{ + + count++; + + if(button_sensor.status(SENSORS_READY)){ + button_sensor.value(0); // sensors_changed is called inside this function. + } + + if(etimer_pending()) { + etimer_request_poll(); + } + + if (--second_countdown == 0) { + current_seconds++; + second_countdown = CLOCK_SECOND; + } + +} + +/*---------------------------------------------------------------------------*/ + +void clock_init(void) +{ + + INTERRUPTS_OFF(); + + //Counts the number of ticks. + count = 0; + + SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); + SysTick_SetReload(RELOAD_VALUE); + SysTick_ITConfig(ENABLE); + SysTick_CounterCmd(SysTick_Counter_Enable); + + INTERRUPTS_ON(); +} + +/*---------------------------------------------------------------------------*/ + +clock_time_t clock_time(void) +{ + return count; +} + +/*---------------------------------------------------------------------------*/ +/** + * Delay the CPU for a multiple of TODO + */ +void clock_delay(unsigned int i) +{ + for (; i > 0; i--) { /* Needs fixing XXX */ + unsigned j; + for (j = 50; j > 0; j--) + asm ("nop"); + } +} + +/*---------------------------------------------------------------------------*/ +/** + * Wait for a multiple of 1 ms. + * + */ +void clock_wait(int i) +{ + clock_time_t start; + + start = clock_time(); + while(clock_time() - start < (clock_time_t)i); +} +/*---------------------------------------------------------------------------*/ + +unsigned long clock_seconds(void) +{ + return current_seconds; +} diff --git a/platform/mbxxx/contiki-conf.h b/platform/mbxxx/contiki-conf.h new file mode 100644 index 000000000..03155b492 --- /dev/null +++ b/platform/mbxxx/contiki-conf.h @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2010, STMicroelectronics. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This file is part of the Contiki OS + * + */ +/*---------------------------------------------------------------------------*/ +/** +* \file +* contiki-conf.h for MBXXX. +* \author +* Salvatore Pitrulli +*/ +/*---------------------------------------------------------------------------*/ + + +#ifndef __CONTIKI_CONF_H__ +#define __CONTIKI_CONF_H__ + +#include PLATFORM_HEADER + +#include +#include // For memcpm(). + +#define CC_CONF_REGISTER_ARGS 0 +#define CC_CONF_FUNCTION_POINTER_ARGS 1 +#define CC_CONF_FASTCALL +#define CC_CONF_VA_ARGS 1 +#define CC_CONF_INLINE inline + +#define CCIF +#define CLIF + +typedef uint8_t u8_t; +typedef uint16_t u16_t; +typedef uint32_t u32_t; +typedef int32_t s32_t; +typedef unsigned short uip_stats_t; + + +//#define FIXED_NET_ADDRESS 1 +//#define NET_ADDR_A 0x2001 +//#define NET_ADDR_B 0xdb8 +//#define NET_ADDR_C 0xbbbb +//#define NET_ADDR_D 0xabcd + +#define UART1_CONF_TX_WITH_INTERRUPT 0 +#define WITH_SERIAL_LINE_INPUT 1 +#define ENERGEST_CONF_ON 0 +#define TELNETD_CONF_NUMLINES 6 + +#define QUEUEBUF_CONF_NUM 2 + + +#define NETSTACK_CONF_RADIO stm32w_radio_driver + +#if WITH_UIP6 + +/* No radio cycling */ +#define NETSTACK_CONF_NETWORK sicslowpan_driver +#define NETSTACK_CONF_MAC nullmac_driver +#define NETSTACK_CONF_RDC sicslowmac_driver +#define NETSTACK_CONF_FRAMER framer_802154 + +#define RIMEADDR_CONF_SIZE 8 +#define UIP_CONF_LL_802154 1 + +#define UIP_CONF_ROUTER 1 +#define UIP_CONF_IPV6_RPL 1 +#define UIP_CONF_ND6_SEND_RA 0 +//#define RPL_BORDER_ROUTER 0 + +/* A trick to resolve a compilation error with IAR. */ +#ifdef __ICCARM__ +#define UIP_CONF_DS6_AADDR_NBU 1 +#endif + +#define UIP_CONF_IPV6 1 +#define UIP_CONF_IPV6_QUEUE_PKT 0 // This is a very costly feature as it increases the RAM usage by approximately UIP_ND6_MAX_NEIGHBORS * UIP_LINK_MTU bytes. +#define UIP_CONF_IPV6_CHECKS 1 +#define UIP_CONF_IPV6_REASSEMBLY 0 +#define UIP_CONF_ND6_MAX_PREFIXES 2 +#define UIP_CONF_ND6_MAX_NEIGHBORS 2 +#define UIP_CONF_ND6_MAX_DEFROUTERS 1 +#define UIP_CONF_IP_FORWARD 0 +#define UIP_CONF_BUFFER_SIZE 140 +#define UIP_CONF_MAX_CONNECTIONS 6 +#define UIP_CONF_MAX_LISTENPORTS 6 +#define UIP_CONF_UDP_CONNS 3 + +#define SICSLOWPAN_CONF_COMPRESSION_IPV6 0 +#define SICSLOWPAN_CONF_COMPRESSION_HC1 1 +#define SICSLOWPAN_CONF_COMPRESSION_HC06 2 +#define SICSLOWPAN_CONF_COMPRESSION SICSLOWPAN_CONF_COMPRESSION_HC06 +#define SICSLOWPAN_CONF_FRAG 1 +#define SICSLOWPAN_CONF_MAX_ADDR_CONTEXTS 2 +#define SICSLOWPAN_CONF_MAXAGE 2 + +#define UIP_CONF_ICMP6 0 +#endif /* WITH_UIP6 */ + +#define UIP_CONF_UDP 1 +#define UIP_CONF_TCP 1 + +#define IEEE802154_CONF_PANID 0x1234 +#define STM32W_NODE_ID 0x5678 // to be deleted +#define RF_CHANNEL 16 +#define RADIO_RXBUFS 2 // Set to a number greater than 1 to decrease packet loss probability at high rates (e.g, with fragmented packets) +#define UIP_CONF_LLH_LEN 0 + +typedef unsigned long clock_time_t; + +#define CLOCK_CONF_SECOND 1000 + +typedef unsigned long long rtimer_clock_t; +#define RTIMER_CLOCK_LT(a,b) ((signed short)((a)-(b)) < 0) + +/* LEDs ports MB8xxx */ + +#define LEDS_CONF_GREEN LED_D1 +#define LEDS_CONF_YELLOW LED_D3 +#define LEDS_CONF_RED LED_D3 + + +#define UIP_ARCH_ADD32 1 +#define UIP_ARCH_CHKSUM 0 + +#define UIP_CONF_BYTE_ORDER UIP_LITTLE_ENDIAN + + +#ifdef PROJECT_CONF_H +#include PROJECT_CONF_H +#endif /* PROJECT_CONF_H */ + + +#endif /* __CONTIKI_CONF_H__ */ diff --git a/platform/mbxxx/contiki-main.c b/platform/mbxxx/contiki-main.c new file mode 100644 index 000000000..33bb39748 --- /dev/null +++ b/platform/mbxxx/contiki-main.c @@ -0,0 +1,265 @@ +/* + * Copyright (c) 2010, STMicroelectronics. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This file is part of the Contiki OS + * + */ +/*---------------------------------------------------------------------------*/ +/** +* \file +* Contiki main file. +* \author +* Salvatore Pitrulli +*/ +/*---------------------------------------------------------------------------*/ + + +#include PLATFORM_HEADER +#include "hal/error.h" +#include "hal/hal.h" +#include BOARD_HEADER +#include "micro/adc.h" + +#include + + +#include "contiki.h" + +#include "dev/watchdog.h" +#include "dev/leds.h" +#include "dev/button-sensor.h" +#include "dev/temperature-sensor.h" +#include "dev/acc-sensor.h" +#include "dev/uart1.h" +#include "dev/serial-line.h" + +#include "dev/stm32w-radio.h" +#include "net/netstack.h" +#include "net/rime/rimeaddr.h" +#include "net/rime.h" +#include "net/rime/rime-udp.h" +#include "net/uip.h" + + +#define DEBUG 1 +#if DEBUG +#include +#define PRINTF(...) printf(__VA_ARGS__) +#define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15]) +#define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",lladdr.u8[0], lladdr.u8[1], lladdr.u8[2], lladdr.u8[3],lladdr.u8[4], lladdr.u8[5], lladdr.u8[6], lladdr.u8[7]) +#else +#define PRINTF(...) +#define PRINT6ADDR(addr) +#define PRINTLLADDR(addr) +#endif + + +#if UIP_CONF_IPV6 +PROCINIT(&etimer_process, &tcpip_process, &sensors_process); +#else +PROCINIT(&etimer_process, &sensors_process); +#warning "No TCP/IP process!" +#endif + +SENSORS(&button_sensor,&temperature_sensor,&acc_sensor); + +/*---------------------------------------------------------------------------*/ +static void +set_rime_addr(void) +{ + int i; + union { + u8_t u8[8]; + }eui64; + + //rimeaddr_t lladdr; + + int8u *stm32w_eui64 = ST_RadioGetEui64(); + { + int8u c; + for(c = 0; c < 8; c++) { // Copy the EUI-64 to lladdr converting from Little-Endian to Big-Endian. + eui64.u8[c] = stm32w_eui64[7 - c]; + } + } + PRINTF("\n\rRadio EUI-64:"); + PRINTLLADDR(eui64); + PRINTF("\n\r"); + +#if UIP_CONF_IPV6 + memcpy(&uip_lladdr.addr, &eui64, sizeof(uip_lladdr.addr)); +#endif + +#if UIP_CONF_IPV6 + rimeaddr_set_node_addr((rimeaddr_t *)&eui64); +#else + rimeaddr_set_node_addr((rimeaddr_t *)&eui64.u8[8-RIMEADDR_SIZE]); +#endif + + printf("Rime started with address "); + for(i = 0; i < sizeof(rimeaddr_t) - 1; i++) { + printf("%d.", rimeaddr_node_addr.u8[i]); + } + printf("%d\n", rimeaddr_node_addr.u8[i]); + +} +/*---------------------------------------------------------------------------*/ +int +main(void) +{ + + /* + * Initialize hardware. + */ + halInit(); + clock_init(); + + uart1_init(115200); + + // Led initialization + leds_init(); + + INTERRUPTS_ON(); + + PRINTF("\r\nStarting "); + PRINTF(CONTIKI_VERSION_STRING); + PRINTF(" on %s\r\n",boardDescription->name); + + /* + * Initialize Contiki and our processes. + */ + + process_init(); + +#if WITH_SERIAL_LINE_INPUT + uart1_set_input(serial_line_input_byte); + serial_line_init(); +#endif + + netstack_init(); +#if !UIP_CONF_IPV6 + ST_RadioEnableAutoAck(FALSE); // Because frames are not 802.15.4 compatible. + ST_RadioEnableAddressFiltering(FALSE); +#endif + + set_rime_addr(); + + ctimer_init(); + rtimer_init(); + + procinit_init(); + + energest_init(); + ENERGEST_ON(ENERGEST_TYPE_CPU); + + autostart_start(autostart_processes); + + + watchdog_start(); + + while(1){ + + int r; + + do { + /* Reset watchdog. */ + watchdog_periodic(); + r = process_run(); + } while(r > 0); + + + + ENERGEST_OFF(ENERGEST_TYPE_CPU); + //watchdog_stop(); + ENERGEST_ON(ENERGEST_TYPE_LPM); + /* Go to idle mode. */ + halSleepWithOptions(SLEEPMODE_IDLE,0); + /* We are awake. */ + //watchdog_start(); + ENERGEST_OFF(ENERGEST_TYPE_LPM); + ENERGEST_ON(ENERGEST_TYPE_CPU); + + } + +} + + + +/*int8u errcode __attribute__(( section(".noinit") )); + +void halBaseBandIsr(){ + + errcode = 1; + leds_on(LEDS_RED); +} + +void BusFault_Handler(){ + + errcode = 2; + leds_on(LEDS_RED); +} + +void halDebugIsr(){ + + errcode = 3; + leds_on(LEDS_RED); +} + +void DebugMon_Handler(){ + + errcode = 4; + //leds_on(LEDS_RED); +} + +void HardFault_Handler(){ + + errcode = 5; + //leds_on(LEDS_RED); + //halReboot(); +} + +void MemManage_Handler(){ + + errcode = 6; + //leds_on(LEDS_RED); + //halReboot(); +} + +void UsageFault_Handler(){ + + errcode = 7; + //leds_on(LEDS_RED); + //halReboot(); +}*/ + +void Default_Handler() +{ + //errcode = 8; + leds_on(LEDS_RED); + halReboot(); +} diff --git a/platform/mbxxx/dev/acc-sensor.c b/platform/mbxxx/dev/acc-sensor.c new file mode 100644 index 000000000..e3e6221c9 --- /dev/null +++ b/platform/mbxxx/dev/acc-sensor.c @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2010, STMicroelectronics. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This file is part of the Contiki OS + * + * $Id: acc-sensor.c,v 1.1 2010/10/25 09:03:39 salvopitru Exp $ + */ +/*---------------------------------------------------------------------------*/ +/** +* \file +* Accelerometer. +* \author +* Salvatore Pitrulli +*/ +/*---------------------------------------------------------------------------*/ + + +#include "dev/acc-sensor.h" +#include "mems.h" + +void clock_wait(int i); + +#define FALSE 0 +#define TRUE 1 + +/*---------------------------------------------------------------------------*/ +static int +active(void) +{ + int8u reg; + if(!i2c_read_reg (kLIS3L02DQ_SLAVE_ADDR,CTRL_REG1, ®, 1)) + return FALSE; + + return (reg & 0x40) ? TRUE : FALSE ; +} +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + + int8s i2c_data = 0; + int8u reg_addr; + + switch(type) { + case ACC_X_AXIS: + reg_addr = OUTX_H; + break; + + case ACC_Y_AXIS: + reg_addr = OUTY_H; + break; + + case ACC_Z_AXIS: + reg_addr = OUTZ_H; + break; + + default: + return 0; + } + + i2c_read_reg(kLIS3L02DQ_SLAVE_ADDR, reg_addr, (int8u *)&i2c_data, 1); + + if(MEMS_GetFullScale()==ACC_HIGH_RANGE){ + return ((int16s)i2c_data)*HIGH_RANGE_SENSITIVITY; + } + else { + return ((int16s)i2c_data)*LOW_RANGE_SENSITIVITY; + } + +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int value) +{ + switch(type) { + + case SENSORS_HW_INIT: + return Mems_Init(); + + case SENSORS_ACTIVE: + if(value){ + if(MEMS_On()){ + clock_wait(8); + return 1; + } + return 0; + } + else + return MEMS_Off(); + + case ACC_RANGE: + return MEMS_SetFullScale((boolean)value); + + case ACC_HPF: + if(value < ACC_HPF_DISABLE){ + return i2c_write_reg(kLIS3L02DQ_SLAVE_ADDR, CTRL_REG2, (1<<4) | (int8u)value); + } + else { + return i2c_write_reg(kLIS3L02DQ_SLAVE_ADDR, CTRL_REG2, 0x00); + } + } + return 0; +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + switch(type) { + + case SENSORS_READY: + return active(); + } + + return 0; +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(acc_sensor, ACC_SENSOR, + value, configure, status); + + + + diff --git a/platform/mbxxx/dev/acc-sensor.h b/platform/mbxxx/dev/acc-sensor.h new file mode 100644 index 000000000..9092db742 --- /dev/null +++ b/platform/mbxxx/dev/acc-sensor.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2010, STMicroelectronics. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This file is part of the Contiki OS + * + * $Id: acc-sensor.h,v 1.1 2010/10/25 09:03:39 salvopitru Exp $ + */ +/*---------------------------------------------------------------------------*/ +/** +* \file +* Accelerometer header file. +* \author +* Salvatore Pitrulli +*/ +/*---------------------------------------------------------------------------*/ + +#ifndef __ACC_SENSOR_H__ +#define __ACC_SENSOR_H__ + +#include "lib/sensors.h" +#include "mems.h" + +extern const struct sensors_sensor acc_sensor; + +#define ACC_SENSOR "Acc" + +/* The type values used in the configure() function. +*/ + +/* Full-scale*/ +#define ACC_RANGE 1 // type + +#define ACC_LOW_RANGE MEMS_LOW_RANGE // Values +#define ACC_HIGH_RANGE MEMS_HIGH_RANGE + +/* High pass filter and cut-off frequencies*/ +#define ACC_HPF 2 // type + +#define ACC_HPF_DISABLE 4 // Values +#define ACC_1HZ 3 +#define ACC_2HZ 2 +#define ACC_4HZ 1 +#define ACC_8HZ 0 + +/* The type values used in the value() function. + * The returned value is expressed in mg units and can be negative. +*/ +#define ACC_X_AXIS 1 +#define ACC_Y_AXIS 2 +#define ACC_Z_AXIS 3 + +#endif /* __ACC_SENSOR_H__ */ diff --git a/platform/mbxxx/dev/button-sensor.c b/platform/mbxxx/dev/button-sensor.c new file mode 100644 index 000000000..5dafe4f2b --- /dev/null +++ b/platform/mbxxx/dev/button-sensor.c @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2010, STMicroelectronics. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This file is part of the Contiki OS + * + * $Id: button-sensor.c,v 1.1 2010/10/25 09:03:39 salvopitru Exp $ + */ +/*---------------------------------------------------------------------------*/ +/** +* \file +* Button sensor. +* \author +* Salvatore Pitrulli +*/ +/*---------------------------------------------------------------------------*/ + +#include "dev/button-sensor.h" +#include "hal.h" +#include "hal/micro/micro-common.h" +#include "hal/micro/cortexm3/micro-common.h" + +#include BOARD_HEADER + +#define DEBOUNCE 1 + +#if DEBOUNCE +static struct timer debouncetimer; +#endif + +#define FALSE 0 +#define TRUE 1 + +uint8_t button_flags = 0; + +#define BUTTON_ACTIVE_FLG 0x01 +#define BUTTON_PRESSED_FLG 0x02 + +#define BUTTON_HAS_BEEN_PRESSED() (button_flags & BUTTON_PRESSED_FLG) +#define BUTTON_HAS_BEEN_RELEASED() (!(button_flags & BUTTON_PRESSED_FLG)) +#define BUTTON_SET_PRESSED() (button_flags |= BUTTON_PRESSED_FLG) +#define BUTTON_SET_RELEASED() (button_flags &= ~BUTTON_PRESSED_FLG) + +/*---------------------------------------------------------------------------*/ +static void +init(void) +{ + #if DEBOUNCE + timer_set(&debouncetimer, 0); + #endif + + /* Configure GPIO for BUTTONSs */ + halInitButton(); + +} +/*---------------------------------------------------------------------------*/ +static void +activate(void) +{ + button_flags |= BUTTON_ACTIVE_FLG; +} +/*---------------------------------------------------------------------------*/ +static void +deactivate(void) +{ + button_flags &= ~BUTTON_ACTIVE_FLG; +} +/*---------------------------------------------------------------------------*/ +static int +active(void) +{ + return (button_flags & BUTTON_ACTIVE_FLG)? 1 : 0; +} +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + if(!active()){ + return 0; + } + + +#if DEBOUNCE + if(timer_expired(&debouncetimer)) { + + if(halGetButtonStatus(BUTTON_S1) == BUTTON_PRESSED){ + + timer_set(&debouncetimer, CLOCK_SECOND / 10); + if(BUTTON_HAS_BEEN_RELEASED()){ // Button has been previously released. + sensors_changed(&button_sensor); + } + BUTTON_SET_PRESSED(); + + return 1; + } + else { + BUTTON_SET_RELEASED(); + return 0; + } + } + else { + return 0; + } +#else + if(halGetButtonStatus(BUTTON_S1) == BUTTON_PRESSED){ + sensors_changed(&button_sensor); + return 1; + } + else { + return 0; + } +#endif + +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int value) +{ + switch(type){ + case SENSORS_HW_INIT: + init(); + return 1; + case SENSORS_ACTIVE: + if(value) + activate(); + else + deactivate(); + return 1; + } + + return 0; +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + switch(type) { + + case SENSORS_READY: + return active(); + } + + return 0; +} +/*---------------------------------------------------------------------------*/ +#if 0 +void BUTTON_S1_ISR(void) +{ + + ENERGEST_ON(ENERGEST_TYPE_IRQ); + + //sensors_handle_irq(IRQ_BUTTON); + + if(INT_GPIOFLAG & BUTTON_S1_FLAG_BIT) { + +#if DEBOUNCE + if(timer_expired(&debouncetimer)) { + timer_set(&debouncetimer, CLOCK_SECOND / 5); + sensors_changed(&button_sensor); + } +#else + sensors_changed(&button_sensor); +#endif + + } + + INT_GPIOFLAG = BUTTON_S1_FLAG_BIT; + + ENERGEST_OFF(ENERGEST_TYPE_IRQ); +} +#endif +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(button_sensor, BUTTON_SENSOR, + value, configure, status); + diff --git a/platform/mbxxx/dev/irq.c b/platform/mbxxx/dev/irq.c new file mode 100644 index 000000000..160e0c1b7 --- /dev/null +++ b/platform/mbxxx/dev/irq.c @@ -0,0 +1,5 @@ +void +irq_init(void) +{ +} + diff --git a/platform/mbxxx/dev/leds-arch.c b/platform/mbxxx/dev/leds-arch.c new file mode 100644 index 000000000..51c756c1f --- /dev/null +++ b/platform/mbxxx/dev/leds-arch.c @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2010, STMicroelectronics. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This file is part of the Contiki OS + * + * $Id: leds-arch.c,v 1.1 2010/10/25 09:03:38 salvopitru Exp $ + */ +/*---------------------------------------------------------------------------*/ +/** +* \file +* Leds. +* \author +* Salvatore Pitrulli +*/ +/*---------------------------------------------------------------------------*/ + +#include PLATFORM_HEADER +#include BOARD_HEADER +#include "hal/micro/led.h" +#include "hal/micro/micro-common.h" +#include "hal/micro/cortexm3/micro-common.h" + + +#include "contiki-conf.h" +#include "dev/leds.h" + +/** + * @brief Read single GPIO bit from PxOUT + * + * @param io The io pin to use, can be specified with the convenience macros + * PORTA_PIN(), PORTB_PIN(), PORTC_PIN() + * @return Bit value: 1 if bit is set, 0 otherwise. + * + */ + +int8u halGpioGetPxOUT(int32u gpio) +{ + if(gpio/8 < 3) { + return (*((volatile int32u *)(GPIO_PxOUT_BASE+(GPIO_Px_OFFSET*(gpio/8)))) & BIT(gpio&7) ) == 0 ? 0 : 1 ; + } + return 0; +} + +/** + * @brief Read single GPIO bit from PxIN + * + * @param io The io pin to use, can be specified with the convenience macros + * PORTA_PIN(), PORTB_PIN(), PORTC_PIN() + * @return Bit value: 1 if bit is set, 0 otherwise. + * + */ + +int8u halGpioGetPxIN(int32u gpio) +{ + if(gpio/8 < 3) { + return (*((volatile int32u *)(GPIO_PxIN_BASE+(GPIO_Px_OFFSET*(gpio/8)))) & BIT(gpio&7) ) == 0 ? 0 : 1 ; + } + return 0; +} + + +/*---------------------------------------------------------------------------*/ +void +leds_arch_init(void) +{ + halInitLed(); +} +/*---------------------------------------------------------------------------*/ +unsigned char +leds_arch_get(void) +{ + return (halGpioGetPxOUT(LEDS_CONF_GREEN) ? 0 : LEDS_GREEN) + | (halGpioGetPxOUT(LEDS_CONF_YELLOW) ? 0 : LEDS_YELLOW) + | (halGpioGetPxOUT(LEDS_CONF_RED) ? 0 : LEDS_RED); +} +/*---------------------------------------------------------------------------*/ +void +leds_arch_set(unsigned char leds) +{ + halGpioSet(LEDS_CONF_GREEN, !(leds & LEDS_GREEN)); + halGpioSet(LEDS_CONF_YELLOW, !(leds & LEDS_YELLOW)); + halGpioSet(LEDS_CONF_RED, !(leds & LEDS_RED)); +} +/*---------------------------------------------------------------------------*/ diff --git a/platform/mbxxx/dev/mems.c b/platform/mbxxx/dev/mems.c new file mode 100644 index 000000000..88dd3de26 --- /dev/null +++ b/platform/mbxxx/dev/mems.c @@ -0,0 +1,368 @@ +/******************** (C) COPYRIGHT 2009 STMicroelectronics ******************** +* File Name : hal_led.c +* Author : MCD Application Team +* Version : V1.0 +* Date : September 2009 +* Description : Driver for leds management on STM32W108 MB851 board +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Includes ------------------------------------------------------------------*/ + +#include PLATFORM_HEADER +#include "mems.h" +#include "timer.h" + +/* Private define -- ---------------------------------------------------------*/ + +#define TIMEOUT 20000 + +#define SUCCESS 1 +#define FAIL 0 + +#define SEND_BYTE(data) do{ SC2_DATA=(data); SC2_TWICTRL1 |= SC_TWISEND; }while(0) + +#define WAIT_CMD_FIN() { \ + struct timer t; \ + timer_set(&t, CLOCK_SECOND/100); \ + while((SC2_TWISTAT&SC_TWICMDFIN)!=SC_TWICMDFIN){ \ + if(timer_expired(&t)){ \ + return FAIL; \ + } \ + } \ + } + +#define WAIT_TX_FIN() { \ + struct timer t; \ + timer_set(&t, CLOCK_SECOND/100); \ + while((SC2_TWISTAT&SC_TWITXFIN)!=SC_TWITXFIN){ \ + if(timer_expired(&t)){ \ + return FAIL; \ + } \ + } \ + } +#define WAIT_RX_FIN() { \ + struct timer t; \ + timer_set(&t, CLOCK_SECOND/100); \ + while((SC2_TWISTAT&SC_TWIRXFIN)!=SC_TWIRXFIN){ \ + if(timer_expired(&t)){ \ + return FAIL; \ + } \ + } \ + } + +/* Private variables ---------------------------------------------------------*/ +static boolean fullscale_state; + +/* Private functions ---------------------------------------------------------*/ +static int8u I2C_MEMS_Init (void); +//extern void halInternalResetWatchDog(void); +static int8u I2C_Send_Frame (int8u DeviceAddress, int8u *pBuffer, int8u NoOfBytes); +int8u i2c_write_reg (int8u slave_addr, int8u reg_addr, int8u reg_value); +//static int8u I2C_MEMS_Read (t_mems_data *mems_data); + +/* Functions -----------------------------------------------------------------*/ + +/******************************************************************************* +* Function Name : Mems_Init +* Description : It inits mems +* Input : None +* Output : status +* Return : None +*******************************************************************************/ +int8u Mems_Init(void) +{ + int8u ret = 0; + + // GPIO assignments + // PA1: SC2SDA (Serial Data) + // PA2: SC2SCL (Serial Clock) + + //-----SC2 I2C Master GPIO configuration + + TIM2_CCER &= 0xFFFFEEEE; + SC2_MODE = SC2_MODE_I2C; + GPIO_PACFGL &= 0xFFFFF00F; + GPIO_PACFGL |= 0x00000DD0; + + SC2_RATELIN = 14; // generates standard 100kbps or 400kbps + SC2_RATEEXP = 1; // 3 yields 100kbps; 1 yields 400kbps + SC2_TWICTRL1 = 0; // start from a clean state + SC2_TWICTRL2 = 0; // start from a clean state + + ret = I2C_MEMS_Init(); + + fullscale_state = MEMS_LOW_RANGE; + +//Add later if really needed +#ifdef ST_DBG + if (!ret) + I2C_DeInit(MEMS_I2C); +#endif + + return ret; +}/* end Mems_Init */ + +/******************************************************************************* +* Function Name : Mems_GetValue +* Description : It returns the 3 mems acceleration values related to x,y,z +* axes in mems_data +* Input : mems_data +* Output : status +* Return : None +*******************************************************************************/ +//int8u Mems_GetValue(t_mems_data *mems_data) +//{ +// int8u i; +// i = I2C_MEMS_Read(mems_data); +// return i; +//} + + +/* Private Functions ---------------------------------------------------------*/ + +/******************************************************************************* +* Function Name : I2C_Send_Frame +* Description : It sends I2C frame +* Input : DeviceAddress is the destination device address +* pBUffer is the buffer data +* NoOfBytes is the number of bytes +* Output : None +* Return : 1 if the frame has been successfully sent, 0 otherwise. +*******************************************************************************/ +static int8u I2C_Send_Frame (int8u DeviceAddress, int8u *pBuffer, int8u NoOfBytes) +{ + int8u i, data; + + SC2_TWICTRL1 |= SC_TWISTART; // send start + WAIT_CMD_FIN(); + + SEND_BYTE(DeviceAddress); // send the address low byte + WAIT_TX_FIN(); + + // loop sending the data + for (i=0; i 1) + addr += REPETIR; + + SC2_TWICTRL1 |= SC_TWISTART; // send start + WAIT_CMD_FIN(); + + SEND_BYTE(slave_addr | 0x00); // send the address low byte + WAIT_TX_FIN(); + + SEND_BYTE(addr); + WAIT_TX_FIN(); + + SC2_TWICTRL1 |= SC_TWISTART; // send start + WAIT_CMD_FIN(); + + SEND_BYTE(slave_addr | 0x01); // send the address low byte + WAIT_TX_FIN(); + + // loop receiving the data + for (i=0;ioutx_h = i2c_buffer[0]; +// mems_data->outx_l = i2c_buffer[1]; +// mems_data->outy_h = i2c_buffer[2]; +// mems_data->outy_l = i2c_buffer[3]; +// mems_data->outz_h = i2c_buffer[4]; +// mems_data->outz_l = i2c_buffer[5]; +// +// return i; +//}/* end I2C_MEMS_Read() */ + +/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/ diff --git a/platform/mbxxx/dev/mems.h b/platform/mbxxx/dev/mems.h new file mode 100644 index 000000000..45fa920fc --- /dev/null +++ b/platform/mbxxx/dev/mems.h @@ -0,0 +1,59 @@ +/******************** (C) COPYRIGHT 2009 STMicroelectronics ******************** +* File Name : mems.h +* Author : MCD Application Team +* Version : V1.0 +* Date : January 2010 +* Description : header file for mems driver +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +#ifndef _MEMS_H_ +#define _MEMS_H_ + +/* Define --------------------------------------------------------------------*/ +#include "mems_regs.h" + +/* Define --------------------------------------------------------------------*/ +// Sensitivity values in mg/digit +#define HIGH_RANGE_SENSITIVITY 72 +#define LOW_RANGE_SENSITIVITY 18 + +#define MEMS_LOW_RANGE 0 +#define MEMS_HIGH_RANGE 1 + +/* Typedef -------------------------------------------------------------------*/ +/* Mems data type: three acceleration values each related to a specific direction + Watch out: only lower data values (e.g. those terminated by the _l) are + currently used by the device */ + +typedef struct { + int8s outx_l; + int8s outx_h; + int8s outy_l; + int8s outy_h; + int8s outz_l; + int8s outz_h; +} t_mems_data; + +/* Functions -----------------------------------------------------------------*/ + +/* Mems Initialization function */ +int8u Mems_Init(void); + +int8u MEMS_On(void); +int8u MEMS_Off(void); +int8u MEMS_SetFullScale(boolean range); +boolean MEMS_GetFullScale(void); + +/* Get mems acceleration values */ +int8u Mems_GetValue(t_mems_data *mems_data); + +#endif /* _MEMS_H_ */ + +/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/ diff --git a/platform/mbxxx/dev/mems_regs.h b/platform/mbxxx/dev/mems_regs.h new file mode 100644 index 000000000..9796339a4 --- /dev/null +++ b/platform/mbxxx/dev/mems_regs.h @@ -0,0 +1,139 @@ +/******************** (C) COPYRIGHT 2009 STMicroelectronics ******************** +* File Name : mems_regs.h +* Author : MCD Application Team +* Version : V1.0 +* Date : January 2010 +* Description : stm32w108 mems registers +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +#ifndef _MEMS_REGS_H_ +#define _MEMS_REGS_H_ + +/* Private include ------------------------------------------------------------*/ + +/* Private define ------------------------------------------------------------*/ + +//---Size of MEMs data--- +#define MEMS_DATA_SIZE 6 + +//---address of slave I want to communicate with--- +#define kLIS3L02DQ_SLAVE_ADDR 0x38 + +//----------------------------------------------------------------------------- +//---accelerometer register addresses--- +#define OFFSET_X 0x16 +#define OFFSET_Y 0x17 +#define OFFSET_Z 0x18 +#define GAIN_X 0x19 +#define GAIN_Y 0x1A +#define GAIN_Z 0x1B +#define CTRL_REG1 0x20 +#define CTRL_REG2 0x21 +#define STATUS_REG 0x27 +#define OUTX_L 0x28 +#define OUTX_H 0x29 +#define OUTY_L 0x2A +#define OUTY_H 0x2B +#define OUTZ_L 0x2C +#define OUTZ_H 0x2D +#define FF_WU_CFG 0x30 +#define FF_WU_SRC 0x31 +#define FF_WU_ACK 0x32 +#define FF_WU_THS_L 0x34 +#define FF_WU_THS_H 0x35 +#define FF_WU_DURATION 0x36 +#define DD_CFG 0x38 +#define DD_SRC 0x39 +#define DD_ACK 0x3A +#define DD_THSI_L 0x3C +#define DD_THSI_H 0x3D +#define DD_THSE_L 0x3E +#define DD_THSE_H 0x3F + +// Communication control settings +// I2C +#define REPETIR 0x80 +// SPI +#define MEMS_SPI_WRITE 0x00 +#define MEMS_SPI_READ 0x80 +#define MEMS_SPI_MULTIPLE_BYTES 0x40 + +//----------------------------------------------------------------------------- +//---Settings for CTRL_REG1--- + +// Power Down Control (PD) +#define LIS_PD_OFF 0x00 +#define LIS_PD_ON 0x40 + +// Decimation Factor Control (DF) +#define LIS_DF_BY128 0x00 +#define LIS_DF_BY64 0x10 +#define LIS_DF_BY32 0x20 +#define LIS_DF_BY8 0x30 + +// Self Test +#define LIS_ST_NORMAL 0x00 +#define LIS_ST_TEST 0x08 + +// Enable Axis +#define LIS_EA_ALL 0x07 + +//----------------------------------------------------------------------------- +//---Settings for CTRL_REG2--- + +// Full Scale (FS) +#define LIS_FS_2G 0x00 +#define LIS_FS_6G 0x80 + +// Block Data Update (BDU) +#define LIS_BDU_CONTINUOUS 0x00 +#define LIS_BDU_WAIT 0x40 + +// Big/Little Endian Selection (BLE) +#define LIS_BLE_LE 0x00 +#define LIS_BLE_BE 0x20 + +// Data Alignment Selection DAS +#define LIS_DAS_12BIT 0x00 +#define LIS_DAS_16BIT 0x01 + +// Interrupt Enable +#define LIS_INTR_DISABLE 0x00 +#define LIS_INTR_ENABLE 0x08 + +// Data Ready Enable +#define LIS_DRDY_DISABLE 0x00 +#define LIS_DRDY_ENABLE 0x04 + +//----------------------------------------------------------------------------- + +#define LIS_BLE_XX LIS_BLE_BE + + +/* Private Functions Prototypes ----------------------------------------------*/ + +//*********************i2c_read_reg***********************// +//----------Reads a register on the I2C target------------// +//------slave addr is the is the I2C target device--------// +//-reg_addr is the address of the register to be written--// +//-pBuffer is the storage destination for the read data---// +//-NoOfBytes is the amount of data to read----------------// +int8u i2c_read_reg (int8u slave_addr, int8u reg_addr, int8u *pBuffer, int8u NoOfBytes); + +//*********************i2c_write_reg**********************// +//----------Writes a register on the I2C target-----------// +//------slave addr is the is the I2C target device--------// +//-reg_addr is the address of the register to be written--// +//-reg_value is the value of the register to be written---// +int8u i2c_write_reg (int8u slave_addr, int8u reg_addr, int8u reg_value); + +#endif /* _MEMS_REGS_H_ */ + +/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/ diff --git a/platform/mbxxx/dev/temperature-sensor.c b/platform/mbxxx/dev/temperature-sensor.c new file mode 100644 index 000000000..f0ebc57e1 --- /dev/null +++ b/platform/mbxxx/dev/temperature-sensor.c @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2010, STMicroelectronics. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This file is part of the Contiki OS + * + * $Id: temperature-sensor.c,v 1.1 2010/10/25 09:03:39 salvopitru Exp $ + */ +/*---------------------------------------------------------------------------*/ +/** +* \file +* Temperature sensor. +* \author +* Salvatore Pitrulli +*/ +/*---------------------------------------------------------------------------*/ + +/** + * NOTE: + * For the temperature measurement, the ADC extended range mode is needed; + * but this is inaccurate due to the high voltage mode bug of the general purpose ADC + * (see STM32W108 errata). + */ + + +#include PLATFORM_HEADER +#include BOARD_HEADER +#include "hal/error.h" +#include "hal/hal.h" +#include "micro/adc.h" + +#include "dev/temperature-sensor.h" + + +/*---------------------------------------------------------------------------*/ +static void +init(void) +{ + halGpioConfig(TEMPERATURE_SENSOR_GPIO,GPIOCFG_ANALOG); + halInternalInitAdc(); + halAdcSetRange(TRUE); +} +/*---------------------------------------------------------------------------*/ +static int +value(int type) +{ + static int16u ADCvalue; + static int16s volts; + + halStartAdcConversion(ADC_USER_APP, ADC_REF_INT, ADC_SOURCE_ADC2_VREF2, ADC_CONVERSION_TIME_US_4096); + + halReadAdcBlocking(ADC_USER_APP, &ADCvalue); // This blocks for a while, about 4ms. + + // 100 uVolts + volts = halConvertValueToVolts(ADCvalue); + + //return ((18641 - (int32s)volts)*100)/1171; // +- 0.23 degC in the range (-10;65) degC + return ((18663 - (int32s)volts)*100)/1169; // +- 0.004 degC in the range (20;30) degC + +} +/*---------------------------------------------------------------------------*/ +static int +configure(int type, int value) +{ + switch(type){ + case SENSORS_HW_INIT: + init(); + return 1; + case SENSORS_ACTIVE: + return 1; + } + + return 0; +} +/*---------------------------------------------------------------------------*/ +static int +status(int type) +{ + switch(type) { + + case SENSORS_READY: + return 1; + } + + return 0; +} +/*---------------------------------------------------------------------------*/ +SENSORS_SENSOR(temperature_sensor, TEMPERATURE_SENSOR, + value, configure, status); diff --git a/platform/mbxxx/dev/temperature-sensor.h b/platform/mbxxx/dev/temperature-sensor.h new file mode 100644 index 000000000..ba0402b9a --- /dev/null +++ b/platform/mbxxx/dev/temperature-sensor.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2010, STMicroelectronics. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * This file is part of the Contiki OS + * + * $Id: temperature-sensor.h,v 1.1 2010/10/25 09:03:39 salvopitru Exp $ + */ +/*---------------------------------------------------------------------------*/ +/** +* \file +* Temperature sensor. +* \author +* Salvatore Pitrulli +*/ +/*---------------------------------------------------------------------------*/ + +/* + * Value returned by temperature_sensor.value() is expressed in units of 0.1 °C. + */ +/** + * NOTE: + * For the temperature measurement, the ADC extended range mode is needed; + * but this is inaccurate due to the high voltage mode bug of the general purpose ADC + * (see STM32W108 errata). + */ + +#ifndef __TEMPERATURE_SENSOR_H__ +#define __TEMPERATURE_SENSOR_H__ + +#include "lib/sensors.h" + +extern const struct sensors_sensor temperature_sensor; + +#define TEMPERATURE_SENSOR "Temperature" + +#endif /* __TEMPERATURE_SENSOR_H__ */