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:c92ca0229c9a, committed 2014-05-09
- Comitter:
- knuderich
- Date:
- Fri May 09 11:30:55 2014 +0000
- Commit message:
- P029 P030 gpio
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/MCP23008/MCP23008.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,154 @@ +/* MCP23017 library for Arduino + Copyright (C) 2009 David Pye <davidmpye@gmail.com + Modified for use on the MBED ARM platform + + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#include "MCP23008.h" +#include "mbed.h" + +//=============================================================================================== +// Init and setup functions +//=============================================================================================== + +/*----------------------------------------------------------------------------- + * + */ +MCP23008::MCP23008(PinName sda, PinName scl, int i2cAddress) : _i2c(sda, scl) +{ + MCP23008_i2cAddress = i2cAddress; + reset(); // initialise chip to power-on condition +} + +/*----------------------------------------------------------------------------- + * reset + * Set configuration (IOCON) and direction(IODIR) registers to initial state + */ +void MCP23008::reset() +{ + // + // set direction registers to inputs + // + writeRegister(IODIR, (unsigned char)0xFF); + // + // set all other registers to zero (last of 10 registers is OLAT) + // + for (int reg_addr = IPOL ; reg_addr <= OLAT ; reg_addr++) { + writeRegister(reg_addr, (unsigned char)0x00); + } + + // + // Set the shadow registers to power-on state + // + shadow_IODIR = 0xFF; + shadow_GPIO = 0; + shadow_GPPU = 0; + shadow_IPOL = 0; +} + + + +/*----------------------------------------------------------------------------- + * Config + * set direction and pull-up registers + */ +void MCP23008::config(unsigned char dir_config, unsigned char pullup_config, unsigned char polarity_config) +{ + shadow_IODIR = dir_config; + writeRegister(IODIR, (unsigned char)shadow_IODIR); + shadow_GPPU = pullup_config; + writeRegister(GPPU, (unsigned char)shadow_GPPU); + shadow_IPOL = polarity_config; + writeRegister(IPOL, (unsigned char)shadow_IPOL); +} + + + +/*----------------------------------------------------------------------------- + * writeRegister + * write a byte + */ +void MCP23008::writeRegister(int regAddress, unsigned char data) +{ + char buffer[2]; + + buffer[0] = regAddress; + buffer[1] = data; + _i2c.write(MCP23008_i2cAddress, buffer, 2); +} + + + +/*----------------------------------------------------------------------------- + * readRegister + */ +unsigned char MCP23008::readRegister(int regAddress) +{ + char buffer[1]; + + buffer[0] = regAddress; + _i2c.write(MCP23008_i2cAddress, buffer, 1); + _i2c.read(MCP23008_i2cAddress, buffer, 1); + + return ( (unsigned char)buffer[0] ); +} + + +//=============================================================================================== +// User functions +//=============================================================================================== + +/*----------------------------------------------------------------------------- + * Write a combination of bits to the 8-bit port + */ +void MCP23008::write_mask(unsigned char data, unsigned char mask) +{ + shadow_GPIO = (shadow_GPIO & ~mask) | data; + writeRegister(GPIO, (unsigned char)shadow_GPIO); +} + +/*----------------------------------------------------------------------------- + * read_mask + */ +int MCP23008::read_mask(unsigned char mask) +{ + shadow_GPIO = readRegister(GPIO); + return (shadow_GPIO & mask); +} + + +/*----------------------------------------------------------------------------- + * write_bit + * Write a 1/0 to a single bit of the 16-bit port + */ +void MCP23008::write_bit(int value, int bit_number) { + if (value == 0) { + shadow_GPIO &= ~(1 << bit_number); + } else { + shadow_GPIO |= 1 << bit_number; + } + writeRegister(GPIO, (unsigned char)shadow_GPIO); +} + + +/*----------------------------------------------------------------------------- + * read_bit + * Read a single bit from the 16-bit port + */ +int MCP23008::read_bit(int bit_number) { + shadow_GPIO = readRegister(GPIO); + return ((shadow_GPIO >> bit_number) & 0x01); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/MCP23008/MCP23008.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,133 @@ +/* MCP23008 library for Arduino + Copyright (C) 2013 Stefan Jaensch, based on MCP23017 from David Pye <davidmpye@gmail.com + Modified for use on the MBED ARM platform + + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef MBED_MCP23008_H +#define MBED_MCP23008_H + +#include "mbed.h" + +// +// Register defines from data sheet - we set IOCON.BANK to 0 +// as it is easier to manage the registers sequentially. +// +#define IODIR 0x00 +#define IPOL 0x01 +#define GPINTEN 0x02 +#define DEFVAL 0x03 +#define INTCON 0x04 +#define IOCON 0x05 +#define GPPU 0x06 +#define INTF 0x07 +#define INTCAP 0x08 +#define GPIO 0x09 +#define OLAT 0x0A + +#define I2C_BASE_ADDRESS 0x40 + +#define DIR_OUTPUT 0 +#define DIR_INPUT 1 + + +#define MCP23008_GP0 ( 0x01 ) +#define MCP23008_GP1 ( 0x02 ) +#define MCP23008_GP2 ( 0x04 ) +#define MCP23008_GP3 ( 0x08 ) +#define MCP23008_GP4 ( 0x10 ) +#define MCP23008_GP5 ( 0x20 ) +#define MCP23008_GP6 ( 0x40 ) +#define MCP23008_GP7 ( 0x80 ) + + +/** MCP23008 class + * + * Allow access to an I2C connected MCP23008 8-bit I/O extender chip + * Example: + * @code + * MCP23008 *par_port; + ... + par_port = new MCP23008( p9, p10, I2C_ADD); + par_port->config(0xF0,0,0); + * @endcode + * + */ +class MCP23008 { +public: + /** Constructor for the MCP23008 connected to specified I2C pins at a specific address + * + * 8-bit I/O expander with I2C interface + * + * @param sda I2C data pin + * @param scl I2C clock pin + * @param i2cAddress I2C address + */ + MCP23008(PinName sda, PinName scl, int i2cAddress); + + /** Reset MCP23008 device to its power-on state + */ + void reset(void); + + /** Configure an MCP23008 device + * + * @param dir_config data direction value (1 = input, 0 = output) + * @param pullup_config 100k pullup value (1 = enabled, 0 = disabled) + * @param polarity_config polarity value (1 = flip, 0 = normal) + */ + void config(unsigned char dir_config, unsigned char pullup_config, unsigned char polarity_config); + + /** Write a masked 16-bit value to the device + * + * @param data 8-bit data value + * @param mask 8-bit mask value + */ + void write_mask(unsigned char data, unsigned char mask); + + /** Read a 8-bit value from the device and apply mask + * + * @param mask 8-bit mask value + * @return 8-bit data with mask applied + */ + int read_mask(unsigned char mask); + + /** Write a 0/1 value to an output bit + * + * @param value 0 or 1 + * @param bit_number bit number range 0 --> 7 + */ + void write_bit(int value, int bit_number); + + /** Read a 0/1 value from an input bit + * + * @param bit_number bit number range 0 --> 7 + * @return 0/1 value read + */ + int read_bit(int bit_number); + + + + +protected: + void writeRegister(int regAddress, unsigned char data); + unsigned char readRegister(int regAddress); + + + I2C _i2c; + int MCP23008_i2cAddress; // physical I2C address + unsigned char shadow_GPIO, shadow_IODIR, shadow_GPPU, shadow_IPOL; // Cached copies of the register values +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/MCP23017/MCP23017.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,242 @@ +/* MCP23017 library for Arduino + Copyright (C) 2009 David Pye <davidmpye@gmail.com + Modified for use on the MBED ARM platform + + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#include "MCP23017.h" +#include "mbed.h" + +union { + uint8_t value8[2]; + uint16_t value16; +} tmp_data; + +/*----------------------------------------------------------------------------- + * + */ +MCP23017::MCP23017(PinName sda, PinName scl, int i2cAddress) : _i2c(sda, scl) { + MCP23017_i2cAddress = i2cAddress; + reset(); // initialise chip to power-on condition +} + +/*----------------------------------------------------------------------------- + * reset + * Set configuration (IOCON) and direction(IODIR) registers to initial state + */ +void MCP23017::reset() { +// +// First make sure that the device is in BANK=0 mode +// + writeRegister(0x05, (unsigned char)0x00); +// +// set direction registers to inputs +// + writeRegister(IODIR, (unsigned short)0xFFFF); +// +// set all other registers to zero (last of 10 registers is OLAT) +// + for (int reg_addr = 2 ; reg_addr <= OLAT ; reg_addr+=2) { + writeRegister(reg_addr, (unsigned short)0x0000); + } +// +// Set the shadow registers to power-on state +// + shadow_IODIR = 0xFFFF; + shadow_GPIO = 0; + shadow_GPPU = 0; + shadow_IPOL = 0; +} + +/*----------------------------------------------------------------------------- + * write_bit + * Write a 1/0 to a single bit of the 16-bit port + */ +void MCP23017::write_bit(int value, int bit_number) { + if (value == 0) { + shadow_GPIO &= ~(1 << bit_number); + } else { + shadow_GPIO |= 1 << bit_number; + } + writeRegister(GPIO, (unsigned short)shadow_GPIO); +} + +/*----------------------------------------------------------------------------- + * Write a combination of bits to the 16-bit port + */ +void MCP23017::write_mask(unsigned short data, unsigned short mask) { + shadow_GPIO = (shadow_GPIO & ~mask) | data; + writeRegister(GPIO, (unsigned short)shadow_GPIO); +} + +/*----------------------------------------------------------------------------- + * read_bit + * Read a single bit from the 16-bit port + */ +int MCP23017::read_bit(int bit_number) { + shadow_GPIO = readRegister(GPIO); + return ((shadow_GPIO >> bit_number) & 0x0001); +} + +/*----------------------------------------------------------------------------- + * read_mask + */ +int MCP23017::read_mask(unsigned short mask) { + shadow_GPIO = readRegister(GPIO); + return (shadow_GPIO & mask); +} + +/*----------------------------------------------------------------------------- + * Config + * set direction and pull-up registers + */ +void MCP23017::config(unsigned short dir_config, unsigned short pullup_config, unsigned short polarity_config) { + shadow_IODIR = dir_config; + writeRegister(IODIR, (unsigned short)shadow_IODIR); + shadow_GPPU = pullup_config; + writeRegister(GPPU, (unsigned short)shadow_GPPU); + shadow_IPOL = polarity_config; + writeRegister(IPOL, (unsigned short)shadow_IPOL); +} + +/*----------------------------------------------------------------------------- + * writeRegister + * write a byte + */ +void MCP23017::writeRegister(int regAddress, unsigned char data) { + char buffer[2]; + + buffer[0] = regAddress; + buffer[1] = data; + _i2c.write(MCP23017_i2cAddress, buffer, 2); +} + +/*---------------------------------------------------------------------------- + * write Register + * write two bytes + */ +void MCP23017::writeRegister(int regAddress, unsigned short data) { + char buffer[3]; + + buffer[0] = regAddress; + tmp_data.value16 = data; + buffer[1] = tmp_data.value8[0]; + buffer[2] = tmp_data.value8[1]; + + _i2c.write(MCP23017_i2cAddress, buffer, 3); +} + +/*----------------------------------------------------------------------------- + * readRegister + */ +int MCP23017::readRegister(int regAddress) { + char buffer[2]; + + buffer[0] = regAddress; + _i2c.write(MCP23017_i2cAddress, buffer, 1); + _i2c.read(MCP23017_i2cAddress, buffer, 2); + + return ((int)(buffer[0] + (buffer[1]<<8))); +} + +/*----------------------------------------------------------------------------- + * pinMode + */ +void MCP23017::pinMode(int pin, int mode) { + if (DIR_INPUT) { + shadow_IODIR |= 1 << pin; + } else { + shadow_IODIR &= ~(1 << pin); + } + writeRegister(IODIR, (unsigned short)shadow_IODIR); +} + +/*----------------------------------------------------------------------------- + * digitalRead + */ +int MCP23017::digitalRead(int pin) { + shadow_GPIO = readRegister(GPIO); + if ( shadow_GPIO & (1 << pin)) { + return 1; + } else { + return 0; + } +} + +/*----------------------------------------------------------------------------- + * digitalWrite + */ +void MCP23017::digitalWrite(int pin, int val) { + //If this pin is an INPUT pin, a write here will + //enable the internal pullup + //otherwise, it will set the OUTPUT voltage + //as appropriate. + bool isOutput = !(shadow_IODIR & 1<<pin); + + if (isOutput) { + //This is an output pin so just write the value + if (val) shadow_GPIO |= 1 << pin; + else shadow_GPIO &= ~(1 << pin); + writeRegister(GPIO, (unsigned short)shadow_GPIO); + } else { + //This is an input pin, so we need to enable the pullup + if (val) { + shadow_GPPU |= 1 << pin; + } else { + shadow_GPPU &= ~(1 << pin); + } + writeRegister(GPPU, (unsigned short)shadow_GPPU); + } +} + +/*----------------------------------------------------------------------------- + * digitalWordRead + */ +unsigned short MCP23017::digitalWordRead() { + shadow_GPIO = readRegister(GPIO); + return shadow_GPIO; +} + +/*----------------------------------------------------------------------------- + * digitalWordWrite + */ +void MCP23017::digitalWordWrite(unsigned short w) { + shadow_GPIO = w; + writeRegister(GPIO, (unsigned short)shadow_GPIO); +} + +/*----------------------------------------------------------------------------- + * inputPolarityMask + */ +void MCP23017::inputPolarityMask(unsigned short mask) { + writeRegister(IPOL, mask); +} + +/*----------------------------------------------------------------------------- + * inputoutputMask + */ +void MCP23017::inputOutputMask(unsigned short mask) { + shadow_IODIR = mask; + writeRegister(IODIR, (unsigned short)shadow_IODIR); +} + +/*----------------------------------------------------------------------------- + * internalPullupMask + */ +void MCP23017::internalPullupMask(unsigned short mask) { + shadow_GPPU = mask; + writeRegister(GPPU, (unsigned short)shadow_GPPU); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/MCP23017/MCP23017.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,143 @@ +/* MCP23017 library for Arduino + Copyright (C) 2009 David Pye <davidmpye@gmail.com + Modified for use on the MBED ARM platform + + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef MBED_MCP23017_H +#define MBED_MCP23017_H + +#include "mbed.h" + +// +// Register defines from data sheet - we set IOCON.BANK to 0 +// as it is easier to manage the registers sequentially. +// +#define IODIR 0x00 +#define IPOL 0x02 +#define GPINTEN 0x04 +#define DEFVAL 0x06 +#define INTCON 0x08 +#define IOCON 0x0A +#define GPPU 0x0C +#define INTF 0x0E +#define INTCAP 0x10 +#define GPIO 0x12 +#define OLAT 0x14 + +#define I2C_BASE_ADDRESS 0x40 + +#define DIR_OUTPUT 0 +#define DIR_INPUT 1 + +/** MCP23017 class + * + * Allow access to an I2C connected MCP23017 16-bit I/O extender chip + * Example: + * @code + * MCP23017 *par_port; + * @endcode + * + */ +class MCP23017 { +public: + /** Constructor for the MCP23017 connected to specified I2C pins at a specific address + * + * 16-bit I/O expander with I2C interface + * + * @param sda I2C data pin + * @param scl I2C clock pin + * @param i2cAddress I2C address + */ + MCP23017(PinName sda, PinName scl, int i2cAddress); + + /** Reset MCP23017 device to its power-on state + */ + void reset(void); + + /** Write a 0/1 value to an output bit + * + * @param value 0 or 1 + * @param bit_number bit number range 0 --> 15 + */ + void write_bit(int value, int bit_number); + + /** Write a masked 16-bit value to the device + * + * @param data 16-bit data value + * @param mask 16-bit mask value + */ + void write_mask(unsigned short data, unsigned short mask); + + /** Read a 0/1 value from an input bit + * + * @param bit_number bit number range 0 --> 15 + * @return 0/1 value read + */ + int read_bit(int bit_number); + + /** Read a 16-bit value from the device and apply mask + * + * @param mask 16-bit mask value + * @return 16-bit data with mask applied + */ + int read_mask(unsigned short mask); + + /** Configure an MCP23017 device + * + * @param dir_config data direction value (1 = input, 0 = output) + * @param pullup_config 100k pullup value (1 = enabled, 0 = disabled) + * @param polarity_config polarity value (1 = flip, 0 = normal) + */ + void config(unsigned short dir_config, unsigned short pullup_config, unsigned short polarity_config); + + void writeRegister(int regAddress, unsigned char val); + void writeRegister(int regAddress, unsigned short val); + int readRegister(int regAddress); + +/*----------------------------------------------------------------------------- + * pinmode + * Set units to sequential, bank0 mode + */ + void pinMode(int pin, int mode); + void digitalWrite(int pin, int val); + int digitalRead(int pin); + +// These provide a more advanced mapping of the chip functionality +// See the data sheet for more information on what they do + +//Returns a word with the current pin states (ie contents of the GPIO register) + unsigned short digitalWordRead(); +// Allows you to write a word to the GPIO register + void digitalWordWrite(unsigned short w); +// Sets up the polarity mask that the MCP23017 supports +// if set to 1, it will flip the actual pin value. + void inputPolarityMask(unsigned short mask); +//Sets which pins are inputs or outputs (1 = input, 0 = output) NB Opposite to arduino's +//definition for these + void inputOutputMask(unsigned short mask); +// Allows enabling of the internal 100k pullup resisters (1 = enabled, 0 = disabled) + void internalPullupMask(unsigned short mask); + int read(void); + void write(int data); + +protected: + I2C _i2c; + int MCP23017_i2cAddress; // physical I2C address + unsigned short shadow_GPIO, shadow_IODIR, shadow_GPPU, shadow_IPOL; // Cached copies of the register values + +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/RS485.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,198 @@ +#include "mbed.h" +#include "RS485.h" +#include "crc16.h" + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- +Serial uartRS485(p28, p27); +DigitalOut RS485RTS( p20 ); +Timeout RS485Timeout; +Timeout RS485TimeoutTx; + + + +//--------------------------------------- +// Prototypes +//--------------------------------------- +void RS485_initStructure( void ); +void RS485_rxCallback( void ); +void RS485_rxTimeout( void ); +void RS485_txTimeout( void ); + +// void testcomm(void); // UpdateAll + +//--------------------------------------- +// Internal variables +//--------------------------------------- +char RS485Buffer[RS485_BUFFER_LENGTH]; + + + +//--------------------------------------- +// External variables +//--------------------------------------- +sRS485_handler RS485_handler; + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- + +void RS485_init( void ) +{ + uartRS485.baud(RS485_BAUDRATE); + // uartRS485.format(RS485_BITS, Serial::None, RS485_STOPBIT ); + uartRS485.format(RS485_BITS, Serial::Even, RS485_STOPBIT ); // 21.01.2014 KED + RS485RTS = 0; + RS485_reset(); + uartRS485.attach( &RS485_rxCallback, Serial::RxIrq ); // call RS485_rx() on every RX interrupt + + /* + #warning TEST + testcomm(); // UpdateAll + wait(1.0); + testcomm(); // UpdateAll + */ +} + + +void RS485_reset( void ) +{ + RS485_handler.ptrRXBuffer = &RS485Buffer[0]; + RS485_handler.bytesToRead = 0; + RS485_handler.mode = RS485_MODE_LISTEN; +} + + +// Return 1: Sending was successfull +// Return 0: Could not send data zero bytes +int RS485_sendData( char *ptrDataToSend, int NumBytes ) +{ + int i; + + if( NumBytes > 0 ) + { + // Set RS485 driver to sending mode + RS485RTS = 1; + + // Send data + for( i = 0; i < NumBytes; i++ ) + { + while( !uartRS485.writeable() ); + uartRS485.putc(*ptrDataToSend); + ptrDataToSend++; + }//for + + // Restart timeout timer + RS485TimeoutTx.attach_us( &RS485_txTimeout, RS485_TX_TIMEOUT_US ); + + return 1; + }//if + + return 0; +} + + + + + + +// Return 1: New data available, get pointer to buffer and number of bytes to read +// Return 0: No new data available +int RS485_receiveData( char *& ptrData, int *ptrNumBytes ) +{ + // Dummy operation to avoid compiler warning + ptrData++; + + if( RS485_handler.mode != RS485_MODE_RX_READY ) + { + *ptrNumBytes = 0; + return 0; + } + + *ptrNumBytes = RS485_handler.bytesToRead; + ptrData = &RS485Buffer[0]; + + RS485_reset(); + return 1; +} + + + +//--------------------------------------- +// Internal Functions +//--------------------------------------- + +// Function is called on RX interrupt +void RS485_rxCallback( void ) +{ + char tempChar; + + tempChar = uartRS485.getc(); + + // Only save data, if not in blocked mode + if( RS485_handler.mode == RS485_MODE_LISTEN ) { + if( RS485_handler.bytesToRead < RS485_BUFFER_LENGTH ) { // no buffer overflow + // Restart timeout timer + RS485Timeout.attach_us( &RS485_rxTimeout, RS485_RX_TIMEOUT_US ); + // Save received byte + *RS485_handler.ptrRXBuffer = tempChar; + RS485_handler.ptrRXBuffer++; + RS485_handler.bytesToRead++; + } + }// if +} + + +void RS485_txTimeout( void ) +{ + // Set RS485 driver to receiving mode + RS485RTS = 0; +} + + +void RS485_rxTimeout( void ) +{ + // Disable timeout + RS485Timeout.detach(); + RS485_handler.mode = RS485_MODE_RX_READY; // Transmission is ready +} + + +/* +// TEST LCD communication +void testcomm(void) // UpdateAll +{ + char i; + unsigned short clacCRC16; + + RS485Buffer[0] = 0x00; // Address + RS485Buffer[1] = 0x04; // Command = UpdateAll + + for (i=0; i < 80; i++) + { + RS485Buffer[i+2] = 0x30; + } + + RS485Buffer[82] = 0x01; // linie + RS485Buffer[83] = 0x02; // pos + RS485Buffer[84] = 0x03; // BlinkingCurserOn + + + RS485Buffer[85] = 0x00; // LED Error + + RS485Buffer[86] = 0x00; // high Contrast + RS485Buffer[87] = 0x00; // low Contrast + + + calcCRC16(&RS485Buffer[0], 88, &clacCRC16); + RS485Buffer[88] = (unsigned char)(0x00FF & (clacCRC16>>8)); // set High Byte + RS485Buffer[89] = (unsigned char)(0x00FF & clacCRC16); // set Low Byte + + RS485_sendData( &RS485Buffer[0], 90 ); +} + +*/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/RS485.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,52 @@ +#include "mbed.h" + + +//--------------------------------------- +// Definitions +//--------------------------------------- +#define RS485_BUFFER_LENGTH 512 +#define RS485_RX_TIMEOUT_US 300 +#define RS485_TX_TIMEOUT_US 300 +#define RS485_BAUDRATE 38400 // Same as Display +#define RS485_BITS 8 +#define RS485_STOPBIT 1 + +//--------------------------------------- +// Enums +//--------------------------------------- +typedef enum +{ + RS485_MODE_LISTEN, + RS485_MODE_RX_READY, + //----------------- + CNT_eRS485_mode +}eRS485_mode; + + +//--------------------------------------- +// Structures +//--------------------------------------- +typedef struct +{ + eRS485_mode mode; // mode of RS485 hardware + char *ptrRXBuffer; // actual read position in buffer + char bytesToRead; // Number of received bytes +}sRS485_handler; + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- +extern sRS485_handler RS485_handler; +extern char RS485Buffer[RS485_BUFFER_LENGTH]; + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void RS485_init( void ); +void RS485_reset( void ); // clear all RS485 registers, --> restart +int RS485_receiveData( char *& ptrData, int *ptrNumBytes ); +int RS485_sendData( char *ptrDataToSend, int NumBytes ); +int RS485_sendDataWithCRC( char *ptrDataToSend, int NumBytes ); \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/ana.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,106 @@ +#include "mbed.h" +#include "ana.h" +#include "filter.h" + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- +AnalogIn ana1(p15); +AnalogIn ana2(p16); +AnalogIn ana3(p17); +Ticker ana_sw_timer; + + +//--------------------------------------- +// Prototypes +//--------------------------------------- +static void ANA_periodical( void ); + + + +//--------------------------------------- +// Internal variables +//--------------------------------------- +static sFLT_LP1Order16BitHandler filterHandlerAna1; +static sFLT_LP1Order16BitHandler filterHandlerAna2; +static sFLT_LP1Order16BitHandler filterHandlerAna3; +static unsigned short ANA_rawSamples[CNT_eANA_Channels]; +static unsigned short ANA_filteredSamples[CNT_eANA_Channels]; + + +//--------------------------------------- +// External variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void ANA_init( void ) +{ + int i; + + for( i = 0; i < CNT_eANA_Channels; i++ ) + { + ANA_rawSamples[i] = 0; + ANA_filteredSamples[i] = 0; + } + + filterHandlerAna1.filterShift = ANA_LP_FILTER_SHIFT_ANA1; + filterHandlerAna2.filterShift = ANA_LP_FILTER_SHIFT_ANA2; + filterHandlerAna3.filterShift = ANA_LP_FILTER_SHIFT_ANA3; + + FLT_LP_1Order_16Bit_Init( &filterHandlerAna1 ); + FLT_LP_1Order_16Bit_Init( &filterHandlerAna2 ); + FLT_LP_1Order_16Bit_Init( &filterHandlerAna3 ); + + ana_sw_timer.attach_us(&ANA_periodical, ANA_SW_TIMER_PERIOD ); +} + + +unsigned short ANA_getRaw(int channel) +{ + + if( (channel < 0) || (channel >= CNT_eANA_Channels) ) + { + return 0; + } + + return ANA_rawSamples[channel]; +} + + +unsigned short ANA_getFiltered( int channel ) +{ + if( (channel < 0) || (channel >= CNT_eANA_Channels) ) + { + return 0; + } + + return ANA_filteredSamples[channel]; +} + + +float ANA_scale(float offset, float gain, unsigned short input) +{ + return (float)input * gain - offset; +} + + + +//--------------------------------------- +// Internal Functions +//--------------------------------------- + +static void ANA_periodical( void ) +{ + ANA_rawSamples[ANA_CH1] = ana1.read_u16(); + ANA_rawSamples[ANA_CH2] = ana2.read_u16(); + ANA_rawSamples[ANA_CH3] = ana3.read_u16(); + + ANA_filteredSamples[ANA_CH1] = FLT_LP_1Order_16Bit( &filterHandlerAna1, ANA_rawSamples[ANA_CH1] ); + ANA_filteredSamples[ANA_CH2] = FLT_LP_1Order_16Bit( &filterHandlerAna2, ANA_rawSamples[ANA_CH2] ); + ANA_filteredSamples[ANA_CH3] = FLT_LP_1Order_16Bit( &filterHandlerAna3, ANA_rawSamples[ANA_CH3] ); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/ana.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,46 @@ +#include "mbed.h" + + + +//--------------------------------------- +// Definitions +//--------------------------------------- +#define ANA_SW_TIMER_PERIOD 10000 // [us] Period time of call +#define ANA_LP_FILTER_SHIFT_ANA1 3 +#define ANA_LP_FILTER_SHIFT_ANA2 3 +#define ANA_LP_FILTER_SHIFT_ANA3 3 + + +//--------------------------------------- +// Enums +//--------------------------------------- + +typedef enum +{ + ANA_CH1, + ANA_CH2, + ANA_CH3, + //----------------- + CNT_eANA_Channels +}eANA_Channels; + + +//--------------------------------------- +// Structures +//--------------------------------------- + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void ANA_init( void ); +unsigned short ANA_getRaw(int channel); +unsigned short ANA_getFiltered( int channel ); +float ANA_scale(float offset, float gain, unsigned short input); \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/can.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,113 @@ +#include "mbed.h" +#include "can.h" + + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- +CAN can1(p30, p29); + + + +//--------------------------------------- +// Prototypes +//--------------------------------------- +static void CAN_rxCallback( void ); + + + +//--------------------------------------- +// Internal variables +//--------------------------------------- +static CANMessage CAN_rxMessage; +static bool CAN_newFrame = false; + + + +//--------------------------------------- +// External variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void CAN_init( void ) +{ + CAN_newFrame = false; + can1.reset(); + can1.frequency( CAN_FREQUENCY ); + can1.attach( &CAN_rxCallback ); +} + + +// Return: +// 0 --> no new frame +// 1 --> new frame was received +int CAN_get( int *ptrID, char *ptrLen, char *ptrData ) +{ + int i; + + // No new frame + if( CAN_newFrame == false ) + { + *ptrID = 0; + *ptrLen = 0; + return 0; + } + + // New frame was received + *ptrID = CAN_rxMessage.id; + *ptrLen = CAN_rxMessage.len; + for( i = 0; i < CAN_rxMessage.len; i++) + { + *ptrData = CAN_rxMessage.data[i]; + ptrData++; + } + + CAN_newFrame = false; + + return 1; +} + + +// Return: +// 0: ID, Len outside limit or CAN hardware problems +// 1: Data have been send +int CAN_send( int ID, char Len, char *ptrData ) +{ + // Check limits + if( Len > 8 ) + { + return 0; + } + + if( (ID < 0) || (ID > 0x7FF) ) + { + return 0; + } + + + if( can1.write( CANMessage(ID, ptrData, Len) ) ) + { + return 1; + } + + // Error sending data --> reset init CAN + CAN_init(); + return 0; +} + + +//--------------------------------------- +// Internal Functions +//--------------------------------------- + +static void CAN_rxCallback( void ) +{ + can1.read(CAN_rxMessage); + CAN_newFrame = true; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/can.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,36 @@ +#include "mbed.h" + + + +//--------------------------------------- +// Definitions +//--------------------------------------- +#define CAN_FREQUENCY ( 500000 ) + + + +//--------------------------------------- +// Enums +//--------------------------------------- + + + + +//--------------------------------------- +// Structures +//--------------------------------------- + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void CAN_init( void ); // Init hardware and internal buffer +int CAN_get( int *ptrID, char *ptrLen, char *ptrData ); +int CAN_send( int ID, char Len, char *ptrData );
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/crc16.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,67 @@ + + +//---------------------------------------- +// Includes +//---------------------------------------- +#include "mbed.h" +#include "crc16.h" + + +//---------------------------------------- +// Prototype declaration +//---------------------------------------- + + +//---------------------------------------- +// global Variables +//---------------------------------------- + + +//---------------------------------------- +// Function +//---------------------------------------- + +//--------------------------------------------------------------------- +// Function Name: calcCRC16 +// Description: calculate CRC16 from preview CRC16 value and new data byte +// +// Inputs: - uint8 *ptrData: first data byte +// - uint16 numbBytes: Number of data bytes to process +// - uint16 *ptrCRC16: return value +// +// Returns: None +// +// Last Change: 05.04.2013 +//----------------------------------------------------------------------- +void calcCRC16(char *ptrData, int numbBytes, unsigned short *ptrCRC16) +{ + unsigned short i; + unsigned short i_bytes; + unsigned short CRC16; + char data; + + CRC16 = CRC16_STARTVALUE; + + for(i_bytes=0; i_bytes < numbBytes; i_bytes++) + { + data = *ptrData++; // get new data, inc pointer + + for(i=0;i<8;i++) + { + if( ( (CRC16 ^ data) & 0x01) == 0x01 ) + { + CRC16 >>= 1; + CRC16 ^= CRC16_POLYNOME; + }//if + else + { + CRC16 >>= 1; + }//else + + data >>= 1; // shift data 1 bit right, dividing it by 2 + }//for + + }//for i_bytes + + *ptrCRC16 = CRC16; //return CRC16 +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/crc16.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,30 @@ +#include "mbed.h" + + +//---------------------------------------- +// Structures +//---------------------------------------- + + +//---------------------------------------- +// Definitions +//---------------------------------------- +#define CRC16_POLYNOME 0xA001 +#define CRC16_STARTVALUE 0xFFFF + + +//---------------------------------------- +// global Variables +//---------------------------------------- + + + +//---------------------------------------- +// global Functions -> Prototype declaration +//---------------------------------------- + +// Inputs: - uint16 *ptrCRC16: must be initialized with 0xFFFF +// - uint8 *ptrData: one byte data + +extern void calcCRC16(char *ptrData, int numbBytes, unsigned short *ptrCRC16); +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/di.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,357 @@ +#include "mbed.h" +#include "uart.h" +#include "interpret.h" +#include "di.h" +#include "MCP23017.h" + + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- +MCP23017 *DI_PIN1_8_I2C; // Pin: 1..8 and Low, High level reference +MCP23017 *DI_PIN9_24_I2C; // Pin: 9..24 + + +//--------------------------------------- +// Prototypes +//--------------------------------------- + + + + +//--------------------------------------- +// Internal variables +//--------------------------------------- +static int DI_PinMapping[ DI_NUMBER_PINS ]; // array, keeps MCP23017 pin number for easy access + + + +//--------------------------------------- +// External variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void DI_init( void ) +{ + DI_PinMapping[0] = DI_PIN1; + DI_PinMapping[1] = DI_PIN2; + DI_PinMapping[2] = DI_PIN3; + DI_PinMapping[3] = DI_PIN4; + DI_PinMapping[4] = DI_PIN5; + DI_PinMapping[5] = DI_PIN6; + DI_PinMapping[6] = DI_PIN7; + DI_PinMapping[7] = DI_PIN8; + DI_PinMapping[8] = DI_PIN9; + DI_PinMapping[9] = DI_PIN10; + DI_PinMapping[10] = DI_PIN11; + DI_PinMapping[11] = DI_PIN12; + DI_PinMapping[12] = DI_PIN13; + DI_PinMapping[13] = DI_PIN14; + DI_PinMapping[14] = DI_PIN15; + DI_PinMapping[15] = DI_PIN16; + DI_PinMapping[16] = DI_PIN17; + DI_PinMapping[17] = DI_PIN18; + DI_PinMapping[18] = DI_PIN19; + DI_PinMapping[19] = DI_PIN20; + DI_PinMapping[20] = DI_PIN21; + DI_PinMapping[21] = DI_PIN22; + DI_PinMapping[22] = DI_PIN23; + DI_PinMapping[23] = DI_PIN24; + + DI_PIN1_8_I2C = new MCP23017( p9, p10, DI_PIN1_8_I2C_ADD); + DI_PIN1_8_I2C->config(0, 0, 0x00FF); // set to outputs, no pull-ups, not-inverted + + + DI_PIN9_24_I2C = new MCP23017( p9, p10, DI_PIN9_24_I2C_ADD); + DI_PIN9_24_I2C->config(0, 0, 0x00FF); // set to outputs, no pull-ups, not-inverted + + + DI_PIN1_8_I2C->write_bit(0, DI_LEVEL_LOW); + DI_PIN1_8_I2C->write_bit(0, DI_LEVEL_HIGH); + DI_PIN1_8_I2C->write_mask(0x00FF, 0x00FF); // set outputs high + DI_PIN9_24_I2C->write_mask(0xFFFF, 0xFFFF); // set outputs high +} + + + +// process commands +void DI_deviceID_process( void ) +{ + unsigned int temp32U; + unsigned short portTemp = 0; + int i; + int j; + + // Get data before processing and save to temp32U; + // maximal 3 bytes + temp32U = 0; + for( i = 0; i < 3; i++ ) + { + temp32U <<= 8; + temp32U |= ( 0x000000FF & (unsigned int)(uartBuffer[INT_BUF_1DATA + i]) ); + } + + + switch( uartBuffer[ INT_BUF_COMMAND ] ) + { + //-------------------- + case DI_SET_LEVEL_LOW: + //-------------------- + { + // Write to DI HW + if( uartBuffer[INT_BUF_1DATA] ) + { + DI_PIN1_8_I2C->write_bit(1, DI_LEVEL_LOW); + } + else + { + DI_PIN1_8_I2C->write_bit(0, DI_LEVEL_LOW); + } + // Generate acknowledge + INT_generateACKFrame(INT_ID_DI, INT_COM_ACK); + + break; + } + + //-------------------- + case DI_GET_LEVEL_LOW: + //-------------------- + { + temp32U = DI_PIN1_8_I2C->read_bit(DI_LEVEL_LOW); + + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_DI; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = 1; + uartBuffer[INT_BUF_1DATA] = (char)(0x000000FF & temp32U); + UART_handler.bytesToWrite = 4; + + break; + } + + //-------------------- + case DI_SET_LEVEL_HIGH: + //-------------------- + { + // Write to DI HW + if( uartBuffer[INT_BUF_1DATA] ) + { + DI_PIN1_8_I2C->write_bit(1, DI_LEVEL_HIGH); + } + else + { + DI_PIN1_8_I2C->write_bit(0, DI_LEVEL_HIGH); + } + // Generate acknowledge + INT_generateACKFrame(INT_ID_DI, INT_COM_ACK); + + break; + } + + //-------------------- + case DI_GET_LEVEL_HIGH: + //-------------------- + { + temp32U = DI_PIN1_8_I2C->read_bit(DI_LEVEL_HIGH); + + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_DI; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = 1; + uartBuffer[INT_BUF_1DATA] = (char)(0x000000FF & temp32U); + UART_handler.bytesToWrite = 4; + + break; + } + + + //-------------------- + case DI_SET_SINGLE: + //-------------------- + { + // Limit check of pin number + if( (uartBuffer[INT_BUF_1DATA] == 0) || (uartBuffer[INT_BUF_1DATA] > DI_NUMBER_PINS) ) + { + INT_generateACKFrame(INT_ID_DI, INT_COM_VAL_NOTVALID); + break; + } + + if( uartBuffer[INT_BUF_1DATA] > 8 ) // --> write to: DI_PIN9_24_I2C + { + DI_PIN9_24_I2C->write_bit(0, DI_PinMapping[uartBuffer[INT_BUF_1DATA] - 1]); + // Generate acknowledge + INT_generateACKFrame(INT_ID_DI, INT_COM_ACK); + break; + } + + // otherwise:--> write to: DI_PIN1_8_I2C + DI_PIN1_8_I2C->write_bit(0, DI_PinMapping[uartBuffer[INT_BUF_1DATA] - 1]); + // Generate acknowledge + INT_generateACKFrame(INT_ID_DI, INT_COM_ACK); + break; + } + + + //-------------------- + case DI_CLEAR_SINGLE: + //-------------------- + { + // Limit check of pin number + if( (uartBuffer[INT_BUF_1DATA] == 0) || (uartBuffer[INT_BUF_1DATA] > DI_NUMBER_PINS) ) + { + INT_generateACKFrame(INT_ID_DI, INT_COM_VAL_NOTVALID); + break; + } + + if( uartBuffer[INT_BUF_1DATA] > 8 ) // --> write to: DI_PIN9_24_I2C + { + DI_PIN9_24_I2C->write_bit(1, DI_PinMapping[uartBuffer[INT_BUF_1DATA] - 1]); + // Generate acknowledge + INT_generateACKFrame(INT_ID_DI, INT_COM_ACK); + break; + } + + // otherwise:--> write to: DI_PIN1_8_I2C + DI_PIN1_8_I2C->write_bit(1, DI_PinMapping[uartBuffer[INT_BUF_1DATA] - 1]); + // Generate acknowledge + INT_generateACKFrame(INT_ID_DI, INT_COM_ACK); + break; + } + + + //-------------------- + case DI_GET_SINGLE: + //-------------------- + { + // Limit check of pin number + if( (uartBuffer[INT_BUF_1DATA] == 0) || (uartBuffer[INT_BUF_1DATA] > DI_NUMBER_PINS) ) + { + INT_generateACKFrame(INT_ID_DI, INT_COM_VAL_NOTVALID); + break; + } + + if( uartBuffer[INT_BUF_1DATA] > 8 ) // --> read from: DI_PIN9_24_I2C + { + temp32U = DI_PIN9_24_I2C->read_bit( DI_PinMapping[uartBuffer[INT_BUF_1DATA] - 1] ); + } + else // otherwise:--> read from: DI_PIN1_8_I2C + { + temp32U = DI_PIN1_8_I2C->read_bit( DI_PinMapping[uartBuffer[INT_BUF_1DATA] - 1] ); + } + + // Invert because DO function is inverted + temp32U = (~temp32U & 0x00000001); + + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_DI; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = 1; + uartBuffer[INT_BUF_1DATA] = (char)(0x000000FF & temp32U); + UART_handler.bytesToWrite = 4; + + break; + } + + + //-------------------- + case DI_SET_PORT: + //-------------------- + { + // process Pin: 1..8 --> write to: DI_PIN1_8_I2C + portTemp = 0; + for( i = 0; i < 8; i++ ) + { + if( temp32U & (1 << i) ) // Bit is set --> set coresponding pin + { + portTemp |= ( 1 << DI_PinMapping[i]); + } + } + DI_PIN1_8_I2C->write_mask((~portTemp & 0x00FF), 0x00FF); // set outputs + + + // process Pin: 9..24 --> write to: DI_PIN9_24_I2C + portTemp = 0; + for( i = 8; i < 24; i++ ) + { + if( temp32U & (1 << i) ) // Bit is set --> set coresponding pin + { + portTemp |= ( 1 << DI_PinMapping[i]); + } + } + DI_PIN9_24_I2C->write_mask(~portTemp, 0xFFFF); // set outputs + + // Generate acknowledge + INT_generateACKFrame(INT_ID_DI, INT_COM_ACK); + + break; + } + + + //-------------------- + case DI_GET_PORT: + //-------------------- + { + temp32U = 0; + + // process Pin: 1..8 --> read to: DI_PIN1_8_I2C + portTemp = ~( DI_PIN1_8_I2C->read_mask(0x00FF) ); + + for( i = 0; i < 8; i++ ) { + if( portTemp & (1 << i) ) { + for( j = 0; j < 16; j++ ) { + if( DI_PinMapping[j] == i ) { + temp32U |= (1 << j); + } + }//for j + + }// if + }// for i + + + // process Pin: 9..24 --> read to: DI_PIN9_24_I2C + portTemp = ~( DI_PIN9_24_I2C->read_mask(0xFFFF) ); + + for( i = 0; i < 16; i++ ) { + if( portTemp & (1 << i) ) { + for( j = 8; j < 24; j++ ) { + if( DI_PinMapping[j] == i ) { + temp32U |= (1 << j); + } + }//for j + + }// if + }// for i + + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_DI; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = 3; + uartBuffer[INT_BUF_1DATA] = (char)(0x000000FF & (temp32U >> 16)); + uartBuffer[INT_BUF_1DATA + 1] = (char)(0x000000FF & (temp32U >> 8) ); + uartBuffer[INT_BUF_1DATA + 2] = (char)(0x000000FF & (temp32U) ); + UART_handler.bytesToWrite = 6; + break; + + } + + + //----------------- + default: + //----------------- + { + // Command not supported + INT_generateACKFrame(INT_ID_DI, INT_COM_COM_NOTSUPP); + } + }//switch + +} + +//--------------------------------------- +// Internal Functions +//--------------------------------------- +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/di.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,87 @@ +#include "mbed.h" + + + +//--------------------------------------- +// Definitions +//--------------------------------------- +#define DI_PIN9_24_I2C_ADD ( I2C_BASE_ADDRESS + (6 << 1) ) // I2C_BASE_ADDRESS is defined in MCP23017 +#define DI_PIN1_8_I2C_ADD ( I2C_BASE_ADDRESS + (4 << 1) ) // I2C_BASE_ADDRESS is defined in MCP23017 + +// define pin number (0..15) of MCP23017 +typedef enum +{ + DI_PIN1 = 7, + DI_PIN2 = 6, + DI_PIN3 = 5, + DI_PIN4 = 4, + DI_PIN5 = 3, + DI_PIN6 = 2, + DI_PIN7 = 1, + DI_PIN8 = 0, + DI_PIN9 = 15, + DI_PIN10 = 14, + DI_PIN11 = 13, + DI_PIN12 = 12, + DI_PIN13 = 11, + DI_PIN14 = 10, + DI_PIN15 = 9, + DI_PIN16 = 8, + DI_PIN17 = 7, + DI_PIN18 = 6, + DI_PIN19 = 5, + DI_PIN20 = 4, + DI_PIN21 = 3, + DI_PIN22 = 2, + DI_PIN23 = 1, + DI_PIN24 = 0, + //--------------- + CNT_DI_ePinTable +}DI_ePinTable; + +#define DI_NUMBER_PINS ( 24 ) + +#define DI_LEVEL_LOW ( 8 ) +#define DI_LEVEL_HIGH ( 9 ) + + +//--------------------------------------- +// Enums +//--------------------------------------- + +// Global commands 0 - 20 are defined in interpret.h +// Commands 21 - 127 are HW-unit commands +typedef enum +{ + DI_SET_LEVEL_LOW = 21, // 21: setLevelLow + DI_GET_LEVEL_LOW, // 22: getLevelLow + DI_SET_LEVEL_HIGH, // 23: setLevelHigh + DI_GET_LEVEL_HIGH, // 24: getLevelHigh + DI_SET_SINGLE, // 25: setSingle + DI_CLEAR_SINGLE, // 26: clearSingle + DI_GET_SINGLE, // 27: getSingle + DI_SET_PORT, // 28: setPort + DI_GET_PORT, // 29: getPort + //---------------- + CNT_eDI_command +}eDI_command; + + + +//--------------------------------------- +// Structures +//--------------------------------------- + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void DI_init( void ); +void DI_deviceID_process( void );
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/dido.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,141 @@ +#include "mbed.h" +#include "dido.h" +#include "MCP23008.h" + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- +MCP23008 *DIDO_I2C; // + + +//--------------------------------------- +// Prototypes +//--------------------------------------- + + + +//--------------------------------------- +// Internal variables +//--------------------------------------- +static unsigned char DIDO_doState; // keeps last written DO state + + +//--------------------------------------- +// External variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- + +void DIDO_init( void ) +{ + unsigned char direction; + + direction = 0x00; // Set all to outputs + direction = ( (1<<DIDO_DI1) | (1<<DIDO_DI2) | (1<<DIDO_DI3) | (1<<DIDO_DI4) | (1<<DIDO_DI5) ); // set some to inputs + + DIDO_I2C = new MCP23008( p9, p10, DIDO_I2C_ADD); + DIDO_I2C->config(direction, 0 , 0); // set direction, no pull-ups, 0 + + DIDO_setDO1OFF(); + DIDO_setDO2OFF(); + DIDO_setRelayOFF(); + +} + + +void DIDO_setDO1ON( void ) +{ + DIDO_doState |= (1 << DIDO_DO1); + DIDO_I2C->write_bit(0, DIDO_DO1); +} + +void DIDO_setDO1OFF( void ) +{ + DIDO_doState &= ~(1 << DIDO_DO1); + DIDO_I2C->write_bit(1, DIDO_DO1); +} + + +void DIDO_setDO2ON( void ) +{ + DIDO_doState |= (1 << DIDO_DO2); + DIDO_I2C->write_bit(0, DIDO_DO2); +} + +void DIDO_setDO2OFF( void ) +{ + DIDO_doState &= ~(1 << DIDO_DO2); + DIDO_I2C->write_bit(1, DIDO_DO2); +} + + +void DIDO_setRelayON( void ) +{ + DIDO_doState |= (1 << DIDO_RELAY); + DIDO_I2C->write_bit(1, DIDO_RELAY); +} + +void DIDO_setRelayOFF( void ) +{ + DIDO_doState &= ~(1 << DIDO_RELAY); + DIDO_I2C->write_bit(0, DIDO_RELAY); +} + + +int DIDO_getDO1( void ) +{ + return ( DIDO_doState & (1 << DIDO_DO1) ); +} + + +int DIDO_getDO2( void ) +{ + return ( DIDO_doState & (1 << DIDO_DO2) ); +} + + +int DIDO_getRelay( void ) +{ + return ( DIDO_doState & (1 << DIDO_RELAY) ); +} + + +int DIDO_getDI1( void ) +{ + return DIDO_I2C->read_bit(DIDO_DI1); +} + + +int DIDO_getDI2( void ) +{ + return DIDO_I2C->read_bit(DIDO_DI2); +} + + +int DIDO_getDI3( void ) +{ + return DIDO_I2C->read_bit(DIDO_DI3); +} + + +int DIDO_getDI4( void ) +{ + return DIDO_I2C->read_bit(DIDO_DI4); +} + + +int DIDO_getDI5( void ) +{ + return DIDO_I2C->read_bit(DIDO_DI5); +} + + +//--------------------------------------- +// Internal Functions +//--------------------------------------- +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/dido.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,63 @@ +#include "mbed.h" + + +//--------------------------------------- +// Definitions +//--------------------------------------- +#define DIDO_I2C_ADD ( I2C_BASE_ADDRESS + (5 << 1) ) // + + +#define DIDO_DO1 ( 2 ) // MCP23008 GPIO: 2 +#define DIDO_DO2 ( 1 ) // MCP23008 GPIO: 1 +#define DIDO_RELAY ( 0 ) // MCP23008 GPIO: 0 +#define DIDO_DI1 ( 7 ) // MCP23008 GPIO: 7 +#define DIDO_DI2 ( 6 ) // MCP23008 GPIO: 6 +#define DIDO_DI3 ( 5 ) // MCP23008 GPIO: 5 +#define DIDO_DI4 ( 4 ) // MCP23008 GPIO: 4 +#define DIDO_DI5 ( 3 ) // MCP23008 GPIO: 3 + + +//--------------------------------------- +// Enums +//--------------------------------------- + + + + + +//--------------------------------------- +// Structures +//--------------------------------------- + + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void DIDO_init( void ); + +void DIDO_setDO1ON( void ); +void DIDO_setDO1OFF( void ); +void DIDO_setDO2ON( void ); +void DIDO_setDO2OFF( void ); +void DIDO_setRelayON( void ); +void DIDO_setRelayOFF( void ); + +// Return: +// 0 = off, or low input level +// >= 1: On, or high input level +int DIDO_getDO1( void ); +int DIDO_getDO2( void ); +int DIDO_getRelay( void ); +int DIDO_getDI1( void ); +int DIDO_getDI2( void ); +int DIDO_getDI3( void ); +int DIDO_getDI4( void ); +int DIDO_getDI5( void );
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/do.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,159 @@ +#include "mbed.h" +#include "uart.h" +#include "interpret.h" +#include "do.h" +#include "MCP23017.h" + + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- +MCP23017 *DO_I2C; // + + +//--------------------------------------- +// Prototypes +//--------------------------------------- + + + + +//--------------------------------------- +// Internal variables +//--------------------------------------- +static int DO_PinMapping[ DO_NUMBER_PINS ]; // array, keeps MCP23017 pin number for easy access + + +//--------------------------------------- +// External variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void DO_init( void ) +{ + DO_PinMapping[0] = DO_PIN1; + DO_PinMapping[1] = DO_PIN2; + DO_PinMapping[2] = DO_PIN3; + DO_PinMapping[3] = DO_PIN4; + DO_PinMapping[4] = DO_PIN5; + DO_PinMapping[5] = DO_PIN6; + DO_PinMapping[6] = DO_PIN7; + DO_PinMapping[7] = DO_PIN8; + DO_PinMapping[8] = DO_PIN9; + DO_PinMapping[9] = DO_PIN10; + DO_PinMapping[10] = DO_PIN11; + DO_PinMapping[11] = DO_PIN12; + DO_PinMapping[12] = DO_PIN13; + DO_PinMapping[13] = DO_PIN14; + DO_PinMapping[14] = DO_PIN15; + DO_PinMapping[15] = DO_PIN16; + + DO_I2C = new MCP23017( p9, p10, DO_I2C_ADD); + DO_I2C->config(0xFFFF,0,0); // set to inputs, no pull-ups, not-inverted +} + + + +// process commands +void DO_deviceID_process( void ) +{ + unsigned int temp32U = 0; + unsigned short portTemp = 0; + int i; + int j; + + temp32U = 0x000000FF & ( (unsigned int)uartBuffer[INT_BUF_1DATA] ); + + switch( uartBuffer[ INT_BUF_COMMAND ] ) { + //-------------------- + case DO_GET_SINGLE: + //-------------------- + { + if( (temp32U == 0) || (temp32U > DO_NUMBER_PINS) ) { + // Generate acknowledge + INT_generateACKFrame(INT_ID_DO, INT_COM_VAL_NOTVALID); + + break; + } + + temp32U = DO_I2C->read_bit( DO_PinMapping[temp32U - 1] ); + + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_DO; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = 1; + uartBuffer[INT_BUF_1DATA] = (char)(0x000000FF & temp32U); + UART_handler.bytesToWrite = 4; + break; + + } + + + //-------------------- + case DO_GET_PORT: + //-------------------- + { + temp32U = 0; + + portTemp = DO_I2C->read_mask(0xFFFF); + + for( i = 0; i < 16; i++ ) { + if( portTemp & (1 << i) ) { + for( j = 0; j < 16; j++ ) { + if( DO_PinMapping[j] == i ) { + temp32U |= (1 << j); + } + }//for j + + }// if + }// for i + + /* + for( i = 0; i < 16; i++ ) + { + if( portTemp & (1 << i) ) + { + for( j = 1; j < 17; j++ ) + { + if( DO_PinMapping[j] == i ) + { + temp32U |= (1 << (j-1)); + } + }//for j + + }// if + }// for i + */ + + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_DO; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = 2; + uartBuffer[INT_BUF_1DATA] = (char)(0x000000FF & (temp32U >> 8) ); + uartBuffer[INT_BUF_1DATA + 1] = (char)(0x000000FF & (temp32U) ); + UART_handler.bytesToWrite = 5; + break; + + } + + + //----------------- + default: + //----------------- + { + // Command not supported + INT_generateACKFrame(INT_ID_DO, INT_COM_COM_NOTSUPP); + } + }//switch + +} + +//--------------------------------------- +// Internal Functions +//--------------------------------------- +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/do.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,66 @@ +#include "mbed.h" + + + +//--------------------------------------- +// Definitions +//--------------------------------------- +#define DO_I2C_ADD ( I2C_BASE_ADDRESS | (7 << 1) ) // I2C_BASE_ADDRESS is defined in MCP23017 +#define DO_NUMBER_PINS ( 16 ) + + +//--------------------------------------- +// Enums +//--------------------------------------- + +// define pin number (0..15) of MCP23017 +typedef enum +{ + DO_PIN1 = 15, + DO_PIN2 = 14, + DO_PIN3 = 13, + DO_PIN4 = 12, + DO_PIN5 = 11, + DO_PIN6 = 10, + DO_PIN7 = 9, + DO_PIN8 = 8, + DO_PIN9 = 7, + DO_PIN10 = 6, + DO_PIN11 = 5, + DO_PIN12 = 4, + DO_PIN13 = 3, + DO_PIN14 = 2, + DO_PIN15 = 1, + DO_PIN16 = 0 +}DO_ePinTable; + + +// Global commands 0 - 20 are defined in interpret.h +// Commands 21 - 127 are HW-unit commands +typedef enum +{ + DO_GET_SINGLE = 21, // 21: getSingle + DO_GET_PORT, // 22: getPort + //---------------- + CNT_eDO_command +}eDO_command; + + + +//--------------------------------------- +// Structures +//--------------------------------------- + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void DO_init( void ); +void DO_deviceID_process( void );
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/filter.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,58 @@ +#include "mbed.h" +#include "filter.h" + + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- + + +//--------------------------------------- +// Prototypes +//--------------------------------------- + + + + +//--------------------------------------- +// Internal variables +//--------------------------------------- + + + +//--------------------------------------- +// External variables +//--------------------------------------- + + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- + +void FLT_LP_1Order_16Bit_Init( sFLT_LP1Order16BitHandler *filterHandler ) +{ + filterHandler->filterReg = 0; +} + + +unsigned short FLT_LP_1Order_16Bit(sFLT_LP1Order16BitHandler *filterHandler, unsigned short input) +{ + unsigned int temp32U; + + temp32U = filterHandler->filterReg; + temp32U = temp32U - (temp32U >> filterHandler->filterShift) + (unsigned int)input; + filterHandler->filterReg = temp32U; + + return (unsigned short)( temp32U >> filterHandler->filterShift); +} + + + + +//--------------------------------------- +// Internal Functions +//--------------------------------------- +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/filter.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,41 @@ +#include "mbed.h" + + + +//--------------------------------------- +// Definitions +//--------------------------------------- + + + + +//--------------------------------------- +// Enums +//--------------------------------------- + + + + + +//--------------------------------------- +// Structures +//--------------------------------------- +typedef struct +{ + unsigned int filterReg; + unsigned int filterShift; +}sFLT_LP1Order16BitHandler; + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void FLT_LP_1Order_16Bit_Init( sFLT_LP1Order16BitHandler *filterHandler ); +unsigned short FLT_LP_1Order_16Bit(sFLT_LP1Order16BitHandler *filterHandler, unsigned short input); \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/interpret.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,226 @@ +#include "interpret.h" +#include "uart.h" +#include "mbed_unit.h" +#include "pt100.h" +#include "di.h" +#include "do.h" +#include "RS485.h" + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- +DigitalOut led_system_ready(LED1); + + +//--------------------------------------- +// Prototypes +//--------------------------------------- + +void INT_selectDeviceID( void ); +void INT_deviceID_process_Broadcast( void ); + + +//--------------------------------------- +// Internal variables +//--------------------------------------- +static sINT_handler INT_handler; + +//--------------------------------------- +// External variables +//--------------------------------------- + + +//--------------------------------------- +// Global Functions +//--------------------------------------- + +void INT_init( void ) +{ + INT_handler.state = INT_STATE_INIT; + INT_handler.receivedDeviceID = 0; + INT_handler.receivedCommand = 0; + led_system_ready = 0; +} + + + +void INT_poll( void ) +{ + switch( INT_handler.state ) + { + //--------------------------- + case INT_STATE_INIT: + //--------------------------- + { + led_system_ready = 0; + DI_init(); + MBED_init(); // PWM, CAN, RS232,... + PT100_init(); + DO_init(); + RS485_init(); + led_system_ready = 1; + INT_handler.state = INT_STATE_IDLE; + + break; + } + + + //--------------------------- + case INT_STATE_IDLE: + //--------------------------- + { + if( UART_newFrame() ) + { + INT_handler.state = INT_STATE_CHECK; + } + + break; + } + + + //--------------------------- + case INT_STATE_CHECK: + //--------------------------- + { + //--------------------------------- + if( UART_checkReceivedCRC() ) + //--------------------------------- + { + // backup device ID and command for acknowledge + INT_handler.receivedDeviceID = uartBuffer[INT_BUF_DEVICEID]; + INT_handler.receivedCommand = uartBuffer[INT_BUF_COMMAND]; + INT_handler.state = INT_STATE_INTERPRET; + } + //------------------------ + else // CRC check failed --> ignore frame + //------------------------ + { + UART_reset(); + INT_handler.state = INT_STATE_IDLE; + } + + break; + } + + + //--------------------------- + case INT_STATE_INTERPRET: + //--------------------------- + { + INT_selectDeviceID(); + INT_handler.state = INT_STATE_ACK; + break; + } + + + //--------------------------- + case INT_STATE_ACK: + //--------------------------- + { + // INT_selectDeviceID() has already updated UART_buffer[] --> just send + UART_sendData(); + INT_handler.state = INT_STATE_IDLE; + break; + } + + + //--------------------------- + default: + //--------------------------- + { + INT_handler.state = INT_STATE_INIT; + } + + }//switch + +} + + +void INT_generateACKFrame(char deviceID, char command) +{ + uartBuffer[INT_BUF_DEVICEID] = deviceID; + uartBuffer[INT_BUF_COMMAND] = command; + uartBuffer[INT_BUF_NUM] = 0; + UART_handler.bytesToWrite = 3; +} + + +//--------------------------------------- +// Local Functions +//--------------------------------------- + + +// Here, the DeviceID gets used to communicate with the requested HW-unit +// Modify this function, if new HW-units are integreted. +// The XXX_deviceID_process() function also generates the acknowledge frame and +// place it in the uartBuffer[]. +// The next state, INT_STATE_ACK, of the Interprete FSM is than sending these data +void INT_selectDeviceID( void ) +{ + switch( INT_handler.receivedDeviceID ) + { + //---------------------- + case INT_ID_BROADCAST: + //---------------------- + { + INT_deviceID_process_Broadcast(); + break; + } + + + //---------------------- + case INT_ID_MBED: + //---------------------- + { + MBED_deviceID_process(); + break; + } + + + //---------------------- + case INT_ID_PT100: + //---------------------- + { + PT100_deviceID_process(); + break; + } + + + //---------------------- + case INT_ID_DI: + //---------------------- + { + DI_deviceID_process(); + break; + } + + + //---------------------- + case INT_ID_DO: + //---------------------- + { + DO_deviceID_process(); + break; + } + + + //---------------------- + default: + //---------------------- + { + // Device ID is not supported, send ack. + INT_generateACKFrame(INT_handler.receivedDeviceID, INT_COM_DEV_NOTSUPP); + } + }//switch +} + + + + +// Interprete Command for broadcast +void INT_deviceID_process_Broadcast( void ) +{ + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/interpret.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,96 @@ +#include "mbed.h" + + +//--------------------------------------- +// Definitions +//--------------------------------------- +#define INT_BUF_DEVICEID 0 // UART buffer index of DeviceID +#define INT_BUF_COMMAND 1 // UART buffer index of Command +#define INT_BUF_NUM 2 // UART buffer index of number of Bytes +#define INT_BUF_1DATA 3 // UART buffer index of first data byte if #Bytes > 0 + + + +//--------------------------------------- +// Enums +//--------------------------------------- +typedef enum +{ + INT_STATE_INIT, + INT_STATE_IDLE, + INT_STATE_CHECK, + INT_STATE_INTERPRET, + INT_STATE_ACK, + //--------------- + CNT_eINT_state +}eINT_state; + + + +typedef enum +{ + INT_ID_BROADCAST = 0, + INT_ID_MBED, + INT_ID_PT100, + INT_ID_DI, + INT_ID_DO, + //---------------------- + CNT_eINT_deviceID + +}eINT_deviceID; + + +// Global commands 0 - 20, +// Commands 21 - 127 are devined inside the individual HW-units +typedef enum +{ + INT_COM_NOP = 0, // 0: NOP + INT_COM_RESET, // 1: Reset + INT_COM_ACK, // 2: ACK + INT_COM_DEV_NOTSUPP, // 3: DeviceID not supported + INT_COM_COM_NOTSUPP, // 4: Command not supported + INT_COM_VAL_NOTVALID, // 5: Value is not valid + INT_COM_6, + INT_COM_7, + INT_COM_8, + INT_COM_9, + INT_COM_10, + INT_COM_11, + INT_COM_12, + INT_COM_13, + INT_COM_14, + INT_COM_15, + INT_COM_16, + INT_COM_17, + INT_COM_18, + INT_COM_19, + INT_COM_20, + //---------------- + CNT_eINT_command +}eINT_command; + + +//--------------------------------------- +// Structures +//--------------------------------------- +typedef struct +{ + eINT_state state; + char receivedDeviceID; + char receivedCommand; + +}sINT_handler; + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void INT_init( void ); +void INT_poll( void ); +void INT_generateACKFrame(char deviceID, char command);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/main.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,54 @@ +#include "mbed.h" +#include "uart.h" +#include "interpret.h" + + +//---------------------------------------------- +// Info: +// MCP23017 class is used from: Gobert Pierre, 13.04.2012 +// +//---------------------------------------------- + +//---------------------------------------------- +// STATUS: +// 2013-01-25: +// - I2C communication with MCP23017 is running +// - PT100, command 21 and 22 is working + +//--------------------------------------- +// Hardware recources +//--------------------------------------- +DigitalOut myled(LED1); + + +//--------------------------------------- +// Internal variables +//--------------------------------------- + + +//--------------------------------------- +// External variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +int main() { + + UART_init(); + INT_init(); + + while(1) + { + INT_poll(); + + }//while +} + + + +//--------------------------------------- +// Internal Functions +//---------------------------------------
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/mbed.bld Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/824293ae5e43 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/mbed_unit.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,426 @@ +#include "mbed.h" +#include "uart.h" +#include "interpret.h" +#include "mbed_unit.h" +#include "rs232.h" +#include "can.h" +#include "pwm.h" +#include "ana.h" +#include "dido.h" +#include "RS485.h" + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- + + +DigitalOut K1_Relay(P0_29); +DigitalOut K2_Relay(P0_30); + + +//--------------------------------------- +// Prototypes +//--------------------------------------- + + + +//--------------------------------------- +// Internal variables +//--------------------------------------- + + +//--------------------------------------- +// External variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void MBED_init( void ) +{ + PWM_init(); + RS232_init(); + CAN_init(); + ANA_init(); + DIDO_init(); + K1_Relay=0; + K2_Relay=0; +} + + + +// process commands +void MBED_deviceID_process( void ) +{ + unsigned int temp32U; + int temp32; + char *ptrTempChar; + int i; + + switch( uartBuffer[ INT_BUF_COMMAND ] ) { + //--------------------------- + case MBED_SET_RPM_LEVEL_LOW: + //--------------------------- + { + if( uartBuffer[INT_BUF_1DATA] ) + K1_Relay=1; + else + K1_Relay=0; + + // Generate acknowledge + INT_generateACKFrame(INT_ID_MBED, INT_COM_ACK); + break; + } + + + //--------------------------- + case MBED_SET_RPM_LEVEL_HIGH: + //--------------------------- + { + if( uartBuffer[INT_BUF_1DATA] ) + K2_Relay=1; + else + K2_Relay=0; + + // Generate acknowledge + INT_generateACKFrame(INT_ID_MBED, INT_COM_ACK); + break; + + } + + + //--------------------------- + case MBED_SET_PWM_PORT: + //--------------------------- + { + // Set output high or low + if( uartBuffer[INT_BUF_1DATA] & (1 << PWM_CH0) ) PWM_setDC(PWM_CH0, 100); + else PWM_setDC(PWM_CH0, 0); + if( uartBuffer[INT_BUF_1DATA] & (1 << PWM_CH1) ) PWM_setDC(PWM_CH1, 100); + else PWM_setDC(PWM_CH1, 0); + if( uartBuffer[INT_BUF_1DATA] & (1 << PWM_CH2) ) PWM_setDC(PWM_CH2, 100); + else PWM_setDC(PWM_CH2, 0); + if( uartBuffer[INT_BUF_1DATA] & (1 << PWM_CH3) ) PWM_setDC(PWM_CH3, 100); + else PWM_setDC(PWM_CH3, 0); + if( uartBuffer[INT_BUF_1DATA] & (1 << PWM_CH4) ) PWM_setDC(PWM_CH4, 100); + else PWM_setDC(PWM_CH4, 0); + if( uartBuffer[INT_BUF_1DATA] & (1 << PWM_CH5) ) PWM_setDC(PWM_CH5, 100); + else PWM_setDC(PWM_CH5, 0); + + // Generate acknowledge + INT_generateACKFrame(INT_ID_MBED, INT_COM_ACK); + + break; + } + + + //--------------------------- + case MBED_SET_PWM_FREQ: + //--------------------------- + { + // Get frequency data in [Hz] + temp32U = 0; + + temp32U = uartBuffer[INT_BUF_1DATA]; + temp32U <<= 8; + temp32U |= (unsigned int)uartBuffer[INT_BUF_1DATA + 1]; + + // check limit + if( PWM_updateFreq(temp32U) ) { + // Generate acknowledge + INT_generateACKFrame(INT_ID_MBED, INT_COM_ACK); + } else { // outside of supported limit + // Generate acknowledge + INT_generateACKFrame(INT_ID_MBED, INT_COM_VAL_NOTVALID); + } + + break; + } + + + //--------------------------- + case MBED_SET_PWM_DC: + //--------------------------- + { + if( PWM_setDC((int)uartBuffer[INT_BUF_1DATA], (int)uartBuffer[INT_BUF_1DATA + 1]) ) { + // Generate acknowledge + INT_generateACKFrame(INT_ID_MBED, INT_COM_ACK); + } else { // outside of supported limit + // Generate acknowledge + INT_generateACKFrame(INT_ID_MBED, INT_COM_VAL_NOTVALID); + } + break; + + } + + + //--------------------------- + case MBED_WRITE_RS232: + //--------------------------- + { + if( RS232_sendData( (char *)&uartBuffer[INT_BUF_1DATA], uartBuffer[INT_BUF_NUM] ) ) { + INT_generateACKFrame(INT_ID_MBED, INT_COM_ACK); + } else { + INT_generateACKFrame(INT_ID_MBED, INT_COM_VAL_NOTVALID); + } + + break; + } + + + //--------------------------- + case MBED_READ_RS232: + //--------------------------- + { + RS232_receiveData( ptrTempChar, &temp32 ); + + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_MBED; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = temp32; + // copy data over to uart buffer + for( i = 0; i < temp32; i++) { + uartBuffer[INT_BUF_1DATA + i] = *ptrTempChar; + ptrTempChar++; + } + UART_handler.bytesToWrite = 3 + temp32; + + break; + } + + + //--------------------------- + case MBED_SET_RTSRS232: + //--------------------------- + { + RS232_setRTS( (int)uartBuffer[INT_BUF_1DATA] ); + INT_generateACKFrame(INT_ID_MBED, INT_COM_ACK); + + break; + } + + + //--------------------------- + case MBED_GET_CTSRS232: + //--------------------------- + { + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_MBED; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = 1; + uartBuffer[INT_BUF_1DATA] = RS232_getCTS(); + UART_handler.bytesToWrite = 4; + + break; + } + + + //--------------------------- + case MBED_WRITE_CAN: + //--------------------------- + { + // Get CAN-ID + temp32 = 0; + + temp32 = uartBuffer[INT_BUF_1DATA]; + temp32 <<= 8; + temp32 |= (int)uartBuffer[INT_BUF_1DATA + 1]; + + if( CAN_send( temp32, uartBuffer[INT_BUF_1DATA + 2], &uartBuffer[INT_BUF_1DATA + 3] ) ) { + INT_generateACKFrame(INT_ID_MBED, INT_COM_ACK); + } else { + INT_generateACKFrame(INT_ID_MBED, INT_COM_VAL_NOTVALID); + } + + break; + } + + + //--------------------------- + case MBED_READ_CAN: + //--------------------------- + { + // Get CAN-ID + temp32 = 0; + + temp32 = uartBuffer[INT_BUF_1DATA]; + temp32 <<= 8; + temp32 |= (int)uartBuffer[INT_BUF_1DATA + 1]; + + if( CAN_get( &temp32, &uartBuffer[INT_BUF_1DATA + 2], &uartBuffer[INT_BUF_1DATA + 3] ) ) { + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_MBED; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = 3 + uartBuffer[INT_BUF_1DATA + 2]; // + uartBuffer[INT_BUF_1DATA] = (char)(0x000000FF & (temp32 >> 8) ); // CAN-ID high byte + uartBuffer[INT_BUF_1DATA + 1] = (char)(0x000000FF & temp32 ); // CAN-ID low byte + + UART_handler.bytesToWrite = 6 + uartBuffer[INT_BUF_1DATA + 2]; // + } else { + // Generate acknowledge, no new data frame + uartBuffer[INT_BUF_DEVICEID] = INT_ID_MBED; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = 3; // + uartBuffer[INT_BUF_1DATA] = 0; // CAN-ID high byte + uartBuffer[INT_BUF_1DATA + 1] = 0 ; // CAN-ID low byte + uartBuffer[INT_BUF_1DATA + 2] = 0 ; // 0 Data frames + UART_handler.bytesToWrite = 6; // + } + + break; + } + + + //--------------------------- + case MBED_READ_ADC: + //--------------------------- + { + // Get ADC value of channel indicated in uartBuffer[] + temp32U = (unsigned int)ANA_getFiltered( (int)uartBuffer[INT_BUF_1DATA] ); + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_MBED; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = 3; + // uartBuffer[INT_BUF_1DATA] = KEEP channel number + uartBuffer[INT_BUF_1DATA + 1] = (char)(0x000000FF & (temp32U >> 8) ); // high byte + uartBuffer[INT_BUF_1DATA + 2] = (char)(0x000000FF & temp32U ); // low byte + + UART_handler.bytesToWrite = 6 ; // + + break; + } + + + //--------------------------- + case MBED_SET_RELAY: + //--------------------------- + { + if( uartBuffer[INT_BUF_1DATA] ) { + DIDO_setRelayON(); + } else { + DIDO_setRelayOFF(); + } + + INT_generateACKFrame(INT_ID_MBED, INT_COM_ACK); + break; + } + + + //--------------------------- + case MBED_SET_DO1: + //--------------------------- + { + if( uartBuffer[INT_BUF_1DATA] ) { + DIDO_setDO1ON(); + } else { + DIDO_setDO1OFF(); + } + + INT_generateACKFrame(INT_ID_MBED, INT_COM_ACK); + break; + } + + + //--------------------------- + case MBED_SET_DO2: + //--------------------------- + { + if( uartBuffer[INT_BUF_1DATA] ) { + DIDO_setDO2ON(); + } else { + DIDO_setDO2OFF(); + } + + INT_generateACKFrame(INT_ID_MBED, INT_COM_ACK); + break; + } + + //--------------------------- + case MBED_GET_DI: + //--------------------------- + { + temp32U = 0; + + if( DIDO_getDI1() ) { + temp32U |= (1 << 0); + } + + if( DIDO_getDI2() ) { + temp32U |= (1 << 1); + } + + if( DIDO_getDI3() ) { + temp32U |= (1 << 2); + } + + if( DIDO_getDI4() ) { + temp32U |= (1 << 3); + } + + if( DIDO_getDI5() ) { + temp32U |= (1 << 4); + } + + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_MBED; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = 1; + uartBuffer[INT_BUF_1DATA] = (char)(0x000000FF & temp32U ); + UART_handler.bytesToWrite = 4 ; // + + break; + } + + + //--------------------------- + case MBED_WRITE_RS485: + //--------------------------- + { + if( RS485_sendData( (char *)&uartBuffer[INT_BUF_1DATA], (int)uartBuffer[INT_BUF_NUM] ) ) { + INT_generateACKFrame(INT_ID_MBED, INT_COM_ACK); + } else { + INT_generateACKFrame(INT_ID_MBED, INT_COM_VAL_NOTVALID); + } + + break; + } + + + //--------------------------- + case MBED_READ_RS485: + //--------------------------- + { + RS485_receiveData( ptrTempChar, (int *)&uartBuffer[INT_BUF_NUM] ); + + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_MBED; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + // copy data over to uart buffer + for( i = 0; i < uartBuffer[INT_BUF_NUM]; i++) { + uartBuffer[INT_BUF_1DATA + i] = *ptrTempChar; + ptrTempChar++; + } + UART_handler.bytesToWrite = 3 + uartBuffer[INT_BUF_NUM]; + + break; + } + + + //----------------- + default: + //----------------- + { + // Command not supported + INT_generateACKFrame(INT_ID_MBED, INT_COM_COM_NOTSUPP); + } + + }//switch + +} + +//--------------------------------------- +// Internal Functions +//---------------------------------------
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/mbed_unit.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,61 @@ +#include "mbed.h" + + + +//--------------------------------------- +// Definitions +//--------------------------------------- + + +//--------------------------------------- +// Enums +//--------------------------------------- + + + + +// Global commands 0 - 20 are defined in interpret.h +// Commands 21 - 127 are HW-unit commands +typedef enum +{ + MBED_SET_RPM_LEVEL_LOW = 21, // 21: setRPMLevelLow + MBED_SET_RPM_LEVEL_HIGH, // 22: setRPMLevelLow + MBED_SET_PWM_PORT, // 23: setPWMPort + MBED_SET_PWM_FREQ, // 24: setPWMFreq + MBED_SET_PWM_DC, // 25: setPWMDutyCycle + MBED_WRITE_RS232, // 26: writeRS232 + MBED_READ_RS232, // 27: readRS232 + MBED_SET_RTSRS232, // 28: setRTSRS232 + MBED_GET_CTSRS232, // 29: getCTSRS232 + MBED_WRITE_CAN, // 30: writeCAN + MBED_READ_CAN, // 31: readCAN + MBED_READ_ADC, // 32: readADC + MBED_SET_RELAY, // 33: setRelay + MBED_SET_DO1, // 34: setDO1 + MBED_SET_DO2, // 35: setDO2 + MBED_GET_DI, // 36: getDI + MBED_WRITE_RS485, // 37: writeRS485 + MBED_READ_RS485, // 38: readRS485 + //---------------- + CNT_eMBED_command +}eMBED_command; + + + +//--------------------------------------- +// Structures +//--------------------------------------- + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void MBED_init( void ); +void MBED_deviceID_process( void );
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/pt100.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,115 @@ +#include "mbed.h" +#include "uart.h" +#include "interpret.h" +#include "pt100.h" +#include "MCP23017.h" + + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- +MCP23017 *PT100_I2C; + + + +//--------------------------------------- +// Prototypes +//--------------------------------------- +unsigned char PT100_mirrowBits( unsigned char input); + + + +//--------------------------------------- +// Internal variables +//--------------------------------------- + + + + +//--------------------------------------- +// External variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void PT100_init( void ) +{ + PT100_I2C = new MCP23017( p9, p10, PT100_I2C_ADD); + PT100_I2C->config(0,0,0); // set to outputs, no pull-ups, not-inverted + PT100_I2C->write_mask(0, 0xFFFF); // set outputs low +} + + + +// process commands +void PT100_deviceID_process( void ) +{ + unsigned short temp16U; + + switch( uartBuffer[ INT_BUF_COMMAND ] ) + { + //----------------- + case PT100_COM_SET: + //----------------- + { + // Get byte + temp16U = (unsigned short)PT100_mirrowBits( uartBuffer[ INT_BUF_1DATA ]); + // Write to PT100 HW + PT100_I2C->write_mask(temp16U, 0x00FF); + // Generate acknowledge + INT_generateACKFrame(INT_ID_PT100, INT_COM_ACK); + + break; + } + + //----------------- + case PT100_COM_GET: + //----------------- + { + temp16U = (unsigned short)PT100_mirrowBits( (unsigned char)(PT100_I2C->read_mask(0x00FF))); + + // Generate acknowledge + uartBuffer[INT_BUF_DEVICEID] = INT_ID_PT100; + uartBuffer[INT_BUF_COMMAND] = INT_COM_ACK; + uartBuffer[INT_BUF_NUM] = 1; + uartBuffer[INT_BUF_1DATA] = (char)(0x00FF & temp16U); + UART_handler.bytesToWrite = 4; + + break; + } + + //----------------- + default: + //----------------- + { + // Command not supported + INT_generateACKFrame(INT_ID_PT100, INT_COM_COM_NOTSUPP); + } + }//switch + +} + +//--------------------------------------- +// Internal Functions +//--------------------------------------- + +unsigned char PT100_mirrowBits( unsigned char input) +{ + char i; + unsigned char tempChar = 0; + + for( i = 0; i < 8; i++) + { + if( input & (1 << i) ) + { + tempChar |= (1 << 7 - i); + } + + } + + return tempChar; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/pt100.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,43 @@ +#include "mbed.h" + + + +//--------------------------------------- +// Definitions +//--------------------------------------- +#define PT100_I2C_ADD ( I2C_BASE_ADDRESS + (0 << 1) ) // I2C_BASE_ADDRESS is defined in MCP23017 + + +//--------------------------------------- +// Enums +//--------------------------------------- + +// Global commands 0 - 20 are defined in interpret.h +// Commands 21 - 127 are HW-unit commands +typedef enum +{ + PT100_COM_SET = 21, // 21: set state + PT100_COM_GET, // 22: get state + //---------------- + CNT_ePT100_command +}ePT100_command; + + + +//--------------------------------------- +// Structures +//--------------------------------------- + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void PT100_init( void ); +void PT100_deviceID_process( void );
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/pwm.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,136 @@ +#include "mbed.h" +#include "pwm.h" + + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- +PwmOut PWM0(p26); +PwmOut PWM1(p25); +PwmOut PWM2(p24); +PwmOut PWM3(p23); +PwmOut PWM4(p22); +PwmOut PWM5(p21); + + + +//--------------------------------------- +// Prototypes +//--------------------------------------- +static int PWM_calcDutyCycle( unsigned char DutyCyclePerc ); +static void PWM_updateAllDC( void ); + + +//--------------------------------------- +// Internal variables +//--------------------------------------- +static int PWMPeriode_us = 0; // [us] actual periode time of pwm signal +static unsigned char PWMDutyCycle[CNT_ePWM_channel]; // [%] Scale: 1 DutyCycle ratio of each channel + + + +//--------------------------------------- +// External variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void PWM_init( void ) +{ + int i; + + for( i = 0; i < CNT_ePWM_channel; i++ ) + { + PWMDutyCycle[i] = 0; + } + + // Update Freq and duty cycle + PWM_updateFreq( PWM_INIT_FREQ ); +} + + +// Return: +// 0: freq_Hz is outside of limit +// 1: frequency was updated +int PWM_updateFreq( unsigned int freq_Hz ) +{ + float tempFloat; + + // check limit + if( (freq_Hz < PWM_FREQ_MIN) || (freq_Hz > PWM_FREQ_MAX) ) + { + return 0; + } + + // convert to period time in us + tempFloat = 1.0 / (float)(freq_Hz); + tempFloat *= 1000000.0; + + // Save period to global variable, update PWM and update duty cycle + PWMPeriode_us = (int)tempFloat; + PWM0.period_us(PWMPeriode_us); // periode effects all 6 channels + PWM_updateAllDC(); + + return 1; +} + + +// Return: +// 0: channel or dutyCycle is outside of limit +// 1: duty cycle was updated +int PWM_setDC( int channel, int dutyCycle ) +{ + // Check duty cycle and channel limits + if( channel >= CNT_ePWM_channel ) + { + return 0; + } + + if( dutyCycle < 0 || dutyCycle > PWM_DC_MAX ) + { + return 0; + } + + // update buffer array of channel and update duty cycle register of all channel + PWMDutyCycle[channel] = dutyCycle; + PWM_updateAllDC(); + + return 1; +} + + + +//--------------------------------------- +// Internal Functions +//--------------------------------------- +static void PWM_updateAllDC( void ) +{ + // update duty cycle of all channels + PWM0.pulsewidth_us( PWM_calcDutyCycle( PWMDutyCycle[PWM_CH0] ) ); + PWM1.pulsewidth_us( PWM_calcDutyCycle( PWMDutyCycle[PWM_CH1] ) ); + PWM2.pulsewidth_us( PWM_calcDutyCycle( PWMDutyCycle[PWM_CH2] ) ); + PWM3.pulsewidth_us( PWM_calcDutyCycle( PWMDutyCycle[PWM_CH3] ) ); + PWM4.pulsewidth_us( PWM_calcDutyCycle( PWMDutyCycle[PWM_CH4] ) ); + PWM5.pulsewidth_us( PWM_calcDutyCycle( PWMDutyCycle[PWM_CH5] ) ); +} + + +// Calculate duty cycle register value with respect to period time +static int PWM_calcDutyCycle( unsigned char DutyCyclePerc ) +{ + if( DutyCyclePerc == 0 ) + { + return 0; + } + + if( DutyCyclePerc >= 100 ) + { + return PWMPeriode_us + 1; + } + + return (int)( (float)PWMPeriode_us * ( (float)DutyCyclePerc / 100.0) ); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/pwm.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,51 @@ +#include "mbed.h" + + + +//--------------------------------------- +// Definitions +//--------------------------------------- +#define PWM_INIT_FREQ ( 1000 ) +#define PWM_FREQ_MIN ( 1 ) +#define PWM_FREQ_MAX ( 5000 ) +#define PWM_DC_MAX ( 100 ) + + + +//--------------------------------------- +// Enums +//--------------------------------------- + +typedef enum +{ + PWM_CH0, + PWM_CH1, + PWM_CH2, + PWM_CH3, + PWM_CH4, + PWM_CH5, + //------------------- + CNT_ePWM_channel +}ePWM_channel; + + + + +//--------------------------------------- +// Structures +//--------------------------------------- + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void PWM_init( void ); +int PWM_updateFreq( unsigned int freq_Hz ); +int PWM_setDC( int channel, int dutyCycle );
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/rs232.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,154 @@ +#include "mbed.h" +#include "rs232.h" + + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- +Serial rs232(p13, p14); // TXD, RXD +Timeout rs232Timeout; +DigitalOut rs232RTS(p12); // RTS +DigitalIn rs232CTS(p11); // CTS + + +//--------------------------------------- +// Prototypes +//--------------------------------------- +static void RS232_reset( void ); +static void RS232_rxCallback( void ); +static void RS232_rxTimeout( void ); + + +//--------------------------------------- +// Internal variables +//--------------------------------------- +static char RS232Buffer[RS232_BUFFER_LENGTH]; +static sRS232_handler RS232_handler; + + + +//--------------------------------------- +// External variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void RS232_init( void ) +{ + rs232RTS = 0; + rs232.baud(RS232_BAUDRATE); + rs232.format(RS232_BITS, Serial::None, RS232_STOPBIT ); + RS232_reset(); + rs232.attach( &RS232_rxCallback, Serial::RxIrq ); // call RS232_rx() on every RX interrupt +} + + + +// Return 1: Sending was successfull +// Return 0: Could not send data, eithter to many bytes or zero bytes +int RS232_sendData( char *ptrData, int NumBytes ) +{ + int i; + + if( (NumBytes == 0) || (NumBytes >= RS232_BUFFER_LENGTH ) ) + { + return 0; + } + + // Send data + for( i = 0; i < NumBytes; i++ ) + { + while( !rs232.writeable() ); + rs232.putc(*ptrData); + ptrData++; + }//for + + return 1; +} + + +// Return 1: New data available, get pointer to buffer and number of bytes to read +// Return 0: No new data available +int RS232_receiveData( char *& ptrData, int *ptrNumBytes ) +{ + // Dummy operation to avoid compiler warning + ptrData++; + + if( RS232_handler.mode != RS232_MODE_RX_READY ) + { + *ptrNumBytes = 0; + return 0; + } + + *ptrNumBytes = RS232_handler.bytesToRead; + ptrData = &RS232Buffer[0]; + + RS232_reset(); + return 1; +} + + + +void RS232_setRTS( int level) +{ + if( level ) + { + rs232RTS = 0; + } + else + { + rs232RTS = 1; + } + +} + + +int RS232_getCTS( void ) +{ + return rs232CTS; +} + + +//--------------------------------------- +// Internal Functions +//--------------------------------------- + +static void RS232_reset( void ) +{ + RS232_handler.ptrReadPositon = &RS232Buffer[0]; + RS232_handler.bytesToRead = 0; + RS232_handler.mode = RS232_MODE_LISTEN; +} + + +// Function is called on RX interrupt +void RS232_rxCallback( void ) +{ + char tempChar; + + tempChar = rs232.getc(); + + if( (RS232_handler.mode != RS232_MODE_LISTEN) || (RS232_handler.bytesToRead >= RS232_BUFFER_LENGTH) ) + { + return; + } + + // Restart timeout timer + rs232Timeout.attach_us( &RS232_rxTimeout, RS232_RX_TIMEOUT_US ); + // Save received byte + *RS232_handler.ptrReadPositon = tempChar; + RS232_handler.ptrReadPositon++; + RS232_handler.bytesToRead++; +} + + +void RS232_rxTimeout( void ) +{ + // Disable timeout + rs232Timeout.detach(); + RS232_handler.mode = RS232_MODE_RX_READY; // Transmission is ready +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/rs232.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,61 @@ +#include "mbed.h" + + + +//--------------------------------------- +// Definitions +//--------------------------------------- +#define RS232_BUFFER_LENGTH 256 +#define RS232_RX_TIMEOUT_US 50000 +#define RS232_BAUDRATE 9600 +#define RS232_BITS 8 +#define RS232_STOPBIT 1 + + +//--------------------------------------- +// Enums +//--------------------------------------- +typedef enum +{ + RS232_MODE_LISTEN, + RS232_MODE_RX_READY, + //----------------- + CNT_eRS232_mode +}eRS232_mode; + + + +//--------------------------------------- +// Structures +//--------------------------------------- +typedef struct +{ + eRS232_mode mode; // mode of RS232 hardware + char *ptrReadPositon; // actual read position in buffer + int bytesToRead; // Number of received bytes +}sRS232_handler; + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void RS232_init( void ); // Init hardware and internal buffer + + +int RS232_receiveData( char *& ptrData, int *ptrNumBytes ); // "char *&" = reference to pointer !! + // return pointer to buffer and number of received bytes + // Return 1: New data available, get pointer to buffer and number of bytes to read + // Return 0: No new data available +int RS232_sendData( char *ptrData, int NumBytes ); // send data NumBytes bytes from source pointer: ptrData + // Return 1: Sending was successfull + // Return 0: Could not send data, eithter to many bytes, zero bytes or UART is still listining + +void RS232_setRTS( int level); +int RS232_getCTS( void ); \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/uart.cpp Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,156 @@ +#include "mbed.h" +#include "uart.h" + + +//--------------------------------------- +// Hardware recources +//--------------------------------------- +Serial uartPC(USBTX, USBRX); +Timeout uartTimeout; + + + +//--------------------------------------- +// Prototypes +//--------------------------------------- +void UART_calcCRC16( void ); +void UART_initStructure( void ); +void UART_rxCallback( void ); +void UART_rxTimeout( void ); + +//--------------------------------------- +// Internal variables +//--------------------------------------- +char uartBuffer[UART_BUFFER_LENGTH]; + + + +//--------------------------------------- +// External variables +//--------------------------------------- +sUART_handler UART_handler; + + + +//--------------------------------------- +// Global Functions +//--------------------------------------- + +void UART_init( void ) +{ + uartPC.baud(UART_BAUDRATE); + uartPC.format(UART_BITS, Serial::None, UART_STOPBIT ); + UART_reset(); + uartPC.attach( &UART_rxCallback, Serial::RxIrq ); // call UART_rx() on every RX interrupt +} + + +void UART_reset( void ) +{ + UART_handler.ptrBuffer = &uartBuffer[0]; + UART_handler.ptrReadPositon = &uartBuffer[0]; + UART_handler.ptrWritePosition = &uartBuffer[0]; + UART_handler.bytesToRead = 0; + UART_handler.bytesToWrite = 0; + UART_handler.mode = UART_MODE_LISTEN; +} + + +// Only send, if UART is not listining because the same buffer is used for RX and TX +// Return 1: Sending was successfull +// Return 0: Could not send data, eithter to many bytes, zero bytes or UART is still listining +int UART_sendData( void ) +{ + int i; + + // 2013-07-04 SJ: Remove check for UART_MODE_RX_READY because UART_reset() is called on UART_newFrame + // if( (UART_handler.bytesToWrite > 0) && (UART_handler.mode == UART_MODE_RX_READY) && (UART_handler.bytesToWrite < UART_BUFFER_LENGTH - 2 ) ) + if( (UART_handler.bytesToWrite > 0) && (UART_handler.bytesToWrite < UART_BUFFER_LENGTH - 2 ) ) + { + // Calc and add CRC16 to buffer + UART_calcCRC16(); + // Send data + for( i = 0; i < UART_handler.bytesToWrite; i++ ) { + while( !uartPC.writeable() ); + uartPC.putc(uartBuffer[i]); + }//for + // Reset UART function, ready to receive again + UART_reset(); + + return 1; + }//if + else { + return 0; + } + +} + + +int UART_newFrame( void ) +{ + if( UART_handler.mode == UART_MODE_RX_READY ) + { + // 2013-07-04 SJ: + #warning UART: This also resets the read pointer position!!! + UART_reset(); + return 1; + } + else + { + return 0; + } +} + + +// Return 0: CRC16 check failed +// Return 1: CRC16 check passed +int UART_checkReceivedCRC( void ) +{ + // calculate CRC16 of received stream and compare with received CRC16 + + return 1; +} + + + +//--------------------------------------- +// Internal Functions +//--------------------------------------- + +// Calculate CRC16 and add to buffer +void UART_calcCRC16( void ) +{ + + +} + + +// Function is called on RX interrupt +void UART_rxCallback( void ) +{ + char tempChar; + + tempChar = uartPC.getc(); + + // Only save data, if not in blocked mode + if( UART_handler.mode == UART_MODE_LISTEN ) { + if( UART_handler.bytesToRead < UART_BUFFER_LENGTH ) { // no buffer overflow + // Restart timeout timer + uartTimeout.attach_us( &UART_rxTimeout, UART_RX_TIMEOUT_US ); + // Save received byte + *UART_handler.ptrReadPositon = tempChar; + UART_handler.ptrReadPositon++; + UART_handler.bytesToRead++; + } + }// if +} + + +void UART_rxTimeout( void ) +{ + // Disable timeout + uartTimeout.detach(); + UART_handler.ptrReadPositon = &uartBuffer[0]; + UART_handler.mode = UART_MODE_RX_READY; // Transmission is ready +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/310-TMC3-TestHW/uart.h Fri May 09 11:30:55 2014 +0000 @@ -0,0 +1,52 @@ +#include "mbed.h" + + +//--------------------------------------- +// Definitions +//--------------------------------------- +#define UART_BUFFER_LENGTH 512 +#define UART_RX_TIMEOUT_US 1000 +#define UART_BAUDRATE 115200 +#define UART_BITS 8 +#define UART_STOPBIT 1 + +//--------------------------------------- +// Enums +//--------------------------------------- +typedef enum { + UART_MODE_LISTEN, + UART_MODE_RX_READY, + //----------------- + CNT_eUART_mode +} eUART_mode; + + +//--------------------------------------- +// Structures +//--------------------------------------- +typedef struct { + eUART_mode mode; // mode of UART hardware + char *ptrBuffer; // start address of buffer + char *ptrReadPositon; // actual read position in buffer + char *ptrWritePosition; // actual write position in buffer + char bytesToRead; // Number of received bytes + char bytesToWrite; // Number of bytes to send +} sUART_handler; + + + +//--------------------------------------- +// Global Variables +//--------------------------------------- +extern sUART_handler UART_handler; +extern char uartBuffer[UART_BUFFER_LENGTH]; + + +//--------------------------------------- +// Global Functions +//--------------------------------------- +void UART_init( void ); +void UART_reset( void ); // clear all UART registers, --> restart +int UART_newFrame( void ); // check for new received frame +int UART_checkReceivedCRC( void ); // check received CRC16 +int UART_sendData( void ); // send data saved in buffer \ No newline at end of file