Hello World via. MSP430 (TI Launchpad)

My box of TI Launchpad’s arrived on Friday, but I have only had the chance to start playing with them today, naturally the first port of call is to get the hello world application working, as such, here is my version of Hello World for the MSP430 running on the TI Launchpad.

Just for those who don’t know or are unsure, the default Hello World application for a Microcontroller is to get an LED blinking.

/*
 
$TITLE		:	TI Launchpad MSP430G2231 Flashing LED Example.
 
$Author		:	Steven from http://hackdev.com
$Version	:	0001
 
*/
 
#include 
 
int main(void) {
 
	//Stop Watchdog Timer
	WDTCTL = WDTPW + WDTHOLD;
 
	//Configure Port Directions
	P1DIR |= 0x41;		//0100 0001
 
	//Loop forever
	for(;;) {
		//Produce a delay
		unsigned int i = 50000;
		while(i>0) {
			i--;
		}
 
		//Toggle the LED pins using XOR.
		P1OUT ^= 0x41;
	}
 
}

The Arduino Brain Machine

First off, if this thing messes up your brain, its your own fault and not my problem.

Getting back to my late night hackery, I was kinda bored last night and unable to sleep; but I was previously reading about brain machines (Thanks Hack A Day), they seem to be getting quite popular all of a sudden. So I decided to try my hand at it and I built a very basic pair of glasses, and a little bit of Arduino code to pulse the lights at a specific frequency. I did like the affect it had, it was very relaxing; which is strange to say the least as your having bright lights flashing in your eyes… Anyways, I then remembered Mitch Altman’s brain machine based on a POV kit from LadaAda and set out to find the source code for it. I was not disappointed, and immediately began porting it over to the Arduino. Now, keep in mind this was done at 3am with NO CAFFEINE and a bit of sleep deprivation thrown in so there probably will be bugs and discrepancies. The things I know of so far are:

  • No Button to start the cycle
  • Doesnt stop (or maybe it does and I just didn’t give it long enough)
  • Frequencies are not exactly the same (But I think they were a few points of a Hz off due to the timing of the AVR)

I have not tested the audio bit yet as I was too lazy to fire up the soldering iron again to build the audio filter circuit.

int leftEye = 13;
int rightEye = 12;
int leftEar = 11;
int rightEar = 10;
 
char brainType[] = {
  'b',
  'a',
  'b',
  'a',
  'b',
  'a',
  'b',
  'a',
  'b',
  'a',
  't',
  'a',
  't',
  'a',
  't',
  'a',
  't',
  'a',
  'b',
  'a',
  't',
  'd',
  't',
  'd',
  't',
  'd',
  't',
  'a',
  'b',
  'a',
  't',
  'a',
  'b',
  'a',
  'b',
  'a',
  'b',
  'a',
  'b',
  'a',
  'b',
  'a',
  'b',
  '0'
};
unsigned long int brainDuration[] = {
  600000 ,
  100000 ,
  200000 ,
  150000 ,
  150000 ,
  200000 ,
  100000 ,
  300000 ,
  50000 ,
  600000 ,
  100000 ,
  300000 ,
  200000 ,
  300000 ,
  300000 ,
  150000 ,
  600000 ,
  150000 ,
  10000 ,
  150000 ,
  600000 ,
  10000 ,
  100000 ,
  10000 ,
  100000 ,
  10000 ,
  300000 ,
  150000 ,
  10000 ,
  150000 ,
  300000 ,
  150000 ,
  10000 ,
  200000 ,
  50000 ,
  200000 ,
  150000 ,
  150000 ,
  200000 ,
  100000 ,
  250000 ,
  50000 ,
  600000
};
 
void delay_one_tenth_ms(unsigned long int ms) {
  delayMicroseconds(ms*100);
}
 
void blink_LEDs(int duration, int onTime, int offTime) {
  for(int i=0; i<(duration/(onTime+offTime)); i++) {
    digitalWrite(leftEye, HIGH);
    digitalWrite(rightEye, HIGH);
    delay_one_tenth_ms(onTime);
    digitalWrite(leftEye, LOW);
    digitalWrite(rightEye, LOW);
    delay_one_tenth_ms(offTime);
  }
}
 
void do_brainwave_element(int index) {
  if(brainType[index] == 'b') {
    blink_LEDs(brainDuration[index], 347, 347);
    tone(leftEar, 14);
  }
 
  else if (brainType[index] == 'a') {
    blink_LEDs(brainDuration[index], 451, 451);
    tone(leftEar, 11);
  }
 
  else if (brainType[index] == 't') {
    blink_LEDs(brainDuration[index], 835, 835);
    tone(leftEar, 6);
  }
 
  else if (brainType[index] == 'd') {
    blink_LEDs(brainDuration[index], 2253, 2253);
    tone(leftEar, 2);
  }
 
  else {
    return;
  }
}
 
