stm-ledstrip : Driver and test routine for WS2811 RGB-LED
stm-ledstrip : Driver and test routine for WS2811 RGB-LED
#include "ws2812.h"
#include <stm32f4xx.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_tim.h>
#include <stm32f4xx_dma.h> static uint16_t PWM_Buffer[ PWM_BUFFER_SIZE ];
uint32_t frame_pos = ;
int incomplete_return = ; void Update_Buffer( uint16_t* buffer ); static void start_dma( void )
{
static DMA_InitTypeDef dma_init =
{ .DMA_BufferSize = PWM_BUFFER_SIZE, .DMA_Channel = DMA_Channel_5, .DMA_DIR =
DMA_DIR_MemoryToPeripheral, .DMA_FIFOMode = DMA_FIFOMode_Disable,
.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull, .DMA_Memory0BaseAddr =
(uint32_t) PWM_Buffer, .DMA_MemoryBurst = DMA_MemoryBurst_Single,
.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord, .DMA_MemoryInc =
DMA_MemoryInc_Enable, .DMA_Mode = DMA_Mode_Circular,
.DMA_PeripheralBaseAddr = (uint32_t) &TIM3->CCR4, .DMA_PeripheralBurst =
DMA_PeripheralBurst_Single, .DMA_PeripheralDataSize =
DMA_PeripheralDataSize_HalfWord, .DMA_PeripheralInc =
DMA_PeripheralInc_Disable, .DMA_Priority = DMA_Priority_Medium }; DMA_Init( DMA1_Stream2, &dma_init );
DMA_Cmd( DMA1_Stream2, ENABLE );
TIM_DMACmd( TIM3, TIM_DMA_CC4, ENABLE );
} static void init_buffers( void )
{
for ( int i = ; i < PWM_BUFFER_SIZE; i++ )
{
PWM_Buffer[ i ] = ;
}
for ( int i = ; i < FRAMEBUFFER_SIZE; i++ )
{
ws2812_framebuffer[ i ].red = ;
ws2812_framebuffer[ i ].green = ;
ws2812_framebuffer[ i ].blue = ;
}
} void ws2812_init( )
{
init_buffers( ); //InitStructures...
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
TIM_OCInitTypeDef TIM_OC_InitStructure;
NVIC_InitTypeDef nvic_init; //Clock für GPIO setzen
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE ); //Clock für TIM4 setzen
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE ); //GPIO_PIN konfigurieren GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init( GPIOC, &GPIO_InitStructure ); //GPIO Alternate function verbinden
GPIO_PinAFConfig( GPIOC, GPIO_PinSource9, GPIO_AF_TIM3 ); TIM_TimeBase_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBase_InitStructure.TIM_Period = ;
TIM_TimeBase_InitStructure.TIM_Prescaler = ;
TIM_TimeBaseInit( TIM3, &TIM_TimeBase_InitStructure ); TIM_OC_InitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OC_InitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OC_InitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
TIM_OC_InitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC_InitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OC_InitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC_InitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OC_InitStructure.TIM_Pulse = ;
TIM_OC4Init( TIM3, &TIM_OC_InitStructure ); TIM_CtrlPWMOutputs( TIM3, ENABLE ); TIM_OC4PreloadConfig( TIM3, TIM_OCPreload_Enable );
TIM_ARRPreloadConfig( TIM3, ENABLE ); TIM_CCxCmd( TIM3, TIM_Channel_4, TIM_CCx_Enable );
TIM_Cmd( TIM3, ENABLE ); // DMA
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_DMA1, ENABLE );
TIM_DMACmd( TIM3, TIM_DMA_CC4, ENABLE );
DMA_ITConfig( DMA1_Stream2, DMA_IT_HT, ENABLE );
DMA_ITConfig( DMA1_Stream2, DMA_IT_TC, ENABLE ); start_dma( ); // NVIC for DMA
nvic_init.NVIC_IRQChannel = DMA1_Stream2_IRQn;
nvic_init.NVIC_IRQChannelPreemptionPriority = ;
nvic_init.NVIC_IRQChannelSubPriority = ;
nvic_init.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init( &nvic_init );
} // writes the pwm values of one byte into the array which will be used by the dma
static inline void color2pwm( uint16_t ** const dest, const uint8_t color )
{
uint8_t mask = 0x80; do
{
if ( color & mask )
{
* *dest = ;
}
else
{
* *dest = ;
}
*dest += ;
mask >>= ;
}while ( mask != );
} void Update_Buffer( uint16_t* buffer )
{
struct led *framebufferp;
uint32_t i, j;
uint16_t * bufp; for ( i = ; i < ( PWM_BUFFER_SIZE / ) / ; i++ )
{
if ( incomplete_return )
{
incomplete_return = ;
for ( j = ; j < ; j++ )
{
buffer[ i * + j ] = ;
} }
else
{
if ( frame_pos == FRAMEBUFFER_SIZE )
{
incomplete_return = ;
frame_pos = ; for ( j = ; j < ; j++ )
{
buffer[ i * + j ] = ;
}
}
else
{
framebufferp = &ws2812_framebuffer[ frame_pos++ ];
bufp = buffer + ( i * ); // edit here to change order of colors in "ws2812_framebuffer" (0x00RRGGBB, 0x00GGBBRR, etc)
// the chip needs G R B
color2pwm( &bufp, framebufferp->green ); // green
color2pwm( &bufp, framebufferp->red ); // red
color2pwm( &bufp, framebufferp->blue ); // blue
}
}
}
} void DMA1_Stream2_IRQHandler( void )
{
// Half-Transfer completed
if ( DMA_GetITStatus( DMA1_Stream2, DMA_IT_HTIF2 ) )
{
DMA_ClearITPendingBit( DMA1_Stream2, DMA_IT_HTIF2 );
Update_Buffer( PWM_Buffer );
} // Transfer completed
if ( DMA_GetITStatus( DMA1_Stream2, DMA_IT_TCIF2 ) )
{
DMA_ClearITPendingBit( DMA1_Stream2, DMA_IT_TCIF2 );
Update_Buffer( PWM_Buffer + ( PWM_BUFFER_SIZE / ) );
} }
stm-ledstrip : Driver and test routine for WS2811 RGB-LED的更多相关文章
- 0xWS2812 STM32 driver for WS2812(B) RGB LEDs
0xWS2812 STM32 driver for WS2812(B) RGB LEDs 0xWS2812 pronounced "hex-WS2812" This code ai ...
- kernel/printk.c
/* * linux/kernel/printk.c * * Copyright (C) 1991, 1992 Linus Torvalds * * Modified to make sys_s ...
- 关于IoCallDriver
通常我们所知IoCallDriver是把irp传递给下一层设备,传递到底是什么意思呢?IoCallDriver中实际调用了IopfCallDriver,其代码如下:NTSTATUSFORCEINLIN ...
- IRP小结 0x01 IRP & IO_STACK_LOCATION(结合WRK理解)
写博客整理记录一下IRP相关的知识点,加深一下印象. 所有的I/O请求都是以IRP的形式提交的.当I/O管理器为了响应某个线程调用的的I/O API的时候,就会构造一个IRP,用于在I/O系统处理这个 ...
- tiny4412 串口驱动分析七 --- log打印的几个阶段之内核启动阶段(earlyprintk)
作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...
- linux内核中mtd架构分析
一. 引言 MTD(memory technology device内存技术设备)是用于访问memory设备(RAM.ROM.flash)的Linux的子系统.MTD的主要目的是为了使新的memory ...
- Device Drivers
Types of Device Drivers Windows可能会有User-mode的驱动,但是我们只关注Kernel-Mode的驱动. WDM Drivers WDM是一种驱动模型,是比较常用的 ...
- linux内核更新前后配置文件的比较
说明:这里先给出一个比较的结果,作为记录,后续会给出内核配置差异的详细解释. [root@xiaolyu linux-4.7.2]# diff .config .config_bak 3c3< ...
- linux设备驱动归纳总结(九):1.platform总线的设备和驱动【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-111745.html linux设备驱动归纳总结(九):1.platform总线的设备和驱动 xxxx ...
随机推荐
- [译]使用chage来管理Linux密码过期时间的七个例子
本文译自 7 Examples to Manage Linux Password Expiration and Aging Using chage 本文主要介绍命令chage的使用,译文会对原文内容会 ...
- 【并行计算】用MPI进行分布式内存编程(一)
通过上一篇关于并行计算准备部分的介绍,我们知道MPI(Message-Passing-Interface 消息传递接口)实现并行是进程级别的,通过通信在进程之间进行消息传递.MPI并不是一种新的开发语 ...
- 003_cd pushd popd三个命令的区别
一. It depends. In zsh you can configure cd to push the old directory on the directory stack automati ...
- pandas删除dataframe列
data2 = data.drop(data.columns[0,1,3,4,6,8,10], 1)
- stylus项目知识点
1.在项目中,引入.sty文件的时候,用来下面方式 @import "~common/stylus/variable.styl" ~ 是stylus的写法,参考https://gi ...
- Intellij Idea 使用时总是打开上次的项目
Appearance&Behaviour-->>System Settings-->>Startup&Shutdown-->>Reopen last ...
- Redux 和 ngrx 创建更佳的 Angular 2
Redux 和 ngrx 创建更佳的 Angular 2 翻译:使用 Redux 和 ngrx 创建更佳的 Angular 2 原文地址:http://onehungrymind.com/build- ...
- myBatsi调用存储过程
1.结构 2.准备数据 建表和插入数据 CREATE TABLE p_user( id INT PRIMARY KEY AUTO_INCREMENT, name ), sex ) ); INSERT ...
- Ubuntu系统无法获得锁/var/lib/dpkg/lock - open (11: 资源暂时不可用)的解决方案
Ubuntu系统无法获得锁/var/lib/dpkg/lock - open (11: 资源暂时不可用)的解决方案 问题 使用Ubuntu打开终端时,输入带有sudo apt-get 命令行是 ...
- 5969 [AK]刻录光盘
题目描述 Description • 在FJOI2010夏令营快要结束的时候,很多营员提出来要把整个夏令营期间的资料刻录成一张光盘给大家,以便大家回去后继续学习.组委会觉得这个主意不错!可是组委会一时 ...