|
Support : Industrial : AIRcable AIRmotes Programming Handbook :
PART 8
PART 8: SLAVE Code Example - A commented programming example
The standard slave program allows other Bluetooth devices, such as cell phones, Palm, PDAs, laptops and PCs to connect to the Bluetooth serial profile of the AIRcable Industrial.
You can download the SLAVE code here (zip file).
To program the AIRcable Industrial with this new program see AIRmotes Programming Manual Part 4.
Operation Principle
The intend is to program the AIRcable in BASIC to do this:
- open the incoming SPP port to allow other Bluetooth device to connect
- allow user to start a shell for manual commands if required
- configure the UART for 9600 baud
- link the wireless SPP port to the UART for streaming data
Theories of Operation
On the AIRcable Industrial (and other AIRmote based devices) a BASIC program controls the Bluetooth baseband processor. Since all operations (Bluetooth, BASIC, timer etc) are running on the same processor the device is implemented as a multi-tasking system where several tasks perform operations concurrently. The BASIC program controls the multi-tasking system underneath it.
Routines
The BASIC program is organized in routines. Each routine is a section in the BASIC program that starts with @ROUTINE and ends with RETURN. These routines are like interrupt routines in an operating system. The BASIC interpreter knows 11 of these routines:
- @INIT
- @IDLE
- @SLAVE
- @ALARM
- @SENSOR
- @PIO_IRQ
- @MASTER
- @PIN_CODE
- @CONTROL
- @MESSAGE
- @INQUIRY
The important programming principle is to keep the routines short. This means that routines should not occupy the processor for very long and then it should end quickly. This allows the Bluetooth baseband processor to execute other tasks that are important for the functionality of the whole system. Routines that don't end (even with WAIT) will block the system.
These routines are scheduled for execution either by the system automatically, or by other parts of the program or by external hardware or Bluetooth events. Some of the routines are actually interrupting the execution of other routines because they have higher priority. For example, the @PIN_CODE routine has the highest priority and will interrupt any other routine. Otherwise routines are scheduled to run after the current routine is finished.
The SLAVE Program
We use the implementation of the simple slave program to show the use of some of the routines and how to program the AIRcable Industrial.
The slave program starts with @ERASE. This is a command to the BASIC loader in the AIRcable when a new program is transmitted via wireless FTP. It will cause the AIRcable to erase its BASIC memory completely.
The first routine is @INIT. It runs only once when the system is booted or a new BASIC program has been uploaded. In the example we first switch on the green LED to show that the AIRcable boots.
The lines 12 to 19 configure the power to the RS232 level shifter and schedules a hardware interrupt on PIO4. This is DSR input on the RS232 connector.
@ERASE
@INIT 10
0 REM LED output and on
10 A=pioout 2
11 A=pioset 2
0 REM RS232_off set
12 A=pioout 5
13 A=pioset 5
0 REM RS232_on set
14 A=pioout 3
15 A=pioset 3
0 REM DTR output clear
16 A=pioout 6
17 A=pioclr 6
0 REM DSR input
18 A=pioin 4
0 REM set DSR to IRQ so that PIO_IRQ is called
19 A=pioirq "P000100"
20 RETURN
Running the Application using the Scheduler
The AIRcable Industrial knows 5 program controlled scheduled routines.
- @INIT
- @IDLE, @SLAVE
- @MASTER
- @ALARM
- @SENSOR
and 5 hardware or event scheduled routines.
- @PIN_CODE
- @PIO_IRQ
- @CONTROL
- @MESSAGE
- @INQUIRY
Slave Connections
@IDLE is called whenever a slave connection is not available or when a slave connection has closed. This routine starts right after @INIT. The BASIC program opens up the incoming serial slave port (SPP) for a number of seconds (A = slave 5) and then exits immediately. The serial slave port (SPP) stays now open for other Bluetooth devices to make connections. If a negative value is being used (e.g. A = slave -5) the AIRcable Industrial will not be discoverable during this time. Other Bluetooth devices must know the address to be able to make connections.
@IDLE will be called again when the time is up and no connection was established. At this time the BASIC program will again open up the slave port for incoming connections to SPP.
When a connection to the slave port was successful, meaning another Bluetooth device has initiated a connection to the AIRcable, the @SLAVE routine starts up. The BASIC program can find out who is connecting using the build-in function getconn (e.g. A = getconn $0).
For a simple slave application the program would just link the wireless SPP port to the UART. In this example, that's exactly what we do here.
@SLAVE 400
0 REM 5 seconds timeout to start shell with '+' and enter
400 TIMEOUTS 5
401 INPUTS $0
402 IF $0[0] = 43 THEN 407
403 B = pioset 2
0 REM set baud rate to 9600
404 C = baud 96
0 REM connect RS232
405 C = link 1
406 RETURN
407 PRINTS "shell started\r\nTAG$ "
408 RETURN
This routine allows an user to start an interactive session with the build-in shell. When a '+' and an enter is typed in immediately after the connections has been established the AIRcable Industrial starts with a shell. The shell is similar to a BASIC interpreter shell. It is possible to change lines of the BASIC program, start or stop routines, print variables and call built-in functions. See: http://www.aircable.net/support-ind-program-manual-pt3.html#command
A successful connection requires that the two partners are paired. PIN code is exchanged and then the a unique link key is stored on both ends to indicate a successful pairing. Then the pairing information is stored persistently for future connections that will not need a PIN code anymore. Pairing information can be erased either when more than 8 peers are stored or manually with the unpair function (single peers) or the @UNPAIR command (all peers) at the beginning of a BASIC program upload.
The PIN code that is used for all connections (slave, master, ftp and obex) is configured here in the BASIC program. The application then has more control over security this way. When @PIN_CODE is called, the Bluetooth address from the requester is available in the $0 string variable. This variable is being used for the PIN code response too.
@PIN_CODE 440
0 REM fixed PIN code
440 $0="1234"
441 RETURN
The last 2 routines @PIO_IRQ and @CONTROL deal with even change on the RS232 ports. If the DSR input on the DB9 connection changes, @PIO_IRQ is called. Remember we initialized that with the instruction "A=pioirq "P000100" in the @INIT routine.
The function modemctl transmits a message through an established SPP connection to the other end. Receiving this message will schedule the @CONTROL routine to be executed. In this example we set or reset the DTR pin of our RS232 connector.
@PIO_IRQ 490
0 REM local request for DSR
490 IF $0[4] = 48 THEN 483
0 REM modem control to other side
491 A = modemctl 1
492 RETURN
493 A = modemctl 0
494 RETURN
@CONTROL 495
0 REM remote request for DTR
495 IF $0[0] = 49 THEN 498
496 A=pioset 6
497 RETURN
498 A=pioclr 6
499 RETURN
The Code
Here attached is the whole program.
@ERASE
@INIT 10
0 REM LED output and on
10 A=pioout 2
11 A=pioset 2
0 REM RS232_off set
12 A=pioout 5
13 A=pioset 5
0 REM RS232_on set
14 A=pioout 3
15 A=pioset 3
0 REM DTR output clear
16 A=pioout 6
17 A=pioclr 6
0 REM DSR input
18 A=pioin 4
0 REM set DSR to IRQ so that PIO_IRQ is called
19 A=pioirq "P000100"
20 RETURN
@IDLE 30
0 REM disconnect RS232
30 B = unlink 1
31 B = slave 5
0 REM blink LED
32 B = pioset 2;
33 B = pioclr 2
34 RETURN
@SLAVE 400
0 REM 5 seconds timeout to start shell with '+' and enter
400 TIMEOUTS 5
401 INPUTS $0
402 IF $0[0] = 43 THEN 407
403 B = pioset 2
0 REM set baud rate to 9600
404 C = baud 96
0 REM connect RS232
405 C = link 1
406 RETURN
407 PRINTS "shell started\r\nTAG$ "
408 RETURN
@PIN_CODE 440
0 REM fixed PIN code
440 $0="1234"
441 RETURN
@PIO_IRQ 480
0 REM when DSR on the RS232 changes
480 IF $0[4]=48 THEN 483
0 REM modem control to other side
481 A=modemctl 1
482 RETURN
483 A=modemctl 0
484 RETURN
@CONTROL 495
0 REM remote request for DTR pin on the RS232
495 IF $0[0] = 49 THEN 498
496 A=pioset 6
497 RETURN
498 A=pioclr 6
499 RETURN
www.aircables.net/support-ind-program-manual-slavecode.html
|