cmem7_gpio.c 5.5 KB
Newer Older
wuyangyong's avatar
wuyangyong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
/**
	*****************************************************************************
	* @file     cmem7_gpio.c
	*
	* @brief    CMEM7 GPIO source file
	*
	*
	* @version  V1.0
	* @date     3. September 2013
	*
	* @note               
	*           
	*****************************************************************************
	* @attention
	*
	* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
	* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
	* TIME. AS A RESULT, CAPITAL-MICRO SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 
	* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
	* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
	* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
	*
	* <h2><center>&copy; COPYRIGHT 2013 Capital-micro </center></h2>
	*****************************************************************************
	*/
	
#include "cmem7_gpio.h"

#define GPIO_GROUP_GPIO_H 					(GPIO_GROUP_GPIO + 1)
#define GPIO_GROUP_GPIO_N 					(GPIO_GROUP_GPIO + 2)

#define IS_INNER_GPIO_GROUP(GROUP)  (((GROUP) == GPIO_GROUP_GPIO) || \
																			((GROUP) == GPIO_GROUP_GPIO_H) || \
																			((GROUP) == GPIO_GROUP_GPIO_N))

#define GPIO_PWM_CHANNEL_GPIO_H_9 	(GPIO_PWM_CHANNEL_GPIO_31 + 1)
#define GPIO_PWM_CHANNEL_GPIO_H_19 	(GPIO_PWM_CHANNEL_GPIO_31 + 2)
#define GPIO_PWM_CHANNEL_GPIO_H_20 	(GPIO_PWM_CHANNEL_GPIO_31 + 3)
        
#define IS_INNER_GPIO_PWM_CHANNEL(CHANNEL)  (((CHANNEL) == GPIO_PWM_CHANNEL_GPIO_31) || \
                                       ((CHANNEL) == GPIO_PWM_CHANNEL_GPIO_H_9) || \
                                       ((CHANNEL) == GPIO_PWM_CHANNEL_GPIO_H_19) || \
                                       ((CHANNEL) == GPIO_PWM_CHANNEL_GPIO_H_20))
static uint32_t gpio_GetClock() {
	return SYSTEM_CLOCK_FREQ / (1 << (GLOBAL_CTRL->CLK_SEL_0_b.GPIO_CLK + 1));
}

void GPIO_Init(uint8_t Group, uint32_t PositiveTrigger) {
	assert_param(IS_GPIO_GROUP(Group));
	
	if (Group == GPIO_GROUP_GPIO) {
	  GPIO->GPIO_POSITIVE_EDGE_INT_TRIGGER = PositiveTrigger;
	} else if (Group == GPIO_GROUP_GPIO_H) {
		GPIO->GPIO_H_POSITIVE_EDGE_INT_TRIGGER = PositiveTrigger;
	} else {
		GPIO->GPIO_N_POSITIVE_EDGE_INT_TRIGGER = PositiveTrigger;
	}
}

void GPIO_EnableOutput(uint8_t Group, uint32_t Enable) {
	assert_param(IS_GPIO_GROUP(Group));
	
	if (Group == GPIO_GROUP_GPIO) {
	  GPIO->GPIO_OE = Enable;
	} else if (Group == GPIO_GROUP_GPIO_H) {
		GPIO->GPIO_H_OE = Enable;
	} else {
		GPIO->GPIO_N_OE = Enable;
	}
}

void GPIO_EnableInt(uint8_t Group, uint32_t Enable) {
	assert_param(IS_GPIO_GROUP(Group));
	
	if (Group == GPIO_GROUP_GPIO) {
	  GPIO->GPIO_INT_MASK = ~Enable;
	} else if (Group == GPIO_GROUP_GPIO_H) {
		GPIO->GPIO_H_INT_MASK = ~Enable;
	} else {
		GPIO->GPIO_N_INT_MASK = ~Enable;
	}
}

uint32_t GPIO_GetIntStatus(uint8_t Group) {
	assert_param(IS_GPIO_GROUP(Group));
	
	if (Group == GPIO_GROUP_GPIO) {
	  return GPIO->GPIO_INT_STATUS;
	} else if (Group == GPIO_GROUP_GPIO_H) {
		return GPIO->GPIO_H_INT_STATUS;
	} 
	
	return GPIO->GPIO_N_INT_STATUS;
}

