mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
add_files
This commit is contained in:
parent
8c9dcfb618
commit
fc69ce4145
@ -1,6 +1,7 @@
|
||||
#include <bl_gpio.h>
|
||||
#include <hosal_gpio.h>
|
||||
#include "../PikaStdDevice/pika_hal.h"
|
||||
#include "pika_hal_BLIOT_irq_task.h"
|
||||
|
||||
int pika_hal_platform_GPIO_open(pika_dev* dev, char* name) {
|
||||
dev->platform_data = pikaMalloc(sizeof(hosal_gpio_dev_t));
|
||||
@ -56,6 +57,15 @@ int pika_hal_platform_GPIO_ioctl_disable(pika_dev* dev) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern volatile _BLIOT_irq g_irq;
|
||||
void _hosal_gpio_irq_handler(void* arg) {
|
||||
if (g_irq.gpio_irq_trigger) {
|
||||
return;
|
||||
}
|
||||
g_irq.gpio_irq_arg = arg;
|
||||
g_irq.gpio_irq_trigger = 1;
|
||||
}
|
||||
|
||||
int pika_hal_platform_GPIO_ioctl_config(pika_dev* dev,
|
||||
pika_hal_GPIO_config* cfg) {
|
||||
hosal_gpio_dev_t* platform_gpio = (hosal_gpio_dev_t*)dev->platform_data;
|
||||
@ -84,9 +94,11 @@ int pika_hal_platform_GPIO_ioctl_config(pika_dev* dev,
|
||||
switch (cfg->pull) {
|
||||
case PIKA_HAL_GPIO_PULL_NONE:
|
||||
platform_gpio->config = INPUT_HIGH_IMPEDANCE;
|
||||
break;
|
||||
|
||||
case PIKA_HAL_GPIO_PULL_UP:
|
||||
platform_gpio->config = INPUT_PULL_UP;
|
||||
break;
|
||||
|
||||
case PIKA_HAL_GPIO_PULL_DOWN:
|
||||
/* not supported */
|
||||
@ -94,6 +106,7 @@ int pika_hal_platform_GPIO_ioctl_config(pika_dev* dev,
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("GPIO set port %d to unknown\r\n",
|
||||
@ -101,5 +114,28 @@ int pika_hal_platform_GPIO_ioctl_config(pika_dev* dev,
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* support event callback */
|
||||
if (NULL != cfg->event_callback &&
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE == cfg->event_callback_ena) {
|
||||
switch (cfg->event_callback_filter) {
|
||||
case PIKA_HAL_GPIO_EVENT_SIGNAL_RISING:
|
||||
hosal_gpio_irq_set(dev->platform_data, HOSAL_IRQ_TRIG_POS_PULSE,
|
||||
_hosal_gpio_irq_handler, dev);
|
||||
break;
|
||||
case PIKA_HAL_GPIO_EVENT_SIGNAL_FALLING:
|
||||
hosal_gpio_irq_set(dev->platform_data, HOSAL_IRQ_TRIG_NEG_PULSE,
|
||||
_hosal_gpio_irq_handler, dev);
|
||||
break;
|
||||
|
||||
default:
|
||||
__platform_printf(
|
||||
"Error: not supported event callback filter %d\r\n",
|
||||
cfg->event_callback_filter);
|
||||
return -1;
|
||||
}
|
||||
/* start irq task thread */
|
||||
_BLIOT_irq_task_start();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -101,7 +101,10 @@ int pika_hal_platform_SPI_ioctl_enable(pika_dev* dev) {
|
||||
platform_spi->port, platform_spi->config.freq,
|
||||
platform_spi->config.mode, platform_spi->config.polar_phase);
|
||||
#endif
|
||||
hosal_spi_init(platform_spi);
|
||||
if (0 != hosal_spi_init(platform_spi)) {
|
||||
__platform_printf("SPI: Open SPI%d failed\r\n", platform_spi->port);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
@ -119,11 +122,25 @@ int pika_hal_platform_SPI_ioctl_disable(pika_dev* dev) {
|
||||
int pika_hal_platform_SPI_write(pika_dev* dev, void* buf, size_t count) {
|
||||
hosal_spi_dev_t* platform_spi = (hosal_spi_dev_t*)dev->platform_data;
|
||||
pika_hal_SPI_config* cfg = (pika_hal_SPI_config*)dev->ioctl_config;
|
||||
return hosal_spi_send(platform_spi, buf, count, cfg->timeout);
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("SPI: Write %d bytes\r\n", count);
|
||||
#endif
|
||||
int ret = hosal_spi_send(platform_spi, buf, count, cfg->timeout);
|
||||
if (0 != ret) {
|
||||
__platform_printf("SPI: Write %d bytes failed\r\n", count);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int pika_hal_platform_SPI_read(pika_dev* dev, void* buf, size_t count) {
|
||||
hosal_spi_dev_t* platform_spi = (hosal_spi_dev_t*)dev->platform_data;
|
||||
pika_hal_SPI_config* cfg = (pika_hal_SPI_config*)dev->ioctl_config;
|
||||
return hosal_spi_recv(platform_spi, buf, count, cfg->timeout);
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("SPI: Read %d bytes\r\n", count);
|
||||
#endif
|
||||
int ret = hosal_spi_recv(platform_spi, buf, count, cfg->timeout);
|
||||
if (0 != ret) {
|
||||
__platform_printf("SPI: Read %d bytes failed\r\n", count);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
161
package/BLIOT/pika_hal_BLIOT_UART.c
Normal file
161
package/BLIOT/pika_hal_BLIOT_UART.c
Normal file
@ -0,0 +1,161 @@
|
||||
#include <hosal_uart.h>
|
||||
#include "../PikaStdDevice/pika_hal.h"
|
||||
#include "pika_hal_BLIOT_irq_task.h"
|
||||
|
||||
extern hosal_uart_dev_t uart_stdio;
|
||||
|
||||
int pika_hal_platform_UART_open(pika_dev* dev, char* name) {
|
||||
/* Support UART1 */
|
||||
if (name[0] == 'U' && name[1] == 'A' && name[2] == 'R' && name[3] == 'T') {
|
||||
hosal_uart_dev_t* platform_uart = &uart_stdio;
|
||||
dev->platform_data = platform_uart;
|
||||
int UART_num = fast_atoi(name + 4);
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("opening UART%d\r\n", UART_num);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int pika_hal_platform_UART_close(pika_dev* dev) {
|
||||
hosal_uart_dev_t* platform_uart = (hosal_uart_dev_t*)dev->platform_data;
|
||||
if (platform_uart == &uart_stdio) {
|
||||
return 0;
|
||||
}
|
||||
if (NULL != platform_uart) {
|
||||
pikaFree(platform_uart, sizeof(hosal_uart_dev_t));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern volatile _BLIOT_irq g_irq;
|
||||
int _hosal_uart_irq_handler(void* p_arg) {
|
||||
if (g_irq.uart_irq_trigger) {
|
||||
return 0;
|
||||
}
|
||||
g_irq.uart_irq_arg = p_arg;
|
||||
g_irq.uart_irq_trigger = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_platform_UART_ioctl_config(pika_dev* dev,
|
||||
pika_hal_UART_config* cfg) {
|
||||
hosal_uart_dev_t* platform_uart = (hosal_uart_dev_t*)dev->platform_data;
|
||||
if (platform_uart != &uart_stdio) {
|
||||
platform_uart->config.baud_rate = cfg->baudrate;
|
||||
switch (cfg->data_bits) {
|
||||
case PIKA_HAL_UART_DATA_BITS_5:
|
||||
platform_uart->config.data_width = HOSAL_DATA_WIDTH_5BIT;
|
||||
break;
|
||||
case PIKA_HAL_UART_DATA_BITS_6:
|
||||
platform_uart->config.data_width = HOSAL_DATA_WIDTH_6BIT;
|
||||
break;
|
||||
case PIKA_HAL_UART_DATA_BITS_7:
|
||||
platform_uart->config.data_width = HOSAL_DATA_WIDTH_7BIT;
|
||||
break;
|
||||
case PIKA_HAL_UART_DATA_BITS_8:
|
||||
platform_uart->config.data_width = HOSAL_DATA_WIDTH_8BIT;
|
||||
break;
|
||||
default:
|
||||
platform_uart->config.data_width = HOSAL_DATA_WIDTH_8BIT;
|
||||
break;
|
||||
}
|
||||
switch (cfg->parity) {
|
||||
case PIKA_HAL_UART_PARITY_NONE:
|
||||
platform_uart->config.parity = HOSAL_NO_PARITY;
|
||||
break;
|
||||
case PIKA_HAL_UART_PARITY_ODD:
|
||||
platform_uart->config.parity = HOSAL_ODD_PARITY;
|
||||
break;
|
||||
case PIKA_HAL_UART_PARITY_EVEN:
|
||||
platform_uart->config.parity = HOSAL_EVEN_PARITY;
|
||||
break;
|
||||
default:
|
||||
platform_uart->config.parity = HOSAL_NO_PARITY;
|
||||
break;
|
||||
}
|
||||
switch (cfg->stop_bits) {
|
||||
case PIKA_HAL_UART_STOP_BITS_1:
|
||||
platform_uart->config.stop_bits = HOSAL_STOP_BITS_1;
|
||||
break;
|
||||
case PIKA_HAL_UART_STOP_BITS_2:
|
||||
platform_uart->config.stop_bits = HOSAL_STOP_BITS_2;
|
||||
break;
|
||||
case PIKA_HAL_UART_STOP_BITS_1_5:
|
||||
platform_uart->config.stop_bits = HOSAL_STOP_BITS_1_5;
|
||||
break;
|
||||
default:
|
||||
platform_uart->config.stop_bits = HOSAL_STOP_BITS_1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* support event callback */
|
||||
if (NULL != cfg->event_callback &&
|
||||
PIKA_HAL_EVENT_CALLBACK_ENA_ENABLE == cfg->event_callback_ena) {
|
||||
hosal_uart_ioctl(platform_uart, HOSAL_UART_MODE_SET,
|
||||
(void*)HOSAL_UART_MODE_INT);
|
||||
switch (cfg->event_callback_filter) {
|
||||
/* Configure UART to interrupt mode */
|
||||
case PIKA_HAL_UART_EVENT_SIGNAL_RX:
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("Setting UART_RX callback\r\n");
|
||||
#endif
|
||||
hosal_uart_callback_set(platform_uart, HOSAL_UART_RX_CALLBACK,
|
||||
_hosal_uart_irq_handler, dev);
|
||||
break;
|
||||
case PIKA_HAL_UART_EVENT_SIGNAL_TX:
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("Setting UART_TX callback\r\n");
|
||||
#endif
|
||||
hosal_uart_callback_set(platform_uart, HOSAL_UART_TX_CALLBACK,
|
||||
_hosal_uart_irq_handler, dev);
|
||||
break;
|
||||
default:
|
||||
__platform_printf(
|
||||
"Error: not supported event callback filter %d\r\n",
|
||||
cfg->event_callback_filter);
|
||||
return -1;
|
||||
}
|
||||
/* start irq task thread */
|
||||
_BLIOT_irq_task_start();
|
||||
}
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_platform_UART_ioctl_enable(pika_dev* dev) {
|
||||
if (!dev->is_enabled) {
|
||||
hosal_uart_dev_t* platform_uart = (hosal_uart_dev_t*)dev->platform_data;
|
||||
if (platform_uart == &uart_stdio) {
|
||||
return 0;
|
||||
}
|
||||
hosal_uart_init(platform_uart);
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int pika_hal_platform_UART_ioctl_disable(pika_dev* dev) {
|
||||
if (dev->is_enabled) {
|
||||
hosal_uart_dev_t* platform_uart = (hosal_uart_dev_t*)dev->platform_data;
|
||||
if (platform_uart == &uart_stdio) {
|
||||
return 0;
|
||||
}
|
||||
hosal_uart_finalize(platform_uart);
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int pika_hal_platform_UART_write(pika_dev* dev, void* buf, size_t count) {
|
||||
hosal_uart_dev_t* platform_uart = (hosal_uart_dev_t*)dev->platform_data;
|
||||
return hosal_uart_send(platform_uart, buf, count);
|
||||
}
|
||||
|
||||
int pika_hal_platform_UART_read(pika_dev* dev, void* buf, size_t count) {
|
||||
hosal_uart_dev_t* platform_uart = (hosal_uart_dev_t*)dev->platform_data;
|
||||
return hosal_uart_receive(platform_uart, buf, count);
|
||||
}
|
52
package/BLIOT/pika_hal_BLIOT_irq_task.c
Normal file
52
package/BLIOT/pika_hal_BLIOT_irq_task.c
Normal file
@ -0,0 +1,52 @@
|
||||
#include "pika_hal_BLIOT_irq_task.h"
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
|
||||
volatile _BLIOT_irq g_irq = {0};
|
||||
|
||||
static void _irq_task(void* pvParameters) {
|
||||
while (1) {
|
||||
vTaskDelay(10);
|
||||
if (g_irq.gpio_irq_trigger) {
|
||||
g_irq.gpio_irq_trigger = 0;
|
||||
pika_dev* dev = (pika_dev*)g_irq.gpio_irq_arg;
|
||||
pika_hal_GPIO_config* cfg = dev->ioctl_config;
|
||||
cfg->event_callback(dev, cfg->event_callback_filter);
|
||||
}
|
||||
if (g_irq.uart_irq_trigger) {
|
||||
g_irq.uart_irq_trigger = 0;
|
||||
pika_dev* dev = (pika_dev*)g_irq.uart_irq_arg;
|
||||
pika_hal_UART_config* cfg = dev->ioctl_config;
|
||||
cfg->event_callback(dev, cfg->event_callback_filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int _BLIOT_irq_init(volatile _BLIOT_irq* irq) {
|
||||
irq->task_created = 0;
|
||||
irq->gpio_irq_trigger = 0;
|
||||
irq->gpio_irq_arg = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _BLIOT_irq_task_start() {
|
||||
/* already started, skip */
|
||||
if (1 == g_irq.task_created) {
|
||||
return 0;
|
||||
}
|
||||
_BLIOT_irq_init(&g_irq);
|
||||
g_irq.task_created = 1;
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
__platform_printf("Creating _irq_task\r\n");
|
||||
#endif
|
||||
BaseType_t ret =
|
||||
xTaskCreate(_irq_task, (char*)"_irq_task", 4096, NULL, 15, NULL);
|
||||
|
||||
#if PIKA_DEBUG_ENABLE
|
||||
if (pdPASS == ret) {
|
||||
__platform_printf("Create _irq_task succeed.\r\n");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
return -1;
|
||||
}
|
12
package/BLIOT/pika_hal_BLIOT_irq_task.h
Normal file
12
package/BLIOT/pika_hal_BLIOT_irq_task.h
Normal file
@ -0,0 +1,12 @@
|
||||
#include "../PikaStdDevice/pika_hal.h"
|
||||
|
||||
typedef struct {
|
||||
uint8_t task_created;
|
||||
uint8_t gpio_irq_trigger;
|
||||
void* gpio_irq_arg;
|
||||
|
||||
uint8_t uart_irq_trigger;
|
||||
void* uart_irq_arg;
|
||||
} _BLIOT_irq;
|
||||
|
||||
int _BLIOT_irq_task_start();
|
Loading…
x
Reference in New Issue
Block a user