# ############################################################################# # # Copyright (c) 2000-2001 by Wayne C. Gramlich & William T. Benson. # All rights reserved. # # Permission to use, copy, modify, distribute, and sell this software # for any purpose is hereby granted without fee provided that the above # copyright notice and this permission are retained. The author makes # no representations about the suitability of this software for any purpose. # It is provided "as is" without express or implied warranty. # # This is the code that implements the InOut4 RoboBrick. Basically # it just waits for commands that come in at 2400 baud and responds # to them. See # # http://web.gramlich.net/projects/robobricks/inout4/index.html # # for more details. # # ############################################################################# processor pic12c509 cp=off wdte=off mclre=off fosc=intrc # define processor constants constant clock_rate 4000000 constant clocks_per_instruction 4 constant instruction_rate clock_rate / clocks_per_instruction # define serial communication control constants constant baud_rate 2400 constant instructions_per_bit instruction_rate / baud_rate constant delays_per_bit 3 constant instructions_per_delay instructions_per_bit / delays_per_bit register osccal 5 constant osccal_unit 0x10 constant inout_bit0 0 constant inout_bit1 1 constant inout_bit2 2 constant serial_in_bit 3 constant inout_bit3 4 constant serial_out_bit 5 constant inout_mask0 (1 << inout_bit0) constant inout_mask1 (1 << inout_bit1) constant inout_mask2 (1 << inout_bit2) constant inout_mask3 (1 << inout_bit3) constant serial_in_mask (1 << serial_in_bit) constant serial_out_mask (1 << serial_out_bit) constant io_mask 0xf constant inout_mask (inout_mask0 | inout_mask1 | inout_mask2 | inout_mask3) constant serial_mask (serial_in_mask | serial_out_mask) # define port bit assignments port porta a bits_and_byte read_write_manual pin inout0 porta inout_bit0 read_write_manual pin inout1 porta inout_bit1 read_write_manual pin inout2 porta inout_bit2 read_write_manual pin inout3 porta inout_bit3 read_write_manual pin serial_in porta serial_in_bit read_only pin serial_out porta serial_out_bit write_only string_constants { id = 1, 0, 19, 0, 0, 0, 0, 0, 0r'16', 7, 0s'InOut4A', 15, 0s'Gramlich&Benson' } constant id_size 8 + 16 + 1 + 7 + 1 + 15 bank 0 global inputs byte global complement byte global direction byte global outputs byte # Interrupt masks: global interrupt_enable bit global interrupt_pending bit global receiving bit global falling byte global high byte global low byte global raising byte # For now put all the smaller routines first so that they can live # within the first 256 bytes of main memory. The PIC12C5xx chips # can only call routines that are within the first 256 bytes (i.e. # the first half) of the code page. bank 0 procedure delay { arguments_none returns_nothing uniform_delay instructions_per_delay # This procedure will delay for one third of a bit time. variable current byte variable changed byte variable previous byte variable not_current byte # Kick the dog: watch_dog_reset # Setup for interrupts: previous := current # Read the I/O port once: inputs := porta inputs := inputs | (inputs >> 1) & 8 current := inputs ^ complement not_current := current ^ 0xf changed := inputs ^ previous # See about triggering the interrupt_pending flag: if ((low & not_current) | (high & current) | (changed & current & raising) | (changed & not_current & falling) != 0) { interrupt_pending := 1 } # Send an interrupt if interrupts are enabled: if (interrupt_pending && interrupt_enable) { # Shove serial out to low: serial_out := 0 } } procedure get_byte { arguments_none returns byte # Wait for a character and return it. # The get_byte() procedure only waits for 9-2/3 bits. That # way the next call to get_byte() will sychronize on the start # bit instead of possibly starting a little later. variable count byte variable char byte # Wait for start bit: receiving := 1 while (serial_in) { call delay() } # Clear interrupts and interrupt pending: interrupt_enable := 0 # Skip over start bit: call delay() call delay() call delay() # Sample in the middle third of each data bit: char := 0 count_down count 8 { call delay() char := char >> 1 if (serial_in) { char := char | 0x80 } call delay() call delay() } # Skip over 2/3's of stop bit: call delay() call delay() return char } procedure send_byte { argument char byte returns_nothing # Send {char} to {tx}: variable count byte # {receiving} will be 1 if the last get/put routine was a get. # Before we start transmitting a response back, we want to ensure # that there has been enough time to turn the line line around. # We delay the first 1/3 of a bit to pad out the 9-2/3 bits from # for get_byte to 10 bits. We delay another 1/3 of a bit just # for good measure. Technically, the second call to delay() # is not really needed. if (receiving) { receiving := 0 call delay() call delay() } # Send the start bit: serial_out := 0 call delay() call delay() call delay() # Send the data: count_down count 8 { serial_out := char@0 char := char >> 1 call delay() call delay() call delay() } # Send the stop bit: serial_out := 1 call delay() call delay() call delay() } procedure update_outputs { arguments_none returns_nothing # This procedure will update the output bits: variable mask byte variable temp byte mask := (direction << 1) & 0x10 | direction & 7 | serial_in_mask assemble { tris 6 } temp := porta & (mask | serial_mask) mask := mask ^ 0x17 porta := temp | ((outputs << 1) | outputs) & mask } origin 0x200 bank 1 procedure bit_to_mask { argument bit byte returns byte # Convert low order 2-bits of {bit} to a mask. variable mask byte bit := bit & 3 mask := 1 while (bit != 0) { mask := mask << 1 bit := bit - 1 } return mask } # The main routine can span the 256 byte boundary: procedure main { arguments_none returns_nothing variable command byte variable glitch byte variable id_index byte variable result byte # Let ROOT=http://web.gramlich.com/projects/robobricks . # For InOut4A specific commands see: # ROOT/InOut4/rev_a/index.html # For shared commands see: # ROOT/specifications.html#Software_Protocol # For shared interruptcommands see: # ROOT/specifications.html#Interrupts # Set the direction: direction inout0 read direction inout1 read direction inout2 read direction inout3 read outputs := 0 direction := 0xff interrupt_enable := 0 interrupt_pending := 0 falling := 0 high := 0 low := 0 raising := 0 glitch := 0 id_index := 0 loop_forever { # Wait for a command: command := get_byte() # Dispatch on command: switch (command >> 6) { case 0 { # Command = 00xx xxxx: switch (command >> 3) { case 0 { # Command = 0000 0xxx: switch (command & 7) { case 0 { # Read Inputs (Command = 0000 0000): call send_byte(inputs ^ complement) } case 1 { # Read Complement Mask (Command = 0000 0001): call send_byte(complement) } case 2 { # Read Low Mask (Command = 0000 0010): call send_byte(low) } case 3 { # Read High Mask (Command = 0000 0011): call send_byte(high) } case 4 { # Read Raising Mask (Command = 0000 0100): call send_byte(raising) } case 5 { # Read Falling Mask (Command = 0000 0101): call send_byte(falling) } case 6 { # Read Direction Mask (Command = 0000 0110): call send_byte(direction) } case 7 { # Read Outputs (Command = 0000 0111): call send_byte(outputs) } } } case 1 { # Command = 0000 1xxx: switch (command & 7) { case 0 { # Read Raw (Command = 0000 1000): call send_byte(inputs) } case 1 { # Set Complement Mask(Command = 0000 1001): complement := get_byte() & io_mask } case 2 { # Set High Mask (Command = 0000 1010): high := get_byte() & io_mask } case 3 { # Set Low Mask (Command = 0000 1011): low := get_byte() & io_mask } case 4 { # Set Raising Mask (Command = 0000 1100): raising := get_byte() & io_mask } case 5 { # Set Falling Mask (Command = 0000 1101): falling := get_byte() & io_mask } case 6 { # Set Direction Mask (Command = 0000 1110): direction := get_byte() & io_mask call update_outputs() } case 7 { # Set Outputs (Command = 0000 1111): outputs := get_byte() & io_mask call update_outputs() } } } case 2 { # Command = 0001 0xxx: if (command@2) { # Bit Set (Command = 0001 01bb): outputs := outputs | bit_to_mask(command) } else { # Bit Clear (Command = 0001 00bb): outputs := outputs & (bit_to_mask(command) ^ io_mask) } } case 3 { # Command = 0001 1xxx: if (command@2) { # Bit Read (Command = 0001 11bb): result := 0 if (outputs & bit_to_mask(command) != 0) { result := 1 } call send_byte(result) } else { # Bit Toggle (Command = 0001 10bb): outputs := outputs ^ bit_to_mask(command) } } default 7 { # Undefined commands; do nothing: } } } case 1 { # do nothing (Command = 01xx xxxx): } case 2 { # Do nothing (Command = 10xx xxxx): } case 3 { # Command = 11xx xxxx: switch ((command >> 3) & 7) { case 0, 1, 2, 3, 4 { # Command = 1100 xxxx or 1110 0xxx: # Do nothing: } case 5 { # Read Interrupt Bits (Command = 1110 1111): if ((command & 7) = 7) { # Return Interrupt Bits: result := 0 if (interrupt_enable) { result := result | 2 } if (interrupt_pending) { result := result | 1 } call send_byte(result) } } case 6 { # Shared Interrupt commands (Command = 1111 0xxx): switch (command & 7) { case 0, 1, 2, 3 { # Set interrupt bits (Command = 1111 10ep): interrupt_enable := command@1 interrupt_pending := command@0 } case 4, 5 { # Set Interrupt Pending (Command = 1111 110p): interrupt_pending := command@0 } case 6, 7 { # Set Interrupt Enable (Command = 1110 111e): interrupt_enable := command@0 } } } case 7 { # Shared commands (Command = 1111 1xxx): switch (command & 7) { case 0 { # Clock Decrement (Command = 1111 1000): osccal := osccal - osccal_unit } case 1 { # Clock Increment (Command = 1111 1001): osccal := osccal + osccal_unit } case 2 { # Clock Read (Command = 1111 1010): call send_byte(osccal) } case 3 { # Clock Pulse (Command = 1111 1011): call send_byte(0) } case 4 { # ID Next (Command = 1111 1100): if (id_index >= id.size) { id_index := 0 } call send_byte(id[id_index]) id_index := id_index + 1 if (id_index >= id_size) { id_index := 0 } } case 5 { # ID Reset (Command = 1111 1101): id_index := 0 } case 6 { # Glitch Read (Command = 1111 1110): call send_byte(glitch) glitch := 0 } case 7 { # Glitch (Command = 1111 1111): if (glitch != 0xff) { glitch := glitch + 1 } } } } } } } } }