void setup() {
  pinMode(leftEye, OUTPUT);
  pinMode(rightEye, OUTPUT);
}
 
void loop() {
  tone(rightEar, 400);
  int j=0;
  while(brainType[j] != 0) {
    do_brainwave_element(j);
    noTone(leftEar);
    j++;
  }
  noTone(rightEar);
}

Now, I didn’t bother trying to figure out why the Arduino IDE was fighting with me over the struct, so I just made two indexed arrays instead (remember, no caffeine). Also, this code is not complete. It is all based on Mitch Altman’s brain machine code, posted below.

/*
Sound & Light Machine
Firmware
for use with ATtiny2313
Make Magazine issue #10
Mitch Altman
19-Mar-07
*/
 
#include              // this contains all the IO port definitions
#include       // definitions for interrupts
#include           // definitions for power-down modes
#include        // definitions or keeping constants in program memory
 
#define TIMER0_PRESCALE_1 1
#define TIMER0_PRESCALE_8 2
#define TIMER0_PRESCALE_64 3
#define TIMER0_PRESCALE_256 4
#define TIMER0_PRESCALE_1024 5
#define TIMER1_PRESCALE_1 1
#define TIMER1_PRESCALE_8 2
#define TIMER1_PRESCALE_64 3
#define TIMER1_PRESCALE_256 4
#define TIMER1_PRESCALE_1024 5
 
/*
The hardware for this project is very simple:
     ATtiny2313 has 20 pins:
       pin 1   connects to serial port programming circuitry
       pin 10  ground
       pin 12  PB0 - Left eye LED1
       pin 13  PB1 - Right eye LED1
       pin 14  OC0A - Left ear speaker (base-frequency)
       pin 15  OC1A - Right ear speaker (Offset Frequencies for binaural beats)
       pin 17  connects to serial port programming circuitry
       pin 18  connects to serial port programming circuitry
       pin 19  connects to serial port programming circuitry
       pin 20  +3v
    All other pins are unused
 
    This firmware requires that the clock frequency of the ATtiny
      is the default that it is shipped with:  8.0MHz
*/
 
/*
The C compiler creates code that will transfer all constants into RAM when the microcontroller
resets.  Since this firmware has a table (brainwaveTab) that is too large to transfer into RAM,
the C compiler needs to be told to keep it in program memory space.  This is accomplished by
the macro PROGMEM (this is used, below, in the definition for the brainwaveTab).  Since the
C compiler assumes that constants are in RAM, rather than in program memory, when accessing
the brainwaveTab, we need to use the pgm_read_byte() and pgm_read_dword() macros, and we need
to use the brainwveTab as an address, i.e., precede it with "&".  For example, to access
brainwaveTab[3].bwType, which is a byte, this is how to do it:
     pgm_read_byte( &brainwaveTab[3].bwType );
And to access brainwaveTab[3].bwDuration, which is a double-word, this is how to do it:
     pgm_read_dword( &brainwaveTab[3].bwDuration );
*/
 
// table of values for meditation
//   start with lots of Beta (awake / conscious)
//   add Alpha (dreamy / trancy to connect with subconscious Theta that'll be coming up)
//   reduce Beta (less conscious)
//   start adding Theta (more subconscious)
//   pulse in some Delta (creativity)
//   and then reverse the above to come up refreshed
struct brainwaveElement {
  char bwType;  // 'a' for Alpha, 'b' for Beta, 't' for Theta, or 'd' for Delta ('0' signifies last entry in table
  unsigned long int bwDuration;  // Duration of this Brainwave Type (divide by 10,000 to get seconds)
} const brainwaveTab[] PROGMEM = {
  { 'b', 600000 },
  { 'a', 100000 },
  { 'b', 200000 },
  { 'a', 150000 },
  { 'b', 150000 },
  { 'a', 200000 },
  { 'b', 100000 },
  { 'a', 300000 },
  { 'b',  50000 },
  { 'a', 600000 },
  { 't', 100000 },
  { 'a', 300000 },
  { 't', 200000 },
  { 'a', 300000 },
  { 't', 300000 },
  { 'a', 150000 },
  { 't', 600000 },
  { 'a', 150000 },
  { 'b',  10000 },
  { 'a', 150000 },
  { 't', 600000 },
  { 'd',  10000 },
  { 't', 100000 },
  { 'd',  10000 },
  { 't', 100000 },
  { 'd',  10000 },
  { 't', 300000 },
  { 'a', 150000 },
  { 'b',  10000 },
  { 'a', 150000 },
  { 't', 300000 },
  { 'a', 150000 },
  { 'b',  10000 },
  { 'a', 200000 },
  { 'b',  50000 },
  { 'a', 200000 },
  { 'b', 150000 },
  { 'a', 150000 },
  { 'b', 200000 },
  { 'a', 100000 },
  { 'b', 250000 },
  { 'a',  50000 },
  { 'b', 600000 },
  { '0',      0 }
};
 
