aboutsummaryrefslogtreecommitdiffstats
path: root/wirish
diff options
context:
space:
mode:
Diffstat (limited to 'wirish')
-rw-r--r--wirish/comm/HardwareSPI.cpp78
-rw-r--r--wirish/comm/HardwareSPI.h40
-rw-r--r--wirish/comm/HardwareSerial.cpp28
-rw-r--r--wirish/comm/HardwareSerial.h54
4 files changed, 100 insertions, 100 deletions
diff --git a/wirish/comm/HardwareSPI.cpp b/wirish/comm/HardwareSPI.cpp
index 5f42db7..5d04ddf 100644
--- a/wirish/comm/HardwareSPI.cpp
+++ b/wirish/comm/HardwareSPI.cpp
@@ -1,4 +1,4 @@
-/* *****************************************************************************
+/******************************************************************************
* The MIT License
*
* Copyright (c) 2010 Perry Hung.
@@ -20,7 +20,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
- * ****************************************************************************/
+ *****************************************************************************/
/**
* @brief HardwareSPI "wiring-like" api for SPI
@@ -52,14 +52,14 @@
#include "HardwareSPI.h"
static const uint32 prescaleFactors[MAX_SPI_FREQS] = {
- SPI_PRESCALE_2, // SPI_18MHZ
- SPI_PRESCALE_4, // SPI_9MHZ
- SPI_PRESCALE_8, // SPI_4_5MHZ
- SPI_PRESCALE_16, // SPI_2_25MHZ
- SPI_PRESCALE_32, // SPI_1_125MHZ
- SPI_PRESCALE_64, // SPI_562_500KHZ
- SPI_PRESCALE_128, // SPI_281_250KHZ
- SPI_PRESCALE_256, // SPI_140_625KHZ
+ SPI_PRESCALE_2, // SPI_18MHZ
+ SPI_PRESCALE_4, // SPI_9MHZ
+ SPI_PRESCALE_8, // SPI_4_5MHZ
+ SPI_PRESCALE_16, // SPI_2_25MHZ
+ SPI_PRESCALE_32, // SPI_1_125MHZ
+ SPI_PRESCALE_64, // SPI_562_500KHZ
+ SPI_PRESCALE_128, // SPI_281_250KHZ
+ SPI_PRESCALE_256, // SPI_140_625KHZ
};
/**
@@ -78,37 +78,38 @@ static const uint32 prescaleFactors[MAX_SPI_FREQS] = {
* @param mode SPI standard CPOL and CPHA levels
*/
void HardwareSPI::begin(SPIFrequency freq, uint32 endianness, uint32 mode) {
- uint32 spi_num = this->spi_num;
- uint32 prescale;
-
- if ((freq >= MAX_SPI_FREQS) ||
- !((endianness == LSBFIRST) ||
- (endianness == MSBFIRST)) ||
- (mode >= 4)) {
- return;
- }
-
- if (spi_num == 1) {
- /* SPI1 is too fast for 140625 */
- if (freq == SPI_140_625KHZ) {
- return;
- }
-
- /* Turn off PWM on shared pins */
- timer_set_mode(TIMER3, 2, TIMER_DISABLED);
- timer_set_mode(TIMER3, 1, TIMER_DISABLED);
- }
-
- endianness = (endianness == LSBFIRST) ? SPI_LSBFIRST : SPI_MSBFIRST;
- prescale = (spi_num == 1) ? prescaleFactors[freq + 1] : prescaleFactors[freq];
-
- spi_init(spi_num, prescale, endianness, 0);
+ uint32 spi_num = this->spi_num;
+ uint32 prescale;
+
+ if ((freq >= MAX_SPI_FREQS) ||
+ !((endianness == LSBFIRST) ||
+ (endianness == MSBFIRST)) ||
+ (mode >= 4)) {
+ return;
+ }
+
+ if (spi_num == 1) {
+ /* SPI1 is too fast for 140625 */
+ if (freq == SPI_140_625KHZ) {
+ return;
+ }
+
+ /* Turn off PWM on shared pins */
+ timer_set_mode(TIMER3, 2, TIMER_DISABLED);
+ timer_set_mode(TIMER3, 1, TIMER_DISABLED);
+ }
+
+ endianness = (endianness == LSBFIRST) ? SPI_LSBFIRST : SPI_MSBFIRST;
+ prescale = (spi_num == 1) ?
+ prescaleFactors[freq + 1] :
+ prescaleFactors[freq];
+
+ spi_init(spi_num, prescale, endianness, 0);
}
/**
- * @brief Initialize a SPI peripheral with a default speed of 1.125 MHZ, MSBFIRST,
- * mode 0
- * @param mode SPI standard CPOL and CPHA levels
+ * @brief Initialize a SPI peripheral with a default speed of 1.125
+ * MHZ, MSBFIRST, mode 0
*/
void HardwareSPI::begin(void) {
begin(SPI_1_125MHZ, MSBFIRST, 0);
@@ -126,7 +127,6 @@ uint8 HardwareSPI::send(uint8 *buf, uint32 len) {
return spi_tx(this->spi_num, buf, len);
}
-
/**
* @brief read a byte from the spi peripheral
* @return byte in the buffer
diff --git a/wirish/comm/HardwareSPI.h b/wirish/comm/HardwareSPI.h
index e606c0c..03d1ea1 100644
--- a/wirish/comm/HardwareSPI.h
+++ b/wirish/comm/HardwareSPI.h
@@ -1,4 +1,4 @@
-/* *****************************************************************************
+/******************************************************************************
* The MIT License
*
* Copyright (c) 2010 Perry Hung.
@@ -20,7 +20,7 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
- * ****************************************************************************/
+ *****************************************************************************/
/**
* @brief HardwareSPI definitions
@@ -30,28 +30,28 @@
#define _HARDWARESPI_H_
typedef enum SPIFrequency {
- SPI_18MHZ = 0,
- SPI_9MHZ = 1,
- SPI_4_5MHZ = 2,
- SPI_2_25MHZ = 3,
- SPI_1_125MHZ = 4,
- SPI_562_500KHZ = 5,
- SPI_281_250KHZ = 6,
- SPI_140_625KHZ = 7,
- MAX_SPI_FREQS = 8,
+ SPI_18MHZ = 0,
+ SPI_9MHZ = 1,
+ SPI_4_5MHZ = 2,
+ SPI_2_25MHZ = 3,
+ SPI_1_125MHZ = 4,
+ SPI_562_500KHZ = 5,
+ SPI_281_250KHZ = 6,
+ SPI_140_625KHZ = 7,
+ MAX_SPI_FREQS = 8,
} SPIFrequency;
class HardwareSPI {
- private:
- uint32 spi_num;
+ private:
+ uint32 spi_num;
- public:
- HardwareSPI(uint32 spi_num);
- void begin(void);
- void begin(SPIFrequency freq, uint32 endianness, uint32 mode);
- uint8 send(uint8 data);
- uint8 send(uint8 *data, uint32 length);
- uint8 recv(void);
+ public:
+ HardwareSPI(uint32 spi_num);
+ void begin(void);
+ void begin(SPIFrequency freq, uint32 endianness, uint32 mode);
+ uint8 send(uint8 data);
+ uint8 send(uint8 *data, uint32 length);
+ uint8 recv(void);
};
#endif
diff --git a/wirish/comm/HardwareSerial.cpp b/wirish/comm/HardwareSerial.cpp
index 902b160..425c610 100644
--- a/wirish/comm/HardwareSerial.cpp
+++ b/wirish/comm/HardwareSerial.cpp
@@ -1,4 +1,4 @@
-/* *****************************************************************************
+/******************************************************************************
* The MIT License
*
* Copyright (c) 2010 Perry Hung.
@@ -20,12 +20,12 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
- * ****************************************************************************/
+ *****************************************************************************/
/**
- * @file HardwareSerial.cpp
+ * @file HardwareSerial.cpp
*
- * @brief Wiring-like serial api
+ * @brief Wiring-like serial api
*/
#include "wirish.h"
@@ -68,18 +68,18 @@ void HardwareSerial::write(unsigned char ch) {
}
void HardwareSerial::begin(uint32 baud) {
- if (baud > max_baud) {
- return;
- }
+ if (baud > max_baud) {
+ return;
+ }
- gpio_set_mode(gpio_port, tx_pin, GPIO_MODE_AF_OUTPUT_PP);
- gpio_set_mode(gpio_port, rx_pin, GPIO_MODE_INPUT_FLOATING);
+ gpio_set_mode(gpio_port, tx_pin, GPIO_MODE_AF_OUTPUT_PP);
+ gpio_set_mode(gpio_port, rx_pin, GPIO_MODE_INPUT_FLOATING);
- if ((usart_num == USART1) ||
- (usart_num == USART2)) {
- /* turn off any pwm if there's a conflict on this usart */
- timer_set_mode(timer_num, compare_num, TIMER_DISABLED);
- }
+ if ((usart_num == USART1) ||
+ (usart_num == USART2)) {
+ /* turn off any pwm if there's a conflict on this usart */
+ timer_set_mode(timer_num, compare_num, TIMER_DISABLED);
+ }
usart_init(usart_num, baud);
}
diff --git a/wirish/comm/HardwareSerial.h b/wirish/comm/HardwareSerial.h
index df8d7bf..17be49f 100644
--- a/wirish/comm/HardwareSerial.h
+++ b/wirish/comm/HardwareSerial.h
@@ -1,4 +1,4 @@
-/* *****************************************************************************
+/******************************************************************************
* The MIT License
*
* Copyright (c) 2010 Perry Hung.
@@ -20,12 +20,12 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
- * ****************************************************************************/
+ *****************************************************************************/
/**
- * @file HardwareSerial.h
+ * @file HardwareSerial.h
*
- * @brief
+ * @brief Wirish interface to hardware serial communications.
*/
#ifndef _HARDWARESERIAL_H_
@@ -34,29 +34,29 @@
#include "Print.h"
class HardwareSerial : public Print {
- private:
- uint8 usart_num;
- uint32 max_baud;
- GPIO_Port *gpio_port;
- uint8 tx_pin;
- uint8 rx_pin;
- uint8 timer_num;
- uint8 compare_num;
- public:
- HardwareSerial(uint8 usart_num,
- uint32 max_baud,
- GPIO_Port *gpio_port,
- uint8 tx_pin,
- uint8 rx_pin,
- uint8 timer_num,
- uint8 compare_num);
- void begin(uint32);
- void end(void);
- uint32 available(void);
- uint8 read(void);
- void flush(void);
- virtual void write(unsigned char);
- using Print::write;
+ private:
+ uint8 usart_num;
+ uint32 max_baud;
+ GPIO_Port *gpio_port;
+ uint8 tx_pin;
+ uint8 rx_pin;
+ uint8 timer_num;
+ uint8 compare_num;
+ public:
+ HardwareSerial(uint8 usart_num,
+ uint32 max_baud,
+ GPIO_Port *gpio_port,
+ uint8 tx_pin,
+ uint8 rx_pin,
+ uint8 timer_num,
+ uint8 compare_num);
+ void begin(uint32);
+ void end(void);
+ uint32 available(void);
+ uint8 read(void);
+ void flush(void);
+ virtual void write(unsigned char);
+ using Print::write;
};
extern HardwareSerial Serial1;
extern HardwareSerial Serial2;