Repository: incubator-mynewt-core Updated Branches: refs/heads/sterly_refactor a2345669a -> 913f7ddbf
note that we need to handle 9-bit values in SPI implementation. Add test sblinky package. Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/913f7ddb Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/913f7ddb Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/913f7ddb Branch: refs/heads/sterly_refactor Commit: 913f7ddbf753549f7d3c740844f9afd316f0e1aa Parents: a234566 Author: Sterling Hughes <[email protected]> Authored: Mon Aug 8 13:44:25 2016 -0700 Committer: Sterling Hughes <[email protected]> Committed: Mon Aug 8 13:44:25 2016 -0700 ---------------------------------------------------------------------- apps/sblinky/pkg.yml | 46 +++++ apps/sblinky/src/main.c | 310 ++++++++++++++++++++++++++++++ hw/mcu/nordic/nrf52xxx/src/hal_spi.c | 4 + 3 files changed, 360 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/913f7ddb/apps/sblinky/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/sblinky/pkg.yml b/apps/sblinky/pkg.yml new file mode 100644 index 0000000..a089b99 --- /dev/null +++ b/apps/sblinky/pkg.yml @@ -0,0 +1,46 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +pkg.name: apps/sblinky +pkg.type: app +pkg.description: Basic example application which blinks an LED. +pkg.author: "Apache Mynewt <[email protected]>" +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + +pkg.req_apis: + - ADC_HW_IMPL + +pkg.feature_blacklist: + ".*": SHELL + +pkg.feature_whitelist: + ".*newtmgr.*": SHELL + +pkg.deps: + - libs/newtmgr + - hw/mcu/nordic + - drivers/adc/adc_nrf52 + - drivers/adc + - libs/console/full + - libs/os + - libs/shell + - sys/config + - sys/log + - sys/stats http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/913f7ddb/apps/sblinky/src/main.c ---------------------------------------------------------------------- diff --git a/apps/sblinky/src/main.c b/apps/sblinky/src/main.c new file mode 100755 index 0000000..03583c4 --- /dev/null +++ b/apps/sblinky/src/main.c @@ -0,0 +1,310 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include "os/os.h" +#include "bsp/bsp.h" +#include "hal/hal_gpio.h" +#include "console/console.h" +#include "shell/shell.h" +#include "log/log.h" +#include "stats/stats.h" +#include "config/config.h" +#include <os/os_dev.h> +#include <adc/adc.h> +#include <adc_nrf52/adc_nrf52.h> +#include <assert.h> +#include <string.h> +#ifdef ARCH_sim +#include <mcu/mcu_sim.h> +#endif +#include "nrf.h" +#include "nrf_drv_saadc.h" +#include "app_util_platform.h" +#include "app_error.h" + +int g_result_mv; +int g_result; +nrf_drv_saadc_config_t adc_config = NRF_DRV_SAADC_DEFAULT_CONFIG; + +/* Init all tasks */ +volatile int tasks_initialized; +int init_tasks(void); + +/* Task 1 */ +#define TASK1_PRIO (1) +#define TASK1_STACK_SIZE OS_STACK_ALIGN(1024) +struct os_task task1; +os_stack_t stack1[TASK1_STACK_SIZE]; +static volatile int g_task1_loops; + +/* Task 2 */ +#define TASK2_PRIO (2) +#define TASK2_STACK_SIZE OS_STACK_ALIGN(1024) +struct os_task task2; +os_stack_t stack2[TASK2_STACK_SIZE]; + +#define SHELL_TASK_PRIO (3) +#define SHELL_MAX_INPUT_LEN (256) +#define SHELL_TASK_STACK_SIZE (OS_STACK_ALIGN(1024)) +os_stack_t shell_stack[SHELL_TASK_STACK_SIZE]; + +struct log_handler log_console_handler; +struct log my_log; + +static volatile int g_task2_loops; + +/* Global test semaphore */ +struct os_sem g_test_sem; + +/* For LED toggling */ +int g_led_pin; + +#define DEFAULT_MBUF_MPOOL_BUF_LEN (256) +#define DEFAULT_MBUF_MPOOL_NBUFS (5) + +uint8_t default_mbuf_mpool_data[DEFAULT_MBUF_MPOOL_BUF_LEN * + DEFAULT_MBUF_MPOOL_NBUFS]; + +struct os_mbuf_pool default_mbuf_pool; +struct os_mempool default_mbuf_mpool; + +struct adc_dev my_dev; + +#if 0 +#define SAADC_SAMPLES_IN_BUFFER (4) +static nrf_saadc_value_t m_buffer_pool[2][SAADC_SAMPLES_IN_BUFFER]; + +int event_finished; +int total_events; + +static void +saadc_cb(const nrf_drv_saadc_evt_t *event) +{ + if (event->type == NRF_DRV_SAADC_EVT_DONE) { + ++event_finished; + } + ++total_events; +} + +void +saadc_test(void) +{ + ret_code_t rc; + nrf_saadc_channel_config_t cc = + NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0); + + rc = nrf_drv_saadc_init(NULL, saadc_cb); + APP_ERROR_CHECK(rc); + rc = nrf_drv_saadc_channel_init(0, &cc); + APP_ERROR_CHECK(rc); + + rc = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAADC_SAMPLES_IN_BUFFER); + APP_ERROR_CHECK(rc); + + rc = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAADC_SAMPLES_IN_BUFFER); + APP_ERROR_CHECK(rc); +} +#endif + +#define ADC_NUMBER_SAMPLES (2) +#define ADC_NUMBER_CHANNELS (1) + +uint8_t *sample_buffer1; +uint8_t *sample_buffer2; + +int adc_result; +int my_result_mv; + +int +adc_read_event(struct adc_dev *dev, void *arg, uint8_t etype, + void *buffer, int buffer_len) +{ + int i; + //int result; + int rc; + + for (i = 0; i < ADC_NUMBER_SAMPLES; i++) { + rc = adc_buf_read(dev, buffer, buffer_len, i, &adc_result); + if (rc != 0) { + goto err; + } + my_result_mv = adc_result_mv(dev, 0, adc_result); + } + + adc_buf_release(dev, buffer, buffer_len); + + return (0); +err: + return (rc); +} + +void +task1_handler(void *arg) +{ + struct os_task *t; + struct adc_dev *adc; + + nrf_saadc_channel_config_t cc = + NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1); + cc.gain = NRF_SAADC_GAIN1_4; + cc.reference = NRF_SAADC_REFERENCE_VDD4; + /* Set the led pin for the E407 devboard */ + g_led_pin = LED_BLINK_PIN; + hal_gpio_init_out(g_led_pin, 1); + + adc = (struct adc_dev *) os_dev_open("adc0", 1, NULL); + assert(adc != NULL); + + adc_chan_config(adc, 0, &cc); + + sample_buffer1 = malloc(adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES)); + sample_buffer2 = malloc(adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES)); + + adc_buf_set(adc, sample_buffer1, sample_buffer2, + adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES)); + adc_event_handler_set(adc, adc_read_event, (void *) NULL); + + while (1) { + t = os_sched_get_current_task(); + assert(t->t_func == task1_handler); +/* + rc = adc_read_channel(adc, 0, &g_result); + assert(rc == 0); + g_result_mv = adc_result_mv(adc, 0, g_result); +*/ + adc_sample(adc); + + ++g_task1_loops; + + /* Wait one second */ + os_time_delay(OS_TICKS_PER_SEC); + + /* Toggle the LED */ + hal_gpio_toggle(g_led_pin); + + /* Release semaphore to task 2 */ + os_sem_release(&g_test_sem); + +#if 0 + nrf_drv_saadc_sample(); +#endif + } + + os_dev_close((struct os_dev *) adc); +} + +void +task2_handler(void *arg) +{ + struct os_task *t; + + while (1) { + /* just for debug; task 2 should be the running task */ + t = os_sched_get_current_task(); + assert(t->t_func == task2_handler); + + /* Increment # of times we went through task loop */ + ++g_task2_loops; + + /* Wait for semaphore from ISR */ + os_sem_pend(&g_test_sem, OS_TIMEOUT_NEVER); + } +} + +/** + * init_tasks + * + * Called by main.c after os_init(). This function performs initializations + * that are required before tasks are running. + * + * @return int 0 success; error otherwise. + */ +int +init_tasks(void) +{ + /* Initialize global test semaphore */ + os_sem_init(&g_test_sem, 0); + + os_task_init(&task1, "task1", task1_handler, NULL, + TASK1_PRIO, OS_WAIT_FOREVER, stack1, TASK1_STACK_SIZE); + + os_task_init(&task2, "task2", task2_handler, NULL, + TASK2_PRIO, OS_WAIT_FOREVER, stack2, TASK2_STACK_SIZE); + + tasks_initialized = 1; + return 0; +} + +/** + * main + * + * The main function for the project. This function initializes the os, calls + * init_tasks to initialize tasks (and possibly other objects), then starts the + * OS. We should not return from os start. + * + * @return int NOTE: this function should never return! + */ +int +main(int argc, char **argv) +{ + int rc; + +#ifdef ARCH_sim + mcu_sim_parse_args(argc, argv); +#endif + + conf_init(); + + os_init(); + + rc = os_mempool_init(&default_mbuf_mpool, DEFAULT_MBUF_MPOOL_NBUFS, + DEFAULT_MBUF_MPOOL_BUF_LEN, default_mbuf_mpool_data, + "default_mbuf_data"); + assert(rc == 0); + + rc = os_mbuf_pool_init(&default_mbuf_pool, &default_mbuf_mpool, + DEFAULT_MBUF_MPOOL_BUF_LEN, DEFAULT_MBUF_MPOOL_NBUFS); + assert(rc == 0); + + rc = os_msys_register(&default_mbuf_pool); + assert(rc == 0); + + shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE, + SHELL_MAX_INPUT_LEN); + + (void) console_init(shell_console_rx_cb); + + stats_module_init(); + + rc = os_dev_create((struct os_dev *) &my_dev, "adc0", + OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT, + nrf52_adc_dev_init, &adc_config); + assert(rc == 0); + +#if 0 + saadc_test(); +#endif + rc = init_tasks(); + os_start(); + + /* os start should never return. If it does, this should be an error */ + assert(0); + + return rc; +} + http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/913f7ddb/hw/mcu/nordic/nrf52xxx/src/hal_spi.c ---------------------------------------------------------------------- diff --git a/hw/mcu/nordic/nrf52xxx/src/hal_spi.c b/hw/mcu/nordic/nrf52xxx/src/hal_spi.c index a6e5840..f12c5f3 100644 --- a/hw/mcu/nordic/nrf52xxx/src/hal_spi.c +++ b/hw/mcu/nordic/nrf52xxx/src/hal_spi.c @@ -26,6 +26,10 @@ #include <nrf_drv_spi.h> #include <app_util_platform.h> +/* + * XXX: Should handle 9-bit SPI values in SW. + */ + #define NRF52_HAL_SPI_MAX (2) struct nrf52_hal_spi {
