From 4e1ce86af16307bf7d42657db07600867c7c4bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Thu, 30 Jun 2016 17:18:49 +0200 Subject: Add some more progress and split non-core functionality to separate repo More progress to implementation and some changes in project it self. This library will implement only drivers for features on chip but nothing else. Everything connected externally is now in separate repository. --- .gitignore | 4 + Makefile | 7 +- README.md | 4 +- docs/index.md | 10 +-- docs/miscelaneous/jobs_vs_tasks.md | 17 ++++ docs/miscellanoues/jobs_vs_tasks.md | 17 ---- docs/parts/ioport.md | 167 ------------------------------------ docs/parts/jobs.md | 15 ---- docs/parts/spi.md | 80 ----------------- docs/parts/tasks.md | 38 -------- docs/parts/timer.md | 0 docs/parts/usart.md | 23 ----- docs/parts/utils/buffer.md | 105 ----------------------- docs/parts/utils/narray.md | 64 -------------- docs/references/ioport.md | 167 ++++++++++++++++++++++++++++++++++++ docs/references/jobs.md | 15 ++++ docs/references/spi.md | 80 +++++++++++++++++ docs/references/tasks.md | 38 ++++++++ docs/references/timer.md | 0 docs/references/usart.md | 23 +++++ docs/references/utils/buffer.md | 105 +++++++++++++++++++++++ docs/references/utils/narray.md | 64 ++++++++++++++ examples/blink/blink.c | 6 +- examples/blink/build/config.h | 1 + examples/dht22usart/dht22.c | 26 ------ examples/dht22usart/makefile | 40 --------- include/adc.h | 1 + include/eeprom.h | 2 + include/error.h | 3 + include/jobs.h | 8 ++ include/sensors/dht22.h | 42 --------- include/spi.h | 10 +-- include/sys/mutex.h | 32 +++++++ include/sys/semaphore.h | 31 +++++++ include/tasks.h | 56 ++---------- include/timer.h | 1 + include/usi-spi.h | 66 ++++++++++++++ include/usi-twi.h | 0 include/utils/timers_div.h | 11 +-- include/wireless/nrf24l01p.h | 0 mkdocs.yml | 20 ++--- src/can/mcp2515.c | 52 ----------- src/sensors/dht22.c | 117 ------------------------- src/tasks.c | 9 -- 44 files changed, 695 insertions(+), 882 deletions(-) create mode 100644 docs/miscelaneous/jobs_vs_tasks.md delete mode 100644 docs/miscellanoues/jobs_vs_tasks.md delete mode 100644 docs/parts/ioport.md delete mode 100644 docs/parts/jobs.md delete mode 100644 docs/parts/spi.md delete mode 100644 docs/parts/tasks.md delete mode 100644 docs/parts/timer.md delete mode 100644 docs/parts/usart.md delete mode 100644 docs/parts/utils/buffer.md delete mode 100644 docs/parts/utils/narray.md create mode 100644 docs/references/ioport.md create mode 100644 docs/references/jobs.md create mode 100644 docs/references/spi.md create mode 100644 docs/references/tasks.md create mode 100644 docs/references/timer.md create mode 100644 docs/references/usart.md create mode 100644 docs/references/utils/buffer.md create mode 100644 docs/references/utils/narray.md create mode 100644 examples/blink/build/config.h delete mode 100644 examples/dht22usart/dht22.c delete mode 100644 examples/dht22usart/makefile create mode 100644 include/eeprom.h create mode 100644 include/error.h create mode 100644 include/jobs.h delete mode 100644 include/sensors/dht22.h create mode 100644 include/sys/mutex.h create mode 100644 include/sys/semaphore.h create mode 100644 include/usi-spi.h create mode 100644 include/usi-twi.h delete mode 100644 include/wireless/nrf24l01p.h delete mode 100644 src/can/mcp2515.c delete mode 100644 src/sensors/dht22.c diff --git a/.gitignore b/.gitignore index fbf84a3..2e0f06f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,9 @@ *.d *.hex *.elf +*.old +html +build trash libioe.a +install.sh diff --git a/Makefile b/Makefile index c17404b..ae17fef 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,4 @@ +# vim:ts=4:sw=4:sts=4:noexpandtab MAKEFLAGS += --no-builtin-rules # Default output path. Can be changed by enviroment to compile to different folder @@ -53,17 +54,17 @@ $(O)/libioe.a: $(OBJ) $(Q)$(AR) -crs $@ $^ $(OBJ): $(O)/build/%.o: src/%.c - $(Q)mkdir -p "$(@D)" + @mkdir -p "$(@D)" @echo " CC $@" $(Q)$(GCC) $(CFLAGS) -c -o $@ $< $(DEP): $(O)/build/%.d: src/%.c $(O)/build/config.h - $(Q)mkdir -p "$(@D)" + @mkdir -p "$(@D)" @echo " DEP $@" $(Q)$(CPP) -MM -MG -MT '$(O)/build/$*.o $@' $(CFLAGS) -o $@ $< $(O)/build/config.h: $(CONFIG) - $(Q)mkdir -p "$(@D)" + @mkdir -p "$(@D)" @echo " GEN $@" $(Q)grep -v "^#" $(CONFIG) | grep "CONFIG_" | sed -e 's/="\(.*\)"/=\1/;s/=/ /;s/^/#define /' > $@ diff --git a/README.md b/README.md index fd3eed6..a1c3b6d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -#AVR-IOE +# AVR-IOE [![Build Status](https://travis-ci.com/Cynerd/avr-ioe.svg?token=j1mJhmD5jiZyJQGyPUkt&branch=master)](https://travis-ci.com/Cynerd/avr-ioe) -##AVR input/output expansion +## AVR input/output expansion This is library that implements different input/output interfaces and other utilities. Its target is to create simple and highly configurable pack of code for diff --git a/docs/index.md b/docs/index.md index c39df46..23b6cb9 100644 --- a/docs/index.md +++ b/docs/index.md @@ -6,8 +6,8 @@ Parts Whole library consists from set of parts. You can use any part directly or only trough other part. Every part can be enabled or disabled. -| Parts | Header | Description | -|-------------------------------|----------|--------------------------------------------------------| -| [IO Ports](parts/ioport.md) | ioport.h | Simple input/output port access | -| [SPI](parts/spi.md) | spi.h | Serial peripheral interface | -| [USART](parts/usart.md) | usart.h | Universal synchronous/asynchronous receive/transmitter | +| Parts | Header | Description | +|----------------------------------|----------|--------------------------------------------------------| +| [IO Ports](references/ioport.md) | ioport.h | Simple input/output port access | +| [SPI](references/spi.md) | spi.h | Serial peripheral interface | +| [USART](references/usart.md) | usart.h | Universal synchronous/asynchronous receive/transmitter | diff --git a/docs/miscelaneous/jobs_vs_tasks.md b/docs/miscelaneous/jobs_vs_tasks.md new file mode 100644 index 0000000..17d7fdd --- /dev/null +++ b/docs/miscelaneous/jobs_vs_tasks.md @@ -0,0 +1,17 @@ +Jobs vs. Tasks +============== +This document refers to [Jobs](/parts/jobs.md) and [Tasks](/parts/tasks.md). + +You should be familiar with threads from other platforms. Tasks are from usability +point of view almost same. They are switched according their priority and +availability. Jobs are different. They are designed to execute single function at +the time and when this function exits it executes another. It cant interrupt +execution when more important task come. So why use jobs instead of tasks? Task +requires for their run stack memory and during whole live of task is this memory +taken. This limits number of tasks running. Jobs are sharing same stack and only +one function has data on it at the time. This results in less memory consumption. +Another huge difference is how planing works. Tasks are planned based on priority. +Task with higher priority will run unless it isn't suspended. Jobs don't have +priority, but they have to specify time until they should be finished (deadline) +and duration of execution. With these two parameters jobs planner can plan their +execution. diff --git a/docs/miscellanoues/jobs_vs_tasks.md b/docs/miscellanoues/jobs_vs_tasks.md deleted file mode 100644 index 17d7fdd..0000000 --- a/docs/miscellanoues/jobs_vs_tasks.md +++ /dev/null @@ -1,17 +0,0 @@ -Jobs vs. Tasks -============== -This document refers to [Jobs](/parts/jobs.md) and [Tasks](/parts/tasks.md). - -You should be familiar with threads from other platforms. Tasks are from usability -point of view almost same. They are switched according their priority and -availability. Jobs are different. They are designed to execute single function at -the time and when this function exits it executes another. It cant interrupt -execution when more important task come. So why use jobs instead of tasks? Task -requires for their run stack memory and during whole live of task is this memory -taken. This limits number of tasks running. Jobs are sharing same stack and only -one function has data on it at the time. This results in less memory consumption. -Another huge difference is how planing works. Tasks are planned based on priority. -Task with higher priority will run unless it isn't suspended. Jobs don't have -priority, but they have to specify time until they should be finished (deadline) -and duration of execution. With these two parameters jobs planner can plan their -execution. diff --git a/docs/parts/ioport.md b/docs/parts/ioport.md deleted file mode 100644 index 96a4b82..0000000 --- a/docs/parts/ioport.md +++ /dev/null @@ -1,167 +0,0 @@ -IO port -======= -And define: `CONFIG_IOE_IOPORT` - -Defines simple access to io ports. This allows runtime access to any pin with just -serialized identifier. - -Most of the functions has group and mask arguments. Group is letter, but in this -library is represented as number and exact number is defined as macro definition. -Mask is one shifted by index number(use \_BV macro). This way can be -addressed all IO ports. Exact identification consult with datasheet. You can also -use more than one index number and control more ports in single group with single -command. - -WARNING: No check is implemented for right group number. Usage of unsupported -value is undefined (write to other parts of memory can happen). - -Configuration -------------- -To use this part, you must enable `CONFIG_IOPORTS` option. -This part also handles pin change interrupts. Enable it using -`CONFIG_IOPORTS_PCINT` option. - -References ----------- -### For output -#### Function io_setout -```C -static inline void io_setout(uint8_t group, uint8_t mask) -``` -Configures port of `group` with `mask` as output. -Parameters: -__group__ - Character specifying exact port group -__mask__ - Binary shifted 1. Shift is equal to port index in specified group. - -#### Function io_hight -```C -static inline void io_hight(uint8_t group, uint8_t mask) -``` -Sets output port to hight (also can be called as 1). -WARNING: Invoke this only if io_setout is called before. - -Parameters: -__group__ - Character specifying exact port group -__mask__ - Binary shifted 1. Shift is equal to port index in specified group. - -#### Function io_low -```C -static inline void io_low(uint8_t group, uint8_t mask) -``` -Sets output port to low (also called as 0). -WARNING: Invoke this only if io_setout is called before. - -Parameters: -__group__ - Number specifying exact port group -__mask__ - Binary shifted 1. Shift is equal to port index in specified group. - -#### Function io_set -```C -static inline void io_set(uint8_t group, uint8_t mask, int8_t val) -``` -Sets output port to value passed as argument. -WARNING: Invoke this only if io_setout is called before. -Parameters: -__group__ - Number specifying exact port group -__mask__ - Binary shifted 1. Shift is equal to port index in specified group. - -### For input -#### Function io_setin -```C -static inline void io_setin(uint8_t group, uint8_t mask, - enum ioeIOInResistor resistor) -``` -Configures port of `group` with `mask` as input with specified pull-up/down -resistor. - -Parameters: -__group__ - Number specifying exact port group -__mask__ - Binary shifted 1. Shift is equal to port index in specified group. - -#### Function io_get -```C -static inline int8_t io_get(uint8_t group, uint8_t mask) -``` -Returns current value of port. Note that you can use this also if port is -configured as output. - -Parameters: -__group__ - Number specifying exact port group -__mask__ - Binary shifted 1. Shift is equal to port index in specified group. - -#### Enum ioeIOInResistor -```C -enum ioeIOInResistor { - IOE_IO_PULLUP, - IOE_IO_PULLDOWN -}; -``` -This enum is used as argument for io_setin. Names should be self explanatory -in this case. - -### Pin change interrupts -#### Function io_change_sethook -```C -int8_t io_change_sethook(uint8_t group, uint8_t mask, uint8_t edge, - void (*change) (uint8_t group, uint8_t mask)) -``` -Registers function `change` as hook for pin change interrupt. `group` and `mask` -specifies port and edge specifies on what edge should hook be called. `edge` can -be IO_RISING or IO_FALLING or their binary combination with operator -`|`. -WARNING: `change` call is call during interrupt handling. You shouldn't be -blocking execution for long time. - -Parameters: -__group__ - Number specifying exact port group. -__mask__ - Binary shifted 1. Shift is equal to port index in specified group. -__edge__ - Signals on what edge should be hook called. -__change__ - Pointer to function used as interupt hook. - -#### Function io_change_remhook -```C -int8_t io_change_remhook(void (*change) (uint8_t group, uint8_t mask)) -``` -Removes `change` hook. - -Parameters: -__change__ - Pointer to function used as hook - -### Others -#### Definitions IO_{GROUP} -This defines exact numbers related to data-sheet groups. Always use these -definition not direct numbers, you can ensure cross MCU support this way. - -#### Definitions IO_{GROUP}{INDEX} -Because specifying group and mask as separate parameters is not always optimal, -mcu support file should define all ports in form of single line definition in -format `IOE_IO_{GROUP}{INDEX}`. Disadvantage is that with these definitions you -can't use binary conjunction and so only one pin can be controlled with it. - -Relevant examples ------------------ -* blink -* pcinterrupt - -Adding support --------------- -For more information on how add support, see [Adding MCU support](/add_support.md). -Main definition is `MCUSUPPORT_IOPORT`. Define it to enable support. - -### IO_{GROUP} -This should define any number that is handy for implementation of `IOE_IOE_PIN`, -`IOE_IO_DDR` and `IOE_IO_PORT`. - -### IO_{PIN/DDR/PORT} -These should calculate exact pointers to register `PORT{GROUP}` for PORT variant -and likewise for others with group as argument. Common implementation would be -like this: -```C -#define IOE_IO_PIN(GROUP) (* (volatile uint8_t *)(PINB + 0x3*GROUP)) -``` -### IO_{GROUP}{INDEX} -This should be pair of group and one binary shifted to left relative to index. - -### MCUSUPPORT_PCINT{NUM} -This defines that MCU supports specific pin change interrupt group. Also it -defines value that is number of pins in group. diff --git a/docs/parts/jobs.md b/docs/parts/jobs.md deleted file mode 100644 index 878a557..0000000 --- a/docs/parts/jobs.md +++ /dev/null @@ -1,15 +0,0 @@ -Jobs -==== -Jobs allows periodic execution of different short functions. It is designed to host -control loops. So called functions should be short. - -Every job must specify deadline and if not set otherwise also its duration. - -If tasks support is enabled jobs can be also executed on multiple tasks, which is -handy if you divide sensor reading and control algorithm, because control -algorithm can than run when for example mcu waiting for response from sensor. -Always be sure that jobs are running on tasks with highest priority, otherwise -deadlines might not be fulfilled every time. - -Be aware of taking mutexes and semaphores. It can sometime result in long task -suspension and that would result to deadline misses. diff --git a/docs/parts/spi.md b/docs/parts/spi.md deleted file mode 100644 index f63d304..0000000 --- a/docs/parts/spi.md +++ /dev/null @@ -1,80 +0,0 @@ -Serial peripheral interface -=========================== -To use include: `spi.h` -This interface is link to MOSI and MISO pins. Also SS pin is used when slave mode -initialized. - -Configuration -------------- -To use SPI you must enable `CONFIG_SPI` configuration symbol. - -References ----------- -### Function spi\_init -```C -static inline void spi_init(enum spiMode mode) -``` -Initializes SPI interface. -Parameters: - mode - Specify mode of SPI interface - -NOTE: Global interrupts must be enabled for right function of SPI. - -### Function spi\_busy -```C -static inline int8_t spi_busy(void) -``` -Returns NULL when device is not busy. -When device is busy return values in non-zero. - -### Function spi\_join -```C -static inline void spi_join(void) -``` -Blocks processor until device is not busy. - -### Function spi\_send -```C -static inline uint8_t spi_send(uint8_t data) -``` -Swap bytes with slave over SPI. -This function blocks execution until device isn't busy (transfer completed). -WARNING: Invoke this only when interface is initialized in MASTER mode. - -### Function spi\_transfer -```C -static inline void spi_transfer(uint8_t data) -``` -Transfer byte to slave over SPI. -This function isn't blocking execution until transfer is complete. -Always call spi\_join before this function when called outside of spi\_receive(). -WARNING: Invoke this only when interface is initialized in MASTER mode. - -### Function spi\_expose -```C -static inline void spi_expose(uint8_t data) -``` -Expose data for next master request. -Please don't use this when device is busy. -Best place to call this is spi\_receive(). -WARNING: Invoke this only when interface is initialized in SLAVE mode. - -### Function pointer spi\_receive -```C -extern void (*spi_receive)(uint8_t data) -``` -This function is called every time transfer is finished. -And until return from this function interrupts are disabled. - -### Enum spiMode -```C -enum spiMode { - SPI_MODE_MASTER, - SPI_MODE_SLAVE -}; -``` -This is used as parameter for spi\_init function. - -Relevant examples ------------------ -* spiblink diff --git a/docs/parts/tasks.md b/docs/parts/tasks.md deleted file mode 100644 index e4f71df..0000000 --- a/docs/parts/tasks.md +++ /dev/null @@ -1,38 +0,0 @@ -Tasks -===== -Tasks can be used for sharing processor for example during period of waiting for -interrupt. Planing is based on priority. - -## Functions -### tasks_run -```C -int8_t tasks_run(void) -``` -This function starts tasks execution. Should be called after all task slots are -prepared and at least one task is started. This function exits if no task exist or -all existing tasks exited. - -### task_init - -## Structures -### Task -```C -typedef struct { - uint8_t flags; - void (*func) (void); -} Task; -``` - -### TaskSlot -```C -typedef struct { - uint8_t flags; - Task *task; - uint8_t stack_size; - uint8_t stack[]; -} TaskSlot; -``` - -### Mutex - -### Semaphore diff --git a/docs/parts/timer.md b/docs/parts/timer.md deleted file mode 100644 index e69de29..0000000 diff --git a/docs/parts/usart.md b/docs/parts/usart.md deleted file mode 100644 index 22e54ab..0000000 --- a/docs/parts/usart.md +++ /dev/null @@ -1,23 +0,0 @@ -Universal synchronous/asynchronous receive/transmitter -====================================================== -This part acts as UART intended as text base interface with computer. It is -using hardware termed as USART by Atmel. This hardware also supports synchronous -communication and can behave as SPI master, but this is not supported by this -library (I don't require this feature, but implementation is welcomed). - -This part implements, if enabled, whole stack for binding input and output to -stdin and stdout. This is handy during development. You can use `printf` and -`scanf` directly. - -This part can be enabled by `CONFIG_USART` configuration option. This enables -more detailed configuration in sub-menu. - -## Usage - - -## References -### usart_init_async -```C -void usart_init_async(void) -``` - diff --git a/docs/parts/utils/buffer.md b/docs/parts/utils/buffer.md deleted file mode 100644 index 4365c25..0000000 --- a/docs/parts/utils/buffer.md +++ /dev/null @@ -1,105 +0,0 @@ -buffer -====== -In whole project are used ring buffers. This is so common that handling them is -exported to separated header file. In fact it contains two implementations of -buffers. One implementation is defined using preprocessor macros. Another one is -plain C code. - -## Preprocessor implementation -Preprocessor implementation is expected to be faster and less memory intense. Also -more suitable for buffers of simple types (such us int). But this buffer has to be -defined with known size at compile time and can't be resized. All this makes it -good implementation for IO devices input and output buffers, but less for more -complicated tasks containing for example sorting. - -### Macros -#### IOEBUFFER -```C -IOEBUFFER(type, name, size) -``` -This macro defines buffer. Use it to define new buffer. -##### Parameters -| type | Specify type of elements in buffer. Can be any valid C type | -| name | Name of buffer. This is C variable name. | -| size | Numerical value of buffer size | - -#### IOEBUFFER_INIT -```C -IOEBUFFER_INIT(name, size) -``` -Use this to initialize buffer. Parameter `size` must be same as for IOEBUFFER -(definition of buffer). You can use this also to reset buffer (remove all data -from buffer). -##### Parameters -| name | Name if buffer. This is C variable name. | -| size | Numerical value of buffer size | - -#### IOEBUFFER_PUT -```C -IOEBUFFER_PUT(name, size, data, mode) -``` -Use this macro to add new data to buffer. -##### Parameters -| name | Name of buffer. This is C variable name. | -| size | Numerical value of buffer size | -| data | Data to be stored to buffer | -| mode | This specifies action if buffer is full. | -##### Modes -| IOEBUFFER_MODE_BLOCK | Block execution until buffer has some free space. | -| IOEBUFFER_MODE_OVERWRITE | Overwrites oldest added (fist to be read) | -| IOEBUFFER_MODE_DROP | New data won't be stored to buffer if full | - -#### IOEBUFFER_GET -```C -IOEBUFFER_GET(name, size, variable) -``` -Get value from buffer. Value is stored in `variable` (it is variable of same type -as buffer data, not pointer to variable of same type). If no data is in buffer, -variable is set to NULL. -##### Parameters -| name | Name of buffer. This is C variable name. | -| size | Numerical value of buffer size. | -| variable | Variable in which data will be stored in. | - -#### IOEBUFFER_CNT -```C -IOEBUFFER_CNT(name, size, variable) -``` -Counts number of data entries in buffer and saves value to variable. -##### Parameters -| name | Name of buffer. This is C variable name. | -| size | Numerical value of buffer size. | -| variable | Int/Unsigned type variable where data count will be stored. | - -## C implementation -C implementation is more general. Size is defined by calling initialization -function. But it is using general pointers, so it is only able store pointers. - -As buffer identification is used defined type `IOEBuffer`. All functions takes -pointer to this type as parameter. Because of this, it won't be described for -every function. - -### Functions -#### ioebuffer_init -```C -int8_t ioebuffer_init(IOEBuffer *buf, uint8_t size, uint8_t flags) -``` -Initialized buffer and allocates required resources. -##### Parameters -| size | Number of elements to be maximally stored in buffer | -| flags | TODO -##### Flags -TODO - -#### ioebuffer_uninit -```C -void ioebuffer_uninit(IOEBuffer *buf) -``` -Frees all resources allocated during initialization of buffer. Use this every time -you are freeing buffer. - -#### ioebuffer_put -```C -int8_t ioebuffer_put(IOEBuffer *buf, void *data) -``` -Add `data` to buffer diff --git a/docs/parts/utils/narray.md b/docs/parts/utils/narray.md deleted file mode 100644 index 3e64f67..0000000 --- a/docs/parts/utils/narray.md +++ /dev/null @@ -1,64 +0,0 @@ -narray.h -======== -This implements some helping functions for null terminated arrays. Null terminated -arrays are used across whole project because they are simple and don't waste -memory space. Their disadvantage is more complicated implementation of utility -functions and much longer adding time (they are reallocated every time new data -are added). - -This implementation is limited to only contains 255 elements(limited by uint8_t -type). But it should be enough for basic usage. If you need store more than that, -you should use different approach. Also only pointers can be stored. - -To define null terminated array just define pointer to you required pointer and set -it to NULL. Such narray is handled as empty. Example: -```C -int **narray = 0; -``` -After adding some data (in example case data of type `int*`) you can access them -same as if you would working with simple array (`narray[i]`). Last element it such -array is `NULL`. No more valid data are stored after `NULL` (This also means that -`NULL` can't be stored to narray). - -All functions are taking as parameter `void ***array`, which is pointer to null -terminated array. This parameter won't be described in functions. - -## Functions and macros -### narray_add -```C -void narray_add(void ***array, void *data) -``` -Add data to end of the array. This increases size of the array by one (two bytes). -#### Parameters -| data | data to be stored to array | - -### narray_remove -```C -void narray_remove(void ***array, void *data) -``` -Remove specified data from array. This effectively decreases size of the array by -one (two bytes). -#### Parameters -| data | data to be removed from array | - -### narray_size -```C -size_t narray_size(void ***array) -``` -Returns size of array. - -### narray_free -```C -inline void narray_free(void ***array) -``` -Frees whole array if allocated. - -### fornarray -```C -#define fornarray(array, i, data) for (i = 0; (data = array[i]) != 0; i++) -``` -This is macro. It implements simple foreach cycle. -#### Parameters -| array | array it self (void ** not pointer) | -| i | unsigned integer (uint8_t) | -| data | variable which will be used to access data | diff --git a/docs/references/ioport.md b/docs/references/ioport.md new file mode 100644 index 0000000..96a4b82 --- /dev/null +++ b/docs/references/ioport.md @@ -0,0 +1,167 @@ +IO port +======= +And define: `CONFIG_IOE_IOPORT` + +Defines simple access to io ports. This allows runtime access to any pin with just +serialized identifier. + +Most of the functions has group and mask arguments. Group is letter, but in this +library is represented as number and exact number is defined as macro definition. +Mask is one shifted by index number(use \_BV macro). This way can be +addressed all IO ports. Exact identification consult with datasheet. You can also +use more than one index number and control more ports in single group with single +command. + +WARNING: No check is implemented for right group number. Usage of unsupported +value is undefined (write to other parts of memory can happen). + +Configuration +------------- +To use this part, you must enable `CONFIG_IOPORTS` option. +This part also handles pin change interrupts. Enable it using +`CONFIG_IOPORTS_PCINT` option. + +References +---------- +### For output +#### Function io_setout +```C +static inline void io_setout(uint8_t group, uint8_t mask) +``` +Configures port of `group` with `mask` as output. +Parameters: +__group__ - Character specifying exact port group +__mask__ - Binary shifted 1. Shift is equal to port index in specified group. + +#### Function io_hight +```C +static inline void io_hight(uint8_t group, uint8_t mask) +``` +Sets output port to hight (also can be called as 1). +WARNING: Invoke this only if io_setout is called before. + +Parameters: +__group__ - Character specifying exact port group +__mask__ - Binary shifted 1. Shift is equal to port index in specified group. + +#### Function io_low +```C +static inline void io_low(uint8_t group, uint8_t mask) +``` +Sets output port to low (also called as 0). +WARNING: Invoke this only if io_setout is called before. + +Parameters: +__group__ - Number specifying exact port group +__mask__ - Binary shifted 1. Shift is equal to port index in specified group. + +#### Function io_set +```C +static inline void io_set(uint8_t group, uint8_t mask, int8_t val) +``` +Sets output port to value passed as argument. +WARNING: Invoke this only if io_setout is called before. +Parameters: +__group__ - Number specifying exact port group +__mask__ - Binary shifted 1. Shift is equal to port index in specified group. + +### For input +#### Function io_setin +```C +static inline void io_setin(uint8_t group, uint8_t mask, + enum ioeIOInResistor resistor) +``` +Configures port of `group` with `mask` as input with specified pull-up/down +resistor. + +Parameters: +__group__ - Number specifying exact port group +__mask__ - Binary shifted 1. Shift is equal to port index in specified group. + +#### Function io_get +```C +static inline int8_t io_get(uint8_t group, uint8_t mask) +``` +Returns current value of port. Note that you can use this also if port is +configured as output. + +Parameters: +__group__ - Number specifying exact port group +__mask__ - Binary shifted 1. Shift is equal to port index in specified group. + +#### Enum ioeIOInResistor +```C +enum ioeIOInResistor { + IOE_IO_PULLUP, + IOE_IO_PULLDOWN +}; +``` +This enum is used as argument for io_setin. Names should be self explanatory +in this case. + +### Pin change interrupts +#### Function io_change_sethook +```C +int8_t io_change_sethook(uint8_t group, uint8_t mask, uint8_t edge, + void (*change) (uint8_t group, uint8_t mask)) +``` +Registers function `change` as hook for pin change interrupt. `group` and `mask` +specifies port and edge specifies on what edge should hook be called. `edge` can +be IO_RISING or IO_FALLING or their binary combination with operator +`|`. +WARNING: `change` call is call during interrupt handling. You shouldn't be +blocking execution for long time. + +Parameters: +__group__ - Number specifying exact port group. +__mask__ - Binary shifted 1. Shift is equal to port index in specified group. +__edge__ - Signals on what edge should be hook called. +__change__ - Pointer to function used as interupt hook. + +#### Function io_change_remhook +```C +int8_t io_change_remhook(void (*change) (uint8_t group, uint8_t mask)) +``` +Removes `change` hook. + +Parameters: +__change__ - Pointer to function used as hook + +### Others +#### Definitions IO_{GROUP} +This defines exact numbers related to data-sheet groups. Always use these +definition not direct numbers, you can ensure cross MCU support this way. + +#### Definitions IO_{GROUP}{INDEX} +Because specifying group and mask as separate parameters is not always optimal, +mcu support file should define all ports in form of single line definition in +format `IOE_IO_{GROUP}{INDEX}`. Disadvantage is that with these definitions you +can't use binary conjunction and so only one pin can be controlled with it. + +Relevant examples +----------------- +* blink +* pcinterrupt + +Adding support +-------------- +For more information on how add support, see [Adding MCU support](/add_support.md). +Main definition is `MCUSUPPORT_IOPORT`. Define it to enable support. + +### IO_{GROUP} +This should define any number that is handy for implementation of `IOE_IOE_PIN`, +`IOE_IO_DDR` and `IOE_IO_PORT`. + +### IO_{PIN/DDR/PORT} +These should calculate exact pointers to register `PORT{GROUP}` for PORT variant +and likewise for others with group as argument. Common implementation would be +like this: +```C +#define IOE_IO_PIN(GROUP) (* (volatile uint8_t *)(PINB + 0x3*GROUP)) +``` +### IO_{GROUP}{INDEX} +This should be pair of group and one binary shifted to left relative to index. + +### MCUSUPPORT_PCINT{NUM} +This defines that MCU supports specific pin change interrupt group. Also it +defines value that is number of pins in group. diff --git a/docs/references/jobs.md b/docs/references/jobs.md new file mode 100644 index 0000000..878a557 --- /dev/null +++ b/docs/references/jobs.md @@ -0,0 +1,15 @@ +Jobs +==== +Jobs allows periodic execution of different short functions. It is designed to host +control loops. So called functions should be short. + +Every job must specify deadline and if not set otherwise also its duration. + +If tasks support is enabled jobs can be also executed on multiple tasks, which is +handy if you divide sensor reading and control algorithm, because control +algorithm can than run when for example mcu waiting for response from sensor. +Always be sure that jobs are running on tasks with highest priority, otherwise +deadlines might not be fulfilled every time. + +Be aware of taking mutexes and semaphores. It can sometime result in long task +suspension and that would result to deadline misses. diff --git a/docs/references/spi.md b/docs/references/spi.md new file mode 100644 index 0000000..f63d304 --- /dev/null +++ b/docs/references/spi.md @@ -0,0 +1,80 @@ +Serial peripheral interface +=========================== +To use include: `spi.h` +This interface is link to MOSI and MISO pins. Also SS pin is used when slave mode +initialized. + +Configuration +------------- +To use SPI you must enable `CONFIG_SPI` configuration symbol. + +References +---------- +### Function spi\_init +```C +static inline void spi_init(enum spiMode mode) +``` +Initializes SPI interface. +Parameters: + mode - Specify mode of SPI interface + +NOTE: Global interrupts must be enabled for right function of SPI. + +### Function spi\_busy +```C +static inline int8_t spi_busy(void) +``` +Returns NULL when device is not busy. +When device is busy return values in non-zero. + +### Function spi\_join +```C +static inline void spi_join(void) +``` +Blocks processor until device is not busy. + +### Function spi\_send +```C +static inline uint8_t spi_send(uint8_t data) +``` +Swap bytes with slave over SPI. +This function blocks execution until device isn't busy (transfer completed). +WARNING: Invoke this only when interface is initialized in MASTER mode. + +### Function spi\_transfer +```C +static inline void spi_transfer(uint8_t data) +``` +Transfer byte to slave over SPI. +This function isn't blocking execution until transfer is complete. +Always call spi\_join before this function when called outside of spi\_receive(). +WARNING: Invoke this only when interface is initialized in MASTER mode. + +### Function spi\_expose +```C +static inline void spi_expose(uint8_t data) +``` +Expose data for next master request. +Please don't use this when device is busy. +Best place to call this is spi\_receive(). +WARNING: Invoke this only when interface is initialized in SLAVE mode. + +### Function pointer spi\_receive +```C +extern void (*spi_receive)(uint8_t data) +``` +This function is called every time transfer is finished. +And until return from this function interrupts are disabled. + +### Enum spiMode +```C +enum spiMode { + SPI_MODE_MASTER, + SPI_MODE_SLAVE +}; +``` +This is used as parameter for spi\_init function. + +Relevant examples +----------------- +* spiblink diff --git a/docs/references/tasks.md b/docs/references/tasks.md new file mode 100644 index 0000000..e4f71df --- /dev/null +++ b/docs/references/tasks.md @@ -0,0 +1,38 @@ +Tasks +===== +Tasks can be used for sharing processor for example during period of waiting for +interrupt. Planing is based on priority. + +## Functions +### tasks_run +```C +int8_t tasks_run(void) +``` +This function starts tasks execution. Should be called after all task slots are +prepared and at least one task is started. This function exits if no task exist or +all existing tasks exited. + +### task_init + +## Structures +### Task +```C +typedef struct { + uint8_t flags; + void (*func) (void); +} Task; +``` + +### TaskSlot +```C +typedef struct { + uint8_t flags; + Task *task; + uint8_t stack_size; + uint8_t stack[]; +} TaskSlot; +``` + +### Mutex + +### Semaphore diff --git a/docs/references/timer.md b/docs/references/timer.md new file mode 100644 index 0000000..e69de29 diff --git a/docs/references/usart.md b/docs/references/usart.md new file mode 100644 index 0000000..22e54ab --- /dev/null +++ b/docs/references/usart.md @@ -0,0 +1,23 @@ +Universal synchronous/asynchronous receive/transmitter +====================================================== +This part acts as UART intended as text base interface with computer. It is +using hardware termed as USART by Atmel. This hardware also supports synchronous +communication and can behave as SPI master, but this is not supported by this +library (I don't require this feature, but implementation is welcomed). + +This part implements, if enabled, whole stack for binding input and output to +stdin and stdout. This is handy during development. You can use `printf` and +`scanf` directly. + +This part can be enabled by `CONFIG_USART` configuration option. This enables +more detailed configuration in sub-menu. + +## Usage + + +## References +### usart_init_async +```C +void usart_init_async(void) +``` + diff --git a/docs/references/utils/buffer.md b/docs/references/utils/buffer.md new file mode 100644 index 0000000..4365c25 --- /dev/null +++ b/docs/references/utils/buffer.md @@ -0,0 +1,105 @@ +buffer +====== +In whole project are used ring buffers. This is so common that handling them is +exported to separated header file. In fact it contains two implementations of +buffers. One implementation is defined using preprocessor macros. Another one is +plain C code. + +## Preprocessor implementation +Preprocessor implementation is expected to be faster and less memory intense. Also +more suitable for buffers of simple types (such us int). But this buffer has to be +defined with known size at compile time and can't be resized. All this makes it +good implementation for IO devices input and output buffers, but less for more +complicated tasks containing for example sorting. + +### Macros +#### IOEBUFFER +```C +IOEBUFFER(type, name, size) +``` +This macro defines buffer. Use it to define new buffer. +##### Parameters +| type | Specify type of elements in buffer. Can be any valid C type | +| name | Name of buffer. This is C variable name. | +| size | Numerical value of buffer size | + +#### IOEBUFFER_INIT +```C +IOEBUFFER_INIT(name, size) +``` +Use this to initialize buffer. Parameter `size` must be same as for IOEBUFFER +(definition of buffer). You can use this also to reset buffer (remove all data +from buffer). +##### Parameters +| name | Name if buffer. This is C variable name. | +| size | Numerical value of buffer size | + +#### IOEBUFFER_PUT +```C +IOEBUFFER_PUT(name, size, data, mode) +``` +Use this macro to add new data to buffer. +##### Parameters +| name | Name of buffer. This is C variable name. | +| size | Numerical value of buffer size | +| data | Data to be stored to buffer | +| mode | This specifies action if buffer is full. | +##### Modes +| IOEBUFFER_MODE_BLOCK | Block execution until buffer has some free space. | +| IOEBUFFER_MODE_OVERWRITE | Overwrites oldest added (fist to be read) | +| IOEBUFFER_MODE_DROP | New data won't be stored to buffer if full | + +#### IOEBUFFER_GET +```C +IOEBUFFER_GET(name, size, variable) +``` +Get value from buffer. Value is stored in `variable` (it is variable of same type +as buffer data, not pointer to variable of same type). If no data is in buffer, +variable is set to NULL. +##### Parameters +| name | Name of buffer. This is C variable name. | +| size | Numerical value of buffer size. | +| variable | Variable in which data will be stored in. | + +#### IOEBUFFER_CNT +```C +IOEBUFFER_CNT(name, size, variable) +``` +Counts number of data entries in buffer and saves value to variable. +##### Parameters +| name | Name of buffer. This is C variable name. | +| size | Numerical value of buffer size. | +| variable | Int/Unsigned type variable where data count will be stored. | + +## C implementation +C implementation is more general. Size is defined by calling initialization +function. But it is using general pointers, so it is only able store pointers. + +As buffer identification is used defined type `IOEBuffer`. All functions takes +pointer to this type as parameter. Because of this, it won't be described for +every function. + +### Functions +#### ioebuffer_init +```C +int8_t ioebuffer_init(IOEBuffer *buf, uint8_t size, uint8_t flags) +``` +Initialized buffer and allocates required resources. +##### Parameters +| size | Number of elements to be maximally stored in buffer | +| flags | TODO +##### Flags +TODO + +#### ioebuffer_uninit +```C +void ioebuffer_uninit(IOEBuffer *buf) +``` +Frees all resources allocated during initialization of buffer. Use this every time +you are freeing buffer. + +#### ioebuffer_put +```C +int8_t ioebuffer_put(IOEBuffer *buf, void *data) +``` +Add `data` to buffer diff --git a/docs/references/utils/narray.md b/docs/references/utils/narray.md new file mode 100644 index 0000000..3e64f67 --- /dev/null +++ b/docs/references/utils/narray.md @@ -0,0 +1,64 @@ +narray.h +======== +This implements some helping functions for null terminated arrays. Null terminated +arrays are used across whole project because they are simple and don't waste +memory space. Their disadvantage is more complicated implementation of utility +functions and much longer adding time (they are reallocated every time new data +are added). + +This implementation is limited to only contains 255 elements(limited by uint8_t +type). But it should be enough for basic usage. If you need store more than that, +you should use different approach. Also only pointers can be stored. + +To define null terminated array just define pointer to you required pointer and set +it to NULL. Such narray is handled as empty. Example: +```C +int **narray = 0; +``` +After adding some data (in example case data of type `int*`) you can access them +same as if you would working with simple array (`narray[i]`). Last element it such +array is `NULL`. No more valid data are stored after `NULL` (This also means that +`NULL` can't be stored to narray). + +All functions are taking as parameter `void ***array`, which is pointer to null +terminated array. This parameter won't be described in functions. + +## Functions and macros +### narray_add +```C +void narray_add(void ***array, void *data) +``` +Add data to end of the array. This increases size of the array by one (two bytes). +#### Parameters +| data | data to be stored to array | + +### narray_remove +```C +void narray_remove(void ***array, void *data) +``` +Remove specified data from array. This effectively decreases size of the array by +one (two bytes). +#### Parameters +| data | data to be removed from array | + +### narray_size +```C +size_t narray_size(void ***array) +``` +Returns size of array. + +### narray_free +```C +inline void narray_free(void ***array) +``` +Frees whole array if allocated. + +### fornarray +```C +#define fornarray(array, i, data) for (i = 0; (data = array[i]) != 0; i++) +``` +This is macro. It implements simple foreach cycle. +#### Parameters +| array | array it self (void ** not pointer) | +| i | unsigned integer (uint8_t) | +| data | variable which will be used to access data | diff --git a/examples/blink/blink.c b/examples/blink/blink.c index 8386d4b..6a81aee 100644 --- a/examples/blink/blink.c +++ b/examples/blink/blink.c @@ -3,12 +3,12 @@ #include int main() { - io_setout(IO_B3); + io_setout(IO_B0); while (1) { - io_hight(IO_B3); + io_hight(IO_B0); _delay_ms(500); - io_low(IO_B3); + io_low(IO_B0); _delay_ms(500); } } diff --git a/examples/blink/build/config.h b/examples/blink/build/config.h new file mode 100644 index 0000000..5fff870 --- /dev/null +++ b/examples/blink/build/config.h @@ -0,0 +1 @@ +#define CONFIG_IOPORTS y diff --git a/examples/dht22usart/dht22.c b/examples/dht22usart/dht22.c deleted file mode 100644 index ec643aa..0000000 --- a/examples/dht22usart/dht22.c +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include -#include -#include "../../usart.h" -//#include "../../sensor_dht22.h" - -void rec(uint8_t data) { - if (data == '\r') { - // TODO read sensor and send data - } -} - -const struct dht22_port dht22_ports[] = { - {&DDRC, &PORTC, &PINC, _BV(PORTC4), NULL, 0} -}; - -int main() { - //usart_async_open(); - //sei(); - //usart_receive = rec; - //dht22_init(0); - - timer_init(COUNTER0_PWM); - while (1); -} diff --git a/examples/dht22usart/makefile b/examples/dht22usart/makefile deleted file mode 100644 index fb69068..0000000 --- a/examples/dht22usart/makefile +++ /dev/null @@ -1,40 +0,0 @@ -MAKEFLAGS += --no-builtin-rules - -MMCU = atmega328p -F_CPU = 16000000L -IOE_PREFIX = ../.. -IOE_SHORTOUTPUT=y -IOE_CFLAGS = -Os -ffunction-sections -fdata-sections -fshort-enums -Wall \ - -DF_CPU=$(F_CPU) -mmcu=$(MMCU) \ - -DCONFIG_IOE_USART \ - -DCONFIG_IOE_USART_OUTBUFFER_SIZE=64 \ - -DCONFIG_IOE_USART_OUTBUFFER_MODE=0 \ - -DCONFIG_IOE_USART_OUTFILE \ - -DCONFIG_IOE_USART_BAUD=115200 \ - -DCONFIG_IOE_USART_PARITY=USART_PARITY_NONE \ - -DCONFIG_IOE_USART_STOPBIT=USART_STOPBIT_SINGLE \ - -DCONFIG_IOE_USART_DATABITS=8 \ - -DCONFIG_IOE_TIMERS \ - -DCONFIG_IOE_SENSOR_DHT22 \ - -DCONFIG_IOE_SENSOR_DHT22_TIMER=COUNTER0_PWM - -all: dht22.hex - @echo Now you can flash dht22.hex to your chip. - -clean: ioeclean - $(RM) dht22.o - $(RM) dht22.elf dht22.hex - -include ../../avr-ioe.mk - -dht22.elf: %.elf: $(IOE_OBJ) dht22.o - @echo " LD $@" - @avr-gcc -Os -mmcu=$(MMCU) $^ -o $@ - -dht22.hex: %.hex: %.elf - @echo " OBJCOPY $@" - @avr-objcopy -O ihex -R .eeprom $< $@ - -dht22.o: %.o: %.c - @echo " CC $@" - @avr-gcc $(IOE_CFLAGS) -c -o $@ $< diff --git a/include/adc.h b/include/adc.h index 86429a1..f865b99 100644 --- a/include/adc.h +++ b/include/adc.h @@ -1,3 +1,4 @@ +// vim:ts=4:sw=4:sts=4:expandtab #include #include #include diff --git a/include/eeprom.h b/include/eeprom.h new file mode 100644 index 0000000..31da039 --- /dev/null +++ b/include/eeprom.h @@ -0,0 +1,2 @@ +#include + diff --git a/include/error.h b/include/error.h new file mode 100644 index 0000000..7b13bcd --- /dev/null +++ b/include/error.h @@ -0,0 +1,3 @@ +#include + +#include diff --git a/include/jobs.h b/include/jobs.h new file mode 100644 index 0000000..d2abbdc --- /dev/null +++ b/include/jobs.h @@ -0,0 +1,8 @@ +#include + +#ifndef _IOE_JOBS_H_ +#define _IOE_JOBS_H_ + + + +#endif /* _IOE_JOBS_H_ */ diff --git a/include/sensors/dht22.h b/include/sensors/dht22.h deleted file mode 100644 index aabb442..0000000 --- a/include/sensors/dht22.h +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include - -#include "../tasks.h" -#include "../timer.h" - -#ifndef _IOE_SENSOR_DHT22_H_ -#define _IOE_SENSOR_DHT22_H_ -#ifdef CONFIG_IOE_SENSOR_DHT22 - -struct dht22_value { - uint8_t integral; - uint8_t decimal; -}; - -struct dht22_port { - volatile uint8_t *DDR; // Address of DDR register - volatile uint8_t *PORT; // Address of PORT register - volatile uint8_t *PIN; // Address of PIN register - uint8_t MASK; // _BV() of index of port - volatile uint8_t *PCMSK; // Address of relevant PCMSK register - uint8_t PCMSK_MASK; // _BV() of index of PCMSK -}; - -extern const struct dht22_port dht22_ports[]; - -/* - * Initialize port for sensor - */ -void dht22_init(uint8_t port); - -/* - * Read values from dht22 - * rh - relative - * t - temperature - */ -int8_t dht22_read(uint8_t portindex, struct dht22_value *rh, - struct dht22_value *t); - -#endif /* CONFIG_IOE_SENSOR_DHT22 */ -#endif /* _IOE_SENSOR_DHT22_H_ */ diff --git a/include/spi.h b/include/spi.h index bdfce25..c4c003b 100644 --- a/include/spi.h +++ b/include/spi.h @@ -1,3 +1,4 @@ +// vim:ts=4:sw=4:sts=4:expandtab #include #include #include @@ -9,11 +10,6 @@ #define _IOE_SPI_H_ #ifdef CONFIG_SPI -/*! \brief Modes definition for spi_init - * - * This enum is used by spi_init to define if SPI should be initialized as master - * or slave. - */ enum spiMode { SPI_MODE_MASTER, SPI_MODE_SLAVE @@ -22,10 +18,6 @@ enum spiMode { volatile extern int8_t _spi_busy; volatile extern Mutex spi_mutex; -/*! \brief Initializes SPI interface. - * - * \param mode Specify mode of SPI interface - */ static inline void spi_init(enum spiMode mode) { _spi_busy = 0; if (mode == SPI_MODE_MASTER) { diff --git a/include/sys/mutex.h b/include/sys/mutex.h new file mode 100644 index 0000000..f682e02 --- /dev/null +++ b/include/sys/mutex.h @@ -0,0 +1,32 @@ +#include +#include +#include + +#include "../tasks.h" + +#ifndef _IOE_SYS_MUTEX_H_ +#define _IOE_SYS_MUTEX_H_ +#ifdef CONFIG_IOE_MUTEX + +#define MUTEX_F_TIMED (1<<0) +#define MUTEX_F_TAKEN (1<<7) +#define mutex_t volatile struct Mutex +struct Mutex { + uint8_t flags; + Task *_task_took; +}; +int8_t mutex_init(struct Mutex * mtx); +int8_t mutex_free(struct Mutex * mtx); +int8_t mutex_take(struct Mutex * mtx, uint16_t timeout); +int8_t mutex_give(struct Mutex * mtx); + +#else /* CONFIG_IOE_MUTEX */ + +#define mutex_t struct { } +#define mutex_init(MTX, FLAGS) +#define mutex_free(MTX) +#define mutex_take(MTX, TIMEOUT) +#define mutex_give(MTX); + +#endif /* CONFIG_IOE_MUTEX */ +#endif /* _IOE_SYS_MUTEX_H_ */ diff --git a/include/sys/semaphore.h b/include/sys/semaphore.h new file mode 100644 index 0000000..0a25af3 --- /dev/null +++ b/include/sys/semaphore.h @@ -0,0 +1,31 @@ +#include +#include +#include + +#ifndef _IOE_SYS_SEMAPHORE_H_ +#define _IOE_SYS_SEMAPHORE_H_ +#ifdef CONFIG_IOE_SEMAPHORE + +#define semaphore_t volatile struct Semaphore +struct Semaphore { + uint8_t flags; + uint8_t _count; + Task **_tasks; + uint8_t _tasks_len; +}; +int8_t semaphore_init(struct Semaphore * sem, uint8_t count); +int8_t semaphore_free(struct Semaphore * sem); +int8_t semaphore_take(struct Semaphore * sem, uint16_t timeout); +int8_t semaphore_give(Semaphore * sem); + +#else /* CONFIG_IOE_SEMAPHORE */ + +#define semaphore_t struct { } +// TODO implement for single processor +#define semaphore_init(SEM, FLAGS, COUNT) +#define semaphore_free(SEM) +#define semaphore_take(SEM, TIMEOUT) +#define semaphore_give(SEM) + +#endif /* CONFIG_IOE_SEMAPHORE */ +#endif /* _IOE_SYS_SEMAPHORE_H_ */ diff --git a/include/tasks.h b/include/tasks.h index 27bd968..b62b093 100644 --- a/include/tasks.h +++ b/include/tasks.h @@ -22,17 +22,21 @@ union StackPointer { uint8_t lh8[2]; uint16_t lh16; }; -typedef struct { + +struct Task { uint8_t flags; void (*func) (void *data); void *data; uint8_t _rflags; uint8_t _stack_size; union StackPointer _sp; -} Task; -int8_t task_spawn(Task * t, uint8_t stack_size); +}; +typedef task_t volatile struct Task; + +int8_t task_spawn(struct Task * t, uint8_t stack_size); +Task *tasks_self(void); void taks_exit(void); -int8_t task_kill(Task * t); +int8_t task_kill(struct Task * t); int8_t task_safe(void); int8_t task_unsafe(void); @@ -40,55 +44,13 @@ int8_t task_delay(uint16_t cycles); int8_t task_delay_ms(uint16_t ms); int8_t task_delay_till(volatile int8_t * boolean, uint16_t ms); -#define MUTEX_F_TIMED (1<<0) -#define MUTEX_F_TAKEN (1<<7) -typedef struct { - uint8_t flags; - Task *_task_took; -} Mutex; -int8_t mutex_init(Mutex * mtx); -int8_t mutex_free(Mutex * mtx); -int8_t mutex_take(Mutex * mtx, uint16_t timeout); -int8_t mutex_give(Mutex * mtx); - -typedef struct { - uint8_t flags; - uint8_t _count; - Task **_tasks; - uint8_t _tasks_len; -} Semaphore; -int8_t semaphore_init(Semaphore * sem, uint8_t count); -int8_t semaphore_free(Semaphore * sem); -int8_t semaphore_take(Semaphore * sem, uint16_t timeout); -int8_t semaphore_give(Semaphore * sem); - #else /* CONFIG_IOE_TASKS */ -typedef struct { -} Task; -#define task_spawn(T, STACK_SIZE) -#define task_exit() -#define task_kill(T) #define task_safe() #define task_unsafe() - -#define task_delay(US) _delay_us(US) // TODO this should be cycles +#define task_delay(CC) _delay_us(CC) // TODO this should be cycles #define task_delay_ms(MS) _delay_ms(MS) int8_t task_delay_till(volatile int8_t * boolean, uint16_t ms); -typedef struct { -} Mutex; -#define mutex_init(MTX, FLAGS) -#define mutex_free(MTX) -#define mutex_take(MTX, TIMEOUT) -#define mutex_give(MTX); - -typedef struct { -} Semaphore; -#define semaphore_init(SEM, FLAGS, COUNT) -#define semaphore_free(SEM) -#define semaphore_take(SEM, TIMEOUT) -#define semaphore_give(SEM) - #endif /* CONFIG_IOE_TASKS */ #endif /* _IOE_TASKS_H_ */ diff --git a/include/timer.h b/include/timer.h index 4fef95c..473110f 100644 --- a/include/timer.h +++ b/include/timer.h @@ -1,3 +1,4 @@ +// vim:ts=4:sw=4:sts=4:expandtab #include #include #include diff --git a/include/usi-spi.h b/include/usi-spi.h new file mode 100644 index 0000000..18534ab --- /dev/null +++ b/include/usi-spi.h @@ -0,0 +1,66 @@ +#include +#include +#include + +#include "mcu/mcu_def.h" + +#ifndef _IOE_USI_SPI_H_ +#define _IOE_USI_SPI_H_ + +#ifndef MCUSUPPORT_USI +#error "No USI interface is known on your mcu." +#endif + +enum usiSpiMode { + // Device is initialized as master + USI_SPI_MODE_MASTER, + // Device is initialized as slave + USI_SPI_MODE_SLAVE, +}; + +/* + * Initialize SPI on USI device + * + * Parameters: + * mode - Specify mode of SPI interface + */ +void usi_spi_init(enum usiSpiMode mode); +/* + * Returns NULL when device is not busy. + * When device is busy return values in non-zero. + */ +int8_t usi_spi_busy(void); +/* + * Blocks processor until device is not busy. + */ +void usi_spi_join(void); +/* + * Swap bytes with slave over SPI. + * This function blocks execution until device isn't busy. + * WARNING: Invoke this only when interface is initialized in MASTER mode. + */ +uint8_t usi_spi_send(uint8_t data); +/* + * Swaps byte with slave over SPI. + * This function isn't checking if device is busy, but it's not blocking execution. + * WARNING: Invoke this only when interface is initialized in MASTER mode. + */ +uint8_t usi_spi_transfer(uint8_t data); +/* + * Expose data for next master request. + * Please don't use this when device is busy. + * Best place to call this is usi_spi_retrieve(). + * WARNING: Invoke this only when interface is initialized in SLAVE mode. + */ +void usi_spi_expose(uint8_t data); + +/* + * This function must be defined by user. + * This function is called every time transfer is finished. + * And until return from this function interrupts are disabled. + * WARNING: Please define this function in your code. + */ +void usi_spi_receive(uint8_t data); + + +#endif /* _IOE_USI_SPI_H_ */ diff --git a/include/usi-twi.h b/include/usi-twi.h new file mode 100644 index 0000000..e69de29 diff --git a/include/utils/timers_div.h b/include/utils/timers_div.h index 9077aac..c63c8eb 100644 --- a/include/utils/timers_div.h +++ b/include/utils/timers_div.h @@ -1,11 +1,6 @@ -//#include "../timer.h" -enum timerDivider { - TIMER_DIVIDER_1, - TIMER_DIVIDER_8, - TIMER_DIVIDER_64, - TIMER_DIVIDER_256, - TIMER_DIVIDER_1024 -}; +#ifndef _IOE_TIMER_H_ +#error Please include time.h before timers_div.h +#endif #ifndef TIMER_DIV_RESTIME #error Please define minimal time (us) before include timers_div.h in TIMER_DIV_RESTIME diff --git a/include/wireless/nrf24l01p.h b/include/wireless/nrf24l01p.h deleted file mode 100644 index e69de29..0000000 diff --git a/mkdocs.yml b/mkdocs.yml index 83ec683..02deb97 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -5,19 +5,19 @@ copyright: GNU General Public License, version 2.0 pages: - Home: index.md - Usage: usage.md -- Parts: - - IO Ports: parts/ioport.md - - SPI: parts/spi.md - - Tasks: parts/tasks.md - - Jobs: parts/jobs.md - - Timer: parts/timer.md - - USART: parts/usart.md +- References: + - IO Ports: references/ioport.md + - SPI: references/spi.md + - Tasks: references/tasks.md + - Jobs: references/jobs.md + - Timer: references/timer.md + - USART: references/usart.md - Utils: - - Buffer: parts/utils/buffer.md - - NArray: parts/utils/narray.md + - Buffer: references/utils/buffer.md + - NArray: references/utils/narray.md - Adding MCU support: add_support.md - Miscellanoues: - - Jobs vs. Tasks: miscellanoues/jobs_vs_tasks.md + - Jobs vs. Tasks: miscelaneous/jobs_vs_tasks.md theme: readthedocs site_dir: html diff --git a/src/can/mcp2515.c b/src/can/mcp2515.c deleted file mode 100644 index 8c9915c..0000000 --- a/src/can/mcp2515.c +++ /dev/null @@ -1,52 +0,0 @@ -#include - -#ifdef CONFIG_IOE_CAN_MCP2515 -// TODO check -#define CAN_MCP2515_RESET 0xC0 -#define CAN_MCP2515_READ 0x03 -#define CAN_MCP2515_READRX 0x90 -#define CAN_MCP2515_WRITE 0x02 -#define CAN_MCP2515_LOADTX 0x40 -#define CAN_MCP2515_RTS 0x80 -#define CAN_MCP2515_RDSTAT 0xA0 -#define CAN_MCP2515_RXSTAT 0xB0 -#define CAN_MCP2515_BITMOD 0x05 - -inline void can_mcp2515_init(canMcp2515 * can, uint8_t group, uint8_t mask) { - can->group = group; - can->mask = mask; - can_mcp2515_reset(can); - // TODO - return 0; -}; - -inline void can_mcp2515_reset(canMcp2515 * can) { - take_mutex(spi_mutex); - spi_join(); - - release_mutex(spi_mutex); -}; - -inline uint8_t can_mcp2515_read(canMcp2515 * can, uint8_t address) { -}; - -inline uint8_t can_mcp2515_readrx(canMcp2515 * can, uint8_t buffer) { -}; - -inline void can_mcp2515_write(canMcp2515 * can, uint8_t address, uint8_t data) { -}; - -inline void can_mcp2515_loadrx(canMcp2515 * can, uint8_t buffer, uint8_t data) { -}; - -inline uint8_t can_mcp2515_rdstat(canMcp2515 * can) { -}; - -inline uint8_t can_mcp2515_rxstat(canMcp2515 * can) { -}; - -inline void can_mcp2515_bitmod(canMcp2515 * can, uint8_t address, uint8_t mask, - uint8_t data) { -}; - -#endif /* CONFIG_IOE_CAN_MCP2515 */ diff --git a/src/sensors/dht22.c b/src/sensors/dht22.c deleted file mode 100644 index 9653ba5..0000000 --- a/src/sensors/dht22.c +++ /dev/null @@ -1,117 +0,0 @@ -#include - -#ifdef CONFIG_IOE_SENSOR_DHT22 - -#define PP dht22_ports[pi] -#define PPP dht22_ports[dht22_measurement.pi] - -#define TIMEOUT F_CPU/40000 - -#define STATUS_NONE 0 -#define STATUS_COMPLETE 1 -#define STATUS_TIMEOUT 2 - -#define PORT_SENSORW 4 -#define PORT_SENSORRESP_L 3 -#define PORT_SENSORRESP_H 2 -#define PORT_H 1 -#define PORT_L 0 - -struct { - uint8_t index; - uint8_t data[5]; - volatile uint8_t status; - uint8_t pi; - uint8_t port_status; -} dht22_measurement; - -inline void dht22_init(uint8_t pi) { - // Set port as output and set level hight - *(PP.DDR) |= PP.MASK; - *(PP.PORT) |= PP.MASK; - // Enable all pin change interrupts - PCIFR = 0xff; -} - - -inline void dht22_read_deac(uint8_t status) { - dht22_measurement.status = status; - // Disable pin change interrupt - *(PPP.PCMSK) &= ~PPP.PCMSK_MASK; - timer_disable(CONFIG_IOE_SENSOR_DHT22_TIMER); -} - -// This is called from pcint.c -void dht22_read_pcint(void) { - switch (dht22_measurement.port_status) { - case PORT_L: - if (!(*(PPP.PIN) & PPP.MASK)) - return; - dht22_measurement.port_status = PORT_H; - break; - case PORT_H: - if (*(PPP.PIN) & PPP.MASK) - return; - dht22_measurement.port_status = PORT_L; - register uint16_t val; - val = timer_value(CONFIG_IOE_SENSOR_DHT22_TIMER); - // TODO compare value - if (val < 1) { - dht22_measurement.data[dht22_measurement.index / 8] - } - if (++(dht22_measurement.index) == 40) { - dht22_read_deac(STATUS_COMPLETE); - return; - } - break; - default: - if (((*(PPP.PIN) & PPP.MASK) && dht22_measurement.port_status == 3) - || !(*(PPP.PIN & PPP.MASK))) - return; - dht22_measurement.port_status--; - } - timer_reset(CONFIG_IOE_SENSOR_DHT22_TIMER); -} - -void dht22_read_timer_overflow(void) { - dht22_read_deac(STATUS_TIMEOUT); -} - -int8_t dht22_read(uint8_t pi, struct dht22_value *rh, - struct dht22_value *t) { - dht22_measurement.index = 0; - dht22_measurement.status = STATUS_NONE; - dht22_measurement.pi = pi; - dht22_measurement.port_status = PORT_SENSORW; - - // Pull port down for 500us - *(PP.PORT) &= ~PP.MASK; - _delay_us(500); - // Initialize timer - timer_init(CONFIG_IOE_SENSOR_DHT22_TIMER, TIMER_DIVIDER_64); - timer_sethook(CONFIG_IOE_SENSOR_DHT22_TIMER, - dht22_read_timer_overflow); - // Set pin change interrupt - *(PP.PCMSK) |= PP.PCMSK_MASK; - // Set port as output and pull-up resistor - *(PP.DDR) &= ~PP.MASK; - *(PP.PORT) |= PP.MASK; - // Delay till reading is done - taskDelayTill(&(dht22_measurement.status)); - - // Copy data - rh->integral = dht22_measurement.data[0]; - rh->decimal = dht22_measurement.data[1]; - t->integral = dht22_measurement.data[2]; - t->decimal = dht22_measurement.data[3]; - // Check sum - uint8_t sum = dht22_measurement.data[0]; - sum += dht22_measurement.data[1]; - sum += dht22_measurement.data[2]; - sum += dht22_measurement.data[3]; - if (sum != dht22_measurement.data[4]) - return 1; - return dht22_measurement.status - 1; -} - -#endif /* CONFIG_IOE_SENSOR_SHT22 */ diff --git a/src/tasks.c b/src/tasks.c index abc2add..27f8fb4 100644 --- a/src/tasks.c +++ b/src/tasks.c @@ -33,15 +33,6 @@ void task_stack_reg(TaskStack * ts, Task * t) { void task_stack_unreg(TaskStack * ts, Task * t) { } -void init_mutex(Mutex * mtx, uint8_t flags) { -}; -void take_mutex(Mutex * mtx) { -}; -int8_t take_mutex_t(Mutex * mtx, uint16_t timeout) { -}; -void give_mutex(Mutex * mtx) { -}; - #else /* CONFIG_IOE_TASKS */ int8_t task_delay_till(volatile int8_t * boolean, uint16_t timeout) { -- cgit v1.2.3