Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .cproject
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
</tool>
</toolChain>
</folderInfo>
<sourceEntries>
<entry excluding="peripherals/i2c.cpp" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
</sourceEntries>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
Expand All @@ -46,4 +49,5 @@
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="refreshScope"/>
</cproject>
2 changes: 1 addition & 1 deletion Drivers/Custom/MCP980x/MCP980x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "stm32l1xx.h"
#include "i2c.h"
#include "MCP980x\MCP980x.h"
#include "MCP980x.h"

/*---------------------------------------------------------------------------------------------------------------------+
| global functions
Expand Down
131 changes: 131 additions & 0 deletions Drivers/Custom/MCP980x/MCP980x_drv_by_PG.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* MCP980x_drv_by_PG.cpp
*
* Created on: 3 mar 2015
* Author: pgielmuda
*/

#include "error.h"
#include "semphr.h"
#include "MCP980x_drv_by_PG.h"
#include "i2c_by_PG.h"


static xSemaphoreHandle _drvMCPSemaphore[8];


enum Error initMCP980x(uint8_t addr)
{
enum Error error=i2cInitialize(); //initialize I2C


vSemaphoreCreateBinary(_drvMCPSemaphore[USR_ADDR(addr)]);

if (_drvMCPSemaphore[USR_ADDR(addr)] == NULL) // semaphore not created?
return ERROR_FreeRTOS_errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;// return with error

//check if there is a connection to MCP with this address
char buff=CONF_REG;
//read from CONF register
error = i2cWriteToSlave(addr, &buff, 1, portMAX_DELAY );
RET_ERROR( error );
buff=0xff;
error = i2cReadFromSlave(addr, &buff, 1,portMAX_DELAY );
if(buff&0x80)//only 7th bit is certainly not zero
return ERROR_MCP;

return error;
}


enum Error readTemperature(float* pvTemp, uint8_t addr)
{
//taking semaphore
portBASE_TYPE ret=xSemaphoreTake(_drvMCPSemaphore[USR_ADDR(addr)], portMAX_DELAY);
enum Error error = errorConvert_portBASE_TYPE(ret);
RET_ERROR( error );

//checking one-shot mode
char confBuff[2]={CONF_REG,0x00};
error = i2cWriteToSlave(addr,confBuff,1,portMAX_DELAY);
RET_ERROR( error );
error = i2cReadFromSlave(addr,(confBuff+1),1,portMAX_DELAY);
RET_ERROR( error );


if( confBuff[1] & 0x01){//one-shot mode it is
confBuff[1]|=0x80;
error = i2cWriteToSlave(addr,confBuff,2,portMAX_DELAY);//starting conversion
RET_ERROR( error );
while(confBuff[1] & 0x80){//waiting for a end of conversion
error = i2cReadFromSlave(addr,(confBuff+1),1,portMAX_DELAY);
RET_ERROR( error );
}
}

//now we can read a tempareature
char tempBuff[2]={TEMP_REG,0x00};
error = i2cWriteToSlave(addr,tempBuff,1,portMAX_DELAY);
RET_ERROR( error );
error = i2cReadFromSlave(addr,tempBuff,2,portMAX_DELAY);
RET_ERROR( error );

//releasing semaphore
xSemaphoreGive(_drvMCPSemaphore[USR_ADDR(addr)]);
//converting temperature to float
*pvTemp=(float)tempBuff[0]+(float)tempBuff[1]/256;
return ERROR_NONE;

}


enum Error readConfRegister(uint8_t* pvConfRegValue, uint8_t addr)
{
//taking semaphore
portBASE_TYPE ret=xSemaphoreTake(_drvMCPSemaphore[USR_ADDR(addr)], portMAX_DELAY);
enum Error error = errorConvert_portBASE_TYPE(ret);
RET_ERROR( error );
//reading conf register
char buff=CONF_REG;
error = i2cWriteToSlave(addr,&buff,1,portMAX_DELAY);
RET_ERROR( error );
error = i2cReadFromSlave(addr,&buff,1,portMAX_DELAY);
RET_ERROR( error );
//releasing semaphore
xSemaphoreGive(_drvMCPSemaphore[USR_ADDR(addr)]);
//returning value
*pvConfRegValue=buff;
return ERROR_NONE;


}

