initial commit

This commit is contained in:
tkl
2016-07-23 07:59:54 +02:00
commit cb58a410f1
1378 changed files with 372066 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
/*
* adc.c
*
* Created on: Dec 23, 2012
* Author: tkl
*/
#include <stddef.h>
#include "adc.h"
//------------------------------------------------------------------------------
int adc_open(const struct adc *device) {
if(NULL == device) {
return -1;
}
adc_fp_open open = device->fp->open;
return open(device->arch_dep_device);
}
//------------------------------------------------------------------------------
int adc_close(const struct adc *device) {
if(NULL == device) {
return -1;
}
adc_fp_close close = device->fp->close;
return close(device->arch_dep_device);
}
//------------------------------------------------------------------------------
uint16_t adc_read(const struct adc *device, int timeout) {
if(NULL == device) {
return -1;
}
adc_fp_read read = device->fp->read;
return read(device->arch_dep_device, timeout);
}

View File

@@ -0,0 +1,38 @@
/*
* adc.h
*
* Created on: Dec 23, 2012
* Author: tkl
*/
#ifndef ADC_H_
#define ADC_H_
#include <stdint.h>
//------------------------------------------------------------------------------
typedef int (*adc_fp_open)(const void *);
typedef int (*adc_fp_close)(const void *);
typedef uint16_t (*adc_fp_read)(const void *, int);
//------------------------------------------------------------------------------
//! \brief Contains the function pointer to access the adc driver.
struct adc_fp {
const adc_fp_open open; //!< Function pointer to the open function.
const adc_fp_close close; //!< Function pointer to the close function.
const adc_fp_read read; //!< Function pointer to the read function.
};
//------------------------------------------------------------------------------
//! \brief Contains the architecture depended device and the access functions to the adc driver.
struct adc {
const void *arch_dep_device; //!< Architecture depended adc device (i.e. msp430_adc_t).
const struct adc_fp *fp; //!< Function pointer for the adc driver access.
};
//------------------------------------------------------------------------------
int adc_open(const struct adc *device);
int adc_close(const struct adc *device);
uint16_t adc_read(const struct adc *device, int timeout);
#endif /* ADC_H_ */

View File

@@ -0,0 +1,25 @@
/*
* battery_monitor.c
*
* Created on: Dec 23, 2012
* Author: tkl
*/
#include "stddef.h"
#include "adc.h"
#include "battery_monitor.h"
//------------------------------------------------------------------------------
struct battery_monitor_voltage get_voltage(const struct battery_monitor *device)
{
struct battery_monitor_voltage ret;
ret.valid = false;
if(NULL == device) {
return ret;
}
adc_open(device->adc);
ret.raw = adc_read(device->adc, device->timeout);
adc_close(device->adc);
ret.voltage = ret.raw * device->factor / device->divider + device->offset;
ret.valid = true;
return ret;
}

View File

@@ -0,0 +1,34 @@
/*
* battery_monitor.h
*
* Created on: Dec 23, 2012
* Author: tkl
*/
#ifndef BATTERY_MONITOR_H_
#define BATTERY_MONITOR_H_
#include "stdbool.h"
//------------------------------------------------------------------------------
//! \brief battery monitor device.
struct battery_monitor {
const struct adc *adc; //!< Adc device for the battery monitor.
const unsigned short timeout; //!< Adc measurement timeout.
const int factor; //!< Factor to calculate voltage from adc result.
const int divider; //!< Divider to calculate voltage from adc result.
const int offset; //!< Offset to calculate voltage from adc result.
};
//------------------------------------------------------------------------------
//! \brief Value container for the battery monitor.
struct battery_monitor_voltage {
unsigned short voltage; //!< Calculated voltage.
unsigned short raw; //!< Raw value of the adc.
bool valid; //!< Indicates if result is valid.
};
//------------------------------------------------------------------------------
struct battery_monitor_voltage get_voltage(const struct battery_monitor * device);
#endif /* BATTERY_MONITOR_H_ */

View File

@@ -0,0 +1,147 @@
/*
* cc110x.c
*
* Created on: Jul 20, 2012
* Author: tkl
*/
#include <stddef.h>
#include <stdbool.h>
#include "board.h"
#include "stack.h"
#include "queue.h"
#include "thread.h"
#include "schedule.h"
#include "irq.h"
#include "sys_tick.h"
#include "cc110x.h"
static volatile bool tx_condition;
extern volatile struct thread_context *current_thread;
static void it_cb(const struct cc110x *cc110x, enum cc110x_it_type it_type);
int cc110x_open(const struct cc110x *cc110x)
{
if(NULL == cc110x)
return (-1);
tx_condition = false;
cc110x->fp->init(cc110x);
cc110x->fp->set_it_callback(cc110x->arch_dep_device, it_cb, cc110x);
cc110x_set_radio_mode(cc110x, CC110X_RADIO_MODE_PWD);
cc110x_write_pa_table(cc110x);
return (0);
}
int cc110x_close(const struct cc110x *cc110x)
{
cc110x_set_radio_mode(cc110x, CC110X_RADIO_MODE_PWD);
blocking_read_wakeup(cc110x);
return 0;
}
void cc110x_set_radio_mode(const struct cc110x *cc110x,
enum cc110x_radio_mode mode)
{
if(NULL == cc110x) {
return;
}
*(cc110x->radio_mode) = mode;
enum cc110x_strobe_cmd cmd = STROBE_SIDLE;
switch(mode) {
case CC110X_RADIO_MODE_NONE: return;
case CC110X_RADIO_MODE_IDLE: cmd = STROBE_SIDLE; break;
case CC110X_RADIO_MODE_RX: cmd = STROBE_SRX; break;
case CC110X_RADIO_MODE_TX: cmd = STROBE_STX; break;
case CC110X_RADIO_MODE_PWD: cmd = STROBE_SPWD; break;
}
cc110x->fp->strobe(cc110x->arch_dep_device, cmd);
}
enum cc110x_radio_mode cc110x_get_radio_mode(const struct cc110x *cc110x)
{
if(NULL == cc110x)
return (CC110X_RADIO_MODE_NONE);
return (*(cc110x->radio_mode));
}
void cc110x_write_pa_table(const struct cc110x *cc110x)
{
if(NULL == cc110x)
return;
cc110x->fp->write_pa_table(cc110x->arch_dep_device);
}
void cc110x_strobe(const struct cc110x *cc110x, enum cc110x_strobe_cmd cmd)
{
if(NULL == cc110x)
return;
cc110x->fp->strobe(cc110x->arch_dep_device, cmd);
}
int cc110x_write(const struct cc110x *cc110x, const char *buffer,
int size)
{
enum cc110x_radio_mode mode;
int ret;
if((NULL == cc110x) || (NULL == buffer))
return (-1);
mode = cc110x_get_radio_mode(cc110x);
cc110x_set_radio_mode(cc110x, CC110X_RADIO_MODE_IDLE);
cc110x_strobe(cc110x, STROBE_SFTX);
ret = cc110x->fp->write(cc110x->arch_dep_device, buffer, size);
tx_condition = true;
cc110x_set_radio_mode(cc110x, CC110X_RADIO_MODE_TX);
while(tx_condition);
cc110x_set_radio_mode(cc110x, CC110X_RADIO_MODE_IDLE);
cc110x_strobe(cc110x, STROBE_SFTX);
cc110x_set_radio_mode(cc110x, mode);
return (ret);
}
int cc110x_read(const struct cc110x *cc110x, char *buffer, int size)
{
int ret;
unsigned int irq;
if((NULL == cc110x) || (NULL == buffer))
return (-1);
if(0 == cc110x_get_rx_count(cc110x)) {
irq = disable_irq();
current_thread->status = THREAD_STATUS_BLOCKING;
current_thread->wakeup_blocking_source = (void*) cc110x;
restore_irq(irq);
schedule();
}
enum cc110x_radio_mode mode = cc110x_get_radio_mode(cc110x);
cc110x_set_radio_mode(cc110x, CC110X_RADIO_MODE_IDLE);
ret = cc110x_get_rx_count(cc110x);
ret = cc110x->fp->read(cc110x->arch_dep_device, buffer, ret);
cc110x_strobe(cc110x, STROBE_SFRX);
cc110x_set_radio_mode(cc110x, mode);
return (ret);
}
int cc110x_get_rx_count(const struct cc110x *cc110x)
{
if(NULL == cc110x)
return (-1);
return (cc110x->fp->get_rx_count(cc110x->arch_dep_device));
}
static void it_cb(const struct cc110x *cc110x,
enum cc110x_it_type it_type)
{
if(NULL == cc110x)
return;
switch(it_type) {
case IT_TYPE_NONE:
break;
case IT_TYPE_RX_COMPLETE:
blocking_read_wakeup(cc110x);
break;
case IT_TYPE_TX_COMPLETE:
tx_condition = false;
break;
}
}

View File

@@ -0,0 +1,88 @@
/*
* cc110x.h
*
* Created on: Jul 20, 2012
* Author: tkl
*/
#ifndef CC110X_H_
#define CC110X_H_
enum cc110x_radio_mode {
CC110X_RADIO_MODE_NONE = 0,
CC110X_RADIO_MODE_IDLE,
CC110X_RADIO_MODE_RX,
CC110X_RADIO_MODE_TX,
CC110X_RADIO_MODE_PWD
};
enum cc110x_strobe_cmd {
STROBE_SRES = 0,
STROBE_SFSTXON,
STROBE_SXOFF,
STROBE_SCAL,
STROBE_SRX,
STROBE_STX,
STROBE_SIDLE,
STROBE_SWOR,
STROBE_SPWD,
STROBE_SFRX,
STROBE_SFTX,
STROBE_SWORRST,
STROBE_SNOP
};
enum cc110x_it_type {
IT_TYPE_NONE = 0,
IT_TYPE_RX_COMPLETE,
IT_TYPE_TX_COMPLETE
};
typedef void (*cc110x_fp_init)(const void *);
typedef void (*cc110x_fp_write_pa_table)(const void *);
typedef void (*cc110x_fp_set_radio_mode)(const void *, enum cc110x_radio_mode);
typedef char (*cc110x_fp_strobe)(const void *, enum cc110x_strobe_cmd);
typedef int (*cc110x_fp_write)(const void *, const char *, int);
typedef int (*cc110x_fp_read)(const void *, char *, int);
typedef int (*cc110x_fp_set_it_cb)(const void *, const void *, const void *);
typedef int (*cc110x_fp_get_rx_count)(const void *);
//! \brief Contains the function pointer to access the cc110x driver.
struct cc110x_fp {
/*! Function pointer to the init function. */
const cc110x_fp_init init;
/*! Function pointer to the strobe function. */
const cc110x_fp_strobe strobe;
/*! Function pointer to the write function. */
const cc110x_fp_write write;
/*! Function pointer to the read function. */
const cc110x_fp_read read;
/*! Function pointer to the write_pa_table function. */
const cc110x_fp_write_pa_table write_pa_table;
/*! Function pointer to the set_it_callback function. */
const cc110x_fp_set_it_cb set_it_callback;
/*! Function pointer to the get_rx_count function. */
const cc110x_fp_get_rx_count get_rx_count;
};
//! \brief The cc110x driver object.
struct cc110x {
/*! Radio mode. */
enum cc110x_radio_mode *radio_mode;
/*! Architecture depended underlaying device (i.e. spi_t or NULL). */
const void *arch_dep_device;
/*! Function pointer for the cc110x driver access. */
const struct cc110x_fp *fp;
};
int cc110x_open(const struct cc110x *cc110x);
int cc110x_close(const struct cc110x *cc110x);
int cc110x_write(const struct cc110x *cc110x, const char *buffer, int size);
int cc110x_read(const struct cc110x *cc110x, char *buffer, int size);
int cc110x_get_rx_count(const struct cc110x *cc110x);
void cc110x_set_radio_mode(const struct cc110x *cc110x, enum cc110x_radio_mode mode);
enum cc110x_radio_mode cc110x_get_radio_mode(const struct cc110x *cc110x);
void cc110x_write_pa_table(const struct cc110x *cc110x);
void cc110x_strobe(const struct cc110x *cc110x, enum cc110x_strobe_cmd cmd);
#endif /* CC110X_H_ */

View File

@@ -0,0 +1,3 @@
SUB_FOLDER += firmware/kernel/driver
INCLUDES += firmware/kernel/driver
DOC_SRC += firmware/kernel/driver

View File

