Jump to content
43oh

Problem creating Tiva C MCP23S17 library


Recommended Posts

Hey!
 
 
Im trying to slap together a library for the microchip mcp23s17 io expander. Ive wrote the functions and wanted try out my creation, but when i verify my code i get error like I did not included SPI.h, but i did.

mcp_lib\MCP23S17.cpp.o: In function `mcp23s17::gpioRegisterWriteByte(unsigned char, unsigned char, bool)':
MCP23S17.cpp:(.text._ZN8mcp23s1721gpioRegisterWriteByteEhhb+0x8): undefined reference to `SPIClass::transfer(unsigned char)'
MCP23S17.cpp:(.text._ZN8mcp23s1721gpioRegisterWriteByteEhhb+0x10): undefined reference to `SPIClass::transfer(unsigned char)'
MCP23S17.cpp:(.text._ZN8mcp23s1721gpioRegisterWriteByteEhhb+0x20): undefined reference to `SPI'
MCP23S17.cpp:(.text._ZN8mcp23s1721gpioRegisterWriteByteEhhb+0x1c): undefined reference to `SPIClass::transfer(unsigned char)'
mcp_lib\MCP23S17.cpp.o: In function `mcp23s17::begin(unsigned char, unsigned char, unsigned char)':
MCP23S17.cpp:(.text._ZN8mcp23s175beginEhhh+0x46): undefined reference to `SPIClass::setModule(unsigned char)'
MCP23S17.cpp:(.text._ZN8mcp23s175beginEhhh+0x4c): undefined reference to `SPIClass::begin()'
MCP23S17.cpp:(.text._ZN8mcp23s175beginEhhh+0x54): undefined reference to `SPIClass::setBitOrder(unsigned char)'
MCP23S17.cpp:(.text._ZN8mcp23s175beginEhhh+0x5c): undefined reference to `SPIClass::setDataMode(unsigned char)'
MCP23S17.cpp:(.text._ZN8mcp23s175beginEhhh+0x8c): undefined reference to `SPI'

Also im not a guru of writing libs, but i wanted to create one that can be used for more than one IC, like the Serials.

 

heres my code: 

#include "Energia.h"
#include <stdio.h>
#include <inttypes.h>
#include <C:\Users\hp\Desktop\energia-0101E0017\hardware\lm4f\libraries\SPI\SPI.h>
#include "MCP23S17.h"





mcp23s17::mcp23s17(){
	
}

void mcp23s17::begin(const uint8_t csPin, const uint8_t haenAdrs, byte spi_module){
	_cs = csPin;
	_adrs = haenAdrs;
	_readCmd = (haenAdrs << 1) | 1;
	_writeCmd = haenAdrs << 1;
	_module = spi_module;
	IOCON = 	0x0A;
	IODIR = 	0x00;
	GPPU = 		0x0C;
	GPIO = 		0x12;
	GPINTEN = 	0x04;
	IPOL = 		0x02;
	DEFVAL = 	0x06;
	INTF = 		0x0E;
	INTCAP = 	0x10;
	OLAT = 		0x14;
	INTCON = 	0x08;
	SPI.setModule(_module);
	SPI.begin();
	SPI.setBitOrder(MSBFIRST);
	SPI.setDataMode(SPI_MODE0);
	pinMode(_cs, OUTPUT);
	digitalWrite(_cs, HIGH);
	delay(100);
	gpioRegisterWriteByte(IOCON, 0b00101000);
	_gpioDirection = 0xFFFF;
	_gpioState = 0xFFFF;
}

void mcp23s17::gpioPinMode(uint16_t mode){
	if (mode == INPUT){
		_gpioDirection = 0xFFFF;
	} else if (mode == OUTPUT){
		_gpioDirection = 0x0000;
		_gpioState = 0x0000;
	} else {
		_gpioDirection = mode;
	}
	gpioRegisterWriteWord(IODIR, _gpioDirection);
	
}

void mcp23s17::gpioPinMode(uint8_t pin, bool mode){
	if (pin < 16){
		mode == INPUT ? _gpioDirection |= (1 << pin) : _gpioDirection &= ~(1 << pin);
		gpioRegisterWriteWord(IODIR, _gpioDirection);
	}
}

void mcp23s17::gpioWritePort(uint16_t value){
	if (value == HIGH){
		_gpioState = 0xFFFF;
	} else if (value == LOW){
		_gpioState = 0x0000;
	} else {
		_gpioState = value;
	}
	gpioRegisterWriteWord(GPIO, _gpioState);
}

void mcp23s17::gpioWritePort(byte lowByte, byte highByte){
	_gpioState = highByte | (lowByte << 8);
	gpioRegisterWriteWord(GPIO, _gpioState);
}

uint16_t mcp23s17::gpioReadPort(){
	return gpioRegisterReadWord(GPIO);
}

void mcp23s17::gpioDigitalWrite(uint8_t pin, bool value){
	if (pin < 16){
		value == HIGH ? _gpioState |= (1 << pin) : _gpioState &= ~(1 << pin);
		gpioRegisterWriteWord(GPIO, _gpioState);
	}
}

int mcp23s17::gpioDigitalRead(uint8_t pin){
	if (pin < 16) return (int)(gpioRegisterReadWord(GPIO) & 1 << pin);
	return 0;
}

uint8_t mcp23s17::gpioRegisterReadByte(byte reg){
	uint8_t data = 0;
	SPI.transfer(reg);
	data = SPI.transfer(0);
	return data;
}

uint16_t mcp23s17::gpioRegisterReadWord(byte reg){
	uint16_t data = 0;
	SPI.transfer(reg);
	data = SPI.transfer(0);
	data = SPI.transfer(0) << 8;
	return data;
}

void mcp23s17::gpioRegisterWriteByte(byte reg, byte data, bool both){
	if (!both){
		SPI.transfer(reg);
		SPI.transfer(data);
	} else {
		SPI.transfer(reg);
		SPI.transfer(data);
		SPI.transfer(data);
	}
}

void mcp23s17::gpioRegisterWriteWord(byte reg, word data){
	SPI.transfer(reg);
	SPI.transfer(data >> 8);
	SPI.transfer(data & 0xFF);
}

void mcp23s17::gpioPortPullup(uint16_t data){
	if (data == HIGH){
		_gpioState = 0xFFFF;
	} else if (data == LOW){
		_gpioState = 0x0000;
	} else {
		_gpioState = data;
	}
	gpioRegisterWriteWord(GPPU, _gpioState);
}

void mcp23s17::startSend(bool mode){
	digitalWrite(_cs, LOW);
	mode == 1 ? SPI.transfer(_readCmd) : SPI.transfer(_writeCmd);
}

void mcp23s17::endSend(){
	digitalWrite(_cs, HIGH);
}

please dont hustle checking the functions, i know they have some bugs, I just want to know how to move forward with this SPI problem and what do i need to write further to be able to call this for multiple expanders.

 

Also i tried SPI.h, "SPI.h" <SPI.h>, but only the concrete location worked when including, why?

 

Is there a detailed how to on making libraries this depth anywhere? I couldnt find any.

 

Thanks!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...