void GPIO_ClearInt(uint8_t Group, uint32_t Clear) {
	assert_param(IS_GPIO_GROUP(Group));
	
	if (Group == GPIO_GROUP_GPIO) {
	  GPIO->GPIO_INT_STATUS = Clear;
	} else if (Group == GPIO_GROUP_GPIO_H) {
		GPIO->GPIO_H_INT_STATUS = Clear;
	} else {
		GPIO->GPIO_N_INT_STATUS = Clear;
	}
}

uint32_t GPIO_Read(uint8_t Group) {
  uint32_t data;
	
	assert_param(IS_GPIO_GROUP(Group));
	
	if (Group == GPIO_GROUP_GPIO) {
	  data = GPIO->GPIO_IN;
	} else if (Group == GPIO_GROUP_GPIO_H) {
		data = GPIO->GPIO_H_IN;
	} else {
		data = GPIO->GPIO_N_IN;
	}
	
	return data;
}

void GPIO_Write(uint8_t Group, uint32_t Unmask, uint32_t data) {
	assert_param(IS_GPIO_GROUP(Group));
	
	if (Group == GPIO_GROUP_GPIO) {
		GPIO->GPIO_OUT_UNMASK = Unmask;
	  GPIO->GPIO_OUT_DATA = data;
	} else if (Group == GPIO_GROUP_GPIO_H) {
		GPIO->GPIO_H_OUT_UNMASK = Unmask;
	  GPIO->GPIO_H_OUT_DATA = data;
	} else {
		GPIO->GPIO_N_OUT_UNMASK = Unmask;
	  GPIO->GPIO_N_OUT_DATA = data;
	}
}

void GPIO_InitPwm(uint8_t Channel, uint32_t HighLevelNanoSecond, uint32_t LowLevelNanoSecond) {
	uint16_t lowTick, highTick;
	
	assert_param(IS_GPIO_PWM_CHANNEL(Channel));
	
	lowTick = LowLevelNanoSecond * (gpio_GetClock() / 1000000) / 1000;
	highTick = HighLevelNanoSecond * (gpio_GetClock() / 1000000) / 1000;
	lowTick = (lowTick < 1) ? lowTick : lowTick - 1;
	highTick = (highTick < 1) ? highTick : highTick - 1;
	
	if (Channel == GPIO_PWM_CHANNEL_GPIO_31) {
		GPIO->PWM_OUT0_LEN_b.LOW_LEVEL_TICK = lowTick; 
		GPIO->PWM_OUT0_LEN_b.HIGH_LEVEL_TICK = highTick; 
	} else if (Channel == GPIO_PWM_CHANNEL_GPIO_H_9) {
		GPIO->PWM_OUT1_LEN_b.LOW_LEVEL_TICK = lowTick; 
		GPIO->PWM_OUT1_LEN_b.HIGH_LEVEL_TICK = highTick; 
	} else if (Channel == GPIO_PWM_CHANNEL_GPIO_H_19) {
		GPIO->PWM_OUT2_LEN_b.LOW_LEVEL_TICK = lowTick; 
		GPIO->PWM_OUT2_LEN_b.HIGH_LEVEL_TICK = highTick; 
  } else {
		GPIO->PWM_OUT3_LEN_b.LOW_LEVEL_TICK = lowTick; 
		GPIO->PWM_OUT3_LEN_b.HIGH_LEVEL_TICK = highTick; 
	}
}

void GPIO_EnablePwm(uint8_t Channel, BOOL Enable) {
	assert_param(IS_GPIO_PWM_CHANNEL(Channel));
	
	if (Channel == GPIO_PWM_CHANNEL_GPIO_31) {
		GPIO->PWM_OUT_EN_b.GPIO_31 = Enable; 
		GPIO->PWM_OUT_SEL_b.GPIO_31 = Enable; 
	} else if (Channel == GPIO_PWM_CHANNEL_GPIO_H_9) {
		GPIO->PWM_OUT_EN_b.GPIO_H_9 = Enable; 
		GPIO->PWM_OUT_SEL_b.GPIO_H_9 = Enable;
	} else if (Channel == GPIO_PWM_CHANNEL_GPIO_H_19) {
		GPIO->PWM_OUT_EN_b.GPIO_H_19 = Enable;
	GPIO->PWM_OUT_SEL_b.GPIO_H_19 = Enable;		
  } else {
		GPIO->PWM_OUT_EN_b.GPIO_H_20 = Enable; 
		GPIO->PWM_OUT_SEL_b.GPIO_H_20 = Enable;
	}
}