Merge pull request #331 from bcostm/master

[NUCLEO_xxx] Update xxx_free() functions + typo
pull/334/head
Bogdan Marinescu 2014-05-27 09:57:57 +01:00
commit 1cdfe81b13
23 changed files with 527 additions and 445 deletions

View File

@ -161,17 +161,17 @@ static inline uint16_t adc_read(analogin_t *obj) {
return 0; return 0;
} }
while(!ADC_GetFlagStatus(adc, ADC_FLAG_ADRDY)); // Wait ADC ready while (!ADC_GetFlagStatus(adc, ADC_FLAG_ADRDY)); // Wait ADC ready
ADC_StartOfConversion(adc); // Start conversion ADC_StartOfConversion(adc); // Start conversion
while(ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion while (ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion
return(ADC_GetConversionValue(adc)); // Get conversion value return (ADC_GetConversionValue(adc)); // Get conversion value
} }
uint16_t analogin_read_u16(analogin_t *obj) { uint16_t analogin_read_u16(analogin_t *obj) {
return(adc_read(obj)); return (adc_read(obj));
} }
float analogin_read(analogin_t *obj) { float analogin_read(analogin_t *obj) {

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

@ -53,8 +53,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,16 +62,21 @@ 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);} static void gpio_irq0(void) {
static void gpio_irq1(void) {handle_interrupt_in(1);} handle_interrupt_in(0);
static void gpio_irq2(void) {handle_interrupt_in(2);} }
static void gpio_irq1(void) {
handle_interrupt_in(1);
}
static void gpio_irq2(void) {
handle_interrupt_in(2);
}
extern uint32_t Set_GPIO_Clock(uint32_t port_idx); extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
@ -91,18 +95,15 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32
irq_n = EXTI0_1_IRQn; irq_n = EXTI0_1_IRQn;
vector = (uint32_t)&gpio_irq0; vector = (uint32_t)&gpio_irq0;
irq_index = 0; irq_index = 0;
} } else if ((pin_index == 2) || (pin_index == 3)) {
else if ((pin_index == 2) || (pin_index == 3)) {
irq_n = EXTI2_3_IRQn; irq_n = EXTI2_3_IRQn;
vector = (uint32_t)&gpio_irq1; vector = (uint32_t)&gpio_irq1;
irq_index = 1; irq_index = 1;
} } else if ((pin_index > 3) && (pin_index < 16)) {
else if ((pin_index > 3) && (pin_index < 16)) {
irq_n = EXTI4_15_IRQn; irq_n = EXTI4_15_IRQn;
vector = (uint32_t)&gpio_irq2; vector = (uint32_t)&gpio_irq2;
irq_index = 2; irq_index = 2;
} } else {
else {
error("InterruptIn error: pin not supported.\n"); error("InterruptIn error: pin not supported.\n");
return -1; return -1;
} }
@ -170,8 +171,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;
} }
@ -181,8 +181,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;
} }
@ -190,8 +189,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

