[NUCLEO_L152RE] Add LSE configuration for RTC and...

... remove trailing spaces, typo corrections
pull/281/head
bcostm 2014-04-29 10:12:59 +02:00
parent af534d61c0
commit 7af317dda2
18 changed files with 437 additions and 407 deletions

View File

@ -94,6 +94,8 @@ extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Cloc
extern void SystemInit(void); extern void SystemInit(void);
extern void SystemCoreClockUpdate(void); extern void SystemCoreClockUpdate(void);
extern void SetSysClock(void);
/** /**
* @} * @}
*/ */

View File

@ -26,13 +26,13 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "analogin_api.h" #include "analogin_api.h"
#include "wait_api.h"
#if DEVICE_ANALOGIN #if DEVICE_ANALOGIN
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
#include "wait_api.h"
static const PinMap PinMap_ADC[] = { static const PinMap PinMap_ADC[] = {
{PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN0 {PA_0, ADC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // ADC_IN0

View File

@ -113,8 +113,7 @@ void analogout_write(dac_t *obj, float value) {
void analogout_write_u16(dac_t *obj, uint16_t value) { void analogout_write_u16(dac_t *obj, uint16_t value) {
if (value > (uint16_t)RANGE_12BIT) { if (value > (uint16_t)RANGE_12BIT) {
dac_write(obj, (uint16_t)RANGE_12BIT); // Max value dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
} } else {
else {
dac_write(obj, value); dac_write(obj, value);
} }
} }

View File

@ -65,8 +65,7 @@ void gpio_mode(gpio_t *obj, PinMode mode) {
void gpio_dir(gpio_t *obj, PinDirection direction) { void gpio_dir(gpio_t *obj, PinDirection direction) {
if (direction == PIN_OUTPUT) { if (direction == PIN_OUTPUT) {
pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_OUT, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_OUT, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF));
} } else { // PIN_INPUT
else { // PIN_INPUT
pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
} }
} }

View File

@ -29,7 +29,6 @@
*/ */
#include <stddef.h> #include <stddef.h>
#include "cmsis.h" #include "cmsis.h"
#include "gpio_irq_api.h" #include "gpio_irq_api.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
@ -53,8 +52,7 @@ static void handle_interrupt_in(uint32_t irq_index) {
uint32_t pin = (uint32_t)(1 << channel_pin[irq_index]); uint32_t pin = (uint32_t)(1 << channel_pin[irq_index]);
// Clear interrupt flag // Clear interrupt flag
if (EXTI_GetITStatus(pin) != RESET) if (EXTI_GetITStatus(pin) != RESET) {
{
EXTI_ClearITPendingBit(pin); EXTI_ClearITPendingBit(pin);
} }
@ -63,20 +61,33 @@ static void handle_interrupt_in(uint32_t irq_index) {
// Check which edge has generated the irq // Check which edge has generated the irq
if ((gpio->IDR & pin) == 0) { if ((gpio->IDR & pin) == 0) {
irq_handler(channel_ids[irq_index], IRQ_FALL); irq_handler(channel_ids[irq_index], IRQ_FALL);
} } else {
else {
irq_handler(channel_ids[irq_index], IRQ_RISE); irq_handler(channel_ids[irq_index], IRQ_RISE);
} }
} }
// The irq_index is passed to the function // The irq_index is passed to the function
static void gpio_irq0(void) {handle_interrupt_in(0);} // EXTI line 0 static void gpio_irq0(void) {
static void gpio_irq1(void) {handle_interrupt_in(1);} // EXTI line 1 handle_interrupt_in(0); // EXTI line 0
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_irq1(void) {
static void gpio_irq4(void) {handle_interrupt_in(4);} // EXTI line 4 handle_interrupt_in(1); // EXTI line 1
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 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
}
extern uint32_t Set_GPIO_Clock(uint32_t port_idx); extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
@ -205,8 +216,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)) { if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
obj->event = EDGE_BOTH; obj->event = EDGE_BOTH;
} } else { // NONE or RISE
else { // NONE or RISE
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
obj->event = EDGE_RISE; obj->event = EDGE_RISE;
} }
@ -216,8 +226,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) { if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
obj->event = EDGE_BOTH; obj->event = EDGE_BOTH;
} } else { // NONE or FALL
else { // NONE or FALL
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
obj->event = EDGE_FALL; obj->event = EDGE_FALL;
} }
@ -225,8 +234,7 @@ void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
if (enable) { if (enable) {
EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_InitStructure.EXTI_LineCmd = ENABLE;
} } else {
else {
EXTI_InitStructure.EXTI_LineCmd = DISABLE; EXTI_InitStructure.EXTI_LineCmd = DISABLE;
} }

