1 #include <twr_rf_ook.h>
4 void _twr_rf_ook_irq_TIM3_handler(
void *param);
8 volatile uint16_t packet_bit_position;
10 uint8_t packet_data[128];
11 volatile bool is_busy;
12 uint32_t bit_length_us;
16 static int _twr_rf_ook_char_to_int(
char input)
18 if(input >=
'0' && input <=
'9')
22 else if(input >=
'A' && input <=
'F')
24 return input -
'A' + 10;
26 else if(input >=
'a' && input <=
'f')
28 return input -
'a' + 10;
36 static uint8_t _twr_rf_ook_string_to_array(
char *str, uint8_t *array)
38 uint8_t array_length = (strlen(str) + 1) / 2;
40 for(
int i = 0; i < array_length; i++)
42 array[i] = _twr_rf_ook_char_to_int(str[i*2]) * 16;
44 if(!(i == array_length - 1 && strlen(str) % 2 == 1))
46 array[i] += _twr_rf_ook_char_to_int(str[i*2 + 1]);
53 static void _twr_rf_ook_tim3_configure(uint32_t resolution_us, uint32_t period_cycles)
56 RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
62 TIM3->CR1 &= ~TIM_CR1_CEN;
65 TIM3->PSC = resolution_us * 32 - 1;
66 TIM3->ARR = period_cycles - 1;
68 TIM3->DIER |= TIM_DIER_UIE;
70 NVIC_EnableIRQ(TIM3_IRQn);
75 memset(&_twr_rf_ook, 0,
sizeof(_twr_rf_ook));
77 _twr_rf_ook.gpio = gpio;
91 _twr_rf_ook.bit_length_us = 1e6 / bitrate;
96 _twr_rf_ook.bit_length_us = bit_length_us;
101 if ((TIM3->CR1 & TIM_CR1_CEN) != 0)
107 if (length >
sizeof(_twr_rf_ook.packet_data))
113 twr_system_pll_enable();
115 _twr_rf_ook_tim3_configure(1, _twr_rf_ook.bit_length_us);
117 _twr_rf_ook.packet_length = length;
118 _twr_rf_ook.packet_bit_position = 0;
120 memcpy(_twr_rf_ook.packet_data, packet, _twr_rf_ook.packet_length);
124 _twr_rf_ook.is_busy =
true;
126 TIM3->CR1 |= TIM_CR1_CEN;
133 uint8_t packet_length = _twr_rf_ook_string_to_array(hex_string, _twr_rf_ook.packet_data);
140 return _twr_rf_ook.is_busy;
145 if ((TIM3->CR1 & TIM_CR1_CEN) != 0)
151 return _twr_rf_ook.is_busy;
154 void _twr_rf_ook_irq_TIM3_handler(
void *param)
157 TIM3->SR = ~TIM_DIER_UIE;
159 uint8_t byte_index = _twr_rf_ook.packet_bit_position / 8;
160 uint8_t bit_index = _twr_rf_ook.packet_bit_position % 8;
162 if(_twr_rf_ook.packet_bit_position / 8 == _twr_rf_ook.packet_length)
168 TIM3->CR1 &= ~TIM_CR1_CEN;
169 _twr_rf_ook.is_busy =
false;
171 twr_system_pll_disable();
176 twr_gpio_set_output(_twr_rf_ook.gpio, _twr_rf_ook.packet_data[byte_index] & (1 << (7 - bit_index)));
178 _twr_rf_ook.packet_bit_position++;
void twr_gpio_set_output(twr_gpio_channel_t channel, int state)
Set output state for GPIO channel.
void twr_gpio_init(twr_gpio_channel_t channel)
Initialize GPIO channel.
twr_gpio_channel_t
GPIO channels.
void twr_gpio_set_mode(twr_gpio_channel_t channel, twr_gpio_mode_t mode)
Set mode of operation for GPIO channel.
@ TWR_GPIO_MODE_OUTPUT
GPIO channel operates as output.
void twr_rf_ook_set_bitrate(uint32_t bitrate)
Configure OOK bitrate.
bool twr_rf_ook_is_busy()
Data sending in progress.
bool twr_rf_ook_send_hex_string(char *hex_string)
Send data with data in hex string.
bool twr_rf_ook_is_ready()
Data can be send.
void twr_rf_ook_set_bitlength(uint32_t bit_length_us)
Configure OOK bitrate.
bool twr_rf_ook_send(uint8_t *packet, uint8_t length)
Send data.
void twr_rf_ook_init(twr_gpio_channel_t gpio)
Initialize RF OOK library.
bool twr_timer_set_irq_handler(TIM_TypeDef *tim, void(*irq_handler)(void *), void *irq_param)
Register timer IRQ handler.