Initial check-in for STM32 board

This commit is contained in:
Ben Lye
2017-11-27 21:19:49 +00:00
parent 9bf5b0c9a7
commit e557155b17
893 changed files with 106516 additions and 34 deletions

View File

@@ -0,0 +1,83 @@
/******************************************************************************
* The MIT License
*
* Copyright (c) 2010 Perry Hung (from libmaple/util.c).
* Copyright (c) 2012 LeafLabs, LLC.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 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.
*****************************************************************************/
/*
* STM32F1 implementations for libmaple/util.c hooks
*
* These need more love and attention before being made public API
* (this includes being easily overridable by user code).
*/
#include <libmaple/nvic.h>
#include <libmaple/gpio.h>
#include <libmaple/stm32.h>
#include <libmaple/timer.h>
#include <libmaple/adc.h>
#include <libmaple/usart.h>
/* Failed ASSERT()s send out a message using this USART config. */
#ifndef ERROR_USART
#define ERROR_USART USART2
#define ERROR_USART_BAUD 9600
#define ERROR_TX_PORT GPIOA
#define ERROR_TX_PIN 2
#endif
/*
* Disables all peripheral interrupts except USB (when available),
* turns off commonly-used peripherals. Called by __error() with
* global interrupts disabled.
*/
void __lm_error(void) {
/* Turn off peripheral interrupts */
nvic_irq_disable_all();
/* Turn off timers */
timer_disable_all();
/* Turn off ADC */
adc_disable_all();
/* Turn off all USARTs */
usart_disable_all();
#if STM32_HAVE_USB
/* Turn the USB interrupt back on so the bootloader keeps on functioning */
nvic_irq_enable(NVIC_USB_HP_CAN_TX);
nvic_irq_enable(NVIC_USB_LP_CAN_RX0);
#endif
}
/*
* Enable the error USART for writing.
*/
usart_dev* __lm_enable_error_usart() {
gpio_set_mode(ERROR_TX_PORT, ERROR_TX_PIN, GPIO_AF_OUTPUT_PP);
usart_init(ERROR_USART);
usart_set_baud_rate(ERROR_USART, USART_USE_PCLK, ERROR_USART_BAUD);
return ERROR_USART;
}

View File

@@ -0,0 +1,75 @@
#include <wiring_pulse.h>
#include "boards.h"
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
* to 3 minutes in length, but must be called at least a few dozen microseconds
* before the start of the pulse. */
/*
* Roger Clark
*
* Note. The API spec for this function published on http://www.arduino.cc/en/Reference/PulseIn
* doesn't reflect what either the AVR or SAM version of this function actualy do with regard to the timeout value
*
* "timeout (optional): the number of microseconds to wait for the pulse to start; default is one second (unsigned long) "
*
* Because the timeout, is actually coded as the total time to both wait while the input is in the state requested
* then wait for the opposite state duration
* then count the length of the pulse when it has the value of state (HIGH or LOW)
*
* So I think the code for both the AVR and the Due is wrong in that it doesnt match the spec
*
* I have done basically the same as the AVR and Due code, except to make the timeout a bit more accurate I have put in a dummy volatile varable
* dummyWidth so that both the waiting while loops take the same number of clock cycles to execute as the acount width counting loop
*
* to be slighly more accurate the maxLoops variable really needs to take into account the loop setup code, but its probably as good as necessary
*
*/
uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout )
{
// cache the port and bit of the pin in order to speed up the
// pulse width measuring loop and achieve finer resolution. calling
// digitalRead() instead yields much coarser resolution.
gpio_dev *dev=PIN_MAP[pin].gpio_device;
uint32_t bit = (1U << PIN_MAP[pin].gpio_bit);
uint32_t width = 0; // keep initialization out of time critical area
// convert the timeout from microseconds to a number of times through
// the initial loop; it takes 16 clock cycles per iteration.
uint32_t numloops = 0;
uint32_t maxloops = timeout * ( F_CPU / 16000000);
volatile uint32_t dummyWidth=0;
// wait for any previous pulse to end
while ( (dev->regs->IDR & bit) == bit) {
if (numloops++ == maxloops) {
return 0;
}
dummyWidth++;
}
// wait for the pulse to start
while ((dev->regs->IDR & bit) != bit) {
if (numloops++ == maxloops) {
return 0;
}
dummyWidth++;
}
// wait for the pulse to stop
while ((dev->regs->IDR & bit) == bit) {
if (numloops++ == maxloops) {
return 0;
}
width++;
}
// Excluding time taking up by the interrupts, it needs 16 clock cycles to look through the last while loop
// 5 is added as a fiddle factor to correct for interrupts etc. But ultimately this would only be accurate if it was done ona hardware timer
return (uint32_t)( ( (unsigned long long)(width+5) * (unsigned long long) 16000000.0) /(unsigned long long)F_CPU ) ;
}

View File

@@ -0,0 +1,41 @@
/******************************************************************************
* The MIT License
*
* Copyright (c) 2011, 2012 LeafLabs, LLC.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 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 wirish/stm32f1/wirish_debug.cpp
* @brief High level debug port configuration
*/
#include <wirish_debug.h>
#include <libmaple/gpio.h>
void disableDebugPorts(void) {
afio_cfg_debug_ports(AFIO_DEBUG_NONE);
}
void enableDebugPorts(void) {
afio_cfg_debug_ports(AFIO_DEBUG_FULL_SWJ);
}

View File

@@ -0,0 +1,92 @@
/******************************************************************************
* The MIT License
*
* Copyright (c) 2010 Perry Hung.
* Copyright (c) 2012 LeafLabs, LLC.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 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.
*****************************************************************************/
/*
* STM32F1 implementations for basic GPIO functionality.
*/
#include <io.h>
#include <libmaple/gpio.h>
#include <libmaple/timer.h>
#include <boards.h>
void pinMode(uint8 pin, WiringPinMode mode) {
gpio_pin_mode outputMode;
bool pwm = false;
if (pin >= BOARD_NR_GPIO_PINS) {
return;
}
switch(mode) {
case OUTPUT:
outputMode = GPIO_OUTPUT_PP;
break;
case OUTPUT_OPEN_DRAIN:
outputMode = GPIO_OUTPUT_OD;
break;
case INPUT:
case INPUT_FLOATING:
outputMode = GPIO_INPUT_FLOATING;
break;
case INPUT_ANALOG:
outputMode = GPIO_INPUT_ANALOG;
break;
case INPUT_PULLUP:
outputMode = GPIO_INPUT_PU;
break;
case INPUT_PULLDOWN:
outputMode = GPIO_INPUT_PD;
break;
case PWM:
outputMode = GPIO_AF_OUTPUT_PP;
pwm = true;
break;
case PWM_OPEN_DRAIN:
outputMode = GPIO_AF_OUTPUT_OD;
pwm = true;
break;
default:
ASSERT(0);
return;
}
gpio_set_mode(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, outputMode);
if (PIN_MAP[pin].timer_device != NULL) {
if ( pwm ) { // we're switching into PWM, enable timer channels
timer_set_mode(PIN_MAP[pin].timer_device,
PIN_MAP[pin].timer_channel,
TIMER_PWM );
} else { // disable channel output in non pwm-Mode
timer_cc_disable(PIN_MAP[pin].timer_device,
PIN_MAP[pin].timer_channel);
}
}
}