1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #17114 from fjmolinas/pr_xbee_ztimer_msec

drivers/xbee: migrate to ztimer_msec
This commit is contained in:
Francisco 2021-11-03 09:05:27 +01:00 committed by GitHub
commit f6012e1bcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 169 additions and 162 deletions

View File

@ -26,7 +26,6 @@
#include <stdint.h>
#include "mutex.h"
#include "xtimer.h"
#include "periph/uart.h"
#include "periph/gpio.h"
#include "net/netdev.h"

View File

@ -1,4 +1,5 @@
FEATURES_REQUIRED += periph_uart
FEATURES_REQUIRED += periph_gpio
USEMODULE += ieee802154
USEMODULE += xtimer
USEMODULE += ztimer
USEMODULE += ztimer_msec

View File

@ -23,12 +23,13 @@
#include <string.h>
#include <errno.h>
#include "xbee.h"
#include "assert.h"
#include "xtimer.h"
#include "net/eui64.h"
#include "net/netdev.h"
#include "net/ieee802154.h"
#include "net/netdev.h"
#include "timex.h"
#include "xbee.h"
#include "ztimer.h"
#ifdef MODULE_GNRC
#include "net/gnrc.h"
#endif
@ -44,16 +45,16 @@
/**
* @brief Delay when entering command mode, must be > 1s
*/
#define ENTER_CMD_MODE_DELAY (1100UL * US_PER_MS)
#define ENTER_CMD_MODE_DELAY_MS (1100UL)
/**
* @brief Delay when resetting the device, 10ms
*/
#define RESET_DELAY (10UL * US_PER_MS)
#define RESET_DELAY_MS (10UL)
/**
* @brief Timeout for receiving AT command response
*/
#define RESP_TIMEOUT_USEC (US_PER_SEC)
#define RESP_TIMEOUT_MS (1UL * MS_PER_SEC)
/**
* @brief Start delimiter in API frame mode
@ -99,6 +100,7 @@ typedef struct {
static uint8_t _cksum(size_t offset, uint8_t *buf, size_t size)
{
uint8_t res = 0xff;
for (size_t i = offset; i < size; i++) {
res -= buf[i];
}
@ -143,24 +145,22 @@ static void _api_at_cmd(xbee_t *dev, uint8_t *cmd, uint8_t size, resp_t *resp)
/* start send data */
uart_write(dev->p.uart, dev->cmd_buf, size + 6);
xtimer_ticks64_t sent_time = xtimer_now64();
ztimer_now_t sent_time = ztimer_now(ZTIMER_MSEC);
xtimer_t resp_timer;
ztimer_t resp_timer;
resp_timer.callback = isr_resp_timeout;
resp_timer.arg = dev;
xtimer_set(&resp_timer, RESP_TIMEOUT_USEC);
ztimer_set(ZTIMER_MSEC, &resp_timer, RESP_TIMEOUT_MS);
/* wait for results */
while ((dev->resp_limit != dev->resp_count) &&
(xtimer_less(
xtimer_diff32_64(xtimer_now64(), sent_time),
xtimer_ticks_from_usec(RESP_TIMEOUT_USEC)))) {
((int32_t)(sent_time + RESP_TIMEOUT_MS - ztimer_now(ZTIMER_MSEC)) > 0)) {
mutex_lock(&(dev->resp_lock));
}
xtimer_remove(&resp_timer);
ztimer_remove(ZTIMER_MSEC, &resp_timer);
if (dev->resp_limit != dev->resp_count) {
DEBUG("[xbee] api_at_cmd: response timeout\n");
@ -232,7 +232,7 @@ static void _rx_cb(void *arg, uint8_t c)
dev->rx_buf[dev->rx_count++] = c;
if (dev->rx_count == dev->rx_limit) {
/* packet is complete */
netdev_trigger_event_isr((netdev_t*) dev);
netdev_trigger_event_isr((netdev_t *)dev);
dev->int_state = XBEE_INT_STATE_IDLE;
}
break;
@ -427,13 +427,14 @@ static int _set_encryption(xbee_t *dev, const uint8_t *val)
{
uint8_t cmd[3];
resp_t resp;
/* get the current state of Encryption */
cmd[0] = 'E';
cmd[1] = 'E';
_api_at_cmd(dev, cmd, 2, &resp);
/* Prevent writing the same value in EE. */
if (val[0] != resp.data[0] ){
if (val[0] != resp.data[0]) {
cmd[0] = 'E';
cmd[1] = 'E';
cmd[2] = val[0];
@ -449,6 +450,7 @@ static int _set_encryption_key(xbee_t *dev, const uint8_t *val, size_t len)
{
uint8_t cmd[18];
resp_t resp;
if (len != 16) { /* the AES key is 128bit, 16 byte */
return -EINVAL;
}
@ -520,6 +522,7 @@ int xbee_build_hdr(xbee_t *dev, uint8_t *xhdr, size_t payload_len,
/* finally configure the packet size and copy the actual dst address */
uint16_t size = (uint16_t)(payload_len + addr_len + 3);
xhdr[1] = (uint8_t)(size >> 8);
xhdr[2] = (uint8_t)(size & 0xff);
memcpy(&xhdr[5], dst_addr, addr_len);
@ -587,19 +590,19 @@ int xbee_init(netdev_t *dev)
/* if reset pin is connected, do a hardware reset */
if (gpio_is_valid(xbee->p.pin_reset)) {
gpio_clear(xbee->p.pin_reset);
xtimer_usleep(RESET_DELAY);
ztimer_sleep(ZTIMER_MSEC, RESET_DELAY_MS);
gpio_set(xbee->p.pin_reset);
}
/* put the XBee device into command mode */
xtimer_usleep(ENTER_CMD_MODE_DELAY);
ztimer_sleep(ZTIMER_MSEC, ENTER_CMD_MODE_DELAY_MS);
_at_cmd(xbee, "+++");
xtimer_usleep(ENTER_CMD_MODE_DELAY);
ztimer_sleep(ZTIMER_MSEC, ENTER_CMD_MODE_DELAY_MS);
/* disable non IEEE802.15.4 extensions */
_at_cmd(xbee, "ATMM2\r");
/* put XBee module in "API mode without escaped characters" */
_at_cmd(xbee, "ATAP1\r");
/* disable xbee CTS and RTS, unless hardware flow control is used */
if(!IS_USED(MODULE_PERIPH_UART_HW_FC)) {
if (!IS_USED(MODULE_PERIPH_UART_HW_FC)) {
DEBUG("[xbee] init: WARNING if using an arduino BOARD + arduino xbee " \
"shield with ICSP connector, hardware flow control can't be " \
"used since CTS pin is connected to ICSP RESET pin\n");
@ -735,6 +738,7 @@ static void xbee_isr(netdev_t *netdev)
static int xbee_get(netdev_t *ndev, netopt_t opt, void *value, size_t max_len)
{
xbee_t *dev = (xbee_t *)ndev;
assert(dev);
switch (opt) {
@ -788,6 +792,7 @@ static int xbee_get(netdev_t *ndev, netopt_t opt, void *value, size_t max_len)
static int xbee_set(netdev_t *ndev, netopt_t opt, const void *value, size_t len)
{
xbee_t *dev = (xbee_t *)ndev;
assert(dev);
switch (opt) {

View File

@ -29,10 +29,12 @@ int main(void)
/* enable pktdump output */
gnrc_netreg_entry_t dump = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL,
gnrc_pktdump_pid);
gnrc_netreg_register(GNRC_NETTYPE_UNDEF, &dump);
/* start the shell */
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
return 0;

View File

@ -1,6 +1,6 @@
USEMODULE += xbee
# No need of big buffer for this test
GNRC_PKTBUF_SIZE=512
GNRC_PKTBUF_SIZE ?= 512
include ../driver_netdev_common/Makefile.netdev.mk