YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
spi_msp432.h
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14// SPI driver for MSP432. It support master
15// and client mode. Interrupts are supported
16// for client mode (RX interrupts).
17//
18
19#ifndef _SPI_MSP432_H_
20#define _SPI_MSP432_H_
21#include "spi_interface.h"
22#include "gpio_msp432.h"
23#include "msp.h"
24
25extern "C" {
26void EUSCIA0_SPI_IRQHandler(void);
27void EUSCIA1_SPI_IRQHandler(void);
28void EUSCIA2_SPI_IRQHandler(void);
29void EUSCIA3_SPI_IRQHandler(void);
30void EUSCIB0_SPI_IRQHandler(void);
31void EUSCIB1_SPI_IRQHandler(void);
32void EUSCIB2_SPI_IRQHandler(void);
33void EUSCIB3_SPI_IRQHandler(void);
34}
35
36namespace SPI {
37const uint16_t CPHA_0 = 0x8000;
38const uint16_t CPHA_1 = 0x0000;
39const uint16_t CPOL_0 = 0x0000;
40const uint16_t CPOL_1 = 0x4000;
41const uint16_t LSB_FIRST = 0x0000;
42const uint16_t MSB_FIRST = 0x2000;
43const uint16_t _8_BIT = 0x0000;
44const uint16_t _7_BIT = 0x1000;
45const uint16_t CLK_ACLK = 0x0040;
46const uint16_t CLK_SMCLK = 0x0080;
47
48// Verbose constants for SPI role
49const bool MASTER = true;
50const bool CLIENT = false;
51}
52
53class spi_msp432 : public spi_interface {
54
55public:
56
58 const bool spi_master = SPI::MASTER,
59 uint16_t mode = SPI::CPOL_0 | SPI::CPHA_0 | SPI::MSB_FIRST |
60 SPI::_8_BIT | SPI::CLK_SMCLK);
61
63 const bool spi_master = SPI::MASTER,
64 uint16_t mode = SPI::CPOL_0 | SPI::CPHA_0 | SPI::MSB_FIRST |
65 SPI::_8_BIT | SPI::CLK_SMCLK);
66
68
69 int16_t spiTxRx(const uint8_t *txbuf, uint8_t *rxbuf, uint16_t len) override;
70 int16_t spiTx (const uint8_t *txbuf, uint16_t len) override ;
71 int16_t spiRx (uint8_t txbyte, uint8_t *rxbuf, uint16_t len) override;
72
73 void setSpeed(uint32_t) override;
74 void generateCS(bool val) override;
75 void setCS(bool val) override;
76
77 void spiAttachRxIrq(function<void(uint8_t data)> f) override;
78
79 // IRQ handlers are our best friends
81 friend void EUSCIA0_SPI_IRQHandler(void);
82 friend void EUSCIA1_SPI_IRQHandler(void);
83 friend void EUSCIA2_SPI_IRQHandler(void);
84 friend void EUSCIA3_SPI_IRQHandler(void);
85 friend void EUSCIB0_SPI_IRQHandler(void);
86 friend void EUSCIB1_SPI_IRQHandler(void);
87 friend void EUSCIB2_SPI_IRQHandler(void);
88 friend void EUSCIB3_SPI_IRQHandler(void);
89
90private:
91
92 bool _initialized;
93 void initialize();
94
95 bool _master;
96 bool _generate_CS;
97
98 volatile uint16_t & _EUSCI_CTLW0;
99 volatile uint16_t & _EUSCI_BRW;
100 volatile uint16_t & _EUSCI_STATW;
101 volatile uint16_t & _EUSCI_RXBUF;
102 volatile uint16_t & _EUSCI_TXBUF;
103 volatile uint16_t & _EUSCI_IE;
104 volatile uint16_t & _EUSCI_IFG;
105 volatile uint16_t & _EUSCI_IV;
106
107 gpio_msp432 _clk;
108 gpio_msp432 _miso;
109 gpio_msp432 _mosi;
110
111 uint16_t _mode;
112 gpio_interface & _cs; // pointer to currently selected CS pin
113 IRQn_Type _irq;
114
115 static function<void(uint8_t)> _intHandler[8];
116};
117
118#endif // _SPI_MSP432_H_