Having experimented with RPCs as a means of putting some structure on I2C communication, I thought the basic ideas might be of interest to others. I was not able to find any information after searching for Picaxe Remote Procedure Call online. The PDF attached to this post provides a detailed account of the RPC implementation I have been using. It also presents a simple demonstration complete with logic analyser traces showing how a system contain a Picaxe RPC server and a Picaxe RPC client synchronise their activities. The sources for the client and the server are also attached to this post as copying source text from a PDF often results in loss of layout details.
--- The attached PDF and the following programs were updated after the discussion below ---
Server Source
Client Source
--- The attached PDF and the following programs were updated after the discussion below ---
Server Source
Code:
'Picaxe I2C Remote Procedure Call - Slave
#Picaxe 28X1
Symbol i2c_slave_address = $C6
Symbol rpc_procedure_id = 0
Symbol rpc_result = rpc_procedure_id + 1
Symbol rpc_parameter_1 = rpc_result + 1
Symbol rpc_parameter_2 = rpc_parameter_1 + 1
Symbol rpc_parameter_3 = rpc_parameter_2 + 1
Symbol no_operation = 0
Symbol procedure_1_id = 1
Symbol procedure_2_id = 2
Symbol procedure_3_id = 3
main:
Call initialise_i2c_as_slave
Do
Call handle_call
Loop
Return
initialise_i2c_as_slave:
hi2csetup i2cslave, i2c_slave_address
Put rpc_procedure_id, no_operation
Return
handle_call:
Symbol procedure_id = b0
ptr = rpc_procedure_id
Do Loop Until @ptr <> no_operation
Get rpc_procedure_id, procedure_id
Select Case procedure_id
Case procedure_1_id: Call procedure_1
Case procedure_2_id: Call procedure_2
Case procedure_3_id: Call procedure_3
Else 'Do nothing
EndSelect
Put rpc_procedure_id, no_operation
Return
procedure_1:
Symbol x1 = b1
Symbol y1 = b2
Symbol sum1 = b3
Get rpc_parameter_1, x1
Get rpc_parameter_2, y1
sum1 = x1 + y1
Put rpc_result, sum1
Return
procedure_2:
Call wait_for_button_press
Return
wait_for_button_press:
Symbol press_button = pinc.0
Symbol contact_bounce_time = 200
Do Loop While press_button = 0
Pause contact_bounce_time
Do Loop While press_button = 1
Return
procedure_3:
Symbol pin = b1
Symbol value = b2
Get rpc_parameter_1, pin
Get rpc_parameter_2, value
ptr = rpc_parameter_3
Do While @ptr <> 0
serout pin, N4800, (@ptrinc)
Loop
serout pin, N4800, (": ", #value, Cr, Lf)
Return
Code:
'Picaxe I2C RPC - Master
#Picaxe 08M2
Symbol i2c_slave_address = $C6
Symbol rpc_procedure_id = 0
Symbol rpc_result = rpc_procedure_id + 1
Symbol rpc_parameter_1 = rpc_result + 1
Symbol rpc_parameter_2 = rpc_parameter_1 + 1
Symbol rpc_parameter_3 = rpc_parameter_2 + 1
Symbol no_operation = 0
Symbol procedure_1_id = 1
Symbol procedure_2_id = 2
Symbol procedure_3_id = 3
Symbol sum1 = b0
main:
Call initialise_i2c
Do
Call wait_for_button_press
sertxd ("Start...", Cr, Lf)
Call procedure_1
sertxd ("procedure_1: done ", #sum1, Cr, Lf)
Call procedure_2
sertxd ("procedure_2: done", Cr, Lf)
Call send_min_value_on_remote_pin1
sertxd ("send_min_value_on_remote_pin1: done", Cr, Lf)
Call send_max_value_on_remote_pin1
sertxd ("send_max_value_on_remote_pin1: done", Cr, Lf)
Call send_count_on_remote_pin2
sertxd ("send_count_on_remote_pin2: done", Cr, Lf)
Loop
Return
initialise_i2c:
hi2csetup i2cmaster, i2c_slave_address, i2cslow, i2cbyte
Return
wait_for_button_press:
Symbol press_button = pinc.3
Do Loop While press_button = 0
Pause 200
Do Loop While press_button = 1
Return
procedure_1:
Symbol x1 = b1
Symbol y1 = b2
x1 = 12
y1 = 15
hi2cout rpc_parameter_1, (x1, y1)
hi2cout rpc_procedure_id, (procedure_1_id)
Call wait_for_rpc_completion
hi2cin rpc_result, (sum1)
Return
procedure_2:
hi2cout rpc_procedure_id, (procedure_2_id)
Call wait_for_rpc_completion
Return
send_min_value_on_remote_pin1:
Symbol min_pin = b1
Symbol min_value = b2
min_pin = 1
min_value = 5
hi2cout rpc_parameter_1, (min_pin, min_value, "Min", 0)
hi2cout rpc_procedure_id, (procedure_3_id)
Call wait_for_rpc_completion
Return
send_max_value_on_remote_pin1:
Symbol max_pin = b1
Symbol max_value = b2
max_pin = 1
max_value = 15
hi2cout rpc_parameter_1, (max_pin, max_value, "Max", 0)
hi2cout rpc_procedure_id, (procedure_3_id)
Call wait_for_rpc_completion
Return
send_count_on_remote_pin2:
Symbol count_pin = b1
Symbol count_value = b2
count_pin = 2
count_value = 100
hi2cout rpc_parameter_1, (count_pin, count_value, "Count", 0)
hi2cout rpc_procedure_id, (procedure_3_id)
Call wait_for_rpc_completion
Return
wait_for_rpc_completion:
Symbol procedure_id = b0
Do
hi2cin rpc_procedure_id, (procedure_id)
Loop Until procedure_id = no_operation
Return
Attachments
-
224.1 KB Views: 53
Last edited: