Skip to content

[NUCLEO_F103RB] Update PWM IOs used + I2C cleanup #126

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 1 commit into from
Dec 13, 2013
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 @@ -27,20 +27,13 @@
#define FLAG_TIMEOUT ((int)0x1000)
#define LONG_TIMEOUT ((int)0x8000)

// Functions exit codes
#define EXIT_OK (0)
#define EXIT_FAIL (1)
#define EXIT_TIMEOUT (0xFFFFFFFF)

static const PinMap PinMap_I2C_SDA[] = {
//{PB_7, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 0)}, // Cannot be used due to TIM4
{PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 7)}, // GPIO_Remap_I2C1
{PB_9, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 8)}, // GPIO_Remap_I2C1
{NC, NC, 0}
};

static const PinMap PinMap_I2C_SCL[] = {
//{PB_6, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 0)}, // // Cannot be used due to TIM4
{PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 7)}, // GPIO_Remap_I2C1
{PB_8, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 8)}, // GPIO_Remap_I2C1
{NC, NC, 0}
};

Expand Down Expand Up @@ -107,17 +100,19 @@ inline int i2c_start(i2c_t *obj) {
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) {
if ((timeout--) == 0) {
return EXIT_TIMEOUT;
return 1;
}
}

return EXIT_OK;
return 0;
}

inline int i2c_stop(i2c_t *obj) {
I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);

I2C_GenerateSTOP(i2c, ENABLE);
return EXIT_OK;

return 0;
}

int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
Expand All @@ -133,7 +128,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
timeout = LONG_TIMEOUT;
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
if ((timeout--) == 0) {
return EXIT_TIMEOUT;
return 0;
}
}
*/
Expand All @@ -147,7 +142,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
timeout = FLAG_TIMEOUT;
while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) {
if ((timeout--) == 0) {
return EXIT_TIMEOUT;
return 0;
}
}

Expand Down Expand Up @@ -182,7 +177,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
timeout = LONG_TIMEOUT;
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
if ((timeout--) == 0) {
return EXIT_TIMEOUT;
return 0;
}
}
*/
Expand All @@ -196,13 +191,14 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
timeout = FLAG_TIMEOUT;
while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) {
if ((timeout--) == 0) {
return EXIT_TIMEOUT;
return 0;
}
}