@ -193,12 +193,12 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
} }
timeout = FLAG_TIMEOUT; timeout = FLAG_TIMEOUT;
while(!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) {
timeout--; timeout--;
if (timeout == 0) return 0; if (timeout == 0) return 0;
} }
if(stop) i2c_stop(obj); if (stop) i2c_stop(obj);
return length; return length;
} }
@ -219,12 +219,12 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
} }
timeout = FLAG_TIMEOUT; timeout = FLAG_TIMEOUT;
while(!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) {
timeout--; timeout--;
if (timeout == 0) return 0; if (timeout == 0) return 0;
} }
if(stop) i2c_stop(obj); if (stop) i2c_stop(obj);
return count; return count;
} }
@ -284,7 +284,7 @@ void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
uint16_t tmpreg; uint16_t tmpreg;
// reset own address enable // reset own address enable
i2c->OAR1 &=~ I2C_OAR1_OA1EN; i2c->OAR1 &= ~ I2C_OAR1_OA1EN;
// Get the old register value // Get the old register value
tmpreg = i2c->OAR1; tmpreg = i2c->OAR1;
@ -310,13 +310,12 @@ int i2c_slave_receive(i2c_t *obj) {
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
int event = NoData; int event = NoData;
if(I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) == SET) { if (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) == SET) {
if(I2C_GetFlagStatus(i2c, I2C_ISR_ADDR) == SET) { if (I2C_GetFlagStatus(i2c, I2C_ISR_ADDR) == SET) {
// Check direction // Check direction
if (I2C_GetFlagStatus(i2c, I2C_ISR_DIR) == SET) { if (I2C_GetFlagStatus(i2c, I2C_ISR_DIR) == SET) {
event = ReadAddressed; event = ReadAddressed;
} } else event = WriteAddressed;
else event = WriteAddressed;
// Clear adress match flag to generate an acknowledge // Clear adress match flag to generate an acknowledge
i2c->ICR |= I2C_ICR_ADDRCF; i2c->ICR |= I2C_ICR_ADDRCF;
} }

View File

@ -58,11 +58,6 @@ struct analogin_s {
PinName pin; PinName pin;
}; };
struct dac_s {
DACName dac;
PinName channel;
};
struct serial_s { struct serial_s {
UARTName uart; UARTName uart;
int index; // Used by irq int index; // Used by irq
@ -70,6 +65,8 @@ struct serial_s {
uint32_t databits; uint32_t databits;
uint32_t stopbits; uint32_t stopbits;
uint32_t parity; uint32_t parity;
PinName pin_tx;
PinName pin_rx;
}; };
struct spi_s { struct spi_s {
@ -80,6 +77,10 @@ struct spi_s {
uint32_t mode; uint32_t mode;
uint32_t nss; uint32_t nss;
uint32_t br_presc; uint32_t br_presc;
PinName pin_miso;
PinName pin_mosi;
PinName pin_sclk;
PinName pin_ssel;
}; };
struct i2c_s { struct i2c_s {

View File

@ -67,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));
} }
} }
@ -91,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