// This function delays the specified number of 1/10 milliseconds
void delay_one_tenth_ms(unsigned long int ms) {
  unsigned long int timer;
  const unsigned long int DelayCount=87;  // this value was determined by trial and error
 
  while (ms != 0) {
    // Toggling PD0 is done here to force the compiler to do this loop, rather than optimize it away
    for (timer=0; timer <= DelayCount; timer++) {PIND |= 0b0000001;};
    ms--;
  }
}
 
// This function blinks the LEDs (connected to PB0, PB1 - for Left eye, Right eye, respectively)
//   at the rate determined by onTime and offTime
//   and keeps them blinking for the Duration specified (Duration given in 1/10 millisecs)
// This function also acts as a delay for the Duration specified
void blink_LEDs( unsigned long int duration, unsigned long int onTime, unsigned long int offTime) {
  for (int i=0; i<(duration/(onTime+offTime)); i++) {     PORTB |= 0b00000011;          // turn on LEDs at PB0, PB1     delay_one_tenth_ms(onTime);   //   for onTime     PORTB &= 0b11111100;          // turn off LEDs at PB0, PB1     delay_one_tenth_ms(offTime);  //   for offTime   } } // This function starts the Offset Frequency audio in the Right ear through output OC1A  (using Timer 1) //   to create a binaural beat (between Left and Right ears) for a Brainwave Element //   (the base-frequency of 400.641Hz is already assumed to be playing in the Left ear before calling this function) //   and blinks the LEDs at the same frequency for the Brainwave Element //   and keeps it going for the Duration specified for the Brainwave Element // The timing for the Right ear is done with 16-bit Timer 1 (set up for CTC Mode, toggling output on each compare) //   Output frequency = Fclk / (2 * Prescale * (1 + OCR1A) ) = 8,000,000 / (2 * (1 + OCR1A) ) void do_brainwave_element(int index) {     char brainChr = pgm_read_byte(&brainwaveTab[index].bwType);     if (brainChr == 'b') {          // PORTB &= 0b00001100;  // (for debugging purposes only -- commented out for SLM)          // PORTB |= 0b10000000;       // Beta       // start Timer 1 with the correct Offset Frequency for a binaural beat for the Brainwave Type       //   to Right ear speaker through output OC1A (PB3, pin 15)       OCR1A = 9637;  // T1 generates 415.024Hz, for a binaural beat of 14.4Hz       // delay for the time specified in the table while blinking the LEDs at the correct rate       //   onTime = 34.7ms, offTime = 34.7ms --> 14.4Hz
      blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 347, 347 );
      return;   // Beta
    }
 
    else if (brainChr == 'a') {
         // PORTB &= 0b00001100;  // (for debugging purposes only -- commented out for SLM)
         // PORTB |= 0b01000000;
      // Alpha
      // start Timer 1 with the correct Offset Frequency for a binaural beat for the Brainwave Type
      //   to Right ear speaker through output OC1A (PB3, pin 15)
      OCR1A = 9714;  // T1 generates 411.734Hz, for a binaural beat of 11.1Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      //   onTime = 45.1ms, offTime = 45.0ms --> 11.1Hz
      blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 451, 450 );
      return;   // Alpha
    }
 
    else if (brainChr == 't') {
         // PORTB &= 0b00001100;  // (for debugging purposes only -- commented out for SLM)
         // PORTB |= 0b00100000;
      // Theta
      // start Timer 1 with the correct Offset Frequency for a binaural beat for the Brainwave Type
      //   to Right ear speaker through output OC1A (PB3, pin 15)
      OCR1A = 9836;  // T1 generates 406.628Hz, for a binaural beat of 6.0Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      //   onTime = 83.5ms, offTime = 83.5ms --> 6.0Hz
      blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 835, 835 );
      return;   // Theta
    }
 
    else if (brainChr == 'd') {
         // PORTB &= 0b00001100;  // (for debugging purposes only -- commented out for SLM)
         // PORTB |= 0b00010000;
      // Delta
      // start Timer 1 with the correct Offset Frequency for a binaural beat for the Brainwave Type
      //   to Right ear speaker through output OC1A (PB3, pin 15)
      OCR1A = 9928;  // T1 generates 402.860Hz, for a binaural beat of 2.2Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      //   onTime = 225.3ms, offTime = 225.3ms --> 2.2Hz
      blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 2253, 2253 );
      return;   // Delta
    }
 
    // this should never be executed, since we catch the end of table in the main loop
    else {
         // PORTB &= 0b00001100;  // (for debugging purposes only -- commented out for SLM)
         // PORTB |= 0b00000010;
      return;      // end of table
    }
}
 
