[NUCLEO_F103RB] Update xxx_free() function + typo corrections

pull/324/head
bcostm 2014-05-23 11:00:26 +02:00
parent 10e3a9cea1
commit 1bbd1fda37
5 changed files with 67 additions and 17 deletions

View File

@ -181,7 +181,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
int timeout;
int count;
i2c_start(obj);
// Send slave address for write
@ -245,7 +245,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
I2C_SendData(i2c, (uint8_t)data);
// Wait until the byte is transmitted
timeout = FLAG_TIMEOUT;
timeout = FLAG_TIMEOUT;
while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
(I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
timeout--;
@ -299,10 +299,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;
@ -317,24 +316,24 @@ int i2c_slave_receive(i2c_t *obj) {
break;
}
// clear ADDR
if((retValue == WriteAddressed) || (retValue == ReadAddressed)){
// clear ADDR
if ((retValue == WriteAddressed) || (retValue == ReadAddressed)) {
// read SR to clear ADDR flag
i2c->SR1;
i2c->SR2;
}
// clear stopf
if(I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == SET) {
if (I2C_GetFlagStatus(i2c, I2C_FLAG_STOPF) == SET) {
// read SR1 and write CR1 to clear STOP flag
i2c->SR1;
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

@ -65,6 +65,8 @@ struct serial_s {
uint32_t databits;
uint32_t stopbits;
uint32_t parity;
PinName pin_tx;
PinName pin_rx;
};
struct spi_s {
@ -75,6 +77,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

@ -98,8 +98,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 GPIO
pin_function(obj->pin, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
}
void pwmout_write(pwmout_t* obj, float value) {

View File

@ -113,6 +113,9 @@ void serial_init(serial_t *obj, PinName tx, PinName rx) {
obj->stopbits = USART_StopBits_1;
obj->parity = USART_Parity_No;
obj->pin_tx = tx;
obj->pin_rx = rx;
init_usart(obj);
// The index is used by irq
@ -128,6 +131,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) {
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_FLOATING, 0));
pin_function(obj->pin_rx, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
serial_irq_ids[obj->index] = 0;
}

View File

@ -119,6 +119,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;
@ -132,8 +137,24 @@ 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);
}
// Configure GPIOs
pin_function(obj->pin_miso, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
pin_function(obj->pin_mosi, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
pin_function(obj->pin_sclk, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
pin_function(obj->pin_ssel, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
}
void spi_format(spi_t *obj, int bits, int mode, int slave) {