Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
skruglewicz
Published

Connect things with code entry 1

For this this project my idea is to connect an Arduino Seeed Grove socket LED module to the Infineon PSoC 6 evaluation kit

BeginnerFull instructions providedOver 2 days66
Connect things with code entry 1

Things used in this project

Hardware components

Infineon PSoC 62S2 evaluation kit (CY8CEVAL-062S2)
×1
Infineon PSoC 62S4 pioneer kit (CY8CKIT- 062S4)
×1
Seeed Studio Grove Socket LED Module
×1

Story

Read more

Code

arduino_gpio.c

C/C++
library file
/*
 * arduino_gpio.c
 *
 *  Created on: 15 Agt 2023
 *      Author: DwiSetyabudi
 */

#include  "arduino_gpio.h"

const PinName digitalPin[]={
		P0__2,				// D0
		P0__3,				// D1
		P5__0,				// D2
		P5__1,				// D3
		P5__6,				// D4
		P5__7,				// D5
		P6__2,				// D6
		P6__3,				// D7
		P2__4,				// D8
		P2__6,				// D9
		P2__3,				// D10
		P2__0,				// D11
		P2__1,				// D12
		P2__2, 				// D13
		P10__0, 				// A0
		P10__1,				// A1
		P10__2,				// A2
		P10__3,				// A3
		P10__4,				// A4
		P10__5,				// A5
		P10__6,				// A6
		P10__7,				// A7
};


// Analog (Ax) pin number array based on PinName digitalPin[]
const uint32_t analogInputPin[] = {
  14,  // A0,  P10_0
  15,  // A1,  P10_1
  16,  // A2,  P10_2
  17,  // A3,  P10_3
  18,  // A4,  P10_4
  19,  // A5,  P10_5
  20,  // A6,  P10_
  21,  // A7,  PA7
};


#define SYS_TICK_INTERVAL_1mS	(32768/1000)-1
// variable for sys_tick
uint32_t tick 			= 0;
uint32_t systick_wrap 	= 0;
uint32_t getcount_flag 	= 0;

static void systick_isr(void)	{ systick_wrap++; }

uint32_t millis()  { return systick_wrap; }
void initSystick(void){
		Cy_SysTick_Init(CY_SYSTICK_CLOCK_SOURCE_CLK_LF, SYS_TICK_INTERVAL_1mS);
		Cy_SysTick_SetCallback(0,systick_isr);
}


void pinMode(uint32_t pin, uint32_t mode)
{
	uint32_t driveMode = CY_GPIO_DM_ANALOG;
	GPIO_PRT_Type* portBase = Cy_GPIO_PortToAddr(pinMap_GET_PORT(digitalPin[pin]));
	uint32_t pinNum = pinMap_GET_PIN(digitalPin[pin]);
	switch(mode)
	{
		case (uint8_t) INPUT:
					break;
		case (uint8_t) INPUT_PULLUP:
					driveMode = CY_GPIO_DM_PULLUP;
					break;
		case (uint8_t) INPUT_PULLDOWN:
					driveMode = CY_GPIO_DM_PULLDOWN;
					break;
		case (uint8_t) OUTPUT:
					driveMode = CY_GPIO_DM_STRONG;
					break;
		default :
					driveMode = CY_GPIO_DM_ANALOG ;
					break;
	};
	uint32_t stat_critical = Cy_SysLib_EnterCriticalSection();
	if ( mode == (uint32_t) CY_GPIO_DM_PULLUP )
		Cy_GPIO_Pin_FastInit(portBase, pinNum, driveMode, 1u, 0u);
	else
		Cy_GPIO_Pin_FastInit(portBase, pinNum, driveMode, 0u, 0u);
	Cy_SysLib_ExitCriticalSection(stat_critical);
}

void digitalWrite(uint32_t pin, uint32_t val)
{
	GPIO_PRT_Type* portBase = Cy_GPIO_PortToAddr(pinMap_GET_PORT(digitalPin[pin]));
	uint32_t pinNum = pinMap_GET_PIN(digitalPin[pin]);
	Cy_GPIO_Write(portBase, pinNum, val);
}

uint32_t digitalRead(uint32_t pin)
{
	GPIO_PRT_Type* portBase = Cy_GPIO_PortToAddr(pinMap_GET_PORT(digitalPin[pin]));
	uint32_t pinNum = pinMap_GET_PIN(digitalPin[pin]);
	return Cy_GPIO_Read(portBase, pinNum);
}

void digitalToggle(uint32_t pin)
{
	GPIO_PRT_Type* portBase = Cy_GPIO_PortToAddr(pinMap_GET_PORT(digitalPin[pin]));
	uint32_t pinNum = pinMap_GET_PIN(digitalPin[pin]);
	Cy_GPIO_Inv(portBase, pinNum);
}

arduino_gpio.h

C Header File
Library header file
/*
 * arduino_gpio.h
 *
 *  Created on: 15 Agt 2023
 *      Author: DwiSetyabudi
 */

#ifndef ARDUINO_GPIO_H_
#define ARDUINO_GPIO_H_

#include "stdio.h"
#include "stdlib.h"

#include "cyhal.h"
#include "cybsp.h"
#include "cy_pdl.h"
#include "cy_gpio.h"
#include "cy_syslib.h"

#include "cyhal_hw_resources.h"

#define LOW		0u
#define HIGH	1u

#define pinMap_GET_GPIO(port, pin)   ((((uint8_t)(port)) << 3U) + ((uint8_t)(pin)))
#define pinMap_GET_PIN(pin)          ((uint8_t)(((uint8_t)pin) & 0x07U))
#define pinMap_GET_PORT(pin)         ((uint8_t)(((uint8_t)pin) >> 3U))


typedef struct pinMap_t{
	GPIO_PRT_Type* port;
	uint32_t pinNum;
}pinMap;

typedef enum{
	INPUT 			= 0,
	INPUT_PULLUP	= 1,
	INPUT_PULLDOWN  = 2,
	OUTPUT			= 3,
}PinMode;

typedef enum {
    NC_ = 0xFF, //!< No Connect/Invalid Pin

	P0__0 = pinMap_GET_GPIO(CYHAL_PORT_0, 0), //!< Port 0 Pin 0
	P0__1 = pinMap_GET_GPIO(CYHAL_PORT_0, 1), //!< Port 0 Pin 1
	P0__2 = pinMap_GET_GPIO(CYHAL_PORT_0, 2), //!< Port 0 Pin 2
	P0__3 = pinMap_GET_GPIO(CYHAL_PORT_0, 3), //!< Port 0 Pin 3
	P0__4 = pinMap_GET_GPIO(CYHAL_PORT_0, 4), //!< Port 0 Pin 4
	P0__5 = pinMap_GET_GPIO(CYHAL_PORT_0, 5), //!< Port 0 Pin 5

	P2__0 = pinMap_GET_GPIO(CYHAL_PORT_2, 0), //!< Port 2 Pin 0
	P2__1 = pinMap_GET_GPIO(CYHAL_PORT_2, 1), //!< Port 2 Pin 1
	P2__2 = pinMap_GET_GPIO(CYHAL_PORT_2, 2), //!< Port 2 Pin 2
	P2__3 = pinMap_GET_GPIO(CYHAL_PORT_2, 3), //!< Port 2 Pin 3
	P2__4 = pinMap_GET_GPIO(CYHAL_PORT_2, 4), //!< Port 2 Pin 4
	P2__5 = pinMap_GET_GPIO(CYHAL_PORT_2, 5), //!< Port 2 Pin 5
	P2__6 = pinMap_GET_GPIO(CYHAL_PORT_2, 6), //!< Port 2 Pin 6
	P2__7 = pinMap_GET_GPIO(CYHAL_PORT_2, 7), //!< Port 2 Pin 7

	P3__0 = pinMap_GET_GPIO(CYHAL_PORT_3, 0), //!< Port 3 Pin 0
	P3__1 = pinMap_GET_GPIO(CYHAL_PORT_3, 1), //!< Port 3 Pin 1

	P5__0 = pinMap_GET_GPIO(CYHAL_PORT_5, 0), //!< Port 5 Pin 0
	P5__1 = pinMap_GET_GPIO(CYHAL_PORT_5, 1), //!< Port 5 Pin 1
	P5__6 = pinMap_GET_GPIO(CYHAL_PORT_5, 6), //!< Port 5 Pin 6
	P5__7 = pinMap_GET_GPIO(CYHAL_PORT_5, 7), //!< Port 5 Pin 7

	P6__2 = pinMap_GET_GPIO(CYHAL_PORT_6, 2), //!< Port 6 Pin 2
	P6__3 = pinMap_GET_GPIO(CYHAL_PORT_6, 3), //!< Port 6 Pin 3
	P6__4 = pinMap_GET_GPIO(CYHAL_PORT_6, 4), //!< Port 6 Pin 4
	P6__5 = pinMap_GET_GPIO(CYHAL_PORT_6, 5), //!< Port 6 Pin 5
	P6__6 = pinMap_GET_GPIO(CYHAL_PORT_6, 6), //!< Port 6 Pin 6
	P6__7 = pinMap_GET_GPIO(CYHAL_PORT_6, 7), //!< Port 6 Pin 7

	P7__0 = pinMap_GET_GPIO(CYHAL_PORT_7, 0), //!< Port 7 Pin 0
	P7__1 = pinMap_GET_GPIO(CYHAL_PORT_7, 1), //!< Port 7 Pin 1
	P7__2 = pinMap_GET_GPIO(CYHAL_PORT_7, 2), //!< Port 7 Pin 2
	P7__3 = pinMap_GET_GPIO(CYHAL_PORT_7, 3), //!< Port 7 Pin 3
	P7__7 = pinMap_GET_GPIO(CYHAL_PORT_7, 7), //!< Port 7 Pin 7

	P8__0 = pinMap_GET_GPIO(CYHAL_PORT_8, 0), //!< Port 8 Pin 0
	P8__1 = pinMap_GET_GPIO(CYHAL_PORT_8, 1), //!< Port 8 Pin 1

	P9__0 = pinMap_GET_GPIO(CYHAL_PORT_9, 0), //!< Port 9 Pin 0
	P9__1 = pinMap_GET_GPIO(CYHAL_PORT_9, 1), //!< Port 9 Pin 1
	P9__2 = pinMap_GET_GPIO(CYHAL_PORT_9, 2), //!< Port 9 Pin 2
	P9__3 = pinMap_GET_GPIO(CYHAL_PORT_9, 3), //!< Port 9 Pin 3

	P10__0 = pinMap_GET_GPIO(CYHAL_PORT_10, 0), //!< Port 10 Pin 0
	P10__1 = pinMap_GET_GPIO(CYHAL_PORT_10, 1), //!< Port 10 Pin 1
	P10__2 = pinMap_GET_GPIO(CYHAL_PORT_10, 2), //!< Port 10 Pin 2
	P10__3 = pinMap_GET_GPIO(CYHAL_PORT_10, 3), //!< Port 10 Pin 3
	P10__4 = pinMap_GET_GPIO(CYHAL_PORT_10, 4), //!< Port 10 Pin 4
	P10__5 = pinMap_GET_GPIO(CYHAL_PORT_10, 5), //!< Port 10 Pin 5
	P10__6 = pinMap_GET_GPIO(CYHAL_PORT_10, 6), //!< Port 10 Pin 6
	P10__7 = pinMap_GET_GPIO(CYHAL_PORT_10, 7), //!< Port 10 Pin 7

	P11__2 = pinMap_GET_GPIO(CYHAL_PORT_11, 2), //!< Port 11 Pin 2
	P11__3 = pinMap_GET_GPIO(CYHAL_PORT_11, 3), //!< Port 11 Pin 3
	P11__4 = pinMap_GET_GPIO(CYHAL_PORT_11, 4), //!< Port 11 Pin 4
	P11__5 = pinMap_GET_GPIO(CYHAL_PORT_11, 5), //!< Port 11 Pin 5
	P11__6 = pinMap_GET_GPIO(CYHAL_PORT_11, 6), //!< Port 11 Pin 6
	P11__7 = pinMap_GET_GPIO(CYHAL_PORT_11, 7), //!< Port 11 Pin 7

	P12__6 = pinMap_GET_GPIO(CYHAL_PORT_12, 6), //!< Port 12 Pin 6
	P12__7 = pinMap_GET_GPIO(CYHAL_PORT_12, 7), //!< Port 12 Pin 7

	USBDP_ = pinMap_GET_GPIO(CYHAL_PORT_14, 0), //!< Port 14 Pin 0
	USBDM_ = pinMap_GET_GPIO(CYHAL_PORT_14, 1), //!< Port 14 Pin 1
} PinName;


static inline void delay(uint16_t t)			 {Cy_SysLib_Delay(t);}

static inline void delayMicroseconds(uint16_t t) { Cy_SysLib_DelayUs(t); }

uint32_t millis();
void initSystick(void);

void pinMode(uint32_t pin, uint32_t mode);
void digitalWrite(uint32_t pin, uint32_t val);
uint32_t digitalRead(uint32_t pin);
void digitalToggle(uint32_t pin);

extern void setup(void);
extern void loop(void);


#endif /* ARDUINO_GPIO_H_ */

main.c

C/C++
Test main -- used to test the library.
/*******************************************************************************
* File Name:   main.c
*
* Description:
*
********************************************************************************/

#include "arduino_gpio.h"

int main(void)
{
    cy_rslt_t result;
#if defined (CY_DEVICE_SECURE)
    cyhal_wdt_t wdt_obj;

    /* Clear watchdog timer so that it doesn't trigger a reset */
    result = cyhal_wdt_init(&wdt_obj, cyhal_wdt_get_max_timeout_ms());
    CY_ASSERT(CY_RSLT_SUCCESS == result);
    cyhal_wdt_free(&wdt_obj);
#endif

    /* Initialize the device and board peripherals */
    result = cybsp_init();

    /* Board init failed. Stop program execution */
    if (result != CY_RSLT_SUCCESS)
    {
        CY_ASSERT(0);
    }

    /* Enable global interrupts */
    __enable_irq();
    initSystick();
    setup();
    for (;;)
    {
    	loop();
    }
}

int pin = 7;
//int pin = 3;  // does not work on PSoC 62S4????
//int pin = 6;
//int pin = 5;
//int pin = 4;
//int pin = 2;
//int pin = 1;
//int pin = 0;
//int pin = 12;   //USER LED


void setup()
{
	pinMode(pin,OUTPUT);
	digitalWrite(pin, HIGH);
}

uint32_t time_millis = 0;
void loop()
{
	if(millis() - time_millis >= 1000u)
	{
		digitalToggle(pin);
		time_millis = millis();
	}
}

Credits

skruglewicz

skruglewicz

24 projects • 10 followers
I am now a retired Senior Software Engineer with a Bachelor’s of Science Degree in Computer Science from Boston University.

Comments