@@ -0,0 +1,126 @@
/*
* ds75.c
*
* Created on: Aug 1, 2012
* Author: tkl
*/
#include <stddef.h>
#include "ds75.h"
//-----------------------------------------------------------------------------
#define TEMPERATURE_REGISTER_ADDR 0x00
#define CONFIGURATION_REGISTER_ADDR 0x01
//-----------------------------------------------------------------------------
int ds75_open(const struct ds75 *device) {
if(NULL == device) {
return -1;
}
*device->config_value = 0x00;
return device->i2c->fp->open(device->i2c->arch_dep_device);
}
//-----------------------------------------------------------------------------
int ds75_close(const struct ds75 *device) {
if(NULL == device) {
return -1;
}
return device->i2c->fp->close(device->i2c->arch_dep_device);
}
//-----------------------------------------------------------------------------
int ds75_set_precision(const struct ds75 *device, enum ds75_precision precision)
{
if(NULL == device) {
return -1;
}
char cfg = *device->config_value | (precision << 5);
char buffer[] = {CONFIGURATION_REGISTER_ADDR, cfg};
int ret = device->i2c->fp->write(device->i2c->arch_dep_device,
device->i2c_slave_addr, buffer, sizeof(buffer));
if (ret >= 0) {
*device->config_value = cfg;
}
return ret;
}
//-----------------------------------------------------------------------------
enum ds75_precision ds_75_get_precision(const struct ds75 *device) {
if(NULL == device) {
return DS_75_PRECISION_INVALID;
}
char cfg = *device->config_value;
cfg >>= 5;
cfg &= 3;
return (enum ds75_precision) cfg;
}
//-----------------------------------------------------------------------------
int ds_75_shut_down(const struct ds75 *device) {
if(NULL == device) {
return -1;
}
char cfg = *device->config_value | (1 << 0);
char buffer[] = {CONFIGURATION_REGISTER_ADDR, cfg};
int ret = device->i2c->fp->write(device->i2c->arch_dep_device,
device->i2c_slave_addr, buffer, sizeof(buffer));
if (ret >= 0) {
*device->config_value = cfg;
}
return ret;
}
//-----------------------------------------------------------------------------
int ds_75_wakeup(const struct ds75 *device) {
if(NULL == device) {
return -1;
}
char cfg = *device->config_value & ~(1 << 0);
char buffer[] = {CONFIGURATION_REGISTER_ADDR, cfg};
int ret = device->i2c->fp->write(device->i2c->arch_dep_device,
device->i2c_slave_addr, buffer, sizeof(buffer));
if (ret >= 0) {
*device->config_value = cfg;
}
return ret;
}
//-----------------------------------------------------------------------------
struct ds75_temperature ds_75_get_temperature(const struct ds75 *device) {
struct ds75_temperature ret;
ret.temperature = 0xFFFF;
ret.raw[0] = 0xFF;
ret.raw[1] = 0xFF;
ret.valid = false;
if(NULL == device) {
return ret;
}
// set temperature register
char addr = TEMPERATURE_REGISTER_ADDR;
int res = device->i2c->fp->write(device->i2c->arch_dep_device, device->i2c_slave_addr, &addr, 1);
if(res < 0) {
return ret;
}
// read temperature register
res = device->i2c->fp->read(device->i2c->arch_dep_device, device->i2c_slave_addr, ret.raw, 2);
if(res < 0) {
return ret;
}
ret.valid = true;
ret.temperature = ret.raw[0];
if(ret.temperature & 0x0080) {
ret.temperature |= 0xFF00;
}
ret.temperature *= 10;
unsigned short s_low = (unsigned char)ret.raw[1];
s_low >>= 4;
s_low *= 625;
s_low += 500;
s_low /= 1000;
ret.temperature += s_low;
return ret;
}

View File

@@ -0,0 +1,49 @@
/*
* ds75.h
*
* Created on: Aug 1, 2012
* Author: tkl
*/
#ifndef DS75_H_
#define DS75_H_
#include <stdbool.h>
#include "i2c.h"
//-----------------------------------------------------------------------------
enum ds75_precision {
DS_75_PRECISION_9_BIT = 0,
DS_75_PRECISION_10_BIT,
DS_75_PRECISION_11_BIT,
DS_75_PRECISION_12_BIT,
DS_75_PRECISION_INVALID
};
//-----------------------------------------------------------------------------
//! \brief The ds75 driver object.
struct ds75 {
const struct i2c * i2c; //!< The i2c device the ds75 is connected to.
const char i2c_slave_addr; //!< The i2c slave address of the ds 75.
char *const config_value; //!< Config value.
};
//-----------------------------------------------------------------------------
//! \brief Data container for the ds 75 measurement result.
struct ds75_temperature {
short temperature; //!< Calculated temperature.
char raw[2]; //!< Raw i2c result.
bool valid; //!< Validity indicator.
};
//-----------------------------------------------------------------------------
int ds75_open(const struct ds75 *device);
int ds75_close(const struct ds75 *device);
int ds75_set_precision(const struct ds75 *device, enum ds75_precision precision);
enum ds75_precision ds_75_get_precision(const struct ds75 *device);
int ds_75_shut_down(const struct ds75 *device);
int ds_75_wakeup(const struct ds75 *device);
struct ds75_temperature ds_75_get_temperature(const struct ds75 *device);
#endif /* DS75_H_ */

View File

@@ -0,0 +1,61 @@
/*! \file gpio.c
* \author tkl
* \date Feb 13, 2012
* \brief Source file of the architecture independent gpio driver.
*/
#include <stddef.h>
#include "gpio.h"
int gpio_open(const struct gpio *device)
{
if(device == NULL)
return -1;
gpio_fp_open_t open = device->fp->open;
return open(device->arch_dep_device);
}
int gpio_close(const struct gpio *device)
{
if(device == NULL)
return -1;
gpio_fp_close_t close = device->fp->close;
return close(device->arch_dep_device);
}
char gpio_read(const struct gpio *device)
{
if(device == NULL)
return 0;
gpio_fp_read_t read = device->fp->read;
return read(device->arch_dep_device);
}
void gpio_write(const struct gpio *device, char byte)
{
if(device == NULL)
return;
gpio_fp_write_t write = device->fp->write;
write(device->arch_dep_device, byte);
}
void gpio_toggle(const struct gpio *device) {
if(device == NULL)
return;
gpio_fp_toggle_t toggle = device->fp->toggle;
toggle(device->arch_dep_device);
}
int gpio_set_exti_callback(const struct gpio *device, const void *callback,
const void *param)
{
if((device == NULL) || (callback == NULL))
return -1;
gpio_fp_set_cb_t set_cb = device->fp->set_cb;
return set_cb(device->arch_dep_device, callback, param);
}

View File

