• Stars
    star
    111
  • Rank 304,388 (Top 7 %)
  • Language
    C
  • License
    The Unlicense
  • Created over 10 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CLUNET library - simple single-wire peer-to-peer network driver for AVR microcontrollers, perfect way to interconnect microcontrollers in your house

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

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

[email protected]

https://github.com/ClusterM

http://clusterrr.com

More Repositories

1

hakchi2

Tool that allows you to add more games to your NES/SNES Classic Mini. WARNING: hakchi2 is no longer supported. Please use hakchi2 CE.
C#
2,926
star
2

retroarch-clover

GLSL
321
star
3

fdskey

Famicom Disk System drive emulator
C
176
star
4

coolgirl-famicom-multicart

Ultimate multigame cartridge for Nintendo Famicom
SystemVerilog
154
star
5

ibutton

Эмулятор домофонных ключей iButton/Cyfral/Metacom
C
118
star
6

sony-headphones-control

Tasker/Locale plugin to control bluetooth headphones from Sony
Java
105
star
7

AlwaysOnTopper

Simple app for Windows, adds 'Always on top' item to system menu of every window
C#
86
star
8

skykettle-ha

Redmond SkyKettle integration for Home Assistant
Python
80
star
9

google-assistant-smart-home

Simple Python framework to control your DIY smart home devices using Google Assistant
Python
75
star
10

coolgirl-multirom-builder

Toolset that allows you to create multirom images for COOLGIRL Famicom cartridges
C#
70
star
11

famicom-dumper-client

Client (PC-software) for Famicom/NES Dumper/Programmer
C#
61
star
12

alice-smart-home

Simple Python framework to control your DIY smart home devices using Alice from Yandex
Python
60
star
13

famicom-dumper

Famicom Dumper/Programmer (deprecated)
C
53
star
14

tuyanet

.NET library to interface with Tuya WiFi smart devices over LAN.
C#
53
star
15

famicom-dumper-writer

Device for dumping and writing Famicom cardridges
C
52
star
16

wii2usb

Device that allows to connect Wii accessories (Nunchuck, Classic Controller, NES/SNES Controller) to PC via USB
C
46
star
17

wear-os-hex-editor-watchface

Hex Editor Watch Face for Wear OS
Java
45
star
18

usb-serial-telnet-server

Android application that binds a USB serial converter to a Telnet client
Java
43
star
19

NesTiler

Tool for converting images into NES format: generating pattern tables, palettes, name tables
C#
34
star
20

nessmd2usb

Переходник для одновременного подключения к компьютеру по USB двух контроллеров от Dendy и двух от Sega Mega Drive
C
32
star
21

nes2wii

NES2Wii - adapter for NES/SNES/N64/SMD/DualShock gamepad to NES Mini, SNES Mini or Wii remote
C
31
star
22

coolboy-multirom-builder

Toolset that allows you to create multirom images for cheap COOLBOY Famicom cartridges
Assembly
29
star
23

nes_mappers

NES mappers
Verilog
28
star
24

omega2-pwm

Simple program to control hardware PWM on Omega2
C
23
star
25

clovershell-client

Client for hakchi mod which allows to access NES Mini's shell, execute commands and transfer files directly via USB, without UART and FEL.
C#
23
star
26

ibutton_client

Клиент под Windows для эмулятора домофонных ключей
C#
23
star
27

nes-warface

Warface demo for NES
Assembly
22
star
28

clukeyboard

Android input method for hardware keyboards
Kotlin
18
star
29

clujtag-avr

Simple JTAG programmer for AVR microcontrollers with hardware USB
C
18
star
30

omega2-ws2811-lkm

Linux kernel module for Onion Omega2 to control WS2811/WS2812 LEDs
C
17
star
31

duplifds

Open source Famicom Disk System copier
Assembly
15
star
32

android-speech-recognition

Continuous speech recognition for Android demo
Java
14
star
33

clujtag-client

Client for JTAG programmer for AVR microcontrollers
C
13
star
34

intercom

Intercom with answering machine / Домофон с автоответчиком
C
13
star
35

clunet-lkm

Linux Kernel Module driver for CLUNET bus
C
12
star
36

dreamcast2gamecube

Dreamcast to GameCube controller adaptor
C
11
star
37

nes-containers

A simple .NET library for working with NES/Famicom containers: .nes (iNES, NES 2.0), .unf (UNIF) and .fds (Famicom Disk System images).
C#
11
star
38

nes-input-test

Simple NES/Famicom ROM to test different input accessories
Assembly
10
star
39

gpio2nesc

Adapter allows to attach any DIY buttons to NES/SNES Classic, good for arcade cabinet
C
9
star
40

rc-transceiver

IR remote control receiver and transmitter based on Onion Omega2
C
9
star
41

clovershell-daemon

hakchi mod which allows to access NES Mini's shell, execute commands and transfer files directly via USB, without UART and FEL.
C
9
star
42

pebble-dembel

Pebble Watchface for russiam army
C
5
star
43

chatgptlib

Just another OpenAI client library (chat completion only) with functions support
C#
5
star
44

kindle-lightfix

Amazon Kindle Paperwhite patch. It allows user to turn off the frontlight completely.
Shell
4
star
45

sharp-dhcp-server-lib

DHCP server with option 82 support .NET library
C#
4
star
46

space-dragon

Space Dragon - simple game for Pebble watch
C
4
star
47

AliceNet

.NET библиотека для лёгкого и быстрого создания навыков для Алисы от Яндекса.
C#
4
star
48

pebble-dos

"DOS" Watchface for Pebble Watch
C
3
star
49

snes2nes

SNES to NES adaptor
C
3
star
50

smpp-sharp-lib

SMPP client .NET library
C#
2
star
51

fdspacker

Simple tool to pack and unpack .fds files
C#
1
star