@ -79,7 +79,6 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
// Configure GPIO // Configure GPIO
pinmap_pinout(pin, PinMap_PWM); pinmap_pinout(pin, PinMap_PWM);
//pin_mode(pin, PullUp);
obj->pin = pin; obj->pin = pin;
obj->period = 0; obj->period = 0;
@ -89,8 +88,8 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
} }
void pwmout_free(pwmout_t* obj) { void pwmout_free(pwmout_t* obj) {
TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); // Configure GPIOs
TIM_DeInit(tim); pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
} }
void pwmout_write(pwmout_t* obj, float value) { void pwmout_write(pwmout_t* obj, float value) {

View File

@ -58,7 +58,7 @@ void rtc_init(void) {
LSEStatus = RCC_GetFlagStatus(RCC_FLAG_LSERDY); LSEStatus = RCC_GetFlagStatus(RCC_FLAG_LSERDY);
wait_ms(1); wait_ms(1);
StartUpCounter++; StartUpCounter++;
} while((LSEStatus == 0) && (StartUpCounter <= LSE_STARTUP_TIMEOUT)); } while ((LSEStatus == 0) && (StartUpCounter <= LSE_STARTUP_TIMEOUT));
if (StartUpCounter > LSE_STARTUP_TIMEOUT) { if (StartUpCounter > LSE_STARTUP_TIMEOUT) {
// The LSE has not started, use LSI instead. // The LSE has not started, use LSI instead.
@ -68,8 +68,7 @@ void rtc_init(void) {
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 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select the RTC Clock Source
rtc_freq = 40000; // [TODO] To be measured precisely using a timer input capture rtc_freq = 40000; // [TODO] To be measured precisely using a timer input capture
} } else {
else {
// The LSE has correctly started // The LSE has correctly started
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select the RTC Clock Source RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select the RTC Clock Source
rtc_freq = LSE_VALUE; rtc_freq = LSE_VALUE;
@ -91,7 +90,16 @@ void rtc_init(void) {
} }
void rtc_free(void) { void rtc_free(void) {
RCC_DeInit(); // Resets the RCC clock configuration to the default reset state // Reset RTC
PWR_BackupAccessCmd(ENABLE); // Enable access to Backup Domain
RTC_DeInit();
RCC_BackupResetCmd(ENABLE);
RCC_BackupResetCmd(DISABLE);
// Disable RTC, LSE and LSI clocks
RCC_RTCCLKCmd(DISABLE);
RCC_LSEConfig(RCC_LSE_OFF);
RCC_LSICmd(DISABLE);
rtc_inited = 0; rtc_inited = 0;
} }

View File

@ -92,9 +92,11 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
// Enable USART clock // Enable USART clock
if (obj->uart == UART_1) { if (obj->uart == UART_1) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
obj->index = 0;
} }
if (obj->uart == UART_2) { if (obj->uart == UART_2) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
obj->index = 1;
} }
// Configure the UART pins // Configure the UART pins
@ -109,11 +111,10 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
obj->stopbits = USART_StopBits_1; obj->stopbits = USART_StopBits_1;
obj->parity = USART_Parity_No; obj->parity = USART_Parity_No;
init_usart(obj); obj->pin_tx = tx;
obj->pin_rx = rx;
// The index is used by irq init_usart(obj);
if (obj->uart == UART_1) obj->index = 0;
if (obj->uart == UART_2) obj->index = 1;
// For stdio management // For stdio management
if (obj->uart == STDIO_UART) { if (obj->uart == STDIO_UART) {
@ -124,6 +125,22 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
} }
void serial_free(serial_t *obj) { void serial_free(serial_t *obj) {
// Reset UART and disable clock
if (obj->uart == UART_1) {
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, DISABLE);
}
if (obj->uart == UART_2) {
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, DISABLE);
}
// Configure GPIOs
pin_function(obj->pin_tx, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
pin_function(obj->pin_rx, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
serial_irq_ids[obj->index] = 0; serial_irq_ids[obj->index] = 0;
} }
@ -135,8 +152,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;
} }
@ -156,8 +172,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;
} }
@ -182,8 +197,12 @@ 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 uart2_irq(void) {
uart_irq((USART_TypeDef*)UART_2, 1);
}
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;
@ -209,8 +228,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);
} }
@ -225,8 +243,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;

View File