View File

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

View File

@ -147,8 +147,7 @@ inline int i2c_stop(i2c_t *obj) {
} }
temp = i2c->SR1; temp = i2c->SR1;
I2C_Cmd(i2c, ENABLE); I2C_Cmd(i2c, ENABLE);
} } else {
else {
I2C_GenerateSTOP(i2c, ENABLE); I2C_GenerateSTOP(i2c, ENABLE);
} }

View File

@ -25,8 +25,7 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * 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. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "cmsis.h"
extern void SystemCoreClockUpdate(void);
// This function is called after RAM initialization and before main. // This function is called after RAM initialization and before main.
void mbed_sdk_init() { void mbed_sdk_init() {

View File

@ -28,12 +28,13 @@
******************************************************************************* *******************************************************************************
*/ */
#include "port_api.h" #include "port_api.h"
#if DEVICE_PORTIN || DEVICE_PORTOUT
#include "pinmap.h" #include "pinmap.h"
#include "gpio_api.h" #include "gpio_api.h"
#include "error.h" #include "error.h"
#if DEVICE_PORTIN || DEVICE_PORTOUT
extern uint32_t Set_GPIO_Clock(uint32_t port_idx); extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
// high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...) // high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
@ -66,8 +67,7 @@ void port_dir(port_t *obj, PinDirection dir) {
if (obj->mask & (1 << i)) { // If the pin is used if (obj->mask & (1 << i)) { // If the pin is used
if (dir == PIN_OUTPUT) { if (dir == PIN_OUTPUT) {
pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_OUT, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)); pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_OUT, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF));
} } else { // PIN_INPUT
else { // PIN_INPUT
pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
} }
} }
@ -90,8 +90,7 @@ void port_write(port_t *obj, int value) {
int port_read(port_t *obj) { int port_read(port_t *obj) {
if (obj->direction == PIN_OUTPUT) { if (obj->direction == PIN_OUTPUT) {
return (*obj->reg_out & obj->mask); return (*obj->reg_out & obj->mask);
} } else { // PIN_INPUT
else { // PIN_INPUT
return (*obj->reg_in & obj->mask); return (*obj->reg_in & obj->mask);
} }
} }

View File

@ -29,6 +29,8 @@
*/ */
#include "pwmout_api.h" #include "pwmout_api.h"
#if DEVICE_PWMOUT
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
@ -225,3 +227,5 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
float value = (float)us / (float)obj->period; float value = (float)us / (float)obj->period;
pwmout_write(obj, value); pwmout_write(obj, value);
} }
#endif

View File

