Merge pull request #329 from bcostm/master

[NUCLEO_L152RE_F401RE] Update xxx_free() functions + typo corrections
pull/333/head
Martin Kojtal 2014-05-27 09:21:47 +02:00
commit a2726bf3f0
12 changed files with 208 additions and 87 deletions

View File

@ -87,7 +87,12 @@ void rtc_init(void) {
}
void rtc_free(void) {
RCC_DeInit(); // Resets the RCC clock configuration to the default reset state
// Disable RTC, LSE and LSI clocks
PWR_BackupAccessCmd(ENABLE); // Allow access to Backup Domain
RCC_RTCCLKCmd(DISABLE);
RCC_LSEConfig(RCC_LSE_OFF);
RCC_LSICmd(DISABLE);
rtc_inited = 0;
}

View File

@ -95,12 +95,15 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
// Enable USART clock
if (obj->uart == UART_1) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
obj->index = 0;
}
if (obj->uart == UART_2) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
obj->index = 1;
}
if (obj->uart == UART_3) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
obj->index = 2;
}
// Configure the UART pins
@ -118,11 +121,6 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
init_usart(obj);
// The index is used by irq
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
if (obj->uart == STDIO_UART) {
stdio_uart_inited = 1;

View File

@ -93,7 +93,7 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
// I2C configuration
i2c_frequency(obj, 100000); // 100 kHz per default
// I2C master by default
obj->slave = 0;
}
@ -111,11 +111,11 @@ void i2c_frequency(i2c_t *obj, int hz) {
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
I2cHandle.Init.OwnAddress1 = 0;
I2cHandle.Init.OwnAddress2 = 0;
HAL_I2C_Init(&I2cHandle);
if(obj->slave) {
HAL_I2C_Init(&I2cHandle);
if (obj->slave) {
/* Enable Address Acknowledge */
I2cHandle.Instance->CR1 |= I2C_CR1_ACK;
}
}
} else {
error("I2C error: frequency setting failed (max 400kHz).");
}
@ -172,10 +172,10 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
return 0;
}
}
i2c->DR = __HAL_I2C_7BIT_ADD_READ(address);
i2c->DR = __HAL_I2C_7BIT_ADD_READ(address);
// Wait address is acknowledged
timeout = FLAG_TIMEOUT;
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == RESET) {
@ -222,9 +222,9 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
return 0;
}
}
i2c->DR = __HAL_I2C_7BIT_ADD_WRITE(address);
// Wait address is acknowledged
timeout = FLAG_TIMEOUT;
@ -235,7 +235,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
}
}
__HAL_I2C_CLEAR_ADDRFLAG(&I2cHandle);
for (count = 0; count < length; count++) {
if (i2c_byte_write(obj, data[count]) != 1) {
i2c_stop(obj);
@ -283,7 +283,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
// Wait until the byte is transmitted
timeout = FLAG_TIMEOUT;
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) &&
(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) {
(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == RESET)) {
if ((timeout--) == 0) {
return 0;
}
@ -325,7 +325,7 @@ 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) {
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
if(enable_slave) {
if (enable_slave) {
obj->slave = 1;
/* Enable Address Acknowledge */
I2cHandle.Instance->CR1 |= I2C_CR1_ACK;
@ -340,19 +340,19 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
int i2c_slave_receive(i2c_t *obj) {
int retValue = NoData;
if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
if(__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TRA) == 1)
retValue = ReadAddressed;
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TRA) == 1)
retValue = ReadAddressed;
else
retValue = WriteAddressed;
retValue = WriteAddressed;
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
}
}
}
return(retValue);
return (retValue);
}
int i2c_slave_read(i2c_t *obj, char *data, int length) {
@ -362,8 +362,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
while(length > 0)
{
while (length > 0) {
/* Wait until RXNE flag is set */
// Wait until the byte is received
Timeout = FLAG_TIMEOUT;
@ -372,14 +371,14 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
if (Timeout == 0) {
return 0;
}
}
}
/* Read data from DR */
(*data++) = I2cHandle.Instance->DR;
length--;
size++;
if((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)){
if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) {
/* Read data from DR */
(*data++) = I2cHandle.Instance->DR;
length--;
@ -394,8 +393,8 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
if (Timeout == 0) {
return 0;
}
}
}
/* Clear STOP flag */
__HAL_I2C_CLEAR_STOPFLAG(&I2cHandle);
@ -418,8 +417,7 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
while(length > 0)
{
while (length > 0) {
/* Wait until TXE flag is set */
Timeout = FLAG_TIMEOUT;
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_TXE) == RESET) {
@ -427,23 +425,22 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
if (Timeout == 0) {
return 0;
}
}
}
/* Write data to DR */
I2cHandle.Instance->DR = (*data++);
length--;
size++;
if((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0))
{
/* Write data to DR */
I2cHandle.Instance->DR = (*data++);
length--;
size++;
}
if ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BTF) == SET) && (length != 0)) {
/* Write data to DR */
I2cHandle.Instance->DR = (*data++);
length--;
size++;
}
}
/* Wait until AF flag is set */
Timeout = FLAG_TIMEOUT;
while (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_AF) == RESET) {
@ -451,9 +448,9 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length) {
if (Timeout == 0) {
return 0;
}
}
}
/* Clear AF flag */
__HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_AF);