@ -33,8 +33,7 @@
#include "cmsis.h" #include "cmsis.h"
void sleep(void) void sleep(void) {
{
// Disable us_ticker update interrupt // Disable us_ticker update interrupt
TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE); TIM_ITConfig(TIM1, TIM_IT_Update, DISABLE);
@ -47,8 +46,7 @@ void sleep(void)
// MCU STOP mode // MCU STOP mode
// Wake-up with external interrupt // Wake-up with external interrupt
void deepsleep(void) void deepsleep(void) {
{
// Enable PWR clock // Enable PWR clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

View File

@ -122,11 +122,15 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
obj->cpha = SPI_CPHA_1Edge; obj->cpha = SPI_CPHA_1Edge;
obj->br_presc = SPI_BaudRatePrescaler_8; // 1 MHz obj->br_presc = SPI_BaudRatePrescaler_8; // 1 MHz
obj->pin_miso = miso;
obj->pin_mosi = mosi;
obj->pin_sclk = sclk;
obj->pin_ssel = 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_Hard; obj->nss = SPI_NSS_Hard;
@ -136,16 +140,31 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
} }
void spi_free(spi_t *obj) { void spi_free(spi_t *obj) {
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); // Reset SPI and disable clock
SPI_I2S_DeInit(spi); if (obj->spi == SPI_1) {
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, DISABLE);
}
if (obj->spi == SPI_2) {
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, DISABLE);
}
// Configure GPIOs
pin_function(obj->pin_miso, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
pin_function(obj->pin_mosi, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
pin_function(obj->pin_sclk, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
pin_function(obj->pin_ssel, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
} }
void spi_format(spi_t *obj, int bits, int mode, int slave) { 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;
} }
@ -171,8 +190,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;
} }
@ -184,26 +202,19 @@ void spi_frequency(spi_t *obj, int hz) {
// Note: The frequencies are obtained with SPI clock = 48 MHz (APB1 & APB2 clocks) // Note: The frequencies are obtained with SPI clock = 48 MHz (APB1 & APB2 clocks)
if (hz < 300000) { if (hz < 300000) {
obj->br_presc = SPI_BaudRatePrescaler_256; // 188 kHz obj->br_presc = SPI_BaudRatePrescaler_256; // 188 kHz
} } else if ((hz >= 300000) && (hz < 700000)) {
else if ((hz >= 300000) && (hz < 700000)) {
obj->br_presc = SPI_BaudRatePrescaler_128; // 375 kHz obj->br_presc = SPI_BaudRatePrescaler_128; // 375 kHz
} } else if ((hz >= 700000) && (hz < 1000000)) {
else if ((hz >= 700000) && (hz < 1000000)) {
obj->br_presc = SPI_BaudRatePrescaler_64; // 750 kHz obj->br_presc = SPI_BaudRatePrescaler_64; // 750 kHz
} } else if ((hz >= 1000000) && (hz < 3000000)) {
else if ((hz >= 1000000) && (hz < 3000000)) {
obj->br_presc = SPI_BaudRatePrescaler_32; // 1.5 MHz obj->br_presc = SPI_BaudRatePrescaler_32; // 1.5 MHz
} } else if ((hz >= 3000000) && (hz < 6000000)) {
else if ((hz >= 3000000) && (hz < 6000000)) {
obj->br_presc = SPI_BaudRatePrescaler_16; // 3 MHz obj->br_presc = SPI_BaudRatePrescaler_16; // 3 MHz
} } else if ((hz >= 6000000) && (hz < 12000000)) {
else if ((hz >= 6000000) && (hz < 12000000)) {
obj->br_presc = SPI_BaudRatePrescaler_8; // 6 MHz obj->br_presc = SPI_BaudRatePrescaler_8; // 6 MHz
} } else if ((hz >= 12000000) && (hz < 24000000)) {
else if ((hz >= 12000000) && (hz < 24000000)) {
obj->br_presc = SPI_BaudRatePrescaler_4; // 12 MHz obj->br_presc = SPI_BaudRatePrescaler_4; // 12 MHz
} } else { // >= 24000000
else { // >= 24000000
obj->br_presc = SPI_BaudRatePrescaler_2; // 24 MHz obj->br_presc = SPI_BaudRatePrescaler_2; // 24 MHz
} }
init_spi(obj); init_spi(obj);
@ -230,8 +241,7 @@ static inline void ssp_write(spi_t *obj, int value) {
while (!ssp_writeable(obj)); while (!ssp_writeable(obj));
if (obj->bits == SPI_DataSize_8b) { if (obj->bits == SPI_DataSize_8b) {
SPI_SendData8(spi, (uint8_t)value); SPI_SendData8(spi, (uint8_t)value);
} } else { // 16-bit
else { // 16-bit
SPI_I2S_SendData16(spi, (uint16_t)value); SPI_I2S_SendData16(spi, (uint16_t)value);
} }
} }
@ -241,8 +251,7 @@ static inline int ssp_read(spi_t *obj) {
while (!ssp_readable(obj)); while (!ssp_readable(obj));
if (obj->bits == SPI_DataSize_8b) { if (obj->bits == SPI_DataSize_8b) {
return (int)SPI_ReceiveData8(spi); return (int)SPI_ReceiveData8(spi);
} } else { // 16-bit
else { // 16-bit
return (int)SPI_I2S_ReceiveData16(spi); return (int)SPI_I2S_ReceiveData16(spi);
} }
} }
@ -267,8 +276,7 @@ int spi_slave_read(spi_t *obj) {
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
if (obj->bits == SPI_DataSize_8b) { if (obj->bits == SPI_DataSize_8b) {
return (int)SPI_ReceiveData8(spi); return (int)SPI_ReceiveData8(spi);
} } else { // 16-bit
else { // 16-bit
return (int)SPI_I2S_ReceiveData16(spi); return (int)SPI_I2S_ReceiveData16(spi);
} }
} }
@ -278,8 +286,7 @@ void spi_slave_write(spi_t *obj, int value) {
while (!ssp_writeable(obj)); while (!ssp_writeable(obj));
if (obj->bits == SPI_DataSize_8b) { if (obj->bits == SPI_DataSize_8b) {
SPI_SendData8(spi, (uint8_t)value); SPI_SendData8(spi, (uint8_t)value);
} } else { // 16-bit
else { // 16-bit
SPI_I2S_SendData16(spi, (uint16_t)value); SPI_I2S_SendData16(spi, (uint16_t)value);
} }
} }

