# CLUNET
The CLUNET library - is a simple single-wire peer-to-peer network driver for AVR microcontrollers, perfect way to interconnect microcontrollers in your house.
Features:
* Requires only a few cheap additional components;
* Using only a single wire;
* Using only two pins;
* There is no master device, all devices are equal;
* You can use a very long cable, more than 100 meters;
* You don't need to care about collisions;
* Automatic CRC calculation and CRC check;
* Pseudo multitasking using interrupts;
* Up to 255 devices on one bus.
## How to use
### Hardware part
![Sample schematic](images/sample-schematic.png)
You need a pin with an external interrupt to read data and any other pin to send data. Transistor method is recommended.
### Software part
You will need one free 8-bit timer with `output compare match` interrupt.
#### Configuration####
Edit *clunet\_config.h* and change some values:
* **CLUNET\_DEVICE\_ID** - address of the device (0-254);
* **CLUNET\_DEVICE\_NAME** - name of the device (optional);
* **CLUNET\_SEND\_BUFFER\_SIZE** - send buffer size and maximum output packet size (memory usage);
* **CLUNET\_READ\_BUFFER\_SIZE** - read buffer size and maximum input packet size (memory usage);
* **CLUNET\_WRITE\_PORT** and **CLUNET\_WRITE\_PIN** - port and pin to send data;
* **CLUNET\_READ\_PORT** and **CLUNET\_READ\_PIN** - port and pin to read data, should be connected directly to the line;
* **CLUNET\_WRITE\_TRANSISTOR** - define if you are using a transistor on output pin (see schematics);
* **CLUNET\_TIMER\_PRESCALER** - prescaler for timer, define it to autocalculate `CLUNET_T` value as `64us`, it *must* match timer initialization code, `(F_CPU / CLUNET_TIMER_PRESCALER) / 15625` *must* be >= 8 and <= 24, thus 64 is the optimal value for 8MHz;
* **CLUNET\_T** - T value, it's a time period between signals in timer ticks, length of logical 0 is T, and logical 1 is 3\*T, lower T is faster while higher T is more stable, it's autocalculated as 64us based on **CLUNET\_TIMER\_PRESCALER** if not defined;
* **CLUNET\_TIMER\_INIT** - code to init timer (normal mode);
* **CLUNET\_TIMER\_REG** - timer/counter register;
* **CLUNET\_TIMER\_REG\_OCR** - timer/counter `output compare` register;
* **CLUNET\_ENABLE\_TIMER\_COMP** and **CLUNET\_DISABLE\_TIMER\_COMP** - code to enable/disable timer `output compare match` interrupt;
* **CLUNET\_ENABLE\_TIMER\_OVF** and **CLUNET\_DISABLE\_TIMER\_OVF** - code to enable/disable timer `overflow` interrupt;
* **CLUNET\_INIT\_INT** - code to init external interrupt (read pin);
* **CLUNET\_TIMER\_COMP\_VECTOR** - timer `output compare match` interrupt vector;
* **CLUNET\_TIMER\_OVF\_VECTOR** - timer `overflow` interrupt vector`;
* **CLUNET\_INT\_VECTOR** - `external interrupt` vector.
Default configuration file is optimised for ATMEGA8 / 8MHz.
#### Code
Include "clunet.h" and create callback function to receive data:
#include "clunet.h"
void data_received(unsigned char src_address, unsigned char dst_address, unsigned char command, char* data, unsigned char size)
{
/* your code here */
}
Add initialization code and enable interrupts:
int main (void)
{
clunet_init();
clunet_set_on_data_received(data_received);
sei();
Also you can use `clunet_set_on_data_received_sniff()` to set callback function which will receive all packets, not only for this device.
Function to send data:
void clunet_send(unsigned char address, unsigned char prio, unsigned char command, char* data, unsigned char size);
* **address** - address of destination device or `CLUNET_BROADCAST_ADDRESS` for multicast
* **prio** - packet priority:
1. `CLUNET_PRIORITY_NOTICE`,
2. `CLUNET_PRIORITY_INFO`,
3. `CLUNET_PRIORITY_MESSAGE`,
4. `CLUNET_PRIORITY_COMMAND`;
* **command** - command ID (0-255), note that some IDs are occupied by predefined commands (see *clunet.h*) and some are reserved (see below);
* **data** - pointer to data if any;
* **size** - data size.
Sample code:
char buffer[1];
buffer[0] = 1;
clunet_send(CLUNET_BROADCAST_ADDRESS, CLUNET_PRIORITY_MESSAGE, CLUNET_COMMAND_DEVICE_POWER_INFO, buffer, sizeof(buffer));
while (clunet_ready_to_send()); // wait while sending, otherwise next call will replace output buffer
// clunet_ready_to_send() returns current task priority
// or 0 if output buffer is not busy
char *hello = "Hello world!";
clunet_send(1, CLUNET_PRIORITY_INFO, 100, hello, strlen(hello));
#### Reserved commands
There are some reserved commands:
* **CLUNET\_COMMAND\_DISCOVERY** (0x00) - send this command as broadcast packet to find all devices in your network, devices will answer with command **CLUNET\_COMMAND\_DISCOVERY\_RESPONSE** (0x01) and **CLUNET\_DEVICE\_NAME** in data section;
* **CLUNET\_COMMAND\_REBOOT** (0x02) - send this command to reboot a device;
* **CLUNET\_COMMAND\_BOOT\_CONTROL** - (0x03) reserved for bootloader and firmware update;
* **CLUNET\_COMMAND\_BOOT\_COMPLETED** (0x04) - sent by device on start (call of `clunet_init()`), data is value of *MCUCSR* register, so your can determine reset source;
* **CLUNET\_COMMAND\_PING** (0xFE) - ping, you can test line using this command, device(s) will answer with **CLUNET\_COMMAND\_PING\_REPLY** (0xFF) and same data.
## Tested on
* ATMEGA8;
* ATMEGA16;
* ATMEGA64.
## Known bugs/problems
* Any data on bus will slow down every connected device a little.
* No delivery check, you can make it in your application code if necessary.
## Author/contacts
**Alexey 'Cluster' Avdyukhin**
clusterrr@clusterrr.com
[https://github.com/ClusterM](https://github.com/ClusterM "https://github.com/ClusterM")
[http://clusterrr.com](http://clusterrr.com "http://clusterrr.com")
没有合适的资源?快使用搜索试试~ 我知道了~
CLUNET 库 - 用于 AVR微控制器 的简单单线对等网络驱动程序_C语言_代码_相关文件_下载
共15个文件
h:7个
c:3个
makefile:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 192 浏览量
2022-07-07
03:34:19
上传
评论
收藏 44KB ZIP 举报
温馨提示
CLUNET 库 - 是用于 AVR 微控制器的简单单线对等网络驱动程序,是在家中互连微控制器的完美方式。 特征: 只需要一些便宜的附加组件; 仅使用一根电线; 仅使用两个引脚; 没有主设备,所有设备都是平等的; 您可以使用很长的电缆,超过 100 米; 你不需要关心碰撞; 自动CRC计算和CRC校验; 使用中断的伪多任务处理; 一条总线上最多 255 个设备。 如何使用 硬件部分 示例示意图 您需要一个带有外部中断的引脚来读取数据和任何其他引脚来发送数据。推荐使用晶体管方法。 软件部分 output compare match您将需要一个带中断的免费 8 位定时器。 配置#### 编辑clunet_config.h并更改一些值: CLUNET_DEVICE_ID - 设备地址(0-254); CLUNET_DEVICE_NAME - 设备名称(可选); CLUNET_SEND_BUFFER_SIZE - 发送缓冲区大小和最大输出包大小(内存使用); 更多详情、使用方法,请下载后阅读README.md文件
资源推荐
资源详情
资源评论
收起资源包目录
clunet-master.zip (15个子文件)
clunet-master
bits.h 1008B
images
sample-schematic.png 23KB
LICENSE 1KB
demo_project
clunet_bootloader
bits.h 1008B
clunet_bootloader.c 5KB
defines.h 35B
Makefile 4KB
clunet.h 13KB
defines.h 25B
clunet-demo.c 550B
Makefile 4KB
clunet_config.h 2KB
clunet.h 13KB
README.md 6KB
clunet.c 15KB
共 15 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9156
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功