Skip to content

[NUCLEOs] Align hal files #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@
extern "C" {
#endif

// See stm32f0xx_hal_gpio.h and stm32f0xx_hal_gpio_ex.h for values of MODE, PUPD and AFNUM
#define STM_PIN_DATA(MODE, PUPD, AFNUM) ((int)(((AFNUM) << 7) | ((PUPD) << 4) | ((MODE) << 0)))

#define STM_PIN_MODE(X) (((X) >> 0) & 0x0F)
#define STM_PIN_PUPD(X) (((X) >> 4) & 0x07)
#define STM_PIN_AFNUM(X) (((X) >> 7) & 0x0F)

#define STM_MODE_INPUT (0)
#define STM_MODE_OUTPUT_PP (1)
#define STM_MODE_OUTPUT_OD (2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

#define DEVICE_STDIO_MESSAGES 1

//#define DEVICE_ERROR_RED 0
#define DEVICE_ERROR_RED 0

#include "objects.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
pin_mode(sda, OpenDrain);
pin_mode(scl, OpenDrain);
}

// Enable I2C2 clock and pinout if not done
if ((obj->i2c == I2C_2) && !i2c2_inited) {
i2c2_inited = 1;
Expand Down Expand Up @@ -176,7 +177,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
int count;
int value;

/* update CR2 register */
// Update CR2 register
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ);

Expand All @@ -199,15 +200,15 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
// If not repeated start, send stop.
if (stop) {
i2c_stop(obj);
/* Wait until STOPF flag is set */
// Wait until STOPF flag is set
timeout = FLAG_TIMEOUT;
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
timeout--;
if (timeout == 0) {
return -1;
}
}
/* Clear STOP Flag */
// Clear STOP Flag
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
}

Expand All @@ -221,12 +222,10 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
int timeout;
int count;

/* update CR2 register */
// Update CR2 register
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE);



for (count = 0; count < length; count++) {
i2c_byte_write(obj, data[count]);
}
Expand All @@ -241,18 +240,18 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
}
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);

// If not repeated start, send stop.
// If not repeated start, send stop
if (stop) {
i2c_stop(obj);
/* Wait until STOPF flag is set */
// Wait until STOPF flag is set
timeout = FLAG_TIMEOUT;
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
timeout--;
if (timeout == 0) {
return -1;
}
}
/* Clear STOP Flag */
// Clear STOP Flag
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
}

Expand Down Expand Up @@ -297,7 +296,7 @@ void i2c_reset(i2c_t *obj)
{
int timeout;

// wait before reset
// Wait before reset
timeout = LONG_TIMEOUT;
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));

Expand Down Expand Up @@ -334,7 +333,6 @@ void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)

void i2c_slave_mode(i2c_t *obj, int enable_slave)
{

I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
uint16_t tmpreg;

Expand All @@ -350,7 +348,6 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave)

// Set new mode
i2c->OAR1 = tmpreg;

}

// See I2CSlave.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
#include "mbed_error.h"

// TIM1 cannot be used because already used by the us_ticker
// Uncomment/comment line above to use an alternate timer,
// If the channel is not the same,
// Please don't forget to uncomment/comment in the pwmout_write() function also
static const PinMap PinMap_PWM[] = {
{PA_4, PWM_14, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF4_TIM14)}, // TIM14_CH1
{PA_6, PWM_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF1_TIM3)}, // TIM3_CH1
Expand Down Expand Up @@ -128,7 +125,6 @@ void pwmout_write(pwmout_t* obj, float value)
// Channels 1
case PA_4:
case PA_6:
// case PA_7:
case PB_1:
case PB_4:
case PB_8:
Expand All @@ -140,7 +136,6 @@ void pwmout_write(pwmout_t* obj, float value)
// Channels 1N
case PB_6:
case PB_7:
// case PB_15:
channel = TIM_CHANNEL_1;
complementary_channel = 1;
break;
Expand All @@ -157,7 +152,6 @@ void pwmout_write(pwmout_t* obj, float value)
channel = TIM_CHANNEL_3;
break;
// Channels 4
// case PB_1:
case PC_9:
channel = TIM_CHANNEL_4;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ void rtc_init(void)

// 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 */
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_CONFIG(RCC_RTCCLKSOURCE_LSE);
Expand All @@ -76,8 +76,8 @@ void rtc_init(void)
}
// Connect LSI to RTC
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);
// [TODO] This value is LSI typical value. To be measured precisely using a timer input capture.
rtc_freq = 32000;
// This value is LSI typical value. To be measured precisely using a timer input capture for example.
rtc_freq = LSI_VALUE;
}

// Enable RTC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
__USART1_CLK_ENABLE();
obj->index = 0;
}

if (obj->uart == UART_2) {
__USART2_CLK_ENABLE();
obj->index = 1;
Expand All @@ -110,8 +111,12 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
// Configure the UART pins
pinmap_pinout(tx, PinMap_UART_TX);
pinmap_pinout(rx, PinMap_UART_RX);
pin_mode(tx, PullUp);
pin_mode(rx, PullUp);
if (tx != NC) {
pin_mode(tx, PullUp);
}
if (rx != NC) {
pin_mode(rx, PullUp);
}

// Configure UART
obj->baudrate = 9600;
Expand Down Expand Up @@ -139,6 +144,7 @@ void serial_free(serial_t *obj)
__USART1_RELEASE_RESET();
__USART1_CLK_DISABLE();
}

