[Solved] Control of GPIO pins

Hello!
I saw Vogtinator's incredible wav player thingy on Omnimaga, and had a rummage through the source code for it, in the hope of controlling some simple electronics with the GPIO pins in the dock connector. I also looked through the Hackspire page on the memory addresses of the GPIOs. The page says that each register is a word, but that only bits 0-7 are used. I couldn't find anything which said how many bits are in a word on a Ti-nspire. From Vogtinator's program it seems that the registers have 32 bits.
I tried writing a small program to blink an LED connected to GPIO4. It didn't work, sadly, and I can't figure out why. My code isn't the most compact, but C is new to me, and I don't care that much about neatness, for now I just want my lights to blink.
Here's the mess I created:
Horrid, isn't it. I'd be very grateful if someone could enlighten me! I may have done something stupid, in which case you're welcome to textually scream at me.
I saw Vogtinator's incredible wav player thingy on Omnimaga, and had a rummage through the source code for it, in the hope of controlling some simple electronics with the GPIO pins in the dock connector. I also looked through the Hackspire page on the memory addresses of the GPIOs. The page says that each register is a word, but that only bits 0-7 are used. I couldn't find anything which said how many bits are in a word on a Ti-nspire. From Vogtinator's program it seems that the registers have 32 bits.
I tried writing a small program to blink an LED connected to GPIO4. It didn't work, sadly, and I can't figure out why. My code isn't the most compact, but C is new to me, and I don't care that much about neatness, for now I just want my lights to blink.
Here's the mess I created:
- Code: Select all
#include <nspireio/nspireio.h>
#include <stdint.h>
#include <math.h>
#include <libndls.h>
#define NSPIRE_WORD uint32_t
#define GPIO_SEC_0 (NSPIRE_WORD) 0x90000000
#define GPIO_SEC_1 (NSPIRE_WORD) 0x90000040
#define GPIO_SEC_2 (NSPIRE_WORD) 0x90000080
#define GPIO_SEC_3 (NSPIRE_WORD) 0x900000C0
#define GPIO_DIRECTION_BIT (NSPIRE_WORD) 0x10
#define GPIO_OUT_BIT (NSPIRE_WORD) 0x14
#define GPIO_IN_BIT (NSPIRE_WORD) 0x18
#define OUT 0
#define IN 1
#define LOW 0
#define HIGH 1
NSPIRE_WORD* getRegPtr(int sectionNum, int offset);
void setPinDirection(int GPIO, int direction);
void setOutValue(int GPIO, int value);
int readValue(int GPIO);
int main() {
nio_console console;
nio_init(&console, NIO_MAX_COLS, NIO_MAX_ROWS, 0, 0, NIO_COLOR_BLACK, NIO_COLOR_WHITE, TRUE);
setPinDirection(4, OUT);
setOutValue(4, HIGH);
nio_fprintf(&console, "GPIO4 is now HIGH.\n");
sleep(2000);
setOutValue(4, LOW);
nio_fprintf(&console, "GPIO4 is now LOW.\n");
sleep(2000);
nio_fprintf(&console, "Done!\n");
wait_key_pressed();
nio_free(&console);
return 0;
}
NSPIRE_WORD* getRegPtr(int sectionNum, int offset) {
if(sectionNum == 0) {
return (NSPIRE_WORD*) (GPIO_SEC_0+offset);
}
else if(sectionNum == 1) {
return (NSPIRE_WORD*) (GPIO_SEC_1+offset);
}
else if(sectionNum == 2) {
return (NSPIRE_WORD*) (GPIO_SEC_2+offset);
}
else {
return (NSPIRE_WORD*) (GPIO_SEC_3+offset);
}
}
void setPinDirection(int GPIO, int direction) {
int sectionNum = floor(GPIO/8);
int bitNum = GPIO%8;
NSPIRE_WORD* regPtr = getRegPtr(sectionNum, GPIO_DIRECTION_BIT);
NSPIRE_WORD gpioDirectionReg = *regPtr;
if(direction == OUT) {
gpioDirectionReg &= ~(0x00000001<<bitNum);
}
else {
gpioDirectionReg |= 0x00000001<<bitNum;
}
*regPtr = gpioDirectionReg;
}
void setOutValue(int GPIO, int value) {
int sectionNum = floor(GPIO/8);
int bitNum = GPIO%8;
NSPIRE_WORD* regPtr = getRegPtr(sectionNum, GPIO_OUT_BIT);
NSPIRE_WORD gpioOutputReg = *regPtr;
if(value == LOW) {
gpioOutputReg &= ~(0x00000001<<bitNum);
}
else {
gpioOutputReg |= 0x00000001<<bitNum;
}
*regPtr = gpioOutputReg;
}
int readValue(int GPIO) {
int sectionNum = floor(GPIO/8);
int bitNum = GPIO%8;
NSPIRE_WORD gpioInputReg = *getRegPtr(sectionNum, GPIO_IN_BIT);
return (gpioInputReg>>bitNum)&0x00000001;
}
Horrid, isn't it. I'd be very grateful if someone could enlighten me! I may have done something stupid, in which case you're welcome to textually scream at me.