123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666 |
- #include <string.h>
- #include "stm32xxx_hal.h"
- #ifndef HAL_I2C_MODULE_ENABLED
- #define HAL_I2C_MODULE_ENABLED
- #pragma message("hal conf should enable i2c")
- #endif
- #include "x-nucleo-6180xa1.h"
- #define V1_CHIP_SWITCH_PORT GPIOA
- #define V1_CHIP_SWITCH_PIN GPIO_PIN_7
- #define V1_CHIP_ENABLE_PORT GPIOA
- #define V1_CHIP_ENABLE_PIN GPIO_PIN_5
- #define V1_IRQ_PIN GPIO_PIN_6
- #define V2_IRQ_PIN GPIO_PIN_0
- #ifdef __STM32F4xx_HAL_H
- #define V1_IRQ EXTI9_5_IRQn
- #define V2_IRQ EXTI0_IRQn
- #else
- #ifdef __STM32L4xx_HAL_H
- #define V1_IRQ EXTI9_5_IRQn
- #define V2_IRQ EXTI0_IRQn
- #else
- #define V1_IRQ EXTI4_15_IRQn
- #define V2_IRQ EXTI0_1_IRQn
- #endif
- #endif
- static void _V1_GPIO_Init(void);
- static void _V1_Set7SegmentDP( int dp);
- static void _V1_Set7Segment(uint8_t value);
- static void _V1_DisplayOff(void);
- static void _V2_GPIO_Init(void);
- static int _V2_GetSwicth(void);
- #define EXPANDER_I2C_ADDRESS (0x42*2)
- #define GPMR 0x10
- #define GPSR 0x12
- #define GPDR 0x14
- #define V2_D1 (1<<7)
- #define V2_D2 (1<<8)
- #define V2_D3 (1<<9)
- #define V2_D4 (1<<10)
- #define V2_DISP_SEL (1<<11)
- #define V2_CHIPEN (1<<12)
- #define V2_CHIPEN_B (1<<13)
- #define V2_CHIPEN_L (1<<14)
- #define V2_CHIPEN_R (1<<15)
- static int IsV2=0;
- I2C_HandleTypeDef *_hi2c;
- uint16_t _V2PadVal;
- static int I2cExpAddr=EXPANDER_I2C_ADDRESS;
- static int _err=0;
- static void _V2_DisplayOff(void);
- static void _V2_SetChipEn( int No, int state );
- int XNUCLEO6180XA1_IsV2(void){
- return IsV2;
- }
- int XNUCLEO6180XA1_EXTI_CallBackHandle(uint16_t GPIO_Pin){
- int IsVL6180XIntPin = IsV2 ? GPIO_Pin == V2_IRQ_PIN : GPIO_Pin == V1_IRQ_PIN;
- if( IsVL6180XIntPin){
- XNUCLEO6180XA1_UserIntHandler();
- }
- return IsVL6180XIntPin;
- }
- void XNUCLEO6180XA1_Reset(int state){
- if( !IsV2)
- HAL_GPIO_WritePin(V1_CHIP_ENABLE_PORT, V1_CHIP_ENABLE_PIN , (GPIO_PinState)state);
- else{
- _V2_SetChipEn(0, state);
- }
- }
- int XNUCLEO6180XA1_ResetId(int state, int id)
- {
- int status;
- if( IsV2 && id>=0 && id<4){
- _V2_SetChipEn(id, state);
- status=0;
- }
- else{
-
- status=-1;
- }
- return status;
- }
- int XNUCLEO6180XA1_GetSwitch(void){
- GPIO_PinState state ;
- if( !IsV2)
- state = HAL_GPIO_ReadPin(V1_CHIP_SWITCH_PORT, V1_CHIP_SWITCH_PIN);
- else{
- state= _V2_GetSwicth() ? GPIO_PIN_SET : GPIO_PIN_RESET ;
- }
- return state;
- }
- void XNUCLEO6180XA1_GPIO_Init(void) {
- }
- int V2_ExpanderRd(int index, uint8_t *data, int n_data){
- int status;
- uint8_t RegAddr;
- RegAddr=index;
- do{
- status=HAL_I2C_Master_Transmit(_hi2c, I2cExpAddr, &RegAddr, 1, 100);
- if( status )
- break;
- status =HAL_I2C_Master_Receive(_hi2c, I2cExpAddr, data, n_data, n_data*100);
- }while(0);
- return status;
- }
- int V2_ExpanderWR( int index, uint8_t *data, int n_data){
- int status;
- uint8_t RegAddr[0x10];
- RegAddr[0]=index;
- memcpy(RegAddr+1, data, n_data);
- status=HAL_I2C_Master_Transmit(_hi2c, I2cExpAddr, RegAddr, n_data+1, 100);
- return status;
- }
- static void _V2_SetChipEn( int No, int state ){
-
- int mask = (No==3) ? V2_CHIPEN_R : ((No==2) ? V2_CHIPEN_B : ((No==1) ? V2_CHIPEN_L : V2_CHIPEN));
- if( state)
- _V2PadVal|=mask ;
- else
- _V2PadVal&=~mask;
- V2_ExpanderWR(GPSR, (uint8_t*)&_V2PadVal,2);
- }
- int _V2_GetSwicth(){
- int status;
- uint16_t Value;
- status=V2_ExpanderRd(GPMR, (uint8_t*)&Value,2);
- if(status ==0 ){
- Value&=V2_DISP_SEL;
- }
- else{
-
- _err++;
- Value=0;
- }
- return Value;
- }
- static void _I2cFailRecover(){
- GPIO_InitTypeDef GPIO_InitStruct;
- int i, nRetry=0;
-
-
-
-
-
-
- __GPIOB_CLK_ENABLE();
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
- GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9 ;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
-
- do{
- for( i=0; i<10; i++){
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
- HAL_Delay(1);
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
- HAL_Delay(1);
- }
- }while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9) == 0 && nRetry++<7);
- if( HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9) == 0 ){
- __GPIOA_CLK_ENABLE();
-
- GPIO_InitStruct.Pin = GPIO_PIN_5 ;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- do{
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
- HAL_Delay(33);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
- HAL_Delay(33);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
- HAL_Delay(33);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
- HAL_Delay(33*20);
- }while(1);
- }
- }
- void XNUCLEO6180XA1_I2C1_Init(I2C_HandleTypeDef *hi2c1) {
- GPIO_InitTypeDef GPIO_InitStruct;
- uint8_t ExpanderID[2];
- int status;
- _I2cFailRecover();
-
- __GPIOB_CLK_ENABLE();
- __I2C1_CLK_ENABLE();
-
- GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
- GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- hi2c1->Instance = I2C1;
- #ifdef __STM32F4xx_HAL_H
- hi2c1->Init.ClockSpeed = 400000;
- hi2c1->Init.DutyCycle = I2C_DUTYCYCLE_2;
- #else
- hi2c1->Init.Timing = 0x00300F38;
- #endif
- hi2c1->Init.OwnAddress1 = 0;
- hi2c1->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- hi2c1->Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
- hi2c1->Init.OwnAddress2 = 0;
- hi2c1->Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
- hi2c1->Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
-
- HAL_I2C_Init(hi2c1);
-
-
-
- _hi2c=hi2c1;
- status = V2_ExpanderRd( 0, ExpanderID, 2);
- if( status == 0 && ExpanderID[0]==0 && ExpanderID[1]==0x16){
- IsV2=1;
- _V2_GPIO_Init();
- }
- else{
- _V1_GPIO_Init();
- }
- }
- #pragma weak XNUCLEO6180XA1_UserIntHandler
- void XNUCLEO6180XA1_UserIntHandler(void) {
- }
- void XNUCLEO6180XA1_ClearInterrupt(void){
- if( !IsV2 )
- NVIC_ClearPendingIRQ(V1_IRQ);
- else
- NVIC_ClearPendingIRQ(V2_IRQ);
- }
- void XNUCLEO6180XA1_DisableInterrupt(void){
- if( !IsV2){
- HAL_NVIC_DisableIRQ(V1_IRQ);
- __HAL_GPIO_EXTI_CLEAR_IT(V1_IRQ_PIN);
- NVIC_ClearPendingIRQ(V1_IRQ);
- }
- else{
- HAL_NVIC_DisableIRQ(V2_IRQ);
- __HAL_GPIO_EXTI_CLEAR_IT(V2_IRQ_PIN);
- NVIC_ClearPendingIRQ(V2_IRQ);
- }
- }
- void XNUCLEO6180XA1_EnableInterrupt(void){
- if( !IsV2 ){
- __HAL_GPIO_EXTI_CLEAR_IT(V1_IRQ_PIN);
- NVIC_ClearPendingIRQ(V1_IRQ);
- HAL_NVIC_EnableIRQ(V1_IRQ);
- }
- else{
- __HAL_GPIO_EXTI_CLEAR_IT(V2_IRQ_PIN);
- NVIC_ClearPendingIRQ(V2_IRQ);
- HAL_NVIC_EnableIRQ(V2_IRQ);
- }
- }
- #define PA 0
- #define PB 1
- #define PC 2
- #define MAKE_PAD(port, pin) (((port)<<4)|pin)
- static uint8_t DisplayEn[] = {
- MAKE_PAD( PB, 0),
- MAKE_PAD( PA, 4),
- MAKE_PAD( PA, 1),
- MAKE_PAD( PA, 0),
- };
- static uint8_t DisplaySeg[] = {
- MAKE_PAD( PB, 6 ),
- MAKE_PAD( PC, 7 ),
- MAKE_PAD( PA, 9 ),
- MAKE_PAD( PA, 8 ),
- MAKE_PAD( PB, 10),
- MAKE_PAD( PB, 4 ),
- MAKE_PAD( PB, 5 ),
- MAKE_PAD( PB, 3 ),
-
- };
- #undef MAKE_PAD
- #undef PA
- #undef PB
- #undef PC
- #define pad_write(pad, state) HAL_GPIO_WritePin( (GPIO_TypeDef*)((char*)GPIOA + (pad>>4)*((char*)GPIOB-(char*)GPIOA)), 1<<(pad&0x0F), state);
- #define DP (1<<7)
- #define NOT_7_NO_DP( ... ) (uint8_t) ~( __VA_ARGS__ + DP )
- #define S0 (1<<0)
- #define S1 (1<<1)
- #define S2 (1<<2)
- #define S3 (1<<3)
- #define S4 (1<<4)
- #define S5 (1<<5)
- #define S6 (1<<6)
- static const uint8_t ascii_to_display_lut[256]={
- [' ']= 0,
- ['-']= S6,
- ['_']= S3,
- ['=']= S3+S6,
- ['~']= S0+S3+S6,
- ['^']= S0,
- ['?']= NOT_7_NO_DP(S5+S3+S2),
- ['*']= NOT_7_NO_DP(),
- ['[']= S0+S3+S4+S5,
- [']']= S0+S3+S2+S1,
- ['@']= S0+S3,
- ['0']= NOT_7_NO_DP(S6),
- ['1']= S1+S2,
- ['2']= S0+S1+S6+S4+S3,
- ['3']= NOT_7_NO_DP(S4+S5),
- ['4']= S5+S1+S6+S2,
- ['5']= NOT_7_NO_DP(S1+S4),
- ['6']= NOT_7_NO_DP(S1),
- ['7']= S0+S1+S2,
- ['8']= NOT_7_NO_DP(0),
- ['9']= NOT_7_NO_DP(S4),
-
- ['a']= S2+ S3+ S4+ S6 ,
- ['b']= NOT_7_NO_DP(S0+S1),
- ['c']= S6+S4+S3,
- ['d']= NOT_7_NO_DP(S0+S5),
- ['e']= NOT_7_NO_DP(S2),
- ['f']= S6+S5+S4+S0,
- ['g']= NOT_7_NO_DP(S4),
- ['h']= S6+S5+S4+S2,
- ['i']= S4,
- ['j']= S1+S2+S3+S4,
- ['k']= S6+S5+S4+S2,
- ['l']= S3+S4,
- ['m']= S0+S4+S2,
- ['n']= S2+S4+S6,
- ['o']= S6+S4+S3+S2,
- ['p']= NOT_7_NO_DP(S3+S2),
- ['q']= S0+S1+S2+S5+S6,
- ['r']= S4+S6,
- ['s']= NOT_7_NO_DP(S1+S4),
- ['t']= NOT_7_NO_DP(S0+S1+S2),
- ['u']= S4+S3+S2+S5+S1,
- ['v']= S4+S3+S2,
- ['w']= S1+S3+S5,
- ['x']= NOT_7_NO_DP(S0+S3),
- ['y']= NOT_7_NO_DP(S0+S4),
- ['z']= S0+S1+S6+S4+S3,
-
- ['A']= NOT_7_NO_DP(S3),
- ['B']= NOT_7_NO_DP(S0+S1),
- ['C']= S0+S3+S4+S5,
- ['E']= NOT_7_NO_DP(S1+S2),
- ['F']= S6+S5+S4+S0,
- ['G']= NOT_7_NO_DP(S4),
- ['H']= NOT_7_NO_DP(S0+S3),
- ['I']= S1+S2,
- ['J']= S1+S2+S3+S4,
- ['K']= NOT_7_NO_DP(S0+S3),
- ['L']= S3+S4+S5,
- ['M']= S0+S4+S2,
- ['N']= S2+S4+S6,
- ['O']= NOT_7_NO_DP(S6),
- ['P']= S0+S1+S2+S5+S6,
- ['Q']= NOT_7_NO_DP(S3+S2),
- ['R']= S4+S6,
- ['S']= NOT_7_NO_DP(S1+S4),
- ['T']= NOT_7_NO_DP(S0+S1+S2),
- ['U']= NOT_7_NO_DP(S6+S0),
- ['V']= S4+S3+S2,
- ['W']= S1+S3+S5,
- ['X']= NOT_7_NO_DP(S0+S3),
- ['Y']= NOT_7_NO_DP(S0+S4),
- ['Z']= S0+S1+S6+S4+S3,
- };
- #undef S0
- #undef S1
- #undef S2
- #undef S3
- #undef S4
- #undef S5
- #undef S6
- #undef DP
- static void _V1_DisplayString(const char *str, int SegDelayMs){
- int i;
- const char *pc;
- for( i=0, pc=str; i<4 && *pc!=0 ; i++, pc++){
- _V1_Set7Segment( ascii_to_display_lut[(uint8_t)*pc]);
- if( *(pc+1)== '.'){
- _V1_Set7SegmentDP(1);
- pc++;
- }
-
- pad_write(DisplayEn[i], GPIO_PIN_RESET);
- XNUCLEO6180XA1_WaitMilliSec(SegDelayMs);
-
- pad_write(DisplayEn[i], GPIO_PIN_SET);
- }
- }
- static void _V2_Set7Segment( int Leds, int digit ){
- _V2PadVal |= 0x7F;
- _V2PadVal |= V2_D1|V2_D2|V2_D3|V2_D4;
- _V2PadVal &= ~(V2_D1<<digit);
- _V2PadVal &= ~(Leds&0x7F);
- V2_ExpanderWR(GPSR, (uint8_t*)&_V2PadVal, 2);
- }
- static void _V2_DisplayString(const char *str, int SegDelayMs){
- int i;
- const char *pc;
- for( i=0, pc=str; i<4 && *pc!=0 ; i++, pc++){
- _V2_Set7Segment( ascii_to_display_lut[(uint8_t)*pc], i);
- if( *(pc+1)== '.'){
- pc++;
- }
- XNUCLEO6180XA1_WaitMilliSec(SegDelayMs);
- _V2_DisplayOff();
- }
- }
- void XNUCLEO6180XA1_DisplayString(const char *str, int SegDelayMs){
- if( !IsV2)
- _V1_DisplayString(str, SegDelayMs);
- else
- _V2_DisplayString(str, SegDelayMs);
- }
- static void _V1_GPIO_Init(void) {
- GPIO_InitTypeDef GPIO_InitStruct;
-
- __GPIOC_CLK_ENABLE();
- __GPIOH_CLK_ENABLE();
- __GPIOA_CLK_ENABLE();
- __GPIOB_CLK_ENABLE();
-
- GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_8 | GPIO_PIN_9;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = GPIO_PIN_6;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_10 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6;
- #ifndef OS_USE_TRACE_ITM
- GPIO_InitStruct.Pin |= GPIO_PIN_3;
- #endif
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
-
- _V1_DisplayOff();
- }
- static void _V2_GPIO_Init(){
- uint16_t PadDir;
- GPIO_InitTypeDef GPIO_InitStruct;
- __GPIOA_CLK_ENABLE();
- __GPIOB_CLK_ENABLE();
-
-
- GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5 |GPIO_PIN_10 ;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
-
- PadDir=~V2_DISP_SEL;
- V2_ExpanderWR(GPDR, (uint8_t*)&PadDir, 2);
- _V2_DisplayOff();
- }
- static void _V2_DisplayOff() {
- _V2PadVal |= (V2_D1|V2_D2|V2_D3| V2_D4);
- V2_ExpanderWR(GPSR, (uint8_t*)&_V2PadVal, 2);
- }
- static void _V1_DisplayOff() {
- int i;
- for( i=0; i<4; i++ ){
- pad_write(DisplayEn[i], GPIO_PIN_SET);
- }
- }
- static void _V1_Set7Segment(const uint8_t value) {
- int i;
- GPIO_PinState state;
- for (i = 0; i < sizeof(DisplaySeg)/sizeof(DisplaySeg[0]); i++) {
- int cur_bit;
- cur_bit = value & (1 << i);
- state = cur_bit ? GPIO_PIN_RESET: GPIO_PIN_SET;
- pad_write(DisplaySeg[i], state);
- }
- }
- static void _V1_Set7SegmentDP( int dp_state) {
- GPIO_PinState state;
- state = dp_state ? GPIO_PIN_RESET: GPIO_PIN_SET;
- pad_write(DisplaySeg[7], state);
-
- }
|