Forum > Embedded - AVR
Is it possible to write bytes to the Master from this procedure ?
(1/1)
pascalbythree:
https://github.com/ccrause/fpc-avr/blob/master/src/library/i2cslave_unit.pas
https://github.com/ccrause/fpc-avr/blob/master/src/examples/i2cslave/i2cslavedemo.pp
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// Reply with current counter value, then increment counterprocedure DataRequestHandler(out data: byte; out ACK: boolean);begin data := counter; inc(counter); // Can supply more data ACK := true; {$ifdef DEBUGPRINT} uart_transmit(ord('T')); uart_transmit_hex(data); uart_transmit(10); {$endif DEBUGPRINT}end;
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// Reply with current counter value, then increment counterprocedure DataRequestHandler(out data: byte; out ACK: boolean);begin //what to add here?end;
Hello to all freepascal.org members, can somebody answer weither it is possible to send strings/bytes from this AVR slave to a master in freepascal? Must i code something in this unit? or am i looking in the wrong direction? Greets, Wouter van Wegen
ccrause:
Short answer,yes. The demo is a trivial counter example and the slave always returns the value of the counter, then increments the local value.
To send more complex data (multi-byte length) the master requesting the data must know how to request and interpret the data. For example to receive text, is the text encoded or plain ASCII, is it fixed or variable length?
A simple implementation for handling a variable length string could be as follows on the slave side:
- Master initiates I2C transaction by writing a byte value to slave that should be interpreted as selecting a status string.
- The slave receives this byte and stores it to know what should be returned when the master issues a read request.
- The master initiates a read request.
- The slave returns the first byte of the string and increments an internal index to the string. End return value with ACK.
- The master receives the byte. If the slave ended with ACK, read another byte.If the slave ended with NACK, end transaction.
- When the slave sends the last byte of the string, end the transaction by sending a NACK.
Untested code for illustration of the slave side implementation:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const SLAVE_STATUS_STRING = 1; var state: byte; // what to return when returning data status: shortstring; index: byte; procedure DataReceivedHandler(data: byte; ACK: boolean);begin state := data; // If addressing status, reset index to start of string if state = SLAVE_STATUS_STRING then index := 1;end; procedure DataRequestHandler(out data: byte; out ACK: boolean);begin case status of SLAVE_STATUS_STRING: begin data := status[index]; inc(index); ACK := index < length(status); end; end;end;
Navigation
[0] Message Index