Aus RN-Wissen.de
Wechseln zu: Navigation, Suche
Rasenmaehroboter Test

K (Versionen)
K (Demo)
Zeile 628: Zeile 628:
  
 
==Demo==
 
==Demo==
Diese '''erste''' Demo zeigt nur ein Lauflicht auf der M32,- über I2C von der M256 WiFi gesteuert.
+
 
 +
===Versionen===
 +
* '''V1.0''' vom 15.09.2012
 +
 
 +
===Demo===
 +
Diese Demo zeigt nur ein Lauflicht auf der M32,- über I2C von der M256 WiFi gesteuert.
  
 
Datei RP6M256_M32_I2CMaster.c:
 
Datei RP6M256_M32_I2CMaster.c:

Version vom 16. September 2012, 12:13 Uhr

Planung

Erst mal die Planung:

  • 1. Der I2C-Slave für die M32 soll genauso arbeiten, wie der RP6Base I2C-Slave (RP6Base_I2CSlave.c) in den Demos.
  • 2. Er soll möglichst (fast) alle Funktionen/Ressourcen der M32 über I2C "fernsteuerbar" bzw. abfragbar machen.
  • 3. Er soll als I2C-Master eine andere M32, die CCPRO M128 oder die M256 WiFi akzeptieren.
  • 4. Er soll die I2C-Adresse 12 bekommen.
  • 5. Er soll über XBUS INT2 mit dem Master verbunden sein.
  • 6. Er soll wie der Base-Slave auch eine Timeout-Funktion haben.
  • 7. Er soll folgende Befehle (commands) über I2C verstehen:
// Commands:
#define CMD_CONFIGIOS				0
#define CMD_SETIOS				1
#define CMD_CONFIG				2
#define CMD_SETLEDS				3
#define CMD_DISCHARGEPEAKDETECTOR		4
#define CMD_GETMICROPHONEPEAK			5
#define CMD_SETMEM_CS2				6
#define CMD_WRITESPI				7
#define CMD_WRITEWORDSPI			8
#define CMD_READSPI				9
#define CMD_READWORDSPI				10

#define CMD_SET_WDT				11
#define CMD_SET_WDT_RQ				12
#define CMD_SET_HEARTBEAT			13

#define CMD_SPI_EEPROM_WRITEBYTE		14
#define CMD_SPI_EEPROM_WRITEWORD		15
#define CMD_SPI_EEPROM_ENABLEWRITE 		16
#define CMD_SPI_EEPROM_DISABLEWRITE 		17
#define CMD_SPI_EEPROM_READBYTE			18
#define CMD_SPI_EEPROM_READWORD			19
#define CMD_SPI_EEPROM_GETSTATUS		20

#define CMD_INITLCD				21
#define CMD_CLEARLCD				22
#define CMD_CLEARPOSLCD				23
#define CMD_WRITECHARLCD			24
#define CMD_WRITEINTEGERLCD			25
#define CMD_SETCURSORPOSLCD			26

#define CMD_BEEP				27
#define CMD_SETBEEPERPITCH			28
#define CMD_SOUND				29
Dabei setzt Befehl 0 die 8 freien I/O-Pins der M32 als Ein- oder Ausgänge.
Befehl 1 schaltet die einzelnen I/O-Portpins.
Befehl 2 ist Platzhalter ohne Funktion (z.B. zur Konfiguration des Slave).
Befehl 3 schaltet die LEDs. Befehle 4, 5 gehen mit dem Mikro um.
Befehle 6-10 sind die SPI-Befehle.
Befehle 11,12 gehören zum Watchdog-Timer (wie bei der Base!).
Befehl 13 schaltet die LCD Heartbeat (Herzschlag) Funktion.
Befehle 14-20 lesen und schreiben von/aus dem SPI-EEPROM auf der M32.
Befehle 21-26 steuern das LCD auf der M32 an.
Befehle 27-29 steuern den Sound mit dem Beeper.
  • 8. Er soll folgende Register zum Lesen durch den Master vorhalten:
#define I2C_REG_STATUS1     	0
#define I2C_REG_STATUS2     	1
#define I2C_REG_IO_STATUS     	2
#define I2C_REG_MEM_CS2    	3
#define I2C_REG_SPIBYTE    	4
#define I2C_REG_SPIWORD_L   	5
#define I2C_REG_SPIWORD_H   	6
#define I2C_REG_SPIEEPROMSTATUS 7
#define I2C_REG_SPIEEPROMBYTE  	8
#define I2C_REG_SPIEEPROMWORD_L 9
#define I2C_REG_SPIEEPROMWORD_H 10
#define I2C_REG_ADC_4_L      	11
#define I2C_REG_ADC_4_H      	12
#define I2C_REG_ADC_3_L      	13
#define I2C_REG_ADC_3_H      	14
#define I2C_REG_ADC_2_L      	15
#define I2C_REG_ADC_2_H      	16
#define I2C_REG_ADC_6_L     	17
#define I2C_REG_ADC_6_H     	18
#define I2C_REG_ADC_5_L    	19
#define I2C_REG_ADC_5_H    	20
#define I2C_REG_ADC_7_L      	21
#define I2C_REG_ADC_7_H    	22
#define I2C_REG_ADC_MIC_L     	23
#define I2C_REG_ADC_MIC_H     	24
#define I2C_REG_ADC_KEYPAD_L    25
#define I2C_REG_ADC_KEYPAD_H   	26
#define I2C_REG_RELEASEDKEYNUMBER 27
#define I2C_REG_PRESSEDKEYNUMBER  28
#define I2C_REG_LEDS      	29
Die REGs 0,1 sind die Interrupt- und Status-Register wie beim Base-Slave.
IO-Status (REG 2) sind die 8 freien I/O-Ports (sofern auf Eingänge geschaltet).
REGs 3-10 sind Leseregister der SPI- und SPI-EEPROM-Funktionen.
REGs 11-22 sind die freien ADC-Kanäle der M32.
REGs 23,24 sind der ADC-Wert des Mikro.
REGs 25,26 sind der ADC-Keypad-Wert.
REGs 27,28 sind die Nummern der zuletzt losgelassenen bzw. gedrückten Taste.
Mit REG 29 läßt sich der aktuelle Stand der 4 LEDs auslesen (an/aus).
  • 9. Er soll auf die RP6Control Library V1.32beta aufsetzen. Grund: Die aktuelle Lib V1.32beta ist voll kompatibel zur neuesten Version 1.3 und stellt mit eigenen Tasks schon regelmäßig die ADC-Werte und Werte der I/O-Ports zur Verfügung.
  • 10. Bei Timeout soll die M32 funktionsfähig bleiben (Base-Slave bleibt dann in einer Endlosschleife stehen und muss resettet werden!).

I2C-Slave

Versionen

  • V1.0 vom 15.09.2012

Slave

Bitte testet diese Version! Was sollte noch geändert/verbessert werden? Fehler?

Datei RP6Control_I2CSlave.c:

/* 
 * ****************************************************************************
 * RP6 ROBOT SYSTEM - RP6 CONTROL M32 EXAMPLES
 * ****************************************************************************
 * Example: I2C Slave
 * Author(s): Dirk
 * ****************************************************************************
 * Description:
 *
 * A very common thing that many users will want to do with their M32 is 
 * to control it with a second controller which has more free resources. 
 * (more free memory, free I/O Ports and ADCs, faster, etc. pp.
 * for example the RP6 C-Control PRO M128 or M256 WIFI expansion Module)
 * 
 * This programs allows you to control the RP6 CONTROL M32 completely via
 * I2C-Bus as a slave device!
 * 
 * ############################################################################
 * The Robot does NOT move in this example! You can simply put it on a table
 * next to your PC and you should connect it to the PC via the USB Interface!
 * ############################################################################
 * ****************************************************************************
 */

/*****************************************************************************/
// Includes:

#include "RP6ControlLib.h"	 	// The RP6 Control Library v1.32beta.
								// Always needs to be included!
#include "RP6I2CslaveTWI.h"     // Include the I²C-Bus Slave Library

/*****************************************************************************/

// The Slave Address on the I2C Bus can be specified here:
#define RP6Control_I2C_SLAVE_ADR 12

/*****************************************************************************/