View File

@ -67,14 +67,12 @@ static void tim_oc_irq_handler(void) {
if (oc_rem_part > 0) { if (oc_rem_part > 0) {
set_compare(oc_rem_part); // Finish the remaining time left set_compare(oc_rem_part); // Finish the remaining time left
oc_rem_part = 0; oc_rem_part = 0;
} } else {
else {
if (oc_int_part > 0) { if (oc_int_part > 0) {
set_compare(0xFFFF); set_compare(0xFFFF);
oc_rem_part = cval; // To finish the counter loop the next time oc_rem_part = cval; // To finish the counter loop the next time
oc_int_part--; oc_int_part--;
} } else {
else {
us_ticker_irq_handler(); us_ticker_irq_handler();
} }
} }
@ -139,8 +137,7 @@ void us_ticker_set_interrupt(unsigned int timestamp) {
if (delta <= 0) { // This event was in the past if (delta <= 0) { // This event was in the past
us_ticker_irq_handler(); us_ticker_irq_handler();
} } else {
else {
oc_int_part = (uint32_t)(delta >> 16); oc_int_part = (uint32_t)(delta >> 16);
oc_rem_part = (uint16_t)(delta & 0xFFFF); oc_rem_part = (uint16_t)(delta & 0xFFFF);
if (oc_rem_part <= (0xFFFF - cval)) { if (oc_rem_part <= (0xFFFF - cval)) {

View File

@ -57,7 +57,7 @@ void analogout_init(dac_t *obj, PinName pin) {
pinmap_pinout(pin, PinMap_DAC); pinmap_pinout(pin, PinMap_DAC);
// Save the channel for future use // Save the channel for future use
obj->channel = pin; obj->pin = pin;
// Enable DAC clock // Enable DAC clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
@ -76,7 +76,7 @@ void analogout_free(dac_t *obj) {
DAC_DeInit(dac); DAC_DeInit(dac);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, DISABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, DISABLE);
// Configure GPIO // Configure GPIO
pin_function(obj->channel, 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));
} }
static inline void dac_write(dac_t *obj, uint16_t value) { static inline void dac_write(dac_t *obj, uint16_t value) {

View File

@ -145,8 +145,7 @@ void i2c_frequency(i2c_t *obj, int hz) {
error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported."); error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported.");
break; break;
} }
} } else if (SystemCoreClock == 72000000) {
else if (SystemCoreClock == 72000000) {
switch (hz) { switch (hz) {
case 100000: case 100000:
tim = 0x10C08DCF; // Standard mode tim = 0x10C08DCF; // Standard mode
@ -171,8 +170,7 @@ void i2c_frequency(i2c_t *obj, int hz) {
error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported."); error("Only 100kHz, 200kHz, 400kHz and 1MHz I2C frequencies are supported.");
break; break;
} }
} } else {
else {
error("System clock setting is not supported."); error("System clock setting is not supported.");
} }
@ -235,12 +233,12 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
} }
timeout = FLAG_TIMEOUT; timeout = FLAG_TIMEOUT;
while(!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) {
timeout--; timeout--;
if (timeout == 0) return 0; if (timeout == 0) return 0;
} }
if(stop) i2c_stop(obj); if (stop) i2c_stop(obj);
return length; return length;
} }
@ -262,12 +260,12 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
} }
timeout = FLAG_TIMEOUT; timeout = FLAG_TIMEOUT;
while(!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) {
timeout--; timeout--;
if (timeout == 0) return 0; if (timeout == 0) return 0;
} }
if(stop) i2c_stop(obj); if (stop) i2c_stop(obj);
return count; return count;
} }
@ -331,7 +329,7 @@ void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
uint16_t tmpreg; uint16_t tmpreg;
// reset own address enable // reset own address enable
i2c->OAR1 &=~ I2C_OAR1_OA1EN; i2c->OAR1 &= ~ I2C_OAR1_OA1EN;
// Get the old register value // Get the old register value
tmpreg = i2c->OAR1; tmpreg = i2c->OAR1;
@ -357,13 +355,12 @@ int i2c_slave_receive(i2c_t *obj) {
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
int event = NoData; int event = NoData;
if(I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) == SET) { if (I2C_GetFlagStatus(i2c, I2C_ISR_BUSY) == SET) {
if(I2C_GetFlagStatus(i2c, I2C_ISR_ADDR) == SET) { if (I2C_GetFlagStatus(i2c, I2C_ISR_ADDR) == SET) {
// Check direction // Check direction
if (I2C_GetFlagStatus(i2c, I2C_ISR_DIR) == SET) { if (I2C_GetFlagStatus(i2c, I2C_ISR_DIR) == SET) {
event = ReadAddressed; event = ReadAddressed;
} } else event = WriteAddressed;
else event = WriteAddressed;
// Clear adress match flag to generate an acknowledge // Clear adress match flag to generate an acknowledge
i2c->ICR |= I2C_ICR_ADDRCF; i2c->ICR |= I2C_ICR_ADDRCF;
} }

View File

@ -60,7 +60,7 @@ struct analogin_s {
struct dac_s { struct dac_s {
DACName dac; DACName dac;
PinName channel; PinName pin;
}; };
struct serial_s { struct serial_s {
@ -70,6 +70,8 @@ struct serial_s {
uint32_t databits; uint32_t databits;
uint32_t stopbits; uint32_t stopbits;
uint32_t parity; uint32_t parity;
PinName pin_tx;
PinName pin_rx;
}; };
struct spi_s { struct spi_s {
@ -80,6 +82,10 @@ struct spi_s {
uint32_t mode; uint32_t mode;
uint32_t nss; uint32_t nss;
uint32_t br_presc; uint32_t br_presc;
PinName pin_miso;
PinName pin_mosi;
PinName pin_sclk;
PinName pin_ssel;
}; };
struct i2c_s { struct i2c_s {

View File

@ -112,8 +112,8 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
} }
void pwmout_free(pwmout_t* obj) { void pwmout_free(pwmout_t* obj) {
TIM_TypeDef *tim = (TIM_TypeDef *)(obj->pwm); // Configure GPIO
TIM_DeInit(tim); pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
} }
void pwmout_write(pwmout_t* obj, float value) { void pwmout_write(pwmout_t* obj, float value) {

View File

@ -58,7 +58,7 @@ void rtc_init(void) {
LSEStatus = RCC_GetFlagStatus(RCC_FLAG_LSERDY); LSEStatus = RCC_GetFlagStatus(RCC_FLAG_LSERDY);
wait_ms(1); wait_ms(1);
StartUpCounter++; StartUpCounter++;
} while((LSEStatus == 0) && (StartUpCounter <= LSE_STARTUP_TIMEOUT)); } while ((LSEStatus == 0) && (StartUpCounter <= LSE_STARTUP_TIMEOUT));
if (StartUpCounter > LSE_STARTUP_TIMEOUT) { if (StartUpCounter > LSE_STARTUP_TIMEOUT) {
// The LSE has not started, use LSI instead. // The LSE has not started, use LSI instead.
@ -68,8 +68,7 @@ void rtc_init(void) {
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 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // Select the RTC Clock Source
rtc_freq = 40000; // [TODO] To be measured precisely using a timer input capture rtc_freq = 40000; // [TODO] To be measured precisely using a timer input capture
} } else {
else {
// The LSE has correctly started // The LSE has correctly started
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select the RTC Clock Source RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Select the RTC Clock Source
rtc_freq = LSE_VALUE; rtc_freq = LSE_VALUE;
@ -91,7 +90,16 @@ void rtc_init(void) {
} }
void rtc_free(void) { void rtc_free(void) {
RCC_DeInit(); // Resets the RCC clock configuration to the default reset state // Reset RTC
PWR_BackupAccessCmd(ENABLE); // Enable access to Backup Domain
RTC_DeInit();
RCC_BackupResetCmd(ENABLE);
RCC_BackupResetCmd(DISABLE);
// Disable RTC, LSE and LSI clocks
RCC_RTCCLKCmd(DISABLE);
RCC_LSEConfig(RCC_LSE_OFF);
RCC_LSICmd(DISABLE);
rtc_inited = 0; rtc_inited = 0;
} }

View File

@ -103,12 +103,15 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
// Enable USART clock // Enable USART clock
if (obj->uart == UART_1) { if (obj->uart == UART_1) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
obj->index = 0;
} }
if (obj->uart == UART_2) { if (obj->uart == UART_2) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
obj->index = 1;
} }
if (obj->uart == UART_3) { if (obj->uart == UART_3) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
obj->index = 2;
} }
// Configure the UART pins // Configure the UART pins
@ -123,12 +126,10 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
obj->stopbits = USART_StopBits_1; obj->stopbits = USART_StopBits_1;
obj->parity = USART_Parity_No; obj->parity = USART_Parity_No;
init_usart(obj); obj->pin_tx = tx;
obj->pin_rx = rx;
// The index is used by irq init_usart(obj);
if (obj->uart == UART_1) obj->index = 0;
if (obj->uart == UART_2) obj->index = 1;
if (obj->uart == UART_3) obj->index = 2;
// For stdio management // For stdio management
if (obj->uart == STDIO_UART) { if (obj->uart == STDIO_UART) {
@ -139,6 +140,27 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
} }
void serial_free(serial_t *obj) { void serial_free(serial_t *obj) {
// Reset UART and disable clock
if (obj->uart == UART_1) {
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, DISABLE);
}
if (obj->uart == UART_2) {
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, DISABLE);
}
if (obj->uart == UART_3) {
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, DISABLE);
}
// Configure GPIOs
pin_function(obj->pin_tx, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
pin_function(obj->pin_rx, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
serial_irq_ids[obj->index] = 0; serial_irq_ids[obj->index] = 0;
} }

View File

@ -125,6 +125,11 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
obj->cpha = SPI_CPHA_1Edge; obj->cpha = SPI_CPHA_1Edge;
obj->br_presc = SPI_BaudRatePrescaler_256; obj->br_presc = SPI_BaudRatePrescaler_256;
obj->pin_miso = miso;
obj->pin_mosi = mosi;
obj->pin_sclk = sclk;
obj->pin_ssel = 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;
@ -138,8 +143,23 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
} }
void spi_free(spi_t *obj) { void spi_free(spi_t *obj) {
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); // Reset SPI and disable clock
SPI_I2S_DeInit(spi); if (obj->spi == SPI_2) {
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, DISABLE);
}
if (obj->spi == SPI_3) {
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, DISABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, DISABLE);
}
// Configure GPIOs
pin_function(obj->pin_miso, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
pin_function(obj->pin_mosi, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
pin_function(obj->pin_sclk, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
pin_function(obj->pin_ssel, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
} }
void spi_format(spi_t *obj, int bits, int mode, int slave) { void spi_format(spi_t *obj, int bits, int mode, int slave) {

View File

@ -90,7 +90,16 @@ void rtc_init(void) {
} }
void rtc_free(void) { void rtc_free(void) {
RCC_DeInit(); // Resets the RCC clock configuration to the default reset state // Reset RTC
PWR_RTCAccessCmd(ENABLE); // Enable access to Backup Domain
RTC_DeInit();
RCC_RTCResetCmd(ENABLE);
RCC_RTCResetCmd(DISABLE);
// Disable RTC, LSE and LSI clocks
RCC_RTCCLKCmd(DISABLE);
RCC_LSEConfig(RCC_LSE_OFF);
RCC_LSICmd(DISABLE);
rtc_inited = 0; rtc_inited = 0;
} }