Implementation of AFE4404 in mbed and C#

Dependencies:   AFE4404 EthernetInterface HTTPServer mbed-rpc mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "HTTPServer.h"
00004 #include "mbed_rpc.h"
00005 #include "RPCFunction.h"
00006 #include "AFE_4404.h"
00007 
00008 EthernetInterface eth;  
00009 HTTPServer svr;
00010 
00011 int baudRate = 115200;
00012 Serial pc(USBTX, USBRX); 
00013 
00014 extern int32_t data;
00015 extern bool dataAvailable;
00016 // p22: rxSupplyEn, p8: txSupplyEn, p6: resetz, p7: powerEn,
00017 // p5: drdy, p21: clk, p9: sda, p10: scl   
00018 AFE_4404 afe(p22, p8, p6, p7, p5, p21, p9, p10);
00019 
00020 Thread rpcThread;
00021 //automatically lock rpcBusy when it's been used
00022 volatile bool rpcBusy = true;
00023 void afeRpc(Arguments *input, Reply *output);
00024 RPCFunction afeRpcFunction(&afeRpc, "afe");
00025 
00026 void afeRpc(Arguments *input, Reply *output){
00027     
00028     int32_t registerAddress;
00029     int32_t registerData;
00030     int32_t suspend; // prevents main from writing to data if it's 1 
00031    
00032     // prevent main loop from writing data to serial
00033     rpcBusy = true;
00034     Thread::wait(100);
00035     
00036     registerAddress = input->getArg<int32_t>();
00037     registerData = input->getArg<int32_t>();
00038     suspend = input->getArg<int32_t>();
00039     
00040     // write data to afe register
00041     afe.writeData(
00042         static_cast<uint8_t>(registerAddress), 
00043         static_cast<uint32_t>(registerData));
00044     
00045     output->putData(registerAddress);
00046     output->putData(registerData);
00047     
00048     // suspend serial writes in main loop. functionality isn't currently
00049     // being used, so rpcBusy is always false in current C# GUI version
00050     rpcBusy = (suspend == 1) ? true : false;
00051 }
00052 
00053 void rpcThreadRoutine() {
00054     
00055     printf("Setting up...\n");
00056     eth.init();
00057     
00058     // wait for a while because the initialization doesn't work without the delay
00059     // maybe the initialization is done asynchronously??
00060     wait(10.0);
00061     
00062     int ethErr = eth.connect();
00063     
00064     if(ethErr < 0) 
00065         printf("Error %d in setup.\n", ethErr);
00066   
00067     svr.addHandler<HTTPRpcRequestHandler>("/rpc");
00068     //attach server to port 80
00069     svr.start(80, &eth);
00070     
00071     // gives enough time to pause terminal application to obtain IP address
00072     Thread::wait(2000);  
00073     printf("Listening...\n");
00074     
00075     rpcBusy = false;
00076     
00077     //Listen indefinitely
00078     while(true) {
00079         svr.poll();
00080         Thread::wait(1000);
00081     }    
00082 }
00083 
00084 int main() {
00085     
00086     pc.baud(baudRate);
00087     
00088     rpcThread.start(rpcThreadRoutine);   
00089     // spin until RPC is setup
00090     while(rpcBusy) {}
00091     
00092     afe.powerUpSequence();
00093     
00094     while(true){
00095         if(dataAvailable && !rpcBusy) {
00096             pc.printf("%d ", data);  
00097             dataAvailable = false;
00098         } 
00099     }
00100 }