// This bitfield contains the main interrupt event status bits. This can be
// read out and any Master devices can react on the specific events.
union {
 	uint8_t byte;
	struct {
		uint8_t timeout:1;
		uint8_t mem_cs2Change:1;
		uint8_t insChange:1;
		uint8_t keyChange:1;
		uint8_t keypressed:1;
		uint8_t unused:3;
	};
} interrupt_status;

// Some status bits with current settings and other things.
union {
 	uint8_t byte;
	struct {
		uint8_t old_mem_cs2:1;
		uint8_t mem_cs2:1;
		uint8_t watchDogTimer:1;
		uint8_t wdtRequest:1;
		uint8_t wdtRequestEnable:1;
		uint8_t heartbeat:1;
		uint8_t unused:2;
	};
} status;

/*****************************************************************************/

/**
 * Generates Interrupt Signal and starts Software Watchdog
 */ 
void signalInterrupt(void)
{
	I2CTWI_dataWasRead = 0;
	DDRD |= EINT2;					// XBUS INT2
	PORTD |= EINT2;
	if(status.watchDogTimer)
		startStopwatch2();
}

/**
 * Clears Interrupt
 */ 
void clearInterrupt(void)
{
	stopStopwatch2();
	setStopwatch2(0);
	status.wdtRequest = false;
	PORTD &= ~EINT2;				// XBUS INT2
	DDRD &= ~EINT2;
}

freeIOs_t old_ins;
uint8_t   old_releasedKeyNumber;
uint8_t   update_count = 0;		

/**
 * This function needs to be called frequently in the main loop. It updates
 * some values (currently only mem_cs2Change, insChange, keyChange and
 * keypressed status, but this may be expanded in future). 
 */ 
void task_update(void)
{
	if(getStopwatch4() > 250)
	{
		// Update mem_cs2 status:
		status.mem_cs2 = PINB & MEM_CS2;
		update_count++;
		setStopwatch4(0);
	}
	if(update_count > 5)
	{
		// Update mem_cs2Change:
		if(!interrupt_status.mem_cs2Change
		 && (status.mem_cs2 != status.old_mem_cs2))
		{
			status.old_mem_cs2 = status.mem_cs2;
			interrupt_status.mem_cs2Change = true;
			signalInterrupt();
		}
		else if(interrupt_status.mem_cs2Change
		 && (status.mem_cs2 == status.old_mem_cs2))
		{
			interrupt_status.mem_cs2Change = false;
			signalInterrupt();
		}
		// Update insChange:
		if(!interrupt_status.insChange && (ins.byte != old_ins.byte))
		{
			old_ins = ins;
			interrupt_status.insChange = true;
			signalInterrupt();
		}
		else if(interrupt_status.insChange && (ins.byte == old_ins.byte))
		{
			interrupt_status.insChange = false;
			signalInterrupt();
		}
		// Update keyChange:
		if(!interrupt_status.keyChange
		 && (releasedKeyNumber != old_releasedKeyNumber))
		{
			old_releasedKeyNumber = releasedKeyNumber;
			interrupt_status.keyChange = true;
			signalInterrupt();
		}
		else if(interrupt_status.keyChange
		 && (releasedKeyNumber == old_releasedKeyNumber))
		{
			interrupt_status.keyChange = false;
			signalInterrupt();
		}
		update_count = 0;
	}
	// Update keypressed status:
	interrupt_status.keypressed = pressedKeyNumber;
}

/*****************************************************************************/
// I2C Registers that can be read by the Master. Their names should 
// be self-explanatory and directly relate to the equivalent variables/functions 
// in the RP6Library

#define I2C_REG_STATUS1 		 	0
#define I2C_REG_STATUS2 		 	1
#define I2C_REG_IO_STATUS	 	 	2
#define I2C_REG_MEM_CS2				3
#define I2C_REG_SPIBYTE				4
#define I2C_REG_SPIWORD_L			5
#define I2C_REG_SPIWORD_H			6
#define I2C_REG_SPIEEPROMSTATUS		7
#define I2C_REG_SPIEEPROMBYTE		8
#define I2C_REG_SPIEEPROMWORD_L		9
#define I2C_REG_SPIEEPROMWORD_H		10
#define I2C_REG_ADC_4_L	 		 	11
#define I2C_REG_ADC_4_H	 		 	12
#define I2C_REG_ADC_3_L	 		 	13
#define I2C_REG_ADC_3_H	 		 	14
#define I2C_REG_ADC_2_L	 		 	15
#define I2C_REG_ADC_2_H	 		 	16
#define I2C_REG_ADC_6_L			 	17
#define I2C_REG_ADC_6_H			 	18
#define I2C_REG_ADC_5_L				19
#define I2C_REG_ADC_5_H				20
#define I2C_REG_ADC_7_L	 		 	21
#define I2C_REG_ADC_7_H 			22
#define I2C_REG_ADC_MIC_L 		 	23
#define I2C_REG_ADC_MIC_H 		 	24
#define I2C_REG_ADC_KEYPAD_L 	 	25
#define I2C_REG_ADC_KEYPAD_H 		26
#define I2C_REG_RELEASEDKEYNUMBER	27
#define I2C_REG_PRESSEDKEYNUMBER 	28
#define I2C_REG_LEDS	 		 	29


// These variables contain the results of the functions readSPI(),
// readWordSPI(), SPI_EEPROM_readByte[s](), SPI_EEPROM_getStatus() after the
// Master sent one of the commands CMD_READSPI, CMD_READWORDSPI,
// CMD_SPI_EEPROM_READBYTE/WORD, CMD_SPI_EEPROM_GETSTATUS. So these variables
// are updated only "on demand" and NOT permanently!
uint8_t  spibyte;
uint16_t spiword;
uint8_t  spieepromstatus;
uint8_t  spieeprombyte;
uint16_t spieepromword;

/**
 * This very important function updates ALL registers that the Master can read.
 * It is called frequently out of the Main loop. 
 */
void task_updateRegisters(void)
{
	if(!I2CTWI_readBusy) 
	{
		I2CTWI_readRegisters[I2C_REG_STATUS1] =				(uint8_t)(interrupt_status.byte);
		I2CTWI_readRegisters[I2C_REG_STATUS2] =				(uint8_t)(status.byte);
		I2CTWI_readRegisters[I2C_REG_IO_STATUS] =			(uint8_t)(ins.byte);
		I2CTWI_readRegisters[I2C_REG_MEM_CS2] =				(uint8_t)(status.mem_cs2);
		I2CTWI_readRegisters[I2C_REG_SPIBYTE] =				(uint8_t)(spibyte);
		I2CTWI_readRegisters[I2C_REG_SPIWORD_L] =			(uint8_t)(spiword);
		I2CTWI_readRegisters[I2C_REG_SPIWORD_H] =			(uint8_t)(spiword>>8);
		I2CTWI_readRegisters[I2C_REG_SPIEEPROMSTATUS] =		(uint8_t)(spieepromstatus);
		I2CTWI_readRegisters[I2C_REG_SPIEEPROMBYTE] = 		(uint8_t)(spieeprombyte);
		I2CTWI_readRegisters[I2C_REG_SPIEEPROMWORD_L] =		(uint8_t)(spieepromword);
		I2CTWI_readRegisters[I2C_REG_SPIEEPROMWORD_H] =		(uint8_t)(spieepromword>>8);
		I2CTWI_readRegisters[I2C_REG_ADC_4_L] =				(uint8_t)(adc4);
		I2CTWI_readRegisters[I2C_REG_ADC_4_H] =				(uint8_t)(adc4>>8);
		I2CTWI_readRegisters[I2C_REG_ADC_3_L] =				(uint8_t)(adc3);
		I2CTWI_readRegisters[I2C_REG_ADC_3_H] =				(uint8_t)(adc3>>8);
		I2CTWI_readRegisters[I2C_REG_ADC_2_L] =				(uint8_t)(adc2);
		I2CTWI_readRegisters[I2C_REG_ADC_2_H] =				(uint8_t)(adc2>>8);
		I2CTWI_readRegisters[I2C_REG_ADC_6_L] =				(uint8_t)(adc6);
		I2CTWI_readRegisters[I2C_REG_ADC_6_H] =				(uint8_t)(adc6>>8);
		I2CTWI_readRegisters[I2C_REG_ADC_5_L] =				(uint8_t)(adc5);
		I2CTWI_readRegisters[I2C_REG_ADC_5_H] =				(uint8_t)(adc5>>8);
		I2CTWI_readRegisters[I2C_REG_ADC_7_L] =				(uint8_t)(adc7);
		I2CTWI_readRegisters[I2C_REG_ADC_7_H] =				(uint8_t)(adc7>>8);
		I2CTWI_readRegisters[I2C_REG_ADC_MIC_L] =			(uint8_t)(adcMic);
		I2CTWI_readRegisters[I2C_REG_ADC_MIC_H] =			(uint8_t)(adcMic>>8);
		I2CTWI_readRegisters[I2C_REG_ADC_KEYPAD_L] =		(uint8_t)(adcKeypad);
		I2CTWI_readRegisters[I2C_REG_ADC_KEYPAD_H] =		(uint8_t)(adcKeypad>>8);
		I2CTWI_readRegisters[I2C_REG_RELEASEDKEYNUMBER] =	(uint8_t)(releasedKeyNumber);
		I2CTWI_readRegisters[I2C_REG_PRESSEDKEYNUMBER] =	(uint8_t)(pressedKeyNumber);
		I2CTWI_readRegisters[I2C_REG_LEDS] =				(uint8_t)(externalPort.LEDS);
		if(I2CTWI_dataWasRead && I2CTWI_dataReadFromReg == 0)
			clearInterrupt();
	}
}

