Skip to content
Open
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
5 changes: 4 additions & 1 deletion port/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ int main(int argc, char **argv)
}

/* printf("netsrv: %zu interface%s\n", have_intfs, have_intfs == 1 ? "" : "s"); */
if (!have_intfs)
#if !LWIP_WIFI
if (have_intfs == 0) {
exit(1);
}
#endif

#if LWIP_IPSEC
ipsecdev_attach(LWIP_IPSEC_DEV);
Expand Down
2 changes: 1 addition & 1 deletion wi-fi/hal/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

NAME := wifi-hal

LOCAL_SRCS := cyhal_gpio.c cyhal_sdio.c cyhal_utils.c cyabs_rtos.c cy_log.c
LOCAL_SRCS := cyhal_gpio.c cyhal_sdio.c cyhal_usb.c cyhal_utils.c cyabs_rtos.c cy_log.c

LOCAL_CFLAGS += -Idrivers

Expand Down
1 change: 1 addition & 0 deletions wi-fi/hal/cy_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ typedef enum {
CYLF_GPIO, /**< GPIO Facility */
CYLF_SDIO, /**< SDIO Facility */
CYLF_MIDDLEWARE, /**< Middleware Facility */
CYLF_USB, /**< USB Facility */

CYLF_MAX /**< Must be last, not an actual index */
} CY_LOG_FACILITY_T;
Expand Down
111 changes: 107 additions & 4 deletions wi-fi/hal/cyabs_rtos.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>


cy_rslt_t cy_rtos_create_thread(cy_thread_t *thread, cy_thread_entry_fn_t entry_function,
const char *name, void *stack, uint32_t stack_size,
cy_thread_priority_t priority, cy_thread_arg_t arg)
const char *name, void *stack, uint32_t stack_size,
cy_thread_priority_t priority, cy_thread_arg_t arg)
{
cy_log_msg(CYLF_RTOS, CY_LOG_DEBUG, "cy_rtos_create_thread (thread=%p name=%s stack=%p stack_size=%u priority=%d)\n",
thread, name, stack, stack_size, priority);
thread, name, stack, stack_size, priority);

if (thread == NULL)
return CY_RTOS_BAD_PARAM;
Expand Down Expand Up @@ -142,7 +143,7 @@ cy_rslt_t cy_rtos_deinit_mutex(cy_mutex_t *mutex)
cy_rslt_t cy_rtos_init_semaphore(cy_semaphore_t *semaphore, uint32_t maxcount, uint32_t initcount)
{
cy_log_msg(CYLF_RTOS, CY_LOG_DEBUG, "cy_rtos_init_semaphore (semaphore=%p maxcount=%u initcount=%u)\n",
semaphore, maxcount, initcount);
semaphore, maxcount, initcount);

if (semaphore == NULL)
return CY_RTOS_BAD_PARAM;
Expand Down Expand Up @@ -265,3 +266,105 @@ cy_rslt_t cy_rtos_delay_milliseconds(cy_time_t num_ms)

return CY_RSLT_SUCCESS;
}


static inline unsigned cy_rtos_queue_increment(cy_queue_t *queue, unsigned index)
{
return (index + 1) & (queue->length - 1);
}


static inline bool cy_rtos_queue_is_power_of_2(size_t n)
{
return (n & (n - 1)) == 0;
}


/* preconditions:
* - length must be a power of 2
* - if queue->data != NULL, it should point to an array of at least length * itemsize bytes
*/
cy_rslt_t cy_rtos_queue_init(cy_queue_t *queue, size_t length, size_t itemsize)
{
if (queue == NULL || !cy_rtos_queue_is_power_of_2(length) || length == 0 || itemsize == 0) {
return CY_RTOS_BAD_PARAM;
}

queue->data = malloc(length * itemsize);
if (queue->data == NULL) {
return CY_RTOS_NO_MEMORY;
}

queue->length = length;
queue->itemsz = itemsize;
atomic_init(&queue->head, 0);
atomic_init(&queue->tail, 0);

return CY_RSLT_SUCCESS;
}