View File

@ -66,6 +66,8 @@ struct serial_s {
uint32_t databits;
uint32_t stopbits;
uint32_t parity;
PinName pin_tx;
PinName pin_rx;
};
struct spi_s {
@ -76,6 +78,10 @@ struct spi_s {
uint32_t mode;
uint32_t nss;
uint32_t br_presc;
PinName pin_miso;
PinName pin_mosi;
PinName pin_sclk;
PinName pin_ssel;
};
struct i2c_s {

View File

@ -93,12 +93,15 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
// Enable USART clock
if (obj->uart == UART_1) {
__USART1_CLK_ENABLE();
obj->index = 0;
}
if (obj->uart == UART_2) {
__USART2_CLK_ENABLE();
obj->index = 1;
}
if (obj->uart == UART_6) {
__USART6_CLK_ENABLE();
obj->index = 2;
}
// Configure the UART pins
@ -113,12 +116,10 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
obj->stopbits = UART_STOPBITS_1;
obj->parity = UART_PARITY_NONE;
init_uart(obj);
obj->pin_tx = tx;
obj->pin_rx = rx;
// The index is used by irq
if (obj->uart == UART_1) obj->index = 0;
if (obj->uart == UART_2) obj->index = 1;
if (obj->uart == UART_6) obj->index = 2;
init_uart(obj);
// For stdio management
if (obj->uart == STDIO_UART) {
@ -129,6 +130,27 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
}
void serial_free(serial_t *obj) {
// Reset UART and disable clock
if (obj->uart == UART_1) {
__USART1_FORCE_RESET();
__USART1_RELEASE_RESET();
__USART1_CLK_DISABLE();
}
if (obj->uart == UART_2) {
__USART2_FORCE_RESET();
__USART2_RELEASE_RESET();
__USART2_CLK_DISABLE();
}
if (obj->uart == UART_6) {
__USART6_FORCE_RESET();
__USART6_RELEASE_RESET();
__USART6_CLK_DISABLE();
}
// Configure GPIOs
pin_function(obj->pin_tx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
pin_function(obj->pin_rx, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
serial_irq_ids[obj->index] = 0;
}

View File

@ -138,6 +138,11 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
obj->cpha = SPI_PHASE_1EDGE;
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) { // SW NSS Master mode
obj->mode = SPI_MODE_MASTER;
obj->nss = SPI_NSS_SOFT;
@ -151,8 +156,30 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
}
void spi_free(spi_t *obj) {
SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
HAL_SPI_DeInit(&SpiHandle);
// Reset SPI and disable clock
if (obj->spi == SPI_1) {
__SPI1_FORCE_RESET();
__SPI1_RELEASE_RESET();
__SPI1_CLK_DISABLE();
}
if (obj->spi == SPI_2) {
__SPI2_FORCE_RESET();
__SPI2_RELEASE_RESET();
__SPI2_CLK_DISABLE();
}
if (obj->spi == SPI_3) {
__SPI3_FORCE_RESET();
__SPI3_RELEASE_RESET();
__SPI3_CLK_DISABLE();
}
// 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));
pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
}
void spi_format(spi_t *obj, int bits, int mode, int slave) {

View File

@ -54,8 +54,8 @@ void analogout_init(dac_t *obj, PinName pin) {
// Configure GPIO
pinmap_pinout(pin, PinMap_DAC);
// Save the channel for the write and read functions
obj->channel = pin;
// Save the pin for future use
obj->pin = pin;
// Enable DAC clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
@ -66,11 +66,11 @@ void analogout_init(dac_t *obj, PinName pin) {
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
if (obj->channel == PA_4) {
if (obj->pin == PA_4) {
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_1, ENABLE);
}
if (obj->channel == PA_5) {
if (obj->pin == PA_5) {
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_2, ENABLE);
}
@ -79,22 +79,24 @@ void analogout_init(dac_t *obj, PinName pin) {
}
void analogout_free(dac_t *obj) {
// Configure GPIOs
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) {
if (obj->channel == PA_4) {
if (obj->pin == PA_4) {
DAC_SetChannel1Data(DAC_Align_12b_R, value);
}
if (obj->channel == PA_5) {
if (obj->pin == PA_5) {
DAC_SetChannel2Data(DAC_Align_12b_R, value);
}
}
static inline int dac_read(dac_t *obj) {
if (obj->channel == PA_4) {
if (obj->pin == PA_4) {
return (int)DAC_GetDataOutputValue(DAC_Channel_1);
}
if (obj->channel == PA_5) {
if (obj->pin == PA_5) {
return (int)DAC_GetDataOutputValue(DAC_Channel_2);
}
return 0;

View File

@ -317,10 +317,9 @@ int i2c_slave_receive(i2c_t *obj) {
uint32_t event;
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
event = I2C_GetLastEvent( i2c );
if(event != 0)
{
switch(event){
event = I2C_GetLastEvent(i2c);
if (event != 0) {
switch (event) {
case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
retValue = WriteAddressed;
break;
@ -335,22 +334,22 @@ int i2c_slave_receive(i2c_t *obj) {
break;
}
// clear ADDR
if((retValue == WriteAddressed) || (retValue == ReadAddressed)){
// clear ADDR
if ((retValue == WriteAddressed) || (retValue == ReadAddressed)) {
i2c->SR1;// read status register 1
i2c->SR2;// read status register 2
}
// clear stopf
if(I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == SET) {
if (I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == SET) {
i2c->SR1;// read status register 1
I2C_Cmd(i2c, ENABLE);
I2C_Cmd(i2c, ENABLE);
}
// clear AF
if(I2C_GetFlagStatus(i2c, I2C_FLAG_AF) == SET) {
if (I2C_GetFlagStatus(i2c, I2C_FLAG_AF) == SET) {
I2C_ClearFlag(i2c, I2C_FLAG_AF);
}
}
}
return(retValue);
return (retValue);
}
int i2c_slave_read(i2c_t *obj, char *data, int length) {

View File

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

View File

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

View File

@ -99,18 +99,23 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
// Enable USART clock
if (obj->uart == UART_1) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
obj->index = 0;
}
if (obj->uart == UART_2) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
obj->index = 1;
}
if (obj->uart == UART_3) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
obj->index = 2;
}
if (obj->uart == UART_4) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
obj->index = 3;
}
if (obj->uart == UART_5) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
obj->index = 4;
}
// Configure the UART pins
@ -125,14 +130,10 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
obj->stopbits = USART_StopBits_1;
obj->parity = USART_Parity_No;
init_usart(obj);
obj->pin_tx = tx;
obj->pin_rx = rx;
// The index is used by irq
if (obj->uart == UART_1) obj->index = 0;
if (obj->uart == UART_2) obj->index = 1;
if (obj->uart == UART_3) obj->index = 2;
if (obj->uart == UART_4) obj->index = 3;
if (obj->uart == UART_5) obj->index = 4;
init_usart(obj);
// For stdio management
if (obj->uart == STDIO_UART) {
@ -142,6 +143,37 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
}
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);
}
if (obj->uart == UART_4) {
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, DISABLE);
}
if (obj->uart == UART_5) {
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, 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;
}

View File

@ -132,6 +132,11 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
obj->cpha = SPI_CPHA_1Edge;
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
obj->mode = SPI_Mode_Master;
obj->nss = SPI_NSS_Soft;
@ -145,8 +150,30 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
}
void spi_free(spi_t *obj) {
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
SPI_I2S_DeInit(spi);
// Reset SPI and disable clock
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);
}
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) {