Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /* mbed Microcontroller Library
switches 0:5c4d7b2438d3 2 * Copyright (c) 2016 ARM Limited
switches 0:5c4d7b2438d3 3 *
switches 0:5c4d7b2438d3 4 * Licensed under the Apache License, Version 2.0 (the "License");
switches 0:5c4d7b2438d3 5 * you may not use this file except in compliance with the License.
switches 0:5c4d7b2438d3 6 * You may obtain a copy of the License at
switches 0:5c4d7b2438d3 7 *
switches 0:5c4d7b2438d3 8 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:5c4d7b2438d3 9 *
switches 0:5c4d7b2438d3 10 * Unless required by applicable law or agreed to in writing, software
switches 0:5c4d7b2438d3 11 * distributed under the License is distributed on an "AS IS" BASIS,
switches 0:5c4d7b2438d3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:5c4d7b2438d3 13 * See the License for the specific language governing permissions and
switches 0:5c4d7b2438d3 14 * limitations under the License.
switches 0:5c4d7b2438d3 15 */
switches 0:5c4d7b2438d3 16
switches 0:5c4d7b2438d3 17 #if !DEVICE_LOWPOWERTIMER
switches 0:5c4d7b2438d3 18 #error [NOT_SUPPORTED] Low power timer not supported for this target
switches 0:5c4d7b2438d3 19 #endif
switches 0:5c4d7b2438d3 20
switches 0:5c4d7b2438d3 21 #include "utest/utest.h"
switches 0:5c4d7b2438d3 22 #include "unity/unity.h"
switches 0:5c4d7b2438d3 23 #include "greentea-client/test_env.h"
switches 0:5c4d7b2438d3 24
switches 0:5c4d7b2438d3 25 #include "mbed.h"
switches 0:5c4d7b2438d3 26 #include "us_ticker_api.h"
switches 0:5c4d7b2438d3 27 #include "lp_ticker_api.h"
switches 0:5c4d7b2438d3 28
switches 0:5c4d7b2438d3 29 using namespace utest::v1;
switches 0:5c4d7b2438d3 30
switches 0:5c4d7b2438d3 31 volatile static bool complete;
switches 0:5c4d7b2438d3 32 static ticker_event_t delay_event;
switches 0:5c4d7b2438d3 33 static const ticker_data_t *lp_ticker_data = get_lp_ticker_data();
switches 0:5c4d7b2438d3 34
switches 0:5c4d7b2438d3 35
switches 0:5c4d7b2438d3 36 /* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */
switches 0:5c4d7b2438d3 37 #define LONG_TIMEOUT (100000)
switches 0:5c4d7b2438d3 38 #define SHORT_TIMEOUT (600)
switches 0:5c4d7b2438d3 39
switches 0:5c4d7b2438d3 40 void cb_done(uint32_t id) {
switches 0:5c4d7b2438d3 41 complete = true;
switches 0:5c4d7b2438d3 42 }
switches 0:5c4d7b2438d3 43
switches 0:5c4d7b2438d3 44 void lp_ticker_delay_us(uint32_t delay_us, uint32_t tolerance)
switches 0:5c4d7b2438d3 45 {
switches 0:5c4d7b2438d3 46 complete = false;
switches 0:5c4d7b2438d3 47 uint32_t delay_ts;
switches 0:5c4d7b2438d3 48
switches 0:5c4d7b2438d3 49 ticker_set_handler(lp_ticker_data, cb_done);
switches 0:5c4d7b2438d3 50 ticker_remove_event(lp_ticker_data, &delay_event);
switches 0:5c4d7b2438d3 51 delay_ts = lp_ticker_read() + delay_us;
switches 0:5c4d7b2438d3 52
switches 0:5c4d7b2438d3 53 timestamp_t start = us_ticker_read();
switches 0:5c4d7b2438d3 54 ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event);
switches 0:5c4d7b2438d3 55 while (!complete);
switches 0:5c4d7b2438d3 56 timestamp_t end = us_ticker_read();
switches 0:5c4d7b2438d3 57
switches 0:5c4d7b2438d3 58 TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, end - start);
switches 0:5c4d7b2438d3 59 TEST_ASSERT_TRUE(complete);
switches 0:5c4d7b2438d3 60 }
switches 0:5c4d7b2438d3 61
switches 0:5c4d7b2438d3 62 #if DEVICE_SLEEP
switches 0:5c4d7b2438d3 63 void lp_ticker_1s_deepsleep()
switches 0:5c4d7b2438d3 64 {
switches 0:5c4d7b2438d3 65 complete = false;
switches 0:5c4d7b2438d3 66 uint32_t delay_ts;
switches 0:5c4d7b2438d3 67
switches 0:5c4d7b2438d3 68 /*
switches 0:5c4d7b2438d3 69 * Since deepsleep() may shut down the UART peripheral, we wait for 10ms
switches 0:5c4d7b2438d3 70 * to allow for hardware serial buffers to completely flush.
switches 0:5c4d7b2438d3 71
switches 0:5c4d7b2438d3 72 * This should be replaced with a better function that checks if the
switches 0:5c4d7b2438d3 73 * hardware buffers are empty. However, such an API does not exist now,
switches 0:5c4d7b2438d3 74 * so we'll use the wait_ms() function for now.
switches 0:5c4d7b2438d3 75 */
switches 0:5c4d7b2438d3 76 wait_ms(10);
switches 0:5c4d7b2438d3 77
switches 0:5c4d7b2438d3 78 ticker_set_handler(lp_ticker_data, cb_done);
switches 0:5c4d7b2438d3 79 ticker_remove_event(lp_ticker_data, &delay_event);
switches 0:5c4d7b2438d3 80 delay_ts = lp_ticker_read() + 1000000;
switches 0:5c4d7b2438d3 81
switches 0:5c4d7b2438d3 82 /*
switches 0:5c4d7b2438d3 83 * We use here lp_ticker_read() instead of us_ticker_read() for start and
switches 0:5c4d7b2438d3 84 * end because the microseconds timer might be disable during deepsleep.
switches 0:5c4d7b2438d3 85 */
switches 0:5c4d7b2438d3 86 timestamp_t start = lp_ticker_read();
switches 0:5c4d7b2438d3 87 ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event);
switches 0:5c4d7b2438d3 88 deepsleep();
switches 0:5c4d7b2438d3 89 while (!complete);
switches 0:5c4d7b2438d3 90 timestamp_t end = lp_ticker_read();
switches 0:5c4d7b2438d3 91
switches 0:5c4d7b2438d3 92 TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
switches 0:5c4d7b2438d3 93 TEST_ASSERT_TRUE(complete);
switches 0:5c4d7b2438d3 94 }
switches 0:5c4d7b2438d3 95
switches 0:5c4d7b2438d3 96 void lp_ticker_1s_sleep()
switches 0:5c4d7b2438d3 97 {
switches 0:5c4d7b2438d3 98 complete = false;
switches 0:5c4d7b2438d3 99 uint32_t delay_ts;
switches 0:5c4d7b2438d3 100
switches 0:5c4d7b2438d3 101 ticker_set_handler(lp_ticker_data, cb_done);
switches 0:5c4d7b2438d3 102 ticker_remove_event(lp_ticker_data, &delay_event);
switches 0:5c4d7b2438d3 103 delay_ts = lp_ticker_read() + 1000000;
switches 0:5c4d7b2438d3 104
switches 0:5c4d7b2438d3 105 timestamp_t start = us_ticker_read();
switches 0:5c4d7b2438d3 106 ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event);
switches 0:5c4d7b2438d3 107 sleep();
switches 0:5c4d7b2438d3 108 while (!complete);
switches 0:5c4d7b2438d3 109 timestamp_t end = us_ticker_read();
switches 0:5c4d7b2438d3 110
switches 0:5c4d7b2438d3 111 TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
switches 0:5c4d7b2438d3 112 TEST_ASSERT_TRUE(complete);
switches 0:5c4d7b2438d3 113 }
switches 0:5c4d7b2438d3 114 #endif /* DEVICE_SLEEP */
switches 0:5c4d7b2438d3 115
switches 0:5c4d7b2438d3 116 void lp_ticker_500us(void)
switches 0:5c4d7b2438d3 117 {
switches 0:5c4d7b2438d3 118 lp_ticker_delay_us(500, SHORT_TIMEOUT);
switches 0:5c4d7b2438d3 119 }
switches 0:5c4d7b2438d3 120
switches 0:5c4d7b2438d3 121 void lp_ticker_1ms(void)
switches 0:5c4d7b2438d3 122 {
switches 0:5c4d7b2438d3 123 lp_ticker_delay_us(1000, SHORT_TIMEOUT);
switches 0:5c4d7b2438d3 124 }
switches 0:5c4d7b2438d3 125
switches 0:5c4d7b2438d3 126 void lp_ticker_1s(void)
switches 0:5c4d7b2438d3 127 {
switches 0:5c4d7b2438d3 128 lp_ticker_delay_us(1000000, LONG_TIMEOUT);
switches 0:5c4d7b2438d3 129 }
switches 0:5c4d7b2438d3 130
switches 0:5c4d7b2438d3 131 void lp_ticker_5s(void)
switches 0:5c4d7b2438d3 132 {
switches 0:5c4d7b2438d3 133 lp_ticker_delay_us(5000000, LONG_TIMEOUT);
switches 0:5c4d7b2438d3 134 }
switches 0:5c4d7b2438d3 135
switches 0:5c4d7b2438d3 136 utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
switches 0:5c4d7b2438d3 137 greentea_case_failure_abort_handler(source, reason);
switches 0:5c4d7b2438d3 138 return STATUS_CONTINUE;
switches 0:5c4d7b2438d3 139 }
switches 0:5c4d7b2438d3 140
switches 0:5c4d7b2438d3 141 Case cases[] = {
switches 0:5c4d7b2438d3 142 Case("500us lp_ticker", lp_ticker_500us, greentea_failure_handler),
switches 0:5c4d7b2438d3 143 Case("1ms lp_ticker", lp_ticker_1ms, greentea_failure_handler),
switches 0:5c4d7b2438d3 144 Case("1s lp_ticker", lp_ticker_1s, greentea_failure_handler),
switches 0:5c4d7b2438d3 145 Case("5s lp_ticker", lp_ticker_5s, greentea_failure_handler),
switches 0:5c4d7b2438d3 146 #if DEVICE_SLEEP
switches 0:5c4d7b2438d3 147 Case("1s lp_ticker sleep", lp_ticker_1s_sleep, greentea_failure_handler),
switches 0:5c4d7b2438d3 148 Case("1s lp_ticker deepsleep", lp_ticker_1s_deepsleep, greentea_failure_handler),
switches 0:5c4d7b2438d3 149 #endif /* DEVICE_SLEEP */
switches 0:5c4d7b2438d3 150 };
switches 0:5c4d7b2438d3 151
switches 0:5c4d7b2438d3 152 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
switches 0:5c4d7b2438d3 153 GREENTEA_SETUP(20, "default_auto");
switches 0:5c4d7b2438d3 154 lp_ticker_data->interface->init();
switches 0:5c4d7b2438d3 155 return greentea_test_setup_handler(number_of_cases);
switches 0:5c4d7b2438d3 156 }
switches 0:5c4d7b2438d3 157
switches 0:5c4d7b2438d3 158 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
switches 0:5c4d7b2438d3 159
switches 0:5c4d7b2438d3 160 int main() {
switches 0:5c4d7b2438d3 161 Harness::run(specification);
switches 0:5c4d7b2438d3 162 }