static inline void *cy_rtos_queue_get_at(cy_queue_t *queue, unsigned int index)
{
return (void *)((uintptr_t)queue->data + (index * queue->itemsz));
}


cy_rslt_t cy_rtos_queue_put(cy_queue_t *queue, const void *item_ptr)
{
const unsigned current_head = atomic_load_explicit(&queue->head, memory_order_relaxed);
const unsigned next_head = cy_rtos_queue_increment(queue, current_head);
if (next_head == atomic_load_explicit(&queue->tail, memory_order_acquire)) {
return CY_RTOS_QUEUE_FULL;
}

(void)memcpy(cy_rtos_queue_get_at(queue, current_head), item_ptr, queue->itemsz);
atomic_store_explicit(&queue->head, next_head, memory_order_release);
return CY_RSLT_SUCCESS;
}


cy_rslt_t cy_rtos_queue_get(cy_queue_t *queue, void *item_ptr)
{
const unsigned current_tail = atomic_load_explicit(&queue->tail, memory_order_relaxed);
if (current_tail == atomic_load_explicit(&queue->head, memory_order_acquire)) {
return CY_RTOS_QUEUE_EMPTY;
}

(void)memcpy(item_ptr, cy_rtos_queue_get_at(queue, current_tail), queue->itemsz);
atomic_store_explicit(&queue->tail, cy_rtos_queue_increment(queue, current_tail), memory_order_release);
return CY_RSLT_SUCCESS;
}


cy_rslt_t cy_rtos_queue_count(cy_queue_t *queue, size_t *num_waiting)
{
*num_waiting = (atomic_load(&queue->head) - atomic_load(&queue->tail)) & (queue->length - 1);
return CY_RSLT_SUCCESS;
}


cy_rslt_t cy_rtos_queue_space(cy_queue_t *queue, size_t *num_spaces)
{
size_t num_waiting;
/* cy_rtos_queue_count cannot error */
(void)cy_rtos_queue_count(queue, &num_waiting);
*num_spaces = (queue->length - 1) - num_waiting;
return CY_RSLT_SUCCESS;
}


cy_rslt_t cy_rtos_queue_reset(cy_queue_t *queue)
{
atomic_store(&queue->head, 0);
atomic_store(&queue->tail, 0);
return CY_RSLT_SUCCESS;
}


cy_rslt_t cy_rtos_queue_deinit(cy_queue_t *queue)
{
free(queue->data);
queue->data = NULL;
return CY_RSLT_SUCCESS;
}
118 changes: 111 additions & 7 deletions wi-fi/hal/cyabs_rtos.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ extern "C" {

/** \} group_abstraction_rtos_common */

/**
* \ingroup group_abstraction_rtos_queue
* \{
*/

/** The Queue is already full and can't accept any more items at this time */
#define CY_RTOS_QUEUE_FULL \
CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_OS, 3)
/** The Queue is empty and has nothing to remove */
#define CY_RTOS_QUEUE_EMPTY \
CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_OS, 4)

/** \} group_abstraction_rtos_queue */

/********************************************* TYPES **********************************************/

/**
Expand All @@ -124,13 +138,6 @@ typedef enum cy_thread_state {
*/
typedef void (*cy_thread_entry_fn_t)(cy_thread_arg_t arg);

typedef struct {
handle_t mutex;
handle_t cond;
volatile unsigned int v;
unsigned int m;
} cy_semaphore_t;


/********************************************* Threads ********************************************/

Expand Down Expand Up @@ -469,6 +476,103 @@ cy_rslt_t cy_rtos_delay_milliseconds(cy_time_t num_ms);

/** \} group_abstraction_rtos_time */


/********************************************* Queue **********************************************/
/**
* \ingroup group_abstraction_rtos_queue
* \{
*/

/** Create a queue.
*
* This is a queue of data where entries are placed on the back of the queue
* and removed from the front of the queue.
*
* @param[out] queue Pointer to the queue handle
* @param[in] length The maximum length of the queue in items
* @param[in] itemsize The size of each item in the queue.
*
* @return The status of the init request. [\ref CY_RSLT_SUCCESS, \ref CY_RTOS_NO_MEMORY, \ref
* CY_RTOS_GENERAL_ERROR]
*/
cy_rslt_t cy_rtos_queue_init(cy_queue_t *queue, size_t length, size_t itemsize);

