RTOS Support.
Dependents: SerialRPC_rtos_example
Fork of RPCInterface by
SerialRPCInterface.cpp@9:2aa76667c340, 2015-04-09 (annotated)
- Committer:
- ban4jp
- Date:
- Thu Apr 09 18:02:10 2015 +0000
- Revision:
- 9:2aa76667c340
- Parent:
- 8:682c65afe534
Change to work with RTOS.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MichaelW | 5:56fd0b265aba | 1 | /** |
MichaelW | 5:56fd0b265aba | 2 | * @section LICENSE |
MichaelW | 5:56fd0b265aba | 3 | *Copyright (c) 2010 ARM Ltd. |
MichaelW | 5:56fd0b265aba | 4 | * |
MichaelW | 5:56fd0b265aba | 5 | *Permission is hereby granted, free of charge, to any person obtaining a copy |
MichaelW | 5:56fd0b265aba | 6 | *of this software and associated documentation files (the "Software"), to deal |
MichaelW | 5:56fd0b265aba | 7 | *in the Software without restriction, including without limitation the rights |
MichaelW | 5:56fd0b265aba | 8 | *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
MichaelW | 5:56fd0b265aba | 9 | *copies of the Software, and to permit persons to whom the Software is |
MichaelW | 5:56fd0b265aba | 10 | *furnished to do so, subject to the following conditions: |
MichaelW | 5:56fd0b265aba | 11 | * |
MichaelW | 5:56fd0b265aba | 12 | *The above copyright notice and this permission notice shall be included in |
MichaelW | 5:56fd0b265aba | 13 | *all copies or substantial portions of the Software. |
MichaelW | 5:56fd0b265aba | 14 | * |
MichaelW | 5:56fd0b265aba | 15 | *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
MichaelW | 5:56fd0b265aba | 16 | *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
MichaelW | 5:56fd0b265aba | 17 | *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
MichaelW | 5:56fd0b265aba | 18 | *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
MichaelW | 5:56fd0b265aba | 19 | *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
MichaelW | 5:56fd0b265aba | 20 | *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
MichaelW | 5:56fd0b265aba | 21 | *THE SOFTWARE. |
MichaelW | 5:56fd0b265aba | 22 | * |
MichaelW | 5:56fd0b265aba | 23 | * |
MichaelW | 5:56fd0b265aba | 24 | * @section DESCRIPTION |
MichaelW | 5:56fd0b265aba | 25 | * |
MichaelW | 5:56fd0b265aba | 26 | *This class sets up RPC communication. This allows objects on mbed to be controlled. Objects can be created or existing objects can be used |
MichaelW | 5:56fd0b265aba | 27 | */ |
MichaelW | 5:56fd0b265aba | 28 | #include "SerialRPCInterface.h" |
MichaelW | 5:56fd0b265aba | 29 | |
MichaelW | 5:56fd0b265aba | 30 | using namespace mbed; |
MichaelW | 5:56fd0b265aba | 31 | |
MichaelW | 5:56fd0b265aba | 32 | //Requires multiple contstructors for each type, serial to set different pin numbers, TCP for port. |
ban4jp | 9:2aa76667c340 | 33 | SerialRPCInterface::SerialRPCInterface(PinName tx, PinName rx, int baud):pc(tx, rx), _thread(SerialRPCInterface::_MsgProcessStarter, this, osPriorityNormal, 1024) { |
MichaelW | 5:56fd0b265aba | 34 | _RegClasses(); |
MichaelW | 5:56fd0b265aba | 35 | _enabled = true; |
ban4jp | 9:2aa76667c340 | 36 | _command_ptr = _command; |
MichaelW | 5:56fd0b265aba | 37 | pc.attach(this, &SerialRPCInterface::_RPCSerial, Serial::RxIrq); |
MichaelW | 5:56fd0b265aba | 38 | if(baud != 9600)pc.baud(baud); |
MichaelW | 5:56fd0b265aba | 39 | } |
MichaelW | 5:56fd0b265aba | 40 | |
MichaelW | 5:56fd0b265aba | 41 | void SerialRPCInterface::_RegClasses(void){ |
MichaelW | 5:56fd0b265aba | 42 | //Register classes with base |
ban4jp | 9:2aa76667c340 | 43 | //RPC::add_rpc_class<RpcAnalogIn>(); |
ban4jp | 9:2aa76667c340 | 44 | RPC::add_rpc_class<RpcDigitalIn>(); |
ban4jp | 9:2aa76667c340 | 45 | RPC::add_rpc_class<RpcDigitalOut>(); |
ban4jp | 9:2aa76667c340 | 46 | RPC::add_rpc_class<RpcDigitalInOut>(); |
ban4jp | 9:2aa76667c340 | 47 | RPC::add_rpc_class<RpcPwmOut>(); |
ban4jp | 9:2aa76667c340 | 48 | RPC::add_rpc_class<RpcTimer>(); |
ban4jp | 9:2aa76667c340 | 49 | //RPC::add_rpc_class<RpcBusOut>(); |
ban4jp | 9:2aa76667c340 | 50 | //RPC::add_rpc_class<RpcBusIn>(); |
ban4jp | 9:2aa76667c340 | 51 | //RPC::add_rpc_class<RpcBusInOut>(); |
ban4jp | 9:2aa76667c340 | 52 | RPC::add_rpc_class<RpcSerial>(); |
ban4jp | 9:2aa76667c340 | 53 | RPC::add_rpc_class<RpcSPI>(); |
ban4jp | 9:2aa76667c340 | 54 | //RPC::add_rpc_class<RpcI2C>(); |
MichaelW | 8:682c65afe534 | 55 | |
MichaelW | 8:682c65afe534 | 56 | //AnalogOut not avaliable on mbed LPC11U24 so only compile for other devices |
MichaelW | 8:682c65afe534 | 57 | #if !defined(TARGET_LPC11U24) |
ban4jp | 9:2aa76667c340 | 58 | //RPC::add_rpc_class<RpcAnalogOut>(); |
MichaelW | 8:682c65afe534 | 59 | #endif |
MichaelW | 5:56fd0b265aba | 60 | } |
MichaelW | 5:56fd0b265aba | 61 | |
MichaelW | 5:56fd0b265aba | 62 | void SerialRPCInterface::Disable(void){ |
MichaelW | 5:56fd0b265aba | 63 | _enabled = false; |
MichaelW | 5:56fd0b265aba | 64 | } |
MichaelW | 5:56fd0b265aba | 65 | void SerialRPCInterface::Enable(void){ |
MichaelW | 5:56fd0b265aba | 66 | _enabled = true; |
MichaelW | 5:56fd0b265aba | 67 | } |
ban4jp | 9:2aa76667c340 | 68 | void SerialRPCInterface::_MsgProcessStarter(const void *args){ |
ban4jp | 9:2aa76667c340 | 69 | SerialRPCInterface *instance = (SerialRPCInterface*)args; |
ban4jp | 9:2aa76667c340 | 70 | while(1){ |
ban4jp | 9:2aa76667c340 | 71 | instance->_MsgProcess(); |
ban4jp | 9:2aa76667c340 | 72 | } |
ban4jp | 9:2aa76667c340 | 73 | } |
MichaelW | 5:56fd0b265aba | 74 | void SerialRPCInterface::_MsgProcess(void) { |
ban4jp | 9:2aa76667c340 | 75 | osEvent evt = _commandMail.get(); |
ban4jp | 9:2aa76667c340 | 76 | if(evt.status == osEventMail){ |
ban4jp | 9:2aa76667c340 | 77 | mail_t *mail = (mail_t*)evt.value.p; |
ban4jp | 9:2aa76667c340 | 78 | if(_enabled == true){ |
ban4jp | 9:2aa76667c340 | 79 | RPC::call(_command, _response); |
ban4jp | 9:2aa76667c340 | 80 | pc.printf("%s\n", _response); |
ban4jp | 9:2aa76667c340 | 81 | } |
ban4jp | 9:2aa76667c340 | 82 | _commandMail.free(mail); |
MichaelW | 5:56fd0b265aba | 83 | } |
MichaelW | 5:56fd0b265aba | 84 | } |
MichaelW | 5:56fd0b265aba | 85 | |
MichaelW | 5:56fd0b265aba | 86 | void SerialRPCInterface::_RPCSerial() { |
MichaelW | 5:56fd0b265aba | 87 | _RPCflag = true; |
MichaelW | 5:56fd0b265aba | 88 | if(_enabled == true){ |
ban4jp | 9:2aa76667c340 | 89 | char c = pc.getc(); |
ban4jp | 9:2aa76667c340 | 90 | if(c == 13){ |
ban4jp | 9:2aa76667c340 | 91 | *_command_ptr = '\0'; |
ban4jp | 9:2aa76667c340 | 92 | mail_t *mail = _commandMail.alloc(); |
ban4jp | 9:2aa76667c340 | 93 | strcpy(mail->command, _command); |
ban4jp | 9:2aa76667c340 | 94 | _commandMail.put(mail); |
ban4jp | 9:2aa76667c340 | 95 | _command_ptr = _command; |
ban4jp | 9:2aa76667c340 | 96 | }else if(_command_ptr - _command < 256){ |
ban4jp | 9:2aa76667c340 | 97 | *_command_ptr = c; |
ban4jp | 9:2aa76667c340 | 98 | _command_ptr++; |
ban4jp | 9:2aa76667c340 | 99 | } |
MichaelW | 5:56fd0b265aba | 100 | } |
MichaelW | 5:56fd0b265aba | 101 | _RPCflag = false; |
MichaelW | 5:56fd0b265aba | 102 | } |