@@ -0,0 +1,78 @@
/*! \file gpio.h
* \author tkl
* \date Feb 13, 2012
* \brief Header file of the architecture independent gpio driver.
*/
#ifndef GPIO_H_
#define GPIO_H_
//! \brief Function pointer to the open function.
typedef int (*gpio_fp_open_t)(const void*);
//! \brief Function pointer to the close function.
typedef int (*gpio_fp_close_t)(const void*);
//! \brief Function pointer to the read function.
typedef char (*gpio_fp_read_t)(const void*);
//! \brief Function pointer to the write function.
typedef void (*gpio_fp_write_t)(const void*, char);
//! \brief Function pointer to the toggle function.
typedef void (*gpio_fp_toggle_t)(const void*);
//! \brief Function pointer to the set_callback function.
typedef int (*gpio_fp_set_cb_t)(const void*, const void*, const void*);
//! \brief Contains the function pointer to access the gpio driver.
struct gpio_fp {
const gpio_fp_open_t open; //!< Function pointer to the open function.
const gpio_fp_close_t close; //!< Function pointer to the close function.
const gpio_fp_read_t read; //!< Function pointer to the read function.
const gpio_fp_write_t write; //!< Function pointer to the write function.
const gpio_fp_toggle_t toggle; //!< Function pointer to the toggle function.
const gpio_fp_set_cb_t set_cb; //!< Function pointer to the set_callback function.
};
//! \brief Contains the architecture depended device and the access functions to the gpio driver.
struct gpio {
const void *arch_dep_device; //!< Architecture depended gpio device (i.e. stm32f10x_gpio_t).
const struct gpio_fp *fp; //!< Function pointer for the gpio driver access.
};
/*! \brief Open a gpio pin.
* \param device The gpio to open.
* \retval -1 in error case.
*/
int gpio_open(const struct gpio *device);
/*! \brief Close a gpio pin
* \param device The gpio to close.
* \retval -1 in error case.
*/
int gpio_close(const struct gpio *device);
/*! \brief read from a gpio pin
* \param device The gpio to read from
* \return Read out value.
*/
char gpio_read(const struct gpio *device);
/*! \brief write to a gpio pin
* \param device The gpio to write to.
* \param byte The value to write.
*/
void gpio_write(const struct gpio *device, char byte);
//! \brief toggle a gpio pin
//! \param device the gpio to toggle.
void gpio_toggle(const struct gpio *device);
//! \brief set the callback for a gpio pin external interrupt
//! \param device The gpio to set a callback for.
//! \param callback The function pointer to be called back.
//! \param param The parameter for the call back.
int gpio_set_exti_callback(const struct gpio *device, const void *callback,
const void *param);
#endif /* GPIO_H_ */

View File

@@ -0,0 +1,53 @@
//! \file i2c.c
//! \author tkl
//! \date Apr 26, 2012
//! \brief Source file of the architecture independent i2c driver.
#include <stddef.h>
#include "i2c.h"
int i2c_open(struct i2c *device)
{
if(device == NULL) {
return -1;
}
i2c_fp_open_t open = device->fp->open;
device->cnt_subscriber++;
return open(device->arch_dep_device);
}
int i2c_close(struct i2c *device)
{
int ret = -1;
if(device == NULL) {
return -1;
}
i2c_fp_close_t close = device->fp->close;
device->cnt_subscriber--;
if(device->cnt_subscriber == 0)
ret = close(device->arch_dep_device);
else
ret = 0; /* close only if all subscribers are gone. */
return ret;
}
//-----------------------------------------------------------------------------
int i2c_write(const struct i2c *device, char addr, const char *buffer,
unsigned int len)
{
if(device == NULL) {
return -1;
}
i2c_fp_write_t write = device->fp->write;
return write(device->arch_dep_device, addr, buffer, len);
}
//-----------------------------------------------------------------------------
int i2c_read(const struct i2c *device, char addr, char *buffer,
unsigned int len)
{
if(device == NULL) {
return -1;
}
i2c_fp_read_t read = device->fp->read;
return read(device->arch_dep_device, addr, buffer, len);
}

View File

@@ -0,0 +1,75 @@
//! \file spi.h
//! \author tkl
//! \date Apr 26, 2012
//! \brief Header file of the architecture independent i2c driver.
#ifndef I2C_H_
#define I2C_H_
//-----------------------------------------------------------------------------
//! \brief Function pointer to the open function.
typedef int (*i2c_fp_open_t)(const void *);
//-----------------------------------------------------------------------------
//! \brief Function pointer to the close function.
typedef int (*i2c_fp_close_t)(const void *);
//-----------------------------------------------------------------------------
//! \brief Function pointer to the write function.
typedef int (*i2c_fp_write_t)(const void *, char addr, const char *buffer,
unsigned int len);
//-----------------------------------------------------------------------------
//! \brief Function pointer to the read function.
typedef int (*i2c_fp_read_t)(const void *, char addr, char *buffer,
unsigned int len);
//-----------------------------------------------------------------------------
//! \brief Contains the function pointer to access the i2c driver.
struct i2c_fp {
const i2c_fp_open_t open; //!< Function pointer to the open function.
const i2c_fp_close_t close; //!< Function pointer to the close function.
const i2c_fp_write_t write; //!< Function pointer to the write function.
const i2c_fp_read_t read; //!< Function pointer to the read function.
};
//-----------------------------------------------------------------------------
//! \brief Contains the architecture depended device and the access functions to the i2c driver.
struct i2c {
const void *arch_dep_device; //!< Architecture depended i2c device (i.e. stm32f10x_i2c_t).
const struct i2c_fp *fp; //!< Function pointer for the i2c driver access.
int cnt_subscriber; //!< Number of opened slaves
};
//-----------------------------------------------------------------------------
//! \brief Open i2c device.
//! \param device The device to open.
//! \retval -1 in error case.
int i2c_open(struct i2c *device);
//-----------------------------------------------------------------------------
//! \brief Close i2c device.
//! \param device The device to close.
//! \retval -1 in error case.
int i2c_close(struct i2c *device);
//-----------------------------------------------------------------------------
//! \brief Write to an i2c device.
//! \param device The device to write to.
//! \param addr The i2c address of the i2c sink.
//! \param buffer The buffer to write.
//! \param len The length of the buffer.
//! \retval -1 in error case, otherwise the number of written bytes.
int i2c_write(const struct i2c *device, char addr, const char *buffer,
unsigned int len);
//-----------------------------------------------------------------------------
//! \brief Read from an i2c device.
//! \param device The device to read from.
//! \param addr The i2c address of the i2c source
//! \param buffer The buffer to read to.
//! \param len The maximum length of the buffer.
//! \retval -1 in error case, otherwise the number of read bytes.
int i2c_read(const struct i2c *device, char addr, char *buffer,
unsigned int len);
#endif /* I2C_H_ */

View File