/*****************************************************************************/
// Command Registers - these can be written by the Master.
// The other registers (read registers) can NOT be written to. The only way to
// communicate with the M32 is via specific commands. 
// Of course you can also add more registers if you like...

// ----------------------

#define I2C_REGW_CMD 0
#define I2C_REGW_CMD_PARAM1 1
#define I2C_REGW_CMD_PARAM2 2
#define I2C_REGW_CMD_PARAM3 3
#define I2C_REGW_CMD_PARAM4 4
#define I2C_REGW_CMD_PARAM5 5
#define I2C_REGW_CMD_PARAM6 6

// ----------------------

uint8_t cmd;
uint8_t param1;
uint8_t param2;
uint8_t param3;
uint8_t param4;
uint8_t param5;
uint8_t param6;

/**
 * Checks if a new Command has been received and also reads all 
 * paramters associated with this command.
 * It returns true if a new command has been received.
 */
uint8_t getCommand(void)
{
	if(I2CTWI_writeRegisters[I2C_REGW_CMD] && !I2CTWI_writeBusy) 
	{
		cmd = I2CTWI_writeRegisters[I2C_REGW_CMD]; // store command register
		I2CTWI_writeRegisters[I2C_REGW_CMD] = 0; // clear command register (!!!)
		param1 = I2CTWI_writeRegisters[I2C_REGW_CMD_PARAM1]; // parameters 1-6...
		param2 = I2CTWI_writeRegisters[I2C_REGW_CMD_PARAM2];
		param3 = I2CTWI_writeRegisters[I2C_REGW_CMD_PARAM3];
		param4 = I2CTWI_writeRegisters[I2C_REGW_CMD_PARAM4];
		param5 = I2CTWI_writeRegisters[I2C_REGW_CMD_PARAM5];
		param6 = I2CTWI_writeRegisters[I2C_REGW_CMD_PARAM6];
		return true;
	}
	return false;
}

/*****************************************************************************/
// Command processor:

// Commands:
#define CMD_CONFIGIOS				0
#define CMD_SETIOS					1
#define CMD_CONFIG					2
#define CMD_SETLEDS					3
#define CMD_DISCHARGEPEAKDETECTOR	4
#define CMD_GETMICROPHONEPEAK		5
#define CMD_SETMEM_CS2				6
#define CMD_WRITESPI				7
#define CMD_WRITEWORDSPI			8
#define CMD_READSPI					9
#define CMD_READWORDSPI				10

#define CMD_SET_WDT					11
#define CMD_SET_WDT_RQ				12
#define CMD_SET_HEARTBEAT			13

#define CMD_SPI_EEPROM_WRITEBYTE	14
#define CMD_SPI_EEPROM_WRITEWORD	15
#define CMD_SPI_EEPROM_ENABLEWRITE 	16
#define CMD_SPI_EEPROM_DISABLEWRITE 17
#define CMD_SPI_EEPROM_READBYTE		18
#define CMD_SPI_EEPROM_READWORD		19
#define CMD_SPI_EEPROM_GETSTATUS	20

#define CMD_INITLCD					21
#define CMD_CLEARLCD				22
#define CMD_CLEARPOSLCD				23
#define CMD_WRITECHARLCD			24
#define CMD_WRITEINTEGERLCD			25
#define CMD_SETCURSORPOSLCD			26

#define CMD_BEEP					27
#define CMD_SETBEEPERPITCH			28
#define CMD_SOUND					29


uint8_t rw_buffer[3];

/**
 * This function checks if commands have been received and processes them.
 */ 
void task_commandProcessor(void)
{
	if(getCommand()) 
	{
		switch(cmd) 
		{
			case CMD_CONFIGIOS: if(param1) setFreeIOsToOUT(); else setFreeIOsToIN(); break;
			case CMD_SETIOS: setOUTs(param1); break;
			case CMD_CONFIG: break;
			case CMD_SETLEDS: setLEDs(param1); break;
			case CMD_DISCHARGEPEAKDETECTOR:	dischargePeakDetector(); break;
			case CMD_GETMICROPHONEPEAK:	adcMic = getMicrophonePeak(); break;
			case CMD_SETMEM_CS2: if(param1) PORTB |= MEM_CS2; else PORTB &= ~MEM_CS2; break;
			case CMD_WRITESPI: writeSPI(param1); break;
			case CMD_WRITEWORDSPI: writeWordSPI((param1<<8)+param2); break;
			case CMD_READSPI: spibyte = readSPI(); break;
			case CMD_READWORDSPI: spiword = readWordSPI(); break;
			case CMD_SET_WDT: status.watchDogTimer = param1 ? true : false; break;
			case CMD_SET_WDT_RQ: status.wdtRequestEnable = param1 ? true : false; break;
			case CMD_SET_HEARTBEAT: status.heartbeat = param1 ? true : false; break;
			case CMD_SPI_EEPROM_WRITEBYTE: SPI_EEPROM_writeByte((param1<<8)+param2, param3); break;
			case CMD_SPI_EEPROM_WRITEWORD: rw_buffer[0] = param3; rw_buffer[1] = param4;
				SPI_EEPROM_writeBytes((param1<<8)+param2, &rw_buffer[0], 2); break;
			case CMD_SPI_EEPROM_ENABLEWRITE: SPI_EEPROM_enableWrite(); break;
			case CMD_SPI_EEPROM_DISABLEWRITE: SPI_EEPROM_disableWrite(); break;
			case CMD_SPI_EEPROM_READBYTE: spieeprombyte = SPI_EEPROM_readByte((param1<<8)+param2); break;
			case CMD_SPI_EEPROM_READWORD: SPI_EEPROM_readBytes((param1<<8)+param2, &rw_buffer[0], 2);
				spieepromword = (rw_buffer[0]<<8)+rw_buffer[1]; break;
			case CMD_SPI_EEPROM_GETSTATUS: spieepromstatus = SPI_EEPROM_getStatus(); break;
			case CMD_INITLCD: initLCD(); break;
			case CMD_CLEARLCD: clearLCD(); break;
			case CMD_CLEARPOSLCD: clearPosLCD(param1, param2, param3); break;
			case CMD_WRITECHARLCD: writeCharLCD(param1); break;
			case CMD_WRITEINTEGERLCD: writeIntegerLCD((param1<<8)+param2, param3); break;
			case CMD_SETCURSORPOSLCD: setCursorPosLCD(param1, param2); break;
			case CMD_BEEP: beep(param1, (param2<<8)+param3); break;
			case CMD_SETBEEPERPITCH: setBeeperPitch(param1); break;
			case CMD_SOUND: sound(param1, (param2<<8)+param3, (param4<<8)+param5); break;
		}
	}
}

/**
 * This is the Software watchdog function. After any interrupt event, a timer is
 * started and if a certain amount of time has passed by with no reaction from
 * the Master, the RP6 CONTROL M32 shows this by blinking with status LEDs 1..4
 * for 3 seconds. After this the interrupt and interrupt status are cleared.
 * Usually the Master program has errors or is locked up if it does not react,
 * so the blinking LEDs show, that there is a communication problem.
 */
