I have controlled an LED with a push button connected to GPIO pins P0.03, P0.04 respectively.Now I want to use those pins as CLK and DIO pins of TM1637 Display.I wrote a basic program to control that display as follows:
#include <zephyr.h>
#include <device.h>
#include <devicetree.h>
#include <drivers/gpio.h>
#include <sys/printk.h>
#define SLEEP_TIME_MS 10
// Defining GPIO pins
#define CLK_NODE DT_ALIAS(gpiocus0)
#if DT_NODE_HAS_STATUS(CLK_NODE, okay)
#define CLK DT_GPIO_LABEL(CLK_NODE, gpios)
#define CLK_PIN DT_GPIO_PIN(CLK_NODE, gpios)
#define CLK_FLAGS DT_GPIO_FLAGS(CLK_NODE, gpios)
#else
#error "Unsupported board: gpio22 devicetree alias is not defined"
#endif
#define DIO_NODE DT_ALIAS(gpiocus1)
#if DT_NODE_HAS_STATUS(DIO_NODE, okay)
#define DIO DT_GPIO_LABEL(DIO_NODE, gpios)
#define DIO_PIN DT_GPIO_PIN(DIO_NODE, gpios)
#define DIO_FLAGS DT_GPIO_FLAGS(DIO_NODE, gpios)
#else
#error "Unsupported board: gpio24 devicetree alias is not defined"
#endif
const struct device *dev;
void writeByte(uint8_t wr_data);
/**
* @brief Function for application main entry.
*/
void main(void)
{
printk("Blink LED \n\r");
int ret;
dev = device_get_binding(CLK);
if (dev == NULL)
{
return;
}
ret = gpio_pin_configure(dev, CLK_PIN, GPIO_OUTPUT_ACTIVE | CLK_FLAGS);
if (ret < 0)
{
return;
}
ret = gpio_pin_configure(dev, DIO_PIN, GPIO_OUTPUT_ACTIVE | DIO_FLAGS);
if (ret < 0)
{
return;
}
writeByte(0x08);
}
/** @} */
void writeByte(uint8_t wr_data)
{
uint8_t i;
for (i = 0; i < 8; i++) // sent 8bit data
{
gpio_pin_set(dev, CLK_PIN, 0);
if (wr_data & 0x01)
{
gpio_pin_set(dev, DIO_PIN, 1);
}
else
{
gpio_pin_set(dev, DIO_PIN, 0);
}
wr_data >>= 1;
gpio_pin_set(dev, CLK_PIN, 1);
}
printk("Data : %d",wr_data);
}
The device tree overlay file is given as :
// To get started, press Ctrl+Space to bring up the completion menu and view the available nodes.
// For more help, browse the DeviceTree documentation at https://docs.zephyrproject.org/latest/guides/dts/index.html
/ {
gpiocustom {
compatible = "gpio-keys";
gpiocus0: gpiocus_0 {
gpios = <&gpio0 3 GPIO_ACTIVE_LOW>;
label = "Custom gpio 3";
};
gpiocus1: gpiocus_1 {
gpios = <&gpio0 4 GPIO_ACTIVE_LOW>;
label = "Custom gpio 4";
};
};
aliases {
gpiocus0 = &gpiocus0;
gpiocus1 = &gpiocus1;
};
};
On building this code, I won't get any error. It build successfully. When I flash that to my nRF52840 DK with TM1637 Display connected to the GPIO pins , I won't get any output on the Display. I don't know what I am missing. Please guide me to get the display working for my DK.
Reference:
TM1637 Display program from Devzone.
I modified the code mentioned by an Nordic Engineer in the above mentioned link, to the current data types and device trees.
Hardware :
nRF52840 DK - 1
TM1637 Four Digit Seven Segment Display - 1
Software :
VS Code with nRF Connect Extension
nRF Connect SDk - v 1.9.1
nRf Connect Toolchain - v 1.9.1
Questions:
1) Is the C program to control the display is correct or not ?
2) I am able to declare only the analog pins (Pin No : 3, 4, 28, 29, 30, 31) led and push button. Other than that , I won't be able to declare other GPIO pins for even Blink example. How can I use other GPIO pins for my purposes ?
Please reply as soon as possible.
Thankyou.
-Vicky