Commit 81257dc6 authored by Matthieu Cattin's avatar Matthieu Cattin

sw: Add light sensor driver skeleton. Lux reading works.

parent 8d1253af
/*
* Copyright (C) 2014 Julian Lewis
* @author Matthieu Cattin <matthieu.cattin@cern.ch>
*
* This program 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
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @brief MAX44009 ambient light sensor driver
*/
#include "light_sensor.h"
#include <math.h>
#include <stdio.h>
#include "i2cdrv.h"
// Writes a register
uint8_t max44009_write_reg(uint8_t reg_add, uint8_t value)
{
I2C_TransferSeq_TypeDef seq;
I2C_TransferReturn_TypeDef ret;
seq.addr = MAX44009_ADDRESS;
seq.flags = I2C_FLAG_WRITE_WRITE; // sequence: write register address (1 byte), write value (1 byte)
// Add register address to buffer
seq.buf[0].data = &reg_add;
seq.buf[0].len = 1;
// Add value to buffer
seq.buf[1].data = &value;
seq.buf[1].len = 1;
// Initiate transfer
ret = I2CDRV_Transfer(&seq);
if(ret != i2cTransferDone)
{
return((uint8_t) ret);
}
return 0;
}
// Reads a register
uint8_t max44009_read_reg(uint8_t reg_add, uint8_t* value)
{
I2C_TransferSeq_TypeDef seq;
I2C_TransferReturn_TypeDef ret;
seq.addr = MAX44009_ADDRESS;
seq.flags = I2C_FLAG_WRITE_READ; // sequence: write register address (1 byte), read value (1 bytes)
// Add command to sequence
seq.buf[0].data = &reg_add;
seq.buf[0].len = 1;
// Read value in buffer
seq.buf[1].data = value;
seq.buf[1].len = 1;
// Initiate transfer
ret = I2CDRV_Transfer(&seq);
if(ret != i2cTransferDone)
{
return((uint8_t) ret);
}
return 0;
}
uint8_t light_sensor_set_int(uint8_t enable)
{
return 0;
}
uint8_t light_sensor_get_isr(uint8_t* isr)
{
return 0;
}
uint8_t light_sensor_set_cfg(Light_Sensor_Conf_TypeDef *cfg)
{
return 0;
}
uint8_t light_sensor_get_lux(double* lux)
{
uint8_t err, high_b, low_b, m, e;
/* TODO
* For accurate reading, high and low registers have to
* be read during the same i2c transfer.
* => Apparently not possible with the current i2cdrvr!
*
* For now, only read high byte
*/
// Read Lux registers high byte
err = max44009_read_reg(MAX44009_REG_LUX_H, &high_b);
if(err)
{
return err;
}
/*
// Read Lux register low byte
err = max44009_read_reg(MAX44009_REG_LUX_L, &low_b);
if(err)
{
return err;
}
*/
// Extract mantissa and exponent
m = high_b & 0xff;
//m = ((high_b & 0xff) << 4) + (low_b & 0xff);
e = ((high_b & 0xff00) >> 4);
// Calculate Lux value
*lux = pow(2,(double) e) * m * 0.72;
//*lux = pow(2,(double) e) * m * 0.045;
return 0;
}
uint8_t light_sensor_set_thres(double thres)
{
return 0;
}
uint8_t light_sensor_set_thres_timer(uint8_t timer)
{
return 0;
}
/*
* Copyright (C) 2014 Julian Lewis
* @author Matthieu Cattin <matthieu.cattin@cern.ch>
*
* This program 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
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @brief MAX44009 ambient light sensor driver
*/
#ifndef __LIGHT_SENSOR_H_
#define __LIGHT_SENSOR_H_
#include <stdint.h>
#include <stdbool.h>
// I2C address
#define MAX44009_ADDRESS 0x94
// MAX44009 registers
#define MAX44009_REG_INT_STAT 0x00
#define MAX44009_REG_INT_EN 0x01
#define MAX44009_REG_CONFIG 0x02
#define MAX44009_REG_LUX_H 0x03
#define MAX44009_REG_LUX_L 0x04
#define MAX44009_REG_THRES_H 0x05
#define MAX44009_REG_THRES_L 0x06
#define MAX44009_REG_THRES_TIME 0x07
// Configuration register bits
#define MAX44009_CFG_CONT 0x80 // Continuous mode
#define MAX44009_CFG_MAN 0x40 // Manual configuration
#define MAX44009_CFG_CDR 0x08 // Current division ratio
#define MAX44009_CFG_TIM 0x07 // Integration time
/** Ambient light sensor configuration structure. */
typedef struct
{
bool cont;
bool manual;
bool cdr;
uint8_t timer;
} Light_Sensor_Conf_TypeDef;
uint8_t light_sensor_set_int(uint8_t enable);
uint8_t light_sensor_get_isr(uint8_t* isr);
uint8_t light_sensor_set_cfg(Light_Sensor_Conf_TypeDef *cfg);
uint8_t light_sensor_get_lux(double* lux);
uint8_t light_sensor_set_thres(double thres);
uint8_t light_sensor_set_thres_timer(uint8_t timer);
#endif /* LIGHT_SENSOR_H */
# Copyright (C) 2014 Julian Lewis
# @author Maciej Suminski <maciej.suminski@cern.ch>
#
# This program 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
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, you may find one here:
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# or you may search the http://www.gnu.org website for the version 2 license,
# or you may write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
####################################################################
# Makefile #
####################################################################
.SUFFIXES: # ignore builtin rules
.PHONY: all debug release clean flash
####################################################################
# Definitions #
####################################################################
DEVICE = EFM32GG330F1024
PROJECTNAME = freewatch
# Name of interface configuration file used by OpenOCD
OOCD_IFACE ?= stlink-v2-1
OBJ_DIR = build
EXE_DIR = exe
LST_DIR = lst
####################################################################
# Definitions of toolchain. #
# You might need to do changes to match your system setup #
####################################################################
# Change path to the tools according to your system configuration
# DO NOT add trailing whitespace chars, they do matter !
WINDOWSCS ?= GNU Tools ARM Embedded\4.7 2012q4
LINUXCS ?= /opt/gcc-arm-none-eabi-4_8-2014q2
RMDIRS := rm -rf
RMFILES := rm -rf
ALLFILES := /*.*
NULLDEVICE := /dev/null
SHELLNAMES := $(ComSpec)$(COMSPEC)
# Try autodetecting the environment
ifeq ($(SHELLNAMES),)
# Assume we are making on a Linux platform
TOOLDIR := $(LINUXCS)
else
QUOTE :="
ifneq ($(COMSPEC),)
# Assume we are making on a mingw/msys/cygwin platform running on Windows
# This is a convenient place to override TOOLDIR, DO NOT add trailing
# whitespace chars, they do matter !
TOOLDIR := $(PROGRAMFILES)/$(WINDOWSCS)
ifeq ($(findstring cygdrive,$(shell set)),)
# We were not on a cygwin platform
NULLDEVICE := NUL
endif
else
# Assume we are making on a Windows platform
# This is a convenient place to override TOOLDIR, DO NOT add trailing
# whitespace chars, they do matter !
SHELL := $(SHELLNAMES)
TOOLDIR := $(ProgramFiles)/$(WINDOWSCS)
RMDIRS := rd /s /q
RMFILES := del /s /q
ALLFILES := \*.*
NULLDEVICE := NUL
endif
endif
# Create directories and do a clean which is compatible with parallel make
$(shell mkdir $(OBJ_DIR)>$(NULLDEVICE) 2>&1)
$(shell mkdir $(EXE_DIR)>$(NULLDEVICE) 2>&1)
$(shell mkdir $(LST_DIR)>$(NULLDEVICE) 2>&1)
ifeq (clean,$(findstring clean, $(MAKECMDGOALS)))
ifneq ($(filter $(MAKECMDGOALS),all debug release),)
$(shell $(RMFILES) $(OBJ_DIR)$(ALLFILES)>$(NULLDEVICE) 2>&1)
$(shell $(RMFILES) $(EXE_DIR)$(ALLFILES)>$(NULLDEVICE) 2>&1)
$(shell $(RMFILES) $(LST_DIR)$(ALLFILES)>$(NULLDEVICE) 2>&1)
endif
endif
CC = $(QUOTE)$(TOOLDIR)/bin/arm-none-eabi-gcc$(QUOTE)
LD = $(QUOTE)$(TOOLDIR)/bin/arm-none-eabi-ld$(QUOTE)
AR = $(QUOTE)$(TOOLDIR)/bin/arm-none-eabi-ar$(QUOTE)
OBJCOPY = $(QUOTE)$(TOOLDIR)/bin/arm-none-eabi-objcopy$(QUOTE)
DUMP = $(QUOTE)$(TOOLDIR)/bin/arm-none-eabi-objdump$(QUOTE)
####################################################################
# Flags #
####################################################################
# -MMD : Don't generate dependencies on system header files.
# -MP : Add phony targets, useful when a h-file is removed from a project.
# -MF : Specify a file to write the dependencies to.
DEPFLAGS = -MMD -MP -MF $(@:.o=.d)
#
# Add -Wa,-ahld=$(LST_DIR)/$(@F:.o=.lst) to CFLAGS to produce assembly list files
#
override CFLAGS += -D$(DEVICE) -Wall -Wextra -mcpu=cortex-m3 -mthumb \
-mfix-cortex-m3-ldrd -ffunction-sections \
-fdata-sections -fomit-frame-pointer -DDEBUG_EFM \
$(DEPFLAGS)
override ASMFLAGS += -x assembler-with-cpp -D$(DEVICE) -Wall -Wextra -mcpu=cortex-m3 -mthumb
#
# NOTE: The -Wl,--gc-sections flag may interfere with debugging using gdb.
#
override LDFLAGS += -Xlinker -Map=$(LST_DIR)/$(PROJECTNAME).map -mcpu=cortex-m3 \
-mthumb -T../common/Device/EnergyMicro/EFM32GG/Source/GCC/efm32gg.ld \
-Wl,--gc-sections
LIBS = -Wl,--start-group -lgcc -lc -lnosys -Wl,--end-group
INCLUDEPATHS += \
-I../common/CMSIS/Include \
-I../common/Device/EnergyMicro/EFM32GG/Include \
-I../common/emlib/inc \
-I../common/drivers \
-I../common/glib \
-I../common
####################################################################
# Files #
####################################################################
C_SRC += \
../common/Device/EnergyMicro/EFM32GG/Source/system_efm32gg.c \
../common/drivers/lcd.c \
../common/emlib/src/em_assert.c \
../common/emlib/src/em_cmu.c \
../common/emlib/src/em_emu.c \
../common/emlib/src/em_gpio.c \
../common/emlib/src/em_i2c.c \
../common/emlib/src/em_int.c \
../common/emlib/src/em_system.c \
../common/emlib/src/em_rtc.c \
../common/emlib/src/em_usart.c \
../common/gfx/graphics.c \
../common/gfx/font_helv11.c \
../common/gfx/font_helv17.c \
../common/gfx/font_helv17b.c \
../common/gfx/font_xm4x5.c \
../common/gfx/font_xm4x6.c \
../common/udelay.c \
../common/delay.c \
../common/drivers/i2cdrv.c \
../common/drivers/light_sensor.c \
../common/drivers/altimeter.c \
main.c
#../common/drivers/display.c \
#../common/drivers/displayls013b7dh03.c \
#../common/drivers/displaypalemlib.c \
s_SRC +=
S_SRC += \
../common/Device/EnergyMicro/EFM32GG/Source/GCC/startup_efm32gg.S
####################################################################
# Rules #
####################################################################
C_FILES = $(notdir $(C_SRC) )
S_FILES = $(notdir $(S_SRC) $(s_SRC) )
#make list of source paths, sort also removes duplicates
C_PATHS = $(sort $(dir $(C_SRC) ) )
S_PATHS = $(sort $(dir $(S_SRC) $(s_SRC) ) )
C_OBJS = $(addprefix $(OBJ_DIR)/, $(C_FILES:.c=.o))
S_OBJS = $(if $(S_SRC), $(addprefix $(OBJ_DIR)/, $(S_FILES:.S=.o)))
s_OBJS = $(if $(s_SRC), $(addprefix $(OBJ_DIR)/, $(S_FILES:.s=.o)))
C_DEPS = $(addprefix $(OBJ_DIR)/, $(C_FILES:.c=.d))
OBJS = $(C_OBJS) $(S_OBJS) $(s_OBJS)
vpath %.c $(C_PATHS)
vpath %.s $(S_PATHS)
vpath %.S $(S_PATHS)
# Default build is debug build
all: debug
debug: CFLAGS += -DDEBUG -O0 -g
debug: $(EXE_DIR)/$(PROJECTNAME).bin
release: CFLAGS += -DNDEBUG -O0 -g
release: $(EXE_DIR)/$(PROJECTNAME).bin
# Create objects from C SRC files
$(OBJ_DIR)/%.o: %.c
@echo "Building file: $<"
$(CC) $(CFLAGS) $(INCLUDEPATHS) -c -o $@ $<
# Assemble .s/.S files
$(OBJ_DIR)/%.o: %.s
@echo "Assembling $<"
$(CC) $(ASMFLAGS) $(INCLUDEPATHS) -c -o $@ $<
$(OBJ_DIR)/%.o: %.S
@echo "Assembling $<"
$(CC) $(ASMFLAGS) $(INCLUDEPATHS) -c -o $@ $<
# Link
$(EXE_DIR)/$(PROJECTNAME).out: $(OBJS)
@echo "Linking target: $@"
$(CC) $(LDFLAGS) $(OBJS) $(LIBS) -lm -o $(EXE_DIR)/$(PROJECTNAME).out
# Create binary file
$(EXE_DIR)/$(PROJECTNAME).bin: $(EXE_DIR)/$(PROJECTNAME).out
@echo "Creating binary file"
$(OBJCOPY) -O binary $(EXE_DIR)/$(PROJECTNAME).out $(EXE_DIR)/$(PROJECTNAME).bin
# Uncomment next line to produce assembly listing of entire program
# $(DUMP) -h -S -C $(EXE_DIR)/$(PROJECTNAME).out>$(LST_DIR)/$(PROJECTNAME)out.lst
clean:
ifeq ($(filter $(MAKECMDGOALS),all debug release),)
$(RMDIRS) $(OBJ_DIR) $(LST_DIR) $(EXE_DIR)
endif
flash: $(EXE_DIR)/$(PROJECTNAME).bin
openocd -s ../common/openocd -f interface/$(OOCD_IFACE).cfg -f init.cfg -c "program $(EXE_DIR)/$(PROJECTNAME).bin 0 verify reset"
# include auto-generated dependency files (explicit rules)
ifneq (clean,$(findstring clean, $(MAKECMDGOALS)))
-include $(C_DEPS)
endif
#! /bin/sh
sudo OOCD_IFACE=stlink-v2 make flash
/*
* Copyright (C) 2014 Julian Lewis
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* This program 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
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @brief Main file.
*/
#include <stdio.h>
#include <math.h>
#include <delay.h>
#include <i2cdrv.h>
#include <em_device.h>
#include <em_cmu.h>
#include <em_gpio.h>
#include <drivers/lcd.h>
#include <drivers/light_sensor.h>
#include <gfx/graphics.h>
#include <drivers/altimeter.h>
/**
* @brief Main function
*/
int main(void)
{
I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT;
//int x, y, i;
char str[20];
uint8_t err;
double temp = 0;
double pressure = 0;
double altitude = 0;
double lux;
/* Setup SysTick Timer for 1 msec interrupts */
if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) while (1);
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_GPIO, true);
// Backlight LEDs
GPIO_PinModeSet(gpioPortE, 11, gpioModePushPull, 0);
GPIO_PinModeSet(gpioPortE, 12, gpioModePushPull, 0);
lcd_init();
I2CDRV_Init(&i2cInit);
err = alti_init();
//GPIO_PinOutSet(gpioPortE, 11);
//GPIO_PinOutSet(gpioPortE, 12);
while(1)
{
err = light_sensor_get_lux(&lux);
sprintf(str, "light: %3.3f lux", lux);
text(&font_helv11, 5, 10, str);
err = alti_get_temp_pressure(&temp, &pressure, true);
sprintf(str, "temp: %f C", temp);
text(&font_helv11, 5, 30, str);
sprintf(str, "pressure: %f mbar", pressure);
text(&font_helv11, 5, 40, str);
err = alti_mbar2altitude(pressure, &altitude);
sprintf(str, "altitude: %f m", altitude);
text(&font_helv11, 5, 50, str);
lcd_update();
//Delay(1000);
box(0, 0, 128, 128, 0);
//lcd_clear();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment