Contiki-NG
zonik.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Zolertia <http://www.zolertia.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 /*---------------------------------------------------------------------------*/
33 /**
34  * \addtogroup remote-zonik
35  * @{
36  * Driver for the RE-Mote Zonik sonometer board
37  * @{
38  * \file
39  * Driver for the RE-Mote Zonik sound sensor (ZONIK)
40  * \author
41  * Aitor Mejias <amejias@zolertia.com>
42  */
43 /*---------------------------------------------------------------------------*/
44 #include <stdio.h>
45 #include "contiki.h"
46 #include "dev/gpio.h"
47 #include "dev/i2c.h"
48 #include "zonik.h"
49 #include "sys/timer.h"
50 #include "sys/etimer.h"
51 #include "lib/sensors.h"
52 /*---------------------------------------------------------------------------*/
53 #define DEBUG 0
54 #if DEBUG
55 #define PRINTF(...) printf(__VA_ARGS__)
56 #else
57 #define PRINTF(...)
58 #endif
59 /*---------------------------------------------------------------------------*/
60 #define ZONIK_INT1_PORT_BASE GPIO_PORT_TO_BASE(ZONIK_INT_PORT)
61 #define ZONIK_INT1_PIN_MASK GPIO_PIN_MASK(ZONIK_INT_PIN)
62 /*---------------------------------------------------------------------------*/
63 static uint8_t zonik_buffer[ZONIK_FRAME_SIZE+1];
64 static uint16_t zonik_status = ZONIK_DISABLED;
65 /*---------------------------------------------------------------------------*/
66 static struct etimer et;
67 /*---------------------------------------------------------------------------*/
68 PROCESS(zonik_stm_process, "Zonik process process handler");
69 /*---------------------------------------------------------------------------*/
70 PROCESS_THREAD(zonik_stm_process, ev, data)
71 {
72  #if DEBUG
73  static int i;
74  #endif
76  PROCESS_BEGIN();
77 
78  while(1) {
79  /* Wait a process */
80  etimer_set(&et, ZONIK_SECOND_INTERVAL);
82 
83  /* Control the interrupt for activate the sensor */
84  GPIO_SET_OUTPUT(ZONIK_INT1_PORT_BASE, ZONIK_INT1_PIN_MASK);
85  GPIO_CLR_PIN(ZONIK_INT1_PORT_BASE, ZONIK_INT1_PIN_MASK);
86  clock_delay_usec(ZONIK_INITIAL_WAIT_DELAY);
88  if(i2c_single_send(ZONIK_ADDR, ZONIK_CMD_READ) != I2C_MASTER_ERR_NONE) {
89  zonik_status = ZONIK_ERROR;
90  PRINTF("Zonik: Error in I2C Communication\n");
91  }
92  GPIO_SET_PIN(ZONIK_INT1_PORT_BASE, ZONIK_INT1_PIN_MASK);
93  GPIO_SET_INPUT(ZONIK_INT1_PORT_BASE, ZONIK_INT1_PIN_MASK);
94  if(zonik_status != ZONIK_ERROR) {
95  etimer_set(&et, ZONIK_WAIT_ACQ);
97  clock_delay_usec(ZONIK_FINAL_WAIT_DELAY);
99  if(i2c_burst_receive(ZONIK_ADDR, &zonik_buffer[0], ZONIK_FRAME_SIZE) !=
100  I2C_MASTER_ERR_NONE) {
101  zonik_status = ZONIK_ERROR;
102  PRINTF("Zonik: Error in I2C Burst Mode Receive");
103  }
104  #if DEBUG
105  PRINTF("\nZonik: ");
106  for(i=0; i<ZONIK_FRAME_SIZE; i++) {
107  PRINTF(" 0x%02x ", zonik_buffer[i]);
108  }
109  PRINTF("\n");
110  #endif
111  if(zonik_status != ZONIK_ERROR) {
112  zonik_status = ZONIK_ACTIVE;
113  }
114  }
115  }
116  PROCESS_END();
117 }
118 /*---------------------------------------------------------------------------*/
119 static int
120 status(int type)
121 {
122  switch(type) {
123  case SENSORS_ACTIVE:
124  case SENSORS_READY:
125  return zonik_status;
126  }
127  return 0;
128 }
129 /*---------------------------------------------------------------------------*/
130 static int
131 value(int type)
132 {
133  /* If the system is enabling the sensor internally. Return the same state */
134  if(zonik_status == ZONIK_HW_INIT) {
135  return ZONIK_HW_INIT;
136  }
137  /* If no valid parameter, return error */
138  if(zonik_status != ZONIK_ACTIVE) {
139  return ZONIK_DISABLED;
140  }
141  /* Return the dBA received data */
142  if(type == ZONIK_DBA_LEQ_VALUE) {
143  return ((uint16_t)zonik_buffer[2] << 8) + zonik_buffer[3];
144  }
145  /* Return the internal counter loop received data */
146  if(type == ZONIK_COUNT_VALUE) {
147  return ((uint16_t)zonik_buffer[0] << 8) + zonik_buffer[1];
148  }
149  return ZONIK_ERROR;
150 }
151 /*---------------------------------------------------------------------------*/
152 static int
153 configure(int type, int value)
154 {
155  if(type == ZONIK_ACTIVE) {
156  if(value == ZONIK_VALUE_DEACTIVATE) {
157  /* Disable the Zonik Sensor reading process */
158  process_exit(&zonik_stm_process);
159  zonik_status = ZONIK_DISABLED;
160  return zonik_status;
161  }
162  /* Enable the Zonik Sensor reading process */
163  i2c_init(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_PORT, I2C_SCL_PIN,
164  I2C_SCL_NORMAL_BUS_SPEED);
165 
166  /* configuration of I2C interrupt control */
167  GPIO_SOFTWARE_CONTROL(ZONIK_INT1_PORT_BASE, ZONIK_INT1_PIN_MASK);
168  GPIO_SET_INPUT(ZONIK_INT1_PORT_BASE, ZONIK_INT1_PIN_MASK);
169  /* Launch the main process */
170  process_start(&zonik_stm_process, NULL);
171  zonik_status = ZONIK_HW_INIT;
172  return zonik_status;
173  }
174  /* Bad configuration */
175  return ZONIK_ERROR;
176 }
177 /*---------------------------------------------------------------------------*/
178 SENSORS_SENSOR(zonik, ZONIK_SENSOR, value, configure, status);
179 /*---------------------------------------------------------------------------*/
180 /**
181  * @}
182  * @}
183  */
184 
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
Header file for the Zolertia Zonik sound sensor.
#define GPIO_SET_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE high.
Definition: gpio.h:106
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
Header file with register and macro declarations for the cc2538 GPIO module.
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
#define GPIO_CLR_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE low.
Definition: gpio.h:113
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
Definition: clock.c:150
Timer library header file.
void process_exit(struct process *p)
Cause a process to exit.
Definition: process.c:202
#define GPIO_SOFTWARE_CONTROL(PORT_BASE, PIN_MASK)
Configure the pin to be software controlled with PIN_MASK of port with PORT_BASE. ...
Definition: gpio.h:258
Event timer header file.
#define PROCESS_EXITHANDLER(handler)
Specify an action when a process exits.
Definition: process.h:254
void i2c_init(uint8_t port_sda, uint8_t pin_sda, uint8_t port_scl, uint8_t pin_scl, uint32_t bus_speed)
Initialize the I2C peripheral and pins.
Definition: i2c.c:49
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
#define GPIO_SET_INPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to input.
Definition: gpio.h:78
#define GPIO_SET_OUTPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to output.
Definition: gpio.h:85
A timer.
Definition: etimer.h:76
uint8_t i2c_burst_receive(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to receive multiple bytes from a slave.
Definition: i2c.c:218
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1110
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
uint8_t i2c_single_send(uint8_t slave_addr, uint8_t data)
Perform all operations to send a byte to a slave.
Definition: i2c.c:159
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
void i2c_master_enable(void)
Enable master I2C module.
Definition: i2c.c:91