This section describes the simplest and most basic things. The ios operation of the cc2540 uses the Port port as the gpio. If you don't talk much about it, let's look at it.
1. Hardware Circuit
The most simple hardware circuit is to use a gpio to control the LED light. Since gpio is used as the output, the gpio mode is not required.
It is quite simple to control the gpio on the left to switch the LED lights.
2. Related registers
P0sel |
P0 [] registers, which are set as common I/O ports by default |
P0indium |
P0 [] can be used as the circuit mode register when the input port is used as the input port. |
P0 |
P0 [] I/O registers addressable |
P0dir |
P0 port input and output settings register |
P0sel 0: Common IO port 1: second function
P0dir 0: Input 1: Output
P0indium 0: Pull up/pull down 1: three States
3. Code Implementation
Let's take p1_0 as an example to see how it is implemented in the code.
Halledinit function in hal_led.c:
void HalLedInit (void){#if (HAL_LED == TRUE) HalLedSet(HAL_LED_ALL, HAL_LED_MODE_OFF); // Initialize all LEDs to OFF. // Set LED GPIOs to outputs. LED1_DDR |= LED1_BV;#if (!defined HAL_PA_LNA && !defined HAL_PA_LNA_CC2590) LED2_DDR |= LED2_BV;#if (!defined CC2540_MINIDK && !defined HAL_BOARD_CC2540USB) LED3_DDR |= LED3_BV;#endif#endif#if defined BLINK_LEDS HalLedStatusControl.sleepActive = FALSE; // Initialize sleepActive to FALSE.#endif#endif
Macro definition
/* 1 - Green */#define LED1_BV BV(0)#define LED1_SBIT P1_0#define LED1_DDR P1DIR#define LED1_POLARITY ACTIVE_HIG
Switch led
#define HAL_TURN_OFF_LED1() st( LED1_SBIT = LED1_POLARITY (0); ) #define HAL_TURN_OFF_LED2() st( LED2_SBIT = LED2_POLARITY (0); ) #define HAL_TURN_OFF_LED3() st( LED3_SBIT = LED3_POLARITY (0); ) #define HAL_TURN_OFF_LED4() HAL_TURN_OFF_LED1() #define HAL_TURN_ON_LED1() st( LED1_SBIT = LED1_POLARITY (1); ) #define HAL_TURN_ON_LED2() st( LED2_SBIT = LED2_POLARITY (1); ) #define HAL_TURN_ON_LED3() st( LED3_SBIT = LED3_POLARITY (1); ) #define HAL_TURN_ON_LED4() HAL_TURN_ON_LED1(
Extract the key code:
1) set p1_0 to output
P1_DIR |= 0x01;
2) Set p1_0 to a high level to light the LED
P1_0 = 1;
2) Set p1_0 to low and turn off the LED
P1_0 = 0;
In fact, it is not difficult to look at the Hal encapsulation for each interface in the protocol stack.
This section is here first.
Bluetooth Low-Power ble: Controls gpio to light LEDs