summaryrefslogtreecommitdiff
path: root/include/arch/msp430fr5994lp/driver
diff options
context:
space:
mode:
Diffstat (limited to 'include/arch/msp430fr5994lp/driver')
-rw-r--r--include/arch/msp430fr5994lp/driver/adc.h17
-rw-r--r--include/arch/msp430fr5994lp/driver/counter.h27
-rw-r--r--include/arch/msp430fr5994lp/driver/gpio.h202
-rw-r--r--include/arch/msp430fr5994lp/driver/i2c.h19
-rw-r--r--include/arch/msp430fr5994lp/driver/spi_a1.h18
-rw-r--r--include/arch/msp430fr5994lp/driver/spi_b.h18
-rw-r--r--include/arch/msp430fr5994lp/driver/stdin.h24
-rw-r--r--include/arch/msp430fr5994lp/driver/stdout.h19
-rw-r--r--include/arch/msp430fr5994lp/driver/timer.h75
-rw-r--r--include/arch/msp430fr5994lp/driver/uptime.h30
10 files changed, 449 insertions, 0 deletions
diff --git a/include/arch/msp430fr5994lp/driver/adc.h b/include/arch/msp430fr5994lp/driver/adc.h
new file mode 100644
index 0000000..d93aed4
--- /dev/null
+++ b/include/arch/msp430fr5994lp/driver/adc.h
@@ -0,0 +1,17 @@
+#ifndef ADC_H
+#define ADC_H
+
+class ADC {
+ private:
+ ADC(ADC const &copy);
+
+ public:
+ ADC() {}
+
+ float getTemp();
+ float getVCC();
+};
+
+extern ADC adc;
+
+#endif
diff --git a/include/arch/msp430fr5994lp/driver/counter.h b/include/arch/msp430fr5994lp/driver/counter.h
new file mode 100644
index 0000000..e7f0507
--- /dev/null
+++ b/include/arch/msp430fr5994lp/driver/counter.h
@@ -0,0 +1,27 @@
+#include <msp430.h>
+#include <stdint.h>
+
+class Counter {
+ private:
+ Counter(const Counter &copy);
+
+ public:
+ uint16_t value;
+ uint8_t overflowed;
+
+ Counter() : overflowed(0) {}
+
+ inline void start() {
+ overflowed = 0;
+ TA2CTL = TASSEL__SMCLK | ID__1 | MC__CONTINUOUS;
+ TA2EX0 = 0;
+ TA2CTL |= TACLR;
+ }
+
+ inline void stop() {
+ TA2CTL = 0;
+ value = TA2R;
+ }
+};
+
+extern Counter counter;
diff --git a/include/arch/msp430fr5994lp/driver/gpio.h b/include/arch/msp430fr5994lp/driver/gpio.h
new file mode 100644
index 0000000..ef681db
--- /dev/null
+++ b/include/arch/msp430fr5994lp/driver/gpio.h
@@ -0,0 +1,202 @@
+#ifndef GPIO_H
+#define GPIO_H
+
+#include <msp430.h>
+
+class GPIO {
+ private:
+ GPIO(const GPIO &copy);
+
+ public:
+ GPIO () {}
+
+ enum Pin : unsigned char {
+ p1_0 = 0, p1_1, p1_2, p1_3, p1_4, p1_5, p1_6, p1_7,
+ p2_0, p2_1, p2_2, p2_3, p2_4, p2_5, p2_6, p2_7,
+ p3_0, p3_1, p3_2, p3_3, p3_4, p3_5, p3_6, p3_7,
+ p4_0, p4_1, p4_2, p4_3, p4_4, p4_5, p4_6, p4_7,
+ p5_0, p5_1, p5_2, p5_3, p5_4, p5_5, p5_6, p5_7,
+ p6_0, p6_1, p6_2, p6_3, p6_4, p6_5, p6_6, p6_7,
+ p7_0, p7_1, p7_2, p7_3, p7_4, p7_5, p7_6, p7_7,
+ p8_0, p8_1, p8_2, p8_3, p8_4, p8_5, p8_6, p8_7,
+ pj_0, pj_1, pj_2, pj_3, pj_4, pj_5, pj_6, pj_7,
+ PIN_INVALID
+ };
+
+ inline void setup() {
+ P1OUT = 0;
+ P2OUT = 0;
+ P3OUT = 0;
+ P4OUT = 0;
+ P5OUT = 0;
+ P6OUT = 0;
+ P7OUT = 0;
+ P8OUT = 0;
+ PJOUT = 0;
+ P1DIR = BIT0 | BIT1 | 0xff; // red LED, green LED
+ P2DIR = 0xff ^ (BIT0 | BIT1); // UART
+ P3DIR = 0xff;
+ P4DIR = 0xff;
+ P5DIR = 0xff;
+ P6DIR = 0xff;
+ P7DIR = 0xff;
+ P8DIR = 0x0f;
+ PJDIR = BIT6 | BIT7; // HFXT (not populated)
+ }
+ inline void led_on(unsigned char id) {
+ if (id == 0) {
+ P1OUT |= BIT0;
+ } else {
+ P1OUT |= BIT1;
+ }
+ }
+ inline void led_off(unsigned char id) {
+ if (id == 0) {
+ P1OUT &= ~BIT0;
+ } else {
+ P1OUT &= ~BIT1;
+ }
+ }
+ inline void led_toggle(unsigned char id) {
+ if (id == 0) {
+ P1OUT ^= BIT0;
+ } else {
+ P1OUT ^= BIT1;
+ }
+ }
+ inline void input(unsigned char const pin) {
+ if (pin < p2_0) {
+ P1DIR &= ~(1 << pin);
+ } else if (pin < p3_0) {
+ P2DIR &= ~(1 << (pin - p2_0));
+ } else if (pin < p4_0) {
+ P3DIR &= ~(1 << (pin - p3_0));
+ } else if (pin < pj_0) {
+ P4DIR &= ~(1 << (pin - p4_0));
+ } else if (pin < PIN_INVALID) {
+ PJDIR &= ~(1 << (pin - pj_0));
+ }
+ }
+ inline void input(unsigned char const pin, unsigned char const pull) {
+ if (pin < p2_0) {
+ P1DIR &= ~(1 << pin);
+ P1OUT = pull ? (P1OUT | (1 << pin)) : (P1OUT & ~(1 << pin));
+ P1REN |= (1 << pin);
+ } else if (pin < p3_0) {
+ P2DIR &= ~(1 << (pin - p2_0));
+ P2OUT = pull ? (P2OUT | (1 << (pin - p2_0))) : (P2OUT & ~(1 << (pin - p2_0)));
+ P2REN |= (1 << (pin - p2_0));
+ } else if (pin < p4_0) {
+ P3DIR &= ~(1 << (pin - p3_0));
+ P3OUT = pull ? (P3OUT | (1 << (pin - p3_0))) : (P3OUT & ~(1 << (pin - p3_0)));
+ P3REN |= (1 << (pin - p3_0));
+ } else if (pin < pj_0) {
+ P4DIR &= ~(1 << (pin - p4_0));
+ P4OUT = pull ? (P4OUT | (1 << (pin - p4_0))) : (P4OUT & ~(1 << (pin - p4_0)));
+ P4REN |= (1 << (pin - p4_0));
+ } else if (pin < PIN_INVALID) {
+ PJDIR &= ~(1 << (pin - pj_0));
+ PJOUT = pull ? (PJOUT | (1 << (pin - pj_0))) : (PJOUT & ~(1 << (pin - pj_0)));
+ PJREN |= (1 << (pin - pj_0));
+ }
+ }
+ inline void output(unsigned char const pin) {
+ if (pin < p2_0) {
+ P1DIR |= (1 << pin);
+ } else if (pin < p3_0) {
+ P2DIR |= (1 << (pin - p2_0));
+ } else if (pin < p4_0) {
+ P3DIR |= (1 << (pin - p3_0));
+ } else if (pin < pj_0) {
+ P4DIR |= (1 << (pin - p4_0));
+ } else if (pin < PIN_INVALID) {
+ PJDIR |= (1 << (pin - pj_0));
+ }
+ }
+ inline void output(unsigned char const pin, unsigned char const value) {
+ if (pin < p2_0) {
+ P1OUT = value ? (P1OUT | (1 << pin)) : (P1OUT & ~(1 << pin));
+ P1DIR |= (1 << pin);
+ } else if (pin < p3_0) {
+ P2OUT = value ? (P2OUT | (1 << (pin - p2_0))) : (P2OUT & ~(1 << (pin - p2_0)));
+ P2DIR |= (1 << (pin - p2_0));
+ } else if (pin < p4_0) {
+ P3OUT = value ? (P3OUT | (1 << (pin - p3_0))) : (P3OUT & ~(1 << (pin - p3_0)));
+ P3DIR |= (1 << (pin - p3_0));
+ } else if (pin < pj_0) {
+ P4OUT = value ? (P4OUT | (1 << (pin - p4_0))) : (P4OUT & ~(1 << (pin - p4_0)));
+ P4DIR |= (1 << (pin - p4_0));
+ } else if (pin < PIN_INVALID) {
+ PJOUT = value ? (PJOUT | (1 << (pin - pj_0))) : (PJOUT & ~(1 << (pin - pj_0)));
+ PJDIR |= (1 << (pin - pj_0));
+ }
+ }
+ inline unsigned char read(unsigned char const pin) {
+ if (pin < p2_0) {
+ return P1IN & (1 << pin);
+ } else if (pin < p3_0) {
+ return P2IN & (1 << (pin - p2_0));
+ } else if (pin < p4_0) {
+ return P3IN & (1 << (pin - p3_0));
+ } else if (pin < pj_0) {
+ return P4IN & (1 << (pin - p4_0));
+ } else if (pin < PIN_INVALID) {
+ return PJIN & (1 << (pin - pj_0));
+ }
+ return 0;
+ }
+ inline void write(unsigned char const pin, unsigned char value) {
+ if (pin < p2_0) {
+ if (value) {
+ P1OUT |= (1 << pin);
+ } else {
+ P1OUT &= ~(1 << pin);
+ }
+ } else if (pin < p3_0) {
+ if (value) {
+ P2OUT |= (1 << (pin - p2_0));
+ } else {
+ P2OUT &= ~(1 << (pin - p2_0));
+ }
+ } else if (pin < p4_0) {
+ if (value) {
+ P3OUT |= (1 << (pin - p3_0));
+ } else {
+ P3OUT &= ~(1 << (pin - p3_0));
+ }
+ } else if (pin < pj_0) {
+ if (value) {
+ P4OUT |= (1 << (pin - p4_0));
+ } else {
+ P4OUT &= ~(1 << (pin - p4_0));
+ }
+ } else if (pin < PIN_INVALID) {
+ if (value) {
+ PJOUT |= (1 << (pin - pj_0));
+ } else {
+ PJOUT &= ~(1 << (pin - pj_0));
+ }
+ }
+ }
+ inline void write_mask(unsigned char const pin_base, unsigned char set_mask, unsigned char clear_mask) {
+ if (pin_base < p2_0) {
+ P1OUT = (P1OUT | set_mask) & ~clear_mask;
+ }
+ if (pin_base < p3_0) {
+ P2OUT = (P2OUT | set_mask) & ~clear_mask;
+ }
+ if (pin_base < p4_0) {
+ P3OUT = (P3OUT | set_mask) & ~clear_mask;
+ }
+ if (pin_base < pj_0) {
+ P4OUT = (P4OUT | set_mask) & ~clear_mask;
+ }
+ if (pin_base < PIN_INVALID) {
+ PJOUT = (PJOUT | set_mask) & ~clear_mask;
+ }
+ }
+};
+
+extern GPIO gpio;
+
+#endif
diff --git a/include/arch/msp430fr5994lp/driver/i2c.h b/include/arch/msp430fr5994lp/driver/i2c.h
new file mode 100644
index 0000000..6d6ea66
--- /dev/null
+++ b/include/arch/msp430fr5994lp/driver/i2c.h
@@ -0,0 +1,19 @@
+#ifndef I2C_H
+#define I2C_H
+
+class I2C {
+ private:
+ I2C(const I2C &copy);
+
+ public:
+ I2C () {}
+ signed char setup();
+ void scan(unsigned int *results);
+ signed char xmit(unsigned char address,
+ unsigned char tx_len, unsigned char *tx_buf,
+ unsigned char rx_len, unsigned char *rx_buf);
+};
+
+extern I2C i2c;
+
+#endif
diff --git a/include/arch/msp430fr5994lp/driver/spi_a1.h b/include/arch/msp430fr5994lp/driver/spi_a1.h
new file mode 100644
index 0000000..8c593ee
--- /dev/null
+++ b/include/arch/msp430fr5994lp/driver/spi_a1.h
@@ -0,0 +1,18 @@
+#ifndef SPI_H
+#define SPI_H
+
+class SPI {
+ private:
+ SPI(const SPI &copy);
+
+ public:
+ SPI () {}
+ signed char setup();
+ signed char xmit(
+ unsigned char tx_len, unsigned char *tx_buf,
+ unsigned char rx_len, unsigned char *rx_buf);
+};
+
+extern SPI spi;
+
+#endif
diff --git a/include/arch/msp430fr5994lp/driver/spi_b.h b/include/arch/msp430fr5994lp/driver/spi_b.h
new file mode 100644
index 0000000..4be7346
--- /dev/null
+++ b/include/arch/msp430fr5994lp/driver/spi_b.h
@@ -0,0 +1,18 @@
+#ifndef SPI_H
+#define SPI_H
+
+class SPI {
+ private:
+ SPI(const SPI &copy);
+
+ public:
+ SPI () {}
+ void setup();
+ signed char xmit(
+ unsigned char tx_len, unsigned char *tx_buf,
+ unsigned char rx_len, unsigned char *rx_buf);
+};
+
+extern SPI spi;
+
+#endif
diff --git a/include/arch/msp430fr5994lp/driver/stdin.h b/include/arch/msp430fr5994lp/driver/stdin.h
new file mode 100644
index 0000000..6462e0c
--- /dev/null
+++ b/include/arch/msp430fr5994lp/driver/stdin.h
@@ -0,0 +1,24 @@
+#ifndef STANDARDINPUT_H
+#define STANDARDINPUT_H
+
+class StandardInput {
+ private:
+ StandardInput(const StandardInput &copy);
+ char buffer[8];
+ unsigned char write_pos, read_pos;
+
+ public:
+ StandardInput() : write_pos(0), read_pos(0) {}
+ void setup();
+ bool hasKey();
+ char getKey();
+
+ inline void addKey(char key) {
+ buffer[write_pos++] = key;
+ write_pos %= 8;
+ }
+};
+
+extern StandardInput kin;
+
+#endif
diff --git a/include/arch/msp430fr5994lp/driver/stdout.h b/include/arch/msp430fr5994lp/driver/stdout.h
new file mode 100644
index 0000000..2eb669d
--- /dev/null
+++ b/include/arch/msp430fr5994lp/driver/stdout.h
@@ -0,0 +1,19 @@
+#ifndef STANDARDOUTPUT_H
+#define STANDARDOUTPUT_H
+
+#include "object/outputstream.h"
+
+class StandardOutput : public OutputStream {
+ private:
+ StandardOutput(const StandardOutput &copy);
+
+ public:
+ StandardOutput () {}
+ void setup();
+
+ virtual void put(char c) override;
+};
+
+extern StandardOutput kout;
+
+#endif
diff --git a/include/arch/msp430fr5994lp/driver/timer.h b/include/arch/msp430fr5994lp/driver/timer.h
new file mode 100644
index 0000000..cb37da2
--- /dev/null
+++ b/include/arch/msp430fr5994lp/driver/timer.h
@@ -0,0 +1,75 @@
+#include <msp430.h>
+#include <stdint.h>
+
+#define ON_TIMER_INTERRUPT_head __attribute__((interrupt(TIMER0_A1_VECTOR))) __attribute__((wakeup)) void handle_timer0_overflow() { if (TA0IV == 0x0e) {
+#define ON_TIMER_INTERRUPT_tail } }
+
+#if F_CPU == 16000000UL
+#define _TA0_MAIN_DIV ID__8
+#elif F_CPU == 8000000UL
+#define _TA0_MAIN_DIV ID__4
+#elif F_CPU == 4000000UL
+#define _TA0_MAIN_DIV ID__2
+#elif F_CPU == 1000000UL
+#define _TA0_MAIN_DIV ID__1
+#else
+#error Unsupported F_CPU
+#endif
+
+class Timer {
+ private:
+ Timer(const Timer &copy);
+
+ public:
+ Timer() {}
+
+#if F_CPU == 1000000UL
+ inline void setup_khz(uint16_t const frequency) {
+ TA0CTL = TASSEL__SMCLK | ID__1; // -> 1 MHz base
+ TA0EX0 = 0;
+ TA0CCR0 = 1000UL / frequency;
+ TA0CTL |= TACLR;
+ }
+
+ inline void setup_hz(uint16_t const frequency) { // 1 MHz base
+ if (frequency < 20) {
+ TA0CTL = TASSEL__SMCLK | ID__8; // /8
+ TA0EX0 = 1; // /2 -> /16 -> 62500 Hz
+ TA0CCR0 = 62500UL / frequency;
+ } else {
+ TA0CTL = TASSEL__SMCLK | ID__1;
+ TA0EX0 = 0;
+ TA0CCR0 = 1000000UL / frequency;
+ }
+ TA0CTL |= TACLR;
+ }
+#else
+ inline void setup_khz(uint16_t const frequency) {
+ TA0CTL = TASSEL__SMCLK | _TA0_MAIN_DIV; // -> 2 MHz base
+ TA0EX0 = 0;
+ TA0CCR0 = 2000UL / frequency;
+ TA0CTL |= TACLR;
+ }
+
+ inline void setup_hz(uint16_t const frequency) { // 2 MHz base
+ TA0CTL = TASSEL__SMCLK | _TA0_MAIN_DIV;
+ TA0EX0 = 0;
+ TA0CCR0 = 2000000UL / frequency;
+ TA0CTL |= TACLR;
+ }
+#endif
+
+ inline void start(unsigned char const interrupt) {
+ if (interrupt) {
+ TA0CTL |= MC__UP | TACLR | TAIE;
+ } else {
+ TA0CTL |= MC__UP | TACLR;
+ }
+ }
+
+ inline void stop() {
+ TA0CTL &= ~MC__UP;
+ }
+};
+
+extern Timer timer;
diff --git a/include/arch/msp430fr5994lp/driver/uptime.h b/include/arch/msp430fr5994lp/driver/uptime.h
new file mode 100644
index 0000000..3a52840
--- /dev/null
+++ b/include/arch/msp430fr5994lp/driver/uptime.h
@@ -0,0 +1,30 @@
+#ifndef UPTIME_H
+#define UPTIME_H
+
+#include <msp430.h>
+#include <stdint.h>
+
+class Uptime {
+ private:
+ Uptime(const Uptime &copy);
+#ifdef TIMER_S
+ uint16_t seconds;
+#endif
+
+ public:
+#ifdef TIMER_S
+ Uptime () : seconds(0) {}
+#else
+ Uptime () {}
+#endif
+ inline uint16_t get_us() { return TA0R; }
+ inline uint16_t get_cycles() { return TA2R; }
+#ifdef TIMER_S
+ inline uint16_t get_s() { return seconds; }
+ inline void tick_s() { seconds++; }
+#endif
+};
+
+extern Uptime uptime;
+
+#endif