int main(void) {
 
  TIMSK = 0x00;  // no Timer interrupts enabled
  DDRB = 0xFF;   // set all PortB pins as outputs
  PORTB = 0x00;  // all PORTB output pins Off
 
  // start up Base frequency = 400.641Hz on Left ear speaker through output OC0A (using Timer 0)
  //   8-bit Timer 0 OC0A (PB2, pin 14) is set up for CTC mode, toggling output on each compare
  //   Fclk = Clock = 8MHz
  //   Prescale = 256
  //   OCR0A = 38
  //   F = Fclk / (2 * Prescale * (1 + OCR0A) ) = 400.641Hz
  TCCR0A = 0b01000010;  // COM0A1:0=01 to toggle OC0A on Compare Match
                        // COM0B1:0=00 to disconnect OC0B
                        // bits 3:2 are unused
                        // WGM01:00=10 for CTC Mode (WGM02=0 in TCCR0B)
  TCCR0B = 0b00000100;  // FOC0A=0 (no force compare)
                        // F0C0B=0 (no force compare)
                        // bits 5:4 are unused
                        // WGM2=0 for CTC Mode (WGM01:00=10 in TCCR0A)
                        // CS02:00=100 for divide by 256 prescaler
  OCR0A = 38;  // to output 400.641Hz on OC0A (PB2, pin 14)
 
  // set up T1 to accept Offset Frequencies on Right ear speaker through OC1A (but don't actually start the Timer 1 here)
  //   16-bit Timer 1 OC1A (PB3, pin 15) is set up for CTC mode, toggling output on each compare
  //   Fclk = Clock = 8MHz
  //   Prescale = 1
  //   OCR0A = value for Beta, Alpha, Theta, or Delta (i.e., 9520, 9714, 9836, or 9928)
  //   F = Fclk / (2 * Prescale * (1 + OCR0A) )
  TCCR1A = 0b01000000;  // COM1A1:0=01 to toggle OC1A on Compare Match
                        // COM1B1:0=00 to disconnect OC1B
                        // bits 3:2 are unused
                        // WGM11:10=00 for CTC Mode (WGM13:12=01 in TCCR1B)
  TCCR1B = 0b00001001;  // ICNC1=0 (no Noise Canceller)
                        // ICES1=0 (don't care about Input Capture Edge)
                        // bit 5 is unused
                        // WGM13:12=01 for CTC Mode (WGM11:10=00 in TCCR1A)
                        // CS12:10=001 for divide by 1 prescaler
  TCCR1C = 0b00000000;  // FOC1A=0 (no Force Output Compare for Channel A)
                        // FOC1B=0 (no Force Output Compare for Channel B)
                        // bits 5:0 are unused
 
  // loop through entire Brainwave Table of Brainwave Elements
  //   each Brainwave Element consists of a Brainwave Type (Beta, Alpha, Theta, or Delta) and a Duration
  // Seeing the LEDs blink and hearing the binaural beats for the sequence of Brainwave Elements
  //   synchs up the user's brain to follow the sequence (hopefully it is a useful sequence)
  int j = 0;
  while (pgm_read_byte(&brainwaveTab[j].bwType) != '0') {  // '0' signifies end of table
    do_brainwave_element(j);
    j++;
  }
 
  // Shut down everything and put the CPU to sleep
  TCCR0B &= 0b11111000;  // CS02:CS00=000 to stop Timer0 (turn off audio in Right ear speaker)
  TCCR1B &= 0b11111000;  // CS12:CS10=000 to stop Timer1 (turn off audio in Left ear speaker)
  MCUCR |= 0b00100000;   // SE=1 (bit 5)
  MCUCR |= 0b00010000;   // SM1:0=01 to enable Power Down Sleep Mode (bits 6, 4)
  delay_one_tenth_ms(10000);  // wait 1 second
  PORTB = 0x00;          // turn off all PORTB outputs
  DDRB = 0x00;           // make PORTB all inputs
  sleep_cpu();           // put CPU into Power Down Sleep Mode
}