@ -29,35 +29,62 @@
*/ */
#include "rtc_api.h" #include "rtc_api.h"
#if DEVICE_RTC
#include "wait_api.h"
#define LSE_STARTUP_TIMEOUT ((uint16_t)400) // delay in ms
static int rtc_inited = 0; static int rtc_inited = 0;
void rtc_init(void) { void rtc_init(void) {
uint32_t StartUpCounter = 0;
uint32_t LSEStatus = 0;
uint32_t rtc_freq = 0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Enable PWR clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); // Enable PWR clock
PWR_RTCAccessCmd(ENABLE); // Enable access to RTC PWR_RTCAccessCmd(ENABLE); // Enable access to Backup domain
// Note: the LSI is used as RTC source clock // Reset RTC and Backup registers
RCC_RTCResetCmd(ENABLE);
RCC_RTCResetCmd(DISABLE);
// Enable LSE clock
RCC_LSEConfig(RCC_LSE_ON);
// Wait till LSE is ready
do {
LSEStatus = RCC_GetFlagStatus(RCC_FLAG_LSERDY);
wait_ms(1);
StartUpCounter++;
} while ((LSEStatus == 0) && (StartUpCounter <= LSE_STARTUP_TIMEOUT));
if (StartUpCounter > LSE_STARTUP_TIMEOUT) {
// The LSE has not started, use LSI instead.
// The RTC Clock may vary due to LSI frequency dispersion. // The RTC Clock may vary due to LSI frequency dispersion.
RCC_LSEConfig(RCC_LSE_OFF);
RCC_LSICmd(ENABLE); // Enable LSI RCC_LSICmd(ENABLE); // Enable LSI
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} // Wait until ready
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select the RTC Clock Source
rtc_freq = 40000; // [TODO] To be measured precisely using a timer input capture
} else {
// The LSE has correctly started
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select the RTC Clock Source
rtc_freq = LSE_VALUE;
}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select LSI as RTC Clock Source RTC_InitTypeDef RTC_InitStructure;
RTC_InitStructure.RTC_AsynchPrediv = 127;
RTC_InitStructure.RTC_SynchPrediv = (rtc_freq / 128) - 1;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStructure);
RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock RCC_RTCCLKCmd(ENABLE); // Enable RTC Clock
RTC_WaitForSynchro(); // Wait for RTC registers synchronization RTC_WaitForSynchro(); // Wait for RTC registers synchronization
uint32_t lsi_freq = 40000; // [TODO] To be measured precisely using a timer input capture PWR_RTCAccessCmd(DISABLE); // Disable access to Backup domain
RTC_InitTypeDef RTC_InitStructure;
RTC_InitStructure.RTC_AsynchPrediv = 127;
RTC_InitStructure.RTC_SynchPrediv = (lsi_freq / 128) - 1;
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStructure);
PWR_RTCAccessCmd(DISABLE); // Disable access to RTC
rtc_inited = 1; rtc_inited = 1;
} }
@ -130,8 +157,10 @@ void rtc_write(time_t t) {
timeStruct.RTC_H12 = RTC_HourFormat_24; timeStruct.RTC_H12 = RTC_HourFormat_24;
// Change the RTC current date/time // Change the RTC current date/time
PWR_RTCAccessCmd(ENABLE); // Enable access to RTC PWR_RTCAccessCmd(ENABLE); // Enable access to Backup domain
RTC_SetDate(RTC_Format_BIN, &dateStruct); RTC_SetDate(RTC_Format_BIN, &dateStruct);
RTC_SetTime(RTC_Format_BIN, &timeStruct); RTC_SetTime(RTC_Format_BIN, &timeStruct);
PWR_RTCAccessCmd(DISABLE); // Disable access to RTC PWR_RTCAccessCmd(DISABLE); // Disable access to Backup domain
} }
#endif

View File

