From a83b0b512dc0c344cd8fafdfb1158d69c45ceb6d Mon Sep 17 00:00:00 2001 From: Rahix <rahix@rahix.de> Date: Sat, 10 Aug 2019 15:16:45 +0200 Subject: [PATCH] chore: Format all hw-tests & libcard10 Signed-off-by: Rahix <rahix@rahix.de> --- hw-tests/api-demo/api/api_caller.c | 16 +- hw-tests/api-demo/api/api_dispatcher.c | 9 +- hw-tests/api-demo/core1-dispatcher.c | 19 +- hw-tests/api-demo/genapi.py | 38 +- hw-tests/api-demo/main.c | 34 +- hw-tests/bmatest/main.c | 169 +++--- hw-tests/bmetest/main.c | 172 +++--- hw-tests/bootloader-update/main.c | 90 ++-- hw-tests/dual-core/core1.c | 3 +- hw-tests/ecgtest/main.c | 715 ++++++++++++------------- hw-tests/hello-freertos/main.c | 459 ++++++++-------- hw-tests/hello-world/main.c | 69 +-- hw-tests/imutest/main.c | 314 +++++------ hw-tests/ips/main.c | 51 +- hw-tests/upy-minimal/main.c | 78 +-- hw-tests/upy-minimal/modules/buzzer.c | 6 +- hw-tests/upy-minimal/modules/leds.c | 37 +- hw-tests/upy-minimal/modules/utime.c | 23 +- hw-tests/upy-minimal/systick.c | 19 +- hw-tests/upy-minimal/uart.c | 13 +- lib/card10/bosch.c | 61 ++- lib/card10/leds.c | 235 ++++---- lib/card10/pmic.c | 191 +++---- 23 files changed, 1466 insertions(+), 1355 deletions(-) diff --git a/hw-tests/api-demo/api/api_caller.c b/hw-tests/api-demo/api/api_caller.c index 311dcc55..eb7162c1 100644 --- a/hw-tests/api-demo/api/api_caller.c +++ b/hw-tests/api-demo/api/api_caller.c @@ -3,24 +3,26 @@ #include "api/api_caller.h" #include "tmr_utils.h" -void* api_call_start (uint32_t id, void* args, uint32_t size) +void *api_call_start(uint32_t id, void *args, uint32_t size) { // aquire semaphore - while (E_BUSY == SEMA_GetSema (API_CALL_SEMA)) ; + while (E_BUSY == SEMA_GetSema(API_CALL_SEMA)) + ; - ApiCallSpace->id = id; + ApiCallSpace->id = id; ApiCallSpace->returning = 0; return ApiCallSpace->buf; } -void* api_call_bother_dispatcher (void* buf) +void *api_call_bother_dispatcher(void *buf) { - SEMA_FreeSema (API_CALL_SEMA); + SEMA_FreeSema(API_CALL_SEMA); // TODO: set event - while(1) { + while (1) { // aquire semaphore - while (E_BUSY == SEMA_GetSema (API_CALL_SEMA)) ; + while (E_BUSY == SEMA_GetSema(API_CALL_SEMA)) + ; if (ApiCallSpace->returning == 1) { break; } diff --git a/hw-tests/api-demo/api/api_dispatcher.c b/hw-tests/api-demo/api/api_dispatcher.c index 1c9c73dd..1688ae98 100644 --- a/hw-tests/api-demo/api/api_dispatcher.c +++ b/hw-tests/api-demo/api/api_dispatcher.c @@ -1,10 +1,10 @@ #include "api_dispatcher.h" -int api_init (sys_cfg_sema_t *sys_cfg) +int api_init(sys_cfg_sema_t *sys_cfg) { int ret; - ret = SEMA_Init (sys_cfg); + ret = SEMA_Init(sys_cfg); /* Set the status of the flag to a valid initial state (right before * api-calls are made) */ ApiCallSpace->returning = 1; @@ -13,11 +13,12 @@ int api_init (sys_cfg_sema_t *sys_cfg) } /* Generated function */ -void __api_dispatch_call(uint32_t id, void*buffer); +void __api_dispatch_call(uint32_t id, void *buffer); void api_dispatcher() { - while (SEMA_GetSema(API_CALL_SEMA) == E_BUSY) {} + while (SEMA_GetSema(API_CALL_SEMA) == E_BUSY) { + } if (ApiCallSpace->returning == 1) { SEMA_FreeSema(API_CALL_SEMA); diff --git a/hw-tests/api-demo/core1-dispatcher.c b/hw-tests/api-demo/core1-dispatcher.c index b0ad7743..d96a5f71 100644 --- a/hw-tests/api-demo/core1-dispatcher.c +++ b/hw-tests/api-demo/core1-dispatcher.c @@ -7,7 +7,9 @@ #include "api/api_dispatcher.h" -static const gpio_cfg_t motor_pin = {PORT_0, PIN_8, GPIO_FUNC_OUT, GPIO_PAD_NONE}; +static const gpio_cfg_t motor_pin = { + PORT_0, PIN_8, GPIO_FUNC_OUT, GPIO_PAD_NONE +}; void api_set_buzzer(uint8_t state) { @@ -23,15 +25,21 @@ void api_set_buzzer(uint8_t state) void api_set_led(uint8_t led, led_color_t color) { printf("API: Changing color of led %d.\n", led); - printf("Color { r: %3d, g: %3d, b: %3d }\n", color.red, color.green, color.blue); + printf("Color { r: %3d, g: %3d, b: %3d }\n", + color.red, + color.green, + color.blue); leds_set(led, color.red, color.green, color.blue); leds_update(); } void api_test(char test0, short test1, int test2, long test3) { - printf ("test0: %x, test1: %d, test2: %x, test3: %lx\n", - test0, (int)test1, test2, test3); + printf("test0: %x, test1: %d, test2: %x, test3: %lx\n", + test0, + (int)test1, + test2, + test3); } int main(void) @@ -41,10 +49,9 @@ int main(void) while (1) { api_dispatcher(); - TMR_Delay(MXC_TMR1, MSEC(100), 0); + TMR_Delay(MXC_TMR1, MSEC(100), 0); } - #if 0 // Enable rxev on core1 MXC_GCR->evten |= 0x20; diff --git a/hw-tests/api-demo/genapi.py b/hw-tests/api-demo/genapi.py index 40cf1099..df0f07ff 100644 --- a/hw-tests/api-demo/genapi.py +++ b/hw-tests/api-demo/genapi.py @@ -50,18 +50,19 @@ def main(): f_client = cx.enter_context(open(args.client, "w")) f_server = cx.enter_context(open(args.server, "w")) - print('#include "{}"\n'.format( - os.path.basename(args.header) - ), file=f_client) + print('#include "{}"\n'.format(os.path.basename(args.header)), file=f_client) - print("""\ + print( + """\ #include "{}" void __api_dispatch_call(uint32_t id, void*buffer) {{ switch (id) {{""".format( - os.path.basename(args.header) - ), file=f_server) + os.path.basename(args.header) + ), + file=f_server, + ) for match in matcher.finditer(source): api_id = match.group("id") @@ -100,9 +101,12 @@ void {cdecl}({cargs}) file=f_client, ) - print("""\ + print( + """\ case {id}: - {cdecl}(""".format(id=api_id, cdecl=api_decl), + {cdecl}(""".format( + id=api_id, cdecl=api_decl + ), file=f_server, ) @@ -122,18 +126,17 @@ void {cdecl}({cargs}) print( """\ *({type}*)(buffer + {offset})""".format( - type=ty, - offset=" + ".join(api_args_sizes[:i]) if i > 0 else "0", + type=ty, offset=" + ".join(api_args_sizes[:i]) if i > 0 else "0" ), file=f_server, end="", ) - print(""" + print( + """ ); break;""".format( - cdecl=api_decl, - args=", ".join(api_args_names), + cdecl=api_decl, args=", ".join(api_args_names) ), file=f_server, ) @@ -154,14 +157,17 @@ void {cdecl}({cargs}) file=f_client, ) - print("""\ + print( + """\ default: printf("Error: API function %x is unknown!!\\n", {id}); break; }} }}""".format( - id=api_id, - ), file=f_server) + id=api_id + ), + file=f_server, + ) if __name__ == "__main__": diff --git a/hw-tests/api-demo/main.c b/hw-tests/api-demo/main.c index 9f2118b2..dc7a6dc6 100644 --- a/hw-tests/api-demo/main.c +++ b/hw-tests/api-demo/main.c @@ -8,26 +8,26 @@ int main(void) { - int count = 0; - led_color_t red = {0x10, 0, 0}; + int count = 0; + led_color_t red = { 0x10, 0, 0 }; - card10_init(); - card10_diag(); + card10_init(); + card10_diag(); - printf("API Test.\n"); - printf("core0: Starting dispatcher on core1\n"); - core1_start(); - TMR_Delay(MXC_TMR0, MSEC(100), 0); + printf("API Test.\n"); + printf("core0: Starting dispatcher on core1\n"); + core1_start(); + TMR_Delay(MXC_TMR0, MSEC(100), 0); - api_set_buzzer(1); - TMR_Delay(MXC_TMR0, MSEC(300), 0); - api_set_buzzer(0); - api_set_led(5, red); - api_test(0xAB, 0x7DEF, 0x01, 0x02); + api_set_buzzer(1); + TMR_Delay(MXC_TMR0, MSEC(300), 0); + api_set_buzzer(0); + api_set_led(5, red); + api_test(0xAB, 0x7DEF, 0x01, 0x02); - while(1) { - printf("count = %d\n", count++); + while (1) { + printf("count = %d\n", count++); - TMR_Delay(MXC_TMR0, SEC(1), 0); - } + TMR_Delay(MXC_TMR0, SEC(1), 0); + } } diff --git a/hw-tests/bmatest/main.c b/hw-tests/bmatest/main.c index 1db3b40b..2d84dd99 100644 --- a/hw-tests/bmatest/main.c +++ b/hw-tests/bmatest/main.c @@ -27,117 +27,114 @@ /***** Functions *****/ void print_rslt(int8_t rslt) { - switch (rslt) { - case BMA400_OK: - /* Do nothing */ - break; - case BMA400_E_NULL_PTR: - printf("Error [%d] : Null pointer\r\n", rslt); - break; - case BMA400_E_COM_FAIL: - printf("Error [%d] : Communication failure\r\n", rslt); - break; - case BMA400_E_DEV_NOT_FOUND: - printf("Error [%d] : Device not found\r\n", rslt); - break; - case BMA400_E_INVALID_CONFIG: - printf("Error [%d] : Invalid configuration\r\n", rslt); - break; - case BMA400_W_SELF_TEST_FAIL: - printf("Warning [%d] : Self test failed\r\n", rslt); - break; - default: - printf("Error [%d] : Unknown error code\r\n", rslt); - break; - } + switch (rslt) { + case BMA400_OK: + /* Do nothing */ + break; + case BMA400_E_NULL_PTR: + printf("Error [%d] : Null pointer\r\n", rslt); + break; + case BMA400_E_COM_FAIL: + printf("Error [%d] : Communication failure\r\n", rslt); + break; + case BMA400_E_DEV_NOT_FOUND: + printf("Error [%d] : Device not found\r\n", rslt); + break; + case BMA400_E_INVALID_CONFIG: + printf("Error [%d] : Invalid configuration\r\n", rslt); + break; + case BMA400_W_SELF_TEST_FAIL: + printf("Warning [%d] : Self test failed\r\n", rslt); + break; + default: + printf("Error [%d] : Unknown error code\r\n", rslt); + break; + } } // ***************************************************************************** int main(void) { - card10_init(); - card10_diag(); + card10_init(); + card10_diag(); - struct bma400_dev bma; - struct bma400_int_enable tap_int[2]; - struct bma400_sensor_conf conf[2]; - int8_t rslt; - uint32_t poll_period = 5, test_dur_ms = 30000; - uint16_t int_status; + struct bma400_dev bma; + struct bma400_int_enable tap_int[2]; + struct bma400_sensor_conf conf[2]; + int8_t rslt; + uint32_t poll_period = 5, test_dur_ms = 30000; + uint16_t int_status; + bma.intf_ptr = NULL; /* To attach your interface device reference */ + bma.delay_ms = card10_bosch_delay; + bma.dev_id = BMA400_I2C_ADDRESS_SDO_LOW; + bma.read = card10_bosch_i2c_read_ex; + bma.write = card10_bosch_i2c_write_ex; + bma.intf = BMA400_I2C_INTF; - bma.intf_ptr = NULL; /* To attach your interface device reference */ - bma.delay_ms = card10_bosch_delay; - bma.dev_id = BMA400_I2C_ADDRESS_SDO_LOW; - bma.read = card10_bosch_i2c_read_ex; - bma.write = card10_bosch_i2c_write_ex; - bma.intf = BMA400_I2C_INTF; + rslt = bma400_init(&bma); + if (rslt == BMA400_OK) { + printf("BMA400 found with chip ID 0x%X\r\n", bma.chip_id); + } - rslt = bma400_init(&bma); - if (rslt == BMA400_OK) { - printf("BMA400 found with chip ID 0x%X\r\n", bma.chip_id); - } + print_rslt(rslt); + rslt = bma400_soft_reset(&bma); + print_rslt(rslt); - print_rslt(rslt); + conf[0].type = BMA400_ACCEL; + conf[1].type = BMA400_TAP_INT; - rslt = bma400_soft_reset(&bma); - print_rslt(rslt); + rslt = bma400_get_sensor_conf(conf, 2, &bma); + print_rslt(rslt); - conf[0].type = BMA400_ACCEL; - conf[1].type = BMA400_TAP_INT; + conf[0].param.accel.odr = BMA400_ODR_200HZ; + conf[0].param.accel.range = BMA400_4G_RANGE; + conf[0].param.accel.data_src = BMA400_DATA_SRC_ACCEL_FILT_1; + conf[0].param.accel.filt1_bw = BMA400_ACCEL_FILT1_BW_1; - rslt = bma400_get_sensor_conf(conf, 2, &bma); - print_rslt(rslt); + conf[1].param.tap.int_chan = BMA400_UNMAP_INT_PIN; + conf[1].param.tap.axes_sel = BMA400_Z_AXIS_EN_TAP; + conf[1].param.tap.sensitivity = BMA400_TAP_SENSITIVITY_0; - conf[0].param.accel.odr = BMA400_ODR_200HZ; - conf[0].param.accel.range = BMA400_4G_RANGE; - conf[0].param.accel.data_src = BMA400_DATA_SRC_ACCEL_FILT_1; - conf[0].param.accel.filt1_bw = BMA400_ACCEL_FILT1_BW_1; + rslt = bma400_set_sensor_conf(conf, 2, &bma); + print_rslt(rslt); - conf[1].param.tap.int_chan = BMA400_UNMAP_INT_PIN; - conf[1].param.tap.axes_sel = BMA400_Z_AXIS_EN_TAP; - conf[1].param.tap.sensitivity = BMA400_TAP_SENSITIVITY_0; + bma.delay_ms(100); - rslt = bma400_set_sensor_conf(conf, 2, &bma); - print_rslt(rslt); + tap_int[0].type = BMA400_SINGLE_TAP_INT_EN; + tap_int[0].conf = BMA400_ENABLE; - bma.delay_ms(100); + tap_int[1].type = BMA400_DOUBLE_TAP_INT_EN; + tap_int[1].conf = BMA400_ENABLE; - tap_int[0].type = BMA400_SINGLE_TAP_INT_EN; - tap_int[0].conf = BMA400_ENABLE; + rslt = bma400_enable_interrupt(tap_int, 2, &bma); + print_rslt(rslt); - tap_int[1].type = BMA400_DOUBLE_TAP_INT_EN; - tap_int[1].conf = BMA400_ENABLE; + bma.delay_ms(100); - rslt = bma400_enable_interrupt(tap_int, 2, &bma); - print_rslt(rslt); + rslt = bma400_set_power_mode(BMA400_NORMAL_MODE, &bma); + print_rslt(rslt); - bma.delay_ms(100); + bma.delay_ms(100); - rslt = bma400_set_power_mode(BMA400_NORMAL_MODE, &bma); - print_rslt(rslt); + if (rslt == BMA400_OK) { + printf("Tap configured.\r\n"); - bma.delay_ms(100); + while (test_dur_ms) { + bma.delay_ms(poll_period); - if (rslt == BMA400_OK) { - printf("Tap configured.\r\n"); + rslt = bma400_get_interrupt_status(&int_status, &bma); + print_rslt(rslt); - while (test_dur_ms) { - bma.delay_ms(poll_period); + if (int_status & BMA400_S_TAP_INT_ASSERTED) { + printf("Single tap detected!\r\n"); + } - rslt = bma400_get_interrupt_status(&int_status, &bma); - print_rslt(rslt); + if (int_status & BMA400_D_TAP_INT_ASSERTED) { + printf("Double tap detected!\r\n"); + } - if (int_status & BMA400_S_TAP_INT_ASSERTED) { - printf("Single tap detected!\r\n"); - } - - if (int_status & BMA400_D_TAP_INT_ASSERTED) { - printf("Double tap detected!\r\n"); - } - - test_dur_ms -= poll_period; - } - - } + test_dur_ms -= poll_period; + } + } } diff --git a/hw-tests/bmetest/main.c b/hw-tests/bmetest/main.c index 0c440129..5cc2c38f 100644 --- a/hw-tests/bmetest/main.c +++ b/hw-tests/bmetest/main.c @@ -24,90 +24,94 @@ // ***************************************************************************** int main(void) { - card10_init(); - card10_diag(); - - struct bme680_dev gas_sensor; - gas_sensor.dev_id = BME680_I2C_ADDR_PRIMARY; - gas_sensor.intf = BME680_I2C_INTF; - gas_sensor.read = card10_bosch_i2c_read; - gas_sensor.write = card10_bosch_i2c_write; - gas_sensor.delay_ms = card10_bosch_delay; - /* amb_temp can be set to 25 prior to configuring the gas sensor + card10_init(); + card10_diag(); + + struct bme680_dev gas_sensor; + gas_sensor.dev_id = BME680_I2C_ADDR_PRIMARY; + gas_sensor.intf = BME680_I2C_INTF; + gas_sensor.read = card10_bosch_i2c_read; + gas_sensor.write = card10_bosch_i2c_write; + gas_sensor.delay_ms = card10_bosch_delay; + /* amb_temp can be set to 25 prior to configuring the gas sensor * or by performing a few temperature readings without operating the gas sensor. */ - gas_sensor.amb_temp = 25; - - int8_t rslt = BME680_OK; - rslt = bme680_init(&gas_sensor); - - if(rslt != BME680_OK) { - printf("Failed to init BME680\n"); - } - uint8_t set_required_settings; - - /* Set the temperature, pressure and humidity settings */ - gas_sensor.tph_sett.os_hum = BME680_OS_2X; - gas_sensor.tph_sett.os_pres = BME680_OS_4X; - gas_sensor.tph_sett.os_temp = BME680_OS_8X; - gas_sensor.tph_sett.filter = BME680_FILTER_SIZE_3; - - /* Set the remaining gas sensor settings and link the heating profile */ - gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS; - /* Create a ramp heat waveform in 3 steps */ - gas_sensor.gas_sett.heatr_temp = 320; /* degree Celsius */ - gas_sensor.gas_sett.heatr_dur = 150; /* milliseconds */ - - /* Select the power mode */ - /* Must be set before writing the sensor configuration */ - gas_sensor.power_mode = BME680_FORCED_MODE; - - /* Set the required sensor settings needed */ - set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL - | BME680_GAS_SENSOR_SEL; - - /* Set the desired sensor configuration */ - rslt = bme680_set_sensor_settings(set_required_settings,&gas_sensor); - - /* Set the power mode */ - rslt = bme680_set_sensor_mode(&gas_sensor); - - - while (1) { - TMR_Delay(MXC_TMR0, MSEC(1000), 0); - - struct bme680_field_data data; - rslt = bme680_get_sensor_data(&data, &gas_sensor); - - printf("T: %.2Lf degC, P: %.2Lf hPa, H %.2Lf %%rH ", data.temperature / 100.0l, - data.pressure / 100.0l, data.humidity / 1000.0l ); - - char buf[128]; - sprintf(buf, "T: %.2Lf degC", data.temperature / 100.0l); - Paint_DrawString_EN(0, 0, buf, &Font16, 0x0000, 0xffff); - - sprintf(buf, "P: %.2Lf hPa", data.pressure / 100.0l); - Paint_DrawString_EN(0, 16, buf, &Font16, 0x0000, 0xffff); - - sprintf(buf, "H: %.2Lf %%rH", data.humidity / 1000.0l); - Paint_DrawString_EN(0, 32, buf, &Font16, 0x0000, 0xffff); - - //printf("%.2f,%.2f,%.2f\n", data.temperature / 100.0f, - // data.pressure / 100.0f, data.humidity / 1000.0f ); - /* Avoid using measurements from an unstable heating setup */ - if(data.status & BME680_GASM_VALID_MSK) { - printf(", G: %ld ohms", data.gas_resistance); - sprintf(buf, "G: %ld ohms", data.gas_resistance); - Paint_DrawString_EN(0, 48, buf, &Font16, 0x0000, 0xffff); - } - - LCD_Update(); - - printf("\n"); - - /* Trigger the next measurement if you would like to read data out continuously */ - if (gas_sensor.power_mode == BME680_FORCED_MODE) { - rslt = bme680_set_sensor_mode(&gas_sensor); - } - } + gas_sensor.amb_temp = 25; + + int8_t rslt = BME680_OK; + rslt = bme680_init(&gas_sensor); + + if (rslt != BME680_OK) { + printf("Failed to init BME680\n"); + } + uint8_t set_required_settings; + + /* Set the temperature, pressure and humidity settings */ + gas_sensor.tph_sett.os_hum = BME680_OS_2X; + gas_sensor.tph_sett.os_pres = BME680_OS_4X; + gas_sensor.tph_sett.os_temp = BME680_OS_8X; + gas_sensor.tph_sett.filter = BME680_FILTER_SIZE_3; + + /* Set the remaining gas sensor settings and link the heating profile */ + gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS; + /* Create a ramp heat waveform in 3 steps */ + gas_sensor.gas_sett.heatr_temp = 320; /* degree Celsius */ + gas_sensor.gas_sett.heatr_dur = 150; /* milliseconds */ + + /* Select the power mode */ + /* Must be set before writing the sensor configuration */ + gas_sensor.power_mode = BME680_FORCED_MODE; + + /* Set the required sensor settings needed */ + set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | + BME680_OSH_SEL | BME680_FILTER_SEL | + BME680_GAS_SENSOR_SEL; + + /* Set the desired sensor configuration */ + rslt = bme680_set_sensor_settings(set_required_settings, &gas_sensor); + + /* Set the power mode */ + rslt = bme680_set_sensor_mode(&gas_sensor); + + while (1) { + TMR_Delay(MXC_TMR0, MSEC(1000), 0); + + struct bme680_field_data data; + rslt = bme680_get_sensor_data(&data, &gas_sensor); + + printf("T: %.2Lf degC, P: %.2Lf hPa, H %.2Lf %%rH ", + data.temperature / 100.0l, + data.pressure / 100.0l, + data.humidity / 1000.0l); + + char buf[128]; + sprintf(buf, "T: %.2Lf degC", data.temperature / 100.0l); + Paint_DrawString_EN(0, 0, buf, &Font16, 0x0000, 0xffff); + + sprintf(buf, "P: %.2Lf hPa", data.pressure / 100.0l); + Paint_DrawString_EN(0, 16, buf, &Font16, 0x0000, 0xffff); + + sprintf(buf, "H: %.2Lf %%rH", data.humidity / 1000.0l); + Paint_DrawString_EN(0, 32, buf, &Font16, 0x0000, 0xffff); + + //printf("%.2f,%.2f,%.2f\n", data.temperature / 100.0f, + // data.pressure / 100.0f, data.humidity / 1000.0f ); + /* Avoid using measurements from an unstable heating setup */ + if (data.status & BME680_GASM_VALID_MSK) { + printf(", G: %ld ohms", data.gas_resistance); + sprintf(buf, "G: %ld ohms", data.gas_resistance); + Paint_DrawString_EN( + 0, 48, buf, &Font16, 0x0000, 0xffff + ); + } + + LCD_Update(); + + printf("\n"); + + /* Trigger the next measurement if you would like to read data out continuously */ + if (gas_sensor.power_mode == BME680_FORCED_MODE) { + rslt = bme680_set_sensor_mode(&gas_sensor); + } + } } diff --git a/hw-tests/bootloader-update/main.c b/hw-tests/bootloader-update/main.c index 684890d2..9e96aa38 100644 --- a/hw-tests/bootloader-update/main.c +++ b/hw-tests/bootloader-update/main.c @@ -9,7 +9,6 @@ #include "GUI_Paint.h" #include "pmic.h" - #include "flc.h" #include "icc.h" #include "tmr_utils.h" @@ -37,47 +36,60 @@ static void pmic_button(bool falling) } } - int main(void) { + card10_init(); + pmic_set_button_callback(pmic_button); + + printf("Erasing bootloader.\n"); + Paint_DrawString_EN( + 0, 16 * 0, "Erasing bootloader", &Font16, 0x0000, 0xffff + ); + LCD_Update(); + + ICC_Disable(); + + int ret = FLC_MultiPageErase(0x10000000, 0x10000000 + 1024 * 64 - 1); + if (ret != E_NO_ERROR) { + printf("FLC_MultiPageErase failed with %d\n", ret); + Paint_DrawString_EN( + 0, 16 * 1, "Fail.", &Font16, 0x0000, 0xffff + ); + LCD_Update(); + while (1) + ; + } - card10_init(); - pmic_set_button_callback(pmic_button); - - printf("Erasing bootloader.\n"); - Paint_DrawString_EN(0, 16*0, "Erasing bootloader", &Font16, 0x0000, 0xffff); - LCD_Update(); - - ICC_Disable(); - - int ret = FLC_MultiPageErase(0x10000000, 0x10000000 + 1024*64-1); - if(ret != E_NO_ERROR) { - printf("FLC_MultiPageErase failed with %d\n", ret); - Paint_DrawString_EN(0, 16*1, "Fail.", &Font16, 0x0000, 0xffff); - LCD_Update(); - while(1); - } - - - printf("Writing bootloader.\n"); - Paint_DrawString_EN(0, 16*0, "Writing bootloader ", &Font16, 0x0000, 0xffff); - LCD_Update(); - - ret = FLC_Write(0x10000000, sizeof(bootloader_data), (uint32_t *)bootloader_data); - if(ret != E_NO_ERROR) { - printf("FLC_Write failed with %d\n", ret); - Paint_DrawString_EN(0, 16*1, "Fail.", &Font16, 0x0000, 0xffff); - LCD_Update(); - while(1); - } - ICC_Enable(); + printf("Writing bootloader.\n"); + Paint_DrawString_EN( + 0, 16 * 0, "Writing bootloader ", &Font16, 0x0000, 0xffff + ); + LCD_Update(); + + ret = FLC_Write( + 0x10000000, + sizeof(bootloader_data), + (uint32_t *)bootloader_data + ); + if (ret != E_NO_ERROR) { + printf("FLC_Write failed with %d\n", ret); + Paint_DrawString_EN( + 0, 16 * 1, "Fail.", &Font16, 0x0000, 0xffff + ); + LCD_Update(); + while (1) + ; + } + ICC_Enable(); - printf("Done.\n"); - Paint_DrawString_EN(0, 16*1, "Done.", &Font16, 0x0000, 0xffff); - Paint_DrawString_EN(0, 16*2, "Please restart", &Font16, 0x0000, 0xffff); - LCD_Update(); + printf("Done.\n"); + Paint_DrawString_EN(0, 16 * 1, "Done.", &Font16, 0x0000, 0xffff); + Paint_DrawString_EN( + 0, 16 * 2, "Please restart", &Font16, 0x0000, 0xffff + ); + LCD_Update(); - while (1) { - card10_poll(); - } + while (1) { + card10_poll(); + } } diff --git a/hw-tests/dual-core/core1.c b/hw-tests/dual-core/core1.c index 05bc29bf..1a917cff 100644 --- a/hw-tests/dual-core/core1.c +++ b/hw-tests/dual-core/core1.c @@ -2,7 +2,8 @@ #include "gpio.h" #include "mxc_delay.h" -static const __attribute__((unused)) gpio_cfg_t motor_pin = {PORT_0, PIN_8, GPIO_FUNC_OUT, GPIO_PAD_NONE}; +static const __attribute__((unused)) +gpio_cfg_t motor_pin = { PORT_0, PIN_8, GPIO_FUNC_OUT, GPIO_PAD_NONE }; int main(void) { diff --git a/hw-tests/ecgtest/main.c b/hw-tests/ecgtest/main.c index da01840f..965d325c 100644 --- a/hw-tests/ecgtest/main.c +++ b/hw-tests/ecgtest/main.c @@ -27,119 +27,120 @@ static bool ecg_switch; static bool internal_pull; -#define Y_OFFSET 36 -#define Y_SCALE 35 +#define Y_OFFSET 36 +#define Y_SCALE 35 /***** Functions *****/ static uint32_t ecg_read_reg(uint8_t reg) { - spi_req_t req; - uint8_t tx_data[] = {(reg << 1) | 1, 0, 0, 0}; - uint8_t rx_data[] = {0, 0, 0, 0}; - req.tx_data = tx_data; - req.rx_data = rx_data; - req.len = 4; - req.bits = 8; - req.width = SPI17Y_WIDTH_1; - req.ssel = 0; - req.deass = 1; - req.ssel_pol = SPI17Y_POL_LOW; - req.tx_num = 0; - req.rx_num = 0; - - SPI_MasterTrans(SPI0, &req); - - return (rx_data[1] << 16) | (rx_data[2] << 8) | rx_data[3]; + spi_req_t req; + uint8_t tx_data[] = { (reg << 1) | 1, 0, 0, 0 }; + uint8_t rx_data[] = { 0, 0, 0, 0 }; + req.tx_data = tx_data; + req.rx_data = rx_data; + req.len = 4; + req.bits = 8; + req.width = SPI17Y_WIDTH_1; + req.ssel = 0; + req.deass = 1; + req.ssel_pol = SPI17Y_POL_LOW; + req.tx_num = 0; + req.rx_num = 0; + + SPI_MasterTrans(SPI0, &req); + + return (rx_data[1] << 16) | (rx_data[2] << 8) | rx_data[3]; } static void ecg_write_reg(uint8_t reg, uint32_t data) { - printf("write %02x %06lx\n", reg, data); - spi_req_t req; - uint8_t tx_data[] = {(reg << 1) | 0 , data >> 16, (data >> 8 ) & 0xFF, data & 0xFF}; - uint8_t rx_data[] = {0, 0, 0, 0}; - req.tx_data = tx_data; - req.rx_data = rx_data; - req.len = 4; - req.bits = 8; - req.width = SPI17Y_WIDTH_1; - req.ssel = 0; - req.deass = 1; - req.ssel_pol = SPI17Y_POL_LOW; - req.tx_num = 0; - req.rx_num = 0; - - SPI_MasterTrans(SPI0, &req); + printf("write %02x %06lx\n", reg, data); + spi_req_t req; + uint8_t tx_data[] = { + (reg << 1) | 0, data >> 16, (data >> 8) & 0xFF, data & 0xFF + }; + uint8_t rx_data[] = { 0, 0, 0, 0 }; + req.tx_data = tx_data; + req.rx_data = rx_data; + req.len = 4; + req.bits = 8; + req.width = SPI17Y_WIDTH_1; + req.ssel = 0; + req.deass = 1; + req.ssel_pol = SPI17Y_POL_LOW; + req.tx_num = 0; + req.rx_num = 0; + + SPI_MasterTrans(SPI0, &req); } static void ecg_config(bool enable_internal_pull) { - // Reset ECG to clear registers - ecg_write_reg(SW_RST , 0); - - // General config register setting - union GeneralConfiguration_u CNFG_GEN_r; - CNFG_GEN_r.bits.en_ecg = 1; // Enable ECG channel - if(enable_internal_pull) { - CNFG_GEN_r.bits.rbiasn = 1; // Enable resistive bias on negative input - CNFG_GEN_r.bits.rbiasp = 1; // Enable resistive bias on positive input - CNFG_GEN_r.bits.en_rbias = 1; // Enable resistive bias - } else { - CNFG_GEN_r.bits.rbiasn = 0; // Enable resistive bias on negative input - CNFG_GEN_r.bits.rbiasp = 0; // Enable resistive bias on positive input - CNFG_GEN_r.bits.en_rbias = 0; // Enable resistive bias - } - - CNFG_GEN_r.bits.imag = 2; // Current magnitude = 10nA - CNFG_GEN_r.bits.en_dcloff = 1; // Enable DC lead-off detection - ecg_write_reg(CNFG_GEN , CNFG_GEN_r.all); - - - // ECG Config register setting - union ECGConfiguration_u CNFG_ECG_r; - CNFG_ECG_r.bits.dlpf = 1; // Digital LPF cutoff = 40Hz - CNFG_ECG_r.bits.dhpf = 1; // Digital HPF cutoff = 0.5Hz - //CNFG_ECG_r.bits.gain = 3; // ECG gain = 160V/V - CNFG_ECG_r.bits.gain = 0; - CNFG_ECG_r.bits.rate = 2; // Sample rate = 128 sps - //CNFG_ECG_r.bits.rate = 1; // Sample rate = 256 sps - ecg_write_reg(CNFG_ECG , CNFG_ECG_r.all); - - - //R-to-R configuration - union RtoR1Configuration_u CNFG_RTOR_r; - CNFG_RTOR_r.bits.en_rtor = 1; // Enable R-to-R detection - ecg_write_reg(CNFG_RTOR1 , CNFG_RTOR_r.all); - - - //Manage interrupts register setting - union ManageInterrupts_u MNG_INT_r; - MNG_INT_r.bits.efit = 0b00011; // Assert EINT w/ 4 unread samples - MNG_INT_r.bits.clr_rrint = 0b01; // Clear R-to-R on RTOR reg. read back - ecg_write_reg(MNGR_INT , MNG_INT_r.all); - - - //Enable interrupts register setting - union EnableInterrupts_u EN_INT_r; - EN_INT_r.all = 0; - EN_INT_r.bits.en_eint = 1; // Enable EINT interrupt - EN_INT_r.bits.en_rrint = 0; // Disable R-to-R interrupt - EN_INT_r.bits.intb_type = 3; // Open-drain NMOS with internal pullup - ecg_write_reg(EN_INT , EN_INT_r.all); - - - //Dyanmic modes config - union ManageDynamicModes_u MNG_DYN_r; - MNG_DYN_r.bits.fast = 0; // Fast recovery mode disabled - ecg_write_reg(MNGR_DYN , MNG_DYN_r.all); - - // MUX Config - union MuxConfiguration_u CNFG_MUX_r; - CNFG_MUX_r.bits.openn = 0; // Connect ECGN to AFE channel - CNFG_MUX_r.bits.openp = 0; // Connect ECGP to AFE channel - ecg_write_reg(CNFG_EMUX , CNFG_MUX_r.all); - - return; + // Reset ECG to clear registers + ecg_write_reg(SW_RST, 0); + + // General config register setting + union GeneralConfiguration_u CNFG_GEN_r; + CNFG_GEN_r.bits.en_ecg = 1; // Enable ECG channel + if (enable_internal_pull) { + CNFG_GEN_r.bits.rbiasn = + 1; // Enable resistive bias on negative input + CNFG_GEN_r.bits.rbiasp = + 1; // Enable resistive bias on positive input + CNFG_GEN_r.bits.en_rbias = 1; // Enable resistive bias + } else { + CNFG_GEN_r.bits.rbiasn = + 0; // Enable resistive bias on negative input + CNFG_GEN_r.bits.rbiasp = + 0; // Enable resistive bias on positive input + CNFG_GEN_r.bits.en_rbias = 0; // Enable resistive bias + } + + CNFG_GEN_r.bits.imag = 2; // Current magnitude = 10nA + CNFG_GEN_r.bits.en_dcloff = 1; // Enable DC lead-off detection + ecg_write_reg(CNFG_GEN, CNFG_GEN_r.all); + + // ECG Config register setting + union ECGConfiguration_u CNFG_ECG_r; + CNFG_ECG_r.bits.dlpf = 1; // Digital LPF cutoff = 40Hz + CNFG_ECG_r.bits.dhpf = 1; // Digital HPF cutoff = 0.5Hz + //CNFG_ECG_r.bits.gain = 3; // ECG gain = 160V/V + CNFG_ECG_r.bits.gain = 0; + CNFG_ECG_r.bits.rate = 2; // Sample rate = 128 sps + //CNFG_ECG_r.bits.rate = 1; // Sample rate = 256 sps + ecg_write_reg(CNFG_ECG, CNFG_ECG_r.all); + + //R-to-R configuration + union RtoR1Configuration_u CNFG_RTOR_r; + CNFG_RTOR_r.bits.en_rtor = 1; // Enable R-to-R detection + ecg_write_reg(CNFG_RTOR1, CNFG_RTOR_r.all); + + //Manage interrupts register setting + union ManageInterrupts_u MNG_INT_r; + MNG_INT_r.bits.efit = 0b00011; // Assert EINT w/ 4 unread samples + MNG_INT_r.bits.clr_rrint = 0b01; // Clear R-to-R on RTOR reg. read back + ecg_write_reg(MNGR_INT, MNG_INT_r.all); + + //Enable interrupts register setting + union EnableInterrupts_u EN_INT_r; + EN_INT_r.all = 0; + EN_INT_r.bits.en_eint = 1; // Enable EINT interrupt + EN_INT_r.bits.en_rrint = 0; // Disable R-to-R interrupt + EN_INT_r.bits.intb_type = 3; // Open-drain NMOS with internal pullup + ecg_write_reg(EN_INT, EN_INT_r.all); + + //Dyanmic modes config + union ManageDynamicModes_u MNG_DYN_r; + MNG_DYN_r.bits.fast = 0; // Fast recovery mode disabled + ecg_write_reg(MNGR_DYN, MNG_DYN_r.all); + + // MUX Config + union MuxConfiguration_u CNFG_MUX_r; + CNFG_MUX_r.bits.openn = 0; // Connect ECGN to AFE channel + CNFG_MUX_r.bits.openp = 0; // Connect ECGP to AFE channel + ecg_write_reg(CNFG_EMUX, CNFG_MUX_r.all); + + return; } #define SIZE_X 160 @@ -149,60 +150,63 @@ static uint8_t prev; static void clear(void) { - Paint_Clear(BLACK); - prev = Y_OFFSET; + Paint_Clear(BLACK); + prev = Y_OFFSET; } - static void set(uint8_t index, int8_t val) { - - if(val < -Y_SCALE) val = -Y_SCALE; - if(val > Y_SCALE) val = Y_SCALE; - - int8_t pos = Y_OFFSET + val; - - int min, max; - if(prev < pos) { - min = prev; - max = pos; - } else { - min = pos; - max = prev; - } - - for(int i = min; i < max + 1; i++) { - LCD_SetUWORD(SIZE_X - index - 1, i, RED); - } - - prev = pos; + if (val < -Y_SCALE) + val = -Y_SCALE; + if (val > Y_SCALE) + val = Y_SCALE; + + int8_t pos = Y_OFFSET + val; + + int min, max; + if (prev < pos) { + min = prev; + max = pos; + } else { + min = pos; + max = prev; + } + + for (int i = min; i < max + 1; i++) { + LCD_SetUWORD(SIZE_X - index - 1, i, RED); + } + + prev = pos; } static int16_t samples[SIZE_X]; void update(void) { - clear(); - - int16_t max = 0; - for(int i=0; i<SIZE_X; i++) { - if(abs(samples[i]) > max) { - max = abs(samples[i]); - } - } - - int16_t scale = max / Y_SCALE + 1; - - char buf[128]; - sprintf(buf, "Switch: %d Pull: %d Scale: %d", ecg_switch, internal_pull, scale); - Paint_DrawString_EN(0, 0, buf, &Font8, 0x0000, 0xffff); - - - for(int i=0; i<SIZE_X; i++) { - set(i, (samples[i] / scale)); - //set(i, ((samples[i*2] + samples[i*2 + 1]) / scale) / 2); - } - - LCD_Update(); + clear(); + + int16_t max = 0; + for (int i = 0; i < SIZE_X; i++) { + if (abs(samples[i]) > max) { + max = abs(samples[i]); + } + } + + int16_t scale = max / Y_SCALE + 1; + + char buf[128]; + sprintf(buf, + "Switch: %d Pull: %d Scale: %d", + ecg_switch, + internal_pull, + scale); + Paint_DrawString_EN(0, 0, buf, &Font8, 0x0000, 0xffff); + + for (int i = 0; i < SIZE_X; i++) { + set(i, (samples[i] / scale)); + //set(i, ((samples[i*2] + samples[i*2 + 1]) / scale) / 2); + } + + LCD_Update(); } #if 0 @@ -274,245 +278,220 @@ sampling frequency: 200 Hz #define FILTER_TAP_NUM 71 static double filter_taps[FILTER_TAP_NUM] = { - 0.0057445089465822975, - 0.007451288673767406, - -0.0011523652638272097, - -0.0030609271459832005, - -0.002596310763437956, - 0.004452951934981218, - 0.003740429140762826, - -0.002351310203707235, - -0.00638990322759006, - 0.000013108391204023357, - 0.007226767366250225, - 0.003994033360879168, - -0.00665189327484351, - -0.008058131222070393, - 0.0035814528837470965, - 0.011450459869389184, - 0.0016796020911726648, - -0.012657785603199918, - -0.008611714082779583, - 0.010587105712461298, - 0.01581354994840992, - -0.0044174953741535905, - -0.021419548904285074, - -0.005927897725436821, - 0.023159558089340265, - 0.01973520678092361, - -0.018604692334579287, - -0.035495574517452874, - 0.004958680854279203, - 0.05116349477789529, - 0.02244341207607204, - -0.06449371846227892, - -0.0790927656214483, - 0.07342794634140555, - 0.3089023630319309, - 0.42341742792869774, - 0.3089023630319309, - 0.07342794634140555, - -0.0790927656214483, - -0.06449371846227892, - 0.02244341207607204, - 0.05116349477789529, - 0.004958680854279203, - -0.035495574517452874, - -0.018604692334579287, - 0.01973520678092361, - 0.023159558089340265, - -0.005927897725436821, - -0.021419548904285074, - -0.0044174953741535905, - 0.01581354994840992, - 0.010587105712461298, - -0.008611714082779583, - -0.012657785603199918, - 0.0016796020911726648, - 0.011450459869389184, - 0.0035814528837470965, - -0.008058131222070393, - -0.00665189327484351, - 0.003994033360879168, - 0.007226767366250225, - 0.000013108391204023357, - -0.00638990322759006, - -0.002351310203707235, - 0.003740429140762826, - 0.004452951934981218, - -0.002596310763437956, - -0.0030609271459832005, - -0.0011523652638272097, - 0.007451288673767406, - 0.0057445089465822975 + 0.0057445089465822975, 0.007451288673767406, + -0.0011523652638272097, -0.0030609271459832005, + -0.002596310763437956, 0.004452951934981218, + 0.003740429140762826, -0.002351310203707235, + -0.00638990322759006, 0.000013108391204023357, + 0.007226767366250225, 0.003994033360879168, + -0.00665189327484351, -0.008058131222070393, + 0.0035814528837470965, 0.011450459869389184, + 0.0016796020911726648, -0.012657785603199918, + -0.008611714082779583, 0.010587105712461298, + 0.01581354994840992, -0.0044174953741535905, + -0.021419548904285074, -0.005927897725436821, + 0.023159558089340265, 0.01973520678092361, + -0.018604692334579287, -0.035495574517452874, + 0.004958680854279203, 0.05116349477789529, + 0.02244341207607204, -0.06449371846227892, + -0.0790927656214483, 0.07342794634140555, + 0.3089023630319309, 0.42341742792869774, + 0.3089023630319309, 0.07342794634140555, + -0.0790927656214483, -0.06449371846227892, + 0.02244341207607204, 0.05116349477789529, + 0.004958680854279203, -0.035495574517452874, + -0.018604692334579287, 0.01973520678092361, + 0.023159558089340265, -0.005927897725436821, + -0.021419548904285074, -0.0044174953741535905, + 0.01581354994840992, 0.010587105712461298, + -0.008611714082779583, -0.012657785603199918, + 0.0016796020911726648, 0.011450459869389184, + 0.0035814528837470965, -0.008058131222070393, + -0.00665189327484351, 0.003994033360879168, + 0.007226767366250225, 0.000013108391204023357, + -0.00638990322759006, -0.002351310203707235, + 0.003740429140762826, 0.004452951934981218, + -0.002596310763437956, -0.0030609271459832005, + -0.0011523652638272097, 0.007451288673767406, + 0.0057445089465822975 }; #endif int32_t fir(int32_t a) { - static int32_t x[FILTER_TAP_NUM]; - memmove(x + 1, x, sizeof(*x) * (FILTER_TAP_NUM-1)); - x[0] = a; - - int32_t y = 0; - int i; - for(i = 0; i < FILTER_TAP_NUM; i++) { - y += filter_taps[i] * x[i]; - } - return y; + static int32_t x[FILTER_TAP_NUM]; + memmove(x + 1, x, sizeof(*x) * (FILTER_TAP_NUM - 1)); + x[0] = a; + + int32_t y = 0; + int i; + for (i = 0; i < FILTER_TAP_NUM; i++) { + y += filter_taps[i] * x[i]; + } + return y; } static uint8_t sample_count = 0; static void add_sample(int16_t sample) { - memmove(samples, samples + 1, sizeof(*samples) * (SIZE_X-1)); - samples[SIZE_X-1] = sample; - sample_count++; - - if(sample_count == 5) { - sample_count = 0; - update(); - } + memmove(samples, samples + 1, sizeof(*samples) * (SIZE_X - 1)); + samples[SIZE_X - 1] = sample; + sample_count++; + + if (sample_count == 5) { + sample_count = 0; + update(); + } } static volatile bool ecgFIFOIntFlag = false; -static void ecgFIFO_callback(void *data) { - ecgFIFOIntFlag = true; +static void ecgFIFO_callback(void *data) +{ + ecgFIFOIntFlag = true; } // ***************************************************************************** int main(void) { + card10_init(); + card10_diag(); - card10_init(); - card10_diag(); - - const gpio_cfg_t interrupt_pin = {PORT_1, PIN_12, GPIO_FUNC_IN, GPIO_PAD_PULL_UP}; - GPIO_Config(&interrupt_pin); - GPIO_RegisterCallback(&interrupt_pin, ecgFIFO_callback, NULL); - GPIO_IntConfig(&interrupt_pin, GPIO_INT_EDGE, GPIO_INT_FALLING); - GPIO_IntEnable(&interrupt_pin); - NVIC_EnableIRQ(MXC_GPIO_GET_IRQ(PORT_1)); - - const gpio_cfg_t analog_switch = {PORT_0, PIN_31, GPIO_FUNC_OUT, GPIO_PAD_NONE}; - GPIO_Config(&analog_switch); - - - ecg_switch = false; - GPIO_OutClr(&analog_switch); // Wrist - - internal_pull = true; - - #if 1 - ecg_config(internal_pull); - - for(int i=0; i<0x20; i++) { - uint32_t val = ecg_read_reg(i); - printf("%02x: 0x%06lx\n", i, val); - } - - ecg_write_reg(SYNCH, 0); - #else - max86150_begin(); - max86150_setup(0x1F, 4, 3, 400, 411, 4096); - #endif - - - uint32_t ecgFIFO, readECGSamples, idx, ETAG[32], status; - int16_t ecgSample[32]; - const int EINT_STATUS_MASK = 1 << 23; - const int FIFO_OVF_MASK = 0x7; - const int FIFO_VALID_SAMPLE_MASK = 0x0; - const int FIFO_FAST_SAMPLE_MASK = 0x1; - const int ETAG_BITS_MASK = 0x7; - - - - while(1) { - #if 1 - // Read back ECG samples from the FIFO - if( ecgFIFOIntFlag ) { - ecgFIFOIntFlag = false; - - if(PB_Get(3)) { - ecg_switch = !ecg_switch; - while(PB_Get(0)); - if(ecg_switch) { - GPIO_OutSet(&analog_switch); // USB - } else { - GPIO_OutClr(&analog_switch); // Wrist - } - } - - if(PB_Get(4)) { - internal_pull =! internal_pull; - while(PB_Get(2)); - ecg_config(internal_pull); - } - - //printf("Int\n"); - status = ecg_read_reg( STATUS ); // Read the STATUS register - - // Check if EINT interrupt asserted - if ( ( status & EINT_STATUS_MASK ) == EINT_STATUS_MASK ) { - - readECGSamples = 0; // Reset sample counter - - do { - ecgFIFO = ecg_read_reg(ECG_FIFO ); // Read FIFO - ecgSample[readECGSamples] = ecgFIFO >> 8; // Isolate voltage data - ETAG[readECGSamples] = ( ecgFIFO >> 3 ) & ETAG_BITS_MASK; // Isolate ETAG - readECGSamples++; // Increment sample counter - - // Check that sample is not last sample in FIFO - } while ( ETAG[readECGSamples-1] == FIFO_VALID_SAMPLE_MASK || - ETAG[readECGSamples-1] == FIFO_FAST_SAMPLE_MASK ); - - // Check if FIFO has overflowed - if( ETAG[readECGSamples - 1] == FIFO_OVF_MASK ){ - ecg_write_reg(FIFO_RST , 0); // Reset FIFO - //printf("OV\n"); - // notifies the user that an over flow occured - //LED_On(0); - pmic_set_led(0, 31); - } - - // Print results - for( idx = 0; idx < readECGSamples; idx++ ) { - //printf("%6d\r\n", ecgSample[idx]); - add_sample(ecgSample[idx]); - } - } - } - #else - static float y1, y2, y3; - - static const float alpha = 0.001; - - if(max86150_check()>0) { - while(max86150_available()) { - uint32_t a = max86150_getFIFORed(); - uint32_t b = max86150_getFIFOIR(); - //if( a & (0x1 << 17) ) { - // a |= 0xFFFC0000; - //} - - //int32_t b = (int32_t)a; - //int32_t b = (int16_t)max86150_getFIFOEcg(); - int16_t c = (int16_t)max86150_getFIFOECG(); - c = fir(c); - - y1 = (1. - alpha) * y1 + alpha * a; - y2 = (1. - alpha) * y2 + alpha * b; - y3 = (1. - alpha) * y3 + alpha * c; - - //printf("%d,%d,%d\n", a - y1, b - y2, c - y3); - //printf("%d,%d,%d\n", a, b, c); - - add_sample(c - y3); - max86150_nextSample(); - } - } - #endif - } + const gpio_cfg_t interrupt_pin = { + PORT_1, PIN_12, GPIO_FUNC_IN, GPIO_PAD_PULL_UP + }; + GPIO_Config(&interrupt_pin); + GPIO_RegisterCallback(&interrupt_pin, ecgFIFO_callback, NULL); + GPIO_IntConfig(&interrupt_pin, GPIO_INT_EDGE, GPIO_INT_FALLING); + GPIO_IntEnable(&interrupt_pin); + NVIC_EnableIRQ(MXC_GPIO_GET_IRQ(PORT_1)); + + const gpio_cfg_t analog_switch = { + PORT_0, PIN_31, GPIO_FUNC_OUT, GPIO_PAD_NONE + }; + GPIO_Config(&analog_switch); + + ecg_switch = false; + GPIO_OutClr(&analog_switch); // Wrist + + internal_pull = true; + +#if 1 + ecg_config(internal_pull); + + for (int i = 0; i < 0x20; i++) { + uint32_t val = ecg_read_reg(i); + printf("%02x: 0x%06lx\n", i, val); + } + + ecg_write_reg(SYNCH, 0); +#else + max86150_begin(); + max86150_setup(0x1F, 4, 3, 400, 411, 4096); +#endif + + uint32_t ecgFIFO, readECGSamples, idx, ETAG[32], status; + int16_t ecgSample[32]; + const int EINT_STATUS_MASK = 1 << 23; + const int FIFO_OVF_MASK = 0x7; + const int FIFO_VALID_SAMPLE_MASK = 0x0; + const int FIFO_FAST_SAMPLE_MASK = 0x1; + const int ETAG_BITS_MASK = 0x7; + + while (1) { +#if 1 + // Read back ECG samples from the FIFO + if (ecgFIFOIntFlag) { + ecgFIFOIntFlag = false; + + if (PB_Get(3)) { + ecg_switch = !ecg_switch; + while (PB_Get(0)) + ; + if (ecg_switch) { + GPIO_OutSet(&analog_switch); // USB + } else { + GPIO_OutClr(&analog_switch); // Wrist + } + } + + if (PB_Get(4)) { + internal_pull = !internal_pull; + while (PB_Get(2)) + ; + ecg_config(internal_pull); + } + + //printf("Int\n"); + status = ecg_read_reg( + STATUS); // Read the STATUS register + + // Check if EINT interrupt asserted + if ((status & EINT_STATUS_MASK) == EINT_STATUS_MASK) { + readECGSamples = 0; // Reset sample counter + + do { + ecgFIFO = ecg_read_reg( + ECG_FIFO); // Read FIFO + ecgSample[readECGSamples] = + ecgFIFO >> + 8; // Isolate voltage data + ETAG[readECGSamples] = + (ecgFIFO >> 3) & + ETAG_BITS_MASK; // Isolate ETAG + readECGSamples++; // Increment sample counter + + // Check that sample is not last sample in FIFO + } while (ETAG[readECGSamples - 1] == + FIFO_VALID_SAMPLE_MASK || + ETAG[readECGSamples - 1] == + FIFO_FAST_SAMPLE_MASK); + + // Check if FIFO has overflowed + if (ETAG[readECGSamples - 1] == FIFO_OVF_MASK) { + ecg_write_reg( + FIFO_RST, 0); // Reset FIFO + //printf("OV\n"); + // notifies the user that an over flow occured + //LED_On(0); + pmic_set_led(0, 31); + } + + // Print results + for (idx = 0; idx < readECGSamples; idx++) { + //printf("%6d\r\n", ecgSample[idx]); + add_sample(ecgSample[idx]); + } + } + } +#else + static float y1, y2, y3; + + static const float alpha = 0.001; + + if (max86150_check() > 0) { + while (max86150_available()) { + uint32_t a = max86150_getFIFORed(); + uint32_t b = max86150_getFIFOIR(); + //if( a & (0x1 << 17) ) { + // a |= 0xFFFC0000; + //} + + //int32_t b = (int32_t)a; + //int32_t b = (int16_t)max86150_getFIFOEcg(); + int16_t c = (int16_t)max86150_getFIFOECG(); + c = fir(c); + + y1 = (1. - alpha) * y1 + alpha * a; + y2 = (1. - alpha) * y2 + alpha * b; + y3 = (1. - alpha) * y3 + alpha * c; + + //printf("%d,%d,%d\n", a - y1, b - y2, c - y3); + //printf("%d,%d,%d\n", a, b, c); + + add_sample(c - y3); + max86150_nextSample(); + } + } +#endif + } } diff --git a/hw-tests/hello-freertos/main.c b/hw-tests/hello-freertos/main.c index c0f74a29..e8b1b7a9 100644 --- a/hw-tests/hello-freertos/main.c +++ b/hw-tests/hello-freertos/main.c @@ -74,10 +74,10 @@ unsigned int disable_tickless = 1; #define STRING_(x) #x /* Console ISR selection */ -#if (CONSOLE_UART==0) +#if (CONSOLE_UART == 0) #define UARTx_IRQHandler UART0_IRQHandler #define UARTx_IRQn UART0_IRQn -#elif (CONSOLE_UART==1) +#elif (CONSOLE_UART == 1) #define UARTx_IRQHandler UART1_IRQHandler #define UARTx_IRQn UART1_IRQn #else @@ -86,8 +86,8 @@ unsigned int disable_tickless = 1; mxc_uart_regs_t *ConsoleUART = MXC_UART_GET_UART(CONSOLE_UART); /* Array sizes */ -#define CMD_LINE_BUF_SIZE 80 -#define OUTPUT_BUF_SIZE 512 +#define CMD_LINE_BUF_SIZE 80 +#define OUTPUT_BUF_SIZE 512 /* =| vTask0 |============================================ * @@ -100,31 +100,31 @@ mxc_uart_regs_t *ConsoleUART = MXC_UART_GET_UART(CONSOLE_UART); */ void vTask0(void *pvParameters) { - TickType_t xLastWakeTime; - unsigned int x = LED_OFF; - - /* Get task start time */ - xLastWakeTime = xTaskGetTickCount(); - - while (1) { - /* Protect hardware access with mutex + TickType_t xLastWakeTime; + unsigned int x = LED_OFF; + + /* Get task start time */ + xLastWakeTime = xTaskGetTickCount(); + + while (1) { + /* Protect hardware access with mutex * * Note: This is not strictly necessary, since GPIO_SetOutVal() is implemented with bit-band * access, which is inherently task-safe. However, for other drivers, this would be required. * */ - if (xSemaphoreTake(xGPIOmutex, portMAX_DELAY) == pdTRUE) { - if (x == LED_OFF) { - x = LED_ON; - } else { - x = LED_OFF; - } - /* Return the mutex after we have modified the hardware state */ - xSemaphoreGive(xGPIOmutex); - } - /* Wait 1 second until next run */ - vTaskDelayUntil(&xLastWakeTime, configTICK_RATE_HZ); - } + if (xSemaphoreTake(xGPIOmutex, portMAX_DELAY) == pdTRUE) { + if (x == LED_OFF) { + x = LED_ON; + } else { + x = LED_OFF; + } + /* Return the mutex after we have modified the hardware state */ + xSemaphoreGive(xGPIOmutex); + } + /* Wait 1 second until next run */ + vTaskDelayUntil(&xLastWakeTime, configTICK_RATE_HZ); + } } /* =| vTask1 |============================================ @@ -141,33 +141,33 @@ void vTask0(void *pvParameters) */ void vTask1(void *pvParameters) { - TickType_t xLastWakeTime; - unsigned int x = LED_ON; - - /* Get task start time */ - xLastWakeTime = xTaskGetTickCount(); - - while (1) { - /* Protect hardware access with mutex + TickType_t xLastWakeTime; + unsigned int x = LED_ON; + + /* Get task start time */ + xLastWakeTime = xTaskGetTickCount(); + + while (1) { + /* Protect hardware access with mutex * * Note: This is not strictly necessary, since GPIO_SetOutVal() is implemented with bit-band * access, which is inherently task-safe. However, for other drivers, this would be required. * */ - if (xSemaphoreTake(xGPIOmutex, portMAX_DELAY) == pdTRUE) { - if (x == LED_OFF) { - LED_On(0); - x = LED_ON; - } else { - LED_Off(0); - x = LED_OFF; - } - /* Return the mutex after we have modified the hardware state */ - xSemaphoreGive(xGPIOmutex); - } - /* Wait 1 second until next run */ - vTaskDelayUntil(&xLastWakeTime, configTICK_RATE_HZ); - } + if (xSemaphoreTake(xGPIOmutex, portMAX_DELAY) == pdTRUE) { + if (x == LED_OFF) { + LED_On(0); + x = LED_ON; + } else { + LED_Off(0); + x = LED_OFF; + } + /* Return the mutex after we have modified the hardware state */ + xSemaphoreGive(xGPIOmutex); + } + /* Wait 1 second until next run */ + vTaskDelayUntil(&xLastWakeTime, configTICK_RATE_HZ); + } } /* =| vTickTockTask |============================================ @@ -178,19 +178,20 @@ void vTask1(void *pvParameters) */ void vTickTockTask(void *pvParameters) { - TickType_t ticks = 0; - TickType_t xLastWakeTime; - - /* Get task start time */ - xLastWakeTime = xTaskGetTickCount(); - - while (1) { - ticks = xTaskGetTickCount(); - printf("Uptime is 0x%08lx (%lu seconds), tickless-idle is %s\n", - ticks, ticks / configTICK_RATE_HZ, - disable_tickless ? "disabled" : "ENABLED"); - vTaskDelayUntil(&xLastWakeTime, (configTICK_RATE_HZ * 60)); - } + TickType_t ticks = 0; + TickType_t xLastWakeTime; + + /* Get task start time */ + xLastWakeTime = xTaskGetTickCount(); + + while (1) { + ticks = xTaskGetTickCount(); + printf("Uptime is 0x%08lx (%lu seconds), tickless-idle is %s\n", + ticks, + ticks / configTICK_RATE_HZ, + disable_tickless ? "disabled" : "ENABLED"); + vTaskDelayUntil(&xLastWakeTime, (configTICK_RATE_HZ * 60)); + } } /* =| UART0_IRQHandler |====================================== @@ -203,7 +204,7 @@ void vTickTockTask(void *pvParameters) */ void UARTx_IRQHandler(void) { - UART_Handler(ConsoleUART); + UART_Handler(ConsoleUART); } /* =| vCmdLineTask_cb |====================================== @@ -215,12 +216,12 @@ void UARTx_IRQHandler(void) */ void vCmdLineTask_cb(uart_req_t *req, int error) { - BaseType_t xHigherPriorityTaskWoken; + BaseType_t xHigherPriorityTaskWoken; - /* Wake the task */ - xHigherPriorityTaskWoken = pdFALSE; - vTaskNotifyGiveFromISR(cmd_task_id, &xHigherPriorityTaskWoken); - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + /* Wake the task */ + xHigherPriorityTaskWoken = pdFALSE; + vTaskNotifyGiveFromISR(cmd_task_id, &xHigherPriorityTaskWoken); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); } /* =| vCmdLineTask |====================================== @@ -237,99 +238,114 @@ void vCmdLineTask_cb(uart_req_t *req, int error) */ void vCmdLineTask(void *pvParameters) { - unsigned char tmp; - unsigned int index; /* Index into buffer */ - unsigned int x; - char buffer[CMD_LINE_BUF_SIZE]; /* Buffer for input */ - char output[OUTPUT_BUF_SIZE]; /* Buffer for output */ - BaseType_t xMore; - uart_req_t async_read_req; - gpio_cfg_t uart_rx_pin = {PORT_0, PIN_10, GPIO_FUNC_IN, GPIO_PAD_NONE}; - - memset(buffer, 0, CMD_LINE_BUF_SIZE); - index = 0; - - /* Register available CLI commands */ - vRegisterCLICommands(); + unsigned char tmp; + unsigned int index; /* Index into buffer */ + unsigned int x; + char buffer[CMD_LINE_BUF_SIZE]; /* Buffer for input */ + char output[OUTPUT_BUF_SIZE]; /* Buffer for output */ + BaseType_t xMore; + uart_req_t async_read_req; + gpio_cfg_t uart_rx_pin = { PORT_0, PIN_10, GPIO_FUNC_IN, GPIO_PAD_NONE }; + + memset(buffer, 0, CMD_LINE_BUF_SIZE); + index = 0; + + /* Register available CLI commands */ + vRegisterCLICommands(); #if configUSE_TICKLESS_IDLE - /* Configure wake-up for GPIO pin corresponding to the UART RX line */ - LP_EnableGPIOWakeup(&uart_rx_pin); - GPIO_IntConfig(&uart_rx_pin, GPIO_INT_EDGE, GPIO_INT_FALLING); + /* Configure wake-up for GPIO pin corresponding to the UART RX line */ + LP_EnableGPIOWakeup(&uart_rx_pin); + GPIO_IntConfig(&uart_rx_pin, GPIO_INT_EDGE, GPIO_INT_FALLING); #endif - /* Enable UART0 interrupt */ - NVIC_ClearPendingIRQ(UARTx_IRQn); - NVIC_DisableIRQ(UARTx_IRQn); - NVIC_SetPriority(UARTx_IRQn, 1); - NVIC_EnableIRQ(UARTx_IRQn); - - /* Async read will be used to wake process */ - async_read_req.data = &tmp; - async_read_req.len = 1; - async_read_req.callback = vCmdLineTask_cb; - - printf("\nEnter 'help' to view a list of available commands.\n"); - printf("cmd> "); - fflush(stdout); - while (1) { - /* Register async read request */ - if (UART_ReadAsync(ConsoleUART, &async_read_req) != E_NO_ERROR) { - printf("Error registering async request. Command line unavailable.\n"); - vTaskDelay(portMAX_DELAY); - } - /* Hang here until ISR wakes us for a character */ - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); - /* Check that we have a valid character */ - if (async_read_req.num > 0) { - /* Process character */ - do { - if (tmp == 0x08) { - /* Backspace */ - if (index > 0) { - index--; - printf("\x08 \x08"); - } - fflush(stdout); - } else if (tmp == 0x03) { - /* ^C abort */ - index = 0; - printf("^C"); - printf("\ncmd> "); - fflush(stdout); - } else if ((tmp == '\r') || - (tmp == '\n')) { - printf("\r\n"); - /* Null terminate for safety */ - buffer[index] = 0x00; - /* Evaluate */ - do { - xMore = FreeRTOS_CLIProcessCommand(buffer, output, OUTPUT_BUF_SIZE); - /* If xMore == pdTRUE, then output buffer contains no null termination, so + /* Enable UART0 interrupt */ + NVIC_ClearPendingIRQ(UARTx_IRQn); + NVIC_DisableIRQ(UARTx_IRQn); + NVIC_SetPriority(UARTx_IRQn, 1); + NVIC_EnableIRQ(UARTx_IRQn); + + /* Async read will be used to wake process */ + async_read_req.data = &tmp; + async_read_req.len = 1; + async_read_req.callback = vCmdLineTask_cb; + + printf("\nEnter 'help' to view a list of available commands.\n"); + printf("cmd> "); + fflush(stdout); + while (1) { + /* Register async read request */ + if (UART_ReadAsync(ConsoleUART, &async_read_req) != + E_NO_ERROR) { + printf("Error registering async request. Command line unavailable.\n"); + vTaskDelay(portMAX_DELAY); + } + /* Hang here until ISR wakes us for a character */ + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + /* Check that we have a valid character */ + if (async_read_req.num > 0) { + /* Process character */ + do { + if (tmp == 0x08) { + /* Backspace */ + if (index > 0) { + index--; + printf("\x08 \x08"); + } + fflush(stdout); + } else if (tmp == 0x03) { + /* ^C abort */ + index = 0; + printf("^C"); + printf("\ncmd> "); + fflush(stdout); + } else if ((tmp == '\r') || (tmp == '\n')) { + printf("\r\n"); + /* Null terminate for safety */ + buffer[index] = 0x00; + /* Evaluate */ + do { + xMore = FreeRTOS_CLIProcessCommand( + buffer, + output, + OUTPUT_BUF_SIZE + ); + /* If xMore == pdTRUE, then output buffer contains no null termination, so * we know it is OUTPUT_BUF_SIZE. If pdFALSE, we can use strlen. */ - for (x = 0; x < (xMore == pdTRUE ? OUTPUT_BUF_SIZE : strlen(output)) ; x++) { - putchar(*(output+x)); - } - } while (xMore != pdFALSE); - /* New prompt */ - index = 0; - printf("\ncmd> "); - fflush(stdout); - } else if (index < CMD_LINE_BUF_SIZE) { - putchar(tmp); - buffer[index++] = tmp; - fflush(stdout); - } else { - /* Throw away data and beep terminal */ - putchar(0x07); - fflush(stdout); + for (x = 0; + x < + (xMore == pdTRUE ? + OUTPUT_BUF_SIZE : + strlen(output)); + x++) { + putchar(*(output + x)); + } + } while (xMore != pdFALSE); + /* New prompt */ + index = 0; + printf("\ncmd> "); + fflush(stdout); + } else if (index < CMD_LINE_BUF_SIZE) { + putchar(tmp); + buffer[index++] = tmp; + fflush(stdout); + } else { + /* Throw away data and beep terminal */ + putchar(0x07); + fflush(stdout); + } + /* If more characters are ready, process them here */ + } while ((UART_NumReadAvail(MXC_UART_GET_UART( + CONSOLE_UART)) > 0) && + UART_Read( + MXC_UART_GET_UART(CONSOLE_UART), + (uint8_t *)&tmp, + 1, + NULL) + ); + } } - /* If more characters are ready, process them here */ - } while ((UART_NumReadAvail(MXC_UART_GET_UART(CONSOLE_UART)) > 0) && - UART_Read(MXC_UART_GET_UART(CONSOLE_UART), (uint8_t *)&tmp, 1, NULL)); - } - } } #if configUSE_TICKLESS_IDLE @@ -342,17 +358,17 @@ void vCmdLineTask(void *pvParameters) */ int freertos_permit_tickless(void) { - if (disable_tickless == 1) { - return E_BUSY; - } + if (disable_tickless == 1) { + return E_BUSY; + } - return UART_PrepForSleep(MXC_UART_GET_UART(CONSOLE_UART)); + return UART_PrepForSleep(MXC_UART_GET_UART(CONSOLE_UART)); } #endif void RTC_IRQHandler(void) { - MXC_RTC->ctrl &= ~(MXC_F_RTC_CTRL_ALSF); + MXC_RTC->ctrl &= ~(MXC_F_RTC_CTRL_ALSF); } /* =| main |============================================== @@ -365,63 +381,84 @@ void RTC_IRQHandler(void) int main(void) { #if configUSE_TICKLESS_IDLE - uart_cfg_t uart_cfg = { - .parity = UART_PARITY_DISABLE, - .size = UART_DATA_SIZE_8_BITS, - .stop = UART_STOP_1, - .flow = UART_FLOW_CTRL_DIS, - .pol = UART_FLOW_POL_DIS, - .baud = 115200, - .clksel = UART_CLKSEL_SYSTEM - }; - sys_cfg_uart_t uart_sys_cfg = {MAP_A, Enable}; - - /* The RTC must be enabled for tickless operation */ - RTC_Init(MXC_RTC, 0, 0, NULL); - RTC_EnableRTCE(MXC_RTC); - NVIC_ClearPendingIRQ(RTC_IRQn); - NVIC_EnableIRQ(RTC_IRQn); - LP_EnableRTCAlarmWakeup(); - /* If running tickless idle, must reduce baud rate to avoid losing character */ - if (UART_Init(ConsoleUART, &uart_cfg, &uart_sys_cfg) != E_NO_ERROR) { - MXC_ASSERT_FAIL(); - } + uart_cfg_t uart_cfg = { .parity = UART_PARITY_DISABLE, + .size = UART_DATA_SIZE_8_BITS, + .stop = UART_STOP_1, + .flow = UART_FLOW_CTRL_DIS, + .pol = UART_FLOW_POL_DIS, + .baud = 115200, + .clksel = UART_CLKSEL_SYSTEM }; + sys_cfg_uart_t uart_sys_cfg = { MAP_A, Enable }; + + /* The RTC must be enabled for tickless operation */ + RTC_Init(MXC_RTC, 0, 0, NULL); + RTC_EnableRTCE(MXC_RTC); + NVIC_ClearPendingIRQ(RTC_IRQn); + NVIC_EnableIRQ(RTC_IRQn); + LP_EnableRTCAlarmWakeup(); + /* If running tickless idle, must reduce baud rate to avoid losing character */ + if (UART_Init(ConsoleUART, &uart_cfg, &uart_sys_cfg) != E_NO_ERROR) { + MXC_ASSERT_FAIL(); + } #endif - - /* Print banner (RTOS scheduler not running) */ - printf("\n-=- %s FreeRTOS (%s) Demo -=-\n", STRING(TARGET), tskKERNEL_VERSION_NUMBER); + + /* Print banner (RTOS scheduler not running) */ + printf("\n-=- %s FreeRTOS (%s) Demo -=-\n", + STRING(TARGET), + tskKERNEL_VERSION_NUMBER); #if configUSE_TICKLESS_IDLE - printf("Tickless idle is configured. Type 'tickless 1' to enable.\n"); + printf("Tickless idle is configured. Type 'tickless 1' to enable.\n"); #endif - /* Create mutexes */ - xGPIOmutex = xSemaphoreCreateMutex(); - if (xGPIOmutex == NULL) { - printf("xSemaphoreCreateMutex failed to create a mutex.\n"); - } else { - /* Configure task */ - if ((xTaskCreate(vTask0, (const char *)"Task0", - configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+1, NULL) != pdPASS) || - (xTaskCreate(vTask1, (const char *)"Task1", - configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+1, NULL) != pdPASS) || - (xTaskCreate(vTickTockTask, (const char *)"TickTock", - 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+2, NULL) != pdPASS) || - (xTaskCreate(vCmdLineTask, (const char *)"CmdLineTask", - configMINIMAL_STACK_SIZE+CMD_LINE_BUF_SIZE+OUTPUT_BUF_SIZE, NULL, tskIDLE_PRIORITY+1, &cmd_task_id) != pdPASS)) { - printf("xTaskCreate() failed to create a task.\n"); - } else { - /* Start scheduler */ - printf("Starting scheduler.\n"); - vTaskStartScheduler(); - } - } - - /* This code is only reached if the scheduler failed to start */ - printf("ERROR: FreeRTOS did not start due to above error!\n"); - while (1) { - __NOP(); - } - - /* Quiet GCC warnings */ - return -1; + /* Create mutexes */ + xGPIOmutex = xSemaphoreCreateMutex(); + if (xGPIOmutex == NULL) { + printf("xSemaphoreCreateMutex failed to create a mutex.\n"); + } else { + /* Configure task */ + if ((xTaskCreate( + vTask0, + (const char *)"Task0", + configMINIMAL_STACK_SIZE, + NULL, + tskIDLE_PRIORITY + 1, + NULL) != pdPASS) || + (xTaskCreate( + vTask1, + (const char *)"Task1", + configMINIMAL_STACK_SIZE, + NULL, + tskIDLE_PRIORITY + 1, + NULL) != pdPASS) || + (xTaskCreate( + vTickTockTask, + (const char *)"TickTock", + 2 * configMINIMAL_STACK_SIZE, + NULL, + tskIDLE_PRIORITY + 2, + NULL) != pdPASS) || + (xTaskCreate( + vCmdLineTask, + (const char *)"CmdLineTask", + configMINIMAL_STACK_SIZE + CMD_LINE_BUF_SIZE + + OUTPUT_BUF_SIZE, + NULL, + tskIDLE_PRIORITY + 1, + &cmd_task_id) != pdPASS)) { + printf("xTaskCreate() failed to create a task.\n"); + } else { + /* Start scheduler */ + printf("Starting scheduler.\n"); + vTaskStartScheduler(); + } + } + + /* This code is only reached if the scheduler failed to start */ + printf("ERROR: FreeRTOS did not start due to above error!\n"); + while (1) { + __NOP(); + } + + /* Quiet GCC warnings */ + return -1; } diff --git a/hw-tests/hello-world/main.c b/hw-tests/hello-world/main.c index 6581b97c..d5787c09 100644 --- a/hw-tests/hello-world/main.c +++ b/hw-tests/hello-world/main.c @@ -19,27 +19,28 @@ /***** Definitions *****/ /***** Globals *****/ -static __attribute__((unused)) const gpio_cfg_t motor_pin = {PORT_0, PIN_8, GPIO_FUNC_OUT, GPIO_PAD_NONE}; +static __attribute__((unused)) +const gpio_cfg_t motor_pin = { PORT_0, PIN_8, GPIO_FUNC_OUT, GPIO_PAD_NONE }; int main(void) { - int count = 0; + int count = 0; - card10_init(); - card10_diag(); + card10_init(); + card10_diag(); - Paint_DrawImage(Heart, 0, 0, 160, 80); - LCD_Update(); + Paint_DrawImage(Heart, 0, 0, 160, 80); + LCD_Update(); - for(int i=0; i<11; i++) { - leds_set_dim(i, 1); - } + for (int i = 0; i < 11; i++) { + leds_set_dim(i, 1); + } - int __attribute__((unused)) h = 0; - while (1) { + int __attribute__((unused)) h = 0; + while (1) { #if 0 - #define NUM 15 +#define NUM 15 for(int i=0; i<NUM; i++) { if(i < 12) { leds_set_hsv(i, (h + 360/NUM * i) % 360, 1., 1./8); @@ -53,25 +54,25 @@ int main(void) h++; #endif #if 1 - pmic_set_led(0, 31); - pmic_set_led(1, 0); - pmic_set_led(2, 0); - TMR_Delay(MXC_TMR0, MSEC(200), 0); - - pmic_set_led(0, 0); - pmic_set_led(1, 31); - pmic_set_led(2, 0); - TMR_Delay(MXC_TMR0, MSEC(200), 0); - - pmic_set_led(0, 0); - pmic_set_led(1, 0); - pmic_set_led(2, 31); - TMR_Delay(MXC_TMR0, MSEC(200), 0); - - pmic_set_led(0, 0); - pmic_set_led(1, 0); - pmic_set_led(2, 0); - //TMR_Delay(MXC_TMR0, MSEC(200), 0); + pmic_set_led(0, 31); + pmic_set_led(1, 0); + pmic_set_led(2, 0); + TMR_Delay(MXC_TMR0, MSEC(200), 0); + + pmic_set_led(0, 0); + pmic_set_led(1, 31); + pmic_set_led(2, 0); + TMR_Delay(MXC_TMR0, MSEC(200), 0); + + pmic_set_led(0, 0); + pmic_set_led(1, 0); + pmic_set_led(2, 31); + TMR_Delay(MXC_TMR0, MSEC(200), 0); + + pmic_set_led(0, 0); + pmic_set_led(1, 0); + pmic_set_led(2, 0); + //TMR_Delay(MXC_TMR0, MSEC(200), 0); #endif #if 0 //TMR_Delay(MXC_TMR0, MSEC(600), 0); @@ -87,8 +88,8 @@ int main(void) TMR_Delay(MXC_TMR0, MSEC(500), 0); #endif #if 1 - TMR_Delay(MXC_TMR0, MSEC(1000), 0); - printf("count = %d\n", count++); + TMR_Delay(MXC_TMR0, MSEC(1000), 0); + printf("count = %d\n", count++); #endif - } + } } diff --git a/hw-tests/imutest/main.c b/hw-tests/imutest/main.c index 8b5f1ec4..7e3db466 100644 --- a/hw-tests/imutest/main.c +++ b/hw-tests/imutest/main.c @@ -23,113 +23,113 @@ #include <string.h> #include <stdbool.h> -#define M_PI 3.1415 +#define M_PI 3.1415 /***** Definitions *****/ /* should be greater or equal to 69 bytes, page size (50) + maximum packet size(18) + 1 */ -#define FIFO_SIZE 300 -#define ROTATION_VECTOR_SAMPLE_RATE 10 -#define MAX_PACKET_LENGTH 18 -#define OUT_BUFFER_SIZE 60 - +#define FIFO_SIZE 300 +#define ROTATION_VECTOR_SAMPLE_RATE 10 +#define MAX_PACKET_LENGTH 18 +#define OUT_BUFFER_SIZE 60 /***** Globals *****/ -char out_buffer[OUT_BUFFER_SIZE] = " W: 0.999 X: 0.999 Y: 0.999 Z: 0.999 \r"; +char out_buffer[OUT_BUFFER_SIZE] = + " W: 0.999 X: 0.999 Y: 0.999 Z: 0.999 \r"; uint8_t fifo[FIFO_SIZE]; void draw_arrow(int angle, int color) { - float sin = sinf(angle * 2 * M_PI / 360.); - float cos = cosf(angle * 2 * M_PI / 360.); + float sin = sinf(angle * 2 * M_PI / 360.); + float cos = cosf(angle * 2 * M_PI / 360.); - int x1 = 160/2 + 30; - int y1 = 80/2; + int x1 = 160 / 2 + 30; + int y1 = 80 / 2; - int x2 = x1 - sin * 30; - int y2 = y1 - cos * 30; + int x2 = x1 - sin * 30; + int y2 = y1 - cos * 30; - Paint_DrawLine(x1, y1, x2, y2, color, LINE_STYLE_SOLID, DOT_PIXEL_2X2); + Paint_DrawLine(x1, y1, x2, y2, color, LINE_STYLE_SOLID, DOT_PIXEL_2X2); - sin = sinf((angle - 140) * 2 * M_PI / 360.); - cos = cosf((angle - 140) * 2 * M_PI / 360.); + sin = sinf((angle - 140) * 2 * M_PI / 360.); + cos = cosf((angle - 140) * 2 * M_PI / 360.); - int x3 = x2 - sin * 10; - int y3 = y2 - cos * 10; + int x3 = x2 - sin * 10; + int y3 = y2 - cos * 10; - Paint_DrawLine(x2, y2, x3, y3, color, LINE_STYLE_SOLID, DOT_PIXEL_2X2); + Paint_DrawLine(x2, y2, x3, y3, color, LINE_STYLE_SOLID, DOT_PIXEL_2X2); - sin = sinf((angle + 140) * 2 * M_PI / 360.); - cos = cosf((angle + 140) * 2 * M_PI / 360.); + sin = sinf((angle + 140) * 2 * M_PI / 360.); + cos = cosf((angle + 140) * 2 * M_PI / 360.); - int x4 = x2 - sin * 10; - int y4 = y2 - cos * 10; + int x4 = x2 - sin * 10; + int y4 = y2 - cos * 10; - Paint_DrawLine(x2, y2, x4, y4, color, LINE_STYLE_SOLID, DOT_PIXEL_2X2); + Paint_DrawLine(x2, y2, x4, y4, color, LINE_STYLE_SOLID, DOT_PIXEL_2X2); } /***** Functions *****/ -static void sensors_callback_orientation(bhy_data_generic_t * sensor_data, bhy_virtual_sensor_t sensor_id) -{ - static int prev = -1; - printf("azimuth=%05d, pitch=%05d, roll=%05d status=%d\n", - sensor_data->data_vector.x * 360 / 32768, - sensor_data->data_vector.y * 360 / 32768, - sensor_data->data_vector.z * 360 / 32768, - sensor_data->data_vector.status - ); - - int angle = sensor_data->data_vector.x * 360 / 32768; - - if(angle != prev) { - Paint_Clear(BLACK); - int colors[] = {RED, YELLOW, YELLOW, GREEN}; - int color = colors[sensor_data->data_vector.status]; - draw_arrow(sensor_data->data_vector.x * 360 / 32768, color); - - char buf[128]; - //sprintf(buf, "Azimuth: %3d", angle); - //Paint_DrawString_EN(0, 0, buf, &Font12, BLACK, color); - - sprintf(buf, "%3d", angle); - Paint_DrawString_EN(0, 30, buf, &Font24, BLACK, color); - Paint_DrawCircle(57,35, 4, color, DRAW_FILL_EMPTY, DOT_PIXEL_1X1); - - - LCD_Update(); - prev = angle; - } +static void sensors_callback_orientation( + bhy_data_generic_t *sensor_data, bhy_virtual_sensor_t sensor_id +) { + static int prev = -1; + printf("azimuth=%05d, pitch=%05d, roll=%05d status=%d\n", + sensor_data->data_vector.x * 360 / 32768, + sensor_data->data_vector.y * 360 / 32768, + sensor_data->data_vector.z * 360 / 32768, + sensor_data->data_vector.status); + + int angle = sensor_data->data_vector.x * 360 / 32768; + + if (angle != prev) { + Paint_Clear(BLACK); + int colors[] = { RED, YELLOW, YELLOW, GREEN }; + int color = colors[sensor_data->data_vector.status]; + draw_arrow(sensor_data->data_vector.x * 360 / 32768, color); + + char buf[128]; + //sprintf(buf, "Azimuth: %3d", angle); + //Paint_DrawString_EN(0, 0, buf, &Font12, BLACK, color); + + sprintf(buf, "%3d", angle); + Paint_DrawString_EN(0, 30, buf, &Font24, BLACK, color); + Paint_DrawCircle( + 57, 35, 4, color, DRAW_FILL_EMPTY, DOT_PIXEL_1X1 + ); + + LCD_Update(); + prev = angle; + } } - -static __attribute__((unused)) void sensors_callback_vector(bhy_data_generic_t * sensor_data, bhy_virtual_sensor_t sensor_id) -{ - printf("x=%05d, y=%05d, z=%05d status=%d\n", - sensor_data->data_vector.x, - sensor_data->data_vector.y, - sensor_data->data_vector.z, - sensor_data->data_vector.status - ); +static __attribute__((unused)) void sensors_callback_vector( + bhy_data_generic_t *sensor_data, bhy_virtual_sensor_t sensor_id +) { + printf("x=%05d, y=%05d, z=%05d status=%d\n", + sensor_data->data_vector.x, + sensor_data->data_vector.y, + sensor_data->data_vector.z, + sensor_data->data_vector.status); } -static __attribute__((unused)) void sensors_callback_vector_uncalib(bhy_data_generic_t * sensor_data, bhy_virtual_sensor_t sensor_id) -{ - printf("x=%05d, y=%05d, z=%05d status=%d\n", - sensor_data->data_uncalib_vector.x, - sensor_data->data_uncalib_vector.y, - sensor_data->data_uncalib_vector.z, - sensor_data->data_uncalib_vector.status - ); +static __attribute__((unused)) void sensors_callback_vector_uncalib( + bhy_data_generic_t *sensor_data, bhy_virtual_sensor_t sensor_id +) { + printf("x=%05d, y=%05d, z=%05d status=%d\n", + sensor_data->data_uncalib_vector.x, + sensor_data->data_uncalib_vector.y, + sensor_data->data_uncalib_vector.z, + sensor_data->data_uncalib_vector.status); } - /*! * @brief This function is callback function for acquring sensor datas * * @param[in] sensor_data * @param[in] sensor_id */ -static __attribute__((unused)) void sensors_callback_rotation_vector(bhy_data_generic_t * sensor_data, bhy_virtual_sensor_t sensor_id) -{ +static __attribute__((unused)) void sensors_callback_rotation_vector( + bhy_data_generic_t *sensor_data, bhy_virtual_sensor_t sensor_id +) { #if 0 float temp; uint8_t index; @@ -179,28 +179,27 @@ static __attribute__((unused)) void sensors_callback_rotation_vector(bhy_data_ge } #endif - printf("x=%d, y=%d, z=%d, w=%d\n", - sensor_data->data_quaternion.x, - sensor_data->data_quaternion.y, - sensor_data->data_quaternion.z, - sensor_data->data_quaternion.w - ); + printf("x=%d, y=%d, z=%d, w=%d\n", + sensor_data->data_quaternion.x, + sensor_data->data_quaternion.y, + sensor_data->data_quaternion.z, + sensor_data->data_quaternion.w); } // ***************************************************************************** int main(void) { - /* BHY Variable*/ - uint8_t *fifoptr = NULL; - uint8_t bytes_left_in_fifo = 0; - uint16_t bytes_remaining = 0; - uint16_t bytes_read = 0; - bhy_data_generic_t fifo_packet; - bhy_data_type_t packet_type; - BHY_RETURN_FUNCTION_TYPE result; - - card10_init(); - card10_diag(); + /* BHY Variable*/ + uint8_t *fifoptr = NULL; + uint8_t bytes_left_in_fifo = 0; + uint16_t bytes_remaining = 0; + uint16_t bytes_read = 0; + bhy_data_generic_t fifo_packet; + bhy_data_type_t packet_type; + BHY_RETURN_FUNCTION_TYPE result; + + card10_init(); + card10_diag(); #if 0 /* install the callback function for parse fifo data */ @@ -210,14 +209,16 @@ int main(void) } #endif - //if(bhy_install_sensor_callback(VS_TYPE_GEOMAGNETIC_FIELD, VS_WAKEUP, sensors_callback_vector)) - //if(bhy_install_sensor_callback(VS_TYPE_GRAVITY, VS_WAKEUP, sensors_callback_vector)) - //if(bhy_install_sensor_callback(VS_TYPE_ACCELEROMETER, VS_WAKEUP, sensors_callback_vector)) - //if(bhy_install_sensor_callback(VS_TYPE_MAGNETIC_FIELD_UNCALIBRATED, VS_WAKEUP, sensors_callback_vector_uncalib)) - if(bhy_install_sensor_callback(VS_TYPE_ORIENTATION, VS_WAKEUP, sensors_callback_orientation)) - { - printf("Fail to install sensor callback\n"); - } + //if(bhy_install_sensor_callback(VS_TYPE_GEOMAGNETIC_FIELD, VS_WAKEUP, sensors_callback_vector)) + //if(bhy_install_sensor_callback(VS_TYPE_GRAVITY, VS_WAKEUP, sensors_callback_vector)) + //if(bhy_install_sensor_callback(VS_TYPE_ACCELEROMETER, VS_WAKEUP, sensors_callback_vector)) + //if(bhy_install_sensor_callback(VS_TYPE_MAGNETIC_FIELD_UNCALIBRATED, VS_WAKEUP, sensors_callback_vector_uncalib)) + if (bhy_install_sensor_callback( + VS_TYPE_ORIENTATION, + VS_WAKEUP, + sensors_callback_orientation)) { + printf("Fail to install sensor callback\n"); + } #if 0 /* enables the virtual sensor */ @@ -227,54 +228,69 @@ int main(void) } #endif - - /* enables the virtual sensor */ - //if(bhy_enable_virtual_sensor(VS_TYPE_GEOMAGNETIC_FIELD, VS_WAKEUP, ROTATION_VECTOR_SAMPLE_RATE, 0, VS_FLUSH_NONE, 0, 0)) - //if(bhy_enable_virtual_sensor(VS_TYPE_GRAVITY, VS_WAKEUP, ROTATION_VECTOR_SAMPLE_RATE, 0, VS_FLUSH_NONE, 0, 0)) - //if(bhy_enable_virtual_sensor(VS_TYPE_ACCELEROMETER, VS_WAKEUP, ROTATION_VECTOR_SAMPLE_RATE, 0, VS_FLUSH_NONE, 0, 0)) - //if(bhy_enable_virtual_sensor(VS_TYPE_MAGNETIC_FIELD_UNCALIBRATED, VS_WAKEUP, ROTATION_VECTOR_SAMPLE_RATE, 0, VS_FLUSH_NONE, 0, 0)) - if(bhy_enable_virtual_sensor(VS_TYPE_ORIENTATION, VS_WAKEUP, ROTATION_VECTOR_SAMPLE_RATE, 0, VS_FLUSH_NONE, 0, 0)) - { - printf("Fail to enable sensor id=%d\n", VS_TYPE_GEOMAGNETIC_FIELD); - } - - - while(1) - { - /* wait until the interrupt fires */ - /* unless we already know there are bytes remaining in the fifo */ - while (!GPIO_InGet(&bhi_interrupt_pin) && !bytes_remaining); - - bhy_read_fifo(fifo + bytes_left_in_fifo, FIFO_SIZE - bytes_left_in_fifo, &bytes_read, &bytes_remaining); - bytes_read += bytes_left_in_fifo; - fifoptr = fifo; - packet_type = BHY_DATA_TYPE_PADDING; - - do - { - /* this function will call callbacks that are registered */ - result = bhy_parse_next_fifo_packet(&fifoptr, &bytes_read, &fifo_packet, &packet_type); - - /* prints all the debug packets */ - if (packet_type == BHY_DATA_TYPE_DEBUG) - { - bhy_print_debug_packet(&fifo_packet.data_debug, bhy_printf); - } - - /* the logic here is that if doing a partial parsing of the fifo, then we should not parse */ - /* the last 18 bytes (max length of a packet) so that we don't try to parse an incomplete */ - /* packet */ - } while ((result == BHY_SUCCESS) && (bytes_read > (bytes_remaining ? MAX_PACKET_LENGTH : 0))); - - bytes_left_in_fifo = 0; - - if (bytes_remaining) - { - /* shifts the remaining bytes to the beginning of the buffer */ - while (bytes_left_in_fifo < bytes_read) - { - fifo[bytes_left_in_fifo++] = *(fifoptr++); - } - } - } + /* enables the virtual sensor */ + //if(bhy_enable_virtual_sensor(VS_TYPE_GEOMAGNETIC_FIELD, VS_WAKEUP, ROTATION_VECTOR_SAMPLE_RATE, 0, VS_FLUSH_NONE, 0, 0)) + //if(bhy_enable_virtual_sensor(VS_TYPE_GRAVITY, VS_WAKEUP, ROTATION_VECTOR_SAMPLE_RATE, 0, VS_FLUSH_NONE, 0, 0)) + //if(bhy_enable_virtual_sensor(VS_TYPE_ACCELEROMETER, VS_WAKEUP, ROTATION_VECTOR_SAMPLE_RATE, 0, VS_FLUSH_NONE, 0, 0)) + //if(bhy_enable_virtual_sensor(VS_TYPE_MAGNETIC_FIELD_UNCALIBRATED, VS_WAKEUP, ROTATION_VECTOR_SAMPLE_RATE, 0, VS_FLUSH_NONE, 0, 0)) + if (bhy_enable_virtual_sensor( + VS_TYPE_ORIENTATION, + VS_WAKEUP, + ROTATION_VECTOR_SAMPLE_RATE, + 0, + VS_FLUSH_NONE, + 0, + 0)) { + printf("Fail to enable sensor id=%d\n", + VS_TYPE_GEOMAGNETIC_FIELD); + } + + while (1) { + /* wait until the interrupt fires */ + /* unless we already know there are bytes remaining in the fifo */ + while (!GPIO_InGet(&bhi_interrupt_pin) && !bytes_remaining) + ; + + bhy_read_fifo( + fifo + bytes_left_in_fifo, + FIFO_SIZE - bytes_left_in_fifo, + &bytes_read, + &bytes_remaining + ); + bytes_read += bytes_left_in_fifo; + fifoptr = fifo; + packet_type = BHY_DATA_TYPE_PADDING; + + do { + /* this function will call callbacks that are registered */ + result = bhy_parse_next_fifo_packet( + &fifoptr, + &bytes_read, + &fifo_packet, + &packet_type + ); + + /* prints all the debug packets */ + if (packet_type == BHY_DATA_TYPE_DEBUG) { + bhy_print_debug_packet( + &fifo_packet.data_debug, bhy_printf + ); + } + + /* the logic here is that if doing a partial parsing of the fifo, then we should not parse */ + /* the last 18 bytes (max length of a packet) so that we don't try to parse an incomplete */ + /* packet */ + } while ((result == BHY_SUCCESS) && + (bytes_read > + (bytes_remaining ? MAX_PACKET_LENGTH : 0))); + + bytes_left_in_fifo = 0; + + if (bytes_remaining) { + /* shifts the remaining bytes to the beginning of the buffer */ + while (bytes_left_in_fifo < bytes_read) { + fifo[bytes_left_in_fifo++] = *(fifoptr++); + } + } + } } diff --git a/hw-tests/ips/main.c b/hw-tests/ips/main.c index e47610b7..a4fd5ad7 100644 --- a/hw-tests/ips/main.c +++ b/hw-tests/ips/main.c @@ -16,30 +16,35 @@ #include <stdint.h> #include <string.h> - // ***************************************************************************** int main(void) { - int count = 0; - - card10_init(); - card10_diag(); - - Paint_DrawString_EN(0, 0, "123", &Font24, 0x000f, 0xfff0); - Paint_DrawString_EN(0, 23, "ABC", &Font24, BLUE, CYAN); - Paint_DrawString_CN(20,42, "΢ѩµç×Ó", &Font24CN, WHITE, RED); - Paint_DrawRectangle(70, 10, 100, 40, RED,DRAW_FILL_EMPTY, DOT_PIXEL_2X2 ); - Paint_DrawLine(70, 10, 100, 40, MAGENTA, LINE_STYLE_SOLID, DOT_PIXEL_2X2); - Paint_DrawLine(100, 10, 70, 40, MAGENTA, LINE_STYLE_SOLID, DOT_PIXEL_2X2); - Paint_DrawCircle(85,25, 22, GREEN, DRAW_FILL_EMPTY, DOT_PIXEL_2X2); - - Paint_DrawImage(gImage_40X40,120, 0,40, 40); - Paint_DrawImage(gImage_160X80,0, 0, 160, 80); - LCD_Update(); - DEV_Delay_ms(3000); - - while (1) { - TMR_Delay(MXC_TMR0, MSEC(1000), 0); - printf("count = %d\n", count++); - } + int count = 0; + + card10_init(); + card10_diag(); + + Paint_DrawString_EN(0, 0, "123", &Font24, 0x000f, 0xfff0); + Paint_DrawString_EN(0, 23, "ABC", &Font24, BLUE, CYAN); + Paint_DrawString_CN(20, 42, "΢ѩµç×Ó", &Font24CN, WHITE, RED); + Paint_DrawRectangle( + 70, 10, 100, 40, RED, DRAW_FILL_EMPTY, DOT_PIXEL_2X2 + ); + Paint_DrawLine( + 70, 10, 100, 40, MAGENTA, LINE_STYLE_SOLID, DOT_PIXEL_2X2 + ); + Paint_DrawLine( + 100, 10, 70, 40, MAGENTA, LINE_STYLE_SOLID, DOT_PIXEL_2X2 + ); + Paint_DrawCircle(85, 25, 22, GREEN, DRAW_FILL_EMPTY, DOT_PIXEL_2X2); + + Paint_DrawImage(gImage_40X40, 120, 0, 40, 40); + Paint_DrawImage(gImage_160X80, 0, 0, 160, 80); + LCD_Update(); + DEV_Delay_ms(3000); + + while (1) { + TMR_Delay(MXC_TMR0, MSEC(1000), 0); + printf("count = %d\n", count++); + } } diff --git a/hw-tests/upy-minimal/main.c b/hw-tests/upy-minimal/main.c index 22d88bdf..f6e016e4 100644 --- a/hw-tests/upy-minimal/main.c +++ b/hw-tests/upy-minimal/main.c @@ -18,55 +18,69 @@ static char heap[4096]; int mp_hal_stdin_rx_chr(void); -int main(int argc, char **argv) { - int stack_dummy; - stack_top = (char*)&stack_dummy; - leds_init(); +int main(int argc, char **argv) +{ + int stack_dummy; + stack_top = (char *)&stack_dummy; + leds_init(); - #if MICROPY_ENABLE_GC - gc_init(heap, heap + sizeof(heap)); - #endif +#if MICROPY_ENABLE_GC + gc_init(heap, heap + sizeof(heap)); +#endif - mp_init(); - pyexec_friendly_repl(); - mp_deinit(); - return 0; + mp_init(); + pyexec_friendly_repl(); + mp_deinit(); + return 0; } -void gc_collect(void) { - // WARNING: This gc_collect implementation doesn't try to get root - // pointers from CPU registers, and thus may function incorrectly. - void *dummy; - gc_collect_start(); - gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); - gc_collect_end(); - gc_dump_info(); +void gc_collect(void) +{ + // WARNING: This gc_collect implementation doesn't try to get root + // pointers from CPU registers, and thus may function incorrectly. + void *dummy; + gc_collect_start(); + gc_collect_root( + &dummy, + ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t) + ); + gc_collect_end(); + gc_dump_info(); } -mp_lexer_t *mp_lexer_new_from_file(const char *filename) { - mp_raise_OSError(MP_ENOENT); +mp_lexer_t *mp_lexer_new_from_file(const char *filename) +{ + mp_raise_OSError(MP_ENOENT); } -mp_import_stat_t mp_import_stat(const char *path) { - return MP_IMPORT_STAT_NO_EXIST; +mp_import_stat_t mp_import_stat(const char *path) +{ + return MP_IMPORT_STAT_NO_EXIST; } -mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { - return mp_const_none; +mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) +{ + return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); -void nlr_jump_fail(void *val) { - while (1); +void nlr_jump_fail(void *val) +{ + while (1) + ; } -void NORETURN __fatal_error(const char *msg) { - while (1); +void NORETURN __fatal_error(const char *msg) +{ + while (1) + ; } #ifndef NDEBUG -void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) { - printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); - __fatal_error("Assertion failed"); +void MP_WEAK +__assert_func(const char *file, int line, const char *func, const char *expr) +{ + printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); + __fatal_error("Assertion failed"); } #endif diff --git a/hw-tests/upy-minimal/modules/buzzer.c b/hw-tests/upy-minimal/modules/buzzer.c index e2c8fcae..8ece2ca4 100644 --- a/hw-tests/upy-minimal/modules/buzzer.c +++ b/hw-tests/upy-minimal/modules/buzzer.c @@ -13,7 +13,7 @@ STATIC mp_obj_t buzzer_set(mp_obj_t state_obj) if (state_obj == mp_const_true) { printf("Buzzer ON!\n"); GPIO_OutSet(&motor_pin); - } else if (state_obj == mp_const_false){ + } else if (state_obj == mp_const_false) { printf("Buzzer OFF!\n"); GPIO_OutClr(&motor_pin); } else { @@ -36,8 +36,8 @@ STATIC MP_DEFINE_CONST_DICT(buzzer_module_globals, buzzer_module_globals_table); // Define module object. const mp_obj_module_t buzzer_module = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&buzzer_module_globals, + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&buzzer_module_globals, }; // Register the module to make it available in Python diff --git a/hw-tests/upy-minimal/modules/leds.c b/hw-tests/upy-minimal/modules/leds.c index f7a670fe..87645d2a 100644 --- a/hw-tests/upy-minimal/modules/leds.c +++ b/hw-tests/upy-minimal/modules/leds.c @@ -5,22 +5,19 @@ STATIC mp_obj_t mp_leds_set(size_t n_args, const mp_obj_t *args) { - assert (n_args == 4); - int led = mp_obj_get_int (args[0]); - int r = mp_obj_get_int (args[1]); - int g = mp_obj_get_int (args[2]); - int b = mp_obj_get_int (args[3]); - - if ( (0 > led || 256 < led) && - (0 > r || 256 < r) && - (0 > g || 256 < g) && - (0 > b || 256 < b) ) - { + assert(n_args == 4); + int led = mp_obj_get_int(args[0]); + int r = mp_obj_get_int(args[1]); + int g = mp_obj_get_int(args[2]); + int b = mp_obj_get_int(args[3]); + + if ((0 > led || 256 < led) && (0 > r || 256 < r) && + (0 > g || 256 < g) && (0 > b || 256 < b)) { mp_raise_ValueError("out of bounds"); } printf("Set %u to (%x %x %x)\n", led, r, g, b); - leds_set (led, r, g, b); - leds_update (); + leds_set(led, r, g, b); + leds_update(); return mp_const_none; } @@ -28,15 +25,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(leds_set_obj, 4, 4, mp_leds_set); STATIC mp_obj_t mp_leds_set_dim(mp_obj_t led_obj, mp_obj_t dim_obj) { - int led = mp_obj_get_int (led_obj); - int dim = mp_obj_get_int (dim_obj); + int led = mp_obj_get_int(led_obj); + int dim = mp_obj_get_int(dim_obj); - if ( (0 > led || 256 < led) && - (0 > dim || 256 < dim) ) - { + if ((0 > led || 256 < led) && (0 > dim || 256 < dim)) { mp_raise_ValueError("out of bounds"); } - leds_set_dim (led, dim); + leds_set_dim(led, dim); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_2(leds_set_dim_obj, mp_leds_set_dim); @@ -55,8 +50,8 @@ STATIC MP_DEFINE_CONST_DICT(leds_module_globals, leds_module_globals_table); // Define module object. const mp_obj_module_t leds_module = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&leds_module_globals, + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&leds_module_globals, }; // Register the module to make it available in Python diff --git a/hw-tests/upy-minimal/modules/utime.c b/hw-tests/upy-minimal/modules/utime.c index 98aca15a..e47305be 100644 --- a/hw-tests/upy-minimal/modules/utime.c +++ b/hw-tests/upy-minimal/modules/utime.c @@ -8,22 +8,23 @@ #include "extmod/utime_mphal.h" STATIC const mp_rom_map_elem_t time_module_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) }, - { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) }, - { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) }, - { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) }, + { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) }, + { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) }, + { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_diff), + MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, }; STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); const mp_obj_module_t mp_module_utime = { - .base = { &mp_type_module }, - .globals = (mp_obj_dict_t*)&time_module_globals, + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&time_module_globals, }; // Register the module to make it available in Python diff --git a/hw-tests/upy-minimal/systick.c b/hw-tests/upy-minimal/systick.c index 635e7d3e..fc4e51dc 100644 --- a/hw-tests/upy-minimal/systick.c +++ b/hw-tests/upy-minimal/systick.c @@ -1,22 +1,27 @@ #include "py/mpconfig.h" #include "mxc_delay.h" -void mp_hal_delay_ms(mp_uint_t ms) { - mxc_delay (ms * 1000); // TODO check return value +void mp_hal_delay_ms(mp_uint_t ms) +{ + mxc_delay(ms * 1000); // TODO check return value } -void mp_hal_delay_us(mp_uint_t us) { - mxc_delay (us); // TODO check return value +void mp_hal_delay_us(mp_uint_t us) +{ + mxc_delay(us); // TODO check return value } -mp_uint_t mp_hal_ticks_ms(void) { +mp_uint_t mp_hal_ticks_ms(void) +{ return 0; } -mp_uint_t mp_hal_ticks_us(void) { +mp_uint_t mp_hal_ticks_us(void) +{ return 0; } -mp_uint_t mp_hal_ticks_cpu(void) { +mp_uint_t mp_hal_ticks_cpu(void) +{ return 0; } diff --git a/hw-tests/upy-minimal/uart.c b/hw-tests/upy-minimal/uart.c index 2ef44317..243307f1 100644 --- a/hw-tests/upy-minimal/uart.c +++ b/hw-tests/upy-minimal/uart.c @@ -6,18 +6,21 @@ * Core UART functions to implement for a port */ -extern mxc_uart_regs_t * ConsoleUart; +extern mxc_uart_regs_t *ConsoleUart; // Receive single character -int mp_hal_stdin_rx_chr(void) { +int mp_hal_stdin_rx_chr(void) +{ return UART_ReadByte(ConsoleUart); } // Send string of given length -void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { - UART_Write(ConsoleUart, (uint8_t*)str, len); +void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) +{ + UART_Write(ConsoleUart, (uint8_t *)str, len); } -void mp_hal_set_interrupt_char(char c) { +void mp_hal_set_interrupt_char(char c) +{ return; } diff --git a/lib/card10/bosch.c b/lib/card10/bosch.c index 95d4719c..98b3e441 100644 --- a/lib/card10/bosch.c +++ b/lib/card10/bosch.c @@ -5,7 +5,7 @@ #include <stdio.h> #include <string.h> -#define I2C_DEVICE MXC_I2C1_BUS0 +#define I2C_DEVICE MXC_I2C1_BUS0 /********************************************************************************/ /* EXTERN FUNCTION DECLARATIONS */ /********************************************************************************/ @@ -20,46 +20,49 @@ static void hexdump(uint8_t *data, uint8_t len) } #endif -int8_t card10_bosch_i2c_write(uint8_t addr, uint8_t reg, uint8_t *p_buf, uint16_t size) +int8_t +card10_bosch_i2c_write(uint8_t addr, uint8_t reg, uint8_t *p_buf, uint16_t size) { - uint8_t buf[size + 1]; + uint8_t buf[size + 1]; - //printf("sensor_i2c_write 0x%02x:", reg); hexdump(p_buf, size); - buf[0] = reg; - memcpy(buf + 1, p_buf, size); + //printf("sensor_i2c_write 0x%02x:", reg); hexdump(p_buf, size); + buf[0] = reg; + memcpy(buf + 1, p_buf, size); - int l = I2C_MasterWrite(I2C_DEVICE, addr << 1, buf, size + 1, 0); - //printf("wrote: %d\n", l); - return l == size + 1 ? 0 : -1; - //return l == size + 1 ? BHY_SUCCESS : BHY_ERROR; + int l = I2C_MasterWrite(I2C_DEVICE, addr << 1, buf, size + 1, 0); + //printf("wrote: %d\n", l); + return l == size + 1 ? 0 : -1; + //return l == size + 1 ? BHY_SUCCESS : BHY_ERROR; } -int8_t card10_bosch_i2c_read(uint8_t addr, uint8_t reg, uint8_t *p_buf, uint16_t size) +int8_t +card10_bosch_i2c_read(uint8_t addr, uint8_t reg, uint8_t *p_buf, uint16_t size) { - //printf("sensor_i2c_read 0x%02x %d\n", reg, size); - if(I2C_MasterWrite(I2C_DEVICE, addr << 1, ®, 1, 1) == 1) { - //printf("setup read ok\n"); - int l = I2C_MasterRead(I2C_DEVICE, addr << 1, p_buf, size, 0); - //printf("read:"); hexdump(p_buf, l); - return l == size ? 0 : -1; - //return l == size ? BHY_SUCCESS : BHY_ERROR; - - } - return -1; - //return BHY_ERROR; + //printf("sensor_i2c_read 0x%02x %d\n", reg, size); + if (I2C_MasterWrite(I2C_DEVICE, addr << 1, ®, 1, 1) == 1) { + //printf("setup read ok\n"); + int l = I2C_MasterRead(I2C_DEVICE, addr << 1, p_buf, size, 0); + //printf("read:"); hexdump(p_buf, l); + return l == size ? 0 : -1; + //return l == size ? BHY_SUCCESS : BHY_ERROR; + } + return -1; + //return BHY_ERROR; } void card10_bosch_delay(uint32_t msec) { - TMR_Delay(MXC_TMR0, MSEC(msec), 0); + TMR_Delay(MXC_TMR0, MSEC(msec), 0); } -int8_t card10_bosch_i2c_write_ex(void *_, uint8_t addr, uint8_t reg, uint8_t *p_buf, uint16_t size) -{ - return card10_bosch_i2c_write(addr, reg, p_buf, size); +int8_t card10_bosch_i2c_write_ex( + void *_, uint8_t addr, uint8_t reg, uint8_t *p_buf, uint16_t size +) { + return card10_bosch_i2c_write(addr, reg, p_buf, size); } -int8_t card10_bosch_i2c_read_ex(void *_, uint8_t addr, uint8_t reg, uint8_t *p_buf, uint16_t size) -{ - return card10_bosch_i2c_read(addr, reg, p_buf, size); +int8_t card10_bosch_i2c_read_ex( + void *_, uint8_t addr, uint8_t reg, uint8_t *p_buf, uint16_t size +) { + return card10_bosch_i2c_read(addr, reg, p_buf, size); } diff --git a/lib/card10/leds.c b/lib/card10/leds.c index 8d7d21fe..c847709a 100644 --- a/lib/card10/leds.c +++ b/lib/card10/leds.c @@ -4,25 +4,29 @@ #include <stdint.h> #include <string.h> -#define NUM_LEDS 15 - -static const gpio_cfg_t rgb_dat_pin = {PORT_1, PIN_14, GPIO_FUNC_OUT, GPIO_PAD_NONE}; -static const gpio_cfg_t rgb_clk_pin = {PORT_1, PIN_15, GPIO_FUNC_OUT, GPIO_PAD_NONE}; +#define NUM_LEDS 15 + +static const gpio_cfg_t rgb_dat_pin = { + PORT_1, PIN_14, GPIO_FUNC_OUT, GPIO_PAD_NONE +}; +static const gpio_cfg_t rgb_clk_pin = { + PORT_1, PIN_15, GPIO_FUNC_OUT, GPIO_PAD_NONE +}; static uint8_t leds[NUM_LEDS][4]; /***** Functions *****/ // ***************************************************************************** typedef struct { - float r; // a fraction between 0 and 1 - float g; // a fraction between 0 and 1 - float b; // a fraction between 0 and 1 + float r; // a fraction between 0 and 1 + float g; // a fraction between 0 and 1 + float b; // a fraction between 0 and 1 } rgb; typedef struct { - float h; // angle in degrees - float s; // a fraction between 0 and 1 - float v; // a fraction between 0 and 1 + float h; // angle in degrees + float s; // a fraction between 0 and 1 + float v; // a fraction between 0 and 1 } hsv; #if 0 @@ -71,149 +75,156 @@ static hsv rgb2hsv(rgb in) } #endif - static rgb hsv2rgb(hsv in) { - float hh, p, q, t, ff; - long i; - rgb out; - - if(in.s <= 0.0) { // < is bogus, just shuts up warnings - out.r = in.v; - out.g = in.v; - out.b = in.v; - return out; - } - hh = in.h; - if(hh >= 360.0) hh = 0.0; - hh /= 60.0; - i = (long)hh; - ff = hh - i; - p = in.v * (1.0 - in.s); - q = in.v * (1.0 - (in.s * ff)); - t = in.v * (1.0 - (in.s * (1.0 - ff))); - - switch(i) { - case 0: - out.r = in.v; - out.g = t; - out.b = p; - break; - case 1: - out.r = q; - out.g = in.v; - out.b = p; - break; - case 2: - out.r = p; - out.g = in.v; - out.b = t; - break; - - case 3: - out.r = p; - out.g = q; - out.b = in.v; - break; - case 4: - out.r = t; - out.g = p; - out.b = in.v; - break; - case 5: - default: - out.r = in.v; - out.g = p; - out.b = q; - break; - } - return out; + float hh, p, q, t, ff; + long i; + rgb out; + + if (in.s <= 0.0) { // < is bogus, just shuts up warnings + out.r = in.v; + out.g = in.v; + out.b = in.v; + return out; + } + hh = in.h; + if (hh >= 360.0) + hh = 0.0; + hh /= 60.0; + i = (long)hh; + ff = hh - i; + p = in.v * (1.0 - in.s); + q = in.v * (1.0 - (in.s * ff)); + t = in.v * (1.0 - (in.s * (1.0 - ff))); + + switch (i) { + case 0: + out.r = in.v; + out.g = t; + out.b = p; + break; + case 1: + out.r = q; + out.g = in.v; + out.b = p; + break; + case 2: + out.r = p; + out.g = in.v; + out.b = t; + break; + + case 3: + out.r = p; + out.g = q; + out.b = in.v; + break; + case 4: + out.r = t; + out.g = p; + out.b = in.v; + break; + case 5: + default: + out.r = in.v; + out.g = p; + out.b = q; + break; + } + return out; } static void shift(uint8_t data) { - for(int i=0; i<8; i++) { - if(data & 0x80) { - GPIO_OutSet(&rgb_dat_pin); - }else{ - GPIO_OutClr(&rgb_dat_pin); - } - GPIO_OutClr(&rgb_clk_pin); - //TMR_Delay(MXC_TMR0, MSEC(1), 0); - GPIO_OutSet(&rgb_clk_pin); - //TMR_Delay(MXC_TMR0, MSEC(1), 0); - data <<= 1; - } + for (int i = 0; i < 8; i++) { + if (data & 0x80) { + GPIO_OutSet(&rgb_dat_pin); + } else { + GPIO_OutClr(&rgb_dat_pin); + } + GPIO_OutClr(&rgb_clk_pin); + //TMR_Delay(MXC_TMR0, MSEC(1), 0); + GPIO_OutSet(&rgb_clk_pin); + //TMR_Delay(MXC_TMR0, MSEC(1), 0); + data <<= 1; + } } static void leds_start(void) { - shift(0x00); shift(0x00); shift(0x00); shift(0x00); + shift(0x00); + shift(0x00); + shift(0x00); + shift(0x00); } static void leds_shift(uint8_t r, uint8_t g, uint8_t b, uint8_t dim) { - shift(0xE0 | (dim & 0x1F)); shift(b); shift(g); shift(r); + shift(0xE0 | (dim & 0x1F)); + shift(b); + shift(g); + shift(r); } static void leds_stop(void) { - shift(0xFF); shift(0xFF); shift(0xFF); shift(0xFF); + shift(0xFF); + shift(0xFF); + shift(0xFF); + shift(0xFF); } void leds_set_dim(uint8_t led, uint8_t dim) { - leds[led][3] = dim; + leds[led][3] = dim; } void leds_set(uint8_t led, uint8_t r, uint8_t g, uint8_t b) { - leds[led][0] = r; - leds[led][1] = g; - leds[led][2] = b; + leds[led][0] = r; + leds[led][1] = g; + leds[led][2] = b; } void leds_set_hsv(uint8_t led, float h, float s, float v) { - hsv in = {h, s, v}; - rgb out = hsv2rgb(in); - leds[led][0] = out.r * 255; - leds[led][1] = out.g * 255; - leds[led][2] = out.b * 255; + hsv in = { h, s, v }; + rgb out = hsv2rgb(in); + leds[led][0] = out.r * 255; + leds[led][1] = out.g * 255; + leds[led][2] = out.b * 255; } void leds_update(void) { - leds_start(); - for(int i=NUM_LEDS-1; i>=0; i--) { - leds_shift(leds[i][0], leds[i][1], leds[i][2], leds[i][3]); - } - leds_stop(); + leds_start(); + for (int i = NUM_LEDS - 1; i >= 0; i--) { + leds_shift(leds[i][0], leds[i][1], leds[i][2], leds[i][3]); + } + leds_stop(); } void leds_init(void) { - GPIO_Config(&rgb_clk_pin); - GPIO_Config(&rgb_dat_pin); + GPIO_Config(&rgb_clk_pin); + GPIO_Config(&rgb_dat_pin); - GPIO_OutSet(&rgb_clk_pin); - GPIO_OutClr(&rgb_dat_pin); + GPIO_OutSet(&rgb_clk_pin); + GPIO_OutClr(&rgb_dat_pin); - memset(leds, 0, sizeof(leds)); + memset(leds, 0, sizeof(leds)); - for(int i=0; i<NUM_LEDS; i++) { - leds[i][3] = 8; - } + for (int i = 0; i < NUM_LEDS; i++) { + leds[i][3] = 8; + } - if(portexpander_detected()) { - // Turn on LEDs - // TODO: only turn on LEDs if value != 0,0,0 && dim > 0 - portexpander_set(0, 0); - portexpander_set(1, 0); - portexpander_set(2, 0); - } + if (portexpander_detected()) { + // Turn on LEDs + // TODO: only turn on LEDs if value != 0,0,0 && dim > 0 + portexpander_set(0, 0); + portexpander_set(1, 0); + portexpander_set(2, 0); + } - leds_update(); + leds_update(); } - - diff --git a/lib/card10/pmic.c b/lib/card10/pmic.c index 30c7d3a2..5927ff55 100644 --- a/lib/card10/pmic.c +++ b/lib/card10/pmic.c @@ -5,125 +5,136 @@ #include <stdio.h> static const gpio_cfg_t pmic_interrupt_pin = { - PORT_0, PIN_12, GPIO_FUNC_IN, GPIO_PAD_PULL_UP + PORT_0, PIN_12, GPIO_FUNC_IN, GPIO_PAD_PULL_UP }; static pmic_button_callback_fn pmic_button_callback = NULL; static volatile bool interrupt_pending; void pmic_init(void) { - uint8_t didm = MAX77650_getDIDM(); - uint8_t cid = MAX77650_getChipID(); - interrupt_pending = false; + uint8_t didm = MAX77650_getDIDM(); + uint8_t cid = MAX77650_getChipID(); + interrupt_pending = false; - printf("MAX7765x DIDM: 0x%02x CID: 0x%02x\n", didm, cid); + printf("MAX7765x DIDM: 0x%02x CID: 0x%02x\n", didm, cid); - MAX77650_setIP_SBB0(0b11); //Limit switch current of SBB0 to 500mA for noise reduction - //MAX77650_setIP_SBB0(0b00); //Limit switch current of SBB0 to 1000 mA - MAX77650_setTV_SBB0(0b101000); //Set output Voltage of SBB0 to 1.8V - MAX77650_setADE_SBB0(0b0); //Disable Active Discharge at SBB0 Output - MAX77650_setEN_SBB0(0b110); //Enable SBB0 is on irrespective of FPS whenever the on/off controller is in its "On via Software" or "On via On/Off Controller" states + MAX77650_setIP_SBB0( + 0b11); //Limit switch current of SBB0 to 500mA for noise reduction + //MAX77650_setIP_SBB0(0b00); //Limit switch current of SBB0 to 1000 mA + MAX77650_setTV_SBB0(0b101000); //Set output Voltage of SBB0 to 1.8V + MAX77650_setADE_SBB0(0b0); //Disable Active Discharge at SBB0 Output + MAX77650_setEN_SBB0( + 0b110); //Enable SBB0 is on irrespective of FPS whenever the on/off controller is in its "On via Software" or "On via On/Off Controller" states - //MAX77650_setIP_SBB1(0b11); //Limit output of SBB1 to 500mA + //MAX77650_setIP_SBB1(0b11); //Limit output of SBB1 to 500mA #if BOARD_EVKIT - MAX77650_setTV_SBB1(0b100000); //Set output Voltage of SBB1 to 1.2V + MAX77650_setTV_SBB1(0b100000); //Set output Voltage of SBB1 to 1.2V #else - MAX77650_setTV_SBB1(0b001001); //Set output Voltage of SBB1 to 3.3V + MAX77650_setTV_SBB1(0b001001); //Set output Voltage of SBB1 to 3.3V #endif - MAX77650_setADE_SBB1(0b0); //Disable Active Discharge at SBB1 Output - MAX77650_setEN_SBB1(0b110); //Enable SBB1 is on irrespective of FPS whenever the on/off controller is in its "On via Software" or "On via On/Off Controller" states + MAX77650_setADE_SBB1(0b0); //Disable Active Discharge at SBB1 Output + MAX77650_setEN_SBB1( + 0b110); //Enable SBB1 is on irrespective of FPS whenever the on/off controller is in its "On via Software" or "On via On/Off Controller" states - MAX77650_setIP_SBB2(0b11); //Limit switch current of SBB2 to 500mA for noise reduction + MAX77650_setIP_SBB2( + 0b11); //Limit switch current of SBB2 to 500mA for noise reduction #if BOARD_EVKIT - MAX77650_setTV_SBB2(0b110010); //Set output Voltage of SBB2 to 3.3V + MAX77650_setTV_SBB2(0b110010); //Set output Voltage of SBB2 to 3.3V #else - //MAX77650_setTV_SBB2(0b110100); //Set output Voltage of SBB2 to 5.0V - MAX77650_setTV_SBB2(0b010010); //Set output Voltage of SBB2 to 3.3V + //MAX77650_setTV_SBB2(0b110100); //Set output Voltage of SBB2 to 5.0V + MAX77650_setTV_SBB2(0b010010); //Set output Voltage of SBB2 to 3.3V #endif - MAX77650_setADE_SBB2(0b0); //Disable Active Discharge at SBB2 Output - MAX77650_setEN_SBB2(0b110); //Enable SBB2 is on irrespective of FPS whenever the on/off controller is in its "On via Software" or "On via On/Off Controller" states - - // Prepare the PMIC LEDs - MAX77650_setLED_FS0(0b11); - MAX77650_setINV_LED0(false); //LED red: phase operation - MAX77650_setBRT_LED0(0b00000); //LED red: brightness - MAX77650_setP_LED0(0b1111); //LED red: LED period - MAX77650_setD_LED0(0b1111); //LED red: LED duty-cycle - - MAX77650_setLED_FS1(0b11); - MAX77650_setINV_LED1(false); //LED green: phase operation - MAX77650_setBRT_LED1(0b00000); //LED green: brightness - MAX77650_setP_LED1(0b1111); //LED green: LED period - MAX77650_setD_LED1(0b1111); //LED green: LED duty-cycle - - MAX77650_setLED_FS2(0b11); - MAX77650_setINV_LED2(false); //LED blue: phase operation - MAX77650_setBRT_LED2(0b00000); //LED blue: brightness - MAX77650_setP_LED2(0b1111); //LED blue: LED period - MAX77650_setD_LED2(0b1111); //LED blue: LED duty-cycle - - MAX77650_setEN_LED_MSTR(true); //LEDs Master enable - - MAX77650_setCHG_CV(0b11000); // 4.2 V target battery voltage - MAX77650_setICHGIN_LIM(1); // 190 mA limit on USB - MAX77650_setCHG_CC(0b1011); // 90 mA fast charge current - MAX77650_setCHG_EN(1); // Turn on charger - - MAX77650_setVSYS_REG(0b11000); // Set VSYS to 4.7 V to reduce voltage across bypass diode - - /* Setup interrupt & callback */ - GPIO_Config(&pmic_interrupt_pin); - GPIO_RegisterCallback(&pmic_interrupt_pin, pmic_interrupt_callback, NULL); - GPIO_IntConfig(&pmic_interrupt_pin, GPIO_INT_EDGE, GPIO_INT_FALLING); - GPIO_IntEnable(&pmic_interrupt_pin); - NVIC_SetPriority((IRQn_Type)MXC_GPIO_GET_IRQ(pmic_interrupt_pin.port), 2); - NVIC_EnableIRQ((IRQn_Type)MXC_GPIO_GET_IRQ(pmic_interrupt_pin.port)); - - /* Setup power button interrupt */ - MAX77650_setINT_M_GLBL(~(MAX77650_INT_nEN_R | MAX77650_INT_nEN_F)); - /* Clear existing interrupts */ - MAX77650_getINT_GLBL(); + MAX77650_setADE_SBB2(0b0); //Disable Active Discharge at SBB2 Output + MAX77650_setEN_SBB2( + 0b110); //Enable SBB2 is on irrespective of FPS whenever the on/off controller is in its "On via Software" or "On via On/Off Controller" states + + // Prepare the PMIC LEDs + MAX77650_setLED_FS0(0b11); + MAX77650_setINV_LED0(false); //LED red: phase operation + MAX77650_setBRT_LED0(0b00000); //LED red: brightness + MAX77650_setP_LED0(0b1111); //LED red: LED period + MAX77650_setD_LED0(0b1111); //LED red: LED duty-cycle + + MAX77650_setLED_FS1(0b11); + MAX77650_setINV_LED1(false); //LED green: phase operation + MAX77650_setBRT_LED1(0b00000); //LED green: brightness + MAX77650_setP_LED1(0b1111); //LED green: LED period + MAX77650_setD_LED1(0b1111); //LED green: LED duty-cycle + + MAX77650_setLED_FS2(0b11); + MAX77650_setINV_LED2(false); //LED blue: phase operation + MAX77650_setBRT_LED2(0b00000); //LED blue: brightness + MAX77650_setP_LED2(0b1111); //LED blue: LED period + MAX77650_setD_LED2(0b1111); //LED blue: LED duty-cycle + + MAX77650_setEN_LED_MSTR(true); //LEDs Master enable + + MAX77650_setCHG_CV(0b11000); // 4.2 V target battery voltage + MAX77650_setICHGIN_LIM(1); // 190 mA limit on USB + MAX77650_setCHG_CC(0b1011); // 90 mA fast charge current + MAX77650_setCHG_EN(1); // Turn on charger + + MAX77650_setVSYS_REG( + 0b11000); // Set VSYS to 4.7 V to reduce voltage across bypass diode + + /* Setup interrupt & callback */ + GPIO_Config(&pmic_interrupt_pin); + GPIO_RegisterCallback( + &pmic_interrupt_pin, pmic_interrupt_callback, NULL + ); + GPIO_IntConfig(&pmic_interrupt_pin, GPIO_INT_EDGE, GPIO_INT_FALLING); + GPIO_IntEnable(&pmic_interrupt_pin); + NVIC_SetPriority( + (IRQn_Type)MXC_GPIO_GET_IRQ(pmic_interrupt_pin.port), 2 + ); + NVIC_EnableIRQ((IRQn_Type)MXC_GPIO_GET_IRQ(pmic_interrupt_pin.port)); + + /* Setup power button interrupt */ + MAX77650_setINT_M_GLBL(~(MAX77650_INT_nEN_R | MAX77650_INT_nEN_F)); + /* Clear existing interrupts */ + MAX77650_getINT_GLBL(); } -__attribute__((weak)) void pmic_interrupt_callback(void*_) +__attribute__((weak)) void pmic_interrupt_callback(void *_) { - interrupt_pending = true; + interrupt_pending = true; } void pmic_poll(void) { - if(interrupt_pending) { - /* There might be a race condition here. Don't care ATM. */ - interrupt_pending = false; - uint8_t int_flag = MAX77650_getINT_GLBL(); - - if (int_flag & (MAX77650_INT_nEN_R | MAX77650_INT_nEN_F)) { - if (pmic_button_callback != NULL) { - (*pmic_button_callback)(int_flag & MAX77650_INT_nEN_F); - } - } - /* TODO: Other pmic interrupts */ - } + if (interrupt_pending) { + /* There might be a race condition here. Don't care ATM. */ + interrupt_pending = false; + uint8_t int_flag = MAX77650_getINT_GLBL(); + + if (int_flag & (MAX77650_INT_nEN_R | MAX77650_INT_nEN_F)) { + if (pmic_button_callback != NULL) { + (*pmic_button_callback)( + int_flag & MAX77650_INT_nEN_F + ); + } + } + /* TODO: Other pmic interrupts */ + } } void pmic_set_button_callback(pmic_button_callback_fn cb) { - pmic_button_callback = cb; + pmic_button_callback = cb; } void pmic_set_led(uint8_t led, uint8_t val) { - if(led == 0) { - MAX77650_setLED_FS0(val > 0 ? 0b01 : 0); - MAX77650_setBRT_LED0(val); - } - if(led == 1) { - MAX77650_setLED_FS1(val > 0 ? 0b01 : 0); - MAX77650_setBRT_LED1(val); - } - if(led == 2) { - MAX77650_setLED_FS2(val > 0 ? 0b01 : 0); - MAX77650_setBRT_LED2(val); - } - + if (led == 0) { + MAX77650_setLED_FS0(val > 0 ? 0b01 : 0); + MAX77650_setBRT_LED0(val); + } + if (led == 1) { + MAX77650_setLED_FS1(val > 0 ? 0b01 : 0); + MAX77650_setBRT_LED1(val); + } + if (led == 2) { + MAX77650_setLED_FS2(val > 0 ? 0b01 : 0); + MAX77650_setBRT_LED2(val); + } } -- GitLab