move user application code to app folder

This commit is contained in:
Joey Castillo 2021-08-01 18:40:03 -04:00
parent a547d78c1e
commit 7e330befff
7 changed files with 326 additions and 186 deletions

View File

@ -0,0 +1,193 @@
#include <stdio.h>
#include <string.h>
#include "app.h"
// these are implemented in main.c, just want to have access to them here.
void uart_putc(char c);
void uart_puts(char *s);
typedef enum ApplicationMode {
MODE_TIME,
MODE_SENSE,
MODE_SET_HOUR,
MODE_SET_MINUTE,
MODE_SET_SECOND
} ApplicationMode;
typedef struct ApplicationState {
ApplicationMode mode;
bool light_pressed;
bool mode_pressed;
bool alarm_pressed;
bool light_on;
uint16_t dig_T1; /**< dig_T1 cal register. */
int16_t dig_T2; /**< dig_T2 cal register. */
int16_t dig_T3; /**< dig_T3 cal register. */
} ApplicationState;
ApplicationState applicationState;
void cb_light_pressed() {
applicationState.light_pressed = true;
}
void cb_mode_pressed() {
applicationState.mode_pressed = true;
}
void cb_alarm_pressed() {
applicationState.alarm_pressed = true;
}
void cb_tick() {
;
}
#define BMP280_REGISTER_DIG_T1 0x88
#define BMP280_REGISTER_DIG_T2 0x8A
#define BMP280_REGISTER_DIG_T3 0x8C
#define BMP280_REGISTER_SOFTRESET 0xE0
#define BMP280_REGISTER_STATUS 0xF3
#define BMP280_REGISTER_CONTROL 0xF4
#define BMP280_REGISTER_CONFIG 0xF5
#define BMP280_REGISTER_PRESSUREDATA 0xF7
#define BMP280_REGISTER_TEMPDATA 0xFA
uint16_t read8(uint8_t reg) {
uint8_t val;
watch_i2c_send(0x77, &reg, 1);
watch_i2c_receive(0x77, &val, 1);
return val;
}
uint16_t read16(uint8_t reg) {
uart_puts("\nReading 2 bytes... ");
uint8_t buf[2];
watch_i2c_send(0x77, &reg, 1);
watch_i2c_receive(0x77, (uint8_t *)&buf, 2);
uart_puts("received!\n");
char buf2[32] = {0};
sprintf(buf2, "buf has values: %#02x, %#02x", buf[0], buf[1]);
uart_puts(buf2);
return (buf[0] << 8) | buf[1];
}
uint32_t read24(uint8_t reg) {
uart_puts("\nReading 3 bytes... ");
uint32_t value;
uint8_t buf[3];
watch_i2c_send(0x77, &reg, 1);
watch_i2c_receive(0x77, (uint8_t *)&buf, 3);
uart_puts("received!\n");
char buf2[33] = {0};
sprintf(buf2, "buf has values: %#02x, %#02x, %#02x", buf[0], buf[1], buf[2]);
uart_puts(buf2);
value = buf[0];
value <<= 8;
value |= buf[1];
value <<= 8;
value |= buf[2];
return value;
}
uint16_t read16_LE(uint8_t reg) {
uint16_t temp = read16(reg);
return (temp >> 8) | (temp << 8);
}
int16_t readS16(uint8_t reg) {
return (int16_t)read16(reg);
}
int16_t readS16_LE(uint8_t reg) {
return (int16_t)read16_LE(reg);
}
void print_temperature() {
int32_t var1, var2;
int32_t t_fine;
int32_t adc_T = read24(BMP280_REGISTER_TEMPDATA);
adc_T >>= 4;
var1 = ((((adc_T >> 3) - ((int32_t)applicationState.dig_T1 << 1))) *
((int32_t)applicationState.dig_T2)) >>
11;
var2 = (((((adc_T >> 4) - ((int32_t)applicationState.dig_T1)) *
((adc_T >> 4) - ((int32_t)applicationState.dig_T1))) >>
12) *
((int32_t)applicationState.dig_T3)) >>
14;
t_fine = var1 + var2;
float T = ((t_fine * 5 + 128) >> 8) / 100.0;
char buf[32] = {0};
sprintf(buf, "\n\nTemp is %3.2f degrees C", T);
uart_puts(buf);
}
/**
* @brief the app_init function is like setup() in Arduino. It is called once when the
* program begins. You should set pin modes and enable any peripherals you want to
* set up (real-time clock, I2C, etc.)
*
* @note If your app enters the ultra-low power BACKUP sleep mode, this function will
* be called again when it wakes from that deep sleep state. In this state, the RTC will
* still be configured with the correct date and time.
*/
void app_init() {
memset(&applicationState, 0, sizeof(applicationState));
watch_enable_buttons();
watch_register_button_callback(BTN_LIGHT, cb_light_pressed);
watch_enable_date_time();
watch_enable_tick_callback(cb_tick);
watch_enable_display();
watch_enable_led();
watch_enable_i2c();
uart_puts("\n\nI2C Driver Test\n");
uint8_t reset_cmd[] = {0xE0, 0xB6};
watch_i2c_send(0x77, reset_cmd, 2);
uart_puts("Reset BMP280\n");
applicationState.dig_T1 = read16_LE(BMP280_REGISTER_DIG_T1);
applicationState.dig_T2 = readS16_LE(BMP280_REGISTER_DIG_T2);
applicationState.dig_T3 = readS16_LE(BMP280_REGISTER_DIG_T3);
uint8_t ctrl_cmd[] = {0xF4, 0xA3};
watch_i2c_send(0x77, ctrl_cmd, 2);
}
/**
* @brief the app_prepare_for_sleep function is called before the watch goes into the
* STANDBY sleep mode. In STANDBY mode, most peripherals are shut down, and no code
* will run until the watch receives an interrupt (generally either the 1Hz tick or
* a press on one of the buttons).
*/
void app_prepare_for_sleep() {
applicationState.light_pressed = false;
applicationState.mode_pressed = false;
applicationState.alarm_pressed = false;
}
/**
* @brief the app_wake_from_sleep function is called after the watch wakes from the
* STANDBY sleep mode.
*/
void app_wake_from_sleep() {
}
void app_loop() {
if (applicationState.light_pressed) {
print_temperature();
}
uart_putc('.');
}