void task_MasterTimeout(void)
{
	if(status.watchDogTimer)
	{
		static uint8_t blinkflag = 0;
		if(getStopwatch2() > 3000)	// 3 seconds timeout for the master to react on
		{							// our interrupt events - if he does not react, we 
									// blink with LEDs 1..4 for another 3 seconds!
			interrupt_status.timeout = true;
			if(getStopwatch2() > 6000)
			{
				setLEDs(0b0000);	// Clear LEDs
				interrupt_status.byte = 0; // Clear interrupt status
				setStopwatch4(0);
				clearInterrupt();	// Clear interrupt
			}
		}
		else if(getStopwatch3() > 250)
		{
			status.wdtRequest = true;
			signalInterrupt();
			setStopwatch3(0);
		}
		if(interrupt_status.timeout)
		{
			if(getStopwatch5() > 200)
			{
				if(blinkflag)
				{
					setLEDs(0b1001);
					blinkflag = 0;
				}
				else
				{
					setLEDs(0b0110);
					blinkflag = 1;
				}
				setStopwatch5(0);
			}
		}
	}
}

/**
 * LCD Heartbeat function
 */
void task_LCDHeartbeat(void)
{
	if(status.heartbeat)
	{
		if(getStopwatch1() > 500)
		{
			static uint8_t heartbeat = false;
			if(heartbeat)
			{
				clearPosLCD(1, 15, 1);
				heartbeat = false;
			}
			else
			{
				setCursorPosLCD(1, 15);
				writeStringLCD_P("*"); 
				heartbeat = true;
			}
			setStopwatch1(0);
		}
	}
}

/*****************************************************************************/
// Main - The program starts here:

int16_t main(void)
{
	initRP6Control();

	clearLCD();

	setLEDs(0b1111);
	mSleep(500);	   
	setLEDs(0b0000);

	I2CTWI_initSlave(RP6Control_I2C_SLAVE_ADR);

	status.byte = 0;
	interrupt_status.byte = 0;

	startStopwatch1(); // For LCDHeartbeat function
	startStopwatch3();
	startStopwatch4();
	startStopwatch5();

	while(true) 
	{
		task_commandProcessor();
		task_update();
		task_updateRegisters();
		task_RP6M32System();
		task_MasterTimeout();
		task_LCDHeartbeat();
	}

	return 0;
}

I2C-Master (RP6v2 M256 WiFi)

Vorbereitung

Um für die Funktionsnamen der Sound- und EEPROM-Befehle keine Probleme zu bekommen, müssen wir die aktuelle RP6M256Lib.h (Version 1.1 - 16.07.2012) anpassen.

Die folgenden Zeilen müssen auskommentiert werden:

...
uint8_t SPI_EEPROM_readByte(uint16_t memAddr);
void SPI_EEPROM_writeByte(uint16_t memAddr, uint8_t data);
void SPI_EEPROM_enableWrite(void);
void SPI_EEPROM_disableWrite(void);
uint8_t SPI_EEPROM_getStatus(void);

void SPI_EEPROM_writeBytes(uint16_t startAddr, uint8_t *buffer, uint8_t length);
void SPI_EEPROM_readBytes(uint16_t startAddr, uint8_t *buffer, uint8_t length);
...
void beep(unsigned char pitch, unsigned int time);
void setBeeperPitch(uint8_t pitch);
#define sound(_pitch_,_time_,_delay_) {beep(_pitch_,_time_);mSleep(_delay_ + _time_);}
...

Keine Sorge! Dadurch verändert sich in der RP6M256Lib nichts, weil es alle diese Funktionen nicht gibt (es gibt ja auch kein SPI-EEPROM und keinen Beeper auf der M256 WiFi)!

Demo

Versionen

  • V1.0 vom 15.09.2012

Demo

Diese Demo zeigt nur ein Lauflicht auf der M32,- über I2C von der M256 WiFi gesteuert.

Datei RP6M256_M32_I2CMaster.c:

/* 
 * ****************************************************************************
 * RP6 ROBOT SYSTEM - RP6 CONTROL M256 Examples
 * ****************************************************************************
 * Example: M32 I2C Master
 * Author(s): Dirk
 * ****************************************************************************
 * Description:
 * In this example we show how to react on interrupt requests from the 
 * Microcontroller on the RP6 CONTROL M32. It sets the signal INT2 (that's what
 * it is called on the RP6 hardware...) which is connected to the external 
 * interrupt PCINT 15 of the Microcontroller. We will NOT use the interrupt
 * routine to react on the interrupts. Instead we simply poll the interrupt
 * pin and check if there is an request. This works better together with
 * the I2C Bus Master routines of the RP6Library. They are also interrupt
 * based and thus we can not directly start a new transmission out of the
 * interrupt service routine of a Pin Change Interrupt. 
 * It would not make any difference in this case, but we could get new
 * problems when there is a I2C Bus transfer in progress when we get
 * an interrupt event. Then it could get cancelled and replaced by the 
 * new request, which would cause problems sometimes.
 *
 * When an Interrupt Event occurs on the Slave Controller, the program 
 * initiates an I2C-Bus request for the first 3 Status Registers. 
 * In the Status registers some bits change depending on what has caused 
 * the interrupt Event (e.g. a key on the RP6 CONTROL M32 has been pressed
 * or released...) so we can determine what happened and can react on it 
 * accordingly.
 * 
 * ############################################################################
 * The Robot does NOT move in this example! You can simply put it on a table
 * next to your PC and you should connect it to the PC via the USB Interface!
 * You should also connect to it via WIFI.
 * ############################################################################
 * ****************************************************************************
 */

/*****************************************************************************/
// Includes:

#include "RP6M256Lib.h" 				// The RP6 M256 Library. 
										// Always needs to be included!
#include "RP6I2CmasterTWI.h"			// Include the I2C-Bus Master Library

/*****************************************************************************/
/*****************************************************************************/
// Include our new "RP6M256 M32 I2C Master library":

#include "RP6M256_M32_I2CMasterLib.h"

/*****************************************************************************/

/**
 * Here we do a small M32 LED test 
 * (a running light with direction changing)!
 * For timing we use Stopwatch #2. 
 */
void task_runningLight(void)
{
	static uint8_t runLight = 1; 
	static uint8_t dir;  // Moving direction for running light
	if(getStopwatch2() > 100) // 100ms
	{
		// Set status LEDs to the value of the variable runLight:
		setM32LEDs(runLight); 
	
		// Shift the LED bit left or right depending on direction:
		if(dir == 0)
			runLight <<= 1; 
		else
			runLight >>= 1;
			
		// Change the direction if we reached one of the two outer LEDs:
		if(runLight > 7 ) 
			dir = 1;			
		else if (runLight < 2 ) 
			dir = 0;
		
		// Reset Stopwatch2:
		setStopwatch2(0);
	}
}

/*****************************************************************************/
// I2C Requests: 

/**
 * The I2C_requestedDataReady Event Handler
 */
void I2C_requestedDataReady(uint8_t dataRequestID)
{
	checkM32Status(dataRequestID);
}

/*****************************************************************************/
// I2C Error handler

/**
 * This function gets called automatically if there was an I2C Error like
 * the slave sent a "not acknowledge" (NACK, error codes e.g. 0x20 or 0x30).
 * The most common mistakes are: 
 */
void I2C_transmissionError(uint8_t errorState)
{
	writeString_P_WIFI("\nI2C ERROR - TWI STATE: 0x");
	writeInteger_WIFI(errorState, HEX);
	writeChar_WIFI('\n');
}

/*****************************************************************************/
// Main function - The program starts here:

int main(void)
{
	initRP6M256();    // Always call this first! The Processor will not work
					  // correctly otherwise. 

	initLCD(); // Initialize the LC-Display (LCD)
			   // Always call this before using the LCD!

	setLEDs(0b1111);
	mSleep(500);
	setLEDs(0b0000);

	writeString_P_WIFI("\n\nRP6 CONTROL M32 I2C Master Example Program!\n"); 

	// IMPORTANT:
	I2CTWI_initMaster(100); // Initialize the TWI Module for Master operation
							// with 100kHz SCL Frequency
							
    // Also very important in this example:                        
	// Register the event handlers:
	I2CTWI_setTransmissionErrorHandler(I2C_transmissionError);
	I2CTWI_setRequestedDataReadyHandler(I2C_requestedDataReady);

	setLEDs(0b1111);

	// Write a text message to the LCD:
	showScreenLCD("################", "################");
	mSleep(1500);
	showScreenLCD("RP6v2-M256-WIFI ", "<<LC - DISPLAY>>");
	mSleep(2500); 
	showScreenLCD(" M32 I2C Master", "Example Program");
	mSleep(2500);
	clearLCD(); 

	setLEDs(0b0000);

	// ---------------------------------------
	
	// Enable LCD Heartbeat function on Slave:
	I2CTWI_transmit3Bytes(I2C_RP6_M32_ADR, 0, CMD_SET_HEARTBEAT, true);
	// (The LCD Heartbeat function lets a '*' character blink with 1Hz on
	// the LC-Display of the Slave if the Slave functions properly.) 
	// Enable Software Watchdog timer on Slave:
	I2CTWI_transmit3Bytes(I2C_RP6_M32_ADR, 0, CMD_SET_WDT, true);
	// (This timer shows blinking LEDs on the Slave if the Master Controller 
	// does not react on interrupt requests after a few seconds. This can
	// help to find communication problems ...)
	
	// ---------------------------------------
	
	startStopwatch1(); // For LCDHeartbeat function
	startStopwatch2(); // For runningLight function
	
	while(true) 
	{
        // You need to call task_I2CTWI frequently out of the
        // main loop - otherwise the I2C Bus request functions don't work.  
		task_checkPCINT15();
        task_I2CTWI();
		task_LCDHeartbeat();
		task_runningLight();
	}

	return 0;
}

M32 I2C Master Library

Hier mal eine erste Version V0.9beta der M32 I2C Master Library, die die Befehle enthält, um die M32 über I2C (fast) auf dieselbe Weise zu steuern, wie das mit einem Programm auf der M32 direkt der Fall wäre.

In der Lib ist Vieles noch nicht fertig. Bitte gern ergänzen,- insbesondere die komplexeren SPI- und LCD-Funktionen fehlen noch und warten auf Ergänzung ;-).

Header

Datei RP6M256_M32_I2CMasterLib.h:

/* 
 * ****************************************************************************
 * RP6 ROBOT SYSTEM - RP6 CONTROL M256 Examples
 * ****************************************************************************
 * Example: M32 I2C Master Library
 * Author(s): Dirk
 * ****************************************************************************
 * Description:
 * Header file for new library.
 *
 * ****************************************************************************
 */

#ifndef RP6M256_M32_I2CMASTERLIB_H
#define RP6M256_M32_I2CMASTERLIB_H


/*****************************************************************************/
// Includes:

#include "RP6M256Lib.h"
#include "RP6I2CmasterTWI.h"

// Define the RP6 CONTROL M32 address here:
#define I2C_RP6_M32_ADR 12

/*****************************************************************************/
// These are the same register/command definitions as you can find them in
// the I2C Bus Slave Example program for RP6 CONTROL M32:

#define I2C_REG_STATUS1 		 	0
#define I2C_REG_STATUS2 		 	1
#define I2C_REG_IO_STATUS	 	 	2
#define I2C_REG_MEM_CS2				3
#define I2C_REG_SPIBYTE				4
#define I2C_REG_SPIWORD_L			5
#define I2C_REG_SPIWORD_H			6
#define I2C_REG_SPIEEPROMSTATUS		7
#define I2C_REG_SPIEEPROMBYTE		8
#define I2C_REG_SPIEEPROMWORD_L		9
#define I2C_REG_SPIEEPROMWORD_H		10
#define I2C_REG_ADC_4_L	 		 	11
#define I2C_REG_ADC_4_H	 		 	12
#define I2C_REG_ADC_3_L	 		 	13
#define I2C_REG_ADC_3_H	 		 	14
#define I2C_REG_ADC_2_L	 		 	15
#define I2C_REG_ADC_2_H	 		 	16
#define I2C_REG_ADC_6_L			 	17
#define I2C_REG_ADC_6_H			 	18
#define I2C_REG_ADC_5_L				19
#define I2C_REG_ADC_5_H				20
#define I2C_REG_ADC_7_L	 		 	21
#define I2C_REG_ADC_7_H 			22
#define I2C_REG_ADC_MIC_L 		 	23
#define I2C_REG_ADC_MIC_H 		 	24
#define I2C_REG_ADC_KEYPAD_L 	 	25
#define I2C_REG_ADC_KEYPAD_H 		26
#define I2C_REG_RELEASEDKEYNUMBER	27
#define I2C_REG_PRESSEDKEYNUMBER 	28
#define I2C_REG_LEDS	 		 	29

#define CMD_CONFIGIOS				0
#define CMD_SETIOS					1
#define CMD_CONFIG					2
#define CMD_SETLEDS					3
#define CMD_DISCHARGEPEAKDETECTOR	4
#define CMD_GETMICROPHONEPEAK		5
#define CMD_SETMEM_CS2				6
#define CMD_WRITESPI				7
#define CMD_WRITEWORDSPI			8
#define CMD_READSPI					9
#define CMD_READWORDSPI				10
#define CMD_SET_WDT					11
#define CMD_SET_WDT_RQ				12
#define CMD_SET_HEARTBEAT			13
#define CMD_SPI_EEPROM_WRITEBYTE	14
#define CMD_SPI_EEPROM_WRITEWORD	15
#define CMD_SPI_EEPROM_ENABLEWRITE 	16
#define CMD_SPI_EEPROM_DISABLEWRITE 17
#define CMD_SPI_EEPROM_READBYTE		18
#define CMD_SPI_EEPROM_READWORD		19
#define CMD_SPI_EEPROM_GETSTATUS	20
#define CMD_INITLCD					21
#define CMD_CLEARLCD				22
#define CMD_CLEARPOSLCD				23
#define CMD_WRITECHARLCD			24
#define CMD_WRITEINTEGERLCD			25
#define CMD_SETCURSORPOSLCD			26
#define CMD_BEEP					27
#define CMD_SETBEEPERPITCH			28
#define CMD_SOUND					29


/*****************************************************************************/
// The Status Struct - here we write the data of the main status register.
// It is the same definition as it can be found in the RP6Control_I2CSlave
// program!
union {
 	uint8_t byte;
	struct {
		uint8_t timeout:1;
		uint8_t mem_cs2Change:1;
		uint8_t insChange:1;
		uint8_t keyChange:1;
		uint8_t keypressed:1;
		uint8_t unused:3;
	};
} interrupt_status;

// Some slave status bits with current settings and other things:
union {
 	uint8_t byte;
	struct {
		uint8_t old_mem_cs2:1;
		uint8_t mem_cs2:1;
		uint8_t watchDogTimer:1;
		uint8_t wdtRequest:1;
		uint8_t wdtRequestEnable:1;
		uint8_t heartbeat:1;
		uint8_t unused:2;
	};
} status;

// These variables contain the results of the M32 functions readSPI(),
// readWordSPI(), SPI_EEPROM_readByte[s](), SPI_EEPROM_getStatus() after the
// Master sent one of the commands CMD_READSPI, CMD_READWORDSPI,
// CMD_SPI_EEPROM_READBYTE/WORD, CMD_SPI_EEPROM_GETSTATUS. These variables
// can be found identically in the RP6Control_I2CSlave program!
uint8_t  spibyte;
uint16_t spiword;
uint8_t  spieepromstatus;
uint8_t  spieeprombyte;
uint16_t spieepromword;

/*****************************************************************************/
// M32 slave variables and typedefs

// Free M32 IO portpins:
typedef union {
	uint8_t byte;
	struct {
		unsigned pd5:1;				// IO_PD5
		unsigned pd6:1;				// IO_PD6
		unsigned pc2:1;				// IO_PC2
		unsigned pc3:1;				// IO_PC3
		unsigned pc4:1;				// IO_PC4
		unsigned pc5:1;				// IO_PC5
		unsigned pc6:1;				// IO_PC6
		unsigned pc7:1;				// IO_PC7
	};
} freeIOs_t;
extern freeIOs_t ins;
extern freeIOs_t outs;

// Free M32 ADCs:
extern uint16_t adc7;
extern uint16_t adc6;
extern uint16_t adc5;
extern uint16_t adc4;
extern uint16_t adc3;
extern uint16_t adc2;
extern uint16_t adcKeypad;
extern uint16_t adcMic;

// Key pressed/released number:
extern uint8_t releasedKeyNumber;
extern uint8_t pressedKeyNumber;