for (count = 0; count < length; count++) {
if (i2c_byte_write(obj, data[count]) != EXIT_OK) {
return EXIT_FAIL;
if (i2c_byte_write(obj, data[count]) != 1) {
i2c_stop(obj);
return 0;
}
}

Expand Down Expand Up @@ -231,7 +227,7 @@ int i2c_byte_read(i2c_t *obj, int last) {
timeout = FLAG_TIMEOUT;
while (I2C_GetFlagStatus(i2c, I2C_FLAG_RXNE) == RESET) {
if ((timeout--) == 0) {
return EXIT_TIMEOUT;
return 0;
}
}

Expand All @@ -252,11 +248,11 @@ int i2c_byte_write(i2c_t *obj, int data) {
while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
(I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
if ((timeout--) == 0) {
return EXIT_TIMEOUT;
return 0;
}
}

return EXIT_OK;
return 1;
}

void i2c_reset(i2c_t *obj) {
Expand Down Expand Up @@ -290,29 +286,37 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
// Nothing to do
}

#define NoData 0
#define ReadAddressed 1
#define WriteGeneral 2
#define WriteAddressed 3
// See I2CSlave.h
#define NoData 0 // the slave has not been addressed
#define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
#define WriteGeneral 2 // the master is writing to all slave
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)

int i2c_slave_receive(i2c_t *obj) {
//I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
int retval = NoData;
//int status;

//if (I2C_GetFlagStatus(i2c, I2C_FLAG_GENCALL) == SET) retval = WriteGeneral;

//status = I2C_GetLastEvent(i2c);

return(retval);
// TO BE DONE
return(0);
}

int i2c_slave_read(i2c_t *obj, char *data, int length) {
return 0;
int count = 0;

// Read all bytes
for (count = 0; count < length; count++) {
data[count] = i2c_byte_read(obj, 0);
}

return count;
}

int i2c_slave_write(i2c_t *obj, const char *data, int length) {
return 0;
int count = 0;

// Write all bytes
for (count = 0; count < length; count++) {
i2c_byte_write(obj, data[count]);
}

return count;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

// Alternate-function mapping
static const uint32_t AF_mapping[] = {
0, // 0 = No AF
GPIO_Remap_SPI1, // 1
GPIO_Remap_I2C1, // 2
GPIO_Remap_USART1, // 3
GPIO_Remap_USART2, // 4
GPIO_FullRemap_TIM2, // 5
GPIO_FullRemap_TIM3, // 6
GPIO_Remap_I2C1 // 7
0, // 0 = No AF
GPIO_Remap_SPI1, // 1
GPIO_Remap_I2C1, // 2
GPIO_Remap_USART1, // 3
GPIO_Remap_USART2, // 4
GPIO_FullRemap_TIM2, // 5
GPIO_FullRemap_TIM3, // 6
GPIO_PartialRemap_TIM3, // 7
GPIO_Remap_I2C1 // 8
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@

// Only TIM2 and TIM3 can be used (TIM1 and TIM4 are used by the us_ticker)
static const PinMap PinMap_PWM[] = {
// TIM2
{PA_2, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH3 - ARDUINO D1 (extra)
{PA_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH4 - ARDUINO D0 (extra)
// TIM2 remap
{PB_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2r_CH2 - ARDUINO D3
{PB_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2r_CH3 - ARDUINO D6
// TIM3
{PA_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH1 - ARDUINO D12 (extra)
{PA_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH2 - ARDUINO D11
// TIM3 remap
{PB_4, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3r_CH1 - ARDUINO D5
{PC_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3r_CH2 - ARDUINO D9
// TIM2 default
//{PA_2, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH3 - ARDUINO D1
//{PA_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM2_CH4 - ARDUINO D0
// TIM2 full remap
{PB_3, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2fr_CH2 - ARDUINO D3
//{PB_10, PWM_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 5)}, // TIM2fr_CH3 - ARDUINO D6
// TIM3 default
//{PA_6, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH1 - ARDUINO D12
//{PA_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)}, // TIM3_CH2 - ARDUINO D11
// TIM3 full remap
//{PC_7, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 6)}, // TIM3fr_CH2 - ARDUINO D9
// TIM3 partial remap
{PB_4, PWM_3, STM_PIN_DATA(GPIO_Mode_AF_PP, 7)}, // TIM3pr_CH1 - ARDUINO D5
{NC, NC, 0}
};

Expand Down Expand Up @@ -83,29 +84,29 @@ void pwmout_write(pwmout_t* obj, float value) {
TIM_OCInitStructure.TIM_Pulse = obj->pulse;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

// TIM Channel 1
if ((obj->pin == PA_6) || (obj->pin == PB_4)) {
// Configure channel 1
if (obj->pin == PB_4) {
TIM_OC1PreloadConfig(tim, TIM_OCPreload_Enable);
TIM_OC1Init(tim, &TIM_OCInitStructure);
}

// TIM Channel 2
if ((obj->pin == PA_7) || (obj->pin == PB_3) || (obj->pin == PC_7)) {
// Configure channel 2
if (obj->pin == PB_3) {
TIM_OC2PreloadConfig(tim, TIM_OCPreload_Enable);
TIM_OC2Init(tim, &TIM_OCInitStructure);
}

// TIM Channel 3
if ((obj->pin == PA_2) || (obj->pin == PB_10)) {
TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable);
TIM_OC3Init(tim, &TIM_OCInitStructure);
}

// TIM Channel 4
if (obj->pin == PA_3) {
TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable);
TIM_OC4Init(tim, &TIM_OCInitStructure);
}
// Configure channel 3
//if (obj->pin == PB_10) {
// TIM_OC3PreloadConfig(tim, TIM_OCPreload_Enable);
// TIM_OC3Init(tim, &TIM_OCInitStructure);
//}

// Configure channel 4
//if (obj->pin == PA_3) {
// TIM_OC4PreloadConfig(tim, TIM_OCPreload_Enable);
// TIM_OC4Init(tim, &TIM_OCInitStructure);
//}
}

float pwmout_read(pwmout_t* obj) {
Expand Down