/** Put an item in a queue.
*
* This function puts an item in the queue. The item is copied
* into the queue using a memory copy and the data pointed to by item_ptr
* is no longer referenced once the call returns.
*
* @param[in] queue Pointer to the queue handle
* @param[in] item_ptr Pointer to the item to place in the queue
*
* @return The status of the put request. [\ref CY_RSLT_SUCCESS, \ref CY_RTOS_NO_MEMORY, \ref
* CY_RTOS_GENERAL_ERROR, \ref CY_RTOS_QUEUE_FULL]
*/
cy_rslt_t cy_rtos_queue_put(cy_queue_t *queue, const void *item_ptr);

/** Gets an item in a queue.
*
* This function gets an item from the queue. The item is copied
* out of the queue into the memory provide by item_ptr. This space must be
* large enough to hold a queue entry as defined when the queue was initialized.
*
* @param[in] queue Pointer to the queue handle
* @param[in] item_ptr Pointer to the memory for the item from the queue
*
* @return The status of the get request. [\ref CY_RSLT_SUCCESS, \ref CY_RTOS_NO_MEMORY, \ref
* CY_RTOS_GENERAL_ERROR, \ref CY_RTOS_QUEUE_EMPTY]
*/
cy_rslt_t cy_rtos_queue_get(cy_queue_t *queue, void *item_ptr);

/** Return the number of items in the queue.
*
* This function returns the number of items currently in the queue.
*
* @param[in] queue Pointer to the queue handle
* @param[out] num_waiting Pointer to the return count
*
* @return The status of the count request. [\ref CY_RSLT_SUCCESS, \ref CY_RTOS_GENERAL_ERROR]
*/
cy_rslt_t cy_rtos_queue_count(cy_queue_t *queue, size_t *num_waiting);

/** Return the amount of empty space in the queue.
*
* This function returns the amount of empty space in the
* queue. For instance, if the queue was created with 10 entries max and there
* are currently 2 entries in the queue, this will return 8.
*
* @param[in] queue Pointer to the queue handle
* @param[out] num_spaces Pointer to the return count.
*
* @return The status of the space request. [\ref CY_RSLT_SUCCESS, \ref CY_RTOS_GENERAL_ERROR]
*/
cy_rslt_t cy_rtos_queue_space(cy_queue_t *queue, size_t *num_spaces);

/** Reset the queue.
*
* This function sets the queue to empty.
*
* @param[in] queue pointer to the queue handle
*
* @return The status of the reset request. [\ref CY_RSLT_SUCCESS, \ref CY_RTOS_GENERAL_ERROR]
*/
cy_rslt_t cy_rtos_queue_reset(cy_queue_t *queue);

/** Deinitialize the queue handle.
*
* This function de-initializes the queue and returns all
* resources used by the queue.
*
* @param[in] queue Pointer to the queue handle
*
* @return The status of the deinit request. [\ref CY_RSLT_SUCCESS, \ref CY_RTOS_GENERAL_ERROR]
*/
cy_rslt_t cy_rtos_queue_deinit(cy_queue_t *queue);

/** \} group_abstraction_rtos_queue */


#ifdef __cplusplus
} // extern "C"
#endif
30 changes: 22 additions & 8 deletions wi-fi/hal/cyabs_rtos_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#pragma once

#include <stdatomic.h>
#include <sys/types.h>
#include <sys/threads.h>