// M32 status LEDs:
typedef union {
 	uint8_t byte;
	struct {
		uint8_t LEDS:4;
		uint8_t LCDD:4;
	};
	struct {
		uint8_t LED4:1;
		uint8_t LED3:1;
		uint8_t LED2:1;
		uint8_t LED1:1;
		uint8_t D0:1;
		uint8_t D1:1;
		uint8_t D2:1;
		uint8_t D3:1;
	};
} externalPort_t;
extern externalPort_t externalPort;

// M32 PORTA A/D Convertor channels:
#define ADC_7			7
#define ADC_6 			6
#define ADC_5 			5
#define ADC_4 			4
#define ADC_3 			3
#define ADC_2 			2
#define ADC_KEYPAD 		1
#define ADC_MIC 		0

#define PCINT15_STATUS_CHECK 0

/*****************************************************************************/
// Master functions

void task_LCDHeartbeat(void);
void getAllSensors(void);
void task_checkPCINT15(void);
uint8_t checkM32Status(uint8_t dataRequestID);
void I2C_transmissionError(uint8_t errorState);
void WDT_setRequestHandler(void (*requestHandler)(void));
void waitForTransmitComplete(void);

/*****************************************************************************/
// M32 Slave functions

uint16_t readM32ADC(uint8_t channel);
void setFreeIOsToIN(void);
void setFreeIOsToOUT(void);
void updateOUTs(void);
void setOUTs(uint8_t out);

void writeM32SPI(uint8_t data);
uint8_t readM32SPI(void);
uint16_t readWordM32SPI(void);
void writeWordM32SPI(uint16_t data);
void writeBufferM32SPI(uint8_t *buffer, uint8_t length);
void readBufferM32SPI(uint8_t *buffer, uint8_t length);

uint8_t SPI_EEPROM_readByte(uint16_t memAddr);
void SPI_EEPROM_writeByte(uint16_t memAddr, uint8_t data);
void SPI_EEPROM_enableWrite(void);
void SPI_EEPROM_disableWrite(void);
uint8_t SPI_EEPROM_getStatus(void);
void SPI_EEPROM_writeBytes(uint16_t startAddr, uint8_t *buffer, uint8_t length);
void SPI_EEPROM_readBytes(uint16_t startAddr, uint8_t *buffer, uint8_t length);

void outputExt(void);
void setM32LEDs(uint8_t leds);

void initM32LCD(void);
void clearM32LCD(void);
void clearPosM32LCD(uint8_t line, uint8_t pos, uint8_t length);
void writeCharM32LCD(uint8_t ch);
//void writeStringM32LCD(char *string);
//void writeStringLengthM32LCD(char *string, uint8_t length, uint8_t offset);
//void showScreenM32LCD(const char *line1, const char *line2);
void writeIntegerM32LCD(int16_t number, uint8_t base);
//void writeIntegerLengthM32LCD(int16_t number, uint8_t base, uint8_t length);
void setCursorPosM32LCD(uint8_t line, uint8_t pos);

uint8_t getM32PressedKeyNumber(void);
void dischargePeakDetector(void);
uint16_t getMicrophonePeak(void);

void beep(unsigned char pitch, unsigned int time);
void setBeeperPitch(uint8_t pitch);
void sound(uint8_t pitch, unsigned int time, uint16_t delay);

/*****************************************************************************/

#endif

Library

Datei RP6M256_M32_I2CMasterLib.c:

/* 
 * ****************************************************************************
 * RP6 ROBOT SYSTEM - RP6 CONTROL M256 Examples
 * ****************************************************************************
 * Example: M32 I2C Master Library
 * Author(s): Dirk
 * ****************************************************************************
 * Description:
 * 
 * This is our new Library that allows us to control the M32 nearly the same
 * as it was with RP6ControlLib. 
 * Some details are different, but in general it is the same.
 *
 * ****************************************************************************
 */

/*****************************************************************************/
// Includes:

#include "RP6M256_M32_I2CMasterLib.h" 		

/*****************************************************************************/

/*****************************************************************************/
// WDT:

// -------------------------------
// WDT Request Event Handler:

void WDT_request_DUMMY(void){}
static void (*WDT_requestHandler)(void) = WDT_request_DUMMY;

void WDT_setRequestHandler(void (*requestHandler)(void)) 
{
	WDT_requestHandler = requestHandler;
}

/**
 * A small useful routine, to show that the Program is running and not locked up.
 * It lets a '*' character blink with 1Hz on the LC-Display.
 * When you change the program and it seems to lock up under certain conditions, you
 * can see if at least the main loop is still working or if only the I2C Bus Interface
 * is locked up. 
 */
void task_LCDHeartbeat(void)
{
	static uint8_t heartbeat = false;
	if(getStopwatch1() > 500)
	{
		if(heartbeat)
		{
			clearPosLCD(0, 15, 1);
			heartbeat = false;
		}
		else
		{
			setCursorPosLCD(0, 15);
			writeStringLCD_P("*"); 
			heartbeat = true;
		}
		setStopwatch1(0);
	}
}

/*****************************************************************************/
// The M32 variables

// Here we define the same variables as can be found in the RP6Control
// Library. You can use them exactly the same as in a program for the RP6
// CONTROL M32!

// Free M32 IO portpins:
freeIOs_t ins;
freeIOs_t outs;

// Free M32 ADCs:
uint16_t adc7;
uint16_t adc6;
uint16_t adc5;
uint16_t adc4;
uint16_t adc3;
uint16_t adc2;
uint16_t adcKeypad;
uint16_t adcMic;

// Key pressed/released number:
uint8_t releasedKeyNumber;
uint8_t pressedKeyNumber;

// M32 status LEDs:
externalPort_t externalPort;

/*****************************************************************************/

uint8_t sensorBuf[30]; 

/**
 * In order to use the same register names as in the RP6ControlLib,
 * this function reads all ADC channels into the same values
 * as in the RP6ControlLib. 
 * Of course this function needs some time to read all these
 * 30 registers via the I2C Bus. 
 */
void getAllSensors(void)
{
	I2CTWI_readRegisters(I2C_RP6_M32_ADR, 0, sensorBuf, 30);

	interrupt_status.byte = sensorBuf[0];
	status.byte = sensorBuf[1];
	ins.byte = sensorBuf[2];
	status.mem_cs2 = sensorBuf[3];
	spibyte = sensorBuf[4];
	spiword = sensorBuf[5] + (sensorBuf[6]<<8);
	spieepromstatus = sensorBuf[7];
	spieeprombyte = sensorBuf[8];
	spieepromword = sensorBuf[9] + (sensorBuf[10]<<8);
	adc4 = sensorBuf[11] + (sensorBuf[12]<<8);
	adc3 = sensorBuf[13] + (sensorBuf[14]<<8);
	adc2 = sensorBuf[15] + (sensorBuf[16]<<8);
	adc6 = sensorBuf[17] + (sensorBuf[18]<<8);
	adc5 = sensorBuf[19] + (sensorBuf[20]<<8);
	adc7 = sensorBuf[21] + (sensorBuf[22]<<8);
	adcMic = sensorBuf[23] + (sensorBuf[24]<<8);
	adcKeypad = sensorBuf[25] + (sensorBuf[26]<<8);
	releasedKeyNumber = sensorBuf[27];
	pressedKeyNumber = sensorBuf[28];
	externalPort.LEDS = sensorBuf[29];
}

/*****************************************************************************/
/*****************************************************************************/
// PCINT15 Check:

#define PCINT15_STATUS_CHECK 0
uint8_t block = false;

/** 
 * This function has to be called VERY frequently out of the main loop.
 * Bigger delays result in slower reaction to Interrupt requests of the 
 * Slave. 
 * This function initiates a request of the first 3 Registers of the I2C Bus
 * Slave Controller - these Bytes contain status bits, which tell us what
 * caused the Interrupt request. 
 * They are checked in the requested data ready handler which should call
 * the function checkM32Status below. 
 */
void task_checkPCINT15(void)
{
	if(!block && (PINJ & INT2_PI15))	// XBUS INT2 -> PJ6 (PCINT15)
	{
		block = true; // Block further requests and wait until 
					  // this request has been processed.
		I2CTWI_requestRegisterFromDevice(I2C_RP6_M32_ADR, PCINT15_STATUS_CHECK, 0, 3);
	}
}

/*****************************************************************************/

