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 // maximum request size
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)
25 typedef uint16_t bufptr_t;
27 typedef uint8_t bufptr_t;
30 static volatile bufptr_t buf_len, tx_ptr;
31 static volatile uint8_t buffer[BUFSIZE], transmitting;
32 static volatile uint16_t last_rx;
34 static uint8_t mb_unit_id;
37 #define UART_BAUD 9600
38 #define UBRR_VAL ((F_CPU + 8UL * UART_BAUD) / (16UL*UART_BAUD) - 1)
41 * According to Wikipedia, it is indeed 28 bits = 3.5 bytes without
42 * start- and stopbits.
44 #define REQ_TIMEOUT (28*CLOCK_HZ/UART_BAUD)
46 uint16_t hold_regs[MB_N_HOLD_REGS];
48 #if MB_N_HOLD_REGS_EEPROM > 0
49 static uint16_t hold_regs_ee[MB_N_HOLD_REGS_EEPROM] EEMEM = {
51 0, 0, 0, 30, 30, 30, 30, 0, 0, 0, 0, 30,
52 (1 << 4) | (1 << 11), // LED 1
58 void modbus_init(uint8_t unit)
65 #if MB_N_HOLD_REGS_EEPROM > 0
68 for (i = 0; i < MB_N_HOLD_REGS_EEPROM; i++)
69 hold_regs[i] = eeprom_read_word(&hold_regs_ee[i]);
79 UCSR0B = _BV(RXCIE0)|_BV(RXEN0)|_BV(TXEN0);
80 UCSR0C = _BV(UCSZ01)|_BV(UCSZ00);
83 static uint16_t compute_crc(volatile uint8_t *buf, bufptr_t len)
86 uint16_t crc = 0xFFFF;
88 for (i = 0; i < len; i++) {
90 crc ^= (uint16_t)(buf[i]);
91 for(j = 0; j < 8; j++) {
104 static void make_exception(mb_exception code)
111 #define get_word(ptr, off) (((uint16_t)ptr[off] << 8) | ptr[off+1])
112 void put_byte(uint8_t byte)
114 buffer[buf_len++] = byte;
117 void put_word(uint16_t word)
119 buffer[buf_len++] = word >> 8;
120 buffer[buf_len++] = word & 0xFF;
123 static mb_exception read_holding_regs(uint16_t start, uint16_t len)
125 if (len > BUFSIZE/2 - 3)
126 return MB_ILLEGAL_ADDR;
128 if (start < MB_HOLD_REGS_BASE
129 || start + len > MB_HOLD_REGS_BASE + MB_N_HOLD_REGS)
130 return MB_ILLEGAL_ADDR;
134 start -= MB_HOLD_REGS_BASE;
136 put_word(hold_regs[start++]);
141 static mb_exception write_single_reg(uint16_t reg, uint16_t val)
143 if (reg < MB_HOLD_REGS_BASE
144 || reg >= MB_HOLD_REGS_BASE + MB_N_HOLD_REGS)
145 return MB_ILLEGAL_ADDR;
147 if (!hold_reg_is_valid(reg, val))
148 return MB_ILLEGAL_VAL;
150 reg -= MB_HOLD_REGS_BASE;
151 hold_regs[reg] = val;
152 #if MB_N_HOLD_REGS_EEPROM > 0
153 if (reg < MB_N_HOLD_REGS_EEPROM)
154 eeprom_write_word(&hold_regs_ee[reg], val);
156 put_word(reg + MB_HOLD_REGS_BASE);
162 uint8_t modbus_poll()
168 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
172 if (get_clock() - last_rx < REQ_TIMEOUT) // still receiving
175 if (buf_len < 4) { // too short (or not for us)
180 transmitting = 1; // disable further reads
181 packet_len = buf_len;
184 crc = compute_crc(buffer, packet_len - 2);
186 if ((crc & 0xFF) != buffer[packet_len-2]
187 || (crc >> 8) != buffer[packet_len-1]) { // bad CRC
188 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
195 packet_len -= 2; // strip the CRC
196 buf_len = 2; // keep the first two bytes (unit ID and function) for TX
198 rv = MB_ILLEGAL_FUNC;
200 switch (buffer[1]) { // function
203 rv = read_holding_regs(
210 rv = write_single_reg(
221 crc = compute_crc(buffer, buf_len);
222 put_byte(crc & 0xFF);
225 // send out the reply
228 UCSR0B |= _BV(UDRIE0);
235 uint8_t rx_byte = UDR0;
236 clock_t now = get_clock();
238 if (transmitting) // how did we get here? discard it
241 if (buf_len && buffer[0] != mb_unit_id) // not for us
244 if (buf_len == BUFSIZE) { // overrun - discard the packet
250 if (now - last_rx >= REQ_TIMEOUT) { // new packet; start over
254 // TODO: we can probably calculate the CRC here as well
255 buffer[buf_len++] = rx_byte;
262 UCSR0B &= ~_BV(TXCIE0); // disable further IRQs
265 transmitting = 0; // enable receiving
270 if (tx_ptr >= buf_len) {
271 UCSR0A |= _BV(TXC0); // clear the pending TXC flag
272 UCSR0B |= _BV(TXCIE0); // enable xmit complete irq
273 UCSR0B &= ~_BV(UDRIE0); // disable ourselves
275 UDR0 = buffer[tx_ptr++];