Expand Down Expand Up @@ -65,14 +66,14 @@ extern "C" {
* MAX >= REALTIME >= HIGH >= ABOVENORMAL >= NORMAL >= BELOWNORMAL >= LOW >= MIN
*/
typedef enum {
CY_RTOS_PRIORITY_MIN = 0, /**< Minimum allowable Thread priority */
CY_RTOS_PRIORITY_LOW = 1, /**< A low priority Thread */
CY_RTOS_PRIORITY_BELOWNORMAL = 2, /**< A slightly below normal Thread priority */
CY_RTOS_PRIORITY_NORMAL = 3, /**< The normal Thread priority */
CY_RTOS_PRIORITY_ABOVENORMAL = 4, /**< A slightly elevated Thread priority */
CY_RTOS_PRIORITY_HIGH = 5, /**< A high priority Thread */
CY_RTOS_PRIORITY_REALTIME = 6, /**< Realtime Thread priority */
CY_RTOS_PRIORITY_MAX = 7 /**< Maximum allowable Thread priority */
CY_RTOS_PRIORITY_MIN = 7, /**< Minimum allowable Thread priority */
CY_RTOS_PRIORITY_LOW = 6, /**< A low priority Thread */
CY_RTOS_PRIORITY_BELOWNORMAL = 5, /**< A slightly below normal Thread priority */
CY_RTOS_PRIORITY_NORMAL = 4, /**< The normal Thread priority */
CY_RTOS_PRIORITY_ABOVENORMAL = 3, /**< A slightly elevated Thread priority */
CY_RTOS_PRIORITY_HIGH = 2, /**< A high priority Thread */
CY_RTOS_PRIORITY_REALTIME = 1, /**< Realtime Thread priority */
CY_RTOS_PRIORITY_MAX = 0 /**< Maximum allowable Thread priority */
} cy_thread_priority_t;

/** Alias for the RTOS specific definition of a thread handle */
Expand All @@ -83,6 +84,19 @@ typedef void *cy_thread_arg_t;
typedef handle_t cy_mutex_t;
/** Alias for the RTOS specific time unit (in milliseconds) */
typedef time_t cy_time_t;
/** Alias for the RTOS specific semaphore implementation */
typedef struct {
handle_t mutex;
handle_t cond;
volatile unsigned int v;
unsigned int m;
} cy_semaphore_t;
/** Alias for the RTOS specific queue implementation */
typedef struct {
atomic_uint head, tail;
size_t length, itemsz;
void *data;
} cy_queue_t;

/** \} group_abstraction_rtos_port */

Expand Down
3 changes: 3 additions & 0 deletions wi-fi/hal/cyhal.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@
/** Macro specifying whether the SPI driver is available for the current device \def CYHAL_DRIVER_AVAILABLE_SPI
*/
#define CYHAL_DRIVER_AVAILABLE_SPI 0
/** Macro specifying whether the USB Dev driver is available for the current device \def CYHAL_DRIVER_AVAILABLE_USB_DEV
*/
#define CYHAL_DRIVER_AVAILABLE_USB_DEV 1
5 changes: 5 additions & 0 deletions wi-fi/hal/cyhal_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

#include "cybsp.h"
#if !((CYBSP_WIFI_INTERFACE_TYPE == CYBSP_M2M_INTERFACE) || (CYBSP_WIFI_INTERFACE_TYPE == CYBSP_USB_INTERFACE))

#include "cyhal_gpio.h"
#include "cyhal_utils.h"
#include "cyabs_rtos.h"
Expand Down Expand Up @@ -167,3 +170,5 @@ void cyhal_gpio_irq_enable(cyhal_gpio_t pin, cyhal_gpio_irq_event_t event, bool
{
cy_log_msg(CYLF_GPIO, CY_LOG_ERR, "cyhal_gpio_irq_enable - not implemented!\n");
}

#endif /* !((CYBSP_WIFI_INTERFACE_TYPE == CYBSP_M2M_INTERFACE) || (CYBSP_WIFI_INTERFACE_TYPE == CYBSP_USB_INTERFACE)) */
6 changes: 6 additions & 0 deletions wi-fi/hal/cyhal_hw_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
/*
#include "TODO: Port specific header file"
*/
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -84,6 +85,11 @@ typedef struct
void *empty;
} cyhal_m2m_t;

/** USB object */
typedef struct {
void *usb_priv;
} cyhal_usb_t;

/** \} group_hal_hw_types_data_structures */

#if defined(__cplusplus)
Expand Down
Loading
Loading