enum Error setConfRegister(uint8_t confRegValue, uint8_t addr)
{
//taking semaphore
portBASE_TYPE ret=xSemaphoreTake(_drvMCPSemaphore[USR_ADDR(addr)], portMAX_DELAY);
enum Error error = errorConvert_portBASE_TYPE(ret);
RET_ERROR( error );

//setting conf register
char buff[2]={CONF_REG,confRegValue};
error = i2cWriteToSlave(addr,buff,2,portMAX_DELAY);
RET_ERROR( error );

//reading conf register
buff[1]=0xff;
error = i2cReadFromSlave(addr,(buff+1),1,portMAX_DELAY);
RET_ERROR( error );

//checking if everything is ok
if(buff[1]!=confRegValue)
return ERROR_MCP;

//releasing semaphore
xSemaphoreGive(_drvMCPSemaphore[USR_ADDR(addr)]);

return ERROR_NONE;


}

58 changes: 58 additions & 0 deletions Drivers/Custom/MCP980x/MCP980x_drv_by_PG.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* MCP980x_drv.h
*
* Created on: 1 mar 2015
* Author: pgielmuda
*/

#ifndef PERIPHERALS_MCP980X_DRV_H_
#define PERIPHERALS_MCP980X_DRV_H_

#define TEMP_REG 0x00
#define CONF_REG 0x01
#define HYST_REG 0x02
#define LIMIT_REG 0x03
#define RET_ERROR( error ) if(error != ERROR_NONE) return error

#define MCP_MSB_ADDR 0x48
#define USR_ADDR( addr ) addr & 0x03


/** \brief Initialize MCP980x in continuous conversion mode
* Initialize MCP980x in continuous conversion mode
* \param[in] addr -I2C address of MCP980x
* \return ERROR_NONE on success, otherwise an error code defined in the file error.h
*/
enum Error initMCP980x(uint8_t addr);

/** \brief Read temperature from MCP980x
* Read last converted temperature or make a single measurement
* \param[out] pvTemp - pointer to a float where temperature will be written
* \param[in] addr -I2C address of MCP980x
* \return ERROR_NONE on success, otherwise an error code defined in the file error.h
*/
enum Error readTemperature(float* pvTemp, uint8_t addr);

/** \brief Read conf register from MCP980x
* Read conf register from MCP980x
* \param[out] pvConfRegVAlue - pointer to a uint_8 where conf register value will be written
* \param[in] addr -I2C address of MCP980x
* \return ERROR_NONE on success, otherwise an error code defined in the file error.h
*/
enum Error readConfRegister(uint8_t* pvConfRegValue, uint8_t addr);

/** Set conf register to MCP980x
* \param[in] confRegVAlue - uint_8 value to write into conf register
* (all bits of configuration register are descibed in datasheet)
* \param[in] addr -I2C address of MCP980x
* \return ERROR_NONE on success, otherwise an error code defined in the file error.h
*/
enum Error setConfRegister(uint8_t confRegValue, uint8_t addr);
enum Error readHyst(uint16_t* pvHystValue, uint8_t addr);
enum Error setHyst(uint16_t hystValue, uint8_t addr);





#endif /* PERIPHERALS_MCP980X_DRV_H_ */
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ AS_DEFS =
# folders with source files, current folder is always included)
SRCS_DIRS = configuration hdr Inc FatFS peripherals Drivers/ST/STM32_USB_Device_Library/Class/CDC Drivers/ST/STM32_USB_Device_Library/Core \
Drivers Drivers/CMSIS Drivers/CMSIS/Device/ST/STM32L1xx Drivers/CMSIS/Include Drivers/STM32L1xx_HAL_Driver \
FreeRTOS FreeRTOS/portable/GCC/ARM_CM3 FreeRTOS/portable/MemMang
FreeRTOS FreeRTOS/portable/GCC/ARM_CM3 FreeRTOS/portable/MemMang Drivers/Custom/MCP980x

# include directories (absolute or relative paths to additional folders with
# headers, current folder is always included)
INC_DIRS = configuration hdr Inc FatFS peripherals Drivers/ST/STM32_USB_Device_Library/Class/CDC Drivers/ST/STM32_USB_Device_Library/Core \
Drivers Drivers/CMSIS Drivers/CMSIS/Device/ST/STM32L1xx Drivers/CMSIS/Include Drivers/STM32L1xx_HAL_Driver \
FreeRTOS/include FreeRTOS FreeRTOS/portable/GCC/ARM_CM3 FreeRTOS/portable/MemMang
FreeRTOS/include FreeRTOS FreeRTOS/portable/GCC/ARM_CM3 FreeRTOS/portable/MemMang Drivers/Custom/MCP980x

# library directories (absolute or relative paths to additional folders with
# libraries)
Expand Down
4 changes: 4 additions & 0 deletions configuration/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define USART_RX_TASK_PRIORITY (tskIDLE_PRIORITY + 2)
#define USART_RX_STACK_SIZE 256