// Bitmasks for comparison: 
#define MASK_KEYPRESSED		0b00010000 // keypressed bit 
#define MASK_KEYCHANGE		0b00001000 // key Change bit 
#define MASK_INSCHANGE		0b00000100 // ins Change bit
#define MASK_MEM_CS2CHANGE	0b00000010 // mem_cs2 Change bit
#define MASK_TIMEOUT		0b00000001 // timeout bit

uint8_t messageBuf[16]; 

/**
 * Call this function from the requestedDataReady Handler for the I2C Bus. 
 * It will then check if the request was a PCINT15 Request from this library
 * and then it will process the event and call event handlers is neccessary. 
 * It returns true if it was a PCINT15 request and false otherwise.
 * This way you can check if it was a request that you initiated or a 
 * request initiated in the library...
 */
uint8_t checkM32Status(uint8_t dataRequestID)
{
	if(dataRequestID == PCINT15_STATUS_CHECK) 
	{            
                         
		// get received data: 
		I2CTWI_getReceivedData(messageBuf, 3);
		
		// We want to check if the ACS status bits have changed, so we XOR 
		// ('^' is eXclusive OR operator) them with old 
		// value for comparison and later mask them in the if conditions below...
//		uint8_t compare = messageBuf[0] ^ interrupt_status.byte;
		interrupt_status.byte = messageBuf[0]; // Update local register


// ToDo:

		block = false; // clear block flag! 
		return true; // It was a PCINT15 request --> return true;
	}
	return false; // It was not a PCINT15 request --> return false
}

/**
 * Waits until I2C transmission is complete.
 *
 */
void waitForTransmitComplete(void)
{
	while(I2CTWI_isBusy() || TWI_operation != I2CTWI_NO_OPERATION) task_I2CTWI();
	mSleep(6);
	task_checkPCINT15();
	while(I2CTWI_isBusy() || TWI_operation != I2CTWI_NO_OPERATION) task_I2CTWI();
}

/*****************************************************************************/
// M32 slave functions

/**
 * Read ADC channel (10 bit -> result is an integer from 0 to 1023).
 * The channels (ADC_BAT etc.) are defined in the RP6Control.h and
 * RP6M256_M32_I2CMaster.h files.
 *
 */
uint16_t readM32ADC(uint8_t channel)
{
	switch(channel) 
	{
		case ADC_MIC:
			I2CTWI_readRegisters(I2C_RP6_M32_ADR, I2C_REG_ADC_MIC_L, sensorBuf, 2);
			adcMic = sensorBuf[0] + (sensorBuf[1]<<8);
			return adcMic; break;
		case ADC_KEYPAD:
			I2CTWI_readRegisters(I2C_RP6_M32_ADR, I2C_REG_ADC_KEYPAD_L, sensorBuf, 2);
			adcKeypad = sensorBuf[0] + (sensorBuf[1]<<8);
			return adcKeypad; break;
		case ADC_2:
			I2CTWI_readRegisters(I2C_RP6_M32_ADR, I2C_REG_ADC_2_L, sensorBuf, 2);
			adc2 = sensorBuf[0] + (sensorBuf[1]<<8);
			return adc2; break;
		case ADC_3:
			I2CTWI_readRegisters(I2C_RP6_M32_ADR, I2C_REG_ADC_3_L, sensorBuf, 2);
			adc3 = sensorBuf[0] + (sensorBuf[1]<<8);
			return adc3; break;
		case ADC_4:
			I2CTWI_readRegisters(I2C_RP6_M32_ADR, I2C_REG_ADC_4_L, sensorBuf, 2);
			adc4 = sensorBuf[0] + (sensorBuf[1]<<8);
			return adc4; break;
		case ADC_5:
			I2CTWI_readRegisters(I2C_RP6_M32_ADR, I2C_REG_ADC_5_L, sensorBuf, 2);
			adc5 = sensorBuf[0] + (sensorBuf[1]<<8);
			return adc5; break;
		case ADC_6:
			I2CTWI_readRegisters(I2C_RP6_M32_ADR, I2C_REG_ADC_6_L, sensorBuf, 2);
			adc6 = sensorBuf[0] + (sensorBuf[1]<<8);
			return adc6; break;
		case ADC_7:
			I2CTWI_readRegisters(I2C_RP6_M32_ADR, I2C_REG_ADC_7_L, sensorBuf, 2);
			adc7 = sensorBuf[0] + (sensorBuf[1]<<8);
			return adc7; break;
	}
	return 0;
}

/**
 * This function switches all free ports (PC2..7, PD5, PD6) to input!
 */
void setFreeIOsToIN(void)
{
	I2CTWI_transmit3Bytes(I2C_RP6_M32_ADR, 0, CMD_CONFIGIOS, false);
}

/**
 * This function switches all free ports (PC2..7, PD5, PD6) to output!
 */
void setFreeIOsToOUT(void)
{
	I2CTWI_transmit3Bytes(I2C_RP6_M32_ADR, 0, CMD_CONFIGIOS, true);
}

/**
 * Update the ports with current value from the global outs variable!
 * If one or more port pins were switched to output before, they will be
 * set (high) or cleared (low).
 *
 * Example:
 *
 *			outs.byte = 0b00101001;
 *			updateOUTs();
 *			// This clears all ports and sets the ports IO_PD5, IO_PC3
 *			// and IO_PC5!
 *
 *			// Other possibility:
 *			outs.pc2 = true;
 *			updateOUTs();
 *			// This sets IO_PC2 and does not affect any other port!
 */
void updateOUTs(void)
{
	I2CTWI_transmit3Bytes(I2C_RP6_M32_ADR, 0, CMD_SETIOS, outs.byte);
}

/**
 * Set IO ports - this is very handy if you want to set all ports!
 *
 * Example:
 *
 *			setOUTs(0b00101001);
 *			// This clears all ports and sets the ports IO_PD5, IO_PC3
 *			// and IO_PC5!
 */
void setOUTs(uint8_t out)
{
	outs.byte = out;
	updateOUTs();
}

/**
 * Writes a single Databyte to the SPI Interface.
 */
void writeM32SPI(uint8_t data)
{
	I2CTWI_transmit3Bytes(I2C_RP6_M32_ADR, 0, CMD_WRITESPI, data);
}

/**
 * Reads a single Databyte from the SPI Interface.
 */
uint8_t readM32SPI(void)
{
	return 0;
}

/**
 * Reads TWO Bytes from the SPI Interface and returns them as
 * a 16 Bit value with first byte read in the upper 8 bits.
 */
uint16_t readWordM32SPI(void)
{
	return 0;
}

/**
 * Writes two Bytes contained in the 16 Bit parameter "data".
 * The first byte to be written needs to be in the upper 8 Bits.
 */
void writeWordM32SPI(uint16_t data)
{
	I2CTWI_transmit4Bytes(I2C_RP6_M32_ADR, 0, CMD_WRITEWORDSPI, (data>>8), data);
}

/** 
 * This function writes up to 255 Bytes to the SPI Interface.
 * The numer of bytes in the Buffer that shall be written is given 
 * by the parameter length.
 */
void writeBufferM32SPI(uint8_t *buffer, uint8_t length)
{
}

/** 
 * Reads "length" Bytes from SPI Interface into the buffer.
 */
void readBufferM32SPI(uint8_t *buffer, uint8_t length)
{
}

/**
 * Reads a single Byte from the external EEPROM.
 */
uint8_t SPI_EEPROM_readByte(uint16_t memAddr)
{
	return 0;
}

/**
 * Write a single data byte to the specified EEPROM address.
 */
void SPI_EEPROM_writeByte(uint16_t memAddr, uint8_t data)
{
	uint8_t msg[5];
	msg[0] = 0; msg[1] = CMD_SPI_EEPROM_WRITEBYTE;
	msg[2] = (memAddr>>8); msg[3] = memAddr; msg[4] = data;
	I2CTWI_transmitBytes(I2C_RP6_M32_ADR, &msg[0], 5);
}

/**
 * Enable Write Mode
 */
void SPI_EEPROM_enableWrite(void)
{
	I2CTWI_transmit2Bytes(I2C_RP6_M32_ADR, 0, CMD_SPI_EEPROM_ENABLEWRITE);
}

/**
 * Disable Write Mode
 */
void SPI_EEPROM_disableWrite(void)
{
	I2CTWI_transmit2Bytes(I2C_RP6_M32_ADR, 0, CMD_SPI_EEPROM_DISABLEWRITE);
}

