Lets you control the FRDM-K64F board over the PC using the GUI software (https://github.com/navin-bhaskar/Controller) written in Python. To use the software, connect the FRDM-K64Fto your PC (via USB cable connected to USB port labelled as "open SDA") and start the software to control the FRDM-K64F. For more info on usage, please go to: http://navinbhaskar.blogspot.in/2013/02/arduino-controller-3.html
Dependencies: mbed
Revision 0:7ce46d3f9f5d, committed 2014-08-08
- Comitter:
- Navin
- Date:
- Fri Aug 08 02:27:42 2014 +0000
- Commit message:
- This application lets you control the FRDM-K64F Frdm board with a front end GUI software written in Python (https://github.com/navin-bhaskar/Controller)
Changed in this revision
diff -r 000000000000 -r 7ce46d3f9f5d Console.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Console.h Fri Aug 08 02:27:42 2014 +0000 @@ -0,0 +1,52 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + + + #ifndef __CONSOLE_H + #define __CONSOLE_H + /** + * \brief Provides the interfaces for console IO functionality + */ +#include "error.h" +class Console +{ + public: + virtual char getCh() = 0; + virtual void putCh(char ch) = 0; + virtual void puts(char *str) = 0; + virtual void printf(char *fmt, ...) = 0; + virtual void printErr(uint err) + { + char errMsgs[][20] = { + "ERROR", + "SUCCESS", + "Invalid pin", + "Invalid arguments" + }; + if (err > 4) + { + err = 0; + } + puts(&errMsgs[err][0]); + puts("OK"); + } + virtual int available() = 0; +}; + +#endif +
diff -r 000000000000 -r 7ce46d3f9f5d MbedConsole.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedConsole.cpp Fri Aug 08 02:27:42 2014 +0000 @@ -0,0 +1,40 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +#include "Console.h" +#include <stdarg.h> +#include "MbedConsole.h" +#include "mbed.h" + +extern Serial pc; + +void MbedConsole::printf(char * fmt, ...) +{ + char tmp[64]; + va_list args; + va_start(args, fmt); + vsnprintf(tmp, 64, fmt, args); + va_end(args); + pc.printf("%s", tmp); +} + +int MbedConsole::available() +{ + return pc.readable(); +} +
diff -r 000000000000 -r 7ce46d3f9f5d MbedConsole.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedConsole.h Fri Aug 08 02:27:42 2014 +0000 @@ -0,0 +1,47 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +#ifndef __MBEDCONSOLE_H +#define __MBEDCONSOLE_H +#include "Console.h" +#include "mbed.h" +#include <stdio.h> + +extern Serial pc; + +class MbedConsole : public Console +{ + public: + virtual char getCh() + { + return pc.getc(); + } + virtual void putCh(char ch) + { + pc.printf("%c", ch); + } + virtual void puts(char *str) + { + pc.printf("%s", str); + } + virtual void printf(char *fmt, ...); + virtual int available(); +}; + +#endif +
diff -r 000000000000 -r 7ce46d3f9f5d MbedPerAccess.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedPerAccess.cpp Fri Aug 08 02:27:42 2014 +0000 @@ -0,0 +1,108 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +/** +* \brief Implements the mbed perephiral access interface +* \author Navin Bhaskar +*/ + +/** + * Implements the peripheral access functionalities + */ + +#include "MbedPerAccess.h" +#include "error.h" +#include "mbed.h" + +/** + * Outputs the given logic level at the given pin + */ + +uint MbedPerAccess::digitalOut(uint pinNo, uint val) +{ + DigitalOut ports[] = { + D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13, D14, D15, + A0, A1, A2, A3, A4, A5, LED_RED, LED_GREEN, LED_BLUE + }; + if (pinNo > _maxDigiOutPins) { + + return ERR_INVALID_PIN; + } + if (val == 0) { + ports[pinNo] = 0; + } else { + ports[pinNo] = 1; + } + return ERR_SUCCESS; +} + +/** + * Reads the voltage level at given pin and returns + * it's logical value. + */ + +uint MbedPerAccess::digitalIn(uint pinNo, uint * val) +{ + DigitalIn ports[] = { + D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13, D14, D15, + A0, A1, A2, A3, A4, A5 + }; + if (pinNo > _maxDigiInPins) { + return ERR_INVALID_PIN; + } + + + *val = ports[pinNo]; + return ERR_SUCCESS; +} + +/** + * Outputs the analog value. + */ + +uint MbedPerAccess::analogOut(uint pinNo, uint val) +{ + AnalogOut aout(DAC0_OUT); + if (val > _maxAnOutVal) { + return ERR_INVALID_ARG; + } + /* Only one analog out */ + if (pinNo != 0) { + return ERR_INVALID_PIN; + } + aout = val/100.0; + return ERR_SUCCESS; +} + +/** + * Reads the volatge at the given analog input pin + * and returns the digital representation of the same + */ + +uint MbedPerAccess::analogIn(uint pinNo, uint * outVal) +{ + float val; + AnalogIn ana_in[] = { A0, A1, A2, A3, A4, A5 }; + if (pinNo > _maxAnInPins) { + return ERR_INVALID_PIN; + } + + val = ana_in[pinNo]; + *outVal = (int)(val*100); + return ERR_SUCCESS; +}
diff -r 000000000000 -r 7ce46d3f9f5d MbedPerAccess.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedPerAccess.h Fri Aug 08 02:27:42 2014 +0000 @@ -0,0 +1,54 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +/** +* \brief Implements the mbed perephiral access interface +* \author Navin Bhaskar +*/ + +#ifndef _ARD_PER_ACCESS_H +#define _ARD_PER_ACCESS_H + +#include "PerAccess.h" +#include "error.h" + +#ifndef MBED +#define MBED /**< Build for mbed */ +#endif + +class MbedPerAccess : public PerAccess +{ +public: + virtual uint digitalOut(uint pinNo, uint val); + virtual uint digitalIn(uint pinNo, uint * val); + virtual uint analogOut(uint pinNo, uint val); + virtual uint analogIn(uint pinNo, uint * outVal); +private: + + + static const uint _maxDigiOutPins = 25; /**< Maximun number of digital out pins */ + static const uint _maxDigiInPins = 25-4; /**< Maximum number of digital in pins */ + static const uint _maxAnInPins = 6; /**< Maximum number of ADC channels */ + static const uint _maxAnOutVal = 4096; /**< Maximum value that can be output by the ADC uint */ + + + +}; + +#endif +
diff -r 000000000000 -r 7ce46d3f9f5d PerAccess.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PerAccess.h Fri Aug 08 02:27:42 2014 +0000 @@ -0,0 +1,36 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ +/** + * Defines a abstract class that allows access to the peripherlas + * on board when implemented completely. + */ + +#ifndef _PER_ACCESS_H +#define _PER_ACCESS_H +#include "error.h" +#include <stdint.h> +class PerAccess +{ + public: + virtual uint digitalOut(uint pinNo, uint val) = 0; + virtual uint digitalIn(uint pinNo, uint * outVal) = 0; + virtual uint analogIn(uint pinNo, uint * outVal) = 0; + virtual uint analogOut(uint pinNo, uint val) = 0; + +}; +#endif
diff -r 000000000000 -r 7ce46d3f9f5d TransLayer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TransLayer.cpp Fri Aug 08 02:27:42 2014 +0000 @@ -0,0 +1,165 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + + + +/** +* \brief Implements a simple protocol for communication with PC +* \author Navin Bhaskar +*/ + +#include "TransLayer.h" + + + +/** +* \fn TransLayer() +* \brief constructor for trans layer. Initializes the pointers +* \param none +* \return none +*/ + +TransLayer::TransLayer() +{ + _head =_tail = NULL; +} + +/** +* \fn AddService(call_back_ptr cb, char flag) +* \brief Adds a service to the layer. The arguments are passed to +* the call back function +* \param[in] cb call back pointer +* \param[in] flag flag to which this packet must respond +* \return -1 on error +*/ + +int TransLayer::AddService(call_back_ptr cb, char flag) +{ + service_ptr temp=_head, mid; + + if (NULL == cb) { + return -1; + } + + // we are going to maintain a linked list of services + mid = (service_ptr)malloc(sizeof(service_list)); + + if (NULL == mid) { + return -1; + } + + // record entries + mid->service_function = cb; + mid->service_flag = flag; + if (NULL == _head) { + // first entry ever + _head = mid; + mid->next_service = NULL; + } else { + // traverese to end of the list for insertion + while (temp->next_service != NULL) { + temp = temp->next_service; + } + temp->next_service = mid; + mid->next_service = NULL; + } + return 0; +} + + + +/** +* \fn LookForService(char flag) +* \brief This function looks for a service that services given flag +* \param[in] flag flag of the service +* \return service ptr containing that has info on service or NULL if service flag +* was not found +*/ + +service_ptr TransLayer::LookForService(char flag) +{ + service_ptr temp = _head; + + while(temp != NULL) { + if(temp->service_flag == flag) { + return temp; + } + temp = temp->next_service; + } + + return NULL; +} + + +/** +* \fn MainLoop(void) +* \brief This function waits for a flag and then calls appropraite service +* \param none +* \return none +* \note This function never returns has a while(1) at its heart +*/ + +void TransLayer::MainLoop(Console * cons, PerAccess * per) +{ + char temp, cmd=0; + char data_buff[20]; + bool start_flag = false, data_start_flag=false, packet_formed = false; + int i =0; + + + while(1) { + if(cons->available() > 0) { + temp = cons->getCh(); + if(PACKET_START == temp && data_start_flag == false) { + // start of packet, siganl the starting of packet + start_flag = true; + i=0; + } else if (start_flag == true) { + cmd = temp; // this the flag that is going to indicate the service + start_flag = false; + data_start_flag = true; + } else if(data_start_flag == true) { + + if(temp != 0x0d || temp != 0x0a || temp != '\n') { + data_buff[i] = temp; + i++; + } + if(temp == 0x0d || temp == 0x0a) { + data_buff[i] = '\0'; + packet_formed = true; + data_start_flag = false; + } + + } + if(packet_formed == true) { + service_ptr ser; + //SeeServices(); + ser = LookForService(cmd); + if(ser != NULL) { + ser->service_function(cons, per, data_buff, i); + } + packet_formed = false; + } + } + } +} + + + + +
diff -r 000000000000 -r 7ce46d3f9f5d TransLayer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TransLayer.h Fri Aug 08 02:27:42 2014 +0000 @@ -0,0 +1,71 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +/** +* \brief This file contains functions defines and definations +* required by trans_layer.c +* \author Navin Bhaskar +*/ + + +#ifndef __TRANS_LAYER_H +#define __TRANS_LAYER_H + +#include <stdlib.h> +#include "Console.h" +#include "PerAccess.h" + + +typedef void(* call_back_ptr)(Console * cons, PerAccess * per, char*, int ); /**< typedef for callback function pointer for protocol packets*/ + +typedef struct service_list* service_ptr; /**< pointer to next service */ + +struct service_list /**< Struct for holding service list */ +{ + //char* service_name; /**< pointer to the name of the service */ + call_back_ptr service_function; /**< call back function pointer */ + char service_flag; /**< Packet service flag for which this service responds*/ + service_ptr next_service; +}; + + +#define PACKET_START 'S' /**< Start of packet indicator */ +#define QUERY_ADC_DATA 'A' /**< Control pcaket for quering ADC data */ +#define SET_LCD_DATA 'L' /**< To transmit string to be displayed on LCD */ +#define PACKET_ACK 'V' /**< Acknowledgement flag */ +#define PACKET_NACK 'N' /**< Negative acknowledgement */ +#define PACKET_DAT 'D' /**< Data follows after length field */ +#define PACKET_CMD 'C' /**< Command follows this field */ + +#define PIN_OP 'P' /**< Flag indicating pin operations */ + +class TransLayer +{ + private: + service_ptr _head, _tail; + public: + TransLayer(); + int AddService(call_back_ptr, char flag); + void MainLoop(Console *, PerAccess *); + service_ptr LookForService(char flag); +}; + +#define PRINT_OK Serial.println(" OK"); /**< A framing method */ + +#endif +
diff -r 000000000000 -r 7ce46d3f9f5d error.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/error.h Fri Aug 08 02:27:42 2014 +0000 @@ -0,0 +1,33 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ + +/** + * Defines all the error codes + */ + +#ifndef ERROR_H +#define ERROR_H + +typedef unsigned int uint; + +#define ERR_ERROR 0 /**< Generic error code */ +#define ERR_SUCCESS 1 /**< Success code */ +#define ERR_INVALID_PIN 2 /**< If there is no such physical pin */ +#define ERR_INVALID_ARG 3 /**< If the passed argument was invalid */ + +#endif
diff -r 000000000000 -r 7ce46d3f9f5d main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Aug 08 02:27:42 2014 +0000 @@ -0,0 +1,152 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ +#include "mbed.h" +#include "TransLayer.h" +#include "MbedConsole.h" +#include "MbedPerAccess.h" + +/** + * \brief The main entry point of our application. + * \author Navin Bhaskar + */ + +Serial pc(USBTX, USBRX); +/** +* \fn number(char* buff) +* \brief This function converts a string into a number. +* \param[in] buff buffer containg string representation of a number (should be decimal representation). +* \retrun converted number +*/ + +int number(char *buff) +{ + int i = strlen(buff); + int j,temp=0; + + for(j=0; j<i; j++) { + if(buff[j] >= '0' && buff[j] <= '9') { + temp = temp*10; + temp = temp + buff[j] - '0'; + + } + } + + return temp; +} +/** +* \fn pin_control(char* buff, int len) +* \brief sets or resets a pin +*/ +void pin_control(Console * cons, PerAccess * per, char* buff, int len) +{ + uint temp = number(buff); + uint pinno, pinst; + uint status; + + if( len < 3) { + cons->printErr(ERR_INVALID_ARG); + } else { + pinst = temp%10; // LSB is pin state + pinno = temp/10; // rest of it is pin number + status = per->digitalOut(pinno, pinst); + cons->printErr(status); + } +} + +/** +* \fn analog_out(char* buff, int len) +* \brief Outputs an anolog voltage on a given PWM channel +*/ + +void analog_out(Console * cons, PerAccess * per, char* buff, int len) +{ + int temp = number(buff); + int pinno, pinval; + uint status; + if( len < 3) { + cons->printErr(ERR_INVALID_ARG); + return ; + } + + pinno = temp&0xff; // LSB is pin value + pinval = temp>>8; // MSB is pin no + + status = per->analogOut(pinno, pinval); + cons->printErr(status); +} + +/** +* \fn analog_in(char* buff, int len) +* \brief This function reads an analog volatge on a given channel and prints +* it over on the serial terminal +*/ + +void analog_in(Console * cons, PerAccess * per, char* buff, int len) +{ + uint adc_val; + uint ch=number(buff); + uint status; + status = per->analogIn(ch, &adc_val); + if (status == ERR_SUCCESS) { + cons->printf("%d\n", adc_val); + } + cons->printErr(status); +} + +/** +* \fn read_pin(char* buff, int len) +* \brief This function reads digital logic level at a specified pin and prints +* it over serial port prints 1 if high else it prints 0 +*/ + +void read_pin(Console * cons, PerAccess * per, char* buff, int len) +{ + uint read_val; + uint pin=number(buff); + uint status; + + status = per->digitalIn(pin, &read_val); + + if (status == ERR_SUCCESS) { + cons->printf("%d\n", read_val); + } + cons->printErr(status); +} + + +int main(void) +{ + TransLayer comm_packet; + MbedConsole cons; + MbedPerAccess per; + + Console *transCons; + PerAccess *transPer; + + transCons = &cons; + transPer = &per; + + + comm_packet.AddService(pin_control, 'P'); + comm_packet.AddService(analog_out, 'A'); + comm_packet.AddService(analog_in, 'I'); + comm_packet.AddService(read_pin, 'R'); + comm_packet.MainLoop(transCons, transPer); + + while(1); +}
diff -r 000000000000 -r 7ce46d3f9f5d mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Aug 08 02:27:42 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/6213f644d804 \ No newline at end of file