Linux Firewalls – Attack detection and response with iptables, PSAD, and FWSNORT.

I seem to work best at night, it’s dark out, and generally quite dark inside too; there is more bandwidth because it is off peek, and in general there is nobody coming over or phoning and interrupting me. Incidentally, its also when I do the majority of my reading, at home; in bed, at night. Much more pleasant that way…
PSAD
Anyway, the latest book I have been reading is called “Linux Firewalls – Attack detection and response with iptables, PSAD and FWSNORT” by Michael Rash who, incidentally runs CipherDyne.org – the site where PSAD, FWSNORT and a fair few other Linux and security tools are developed. Now, you may think that with a title like that your going to have to know a lot about Linux and Firewalls before you begin but that is simply not true, Linux Firewalls takes you right from the very start of how iptables works to manually porting Snort rules over to iptables for detection, and in my humble opinion, it does a damn good job of it. In fact, it done such a good job of it, I have used the reference firewall scripts and PSAD configurations to install and configure iptables and PSAD on my new VPS, which I will hopefully be moving some of my sites to.

Some of the things you should probably have already if you want to get the best out of Linux Firewalls is…

  • A comfortable familiarity with the distribution of Linux you wish to use.
  • Basic understanding of how networks communicate.
  • Reasonable understanding of how TCP works (ie. the three way handshake)
  • Reasonable understanding of how UDP works
  • A system to test this stuff on, if you don’t have a system to test this stuff out on, you could always set up a few virtual machines using VirtualBox or VMWare.
  • A will to learn about this kind of stuff

Missing any one of these things (except the will to learn, thats pretty important) probably isn’t going to be a huge deal, but if your missing a lot of them, you might struggle a bit with some of the concepts.

Here is a sample of one of the basic Linux Firewall scripts that I have adapted from the book…

#!/bin/sh
 
#Port and IP addresses changed to protect the innocent.
 
IPTABLES=/sbin/iptables
MODPROBE=/sbin/modprobe
 
### Flush existing rules and settings. Set to default drop.
echo "[+] Flushing existing iptables rules..."
$IPTABLES -F
$IPTABLES -X
#$IPTABLES -t nat -F
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
#$IPTABLES -P FORWARD DROP
 
### Load the connection tracking modules. Not going to bother with NAT
echo "[+] Loading conntrack support..."
$MODPROBE ip_conntrack
$MODPROBE ip_conntrack_ftp
 
### Input Chain
echo "[+] Setting up INPUT chain..."
$IPTABLES -A INPUT -m state --state INVALID -j LOG --log-prefix "DROP INVALID " --log-ip-options --log-tcp-options
$IPTABLES -A INPUT -m state --state INVALID -j DROP
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
 
### Allowing Broadcasts
$IPTABLES -A INPUT -d 255.255.255.255 -j ACCEPT
$IPTABLES -A INPUT -d x.x.x.x -j ACCEPT #Replace the x's with your network's broadcast address
 
### Anti-spoofing measures
#$IPTABLES -A INPUT -i eth0 -j LOG --log-prefix "SPOOFED PKT "
#$IPTABLES -A INPUT -i eth0 -j DROP
 
### Accept rules...
$IPTABLES -A INPUT -i lo -j ACCEPT #All local connections
$IPTABLES -A INPUT -i eth0 -p tcp --dport 80 --syn -m state --state NEW -j ACCEPT #Webserver
$IPTABLES -A INPUT -i eth0 -p tcp --dport 22 --syn -m state --state NEW -j ACCEPT #SSHd
$IPTABLES -A INPUT -i eth0 -p tcp --dport 1000 --syn -m state --state NEW -j ACCEPT #Webmin
$IPTABLES -A INPUT -p icmp --icmp-type echo-request -j ACCEPT #Accept Ping's
 
### Output Chain
echo "[+] Setting up OUTPUT chain - Allowing all!"
$IPTABLES -A OUTPUT -j ACCEPT
 
### Forward Chain
echo "[+] Setting up FORWARD chain - Nothing to do."
$IPTABLES -A FORWARD -j LOG --log-prefix "DROP FORWARD " --log-ip-options --log-tcp-options
$IPTABLES -A FORWARD -j DROP

I hope that this impromptu book review is of at least some help to you.

Side Note: The bold bits in the posting are for a little test that I  am doing on keyword density… I’ll let you all know how it goes.