/**
 * Returns EEPROM Status register - for checking if EEPROM is busy. 
 * Writing takes about 5ms. 
 */
uint8_t SPI_EEPROM_getStatus(void)
{
	return 0;
}

/**
 * Write "length" Bytes from the Buffer to the EEPROM. 
 * YOU CAN ONLY WRITE MAXIMAL 64 BYTES AT ONCE!!! This is the Pagesize!
 * You can NOT cross a page boundary! For example when you write 20 Bytes 
 * starting at address 54, you will not write up to Byte address 74, but
 * instead only up to 63 and then it continues at Byte 0 and writes 
 * the rest up to Byte 10!
 *
 */
void SPI_EEPROM_writeBytes(uint16_t startAddr, uint8_t *buffer, uint8_t length)
{
}

/**
 * Reads "length" Bytes into the Buffer "buffer" from startAdr on. 
 * You can read the complete EEPROM into a buffer at once - if it is large enough. 
 */
void SPI_EEPROM_readBytes(uint16_t startAddr, uint8_t *buffer, uint8_t length)
{
}

/**
 * Same as on RP6ControlLib - takes value from externalPort register and sets
 * LEDs on the M32.
 */
void outputExt(void)
{
	I2CTWI_transmit3Bytes(I2C_RP6_M32_ADR, 0, CMD_SETLEDS, externalPort.byte);
}

/**
 * Same as setLEDs from RP6ControlLib or RP6M256Lib. Sets LEDs on the M32.
 * We need to call it setM32LEDs because setLEDs function is already
 * defined in RP6M256Lib.
 */
void setM32LEDs(uint8_t leds)
{
	externalPort.LEDS = leds;
	outputExt();
}

/**
 * Initialize the LCD. Always call this before using the LCD! 
 *
 */
void initM32LCD(void)
{
	I2CTWI_transmit2Bytes(I2C_RP6_M32_ADR, 0, CMD_INITLCD);
}

/**
 * Clears the whole LCD!
 */
void clearM32LCD(void)
{
	I2CTWI_transmit2Bytes(I2C_RP6_M32_ADR, 0, CMD_CLEARLCD);
}

/**
 * Clears some characters after the given position.
 */
void clearPosM32LCD(uint8_t line, uint8_t pos, uint8_t length)
{
	uint8_t msg[5];
	msg[0] = 0; msg[1] = CMD_CLEARPOSLCD;
	msg[2] = line; msg[3] = pos; msg[4] = length;
	I2CTWI_transmitBytes(I2C_RP6_M32_ADR, &msg[0], 5);
}

/**
 * Write a single character to the LCD.
 *
 * Example:
 *
 *			writeCharM32LCD('R');
 *			writeCharM32LCD('P');
 *			writeCharM32LCD('6');
 *			writeCharM32LCD(' ');
 *			writeCharM32LCD('0');
 *			writeCharM32LCD(48); // 48 is ASCII code for '0'
 *			writeCharM32LCD(49); // '1'
 *			writeCharM32LCD(50); // '2'
 *			writeCharM32LCD(51); // '3'
 *			//...
 *
 *			would output:
 *			RP6 00123
 *			at the current cursor position!
 *			use setCursorPosM32LCD function to move the cursor to a 
 *			different location!
 */
void writeCharM32LCD(uint8_t ch)
{
	I2CTWI_transmit3Bytes(I2C_RP6_M32_ADR, 0, CMD_WRITECHARLCD, ch);
}

// ToDo:
//void writeStringM32LCD(char *string) {}
//void writeStringLengthM32LCD(char *string, uint8_t length, uint8_t offset) {}
//void showScreenM32LCD(const char *line1, const char *line2) {}

/**
 * Write a number (with specified base) to the LCD.
 *
 * Example:
 *
 *			// Write a hexadecimal number to the LCD:
 *			writeIntegerM32LCD(0xAACC,16);
 *			// Instead of 16 you can also write "HEX" as this is defined in the
 *			// RP6M256Lib.h :
 *			writeIntegerM32LCD(0xAACC, HEX);
 *			// Other Formats:
 *			writeIntegerM32LCD(1024,DEC);  	// Decimal
 *			writeIntegerM32LCD(511,OCT);		// Octal
 *			writeIntegerM32LCD(0b11010111,BIN); // Binary
 */
void writeIntegerM32LCD(int16_t number, uint8_t base)
{
	uint8_t msg[5];
	msg[0] = 0; msg[1] = CMD_WRITEINTEGERLCD;
	msg[2] = (number>>8); msg[3] = number; msg[4] = base;
	I2CTWI_transmitBytes(I2C_RP6_M32_ADR, &msg[0], 5);
}

// ToDo:
//void writeIntegerLengthM32LCD(int16_t number, uint8_t base, uint8_t length) {}

/**
 * Sets the cursor position on LCD.
 */
void setCursorPosM32LCD(uint8_t line, uint8_t pos)
{
	I2CTWI_transmit4Bytes(I2C_RP6_M32_ADR, 0, CMD_SETCURSORPOSLCD, line, pos);
}

/**
 * Checks which key is pressed - returns the key number,
 * or 0, if no key is pressed.
 *
 */
uint8_t getM32PressedKeyNumber(void)
{
	I2CTWI_readRegisters(I2C_RP6_M32_ADR, I2C_REG_PRESSEDKEYNUMBER, sensorBuf, 1);
	pressedKeyNumber = sensorBuf[0];
	return pressedKeyNumber;
}

/** 
 * This function discharges the Capacitor of the peak detection circuit 
 * used for the Microphone. This is required to remove any previous
 * charge from the capacitor.
 *
 * Normally this function should NOT be used, because the m32 slave
 * program does all this automatically!
 * 
 */
void dischargePeakDetector(void)
{
	I2CTWI_transmit2Bytes(I2C_RP6_M32_ADR, 0, CMD_DISCHARGEPEAKDETECTOR);
}

/**
 * Reads the Microphone peak detector.
 * 
 */
uint16_t getMicrophonePeak(void)
{
	I2CTWI_readRegisters(I2C_RP6_M32_ADR, I2C_REG_ADC_MIC_L, sensorBuf, 2);
	adcMic = sensorBuf[0] + (sensorBuf[1]<<8);
	return adcMic;
}

/**
 * You can use this function to make the beeper beep ;) 
 * But this function should not be used as it does not
 * generate a delay for the sound and a delay between 
 * two sounds. Better is to use the "sound" function, which
 * uses this function and adds the required delays.
 *
 */
void beep(unsigned char pitch, unsigned int time)
{
	uint8_t msg[5];
	msg[0] = 0; msg[1] = CMD_BEEP;
	msg[2] = pitch; msg[3] = (time>>8); msg[4] = time; 
	I2CTWI_transmitBytes(I2C_RP6_M32_ADR, &msg[0], 5);
}

/**
 * This function has no timing stuff, but otherwise
 * it has the same effect as "beep". It only sets the pitch
 * and this can be used to generate tone sequences which
 * would sound bad if the beeper turns of for a very short time
 * in between - such as alarm tones or special melodies etc. 
 */
void setBeeperPitch(uint8_t pitch)
{
	I2CTWI_transmit3Bytes(I2C_RP6_M32_ADR, 0, CMD_SETBEEPERPITCH, pitch);
}

 /**
 * You can use this function to make the beeper beep ;) 
 * like with the function beep, but it adds a delay between 
 * two sounds.
 *
 * "sound(pitch,time,delay)"
 *
 * 0 = lowest frequency
 * 255 = highest frequency
 *
 * Example:
 * sound(150,50,25);
 * sound(200,50,25);
 *
 * This function is BLOCKING during the sound latency, because
 * on the M32 slave the sound() macro is blocking as well!
 */
void sound(uint8_t pitch, unsigned int time, uint16_t delay)
{
	uint8_t msg[7];
	msg[0] = 0; msg[1] = CMD_SOUND;
	msg[2] = pitch; msg[3] = (time>>8); msg[4] = time;
	msg[5] = (delay>>8); msg[6] = delay;
	I2CTWI_transmitBytes(I2C_RP6_M32_ADR, &msg[0], 6);
	mSleep(time + delay);			// M32 sound() macro is blocking!
}

Siehe auch


Weblinks


Autoren

--Dirk 11:38, 15. Sep 2012 (CET)


LiFePO4 Speicher Test