onewire_slave: minor, use newer GPIO definition

This commit is contained in:
King Kévin 2020-02-17 15:03:28 +01:00
parent 842b1e26bb
commit 48bc700ed7
1 changed files with 18 additions and 19 deletions

View File

@ -49,8 +49,7 @@
* @note external pull-up resistor on pin is required (< 5 kOhm), generally provided by the master
* @{
*/
#define ONEWIRE_SLAVE_PORT A /**< GPIO port */
#define ONEWIRE_SLAVE_PIN 4 /**< GPIO pin */
#define ONEWIRE_SLAVE_PIN PA4 /**< GPIO pin */
/** @} */
/** state of 1-Wire communication */
@ -144,14 +143,14 @@ void onewire_slave_setup(uint8_t family, uint64_t serial)
onewire_slave_transfer_complete = false; // reset state
// setup GPIO with external interrupt
rcc_periph_clock_enable(RCC_GPIO(ONEWIRE_SLAVE_PORT)); // enable clock for GPIO peripheral
gpio_set(GPIO(ONEWIRE_SLAVE_PORT), GPIO(ONEWIRE_SLAVE_PIN)); // idle is high (using pull-up resistor)
gpio_set_mode(GPIO(ONEWIRE_SLAVE_PORT), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(ONEWIRE_SLAVE_PIN)); // control output using open drain (this mode also allows to read the input signal)
rcc_periph_clock_enable(GPIO_RCC(ONEWIRE_SLAVE_PIN)); // enable clock for GPIO peripheral
gpio_set(GPIO_PORT(ONEWIRE_SLAVE_PIN), GPIO_PIN(ONEWIRE_SLAVE_PIN)); // idle is high (using pull-up resistor)
gpio_set_mode(GPIO_PORT(ONEWIRE_SLAVE_PIN), GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO_PIN(ONEWIRE_SLAVE_PIN)); // control output using open drain (this mode also allows to read the input signal)
rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
exti_select_source(EXTI(ONEWIRE_SLAVE_PIN), GPIO(ONEWIRE_SLAVE_PORT)); // mask external interrupt of this pin only for this port
exti_set_trigger(EXTI(ONEWIRE_SLAVE_PIN), EXTI_TRIGGER_BOTH); // trigger on signal change
exti_enable_request(EXTI(ONEWIRE_SLAVE_PIN)); // enable external interrupt
nvic_enable_irq(NVIC_EXTI_IRQ(ONEWIRE_SLAVE_PIN)); // enable interrupt
exti_select_source(GPIO_EXTI(ONEWIRE_SLAVE_PIN), GPIO_PORT(ONEWIRE_SLAVE_PIN)); // mask external interrupt of this pin only for this port
exti_set_trigger(GPIO_EXTI(ONEWIRE_SLAVE_PIN), EXTI_TRIGGER_BOTH); // trigger on signal change
exti_enable_request(GPIO_EXTI(ONEWIRE_SLAVE_PIN)); // enable external interrupt
nvic_enable_irq(GPIO_NVIC_EXTI_IRQ(ONEWIRE_SLAVE_PIN)); // enable interrupt
}
bool onewire_slave_function_read(uint8_t* data, size_t size)
@ -194,10 +193,10 @@ bool onewire_slave_function_write(const uint8_t* data, size_t size)
}
/** interrupt service routine called when 1-Wire signal changes */
void EXTI_ISR(ONEWIRE_SLAVE_PIN)(void)
void GPIO_EXTI_ISR(ONEWIRE_SLAVE_PIN)(void)
{
exti_reset_request(EXTI(ONEWIRE_SLAVE_PIN)); // reset interrupt
if (gpio_get(GPIO(ONEWIRE_SLAVE_PORT), GPIO(ONEWIRE_SLAVE_PIN))) { // it's a rising edge
exti_reset_request(GPIO_EXTI(ONEWIRE_SLAVE_PIN)); // reset interrupt
if (gpio_get(GPIO_PORT(ONEWIRE_SLAVE_PIN), GPIO_PIN(ONEWIRE_SLAVE_PIN))) { // it's a rising edge
switch (onewire_slave_state) {
case ONEWIRE_STATE_RESET: // reset pulse has ended
timer_disable_counter(TIM(ONEWIRE_SLAVE_TIMER)); // stop timer for reconfiguration
@ -235,7 +234,7 @@ void EXTI_ISR(ONEWIRE_SLAVE_PIN)(void)
case ONEWIRE_STATE_FUNCTION_WRITE: // write function data bit
timer_enable_irq(TIM(ONEWIRE_SLAVE_TIMER), TIM_DIER_CC2IE); // enable timer for reading bit
if (0 == (bits_buffer & (1 << (bits_bit % 8)))) { // need to send a 0 bit
gpio_clear(GPIO(ONEWIRE_SLAVE_PORT), GPIO(ONEWIRE_SLAVE_PIN)); // hold low to send 0 bit
gpio_clear(GPIO_PORT(ONEWIRE_SLAVE_PIN), GPIO_PIN(ONEWIRE_SLAVE_PIN)); // hold low to send 0 bit
}
break;
case ONEWIRE_STATE_IDLE: // we only expect a reset
@ -252,7 +251,7 @@ void EXTI_ISR(ONEWIRE_SLAVE_PIN)(void)
void TIM_ISR(ONEWIRE_SLAVE_TIMER)(void)
{
if (timer_interrupt_source(TIM(ONEWIRE_SLAVE_TIMER), TIM_SR_UIF)) { // reset timer triggered, verify if it's a reset
if (0 == gpio_get(GPIO(ONEWIRE_SLAVE_PORT), GPIO(ONEWIRE_SLAVE_PIN))) { // signal it still low, thus it must be a reset
if (0 == gpio_get(GPIO_PORT(ONEWIRE_SLAVE_PIN), GPIO_PIN(ONEWIRE_SLAVE_PIN))) { // signal it still low, thus it must be a reset
onewire_slave_state = ONEWIRE_STATE_RESET; // update state
}
timer_disable_counter(TIM(ONEWIRE_SLAVE_TIMER)); // stop timer since there is nothing more to measure
@ -263,7 +262,7 @@ void TIM_ISR(ONEWIRE_SLAVE_TIMER)(void)
timer_clear_flag(TIM(ONEWIRE_SLAVE_TIMER), TIM_SR_CC1IF); // clear flag
if (ONEWIRE_STATE_WAIT_PRESENCE == onewire_slave_state) { // we can now send the pulse
onewire_slave_state = ONEWIRE_STATE_PULSE_PRESENCE; // save new state
gpio_clear(GPIO(ONEWIRE_SLAVE_PORT), GPIO(ONEWIRE_SLAVE_PIN)); // send presence pulse (will also trigger the timer start)
gpio_clear(GPIO_PORT(ONEWIRE_SLAVE_PIN), GPIO_PIN(ONEWIRE_SLAVE_PIN)); // send presence pulse (will also trigger the timer start)
}
}
if (timer_interrupt_source(TIM(ONEWIRE_SLAVE_TIMER), TIM_SR_CC2IF)) { // time to read the bit, or stop writing it
@ -274,7 +273,7 @@ void TIM_ISR(ONEWIRE_SLAVE_TIMER)(void)
case ONEWIRE_STATE_FUNCTION_COMMAND: // read function command code bit
case ONEWIRE_STATE_ROM_SEARCH_SELECT: // read selected ROM code bit
case ONEWIRE_STATE_FUNCTION_READ: // read function data bit
if (gpio_get(GPIO(ONEWIRE_SLAVE_PORT), GPIO(ONEWIRE_SLAVE_PIN))) { // bit is set to 1
if (gpio_get(GPIO_PORT(ONEWIRE_SLAVE_PIN), GPIO_PIN(ONEWIRE_SLAVE_PIN))) { // bit is set to 1
bits_buffer |= (1 << (bits_bit % 8)); // set bit
} else { // bit is set to 0
bits_buffer &= ~(1 << (bits_bit % 8)); // clear bit
@ -283,12 +282,12 @@ void TIM_ISR(ONEWIRE_SLAVE_TIMER)(void)
break;
case ONEWIRE_STATE_ROM_READ: // write ROM code
case ONEWIRE_STATE_FUNCTION_WRITE: // write function data bit
gpio_set(GPIO(ONEWIRE_SLAVE_PORT), GPIO(ONEWIRE_SLAVE_PIN)); // stop sending bit
gpio_set(GPIO_PORT(ONEWIRE_SLAVE_PIN), GPIO_PIN(ONEWIRE_SLAVE_PIN)); // stop sending bit
bits_bit++; // go to next bit
break;
case ONEWIRE_STATE_ROM_SEARCH_TRUE: // ROM code bit is sent
case ONEWIRE_STATE_ROM_SEARCH_FALSE: // ROM code bit is sent
gpio_set(GPIO(ONEWIRE_SLAVE_PORT), GPIO(ONEWIRE_SLAVE_PIN)); // stop sending bit
gpio_set(GPIO_PORT(ONEWIRE_SLAVE_PIN), GPIO_PIN(ONEWIRE_SLAVE_PIN)); // stop sending bit
break;
default: // these states don't need read/write
break;
@ -402,7 +401,7 @@ void TIM_ISR(ONEWIRE_SLAVE_TIMER)(void)
if (timer_interrupt_source(TIM(ONEWIRE_SLAVE_TIMER), TIM_SR_CC3IF)) { // end of presence pulse timer triggered
timer_clear_flag(TIM(ONEWIRE_SLAVE_TIMER), TIM_SR_CC3IF); // clear flag
if (ONEWIRE_STATE_PULSE_PRESENCE == onewire_slave_state) {
gpio_set(GPIO(ONEWIRE_SLAVE_PORT), GPIO(ONEWIRE_SLAVE_PIN)); // stop sending presence pulse
gpio_set(GPIO_PORT(ONEWIRE_SLAVE_PIN), GPIO_PIN(ONEWIRE_SLAVE_PIN)); // stop sending presence pulse
// if the pin stays low the reset timer will catch it
}
}