2 * Loosely modelled after AVR-RS485 by Yoshinori Kohyama (http://algobit.jp/),
3 * available at https://github.com/kohyama/AVR-RS485/
5 * All bugs by Jan "Yenya" Kasprzak <kas@fi.muni.cz> :-)
8 #include <avr/eeprom.h>
10 #include <avr/interrupt.h>
11 #include <util/atomic.h>
12 #include <util/delay.h>
17 #define BUFSIZE 128 // must be a power of two
19 // configure the control pin
20 #define ctl_pin_setup() do { DDRD |= _BV(PD2); } while (0)
21 #define ctl_pin_on() do { PORTD |= _BV(PD2); } while (0)
22 #define ctl_pin_off() do { PORTD &= ~_BV(PD2); } while (0)
24 #define BUFMASK (BUFSIZE-1)
25 #if (BUFSIZE & BUFMASK)
26 #error BUFSIZE must be a power of two
30 typedef uint16_t bufptr_t;
32 typedef uint8_t bufptr_t;
35 #define bufptr_inc(x) ((x + 1) & BUFMASK)
37 static volatile bufptr_t rx_bytes, tx_head, tx_tail;
38 static volatile uint8_t rxbuf[BUFSIZE], txbuf[BUFSIZE];
39 static volatile uint16_t last_rx;
41 static uint8_t mb_unit_id;
44 #define UART_BAUD 9600
45 #define UBRR_VAL ((F_CPU + 8UL * UART_BAUD) / (16UL*UART_BAUD) - 1)
46 #define wait_one_byte() _delay_us(10*1000000/UART_BAUD)
49 * According to Wikipedia, it is indeed 28 bits = 3.5 bytes without
50 * start- and stopbits.
52 #define REQ_TIMEOUT (28*CLOCK_HZ/UART_BAUD)
54 uint16_t hold_regs[MB_N_HOLD_REGS];
56 #if MB_N_HOLD_REGS_EEPROM > 0
57 static uint16_t hold_regs_ee[MB_N_HOLD_REGS_EEPROM] EEMEM = {
59 0, 0, 0, 30, 30, 30, 30, 0, 0, 0, 0, 30,
60 (1 << 4) | (1 << 11), // LED 1
66 void modbus_init(uint8_t unit)
69 tx_head = tx_tail = 0;
73 #if MB_N_HOLD_REGS_EEPROM > 0
76 for (i = 0; i < MB_N_HOLD_REGS_EEPROM; i++)
77 hold_regs[i] = eeprom_read_word(&hold_regs_ee[i]);
86 UCSR0B = _BV(RXCIE0)|_BV(RXEN0)|_BV(TXEN0);
87 UCSR0C = _BV(UCSZ01)|_BV(UCSZ00);
90 void rs485_send(char *p)
97 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
98 next = bufptr_inc(tx_head);
99 while (next != tx_tail && *p != '\0') {
100 txbuf[tx_head] = *p++;
102 next = bufptr_inc(tx_head);
105 UCSR0B |= _BV(UDRIE0);
109 static uint16_t compute_crc(volatile uint8_t *buf, bufptr_t len)
112 uint16_t crc = 0xFFFF;
114 for (i = 0; i < len; i++) {
116 crc ^= (uint16_t)(buf[i]);
117 for(j = 0; j < 8; j++) {
130 static void make_exception(mb_exception code)
137 #define get_word(ptr, off) (((uint16_t)ptr[off] << 8) | ptr[off+1])
138 void put_byte(uint8_t byte)
140 txbuf[tx_head++] = byte;
143 void put_word(uint16_t word)
145 txbuf[tx_head++] = word >> 8;
146 txbuf[tx_head++] = word & 0xFF;
149 static mb_exception read_holding_regs(uint16_t start, uint16_t len)
151 if (len > BUFSIZE/2 - 3)
152 return MB_ILLEGAL_ADDR;
154 if (start < MB_HOLD_REGS_BASE
155 || start + len > MB_HOLD_REGS_BASE + MB_N_HOLD_REGS)
156 return MB_ILLEGAL_ADDR;
160 start -= MB_HOLD_REGS_BASE;
162 put_word(hold_regs[start++]);
167 static mb_exception write_single_reg(uint16_t reg, uint16_t val)
169 if (reg < MB_HOLD_REGS_BASE
170 || reg >= MB_HOLD_REGS_BASE + MB_N_HOLD_REGS)
171 return MB_ILLEGAL_ADDR;
173 if (!hold_reg_is_valid(reg, val))
174 return MB_ILLEGAL_VAL;
176 reg -= MB_HOLD_REGS_BASE;
177 hold_regs[reg] = val;
178 #if MB_N_HOLD_REGS_EEPROM > 0
179 if (reg < MB_N_HOLD_REGS_EEPROM)
180 eeprom_write_word(&hold_regs_ee[reg], val);
182 put_word(reg + MB_HOLD_REGS_BASE);
194 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
195 if (rx_bytes == 0) // nothing received yet
198 if (get_clock() - last_rx < REQ_TIMEOUT) // still receiving
201 if (rx_bytes < 4) { // too short
206 if (rxbuf[0] != mb_unit_id) { // not for myself
211 if (tx_tail) { // still sending?
216 packet_len = rx_bytes; // make a copy
219 crc = compute_crc(rxbuf, packet_len - 2);
221 if ((crc & 0xFF) != rxbuf[packet_len-2]
222 || (crc >> 8) != rxbuf[packet_len-1]) // bad crc
225 txbuf[0] = rxbuf[0]; // not mb_unit_id in case it gets changed
230 switch (rxbuf[1]) { // function
232 rv = read_holding_regs(get_word(rxbuf, 2), get_word(rxbuf, 4));
235 rv = write_single_reg(get_word(rxbuf, 2), get_word(rxbuf, 4));
238 make_exception(MB_ILLEGAL_FUNC); // illegal function
245 crc = compute_crc(txbuf, tx_head);
246 txbuf[tx_head++] = crc & 0xFF;
247 txbuf[tx_head++] = crc >> 8;
252 UCSR0B |= _BV(UDRIE0);
255 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
262 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
263 rxbuf[rx_bytes] = UDR0;
264 if (rx_bytes + 1 < BUFSIZE) // ignore overruns
266 last_rx = get_clock();
272 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
273 if (tx_head == tx_tail) {
274 UCSR0B &= ~_BV(UDRIE0);
275 tx_tail = tx_head = 0;
276 wait_one_byte(); // FIXME: too long busy-wait
279 UDR0 = txbuf[tx_tail];
280 tx_tail = bufptr_inc(tx_tail);