Hello everybody!
It's my first question and I'm new at mcu programming. I hope for your understanding!
I have a task:
count seconds using a counter and display them on a LED matrix (I attached the code only for initialization and places with the counter and its callback function) at the moment I want to use the following logic:
the counter overflows, the callback function is called, a variable is incremented in it, which, when counting the seconds passed, is multiplied by 512 (since the counter is 24 bits, and the frequency is 32768 Hz) and added to the time that is currently saturated
But now it doesn't work. i can't see printk from counter_callback in rtt viewer
Another code works. Work queue works. Seconds that counted in counter less than 512 I can display
#include <stdio.h> #include <zephyr/kernel.h> #include <zephyr/drivers/gpio.h> #include <zephyr/drivers/counter.h> #include <zephyr/device.h> #include <zephyr/bluetooth/bluetooth.h> #include <zephyr/bluetooth/gap.h> //INITIALIZATION FUNCTIONS //int counter_init(void); //FUNCTION TO INIT COUNTER int bt_le_init(void); //FUNCTION TO INIT BLE int number_length(int num); //FUNCTION TO CALCULATE THE LENGTH OF THE NUMBER OF SECONDS ELAPSED void number_to_arr(uint32_t num, uint8_t *arr); //FUNCTION TO DIVIDE NUMBER TO DIGITS IN ARRAY void array_remove(const uint8_t *arr_initial, uint8_t *arr_final, uint8_t size, uint8_t index); //FUNCTION TO REMOVE ELEMENT FROM ARRAY void button_isr(const struct device *dev, struct gpio_callback *cb, gpio_port_pins_t pins); //BUTTON ISR PROTOTYPE //static void counter_top_callback(const struct device *dev, void *user_data); //COUNTER ISR PROTOTYPE void k_work_q_seconds_display(struct k_work *work); //K_WORK_Q FUNCTION TO DISPLAY DIGITS void seconds_display(void); //FUNCTION TO DISLAY TIME #define BUTTON_PIN 25 //BUTTON PIN NUMBER #define LED_GPIO DT_NODELABEL(gpio0) //GPIO0 #define COUNTER DT_ALIAS(rtc0) //COUNTER #define THREAD_STACK_SIZE 2048 //STACKSIZE FOR WORK QUEUE #define WORQ_PRIORITY 5 //WORK QUEUE PRIORITY #define INITQ_PRIORITY 4 //INITIALIZATION QUEUE PRIORITY #define LED_ON_TIME 900 //HOW LONG WILL EACH DIGIT LIGHT UP #define LED_OFF_TIME (1000 - LED_ON_TIME) //HOW LONG WILL EACH DIGIT LIGHT DOWN //DEFINES FOR BLE #define DEVICE_NAME CONFIG_BT_DEVICE_NAME //DEVICE NAME #define DEVICE_NAME_LEN sizeof(DEVICE_NAME) - 1 //DEVICE NAME LENGTH //VARIABLES uint8_t index; //INDEX OF THE PIN TO BE REMOVED FROM THE ARRAY const uint8_t led_matrix [9] = {5, 6, 9, 10, 12, 14, 15, 16, 18}; //LED MATRIX PINS uint8_t led_matrix_arr_size = sizeof(led_matrix) / sizeof(led_matrix[0]); //LED MATRIX ARRAY LENGTH uint8_t turned_on_led [8]; //ARRAY FOR uint8_t seconds_length; //LENGTH OF SECONDS STRING uint8_t seconds_arr [9]; //SECONDS AS ARRAY, WHERE FIRST DIGIT IN ARRAY IS FIRST DIGIT IN LED MATRIX uint32_t seconds = 0; //SECONDS ELATED const uint8_t NUMBERS [10][8] = { {0, 0, 0, 0, 1, 1, 0, 0}, {1, 1, 0, 1, 1, 0, 1, 0}, {0, 1, 0, 1, 1, 1, 1, 0}, {0, 1, 1, 0, 1, 1, 0, 0}, {0, 1, 1, 1, 0, 1, 1, 0}, {1, 1, 1, 1, 0, 1, 1, 0}, {0, 0, 0, 1, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 0}, {1, 0, 1, 1, 1, 1, 1, 0} }; const struct device *gpio_dev = DEVICE_DT_GET(LED_GPIO); static struct gpio_callback button_cb_data; struct counter_alarm_cfg alarm_cfg; static K_THREAD_STACK_DEFINE(Time_Keeper_stack_area, THREAD_STACK_SIZE); static struct k_work_q Time_Keeper_work_q = {0}; static unsigned char url_data[] ={0x17,'/','/','a','c','a','d','e','m','y','.', 'n','o','r','d','i','c','s','e','m','i','.', 'c','o','m'}; static const struct bt_data sd[] = { BT_DATA(BT_DATA_URI, url_data,sizeof(url_data)), }; static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR), BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; struct work_info { struct k_work work; char name[25]; }display_work; //ISR volatile uint8_t matrix_on = 0; void button_isr(const struct device *dev, struct gpio_callback *cb, gpio_port_pins_t pins){ if (matrix_on == 0){ matrix_on = 1; printk("LED MATRIX turned on\n"); k_work_submit_to_queue(&Time_Keeper_work_q, &display_work.work); } } uint32_t full_counter_times = 0; static void my_counter_handler(const struct device *dev, void *user_data) { full_counter_times++; printk("Tick! %d seconds\n", full_counter_times); } const struct device *counter_dev = DEVICE_DT_GET(COUNTER); int main(void) { //INITIALIZING LEDs AND BUTTON printk("main() started\n"); if (!device_is_ready(gpio_dev)){ printk("GPIO0 is not ready\n"); return -1; } printk("GPIO0 is ready\n"); for (int i = 0; i < 9; i++){ if (gpio_pin_configure(gpio_dev, led_matrix[i], GPIO_OUTPUT) < 0){ printk("GPIO0 %d is not configured\n", led_matrix[i]); return -1; } printk("GPIO0 %d is configured\n", led_matrix[i]); } if (gpio_pin_configure(gpio_dev, BUTTON_PIN, GPIO_INPUT|GPIO_PULL_DOWN) < 0){ printk("BUTTON is not configured\n"); return -1; } printk("BUTTON is configured\n"); //BUTTON INTERRUPT gpio_pin_interrupt_configure(gpio_dev, BUTTON_PIN, GPIO_INT_EDGE_TO_ACTIVE); gpio_init_callback(&button_cb_data, button_isr, BIT(BUTTON_PIN)); gpio_add_callback(gpio_dev, &button_cb_data); if (!device_is_ready(counter_dev)){ //LOG_ERR("COUNTER is not ready"); printk("Counter is not ready\n"); return -1; } counter_start(counter_dev); uint32_t freq = counter_get_frequency(counter_dev) * 10; counter_set_top_value(counter_dev, &(struct counter_top_cfg){ .callback = my_counter_handler, .ticks = freq, .user_data = NULL, .flags = 0, }); //WORK QUEUE FOR SECONDS_DISPLAY TO VIEW SECONDS k_work_queue_start(&Time_Keeper_work_q, Time_Keeper_stack_area, K_THREAD_STACK_SIZEOF(Time_Keeper_stack_area), WORQ_PRIORITY, NULL); k_work_init(&display_work.work, k_work_q_seconds_display); //INITIALIZING COUNTER AND BLE //counter_init(); bt_le_init(); while(1){ k_msleep(1000); } return 0; }