This commit is contained in:
Milanka Ringwald 2015-06-24 17:21:17 +02:00
commit 8e8debb67a
3 changed files with 78 additions and 31 deletions

View File

@ -4,8 +4,15 @@
#include <Arduino.h>
#include <SPI.h>
#ifdef __AVR__
#include <avr/wdt.h>
#endif
#if __arm__
#include <Reset.h> // also provides NVIC_SystemReset
#endif
#include "BTstack.h"
#include "btstack_memory.h"
@ -89,12 +96,29 @@ extern "C" int putchar(int c) {
return c;
}
#else
#ifdef __AVR__
static FILE uartout = {0} ;
static int uart_putchar (char c, FILE *stream) {
Serial.write(c);
return 0;
}
#endif
// added for Arduino Zero. Arduino Due already has tis own _write(..) implementation
// in /Users/mringwal/Library/Arduino15/packages/arduino/hardware/sam/1.6.4/cores/arduino/syscalls_sam3.c
#if defined(__SAMD21G18A__)
// #ifdef __arm__
extern "C" int _write(int file, char *ptr, int len){
int i;
for (i = 0; i < len; i++) {
if (ptr[i] == '\n') {
Serial.write((uint8_t)'\r');
}
Serial.write((uint8_t)ptr[i]);
}
return i;
}
#endif
#endif
// HAL CPU Implementation
extern "C" void hal_cpu_disable_irqs(void){ }
@ -173,8 +197,12 @@ static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *pack
}
static void gatt_client_callback(le_event_t * event){
// le_characteristic_t characteristic;
// le_characteristic_value_event_t * value_event;
// if (hci) event is not 4-byte aligned, event->handle causes crash
// workaround: check event type, assuming GATT event types are contagious
if (event->type < GATT_QUERY_COMPLETE) return;
if (event->type > GATT_MTU) return;
gatt_complete_event_t * gatt_complete_event;
BLEDevice device(event->handle);
@ -656,8 +684,14 @@ void BTstackManager::setPublicBdAddr(bd_addr_t addr){
void bluetooth_hardware_error(){
printf("Bluetooth Hardware Error event. Restarting...\n\n\n");
#ifdef __AVR__
wdt_enable(WDTO_15MS);
// wait for watchdog to trigger
#endif
#ifdef __arm__
NVIC_SystemReset();
#endif
while(1);
}

View File

@ -19,21 +19,30 @@
#ifdef ENERGIA
// CMM 9301 Configuration for TI Launchpad
#define PIN_SCK 7
#define PIN_SPI_SCK 7
#define PIN_CS 8
#define PIN_SHUTDOWN 11
#define PIN_IRQ_DATA 13
#define PIN_MISO 14
#define PIN_MOSI 15
#define PIN_SPI_MISO 14
#define PIN_SPI_MOSI 15
#else // ARDUINO
// CMM 9301 Configuration on Arduino
// CMM 9301 Configuration for Arduino
#define PIN_IRQ_DATA 2
#define PIN_CS 4
#define PIN_SHUTDOWN 5
#define PIN_MISO 50
#define PIN_MOSI 51
#define PIN_SCK 52
// -- SPI defines for Arduino Mega
#ifndef PIN_SPI_MISO
#define PIN_SPI_MISO 50
#endif
#ifndef PIN_SPI_MOSI
#define PIN_SPI_MOSI 51
#endif
#ifndef PIN_SPI_SCK
#define PIN_SPI_SCK 52
#endif
#endif
// rx state
@ -51,37 +60,40 @@ static void (*tx_done_handler)(void) = dummy_handler;
static void bt_setup(void){
pinMode(PIN_CS, OUTPUT);
pinMode(PIN_MOSI, OUTPUT);
pinMode(PIN_SCK, OUTPUT);
pinMode(PIN_SPI_MOSI, OUTPUT);
pinMode(PIN_SPI_SCK, OUTPUT);
pinMode(PIN_SHUTDOWN, OUTPUT);
pinMode(PIN_IRQ_DATA, INPUT);
digitalWrite(PIN_CS, HIGH);
digitalWrite(PIN_MOSI, LOW);
digitalWrite(PIN_SPI_MOSI, LOW);
digitalWrite(PIN_SHUTDOWN, HIGH);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
// SPI settings are reset in SPI.begin() - calls hang on Arduino Zero, too.
// SPI.setBitOrder(MSBFIRST);
// SPI.setDataMode(SPI_MODE0);
// SPI.end();
}
#ifdef HAVE_SHUTDOWN
static void bt_power_cycle(void){
// power cycle. set CPU outputs to input to not power EM9301 via IOs
// pinMode(PIN_MOSI, INPUT);
// pinMode(PIN_SPI_MOSI, INPUT);
// pinMode(PIN_CS, INPUT);
pinMode(PIN_CS, OUTPUT);
pinMode(PIN_MOSI, OUTPUT);
pinMode(PIN_SCK, OUTPUT);
pinMode(PIN_SPI_MOSI, OUTPUT);
pinMode(PIN_SPI_SCK, OUTPUT);
pinMode(PIN_SHUTDOWN, OUTPUT);
digitalWrite(PIN_CS, LOW);
digitalWrite(PIN_MOSI, LOW);
digitalWrite(PIN_SCK, LOW);
digitalWrite(PIN_SPI_MOSI, LOW);
digitalWrite(PIN_SPI_SCK, LOW);
digitalWrite(PIN_SHUTDOWN, HIGH);
delay(500);
pinMode(PIN_MOSI, OUTPUT);
pinMode(PIN_SPI_MOSI, OUTPUT);
pinMode(PIN_CS, OUTPUT);
digitalWrite(PIN_MOSI, LOW);
digitalWrite(PIN_SPI_MOSI, LOW);
digitalWrite(PIN_CS, HIGH);
digitalWrite(PIN_SHUTDOWN, LOW);
delay(1000);
@ -90,7 +102,7 @@ static void bt_power_cycle(void){
#ifndef HAVE_SHUTDOWN
static void bt_send_illegal(void){
digitalWrite(PIN_MOSI, HIGH);
digitalWrite(PIN_SPI_MOSI, HIGH);
digitalWrite(PIN_CS, LOW);
printf("Illegal start\n");
SPI.begin();
@ -105,7 +117,7 @@ static void bt_send_illegal(void){
}
static void bt_flush_input(void){
digitalWrite(PIN_MOSI, LOW);
digitalWrite(PIN_SPI_MOSI, LOW);
digitalWrite(PIN_CS, LOW);
SPI.begin();
while (digitalRead(PIN_IRQ_DATA) == HIGH){
@ -116,7 +128,7 @@ static void bt_flush_input(void){
}
static void bt_send_reset(void){
digitalWrite(PIN_MOSI, HIGH);
digitalWrite(PIN_SPI_MOSI, HIGH);
digitalWrite(PIN_CS, LOW);
SPI.begin();
SPI.transfer(0x01);
@ -134,13 +146,13 @@ static void bt_try_send(void){
if (!bytes_to_write) return;
// activate module
pinMode(PIN_MOSI, OUTPUT);
digitalWrite(PIN_MOSI, HIGH);
pinMode(PIN_SPI_MOSI, OUTPUT);
digitalWrite(PIN_SPI_MOSI, HIGH);
digitalWrite(PIN_CS, LOW);
// module ready
int tx_done = 0;
if (digitalRead(PIN_MISO) == HIGH){
if (digitalRead(PIN_SPI_MISO) == HIGH){
// printf("Sending: ");
SPI.begin();
@ -157,8 +169,8 @@ static void bt_try_send(void){
// deactivate module
digitalWrite(PIN_CS, HIGH);
digitalWrite(PIN_MOSI, LOW);
pinMode(PIN_MOSI, OUTPUT);
digitalWrite(PIN_SPI_MOSI, LOW);
pinMode(PIN_SPI_MOSI, OUTPUT);
// notify upper layer
if (tx_done) {
@ -177,7 +189,7 @@ static int bt_try_read(void){
// printf("Reading (%u): ", bytes_to_read);
// activate module
digitalWrite(PIN_MOSI, LOW);
digitalWrite(PIN_SPI_MOSI, LOW);
digitalWrite(PIN_CS, LOW);
SPI.begin();
do {

View File

@ -11,6 +11,7 @@ In the second part, we'll explain the examples to get you starting with creating
It should work with any board that provides at least 64 kB Flash and a working SPI driver.
So far, we did test the BTstack LE Arduino Shield together with these Arduino boards:
- Arduino Zero
- Arduino Mega 2560 R3
- Arduino Mega 2560
- Arduino Mega 1280