View File

@ -0,0 +1,29 @@
/**
* Header file for Sensor Watch application
*
* Ideally you should implement your app entirely within these functions, as well as any
* interrupt callbacks you register with the watch API. The general flow is as follows:
*
* 1. main.c configures the watch
* 2. main.c calls your app_init() function.
* - You may wish to enable some functionality and peripherals here.
* - You should definitely set up some wake-up sources here.
* 3. main.c calls your app_loop() function.
* - Run code and update your UI here.
* 4. main.c calls your app_prepare_for_sleep() function.
* - Consider resetting any state that was set in your wakeup callback here.
* - You may also want to disable / depower external sensors or peripherals here.
* 5. main.c enters the STANDBY sleep mode.
* - No user code will run, and the watch will enter a low power mode.
* - The watch will remain in this state until something from (2) wakes it.
* 6. main.c calls your app_wake_from_sleep() function.
* - You may wish to re-enable any peripherals you disabled.
* - After this, execution resumes at step (3).
*/
#include "watch.h"
void app_init();
void app_loop();
void app_prepare_for_sleep();
void app_wake_from_sleep();

View File

@ -25,25 +25,6 @@ struct pwm_descriptor PWM_0;
struct pwm_descriptor PWM_1; struct pwm_descriptor PWM_1;
void ADC_0_PORT_init(void)
{
// Disable digital pin circuitry
gpio_set_pin_direction(A1, GPIO_DIRECTION_OFF);
gpio_set_pin_function(A1, PINMUX_PB01B_ADC_AIN9);
// Disable digital pin circuitry
gpio_set_pin_direction(A2, GPIO_DIRECTION_OFF);
gpio_set_pin_function(A2, PINMUX_PB02B_ADC_AIN10);
// Disable digital pin circuitry
gpio_set_pin_direction(A0, GPIO_DIRECTION_OFF);
gpio_set_pin_function(A0, PINMUX_PB04B_ADC_AIN12);
}
void ADC_0_CLOCK_init(void) void ADC_0_CLOCK_init(void)
{ {
hri_mclk_set_APBCMASK_ADC_bit(MCLK); hri_mclk_set_APBCMASK_ADC_bit(MCLK);
@ -53,12 +34,10 @@ void ADC_0_CLOCK_init(void)
void ADC_0_init(void) void ADC_0_init(void)
{ {
ADC_0_CLOCK_init(); ADC_0_CLOCK_init();
ADC_0_PORT_init();
adc_sync_init(&ADC_0, ADC, (void *)NULL); adc_sync_init(&ADC_0, ADC, (void *)NULL);
} }
void EXTERNAL_IRQ_0_init(void) void EXTERNAL_IRQ_0_init(void) {
{
hri_gclk_write_PCHCTRL_reg(GCLK, EIC_GCLK_ID, CONF_GCLK_EIC_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos)); hri_gclk_write_PCHCTRL_reg(GCLK, EIC_GCLK_ID, CONF_GCLK_EIC_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
hri_mclk_set_APBAMASK_EIC_bit(MCLK); hri_mclk_set_APBAMASK_EIC_bit(MCLK);
@ -104,19 +83,16 @@ void EXTERNAL_IRQ_0_init(void)
ext_irq_init(); ext_irq_init();
} }
void CALENDAR_0_CLOCK_init(void) void CALENDAR_0_CLOCK_init(void) {
{
hri_mclk_set_APBAMASK_RTC_bit(MCLK); hri_mclk_set_APBAMASK_RTC_bit(MCLK);
} }
void CALENDAR_0_init(void) void CALENDAR_0_init(void) {
{
CALENDAR_0_CLOCK_init(); CALENDAR_0_CLOCK_init();
calendar_init(&CALENDAR_0, RTC); calendar_init(&CALENDAR_0, RTC);
} }
void I2C_0_PORT_init(void) void I2C_0_PORT_init(void) {
{
gpio_set_pin_pull_mode(SDA, gpio_set_pin_pull_mode(SDA,
// <y> Pull configuration // <y> Pull configuration
@ -139,129 +115,89 @@ void I2C_0_PORT_init(void)
gpio_set_pin_function(SCL, PINMUX_PB31C_SERCOM1_PAD1); gpio_set_pin_function(SCL, PINMUX_PB31C_SERCOM1_PAD1);
} }
void I2C_0_CLOCK_init(void) void I2C_0_CLOCK_init(void) {
{
hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM1_GCLK_ID_CORE, CONF_GCLK_SERCOM1_CORE_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos)); hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM1_GCLK_ID_CORE, CONF_GCLK_SERCOM1_CORE_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM1_GCLK_ID_SLOW, CONF_GCLK_SERCOM1_SLOW_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos)); hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM1_GCLK_ID_SLOW, CONF_GCLK_SERCOM1_SLOW_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
hri_mclk_set_APBCMASK_SERCOM1_bit(MCLK); hri_mclk_set_APBCMASK_SERCOM1_bit(MCLK);
} }
void I2C_0_init(void) void I2C_0_init(void) {
{
I2C_0_CLOCK_init(); I2C_0_CLOCK_init();
i2c_m_sync_init(&I2C_0, SERCOM1); i2c_m_sync_init(&I2C_0, SERCOM1);
I2C_0_PORT_init(); I2C_0_PORT_init();
} }
void delay_driver_init(void) void delay_driver_init(void) {
{
delay_init(SysTick); delay_init(SysTick);
} }
void PWM_0_PORT_init(void) void PWM_0_PORT_init(void) {
{
gpio_set_pin_function(RED, PINMUX_PA20E_TC3_WO0); gpio_set_pin_function(RED, PINMUX_PA20E_TC3_WO0);
gpio_set_pin_function(GREEN, PINMUX_PA21E_TC3_WO1); gpio_set_pin_function(GREEN, PINMUX_PA21E_TC3_WO1);
} }
void PWM_0_CLOCK_init(void) void PWM_0_CLOCK_init(void) {
{
hri_mclk_set_APBCMASK_TC3_bit(MCLK); hri_mclk_set_APBCMASK_TC3_bit(MCLK);
hri_gclk_write_PCHCTRL_reg(GCLK, TC3_GCLK_ID, CONF_GCLK_TC3_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos)); hri_gclk_write_PCHCTRL_reg(GCLK, TC3_GCLK_ID, CONF_GCLK_TC3_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
} }
void PWM_0_init(void) void PWM_0_init(void) {
{
PWM_0_CLOCK_init(); PWM_0_CLOCK_init();
PWM_0_PORT_init(); PWM_0_PORT_init();
pwm_init(&PWM_0, TC3, _tc_get_pwm()); pwm_init(&PWM_0, TC3, _tc_get_pwm());
} }
void PWM_1_PORT_init(void) void PWM_1_PORT_init(void) {
{
gpio_set_pin_function(BUZZER, PINMUX_PA27F_TCC0_WO5); gpio_set_pin_function(BUZZER, PINMUX_PA27F_TCC0_WO5);
} }
void PWM_1_CLOCK_init(void) void PWM_1_CLOCK_init(void) {
{
hri_mclk_set_APBCMASK_TCC0_bit(MCLK); hri_mclk_set_APBCMASK_TCC0_bit(MCLK);
hri_gclk_write_PCHCTRL_reg(GCLK, TCC0_GCLK_ID, CONF_GCLK_TCC0_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos)); hri_gclk_write_PCHCTRL_reg(GCLK, TCC0_GCLK_ID, CONF_GCLK_TCC0_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
} }
void PWM_1_init(void) void PWM_1_init(void) {
{
PWM_1_CLOCK_init(); PWM_1_CLOCK_init();
PWM_1_PORT_init(); PWM_1_PORT_init();
pwm_init(&PWM_1, TCC0, _tcc_get_pwm()); pwm_init(&PWM_1, TCC0, _tcc_get_pwm());
} }
void SEGMENT_LCD_0_PORT_init(void) void SEGMENT_LCD_0_PORT_init(void) {
{
gpio_set_pin_function(COM0, PINMUX_PB06B_SLCD_LP0); gpio_set_pin_function(COM0, PINMUX_PB06B_SLCD_LP0);
gpio_set_pin_function(COM1, PINMUX_PB07B_SLCD_LP1); gpio_set_pin_function(COM1, PINMUX_PB07B_SLCD_LP1);
gpio_set_pin_function(COM2, PINMUX_PB08B_SLCD_LP2); gpio_set_pin_function(COM2, PINMUX_PB08B_SLCD_LP2);
gpio_set_pin_function(SEG0, PINMUX_PB09B_SLCD_LP3); gpio_set_pin_function(SEG0, PINMUX_PB09B_SLCD_LP3);
gpio_set_pin_function(SEG1, PINMUX_PA04B_SLCD_LP4); gpio_set_pin_function(SEG1, PINMUX_PA04B_SLCD_LP4);
gpio_set_pin_function(SEG2, PINMUX_PA05B_SLCD_LP5); gpio_set_pin_function(SEG2, PINMUX_PA05B_SLCD_LP5);
gpio_set_pin_function(SEG3, PINMUX_PA06B_SLCD_LP6); gpio_set_pin_function(SEG3, PINMUX_PA06B_SLCD_LP6);
gpio_set_pin_function(SEG4, PINMUX_PA07B_SLCD_LP7); gpio_set_pin_function(SEG4, PINMUX_PA07B_SLCD_LP7);
gpio_set_pin_function(SEG5, PINMUX_PA08B_SLCD_LP11); gpio_set_pin_function(SEG5, PINMUX_PA08B_SLCD_LP11);
gpio_set_pin_function(SEG6, PINMUX_PA09B_SLCD_LP12); gpio_set_pin_function(SEG6, PINMUX_PA09B_SLCD_LP12);
gpio_set_pin_function(SEG7, PINMUX_PA10B_SLCD_LP13); gpio_set_pin_function(SEG7, PINMUX_PA10B_SLCD_LP13);
gpio_set_pin_function(SEG8, PINMUX_PA11B_SLCD_LP14); gpio_set_pin_function(SEG8, PINMUX_PA11B_SLCD_LP14);
gpio_set_pin_function(SEG9, PINMUX_PB11B_SLCD_LP21); gpio_set_pin_function(SEG9, PINMUX_PB11B_SLCD_LP21);
gpio_set_pin_function(SEG10, PINMUX_PB12B_SLCD_LP22); gpio_set_pin_function(SEG10, PINMUX_PB12B_SLCD_LP22);
gpio_set_pin_function(SEG11, PINMUX_PB13B_SLCD_LP23); gpio_set_pin_function(SEG11, PINMUX_PB13B_SLCD_LP23);
gpio_set_pin_function(SEG12, PINMUX_PB14B_SLCD_LP24); gpio_set_pin_function(SEG12, PINMUX_PB14B_SLCD_LP24);
gpio_set_pin_function(SEG13, PINMUX_PB15B_SLCD_LP25); gpio_set_pin_function(SEG13, PINMUX_PB15B_SLCD_LP25);
gpio_set_pin_function(SEG14, PINMUX_PA12B_SLCD_LP28); gpio_set_pin_function(SEG14, PINMUX_PA12B_SLCD_LP28);
gpio_set_pin_function(SEG15, PINMUX_PA13B_SLCD_LP29); gpio_set_pin_function(SEG15, PINMUX_PA13B_SLCD_LP29);
gpio_set_pin_function(SEG16, PINMUX_PA14B_SLCD_LP30); gpio_set_pin_function(SEG16, PINMUX_PA14B_SLCD_LP30);
gpio_set_pin_function(SEG17, PINMUX_PA15B_SLCD_LP31); gpio_set_pin_function(SEG17, PINMUX_PA15B_SLCD_LP31);
gpio_set_pin_function(SEG18, PINMUX_PA16B_SLCD_LP32); gpio_set_pin_function(SEG18, PINMUX_PA16B_SLCD_LP32);
gpio_set_pin_function(SEG19, PINMUX_PA17B_SLCD_LP33); gpio_set_pin_function(SEG19, PINMUX_PA17B_SLCD_LP33);
gpio_set_pin_function(SEG20, PINMUX_PA18B_SLCD_LP34); gpio_set_pin_function(SEG20, PINMUX_PA18B_SLCD_LP34);
gpio_set_pin_function(SEG21, PINMUX_PA19B_SLCD_LP35); gpio_set_pin_function(SEG21, PINMUX_PA19B_SLCD_LP35);
gpio_set_pin_function(SEG22, PINMUX_PB16B_SLCD_LP42); gpio_set_pin_function(SEG22, PINMUX_PB16B_SLCD_LP42);
gpio_set_pin_function(SEG23, PINMUX_PB17B_SLCD_LP43); gpio_set_pin_function(SEG23, PINMUX_PB17B_SLCD_LP43);
} }
/** /**
* \brief SLCD initialization function * \brief SLCD initialization function
* *
* Enables SLCD peripheral, clocks and initializes SLCD driver * Enables SLCD peripheral, clocks and initializes SLCD driver
*/ */
void SEGMENT_LCD_0_init(void) void SEGMENT_LCD_0_init(void) {
{
hri_mclk_set_APBCMASK_SLCD_bit(SLCD); hri_mclk_set_APBCMASK_SLCD_bit(SLCD);
slcd_sync_init(&SEGMENT_LCD_0, SLCD); slcd_sync_init(&SEGMENT_LCD_0, SLCD);
SEGMENT_LCD_0_PORT_init(); SEGMENT_LCD_0_PORT_init();

View File

@ -7,19 +7,17 @@
#include "watch.h" #include "watch.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
void watch_init(Watch *watch) { void watch_init() {
memset(watch, 0, sizeof(*watch));
// use switching regulator // use switching regulator
SUPC->VREG.bit.SEL = 1; SUPC->VREG.bit.SEL = 1;
while(!SUPC->STATUS.bit.VREGRDY); while(!SUPC->STATUS.bit.VREGRDY);
// TODO: use performance level 0 // TODO: use performance level 0?
// _set_performance_level(0); // _set_performance_level(0);
// hri_pm_write_PLCFG_PLDIS_bit(PM, true); // hri_pm_write_PLCFG_PLDIS_bit(PM, true);
} }
const uint8_t Character_Set[] = static const uint8_t Character_Set[] =
{ {
0b00000000, // 0b00000000, //
0b00000000, // ! 0b00000000, // !
@ -118,10 +116,7 @@ const uint8_t Character_Set[] =
0b00000001, // ~ 0b00000001, // ~
}; };
void watch_enable_display(Watch *watch) { static const uint64_t Segment_Map[] = {
if (watch->display_enabled) return;
static const uint64_t segmap[] = {
0x4e4f0e8e8f8d4d0d, // Position 8 0x4e4f0e8e8f8d4d0d, // Position 8
0xc8c4c4c8b4b4b0b, // Position 9 0xc8c4c4c8b4b4b0b, // Position 9
0xc049c00a49890949, // Position 6 0xc049c00a49890949, // Position 6
@ -132,25 +127,25 @@ void watch_enable_display(Watch *watch) {
0xc041804000018a81, // Position 3 0xc041804000018a81, // Position 3
0xc043420203048382, // Position 4 0xc043420203048382, // Position 4
0xc045440506468584, // Position 5 0xc045440506468584, // Position 5
}; };
watch->num_chars = 10;
watch->segment_map = &segmap[0];
static const uint8_t Num_Chars = 10;
void watch_enable_display() {
SEGMENT_LCD_0_init(); SEGMENT_LCD_0_init();
slcd_sync_enable(&SEGMENT_LCD_0); slcd_sync_enable(&SEGMENT_LCD_0);
watch->display_enabled = true;
} }
void watch_display_pixel(Watch *watch, uint8_t com, uint8_t seg) { void watch_display_pixel(uint8_t com, uint8_t seg) {
slcd_sync_seg_on(&SEGMENT_LCD_0, SLCD_SEGID(com, seg)); slcd_sync_seg_on(&SEGMENT_LCD_0, SLCD_SEGID(com, seg));
} }
void watch_clear_pixel(Watch *watch, uint8_t com, uint8_t seg) { void watch_clear_pixel(uint8_t com, uint8_t seg) {
slcd_sync_seg_off(&SEGMENT_LCD_0, SLCD_SEGID(com, seg)); slcd_sync_seg_off(&SEGMENT_LCD_0, SLCD_SEGID(com, seg));
} }
void watch_display_character(Watch *watch, uint8_t character, uint8_t position) { void watch_display_character(uint8_t character, uint8_t position) {
uint64_t segmap = watch->segment_map[position]; uint64_t segmap = Segment_Map[position];
uint64_t segdata = Character_Set[character - 0x20]; uint64_t segdata = Character_Set[character - 0x20];
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
@ -169,43 +164,40 @@ void watch_display_character(Watch *watch, uint8_t character, uint8_t position)
} }
} }
void watch_display_string(Watch *watch, char *string, uint8_t position) { void watch_display_string(char *string, uint8_t position) {
size_t i = 0; size_t i = 0;
while(string[i] != 0) { while(string[i] != 0) {
watch_display_character(watch, string[i], position + i); watch_display_character(string[i], position + i);
i++; i++;
if (i >= watch->num_chars) break; if (i >= Num_Chars) break;
} }
} }
void watch_enable_buttons(Watch *watch) { void watch_enable_buttons() {
EXTERNAL_IRQ_0_init(); EXTERNAL_IRQ_0_init();
} }
void watch_register_button_callback(Watch *watch, const uint32_t pin, ext_irq_cb_t callback) { void watch_register_button_callback(const uint32_t pin, ext_irq_cb_t callback) {
ext_irq_register(pin, callback); ext_irq_register(pin, callback);
} }
void watch_enable_led(Watch *watch) { static bool PWM_0_enabled = false;
if (watch->led_enabled) return;
PWM_0_init(); void watch_enable_led() {
if (!PWM_0_enabled) PWM_0_init();
PWM_0_enabled = true;
pwm_set_parameters(&PWM_0, 10000, 0); pwm_set_parameters(&PWM_0, 10000, 0);
pwm_enable(&PWM_0); pwm_enable(&PWM_0);
watch->led_enabled = true;
watch_set_led_off(); watch_set_led_off();
} }
void watch_disable_led(Watch *watch) { void watch_disable_led() {
if (!watch->led_enabled) return;
gpio_set_pin_function(RED, GPIO_PIN_FUNCTION_OFF); gpio_set_pin_function(RED, GPIO_PIN_FUNCTION_OFF);
gpio_set_pin_function(GREEN, GPIO_PIN_FUNCTION_OFF); gpio_set_pin_function(GREEN, GPIO_PIN_FUNCTION_OFF);
pwm_disable(&PWM_0); pwm_disable(&PWM_0);
PWM_0_enabled = false;
watch->led_enabled = false;
} }
void watch_set_led_color(uint16_t red, uint16_t green) { void watch_set_led_color(uint16_t red, uint16_t green) {
@ -225,12 +217,9 @@ void watch_set_led_off() {
watch_set_led_color(0, 0); watch_set_led_color(0, 0);
} }
void watch_enable_date_time(Watch *watch) { void watch_enable_date_time() {
if (watch->calendar_enabled) return;
CALENDAR_0_init(); CALENDAR_0_init();
calendar_enable(&CALENDAR_0); calendar_enable(&CALENDAR_0);
watch->calendar_enabled = true;
} }
void watch_set_date_time(struct calendar_date_time date_time) { void watch_set_date_time(struct calendar_date_time date_time) {
@ -248,15 +237,19 @@ static void tick_callback(struct calendar_dev *const dev) {
tick_user_callback(); tick_user_callback();
} }
void watch_enable_tick(ext_irq_cb_t callback) { void watch_enable_tick_callback(ext_irq_cb_t callback) {
tick_user_callback = callback; tick_user_callback = callback;
// TODO: rename this method to reflect that it now sets the PER7 interrupt. // TODO: rename this method to reflect that it now sets the PER7 interrupt.
_tamper_register_callback(&CALENDAR_0.device, &tick_callback); _tamper_register_callback(&CALENDAR_0.device, &tick_callback);
} }
void watch_enable_analog(Watch *watch, const uint8_t pin) { static bool ADC_0_ENABLED = false;
if (!watch->adc_enabled) ADC_0_init();
void watch_enable_analog(const uint8_t pin) {
if (!ADC_0_ENABLED) ADC_0_init();
ADC_0_ENABLED = true;
gpio_set_pin_direction(pin, GPIO_DIRECTION_OFF);
switch (pin) { switch (pin) {
case A0: case A0:
gpio_set_pin_function(A0, PINMUX_PB04B_ADC_AIN12); gpio_set_pin_function(A0, PINMUX_PB04B_ADC_AIN12);
@ -270,7 +263,6 @@ void watch_enable_analog(Watch *watch, const uint8_t pin) {
default: default:
return; return;
} }
gpio_set_pin_direction(pin, GPIO_DIRECTION_OFF);
} }
void watch_enable_digital_input(const uint8_t pin) { void watch_enable_digital_input(const uint8_t pin) {
@ -278,15 +270,30 @@ void watch_enable_digital_input(const uint8_t pin) {
gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF); gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
} }
void watch_enable_pull_up(const uint8_t pin) {
gpio_set_pin_pull_mode(pin, GPIO_PULL_UP);
}
void watch_enable_pull_down(const uint8_t pin) {
gpio_set_pin_pull_mode(pin, GPIO_PULL_DOWN);
}
bool watch_get_pin_level(const uint8_t pin, const bool level) {
return gpio_get_pin_level(pin);
}
void watch_enable_digital_output(const uint8_t pin) { void watch_enable_digital_output(const uint8_t pin) {
gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT); gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT);
gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF); gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF);
} }
void watch_set_pin_level(const uint8_t pin, const bool level) {
gpio_set_pin_level(pin, level);
}
struct io_descriptor *I2C_0_io; struct io_descriptor *I2C_0_io;
void watch_enable_i2c(Watch *watch) { void watch_enable_i2c() {
if (watch->i2c_enabled) return;
I2C_0_init(); I2C_0_init();
i2c_m_sync_get_io_descriptor(&I2C_0, &I2C_0_io); i2c_m_sync_get_io_descriptor(&I2C_0, &I2C_0_io);
i2c_m_sync_enable(&I2C_0); i2c_m_sync_enable(&I2C_0);

View File

@ -13,50 +13,41 @@
#include "hpl_calendar.h" #include "hpl_calendar.h"
#include "hal_ext_irq.h" #include "hal_ext_irq.h"
typedef struct Watch { void watch_init();
bool display_enabled;
bool led_enabled;
bool buzzer_enabled;
bool calendar_enabled;
bool adc_enabled;
bool i2c_enabled;
bool spi_enabled;
bool eic_enabled;
uint8_t num_chars; void watch_enable_display();
const uint64_t* segment_map; void watch_display_pixel(uint8_t com, uint8_t seg);
} Watch; void watch_display_string(char *string, uint8_t position);
void watch_init(Watch *watch); void watch_enable_led();
void watch_disable_led();
void watch_enable_display(Watch *watch);
void watch_display_pixel(Watch *watch, uint8_t com, uint8_t seg);
void watch_display_string(Watch *watch, char *string, uint8_t position);
void watch_enable_led(Watch *watch);
void watch_disable_led(Watch *watch);
void watch_set_led_color(uint16_t red, uint16_t green); void watch_set_led_color(uint16_t red, uint16_t green);
void watch_set_led_red(); void watch_set_led_red();
void watch_set_led_green(); void watch_set_led_green();
void watch_set_led_off(); void watch_set_led_off();
void watch_enable_date_time(Watch *watch); void watch_enable_date_time();
void watch_set_date_time(struct calendar_date_time date_time); void watch_set_date_time(struct calendar_date_time date_time);
void watch_get_date_time(struct calendar_date_time *date_time); void watch_get_date_time(struct calendar_date_time *date_time);
void watch_enable_tick(ext_irq_cb_t callback); void watch_enable_tick_callback(ext_irq_cb_t callback);
void watch_enable_analog(Watch *watch, const uint8_t pin); void watch_enable_analog(const uint8_t pin);
void watch_enable_buttons(Watch *watch); void watch_enable_buttons();
void watch_register_button_callback(Watch *watch, const uint32_t pin, ext_irq_cb_t callback); void watch_register_button_callback(const uint32_t pin, ext_irq_cb_t callback);
void watch_enable_digital_input(const uint8_t pin); void watch_enable_digital_input(const uint8_t pin);
void watch_enable_pull_up(const uint8_t pin);
void watch_enable_pull_down(const uint8_t pin);
bool watch_get_pin_level(const uint8_t pin, const bool level);
void watch_enable_digital_output(const uint8_t pin); void watch_enable_digital_output(const uint8_t pin);
void watch_set_pin_level(const uint8_t pin, const bool level);
struct io_descriptor *I2C_0_io; struct io_descriptor *I2C_0_io;
void watch_enable_i2c(Watch *watch); void watch_enable_i2c();
void watch_i2c_send(int16_t addr, uint8_t *buf, uint16_t length); void watch_i2c_send(int16_t addr, uint8_t *buf, uint16_t length);
void watch_i2c_receive(int16_t addr, uint8_t *buf, uint16_t length); void watch_i2c_receive(int16_t addr, uint8_t *buf, uint16_t length);

View File

@ -39,13 +39,12 @@
#include "hal_gpio.h" #include "hal_gpio.h"
#include "atmel_start_pins.h" #include "atmel_start_pins.h"
#include "watch.h" #include "watch.h"
#include "app.h"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
HAL_GPIO_PIN(UART_TX, B, 0) HAL_GPIO_PIN(UART_TX, B, 0)
HAL_GPIO_PIN(UART_RX, B, 2) HAL_GPIO_PIN(UART_RX, B, 2)
Watch watch;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void uart_init(uint32_t baud) { static void uart_init(uint32_t baud) {
uint64_t br = (uint64_t)65536 * (CONF_CPU_FREQUENCY - 16 * baud) / CONF_CPU_FREQUENCY; uint64_t br = (uint64_t)65536 * (CONF_CPU_FREQUENCY - 16 * baud) / CONF_CPU_FREQUENCY;
@ -73,52 +72,35 @@ static void uart_init(uint32_t baud) {
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void uart_putc(char c) { void uart_putc(char c) {
while (!(SERCOM3->USART.INTFLAG.reg & SERCOM_USART_INTFLAG_DRE)); while (!(SERCOM3->USART.INTFLAG.reg & SERCOM_USART_INTFLAG_DRE));
SERCOM3->USART.DATA.reg = c; SERCOM3->USART.DATA.reg = c;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void uart_puts(char *s) { void uart_puts(char *s) {
while (*s) uart_putc(*s++); while (*s) uart_putc(*s++);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static void sys_init(void) { static void sys_init(void) {
uart_puts("init_mcu\n");
init_mcu(); init_mcu();
watch_init(&watch); uart_puts("watch_init\n");
watch_enable_display(&watch); watch_init();
watch_enable_led(&watch); uart_puts("app_init\n");
watch_enable_date_time(&watch); app_init();
watch_enable_analog(&watch, A0);
watch_enable_buttons(&watch);
watch_enable_i2c(&watch);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
int main(void) { int main(void) {
sys_init();
uart_init(115200); uart_init(115200);
sys_init();
char buf[20] = {0};
uart_puts("\n\nI2C Driver Test\n");
uint8_t reset_cmd[] = {0xE0, 0xB6};
watch_i2c_send(0x77, reset_cmd, 2);
uart_puts("Reset BMP280\n");
uint8_t chip_id_cmd = 0xD0;
uint8_t chip_id = 0;
watch_i2c_send(0x77, &chip_id_cmd, 1);
uart_puts("Chip ID... ");
watch_i2c_receive(0x77, &chip_id, 1);
uart_puts("received!\n");
sprintf(buf, "Chip ID is %#02x", chip_id);
uart_puts(buf);
uart_puts("\nDone\n");
uint16_t red = 0;
uint16_t green = 0;
while (1) { while (1) {
app_loop();
app_prepare_for_sleep();
sleep(4);
} }
return 0; return 0;

View File

@ -56,6 +56,7 @@ INCLUDES += \
-I../hri/ \ -I../hri/ \
-I../config/ \ -I../config/ \
-I../hw/ \ -I../hw/ \
-I../app/ \
-I.. -I..
SRCS += \ SRCS += \
@ -63,6 +64,7 @@ SRCS += \
../startup_saml22.c \ ../startup_saml22.c \
../hw/driver_init.c \ ../hw/driver_init.c \
../hw/watch.c \ ../hw/watch.c \
../app/app.c \
../hal/src/hal_adc_sync.c \ ../hal/src/hal_adc_sync.c \
../hal/src/hal_atomic.c \ ../hal/src/hal_atomic.c \
../hal/src/hal_calendar.c \ ../hal/src/hal_calendar.c \