// I2C task
#define I2C_TASK_PRIORITY (tskIDLE_PRIORITY + 2)
#define I2C_TASK_STACK_SIZE 128

#define CONTEXT_SWITCH() vTaskDelay(0);
/*---------------------------------------------------------------------------------------------------------------------+
| Runtime stats configuration
Expand Down
2 changes: 2 additions & 0 deletions configuration/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@

#define I2C_FREQUENCY 100000

#define I2C_RXTX_QUEUE_LENGTH 16

/*---------------------------------------------------------------------------------------------------------------------+
| commands
+---------------------------------------------------------------------------------------------------------------------*/
Expand Down
1 change: 1 addition & 0 deletions configuration/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
enum Error
{
// --- negative values ---
ERROR_MCP = -127,
ERROR_FreeRTOS_errSCHEDULER_FAIL, // Scheduler leak

// FreeRTOS errors from projdefs.h
Expand Down
57 changes: 57 additions & 0 deletions configuration/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include "st_rcc.h"
#include "pwr.h"
#include "stm32l1xx_gpio.h"
#include "i2c_by_PG.h"
#include "MCP980x_drv_by_PG.h"

/* Private variables ---------------------------------------------------------*/

Expand All @@ -70,6 +72,10 @@ static enum Error _runtimestatsHandler(const char **arguments_array, uint32_t ar
static enum Error _tasklistHandler(const char **arguments_array, uint32_t arguments_count, char *output_buffer,
size_t output_buffer_length);

void myTask();
void initializeMyTask();
void ButtonIRQInit();

/*---------------------------------------------------------------------------------------------------------------------+
| local variables
+---------------------------------------------------------------------------------------------------------------------*/
Expand Down Expand Up @@ -110,10 +116,15 @@ int main(void)
{
/* Configure the system clock and pll*/
_sysInit();
//ButtonIRQInit();


gpioInitialize();
gpioConfigurePin(GPIOA, BUTTON_PIN, BUTTON_CONFIGURATION);

enum Error error = usartInitialize();
//??
//i2cInitialize();

FRESULT fresult = f_mount(0, &_fileSystem); // try mounting the filesystem on SD card
ASSERT("f_mount()", fresult == FR_OK);
Expand All @@ -124,8 +135,11 @@ int main(void)
error = _initializePowerSaveTask();
ASSERT("_initializeHeartbeatTask()", error == ERROR_NONE);

initializeMyTask();
gpioInitialize();



/* Special delay for debugging because after scheduler start
* it may be hard to catch core in run mode to connect to debugger
*/
Expand Down Expand Up @@ -261,6 +275,47 @@ static enum Error _dirHandler(const char **arguments_array, uint32_t arguments_c
return error;
}



/**
* Moja własna funkcja, sterowanie drugim ledem
*/
void myTask()
{

float temp;
initMCP980x(MCP_MSB_ADDR);
setConfRegister(0x61,MCP_MSB_ADDR);
for(;;)
{
int bv=1;
gpioInitialize();
gpioConfigurePin(LED_GPIO, LED_pin, GPIO_OUT_PP_2MHz);
gpioConfigurePin(GPIOA, BUTTON_PIN, BUTTON_CONFIGURATION);

bv=(int)(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0 ));
//odczytanie pozycji przycisku
if(bv){
setConfRegister(0x01, MCP_MSB_ADDR);
LED_bb |=0x01;
readTemperature(&temp, 0x48);
} else
LED_bb &=~0x01;
//włączenie diody jeśli przycisk jest włączony
vTaskDelay(100/portTICK_RATE_MS);
}
}

void initializeMyTask()
{
xTaskHandle xHandleMyTask;

xTaskCreate(myTask, (signed char*)"myTask", HEARTBEAT_STACK_SIZE, NULL, HEARTBEAT_TASK_PRIORITY, &xHandleMyTask );


}


/**
* \brief Heartbeat task that simulate real system behavior
*
Expand All @@ -274,6 +329,7 @@ static void _heartbeatTask(void *parameters)
portTickType xLastHeartBeat;

xLastHeartBeat = xTaskGetTickCount();
float temp;

for(;;){
int a = 0,b = 0;
Expand All @@ -283,6 +339,7 @@ static void _heartbeatTask(void *parameters)
LED1_bb ^= 1;
for(int i=0; i<700000; i++) a = 2*b+1; //do some fake calculations
LED1_bb ^= 1;
readTemperature(&temp, 0x48);

vTaskDelay(100/portTICK_RATE_MS); //Then go sleep
}
Expand Down
Loading