Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:fe5850ccdb6f, committed 2013-02-26
- Comitter:
- Navin
- Date:
- Tue Feb 26 03:45:52 2013 +0000
- Child:
- 1:9d3340bcd863
- Commit message:
- Initial commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Console.h Tue Feb 26 03:45:52 2013 +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
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MbedConsole.cpp Tue Feb 26 03:45:52 2013 +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();
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MbedConsole.h Tue Feb 26 03:45:52 2013 +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
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MbedPerAccess.cpp Tue Feb 26 03:45:52 2013 +0000
@@ -0,0 +1,83 @@
+
+
+/**
+ * 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[] = {p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20,
+ p21, p22, p23, p24, p25, p26, p27, p28, p29, LED1, LED1, LED2, LED3, LED4
+ };
+ 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[] = {p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20,
+ p21, p22, p23, p24, p25, p26, p27, p28, p29
+ };
+ 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(p18);
+ if (val > _maxAnOutVal) {
+ return ERR_INVALID_ARG;
+ }
+ /* Only one analog out */
+ if (pinNo != 18) {
+ return ERR_INVALID_PIN;
+ }
+ aout = val;
+ 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)
+{
+ AnalogIn ana_in[] = { p15, p16, p17, p18, p19, p20};
+ if (pinNo > _maxAnInPins) {
+ return ERR_INVALID_PIN;
+ }
+
+ *outVal = ana_in[pinNo];
+ return ERR_SUCCESS;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MbedPerAccess.h Tue Feb 26 03:45:52 2013 +0000
@@ -0,0 +1,34 @@
+#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:
+#ifdef ARDUINO
+ static const uint _maxDigiPins = 14; /**< Maximun number of digital pins */
+ static const uint _maxAnInPins = 6; /**< Maximum number of ADC channels */
+ static const uint _maxAnOutVal = 255; /**< Maximum value that can be output by the PWM uint */
+#elif defined (MBED)
+ 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 = 1024; /**< Maximum value that can be output by the ADC uint */
+#endif
+
+
+};
+
+#endif
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PerAccess.h Tue Feb 26 03:45:52 2013 +0000
@@ -0,0 +1,19 @@
+/**
+ * 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TransLayer.cpp Tue Feb 26 03:45:52 2013 +0000
@@ -0,0 +1,166 @@
+/*
+ * 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.
+ *
+ */
+
+
+
+/**
+* \file Trans_layer.cpp
+* \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;
+ }
+ }
+ }
+}
+
+
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TransLayer.h Tue Feb 26 03:45:52 2013 +0000
@@ -0,0 +1,72 @@
+/*
+ * 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.
+ *
+ */
+
+/**
+* \file trans_layer.h
+* \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
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/error.h Tue Feb 26 03:45:52 2013 +0000 @@ -0,0 +1,17 @@ + + +/** + * 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Feb 26 03:45:52 2013 +0000
@@ -0,0 +1,131 @@
+#include "mbed.h"
+#include "TransLayer.h"
+#include "MbedConsole.h"
+#include "MbedPerAccess.h"
+
+
+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);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Feb 26 03:45:52 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e2ed12d17f06 \ No newline at end of file