@ -28,6 +28,9 @@
******************************************************************************* *******************************************************************************
*/ */
#include "serial_api.h" #include "serial_api.h"
#if DEVICE_SERIAL
#include "cmsis.h" #include "cmsis.h"
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
@ -150,8 +153,7 @@ void serial_baud(serial_t *obj, int baudrate) {
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) { void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) {
if (data_bits == 8) { if (data_bits == 8) {
obj->databits = USART_WordLength_8b; obj->databits = USART_WordLength_8b;
} } else {
else {
obj->databits = USART_WordLength_9b; obj->databits = USART_WordLength_9b;
} }
@ -171,8 +173,7 @@ void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_b
if (stop_bits == 2) { if (stop_bits == 2) {
obj->stopbits = USART_StopBits_2; obj->stopbits = USART_StopBits_2;
} } else {
else {
obj->stopbits = USART_StopBits_1; obj->stopbits = USART_StopBits_1;
} }
@ -197,11 +198,21 @@ static void uart_irq(USART_TypeDef* usart, int id) {
} }
} }
static void uart1_irq(void) {uart_irq((USART_TypeDef*)UART_1, 0);} static void uart1_irq(void) {
static void uart2_irq(void) {uart_irq((USART_TypeDef*)UART_2, 1);} uart_irq((USART_TypeDef*)UART_1, 0);
static void uart3_irq(void) {uart_irq((USART_TypeDef*)UART_3, 2);} }
static void uart4_irq(void) {uart_irq((USART_TypeDef*)UART_4, 3);} static void uart2_irq(void) {
static void uart5_irq(void) {uart_irq((USART_TypeDef*)UART_5, 4);} uart_irq((USART_TypeDef*)UART_2, 1);
}
static void uart3_irq(void) {
uart_irq((USART_TypeDef*)UART_3, 2);
}
static void uart4_irq(void) {
uart_irq((USART_TypeDef*)UART_4, 3);
}
static void uart5_irq(void) {
uart_irq((USART_TypeDef*)UART_5, 4);
}
void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) { void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) {
irq_handler = handler; irq_handler = handler;
@ -242,8 +253,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
if (irq == RxIrq) { if (irq == RxIrq) {
USART_ITConfig(usart, USART_IT_RXNE, ENABLE); USART_ITConfig(usart, USART_IT_RXNE, ENABLE);
} } else { // TxIrq
else { // TxIrq
USART_ITConfig(usart, USART_IT_TC, ENABLE); USART_ITConfig(usart, USART_IT_TC, ENABLE);
} }
@ -258,8 +268,7 @@ void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) {
USART_ITConfig(usart, USART_IT_RXNE, DISABLE); USART_ITConfig(usart, USART_IT_RXNE, DISABLE);
// Check if TxIrq is disabled too // Check if TxIrq is disabled too
if ((usart->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1; if ((usart->CR1 & USART_CR1_TXEIE) == 0) all_disabled = 1;
} } else { // TxIrq
else { // TxIrq
USART_ITConfig(usart, USART_IT_TXE, DISABLE); USART_ITConfig(usart, USART_IT_TXE, DISABLE);
// Check if RxIrq is disabled too // Check if RxIrq is disabled too
if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1; if ((usart->CR1 & USART_CR1_RXNEIE) == 0) all_disabled = 1;
@ -319,3 +328,5 @@ void serial_break_set(serial_t *obj) {
void serial_break_clear(serial_t *obj) { void serial_break_clear(serial_t *obj) {
} }
#endif

View File

@ -28,14 +28,13 @@
******************************************************************************* *******************************************************************************
*/ */
#include "sleep_api.h" #include "sleep_api.h"
#if DEVICE_SLEEP
#include "cmsis.h" #include "cmsis.h"
// This function is in the system_stm32l1xx.c file
extern void SetSysClock(void);
// MCU SLEEP mode // MCU SLEEP mode
void sleep(void) void sleep(void) {
{
// Enable PWR clock // Enable PWR clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
@ -44,8 +43,7 @@ void sleep(void)
} }
// MCU STOP mode (Regulator in LP mode, LSI, HSI and HSE OFF) // MCU STOP mode (Regulator in LP mode, LSI, HSI and HSE OFF)
void deepsleep(void) void deepsleep(void) {
{
// Enable PWR clock // Enable PWR clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
@ -58,3 +56,5 @@ void deepsleep(void)
// After wake-up from STOP reconfigure the PLL // After wake-up from STOP reconfigure the PLL
SetSysClock(); SetSysClock();
} }
#endif

View File

@ -135,8 +135,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
if (ssel == NC) { // Master if (ssel == NC) { // Master
obj->mode = SPI_Mode_Master; obj->mode = SPI_Mode_Master;
obj->nss = SPI_NSS_Soft; obj->nss = SPI_NSS_Soft;
} } else { // Slave
else { // Slave
pinmap_pinout(ssel, PinMap_SPI_SSEL); pinmap_pinout(ssel, PinMap_SPI_SSEL);
obj->mode = SPI_Mode_Slave; obj->mode = SPI_Mode_Slave;
obj->nss = SPI_NSS_Soft; obj->nss = SPI_NSS_Soft;
@ -154,8 +153,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
// Save new values // Save new values
if (bits == 8) { if (bits == 8) {
obj->bits = SPI_DataSize_8b; obj->bits = SPI_DataSize_8b;
} } else {
else {
obj->bits = SPI_DataSize_16b; obj->bits = SPI_DataSize_16b;
} }
@ -181,8 +179,7 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
if (slave == 0) { if (slave == 0) {
obj->mode = SPI_Mode_Master; obj->mode = SPI_Mode_Master;
obj->nss = SPI_NSS_Soft; obj->nss = SPI_NSS_Soft;
} } else {
else {
obj->mode = SPI_Mode_Slave; obj->mode = SPI_Mode_Slave;
obj->nss = SPI_NSS_Hard; obj->nss = SPI_NSS_Hard;
} }
@ -195,52 +192,37 @@ void spi_frequency(spi_t *obj, int hz) {
if (SystemCoreClock == 32000000) { // HSI if (SystemCoreClock == 32000000) { // HSI
if (hz < 250000) { if (hz < 250000) {
obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz
} } else if ((hz >= 250000) && (hz < 500000)) {
else if ((hz >= 250000) && (hz < 500000)) {
obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz
} } else if ((hz >= 500000) && (hz < 1000000)) {
else if ((hz >= 500000) && (hz < 1000000)) {
obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz
} } else if ((hz >= 1000000) && (hz < 2000000)) {
else if ((hz >= 1000000) && (hz < 2000000)) {
obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz
} } else if ((hz >= 2000000) && (hz < 4000000)) {
else if ((hz >= 2000000) && (hz < 4000000)) {
obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz
} } else if ((hz >= 4000000) && (hz < 8000000)) {
else if ((hz >= 4000000) && (hz < 8000000)) {
obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz
} } else if ((hz >= 8000000) && (hz < 16000000)) {
else if ((hz >= 8000000) && (hz < 16000000)) {
obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz
} } else { // >= 16000000
else { // >= 16000000
obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz
} }
} } else { // 24 MHz - HSE
else { // 24 MHz - HSE
if (hz < 180000) { if (hz < 180000) {
obj->br_presc = SPI_BaudRatePrescaler_256; // 94 kHz obj->br_presc = SPI_BaudRatePrescaler_256; // 94 kHz
} } else if ((hz >= 180000) && (hz < 350000)) {
else if ((hz >= 180000) && (hz < 350000)) {
obj->br_presc = SPI_BaudRatePrescaler_128; // 188 kHz obj->br_presc = SPI_BaudRatePrescaler_128; // 188 kHz
} } else if ((hz >= 350000) && (hz < 750000)) {
else if ((hz >= 350000) && (hz < 750000)) {
obj->br_presc = SPI_BaudRatePrescaler_64; // 375 kHz obj->br_presc = SPI_BaudRatePrescaler_64; // 375 kHz
} } else if ((hz >= 750000) && (hz < 1000000)) {
else if ((hz >= 750000) && (hz < 1000000)) {
obj->br_presc = SPI_BaudRatePrescaler_32; // 750 kHz obj->br_presc = SPI_BaudRatePrescaler_32; // 750 kHz
} } else if ((hz >= 1000000) && (hz < 3000000)) {
else if ((hz >= 1000000) && (hz < 3000000)) {
obj->br_presc = SPI_BaudRatePrescaler_16; // 1.5 MHz obj->br_presc = SPI_BaudRatePrescaler_16; // 1.5 MHz
} } else if ((hz >= 3000000) && (hz < 6000000)) {
else if ((hz >= 3000000) && (hz < 6000000)) {
obj->br_presc = SPI_BaudRatePrescaler_8; // 3 MHz obj->br_presc = SPI_BaudRatePrescaler_8; // 3 MHz
} } else if ((hz >= 6000000) && (hz < 12000000)) {
else if ((hz >= 6000000) && (hz < 12000000)) {
obj->br_presc = SPI_BaudRatePrescaler_4; // 6 MHz obj->br_presc = SPI_BaudRatePrescaler_4; // 6 MHz
} } else { // >= 12000000
else { // >= 12000000
obj->br_presc = SPI_BaudRatePrescaler_2; // 12 MHz obj->br_presc = SPI_BaudRatePrescaler_2; // 12 MHz
} }
} }