if (obj->uart == UART_2) {
__USART2_FORCE_RESET();
__USART2_RELEASE_RESET();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void spi_free(spi_t *obj)
__SPI2_CLK_DISABLE();
}

// Configure GPIO
// Configure GPIOs
pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
Expand Down Expand Up @@ -220,6 +220,7 @@ void spi_frequency(spi_t *obj, int hz)
} else { // >= 24000000
obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 24 MHz
}

init_spi(obj);
}

Expand All @@ -245,13 +246,12 @@ static inline void ssp_write(spi_t *obj, int value)
{
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
while (!ssp_writeable(obj));

if (obj->bits <= SPI_DATASIZE_8BIT) {
// force 8-bit access the data register due to SPI data buffer in this device
if (obj->bits == SPI_DATASIZE_8BIT) {
// Force 8-bit access to the data register
uint8_t *p_spi_dr = 0;
p_spi_dr = (uint8_t *) & (spi->DR);
*p_spi_dr = (uint8_t)value;
} else {
} else { // SPI_DATASIZE_16BIT
spi->DR = (uint16_t)value;
}
}
Expand All @@ -260,9 +260,8 @@ static inline int ssp_read(spi_t *obj)
{
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
while (!ssp_readable(obj));

if (obj->bits <= SPI_DATASIZE_8BIT) {
// force 8-bit access the data register due to SPI data buffer in this device
if (obj->bits == SPI_DATASIZE_8BIT) {
// Force 8-bit access to the data register
uint8_t *p_spi_dr = 0;
p_spi_dr = (uint8_t *) & (spi->DR);
return (int)(*p_spi_dr);
Expand Down Expand Up @@ -294,14 +293,28 @@ int spi_slave_read(spi_t *obj)
{
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
while (!ssp_readable(obj));
return (int)spi->DR;
if (obj->bits == SPI_DATASIZE_8BIT) {
// Force 8-bit access to the data register
uint8_t *p_spi_dr = 0;
p_spi_dr = (uint8_t *) & (spi->DR);
return (int)(*p_spi_dr);
} else {
return (int)spi->DR;
}
}

void spi_slave_write(spi_t *obj, int value)
{
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
while (!ssp_writeable(obj));
spi->DR = (uint16_t)value;
if (obj->bits == SPI_DATASIZE_8BIT) {
// Force 8-bit access to the data register
uint8_t *p_spi_dr = 0;
p_spi_dr = (uint8_t *) & (spi->DR);
*p_spi_dr = (uint8_t)value;
} else { // SPI_DATASIZE_16BIT
spi->DR = (uint16_t)value;
}
}

int spi_busy(spi_t *obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
pin_mode(sda, OpenDrain);
pin_mode(scl, OpenDrain);
}

// Enable I2C2 clock and pinout if not done
if ((obj->i2c == I2C_2) && !i2c2_inited) {
i2c2_inited = 1;
Expand Down Expand Up @@ -178,7 +179,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
int count;
int value;

/* update CR2 register */
// Update CR2 register
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_READ);

Expand All @@ -201,15 +202,15 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
// If not repeated start, send stop.
if (stop) {
i2c_stop(obj);
/* Wait until STOPF flag is set */
// Wait until STOPF flag is set
timeout = FLAG_TIMEOUT;
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
timeout--;
if (timeout == 0) {
return -1;
}
}
/* Clear STOP Flag */
// Clear STOP Flag
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
}

Expand All @@ -223,12 +224,10 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
int timeout;
int count;

/* update CR2 register */
// Update CR2 register
i2c->CR2 = (i2c->CR2 & (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP)))
| (uint32_t)(((uint32_t)address & I2C_CR2_SADD) | (((uint32_t)length << 16) & I2C_CR2_NBYTES) | (uint32_t)I2C_SOFTEND_MODE | (uint32_t)I2C_GENERATE_START_WRITE);



for (count = 0; count < length; count++) {
i2c_byte_write(obj, data[count]);
}
Expand All @@ -243,18 +242,18 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
}
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_TC);

// If not repeated start, send stop.
// If not repeated start, send stop
if (stop) {
i2c_stop(obj);
/* Wait until STOPF flag is set */
// Wait until STOPF flag is set
timeout = FLAG_TIMEOUT;
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_STOPF) == RESET) {
timeout--;
if (timeout == 0) {
return -1;
}
}
/* Clear STOP Flag */
// Clear STOP Flag
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_STOPF);
}

Expand Down Expand Up @@ -299,7 +298,7 @@ void i2c_reset(i2c_t *obj)
{
int timeout;

// wait before reset
// Wait before reset
timeout = LONG_TIMEOUT;
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));

Expand Down Expand Up @@ -336,7 +335,6 @@ void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)

void i2c_slave_mode(i2c_t *obj, int enable_slave)
{

I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
uint16_t tmpreg;

Expand All @@ -352,7 +350,6 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave)

// Set new mode
i2c->OAR1 = tmpreg;

}

// See I2CSlave.h
Expand Down
Loading