@@ -0,0 +1,218 @@
//! \file rtc.c
//! \author tkl
//! \date Jul 8, 2012
//! \brief Source file of the architecture independent rtc implementation.
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include "rtc.h"
//-----------------------------------------------------------------------------
int rtc_open(const struct rtc *device) {
if(NULL == device) {
return (-1);
}
rtc_fp_open_t open = device->fp->open;
return (open(device->arch_dep_device));
}
//-----------------------------------------------------------------------------
int rtc_close(const struct rtc *device) {
if(NULL == device) {
return (-1);
}
rtc_fp_close_t close = device->fp->close;
return (close(device->arch_dep_device));
}
//-----------------------------------------------------------------------------
void rtc_set_time(const struct rtc *device, const struct loki_time *time) {
if(NULL == device) {
return;
}
rtc_fp_set_time_t set_time = device->fp->set_time;
set_time(device->arch_dep_device, time);
}
//-----------------------------------------------------------------------------
struct loki_time *rtc_get_time(const struct rtc *device, struct loki_time *time) {
if(NULL == device) {
return (NULL);
}
rtc_fp_get_time_t get_time = device->fp->get_time;
return (get_time(device->arch_dep_device, time));
}
#ifndef __isleap
//! Nonzero if YEAR is a leap year (every 4 years, except every 100th isn't, and every 400th is).
#define __isleap(year) \
((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
#endif
//! Seconds per hour.
#define SECS_PER_HOUR (long)(60 * 60)
//! Seconds per day.
#define SECS_PER_DAY (long)(SECS_PER_HOUR * 24)
//! Clocks per sec.
#define CLOCKS_PER_SEC 1000
static const uint8_t __mon_lengths[2][12] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, /* Normal years. */
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* Leap years. */
};
//-----------------------------------------------------------------------------
uint32_t time_to_tick(const struct loki_time *time) {
if(NULL == time) {
return (0);
}
uint32_t ret = 0;
uint32_t year = time->year - 1970;
ret = time->sec + time->min * 60 + time->hour * 3600;
uint32_t days_in_year = 0;
switch(time->mon) {
case 1:
days_in_year = time->day;
break;
case 2:
days_in_year = time->day + 31;
break;
case 3:
days_in_year = time->day + 31 + 28;
break;
case 4:
days_in_year = time->day + 31 + 28 + 31;
break;
case 5:
days_in_year = time->day + 31 + 28 + 31 + 30;
break;
case 6:
days_in_year = time->day + 31 + 28 + 31 + 30 + 31;
break;
case 7:
days_in_year = time->day + 31 + 28 + 31 + 30 + 31 + 30;
break;
case 8:
days_in_year = time->day + 31 + 28 + 31 + 30 + 31 + 30 + 31;
break;
case 9:
days_in_year = time->day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
break;
case 10:
days_in_year = time->day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
break;
case 11:
days_in_year = time->day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
break;
case 12:
days_in_year = time->day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
break;
}
if(days_in_year > 0) {
days_in_year--;
}
uint32_t leap_days = 0;
uint32_t y = time->year;
while(y >= 1970) {
leap_days += __isleap(y) ? 1 : 0;
y--;
}
if(__isleap(time->year)) {
if(days_in_year < 59) {
if(leap_days > 0) {
leap_days--;
}
}
}
ret += (days_in_year + leap_days) * 60 * 60 * 24;
ret += (year * 60 * 60 * 24 * 365);
return (ret);
}
//-----------------------------------------------------------------------------
struct loki_time tick_to_time(uint32_t tick) {
struct loki_time ret;
uint32_t days, rem;
uint32_t y;
char *ip;
days = tick / SECS_PER_DAY;
rem = tick % SECS_PER_DAY;
while (rem < 0) {
rem += SECS_PER_DAY;
--days;
}
while (rem >= SECS_PER_DAY) {
rem -= SECS_PER_DAY;
++days;
}
ret.hour = rem / SECS_PER_HOUR;
rem %= SECS_PER_HOUR;
ret.min = rem / 60;
ret.sec = rem % 60;
y = 1970;
while (days >= (rem = __isleap(y) ? 366 : 365)) {
++y;
days -= rem;
}
while (days < 0) {
--y;
days += __isleap(y) ? 366 : 365;
}
ret.year = y;
ip = (char *)__mon_lengths[__isleap(y)];
for (y = 0; days >= ip[y]; ++y)
days -= ip[y];
ret.mon = y + 1;
ret.day = days + 1;
return (ret);
}
//-----------------------------------------------------------------------------
int rtc_start_interval_event(const struct rtc *device, enum rtc_interval interval,
const void *callback, const void *argument) {
if(NULL == device) {
return (-1);
}
rtc_fp_start_interval_event start_interval = device->fp->start_interval;
return (start_interval(device->arch_dep_device, interval, callback, argument));
}
//-----------------------------------------------------------------------------
int rtc_stop_interval_event(const struct rtc *device) {
if(NULL == device) {
return (-1);
}
rtc_fp_stop_interval_event stop_interval = device->fp->stop_interval;
return (stop_interval(device->arch_dep_device));
}
//-----------------------------------------------------------------------------
int rtc_start_alarm_event(const struct rtc *device,
const struct loki_time *alarm_time, enum rtc_alarm_mask alarm_mask,
const void *callback, const void *argument)
{
if(NULL == device) {
return (-1);
}
rtc_fp_start_alarm_event start_alarm = device->fp->start_alarm;
return (start_alarm(device->arch_dep_device, alarm_time, alarm_mask,
callback, argument));
}
//-----------------------------------------------------------------------------
int rtc_stop_alarm_event(const struct rtc *device) {
if(NULL == device) {
return (-1);
}
rtc_fp_stop_alarm_event stop_alarm = device->fp->stop_alarm;
return (stop_alarm(device->arch_dep_device));
}

View File

@@ -0,0 +1,161 @@
//! \file rtc.h
//! \author tkl
//! \date Jul 8, 2012
//! \brief Header file of the architecture independent rtc implementation.
#ifndef RTC_H_
#define RTC_H_
#include <stdint.h>
//-----------------------------------------------------------------------------
//! \brief Loki time container.
struct loki_time {
uint32_t sec; //!< Seconds. [0-60]
uint32_t min; //!< Minutes. [0-59]
uint32_t hour; //!< Hours. [0-23]
uint32_t day; //!< Day. [1-31]
uint32_t mon; //!< Month. [0-11]
uint32_t year; //!< Year.
};
//-----------------------------------------------------------------------------
//! \brief Rtc interrupt interval.
enum rtc_interval {
RTC_INTERVAL_MINUTE = 0, //!< rtc interrupt every minute.
RTC_INTERVAL_HOUR, //!< rtc interrupt every hour.
RTC_INTERVAL_MIDNIGHT, //!< rtc interrupt every day at 00:00.
RTC_INTERVAL_NOON //!< rtc interrupt every day at 12:00.
};
//-----------------------------------------------------------------------------
//! \brief Rtc alarm mask.
enum rtc_alarm_mask {
RTC_ALARM_DISABLED = 0x00, //!< Alarm mask disabled.
RTC_ALARM_MINUTE = 0x01, //!< Alarm mask minute.
RTC_ALARM_HOUR = 0x02, //!< Alarm mask hour.
RTC_ALARM_DAY_OF_WEEK = 0x04, //!< Alarm mask day of week.
RTC_ALARM_DAY_OF_MONTH = 0x08 //!< Alarm mask day of month.
};
//-----------------------------------------------------------------------------
//! Callback for the open function of the arch depended rtc driver.
typedef int (*rtc_fp_open_t)(const void*);
//! Callback for the close function of the arch depended rtc driver.
typedef int (*rtc_fp_close_t)(const void*);
//! Callback for the time set function of the arch depended rtc driver.
typedef void (*rtc_fp_set_time_t)(const void*, const struct loki_time*);
//! Callback for the time get function of the arch depended rtc driver.
typedef struct loki_time *(*rtc_fp_get_time_t)(const void*, struct loki_time*);
//! Callback for the start interval event function.
typedef int (*rtc_fp_start_interval_event)
(const void *, enum rtc_interval, const void *, const void *);
//! Callback for the stop interval event.
typedef int (*rtc_fp_stop_interval_event)(const void *);
//! Callback for the start alarm event.
typedef int (*rtc_fp_start_alarm_event)
(const void *, const struct loki_time*, enum rtc_alarm_mask, const void *, const void *);
//! Callback for the stop alarm event.
typedef int (*rtc_fp_stop_alarm_event)(const void *);
//-----------------------------------------------------------------------------
//! \brief Function pointer to access the rct driver.
struct rtc_fp {
const rtc_fp_open_t open; //!< Function pointer to the open function.
const rtc_fp_close_t close; //!< Function pointer to the close function.
const rtc_fp_set_time_t set_time; //!< Function pointer to the set_time function.
const rtc_fp_get_time_t get_time; //!< Function pointer to the get_time function.
const rtc_fp_start_interval_event start_interval; //!< Function pointer to the start_interval function.
const rtc_fp_stop_interval_event stop_interval; //!< Function pointer to the stop_interval function.
const rtc_fp_start_alarm_event start_alarm; //!< Function pointer to the start_alarm function.
const rtc_fp_stop_alarm_event stop_alarm; //!< Function pointer to the stop_alarm function.
};
//-----------------------------------------------------------------------------
//! \brief Contains the architecture depended device and the access functions to the rtc driver.
struct rtc {
const void *arch_dep_device; //!< Architecture depended rtc device (i.e. msp430_rtc_t).
const struct rtc_fp *fp; //!< Function pointer for the rtc driver access.
};
//-----------------------------------------------------------------------------
//! \brief Open a rtc device.
//! \param device The rtc device to open.
//! \retval -1 in error case.
int rtc_open(const struct rtc *device);
//-----------------------------------------------------------------------------
//! \brief Close a rtc device.
//! \param device The rtc device to close.
//! \retval -1 in error case.
int rtc_close(const struct rtc *device);
//-----------------------------------------------------------------------------
//! \brief Set rtc time.
//! \param device The rtc device.
//! \param time The time to set.
void rtc_set_time(const struct rtc *device, const struct loki_time *time);
//-----------------------------------------------------------------------------
//! \brief Get rtc time.
//! \param device The rtc device.
//! \param time The time to get.
//! \retval NULL in error case, otherwise points to time.
struct loki_time *rtc_get_time(const struct rtc *device,
struct loki_time *time);
//-----------------------------------------------------------------------------
//! \brief Converts a Unix timestamp to a loki_time.
//! \param tick The Unix timestamp.
//! \return the converted time.
struct loki_time tick_to_time(uint32_t tick);
//-----------------------------------------------------------------------------
//! \brief Converts a loki_time to aunix timestamp.
//! \param time The time to convert.
//! \return The converted unix timestamp.
uint32_t time_to_tick(const struct loki_time *time);
//-----------------------------------------------------------------------------
//! \brief Starts a rtc interval event.
//! On every occurance of interval the rtc event gets fired.
//! \param device The rtc device.
//! \param interval The interval of rtc event occurance.
//! \param callback The callback is executed in case of interval event.
//! \param argument The argument for the callback.
//! \retval -1 in error case.
int rtc_start_interval_event(const struct rtc *device, enum rtc_interval interval,
const void * callback, const void *argument);
//-----------------------------------------------------------------------------
//! \brief Stops a rtc interval event.
//! \param device The rtc device.
//! \retval -1 in error case.
int rtc_stop_interval_event(const struct rtc *device);
//-----------------------------------------------------------------------------
//! \brief Starts a rtc alarm event.
//! \param device The rtc device.
//! \param alarm_time The time to fire the alarm.
//! \param alarm_mask The mask to filter the alarm time.
//! \param callback The callback is executed in case of alarm.
//! \param argument The argument for the callback.
//! \retval -1 in error case.
int rtc_start_alarm_event(const struct rtc *device,
const struct loki_time *alarm_time, enum rtc_alarm_mask alarm_mask,
const void * callback, const void *argument);
//-----------------------------------------------------------------------------
//! \brief Stops a rtc alarm.
//! \param device The rtc device.
//! \retval -1 in error case.
int rtc_stop_alarm_event(const struct rtc *device);
#endif /* RTC_H_ */

View File

@@ -0,0 +1,51 @@
//! \file spi.c
//! \author tkl
//! \date Feb 11, 2012
//! \brief Source file of the architecture independent spi driver.
#include <stddef.h>
#include "spi.h"
//-----------------------------------------------------------------------------
int spi_open(const struct spi *device) {
if(device == NULL) {
return -1;
}
spi_fp_open_t open = device->fp->open;
return open(device->arch_dep_device);
}
//-----------------------------------------------------------------------------
int spi_close(const struct spi *device) {
if(device == NULL) {
return -1;
}
spi_fp_close_t close = device->fp->close;
return close(device->arch_dep_device);
}
//-----------------------------------------------------------------------------
void spi_assert_cs(const struct spi * device) {
if(device == NULL) {
return;
}
spi_fp_assert_cs_t assert_cs = device->fp->assert_cs;
assert_cs(device->arch_dep_device);
}
//-----------------------------------------------------------------------------
void spi_deassert_cs(const struct spi * device) {
if(device == NULL) {
return;
}
spi_fp_deassert_cs_t deassert_cs = device->fp->deassert_cs;
deassert_cs(device->arch_dep_device);
}
//-----------------------------------------------------------------------------
char spi_rxtx_byte(const struct spi * device, char byte) {
if(device == NULL) {
return 0;
}
spi_fp_rxtx_byte_t rxtx_byte = device->fp->rxtx_byte;
return rxtx_byte(device->arch_dep_device, byte);
}

View File

@@ -0,0 +1,75 @@
//! \file spi.h
//! \author tkl
//! \date Feb 11, 2012
//! \brief Header file of the architecture independent spi driver.
#ifndef SPI_H_
#define SPI_H_
//-----------------------------------------------------------------------------
//! \brief Function pointer to the open function.
typedef int (*spi_fp_open_t)(const void *);
//-----------------------------------------------------------------------------
//! \brief Function pointer to the close function.
typedef int (*spi_fp_close_t)(const void *);
//-----------------------------------------------------------------------------
//! \brief Function pointer to the assert chip select function.
typedef void (*spi_fp_assert_cs_t)(const void *);
//-----------------------------------------------------------------------------
//! \brief Function pointer to the deassert chip select function.
typedef void (*spi_fp_deassert_cs_t)(const void *);
//-----------------------------------------------------------------------------
//! \brief Function pointer to the receive / transmit function.
typedef char (*spi_fp_rxtx_byte_t)(const void *, char);
//-----------------------------------------------------------------------------
//! \brief Contains the function pointer to access the spi driver.
struct spi_fp {
const spi_fp_open_t open; //!< Function pointer to the open function.
const spi_fp_close_t close; //!< Function pointer to the close function.
const spi_fp_assert_cs_t assert_cs; //!< Function pointer to the assert chip select function.
const spi_fp_deassert_cs_t deassert_cs; //!< Function pointer to the deassert chip select function.
const spi_fp_rxtx_byte_t rxtx_byte; //!< Function pointer to the receive / transmit function.
};
//-----------------------------------------------------------------------------
//! \brief Contains the architecture depended device and the access functions to the spi driver.
struct spi {
const void *arch_dep_device; //!< Architecture depended spi device (i.e. stm32f10x_spi_t).
const struct spi_fp *fp; //!< Function pointer for the spi driver access.
};
//-----------------------------------------------------------------------------
//! \brief Open spi device.
//! \param device The spi to open.
int spi_open(const struct spi *device);
//-----------------------------------------------------------------------------
//! \brief Close spi device.
//! \param device The spi to close.
//! \retval -1 in error case.
int spi_close(const struct spi *device);
//-----------------------------------------------------------------------------
//! \brief Assert the spi's chip select.
//! \param device The spi to assert.
//! \retval -1 in error case.
void spi_assert_cs(const struct spi * device);
//-----------------------------------------------------------------------------
//! \brief Deassert the spi'schip select.
//! \param device The spi to deassert.
//! \retval -1 in error case.
void spi_deassert_cs(const struct spi * device);
//-----------------------------------------------------------------------------
//! \brief rx/tx byte over spi.
//! \param device The spi to act on.
//! \param byte The data to transmit.
//! \return The received data
char spi_rxtx_byte(const struct spi * device, char byte);
#endif /* SPI_H_ */

View File

@@ -0,0 +1,139 @@
/*
* ssd1306.c
*
* Created on: Jan 13, 2016
* Author: tkl
*/
#include <string.h>
#include "i2c.h"
#include "font.h"
#include "bitmap.h"
#include "ssd1306.h"
#define MAX_COLUMN 128
#define MAX_ROW 64
#define PAGE_COUNT 8
#define HEAD_PAGE_COUNT 2
#define BODY_PAGE_COUNT PAGE_COUNT - HEAD_PAGE_COUNT
#define CMD_MODE 0x80
#define DATA_MODE 0x40
#define COLUMN_ADDR 0x21
#define PAGE_ADDR 0x22
#define DISPLAY_OFF 0xAE
static const char init_sequence[] = {
0x80, 0xAE, 0x80, 0x20, 0x80, 0x10, 0x80, 0xb0, 0x80, 0xc8, 0x80, 0x00,
0x80, 0x10, 0x80, 0x40, 0x80, 0x81, 0x80, 0x7f, 0x80, 0xa1, 0x80, 0xa6,
0x80, 0xa8, 0x80, 0x3f, 0x80, 0xa4, 0x80, 0xd3, 0x80, 0x00, 0x80, 0xd5,
0x80, 0xf0, 0x80, 0xd9, 0x80, 0x22, 0x80, 0xda, 0x80, 0x12, 0x80, 0xdb,
0x80, 0x20, 0x80, 0x8d, 0x80, 0x14, 0x80, 0xaf
};
static int set_column(const struct ssd1306 *dev, char column)
{
char buf[6];
buf[0] = CMD_MODE;
buf[1] = COLUMN_ADDR;
buf[2] = CMD_MODE;
buf[3] = column;
buf[4] = CMD_MODE;
buf[5] = MAX_COLUMN - 1;
return i2c_write(dev->dev, dev->i2c_addr, buf, 6);
}
static int set_page(const struct ssd1306 *dev, char page)
{
char buf[6];
buf[0] = CMD_MODE;
buf[1] = PAGE_ADDR;
buf[2] = CMD_MODE;
buf[3] = page;
buf[4] = CMD_MODE;
buf[5] = MAX_ROW / PAGE_COUNT - 1;
return i2c_write(dev->dev, dev->i2c_addr, buf, 6);
}
static int clear_head(const struct ssd1306 *dev)
{
int ret, i, j;
char buf[2];
ret = set_column(dev, 0);
ret |= set_page(dev, 0);
buf[0] = DATA_MODE;
buf[1] = 0x00;
for(i = 0; i < HEAD_PAGE_COUNT; i++)
for(j = 0; j < MAX_COLUMN; j++)
ret |= i2c_write(dev->dev, dev->i2c_addr, buf, 2);
return ret;
}
static int clear_body(const struct ssd1306 *dev)
{
int ret, i, j;
char buf[2];
ret = set_column(dev, 0);
ret |= set_page(dev, 2);
buf[0] = DATA_MODE;
buf[1] = 0x00;
for(i = 0; i < BODY_PAGE_COUNT; i++)
for(j = 0; j < MAX_COLUMN; j++)
ret |= i2c_write(dev->dev, dev->i2c_addr, buf, 2);
return ret;
}
static int clear(const struct ssd1306 *dev)
{
int ret;
ret = clear_head(dev);
ret |= clear_body(dev);
return ret;
}
int ssd1306_open(const struct ssd1306 *dev)
{
int ret;
ret = i2c_open((struct i2c *)dev->dev);
ret = i2c_write(dev->dev, dev->i2c_addr, init_sequence,
sizeof(init_sequence) / sizeof(init_sequence[0]));
ret |= clear(dev);
ret |= set_column(dev, 0);
ret |= set_page(dev, 0);
return ret;
}
int ssd1306_close(const struct ssd1306 *dev)
{
int ret;
char buf[2];
buf[0] = CMD_MODE;
buf[1] = DISPLAY_OFF;
ret = i2c_write(dev->dev, dev->i2c_addr, buf, 2);
ret |= i2c_close((struct i2c *)dev->dev);
return ret;
}
int ssd1306_write(const struct ssd1306 *dev, const struct bitmap *bitmap)
{
int ret, i, len = bitmap->x_size * bitmap->y_size / 8;
char buf[2] = {DATA_MODE, 0x00};
if(len != MAX_COLUMN * PAGE_COUNT)
return -1;
ret = set_column(dev, 0);
ret |= set_page(dev, 0);
for(i = 0; i < len; i++) {
buf[1] = bitmap->buffer[i];
ret |= i2c_write(dev->dev, dev->i2c_addr, buf, 2);
}
return ret;
}

View File

@@ -0,0 +1,22 @@
/*
* ssd1306.h
*
* Created on: Jan 13, 2016
* Author: tkl
*/
#ifndef SSD1306_H_
#define SSD1306_H_
struct ssd1306 {
const struct i2c *dev;
const char i2c_addr;
};
struct bitmap;
int ssd1306_open(const struct ssd1306 *dev);
int ssd1306_close(const struct ssd1306 *dev);
int ssd1306_write(const struct ssd1306 *dev, const struct bitmap *bitmap);
#endif /* SSD1306_H_ */

View File

@@ -0,0 +1,35 @@
//! \file timer.c
//! \author tkl
//! \date Jul 5, 2012
//! \brief Source file of the architecture independent timer implementation.
#include <stddef.h>
#include "timer.h"
int timer_open(const struct loki_timer *device)
{
if(device == NULL)
return -1;
timer_fp_open_t open = device->fp->open;
return open(device->arch_dep_device);
}
int timer_close(const struct loki_timer *device)
{
if(device == NULL)
return -1;
timer_fp_close_t close = device->fp->close;
return close(device->arch_dep_device);
}
int timer_set_it_callback(const struct loki_timer *device, const void *callback,
const void *param)
{
if((device == NULL) || (callback == NULL))
return -1;
timer_fp_set_cb_t set_cb = device->fp->set_cb;
return set_cb(device->arch_dep_device, callback, param);
}

View File

@@ -0,0 +1,43 @@
//! \file timer.h
//! \author tkl
//! \date Jul 4, 2012
//! \brief Header file of the architecture independent timer implementation.
#ifndef TIMER_H_
#define TIMER_H_
typedef int (*timer_fp_open_t)(const void*); //!< callback for the open function of the arch depended timer driver.
typedef int (*timer_fp_close_t)(const void*); //!< callback for the close function of the arch depended timer driver.
typedef int (*timer_fp_set_cb_t)(const void*, const void *, const void *); //!< callback for set_callback function of the arch depended timer driver.
//! \brief The access function pointer for the timer.
struct timer_fp {
const timer_fp_open_t open; //!< Function pointer to the open function.
const timer_fp_close_t close; //!< Function pointer to the close function.
const timer_fp_set_cb_t set_cb; //!< Function pointer to the set_callback function.
};
//! \brief Contains the architecture depended device informations.
struct loki_timer {
const void *arch_dep_device; //!< Architecture depended timer device (i.e. msp430_timer).
const struct timer_fp *fp; //!< Function pointer to the architectur depended timer driver access.
};
//! \brief Open a timer timer
//! \param device The timer device to open.
//! \retval -1 in error case.
int timer_open(const struct loki_timer *device);
//! \brief Close a timer timer
//! \param device The timer device to close.
//! \retval -1 in error case.
int timer_close(const struct loki_timer *device);
//! \brief Set the callback for a sys tick timer
//! \param device The timer device to set the callback for.
//! \param callback The function pointer to call back.
//! \param param The parameter for the call back.
//! \retval -1 in error case.
int timer_set_it_callback(const struct loki_timer *device, const void *callback,
const void *param);
#endif /* TIMER_H_ */

View File

@@ -0,0 +1,78 @@
//! \file uart.c
//! \author tkl
//! \date Jul 15, 2012
//! \brief Source file of the architecture independent uart implementation.
#include <stddef.h>
#include <stdbool.h>
#include "ringbuffer.h"
#include "irq.h"
#include "stack.h"
#include "queue.h"
#include "thread.h"
#include "schedule.h"
#include "uart.h"
extern volatile struct thread_context *current_thread;
static const void *uart_it_callback(const void *param);
int uart_open(const struct uart *this)
{
if(NULL == this)
return (-1);
int ret = this->fp->set_cb(this, uart_it_callback, this);
ret |= this->fp->open(this->arch_dep_device);
return (ret);
}
int uart_close(const struct uart *this)
{
if(NULL == this)
return (-1);
blocking_read_wakeup((const void *)this);
return (this->fp->close(this->arch_dep_device));
}
int uart_read(const struct uart *this, char *buffer, int len)
{
int ret = -1;
unsigned int irq;
if(NULL == this)
return (-1);
irq = disable_irq();
if(ringbuffer_is_empty(this->buffer)) {
current_thread->status = THREAD_STATUS_BLOCKING;
current_thread->wakeup_blocking_source = (void*) this;
restore_irq(irq);
schedule();
}
ret = ringbuffer_read(this->buffer, buffer, len);
restore_irq(irq);
return ret;
}
int uart_write(const struct uart *this, const char *buffer, int len)
{
if(NULL == this)
return (-1);
return (this->fp->write(this->arch_dep_device, buffer, len));
}
static const void *uart_it_callback(const void *param)
{
if(NULL == param)
return (NULL);
char c = 0;
struct uart *uart = (struct uart *)param;
uart->fp->read((const void *)uart->arch_dep_device, &c, 1);
if(!ringbuffer_is_full(uart->buffer))
ringbuffer_write(uart->buffer, &c, 1);
blocking_read_wakeup(param);
return (param);
}

View File

@@ -0,0 +1,63 @@
//! \file uart.h
//! \author tkl
//! \date Jul 15, 2012
//! \brief Header file of the architecture independent uart implementation.
#ifndef UART_H_
#define UART_H_
//! Function pointer for the uart open function.
typedef int (*uart_fp_open_t)(const void*);
//! Function pointer for the uart close function.
typedef int (*uart_fp_close_t)(const void*);
//! Function pointer for the uart read function.
typedef int (*uart_fp_read_t)(const void*, char*, int);
//! Function pointer for the uart write function.
typedef int (*uart_fp_write_t)(const void*, const char*, int);
//! Function pointer for the uart set callback function.
typedef int (*uart_fp_set_cb_t)(const void*, const void *, const void*);
//! \brief Uart access.
struct uart_fp {
const uart_fp_open_t open; //!< Open.
const uart_fp_close_t close; //!< Close.
const uart_fp_read_t read; //!< Read.
const uart_fp_write_t write; //!< Write.
const uart_fp_set_cb_t set_cb; //!< Set callback.
};
//! \brief Uart driver object.
struct uart {
const void *arch_dep_device; //!< Architecture depended uart device.
const struct uart_fp *fp; //!< Uart access.
struct ringbuffer *buffer; //!< Intermediate buffer.
};
//! \brief Open an uart device.
//! \param this The uart device to open.
//! \retval -1 in error case.
int uart_open(const struct uart *this);
//! \brief Close an uart device.
//! \param this The uart device to close.
//! \retval -1 in error case.
int uart_close(const struct uart *this);
//! \brief Read from an uart device.
//! \param this The uart device to read from.
//! \param buffer The buffer to read to.
//! \param len The size of the buffer.
//! \retval -1 in error case, otherwise the number of read characters.
int uart_read(const struct uart *this, char *buffer, int len);
//! \brief Write to an uart device.
//! \param this The uart device to write to.
//! \param buffer The buffer to write.
//! \param len The number of characters to write.
//! \retval -1 in error case, otherwise the number of written characters.
int uart_write(const struct uart *this, const char *buffer, int len);
#endif /* UART_H_ */