diff --git a/Sensor Watch Starter Project/hw/driver_init.c b/Sensor Watch Starter Project/hw/driver_init.c index e69de29b..bbee2a1b 100644 --- a/Sensor Watch Starter Project/hw/driver_init.c +++ b/Sensor Watch Starter Project/hw/driver_init.c @@ -0,0 +1,268 @@ +/* + * Code generated from Atmel Start. + * + * This file will be overwritten when reconfiguring your Atmel Start project. + * Please copy examples or other code you want to keep to a separate file + * to avoid losing it when reconfiguring. + */ + +#include "driver_init.h" +#include +#include +#include + +#include + +struct slcd_sync_descriptor SEGMENT_LCD_0; + +struct adc_sync_descriptor ADC_0; + +struct calendar_descriptor CALENDAR_0; + +struct i2c_m_sync_desc I2C_0; + +struct pwm_descriptor PWM_0; + +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) +{ + hri_mclk_set_APBCMASK_ADC_bit(MCLK); + hri_gclk_write_PCHCTRL_reg(GCLK, ADC_GCLK_ID, CONF_GCLK_ADC_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos)); +} + +void ADC_0_init(void) +{ + ADC_0_CLOCK_init(); + ADC_0_PORT_init(); + adc_sync_init(&ADC_0, ADC, (void *)NULL); +} + +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_mclk_set_APBAMASK_EIC_bit(MCLK); + + // Set pin direction to input + gpio_set_pin_direction(BTN_ALARM, GPIO_DIRECTION_IN); + + gpio_set_pin_pull_mode(BTN_ALARM, + // Pull configuration + // pad_pull_config + // Off + // Pull-up + // Pull-down + GPIO_PULL_DOWN); + + gpio_set_pin_function(BTN_ALARM, PINMUX_PB05A_EIC_EXTINT5); + + // Set pin direction to input + gpio_set_pin_direction(BTN_LIGHT, GPIO_DIRECTION_IN); + + gpio_set_pin_pull_mode(BTN_LIGHT, + // Pull configuration + // pad_pull_config + // Off + // Pull-up + // Pull-down + GPIO_PULL_DOWN); + + gpio_set_pin_function(BTN_LIGHT, PINMUX_PA22A_EIC_EXTINT6); + + // Set pin direction to input + gpio_set_pin_direction(BTN_MODE, GPIO_DIRECTION_IN); + + gpio_set_pin_pull_mode(BTN_MODE, + // Pull configuration + // pad_pull_config + // Off + // Pull-up + // Pull-down + GPIO_PULL_DOWN); + + gpio_set_pin_function(BTN_MODE, PINMUX_PA23A_EIC_EXTINT7); + + ext_irq_init(); +} + +void CALENDAR_0_CLOCK_init(void) +{ + hri_mclk_set_APBAMASK_RTC_bit(MCLK); +} + +void CALENDAR_0_init(void) +{ + CALENDAR_0_CLOCK_init(); + calendar_init(&CALENDAR_0, RTC); +} + +void I2C_0_PORT_init(void) +{ + + gpio_set_pin_pull_mode(SDA, + // Pull configuration + // pad_pull_config + // Off + // Pull-up + // Pull-down + GPIO_PULL_OFF); + + gpio_set_pin_function(SDA, PINMUX_PB30C_SERCOM1_PAD0); + + gpio_set_pin_pull_mode(SCL, + // Pull configuration + // pad_pull_config + // Off + // Pull-up + // Pull-down + GPIO_PULL_OFF); + + gpio_set_pin_function(SCL, PINMUX_PB31C_SERCOM1_PAD1); +} + +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_SLOW, CONF_GCLK_SERCOM1_SLOW_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos)); + hri_mclk_set_APBCMASK_SERCOM1_bit(MCLK); +} + +void I2C_0_init(void) +{ + I2C_0_CLOCK_init(); + i2c_m_sync_init(&I2C_0, SERCOM1); + I2C_0_PORT_init(); +} + +void delay_driver_init(void) +{ + delay_init(SysTick); +} + +void PWM_0_PORT_init(void) +{ + + gpio_set_pin_function(RED, PINMUX_PA20E_TC3_WO0); + + gpio_set_pin_function(GREEN, PINMUX_PA21E_TC3_WO1); +} + +void PWM_0_CLOCK_init(void) +{ + 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)); +} + +void PWM_0_init(void) +{ + PWM_0_CLOCK_init(); + PWM_0_PORT_init(); + pwm_init(&PWM_0, TC3, _tc_get_pwm()); +} + +void PWM_1_PORT_init(void) +{ + + gpio_set_pin_function(BUZZER, PINMUX_PA27F_TCC0_WO5); +} + +void PWM_1_CLOCK_init(void) +{ + 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)); +} + +void PWM_1_init(void) +{ + PWM_1_CLOCK_init(); + PWM_1_PORT_init(); + pwm_init(&PWM_1, TCC0, _tcc_get_pwm()); +} + +void SEGMENT_LCD_0_PORT_init(void) +{ + + gpio_set_pin_function(COM0, PINMUX_PB06B_SLCD_LP0); + + gpio_set_pin_function(COM1, PINMUX_PB07B_SLCD_LP1); + + gpio_set_pin_function(COM2, PINMUX_PB08B_SLCD_LP2); + + gpio_set_pin_function(SEG0, PINMUX_PB09B_SLCD_LP3); + + gpio_set_pin_function(SEG1, PINMUX_PA04B_SLCD_LP4); + + gpio_set_pin_function(SEG2, PINMUX_PA05B_SLCD_LP5); + + gpio_set_pin_function(SEG3, PINMUX_PA06B_SLCD_LP6); + + gpio_set_pin_function(SEG4, PINMUX_PA07B_SLCD_LP7); + + gpio_set_pin_function(SEG5, PINMUX_PA08B_SLCD_LP11); + + gpio_set_pin_function(SEG6, PINMUX_PA09B_SLCD_LP12); + + gpio_set_pin_function(SEG7, PINMUX_PA10B_SLCD_LP13); + + gpio_set_pin_function(SEG8, PINMUX_PA11B_SLCD_LP14); + + gpio_set_pin_function(SEG9, PINMUX_PB11B_SLCD_LP21); + + gpio_set_pin_function(SEG10, PINMUX_PB12B_SLCD_LP22); + + gpio_set_pin_function(SEG11, PINMUX_PB13B_SLCD_LP23); + + gpio_set_pin_function(SEG12, PINMUX_PB14B_SLCD_LP24); + + gpio_set_pin_function(SEG13, PINMUX_PB15B_SLCD_LP25); + + gpio_set_pin_function(SEG14, PINMUX_PA12B_SLCD_LP28); + + gpio_set_pin_function(SEG15, PINMUX_PA13B_SLCD_LP29); + + gpio_set_pin_function(SEG16, PINMUX_PA14B_SLCD_LP30); + + gpio_set_pin_function(SEG17, PINMUX_PA15B_SLCD_LP31); + + gpio_set_pin_function(SEG18, PINMUX_PA16B_SLCD_LP32); + + gpio_set_pin_function(SEG19, PINMUX_PA17B_SLCD_LP33); + + gpio_set_pin_function(SEG20, PINMUX_PA18B_SLCD_LP34); + + gpio_set_pin_function(SEG21, PINMUX_PA19B_SLCD_LP35); + + gpio_set_pin_function(SEG22, PINMUX_PB16B_SLCD_LP42); + + gpio_set_pin_function(SEG23, PINMUX_PB17B_SLCD_LP43); +} +/** + * \brief SLCD initialization function + * + * Enables SLCD peripheral, clocks and initializes SLCD driver + */ +void SEGMENT_LCD_0_init(void) +{ + hri_mclk_set_APBCMASK_SLCD_bit(SLCD); + slcd_sync_init(&SEGMENT_LCD_0, SLCD); + SEGMENT_LCD_0_PORT_init(); +} diff --git a/Sensor Watch Starter Project/hw/watch.c b/Sensor Watch Starter Project/hw/watch.c new file mode 100644 index 00000000..45f814f8 --- /dev/null +++ b/Sensor Watch Starter Project/hw/watch.c @@ -0,0 +1,303 @@ +/* + * watch.c + * + * Created: 4/25/2021 10:22:10 AM + * Author: joeycastillo + */ + +#include "watch.h" +#include +#include + +void watch_init(Watch *watch) { + memset(watch, 0, sizeof(*watch)); + // use switching regulator + SUPC->VREG.bit.SEL = 1; + while(!SUPC->STATUS.bit.VREGRDY); + // TODO: use performance level 0 +// _set_performance_level(0); +// hri_pm_write_PLCFG_PLDIS_bit(PM, true); +} + +const uint8_t Character_Set[] = +{ + 0b00000000, // + 0b00000000, // ! + 0b00100010, // " + 0b00000000, // # + 0b00000000, // $ + 0b00000000, // % + 0b01000100, // & + 0b00100000, // ' + 0b00000000, // ( + 0b00000000, // ) + 0b00000000, // * + 0b11000000, // + + 0b00010000, // , + 0b01000000, // - + 0b00000100, // . + 0b00010010, // / + 0b00111111, // 0 + 0b00000110, // 1 + 0b01011011, // 2 + 0b01001111, // 3 + 0b01100110, // 4 + 0b01101101, // 5 + 0b01111101, // 6 + 0b00000111, // 7 + 0b01111111, // 8 + 0b01101111, // 9 + 0b00000000, // : + 0b00000000, // ; + 0b01011000, // < + 0b01001000, // = + 0b01001100, // > + 0b01010011, // ? + 0b11111111, // @ + 0b01110111, // A + 0b01111111, // B + 0b00111001, // C + 0b00111111, // D + 0b01111001, // E + 0b01110001, // F + 0b00111101, // G + 0b01110110, // H + 0b10001001, // I + 0b00001110, // J + 0b11101010, // K + 0b00111000, // L + 0b10110111, // M + 0b00110111, // N + 0b00111111, // O + 0b01110011, // P + 0b01100111, // Q + 0b11110111, // R + 0b01101101, // S + 0b10000001, // T + 0b00111110, // U + 0b00111110, // V + 0b10111110, // W + 0b01111110, // X + 0b01101110, // Y + 0b00011011, // Z + 0b00111001, // [ + 0b00100100, // backslash + 0b00001111, // ] + 0b00100110, // ^ + 0b00001000, // _ + 0b00000010, // ` + 0b01011111, // a + 0b01111100, // b + 0b01011000, // c + 0b01011110, // d + 0b01111011, // e + 0b01110001, // f + 0b01101111, // g + 0b01110100, // h + 0b00010000, // i + 0b01000010, // j + 0b11101010, // k + 0b00110000, // l + 0b10110111, // m + 0b01010100, // n + 0b01011100, // o + 0b01110011, // p + 0b01100111, // q + 0b01010000, // r + 0b01101101, // s + 0b01111000, // t + 0b01100010, // u + 0b01100010, // v + 0b10111110, // w + 0b01111110, // x + 0b01101110, // y + 0b00011011, // z + 0b00111001, // { + 0b00110000, // | + 0b00001111, // } + 0b00000001, // ~ +}; + +void watch_enable_display(Watch *watch) { + if (watch->display_enabled) return; + + static const uint64_t segmap[] = { + 0x4e4f0e8e8f8d4d0d, // Position 8 + 0xc8c4c4c8b4b4b0b, // Position 9 + 0xc049c00a49890949, // Position 6 + 0xc048088886874707, // Position 7 + 0xc053921252139352, // Position 0 + 0xc054511415559594, // Position 1 + 0xc057965616179716, // Position 2 + 0xc041804000018a81, // Position 3 + 0xc043420203048382, // Position 4 + 0xc045440506468584, // Position 5 + }; + watch->num_chars = 10; + watch->segment_map = &segmap[0]; + + SEGMENT_LCD_0_init(); + slcd_sync_enable(&SEGMENT_LCD_0); + watch->display_enabled = true; +} + +void watch_display_pixel(Watch *watch, uint8_t com, uint8_t seg) { + slcd_sync_seg_on(&SEGMENT_LCD_0, SLCD_SEGID(com, seg)); +} + +void watch_clear_pixel(Watch *watch, uint8_t com, uint8_t seg) { + slcd_sync_seg_off(&SEGMENT_LCD_0, SLCD_SEGID(com, seg)); +} + +void watch_display_character(Watch *watch, uint8_t character, uint8_t position) { + uint64_t segmap = watch->segment_map[position]; + uint64_t segdata = Character_Set[character - 0x20]; + + for (int i = 0; i < 8; i++) { + uint8_t com = (segmap & 0xFF) >> 6; + if (com > 2) { + // COM3 means no segment exists; skip it. + segmap = segmap >> 8; + segdata = segdata >> 1; + continue; + } + uint8_t seg = segmap & 0x3F; + slcd_sync_seg_off(&SEGMENT_LCD_0, SLCD_SEGID(com, seg)); + if (segdata & 1) slcd_sync_seg_on(&SEGMENT_LCD_0, SLCD_SEGID(com, seg)); + segmap = segmap >> 8; + segdata = segdata >> 1; + } +} + +void watch_display_string(Watch *watch, char *string, uint8_t position) { + size_t i = 0; + while(string[i] != 0) { + watch_display_character(watch, string[i], position + i); + i++; + if (i >= watch->num_chars) break; + } +} + +void watch_enable_buttons(Watch *watch) { + EXTERNAL_IRQ_0_init(); +} + +void watch_register_button_callback(Watch *watch, const uint32_t pin, ext_irq_cb_t callback) { + ext_irq_register(pin, callback); +} + +void watch_enable_led(Watch *watch) { + if (watch->led_enabled) return; + + PWM_0_init(); + pwm_set_parameters(&PWM_0, 10000, 0); + pwm_enable(&PWM_0); + + watch->led_enabled = true; + watch_set_led_off(); +} + +void watch_disable_led(Watch *watch) { + if (!watch->led_enabled) return; + + gpio_set_pin_function(RED, GPIO_PIN_FUNCTION_OFF); + gpio_set_pin_function(GREEN, GPIO_PIN_FUNCTION_OFF); + + pwm_disable(&PWM_0); + + watch->led_enabled = false; +} + +void watch_set_led_color(uint16_t red, uint16_t green) { + TC3->COUNT16.CC[0].reg = red; + TC3->COUNT16.CC[1].reg = green; +} + +void watch_set_led_red() { + watch_set_led_color(65535, 0); +} + +void watch_set_led_green() { + watch_set_led_color(0, 65535); +} + +void watch_set_led_off() { + watch_set_led_color(0, 0); +} + +void watch_enable_date_time(Watch *watch) { + if (watch->calendar_enabled) return; + CALENDAR_0_init(); + calendar_enable(&CALENDAR_0); + + watch->calendar_enabled = true; +} + +void watch_set_date_time(struct calendar_date_time date_time) { + calendar_set_date(&CALENDAR_0, &date_time.date); + calendar_set_time(&CALENDAR_0, &date_time.time); +} + +void watch_get_date_time(struct calendar_date_time *date_time) { + calendar_get_date_time(&CALENDAR_0, date_time); +} + +static ext_irq_cb_t tick_user_callback; + +static void tick_callback(struct calendar_dev *const dev) { + tick_user_callback(); +} + +void watch_enable_tick(ext_irq_cb_t callback) { + tick_user_callback = callback; + // TODO: rename this method to reflect that it now sets the PER7 interrupt. + _tamper_register_callback(&CALENDAR_0.device, &tick_callback); +} + +void watch_enable_analog(Watch *watch, const uint8_t pin) { + if (!watch->adc_enabled) ADC_0_init(); + + switch (pin) { + case A0: + gpio_set_pin_function(A0, PINMUX_PB04B_ADC_AIN12); + break; + case A1: + gpio_set_pin_function(A1, PINMUX_PB01B_ADC_AIN9); + break; + case A2: + gpio_set_pin_function(A2, PINMUX_PB02B_ADC_AIN10); + break; + default: + return; + } + gpio_set_pin_direction(pin, GPIO_DIRECTION_OFF); +} + +void watch_enable_digital_input(const uint8_t pin) { + gpio_set_pin_direction(pin, GPIO_DIRECTION_IN); + gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF); +} + +void watch_enable_digital_output(const uint8_t pin) { + gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT); + gpio_set_pin_function(pin, GPIO_PIN_FUNCTION_OFF); +} + +struct io_descriptor *I2C_0_io; + +void watch_enable_i2c(Watch *watch) { + if (watch->i2c_enabled) return; + I2C_0_init(); + i2c_m_sync_get_io_descriptor(&I2C_0, &I2C_0_io); + i2c_m_sync_enable(&I2C_0); +} + +void watch_i2c_send(int16_t addr, uint8_t *buf, uint16_t length) { + i2c_m_sync_set_slaveaddr(&I2C_0, addr, I2C_M_SEVEN); + io_write(I2C_0_io, buf, length); +} + +void watch_i2c_receive(int16_t addr, uint8_t *buf, uint16_t length) { + i2c_m_sync_set_slaveaddr(&I2C_0, addr, I2C_M_SEVEN); + io_read(I2C_0_io, buf, length); +} diff --git a/Sensor Watch Starter Project/hw/watch.h b/Sensor Watch Starter Project/hw/watch.h new file mode 100644 index 00000000..53a0c111 --- /dev/null +++ b/Sensor Watch Starter Project/hw/watch.h @@ -0,0 +1,63 @@ +/* + * Watch.h + * + * Created: 4/25/2021 8:29:16 AM + * Author: joeycastillo + */ + + +#ifndef WATCH_H_ +#define WATCH_H_ +#include +#include "driver_init.h" +#include "hpl_calendar.h" +#include "hal_ext_irq.h" + +typedef struct Watch { + 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; + const uint64_t* segment_map; +} Watch; + +void watch_init(Watch *watch); + +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_red(); +void watch_set_led_green(); +void watch_set_led_off(); + +void watch_enable_date_time(Watch *watch); +void watch_set_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_analog(Watch *watch, const uint8_t pin); + +void watch_enable_buttons(Watch *watch); +void watch_register_button_callback(Watch *watch, const uint32_t pin, ext_irq_cb_t callback); + +void watch_enable_digital_input(const uint8_t pin); +void watch_enable_digital_output(const uint8_t pin); + +struct io_descriptor *I2C_0_io; + +void watch_enable_i2c(Watch *watch); +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); + +#endif /* WATCH_H_ */ \ No newline at end of file diff --git a/Sensor Watch Starter Project/main.c b/Sensor Watch Starter Project/main.c index 07f3be90..4569c4be 100755 --- a/Sensor Watch Starter Project/main.c +++ b/Sensor Watch Starter Project/main.c @@ -32,16 +32,20 @@ #include #include #include +#include #include "saml22.h" #include "hal_init.h" #include "peripheral_clk_config.h" #include "hal_gpio.h" +#include "atmel_start_pins.h" +#include "watch.h" //----------------------------------------------------------------------------- -HAL_GPIO_PIN(LED, A, 21) HAL_GPIO_PIN(UART_TX, B, 0) HAL_GPIO_PIN(UART_RX, B, 2) +Watch watch; + //----------------------------------------------------------------------------- static void uart_init(uint32_t baud) { uint64_t br = (uint64_t)65536 * (CONF_CPU_FREQUENCY - 16 * baud) / CONF_CPU_FREQUENCY; @@ -82,19 +86,39 @@ static void uart_puts(char *s) { //----------------------------------------------------------------------------- static void sys_init(void) { init_mcu(); + watch_init(&watch); + watch_enable_display(&watch); + watch_enable_led(&watch); + watch_enable_date_time(&watch); + watch_enable_analog(&watch, A0); + watch_enable_buttons(&watch); + watch_enable_i2c(&watch); } //----------------------------------------------------------------------------- int main(void) { sys_init(); uart_init(115200); - HAL_GPIO_LED_out(); - HAL_GPIO_LED_set(); - uart_puts("\r\nHello, world!\r\n"); + 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) { - uart_putc('.'); } return 0; diff --git a/Sensor Watch Starter Project/make/Makefile b/Sensor Watch Starter Project/make/Makefile index 7a402524..43d0e803 100755 --- a/Sensor Watch Starter Project/make/Makefile +++ b/Sensor Watch Starter Project/make/Makefile @@ -61,6 +61,8 @@ INCLUDES += \ SRCS += \ ../main.c \ ../startup_saml22.c \ + ../hw/driver_init.c \ + ../hw/watch.c \ ../hal/src/hal_adc_sync.c \ ../hal/src/hal_atomic.c \ ../hal/src/hal_calendar.c \