1 #include <twr_td1207r.h>
3 #define TWR_TD1207R_DELAY_RUN 100
4 #define TWR_TD1207R_DELAY_INITIALIZATION_RESET_H 100
5 #define TWR_TD1207R_DELAY_INITIALIZATION_AT_COMMAND 3000
6 #define TWR_TD1207R_DELAY_INITIALIZATION_AT_RESPONSE 100
7 #define TWR_TD1207R_DELAY_SEND_RF_FRAME_RESPONSE 8000
9 static void _twr_td1207r_task(
void *param);
15 memset(
self, 0,
sizeof(*
self));
17 self->_reset_signal = reset_signal;
18 self->_uart_channel = uart_channel;
24 twr_fifo_init(&self->_tx_fifo, self->_tx_fifo_buffer,
sizeof(self->_tx_fifo_buffer));
25 twr_fifo_init(&self->_rx_fifo, self->_rx_fifo_buffer,
sizeof(self->_rx_fifo_buffer));
33 self->_state = TWR_TD1207R_STATE_INITIALIZE;
38 self->_event_handler = event_handler;
39 self->_event_param = event_param;
44 return self->_state == TWR_TD1207R_STATE_READY ? true :
false;
54 self->_message_length = length;
56 memcpy(self->_message_buffer, buffer, self->_message_length);
58 self->_state = TWR_TD1207R_STATE_SEND_RF_FRAME_COMMAND;
65 static void _twr_td1207r_task(
void *param)
73 case TWR_TD1207R_STATE_READY:
75 if (self->_event_handler != NULL)
82 case TWR_TD1207R_STATE_ERROR:
84 if (self->_event_handler != NULL)
89 self->_state = TWR_TD1207R_STATE_INITIALIZE;
93 case TWR_TD1207R_STATE_INITIALIZE:
95 self->_state = TWR_TD1207R_STATE_INITIALIZE_RESET_L;
99 case TWR_TD1207R_STATE_INITIALIZE_RESET_L:
103 self->_state = TWR_TD1207R_STATE_INITIALIZE_RESET_H;
109 case TWR_TD1207R_STATE_INITIALIZE_RESET_H:
113 self->_state = TWR_TD1207R_STATE_INITIALIZE_AT_COMMAND;
119 case TWR_TD1207R_STATE_INITIALIZE_AT_COMMAND:
121 self->_state = TWR_TD1207R_STATE_ERROR;
125 strcpy(self->_command,
"\rAT\r");
127 size_t length = strlen(self->_command);
134 self->_state = TWR_TD1207R_STATE_INITIALIZE_AT_RESPONSE;
140 case TWR_TD1207R_STATE_INITIALIZE_AT_RESPONSE:
142 self->_state = TWR_TD1207R_STATE_ERROR;
144 if (!_twr_td1207r_read_response(
self))
149 if (strcmp(self->_response, self->_command + 1) != 0)
154 if (!_twr_td1207r_read_response(
self))
159 if (strcmp(self->_response,
"OK\r") != 0)
164 self->_state = TWR_TD1207R_STATE_READY;
168 case TWR_TD1207R_STATE_SEND_RF_FRAME_COMMAND:
170 self->_state = TWR_TD1207R_STATE_ERROR;
172 static const char *hex_lookup_table[] =
174 "00",
"01",
"02",
"03",
"04",
"05",
"06",
"07",
"08",
"09",
"0A",
"0B",
"0C",
"0D",
"0E",
"0F",
175 "10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"1A",
"1B",
"1C",
"1D",
"1E",
"1F",
176 "20",
"21",
"22",
"23",
"24",
"25",
"26",
"27",
"28",
"29",
"2A",
"2B",
"2C",
"2D",
"2E",
"2F",
177 "30",
"31",
"32",
"33",
"34",
"35",
"36",
"37",
"38",
"39",
"3A",
"3B",
"3C",
"3D",
"3E",
"3F",
178 "40",
"41",
"42",
"43",
"44",
"45",
"46",
"47",
"48",
"49",
"4A",
"4B",
"4C",
"4D",
"4E",
"4F",
179 "50",
"51",
"52",
"53",
"54",
"55",
"56",
"57",
"58",
"59",
"5A",
"5B",
"5C",
"5D",
"5E",
"5F",
180 "60",
"61",
"62",
"63",
"64",
"65",
"66",
"67",
"68",
"69",
"6A",
"6B",
"6C",
"6D",
"6E",
"6F",
181 "70",
"71",
"72",
"73",
"74",
"75",
"76",
"77",
"78",
"79",
"7A",
"7B",
"7C",
"7D",
"7E",
"7F",
182 "80",
"81",
"82",
"83",
"84",
"85",
"86",
"87",
"88",
"89",
"8A",
"8B",
"8C",
"8D",
"8E",
"8F",
183 "90",
"91",
"92",
"93",
"94",
"95",
"96",
"97",
"98",
"99",
"9A",
"9B",
"9C",
"9D",
"9E",
"9F",
184 "A0",
"A1",
"A2",
"A3",
"A4",
"A5",
"A6",
"A7",
"A8",
"A9",
"AA",
"AB",
"AC",
"AD",
"AE",
"AF",
185 "B0",
"B1",
"B2",
"B3",
"B4",
"B5",
"B6",
"B7",
"B8",
"B9",
"BA",
"BB",
"BC",
"BD",
"BE",
"BF",
186 "C0",
"C1",
"C2",
"C3",
"C4",
"C5",
"C6",
"C7",
"C8",
"C9",
"CA",
"CB",
"CC",
"CD",
"CE",
"CF",
187 "D0",
"D1",
"D2",
"D3",
"D4",
"D5",
"D6",
"D7",
"D8",
"D9",
"DA",
"DB",
"DC",
"DD",
"DE",
"DF",
188 "E0",
"E1",
"E2",
"E3",
"E4",
"E5",
"E6",
"E7",
"E8",
"E9",
"EA",
"EB",
"EC",
"ED",
"EE",
"EF",
189 "F0",
"F1",
"F2",
"F3",
"F4",
"F5",
"F6",
"F7",
"F8",
"F9",
"FA",
"FB",
"FC",
"FD",
"FE",
"FF"
192 strcpy(self->_command,
"AT$SF=");
194 for (
size_t i = 0; i <
self->_message_length; i++)
196 strcat(self->_command, hex_lookup_table[*((uint8_t *) self->_message_buffer + i)]);
199 strcat(self->_command,
"\r");
201 size_t length = strlen(self->_command);
208 self->_state = TWR_TD1207R_STATE_SEND_RF_FRAME_RESPONSE;
210 if (self->_event_handler != NULL)
219 case TWR_TD1207R_STATE_SEND_RF_FRAME_RESPONSE:
221 self->_state = TWR_TD1207R_STATE_ERROR;
223 if (!_twr_td1207r_read_response(
self))
228 if (strcmp(self->_response, self->_command) != 0)
233 if (!_twr_td1207r_read_response(
self))
238 if (strcmp(self->_response,
"OK\r") != 0)
243 self->_state = TWR_TD1207R_STATE_READY;
245 if (self->_event_handler != NULL)
273 if (rx_character ==
'\n')
278 self->_response[length++] = rx_character;
280 if (rx_character ==
'\r')
289 self->_response[length] =
'\0';
294 if (length ==
sizeof(self->_response) - 1)
void twr_fifo_init(twr_fifo_t *fifo, void *buffer, size_t size)
Initialize FIFO buffer.
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_scheduler_plan_current_from_now(twr_tick_t tick)
Schedule current task to tick relative from now.
void twr_scheduler_plan_now(twr_scheduler_task_id_t task_id)
Schedule specified task for immediate execution.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
bool twr_td1207r_send_rf_frame(twr_td1207r_t *self, const void *buffer, size_t length)
Send RF frame command.
bool twr_td1207r_is_ready(twr_td1207r_t *self)
Check if modem is ready for commands.
void twr_td1207r_init(twr_td1207r_t *self, twr_gpio_channel_t reset_signal, twr_uart_channel_t uart_channel)
Initialize TD1207R.
void twr_td1207r_set_event_handler(twr_td1207r_t *self, void(*event_handler)(twr_td1207r_t *, twr_td1207r_event_t, void *), void *event_param)
Set callback function.
twr_td1207r_event_t
Callback events.
struct twr_td1207r_t twr_td1207r_t
TD1207R instance.
@ TWR_TD1207R_EVENT_SEND_RF_FRAME_DONE
RF frame transmission finished event.
@ TWR_TD1207R_EVENT_READY
Ready event.
@ TWR_TD1207R_EVENT_SEND_RF_FRAME_START
RF frame transmission started event.
@ TWR_TD1207R_EVENT_ERROR
Error event.
#define TWR_TICK_INFINITY
Maximum timestamp value.
twr_uart_channel_t
UART channels.
void twr_uart_init(twr_uart_channel_t channel, twr_uart_baudrate_t baudrate, twr_uart_setting_t setting)
Initialize UART channel.
size_t twr_uart_async_write(twr_uart_channel_t channel, const void *buffer, size_t length)
Add data to be transmited in async mode.
void twr_uart_set_async_fifo(twr_uart_channel_t channel, twr_fifo_t *write_fifo, twr_fifo_t *read_fifo)
Set buffers for async transfers.
bool twr_uart_async_read_start(twr_uart_channel_t channel, twr_tick_t timeout)
Start async reading.
size_t twr_uart_async_read(twr_uart_channel_t channel, void *buffer, size_t length)
Get data that has been received in async mode.
@ TWR_UART_BAUDRATE_9600
UART baudrat 9600 bps.
@ TWR_UART_SETTING_8N1
8N1: 8 data bits, none parity bit, 1 stop bit