Merge pull request #277 from bcostm/master

[NUCLEO_F401RE] Add LSE configuration for RTC, remove trailing spaces, t...
pull/279/head
Bogdan Marinescu 2014-04-28 15:55:37 +01:00
commit d57a23dd6e
22 changed files with 458 additions and 395 deletions

View File

@ -27,7 +27,8 @@
; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LR_IROM1 0x08000000 0x80000 { ; load region size_region (512K)
; STM32F401RE: 512KB FLASH + 96KB SRAM
LR_IROM1 0x08000000 0x80000 { ; load region size_region
ER_IROM1 0x08000000 0x80000 { ; load address = execution address
*.o (RESET, +First)

View File

@ -27,7 +27,8 @@
; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LR_IROM1 0x08000000 0x80000 { ; load region size_region (512K)
; STM32F401RE: 512KB FLASH + 96KB SRAM
LR_IROM1 0x08000000 0x80000 { ; load region size_region
ER_IROM1 0x08000000 0x80000 { ; load address = execution address
*.o (RESET, +First)

View File

@ -196,6 +196,10 @@ typedef enum
* @}
*/
#if defined (USE_HAL_DRIVER)
#include "stm32f4xx_hal.h"
#endif /* USE_HAL_DRIVER */
#ifdef __cplusplus
}

View File

@ -103,7 +103,7 @@
#endif /* HSE_VALUE */
#if !defined (HSE_STARTUP_TIMEOUT)
#define HSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSE start up, in ms */
#define HSE_STARTUP_TIMEOUT ((uint32_t)50) /*!< Time out for HSE start up, in ms */
#endif /* HSE_STARTUP_TIMEOUT */
/**
@ -115,6 +115,14 @@
#define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
#endif /* HSI_VALUE */
/**
* @brief External Low Speed oscillator (LSE) value.
* This value is used by the UART, RTC HAL module to compute the system frequency
*/
#if !defined (LSE_VALUE)
#define LSE_VALUE ((uint32_t)32768) /*!< Value of the External oscillator in Hz*/
#endif /* LSE_VALUE */
/**
* @brief External clock source for I2S peripheral
* This value is used by the I2S HAL module to compute the I2S clock source
@ -132,6 +140,7 @@
* @brief This is the HAL system configuration section
*/
#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */
#define TICK_INT_PRIORITY ((uint32_t)3) /*!< tick interrupt priority */
#define USE_RTOS 0
#define PREFETCH_ENABLE 1
#define INSTRUCTION_CACHE_ENABLE 1

View File

@ -190,7 +190,7 @@ typedef struct
#define DBP_TIMEOUT_VALUE ((uint32_t)100)
#define LSE_TIMEOUT_VALUE ((uint32_t)5000)
#define LSE_TIMEOUT_VALUE ((uint32_t)100)
/**
* @}
*/

View File

@ -26,14 +26,13 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "analogin_api.h"
#include "wait_api.h"
#if DEVICE_ANALOGIN
#include "wait_api.h"
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
#include "stm32f4xx_hal.h"
static const PinMap PinMap_ADC[] = {
{PA_0, ADC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // ADC1_IN0
@ -60,7 +59,7 @@ ADC_HandleTypeDef AdcHandle;
int adc_inited = 0;
void analogin_init(analogin_t *obj, PinName pin) {
// Get the peripheral name (ADC_1, ADC_2...) from the pin and assign it to the object
// Get the peripheral name from the pin and assign it to the object
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
if (obj->adc == (ADCName)NC) {
@ -165,14 +164,10 @@ static inline uint16_t adc_read(analogin_t *obj) {
HAL_ADC_Start(&AdcHandle); // Start conversion
HAL_ADC_PollForConversion(&AdcHandle, 10); // Wait end of conversion
if (HAL_ADC_GetState(&AdcHandle) == HAL_ADC_STATE_EOC_REG)
{
return(HAL_ADC_GetValue(&AdcHandle)); // Get conversion value
}
else
{
// Wait end of conversion and get value
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
return (HAL_ADC_GetValue(&AdcHandle));
} else {
return 0;
}
}

View File

@ -30,7 +30,6 @@
#include "gpio_api.h"
#include "pinmap.h"
#include "error.h"
#include "stm32f4xx_hal.h"
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
@ -66,8 +65,7 @@ void gpio_mode(gpio_t *obj, PinMode mode) {
void gpio_dir(gpio_t *obj, PinDirection direction) {
if (direction == PIN_OUTPUT) {
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
}
else { // PIN_INPUT
} else { // PIN_INPUT
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
}
}

View File

@ -29,10 +29,8 @@
*/
#include <stddef.h>
#include "cmsis.h"
#include "gpio_irq_api.h"
#include "pinmap.h"
#include "stm32f4xx_hal.h"
#include "error.h"
#define EDGE_NONE (0)
@ -54,8 +52,7 @@ static void handle_interrupt_in(uint32_t irq_index) {
uint32_t pin = (uint32_t)(1 << channel_pin[irq_index]);
// Clear interrupt flag
if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET)
{
if (__HAL_GPIO_EXTI_GET_FLAG(pin) != RESET) {
__HAL_GPIO_EXTI_CLEAR_FLAG(pin);
}
@ -64,20 +61,40 @@ static void handle_interrupt_in(uint32_t irq_index) {
// Check which edge has generated the irq
if ((gpio->IDR & pin) == 0) {
irq_handler(channel_ids[irq_index], IRQ_FALL);
}
else {
} else {
irq_handler(channel_ids[irq_index], IRQ_RISE);
}
}
// The irq_index is passed to the function
static void gpio_irq0(void) {handle_interrupt_in(0);} // EXTI line 0
static void gpio_irq1(void) {handle_interrupt_in(1);} // EXTI line 1
static void gpio_irq2(void) {handle_interrupt_in(2);} // EXTI line 2
static void gpio_irq3(void) {handle_interrupt_in(3);} // EXTI line 3
static void gpio_irq4(void) {handle_interrupt_in(4);} // EXTI line 4
static void gpio_irq5(void) {handle_interrupt_in(5);} // EXTI lines 5 to 9
static void gpio_irq6(void) {handle_interrupt_in(6);} // EXTI lines 10 to 15
// EXTI line 0
static void gpio_irq0(void) {
handle_interrupt_in(0);
}
// EXTI line 1
static void gpio_irq1(void) {
handle_interrupt_in(1);
}
// EXTI line 2
static void gpio_irq2(void) {
handle_interrupt_in(2);
}
// EXTI line 3
static void gpio_irq3(void) {
handle_interrupt_in(3);
}
// EXTI line 4
static void gpio_irq4(void) {
handle_interrupt_in(4);
}
// EXTI lines 5 to 9
static void gpio_irq5(void) {
handle_interrupt_in(5);
}
// EXTI lines 10 to 15
static void gpio_irq6(void) {
handle_interrupt_in(6);
}
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
@ -138,7 +155,7 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
irq_index = 6;
break;
default:
error("InterruptIn error: pin not supported.");
error("InterruptIn error: pin not supported.\n");
return -1;
}
@ -187,8 +204,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
mode = STM_MODE_IT_RISING_FALLING;
obj->event = EDGE_BOTH;
}
else { // NONE or RISE
} else { // NONE or RISE
mode = STM_MODE_IT_RISING;
obj->event = EDGE_RISE;
}
@ -198,14 +214,12 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
mode = STM_MODE_IT_RISING_FALLING;
obj->event = EDGE_BOTH;
}
else { // NONE or FALL
} else { // NONE or FALL
mode = STM_MODE_IT_FALLING;
obj->event = EDGE_FALL;
}
}
}
else {
} else {
mode = STM_MODE_INPUT;
pull = GPIO_NOPULL;
obj->event = EDGE_NONE;

View File

@ -50,8 +50,7 @@ typedef struct {
static inline void gpio_write(gpio_t *obj, int value) {
if (value) {
*obj->reg_set = obj->mask;
}
else {
} else {
*obj->reg_clr = obj->mask;
}
}

View File

@ -34,7 +34,6 @@
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
#include "stm32f4xx_hal.h"
/* Timeout values for flags and events waiting loops. These timeouts are
not based on accurate values, they just guarantee that the application will

View File

@ -0,0 +1,34 @@
/* mbed Microcontroller Library
* Copyright (c) 2014, STMicroelectronics
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cmsis.h"
// This function is called after RAM initialization and before main.
void mbed_sdk_init() {
// Update the SystemCoreClock variable.
SystemCoreClockUpdate();
}

View File

@ -30,7 +30,6 @@
#include "pinmap.h"
#include "PortNames.h"
#include "error.h"
#include "stm32f4xx_hal.h"
// GPIO mode look-up table
static const uint32_t gpio_mode[12] = {

View File

@ -31,7 +31,6 @@
#include "pinmap.h"
#include "gpio_api.h"
#include "error.h"
#include "stm32f4xx_hal.h"
#if DEVICE_PORTIN || DEVICE_PORTOUT
@ -67,8 +66,7 @@ void port_dir(port_t *obj, PinDirection dir) {
if (obj->mask & (1 << i)) { // If the pin is used
if (dir == PIN_OUTPUT) {
pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
}
else { // PIN_INPUT
} else { // PIN_INPUT
pin_function(port_pin(obj->port, i), STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
}
}
@ -91,8 +89,7 @@ void port_write(port_t *obj, int value) {
int port_read(port_t *obj) {
if (obj->direction == PIN_OUTPUT) {
return (*obj->reg_out & obj->mask);
}
else { // PIN_INPUT
} else { // PIN_INPUT
return (*obj->reg_in & obj->mask);
}
}

View File

@ -29,10 +29,11 @@
*/
#include "pwmout_api.h"
#if DEVICE_PWMOUT
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
#include "stm32f4xx_hal.h"
// TIM5 cannot be used because already used by the us_ticker
static const PinMap PinMap_PWM[] = {
@ -112,10 +113,7 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
}
void pwmout_free(pwmout_t* obj) {
TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
HAL_TIM_PWM_DeInit(&TimHandle);
// Configure GPIO
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
}
@ -268,3 +266,5 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
float value = (float)us / (float)obj->period;
pwmout_write(obj, value);
}
#endif

View File

@ -28,7 +28,9 @@
*******************************************************************************
*/
#include "rtc_api.h"
#include "stm32f4xx_hal.h"
#if DEVICE_RTC
#include "error.h"
static int rtc_inited = 0;
@ -36,6 +38,9 @@ static int rtc_inited = 0;
static RTC_HandleTypeDef RtcHandle;
void rtc_init(void) {
RCC_OscInitTypeDef RCC_OscInitStruct;
uint32_t rtc_freq = 0;
if (rtc_inited) return;
rtc_inited = 1;
@ -44,39 +49,49 @@ void rtc_init(void) {
// Enable Power clock
__PWR_CLK_ENABLE();
// Allow access to RTC
// Enable access to Backup domain
HAL_PWR_EnableBkUpAccess();
// Reset Backup domain
__HAL_RCC_BACKUPRESET_FORCE();
__HAL_RCC_BACKUPRESET_RELEASE();
// Enable LSE Oscillator
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; /* Mandatory, otherwise the PLL is reconfigured! */
RCC_OscInitStruct.LSEState = RCC_LSE_ON; /* External 32.768 kHz clock on OSC_IN/OSC_OUT */
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK)
{
// Connect LSE to RTC
__HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
rtc_freq = LSE_VALUE;
} else {
// Enable LSI clock
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; // Mandatory, otherwise the PLL is reconfigured!
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
error("RTC error: LSI clock initialization failed.");
}
// Connect LSI to RTC
__HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSI);
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
// Enable RTC clock
__HAL_RCC_RTC_ENABLE();
// [TODO] This value is LSI typical value. To be measured precisely using a timer input capture
uint32_t lsi_freq = 32000;
rtc_freq = 32000;
}
// Enable RTC
__HAL_RCC_RTC_ENABLE();
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
RtcHandle.Init.AsynchPrediv = 127;
RtcHandle.Init.SynchPrediv = (lsi_freq / 128) - 1;
RtcHandle.Init.SynchPrediv = (rtc_freq / 128) - 1;
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if (HAL_RTC_Init(&RtcHandle) != HAL_OK) {
error("RTC error: RTC initialization failed.");
}
@ -86,18 +101,22 @@ void rtc_free(void) {
// Enable Power clock
__PWR_CLK_ENABLE();
// Allow access to RTC
// Enable access to Backup domain
HAL_PWR_EnableBkUpAccess();
// Reset Backup domain
__HAL_RCC_BACKUPRESET_FORCE();
__HAL_RCC_BACKUPRESET_RELEASE();
// Disable LSI clock
// Disable access to Backup domain
HAL_PWR_DisableBkUpAccess();
// Disable LSI and LSE clocks
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
rtc_inited = 0;
@ -176,3 +195,5 @@ void rtc_write(time_t t) {
HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN);
}
#endif

View File

@ -28,11 +28,13 @@
*******************************************************************************
*/
#include "serial_api.h"
#if DEVICE_SERIAL
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
#include <string.h>
#include "stm32f4xx_hal.h"
static const PinMap PinMap_UART_TX[] = {
{PA_2, UART_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF7_USART2)},
@ -54,7 +56,7 @@ static const PinMap PinMap_UART_RX[] = {
#define UART_NUM (3)
static uint32_t serial_irq_ids[UART_NUM] = {0};
static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0};
static uart_irq_handler irq_handler;
@ -138,8 +140,7 @@ void serial_baud(serial_t *obj, int baudrate) {
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
if (data_bits == 8) {
obj->databits = UART_WORDLENGTH_8B;
}
else {
} else {
obj->databits = UART_WORDLENGTH_9B;
}
@ -159,8 +160,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
if (stop_bits == 2) {
obj->stopbits = UART_STOPBITS_2;
}
else {
} else {
obj->stopbits = UART_STOPBITS_1;
}
@ -171,10 +171,8 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
* INTERRUPTS HANDLING
******************************************************************************/
// Not part of mbed api
static void uart_irq(UARTName name, int id) {
UartHandle.Instance = (USART_TypeDef *)name;
if (serial_irq_ids[id] != 0) {
if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TC) != RESET) {
irq_handler(serial_irq_ids[id], TxIrq);
@ -187,10 +185,15 @@ static void uart_irq(UARTName name, int id) {
}
}
// Not part of mbed api
static void uart1_irq(void) {uart_irq(UART_1, 0);}
static void uart2_irq(void) {uart_irq(UART_2, 1);}
static void uart6_irq(void) {uart_irq(UART_6, 2);}
static void uart1_irq(void) {
uart_irq(UART_1, 0);
}
static void uart2_irq(void) {
uart_irq(UART_2, 1);
}
static void uart6_irq(void) {
uart_irq(UART_6, 2);
}
void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
irq_handler = handler;
@ -222,8 +225,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
if (irq == RxIrq) {
__HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE);
}
else { // TxIrq
} else { // TxIrq
__HAL_UART_ENABLE_IT(&UartHandle, UART_IT_TC);
}
@ -238,8 +240,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
__HAL_UART_DISABLE_IT(&UartHandle, UART_IT_RXNE);
// Check if TxIrq is disabled too
if ((UartHandle.Instance->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1;
}
else { // TxIrq
} else { // TxIrq
__HAL_UART_DISABLE_IT(&UartHandle, UART_IT_TXE);
// Check if RxIrq is disabled too
if ((UartHandle.Instance->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
@ -299,3 +300,5 @@ void serial_break_set(serial_t *obj) {
void serial_break_clear(serial_t *obj) {
}
#endif

View File

@ -28,24 +28,24 @@
*******************************************************************************
*/
#include "sleep_api.h"
#include "cmsis.h"
#include "stm32f4xx_hal.h"
// This function is in the system_stm32f4xx.c file
extern void SystemClock_Config(void);
#if DEVICE_SLEEP
#include "cmsis.h"
static TIM_HandleTypeDef TimMasterHandle;
void sleep(void)
{
void sleep(void) {
// Request to enter SLEEP mode
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
}
void deepsleep(void)
{
void deepsleep(void) {
// Request to enter STOP mode with regulator in low power mode
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
// After wake-up from STOP reconfigure the PLL
SystemClock_Config();
SetSysClock();
}
#endif

View File

@ -35,7 +35,6 @@
#include "cmsis.h"
#include "pinmap.h"
#include "error.h"
#include "stm32f4xx_hal.h"
static const PinMap PinMap_SPI_MOSI[] = {
{PA_7, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
@ -142,8 +141,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
if (ssel == NC) { // SW NSS Master mode
obj->mode = SPI_MODE_MASTER;
obj->nss = SPI_NSS_SOFT;
}
else { // Slave
} else { // Slave
pinmap_pinout(ssel, PinMap_SPI_SSEL);
obj->mode = SPI_MODE_SLAVE;
obj->nss = SPI_NSS_HARD_INPUT;
@ -161,8 +159,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
// Save new values
if (bits == 8) {
obj->bits = SPI_DATASIZE_8BIT;
}
else {
} else {
obj->bits = SPI_DATASIZE_16BIT;
}
@ -188,8 +185,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
if (slave == 0) {
obj->mode = SPI_MODE_MASTER;
obj->nss = SPI_NSS_SOFT;
}
else {
} else {
obj->mode = SPI_MODE_SLAVE;
obj->nss = SPI_NSS_HARD_INPUT;
}
@ -201,26 +197,19 @@ void spi_frequency(spi_t *obj, int hz) {
// Note: The frequencies are obtained with SPI1 clock = 84 MHz (APB2 clock)
if (hz < 600000) {
obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 330 kHz
}
else if ((hz >= 600000) && (hz < 1000000)) {
} else if ((hz >= 600000) && (hz < 1000000)) {
obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 656 kHz
}
else if ((hz >= 1000000) && (hz < 2000000)) {
} else if ((hz >= 1000000) && (hz < 2000000)) {
obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1.3 MHz
}
else if ((hz >= 2000000) && (hz < 5000000)) {
} else if ((hz >= 2000000) && (hz < 5000000)) {
obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 2.6 MHz
}
else if ((hz >= 5000000) && (hz < 10000000)) {
} else if ((hz >= 5000000) && (hz < 10000000)) {
obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 5.25 MHz
}
else if ((hz >= 10000000) && (hz < 21000000)) {
} else if ((hz >= 10000000) && (hz < 21000000)) {
obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 10.5 MHz
}
else if ((hz >= 21000000) && (hz < 42000000)) {
} else if ((hz >= 21000000) && (hz < 42000000)) {
obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 21 MHz
}
else { // >= 42000000
} else { // >= 42000000
obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 42 MHz
}
init_spi(obj);
@ -267,11 +256,12 @@ int spi_master_write(spi_t *obj, int value) {
}
int spi_slave_receive(spi_t *obj) {
return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0);
return (!ssp_busy(obj)) ? (1) : (0);
};
int spi_slave_read(spi_t *obj) {
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
while (!ssp_readable(obj));
return (int)spi->DR;
}

View File

@ -28,7 +28,6 @@
#include <stddef.h>
#include "us_ticker_api.h"
#include "PeripheralNames.h"
#include "stm32f4xx_hal.h"
// 32-bit timer selection
#define TIM_MST TIM5